blob: e7d0585d4c224d4e2d4f55c805d8b75fbf9cf611 [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;
q.huangec88da92022-03-29 04:17:32 -0400103typedef enum{
104 LYNQ_E_VOLUMN_SET_DTMF,
105 LYNQ_E_VOLUMN_SET_SPEECH
106}LYNQ_E_VOLUMN_SET;
lh7b0674a2022-01-10 00:34:35 -0800107
108int lynq_call_state =CALL_OFF;
lh42c1e572022-01-25 18:47:39 -0800109int lynq_call_client_sockfd = 0;
110int Global_uToken_call = 0;
lh7b0674a2022-01-10 00:34:35 -0800111int global_call_count =0;
112int global_call_auto_answer = 0;
lh42c1e572022-01-25 18:47:39 -0800113bool urc_call_recive_status = 1;
lh7b0674a2022-01-10 00:34:35 -0800114bool call_list_loop = 1;
115int isDial = 0;
116int lynqIncomingCallId = 0;
lla8c25a82022-03-17 05:31:33 +0000117/**
118 * @brief mark call initialization state
119 * 0: deinit state
120 * 1: init state
121 */
122int g_lynq_call_init_flag = 0;
lh7b0674a2022-01-10 00:34:35 -0800123
124int JumpHeader(Parcel &p,int *resp_type,int *request,int *slot_id,int *error)
125{
126 if(p.dataAvail() > 0)
127 {
128 p.readInt32(resp_type);
129 p.readInt32(request);
130 p.readInt32(slot_id);
131 p.readInt32(error);
132 return 0;
133 }
134 else
135 {
136 return -1;
137 }
138}
139int send_request(int sockfd,lynq_client_t *client_tmp)
140{
141 int ret=0;
rjw5d2a50e2022-02-28 15:01:49 +0800142 ret = sendto(sockfd, client_tmp, client_size, 0, (struct sockaddr *)&addr_serv, len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800143 if(ret==-1)
144 {
ll3fe03462022-03-01 09:18:53 +0000145 LYERRLOG("sendto error\n");
lh7b0674a2022-01-10 00:34:35 -0800146 return -1;
147 }
148 return 0;
149}
150
151int get_response(int sockfd,Parcel &p)
152{
153 int len = 0;
154 char recvline[LYNQ_REC_BUF];
155 bzero(recvline,LYNQ_REC_BUF);
156 /* receive data from server */
rjw5d2a50e2022-02-28 15:01:49 +0800157 len = recvfrom(sockfd,recvline,LYNQ_REC_BUF, 0, (struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800158 if(len == -1)
159 {
ll3fe03462022-03-01 09:18:53 +0000160 LYERRLOG("recvfrom error\n");
lh7b0674a2022-01-10 00:34:35 -0800161 return -1;
162 }
lh7b0674a2022-01-10 00:34:35 -0800163 if (recvline != NULL) {
164 p.setData((uint8_t *)recvline,len); // p.setData((uint8_t *) buffer, buflen);
165 p.setDataPosition(0);
166 }
167 return 0;
168}
169static char *strdupReadString(Parcel &p) {
ll04ae4142022-01-27 05:54:38 +0000170 size_t stringlen;
lh7b0674a2022-01-10 00:34:35 -0800171 const char16_t *s16;
172 s16 = p.readString16Inplace(&stringlen);
ll04ae4142022-01-27 05:54:38 +0000173 return strndup16to8(s16, stringlen);
lh7b0674a2022-01-10 00:34:35 -0800174}
175
176int lynq_get_current_call_list(lynq_call_list_t call_list[LYNQ_CALL_MAX])
177{
178 Parcel p;
179 lynq_client_t client;
180 int resp_type = -1;
181 int request = -1;
182 int slot_id = -1;
183 int error = -1;
184 int call_num = 0;
185 int temp = 0;
186 char *remote_phoneNum = NULL;
187 char *remote_name= NULL;
188 char *uusData = NULL;
lh42c1e572022-01-25 18:47:39 -0800189 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800190 client.request = 9;//RIL_REQUEST_GET_CURRENT_CALLS
191 client.paramLen = 0;
192 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
193 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800194 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800195 {
196 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800197 return -1;
198 }
lh42c1e572022-01-25 18:47:39 -0800199 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800200 JumpHeader(p,&resp_type,&request,&slot_id,&error);
201 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
202 if(error == 0)
203 {
204 p.readInt32(&call_num);
205 global_call_count = call_num;
206 if(call_num<=0)
207 {
208 lynq_call_state = CALL_OFF;
lhec17b0a2022-02-13 23:56:05 -0800209 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800210 return 0;
211 }
lhec17b0a2022-02-13 23:56:05 -0800212 lynq_call_state = CALL_ON;
213 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800214 for(int i = 0;i < call_num;i++)
215 {
216 p.readInt32(&temp);
217 call_list[i].call_state = temp;
218 p.readInt32(&call_list[i].call_id);
219 p.readInt32(&call_list[i].toa);
220 p.readInt32(&temp);
221 p.readInt32(&temp);
222 call_list[i].direction = temp;
223 p.readInt32(&temp);
224 p.readInt32(&temp);
225 p.readInt32(&temp);
226 remote_phoneNum = strdupReadString(p);
227 memcpy(call_list[i].addr,remote_phoneNum,strlen(remote_phoneNum));
228 LYINFLOG("call_id=%d,call_state=%d,direction=%d,addr=%s,toa=%d",call_list[i].call_id,call_list[i].call_state,
229 call_list[i].direction,call_list[i].addr,call_list[i].toa);
230 p.readInt32(&temp);
231 remote_name = strdupReadString(p);
232 p.readInt32(&temp);
233 p.readInt32(&temp);
234 if(temp==0)
235 {
236 continue;
237 }
238 p.readInt32(&temp); /* UUS Information is present */
239 p.readInt32(&temp);
240 p.readInt32(&temp);
241 p.read(uusData,temp);
242 }
243 }
244 return 0;
245}
246
247void cleanCallList(int lynq_call_id)
248{
249 lynq_call_lists[lynq_call_id].call_id = 0;
250 lynq_call_lists[lynq_call_id].call_state = 0;
251 lynq_call_lists[lynq_call_id].toa = 0;
252 lynq_call_lists[lynq_call_id].direction = 0;
253 lynq_call_lists[lynq_call_id].used = 0;
254 lynq_call_lists[lynq_call_id].hasTimeout = 0;
255 memset(lynq_call_lists[lynq_call_id].addr,0,sizeof(lynq_call_lists[lynq_call_id].addr));
256}
257int getUnusedElement()
258{
259 for(int i=0;i < LYNQ_CALL_MAX; i++)
260 {
261 if(lynq_call_lists[i].used!=1)
262 {
263 return i;
264 }
265 }
266 return -1;
267}
268int updateAddr(char addr[])
269{
270 int ret = 0;
271 ret = getUnusedElement();
272 memcpy(lynq_call_lists[ret].addr,addr,strlen(addr)+1);
273 lynq_call_lists[ret].used = 1;
274 return ret;
275}
276void updateCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction)
277{
278 callList->call_id = call_id;
279 callList->call_state = call_state;
280 callList->toa = toa;
281 callList->direction = direction;
282 callList->used = 1;
283 callList->hasTimeout = 0;
284 return;
285}
286int waitCallstateChange(int mtime)
287{
288 int ret = 0;
289 int sec = 0;
290 int usec = 0;
lh2afc7732022-01-10 02:24:31 -0800291 struct timeval now;
292 struct timespec timeout;
293 gettimeofday(&now,NULL);
lh7b0674a2022-01-10 00:34:35 -0800294 sec = mtime/1000;
295 usec = mtime%1000;
lh2afc7732022-01-10 02:24:31 -0800296 timeout.tv_sec = now.tv_sec+sec;
297 timeout.tv_nsec = now.tv_usec*1000+usec*1000000;
lh7b0674a2022-01-10 00:34:35 -0800298 pthread_mutex_lock(&call_state_change_mutex);
lh2afc7732022-01-10 02:24:31 -0800299 ret = pthread_cond_timedwait(&call_state_change_cond,&call_state_change_mutex,&timeout);
lh7b0674a2022-01-10 00:34:35 -0800300 pthread_mutex_unlock(&call_state_change_mutex);
301 return ret;
302}
303int waitIncomingCall()
304{
305 int ret = 0;
306 pthread_mutex_lock(&s_incoming_call_mutex);
307 ret = pthread_cond_wait(&s_incoming_call_cond,&s_incoming_call_mutex);
308 pthread_mutex_unlock(&s_incoming_call_mutex);
309 return ret;
310}
311int checkHasCall(char addr[])
312{
313 for(int i = 0;i<LYNQ_CALL_MAX;i++)
314 {
315 if(strcmp(lynq_call_lists[i].addr,addr)==0)
316 {
317 return 1;
318 }
319 }
320 return 0;
321}
322void sendSignalToWaitCallStateChange()
323{
324 pthread_mutex_lock(&call_state_change_mutex);
325 pthread_cond_signal(&call_state_change_cond);
326 pthread_mutex_unlock(&call_state_change_mutex);
327}
328void sendSignalIncomingCall()
329{
330 pthread_mutex_lock(&s_incoming_call_mutex);
331 pthread_cond_signal(&s_incoming_call_cond);
332 pthread_mutex_unlock(&s_incoming_call_mutex);
333}
334
335void addCallListToLynqCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction,char addr[LYNQ_PHONE_NUMBER_MAX])
336{
337 callList->call_id = call_id;
338 callList->call_state = call_state;
339 callList->toa = toa;
340 callList->direction = direction;
341 memcpy(callList->addr,addr,strlen(addr)+1);
342 callList->used = 1;
343 callList->hasTimeout = 0;
344 return;
345}
346
347void *triggerGetCallList(void *parg)
ll04ae4142022-01-27 05:54:38 +0000348{
349 int ret=0;
350 lynq_call_list_t call_list[LYNQ_CALL_MAX];
lh7b0674a2022-01-10 00:34:35 -0800351 while(call_list_loop)
ll04ae4142022-01-27 05:54:38 +0000352 {
353 pthread_mutex_lock(&s_urc_call_state_change_mutex);
354 pthread_cond_wait(&s_urc_call_state_change_cond, &s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800355 LYDBGLOG("triggerGetCallList event!!!\n");
ll04ae4142022-01-27 05:54:38 +0000356 memset(call_list,0,sizeof(call_list));
357 ret = lynq_get_current_call_list(call_list);
358 if(ret != 0)
359 {
360 printf("get current call list failure!!!\n");
361 break;
362 }
lh7b0674a2022-01-10 00:34:35 -0800363 for(int i = 0;i < LYNQ_CALL_MAX;i++)
364 {
365 if(call_list[i].direction == 1)//MT call
366 {
367 if(call_list[i].call_state ==4)//LYNQ_CALL_INCOMING = 4, /* MT call only */
368 {
ll72bf6c12022-03-24 10:22:25 +0800369 /*you call me, and i call you,One party failed to dial*/
lh7b0674a2022-01-10 00:34:35 -0800370 if(!checkHasCall(call_list[i].addr))
371 {
372 lynqIncomingCallId = getUnusedElement();
373 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);
374 sendSignalIncomingCall();
375 }
376 }
ll72bf6c12022-03-24 10:22:25 +0800377 /*if state changed*/
378 else
379 {
380 /*update call state*/
381 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
382 {
383 if(strcmp(call_list[i].addr,lynq_call_lists[n].addr)==0)
384 {
385 updateCallList(&lynq_call_lists[n],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction);
386 }
387 }
388 }
lh7b0674a2022-01-10 00:34:35 -0800389 }
390 else
391 {
392 if(call_list[i].call_id==0)
393 {
394 break;
395 }
396 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
397 {
398 if(lynq_call_lists[n].hasTimeout==1)
399 {
400 /*hangup call with id*/
401 lynq_call_hungup(&n);
402 lynq_call_lists[n].hasTimeout==0;
403 continue;
404 }
405 if(strcmp(call_list[i].addr,lynq_call_lists[n].addr)==0)
406 {
407 updateCallList(&lynq_call_lists[n],call_list[i].call_id,call_list[i].call_state,call_list[i].toa,call_list[i].direction);
408 }
409 }
410 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,
411 call_list[i].direction,call_list[i].addr,call_list[i].toa);
412 }
ll04ae4142022-01-27 05:54:38 +0000413 }
lh42c1e572022-01-25 18:47:39 -0800414 s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -0800415 if(isDial==1)
416 {
417 sendSignalToWaitCallStateChange();
418 isDial = 0;
419 }
ll04ae4142022-01-27 05:54:38 +0000420 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
421 }
422 return NULL;
lh7b0674a2022-01-10 00:34:35 -0800423}
424
425void lynqRespWatingEvent()
ll04ae4142022-01-27 05:54:38 +0000426{
427 if(s_call_urc_event_complete==1)
428 {
429 pthread_mutex_lock(&s_urc_call_state_change_mutex);
430 pthread_cond_signal(&s_urc_call_state_change_cond);
431 s_call_urc_event_complete = 0;
432 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800433 }
ll04ae4142022-01-27 05:54:38 +0000434 return;
lh7b0674a2022-01-10 00:34:35 -0800435}
436
437/*Warren add for T800 platform 2021/11/19 start*/
438int lynq_socket_client_start()
rjw5d2a50e2022-02-28 15:01:49 +0800439{
440 #if 0
lh7b0674a2022-01-10 00:34:35 -0800441 struct sockaddr_in lynq_socket_server_addr;
442 /* init lynq_socket_server_addr */
443 bzero(&lynq_socket_server_addr, sizeof(lynq_socket_server_addr));
444 lynq_socket_server_addr.sin_family = AF_INET;
445 lynq_socket_server_addr.sin_port = htons(LYNQ_SERVICE_PORT);
446 lynq_socket_server_addr.sin_addr.s_addr = htons(INADDR_ANY);
rjw5d2a50e2022-02-28 15:01:49 +0800447
lh7b0674a2022-01-10 00:34:35 -0800448 /*
449 if(inet_pton(AF_INET,"127.0.0.1", &lynq_socket_server_addr.sin_addr) <= 0)
450 {
451 printf("[%s] is not a valid IPaddress\n", argv[1]);
452 exit(1);
453 }
454*/
lh42c1e572022-01-25 18:47:39 -0800455 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
456 if(connect(lynq_call_client_sockfd, (struct sockaddr *)&lynq_socket_server_addr, sizeof(lynq_socket_server_addr)) == -1)
lh7b0674a2022-01-10 00:34:35 -0800457 {
ll3fe03462022-03-01 09:18:53 +0000458 LYERRLOG("connect error\n");
lh7b0674a2022-01-10 00:34:35 -0800459 return -1;
460 }
rjw5d2a50e2022-02-28 15:01:49 +0800461 #endif
462 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
463 if (-1 == lynq_call_client_sockfd)
464 {
465 return lynq_call_client_sockfd;
466 }
467 /* 设置address */
468 memset(&addr_serv, 0, sizeof(addr_serv));
469 addr_serv.sin_family = AF_INET;
470 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
471 addr_serv.sin_port = htons(LYNQ_SERVICE_PORT);
472 len_addr_serv = sizeof(addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800473 return 0;
474}
475int lynq_update_call_list_loop()
476{
477 int ret = 0;
lh7b0674a2022-01-10 00:34:35 -0800478 pthread_attr_t attr;
479 pthread_attr_init(&attr);
480 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800481 ret = pthread_create(&lynq_call_list_loop_tid,&attr,triggerGetCallList,NULL);
lh7b0674a2022-01-10 00:34:35 -0800482 if(ret < 0)
483 {
484 LYERRLOG("lynq_update_call_list_loop fail!!!");
485 return -1;
486 }
487 LYDBGLOG("lynq_update_call_list_loop success!!!\n");
488 return 0;
489
490}
491void *thread_urc_recv(void *parg)
ll04ae4142022-01-27 05:54:38 +0000492{
lh7b0674a2022-01-10 00:34:35 -0800493 int socket_fd = (int64_t)parg;
494 int len=0;
495 socklen_t addr_len=0;
496 uint8_t *dataLength = NULL;
497 char urc_data[LYNQ_REC_BUF];
498 int slot_id = -1;
499 int resp_type = -1;
500 int urcid = -1;
501 Parcel *p = NULL;
502 struct sockaddr_in dest_addr;
503 LYINFLOG("thread_urc_recv in running....\n");
lh42c1e572022-01-25 18:47:39 -0800504 while(urc_call_recive_status)
lh7b0674a2022-01-10 00:34:35 -0800505 {
506 bzero(urc_data,LYNQ_REC_BUF);
507 //get data msg
508 len = recvfrom(socket_fd,urc_data,LYNQ_REC_BUF,0,(struct sockaddr *)&dest_addr,&addr_len);
509 if(len <= 0)
ll04ae4142022-01-27 05:54:38 +0000510 {
ll3fe03462022-03-01 09:18:53 +0000511 LYERRLOG("thread_urc_recv step2 fail\n");
ll04ae4142022-01-27 05:54:38 +0000512 break;
lh7b0674a2022-01-10 00:34:35 -0800513 }
514 LYDBGLOG("=====>urc data len<=====:%d\n",len);
515 p = new Parcel();
516 if(p==NULL)
517 {
lh21502f52022-01-27 00:27:12 -0800518 LYERRLOG("new parcel failure!!!");
lh7b0674a2022-01-10 00:34:35 -0800519 break;
520 }
521 p->setData((uint8_t *)urc_data,len); // p.setData((uint8_t *) buffer, buflen);
522 p->setDataPosition(0);
523 if(p->dataAvail() > 0)
524 {
525 p->readInt32(&resp_type);
526 p->readInt32(&urcid);
527 p->readInt32(&slot_id);
528 //LYDBGLOG("*******Warren test*******:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
529 switch (urcid)
530 {
531 case 1001://RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED
532 {
533 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
534 lynqRespWatingEvent();
535 break;
536 }
537 case 1018://RIL_UNSOL_CALL_RING
538 {
539 if(global_call_auto_answer==1)
540 {
541 lynq_call_answer();
542 }
543 break;
544 }
545 case 1029://RIL_UNSOL_RINGBACK_TONE
546 case 3049://RIL_UNSOL_CALL_INFO_INDICATION
547 {
548 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
549 break;
550 }
551 default:
552 break;
553 }
554 }
555 delete p;
556 p = NULL;
557 }
558 close(socket_fd);
559}
560int lynq_socket_urc_start()
561{
562 int socket_fd=0;
563 int rt=0;
564 int len=0;
565 int on=1;
566 struct sockaddr_in urc_local_addr;
lh7b0674a2022-01-10 00:34:35 -0800567 pthread_attr_t attr;
ll04ae4142022-01-27 05:54:38 +0000568 socket_fd = socket(AF_INET,SOCK_DGRAM,0);
ll04ae4142022-01-27 05:54:38 +0000569 if(socket_fd < 0)
570 {
ll3fe03462022-03-01 09:18:53 +0000571 LYERRLOG("creaet socket for udp fail\n");
ll04ae4142022-01-27 05:54:38 +0000572 return -1;
lh7b0674a2022-01-10 00:34:35 -0800573 }
574 urc_local_addr.sin_family = AF_INET;
575 urc_local_addr.sin_port = htons(LYNQ_URC_SERVICE_PORT);
576 urc_local_addr.sin_addr.s_addr = htons(INADDR_ANY);
577 /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
ll04ae4142022-01-27 05:54:38 +0000578 rt = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
579 if(rt<0)
580 {
ll3fe03462022-03-01 09:18:53 +0000581 LYERRLOG("SO_REUSEADDR fail\n");
lh7b0674a2022-01-10 00:34:35 -0800582 return -1;
583 }
584 rt = bind(socket_fd ,(struct sockaddr*)&urc_local_addr, sizeof(urc_local_addr));
ll04ae4142022-01-27 05:54:38 +0000585 if (rt == -1)
586 {
lh21502f52022-01-27 00:27:12 -0800587 LYERRLOG("bind failed");
ll04ae4142022-01-27 05:54:38 +0000588 return -1;
lh7b0674a2022-01-10 00:34:35 -0800589 }
590 pthread_attr_init(&attr);
591 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800592 rt = pthread_create(&lynq_call_urc_tid,&attr,thread_urc_recv,(void *)socket_fd);
lh7b0674a2022-01-10 00:34:35 -0800593 if(rt < 0)
594 {
lh21502f52022-01-27 00:27:12 -0800595 LYERRLOG("urc loop failure!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800596 return -1;
597 }
lh21502f52022-01-27 00:27:12 -0800598 LYDBGLOG("urc loop success!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800599 return 0;
600}
601int getSelfElement(char addr[])
602{
603 for(int i=0;i < LYNQ_CALL_MAX; i++)
604 {
605 if(lynq_call_lists[i].used==1)
606 {
607 if(strcmp(lynq_call_lists[i].addr,addr)==0)
608 {
609 return i;
610 }
611
612 }
613 }
614 return -1;
615}
616
617void lynq_call_state_change_test(int soc_id)
618{
619 printf("call state change,sim:%d\n",soc_id);
620}
621int lynq_init_call(int uToken)
lla8c25a82022-03-17 05:31:33 +0000622{
623 if(g_lynq_call_init_flag == 1){
624 LYDBGLOG("lynq init call failed!!!");
625 return -1;
626 }
627 g_lynq_call_init_flag = 1;
lh7b0674a2022-01-10 00:34:35 -0800628 int result = 0;
lh42c1e572022-01-25 18:47:39 -0800629 Global_uToken_call = uToken;
lh21502f52022-01-27 00:27:12 -0800630 urc_call_recive_status = 1;
rjw5d2a50e2022-02-28 15:01:49 +0800631 client_size = sizeof(client_t);
lh7b0674a2022-01-10 00:34:35 -0800632 LYLOGSET(LOG_INFO);
633 LYLOGEINIT(USER_LOG_TAG);
634 result = lynq_socket_client_start();
635 if(result!=0)
636 {
637 return -1;
638 }
639 result = lynq_socket_urc_start();
640 if(result!=0)
641 {
642 return -1;
643 }
644 result = lynq_update_call_list_loop();
645 if(result!=0)
646 {
647 return -1;
648 }
649 memset(lynq_call_lists,0,sizeof(lynq_call_lists));
650 LYDBGLOG("lynq init call success!!!");
651 return 0;
652}
653int lynq_deinit_call()
654{
lla8c25a82022-03-17 05:31:33 +0000655 if(g_lynq_call_init_flag == 0)
lhec17b0a2022-02-13 23:56:05 -0800656 {
lla8c25a82022-03-17 05:31:33 +0000657 LYDBGLOG("lynq_deinit_call failed!!!");
658 return -1;
lhec17b0a2022-02-13 23:56:05 -0800659 }
lla8c25a82022-03-17 05:31:33 +0000660 else
661 {
662 g_lynq_call_init_flag = 0;
663 int ret = -1;
664 if(lynq_call_client_sockfd>0)
665 {
666 close(lynq_call_client_sockfd);
667 }
668 urc_call_recive_status = 0;
669 call_list_loop = 0;
670 if(lynq_call_urc_tid == -1 || lynq_call_list_loop_tid == -1)
671 {
672 return -1;
673 }
674 ret = pthread_cancel(lynq_call_urc_tid);
675 LYDBGLOG("pthread cancel ret = %d",ret);
676 ret = pthread_cancel(lynq_call_list_loop_tid);
677 LYDBGLOG("pthread cancel ret = %d",ret);
678 ret = pthread_join(lynq_call_urc_tid,NULL);
679 LYDBGLOG("pthread join ret = %d",ret);
680 ret = pthread_join(lynq_call_list_loop_tid,NULL);
681 LYDBGLOG("pthread join ret = %d",ret);
682 return 0;
683 }
lh7b0674a2022-01-10 00:34:35 -0800684}
685int lynq_call(int* handle,char addr[])
686{
687 Parcel p;
688 lynq_client_t client;
689 int resp_type = -1;
690 int request = -1;
691 int slot_id = -1;
692 int error = -1;
693 int lynq_call_id = -1;
694 if(addr==NULL)
695 {
696 LYERRLOG("Phone num is null!!!");
697 return -1;
698 }
lh42c1e572022-01-25 18:47:39 -0800699 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800700 client.request = 10;//RIL_REQUEST_DIAL
701 client.paramLen = 2;
702 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
703 memcpy(client.param,addr,strlen(addr)+1);
704 strcat(client.param," 0");
705 LYERRLOG("uToken=%d,request=%d,paralen=%d,param=%s",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800706 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800707 {
708 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800709 return -1;
710 }
lh42c1e572022-01-25 18:47:39 -0800711 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800712 JumpHeader(p,&resp_type,&request,&slot_id,&error);
713 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
714 lynq_call_id = updateAddr(addr);
715 if(error==0)
716 {
717 isDial = 1;
ll72bf6c12022-03-24 10:22:25 +0800718 if(waitCallstateChange(6000)==ETIMEDOUT)//6000ms
lh7b0674a2022-01-10 00:34:35 -0800719 {
720 error = LYNQ_E_TIME_OUT;
721 LYERRLOG("timeout:wait Call state fail!!!");
722 lynq_call_lists[lynq_call_id].hasTimeout = 1;
723 return error;
724 }
725 *handle = lynq_call_id;
726 }
727 return error;
728}
729int lynq_call_answer()
730{
731 Parcel p;
732 lynq_client_t client;
733 int resp_type = -1;
734 int request = -1;
735 int slot_id = -1;
736 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800737 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800738 client.request = 40;//RIL_REQUEST_DIAL
739 client.paramLen = 0;
740 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
741 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800742 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800743 {
744 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800745 return -1;
746 }
lh42c1e572022-01-25 18:47:39 -0800747 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800748 JumpHeader(p,&resp_type,&request,&slot_id,&error);
749 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
750 return error;
751}
752int lynq_call_hungup(int* handle)
753{
754 Parcel p;
755 lynq_client_t client;
756 int resp_type = -1;
757 int request = -1;
758 int slot_id = -1;
759 int error = -1;
760 int call_id = 0;
761 int lynq_call_id = 0;
lh42c1e572022-01-25 18:47:39 -0800762 if(handle==NULL||!((*handle>=0)&&(*handle<10)))
763 {
764 LYERRLOG("[%s] illegal input!!!!",__FUNCTION__);
765 return LYNQ_E_CONFLICT;
766 }
767 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800768 client.request = 12;//RIL_REQUEST_HUNGUP
769 client.paramLen = 1;
770 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
771 lynq_call_id = *handle;
772 call_id = lynq_call_lists[lynq_call_id].call_id;
773 sprintf(client.param,"%d",call_id);
774 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800775 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800776 {
777 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800778 return -1;
779 }
lh42c1e572022-01-25 18:47:39 -0800780 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800781 JumpHeader(p,&resp_type,&request,&slot_id,&error);
782 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
783 if(error==0)
784 {
785 cleanCallList(lynq_call_id);
786 }
787 return error;
788}
789int lynq_call_hungup_all()
790{
791 Parcel p;
792 lynq_client_t client;
793 int resp_type = -1;
794 int request = -1;
795 int slot_id = -1;
796 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800797 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800798 client.request = 17;//RIL_REQUEST_UDUB
799 client.paramLen = 0;
800 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
801 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800802 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800803 {
804 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800805 return -1;
806 }
lh42c1e572022-01-25 18:47:39 -0800807 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800808 JumpHeader(p,&resp_type,&request,&slot_id,&error);
809 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
810 return error;
811}
812int lynq_wait_incoming_call(int *handle)
813{
814 waitIncomingCall();
815 *handle = lynqIncomingCallId;
816 LYINFLOG("lynq incoming call id:%d",lynqIncomingCallId);
817 return 0;
818}
819
820int lynq_set_auto_answercall(const int mode)
821{
822 global_call_auto_answer = mode;
823 LYINFLOG("auto answer call mode =%d",mode);
824 return 0;
825}
q.huangec88da92022-03-29 04:17:32 -0400826int lynq_get_current_call_state(int *handle, int *call_state,int *toa,int *direction,char addr[])
827{
828 int lynq_call_id = 0;
829 if(handle==NULL)
830 {
831 return LYNQ_E_NULL_ANONALY;
832 }
833 lynq_call_id = *handle;
834 *call_state = lynq_call_lists[lynq_call_id].call_state;
835 *toa = lynq_call_lists[lynq_call_id].toa;
836 *direction = lynq_call_lists[lynq_call_id].direction;
837 memcpy(addr,lynq_call_lists[lynq_call_id].addr,strlen(lynq_call_lists[lynq_call_id].addr)+1);
838 return 0;
839}
840
841/*audio begin*/
842int lynq_set_common_request(int request_id, int argc, const char* format,...)
843{
844 Parcel p;
845 lynq_client_t client;
846 int resp_type = -1;
847 int request = -1;
848 int slot_id = -1;
849 int error = -1;
850 //if(!lynq_call_state)
851 //{
852 // LYERRLOG("LYNQ_E_CONFLICT");
853 // return LYNQ_E_CONFLICT;
854 //}
855 client.uToken = Global_uToken_call;
856 client.request = request_id;
857 client.paramLen = argc;
858 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
859 va_list args;
860 va_start(args, format);
861 vsnprintf(client.param, LYNQ_REQUEST_PARAM_BUF, format, args);
862 va_end(args);
863 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
864 if(send_request(lynq_call_client_sockfd,&client)==-1)
865 {
866 LYERRLOG("send request fail");
867 return -1;
868 }
869 if(get_response(lynq_call_client_sockfd,p)==0)
870 {
871 JumpHeader(p,&resp_type,&request,&slot_id,&error);
872 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
873 }
874 return error;
875}
876
877int lynq_get_common_request(int request_id, int* status)
lh7b0674a2022-01-10 00:34:35 -0800878{
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(status==NULL)
886 {
887 LYERRLOG("status is null");
888 return -1;
889 }
lh42c1e572022-01-25 18:47:39 -0800890 client.uToken = Global_uToken_call;
q.huangec88da92022-03-29 04:17:32 -0400891 client.request = request_id;
lh7b0674a2022-01-10 00:34:35 -0800892 client.paramLen = 0;
893 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
894 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800895 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800896 {
897 LYERRLOG("send request fail");
lh7b0674a2022-01-10 00:34:35 -0800898 return -1;
899 }
q.huangec88da92022-03-29 04:17:32 -0400900 if(get_response(lynq_call_client_sockfd,p)==0)
901 {
902 JumpHeader(p,&resp_type,&request,&slot_id,&error);
903 p.readInt32(status);
904 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
905 }
lh7b0674a2022-01-10 00:34:35 -0800906 return error;
907}
lldc99c9b2022-01-24 12:16:22 +0000908
909static int judge_mic(const int enable){
llf512fa32022-02-14 08:58:16 +0000910 switch(enable){
911 case 0:
912 return 1;
913 case 1:
914 return 1;
915 default:
916 return 0;
lldc99c9b2022-01-24 12:16:22 +0000917 }
918}
919
lh7b0674a2022-01-10 00:34:35 -0800920int lynq_set_mute_mic(const int enable)
lldc99c9b2022-01-24 12:16:22 +0000921{
922 if(!judge_mic(enable)){
923 return LYNQ_E_CONFLICT;
924 }
q.huangec88da92022-03-29 04:17:32 -0400925 return lynq_set_common_request(53,1,"%d",enable); //RIL_REQUEST_SET_MUTE
926}
927int lynq_get_mute_mic(int *status)
928{
929 return lynq_get_common_request(54,status);//RIL_REQUEST_GET_MUTE
lh7b0674a2022-01-10 00:34:35 -0800930}
ll72bf6c12022-03-24 10:22:25 +0800931
932/**
933 * @brief Check whether DTMF is valid
934 *
935 * @param callnum dtmf eg:0-9 * #
936 * @return int
937 */
938static int judge_dtmf(const char callnum)
939{
940 if(callnum == '#')
941 {
942 return 1;
943 }
944 if(callnum == '*')
945 {
946 return 1;
947 }
948 if(callnum >= '0'&& callnum <= '9')
949 {
950 return 1;
951 }
952 return 0;
953}
954
lh7b0674a2022-01-10 00:34:35 -0800955int lynq_set_DTMF(const char callnum)
956{
ll72bf6c12022-03-24 10:22:25 +0800957 if(!judge_dtmf(callnum))
958 {
959 return LYNQ_E_CONFLICT;
960 }
lh7b0674a2022-01-10 00:34:35 -0800961 if(!lynq_call_state)
962 {
963 LYERRLOG("LYNQ_E_CONFLICT");
964 return LYNQ_E_CONFLICT;
965 }
q.huangec88da92022-03-29 04:17:32 -0400966 return lynq_set_common_request(24,1,"%c",callnum); //RIL_REQUEST_DTMF
lh7b0674a2022-01-10 00:34:35 -0800967}
q.huangec88da92022-03-29 04:17:32 -0400968static int judge_volume(LYNQ_E_VOLUMN_SET set,const int volume){
969 if(set==LYNQ_E_VOLUMN_SET_DTMF){
970 if(volume < 0 ||volume >36){
971 return 0;
972 }
973 }
974 else if (set==LYNQ_E_VOLUMN_SET_SPEECH)
975 {
976 if(volume < 1 ||volume >7){
977 return 0;
978 }
lldc99c9b2022-01-24 12:16:22 +0000979 }
ll04ae4142022-01-27 05:54:38 +0000980 return 1;
lldc99c9b2022-01-24 12:16:22 +0000981}
lh7b0674a2022-01-10 00:34:35 -0800982int lynq_set_DTMF_volume(const int volume)
lldc99c9b2022-01-24 12:16:22 +0000983{
q.huangec88da92022-03-29 04:17:32 -0400984 if(!judge_volume(LYNQ_E_VOLUMN_SET_DTMF,volume)){
lldc99c9b2022-01-24 12:16:22 +0000985 return LYNQ_E_CONFLICT;
986 }
q.huangec88da92022-03-29 04:17:32 -0400987 return lynq_set_common_request(8003,1,"%d",volume);//LYNQ_REQUEST_SET_DTMF_VOLUME
lh7b0674a2022-01-10 00:34:35 -0800988}
q.huangec88da92022-03-29 04:17:32 -0400989int lynq_set_speech_volume(int volume)//mixer_set_volume
lh7b0674a2022-01-10 00:34:35 -0800990{
q.huangec88da92022-03-29 04:17:32 -0400991 if(!judge_volume(LYNQ_E_VOLUMN_SET_SPEECH,volume))
lh7b0674a2022-01-10 00:34:35 -0800992 {
q.huangec88da92022-03-29 04:17:32 -0400993 return LYNQ_E_CONFLICT;
lh7b0674a2022-01-10 00:34:35 -0800994 }
q.huangec88da92022-03-29 04:17:32 -0400995 return lynq_set_common_request(8009,1,"%d",volume); //LYNQ_REQUEST_SET_SPEECH_VOLUME
lh7b0674a2022-01-10 00:34:35 -0800996}
q.huangec88da92022-03-29 04:17:32 -0400997int lynq_get_speech_volume(int* volumn)//mixer_get_volume
998{
999 return lynq_get_common_request(8010,volumn);//LYNQ_REQUEST_GET_SPEECH_VOLUME
1000}
1001int lynq_incall_record_start(char* file_path)
1002{
1003 return lynq_set_common_request(8011,2,"%s\0%s","1",file_path); //LYNQ_REQUEST_RECORD
1004}
1005int lynq_incall_record_stop()
1006{
1007 return lynq_set_common_request(8011,2,"%s\0%s","0",""); //LYNQ_REQUEST_RECORD
1008}
1009/*audio end*/
lh7b0674a2022-01-10 00:34:35 -08001010#if 0
1011int main(int argc,char **argv)
1012{
1013 int n = 0;
1014 n = lynq_init_call(lynq_call_state_change_test,2222);
1015 if(n<0)
1016 {
1017 printf("lynq init call fail!!!\n");
1018 return -1;
1019 }
1020 printf("lynq call init success!!!\n");
1021 char phoneNum[LYNQ_PHONE_NUMBER_MAX];
1022 sprintf(phoneNum,"18180053406 0",strlen("18180053406 0")+1);
1023 lynq_call(phoneNum);
1024 while(1)
1025 {
1026 sleep(1);
1027 }
1028 return 0;
1029}
1030#endif
1031/*Warren add for T800 platform 2021/11/19 end*/