blob: 17c3e4003d7fb888fd384bd502af5b823ff200f3 [file] [log] [blame]
llb52a49f2022-09-05 00:23:18 -07001#include <stdio.h>
2#include <sys/types.h>
3#include <sys/socket.h>
4#include <arpa/inet.h>
5#include <string.h>
6#include <unistd.h>
7#include <binder/Parcel.h>
8#include <log/log.h>
9#include <cutils/jstring.h>
10#include <pthread.h>
11#include <sys/socket.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <errno.h>
15#include "lynq_timer.h"
jb.qia7620e72023-09-02 01:35:17 -070016#include <include/lynq_uci.h>
17
llb52a49f2022-09-05 00:23:18 -070018
19#define DEST_PORT 8088
20#define DSET_IP_ADDRESS "127.0.0.1"
ll892b6ef2022-09-08 06:45:34 -070021#define RIL_REQUEST_DEVICE_IDENTITY 98
llb52a49f2022-09-05 00:23:18 -070022#define MAX_LEN 1024
jb.qi3d1d9a42023-05-16 06:55:53 -070023#define TIMER 30
llb52a49f2022-09-05 00:23:18 -070024
jb.qia1a08a92023-10-16 02:32:00 -070025
26#define LOG_UCI_FILE "lynq_uci"
27#define LOG_UCI_MODULE "debug_mode"
28
llb52a49f2022-09-05 00:23:18 -070029using ::android::Parcel;
30
31static pthread_mutex_t g_lynq_sim_sendto_mutex;
32
33typedef struct{
34 int uToken;
35 int request;
36 int paramLen;
37 char param[MAX_LEN];
38}lynq_client_t;
39
jb.qia7620e72023-09-02 01:35:17 -070040int num = 0;
41
llb52a49f2022-09-05 00:23:18 -070042static int get_md_sta(void)
43{
44 FILE *fp;
jb.qia7620e72023-09-02 01:35:17 -070045 char buffer[64]={};
llb52a49f2022-09-05 00:23:18 -070046 fp = popen("cat /sys/kernel/ccci/boot","r");
47 if(fp == NULL)
48 {
ll892b6ef2022-09-08 06:45:34 -070049 RLOGD("function %s fp is null\n", __FUNCTION__);
ll2f5e1c82022-09-07 00:01:43 -070050 return 0;
51 }
llb52a49f2022-09-05 00:23:18 -070052 fgets(buffer,sizeof(buffer),fp);
ll892b6ef2022-09-08 06:45:34 -070053 if(!strlen(buffer))
54 {
55 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
56 pclose(fp);
57 return 0;
58 }
llb52a49f2022-09-05 00:23:18 -070059 if(buffer[4] == '4')
60 {
61 pclose(fp);
62 return 1;
63 }
ll892b6ef2022-09-08 06:45:34 -070064 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
llb52a49f2022-09-05 00:23:18 -070065 pclose(fp);
66 return 0;
67}
68
69static int check_service(const char *service)
70{
71 FILE *fp;
72 char buffer[1024]={};
73 if(!strcmp(service, "/usr/sbin/mtkfusionrild"))
74 {
75 fp = popen("ps -ef|grep rild","r");
76 }
77 else if(!strcmp(service, "lynq-ril-service"))
78 {
79 fp = popen("ps -ef|grep ril-service","r");
80 }
81 if(fp == NULL)
82 {
ll892b6ef2022-09-08 06:45:34 -070083 RLOGD("function %s fp is null\n", __FUNCTION__);
llb52a49f2022-09-05 00:23:18 -070084 return 0;
85 }
86 while(NULL != fgets(buffer,sizeof(buffer),fp))
87 {
88 if(strstr(buffer,service))
89 {
90 pclose(fp);
ll88c42532023-02-01 15:34:52 +080091 RLOGD("check_service 1\n");
llb52a49f2022-09-05 00:23:18 -070092 return 1;
93 }
94 }
ll88c42532023-02-01 15:34:52 +080095 RLOGD("check_service 0\n");
llb52a49f2022-09-05 00:23:18 -070096 pclose(fp);
97 return 0;
98}
99
100/**
jb.qi3d1d9a42023-05-16 06:55:53 -0700101 * @brief 30s request imei
llb52a49f2022-09-05 00:23:18 -0700102 *
103 * @param arg
104 * @return void*
105 */
106void * timer_request_imei(void * arg)
107{
108 int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
109 if (-1 == sock_fd)
110 {
111 RLOGD("__FUNCTION__ %s create sock_fd failed %s\n", __FUNCTION__, strerror(errno));
112 /*set uci*/
113 /*socket abnormal*/
114 system("uci set lynq_uci.sdk_ready='3'");
115 /*set uci*/
116 return NULL;
117 }
118 struct timeval timeOut;
119 timeOut.tv_sec = 3;//timeout time 3s
120 timeOut.tv_usec = 0;
121 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0)
122 {
123 RLOGD("__FUNCTION__ %s time out setting failed %s\n", __FUNCTION__, strerror(errno));
124 /*set uci*/
125 /*socket abnormal*/
126 system("uci set lynq_uci.sdk_ready='3'");
127 /*set uci*/
128 return NULL;
129 }
jb.qi3d1d9a42023-05-16 06:55:53 -0700130 struct sockaddr_in addr_serv;
131 memset(&addr_serv, 0, sizeof(addr_serv));
132 addr_serv.sin_family = AF_INET;
133 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
134 addr_serv.sin_port = htons(DEST_PORT);
135 int len_addr_serv = sizeof(addr_serv);
llb52a49f2022-09-05 00:23:18 -0700136 int send_num = -1;
137 int recv_num = -1;
138 int resp_type = -1;
139 int request = -1;
140 int slot_id = -1;
llf0936572022-09-15 19:06:10 +0800141 int token = -1;
llb52a49f2022-09-05 00:23:18 -0700142 int res_error = -1;
143 lynq_client_t client_t;
144 char res_data[MAX_LEN] = {0};
ll892b6ef2022-09-08 06:45:34 -0700145 client_t.request = RIL_REQUEST_DEVICE_IDENTITY;
llb52a49f2022-09-05 00:23:18 -0700146 client_t.paramLen = 0;
147 client_t.uToken = 999;
148 memset(client_t.param, 0, sizeof(client_t.param));
149 while (1)
150 {
llb52a49f2022-09-05 00:23:18 -0700151 bzero(res_data, MAX_LEN);
152 pthread_mutex_lock(&g_lynq_sim_sendto_mutex);
jb.qi3d1d9a42023-05-16 06:55:53 -0700153 send_num = sendto(sock_fd, &client_t, sizeof(client_t), 0, (struct sockaddr *)&addr_serv, len_addr_serv);
ll88c42532023-02-01 15:34:52 +0800154 RLOGD("send_num %d\n", send_num);
llb52a49f2022-09-05 00:23:18 -0700155 if(send_num == 0)
156 {
157 RLOGD("__FUNCTION__ %s Close to end %s\n", __FUNCTION__, strerror(errno));
158 //unknow
159 system("uci set lynq_uci.sdk_ready='4'");
160 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
161 continue;
162 }
163 if(send_num < 0)
164 {
165 RLOGD("__FUNCTION__ %s sendto error %s\n", __FUNCTION__, strerror(errno));
166 //unknow
167 system("uci set lynq_uci.sdk_ready='4'");
168 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
169 continue;
170 }
171 //get data msg
jb.qi3d1d9a42023-05-16 06:55:53 -0700172 recv_num = recvfrom(sock_fd,res_data,sizeof(char)*MAX_LEN,0,(struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
ll88c42532023-02-01 15:34:52 +0800173 RLOGD("recv_num %d\n", recv_num);
llb52a49f2022-09-05 00:23:18 -0700174 if(recv_num == 0)
175 {
176 RLOGD("__FUNCTION__ %s Close to end\n", __FUNCTION__);
177 //unknow
178 system("uci set lynq_uci.sdk_ready='4'");
179 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
180 continue;;
181 }
182 if(recv_num == -1 && errno == EAGAIN)
183 {
184 RLOGD("__FUNCTION__ %srecvfrom fail because timeout\n", __FUNCTION__);
185 /*set uci*/
186 if(!get_md_sta())
187 {
188 system("uci set lynq_uci.sdk_ready='1'");
189 }
190 else
191 {
192 if(check_service("/usr/sbin/mtkfusionrild"))//rild work
193 {
194 if(!check_service("lynq-ril-service"))//not find,must be socket error
195 {
196 system("uci set lynq_uci.sdk_ready='3'");
197 }
198 else
199 {
200 //unknow error
201 system("uci set lynq_uci.sdk_ready='4'");
202 }
203
204 }
205 else//rild no work
206 {
207 //unknow
208 system("uci set lynq_uci.sdk_ready='4'");
209 }
210 }
211 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
212 continue;;
213 }
214 if(recv_num < 0)
215 {
216 RLOGD("__FUNCTION__ %srecvfrom fail %s\n", __FUNCTION__, strerror(errno));
217 //unknow
218 system("uci set lynq_uci.sdk_ready='4'");
219 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
220 continue;;
221 }
222 Parcel p;
223 p.setData((uint8_t *)res_data,sizeof(char)*recv_num);
224 p.setDataPosition(0);
225 if(p.dataAvail() > 0)
226 {
227 p.readInt32(&resp_type);
llf0936572022-09-15 19:06:10 +0800228 p.readInt32(&token);
llb52a49f2022-09-05 00:23:18 -0700229 p.readInt32(&request);
230 p.readInt32(&slot_id);
231 p.readInt32(&res_error);
232 }
233 /*judge the res_error*/
234 if(!res_error)
235 {
ll892b6ef2022-09-08 06:45:34 -0700236 system("uci set lynq_uci.sdk_ready='0'");
llb52a49f2022-09-05 00:23:18 -0700237 }
238 else
239 {
ll892b6ef2022-09-08 06:45:34 -0700240 system("uci set lynq_uci.sdk_ready='2'"); //Md configurations are inconsistent with AP configurations
llb52a49f2022-09-05 00:23:18 -0700241 }
242 RLOGD("__FUNCTION__ %s res_error %d\n", __FUNCTION__, res_error);
243 /*judge the res_error*/
244 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
ll2f5e1c82022-09-07 00:01:43 -0700245 sleep(TIMER);
llb52a49f2022-09-05 00:23:18 -0700246 }
247 return NULL;
248}
249
250
qjbd37f0902023-08-04 04:08:52 -0700251void delete_enter(char data[])
252{
253 char *find = strchr(data, '\n');
254 if(find)
255 *find = '\0';
256 return ;
qjbdeea8452023-08-03 06:35:43 -0700257}
258
jb.qia1a08a92023-10-16 02:32:00 -0700259
qjbdeea8452023-08-03 06:35:43 -0700260void *check(void * arg)
261{
262
263 char pid[200][8];
qjbd37f0902023-08-04 04:08:52 -0700264 char ffd[200][4];
qjbdeea8452023-08-03 06:35:43 -0700265 char buf[64];
266 char check_time[4];
267 char timebuf[32];
qjbd37f0902023-08-04 04:08:52 -0700268 int num = 1;
269 int i=0;
qjbdeea8452023-08-03 06:35:43 -0700270 int j;
271 FILE *fp,*fp_1;
272 int ret;
273
274 RLOGD("check cpu/pid/interrupts/fd!!!\n");
qjbd37f0902023-08-04 04:08:52 -0700275 system("mkdir /media/check_file");
qjbdeea8452023-08-03 06:35:43 -0700276 system("touch /media/check_time");
277 fp_1 = popen("cat /media/check_time","r");
278 if(fgets(check_time, 4, fp_1) != NULL)
279 {
280 num = atoi(check_time);
281 }
282 pclose(fp_1);
283 while(1)
284 {
285 i = 0;
286 system("date >>/media/check_file/cpu_moniter.txt");
287 ret = system("top -b |head -n 25 >>/media/check_file/cpu_moniter.txt");
288 RLOGD("cpu ret %d", ret);
289 system("date >>/media/check_file/inter_moniter.txt");
qjbd37f0902023-08-04 04:08:52 -0700290 ret = system("cat /proc/interrupts |grep -v \": 0 0\" >>/media/check_file/inter_moniter.txt");
qjbdeea8452023-08-03 06:35:43 -0700291 RLOGD("interrupts ret %d", ret);
jb.qibad810e2023-08-29 03:55:18 -0700292
qjbdeea8452023-08-03 06:35:43 -0700293 system("date >>/media/check_file/pid_moniter.txt");
294 ret = system("ps -eo \"%p %a\" | grep -v \"\\[\" >>/media/check_file/pid_moniter.txt");
295 RLOGD("pid ret %d", ret);
qjbdeea8452023-08-03 06:35:43 -0700296
jb.qibad810e2023-08-29 03:55:18 -0700297 system("date >>/media/check_file/meminfo_moniter.txt");
298 ret = system("cat /proc/meminfo >>/media/check_file/meminfo_moniter.txt");
299 RLOGD("meminfo ret %d", ret);
300
301 system("date >>/media/check_file/buddyinfo_moniter.txt");
302 ret = system("cat /proc/buddyinfo >>/media/check_file/buddyinfo_moniter.txt");
303 RLOGD("buddyinfo ret %d", ret);
304
305 system("date >>/media/check_file/ps_auxw_moniter.txt");
306 ret = system("ps -auxw|grep -v \"0.0 0.0\"|grep -v \"0.1 0.0\"|grep -v \"0.0 0.1\" >>/media/check_file/ps_auxw_moniter.txt");
307 RLOGD("ps_auxw ret %d", ret);
308
309 system("date >>/media/check_file/cur_freq_moniter.txt");
310 ret = system("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq >>/media/check_file/cur_freq_moniter.txt");
311 RLOGD("cur_freq ret %d", ret);
312
313 system("date >>/media/check_file/available_frequencies_moniter.txt");
314 ret = system("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies >>/media/check_file/available_frequencies_moniter.txt");
315 RLOGD("available_frequencies ret %d", ret);
316
317 system("date >>/media/check_file/fd_moniter.txt");
qjbdeea8452023-08-03 06:35:43 -0700318 fp = popen("ps -eo \"%p %a\" | grep -v \"\\[\"|awk '{print $1}'","r");
319 while(fgets(pid[i], 8, fp) != NULL)
320 {
321 delete_enter(pid[i]);
322 i++;
323 }
324 pclose(fp);
325
326 for(j=1;j<i;j++)
327 {
328 sprintf(buf, "ls /proc/%s/fd | wc -l", pid[j]);
329 fp = popen(buf, "r");
330 fgets(ffd[j], 4, fp);
331 sprintf(buf,"echo \"pid: %s, fd_num: %s\" >>/media/check_file/fd_moniter.txt",pid[j], ffd[j]);
332 system(buf);
333 pclose(fp);
334 }
qjbd37f0902023-08-04 04:08:52 -0700335
jb.qibad810e2023-08-29 03:55:18 -0700336 if(num > 228)
qjbd37f0902023-08-04 04:08:52 -0700337 {
338 system("cp /media/check_file/cpu_moniter.txt /media/check_file/cpu_moniter_1.txt");
jb.qibad810e2023-08-29 03:55:18 -0700339 system("cp /media/check_file/inter_moniter.txt /media/check_file/inter_moniter_1.txt");
qjbd37f0902023-08-04 04:08:52 -0700340 system("cp /media/check_file/pid_moniter.txt /media/check_file/pid_moniter_1.txt");
341 system("cp /media/check_file/fd_moniter.txt /media/check_file/fd_moniter_1.txt");
jb.qibad810e2023-08-29 03:55:18 -0700342 system("cp /media/check_file/fd_moniter.txt /media/check_file/meminfo_moniter_1.txt");
343 system("cp /media/check_file/fd_moniter.txt /media/check_file/buddyinfo_moniter_1.txt");
344 system("cp /media/check_file/fd_moniter.txt /media/check_file/ps_auxw_moniter_1.txt");
345 system("cp /media/check_file/fd_moniter.txt /media/check_file/cur_freq_moniter_1.txt");
346 system("cp /media/check_file/fd_moniter.txt /media/check_file/available_frequencies_moniter_1.txt");
347
348
qjbd37f0902023-08-04 04:08:52 -0700349 system("rm -rf /media/check_file/cpu_moniter.txt");
350 system("rm -rf /media/check_file/inter_moniter.txt");
351 system("rm -rf /media/check_file/pid_moniter.txt");
352 system("rm -rf /media/check_file/fd_moniter.txt");
jb.qibad810e2023-08-29 03:55:18 -0700353 system("rm -rf /media/check_file/meminfo_moniter.txt");
354 system("rm -rf /media/check_file/buddyinfo_moniter.txt");
355 system("rm -rf /media/check_file/ps_auxw_moniter.txt");
356 system("rm -rf /media/check_file/cur_freq_moniter.txt");
357 system("rm -rf /media/check_file/available_frequencies_moniter.txt");
358
qjbd37f0902023-08-04 04:08:52 -0700359 num = 0;
360 }
qjbdeea8452023-08-03 06:35:43 -0700361 num++;
362 sprintf(timebuf, "echo \"%d\" >/media/check_time", num);
363 ret = system(timebuf);
qjbdeea8452023-08-03 06:35:43 -0700364 sleep(300);
qjbdeea8452023-08-03 06:35:43 -0700365 }
jb.qia1a08a92023-10-16 02:32:00 -0700366 return NULL;
qjbdeea8452023-08-03 06:35:43 -0700367
qjbdeea8452023-08-03 06:35:43 -0700368}
jb.qia1a08a92023-10-16 02:32:00 -0700369
qjbdeea8452023-08-03 06:35:43 -0700370
jb.qia7620e72023-09-02 01:35:17 -0700371void *check_uci(void * arg)
372{
373 int ret[6];
374 int result = 0;
375 int flag = 0;
376
377 char uci_0[]="/etc/config/lynq_uci";
378 char uci_1[]="/etc/config/lynq_uci_ro";
379 char uci_2[]="/etc/config/mdlog";
380 char uci_3[]="/etc/config/radio_property";
381 char uci_4[]="/etc/config/service";
382 char uci_5[]="/etc/config/usb";
383 RLOGD("start check uci\n");
384 while(num++ < 4)
385 {
386 RLOGD("@@@@@@@num=%d\n", num);
387 flag = 0;
388 ret[0] = system("uci show | grep \"lynq_uci.lynq_ril\"");
389 if(ret[0] != 0)
390 {
391 RLOGD("lynq_uci unload\n");
392 flag = 1;
393 }
394
395 ret[1] = system("uci show | grep \"^lynq_uci_ro\.\"");
396 if(ret[1] != 0)
397 {
398 RLOGD("lynq_uci_ro unload\n");
399 flag = 1;
400 }
401
402 ret[2] = system("uci show | grep \"^mdlog\.\"");
403 if(ret[2] != 0)
404 {
405 RLOGD("mdlog unload\n");
406 flag = 1;
407 }
408
409 ret[3] = system("uci show | grep \"^radio_property\.\"");
410 if(ret[3] != 0)
411 {
412 RLOGD("radio_property unload\n");
413 flag = 1;
414 }
415
416 ret[4] = system("uci show | grep \"^service\.\"");
417 if(ret[4] != 0)
418 {
419 RLOGD("service unload\n");
420 flag = 1;
421 }
422
423 ret[5] = system("uci show | grep \"^usb\.\"");
424 if(ret[5] != 0)
425 {
426 RLOGD("usb unload\n");
427 flag = 1;
428 }
429
430 RLOGD("flag=%d\n",flag);
431 if(flag != 0)
432 {
433 RLOGD("config reload\n");
434 result = chdir("/data_backup/");
435 result =system("tar -zxvf userdata.tar.gz -C /STATE/");
436 if(result!= 0)
437 {
438 RLOGD("cp config fail\n");
439 }
440 if(ret[0] != 0)
441 {
442 lynq_load_config(uci_0);
443 RLOGD("reload lynq_uci\n");
444 system("systemctl start autosuspend");
445 }
446 if(ret[1] != 0)
447 {
448 lynq_load_config(uci_1);
449 RLOGD("reload lynq_uci_ro\n");
450 }
451 if(ret[2] != 0)
452 {
453 lynq_load_config(uci_2);
454 RLOGD("reload mdlog\n");
455 }
456 if(ret[3] != 0)
457 {
458 lynq_load_config(uci_3);
459 RLOGD("reload radio_property\n");
460 system("systemctl restart mtkfusionrild");
461 }
462 if(ret[4] != 0)
463 {
464 lynq_load_config(uci_4);
465 RLOGD("reload service\n");
466 }
467 if(ret[5] != 0)
468 {
469 lynq_load_config(uci_5);
470 RLOGD("reload usb\n");
471 }
472 }
473 else
474 {
475 RLOGD("uci check success, exit check!\n");
476 break;
477 }
478 sleep(2);
479 }
jb.qia1a08a92023-10-16 02:32:00 -0700480 return NULL;
jb.qia7620e72023-09-02 01:35:17 -0700481}
482
483
jb.qibad810e2023-08-29 03:55:18 -0700484
llb52a49f2022-09-05 00:23:18 -0700485void start_timer_request(void)
486{
jb.qia7620e72023-09-02 01:35:17 -0700487 pthread_t thid,thid_1,thid_2;
llb52a49f2022-09-05 00:23:18 -0700488 pthread_attr_t a;
489 pthread_attr_init(&a);
490 pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
qjbdeea8452023-08-03 06:35:43 -0700491
jb.qia1a08a92023-10-16 02:32:00 -0700492 char tmp[20];
493 int debug_mode;
494 int ret;
495
496 ret = pthread_create(&thid, &a, timer_request_imei, NULL);
llb52a49f2022-09-05 00:23:18 -0700497 if(ret != 0){
498 RLOGD("pthread_create error!!!");
499 return;
500 }
jb.qieb2cc522023-08-18 01:45:07 +0800501 sleep(20);
jb.qia1a08a92023-10-16 02:32:00 -0700502 ret = lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "sysinfo_debug_status", tmp);
503 RLOGD("ret =%d, tmp is %s\n", ret, tmp);
504 debug_mode=atoi(tmp);
505 RLOGD("debug_mode is %d!!!\n", debug_mode);
506 if(debug_mode == 1)
507 {
508 RLOGD("debug_mode is 1, pthread_create start!!!\n");
509 ret = pthread_create(&thid_1, NULL, check, NULL);
510 if(ret != 0)
511 {
512 RLOGD("pthread_create error!!!");
513 return;
514 }
515
qjbdeea8452023-08-03 06:35:43 -0700516 }
jb.qia1a08a92023-10-16 02:32:00 -0700517
518
jb.qia7620e72023-09-02 01:35:17 -0700519
520 ret = pthread_create(&thid_2,NULL,check_uci,NULL);
521 if(ret != 0){
522 RLOGD("pthread_create error!!!");
523 return;
524 }
525 pthread_detach(thid_2);
llb52a49f2022-09-05 00:23:18 -0700526 return;
527}
528