blob: 0b4ae924ef66217912dd4fdb8ed5cd9ff3b095a4 [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
rjw5d2a50e2022-02-28 15:01:49 +080018#define DSET_IP_ADDRESS "127.0.0.1"
lh7b0674a2022-01-10 00:34:35 -080019#define LYNQ_URC_SERVICE_PORT 8086
20#define LYNQ_REC_BUF 8192
21#define LYNQ_REQUEST_PARAM_BUF 8192
22#define LYQN_SEDN_BUF 1024*8+sizeof(int)*3
23#define USER_LOG_TAG "LYNQ_CALL"
24
25using ::android::Parcel;
26 typedef enum {
27 LYNQ_CALL_ACTIVE = 0,
28 LYNQ_CALL_HOLDING = 1,
29 LYNQ_CALL_DIALING = 2, /* MO call only */
30 LYNQ_CALL_ALERTING = 3, /* MO call only */
31 LYNQ_CALL_INCOMING = 4, /* MT call only */
32 LYNQ_CALL_WAITING = 5 ,/* MT call only */
33}lynq_call_state_t;
34
35typedef struct{
36 int uToken;
37 int request;
38 int paramLen;
39 char param[LYNQ_REQUEST_PARAM_BUF];
40}lynq_client_t;
41typedef struct
42{
43 int used;
44 int call_id;
45 int call_state;
46 int toa;
47 int direction;/*0: MO call,1:MT call*/
48 char addr[LYNQ_PHONE_NUMBER_MAX];
49 int hasTimeout;
50}lynq_call_list_e_t;
51typedef struct
52{
53 int call_id;
54 int call_state;
55 int toa;
56 int direction;/*0: MO call,1:MT call*/
57 char addr[LYNQ_PHONE_NUMBER_MAX];
58}lynq_call_list_t;
59
60lynq_call_list_e_t lynq_call_lists[LYNQ_CALL_MAX]={};
61static pthread_mutex_t call_state_change_mutex = PTHREAD_MUTEX_INITIALIZER;
62static pthread_cond_t call_state_change_cond = PTHREAD_COND_INITIALIZER;
ll04ae4142022-01-27 05:54:38 +000063static pthread_mutex_t s_urc_call_state_change_mutex = PTHREAD_MUTEX_INITIALIZER;
lh7b0674a2022-01-10 00:34:35 -080064static pthread_cond_t s_urc_call_state_change_cond = PTHREAD_COND_INITIALIZER;
65static pthread_mutex_t s_incoming_call_mutex = PTHREAD_MUTEX_INITIALIZER;
66static pthread_cond_t s_incoming_call_cond = PTHREAD_COND_INITIALIZER;
lhec17b0a2022-02-13 23:56:05 -080067pthread_t lynq_call_urc_tid;
68pthread_t lynq_call_list_loop_tid;
69
rjw5d2a50e2022-02-28 15:01:49 +080070/*lei add*/
71/* socket文件描述符 */
72int len_addr_serv;
73struct sockaddr_in addr_serv;
74lynq_client_t client_t;
75int client_size = 0;
76/*lei add*/
lh7b0674a2022-01-10 00:34:35 -080077
lh42c1e572022-01-25 18:47:39 -080078int s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -080079
80enum{
81 CALL_OFF=0,
82 CALL_ON=1
83}call_state;
ll04ae4142022-01-27 05:54:38 +000084typedef enum{
85 LYNQ_E_CARDSTATE_ERROR=8000,
86 /* The voice service state is out of service*/
87 LYNQ_E_STATE_OUT_OF_SERVICE=8001,
88 /* The voice service state is EMERGENCY_ONLY*/
89 LYNQ_E_STATE_EMERGENCY_ONLY=8002,
90 /* The radio power is power off*/
91 LYNQ_E_STATE_POWER_OFF=8003,
92 LYNQ_E_TIME_OUT=8004,
93 /*create or open sms DB fail */
94 LYNQ_E_SMS_DB_FAIL=8005,
95 /*Failed to execute sql statement*/
96 LYNQ_E_SMS_SQL_FAIL = 8006,
97 LYNQ_E_SMS_NOT_FIND = 8007,
98 /* The logic conflict*/
99 LYNQ_E_CONFLICT=9000,
100 /*Null anomaly*/
101 LYNQ_E_NULL_ANONALY=9001
lh7b0674a2022-01-10 00:34:35 -0800102}LYNQ_E;
103
104int lynq_call_state =CALL_OFF;
lh42c1e572022-01-25 18:47:39 -0800105int lynq_call_client_sockfd = 0;
106int Global_uToken_call = 0;
lh7b0674a2022-01-10 00:34:35 -0800107int global_call_count =0;
108int global_call_auto_answer = 0;
lh42c1e572022-01-25 18:47:39 -0800109bool urc_call_recive_status = 1;
lh7b0674a2022-01-10 00:34:35 -0800110bool call_list_loop = 1;
111int isDial = 0;
112int lynqIncomingCallId = 0;
113
114int JumpHeader(Parcel &p,int *resp_type,int *request,int *slot_id,int *error)
115{
116 if(p.dataAvail() > 0)
117 {
118 p.readInt32(resp_type);
119 p.readInt32(request);
120 p.readInt32(slot_id);
121 p.readInt32(error);
122 return 0;
123 }
124 else
125 {
126 return -1;
127 }
128}
129int send_request(int sockfd,lynq_client_t *client_tmp)
130{
131 int ret=0;
rjw5d2a50e2022-02-28 15:01:49 +0800132 ret = sendto(sockfd, client_tmp, client_size, 0, (struct sockaddr *)&addr_serv, len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800133 if(ret==-1)
134 {
ll3fe03462022-03-01 09:18:53 +0000135 LYERRLOG("sendto error\n");
lh7b0674a2022-01-10 00:34:35 -0800136 return -1;
137 }
138 return 0;
139}
140
141int get_response(int sockfd,Parcel &p)
142{
143 int len = 0;
144 char recvline[LYNQ_REC_BUF];
145 bzero(recvline,LYNQ_REC_BUF);
146 /* receive data from server */
rjw5d2a50e2022-02-28 15:01:49 +0800147 len = recvfrom(sockfd,recvline,LYNQ_REC_BUF, 0, (struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800148 if(len == -1)
149 {
ll3fe03462022-03-01 09:18:53 +0000150 LYERRLOG("recvfrom error\n");
lh7b0674a2022-01-10 00:34:35 -0800151 return -1;
152 }
lh7b0674a2022-01-10 00:34:35 -0800153 if (recvline != NULL) {
154 p.setData((uint8_t *)recvline,len); // p.setData((uint8_t *) buffer, buflen);
155 p.setDataPosition(0);
156 }
157 return 0;
158}
159static char *strdupReadString(Parcel &p) {
ll04ae4142022-01-27 05:54:38 +0000160 size_t stringlen;
lh7b0674a2022-01-10 00:34:35 -0800161 const char16_t *s16;
162 s16 = p.readString16Inplace(&stringlen);
ll04ae4142022-01-27 05:54:38 +0000163 return strndup16to8(s16, stringlen);
lh7b0674a2022-01-10 00:34:35 -0800164}
165
166int lynq_get_current_call_list(lynq_call_list_t call_list[LYNQ_CALL_MAX])
167{
168 Parcel p;
169 lynq_client_t client;
170 int resp_type = -1;
171 int request = -1;
172 int slot_id = -1;
173 int error = -1;
174 int call_num = 0;
175 int temp = 0;
176 char *remote_phoneNum = NULL;
177 char *remote_name= NULL;
178 char *uusData = NULL;
lh42c1e572022-01-25 18:47:39 -0800179 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800180 client.request = 9;//RIL_REQUEST_GET_CURRENT_CALLS
181 client.paramLen = 0;
182 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
183 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800184 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800185 {
186 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800187 return -1;
188 }
lh42c1e572022-01-25 18:47:39 -0800189 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800190 JumpHeader(p,&resp_type,&request,&slot_id,&error);
191 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
192 if(error == 0)
193 {
194 p.readInt32(&call_num);
195 global_call_count = call_num;
196 if(call_num<=0)
197 {
198 lynq_call_state = CALL_OFF;
lhec17b0a2022-02-13 23:56:05 -0800199 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800200 return 0;
201 }
lhec17b0a2022-02-13 23:56:05 -0800202 lynq_call_state = CALL_ON;
203 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800204 for(int i = 0;i < call_num;i++)
205 {
206 p.readInt32(&temp);
207 call_list[i].call_state = temp;
208 p.readInt32(&call_list[i].call_id);
209 p.readInt32(&call_list[i].toa);
210 p.readInt32(&temp);
211 p.readInt32(&temp);
212 call_list[i].direction = temp;
213 p.readInt32(&temp);
214 p.readInt32(&temp);
215 p.readInt32(&temp);
216 remote_phoneNum = strdupReadString(p);
217 memcpy(call_list[i].addr,remote_phoneNum,strlen(remote_phoneNum));
218 LYINFLOG("call_id=%d,call_state=%d,direction=%d,addr=%s,toa=%d",call_list[i].call_id,call_list[i].call_state,
219 call_list[i].direction,call_list[i].addr,call_list[i].toa);
220 p.readInt32(&temp);
221 remote_name = strdupReadString(p);
222 p.readInt32(&temp);
223 p.readInt32(&temp);
224 if(temp==0)
225 {
226 continue;
227 }
228 p.readInt32(&temp); /* UUS Information is present */
229 p.readInt32(&temp);
230 p.readInt32(&temp);
231 p.read(uusData,temp);
232 }
233 }
234 return 0;
235}
236
237void cleanCallList(int lynq_call_id)
238{
239 lynq_call_lists[lynq_call_id].call_id = 0;
240 lynq_call_lists[lynq_call_id].call_state = 0;
241 lynq_call_lists[lynq_call_id].toa = 0;
242 lynq_call_lists[lynq_call_id].direction = 0;
243 lynq_call_lists[lynq_call_id].used = 0;
244 lynq_call_lists[lynq_call_id].hasTimeout = 0;
245 memset(lynq_call_lists[lynq_call_id].addr,0,sizeof(lynq_call_lists[lynq_call_id].addr));
246}
247int getUnusedElement()
248{
249 for(int i=0;i < LYNQ_CALL_MAX; i++)
250 {
251 if(lynq_call_lists[i].used!=1)
252 {
253 return i;
254 }
255 }
256 return -1;
257}
258int updateAddr(char addr[])
259{
260 int ret = 0;
261 ret = getUnusedElement();
262 memcpy(lynq_call_lists[ret].addr,addr,strlen(addr)+1);
263 lynq_call_lists[ret].used = 1;
264 return ret;
265}
266void updateCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction)
267{
268 callList->call_id = call_id;
269 callList->call_state = call_state;
270 callList->toa = toa;
271 callList->direction = direction;
272 callList->used = 1;
273 callList->hasTimeout = 0;
274 return;
275}
276int waitCallstateChange(int mtime)
277{
278 int ret = 0;
279 int sec = 0;
280 int usec = 0;
lh2afc7732022-01-10 02:24:31 -0800281 struct timeval now;
282 struct timespec timeout;
283 gettimeofday(&now,NULL);
lh7b0674a2022-01-10 00:34:35 -0800284 sec = mtime/1000;
285 usec = mtime%1000;
lh2afc7732022-01-10 02:24:31 -0800286 timeout.tv_sec = now.tv_sec+sec;
287 timeout.tv_nsec = now.tv_usec*1000+usec*1000000;
lh7b0674a2022-01-10 00:34:35 -0800288 pthread_mutex_lock(&call_state_change_mutex);
lh2afc7732022-01-10 02:24:31 -0800289 ret = pthread_cond_timedwait(&call_state_change_cond,&call_state_change_mutex,&timeout);
lh7b0674a2022-01-10 00:34:35 -0800290 pthread_mutex_unlock(&call_state_change_mutex);
291 return ret;
292}
293int waitIncomingCall()
294{
295 int ret = 0;
296 pthread_mutex_lock(&s_incoming_call_mutex);
297 ret = pthread_cond_wait(&s_incoming_call_cond,&s_incoming_call_mutex);
298 pthread_mutex_unlock(&s_incoming_call_mutex);
299 return ret;
300}
301int checkHasCall(char addr[])
302{
303 for(int i = 0;i<LYNQ_CALL_MAX;i++)
304 {
305 if(strcmp(lynq_call_lists[i].addr,addr)==0)
306 {
307 return 1;
308 }
309 }
310 return 0;
311}
312void sendSignalToWaitCallStateChange()
313{
314 pthread_mutex_lock(&call_state_change_mutex);
315 pthread_cond_signal(&call_state_change_cond);
316 pthread_mutex_unlock(&call_state_change_mutex);
317}
318void sendSignalIncomingCall()
319{
320 pthread_mutex_lock(&s_incoming_call_mutex);
321 pthread_cond_signal(&s_incoming_call_cond);
322 pthread_mutex_unlock(&s_incoming_call_mutex);
323}
324
325void addCallListToLynqCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction,char addr[LYNQ_PHONE_NUMBER_MAX])
326{
327 callList->call_id = call_id;
328 callList->call_state = call_state;
329 callList->toa = toa;
330 callList->direction = direction;
331 memcpy(callList->addr,addr,strlen(addr)+1);
332 callList->used = 1;
333 callList->hasTimeout = 0;
334 return;
335}
336
337void *triggerGetCallList(void *parg)
ll04ae4142022-01-27 05:54:38 +0000338{
339 int ret=0;
340 lynq_call_list_t call_list[LYNQ_CALL_MAX];
lh7b0674a2022-01-10 00:34:35 -0800341 while(call_list_loop)
ll04ae4142022-01-27 05:54:38 +0000342 {
343 pthread_mutex_lock(&s_urc_call_state_change_mutex);
344 pthread_cond_wait(&s_urc_call_state_change_cond, &s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800345 LYDBGLOG("triggerGetCallList event!!!\n");
ll04ae4142022-01-27 05:54:38 +0000346 memset(call_list,0,sizeof(call_list));
347 ret = lynq_get_current_call_list(call_list);
348 if(ret != 0)
349 {
350 printf("get current call list failure!!!\n");
351 break;
352 }
lh7b0674a2022-01-10 00:34:35 -0800353 for(int i = 0;i < LYNQ_CALL_MAX;i++)
354 {
355 if(call_list[i].direction == 1)//MT call
356 {
357 if(call_list[i].call_state ==4)//LYNQ_CALL_INCOMING = 4, /* MT call only */
358 {
359 if(!checkHasCall(call_list[i].addr))
360 {
361 lynqIncomingCallId = getUnusedElement();
362 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);
363 sendSignalIncomingCall();
364 }
365 }
366 }
367 else
368 {
369 if(call_list[i].call_id==0)
370 {
371 break;
372 }
373 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
374 {
375 if(lynq_call_lists[n].hasTimeout==1)
376 {
377 /*hangup call with id*/
378 lynq_call_hungup(&n);
379 lynq_call_lists[n].hasTimeout==0;
380 continue;
381 }
382 if(strcmp(call_list[i].addr,lynq_call_lists[n].addr)==0)
383 {
384 updateCallList(&lynq_call_lists[n],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction);
385 }
386 }
387 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,
388 call_list[i].direction,call_list[i].addr,call_list[i].toa);
389 }
ll04ae4142022-01-27 05:54:38 +0000390 }
lh42c1e572022-01-25 18:47:39 -0800391 s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -0800392 if(isDial==1)
393 {
394 sendSignalToWaitCallStateChange();
395 isDial = 0;
396 }
ll04ae4142022-01-27 05:54:38 +0000397 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
398 }
399 return NULL;
lh7b0674a2022-01-10 00:34:35 -0800400}
401
402void lynqRespWatingEvent()
ll04ae4142022-01-27 05:54:38 +0000403{
404 if(s_call_urc_event_complete==1)
405 {
406 pthread_mutex_lock(&s_urc_call_state_change_mutex);
407 pthread_cond_signal(&s_urc_call_state_change_cond);
408 s_call_urc_event_complete = 0;
409 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800410 }
ll04ae4142022-01-27 05:54:38 +0000411 return;
lh7b0674a2022-01-10 00:34:35 -0800412}
413
414/*Warren add for T800 platform 2021/11/19 start*/
415int lynq_socket_client_start()
rjw5d2a50e2022-02-28 15:01:49 +0800416{
417 #if 0
lh7b0674a2022-01-10 00:34:35 -0800418 struct sockaddr_in lynq_socket_server_addr;
419 /* init lynq_socket_server_addr */
420 bzero(&lynq_socket_server_addr, sizeof(lynq_socket_server_addr));
421 lynq_socket_server_addr.sin_family = AF_INET;
422 lynq_socket_server_addr.sin_port = htons(LYNQ_SERVICE_PORT);
423 lynq_socket_server_addr.sin_addr.s_addr = htons(INADDR_ANY);
rjw5d2a50e2022-02-28 15:01:49 +0800424
lh7b0674a2022-01-10 00:34:35 -0800425 /*
426 if(inet_pton(AF_INET,"127.0.0.1", &lynq_socket_server_addr.sin_addr) <= 0)
427 {
428 printf("[%s] is not a valid IPaddress\n", argv[1]);
429 exit(1);
430 }
431*/
lh42c1e572022-01-25 18:47:39 -0800432 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
433 if(connect(lynq_call_client_sockfd, (struct sockaddr *)&lynq_socket_server_addr, sizeof(lynq_socket_server_addr)) == -1)
lh7b0674a2022-01-10 00:34:35 -0800434 {
ll3fe03462022-03-01 09:18:53 +0000435 LYERRLOG("connect error\n");
lh7b0674a2022-01-10 00:34:35 -0800436 return -1;
437 }
rjw5d2a50e2022-02-28 15:01:49 +0800438 #endif
439 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
440 if (-1 == lynq_call_client_sockfd)
441 {
442 return lynq_call_client_sockfd;
443 }
444 /* 设置address */
445 memset(&addr_serv, 0, sizeof(addr_serv));
446 addr_serv.sin_family = AF_INET;
447 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
448 addr_serv.sin_port = htons(LYNQ_SERVICE_PORT);
449 len_addr_serv = sizeof(addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800450 return 0;
451}
452int lynq_update_call_list_loop()
453{
454 int ret = 0;
lh7b0674a2022-01-10 00:34:35 -0800455 pthread_attr_t attr;
456 pthread_attr_init(&attr);
457 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800458 ret = pthread_create(&lynq_call_list_loop_tid,&attr,triggerGetCallList,NULL);
lh7b0674a2022-01-10 00:34:35 -0800459 if(ret < 0)
460 {
461 LYERRLOG("lynq_update_call_list_loop fail!!!");
462 return -1;
463 }
464 LYDBGLOG("lynq_update_call_list_loop success!!!\n");
465 return 0;
466
467}
468void *thread_urc_recv(void *parg)
ll04ae4142022-01-27 05:54:38 +0000469{
lh7b0674a2022-01-10 00:34:35 -0800470 int socket_fd = (int64_t)parg;
471 int len=0;
472 socklen_t addr_len=0;
473 uint8_t *dataLength = NULL;
474 char urc_data[LYNQ_REC_BUF];
475 int slot_id = -1;
476 int resp_type = -1;
477 int urcid = -1;
478 Parcel *p = NULL;
479 struct sockaddr_in dest_addr;
480 LYINFLOG("thread_urc_recv in running....\n");
lh42c1e572022-01-25 18:47:39 -0800481 while(urc_call_recive_status)
lh7b0674a2022-01-10 00:34:35 -0800482 {
483 bzero(urc_data,LYNQ_REC_BUF);
484 //get data msg
485 len = recvfrom(socket_fd,urc_data,LYNQ_REC_BUF,0,(struct sockaddr *)&dest_addr,&addr_len);
486 if(len <= 0)
ll04ae4142022-01-27 05:54:38 +0000487 {
ll3fe03462022-03-01 09:18:53 +0000488 LYERRLOG("thread_urc_recv step2 fail\n");
ll04ae4142022-01-27 05:54:38 +0000489 break;
lh7b0674a2022-01-10 00:34:35 -0800490 }
491 LYDBGLOG("=====>urc data len<=====:%d\n",len);
492 p = new Parcel();
493 if(p==NULL)
494 {
lh21502f52022-01-27 00:27:12 -0800495 LYERRLOG("new parcel failure!!!");
lh7b0674a2022-01-10 00:34:35 -0800496 break;
497 }
498 p->setData((uint8_t *)urc_data,len); // p.setData((uint8_t *) buffer, buflen);
499 p->setDataPosition(0);
500 if(p->dataAvail() > 0)
501 {
502 p->readInt32(&resp_type);
503 p->readInt32(&urcid);
504 p->readInt32(&slot_id);
505 //LYDBGLOG("*******Warren test*******:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
506 switch (urcid)
507 {
508 case 1001://RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED
509 {
510 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
511 lynqRespWatingEvent();
512 break;
513 }
514 case 1018://RIL_UNSOL_CALL_RING
515 {
516 if(global_call_auto_answer==1)
517 {
518 lynq_call_answer();
519 }
520 break;
521 }
522 case 1029://RIL_UNSOL_RINGBACK_TONE
523 case 3049://RIL_UNSOL_CALL_INFO_INDICATION
524 {
525 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
526 break;
527 }
528 default:
529 break;
530 }
531 }
532 delete p;
533 p = NULL;
534 }
535 close(socket_fd);
536}
537int lynq_socket_urc_start()
538{
539 int socket_fd=0;
540 int rt=0;
541 int len=0;
542 int on=1;
543 struct sockaddr_in urc_local_addr;
lh7b0674a2022-01-10 00:34:35 -0800544 pthread_attr_t attr;
ll04ae4142022-01-27 05:54:38 +0000545 socket_fd = socket(AF_INET,SOCK_DGRAM,0);
ll04ae4142022-01-27 05:54:38 +0000546 if(socket_fd < 0)
547 {
ll3fe03462022-03-01 09:18:53 +0000548 LYERRLOG("creaet socket for udp fail\n");
ll04ae4142022-01-27 05:54:38 +0000549 return -1;
lh7b0674a2022-01-10 00:34:35 -0800550 }
551 urc_local_addr.sin_family = AF_INET;
552 urc_local_addr.sin_port = htons(LYNQ_URC_SERVICE_PORT);
553 urc_local_addr.sin_addr.s_addr = htons(INADDR_ANY);
554 /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
ll04ae4142022-01-27 05:54:38 +0000555 rt = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
556 if(rt<0)
557 {
ll3fe03462022-03-01 09:18:53 +0000558 LYERRLOG("SO_REUSEADDR fail\n");
lh7b0674a2022-01-10 00:34:35 -0800559 return -1;
560 }
561 rt = bind(socket_fd ,(struct sockaddr*)&urc_local_addr, sizeof(urc_local_addr));
ll04ae4142022-01-27 05:54:38 +0000562 if (rt == -1)
563 {
lh21502f52022-01-27 00:27:12 -0800564 LYERRLOG("bind failed");
ll04ae4142022-01-27 05:54:38 +0000565 return -1;
lh7b0674a2022-01-10 00:34:35 -0800566 }
567 pthread_attr_init(&attr);
568 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800569 rt = pthread_create(&lynq_call_urc_tid,&attr,thread_urc_recv,(void *)socket_fd);
lh7b0674a2022-01-10 00:34:35 -0800570 if(rt < 0)
571 {
lh21502f52022-01-27 00:27:12 -0800572 LYERRLOG("urc loop failure!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800573 return -1;
574 }
lh21502f52022-01-27 00:27:12 -0800575 LYDBGLOG("urc loop success!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800576 return 0;
577}
578int getSelfElement(char addr[])
579{
580 for(int i=0;i < LYNQ_CALL_MAX; i++)
581 {
582 if(lynq_call_lists[i].used==1)
583 {
584 if(strcmp(lynq_call_lists[i].addr,addr)==0)
585 {
586 return i;
587 }
588
589 }
590 }
591 return -1;
592}
593
594void lynq_call_state_change_test(int soc_id)
595{
596 printf("call state change,sim:%d\n",soc_id);
597}
598int lynq_init_call(int uToken)
599{
600 int result = 0;
lh42c1e572022-01-25 18:47:39 -0800601 Global_uToken_call = uToken;
lh21502f52022-01-27 00:27:12 -0800602 urc_call_recive_status = 1;
rjw5d2a50e2022-02-28 15:01:49 +0800603 client_size = sizeof(client_t);
lh7b0674a2022-01-10 00:34:35 -0800604 LYLOGSET(LOG_INFO);
605 LYLOGEINIT(USER_LOG_TAG);
606 result = lynq_socket_client_start();
607 if(result!=0)
608 {
609 return -1;
610 }
611 result = lynq_socket_urc_start();
612 if(result!=0)
613 {
614 return -1;
615 }
616 result = lynq_update_call_list_loop();
617 if(result!=0)
618 {
619 return -1;
620 }
621 memset(lynq_call_lists,0,sizeof(lynq_call_lists));
622 LYDBGLOG("lynq init call success!!!");
623 return 0;
624}
625int lynq_deinit_call()
626{
lhec17b0a2022-02-13 23:56:05 -0800627 int ret = -1;
628 if(lynq_call_client_sockfd>0)
629 {
630 close(lynq_call_client_sockfd);
631 }
lh42c1e572022-01-25 18:47:39 -0800632 urc_call_recive_status = 0;
lh7b0674a2022-01-10 00:34:35 -0800633 call_list_loop = 0;
lhec17b0a2022-02-13 23:56:05 -0800634 ret = pthread_cancel(lynq_call_urc_tid);
635 LYDBGLOG("pthread cancel ret = %d",ret);
636 ret = pthread_cancel(lynq_call_list_loop_tid);
637 LYDBGLOG("pthread cancel ret = %d",ret);
638 ret = pthread_join(lynq_call_urc_tid,NULL);
639 LYDBGLOG("pthread join ret = %d",ret);
640 ret = pthread_join(lynq_call_list_loop_tid,NULL);
641 LYDBGLOG("pthread join ret = %d",ret);
lh7b0674a2022-01-10 00:34:35 -0800642 return 0;
643}
644int lynq_call(int* handle,char addr[])
645{
646 Parcel p;
647 lynq_client_t client;
648 int resp_type = -1;
649 int request = -1;
650 int slot_id = -1;
651 int error = -1;
652 int lynq_call_id = -1;
653 if(addr==NULL)
654 {
655 LYERRLOG("Phone num is null!!!");
656 return -1;
657 }
lh42c1e572022-01-25 18:47:39 -0800658 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800659 client.request = 10;//RIL_REQUEST_DIAL
660 client.paramLen = 2;
661 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
662 memcpy(client.param,addr,strlen(addr)+1);
663 strcat(client.param," 0");
664 LYERRLOG("uToken=%d,request=%d,paralen=%d,param=%s",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800665 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800666 {
667 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800668 return -1;
669 }
lh42c1e572022-01-25 18:47:39 -0800670 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800671 JumpHeader(p,&resp_type,&request,&slot_id,&error);
672 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
673 lynq_call_id = updateAddr(addr);
674 if(error==0)
675 {
676 isDial = 1;
677 if(waitCallstateChange(3000)==ETIMEDOUT)//3000ms
678 {
679 error = LYNQ_E_TIME_OUT;
680 LYERRLOG("timeout:wait Call state fail!!!");
681 lynq_call_lists[lynq_call_id].hasTimeout = 1;
682 return error;
683 }
684 *handle = lynq_call_id;
685 }
686 return error;
687}
688int lynq_call_answer()
689{
690 Parcel p;
691 lynq_client_t client;
692 int resp_type = -1;
693 int request = -1;
694 int slot_id = -1;
695 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800696 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800697 client.request = 40;//RIL_REQUEST_DIAL
698 client.paramLen = 0;
699 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
700 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800701 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800702 {
703 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800704 return -1;
705 }
lh42c1e572022-01-25 18:47:39 -0800706 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800707 JumpHeader(p,&resp_type,&request,&slot_id,&error);
708 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
709 return error;
710}
711int lynq_call_hungup(int* handle)
712{
713 Parcel p;
714 lynq_client_t client;
715 int resp_type = -1;
716 int request = -1;
717 int slot_id = -1;
718 int error = -1;
719 int call_id = 0;
720 int lynq_call_id = 0;
lh42c1e572022-01-25 18:47:39 -0800721 if(handle==NULL||!((*handle>=0)&&(*handle<10)))
722 {
723 LYERRLOG("[%s] illegal input!!!!",__FUNCTION__);
724 return LYNQ_E_CONFLICT;
725 }
726 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800727 client.request = 12;//RIL_REQUEST_HUNGUP
728 client.paramLen = 1;
729 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
730 lynq_call_id = *handle;
731 call_id = lynq_call_lists[lynq_call_id].call_id;
732 sprintf(client.param,"%d",call_id);
733 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800734 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800735 {
736 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800737 return -1;
738 }
lh42c1e572022-01-25 18:47:39 -0800739 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800740 JumpHeader(p,&resp_type,&request,&slot_id,&error);
741 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
742 if(error==0)
743 {
744 cleanCallList(lynq_call_id);
745 }
746 return error;
747}
748int lynq_call_hungup_all()
749{
750 Parcel p;
751 lynq_client_t client;
752 int resp_type = -1;
753 int request = -1;
754 int slot_id = -1;
755 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800756 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800757 client.request = 17;//RIL_REQUEST_UDUB
758 client.paramLen = 0;
759 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
760 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800761 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800762 {
763 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800764 return -1;
765 }
lh42c1e572022-01-25 18:47:39 -0800766 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800767 JumpHeader(p,&resp_type,&request,&slot_id,&error);
768 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
769 return error;
770}
771int lynq_wait_incoming_call(int *handle)
772{
773 waitIncomingCall();
774 *handle = lynqIncomingCallId;
775 LYINFLOG("lynq incoming call id:%d",lynqIncomingCallId);
776 return 0;
777}
778
779int lynq_set_auto_answercall(const int mode)
780{
781 global_call_auto_answer = mode;
782 LYINFLOG("auto answer call mode =%d",mode);
783 return 0;
784}
785int lynq_get_mute_status(int *status)
786{
787 Parcel p;
788 lynq_client_t client;
789 int resp_type = -1;
790 int request = -1;
791 int slot_id = -1;
792 int error = -1;
793 if(status==NULL)
794 {
795 LYERRLOG("status is null");
796 return -1;
797 }
lh42c1e572022-01-25 18:47:39 -0800798 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800799 client.request = 54;//RIL_REQUEST_GET_MUTE
800 client.paramLen = 0;
801 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
802 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800803 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800804 {
805 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800806 return -1;
807 }
lh42c1e572022-01-25 18:47:39 -0800808 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800809 JumpHeader(p,&resp_type,&request,&slot_id,&error);
810 p.readInt32(status);
811 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
812 return error;
813}
lldc99c9b2022-01-24 12:16:22 +0000814
815static int judge_mic(const int enable){
llf512fa32022-02-14 08:58:16 +0000816 switch(enable){
817 case 0:
818 return 1;
819 case 1:
820 return 1;
821 default:
822 return 0;
lldc99c9b2022-01-24 12:16:22 +0000823 }
824}
825
lh7b0674a2022-01-10 00:34:35 -0800826int lynq_set_mute_mic(const int enable)
lldc99c9b2022-01-24 12:16:22 +0000827{
828 if(!judge_mic(enable)){
829 return LYNQ_E_CONFLICT;
830 }
lh7b0674a2022-01-10 00:34:35 -0800831 Parcel p;
832 lynq_client_t client;
833 int resp_type = -1;
834 int request = -1;
835 int slot_id = -1;
836 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800837 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800838 client.request = 53;//RIL_REQUEST_SET_MUTE
839 client.paramLen = 1;
840 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
841 sprintf(client.param,"%d",enable);
842 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800843 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800844 {
845 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800846 return -1;
847 }
lh42c1e572022-01-25 18:47:39 -0800848 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800849 JumpHeader(p,&resp_type,&request,&slot_id,&error);
850 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
851 return error;
852}
853int lynq_set_DTMF(const char callnum)
854{
855 Parcel p;
856 lynq_client_t client;
857 int resp_type = -1;
858 int request = -1;
859 int slot_id = -1;
860 int error = -1;
861 if(!lynq_call_state)
862 {
863 LYERRLOG("LYNQ_E_CONFLICT");
864 return LYNQ_E_CONFLICT;
865 }
lh42c1e572022-01-25 18:47:39 -0800866 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800867 client.request = 24;//RIL_REQUEST_DTMF
868 client.paramLen = 1;
869 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
870 sprintf(client.param,"%c",callnum);
871 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800872 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800873 {
874 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800875 return -1;
876 }
lh42c1e572022-01-25 18:47:39 -0800877 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800878 JumpHeader(p,&resp_type,&request,&slot_id,&error);
879 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
880 return error;
881
882}
lldc99c9b2022-01-24 12:16:22 +0000883
884static int judge_volume(const int volume){
885 if(volume < 0 ||volume >36){
886 return 0;
887 }
ll04ae4142022-01-27 05:54:38 +0000888 return 1;
lldc99c9b2022-01-24 12:16:22 +0000889}
890
lh7b0674a2022-01-10 00:34:35 -0800891int lynq_set_DTMF_volume(const int volume)
lldc99c9b2022-01-24 12:16:22 +0000892{
893 if(!judge_volume(volume)){
894 return LYNQ_E_CONFLICT;
895 }
lh7b0674a2022-01-10 00:34:35 -0800896 Parcel p;
897 lynq_client_t client;
898 int resp_type = -1;
899 int request = -1;
900 int slot_id = -1;
901 int error = -1;
902 //if(!lynq_call_state)
903 //{
904 // LYERRLOG("LYNQ_E_CONFLICT");
905 // return LYNQ_E_CONFLICT;
906 //}
lh42c1e572022-01-25 18:47:39 -0800907 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800908 client.request = 8003;//LYNQ_REQUEST_SET_DTMF_VOLUME
909 client.paramLen = 1;
910 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
911 sprintf(client.param,"%d",volume);
912 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800913 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800914 {
915 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800916 return -1;
917 }
lh42c1e572022-01-25 18:47:39 -0800918 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800919 JumpHeader(p,&resp_type,&request,&slot_id,&error);
920 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
921 return 0;
922}
923int lynq_get_current_call_state(int *handle, int *call_state,int *toa,int *direction,char addr[])
924{
925 int lynq_call_id = 0;
926 if(handle==NULL)
927 {
928 return LYNQ_E_NULL_ANONALY;
929 }
930 lynq_call_id = *handle;
931 *call_state = lynq_call_lists[lynq_call_id].call_state;
932 *toa = lynq_call_lists[lynq_call_id].toa;
933 *direction = lynq_call_lists[lynq_call_id].direction;
934 memcpy(addr,lynq_call_lists[lynq_call_id].addr,strlen(lynq_call_lists[lynq_call_id].addr)+1);
935 return 0;
936}
937
938#if 0
939int main(int argc,char **argv)
940{
941 int n = 0;
942 n = lynq_init_call(lynq_call_state_change_test,2222);
943 if(n<0)
944 {
945 printf("lynq init call fail!!!\n");
946 return -1;
947 }
948 printf("lynq call init success!!!\n");
949 char phoneNum[LYNQ_PHONE_NUMBER_MAX];
950 sprintf(phoneNum,"18180053406 0",strlen("18180053406 0")+1);
951 lynq_call(phoneNum);
952 while(1)
953 {
954 sleep(1);
955 }
956 return 0;
957}
958#endif
959/*Warren add for T800 platform 2021/11/19 end*/