blob: fa71d82a1d6621f13a90bef840d45dabf132b917 [file] [log] [blame]
q.huang8878f362022-10-20 15:25:45 +08001#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 <list>
12#include <vendor-ril/telephony/ril.h>
13#include <vendor-ril/telephony/mtk_ril_sp.h>
14#include "lynq_network.h"
15#include "lynq_module_common.h"
16#include "lynq_module_socket.h"
17#include "liblog/lynq_deflog.h"
18
19#define LYNQ_SERVICE_PORT 8088
20#define LYNQ_ADDRESS "127.0.0.1"
21#define LYNQ_URC_SERVICE_PORT 8086
22#define LYNQ_URC_ADDRESS "0.0.0.0"
23#define LYNQ_REQUEST_PARAM_BUF 8192
24#define LYNQ_REC_BUF 8192
25
26#define USER_LOG_TAG "LYNQ_NETWORK"
27
28typedef struct{
29 int uToken;
30 int request;
31 int paramLen;
32 char param[LYNQ_REQUEST_PARAM_BUF];
33}lynq_client_t;
34
35typedef struct{
36 int resp_type;
37 int token;
38 int request;
39 int slot_id;
40 int error;
41}lynq_resp_t;
42
43lynq_client_t client_t;
44lynq_resp_t response;
45
46int module_len_rc_addr_serv;
47int module_len_urc_addr_serv;
48struct sockaddr_in module_rc_addr_serv;
49struct sockaddr_in module_urc_addr_serv;
50static int module_rc_sock_fd = -1;
51static int module_urc_sock_fd = -1;
52int module_urc_status = 1;
53int module_rc_status = 1;
54pthread_t module_urc_tid = -1;
55pthread_t module_rc_tid = -1;
56static pthread_mutex_t s_urc_mutex = PTHREAD_MUTEX_INITIALIZER;
57
58/*hq add for set waiting time 2022/09/13 begin*/
59static pthread_mutex_t s_sendto_mutex = PTHREAD_MUTEX_INITIALIZER;
60static pthread_mutex_t s_RecvMsgBlockMutex = PTHREAD_MUTEX_INITIALIZER;
61static pthread_cond_t s_WaitRecvMsgCond = PTHREAD_COND_INITIALIZER;
62#define BLOCK_RECV_MSG_LOCK() pthread_mutex_lock(&s_RecvMsgBlockMutex)
63#define BLOCK_RECV_MSG_UNLOCK() pthread_mutex_unlock(&s_RecvMsgBlockMutex)
64#define BLOCK_WAIT_RECV_MSG_TIME_OUT(a) pthread_cond_timedwait(&s_WaitRecvMsgCond, &s_RecvMsgBlockMutex,(a))
65#define BLOCK_WAKEUP_RECV_MSG() pthread_cond_broadcast(&s_WaitRecvMsgCond)
66static std::list<Parcel*> s_recv_parcel_list;
67const int waitResponse(int token,int time_out);
68/*hq add for set waiting time 2022/09/13 end*/
69
70int g_module_Global_uToken = 0;
71int g_wait_time=5;
72
73/*inner test*/
74static int s_inner_test=0;
75
76/**@brief just for inner test
77* @param test_mode [IN]: test mode
78* 0:success
79* other:failure
80*/
81void lynq_set_test_mode(const int test_mode)
82{
83 if(test_mode<0)
84 {
85 g_wait_time = -test_mode;
86 }
87 else if(test_mode==9999)
88 {
89 LYERRLOG("%s inner test para %d %d",__func__,s_inner_test,g_wait_time);
90 }
91 else
92 {
93 s_inner_test = test_mode;
94 }
95}
96
97void cleanup_RecvMsgBlock_mutex(void *arg)
98{
99 BLOCK_RECV_MSG_UNLOCK();
100}
101
102/**@brief wait response with expected token and write msg to parcel in some time
103* @param p [IN]: pointer the parcel
104* @param token [IN]: the expected token for the response msg
105* @param time_out [IN]: timeout.
106* @return
107* 0:success
108* other:failure
109*/
110const int waitResponse(Parcel*& p, int token,int time_out)
111{
112 int waitToken = token;
113 int wakeup_token=-1;
114 int resp_type;
115 struct timeval now;
116 struct timespec timeout;
117 int ret;
118 std::list<Parcel*>::iterator iter;
119 int cnt=0;
120
121 gettimeofday(&now,NULL);
122 timeout.tv_sec = now.tv_sec+time_out; //timeout is 1min
123 timeout.tv_nsec = now.tv_usec*1000;
124
125 LYINFLOG("%s wait token is %d, wait time is %d",__func__,waitToken,time_out);
126
127 BLOCK_RECV_MSG_LOCK();
128 pthread_cleanup_push(cleanup_RecvMsgBlock_mutex, NULL); // thread cleanup handler
129 while(module_rc_status) {
130 cnt++;
131 for(iter=s_recv_parcel_list.begin();iter!=s_recv_parcel_list.end();++iter)
132 {
133 (*iter)->setDataPosition(0);
134 (*iter)->readInt32(&resp_type);
135 (*iter)->readInt32(&wakeup_token);
136 if(wakeup_token==waitToken)
137 {
138 LYINFLOG("%s get waitToken",__func__);
139 p=(*iter);
140 p->setDataPosition(0);
141 s_recv_parcel_list.erase(iter);
142 goto waitResponse_end;
143 }
144 }
145 LYINFLOG("%s no wait Token in msg list, list size is %d",__func__,s_recv_parcel_list.size());
146 ret=BLOCK_WAIT_RECV_MSG_TIME_OUT(&timeout);
147 if(ret!=0)
148 {
149 LYERRLOG("%s no expected token %d after %d second",__func__,waitToken,time_out);
150 break;
151 }
152 }
153waitResponse_end:
154 LYINFLOG("%s wait token is %d wakeup_token is %d, cnt is %d",__func__,waitToken,wakeup_token,cnt);
155 pthread_cleanup_pop(0);
156 BLOCK_RECV_MSG_UNLOCK();
157 return wakeup_token==waitToken ? 0:LYNQ_E_TIME_OUT;
158}
159
160/**@brief print solicied response msg's head information
161* @param head [IN]: head information
162* @return none
163*/
164void PrintHeader(lynq_resp_t& head)
165{
166 LYINFLOG("resp_type=%d,token=%d,request=%d,slot_id=%d,error_code=%d",head.resp_type,head.token,head.request,head.slot_id,head.error);
167}
168
169/**@brief get solicied response msg's head
170* @param head [OUT]: head information
171* @return
172* 0:success
173* other:failure
174*/
175int GetHeader(Parcel* &p, lynq_resp_t& head)
176{
177 LYINFLOG("get header");
178 if(p->dataAvail() > 0)
179 {
180 p->readInt32(&(head.resp_type));
181 p->readInt32(&(head.token));
182 p->readInt32(&(head.request));
183 p->readInt32(&(head.slot_id));
184 p->readInt32(&(head.error));
185 PrintHeader(head);
186 return RESULT_OK;
187 }
188 else
189 {
190 return RESULT_ERROR;
191 }
192}
193
194/**@brief send msg to service and get response from service
195* @param request_id [IN]: request id
196* @param time_out [IN]: wait time uplimit
197* @param p [IN]: point to response msg's parcel
198* @param argc [IN]: how many parameters in parameter string
199* @param format [IN]: parameter string's format
200* @param ... [IN]: the specific parameter
201* @return
202* 0:success
203* other:failure
204*/
205int lynq_send_common_request(Parcel*& p, int time_out, int request_id, int argc, const char* format,...)
206{
207 lynq_client_t client;
208 int ret;
209 int send_num;
210
211 client.uToken = g_module_Global_uToken;
212 g_module_Global_uToken=(g_module_Global_uToken+1)%10000;/*0-10000*/
213 client.request = request_id;
214 client.paramLen = argc;
215 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
216 if(argc!=0)
217 {
218 va_list args;
219 va_start(args, format);
220 vsnprintf(client.param, LYNQ_REQUEST_PARAM_BUF, format, args);
221 va_end(args);
222 }
223 LYINFLOG("uToken=%d,request=%d,paralen=%d,param=%s",client.uToken,client.request,client.paramLen,client.param);
224 pthread_mutex_lock(&s_sendto_mutex);
225 if(s_inner_test==1)
226 {
227 send_num = 1;
228 }
229 else
230 {
231 send_num = sendto(module_rc_sock_fd,&client,sizeof(client),0,(struct sockaddr *)&module_rc_addr_serv,module_len_rc_addr_serv);
232 }
233 if(s_inner_test>=1000)
234 {
235 time_out = s_inner_test-1000;
236 }
237 pthread_mutex_unlock(&s_sendto_mutex);
238
239 if(send_num <= 0)
240 {
241 LYERRLOG("send request fail, send num is %d", send_num);
242 return LYNQ_E_SEND_REQUEST_FAIL;
243 }
244 ret = waitResponse(p, client.uToken,time_out);
245
246 if(ret==0)
247 {
248 lynq_resp_t head;
249 ret=GetHeader(p,head);
250 if(ret!=0)
251 {
252 LYERRLOG("%s %d get head error %d",__func__,client.uToken,ret);
253 delete p;
254 return LYNQ_E_GET_HEAD_ERROR;
255 }
256 if(head.error!=0)
257 {
258 LYERRLOG("%s %d mdm return head error %d",__func__,client.uToken,head.error);
259 delete p;
260 return head.error;
261 }
262 LYERRLOG("%s %d suc",__func__,client.uToken);
263 return RESULT_OK;
264 }
265
266 LYERRLOG("%s %d fail, ret is %d",__func__,client.uToken,ret);
267
268 return ret;
269}
270
271/**@ a thread just for recv\buffer solicited msg's response and notice waiting thread
272* @param p [IN]: no meaning
273* @return
274* always null
275*/
276void *thread_rc_recv(void *p)
277{
278 Parcel* rc_p;
279 std::list<Parcel*>::iterator iter;
280 int resp_type = -1;
281 char rc_data[LYNQ_REC_BUF];
282 int rc_len;
283 int null_cnt=0;
284 int wakeup_token;
285
286 LYINFLOG("rc thread is running");
287 while(module_rc_status)
288 {
289 bzero(rc_data,LYNQ_REC_BUF);
290 while(true)
291 {
292 rc_len = recvfrom(module_rc_sock_fd,rc_data,sizeof(rc_data),0,(struct sockaddr *)&module_rc_addr_serv,(socklen_t *)&module_len_rc_addr_serv);
293 if(rc_len<sizeof(int32_t)*2)
294 {
295 LYERRLOG("%s recv len %d less %d",__func__, rc_len,sizeof(int)*2);
296 continue;
297 }
298 rc_p= new Parcel;
299 if(rc_p==NULL)
300 {
301 null_cnt++;
302 LYERRLOG("%s rc_p is NULL, cnt is %d",__func__, null_cnt);
303 if(null_cnt>20)
304 {
305 goto rc_recv_end;
306 }
307 continue;
308 }
309 else
310 {
311 null_cnt=0;
312 }
313
314 rc_p->setData((uint8_t *)rc_data,rc_len); // p.setData((uint8_t *) buffer, buflen);
315 rc_p->setDataPosition(0);
316 if(rc_p->dataAvail()>0)
317 {
318 rc_p->readInt32(&resp_type);
319 rc_p->readInt32(&wakeup_token);
320 BLOCK_RECV_MSG_LOCK();
321 s_recv_parcel_list.push_back(rc_p);
322 LYINFLOG("%s wakeup token is %d, list size is %d!",__func__,wakeup_token,s_recv_parcel_list.size());
323 if(s_recv_parcel_list.size()>20) //max 20
324 {
325 iter=s_recv_parcel_list.begin();
326 (*iter)->setDataPosition(0);
327 (*iter)->readInt32(&resp_type);
328 (*iter)->readInt32(&wakeup_token);
329 delete (*(s_recv_parcel_list.begin()));
330 LYERRLOG("%s wakeup token %d is deleted!",__func__,wakeup_token);
331 s_recv_parcel_list.erase(iter);
332 }
333 BLOCK_WAKEUP_RECV_MSG();
334 BLOCK_RECV_MSG_UNLOCK();
335 break;
336 }
337 else
338 {
339 LYERRLOG("%s rc_p data Avail %d not greater than 0",__func__, rc_p->dataAvail());
340 delete rc_p;
341 }
342 }
343 }
344
345rc_recv_end:
346 LYINFLOG("rc thread ended");
347 return NULL;
348}
349
350void *thread_urc_recv(void *p)
351{
352 Parcel *urc_p =NULL;
353 char urc_data[LYNQ_REC_BUF];
354 int res = 0;
355
356 LYINFLOG("urc thread is running");
357 while(module_urc_status)
358 {
359 bzero(urc_data,LYNQ_REC_BUF);
360 res = recvfrom(module_urc_sock_fd,urc_data,sizeof(urc_data),0,(struct sockaddr *)&module_urc_addr_serv,(socklen_t*)&module_len_urc_addr_serv);
361 if(res<=0)
362 {
363 LYERRLOG("thread_urc_recv step2 fail:");
364 break;
365 }
366 urc_p = new Parcel();
367 if(urc_p == NULL)
368 {
369 LYERRLOG("new parcel failure!!!");
370 break;
371 }
372 urc_p->setData((uint8_t *)urc_data,res); // p.setData((uint8_t *) buffer, buflen);
373 urc_p->setDataPosition(0);
374 if(urc_p->dataAvail()>0)
375 {
376 pthread_mutex_lock(&s_urc_mutex);
377 urc_msg_process(urc_p);
378 pthread_mutex_unlock(&s_urc_mutex);
379 }
380 delete urc_p;
381 urc_p = NULL;
382 }
383 LYINFLOG("urc thread ended");
384 return NULL;
385}
386
387int lynq_server_socket_start()
388{
389 module_rc_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
390 if(module_rc_sock_fd < 0)
391 {
392 LYERRLOG("socket open error");
393 return -1;
394 }
395
396 LYINFLOG("module_rc_sock_fd = %d",module_rc_sock_fd);
397
398 memset(&module_rc_addr_serv, 0, sizeof(module_rc_addr_serv));
399 module_rc_addr_serv.sin_family = AF_INET;
400 module_rc_addr_serv.sin_addr.s_addr = inet_addr(LYNQ_ADDRESS);
401 module_rc_addr_serv.sin_port = htons(LYNQ_SERVICE_PORT);
402 module_len_rc_addr_serv = sizeof(module_rc_addr_serv);
403
404 BLOCK_RECV_MSG_LOCK();
405 std::list<Parcel*>::iterator iter;
406 for(iter=s_recv_parcel_list.begin();iter!=s_recv_parcel_list.end();++iter)
407 {
408 delete (*iter);
409 }
410 s_recv_parcel_list.clear();
411 BLOCK_RECV_MSG_UNLOCK();
412
413// pthread_attr_t attr;
414 int ret;
415
416 pthread_mutex_init(&s_sendto_mutex, NULL);
417 pthread_mutex_init(&s_RecvMsgBlockMutex, NULL);
418
419 module_rc_status = 1;
420// pthread_attr_init(&attr);
421// pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
422 ret = pthread_create(&module_rc_tid,/*&attr*/NULL,thread_rc_recv,NULL);
423 if(ret <0)
424 {
425 LYERRLOG("rc pthread create error");
426 module_rc_status =0;
427 close(module_rc_sock_fd);
428 module_rc_sock_fd =-1;
429 return ret;
430 }
431
432 LYINFLOG("rc start success");
433
434 return RESULT_OK;
435}
436
437int lynq_urc_socket_start()
438{
439// pthread_t tid;
440// pthread_attr_t attr;
441 int on = 1;
442 int ret = 0;
443 module_len_urc_addr_serv = sizeof(sockaddr_in);
444 module_urc_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
445 if (module_urc_sock_fd <0){
446 LYERRLOG("urc socket error");
447 return RESULT_ERROR;
448 }
449 module_urc_addr_serv.sin_family = AF_INET;
450 module_urc_addr_serv.sin_port = htons(LYNQ_URC_SERVICE_PORT);
451 module_urc_addr_serv.sin_addr.s_addr = inet_addr(LYNQ_URC_ADDRESS);
452 /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
453 ret = setsockopt(module_urc_sock_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
454 if(ret <0)
455 {
456 LYERRLOG("urc socket set error");
457 close(module_urc_sock_fd);
458 module_urc_sock_fd =-1;
459 return RESULT_ERROR;
460 }
461 ret = bind(module_urc_sock_fd ,(struct sockaddr*)&module_urc_addr_serv, sizeof(module_urc_addr_serv));
462 if(ret <0)
463 {
464 LYERRLOG("urc socket bind error");
465 close(module_urc_sock_fd);
466 module_urc_sock_fd =-1;
467 return RESULT_ERROR;
468 }
469
470 module_urc_status = 1;
471 // pthread_attr_init(&attr);
472 // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
473 ret = pthread_create(&module_urc_tid,/*&attr*/NULL,thread_urc_recv,NULL);
474 if(ret <0)
475 {
476 LYERRLOG("urc pthread create error");
477 module_urc_status = 0;
478 close(module_urc_sock_fd);
479 module_urc_sock_fd =-1;
480 return RESULT_ERROR;
481 }
482 LYINFLOG("urc start success");
483
484 return RESULT_OK;
485}
486
487void lynq_close_urc_thread()
488{
489 int ret;
490
491 pthread_mutex_lock(&s_urc_mutex); //just cancel urc tid when recvfrom avoid mutual lock for tid may call pthread_cond_wait
492 module_urc_status = 0;
493 if(module_urc_tid!=-1)
494 {
495 ret = pthread_cancel(module_urc_tid);
496 LYINFLOG("pthread cancel urc ret = %d",ret);
497 }
498 pthread_mutex_unlock(&s_urc_mutex);
499 if(module_urc_tid != -1)
500 {
501 ret = pthread_join(module_urc_tid,NULL);
502 LYINFLOG("pthread join urc tid ret = %d",ret);
503 module_urc_tid =-1;
504 }
505 if (module_urc_sock_fd > 0)
506 {
507 close(module_urc_sock_fd);
508 module_urc_sock_fd =-1;
509 }
510}
511
512void lynq_close_rc_thread()
513{
514 int ret;
515 BLOCK_RECV_MSG_LOCK();
516 module_rc_status = 0;
517 BLOCK_WAKEUP_RECV_MSG();
518 if(module_rc_tid != -1)
519 {
520 ret = pthread_cancel(module_rc_tid);
521 LYINFLOG("pthread cancel rc ret = %d",ret);
522 }
523 BLOCK_RECV_MSG_UNLOCK();
524 if(module_rc_tid != -1)
525 {
526 ret = pthread_join(module_rc_tid,NULL);
527 module_rc_tid =-1;
528 LYINFLOG("pthread join rc tid ret = %d",ret);
529
530 }
531
532 if (module_rc_sock_fd > 0)
533 {
534 close(module_rc_sock_fd);
535 module_rc_sock_fd =-1;
536 }
537
538 BLOCK_RECV_MSG_LOCK();
539 std::list<Parcel*>::iterator iter;
540 for(iter=s_recv_parcel_list.begin();iter!=s_recv_parcel_list.end();++iter)
541 {
542 delete (*iter);
543 }
544 s_recv_parcel_list.clear();
545 BLOCK_RECV_MSG_UNLOCK();
546}
547