wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 1 | #if 1 |
| 2 | #include <stdio.h> |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 3 | #include <stdlib.h> |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 4 | #include <unistd.h> |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 5 | #include <errno.h> |
| 6 | #include <pthread.h> |
| 7 | //#include <string.h> |
| 8 | #include <fcntl.h> |
| 9 | //#include <signal.h> |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 10 | #include <cutils/properties.h> |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 11 | #include <stdint.h> |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 12 | |
| 13 | #include "mbtk_log.h" |
| 14 | #include "mbtk_info_api.h" |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 15 | #include "mbtk_utils.h" |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 16 | |
| 17 | #define MBTK_RESULT_FAIL -1 |
| 18 | #define MBTK_RESULT_SUCCESS 0 |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 19 | #define BOOT_CHECK_TIME 200 //200ms |
| 20 | #define NORMAL_CHECK_TIME 30000 //30s |
| 21 | #define PERIOD_LEN 10 |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 22 | #define update_ril_digit 0x7 //The last three digits are reserved for RIL service,0x111 |
| 23 | #define update_gnss_digit 1<<4 |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 24 | int in_period[PERIOD_LEN] = {100,100,200,200,500,500,500,500,2000,5000};//ms |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame] | 25 | |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 26 | static mbtk_ready_status_type mbtk_ready_status = MBTK_READY_INIT; |
| 27 | static mbtk_info_handle_t* mbtk_handle = NULL; |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 28 | static pthread_mutex_t lock; |
| 29 | int tag = 0; |
| 30 | void update_ril_value(int value) |
| 31 | { |
| 32 | FILE *fp; |
| 33 | char buf[16]; |
| 34 | char cmd[64]; |
| 35 | int rd_value = 0; |
| 36 | int ret = 0; |
| 37 | |
| 38 | pthread_mutex_lock(&lock); |
| 39 | |
| 40 | fp = popen("uci get lynq_uci.sdk_ready","r"); |
| 41 | if(fp == NULL) |
| 42 | { |
| 43 | LOGE("Failed to run uci get lynq_uci.sdk_ready\n"); |
| 44 | pthread_mutex_unlock(&lock); |
| 45 | return; |
| 46 | } |
| 47 | if(fgets(buf, sizeof(buf) - 1, fp) == NULL) |
| 48 | { |
| 49 | LOGE("update_ril_value fgets failed"); |
| 50 | pthread_mutex_unlock(&lock); |
| 51 | pclose(fp); |
| 52 | return; |
| 53 | } |
| 54 | if(tag != 0) |
| 55 | { |
| 56 | rd_value = atoi(buf); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | tag = 1; |
| 61 | } |
| 62 | |
| 63 | rd_value &= ~(update_ril_digit);//The last three digits are reserved for RIL service |
| 64 | rd_value |= (value & update_ril_digit); |
| 65 | sprintf(cmd,"uci set lynq_uci.sdk_ready='%d'",rd_value); |
| 66 | ret = system(cmd); |
| 67 | if(ret != 0) |
| 68 | { |
| 69 | LOGE("Failed to run uci set lynq_uci.sdk_ready\n"); |
| 70 | } |
| 71 | |
| 72 | pclose(fp); |
| 73 | pthread_mutex_unlock(&lock); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | void update_gnss_value() |
| 78 | { |
| 79 | FILE *fp; |
| 80 | char buf[16]; |
| 81 | char cmd[64]; |
| 82 | int rd_value = 0; |
| 83 | int ret = 0; |
| 84 | |
| 85 | pthread_mutex_lock(&lock); |
| 86 | |
| 87 | fp = popen("uci get lynq_uci.sdk_ready","r"); |
| 88 | if(fp == NULL) |
| 89 | { |
| 90 | LOGE("Failed to run uci get lynq_uci.sdk_ready\n"); |
| 91 | pthread_mutex_unlock(&lock); |
| 92 | return; |
| 93 | } |
| 94 | if(fgets(buf, sizeof(buf) - 1, fp) == NULL) |
| 95 | { |
| 96 | LOGE("update_gnss_value fgets failed"); |
| 97 | pthread_mutex_unlock(&lock); |
| 98 | pclose(fp); |
| 99 | return; |
| 100 | } |
| 101 | rd_value = atoi(buf); |
| 102 | rd_value &= ~(update_gnss_digit); |
| 103 | rd_value |= (update_gnss_digit); //The fifth digits is reserved for GNSS service |
| 104 | sprintf(cmd,"uci set lynq_uci.sdk_ready='%d'",rd_value); |
| 105 | ret = system(cmd); |
| 106 | if(ret != 0) |
| 107 | { |
| 108 | LOGE("Failed to run uci set lynq_uci.sdk_ready\n"); |
| 109 | } |
| 110 | pthread_mutex_unlock(&lock); |
| 111 | pclose(fp); |
| 112 | return; |
| 113 | } |
| 114 | |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 115 | #if 1 |
| 116 | static mbtk_ready_status_type modem_check(void) |
| 117 | { |
| 118 | char imei[16]= {0}; |
| 119 | int cme_err = 0; |
| 120 | |
| 121 | if(mbtk_handle == NULL) |
| 122 | { |
| 123 | mbtk_handle = mbtk_info_handle_get(); |
| 124 | if(mbtk_handle == NULL) |
| 125 | { |
| 126 | LOGE("[SDK_READY] mbtk_info_handle_get fail."); |
| 127 | return MBTK_READY_RIL_FAIL; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | mbtk_handle->info_err = 0; |
| 132 | cme_err = mbtk_imei_get(mbtk_handle, imei); |
| 133 | //LOGE("[SDK_READY] imei = [%s], cme_err = [%d]", imei, cme_err); |
| 134 | if(cme_err == 0 && strlen(imei) == 0) |
| 135 | { |
| 136 | mbtk_info_handle_free(&mbtk_handle); |
| 137 | mbtk_handle = NULL; |
| 138 | return MBTK_READY_RIL_FAIL; |
| 139 | } |
| 140 | if(cme_err != 0 || strlen(imei) == 0) |
| 141 | { |
| 142 | mbtk_info_handle_free(&mbtk_handle); |
| 143 | mbtk_handle = NULL; |
| 144 | return MBTK_READY_MODEM_FAIL; |
| 145 | } |
| 146 | return MBTK_READY_SUCCESS; |
| 147 | } |
| 148 | #endif |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 149 | long get_uptime() |
| 150 | { |
| 151 | struct timespec start_time; |
| 152 | clock_gettime(CLOCK_MONOTONIC, &start_time); |
| 153 | return start_time.tv_sec; |
| 154 | } |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 155 | static void* sdk_ready_check_pthread(void *arg) |
| 156 | { |
| 157 | UNUSED(arg); |
| 158 | LOGE("[SDK_READY] sdk_ready_check_pthread entry."); |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 159 | mbtk_ready_status_type now_ready_status = MBTK_READY_INIT; |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 160 | //char buf[MBTK_READY_STRING_SIZE_MAX] = {0}; |
| 161 | // buf[0] = '0' + now_ready_status; |
| 162 | // property_set(MBTK_READY_UCI, buf);// init state value |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 163 | int sleep_time = 30; |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 164 | long uptime = 0; |
| 165 | int count = 0; |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 166 | while(1) |
| 167 | { |
| 168 | now_ready_status = modem_check(); |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 169 | //buf[0] = '0' + now_ready_status; |
| 170 | //property_set(MBTK_READY_UCI, buf); |
| 171 | update_ril_value(now_ready_status); |
| 172 | mbtk_ready_status = now_ready_status; |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 173 | uptime = get_uptime(); |
| 174 | if(uptime < 50)//in 50s |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 175 | { |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 176 | if(now_ready_status != MBTK_READY_SUCCESS) |
| 177 | { |
| 178 | sleep_time = BOOT_CHECK_TIME; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | sleep_time = NORMAL_CHECK_TIME; |
| 183 | } |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 184 | } |
| 185 | else |
| 186 | { |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 187 | if(now_ready_status != MBTK_READY_SUCCESS) |
| 188 | { |
| 189 | if(count < PERIOD_LEN) |
| 190 | { |
| 191 | sleep_time = in_period[count]; |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | count = 0; |
| 196 | sleep_time = in_period[count]; |
| 197 | } |
| 198 | count++; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | sleep_time = NORMAL_CHECK_TIME; |
| 203 | count = 0; |
| 204 | } |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame] | 205 | |
hong.liu | c05984a | 2025-06-24 21:49:16 +0800 | [diff] [blame] | 206 | } |
| 207 | usleep(sleep_time*1000); |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 208 | } |
| 209 | LOGE("[SDK_READY] sdk_ready_check_pthread exit."); |
| 210 | return NULL; |
| 211 | } |
| 212 | |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 213 | static void* check_gnss() |
| 214 | { |
| 215 | LOGE("[SDK_READY] check_gnss entry."); |
| 216 | //update_gnss_value(); |
| 217 | while(1) |
| 218 | { |
| 219 | sleep(UINT32_MAX); |
| 220 | } |
| 221 | return NULL; |
| 222 | } |
| 223 | |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 224 | int main(int argc, char *argv[]) |
| 225 | { |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame] | 226 | mbtk_log_init("radio", "MBTK_SDK_READY"); |
| 227 | |
| 228 | MBTK_SOURCE_INFO_PRINT("mbtk_sdk_ready"); |
| 229 | |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 230 | if(mbtk_ready_status != MBTK_READY_INIT) |
| 231 | { |
| 232 | LOGE("[SDK_READY] sdk has check."); |
| 233 | return MBTK_RESULT_FAIL; |
| 234 | } |
| 235 | |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 236 | #ifdef MBTK_DUMP_SUPPORT |
| 237 | mbtk_debug_open(NULL, TRUE); |
| 238 | #endif |
| 239 | |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 240 | LOGE("[SDK_READY] sdk check init."); |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 241 | pthread_t ril_check_pid, gnss_check_pid; |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 242 | pthread_attr_t thread_attr; |
| 243 | pthread_attr_init(&thread_attr); |
| 244 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 245 | { |
| 246 | LOGE("[SDK_READY] pthread_attr_setdetachstate() fail."); |
| 247 | return MBTK_RESULT_FAIL; |
| 248 | } |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame] | 249 | |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 250 | if(pthread_create(&ril_check_pid, &thread_attr, sdk_ready_check_pthread, NULL)) |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 251 | { |
xf.li | 31aad55 | 2025-07-21 23:26:55 -0700 | [diff] [blame] | 252 | LOGE("[SDK_READY] sdk_ready_check_pthread pthread_create() fail."); |
| 253 | } |
| 254 | |
| 255 | sleep(1); |
| 256 | |
| 257 | if(pthread_create(&gnss_check_pid, &thread_attr, check_gnss, NULL)) |
| 258 | { |
| 259 | LOGE("[SDK_READY] check_gnss pthread_create() fail."); |
wangyouqiang | 16aadb0 | 2024-04-12 15:47:13 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | pthread_attr_destroy(&thread_attr); |
| 263 | |
| 264 | LOGE("[SDK_READY] sdk check start."); |
| 265 | while(1) |
| 266 | { |
| 267 | sleep(24 * 60 * 60); |
| 268 | } |
| 269 | |
| 270 | LOGE("[SDK_READY]!!!mbtk_sdk_ready exit!!!"); |
| 271 | return MBTK_RESULT_SUCCESS; |
| 272 | } |
q.huang | 15cf93d | 2025-06-27 17:26:59 +0800 | [diff] [blame] | 273 | #endif |