blob: bf334808cbf5dd6f26c765a04ac3253e42037a44 [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"
16
17#define DEST_PORT 8088
18#define DSET_IP_ADDRESS "127.0.0.1"
ll892b6ef2022-09-08 06:45:34 -070019#define RIL_REQUEST_DEVICE_IDENTITY 98
llb52a49f2022-09-05 00:23:18 -070020#define MAX_LEN 1024
21#define TIMER 30
22
23using ::android::Parcel;
24
25static pthread_mutex_t g_lynq_sim_sendto_mutex;
26
27typedef struct{
28 int uToken;
29 int request;
30 int paramLen;
31 char param[MAX_LEN];
32}lynq_client_t;
33
34static int get_md_sta(void)
35{
36 FILE *fp;
37 char buffer[64]={};
38 fp = popen("cat /sys/kernel/ccci/boot","r");
39 if(fp == NULL)
40 {
ll892b6ef2022-09-08 06:45:34 -070041 RLOGD("function %s fp is null\n", __FUNCTION__);
ll2f5e1c82022-09-07 00:01:43 -070042 return 0;
43 }
llb52a49f2022-09-05 00:23:18 -070044 fgets(buffer,sizeof(buffer),fp);
ll892b6ef2022-09-08 06:45:34 -070045 if(!strlen(buffer))
46 {
47 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
48 pclose(fp);
49 return 0;
50 }
llb52a49f2022-09-05 00:23:18 -070051 if(buffer[4] == '4')
52 {
53 pclose(fp);
54 return 1;
55 }
ll892b6ef2022-09-08 06:45:34 -070056 RLOGD("function %s line %d\n", __FUNCTION__, __LINE__);
llb52a49f2022-09-05 00:23:18 -070057 pclose(fp);
58 return 0;
59}
60
61static int check_service(const char *service)
62{
63 FILE *fp;
64 char buffer[1024]={};
65 if(!strcmp(service, "/usr/sbin/mtkfusionrild"))
66 {
67 fp = popen("ps -ef|grep rild","r");
68 }
69 else if(!strcmp(service, "lynq-ril-service"))
70 {
71 fp = popen("ps -ef|grep ril-service","r");
72 }
73 if(fp == NULL)
74 {
ll892b6ef2022-09-08 06:45:34 -070075 RLOGD("function %s fp is null\n", __FUNCTION__);
llb52a49f2022-09-05 00:23:18 -070076 return 0;
77 }
78 while(NULL != fgets(buffer,sizeof(buffer),fp))
79 {
80 if(strstr(buffer,service))
81 {
82 pclose(fp);
83 printf("check_service 1\n");
84 return 1;
85 }
86 }
87 printf("check_service 0\n");
88 pclose(fp);
89 return 0;
90}
91
92/**
93 * @brief 30s request imei
94 *
95 * @param arg
96 * @return void*
97 */
98void * timer_request_imei(void * arg)
99{
100 int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
101 if (-1 == sock_fd)
102 {
103 RLOGD("__FUNCTION__ %s create sock_fd failed %s\n", __FUNCTION__, strerror(errno));
104 /*set uci*/
105 /*socket abnormal*/
106 system("uci set lynq_uci.sdk_ready='3'");
107 /*set uci*/
108 return NULL;
109 }
110 struct timeval timeOut;
111 timeOut.tv_sec = 3;//timeout time 3s
112 timeOut.tv_usec = 0;
113 if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0)
114 {
115 RLOGD("__FUNCTION__ %s time out setting failed %s\n", __FUNCTION__, strerror(errno));
116 /*set uci*/
117 /*socket abnormal*/
118 system("uci set lynq_uci.sdk_ready='3'");
119 /*set uci*/
120 return NULL;
121 }
122 struct sockaddr_in addr_serv;
123 memset(&addr_serv, 0, sizeof(addr_serv));
124 addr_serv.sin_family = AF_INET;
125 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
126 addr_serv.sin_port = htons(DEST_PORT);
127 int len_addr_serv = sizeof(addr_serv);
128 int send_num = -1;
129 int recv_num = -1;
130 int resp_type = -1;
131 int request = -1;
132 int slot_id = -1;
133 int res_error = -1;
134 lynq_client_t client_t;
135 char res_data[MAX_LEN] = {0};
ll892b6ef2022-09-08 06:45:34 -0700136 client_t.request = RIL_REQUEST_DEVICE_IDENTITY;
llb52a49f2022-09-05 00:23:18 -0700137 client_t.paramLen = 0;
138 client_t.uToken = 999;
139 memset(client_t.param, 0, sizeof(client_t.param));
140 while (1)
141 {
llb52a49f2022-09-05 00:23:18 -0700142 bzero(res_data, MAX_LEN);
143 pthread_mutex_lock(&g_lynq_sim_sendto_mutex);
144 send_num = sendto(sock_fd, &client_t, sizeof(client_t), 0, (struct sockaddr *)&addr_serv, len_addr_serv);
145 printf("send_num %d\n", send_num);
146 if(send_num == 0)
147 {
148 RLOGD("__FUNCTION__ %s Close to end %s\n", __FUNCTION__, strerror(errno));
149 //unknow
150 system("uci set lynq_uci.sdk_ready='4'");
151 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
152 continue;
153 }
154 if(send_num < 0)
155 {
156 RLOGD("__FUNCTION__ %s sendto error %s\n", __FUNCTION__, strerror(errno));
157 //unknow
158 system("uci set lynq_uci.sdk_ready='4'");
159 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
160 continue;
161 }
162 //get data msg
163 recv_num = recvfrom(sock_fd,res_data,sizeof(char)*MAX_LEN,0,(struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
164 printf("recv_num %d\n", recv_num);
165 if(recv_num == 0)
166 {
167 RLOGD("__FUNCTION__ %s Close to end\n", __FUNCTION__);
168 //unknow
169 system("uci set lynq_uci.sdk_ready='4'");
170 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
171 continue;;
172 }
173 if(recv_num == -1 && errno == EAGAIN)
174 {
175 RLOGD("__FUNCTION__ %srecvfrom fail because timeout\n", __FUNCTION__);
176 /*set uci*/
177 if(!get_md_sta())
178 {
179 system("uci set lynq_uci.sdk_ready='1'");
180 }
181 else
182 {
183 if(check_service("/usr/sbin/mtkfusionrild"))//rild work
184 {
185 if(!check_service("lynq-ril-service"))//not find,must be socket error
186 {
187 system("uci set lynq_uci.sdk_ready='3'");
188 }
189 else
190 {
191 //unknow error
192 system("uci set lynq_uci.sdk_ready='4'");
193 }
194
195 }
196 else//rild no work
197 {
198 //unknow
199 system("uci set lynq_uci.sdk_ready='4'");
200 }
201 }
202 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
203 continue;;
204 }
205 if(recv_num < 0)
206 {
207 RLOGD("__FUNCTION__ %srecvfrom fail %s\n", __FUNCTION__, strerror(errno));
208 //unknow
209 system("uci set lynq_uci.sdk_ready='4'");
210 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
211 continue;;
212 }
213 Parcel p;
214 p.setData((uint8_t *)res_data,sizeof(char)*recv_num);
215 p.setDataPosition(0);
216 if(p.dataAvail() > 0)
217 {
218 p.readInt32(&resp_type);
219 p.readInt32(&request);
220 p.readInt32(&slot_id);
221 p.readInt32(&res_error);
222 }
223 /*judge the res_error*/
224 if(!res_error)
225 {
ll892b6ef2022-09-08 06:45:34 -0700226 system("uci set lynq_uci.sdk_ready='0'");
llb52a49f2022-09-05 00:23:18 -0700227 }
228 else
229 {
ll892b6ef2022-09-08 06:45:34 -0700230 system("uci set lynq_uci.sdk_ready='2'"); //Md configurations are inconsistent with AP configurations
llb52a49f2022-09-05 00:23:18 -0700231 }
232 RLOGD("__FUNCTION__ %s res_error %d\n", __FUNCTION__, res_error);
233 /*judge the res_error*/
234 pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
ll2f5e1c82022-09-07 00:01:43 -0700235 sleep(TIMER);
llb52a49f2022-09-05 00:23:18 -0700236 }
237 return NULL;
238}
239
240
241void start_timer_request(void)
242{
243 pthread_t thid;
244 pthread_attr_t a;
245 pthread_attr_init(&a);
246 pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
247 int ret = pthread_create(&thid, &a, timer_request_imei, NULL);
248 if(ret != 0){
249 RLOGD("pthread_create error!!!");
250 return;
251 }
252 return;
253}
254