blob: 5443df3e9cfc9ca786c6721b46e19f26de6530af [file] [log] [blame]
lh7b0674a2022-01-10 00:34:35 -08001#include <stdio.h>
2#include <sys/types.h>
3#include <sys/socket.h>
4#include <arpa/inet.h>
5#include <fcntl.h>
6#include <string.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <binder/Parcel.h>
10#include <log/log.h>
11#include "lynq_call.h"
12#include <cutils/jstring.h>
13#include <pthread.h>
14#include "liblog/lynq_deflog.h"
lh2afc7732022-01-10 02:24:31 -080015#include <sys/time.h>
lh21502f52022-01-27 00:27:12 -080016#include <string.h>
lh7b0674a2022-01-10 00:34:35 -080017#define LYNQ_SERVICE_PORT 8088
18#define LYNQ_URC_SERVICE_PORT 8086
19#define LYNQ_REC_BUF 8192
20#define LYNQ_REQUEST_PARAM_BUF 8192
21#define LYQN_SEDN_BUF 1024*8+sizeof(int)*3
22#define USER_LOG_TAG "LYNQ_CALL"
23
24using ::android::Parcel;
25 typedef enum {
26 LYNQ_CALL_ACTIVE = 0,
27 LYNQ_CALL_HOLDING = 1,
28 LYNQ_CALL_DIALING = 2, /* MO call only */
29 LYNQ_CALL_ALERTING = 3, /* MO call only */
30 LYNQ_CALL_INCOMING = 4, /* MT call only */
31 LYNQ_CALL_WAITING = 5 ,/* MT call only */
32}lynq_call_state_t;
33
34typedef struct{
35 int uToken;
36 int request;
37 int paramLen;
38 char param[LYNQ_REQUEST_PARAM_BUF];
39}lynq_client_t;
40typedef struct
41{
42 int used;
43 int call_id;
44 int call_state;
45 int toa;
46 int direction;/*0: MO call,1:MT call*/
47 char addr[LYNQ_PHONE_NUMBER_MAX];
48 int hasTimeout;
49}lynq_call_list_e_t;
50typedef struct
51{
52 int call_id;
53 int call_state;
54 int toa;
55 int direction;/*0: MO call,1:MT call*/
56 char addr[LYNQ_PHONE_NUMBER_MAX];
57}lynq_call_list_t;
58
59lynq_call_list_e_t lynq_call_lists[LYNQ_CALL_MAX]={};
60static pthread_mutex_t call_state_change_mutex = PTHREAD_MUTEX_INITIALIZER;
61static pthread_cond_t call_state_change_cond = PTHREAD_COND_INITIALIZER;
ll04ae4142022-01-27 05:54:38 +000062static pthread_mutex_t s_urc_call_state_change_mutex = PTHREAD_MUTEX_INITIALIZER;
lh7b0674a2022-01-10 00:34:35 -080063static pthread_cond_t s_urc_call_state_change_cond = PTHREAD_COND_INITIALIZER;
64static pthread_mutex_t s_incoming_call_mutex = PTHREAD_MUTEX_INITIALIZER;
65static pthread_cond_t s_incoming_call_cond = PTHREAD_COND_INITIALIZER;
lhec17b0a2022-02-13 23:56:05 -080066pthread_t lynq_call_urc_tid;
67pthread_t lynq_call_list_loop_tid;
68
lh7b0674a2022-01-10 00:34:35 -080069
70
lh42c1e572022-01-25 18:47:39 -080071int s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -080072
73enum{
74 CALL_OFF=0,
75 CALL_ON=1
76}call_state;
ll04ae4142022-01-27 05:54:38 +000077typedef enum{
78 LYNQ_E_CARDSTATE_ERROR=8000,
79 /* The voice service state is out of service*/
80 LYNQ_E_STATE_OUT_OF_SERVICE=8001,
81 /* The voice service state is EMERGENCY_ONLY*/
82 LYNQ_E_STATE_EMERGENCY_ONLY=8002,
83 /* The radio power is power off*/
84 LYNQ_E_STATE_POWER_OFF=8003,
85 LYNQ_E_TIME_OUT=8004,
86 /*create or open sms DB fail */
87 LYNQ_E_SMS_DB_FAIL=8005,
88 /*Failed to execute sql statement*/
89 LYNQ_E_SMS_SQL_FAIL = 8006,
90 LYNQ_E_SMS_NOT_FIND = 8007,
91 /* The logic conflict*/
92 LYNQ_E_CONFLICT=9000,
93 /*Null anomaly*/
94 LYNQ_E_NULL_ANONALY=9001
lh7b0674a2022-01-10 00:34:35 -080095}LYNQ_E;
96
97int lynq_call_state =CALL_OFF;
lh42c1e572022-01-25 18:47:39 -080098int lynq_call_client_sockfd = 0;
99int Global_uToken_call = 0;
lh7b0674a2022-01-10 00:34:35 -0800100int global_call_count =0;
101int global_call_auto_answer = 0;
lh42c1e572022-01-25 18:47:39 -0800102bool urc_call_recive_status = 1;
lh7b0674a2022-01-10 00:34:35 -0800103bool call_list_loop = 1;
104int isDial = 0;
105int lynqIncomingCallId = 0;
106
107int JumpHeader(Parcel &p,int *resp_type,int *request,int *slot_id,int *error)
108{
109 if(p.dataAvail() > 0)
110 {
111 p.readInt32(resp_type);
112 p.readInt32(request);
113 p.readInt32(slot_id);
114 p.readInt32(error);
115 return 0;
116 }
117 else
118 {
119 return -1;
120 }
121}
122int send_request(int sockfd,lynq_client_t *client_tmp)
123{
124 int ret=0;
125 ret = write(sockfd, client_tmp, LYQN_SEDN_BUF);
126 if(ret==-1)
127 {
lh21502f52022-01-27 00:27:12 -0800128 LYERRLOG("write error:%s",strerror(errno));
lh7b0674a2022-01-10 00:34:35 -0800129 return -1;
130 }
131 return 0;
132}
133
134int get_response(int sockfd,Parcel &p)
135{
136 int len = 0;
137 char recvline[LYNQ_REC_BUF];
138 bzero(recvline,LYNQ_REC_BUF);
139 /* receive data from server */
140 len = read(sockfd, recvline, LYNQ_REC_BUF);
141 if(len == -1)
142 {
lh21502f52022-01-27 00:27:12 -0800143 LYERRLOG("read error:%s",strerror(errno));
lh7b0674a2022-01-10 00:34:35 -0800144 return -1;
145 }
lh7b0674a2022-01-10 00:34:35 -0800146 if (recvline != NULL) {
147 p.setData((uint8_t *)recvline,len); // p.setData((uint8_t *) buffer, buflen);
148 p.setDataPosition(0);
149 }
150 return 0;
151}
152static char *strdupReadString(Parcel &p) {
ll04ae4142022-01-27 05:54:38 +0000153 size_t stringlen;
lh7b0674a2022-01-10 00:34:35 -0800154 const char16_t *s16;
155 s16 = p.readString16Inplace(&stringlen);
ll04ae4142022-01-27 05:54:38 +0000156 return strndup16to8(s16, stringlen);
lh7b0674a2022-01-10 00:34:35 -0800157}
158
159int lynq_get_current_call_list(lynq_call_list_t call_list[LYNQ_CALL_MAX])
160{
161 Parcel p;
162 lynq_client_t client;
163 int resp_type = -1;
164 int request = -1;
165 int slot_id = -1;
166 int error = -1;
167 int call_num = 0;
168 int temp = 0;
169 char *remote_phoneNum = NULL;
170 char *remote_name= NULL;
171 char *uusData = NULL;
lh42c1e572022-01-25 18:47:39 -0800172 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800173 client.request = 9;//RIL_REQUEST_GET_CURRENT_CALLS
174 client.paramLen = 0;
175 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
176 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800177 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800178 {
179 LYERRLOG("send request fail");
180 perror("[LYNQ_CALL] send request fail:");
181 return -1;
182 }
lh42c1e572022-01-25 18:47:39 -0800183 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800184 JumpHeader(p,&resp_type,&request,&slot_id,&error);
185 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
186 if(error == 0)
187 {
188 p.readInt32(&call_num);
189 global_call_count = call_num;
190 if(call_num<=0)
191 {
192 lynq_call_state = CALL_OFF;
lhec17b0a2022-02-13 23:56:05 -0800193 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800194 return 0;
195 }
lhec17b0a2022-02-13 23:56:05 -0800196 lynq_call_state = CALL_ON;
197 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800198 for(int i = 0;i < call_num;i++)
199 {
200 p.readInt32(&temp);
201 call_list[i].call_state = temp;
202 p.readInt32(&call_list[i].call_id);
203 p.readInt32(&call_list[i].toa);
204 p.readInt32(&temp);
205 p.readInt32(&temp);
206 call_list[i].direction = temp;
207 p.readInt32(&temp);
208 p.readInt32(&temp);
209 p.readInt32(&temp);
210 remote_phoneNum = strdupReadString(p);
211 memcpy(call_list[i].addr,remote_phoneNum,strlen(remote_phoneNum));
212 LYINFLOG("call_id=%d,call_state=%d,direction=%d,addr=%s,toa=%d",call_list[i].call_id,call_list[i].call_state,
213 call_list[i].direction,call_list[i].addr,call_list[i].toa);
214 p.readInt32(&temp);
215 remote_name = strdupReadString(p);
216 p.readInt32(&temp);
217 p.readInt32(&temp);
218 if(temp==0)
219 {
220 continue;
221 }
222 p.readInt32(&temp); /* UUS Information is present */
223 p.readInt32(&temp);
224 p.readInt32(&temp);
225 p.read(uusData,temp);
226 }
227 }
228 return 0;
229}
230
231void cleanCallList(int lynq_call_id)
232{
233 lynq_call_lists[lynq_call_id].call_id = 0;
234 lynq_call_lists[lynq_call_id].call_state = 0;
235 lynq_call_lists[lynq_call_id].toa = 0;
236 lynq_call_lists[lynq_call_id].direction = 0;
237 lynq_call_lists[lynq_call_id].used = 0;
238 lynq_call_lists[lynq_call_id].hasTimeout = 0;
239 memset(lynq_call_lists[lynq_call_id].addr,0,sizeof(lynq_call_lists[lynq_call_id].addr));
240}
241int getUnusedElement()
242{
243 for(int i=0;i < LYNQ_CALL_MAX; i++)
244 {
245 if(lynq_call_lists[i].used!=1)
246 {
247 return i;
248 }
249 }
250 return -1;
251}
252int updateAddr(char addr[])
253{
254 int ret = 0;
255 ret = getUnusedElement();
256 memcpy(lynq_call_lists[ret].addr,addr,strlen(addr)+1);
257 lynq_call_lists[ret].used = 1;
258 return ret;
259}
260void updateCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction)
261{
262 callList->call_id = call_id;
263 callList->call_state = call_state;
264 callList->toa = toa;
265 callList->direction = direction;
266 callList->used = 1;
267 callList->hasTimeout = 0;
268 return;
269}
270int waitCallstateChange(int mtime)
271{
272 int ret = 0;
273 int sec = 0;
274 int usec = 0;
lh2afc7732022-01-10 02:24:31 -0800275 struct timeval now;
276 struct timespec timeout;
277 gettimeofday(&now,NULL);
lh7b0674a2022-01-10 00:34:35 -0800278 sec = mtime/1000;
279 usec = mtime%1000;
lh2afc7732022-01-10 02:24:31 -0800280 timeout.tv_sec = now.tv_sec+sec;
281 timeout.tv_nsec = now.tv_usec*1000+usec*1000000;
lh7b0674a2022-01-10 00:34:35 -0800282 pthread_mutex_lock(&call_state_change_mutex);
lh2afc7732022-01-10 02:24:31 -0800283 ret = pthread_cond_timedwait(&call_state_change_cond,&call_state_change_mutex,&timeout);
lh7b0674a2022-01-10 00:34:35 -0800284 pthread_mutex_unlock(&call_state_change_mutex);
285 return ret;
286}
287int waitIncomingCall()
288{
289 int ret = 0;
290 pthread_mutex_lock(&s_incoming_call_mutex);
291 ret = pthread_cond_wait(&s_incoming_call_cond,&s_incoming_call_mutex);
292 pthread_mutex_unlock(&s_incoming_call_mutex);
293 return ret;
294}
295int checkHasCall(char addr[])
296{
297 for(int i = 0;i<LYNQ_CALL_MAX;i++)
298 {
299 if(strcmp(lynq_call_lists[i].addr,addr)==0)
300 {
301 return 1;
302 }
303 }
304 return 0;
305}
306void sendSignalToWaitCallStateChange()
307{
308 pthread_mutex_lock(&call_state_change_mutex);
309 pthread_cond_signal(&call_state_change_cond);
310 pthread_mutex_unlock(&call_state_change_mutex);
311}
312void sendSignalIncomingCall()
313{
314 pthread_mutex_lock(&s_incoming_call_mutex);
315 pthread_cond_signal(&s_incoming_call_cond);
316 pthread_mutex_unlock(&s_incoming_call_mutex);
317}
318
319void addCallListToLynqCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction,char addr[LYNQ_PHONE_NUMBER_MAX])
320{
321 callList->call_id = call_id;
322 callList->call_state = call_state;
323 callList->toa = toa;
324 callList->direction = direction;
325 memcpy(callList->addr,addr,strlen(addr)+1);
326 callList->used = 1;
327 callList->hasTimeout = 0;
328 return;
329}
330
331void *triggerGetCallList(void *parg)
ll04ae4142022-01-27 05:54:38 +0000332{
333 int ret=0;
334 lynq_call_list_t call_list[LYNQ_CALL_MAX];
lh7b0674a2022-01-10 00:34:35 -0800335 while(call_list_loop)
ll04ae4142022-01-27 05:54:38 +0000336 {
337 pthread_mutex_lock(&s_urc_call_state_change_mutex);
338 pthread_cond_wait(&s_urc_call_state_change_cond, &s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800339 LYDBGLOG("triggerGetCallList event!!!\n");
ll04ae4142022-01-27 05:54:38 +0000340 memset(call_list,0,sizeof(call_list));
341 ret = lynq_get_current_call_list(call_list);
342 if(ret != 0)
343 {
344 printf("get current call list failure!!!\n");
345 break;
346 }
lh7b0674a2022-01-10 00:34:35 -0800347 for(int i = 0;i < LYNQ_CALL_MAX;i++)
348 {
349 if(call_list[i].direction == 1)//MT call
350 {
351 if(call_list[i].call_state ==4)//LYNQ_CALL_INCOMING = 4, /* MT call only */
352 {
353 if(!checkHasCall(call_list[i].addr))
354 {
355 lynqIncomingCallId = getUnusedElement();
356 addCallListToLynqCallList(&lynq_call_lists[lynqIncomingCallId],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction,call_list[i].addr);
357 sendSignalIncomingCall();
358 }
359 }
360 }
361 else
362 {
363 if(call_list[i].call_id==0)
364 {
365 break;
366 }
367 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
368 {
369 if(lynq_call_lists[n].hasTimeout==1)
370 {
371 /*hangup call with id*/
372 lynq_call_hungup(&n);
373 lynq_call_lists[n].hasTimeout==0;
374 continue;
375 }
376 if(strcmp(call_list[i].addr,lynq_call_lists[n].addr)==0)
377 {
378 updateCallList(&lynq_call_lists[n],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction);
379 }
380 }
381 LYDBGLOG("[count:%d]call_id=%d,call_state=%d,direction=%d,addr=%s,toa=%d",i,call_list[i].call_id,call_list[i].call_state,
382 call_list[i].direction,call_list[i].addr,call_list[i].toa);
383 }
ll04ae4142022-01-27 05:54:38 +0000384 }
lh42c1e572022-01-25 18:47:39 -0800385 s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -0800386 if(isDial==1)
387 {
388 sendSignalToWaitCallStateChange();
389 isDial = 0;
390 }
ll04ae4142022-01-27 05:54:38 +0000391 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
392 }
393 return NULL;
lh7b0674a2022-01-10 00:34:35 -0800394}
395
396void lynqRespWatingEvent()
ll04ae4142022-01-27 05:54:38 +0000397{
398 if(s_call_urc_event_complete==1)
399 {
400 pthread_mutex_lock(&s_urc_call_state_change_mutex);
401 pthread_cond_signal(&s_urc_call_state_change_cond);
402 s_call_urc_event_complete = 0;
403 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800404 }
ll04ae4142022-01-27 05:54:38 +0000405 return;
lh7b0674a2022-01-10 00:34:35 -0800406}
407
408/*Warren add for T800 platform 2021/11/19 start*/
409int lynq_socket_client_start()
410{
411 struct sockaddr_in lynq_socket_server_addr;
412 /* init lynq_socket_server_addr */
413 bzero(&lynq_socket_server_addr, sizeof(lynq_socket_server_addr));
414 lynq_socket_server_addr.sin_family = AF_INET;
415 lynq_socket_server_addr.sin_port = htons(LYNQ_SERVICE_PORT);
416 lynq_socket_server_addr.sin_addr.s_addr = htons(INADDR_ANY);
417 /*
418 if(inet_pton(AF_INET,"127.0.0.1", &lynq_socket_server_addr.sin_addr) <= 0)
419 {
420 printf("[%s] is not a valid IPaddress\n", argv[1]);
421 exit(1);
422 }
423*/
lh42c1e572022-01-25 18:47:39 -0800424 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
425 if(connect(lynq_call_client_sockfd, (struct sockaddr *)&lynq_socket_server_addr, sizeof(lynq_socket_server_addr)) == -1)
lh7b0674a2022-01-10 00:34:35 -0800426 {
lh21502f52022-01-27 00:27:12 -0800427 LYERRLOG("connect error:%s",strerror(errno));
lh7b0674a2022-01-10 00:34:35 -0800428 return -1;
429 }
430 return 0;
431}
432int lynq_update_call_list_loop()
433{
434 int ret = 0;
lh7b0674a2022-01-10 00:34:35 -0800435 pthread_attr_t attr;
436 pthread_attr_init(&attr);
437 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800438 ret = pthread_create(&lynq_call_list_loop_tid,&attr,triggerGetCallList,NULL);
lh7b0674a2022-01-10 00:34:35 -0800439 if(ret < 0)
440 {
441 LYERRLOG("lynq_update_call_list_loop fail!!!");
442 return -1;
443 }
444 LYDBGLOG("lynq_update_call_list_loop success!!!\n");
445 return 0;
446
447}
448void *thread_urc_recv(void *parg)
ll04ae4142022-01-27 05:54:38 +0000449{
lh7b0674a2022-01-10 00:34:35 -0800450 int socket_fd = (int64_t)parg;
451 int len=0;
452 socklen_t addr_len=0;
453 uint8_t *dataLength = NULL;
454 char urc_data[LYNQ_REC_BUF];
455 int slot_id = -1;
456 int resp_type = -1;
457 int urcid = -1;
458 Parcel *p = NULL;
459 struct sockaddr_in dest_addr;
460 LYINFLOG("thread_urc_recv in running....\n");
lh42c1e572022-01-25 18:47:39 -0800461 while(urc_call_recive_status)
lh7b0674a2022-01-10 00:34:35 -0800462 {
463 bzero(urc_data,LYNQ_REC_BUF);
464 //get data msg
465 len = recvfrom(socket_fd,urc_data,LYNQ_REC_BUF,0,(struct sockaddr *)&dest_addr,&addr_len);
466 if(len <= 0)
ll04ae4142022-01-27 05:54:38 +0000467 {
lh21502f52022-01-27 00:27:12 -0800468 LYERRLOG("thread_urc_recv step2 fail:%s",strerror(errno));
ll04ae4142022-01-27 05:54:38 +0000469 break;
lh7b0674a2022-01-10 00:34:35 -0800470 }
471 LYDBGLOG("=====>urc data len<=====:%d\n",len);
472 p = new Parcel();
473 if(p==NULL)
474 {
lh21502f52022-01-27 00:27:12 -0800475 LYERRLOG("new parcel failure!!!");
lh7b0674a2022-01-10 00:34:35 -0800476 break;
477 }
478 p->setData((uint8_t *)urc_data,len); // p.setData((uint8_t *) buffer, buflen);
479 p->setDataPosition(0);
480 if(p->dataAvail() > 0)
481 {
482 p->readInt32(&resp_type);
483 p->readInt32(&urcid);
484 p->readInt32(&slot_id);
485 //LYDBGLOG("*******Warren test*******:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
486 switch (urcid)
487 {
488 case 1001://RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED
489 {
490 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
491 lynqRespWatingEvent();
492 break;
493 }
494 case 1018://RIL_UNSOL_CALL_RING
495 {
496 if(global_call_auto_answer==1)
497 {
498 lynq_call_answer();
499 }
500 break;
501 }
502 case 1029://RIL_UNSOL_RINGBACK_TONE
503 case 3049://RIL_UNSOL_CALL_INFO_INDICATION
504 {
505 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
506 break;
507 }
508 default:
509 break;
510 }
511 }
512 delete p;
513 p = NULL;
514 }
515 close(socket_fd);
516}
517int lynq_socket_urc_start()
518{
519 int socket_fd=0;
520 int rt=0;
521 int len=0;
522 int on=1;
523 struct sockaddr_in urc_local_addr;
lh7b0674a2022-01-10 00:34:35 -0800524 pthread_attr_t attr;
ll04ae4142022-01-27 05:54:38 +0000525 socket_fd = socket(AF_INET,SOCK_DGRAM,0);
ll04ae4142022-01-27 05:54:38 +0000526 if(socket_fd < 0)
527 {
lh21502f52022-01-27 00:27:12 -0800528 LYERRLOG("creaet socket for udp fail:%s",strerror(errno));
ll04ae4142022-01-27 05:54:38 +0000529 return -1;
lh7b0674a2022-01-10 00:34:35 -0800530 }
531 urc_local_addr.sin_family = AF_INET;
532 urc_local_addr.sin_port = htons(LYNQ_URC_SERVICE_PORT);
533 urc_local_addr.sin_addr.s_addr = htons(INADDR_ANY);
534 /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
ll04ae4142022-01-27 05:54:38 +0000535 rt = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
536 if(rt<0)
537 {
lh21502f52022-01-27 00:27:12 -0800538 LYERRLOG("SO_REUSEADDR fail:%s",strerror(errno));
539 perror("SO_REUSEADDR fail:");
lh7b0674a2022-01-10 00:34:35 -0800540 return -1;
541 }
542 rt = bind(socket_fd ,(struct sockaddr*)&urc_local_addr, sizeof(urc_local_addr));
ll04ae4142022-01-27 05:54:38 +0000543 if (rt == -1)
544 {
lh21502f52022-01-27 00:27:12 -0800545 LYERRLOG("bind failed");
546 perror("bind failed:");
ll04ae4142022-01-27 05:54:38 +0000547 return -1;
lh7b0674a2022-01-10 00:34:35 -0800548 }
549 pthread_attr_init(&attr);
550 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800551 rt = pthread_create(&lynq_call_urc_tid,&attr,thread_urc_recv,(void *)socket_fd);
lh7b0674a2022-01-10 00:34:35 -0800552 if(rt < 0)
553 {
lh21502f52022-01-27 00:27:12 -0800554 LYERRLOG("urc loop failure!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800555 return -1;
556 }
lh21502f52022-01-27 00:27:12 -0800557 LYDBGLOG("urc loop success!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800558 return 0;
559}
560int getSelfElement(char addr[])
561{
562 for(int i=0;i < LYNQ_CALL_MAX; i++)
563 {
564 if(lynq_call_lists[i].used==1)
565 {
566 if(strcmp(lynq_call_lists[i].addr,addr)==0)
567 {
568 return i;
569 }
570
571 }
572 }
573 return -1;
574}
575
576void lynq_call_state_change_test(int soc_id)
577{
578 printf("call state change,sim:%d\n",soc_id);
579}
580int lynq_init_call(int uToken)
581{
582 int result = 0;
lh42c1e572022-01-25 18:47:39 -0800583 Global_uToken_call = uToken;
lh21502f52022-01-27 00:27:12 -0800584 urc_call_recive_status = 1;
lh7b0674a2022-01-10 00:34:35 -0800585 LYLOGSET(LOG_INFO);
586 LYLOGEINIT(USER_LOG_TAG);
587 result = lynq_socket_client_start();
588 if(result!=0)
589 {
590 return -1;
591 }
592 result = lynq_socket_urc_start();
593 if(result!=0)
594 {
595 return -1;
596 }
597 result = lynq_update_call_list_loop();
598 if(result!=0)
599 {
600 return -1;
601 }
602 memset(lynq_call_lists,0,sizeof(lynq_call_lists));
603 LYDBGLOG("lynq init call success!!!");
604 return 0;
605}
606int lynq_deinit_call()
607{
lhec17b0a2022-02-13 23:56:05 -0800608 int ret = -1;
609 if(lynq_call_client_sockfd>0)
610 {
611 close(lynq_call_client_sockfd);
612 }
lh42c1e572022-01-25 18:47:39 -0800613 urc_call_recive_status = 0;
lh7b0674a2022-01-10 00:34:35 -0800614 call_list_loop = 0;
lhec17b0a2022-02-13 23:56:05 -0800615 ret = pthread_cancel(lynq_call_urc_tid);
616 LYDBGLOG("pthread cancel ret = %d",ret);
617 ret = pthread_cancel(lynq_call_list_loop_tid);
618 LYDBGLOG("pthread cancel ret = %d",ret);
619 ret = pthread_join(lynq_call_urc_tid,NULL);
620 LYDBGLOG("pthread join ret = %d",ret);
621 ret = pthread_join(lynq_call_list_loop_tid,NULL);
622 LYDBGLOG("pthread join ret = %d",ret);
lh7b0674a2022-01-10 00:34:35 -0800623 return 0;
624}
625int lynq_call(int* handle,char addr[])
626{
627 Parcel p;
628 lynq_client_t client;
629 int resp_type = -1;
630 int request = -1;
631 int slot_id = -1;
632 int error = -1;
633 int lynq_call_id = -1;
634 if(addr==NULL)
635 {
636 LYERRLOG("Phone num is null!!!");
637 return -1;
638 }
lh42c1e572022-01-25 18:47:39 -0800639 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800640 client.request = 10;//RIL_REQUEST_DIAL
641 client.paramLen = 2;
642 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
643 memcpy(client.param,addr,strlen(addr)+1);
644 strcat(client.param," 0");
645 LYERRLOG("uToken=%d,request=%d,paralen=%d,param=%s",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800646 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800647 {
648 LYERRLOG("send request fail");
649 perror("[LYNQ_CALL] send request fail:");
650 return -1;
651 }
lh42c1e572022-01-25 18:47:39 -0800652 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800653 JumpHeader(p,&resp_type,&request,&slot_id,&error);
654 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
655 lynq_call_id = updateAddr(addr);
656 if(error==0)
657 {
658 isDial = 1;
659 if(waitCallstateChange(3000)==ETIMEDOUT)//3000ms
660 {
661 error = LYNQ_E_TIME_OUT;
662 LYERRLOG("timeout:wait Call state fail!!!");
663 lynq_call_lists[lynq_call_id].hasTimeout = 1;
664 return error;
665 }
666 *handle = lynq_call_id;
667 }
668 return error;
669}
670int lynq_call_answer()
671{
672 Parcel p;
673 lynq_client_t client;
674 int resp_type = -1;
675 int request = -1;
676 int slot_id = -1;
677 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800678 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800679 client.request = 40;//RIL_REQUEST_DIAL
680 client.paramLen = 0;
681 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
682 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800683 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800684 {
685 LYERRLOG("send request fail");
686 perror("[LYNQ_CALL] send request fail:");
687 return -1;
688 }
lh42c1e572022-01-25 18:47:39 -0800689 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800690 JumpHeader(p,&resp_type,&request,&slot_id,&error);
691 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
692 return error;
693}
694int lynq_call_hungup(int* handle)
695{
696 Parcel p;
697 lynq_client_t client;
698 int resp_type = -1;
699 int request = -1;
700 int slot_id = -1;
701 int error = -1;
702 int call_id = 0;
703 int lynq_call_id = 0;
lh42c1e572022-01-25 18:47:39 -0800704 if(handle==NULL||!((*handle>=0)&&(*handle<10)))
705 {
706 LYERRLOG("[%s] illegal input!!!!",__FUNCTION__);
707 return LYNQ_E_CONFLICT;
708 }
709 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800710 client.request = 12;//RIL_REQUEST_HUNGUP
711 client.paramLen = 1;
712 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
713 lynq_call_id = *handle;
714 call_id = lynq_call_lists[lynq_call_id].call_id;
715 sprintf(client.param,"%d",call_id);
716 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800717 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800718 {
719 LYERRLOG("send request fail");
720 perror("[LYNQ_CALL] send request fail:");
721 return -1;
722 }
lh42c1e572022-01-25 18:47:39 -0800723 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800724 JumpHeader(p,&resp_type,&request,&slot_id,&error);
725 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
726 if(error==0)
727 {
728 cleanCallList(lynq_call_id);
729 }
730 return error;
731}
732int lynq_call_hungup_all()
733{
734 Parcel p;
735 lynq_client_t client;
736 int resp_type = -1;
737 int request = -1;
738 int slot_id = -1;
739 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800740 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800741 client.request = 17;//RIL_REQUEST_UDUB
742 client.paramLen = 0;
743 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
744 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800745 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800746 {
747 LYERRLOG("send request fail");
748 perror("[LYNQ_CALL] send request fail:");
749 return -1;
750 }
lh42c1e572022-01-25 18:47:39 -0800751 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800752 JumpHeader(p,&resp_type,&request,&slot_id,&error);
753 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
754 return error;
755}
756int lynq_wait_incoming_call(int *handle)
757{
758 waitIncomingCall();
759 *handle = lynqIncomingCallId;
760 LYINFLOG("lynq incoming call id:%d",lynqIncomingCallId);
761 return 0;
762}
763
764int lynq_set_auto_answercall(const int mode)
765{
766 global_call_auto_answer = mode;
767 LYINFLOG("auto answer call mode =%d",mode);
768 return 0;
769}
770int lynq_get_mute_status(int *status)
771{
772 Parcel p;
773 lynq_client_t client;
774 int resp_type = -1;
775 int request = -1;
776 int slot_id = -1;
777 int error = -1;
778 if(status==NULL)
779 {
780 LYERRLOG("status is null");
781 return -1;
782 }
lh42c1e572022-01-25 18:47:39 -0800783 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800784 client.request = 54;//RIL_REQUEST_GET_MUTE
785 client.paramLen = 0;
786 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
787 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800788 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800789 {
790 LYERRLOG("send request fail");
791 perror("[LYNQ_CALL] send request fail:");
792 return -1;
793 }
lh42c1e572022-01-25 18:47:39 -0800794 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800795 JumpHeader(p,&resp_type,&request,&slot_id,&error);
796 p.readInt32(status);
797 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
798 return error;
799}
lldc99c9b2022-01-24 12:16:22 +0000800
801static int judge_mic(const int enable){
802 if(enable != 1 || enable != 0){
803 return 0;
804 }
ll04ae4142022-01-27 05:54:38 +0000805 return 1;
lldc99c9b2022-01-24 12:16:22 +0000806}
807
lh7b0674a2022-01-10 00:34:35 -0800808int lynq_set_mute_mic(const int enable)
lldc99c9b2022-01-24 12:16:22 +0000809{
810 if(!judge_mic(enable)){
811 return LYNQ_E_CONFLICT;
812 }
lh7b0674a2022-01-10 00:34:35 -0800813 Parcel p;
814 lynq_client_t client;
815 int resp_type = -1;
816 int request = -1;
817 int slot_id = -1;
818 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800819 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800820 client.request = 53;//RIL_REQUEST_SET_MUTE
821 client.paramLen = 1;
822 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
823 sprintf(client.param,"%d",enable);
824 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800825 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800826 {
827 LYERRLOG("send request fail");
828 perror("[LYNQ_CALL] send request fail:");
829 return -1;
830 }
lh42c1e572022-01-25 18:47:39 -0800831 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800832 JumpHeader(p,&resp_type,&request,&slot_id,&error);
833 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
834 return error;
835}
836int lynq_set_DTMF(const char callnum)
837{
838 Parcel p;
839 lynq_client_t client;
840 int resp_type = -1;
841 int request = -1;
842 int slot_id = -1;
843 int error = -1;
844 if(!lynq_call_state)
845 {
846 LYERRLOG("LYNQ_E_CONFLICT");
847 return LYNQ_E_CONFLICT;
848 }
lh42c1e572022-01-25 18:47:39 -0800849 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800850 client.request = 24;//RIL_REQUEST_DTMF
851 client.paramLen = 1;
852 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
853 sprintf(client.param,"%c",callnum);
854 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800855 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800856 {
857 LYERRLOG("send request fail");
858 perror("[LYNQ_CALL] send request fail:");
859 return -1;
860 }
lh42c1e572022-01-25 18:47:39 -0800861 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800862 JumpHeader(p,&resp_type,&request,&slot_id,&error);
863 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
864 return error;
865
866}
lldc99c9b2022-01-24 12:16:22 +0000867
868static int judge_volume(const int volume){
869 if(volume < 0 ||volume >36){
870 return 0;
871 }
ll04ae4142022-01-27 05:54:38 +0000872 return 1;
lldc99c9b2022-01-24 12:16:22 +0000873}
874
lh7b0674a2022-01-10 00:34:35 -0800875int lynq_set_DTMF_volume(const int volume)
lldc99c9b2022-01-24 12:16:22 +0000876{
877 if(!judge_volume(volume)){
878 return LYNQ_E_CONFLICT;
879 }
lh7b0674a2022-01-10 00:34:35 -0800880 Parcel p;
881 lynq_client_t client;
882 int resp_type = -1;
883 int request = -1;
884 int slot_id = -1;
885 int error = -1;
886 //if(!lynq_call_state)
887 //{
888 // LYERRLOG("LYNQ_E_CONFLICT");
889 // return LYNQ_E_CONFLICT;
890 //}
lh42c1e572022-01-25 18:47:39 -0800891 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800892 client.request = 8003;//LYNQ_REQUEST_SET_DTMF_VOLUME
893 client.paramLen = 1;
894 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
895 sprintf(client.param,"%d",volume);
896 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800897 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800898 {
899 LYERRLOG("send request fail");
900 perror("[LYNQ_CALL] send request fail:");
901 return -1;
902 }
lh42c1e572022-01-25 18:47:39 -0800903 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800904 JumpHeader(p,&resp_type,&request,&slot_id,&error);
905 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
906 return 0;
907}
908int lynq_get_current_call_state(int *handle, int *call_state,int *toa,int *direction,char addr[])
909{
910 int lynq_call_id = 0;
911 if(handle==NULL)
912 {
913 return LYNQ_E_NULL_ANONALY;
914 }
915 lynq_call_id = *handle;
916 *call_state = lynq_call_lists[lynq_call_id].call_state;
917 *toa = lynq_call_lists[lynq_call_id].toa;
918 *direction = lynq_call_lists[lynq_call_id].direction;
919 memcpy(addr,lynq_call_lists[lynq_call_id].addr,strlen(lynq_call_lists[lynq_call_id].addr)+1);
920 return 0;
921}
922
923#if 0
924int main(int argc,char **argv)
925{
926 int n = 0;
927 n = lynq_init_call(lynq_call_state_change_test,2222);
928 if(n<0)
929 {
930 printf("lynq init call fail!!!\n");
931 return -1;
932 }
933 printf("lynq call init success!!!\n");
934 char phoneNum[LYNQ_PHONE_NUMBER_MAX];
935 sprintf(phoneNum,"18180053406 0",strlen("18180053406 0")+1);
936 lynq_call(phoneNum);
937 while(1)
938 {
939 sleep(1);
940 }
941 return 0;
942}
943#endif
944/*Warren add for T800 platform 2021/11/19 end*/