blob: c1ab0410f4b2f297ec7cb463631901b799ee85bd [file] [log] [blame]
wangyouqiang16aadb02024-04-12 15:47:13 +08001#if 1
2#include <stdio.h>
3//#include <stdlib.h>
b.liu9e8584b2024-11-06 19:21:28 +08004#include <unistd.h>
wangyouqiang16aadb02024-04-12 15:47:13 +08005#include <errno.h>
6#include <pthread.h>
7//#include <string.h>
8#include <fcntl.h>
9//#include <signal.h>
b.liu9e8584b2024-11-06 19:21:28 +080010#include <cutils/properties.h>
wangyouqiang16aadb02024-04-12 15:47:13 +080011
12#include "mbtk_log.h"
13#include "mbtk_info_api.h"
b.liu9e8584b2024-11-06 19:21:28 +080014#include "mbtk_utils.h"
wangyouqiang16aadb02024-04-12 15:47:13 +080015
16#define MBTK_RESULT_FAIL -1
17#define MBTK_RESULT_SUCCESS 0
hong.liuc05984a2025-06-24 21:49:16 +080018#define BOOT_CHECK_TIME 200 //200ms
19#define NORMAL_CHECK_TIME 30000 //30s
20#define PERIOD_LEN 10
21int in_period[PERIOD_LEN] = {100,100,200,200,500,500,500,500,2000,5000};//ms
wangyouqiang16aadb02024-04-12 15:47:13 +080022typedef enum{
23 MBTK_READY_INIT = -1,
24 MBTK_READY_SUCCESS,
25 MBTK_READY_MODEM_FAIL,
26 MBTK_READY_RESPONSE_FAIL,
27 MBTK_READY_SOCKET_FAIL,
28 MBTK_READY_RIL_FAIL
29}mbtk_ready_status_type;
b.liubcf86c92024-08-19 19:48:28 +080030
wangyouqiang16aadb02024-04-12 15:47:13 +080031static mbtk_ready_status_type mbtk_ready_status = MBTK_READY_INIT;
32static mbtk_info_handle_t* mbtk_handle = NULL;
33#if 1
34static mbtk_ready_status_type modem_check(void)
35{
36 char imei[16]= {0};
37 int cme_err = 0;
38
39 if(mbtk_handle == NULL)
40 {
41 mbtk_handle = mbtk_info_handle_get();
42 if(mbtk_handle == NULL)
43 {
44 LOGE("[SDK_READY] mbtk_info_handle_get fail.");
45 return MBTK_READY_RIL_FAIL;
46 }
47 }
48
49 mbtk_handle->info_err = 0;
50 cme_err = mbtk_imei_get(mbtk_handle, imei);
51 //LOGE("[SDK_READY] imei = [%s], cme_err = [%d]", imei, cme_err);
52 if(cme_err == 0 && strlen(imei) == 0)
53 {
54 mbtk_info_handle_free(&mbtk_handle);
55 mbtk_handle = NULL;
56 return MBTK_READY_RIL_FAIL;
57 }
58 if(cme_err != 0 || strlen(imei) == 0)
59 {
60 mbtk_info_handle_free(&mbtk_handle);
61 mbtk_handle = NULL;
62 return MBTK_READY_MODEM_FAIL;
63 }
64 return MBTK_READY_SUCCESS;
65}
66#endif
hong.liuc05984a2025-06-24 21:49:16 +080067long get_uptime()
68{
69 struct timespec start_time;
70 clock_gettime(CLOCK_MONOTONIC, &start_time);
71 return start_time.tv_sec;
72}
wangyouqiang16aadb02024-04-12 15:47:13 +080073static void* sdk_ready_check_pthread(void *arg)
74{
75 UNUSED(arg);
76 LOGE("[SDK_READY] sdk_ready_check_pthread entry.");
wangyouqiang16aadb02024-04-12 15:47:13 +080077 mbtk_ready_status_type now_ready_status = MBTK_READY_INIT;
78 char buf[2] = {0};
hong.liuc05984a2025-06-24 21:49:16 +080079 buf[0] = '0' + now_ready_status;
80 property_set("persist.mbtk.sdk.state", buf);// init state value
wangyouqiang16aadb02024-04-12 15:47:13 +080081 int sleep_time = 30;
hong.liuc05984a2025-06-24 21:49:16 +080082 long uptime = 0;
83 int count = 0;
wangyouqiang16aadb02024-04-12 15:47:13 +080084 while(1)
85 {
86 now_ready_status = modem_check();
87 if(now_ready_status != mbtk_ready_status)
88 {
89 buf[0] = '0' + now_ready_status;
90 property_set("persist.mbtk.sdk.state", buf);
91 mbtk_ready_status = now_ready_status;
92 }
hong.liuc05984a2025-06-24 21:49:16 +080093 uptime = get_uptime();
94 if(uptime < 50)//in 50s
wangyouqiang16aadb02024-04-12 15:47:13 +080095 {
hong.liuc05984a2025-06-24 21:49:16 +080096 if(now_ready_status != MBTK_READY_SUCCESS)
97 {
98 sleep_time = BOOT_CHECK_TIME;
99 }
100 else
101 {
102 sleep_time = NORMAL_CHECK_TIME;
103 }
wangyouqiang16aadb02024-04-12 15:47:13 +0800104 }
105 else
106 {
hong.liuc05984a2025-06-24 21:49:16 +0800107 if(now_ready_status != MBTK_READY_SUCCESS)
108 {
109 if(count < PERIOD_LEN)
110 {
111 sleep_time = in_period[count];
112 }
113 else
114 {
115 count = 0;
116 sleep_time = in_period[count];
117 }
118 count++;
119 }
120 else
121 {
122 sleep_time = NORMAL_CHECK_TIME;
123 count = 0;
124 }
b.liubcf86c92024-08-19 19:48:28 +0800125
hong.liuc05984a2025-06-24 21:49:16 +0800126 }
127 usleep(sleep_time*1000);
wangyouqiang16aadb02024-04-12 15:47:13 +0800128 }
129 LOGE("[SDK_READY] sdk_ready_check_pthread exit.");
130 return NULL;
131}
132
133int main(int argc, char *argv[])
134{
b.liubcf86c92024-08-19 19:48:28 +0800135 mbtk_log_init("radio", "MBTK_SDK_READY");
136
137 MBTK_SOURCE_INFO_PRINT("mbtk_sdk_ready");
138
wangyouqiang16aadb02024-04-12 15:47:13 +0800139 if(mbtk_ready_status != MBTK_READY_INIT)
140 {
141 LOGE("[SDK_READY] sdk has check.");
142 return MBTK_RESULT_FAIL;
143 }
144
b.liubb590492024-06-13 16:42:08 +0800145#ifdef MBTK_DUMP_SUPPORT
146 mbtk_debug_open(NULL, TRUE);
147#endif
148
wangyouqiang16aadb02024-04-12 15:47:13 +0800149 LOGE("[SDK_READY] sdk check init.");
150 pthread_t sdk_ready_pid;
151 pthread_attr_t thread_attr;
152 pthread_attr_init(&thread_attr);
153 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
154 {
155 LOGE("[SDK_READY] pthread_attr_setdetachstate() fail.");
156 return MBTK_RESULT_FAIL;
157 }
b.liubcf86c92024-08-19 19:48:28 +0800158
wangyouqiang16aadb02024-04-12 15:47:13 +0800159 if(pthread_create(&sdk_ready_pid, &thread_attr, sdk_ready_check_pthread, NULL))
160 {
161 LOGE("[SDK_READY] pthread_create() fail.");
162 }
163
164 pthread_attr_destroy(&thread_attr);
165
166 LOGE("[SDK_READY] sdk check start.");
167 while(1)
168 {
169 sleep(24 * 60 * 60);
170 }
171
172 LOGE("[SDK_READY]!!!mbtk_sdk_ready exit!!!");
173 return MBTK_RESULT_SUCCESS;
174}
hong.liuc05984a2025-06-24 21:49:16 +0800175#endif