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