blob: a72566ce69b5e17f2158ab9033d09ab30a6bade7 [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;
lla8c25a82022-03-17 05:31:33 +000067pthread_t lynq_call_urc_tid = -1;
68pthread_t lynq_call_list_loop_tid = -1;
lhec17b0a2022-02-13 23:56:05 -080069
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;
lla8c25a82022-03-17 05:31:33 +0000113/**
114 * @brief mark call initialization state
115 * 0: deinit state
116 * 1: init state
117 */
118int g_lynq_call_init_flag = 0;
lh7b0674a2022-01-10 00:34:35 -0800119
120int JumpHeader(Parcel &p,int *resp_type,int *request,int *slot_id,int *error)
121{
122 if(p.dataAvail() > 0)
123 {
124 p.readInt32(resp_type);
125 p.readInt32(request);
126 p.readInt32(slot_id);
127 p.readInt32(error);
128 return 0;
129 }
130 else
131 {
132 return -1;
133 }
134}
135int send_request(int sockfd,lynq_client_t *client_tmp)
136{
137 int ret=0;
rjw5d2a50e2022-02-28 15:01:49 +0800138 ret = sendto(sockfd, client_tmp, client_size, 0, (struct sockaddr *)&addr_serv, len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800139 if(ret==-1)
140 {
ll3fe03462022-03-01 09:18:53 +0000141 LYERRLOG("sendto error\n");
lh7b0674a2022-01-10 00:34:35 -0800142 return -1;
143 }
144 return 0;
145}
146
147int get_response(int sockfd,Parcel &p)
148{
149 int len = 0;
150 char recvline[LYNQ_REC_BUF];
151 bzero(recvline,LYNQ_REC_BUF);
152 /* receive data from server */
rjw5d2a50e2022-02-28 15:01:49 +0800153 len = recvfrom(sockfd,recvline,LYNQ_REC_BUF, 0, (struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800154 if(len == -1)
155 {
ll3fe03462022-03-01 09:18:53 +0000156 LYERRLOG("recvfrom error\n");
lh7b0674a2022-01-10 00:34:35 -0800157 return -1;
158 }
lh7b0674a2022-01-10 00:34:35 -0800159 if (recvline != NULL) {
160 p.setData((uint8_t *)recvline,len); // p.setData((uint8_t *) buffer, buflen);
161 p.setDataPosition(0);
162 }
163 return 0;
164}
165static char *strdupReadString(Parcel &p) {
ll04ae4142022-01-27 05:54:38 +0000166 size_t stringlen;
lh7b0674a2022-01-10 00:34:35 -0800167 const char16_t *s16;
168 s16 = p.readString16Inplace(&stringlen);
ll04ae4142022-01-27 05:54:38 +0000169 return strndup16to8(s16, stringlen);
lh7b0674a2022-01-10 00:34:35 -0800170}
171
172int lynq_get_current_call_list(lynq_call_list_t call_list[LYNQ_CALL_MAX])
173{
174 Parcel p;
175 lynq_client_t client;
176 int resp_type = -1;
177 int request = -1;
178 int slot_id = -1;
179 int error = -1;
180 int call_num = 0;
181 int temp = 0;
182 char *remote_phoneNum = NULL;
183 char *remote_name= NULL;
184 char *uusData = NULL;
lh42c1e572022-01-25 18:47:39 -0800185 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800186 client.request = 9;//RIL_REQUEST_GET_CURRENT_CALLS
187 client.paramLen = 0;
188 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
189 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800190 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800191 {
192 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800193 return -1;
194 }
lh42c1e572022-01-25 18:47:39 -0800195 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800196 JumpHeader(p,&resp_type,&request,&slot_id,&error);
197 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
198 if(error == 0)
199 {
200 p.readInt32(&call_num);
201 global_call_count = call_num;
202 if(call_num<=0)
203 {
204 lynq_call_state = CALL_OFF;
lhec17b0a2022-02-13 23:56:05 -0800205 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800206 return 0;
207 }
lhec17b0a2022-02-13 23:56:05 -0800208 lynq_call_state = CALL_ON;
209 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800210 for(int i = 0;i < call_num;i++)
211 {
212 p.readInt32(&temp);
213 call_list[i].call_state = temp;
214 p.readInt32(&call_list[i].call_id);
215 p.readInt32(&call_list[i].toa);
216 p.readInt32(&temp);
217 p.readInt32(&temp);
218 call_list[i].direction = temp;
219 p.readInt32(&temp);
220 p.readInt32(&temp);
221 p.readInt32(&temp);
222 remote_phoneNum = strdupReadString(p);
223 memcpy(call_list[i].addr,remote_phoneNum,strlen(remote_phoneNum));
224 LYINFLOG("call_id=%d,call_state=%d,direction=%d,addr=%s,toa=%d",call_list[i].call_id,call_list[i].call_state,
225 call_list[i].direction,call_list[i].addr,call_list[i].toa);
226 p.readInt32(&temp);
227 remote_name = strdupReadString(p);
228 p.readInt32(&temp);
229 p.readInt32(&temp);
230 if(temp==0)
231 {
232 continue;
233 }
234 p.readInt32(&temp); /* UUS Information is present */
235 p.readInt32(&temp);
236 p.readInt32(&temp);
237 p.read(uusData,temp);
238 }
239 }
240 return 0;
241}
242
243void cleanCallList(int lynq_call_id)
244{
245 lynq_call_lists[lynq_call_id].call_id = 0;
246 lynq_call_lists[lynq_call_id].call_state = 0;
247 lynq_call_lists[lynq_call_id].toa = 0;
248 lynq_call_lists[lynq_call_id].direction = 0;
249 lynq_call_lists[lynq_call_id].used = 0;
250 lynq_call_lists[lynq_call_id].hasTimeout = 0;
251 memset(lynq_call_lists[lynq_call_id].addr,0,sizeof(lynq_call_lists[lynq_call_id].addr));
252}
253int getUnusedElement()
254{
255 for(int i=0;i < LYNQ_CALL_MAX; i++)
256 {
257 if(lynq_call_lists[i].used!=1)
258 {
259 return i;
260 }
261 }
262 return -1;
263}
264int updateAddr(char addr[])
265{
266 int ret = 0;
267 ret = getUnusedElement();
268 memcpy(lynq_call_lists[ret].addr,addr,strlen(addr)+1);
269 lynq_call_lists[ret].used = 1;
270 return ret;
271}
272void updateCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction)
273{
274 callList->call_id = call_id;
275 callList->call_state = call_state;
276 callList->toa = toa;
277 callList->direction = direction;
278 callList->used = 1;
279 callList->hasTimeout = 0;
280 return;
281}
282int waitCallstateChange(int mtime)
283{
284 int ret = 0;
285 int sec = 0;
286 int usec = 0;
lh2afc7732022-01-10 02:24:31 -0800287 struct timeval now;
288 struct timespec timeout;
289 gettimeofday(&now,NULL);
lh7b0674a2022-01-10 00:34:35 -0800290 sec = mtime/1000;
291 usec = mtime%1000;
lh2afc7732022-01-10 02:24:31 -0800292 timeout.tv_sec = now.tv_sec+sec;
293 timeout.tv_nsec = now.tv_usec*1000+usec*1000000;
lh7b0674a2022-01-10 00:34:35 -0800294 pthread_mutex_lock(&call_state_change_mutex);
lh2afc7732022-01-10 02:24:31 -0800295 ret = pthread_cond_timedwait(&call_state_change_cond,&call_state_change_mutex,&timeout);
lh7b0674a2022-01-10 00:34:35 -0800296 pthread_mutex_unlock(&call_state_change_mutex);
297 return ret;
298}
299int waitIncomingCall()
300{
301 int ret = 0;
302 pthread_mutex_lock(&s_incoming_call_mutex);
303 ret = pthread_cond_wait(&s_incoming_call_cond,&s_incoming_call_mutex);
304 pthread_mutex_unlock(&s_incoming_call_mutex);
305 return ret;
306}
307int checkHasCall(char addr[])
308{
309 for(int i = 0;i<LYNQ_CALL_MAX;i++)
310 {
311 if(strcmp(lynq_call_lists[i].addr,addr)==0)
312 {
313 return 1;
314 }
315 }
316 return 0;
317}
318void sendSignalToWaitCallStateChange()
319{
320 pthread_mutex_lock(&call_state_change_mutex);
321 pthread_cond_signal(&call_state_change_cond);
322 pthread_mutex_unlock(&call_state_change_mutex);
323}
324void sendSignalIncomingCall()
325{
326 pthread_mutex_lock(&s_incoming_call_mutex);
327 pthread_cond_signal(&s_incoming_call_cond);
328 pthread_mutex_unlock(&s_incoming_call_mutex);
329}
330
331void addCallListToLynqCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction,char addr[LYNQ_PHONE_NUMBER_MAX])
332{
333 callList->call_id = call_id;
334 callList->call_state = call_state;
335 callList->toa = toa;
336 callList->direction = direction;
337 memcpy(callList->addr,addr,strlen(addr)+1);
338 callList->used = 1;
339 callList->hasTimeout = 0;
340 return;
341}
342
343void *triggerGetCallList(void *parg)
ll04ae4142022-01-27 05:54:38 +0000344{
345 int ret=0;
346 lynq_call_list_t call_list[LYNQ_CALL_MAX];
lh7b0674a2022-01-10 00:34:35 -0800347 while(call_list_loop)
ll04ae4142022-01-27 05:54:38 +0000348 {
349 pthread_mutex_lock(&s_urc_call_state_change_mutex);
350 pthread_cond_wait(&s_urc_call_state_change_cond, &s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800351 LYDBGLOG("triggerGetCallList event!!!\n");
ll04ae4142022-01-27 05:54:38 +0000352 memset(call_list,0,sizeof(call_list));
353 ret = lynq_get_current_call_list(call_list);
354 if(ret != 0)
355 {
356 printf("get current call list failure!!!\n");
357 break;
358 }
lh7b0674a2022-01-10 00:34:35 -0800359 for(int i = 0;i < LYNQ_CALL_MAX;i++)
360 {
361 if(call_list[i].direction == 1)//MT call
362 {
363 if(call_list[i].call_state ==4)//LYNQ_CALL_INCOMING = 4, /* MT call only */
364 {
365 if(!checkHasCall(call_list[i].addr))
366 {
367 lynqIncomingCallId = getUnusedElement();
368 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);
369 sendSignalIncomingCall();
370 }
371 }
372 }
373 else
374 {
375 if(call_list[i].call_id==0)
376 {
377 break;
378 }
379 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
380 {
381 if(lynq_call_lists[n].hasTimeout==1)
382 {
383 /*hangup call with id*/
384 lynq_call_hungup(&n);
385 lynq_call_lists[n].hasTimeout==0;
386 continue;
387 }
388 if(strcmp(call_list[i].addr,lynq_call_lists[n].addr)==0)
389 {
390 updateCallList(&lynq_call_lists[n],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction);
391 }
392 }
393 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,
394 call_list[i].direction,call_list[i].addr,call_list[i].toa);
395 }
ll04ae4142022-01-27 05:54:38 +0000396 }
lh42c1e572022-01-25 18:47:39 -0800397 s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -0800398 if(isDial==1)
399 {
400 sendSignalToWaitCallStateChange();
401 isDial = 0;
402 }
ll04ae4142022-01-27 05:54:38 +0000403 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
404 }
405 return NULL;
lh7b0674a2022-01-10 00:34:35 -0800406}
407
408void lynqRespWatingEvent()
ll04ae4142022-01-27 05:54:38 +0000409{
410 if(s_call_urc_event_complete==1)
411 {
412 pthread_mutex_lock(&s_urc_call_state_change_mutex);
413 pthread_cond_signal(&s_urc_call_state_change_cond);
414 s_call_urc_event_complete = 0;
415 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800416 }
ll04ae4142022-01-27 05:54:38 +0000417 return;
lh7b0674a2022-01-10 00:34:35 -0800418}
419
420/*Warren add for T800 platform 2021/11/19 start*/
421int lynq_socket_client_start()
rjw5d2a50e2022-02-28 15:01:49 +0800422{
423 #if 0
lh7b0674a2022-01-10 00:34:35 -0800424 struct sockaddr_in lynq_socket_server_addr;
425 /* init lynq_socket_server_addr */
426 bzero(&lynq_socket_server_addr, sizeof(lynq_socket_server_addr));
427 lynq_socket_server_addr.sin_family = AF_INET;
428 lynq_socket_server_addr.sin_port = htons(LYNQ_SERVICE_PORT);
429 lynq_socket_server_addr.sin_addr.s_addr = htons(INADDR_ANY);
rjw5d2a50e2022-02-28 15:01:49 +0800430
lh7b0674a2022-01-10 00:34:35 -0800431 /*
432 if(inet_pton(AF_INET,"127.0.0.1", &lynq_socket_server_addr.sin_addr) <= 0)
433 {
434 printf("[%s] is not a valid IPaddress\n", argv[1]);
435 exit(1);
436 }
437*/
lh42c1e572022-01-25 18:47:39 -0800438 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
439 if(connect(lynq_call_client_sockfd, (struct sockaddr *)&lynq_socket_server_addr, sizeof(lynq_socket_server_addr)) == -1)
lh7b0674a2022-01-10 00:34:35 -0800440 {
ll3fe03462022-03-01 09:18:53 +0000441 LYERRLOG("connect error\n");
lh7b0674a2022-01-10 00:34:35 -0800442 return -1;
443 }
rjw5d2a50e2022-02-28 15:01:49 +0800444 #endif
445 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
446 if (-1 == lynq_call_client_sockfd)
447 {
448 return lynq_call_client_sockfd;
449 }
450 /* 设置address */
451 memset(&addr_serv, 0, sizeof(addr_serv));
452 addr_serv.sin_family = AF_INET;
453 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
454 addr_serv.sin_port = htons(LYNQ_SERVICE_PORT);
455 len_addr_serv = sizeof(addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800456 return 0;
457}
458int lynq_update_call_list_loop()
459{
460 int ret = 0;
lh7b0674a2022-01-10 00:34:35 -0800461 pthread_attr_t attr;
462 pthread_attr_init(&attr);
463 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800464 ret = pthread_create(&lynq_call_list_loop_tid,&attr,triggerGetCallList,NULL);
lh7b0674a2022-01-10 00:34:35 -0800465 if(ret < 0)
466 {
467 LYERRLOG("lynq_update_call_list_loop fail!!!");
468 return -1;
469 }
470 LYDBGLOG("lynq_update_call_list_loop success!!!\n");
471 return 0;
472
473}
474void *thread_urc_recv(void *parg)
ll04ae4142022-01-27 05:54:38 +0000475{
lh7b0674a2022-01-10 00:34:35 -0800476 int socket_fd = (int64_t)parg;
477 int len=0;
478 socklen_t addr_len=0;
479 uint8_t *dataLength = NULL;
480 char urc_data[LYNQ_REC_BUF];
481 int slot_id = -1;
482 int resp_type = -1;
483 int urcid = -1;
484 Parcel *p = NULL;
485 struct sockaddr_in dest_addr;
486 LYINFLOG("thread_urc_recv in running....\n");
lh42c1e572022-01-25 18:47:39 -0800487 while(urc_call_recive_status)
lh7b0674a2022-01-10 00:34:35 -0800488 {
489 bzero(urc_data,LYNQ_REC_BUF);
490 //get data msg
491 len = recvfrom(socket_fd,urc_data,LYNQ_REC_BUF,0,(struct sockaddr *)&dest_addr,&addr_len);
492 if(len <= 0)
ll04ae4142022-01-27 05:54:38 +0000493 {
ll3fe03462022-03-01 09:18:53 +0000494 LYERRLOG("thread_urc_recv step2 fail\n");
ll04ae4142022-01-27 05:54:38 +0000495 break;
lh7b0674a2022-01-10 00:34:35 -0800496 }
497 LYDBGLOG("=====>urc data len<=====:%d\n",len);
498 p = new Parcel();
499 if(p==NULL)
500 {
lh21502f52022-01-27 00:27:12 -0800501 LYERRLOG("new parcel failure!!!");
lh7b0674a2022-01-10 00:34:35 -0800502 break;
503 }
504 p->setData((uint8_t *)urc_data,len); // p.setData((uint8_t *) buffer, buflen);
505 p->setDataPosition(0);
506 if(p->dataAvail() > 0)
507 {
508 p->readInt32(&resp_type);
509 p->readInt32(&urcid);
510 p->readInt32(&slot_id);
511 //LYDBGLOG("*******Warren test*******:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
512 switch (urcid)
513 {
514 case 1001://RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED
515 {
516 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
517 lynqRespWatingEvent();
518 break;
519 }
520 case 1018://RIL_UNSOL_CALL_RING
521 {
522 if(global_call_auto_answer==1)
523 {
524 lynq_call_answer();
525 }
526 break;
527 }
528 case 1029://RIL_UNSOL_RINGBACK_TONE
529 case 3049://RIL_UNSOL_CALL_INFO_INDICATION
530 {
531 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
532 break;
533 }
534 default:
535 break;
536 }
537 }
538 delete p;
539 p = NULL;
540 }
541 close(socket_fd);
542}
543int lynq_socket_urc_start()
544{
545 int socket_fd=0;
546 int rt=0;
547 int len=0;
548 int on=1;
549 struct sockaddr_in urc_local_addr;
lh7b0674a2022-01-10 00:34:35 -0800550 pthread_attr_t attr;
ll04ae4142022-01-27 05:54:38 +0000551 socket_fd = socket(AF_INET,SOCK_DGRAM,0);
ll04ae4142022-01-27 05:54:38 +0000552 if(socket_fd < 0)
553 {
ll3fe03462022-03-01 09:18:53 +0000554 LYERRLOG("creaet socket for udp fail\n");
ll04ae4142022-01-27 05:54:38 +0000555 return -1;
lh7b0674a2022-01-10 00:34:35 -0800556 }
557 urc_local_addr.sin_family = AF_INET;
558 urc_local_addr.sin_port = htons(LYNQ_URC_SERVICE_PORT);
559 urc_local_addr.sin_addr.s_addr = htons(INADDR_ANY);
560 /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
ll04ae4142022-01-27 05:54:38 +0000561 rt = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
562 if(rt<0)
563 {
ll3fe03462022-03-01 09:18:53 +0000564 LYERRLOG("SO_REUSEADDR fail\n");
lh7b0674a2022-01-10 00:34:35 -0800565 return -1;
566 }
567 rt = bind(socket_fd ,(struct sockaddr*)&urc_local_addr, sizeof(urc_local_addr));
ll04ae4142022-01-27 05:54:38 +0000568 if (rt == -1)
569 {
lh21502f52022-01-27 00:27:12 -0800570 LYERRLOG("bind failed");
ll04ae4142022-01-27 05:54:38 +0000571 return -1;
lh7b0674a2022-01-10 00:34:35 -0800572 }
573 pthread_attr_init(&attr);
574 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800575 rt = pthread_create(&lynq_call_urc_tid,&attr,thread_urc_recv,(void *)socket_fd);
lh7b0674a2022-01-10 00:34:35 -0800576 if(rt < 0)
577 {
lh21502f52022-01-27 00:27:12 -0800578 LYERRLOG("urc loop failure!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800579 return -1;
580 }
lh21502f52022-01-27 00:27:12 -0800581 LYDBGLOG("urc loop success!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800582 return 0;
583}
584int getSelfElement(char addr[])
585{
586 for(int i=0;i < LYNQ_CALL_MAX; i++)
587 {
588 if(lynq_call_lists[i].used==1)
589 {
590 if(strcmp(lynq_call_lists[i].addr,addr)==0)
591 {
592 return i;
593 }
594
595 }
596 }
597 return -1;
598}
599
600void lynq_call_state_change_test(int soc_id)
601{
602 printf("call state change,sim:%d\n",soc_id);
603}
604int lynq_init_call(int uToken)
lla8c25a82022-03-17 05:31:33 +0000605{
606 if(g_lynq_call_init_flag == 1){
607 LYDBGLOG("lynq init call failed!!!");
608 return -1;
609 }
610 g_lynq_call_init_flag = 1;
lh7b0674a2022-01-10 00:34:35 -0800611 int result = 0;
lh42c1e572022-01-25 18:47:39 -0800612 Global_uToken_call = uToken;
lh21502f52022-01-27 00:27:12 -0800613 urc_call_recive_status = 1;
rjw5d2a50e2022-02-28 15:01:49 +0800614 client_size = sizeof(client_t);
lh7b0674a2022-01-10 00:34:35 -0800615 LYLOGSET(LOG_INFO);
616 LYLOGEINIT(USER_LOG_TAG);
617 result = lynq_socket_client_start();
618 if(result!=0)
619 {
620 return -1;
621 }
622 result = lynq_socket_urc_start();
623 if(result!=0)
624 {
625 return -1;
626 }
627 result = lynq_update_call_list_loop();
628 if(result!=0)
629 {
630 return -1;
631 }
632 memset(lynq_call_lists,0,sizeof(lynq_call_lists));
633 LYDBGLOG("lynq init call success!!!");
634 return 0;
635}
636int lynq_deinit_call()
637{
lla8c25a82022-03-17 05:31:33 +0000638 if(g_lynq_call_init_flag == 0)
lhec17b0a2022-02-13 23:56:05 -0800639 {
lla8c25a82022-03-17 05:31:33 +0000640 LYDBGLOG("lynq_deinit_call failed!!!");
641 return -1;
lhec17b0a2022-02-13 23:56:05 -0800642 }
lla8c25a82022-03-17 05:31:33 +0000643 else
644 {
645 g_lynq_call_init_flag = 0;
646 int ret = -1;
647 if(lynq_call_client_sockfd>0)
648 {
649 close(lynq_call_client_sockfd);
650 }
651 urc_call_recive_status = 0;
652 call_list_loop = 0;
653 if(lynq_call_urc_tid == -1 || lynq_call_list_loop_tid == -1)
654 {
655 return -1;
656 }
657 ret = pthread_cancel(lynq_call_urc_tid);
658 LYDBGLOG("pthread cancel ret = %d",ret);
659 ret = pthread_cancel(lynq_call_list_loop_tid);
660 LYDBGLOG("pthread cancel ret = %d",ret);
661 ret = pthread_join(lynq_call_urc_tid,NULL);
662 LYDBGLOG("pthread join ret = %d",ret);
663 ret = pthread_join(lynq_call_list_loop_tid,NULL);
664 LYDBGLOG("pthread join ret = %d",ret);
665 return 0;
666 }
lh7b0674a2022-01-10 00:34:35 -0800667}
668int lynq_call(int* handle,char addr[])
669{
670 Parcel p;
671 lynq_client_t client;
672 int resp_type = -1;
673 int request = -1;
674 int slot_id = -1;
675 int error = -1;
676 int lynq_call_id = -1;
677 if(addr==NULL)
678 {
679 LYERRLOG("Phone num is null!!!");
680 return -1;
681 }
lh42c1e572022-01-25 18:47:39 -0800682 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800683 client.request = 10;//RIL_REQUEST_DIAL
684 client.paramLen = 2;
685 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
686 memcpy(client.param,addr,strlen(addr)+1);
687 strcat(client.param," 0");
688 LYERRLOG("uToken=%d,request=%d,paralen=%d,param=%s",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800689 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800690 {
691 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800692 return -1;
693 }
lh42c1e572022-01-25 18:47:39 -0800694 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800695 JumpHeader(p,&resp_type,&request,&slot_id,&error);
696 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
697 lynq_call_id = updateAddr(addr);
698 if(error==0)
699 {
700 isDial = 1;
701 if(waitCallstateChange(3000)==ETIMEDOUT)//3000ms
702 {
703 error = LYNQ_E_TIME_OUT;
704 LYERRLOG("timeout:wait Call state fail!!!");
705 lynq_call_lists[lynq_call_id].hasTimeout = 1;
706 return error;
707 }
708 *handle = lynq_call_id;
709 }
710 return error;
711}
712int lynq_call_answer()
713{
714 Parcel p;
715 lynq_client_t client;
716 int resp_type = -1;
717 int request = -1;
718 int slot_id = -1;
719 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800720 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800721 client.request = 40;//RIL_REQUEST_DIAL
722 client.paramLen = 0;
723 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
724 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800725 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800726 {
727 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800728 return -1;
729 }
lh42c1e572022-01-25 18:47:39 -0800730 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800731 JumpHeader(p,&resp_type,&request,&slot_id,&error);
732 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
733 return error;
734}
735int lynq_call_hungup(int* handle)
736{
737 Parcel p;
738 lynq_client_t client;
739 int resp_type = -1;
740 int request = -1;
741 int slot_id = -1;
742 int error = -1;
743 int call_id = 0;
744 int lynq_call_id = 0;
lh42c1e572022-01-25 18:47:39 -0800745 if(handle==NULL||!((*handle>=0)&&(*handle<10)))
746 {
747 LYERRLOG("[%s] illegal input!!!!",__FUNCTION__);
748 return LYNQ_E_CONFLICT;
749 }
750 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800751 client.request = 12;//RIL_REQUEST_HUNGUP
752 client.paramLen = 1;
753 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
754 lynq_call_id = *handle;
755 call_id = lynq_call_lists[lynq_call_id].call_id;
756 sprintf(client.param,"%d",call_id);
757 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800758 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800759 {
760 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800761 return -1;
762 }
lh42c1e572022-01-25 18:47:39 -0800763 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800764 JumpHeader(p,&resp_type,&request,&slot_id,&error);
765 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
766 if(error==0)
767 {
768 cleanCallList(lynq_call_id);
769 }
770 return error;
771}
772int lynq_call_hungup_all()
773{
774 Parcel p;
775 lynq_client_t client;
776 int resp_type = -1;
777 int request = -1;
778 int slot_id = -1;
779 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800780 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800781 client.request = 17;//RIL_REQUEST_UDUB
782 client.paramLen = 0;
783 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
784 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800785 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800786 {
787 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800788 return -1;
789 }
lh42c1e572022-01-25 18:47:39 -0800790 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800791 JumpHeader(p,&resp_type,&request,&slot_id,&error);
792 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
793 return error;
794}
795int lynq_wait_incoming_call(int *handle)
796{
797 waitIncomingCall();
798 *handle = lynqIncomingCallId;
799 LYINFLOG("lynq incoming call id:%d",lynqIncomingCallId);
800 return 0;
801}
802
803int lynq_set_auto_answercall(const int mode)
804{
805 global_call_auto_answer = mode;
806 LYINFLOG("auto answer call mode =%d",mode);
807 return 0;
808}
809int lynq_get_mute_status(int *status)
810{
811 Parcel p;
812 lynq_client_t client;
813 int resp_type = -1;
814 int request = -1;
815 int slot_id = -1;
816 int error = -1;
817 if(status==NULL)
818 {
819 LYERRLOG("status is null");
820 return -1;
821 }
lh42c1e572022-01-25 18:47:39 -0800822 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800823 client.request = 54;//RIL_REQUEST_GET_MUTE
824 client.paramLen = 0;
825 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
826 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800827 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800828 {
829 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800830 return -1;
831 }
lh42c1e572022-01-25 18:47:39 -0800832 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800833 JumpHeader(p,&resp_type,&request,&slot_id,&error);
834 p.readInt32(status);
835 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
836 return error;
837}
lldc99c9b2022-01-24 12:16:22 +0000838
839static int judge_mic(const int enable){
llf512fa32022-02-14 08:58:16 +0000840 switch(enable){
841 case 0:
842 return 1;
843 case 1:
844 return 1;
845 default:
846 return 0;
lldc99c9b2022-01-24 12:16:22 +0000847 }
848}
849
lh7b0674a2022-01-10 00:34:35 -0800850int lynq_set_mute_mic(const int enable)
lldc99c9b2022-01-24 12:16:22 +0000851{
852 if(!judge_mic(enable)){
853 return LYNQ_E_CONFLICT;
854 }
lh7b0674a2022-01-10 00:34:35 -0800855 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;
lh42c1e572022-01-25 18:47:39 -0800861 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800862 client.request = 53;//RIL_REQUEST_SET_MUTE
863 client.paramLen = 1;
864 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
865 sprintf(client.param,"%d",enable);
866 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800867 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800868 {
869 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800870 return -1;
871 }
lh42c1e572022-01-25 18:47:39 -0800872 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800873 JumpHeader(p,&resp_type,&request,&slot_id,&error);
874 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
875 return error;
876}
877int lynq_set_DTMF(const char callnum)
878{
879 Parcel p;
880 lynq_client_t client;
881 int resp_type = -1;
882 int request = -1;
883 int slot_id = -1;
884 int error = -1;
885 if(!lynq_call_state)
886 {
887 LYERRLOG("LYNQ_E_CONFLICT");
888 return LYNQ_E_CONFLICT;
889 }
lh42c1e572022-01-25 18:47:39 -0800890 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800891 client.request = 24;//RIL_REQUEST_DTMF
892 client.paramLen = 1;
893 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
894 sprintf(client.param,"%c",callnum);
895 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800896 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800897 {
898 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800899 return -1;
900 }
lh42c1e572022-01-25 18:47:39 -0800901 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800902 JumpHeader(p,&resp_type,&request,&slot_id,&error);
903 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
904 return error;
905
906}
lldc99c9b2022-01-24 12:16:22 +0000907
908static int judge_volume(const int volume){
909 if(volume < 0 ||volume >36){
910 return 0;
911 }
ll04ae4142022-01-27 05:54:38 +0000912 return 1;
lldc99c9b2022-01-24 12:16:22 +0000913}
914
lh7b0674a2022-01-10 00:34:35 -0800915int lynq_set_DTMF_volume(const int volume)
lldc99c9b2022-01-24 12:16:22 +0000916{
917 if(!judge_volume(volume)){
918 return LYNQ_E_CONFLICT;
919 }
lh7b0674a2022-01-10 00:34:35 -0800920 Parcel p;
921 lynq_client_t client;
922 int resp_type = -1;
923 int request = -1;
924 int slot_id = -1;
925 int error = -1;
926 //if(!lynq_call_state)
927 //{
928 // LYERRLOG("LYNQ_E_CONFLICT");
929 // return LYNQ_E_CONFLICT;
930 //}
lh42c1e572022-01-25 18:47:39 -0800931 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800932 client.request = 8003;//LYNQ_REQUEST_SET_DTMF_VOLUME
933 client.paramLen = 1;
934 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
935 sprintf(client.param,"%d",volume);
936 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800937 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800938 {
939 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800940 return -1;
941 }
lh42c1e572022-01-25 18:47:39 -0800942 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800943 JumpHeader(p,&resp_type,&request,&slot_id,&error);
944 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
945 return 0;
946}
947int lynq_get_current_call_state(int *handle, int *call_state,int *toa,int *direction,char addr[])
948{
949 int lynq_call_id = 0;
950 if(handle==NULL)
951 {
952 return LYNQ_E_NULL_ANONALY;
953 }
954 lynq_call_id = *handle;
955 *call_state = lynq_call_lists[lynq_call_id].call_state;
956 *toa = lynq_call_lists[lynq_call_id].toa;
957 *direction = lynq_call_lists[lynq_call_id].direction;
958 memcpy(addr,lynq_call_lists[lynq_call_id].addr,strlen(lynq_call_lists[lynq_call_id].addr)+1);
959 return 0;
960}
961
962#if 0
963int main(int argc,char **argv)
964{
965 int n = 0;
966 n = lynq_init_call(lynq_call_state_change_test,2222);
967 if(n<0)
968 {
969 printf("lynq init call fail!!!\n");
970 return -1;
971 }
972 printf("lynq call init success!!!\n");
973 char phoneNum[LYNQ_PHONE_NUMBER_MAX];
974 sprintf(phoneNum,"18180053406 0",strlen("18180053406 0")+1);
975 lynq_call(phoneNum);
976 while(1)
977 {
978 sleep(1);
979 }
980 return 0;
981}
982#endif
983/*Warren add for T800 platform 2021/11/19 end*/