blob: dbe9c8c4e0b09256f8d91ef93441ccefb78ccc4a [file] [log] [blame]
wangyouqiangce12f2b2024-04-23 17:57:09 +08001#if 1
2#include <fcntl.h>
3#include <stdint.h>
4#include <libubox/blobmsg_json.h>
5#include <libubus.h>
6
7#include "mbtk_power.h"
8/****************************DEFINE***************************************/
9#define MBTK_POWER_RESULT_FAIL -1
10#define MBTK_POWER_RESULT_SUCCESS 0
11
12
13#define MBTK_POWER_CLOSE_FAIL -1
14#define MBTK_POWER_CLOSE_SUCCESS 0
15
16/****************************DEFINE***************************************/
17
18/****************************VARIABLE***************************************/
19const struct blobmsg_policy mbtk_power_cb_policy1[] = {
20 [0] = {
21 .name = "event",
22 .type = BLOBMSG_TYPE_INT32,
23 },
24};
25/****************************VARIABLE***************************************/
26
27/****************************FUNC***************************************/
28static void mbtk_power_gnss_callback(struct ubus_request *req, int type, struct blob_attr *msg)
29{
30 UNUSED(type);
31
32 struct blob_attr *tb[1];
33 struct blob_attr *cur;
34 unsigned int event;
35 int *ubus_gnss_result = (int *)req->priv;
36 int rc;
37
38 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
39 rc = blobmsg_parse(mbtk_power_cb_policy1, 1, tb, blob_data(msg), blob_len(msg));
40 if (rc < 0)
41 {
42 LOGE("[MBTK_POWER] blobmsg_parse fail.");
43 //printf("blobmsg_parse fail\n");
44 *ubus_gnss_result = MBTK_POWER_CLOSE_FAIL;
45 return;
46 }
47
48 /*parse first parameter*/
49 cur = tb[0];
50 if (!cur)
51 {
52 LOGE("[MBTK_POWER] missing parameter.");
53 //printf("missing parameter\n");
54 *ubus_gnss_result = MBTK_POWER_CLOSE_FAIL;
55 return;
56 }
57
58 event = blobmsg_get_u32(cur);
59 LOGE("[MBTK_POWER] get event = [%d].", event);
60 //printf("get event = [%d].\n", event);
61
yq.wanga40216c2024-06-24 01:38:32 -070062#if defined(MBTK_PROJECT_T108)
yq.wang0ff0ca22024-06-29 02:38:27 -070063 if(event != 0)
yq.wanga40216c2024-06-24 01:38:32 -070064#else
wangyouqiangce12f2b2024-04-23 17:57:09 +080065 if(event != 7)
yq.wanga40216c2024-06-24 01:38:32 -070066#endif
wangyouqiangce12f2b2024-04-23 17:57:09 +080067 {
68 *ubus_gnss_result = MBTK_POWER_CLOSE_FAIL;
69 }
70
71 return ;
72}
73
74static int mbtk_power_ubus_uloop_init(struct ubus_context **ctx)
75{
76 int ret = -1;
77 if(ctx == NULL)
78 {
79 LOGE("[MBTK_POWER] ctx is NULL");
80 return MBTK_POWER_RESULT_FAIL;
81 }
82
83 ret = uloop_init();
84 if(ret != 0)
85 {
86 LOGE("[MBTK_POWER] uloop_init fail.ret = [%d]", ret);
87 return MBTK_POWER_RESULT_FAIL;
88 }
89
90 if(*ctx != NULL)
91 {
92 LOGE("[MBTK_POWER] mbtk_gnss_ctx not NULL.");
93 return MBTK_POWER_RESULT_FAIL;
94 }
95
96 *ctx = ubus_connect(NULL);
97 if( !(*ctx) )
98 {
99 LOGE("[MBTK_POWER] ubus_connect fail.");
100 return MBTK_POWER_RESULT_FAIL;
101 }
102
103 ubus_add_uloop(*ctx);
104
105 return MBTK_POWER_RESULT_SUCCESS;
106}
107
108static int mbtk_power_ubus_uloop_deinit(struct ubus_context *ctx)
109{
110 if(ctx != NULL)
111 {
112 ubus_free(ctx);
113 ctx = NULL;
114 }
115
116 uloop_done();
117
118 return MBTK_POWER_RESULT_SUCCESS;
119}
120
121static int mbtk_power_invoke_reply_data_cb(const char *service, const char *method, struct blob_attr *msg,
122 ubus_data_handler_t cb, void *cb_param, int timeout, struct ubus_context *ctx)
123{
124 int rc = -1;
125 uint32_t id;
126 struct ubus_request req;
127
128 /* Look up the target object id by the object path */
129 rc = ubus_lookup_id(ctx, service, &id);
130 if(rc)
131 {
132 LOGE("[MBTK_POWER] ubus_lookup_id fail.rc = [%d]", rc);
133 return MBTK_POWER_RESULT_FAIL;
134 }
135
136 rc = ubus_invoke(ctx, id, method, msg, cb, cb_param, timeout);
137 if(rc)
138 {
139 LOGE("[MBTK_POWER] ubus_invoke fail.rc = [%d]", rc);
140 return MBTK_POWER_RESULT_FAIL;
141 }
142 return MBTK_POWER_RESULT_SUCCESS;
143}
144
145
146static int mbtk_power_gnss_close(void)
147{
148 int ret = 0;
149 int ubus_gnss_result = MBTK_POWER_CLOSE_SUCCESS;
150 struct ubus_context *mbtk_power_ctx = NULL;
151 ret = mbtk_power_ubus_uloop_init(&mbtk_power_ctx);
152 if(ret < 0)
153 {
154 LOGE("[MBTK_POWER] mbtk_gnss_ubus_uloop_init() fail.");
155 return MBTK_POWER_CLOSE_FAIL;
156 }
157
158 struct blob_buf outBlob;
159 memset(&outBlob, 0, sizeof(outBlob));
160
161 blob_buf_init(&outBlob, 0);
162 blobmsg_add_u32(&outBlob, "gnss_init_param", 0);
yq.wanga40216c2024-06-24 01:38:32 -0700163#if defined(MBTK_PROJECT_T108)
164 ret = mbtk_power_invoke_reply_data_cb("mbtk_gnss", "gnss_deinit", outBlob.head,
165 (ubus_data_handler_t *)mbtk_power_gnss_callback, &ubus_gnss_result, 8000, mbtk_power_ctx);
166#else
wangyouqiangce12f2b2024-04-23 17:57:09 +0800167 ret = mbtk_power_invoke_reply_data_cb("gps", "gnss_deinit", outBlob.head,
168 (ubus_data_handler_t *)mbtk_power_gnss_callback, &ubus_gnss_result, 8000, mbtk_power_ctx);
yq.wanga40216c2024-06-24 01:38:32 -0700169#endif
wangyouqiangce12f2b2024-04-23 17:57:09 +0800170 blob_buf_free(&outBlob);
171
172 if (ret != 0)
173 {
174 LOGE("[MBTK_POWER] mbtk_invoke_reply_data_cb() fail.");
175 return MBTK_POWER_CLOSE_FAIL;
176 }
177
178 if(ubus_gnss_result != MBTK_POWER_CLOSE_SUCCESS)
179 {
180 return MBTK_POWER_CLOSE_FAIL;
181 }
182
183 mbtk_power_ubus_uloop_deinit(mbtk_power_ctx);
184 return MBTK_POWER_CLOSE_SUCCESS;
185}
186
187static int gpio_direct_set(char *value,int gpio)
188{
189 char buffer[50]= {0};
190 int file =-1;
191 int result =-1;
192
193 memset(buffer,0,50);
194 sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
195 file = open(buffer, O_WRONLY);
196 if(file == -1)
197 {
198 LOGE("[MBTK_POWER] Open gpio[%d] direct fail.", gpio);
199 return MBTK_POWER_RESULT_FAIL;
200 }
201
202 result = write(file,value,strlen(value));
203 if(result != strlen(value))
204 {
205 LOGE("[MBTK_POWER] Set gpio[%d] direct fail.", gpio);
206 close(file);
207 return MBTK_POWER_RESULT_FAIL;
208 }
209 close(file);
210
211 return MBTK_POWER_RESULT_SUCCESS;
212}
213
214static int gpio_value_set(int value, int gpio)
215{
216 char buffer[50]= {0};
217 int file =-1;
218 int result =-1;
219
220 memset(buffer,0,50);
221 sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio);
222 file = open(buffer,O_WRONLY);
223 if(file == -1)
224 {
225 LOGE("[MBTK_POWER] Open gpio[%d] value fail.", gpio);
226 return MBTK_POWER_RESULT_FAIL;
227 }
228 if(value == 0) {
229 result = write(file,"0",1);
230 } else {
231 result = write(file,"1",1);
232 }
233 if(result != 1)
234 {
235 LOGE("[MBTK_POWER] Set gpio[%d] value fail.", gpio);
236 close(file);
237 return MBTK_POWER_RESULT_FAIL;
238 }
239 close(file);
240
241 return MBTK_POWER_RESULT_SUCCESS;
242}
243
244//type:0 value 0
245//type:1 value 1
246//type:2 direction "out"
247//type:3 direction "in"
248
249static int one_gpio_export(int gpio, int type)
250{
251 int index=0;
252 int file=-1;
253 int file1=-1;
254 int result =-1;
255 char pin_index_buffer[5]= {0};
256 char buffer[50]= {0};
257 int ret =0;
258
259 file = open("/sys/class/gpio/export",O_WRONLY);
260 if(file == -1)
261 {
262 LOGE("[MBTK_POWER] Open gpio export file fail.");
263 return MBTK_POWER_RESULT_FAIL;
264 }
265
266 memset(buffer,0,50);
267 sprintf(buffer,"/sys/class/gpio/gpio%d", gpio);
268 file1 = open(buffer,O_RDONLY);
269 if(file1 != -1)
270 {
271 //file is created
272 close(file1);
273 }
274 else
275 { // create gpio
276 memset(pin_index_buffer,0,5);
277 sprintf(pin_index_buffer,"%d",gpio);
278 result = write(file,pin_index_buffer,strlen(pin_index_buffer));
279 if(result < 0)
280 {
281 LOGE("[MBTK_POWER] Gpio[%d] export fail.", gpio);
282 return MBTK_POWER_RESULT_FAIL;
283 }
284 }
285
286 close(file);
287
288
289 switch(type)
290 {
291 case 0 :
292 {
293 ret = gpio_value_set(0, gpio);
294 break;
295 }
296 case 1:
297 {
298 ret = gpio_value_set(1, gpio);
299 break;
300 }
301 case 2:
302 {
303 ret = gpio_direct_set("out", gpio);
304 break;
305 }
306 case 3:
307 {
308 ret = gpio_direct_set("in", gpio);
309 break;
310 }
311 default:
312 {
313 break;
314 }
315 }
316
317
318 return ret;
319}
320
321/****************************FUNC***************************************/
322
323/****************************API***************************************/
324#if defined(MBTK_PROJECT_T108)
325int mbtk_system_sleep(void)
326{
327 int ubus_ret = 0;
328
329 ubus_ret = mbtk_power_gnss_close();
330 if(ubus_ret < 0)
331 {
332 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
333 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
334 }
335
336 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
337 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
338
339 if(!access("/sys/power/autosleep", W_OK))
340 {
341 system("echo mem > /sys/power/autosleep");
342 }
343 else
344 {
345 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
346 //printf("/sys/power/autosleep can not write.");
347 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
348 }
349
350 return MBTK_POWER_RESULT_SUCCESS;
351}
352
353#elif defined(MBTK_PROJECT_L508_X6)
354int mbtk_system_sleep(void)
355{
356 int ubus_ret = 0;
357
358 ubus_ret = mbtk_power_gnss_close();
359 if(ubus_ret < 0)
360 {
361 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
362 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
363 }
364
365 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
366 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
367 //close Ldo6
368 system("i2cset -y -f 2 0x31 0x18 0x0f");
369 //GPS_WAKE_HOST to GPIO
370 system("hwacc w 0xd401e198 0x1040");
371 //HOST_WAKE_GPS to GPIO
372 //system("hwacc w 0xd401e0c0 0x1040");
373 //gpio34 to GPIO
374#if 1
375 system("hwacc w 0xd401e164 0x1040");
376 system("hwacc w 0xd401e166 0x1040");
377 system("hwacc w 0xd401e1b4 0x1040");
378
379 system("hwacc w 0xd401e2ec 0xa441");
380 system("hwacc w 0xd401e2f0 0xa441");
381 system("hwacc w 0xd401e2f4 0xa441");
382 system("hwacc w 0xd401e2f8 0xa441");
383 system("hwacc w 0xd401e2fc 0xa441");
384 system("hwacc w 0xd401e300 0xa441");
385#endif
386 one_gpio_export(117, 2);
387 one_gpio_export(117, 0);
388
389 one_gpio_export(119, 2);
390 one_gpio_export(119, 0);
391
392 one_gpio_export(127, 2);
393 one_gpio_export(127, 0);
394
395 one_gpio_export(33, 2);
396 one_gpio_export(33, 0);
397
398 one_gpio_export(34, 2);
399 one_gpio_export(34, 0);
400
401 one_gpio_export(54, 2);
402 one_gpio_export(54, 0);
403
404 one_gpio_export(21, 2);
405 one_gpio_export(21, 0);
406
407 one_gpio_export(118, 2);
408 one_gpio_export(118, 0);
409
410 if(!access("/sys/power/autosleep", W_OK))
411 {
412 system("echo mem > /sys/power/autosleep");
413 }
414 else
415 {
416 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
417 //printf("/sys/power/autosleep can not write.");
418 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
419 }
420
421 return MBTK_POWER_RESULT_SUCCESS;
422}
423
424#elif defined(MBTK_PROJECT_L509)
425int mbtk_system_sleep(void)
426{
427 int ubus_ret = 0;
428
429 ubus_ret = mbtk_power_gnss_close();
430 if(ubus_ret < 0)
431 {
432 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
433 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
434 }
435
436 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
437 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
438
439 one_gpio_export(120, 2);
440 one_gpio_export(120, 0);
441
442 one_gpio_export(21, 2);
443 one_gpio_export(21, 0);
444
445 one_gpio_export(118, 2);
446 one_gpio_export(118, 0);
447
448 if(!access("/sys/power/autosleep", W_OK))
449 {
450 system("echo mem > /sys/power/autosleep");
451 }
452 else
453 {
454 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
455 //printf("/sys/power/autosleep can not write.");
456 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
457 }
458
459 return MBTK_POWER_RESULT_SUCCESS;
460}
461
462#elif defined(MBTK_PROJECT_L508)
463int mbtk_system_sleep(void)
464{
465 int ubus_ret = 0;
466
467 ubus_ret = mbtk_power_gnss_close();
468 if(ubus_ret < 0)
469 {
470 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
471 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
472 }
473
474 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
475 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
476
477 one_gpio_export(119, 2);
478 one_gpio_export(119, 0);
479
480 one_gpio_export(21, 2);
481 one_gpio_export(21, 0);
482
483 one_gpio_export(118, 2);
484 one_gpio_export(118, 0);
485
486 if(!access("/sys/power/autosleep", W_OK))
487 {
488 system("echo mem > /sys/power/autosleep");
489 }
490 else
491 {
492 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
493 //printf("/sys/power/autosleep can not write.");
494 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
495 }
496
497 return MBTK_POWER_RESULT_SUCCESS;
498}
499
500#elif defined(MBTK_PROJECT_PN1803)
501int mbtk_system_sleep(void)
502{
503 int ubus_ret = 0;
504
505 ubus_ret = mbtk_power_gnss_close();
506 if(ubus_ret < 0)
507 {
508 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
509 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
510 }
511
512 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
513 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
514
515 one_gpio_export(119, 2);
516 one_gpio_export(119, 0);
517
518 one_gpio_export(21, 2);
519 one_gpio_export(21, 0);
520
521 one_gpio_export(118, 2);
522 one_gpio_export(118, 0);
523
524 if(!access("/sys/power/autosleep", W_OK))
525 {
526 system("echo mem > /sys/power/autosleep");
527 }
528 else
529 {
530 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
531 //printf("/sys/power/autosleep can not write.");
532 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
533 }
534
535 return MBTK_POWER_RESULT_SUCCESS;
536}
537
538#else
539int mbtk_system_sleep(void)
540{
541 return MBTK_POWER_RESULT_SUCCESS;
542}
543#endif
544/****************************API***************************************/
545#endif