blob: 24ffcb48982f1c902d966b5cf84bad00d9b3ccca [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
25using ::android::Parcel;
26
27static pthread_mutex_t g_lynq_sim_sendto_mutex;
28
29typedef struct{
30 int uToken;
31 int request;
32 int paramLen;
33 char param[MAX_LEN];
34}lynq_client_t;
35
jb.qia7620e72023-09-02 01:35:17 -070036int num = 0;
37
llb52a49f2022-09-05 00:23:18 -070038static int get_md_sta(void)
39{
40 FILE *fp;
jb.qia7620e72023-09-02 01:35:17 -070041 char buffer[64]={};
llb52a49f2022-09-05 00:23:18 -070042 fp = popen("cat /sys/kernel/ccci/boot","r");
43 if(fp == NULL)
44 {
ll892b6ef2022-09-08 06:45:34 -070045 RLOGD("function %s fp is null\n", __FUNCTION__);
ll2f5e1c82022-09-07 00:01:43 -070046 return 0;
47 }
llb52a49f2022-09-05 00:23:18 -070048 fgets(buffer,sizeof(buffer),fp);
ll892b6ef2022-09-08 06:45:34 -070049 if(!strlen(buffer))
50 {
51 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
52 pclose(fp);
53 return 0;
54 }
llb52a49f2022-09-05 00:23:18 -070055 if(buffer[4] == '4')
56 {
57 pclose(fp);
58 return 1;
59 }
ll892b6ef2022-09-08 06:45:34 -070060 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
llb52a49f2022-09-05 00:23:18 -070061 pclose(fp);
62 return 0;
63}
64
65static int check_service(const char *service)
66{
67 FILE *fp;
68 char buffer[1024]={};
69 if(!strcmp(service, "/usr/sbin/mtkfusionrild"))
70 {
71 fp = popen("ps -ef|grep rild","r");
72 }
73 else if(!strcmp(service, "lynq-ril-service"))
74 {
75 fp = popen("ps -ef|grep ril-service","r");
76 }
77 if(fp == NULL)
78 {
ll892b6ef2022-09-08 06:45:34 -070079 RLOGD("function %s fp is null\n", __FUNCTION__);
llb52a49f2022-09-05 00:23:18 -070080 return 0;
81 }
82 while(NULL != fgets(buffer,sizeof(buffer),fp))
83 {
84 if(strstr(buffer,service))
85 {
86 pclose(fp);
ll88c42532023-02-01 15:34:52 +080087 RLOGD("check_service 1\n");
llb52a49f2022-09-05 00:23:18 -070088 return 1;
89 }
90 }
ll88c42532023-02-01 15:34:52 +080091 RLOGD("check_service 0\n");
llb52a49f2022-09-05 00:23:18 -070092 pclose(fp);
93 return 0;
94}
95
96/**
jb.qi3d1d9a42023-05-16 06:55:53 -070097 * @brief 30s request imei
llb52a49f2022-09-05 00:23:18 -070098 *
99 * @param arg
100 * @return void*
101 */
102void * timer_request_imei(void * arg)
103{
104 int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
105 if (-1 == sock_fd)
106 {
107 RLOGD("__FUNCTION__ %s create sock_fd failed %s\n", __FUNCTION__, strerror(errno));
108 /*set uci*/
109 /*socket abnormal*/
110 system("uci set lynq_uci.sdk_ready='3'");
111 /*set uci*/
112 return NULL;
113 }
114 struct timeval timeOut;
115 timeOut.tv_sec = 3;//timeout time 3s
116 timeOut.tv_usec = 0;
117 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0)
118 {
119 RLOGD("__FUNCTION__ %s time out setting failed %s\n", __FUNCTION__, strerror(errno));
120 /*set uci*/
121 /*socket abnormal*/
122 system("uci set lynq_uci.sdk_ready='3'");
123 /*set uci*/
124 return NULL;
125 }
jb.qi3d1d9a42023-05-16 06:55:53 -0700126 struct sockaddr_in addr_serv;
127 memset(&addr_serv, 0, sizeof(addr_serv));
128 addr_serv.sin_family = AF_INET;
129 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
130 addr_serv.sin_port = htons(DEST_PORT);
131 int len_addr_serv = sizeof(addr_serv);
llb52a49f2022-09-05 00:23:18 -0700132 int send_num = -1;
133 int recv_num = -1;
134 int resp_type = -1;
135 int request = -1;
136 int slot_id = -1;
llf0936572022-09-15 19:06:10 +0800137 int token = -1;
llb52a49f2022-09-05 00:23:18 -0700138 int res_error = -1;
139 lynq_client_t client_t;
140 char res_data[MAX_LEN] = {0};
ll892b6ef2022-09-08 06:45:34 -0700141 client_t.request = RIL_REQUEST_DEVICE_IDENTITY;
llb52a49f2022-09-05 00:23:18 -0700142 client_t.paramLen = 0;
143 client_t.uToken = 999;
144 memset(client_t.param, 0, sizeof(client_t.param));
145 while (1)
146 {
llb52a49f2022-09-05 00:23:18 -0700147 bzero(res_data, MAX_LEN);
148 pthread_mutex_lock(&g_lynq_sim_sendto_mutex);
jb.qi3d1d9a42023-05-16 06:55:53 -0700149 send_num = sendto(sock_fd, &client_t, sizeof(client_t), 0, (struct sockaddr *)&addr_serv, len_addr_serv);
ll88c42532023-02-01 15:34:52 +0800150 RLOGD("send_num %d\n", send_num);
llb52a49f2022-09-05 00:23:18 -0700151 if(send_num == 0)
152 {
153 RLOGD("__FUNCTION__ %s Close to end %s\n", __FUNCTION__, strerror(errno));
154 //unknow
155 system("uci set lynq_uci.sdk_ready='4'");
156 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
157 continue;
158 }
159 if(send_num < 0)
160 {
161 RLOGD("__FUNCTION__ %s sendto error %s\n", __FUNCTION__, strerror(errno));
162 //unknow
163 system("uci set lynq_uci.sdk_ready='4'");
164 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
165 continue;
166 }
167 //get data msg
jb.qi3d1d9a42023-05-16 06:55:53 -0700168 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 +0800169 RLOGD("recv_num %d\n", recv_num);
llb52a49f2022-09-05 00:23:18 -0700170 if(recv_num == 0)
171 {
172 RLOGD("__FUNCTION__ %s Close to end\n", __FUNCTION__);
173 //unknow
174 system("uci set lynq_uci.sdk_ready='4'");
175 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
176 continue;;
177 }
178 if(recv_num == -1 && errno == EAGAIN)
179 {
180 RLOGD("__FUNCTION__ %srecvfrom fail because timeout\n", __FUNCTION__);
181 /*set uci*/
182 if(!get_md_sta())
183 {
184 system("uci set lynq_uci.sdk_ready='1'");
185 }
186 else
187 {
188 if(check_service("/usr/sbin/mtkfusionrild"))//rild work
189 {
190 if(!check_service("lynq-ril-service"))//not find,must be socket error
191 {
192 system("uci set lynq_uci.sdk_ready='3'");
193 }
194 else
195 {
196 //unknow error
197 system("uci set lynq_uci.sdk_ready='4'");
198 }
199
200 }
201 else//rild no work
202 {
203 //unknow
204 system("uci set lynq_uci.sdk_ready='4'");
205 }
206 }
207 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
208 continue;;
209 }
210 if(recv_num < 0)
211 {
212 RLOGD("__FUNCTION__ %srecvfrom fail %s\n", __FUNCTION__, strerror(errno));
213 //unknow
214 system("uci set lynq_uci.sdk_ready='4'");
215 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
216 continue;;
217 }
218 Parcel p;
219 p.setData((uint8_t *)res_data,sizeof(char)*recv_num);
220 p.setDataPosition(0);
221 if(p.dataAvail() > 0)
222 {
223 p.readInt32(&resp_type);
llf0936572022-09-15 19:06:10 +0800224 p.readInt32(&token);
llb52a49f2022-09-05 00:23:18 -0700225 p.readInt32(&request);
226 p.readInt32(&slot_id);
227 p.readInt32(&res_error);
228 }
229 /*judge the res_error*/
230 if(!res_error)
231 {
ll892b6ef2022-09-08 06:45:34 -0700232 system("uci set lynq_uci.sdk_ready='0'");
llb52a49f2022-09-05 00:23:18 -0700233 }
234 else
235 {
ll892b6ef2022-09-08 06:45:34 -0700236 system("uci set lynq_uci.sdk_ready='2'"); //Md configurations are inconsistent with AP configurations
llb52a49f2022-09-05 00:23:18 -0700237 }
238 RLOGD("__FUNCTION__ %s res_error %d\n", __FUNCTION__, res_error);
239 /*judge the res_error*/
240 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
ll2f5e1c82022-09-07 00:01:43 -0700241 sleep(TIMER);
llb52a49f2022-09-05 00:23:18 -0700242 }
243 return NULL;
244}
245
246
qjbd37f0902023-08-04 04:08:52 -0700247void delete_enter(char data[])
248{
249 char *find = strchr(data, '\n');
250 if(find)
251 *find = '\0';
252 return ;
qjbdeea8452023-08-03 06:35:43 -0700253}
254
255
256void *check(void * arg)
257{
258
259 char pid[200][8];
qjbd37f0902023-08-04 04:08:52 -0700260 char ffd[200][4];
qjbdeea8452023-08-03 06:35:43 -0700261 char buf[64];
262 char check_time[4];
263 char timebuf[32];
qjbd37f0902023-08-04 04:08:52 -0700264 int num = 1;
265 int i=0;
qjbdeea8452023-08-03 06:35:43 -0700266 int j;
267 FILE *fp,*fp_1;
268 int ret;
269
270 RLOGD("check cpu/pid/interrupts/fd!!!\n");
qjbd37f0902023-08-04 04:08:52 -0700271 system("mkdir /media/check_file");
qjbdeea8452023-08-03 06:35:43 -0700272 system("touch /media/check_time");
273 fp_1 = popen("cat /media/check_time","r");
274 if(fgets(check_time, 4, fp_1) != NULL)
275 {
276 num = atoi(check_time);
277 }
278 pclose(fp_1);
279 while(1)
280 {
281 i = 0;
282 system("date >>/media/check_file/cpu_moniter.txt");
283 ret = system("top -b |head -n 25 >>/media/check_file/cpu_moniter.txt");
284 RLOGD("cpu ret %d", ret);
285 system("date >>/media/check_file/inter_moniter.txt");
qjbd37f0902023-08-04 04:08:52 -0700286 ret = system("cat /proc/interrupts |grep -v \": 0 0\" >>/media/check_file/inter_moniter.txt");
qjbdeea8452023-08-03 06:35:43 -0700287 RLOGD("interrupts ret %d", ret);
jb.qibad810e2023-08-29 03:55:18 -0700288
qjbdeea8452023-08-03 06:35:43 -0700289 system("date >>/media/check_file/pid_moniter.txt");
290 ret = system("ps -eo \"%p %a\" | grep -v \"\\[\" >>/media/check_file/pid_moniter.txt");
291 RLOGD("pid ret %d", ret);
qjbdeea8452023-08-03 06:35:43 -0700292
jb.qibad810e2023-08-29 03:55:18 -0700293 system("date >>/media/check_file/meminfo_moniter.txt");
294 ret = system("cat /proc/meminfo >>/media/check_file/meminfo_moniter.txt");
295 RLOGD("meminfo ret %d", ret);
296
297 system("date >>/media/check_file/buddyinfo_moniter.txt");
298 ret = system("cat /proc/buddyinfo >>/media/check_file/buddyinfo_moniter.txt");
299 RLOGD("buddyinfo ret %d", ret);
300
301 system("date >>/media/check_file/ps_auxw_moniter.txt");
302 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");
303 RLOGD("ps_auxw ret %d", ret);
304
305 system("date >>/media/check_file/cur_freq_moniter.txt");
306 ret = system("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq >>/media/check_file/cur_freq_moniter.txt");
307 RLOGD("cur_freq ret %d", ret);
308
309 system("date >>/media/check_file/available_frequencies_moniter.txt");
310 ret = system("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies >>/media/check_file/available_frequencies_moniter.txt");
311 RLOGD("available_frequencies ret %d", ret);
312
313 system("date >>/media/check_file/fd_moniter.txt");
qjbdeea8452023-08-03 06:35:43 -0700314 fp = popen("ps -eo \"%p %a\" | grep -v \"\\[\"|awk '{print $1}'","r");
315 while(fgets(pid[i], 8, fp) != NULL)
316 {
317 delete_enter(pid[i]);
318 i++;
319 }
320 pclose(fp);
321
322 for(j=1;j<i;j++)
323 {
324 sprintf(buf, "ls /proc/%s/fd | wc -l", pid[j]);
325 fp = popen(buf, "r");
326 fgets(ffd[j], 4, fp);
327 sprintf(buf,"echo \"pid: %s, fd_num: %s\" >>/media/check_file/fd_moniter.txt",pid[j], ffd[j]);
328 system(buf);
329 pclose(fp);
330 }
qjbd37f0902023-08-04 04:08:52 -0700331
jb.qibad810e2023-08-29 03:55:18 -0700332 if(num > 228)
qjbd37f0902023-08-04 04:08:52 -0700333 {
334 system("cp /media/check_file/cpu_moniter.txt /media/check_file/cpu_moniter_1.txt");
jb.qibad810e2023-08-29 03:55:18 -0700335 system("cp /media/check_file/inter_moniter.txt /media/check_file/inter_moniter_1.txt");
qjbd37f0902023-08-04 04:08:52 -0700336 system("cp /media/check_file/pid_moniter.txt /media/check_file/pid_moniter_1.txt");
337 system("cp /media/check_file/fd_moniter.txt /media/check_file/fd_moniter_1.txt");
jb.qibad810e2023-08-29 03:55:18 -0700338 system("cp /media/check_file/fd_moniter.txt /media/check_file/meminfo_moniter_1.txt");
339 system("cp /media/check_file/fd_moniter.txt /media/check_file/buddyinfo_moniter_1.txt");
340 system("cp /media/check_file/fd_moniter.txt /media/check_file/ps_auxw_moniter_1.txt");
341 system("cp /media/check_file/fd_moniter.txt /media/check_file/cur_freq_moniter_1.txt");
342 system("cp /media/check_file/fd_moniter.txt /media/check_file/available_frequencies_moniter_1.txt");
343
344
qjbd37f0902023-08-04 04:08:52 -0700345 system("rm -rf /media/check_file/cpu_moniter.txt");
346 system("rm -rf /media/check_file/inter_moniter.txt");
347 system("rm -rf /media/check_file/pid_moniter.txt");
348 system("rm -rf /media/check_file/fd_moniter.txt");
jb.qibad810e2023-08-29 03:55:18 -0700349 system("rm -rf /media/check_file/meminfo_moniter.txt");
350 system("rm -rf /media/check_file/buddyinfo_moniter.txt");
351 system("rm -rf /media/check_file/ps_auxw_moniter.txt");
352 system("rm -rf /media/check_file/cur_freq_moniter.txt");
353 system("rm -rf /media/check_file/available_frequencies_moniter.txt");
354
qjbd37f0902023-08-04 04:08:52 -0700355 num = 0;
356 }
qjbdeea8452023-08-03 06:35:43 -0700357 num++;
358 sprintf(timebuf, "echo \"%d\" >/media/check_time", num);
359 ret = system(timebuf);
qjbdeea8452023-08-03 06:35:43 -0700360 sleep(300);
qjbdeea8452023-08-03 06:35:43 -0700361 }
362
qjbdeea8452023-08-03 06:35:43 -0700363}
364
jb.qia7620e72023-09-02 01:35:17 -0700365void *check_uci(void * arg)
366{
367 int ret[6];
368 int result = 0;
369 int flag = 0;
370
371 char uci_0[]="/etc/config/lynq_uci";
372 char uci_1[]="/etc/config/lynq_uci_ro";
373 char uci_2[]="/etc/config/mdlog";
374 char uci_3[]="/etc/config/radio_property";
375 char uci_4[]="/etc/config/service";
376 char uci_5[]="/etc/config/usb";
377 RLOGD("start check uci\n");
378 while(num++ < 4)
379 {
380 RLOGD("@@@@@@@num=%d\n", num);
381 flag = 0;
382 ret[0] = system("uci show | grep \"lynq_uci.lynq_ril\"");
383 if(ret[0] != 0)
384 {
385 RLOGD("lynq_uci unload\n");
386 flag = 1;
387 }
388
389 ret[1] = system("uci show | grep \"^lynq_uci_ro\.\"");
390 if(ret[1] != 0)
391 {
392 RLOGD("lynq_uci_ro unload\n");
393 flag = 1;
394 }
395
396 ret[2] = system("uci show | grep \"^mdlog\.\"");
397 if(ret[2] != 0)
398 {
399 RLOGD("mdlog unload\n");
400 flag = 1;
401 }
402
403 ret[3] = system("uci show | grep \"^radio_property\.\"");
404 if(ret[3] != 0)
405 {
406 RLOGD("radio_property unload\n");
407 flag = 1;
408 }
409
410 ret[4] = system("uci show | grep \"^service\.\"");
411 if(ret[4] != 0)
412 {
413 RLOGD("service unload\n");
414 flag = 1;
415 }
416
417 ret[5] = system("uci show | grep \"^usb\.\"");
418 if(ret[5] != 0)
419 {
420 RLOGD("usb unload\n");
421 flag = 1;
422 }
423
424 RLOGD("flag=%d\n",flag);
425 if(flag != 0)
426 {
427 RLOGD("config reload\n");
428 result = chdir("/data_backup/");
429 result =system("tar -zxvf userdata.tar.gz -C /STATE/");
430 if(result!= 0)
431 {
432 RLOGD("cp config fail\n");
433 }
434 if(ret[0] != 0)
435 {
436 lynq_load_config(uci_0);
437 RLOGD("reload lynq_uci\n");
438 system("systemctl start autosuspend");
439 }
440 if(ret[1] != 0)
441 {
442 lynq_load_config(uci_1);
443 RLOGD("reload lynq_uci_ro\n");
444 }
445 if(ret[2] != 0)
446 {
447 lynq_load_config(uci_2);
448 RLOGD("reload mdlog\n");
449 }
450 if(ret[3] != 0)
451 {
452 lynq_load_config(uci_3);
453 RLOGD("reload radio_property\n");
454 system("systemctl restart mtkfusionrild");
455 }
456 if(ret[4] != 0)
457 {
458 lynq_load_config(uci_4);
459 RLOGD("reload service\n");
460 }
461 if(ret[5] != 0)
462 {
463 lynq_load_config(uci_5);
464 RLOGD("reload usb\n");
465 }
466 }
467 else
468 {
469 RLOGD("uci check success, exit check!\n");
470 break;
471 }
472 sleep(2);
473 }
474
475}
476
477
jb.qibad810e2023-08-29 03:55:18 -0700478
llb52a49f2022-09-05 00:23:18 -0700479void start_timer_request(void)
480{
jb.qia7620e72023-09-02 01:35:17 -0700481 pthread_t thid,thid_1,thid_2;
llb52a49f2022-09-05 00:23:18 -0700482 pthread_attr_t a;
483 pthread_attr_init(&a);
484 pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
qjbdeea8452023-08-03 06:35:43 -0700485
llb52a49f2022-09-05 00:23:18 -0700486 int ret = pthread_create(&thid, &a, timer_request_imei, NULL);
487 if(ret != 0){
488 RLOGD("pthread_create error!!!");
489 return;
490 }
jb.qieb2cc522023-08-18 01:45:07 +0800491 sleep(20);
qjbdeea8452023-08-03 06:35:43 -0700492 ret = pthread_create(&thid_1, NULL, check, NULL);
493 if(ret != 0){
494 RLOGD("pthread_create error!!!");
495 return;
496 }
jb.qia7620e72023-09-02 01:35:17 -0700497
498 ret = pthread_create(&thid_2,NULL,check_uci,NULL);
499 if(ret != 0){
500 RLOGD("pthread_create error!!!");
501 return;
502 }
503 pthread_detach(thid_2);
llb52a49f2022-09-05 00:23:18 -0700504 return;
505}
506