blob: f2cc9f3de4cb951c69432e3f4ff318dc9a026aa2 [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>
liuyangcdf88992024-07-05 11:48:24 +08006#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <sys/socket.h>
10#include <sys/un.h>
11#include <netinet/in.h>
12#include <pthread.h>
13#include <sys/epoll.h>
wangyouqiangce12f2b2024-04-23 17:57:09 +080014
15#include "mbtk_power.h"
16/****************************DEFINE***************************************/
17#define MBTK_POWER_RESULT_FAIL -1
18#define MBTK_POWER_RESULT_SUCCESS 0
19
20
21#define MBTK_POWER_CLOSE_FAIL -1
22#define MBTK_POWER_CLOSE_SUCCESS 0
23
liuyangcdf88992024-07-05 11:48:24 +080024#define GNSS_SOCK_PATH "/tmp/mbtk_gnss_sock"
25
26static int sock_listen_fd = -1;
27
28typedef enum {
29 GNSS_CMD_INIT = 0,
30 GNSS_CMD_DEINIT,
31 GNSS_CMD_SETTING,
32 GNSS_CMD_DL
33} gnss_cmd_enum;
34
35
wangyouqiangce12f2b2024-04-23 17:57:09 +080036/****************************DEFINE***************************************/
37
38/****************************VARIABLE***************************************/
39const struct blobmsg_policy mbtk_power_cb_policy1[] = {
40 [0] = {
41 .name = "event",
42 .type = BLOBMSG_TYPE_INT32,
43 },
44};
45/****************************VARIABLE***************************************/
46
47/****************************FUNC***************************************/
48static void mbtk_power_gnss_callback(struct ubus_request *req, int type, struct blob_attr *msg)
49{
50 UNUSED(type);
51
52 struct blob_attr *tb[1];
53 struct blob_attr *cur;
54 unsigned int event;
55 int *ubus_gnss_result = (int *)req->priv;
56 int rc;
57
58 /*parsing blob to be accessed easily with tb array - parse "1" argument*/
59 rc = blobmsg_parse(mbtk_power_cb_policy1, 1, tb, blob_data(msg), blob_len(msg));
60 if (rc < 0)
61 {
62 LOGE("[MBTK_POWER] blobmsg_parse fail.");
63 //printf("blobmsg_parse fail\n");
64 *ubus_gnss_result = MBTK_POWER_CLOSE_FAIL;
65 return;
66 }
67
68 /*parse first parameter*/
69 cur = tb[0];
70 if (!cur)
71 {
72 LOGE("[MBTK_POWER] missing parameter.");
73 //printf("missing parameter\n");
74 *ubus_gnss_result = MBTK_POWER_CLOSE_FAIL;
75 return;
76 }
77
78 event = blobmsg_get_u32(cur);
79 LOGE("[MBTK_POWER] get event = [%d].", event);
80 //printf("get event = [%d].\n", event);
81
r.xiaod888d2b2024-07-03 20:39:42 -070082#if 1
yq.wang0ff0ca22024-06-29 02:38:27 -070083 if(event != 0)
yq.wanga40216c2024-06-24 01:38:32 -070084#else
wangyouqiangce12f2b2024-04-23 17:57:09 +080085 if(event != 7)
yq.wanga40216c2024-06-24 01:38:32 -070086#endif
wangyouqiangce12f2b2024-04-23 17:57:09 +080087 {
88 *ubus_gnss_result = MBTK_POWER_CLOSE_FAIL;
89 }
90
91 return ;
92}
93
94static int mbtk_power_ubus_uloop_init(struct ubus_context **ctx)
95{
96 int ret = -1;
97 if(ctx == NULL)
98 {
99 LOGE("[MBTK_POWER] ctx is NULL");
100 return MBTK_POWER_RESULT_FAIL;
101 }
102
103 ret = uloop_init();
104 if(ret != 0)
105 {
106 LOGE("[MBTK_POWER] uloop_init fail.ret = [%d]", ret);
107 return MBTK_POWER_RESULT_FAIL;
108 }
109
110 if(*ctx != NULL)
111 {
112 LOGE("[MBTK_POWER] mbtk_gnss_ctx not NULL.");
113 return MBTK_POWER_RESULT_FAIL;
114 }
115
116 *ctx = ubus_connect(NULL);
117 if( !(*ctx) )
118 {
119 LOGE("[MBTK_POWER] ubus_connect fail.");
120 return MBTK_POWER_RESULT_FAIL;
121 }
122
123 ubus_add_uloop(*ctx);
124
125 return MBTK_POWER_RESULT_SUCCESS;
126}
127
128static int mbtk_power_ubus_uloop_deinit(struct ubus_context *ctx)
129{
130 if(ctx != NULL)
131 {
132 ubus_free(ctx);
133 ctx = NULL;
134 }
135
136 uloop_done();
137
138 return MBTK_POWER_RESULT_SUCCESS;
139}
140
141static int mbtk_power_invoke_reply_data_cb(const char *service, const char *method, struct blob_attr *msg,
142 ubus_data_handler_t cb, void *cb_param, int timeout, struct ubus_context *ctx)
143{
144 int rc = -1;
145 uint32_t id;
146 struct ubus_request req;
147
148 /* Look up the target object id by the object path */
149 rc = ubus_lookup_id(ctx, service, &id);
150 if(rc)
151 {
152 LOGE("[MBTK_POWER] ubus_lookup_id fail.rc = [%d]", rc);
153 return MBTK_POWER_RESULT_FAIL;
154 }
155
156 rc = ubus_invoke(ctx, id, method, msg, cb, cb_param, timeout);
157 if(rc)
158 {
159 LOGE("[MBTK_POWER] ubus_invoke fail.rc = [%d]", rc);
160 return MBTK_POWER_RESULT_FAIL;
161 }
162 return MBTK_POWER_RESULT_SUCCESS;
163}
164
liuyangcdf88992024-07-05 11:48:24 +0800165 static int mbtk_GPS_process(gnss_cmd_enum cmd, void *arg)
166 {
167 if(sock_listen_fd < 0) {
168 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
169 if(sock_listen_fd < 0)
170 {
r.xiao663d7ce2024-08-02 01:01:06 -0700171 LOGE("[MBTK_POWER]socket() fail[%d].", errno);
liuyangcdf88992024-07-05 11:48:24 +0800172 return -1;
173 }
174
175 struct sockaddr_un cli_addr;
176 memset(&cli_addr, 0, sizeof(cli_addr));
177 cli_addr.sun_family = AF_LOCAL;
178 strcpy(cli_addr.sun_path, GNSS_SOCK_PATH);
179 if(connect(sock_listen_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
180 {
r.xiao663d7ce2024-08-02 01:01:06 -0700181 LOGE("[MBTK_POWER]connect() fail[%d].", errno);
liuyangcdf88992024-07-05 11:48:24 +0800182 close(sock_listen_fd);
183 sock_listen_fd = -1;
184 return -1;
185 }
186 }
187
188 char buff[100] = {0};
189 if(cmd == GNSS_CMD_INIT) {
190 if(arg) {
191 sprintf(buff, "gnss_init:%d", *(int*)arg);
192 } else {
193 return -1;
194 }
195 } else if(cmd == GNSS_CMD_DEINIT) {
196 sprintf(buff, "gnss_deinit");
197 } else if(cmd == GNSS_CMD_SETTING) {
198 sprintf(buff, "gnss_setting:%s", arg);
199 } else if(cmd == GNSS_CMD_DL) {
200 sprintf(buff, "gnss_dl:%s", arg);
201 } else {
r.xiao663d7ce2024-08-02 01:01:06 -0700202 LOGE("[MBTK_POWER]Unknown cmd.");
liuyangcdf88992024-07-05 11:48:24 +0800203 return -1;
204 }
205
206 write(sock_listen_fd, buff, strlen(buff));
207
208 int len = 0;
209 while(1) {
210 memset(buff, 0, sizeof(buff));
211 len = read(sock_listen_fd, buff, sizeof(buff));
212 if(len > 0) {
r.xiao663d7ce2024-08-02 01:01:06 -0700213 LOGE("[MBTK_POWER]RSP : %s", buff);
liuyangcdf88992024-07-05 11:48:24 +0800214 if(cmd == GNSS_CMD_INIT) {
r.xiao663d7ce2024-08-02 01:01:06 -0700215 if(strstr(buff, "gnss_init") != 0) {
216 return 0;
liuyangcdf88992024-07-05 11:48:24 +0800217 } else {
r.xiao663d7ce2024-08-02 01:01:06 -0700218 LOGE("[MBTK_POWER]gnss_init response error.");
liuyangcdf88992024-07-05 11:48:24 +0800219 return -1;
220 }
221 } else if(cmd == GNSS_CMD_DEINIT) {
r.xiao663d7ce2024-08-02 01:01:06 -0700222 if(strstr(buff, "gnss_deinit") != 0) {
223 return 0;
liuyangcdf88992024-07-05 11:48:24 +0800224 } else {
r.xiao663d7ce2024-08-02 01:01:06 -0700225 LOGE("[MBTK_POWER]gnss_deinit response error.");
liuyangcdf88992024-07-05 11:48:24 +0800226 return -1;
227 }
228 } else if(cmd == GNSS_CMD_SETTING) {
r.xiao663d7ce2024-08-02 01:01:06 -0700229 if(strstr(buff, "gnss_setting") != 0) {
230 return 0;
liuyangcdf88992024-07-05 11:48:24 +0800231 } else {
r.xiao663d7ce2024-08-02 01:01:06 -0700232 LOGE("[MBTK_POWER]gnss_setting response error.");
liuyangcdf88992024-07-05 11:48:24 +0800233 return -1;
234 }
235 } else if(cmd == GNSS_CMD_DL) {
r.xiao663d7ce2024-08-02 01:01:06 -0700236 if(strstr(buff, "gnss_dl") != 0) {
237 return 0;
liuyangcdf88992024-07-05 11:48:24 +0800238 } else {
r.xiao663d7ce2024-08-02 01:01:06 -0700239 LOGE("[MBTK_POWER]gnss_dl response error.");
liuyangcdf88992024-07-05 11:48:24 +0800240 return -1;
241 }
242 } else {
r.xiao663d7ce2024-08-02 01:01:06 -0700243 LOGE("[MBTK_POWER]Unknown response.\n");
liuyangcdf88992024-07-05 11:48:24 +0800244 return -1;
245 }
246 } else if(len == 0) {
r.xiao663d7ce2024-08-02 01:01:06 -0700247 LOGE("[MBTK_POWER]RSP is null.");
liuyangcdf88992024-07-05 11:48:24 +0800248 return -1;
249 } else {
r.xiao663d7ce2024-08-02 01:01:06 -0700250 LOGE("[MBTK_POWER]read = %d:errno = %d", len, errno);
liuyangcdf88992024-07-05 11:48:24 +0800251 }
252 }
253 }
254
255
wangyouqiangce12f2b2024-04-23 17:57:09 +0800256
257static int mbtk_power_gnss_close(void)
258{
259 int ret = 0;
liuyangcdf88992024-07-05 11:48:24 +0800260
261 ret = mbtk_GPS_process(GNSS_CMD_DEINIT, NULL);
262
263 if (-1 == ret)
wangyouqiangce12f2b2024-04-23 17:57:09 +0800264 {
liuyangcdf88992024-07-05 11:48:24 +0800265 LOGE("[MBTK_POWER] mbtk_power_gnss_close fail.");
wangyouqiangce12f2b2024-04-23 17:57:09 +0800266 return MBTK_POWER_CLOSE_FAIL;
267 }
268
wangyouqiangce12f2b2024-04-23 17:57:09 +0800269 return MBTK_POWER_CLOSE_SUCCESS;
270}
271
272static int gpio_direct_set(char *value,int gpio)
273{
274 char buffer[50]= {0};
275 int file =-1;
276 int result =-1;
277
278 memset(buffer,0,50);
279 sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
280 file = open(buffer, O_WRONLY);
281 if(file == -1)
282 {
283 LOGE("[MBTK_POWER] Open gpio[%d] direct fail.", gpio);
284 return MBTK_POWER_RESULT_FAIL;
285 }
286
287 result = write(file,value,strlen(value));
288 if(result != strlen(value))
289 {
290 LOGE("[MBTK_POWER] Set gpio[%d] direct fail.", gpio);
291 close(file);
292 return MBTK_POWER_RESULT_FAIL;
293 }
294 close(file);
295
296 return MBTK_POWER_RESULT_SUCCESS;
297}
298
299static int gpio_value_set(int value, int gpio)
300{
301 char buffer[50]= {0};
302 int file =-1;
303 int result =-1;
304
305 memset(buffer,0,50);
306 sprintf(buffer,"/sys/class/gpio/gpio%d/value", gpio);
307 file = open(buffer,O_WRONLY);
308 if(file == -1)
309 {
310 LOGE("[MBTK_POWER] Open gpio[%d] value fail.", gpio);
311 return MBTK_POWER_RESULT_FAIL;
312 }
313 if(value == 0) {
314 result = write(file,"0",1);
315 } else {
316 result = write(file,"1",1);
317 }
318 if(result != 1)
319 {
320 LOGE("[MBTK_POWER] Set gpio[%d] value fail.", gpio);
321 close(file);
322 return MBTK_POWER_RESULT_FAIL;
323 }
324 close(file);
325
326 return MBTK_POWER_RESULT_SUCCESS;
327}
328
329//type:0 value 0
330//type:1 value 1
331//type:2 direction "out"
332//type:3 direction "in"
333
334static int one_gpio_export(int gpio, int type)
335{
336 int index=0;
337 int file=-1;
338 int file1=-1;
339 int result =-1;
340 char pin_index_buffer[5]= {0};
341 char buffer[50]= {0};
342 int ret =0;
343
344 file = open("/sys/class/gpio/export",O_WRONLY);
345 if(file == -1)
346 {
347 LOGE("[MBTK_POWER] Open gpio export file fail.");
348 return MBTK_POWER_RESULT_FAIL;
349 }
350
351 memset(buffer,0,50);
352 sprintf(buffer,"/sys/class/gpio/gpio%d", gpio);
353 file1 = open(buffer,O_RDONLY);
354 if(file1 != -1)
355 {
356 //file is created
357 close(file1);
358 }
359 else
360 { // create gpio
361 memset(pin_index_buffer,0,5);
362 sprintf(pin_index_buffer,"%d",gpio);
363 result = write(file,pin_index_buffer,strlen(pin_index_buffer));
364 if(result < 0)
365 {
366 LOGE("[MBTK_POWER] Gpio[%d] export fail.", gpio);
367 return MBTK_POWER_RESULT_FAIL;
368 }
369 }
370
371 close(file);
372
373
374 switch(type)
375 {
376 case 0 :
377 {
378 ret = gpio_value_set(0, gpio);
379 break;
380 }
381 case 1:
382 {
383 ret = gpio_value_set(1, gpio);
384 break;
385 }
386 case 2:
387 {
388 ret = gpio_direct_set("out", gpio);
389 break;
390 }
391 case 3:
392 {
393 ret = gpio_direct_set("in", gpio);
394 break;
395 }
396 default:
397 {
398 break;
399 }
400 }
401
402
403 return ret;
404}
405
406/****************************FUNC***************************************/
407
408/****************************API***************************************/
409#if defined(MBTK_PROJECT_T108)
410int mbtk_system_sleep(void)
411{
412 int ubus_ret = 0;
413
414 ubus_ret = mbtk_power_gnss_close();
415 if(ubus_ret < 0)
416 {
417 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
418 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
419 }
420
421 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
422 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
423
424 if(!access("/sys/power/autosleep", W_OK))
425 {
426 system("echo mem > /sys/power/autosleep");
427 }
428 else
429 {
430 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
431 //printf("/sys/power/autosleep can not write.");
432 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
433 }
434
435 return MBTK_POWER_RESULT_SUCCESS;
436}
437
438#elif defined(MBTK_PROJECT_L508_X6)
439int mbtk_system_sleep(void)
440{
441 int ubus_ret = 0;
442
443 ubus_ret = mbtk_power_gnss_close();
444 if(ubus_ret < 0)
445 {
446 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
447 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
448 }
449
450 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
451 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
452 //close Ldo6
453 system("i2cset -y -f 2 0x31 0x18 0x0f");
454 //GPS_WAKE_HOST to GPIO
455 system("hwacc w 0xd401e198 0x1040");
456 //HOST_WAKE_GPS to GPIO
457 //system("hwacc w 0xd401e0c0 0x1040");
458 //gpio34 to GPIO
459#if 1
460 system("hwacc w 0xd401e164 0x1040");
461 system("hwacc w 0xd401e166 0x1040");
462 system("hwacc w 0xd401e1b4 0x1040");
463
464 system("hwacc w 0xd401e2ec 0xa441");
465 system("hwacc w 0xd401e2f0 0xa441");
466 system("hwacc w 0xd401e2f4 0xa441");
467 system("hwacc w 0xd401e2f8 0xa441");
468 system("hwacc w 0xd401e2fc 0xa441");
469 system("hwacc w 0xd401e300 0xa441");
470#endif
471 one_gpio_export(117, 2);
472 one_gpio_export(117, 0);
473
474 one_gpio_export(119, 2);
475 one_gpio_export(119, 0);
476
477 one_gpio_export(127, 2);
478 one_gpio_export(127, 0);
479
480 one_gpio_export(33, 2);
481 one_gpio_export(33, 0);
482
483 one_gpio_export(34, 2);
484 one_gpio_export(34, 0);
485
486 one_gpio_export(54, 2);
487 one_gpio_export(54, 0);
488
489 one_gpio_export(21, 2);
490 one_gpio_export(21, 0);
491
492 one_gpio_export(118, 2);
493 one_gpio_export(118, 0);
494
495 if(!access("/sys/power/autosleep", W_OK))
496 {
497 system("echo mem > /sys/power/autosleep");
498 }
499 else
500 {
501 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
502 //printf("/sys/power/autosleep can not write.");
503 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
504 }
505
506 return MBTK_POWER_RESULT_SUCCESS;
507}
508
509#elif defined(MBTK_PROJECT_L509)
510int mbtk_system_sleep(void)
511{
512 int ubus_ret = 0;
513
514 ubus_ret = mbtk_power_gnss_close();
515 if(ubus_ret < 0)
516 {
517 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
518 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
519 }
520
521 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
522 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
523
524 one_gpio_export(120, 2);
525 one_gpio_export(120, 0);
526
527 one_gpio_export(21, 2);
528 one_gpio_export(21, 0);
529
530 one_gpio_export(118, 2);
531 one_gpio_export(118, 0);
532
533 if(!access("/sys/power/autosleep", W_OK))
534 {
535 system("echo mem > /sys/power/autosleep");
536 }
537 else
538 {
539 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
540 //printf("/sys/power/autosleep can not write.");
541 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
542 }
543
544 return MBTK_POWER_RESULT_SUCCESS;
545}
546
547#elif defined(MBTK_PROJECT_L508)
548int mbtk_system_sleep(void)
549{
550 int ubus_ret = 0;
551
552 ubus_ret = mbtk_power_gnss_close();
553 if(ubus_ret < 0)
554 {
555 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
556 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
557 }
558
559 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
560 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
561
562 one_gpio_export(119, 2);
563 one_gpio_export(119, 0);
564
565 one_gpio_export(21, 2);
566 one_gpio_export(21, 0);
567
568 one_gpio_export(118, 2);
569 one_gpio_export(118, 0);
570
571 if(!access("/sys/power/autosleep", W_OK))
572 {
573 system("echo mem > /sys/power/autosleep");
574 }
575 else
576 {
577 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
578 //printf("/sys/power/autosleep can not write.");
579 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
580 }
581
582 return MBTK_POWER_RESULT_SUCCESS;
583}
584
585#elif defined(MBTK_PROJECT_PN1803)
586int mbtk_system_sleep(void)
587{
588 int ubus_ret = 0;
589
590 ubus_ret = mbtk_power_gnss_close();
591 if(ubus_ret < 0)
592 {
593 LOGE("[MBTK_POWER] mbtk_power_gnss_close() fail.");
594 return MBTK_POWER_RESULT_GNSS_CLOSE_FAIL;
595 }
596
597 // echo off > /sys/devices/mbtk-dev-op.10/gps_power && echo mem > /sys/power/autosleep
598 // echo 1 > /sys/devices/asr-rfkill.0/pwr_ctrl && sleep 2 && echo 0 > /sys/devices/asr-rfkill.0/pwr_ctrl
599
600 one_gpio_export(119, 2);
601 one_gpio_export(119, 0);
602
603 one_gpio_export(21, 2);
604 one_gpio_export(21, 0);
605
606 one_gpio_export(118, 2);
607 one_gpio_export(118, 0);
608
609 if(!access("/sys/power/autosleep", W_OK))
610 {
611 system("echo mem > /sys/power/autosleep");
612 }
613 else
614 {
615 LOGE("[MBTK_POWER] /sys/power/autosleep can not write.");
616 //printf("/sys/power/autosleep can not write.");
617 return MBTK_POWER_RESULT_NO_SLEEP_NODE;
618 }
619
620 return MBTK_POWER_RESULT_SUCCESS;
621}
622
623#else
624int mbtk_system_sleep(void)
625{
626 return MBTK_POWER_RESULT_SUCCESS;
627}
628#endif
629/****************************API***************************************/
630#endif