blob: 62e4b11993deeada8409f29d1999c0f46fe7c9cd [file] [log] [blame]
you.chen5ef374a2023-12-26 17:25:16 +08001#ifdef LED_SUPPORT
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <binder/Parcel.h>
6#include <sys/socket.h>
7#include <errno.h>
8#include <unistd.h>
9#include <cutils/jstring.h>
10#include <sys/types.h>
11#include <time.h>
12#include <signal.h>
13#include "led.h"
14#include "led_inner.h"
15#include <liblog/lynq_deflog.h>
16#include "common.h"
17
18#undef LOG_TAG
19#define LOG_TAG "LED"
20
21
22/*************************************************************
23 Constants and Macros
24*************************************************************/
25#define GPIO_NETLED_CONNECT_REFLASH_TIMER 200
26#define GPIO_NETLED_CREG_REFLASH_TIMER 800
27#define MBTK_GPIO_NETLED_N 77
28
29/*************************************************************
30 Extern Function Declaration
31*************************************************************/
32void mbtk_netled_reflash_handle();
33
34/*************************************************************
35 Variables:local,extern
36*************************************************************/
37//static rex_timer_type mbtk_gpio_netled_timer;
38static mbtk_netled_state_t mbtk_netled_state;
39/*************************************************************
40 Definitions:enum,struct,union
41*************************************************************/
42
43/*=============================================
44FUNCTION
45 mbtk_get_netled_state
46
47DESCRIPTION
48 This function to return pointer of mbtk_netled_state.
49
50DEPENDENCIES
51 None
52
53PARAMETERS
54 None
55
56RETURN VALUE
57 &mbtk_netled_state
58
59SIDE EFFECTS
60 None
61=============================================*/
62
63mbtk_netled_state_t * mbtk_get_netled_state()
64{
65 return &mbtk_netled_state;
66}
67
68/*=============================================
69FUNCTION
70 mbtk_init_netled_state
71
72DESCRIPTION
73 This function to init mbtk_netled_state.
74
75DEPENDENCIES
76 None
77
78PARAMETERS
79 None
80
81RETURN VALUE
82 None
83
84SIDE EFFECTS
85 None
86=============================================*/
87
88void mbtk_init_netled_state()
89{
90 mbtk_netled_state_t * netled_state = NULL;
91 netled_state = mbtk_get_netled_state();
92
93 netled_state->gpio_netled_cs_flag = false;
94 netled_state->gpio_netled_goingsleep_flag = false;
95 netled_state->gpio_netled_ppp_flag = false;
96 netled_state->gpio_netled_wwan_flag = false;
97
98}
99
100static timer_t s_mbtk_gpio_netled_timer;
101
102static int s_mbtk_gpio_netled_timer_sig_value = 11; /* 2,3,4 are used by network */
103
104void start_led_timer(timer_t timer, int signal_value, int milliseconds)
105{
106 RLOGD("start_led_timer(), timer_id=%ld, signal_value=%d, time=%d",(long)timer, signal_value, milliseconds);
107
108 struct itimerspec expire;
109 expire.it_interval.tv_sec = 0;
110 expire.it_interval.tv_nsec = 0;
111 expire.it_value.tv_sec = milliseconds/1000;
112 expire.it_value.tv_nsec = (milliseconds%1000)*1000000;
113 if (timer_settime(timer, 0, &expire, NULL) == -1) {
114 RLOGE("timer_settime failed reason=[%s]", strerror(errno));
115 }
116
117}
118
119void stop_led_timer(timer_t timer, int signal_value) {
120 RLOGD("stop_led_timer(), timer_id=%ld, signal_value=%d", (long)timer, signal_value);
121 struct itimerspec timespec;
122 if(timer_gettime(timer, &timespec) == -1) {
123 RLOGE("stop_led_timer(), get time fail(%s)", strerror(errno));
124 return;
125 }
126 RLOGD("stop_led_timer(), tv_sec=%ld, tv_nsec=%ld",timespec.it_value.tv_sec, timespec.it_value.tv_nsec);
127 if((timespec.it_value.tv_sec == 0) && (timespec.it_value.tv_nsec == 0) ) {
128 RLOGD("stop_led_timer(), timer_id(%ld) had stopped, just return", (long)timer);
129 return;
130 } else {
131 start_led_timer(timer, signal_value, 0);
132 }
133}
134
135static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
136void led_timer_handler(sigval_t sig)
137{
138 RLOGD("led_timer_handler, sig_value: %d", sig.sival_int);
139 int s;
140 s = pthread_mutex_lock(&mtx);
141 if(s != 0) {
142 RLOGE("led_timer_handler, pthead_mutex_lock fail");
143 }
144 if(sig.sival_int == s_mbtk_gpio_netled_timer_sig_value)
145 {
146 mbtk_netled_reflash_handle();
147 }
148 s = pthread_mutex_unlock(&mtx);
149 if(s != 0) {
150 RLOGE("led_timer_handler, pthead_mutex_unlock fail");
151 }
152}
153
154void init_led_timer(timer_t* timer, int signal_value)
155{
156 struct sigevent sevp;
157 memset(&sevp, 0, sizeof(sevp));
158 sevp.sigev_value.sival_int = signal_value;
159 sevp.sigev_notify = SIGEV_THREAD;
160 sevp.sigev_notify_function = led_timer_handler;
161
162 if(timer_create(CLOCK_MONOTONIC, &sevp, timer) == -1) {
163 RLOGE("init_led_timer() failed reason=[%s]", strerror(errno));
164 }
165 RLOGD("init_led_timer(), timer_Id = %ld, signal_value=%d", (long)(*timer), signal_value);
166}
167
168void init_led_timer_all()
169{
170 init_led_timer(&s_mbtk_gpio_netled_timer,s_mbtk_gpio_netled_timer_sig_value);
171}
172
173void deinit_led_timer_all()
174{
175 stop_led_timer(s_mbtk_gpio_netled_timer,s_mbtk_gpio_netled_timer_sig_value);
176}
177
178/*=============================================
179FUNCTION
180 mbtk_netled_init
181
182DESCRIPTION
183 This function to init timer
184
185DEPENDENCIES
186 None
187
188PARAMETERS
189 param not used
190
191RETURN VALUE
192 None
193
194SIDE EFFECTS
195 None
196=============================================*/
197void mbtk_netled_init()
198{
199 mbtk_init_netled_state();
200 init_led_timer_all();
201}
202
203/*=============================================
204FUNCTION
205 mbtk_netled_deinit
206
207DESCRIPTION
208 This function to init timer
209
210DEPENDENCIES
211 None
212
213PARAMETERS
214 param not used
215
216RETURN VALUE
217 None
218
219SIDE EFFECTS
220 None
221=============================================*/
222void mbtk_netled_deinit()
223{
224 deinit_led_timer_all();
225}
226
227
228/*=============================================
229FUNCTION
230 mbtk_get_at_netled_cmd
231
232DESCRIPTION
233 This function to return netled switch value.
234
235DEPENDENCIES
236 None
237
238PARAMETERS
239 None
240
241RETURN VALUE
242 int
243
244SIDE EFFECTS
245 None
246=============================================*/
247
248int mbtk_get_at_netled_cmd(void)
249{
250 return 1;
251// return (int) dsatutil_get_val(MBTK_AT_GPIO_CGNETLED_IDX,0,0,NUM_TYPE);
252}
253
254/*=============================================
255FUNCTION
256 mbtk_get_reg_net_status
257
258DESCRIPTION
259 This function that determine if net registers successfully.
260
261DEPENDENCIES
262 None
263
264PARAMETERS
265 param not used
266
267RETURN VALUE
268 mbtk_netled_reg_net_status
269
270SIDE EFFECTS
271 None
272=============================================*/
273
274mbtk_netled_reg_net_status mbtk_get_reg_net_status(void)
275{
276 mbtk_netled_reg_net_status reg_net_status = NET_REG_FAIL;
277
278 if(false == ril_get_if_insert_simcard())
279 {
280 RLOGE("ril_get_if_insert_simcard false" );
281 reg_net_status = NET_REG_FAIL;
282 return reg_net_status;
283 }
284
285 if(true == ril_get_if_3gpp_reg_success())
286 {
287 RLOGD("ril_get_if_3gpp_reg_success true");
288 reg_net_status = NET_REG_SUCCESS;
289 return reg_net_status;
290 }
291
292 return reg_net_status;
293
294}
295
296/*=============================================
297FUNCTION
298 mbtk_netled_get_socket_conn_status
299
300DESCRIPTION
301 This function that socket connects successfully.
302
303DEPENDENCIES
304 None
305
306PARAMETERS
307 param not used
308
309RETURN VALUE
310 mbtk_netled_socket_conn_status
311
312SIDE EFFECTS
313 None
314=============================================*/
315
316mbtk_netled_socket_conn_status mbtk_netled_get_socket_conn_status(void)
317{
318 mbtk_netled_socket_conn_status socket_conn_status = MBTK_SOCKET_DISCONNECT;
319 mbtk_netled_state_t * netled_state = mbtk_get_netled_state();
320
321 if(netled_state->gpio_netled_ppp_flag || netled_state->gpio_netled_wwan_flag)
322 {
323 RLOGD("GPIO: ppp wwan netled state sockNum %d,%d",
324 netled_state->gpio_netled_ppp_flag, netled_state->gpio_netled_wwan_flag);
325
326 socket_conn_status = MBTK_SOCKET_CONNECTED;
327 return socket_conn_status;
328 }
329
330 return socket_conn_status;
331
332}
333
334/*=============================================
335FUNCTION
336 mbtk_netled_get_current_state
337
338DESCRIPTION
339 get the netled status .
340
341DEPENDENCIES
342 None
343
344PARAMETERS
345 param not used
346
347RETURN VALUE
348 mbtk_netled_status_type
349
350SIDE EFFECTS
351 None
352=============================================*/
353mbtk_netled_status_type mbtk_netled_get_current_state(void)
354{
355 mbtk_netled_reg_net_status reg_net_status = NET_REG_FAIL;
356 mbtk_netled_status_type netled_status_type = GPIO_NETLED_OFF;
357 mbtk_netled_socket_conn_status socket_conn_status = MBTK_SOCKET_DISCONNECT;
358 int mbtk_netled_at_cmd_value = mbtk_get_at_netled_cmd(); // 0 -- close netled / 1 -- open netled
359 mbtk_netled_state_t * netled_state = mbtk_get_netled_state();
360
361 if(mbtk_netled_at_cmd_value == 0 ||
362 netled_state->gpio_netled_goingsleep_flag == true)
363 {
364 netled_status_type = GPIO_NETLED_OFF;
365 return netled_status_type;
366 }
367
368 reg_net_status = mbtk_get_reg_net_status();
369 if( NET_REG_FAIL == reg_net_status || true == netled_state->gpio_netled_cs_flag )
370 {
371 netled_status_type = GPIO_NETLED_LIGHT;
372 }
373 else if( NET_REG_SUCCESS == reg_net_status )
374 {
375 socket_conn_status = mbtk_netled_get_socket_conn_status();
376
377 if( MBTK_SOCKET_CONNECTED == socket_conn_status )
378 {
379 netled_status_type = GPIO_NETLED_CONNECT;
380 }
381 else if( MBTK_SOCKET_DISCONNECT == socket_conn_status )
382 {
383 netled_status_type = GPIO_NETLED_REG;
384 }
385 }
386 return netled_status_type;
387}
388
389
390
391bool is_mbtk_timer_finish() {
392 struct itimerspec timespec;
393 if(timer_gettime(s_mbtk_gpio_netled_timer, &timespec) == -1) {
394 RLOGD("%s(), get time fail(%s)", __FUNCTION__, strerror(errno));
395 return true;
396 }
397 RLOGD("%s(), tv_sec=%ld, tv_nsec=%ld", __FUNCTION__,timespec.it_value.tv_sec, timespec.it_value.tv_nsec);
398 if((timespec.it_value.tv_sec == 0) && (timespec.it_value.tv_nsec == 0) ) {
399 RLOGD("%s(), timer_id(%ld) had stopped", __FUNCTION__, (long)s_mbtk_gpio_netled_timer);
400 return true;
401 }
402 return false;
403}
404
405/*=============================================
406FUNCTION
407 mbtk_netled_state_update
408
409DESCRIPTION
410 This function to reflash network led status and write gpio value
411
412DEPENDENCIES
413 None
414
415PARAMETERS
416 param flag
417
418RETURN VALUE
419 None
420
421SIDE EFFECTS
422 None
423=============================================*/
424void mbtk_netled_state_update(mbtk_netled_reflash_type flag)
425{
426// rex_timer_cnt_type prev_value;
427 mbtk_netled_state_t * netled_state = NULL;
428 netled_state = mbtk_get_netled_state();
429
430 switch (flag)
431 {
432 case GPIO_NETLED_REFLASH_NORMAL:
433 break;
434 case GPIO_NETLED_CS_CALLING:
435 netled_state->gpio_netled_cs_flag = true;
436 break;
437 case GPIO_NETLED_PPP_CONNECT:
438 netled_state->gpio_netled_ppp_flag = true;
439 break;
440 case GPIO_NETLED_PPP_CLOSED:
441 netled_state->gpio_netled_ppp_flag = false;
442 break;
443 case GPIO_NETLED_CS_CALLEND:
444 netled_state->gpio_netled_cs_flag = false;
445 break;
446 case GPIO_NETLED_AP_GOINGSLEEP:
447 netled_state->gpio_netled_goingsleep_flag = true;
448 break;
449 case GPIO_NETLED_AP_WAKEUP:
450 netled_state->gpio_netled_goingsleep_flag = false;
451 break;
452 case GPIO_NETLED_WWAN_CONNECT:
453 netled_state->gpio_netled_wwan_flag = true;
454 break;
455 case GPIO_NETLED_WWAN_CLOSED:
456 netled_state->gpio_netled_wwan_flag = false;
457 break;
458
459 }
460
461 //prev_value = rex_get_timer(&mbtk_gpio_netled_timer);
462// prev_value = timer_get_64(&mbtk_gpio_netled_timer, T_MSEC);
463// if( prev_value == 0)
464 if((flag== GPIO_NETLED_AP_GOINGSLEEP) || is_mbtk_timer_finish())
465 {
466 // MBTK_MSG1_HIGH("GPIO: rex_get_timer prev_value =%d",prev_value);
467 //rex_set_timer(&mbtk_gpio_netled_timer, 1);
468 //timer_set_64(&mbtk_gpio_netled_timer,1,0,T_MSEC);
469 start_led_timer(s_mbtk_gpio_netled_timer, s_mbtk_gpio_netled_timer_sig_value, 1);
470 }
471
472 return ;
473}
474
475/*=============================================
476FUNCTION
477 mbtk_netled_reflash_handle
478
479DESCRIPTION
480 This function to reflash network led status and write gpio value
481
482DEPENDENCIES
483 None
484
485PARAMETERS
486 param not used
487
488RETURN VALUE
489 None
490
491SIDE EFFECTS
492 None
493=============================================*/
494
495void mbtk_gpio_write_output(int gpio_port, int value)
496{
497 if(value==1)
498 {
499 system("echo 255 > /sys/class/leds/led9515:green:cellular-radio/brightness");
500 }
501 else
502 {
503 system("echo 0 > /sys/class/leds/led9515:green:cellular-radio/brightness");
504 }
505}
506
507
508/*=============================================
509FUNCTION
510 mbtk_netled_reflash_handle
511
512DESCRIPTION
513 This function to reflash network led status and write gpio value
514
515DEPENDENCIES
516 None
517
518PARAMETERS
519 param not used
520
521RETURN VALUE
522 None
523
524SIDE EFFECTS
525 None
526=============================================*/
527void mbtk_netled_reflash_handle()
528{
529 mbtk_netled_status_type state = GPIO_NETLED_MAX;
530 state = mbtk_netled_get_current_state();
531 RLOGD("mbtk_netled_get_current_state state = %d",state);
532 static int led_val = 0;
533
534 switch(state)
535 {
536 case GPIO_NETLED_OFF:
537 {
538 if( 0 != led_val )
539 {
540 led_val = 0;
541 mbtk_gpio_write_output(MBTK_GPIO_NETLED_N, 0);
542 }
543 }
544 break;
545 case GPIO_NETLED_REG:
546 {
547 led_val = ( (1 == led_val)? 0:1 );
548 mbtk_gpio_write_output(MBTK_GPIO_NETLED_N, led_val);
549 start_led_timer(s_mbtk_gpio_netled_timer, s_mbtk_gpio_netled_timer_sig_value, GPIO_NETLED_CREG_REFLASH_TIMER);
550
551 // timer_set_64(&mbtk_gpio_netled_timer,GPIO_NETLED_CREG_REFLASH_TIMER,0,T_MSEC);
552 //rex_set_timer(&mbtk_gpio_netled_timer, GPIO_NETLED_CREG_REFLASH_TIMER);
553 }
554 break;
555 case GPIO_NETLED_CONNECT:
556 {
557 led_val = ( (1 == led_val)? 0:1 );
558 mbtk_gpio_write_output(MBTK_GPIO_NETLED_N, led_val);
559 start_led_timer(s_mbtk_gpio_netled_timer, s_mbtk_gpio_netled_timer_sig_value, GPIO_NETLED_CONNECT_REFLASH_TIMER);
560 // timer_set_64(&mbtk_gpio_netled_timer,GPIO_NETLED_CONNECT_REFLASH_TIMER,0,T_MSEC);
561 //rex_set_timer(&mbtk_gpio_netled_timer, GPIO_NETLED_CONNECT_REFLASH_TIMER);
562 }
563 break;
564 case GPIO_NETLED_LIGHT:
565 {
566 if( 1 != led_val )
567 {
568 led_val = 1;
569 mbtk_gpio_write_output(MBTK_GPIO_NETLED_N, 1);
570 }
571 }
572 break;
573 default:
574 RLOGE("GPIO: Unkown netled state !");
575 }
576}
577#endif