blob: 6fb06f06e588d714b6e4c01eceecd00d2426b788 [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 {
ll72bf6c12022-03-24 10:22:25 +0800365 /*you call me, and i call you,One party failed to dial*/
lh7b0674a2022-01-10 00:34:35 -0800366 if(!checkHasCall(call_list[i].addr))
367 {
368 lynqIncomingCallId = getUnusedElement();
369 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);
370 sendSignalIncomingCall();
371 }
372 }
ll72bf6c12022-03-24 10:22:25 +0800373 /*if state changed*/
374 else
375 {
376 /*update call state*/
377 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
378 {
379 if(strcmp(call_list[i].addr,lynq_call_lists[n].addr)==0)
380 {
381 updateCallList(&lynq_call_lists[n],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction);
382 }
383 }
384 }
lh7b0674a2022-01-10 00:34:35 -0800385 }
386 else
387 {
388 if(call_list[i].call_id==0)
389 {
390 break;
391 }
392 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
393 {
394 if(lynq_call_lists[n].hasTimeout==1)
395 {
396 /*hangup call with id*/
397 lynq_call_hungup(&n);
398 lynq_call_lists[n].hasTimeout==0;
399 continue;
400 }
401 if(strcmp(call_list[i].addr,lynq_call_lists[n].addr)==0)
402 {
403 updateCallList(&lynq_call_lists[n],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction);
404 }
405 }
406 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,
407 call_list[i].direction,call_list[i].addr,call_list[i].toa);
408 }
ll04ae4142022-01-27 05:54:38 +0000409 }
lh42c1e572022-01-25 18:47:39 -0800410 s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -0800411 if(isDial==1)
412 {
413 sendSignalToWaitCallStateChange();
414 isDial = 0;
415 }
ll04ae4142022-01-27 05:54:38 +0000416 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
417 }
418 return NULL;
lh7b0674a2022-01-10 00:34:35 -0800419}
420
421void lynqRespWatingEvent()
ll04ae4142022-01-27 05:54:38 +0000422{
423 if(s_call_urc_event_complete==1)
424 {
425 pthread_mutex_lock(&s_urc_call_state_change_mutex);
426 pthread_cond_signal(&s_urc_call_state_change_cond);
427 s_call_urc_event_complete = 0;
428 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800429 }
ll04ae4142022-01-27 05:54:38 +0000430 return;
lh7b0674a2022-01-10 00:34:35 -0800431}
432
433/*Warren add for T800 platform 2021/11/19 start*/
434int lynq_socket_client_start()
rjw5d2a50e2022-02-28 15:01:49 +0800435{
436 #if 0
lh7b0674a2022-01-10 00:34:35 -0800437 struct sockaddr_in lynq_socket_server_addr;
438 /* init lynq_socket_server_addr */
439 bzero(&lynq_socket_server_addr, sizeof(lynq_socket_server_addr));
440 lynq_socket_server_addr.sin_family = AF_INET;
441 lynq_socket_server_addr.sin_port = htons(LYNQ_SERVICE_PORT);
442 lynq_socket_server_addr.sin_addr.s_addr = htons(INADDR_ANY);
rjw5d2a50e2022-02-28 15:01:49 +0800443
lh7b0674a2022-01-10 00:34:35 -0800444 /*
445 if(inet_pton(AF_INET,"127.0.0.1", &lynq_socket_server_addr.sin_addr) <= 0)
446 {
447 printf("[%s] is not a valid IPaddress\n", argv[1]);
448 exit(1);
449 }
450*/
lh42c1e572022-01-25 18:47:39 -0800451 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
452 if(connect(lynq_call_client_sockfd, (struct sockaddr *)&lynq_socket_server_addr, sizeof(lynq_socket_server_addr)) == -1)
lh7b0674a2022-01-10 00:34:35 -0800453 {
ll3fe03462022-03-01 09:18:53 +0000454 LYERRLOG("connect error\n");
lh7b0674a2022-01-10 00:34:35 -0800455 return -1;
456 }
rjw5d2a50e2022-02-28 15:01:49 +0800457 #endif
458 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
459 if (-1 == lynq_call_client_sockfd)
460 {
461 return lynq_call_client_sockfd;
462 }
463 /* 设置address */
464 memset(&addr_serv, 0, sizeof(addr_serv));
465 addr_serv.sin_family = AF_INET;
466 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
467 addr_serv.sin_port = htons(LYNQ_SERVICE_PORT);
468 len_addr_serv = sizeof(addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800469 return 0;
470}
471int lynq_update_call_list_loop()
472{
473 int ret = 0;
lh7b0674a2022-01-10 00:34:35 -0800474 pthread_attr_t attr;
475 pthread_attr_init(&attr);
476 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800477 ret = pthread_create(&lynq_call_list_loop_tid,&attr,triggerGetCallList,NULL);
lh7b0674a2022-01-10 00:34:35 -0800478 if(ret < 0)
479 {
480 LYERRLOG("lynq_update_call_list_loop fail!!!");
481 return -1;
482 }
483 LYDBGLOG("lynq_update_call_list_loop success!!!\n");
484 return 0;
485
486}
487void *thread_urc_recv(void *parg)
ll04ae4142022-01-27 05:54:38 +0000488{
lh7b0674a2022-01-10 00:34:35 -0800489 int socket_fd = (int64_t)parg;
490 int len=0;
491 socklen_t addr_len=0;
492 uint8_t *dataLength = NULL;
493 char urc_data[LYNQ_REC_BUF];
494 int slot_id = -1;
495 int resp_type = -1;
496 int urcid = -1;
497 Parcel *p = NULL;
498 struct sockaddr_in dest_addr;
499 LYINFLOG("thread_urc_recv in running....\n");
lh42c1e572022-01-25 18:47:39 -0800500 while(urc_call_recive_status)
lh7b0674a2022-01-10 00:34:35 -0800501 {
502 bzero(urc_data,LYNQ_REC_BUF);
503 //get data msg
504 len = recvfrom(socket_fd,urc_data,LYNQ_REC_BUF,0,(struct sockaddr *)&dest_addr,&addr_len);
505 if(len <= 0)
ll04ae4142022-01-27 05:54:38 +0000506 {
ll3fe03462022-03-01 09:18:53 +0000507 LYERRLOG("thread_urc_recv step2 fail\n");
ll04ae4142022-01-27 05:54:38 +0000508 break;
lh7b0674a2022-01-10 00:34:35 -0800509 }
510 LYDBGLOG("=====>urc data len<=====:%d\n",len);
511 p = new Parcel();
512 if(p==NULL)
513 {
lh21502f52022-01-27 00:27:12 -0800514 LYERRLOG("new parcel failure!!!");
lh7b0674a2022-01-10 00:34:35 -0800515 break;
516 }
517 p->setData((uint8_t *)urc_data,len); // p.setData((uint8_t *) buffer, buflen);
518 p->setDataPosition(0);
519 if(p->dataAvail() > 0)
520 {
521 p->readInt32(&resp_type);
522 p->readInt32(&urcid);
523 p->readInt32(&slot_id);
524 //LYDBGLOG("*******Warren test*******:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
525 switch (urcid)
526 {
527 case 1001://RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED
528 {
529 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
530 lynqRespWatingEvent();
531 break;
532 }
533 case 1018://RIL_UNSOL_CALL_RING
534 {
535 if(global_call_auto_answer==1)
536 {
537 lynq_call_answer();
538 }
539 break;
540 }
541 case 1029://RIL_UNSOL_RINGBACK_TONE
542 case 3049://RIL_UNSOL_CALL_INFO_INDICATION
543 {
544 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
545 break;
546 }
547 default:
548 break;
549 }
550 }
551 delete p;
552 p = NULL;
553 }
554 close(socket_fd);
555}
556int lynq_socket_urc_start()
557{
558 int socket_fd=0;
559 int rt=0;
560 int len=0;
561 int on=1;
562 struct sockaddr_in urc_local_addr;
lh7b0674a2022-01-10 00:34:35 -0800563 pthread_attr_t attr;
ll04ae4142022-01-27 05:54:38 +0000564 socket_fd = socket(AF_INET,SOCK_DGRAM,0);
ll04ae4142022-01-27 05:54:38 +0000565 if(socket_fd < 0)
566 {
ll3fe03462022-03-01 09:18:53 +0000567 LYERRLOG("creaet socket for udp fail\n");
ll04ae4142022-01-27 05:54:38 +0000568 return -1;
lh7b0674a2022-01-10 00:34:35 -0800569 }
570 urc_local_addr.sin_family = AF_INET;
571 urc_local_addr.sin_port = htons(LYNQ_URC_SERVICE_PORT);
572 urc_local_addr.sin_addr.s_addr = htons(INADDR_ANY);
573 /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
ll04ae4142022-01-27 05:54:38 +0000574 rt = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
575 if(rt<0)
576 {
ll3fe03462022-03-01 09:18:53 +0000577 LYERRLOG("SO_REUSEADDR fail\n");
lh7b0674a2022-01-10 00:34:35 -0800578 return -1;
579 }
580 rt = bind(socket_fd ,(struct sockaddr*)&urc_local_addr, sizeof(urc_local_addr));
ll04ae4142022-01-27 05:54:38 +0000581 if (rt == -1)
582 {
lh21502f52022-01-27 00:27:12 -0800583 LYERRLOG("bind failed");
ll04ae4142022-01-27 05:54:38 +0000584 return -1;
lh7b0674a2022-01-10 00:34:35 -0800585 }
586 pthread_attr_init(&attr);
587 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800588 rt = pthread_create(&lynq_call_urc_tid,&attr,thread_urc_recv,(void *)socket_fd);
lh7b0674a2022-01-10 00:34:35 -0800589 if(rt < 0)
590 {
lh21502f52022-01-27 00:27:12 -0800591 LYERRLOG("urc loop failure!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800592 return -1;
593 }
lh21502f52022-01-27 00:27:12 -0800594 LYDBGLOG("urc loop success!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800595 return 0;
596}
597int getSelfElement(char addr[])
598{
599 for(int i=0;i < LYNQ_CALL_MAX; i++)
600 {
601 if(lynq_call_lists[i].used==1)
602 {
603 if(strcmp(lynq_call_lists[i].addr,addr)==0)
604 {
605 return i;
606 }
607
608 }
609 }
610 return -1;
611}
612
613void lynq_call_state_change_test(int soc_id)
614{
615 printf("call state change,sim:%d\n",soc_id);
616}
617int lynq_init_call(int uToken)
lla8c25a82022-03-17 05:31:33 +0000618{
619 if(g_lynq_call_init_flag == 1){
620 LYDBGLOG("lynq init call failed!!!");
621 return -1;
622 }
623 g_lynq_call_init_flag = 1;
lh7b0674a2022-01-10 00:34:35 -0800624 int result = 0;
lh42c1e572022-01-25 18:47:39 -0800625 Global_uToken_call = uToken;
lh21502f52022-01-27 00:27:12 -0800626 urc_call_recive_status = 1;
rjw5d2a50e2022-02-28 15:01:49 +0800627 client_size = sizeof(client_t);
lh7b0674a2022-01-10 00:34:35 -0800628 LYLOGSET(LOG_INFO);
629 LYLOGEINIT(USER_LOG_TAG);
630 result = lynq_socket_client_start();
631 if(result!=0)
632 {
633 return -1;
634 }
635 result = lynq_socket_urc_start();
636 if(result!=0)
637 {
638 return -1;
639 }
640 result = lynq_update_call_list_loop();
641 if(result!=0)
642 {
643 return -1;
644 }
645 memset(lynq_call_lists,0,sizeof(lynq_call_lists));
646 LYDBGLOG("lynq init call success!!!");
647 return 0;
648}
649int lynq_deinit_call()
650{
lla8c25a82022-03-17 05:31:33 +0000651 if(g_lynq_call_init_flag == 0)
lhec17b0a2022-02-13 23:56:05 -0800652 {
lla8c25a82022-03-17 05:31:33 +0000653 LYDBGLOG("lynq_deinit_call failed!!!");
654 return -1;
lhec17b0a2022-02-13 23:56:05 -0800655 }
lla8c25a82022-03-17 05:31:33 +0000656 else
657 {
658 g_lynq_call_init_flag = 0;
659 int ret = -1;
660 if(lynq_call_client_sockfd>0)
661 {
662 close(lynq_call_client_sockfd);
663 }
664 urc_call_recive_status = 0;
665 call_list_loop = 0;
666 if(lynq_call_urc_tid == -1 || lynq_call_list_loop_tid == -1)
667 {
668 return -1;
669 }
670 ret = pthread_cancel(lynq_call_urc_tid);
671 LYDBGLOG("pthread cancel ret = %d",ret);
672 ret = pthread_cancel(lynq_call_list_loop_tid);
673 LYDBGLOG("pthread cancel ret = %d",ret);
674 ret = pthread_join(lynq_call_urc_tid,NULL);
675 LYDBGLOG("pthread join ret = %d",ret);
676 ret = pthread_join(lynq_call_list_loop_tid,NULL);
677 LYDBGLOG("pthread join ret = %d",ret);
678 return 0;
679 }
lh7b0674a2022-01-10 00:34:35 -0800680}
681int lynq_call(int* handle,char addr[])
682{
683 Parcel p;
684 lynq_client_t client;
685 int resp_type = -1;
686 int request = -1;
687 int slot_id = -1;
688 int error = -1;
689 int lynq_call_id = -1;
690 if(addr==NULL)
691 {
692 LYERRLOG("Phone num is null!!!");
693 return -1;
694 }
lh42c1e572022-01-25 18:47:39 -0800695 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800696 client.request = 10;//RIL_REQUEST_DIAL
697 client.paramLen = 2;
698 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
699 memcpy(client.param,addr,strlen(addr)+1);
700 strcat(client.param," 0");
701 LYERRLOG("uToken=%d,request=%d,paralen=%d,param=%s",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800702 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800703 {
704 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800705 return -1;
706 }
lh42c1e572022-01-25 18:47:39 -0800707 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800708 JumpHeader(p,&resp_type,&request,&slot_id,&error);
709 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
710 lynq_call_id = updateAddr(addr);
711 if(error==0)
712 {
713 isDial = 1;
ll72bf6c12022-03-24 10:22:25 +0800714 if(waitCallstateChange(6000)==ETIMEDOUT)//6000ms
lh7b0674a2022-01-10 00:34:35 -0800715 {
716 error = LYNQ_E_TIME_OUT;
717 LYERRLOG("timeout:wait Call state fail!!!");
718 lynq_call_lists[lynq_call_id].hasTimeout = 1;
719 return error;
720 }
721 *handle = lynq_call_id;
722 }
723 return error;
724}
725int lynq_call_answer()
726{
727 Parcel p;
728 lynq_client_t client;
729 int resp_type = -1;
730 int request = -1;
731 int slot_id = -1;
732 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800733 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800734 client.request = 40;//RIL_REQUEST_DIAL
735 client.paramLen = 0;
736 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
737 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800738 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800739 {
740 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800741 return -1;
742 }
lh42c1e572022-01-25 18:47:39 -0800743 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800744 JumpHeader(p,&resp_type,&request,&slot_id,&error);
745 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
746 return error;
747}
748int lynq_call_hungup(int* handle)
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;
756 int call_id = 0;
757 int lynq_call_id = 0;
lh42c1e572022-01-25 18:47:39 -0800758 if(handle==NULL||!((*handle>=0)&&(*handle<10)))
759 {
760 LYERRLOG("[%s] illegal input!!!!",__FUNCTION__);
761 return LYNQ_E_CONFLICT;
762 }
763 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800764 client.request = 12;//RIL_REQUEST_HUNGUP
765 client.paramLen = 1;
766 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
767 lynq_call_id = *handle;
768 call_id = lynq_call_lists[lynq_call_id].call_id;
769 sprintf(client.param,"%d",call_id);
770 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800771 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800772 {
773 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800774 return -1;
775 }
lh42c1e572022-01-25 18:47:39 -0800776 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800777 JumpHeader(p,&resp_type,&request,&slot_id,&error);
778 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
779 if(error==0)
780 {
781 cleanCallList(lynq_call_id);
782 }
783 return error;
784}
785int lynq_call_hungup_all()
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;
lh42c1e572022-01-25 18:47:39 -0800793 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800794 client.request = 17;//RIL_REQUEST_UDUB
795 client.paramLen = 0;
796 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
797 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800798 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800799 {
800 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800801 return -1;
802 }
lh42c1e572022-01-25 18:47:39 -0800803 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800804 JumpHeader(p,&resp_type,&request,&slot_id,&error);
805 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
806 return error;
807}
808int lynq_wait_incoming_call(int *handle)
809{
810 waitIncomingCall();
811 *handle = lynqIncomingCallId;
812 LYINFLOG("lynq incoming call id:%d",lynqIncomingCallId);
813 return 0;
814}
815
816int lynq_set_auto_answercall(const int mode)
817{
818 global_call_auto_answer = mode;
819 LYINFLOG("auto answer call mode =%d",mode);
820 return 0;
821}
822int lynq_get_mute_status(int *status)
823{
824 Parcel p;
825 lynq_client_t client;
826 int resp_type = -1;
827 int request = -1;
828 int slot_id = -1;
829 int error = -1;
830 if(status==NULL)
831 {
832 LYERRLOG("status is null");
833 return -1;
834 }
lh42c1e572022-01-25 18:47:39 -0800835 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800836 client.request = 54;//RIL_REQUEST_GET_MUTE
837 client.paramLen = 0;
838 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
839 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800840 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800841 {
842 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800843 return -1;
844 }
lh42c1e572022-01-25 18:47:39 -0800845 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800846 JumpHeader(p,&resp_type,&request,&slot_id,&error);
847 p.readInt32(status);
848 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
849 return error;
850}
lldc99c9b2022-01-24 12:16:22 +0000851
852static int judge_mic(const int enable){
llf512fa32022-02-14 08:58:16 +0000853 switch(enable){
854 case 0:
855 return 1;
856 case 1:
857 return 1;
858 default:
859 return 0;
lldc99c9b2022-01-24 12:16:22 +0000860 }
861}
862
lh7b0674a2022-01-10 00:34:35 -0800863int lynq_set_mute_mic(const int enable)
lldc99c9b2022-01-24 12:16:22 +0000864{
865 if(!judge_mic(enable)){
866 return LYNQ_E_CONFLICT;
867 }
lh7b0674a2022-01-10 00:34:35 -0800868 Parcel p;
869 lynq_client_t client;
870 int resp_type = -1;
871 int request = -1;
872 int slot_id = -1;
873 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800874 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800875 client.request = 53;//RIL_REQUEST_SET_MUTE
876 client.paramLen = 1;
877 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
878 sprintf(client.param,"%d",enable);
879 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800880 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800881 {
882 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800883 return -1;
884 }
lh42c1e572022-01-25 18:47:39 -0800885 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800886 JumpHeader(p,&resp_type,&request,&slot_id,&error);
887 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
888 return error;
889}
ll72bf6c12022-03-24 10:22:25 +0800890
891/**
892 * @brief Check whether DTMF is valid
893 *
894 * @param callnum dtmf eg:0-9 * #
895 * @return int
896 */
897static int judge_dtmf(const char callnum)
898{
899 if(callnum == '#')
900 {
901 return 1;
902 }
903 if(callnum == '*')
904 {
905 return 1;
906 }
907 if(callnum >= '0'&& callnum <= '9')
908 {
909 return 1;
910 }
911 return 0;
912}
913
lh7b0674a2022-01-10 00:34:35 -0800914int lynq_set_DTMF(const char callnum)
915{
ll72bf6c12022-03-24 10:22:25 +0800916 if(!judge_dtmf(callnum))
917 {
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 = 24;//RIL_REQUEST_DTMF
933 client.paramLen = 1;
934 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
935 sprintf(client.param,"%c",callnum);
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 error;
946
947}
lldc99c9b2022-01-24 12:16:22 +0000948
949static int judge_volume(const int volume){
950 if(volume < 0 ||volume >36){
951 return 0;
952 }
ll04ae4142022-01-27 05:54:38 +0000953 return 1;
lldc99c9b2022-01-24 12:16:22 +0000954}
955
lh7b0674a2022-01-10 00:34:35 -0800956int lynq_set_DTMF_volume(const int volume)
lldc99c9b2022-01-24 12:16:22 +0000957{
958 if(!judge_volume(volume)){
959 return LYNQ_E_CONFLICT;
960 }
lh7b0674a2022-01-10 00:34:35 -0800961 Parcel p;
962 lynq_client_t client;
963 int resp_type = -1;
964 int request = -1;
965 int slot_id = -1;
966 int error = -1;
967 //if(!lynq_call_state)
968 //{
969 // LYERRLOG("LYNQ_E_CONFLICT");
970 // return LYNQ_E_CONFLICT;
971 //}
lh42c1e572022-01-25 18:47:39 -0800972 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800973 client.request = 8003;//LYNQ_REQUEST_SET_DTMF_VOLUME
974 client.paramLen = 1;
975 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
976 sprintf(client.param,"%d",volume);
977 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800978 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800979 {
980 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800981 return -1;
982 }
lh42c1e572022-01-25 18:47:39 -0800983 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800984 JumpHeader(p,&resp_type,&request,&slot_id,&error);
985 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
986 return 0;
987}
988int lynq_get_current_call_state(int *handle, int *call_state,int *toa,int *direction,char addr[])
989{
990 int lynq_call_id = 0;
991 if(handle==NULL)
992 {
993 return LYNQ_E_NULL_ANONALY;
994 }
995 lynq_call_id = *handle;
996 *call_state = lynq_call_lists[lynq_call_id].call_state;
997 *toa = lynq_call_lists[lynq_call_id].toa;
998 *direction = lynq_call_lists[lynq_call_id].direction;
999 memcpy(addr,lynq_call_lists[lynq_call_id].addr,strlen(lynq_call_lists[lynq_call_id].addr)+1);
1000 return 0;
1001}
1002
1003#if 0
1004int main(int argc,char **argv)
1005{
1006 int n = 0;
1007 n = lynq_init_call(lynq_call_state_change_test,2222);
1008 if(n<0)
1009 {
1010 printf("lynq init call fail!!!\n");
1011 return -1;
1012 }
1013 printf("lynq call init success!!!\n");
1014 char phoneNum[LYNQ_PHONE_NUMBER_MAX];
1015 sprintf(phoneNum,"18180053406 0",strlen("18180053406 0")+1);
1016 lynq_call(phoneNum);
1017 while(1)
1018 {
1019 sleep(1);
1020 }
1021 return 0;
1022}
1023#endif
1024/*Warren add for T800 platform 2021/11/19 end*/