blob: 1f711654d148b2c92f06c5b54d7b9626a0825496 [file] [log] [blame]
lh7b0674a2022-01-10 00:34:35 -08001#include <stdio.h>
2#include <sys/types.h>
3#include <sys/socket.h>
4#include <arpa/inet.h>
5#include <fcntl.h>
6#include <string.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <binder/Parcel.h>
10#include <log/log.h>
11#include "lynq_call.h"
12#include <cutils/jstring.h>
13#include <pthread.h>
14#include "liblog/lynq_deflog.h"
lh2afc7732022-01-10 02:24:31 -080015#include <sys/time.h>
lh21502f52022-01-27 00:27:12 -080016#include <string.h>
lh7b0674a2022-01-10 00:34:35 -080017#define LYNQ_SERVICE_PORT 8088
rjw5d2a50e2022-02-28 15:01:49 +080018#define DSET_IP_ADDRESS "127.0.0.1"
lh7b0674a2022-01-10 00:34:35 -080019#define LYNQ_URC_SERVICE_PORT 8086
20#define LYNQ_REC_BUF 8192
21#define LYNQ_REQUEST_PARAM_BUF 8192
22#define LYQN_SEDN_BUF 1024*8+sizeof(int)*3
23#define USER_LOG_TAG "LYNQ_CALL"
24
25using ::android::Parcel;
26 typedef enum {
27 LYNQ_CALL_ACTIVE = 0,
28 LYNQ_CALL_HOLDING = 1,
29 LYNQ_CALL_DIALING = 2, /* MO call only */
30 LYNQ_CALL_ALERTING = 3, /* MO call only */
31 LYNQ_CALL_INCOMING = 4, /* MT call only */
32 LYNQ_CALL_WAITING = 5 ,/* MT call only */
33}lynq_call_state_t;
34
35typedef struct{
36 int uToken;
37 int request;
38 int paramLen;
39 char param[LYNQ_REQUEST_PARAM_BUF];
40}lynq_client_t;
41typedef struct
42{
43 int used;
44 int call_id;
45 int call_state;
46 int toa;
47 int direction;/*0: MO call,1:MT call*/
48 char addr[LYNQ_PHONE_NUMBER_MAX];
49 int hasTimeout;
50}lynq_call_list_e_t;
51typedef struct
52{
53 int call_id;
54 int call_state;
55 int toa;
56 int direction;/*0: MO call,1:MT call*/
57 char addr[LYNQ_PHONE_NUMBER_MAX];
58}lynq_call_list_t;
59
60lynq_call_list_e_t lynq_call_lists[LYNQ_CALL_MAX]={};
61static pthread_mutex_t call_state_change_mutex = PTHREAD_MUTEX_INITIALIZER;
62static pthread_cond_t call_state_change_cond = PTHREAD_COND_INITIALIZER;
ll04ae4142022-01-27 05:54:38 +000063static pthread_mutex_t s_urc_call_state_change_mutex = PTHREAD_MUTEX_INITIALIZER;
lh7b0674a2022-01-10 00:34:35 -080064static pthread_cond_t s_urc_call_state_change_cond = PTHREAD_COND_INITIALIZER;
65static pthread_mutex_t s_incoming_call_mutex = PTHREAD_MUTEX_INITIALIZER;
66static pthread_cond_t s_incoming_call_cond = PTHREAD_COND_INITIALIZER;
lhec17b0a2022-02-13 23:56:05 -080067pthread_t lynq_call_urc_tid;
68pthread_t lynq_call_list_loop_tid;
69
rjw5d2a50e2022-02-28 15:01:49 +080070/*lei add*/
71/* socket文件描述符 */
72int len_addr_serv;
73struct sockaddr_in addr_serv;
74lynq_client_t client_t;
75int client_size = 0;
76/*lei add*/
lh7b0674a2022-01-10 00:34:35 -080077
lh42c1e572022-01-25 18:47:39 -080078int s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -080079
80enum{
81 CALL_OFF=0,
82 CALL_ON=1
83}call_state;
ll04ae4142022-01-27 05:54:38 +000084typedef enum{
85 LYNQ_E_CARDSTATE_ERROR=8000,
86 /* The voice service state is out of service*/
87 LYNQ_E_STATE_OUT_OF_SERVICE=8001,
88 /* The voice service state is EMERGENCY_ONLY*/
89 LYNQ_E_STATE_EMERGENCY_ONLY=8002,
90 /* The radio power is power off*/
91 LYNQ_E_STATE_POWER_OFF=8003,
92 LYNQ_E_TIME_OUT=8004,
93 /*create or open sms DB fail */
94 LYNQ_E_SMS_DB_FAIL=8005,
95 /*Failed to execute sql statement*/
96 LYNQ_E_SMS_SQL_FAIL = 8006,
97 LYNQ_E_SMS_NOT_FIND = 8007,
98 /* The logic conflict*/
99 LYNQ_E_CONFLICT=9000,
100 /*Null anomaly*/
101 LYNQ_E_NULL_ANONALY=9001
lh7b0674a2022-01-10 00:34:35 -0800102}LYNQ_E;
103
104int lynq_call_state =CALL_OFF;
lh42c1e572022-01-25 18:47:39 -0800105int lynq_call_client_sockfd = 0;
106int Global_uToken_call = 0;
lh7b0674a2022-01-10 00:34:35 -0800107int global_call_count =0;
108int global_call_auto_answer = 0;
lh42c1e572022-01-25 18:47:39 -0800109bool urc_call_recive_status = 1;
lh7b0674a2022-01-10 00:34:35 -0800110bool call_list_loop = 1;
111int isDial = 0;
112int lynqIncomingCallId = 0;
113
114int JumpHeader(Parcel &p,int *resp_type,int *request,int *slot_id,int *error)
115{
116 if(p.dataAvail() > 0)
117 {
118 p.readInt32(resp_type);
119 p.readInt32(request);
120 p.readInt32(slot_id);
121 p.readInt32(error);
122 return 0;
123 }
124 else
125 {
126 return -1;
127 }
128}
129int send_request(int sockfd,lynq_client_t *client_tmp)
130{
131 int ret=0;
rjw5d2a50e2022-02-28 15:01:49 +0800132 ret = sendto(sockfd, client_tmp, client_size, 0, (struct sockaddr *)&addr_serv, len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800133 if(ret==-1)
134 {
lh21502f52022-01-27 00:27:12 -0800135 LYERRLOG("write error:%s",strerror(errno));
lh7b0674a2022-01-10 00:34:35 -0800136 return -1;
137 }
138 return 0;
139}
140
141int get_response(int sockfd,Parcel &p)
142{
143 int len = 0;
144 char recvline[LYNQ_REC_BUF];
145 bzero(recvline,LYNQ_REC_BUF);
146 /* receive data from server */
rjw5d2a50e2022-02-28 15:01:49 +0800147 len = recvfrom(sockfd,recvline,LYNQ_REC_BUF, 0, (struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800148 if(len == -1)
149 {
lh21502f52022-01-27 00:27:12 -0800150 LYERRLOG("read error:%s",strerror(errno));
lh7b0674a2022-01-10 00:34:35 -0800151 return -1;
152 }
lh7b0674a2022-01-10 00:34:35 -0800153 if (recvline != NULL) {
154 p.setData((uint8_t *)recvline,len); // p.setData((uint8_t *) buffer, buflen);
155 p.setDataPosition(0);
156 }
157 return 0;
158}
159static char *strdupReadString(Parcel &p) {
ll04ae4142022-01-27 05:54:38 +0000160 size_t stringlen;
lh7b0674a2022-01-10 00:34:35 -0800161 const char16_t *s16;
162 s16 = p.readString16Inplace(&stringlen);
ll04ae4142022-01-27 05:54:38 +0000163 return strndup16to8(s16, stringlen);
lh7b0674a2022-01-10 00:34:35 -0800164}
165
166int lynq_get_current_call_list(lynq_call_list_t call_list[LYNQ_CALL_MAX])
167{
168 Parcel p;
169 lynq_client_t client;
170 int resp_type = -1;
171 int request = -1;
172 int slot_id = -1;
173 int error = -1;
174 int call_num = 0;
175 int temp = 0;
176 char *remote_phoneNum = NULL;
177 char *remote_name= NULL;
178 char *uusData = NULL;
lh42c1e572022-01-25 18:47:39 -0800179 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800180 client.request = 9;//RIL_REQUEST_GET_CURRENT_CALLS
181 client.paramLen = 0;
182 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
183 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800184 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800185 {
186 LYERRLOG("send request fail");
187 perror("[LYNQ_CALL] send request fail:");
188 return -1;
189 }
lh42c1e572022-01-25 18:47:39 -0800190 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800191 JumpHeader(p,&resp_type,&request,&slot_id,&error);
192 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
193 if(error == 0)
194 {
195 p.readInt32(&call_num);
196 global_call_count = call_num;
197 if(call_num<=0)
198 {
199 lynq_call_state = CALL_OFF;
lhec17b0a2022-02-13 23:56:05 -0800200 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800201 return 0;
202 }
lhec17b0a2022-02-13 23:56:05 -0800203 lynq_call_state = CALL_ON;
204 LYINFLOG("lynq_call_state:%d",lynq_call_state);
lh7b0674a2022-01-10 00:34:35 -0800205 for(int i = 0;i < call_num;i++)
206 {
207 p.readInt32(&temp);
208 call_list[i].call_state = temp;
209 p.readInt32(&call_list[i].call_id);
210 p.readInt32(&call_list[i].toa);
211 p.readInt32(&temp);
212 p.readInt32(&temp);
213 call_list[i].direction = temp;
214 p.readInt32(&temp);
215 p.readInt32(&temp);
216 p.readInt32(&temp);
217 remote_phoneNum = strdupReadString(p);
218 memcpy(call_list[i].addr,remote_phoneNum,strlen(remote_phoneNum));
219 LYINFLOG("call_id=%d,call_state=%d,direction=%d,addr=%s,toa=%d",call_list[i].call_id,call_list[i].call_state,
220 call_list[i].direction,call_list[i].addr,call_list[i].toa);
221 p.readInt32(&temp);
222 remote_name = strdupReadString(p);
223 p.readInt32(&temp);
224 p.readInt32(&temp);
225 if(temp==0)
226 {
227 continue;
228 }
229 p.readInt32(&temp); /* UUS Information is present */
230 p.readInt32(&temp);
231 p.readInt32(&temp);
232 p.read(uusData,temp);
233 }
234 }
235 return 0;
236}
237
238void cleanCallList(int lynq_call_id)
239{
240 lynq_call_lists[lynq_call_id].call_id = 0;
241 lynq_call_lists[lynq_call_id].call_state = 0;
242 lynq_call_lists[lynq_call_id].toa = 0;
243 lynq_call_lists[lynq_call_id].direction = 0;
244 lynq_call_lists[lynq_call_id].used = 0;
245 lynq_call_lists[lynq_call_id].hasTimeout = 0;
246 memset(lynq_call_lists[lynq_call_id].addr,0,sizeof(lynq_call_lists[lynq_call_id].addr));
247}
248int getUnusedElement()
249{
250 for(int i=0;i < LYNQ_CALL_MAX; i++)
251 {
252 if(lynq_call_lists[i].used!=1)
253 {
254 return i;
255 }
256 }
257 return -1;
258}
259int updateAddr(char addr[])
260{
261 int ret = 0;
262 ret = getUnusedElement();
263 memcpy(lynq_call_lists[ret].addr,addr,strlen(addr)+1);
264 lynq_call_lists[ret].used = 1;
265 return ret;
266}
267void updateCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction)
268{
269 callList->call_id = call_id;
270 callList->call_state = call_state;
271 callList->toa = toa;
272 callList->direction = direction;
273 callList->used = 1;
274 callList->hasTimeout = 0;
275 return;
276}
277int waitCallstateChange(int mtime)
278{
279 int ret = 0;
280 int sec = 0;
281 int usec = 0;
lh2afc7732022-01-10 02:24:31 -0800282 struct timeval now;
283 struct timespec timeout;
284 gettimeofday(&now,NULL);
lh7b0674a2022-01-10 00:34:35 -0800285 sec = mtime/1000;
286 usec = mtime%1000;
lh2afc7732022-01-10 02:24:31 -0800287 timeout.tv_sec = now.tv_sec+sec;
288 timeout.tv_nsec = now.tv_usec*1000+usec*1000000;
lh7b0674a2022-01-10 00:34:35 -0800289 pthread_mutex_lock(&call_state_change_mutex);
lh2afc7732022-01-10 02:24:31 -0800290 ret = pthread_cond_timedwait(&call_state_change_cond,&call_state_change_mutex,&timeout);
lh7b0674a2022-01-10 00:34:35 -0800291 pthread_mutex_unlock(&call_state_change_mutex);
292 return ret;
293}
294int waitIncomingCall()
295{
296 int ret = 0;
297 pthread_mutex_lock(&s_incoming_call_mutex);
298 ret = pthread_cond_wait(&s_incoming_call_cond,&s_incoming_call_mutex);
299 pthread_mutex_unlock(&s_incoming_call_mutex);
300 return ret;
301}
302int checkHasCall(char addr[])
303{
304 for(int i = 0;i<LYNQ_CALL_MAX;i++)
305 {
306 if(strcmp(lynq_call_lists[i].addr,addr)==0)
307 {
308 return 1;
309 }
310 }
311 return 0;
312}
313void sendSignalToWaitCallStateChange()
314{
315 pthread_mutex_lock(&call_state_change_mutex);
316 pthread_cond_signal(&call_state_change_cond);
317 pthread_mutex_unlock(&call_state_change_mutex);
318}
319void sendSignalIncomingCall()
320{
321 pthread_mutex_lock(&s_incoming_call_mutex);
322 pthread_cond_signal(&s_incoming_call_cond);
323 pthread_mutex_unlock(&s_incoming_call_mutex);
324}
325
326void addCallListToLynqCallList(lynq_call_list_e_t *callList, int call_id,int call_state,int toa,int direction,char addr[LYNQ_PHONE_NUMBER_MAX])
327{
328 callList->call_id = call_id;
329 callList->call_state = call_state;
330 callList->toa = toa;
331 callList->direction = direction;
332 memcpy(callList->addr,addr,strlen(addr)+1);
333 callList->used = 1;
334 callList->hasTimeout = 0;
335 return;
336}
337
338void *triggerGetCallList(void *parg)
ll04ae4142022-01-27 05:54:38 +0000339{
340 int ret=0;
341 lynq_call_list_t call_list[LYNQ_CALL_MAX];
lh7b0674a2022-01-10 00:34:35 -0800342 while(call_list_loop)
ll04ae4142022-01-27 05:54:38 +0000343 {
344 pthread_mutex_lock(&s_urc_call_state_change_mutex);
345 pthread_cond_wait(&s_urc_call_state_change_cond, &s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800346 LYDBGLOG("triggerGetCallList event!!!\n");
ll04ae4142022-01-27 05:54:38 +0000347 memset(call_list,0,sizeof(call_list));
348 ret = lynq_get_current_call_list(call_list);
349 if(ret != 0)
350 {
351 printf("get current call list failure!!!\n");
352 break;
353 }
lh7b0674a2022-01-10 00:34:35 -0800354 for(int i = 0;i < LYNQ_CALL_MAX;i++)
355 {
356 if(call_list[i].direction == 1)//MT call
357 {
358 if(call_list[i].call_state ==4)//LYNQ_CALL_INCOMING = 4, /* MT call only */
359 {
360 if(!checkHasCall(call_list[i].addr))
361 {
362 lynqIncomingCallId = getUnusedElement();
363 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);
364 sendSignalIncomingCall();
365 }
366 }
367 }
368 else
369 {
370 if(call_list[i].call_id==0)
371 {
372 break;
373 }
374 for(int n = 0 ; n < LYNQ_CALL_MAX; n++)
375 {
376 if(lynq_call_lists[n].hasTimeout==1)
377 {
378 /*hangup call with id*/
379 lynq_call_hungup(&n);
380 lynq_call_lists[n].hasTimeout==0;
381 continue;
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 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,
389 call_list[i].direction,call_list[i].addr,call_list[i].toa);
390 }
ll04ae4142022-01-27 05:54:38 +0000391 }
lh42c1e572022-01-25 18:47:39 -0800392 s_call_urc_event_complete = 1;
lh7b0674a2022-01-10 00:34:35 -0800393 if(isDial==1)
394 {
395 sendSignalToWaitCallStateChange();
396 isDial = 0;
397 }
ll04ae4142022-01-27 05:54:38 +0000398 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
399 }
400 return NULL;
lh7b0674a2022-01-10 00:34:35 -0800401}
402
403void lynqRespWatingEvent()
ll04ae4142022-01-27 05:54:38 +0000404{
405 if(s_call_urc_event_complete==1)
406 {
407 pthread_mutex_lock(&s_urc_call_state_change_mutex);
408 pthread_cond_signal(&s_urc_call_state_change_cond);
409 s_call_urc_event_complete = 0;
410 pthread_mutex_unlock(&s_urc_call_state_change_mutex);
lh7b0674a2022-01-10 00:34:35 -0800411 }
ll04ae4142022-01-27 05:54:38 +0000412 return;
lh7b0674a2022-01-10 00:34:35 -0800413}
414
415/*Warren add for T800 platform 2021/11/19 start*/
416int lynq_socket_client_start()
rjw5d2a50e2022-02-28 15:01:49 +0800417{
418 #if 0
lh7b0674a2022-01-10 00:34:35 -0800419 struct sockaddr_in lynq_socket_server_addr;
420 /* init lynq_socket_server_addr */
421 bzero(&lynq_socket_server_addr, sizeof(lynq_socket_server_addr));
422 lynq_socket_server_addr.sin_family = AF_INET;
423 lynq_socket_server_addr.sin_port = htons(LYNQ_SERVICE_PORT);
424 lynq_socket_server_addr.sin_addr.s_addr = htons(INADDR_ANY);
rjw5d2a50e2022-02-28 15:01:49 +0800425
lh7b0674a2022-01-10 00:34:35 -0800426 /*
427 if(inet_pton(AF_INET,"127.0.0.1", &lynq_socket_server_addr.sin_addr) <= 0)
428 {
429 printf("[%s] is not a valid IPaddress\n", argv[1]);
430 exit(1);
431 }
432*/
lh42c1e572022-01-25 18:47:39 -0800433 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
434 if(connect(lynq_call_client_sockfd, (struct sockaddr *)&lynq_socket_server_addr, sizeof(lynq_socket_server_addr)) == -1)
lh7b0674a2022-01-10 00:34:35 -0800435 {
lh21502f52022-01-27 00:27:12 -0800436 LYERRLOG("connect error:%s",strerror(errno));
lh7b0674a2022-01-10 00:34:35 -0800437 return -1;
438 }
rjw5d2a50e2022-02-28 15:01:49 +0800439 #endif
440 lynq_call_client_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
441 if (-1 == lynq_call_client_sockfd)
442 {
443 return lynq_call_client_sockfd;
444 }
445 /* 设置address */
446 memset(&addr_serv, 0, sizeof(addr_serv));
447 addr_serv.sin_family = AF_INET;
448 addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
449 addr_serv.sin_port = htons(LYNQ_SERVICE_PORT);
450 len_addr_serv = sizeof(addr_serv);
lh7b0674a2022-01-10 00:34:35 -0800451 return 0;
452}
453int lynq_update_call_list_loop()
454{
455 int ret = 0;
lh7b0674a2022-01-10 00:34:35 -0800456 pthread_attr_t attr;
457 pthread_attr_init(&attr);
458 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800459 ret = pthread_create(&lynq_call_list_loop_tid,&attr,triggerGetCallList,NULL);
lh7b0674a2022-01-10 00:34:35 -0800460 if(ret < 0)
461 {
462 LYERRLOG("lynq_update_call_list_loop fail!!!");
463 return -1;
464 }
465 LYDBGLOG("lynq_update_call_list_loop success!!!\n");
466 return 0;
467
468}
469void *thread_urc_recv(void *parg)
ll04ae4142022-01-27 05:54:38 +0000470{
lh7b0674a2022-01-10 00:34:35 -0800471 int socket_fd = (int64_t)parg;
472 int len=0;
473 socklen_t addr_len=0;
474 uint8_t *dataLength = NULL;
475 char urc_data[LYNQ_REC_BUF];
476 int slot_id = -1;
477 int resp_type = -1;
478 int urcid = -1;
479 Parcel *p = NULL;
480 struct sockaddr_in dest_addr;
481 LYINFLOG("thread_urc_recv in running....\n");
lh42c1e572022-01-25 18:47:39 -0800482 while(urc_call_recive_status)
lh7b0674a2022-01-10 00:34:35 -0800483 {
484 bzero(urc_data,LYNQ_REC_BUF);
485 //get data msg
486 len = recvfrom(socket_fd,urc_data,LYNQ_REC_BUF,0,(struct sockaddr *)&dest_addr,&addr_len);
487 if(len <= 0)
ll04ae4142022-01-27 05:54:38 +0000488 {
lh21502f52022-01-27 00:27:12 -0800489 LYERRLOG("thread_urc_recv step2 fail:%s",strerror(errno));
ll04ae4142022-01-27 05:54:38 +0000490 break;
lh7b0674a2022-01-10 00:34:35 -0800491 }
492 LYDBGLOG("=====>urc data len<=====:%d\n",len);
493 p = new Parcel();
494 if(p==NULL)
495 {
lh21502f52022-01-27 00:27:12 -0800496 LYERRLOG("new parcel failure!!!");
lh7b0674a2022-01-10 00:34:35 -0800497 break;
498 }
499 p->setData((uint8_t *)urc_data,len); // p.setData((uint8_t *) buffer, buflen);
500 p->setDataPosition(0);
501 if(p->dataAvail() > 0)
502 {
503 p->readInt32(&resp_type);
504 p->readInt32(&urcid);
505 p->readInt32(&slot_id);
506 //LYDBGLOG("*******Warren test*******:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
507 switch (urcid)
508 {
509 case 1001://RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED
510 {
511 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
512 lynqRespWatingEvent();
513 break;
514 }
515 case 1018://RIL_UNSOL_CALL_RING
516 {
517 if(global_call_auto_answer==1)
518 {
519 lynq_call_answer();
520 }
521 break;
522 }
523 case 1029://RIL_UNSOL_RINGBACK_TONE
524 case 3049://RIL_UNSOL_CALL_INFO_INDICATION
525 {
526 LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d\n",resp_type,urcid,slot_id);
527 break;
528 }
529 default:
530 break;
531 }
532 }
533 delete p;
534 p = NULL;
535 }
536 close(socket_fd);
537}
538int lynq_socket_urc_start()
539{
540 int socket_fd=0;
541 int rt=0;
542 int len=0;
543 int on=1;
544 struct sockaddr_in urc_local_addr;
lh7b0674a2022-01-10 00:34:35 -0800545 pthread_attr_t attr;
ll04ae4142022-01-27 05:54:38 +0000546 socket_fd = socket(AF_INET,SOCK_DGRAM,0);
ll04ae4142022-01-27 05:54:38 +0000547 if(socket_fd < 0)
548 {
lh21502f52022-01-27 00:27:12 -0800549 LYERRLOG("creaet socket for udp fail:%s",strerror(errno));
ll04ae4142022-01-27 05:54:38 +0000550 return -1;
lh7b0674a2022-01-10 00:34:35 -0800551 }
552 urc_local_addr.sin_family = AF_INET;
553 urc_local_addr.sin_port = htons(LYNQ_URC_SERVICE_PORT);
554 urc_local_addr.sin_addr.s_addr = htons(INADDR_ANY);
555 /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
ll04ae4142022-01-27 05:54:38 +0000556 rt = setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
557 if(rt<0)
558 {
lh21502f52022-01-27 00:27:12 -0800559 LYERRLOG("SO_REUSEADDR fail:%s",strerror(errno));
560 perror("SO_REUSEADDR fail:");
lh7b0674a2022-01-10 00:34:35 -0800561 return -1;
562 }
563 rt = bind(socket_fd ,(struct sockaddr*)&urc_local_addr, sizeof(urc_local_addr));
ll04ae4142022-01-27 05:54:38 +0000564 if (rt == -1)
565 {
lh21502f52022-01-27 00:27:12 -0800566 LYERRLOG("bind failed");
567 perror("bind failed:");
ll04ae4142022-01-27 05:54:38 +0000568 return -1;
lh7b0674a2022-01-10 00:34:35 -0800569 }
570 pthread_attr_init(&attr);
571 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
lhec17b0a2022-02-13 23:56:05 -0800572 rt = pthread_create(&lynq_call_urc_tid,&attr,thread_urc_recv,(void *)socket_fd);
lh7b0674a2022-01-10 00:34:35 -0800573 if(rt < 0)
574 {
lh21502f52022-01-27 00:27:12 -0800575 LYERRLOG("urc loop failure!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800576 return -1;
577 }
lh21502f52022-01-27 00:27:12 -0800578 LYDBGLOG("urc loop success!!!\n");
lh7b0674a2022-01-10 00:34:35 -0800579 return 0;
580}
581int getSelfElement(char addr[])
582{
583 for(int i=0;i < LYNQ_CALL_MAX; i++)
584 {
585 if(lynq_call_lists[i].used==1)
586 {
587 if(strcmp(lynq_call_lists[i].addr,addr)==0)
588 {
589 return i;
590 }
591
592 }
593 }
594 return -1;
595}
596
597void lynq_call_state_change_test(int soc_id)
598{
599 printf("call state change,sim:%d\n",soc_id);
600}
601int lynq_init_call(int uToken)
602{
603 int result = 0;
lh42c1e572022-01-25 18:47:39 -0800604 Global_uToken_call = uToken;
lh21502f52022-01-27 00:27:12 -0800605 urc_call_recive_status = 1;
rjw5d2a50e2022-02-28 15:01:49 +0800606 client_size = sizeof(client_t);
lh7b0674a2022-01-10 00:34:35 -0800607 LYLOGSET(LOG_INFO);
608 LYLOGEINIT(USER_LOG_TAG);
609 result = lynq_socket_client_start();
610 if(result!=0)
611 {
612 return -1;
613 }
614 result = lynq_socket_urc_start();
615 if(result!=0)
616 {
617 return -1;
618 }
619 result = lynq_update_call_list_loop();
620 if(result!=0)
621 {
622 return -1;
623 }
624 memset(lynq_call_lists,0,sizeof(lynq_call_lists));
625 LYDBGLOG("lynq init call success!!!");
626 return 0;
627}
628int lynq_deinit_call()
629{
lhec17b0a2022-02-13 23:56:05 -0800630 int ret = -1;
631 if(lynq_call_client_sockfd>0)
632 {
633 close(lynq_call_client_sockfd);
634 }
lh42c1e572022-01-25 18:47:39 -0800635 urc_call_recive_status = 0;
lh7b0674a2022-01-10 00:34:35 -0800636 call_list_loop = 0;
lhec17b0a2022-02-13 23:56:05 -0800637 ret = pthread_cancel(lynq_call_urc_tid);
638 LYDBGLOG("pthread cancel ret = %d",ret);
639 ret = pthread_cancel(lynq_call_list_loop_tid);
640 LYDBGLOG("pthread cancel ret = %d",ret);
641 ret = pthread_join(lynq_call_urc_tid,NULL);
642 LYDBGLOG("pthread join ret = %d",ret);
643 ret = pthread_join(lynq_call_list_loop_tid,NULL);
644 LYDBGLOG("pthread join ret = %d",ret);
lh7b0674a2022-01-10 00:34:35 -0800645 return 0;
646}
647int lynq_call(int* handle,char addr[])
648{
649 Parcel p;
650 lynq_client_t client;
651 int resp_type = -1;
652 int request = -1;
653 int slot_id = -1;
654 int error = -1;
655 int lynq_call_id = -1;
656 if(addr==NULL)
657 {
658 LYERRLOG("Phone num is null!!!");
659 return -1;
660 }
lh42c1e572022-01-25 18:47:39 -0800661 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800662 client.request = 10;//RIL_REQUEST_DIAL
663 client.paramLen = 2;
664 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
665 memcpy(client.param,addr,strlen(addr)+1);
666 strcat(client.param," 0");
667 LYERRLOG("uToken=%d,request=%d,paralen=%d,param=%s",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800668 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800669 {
670 LYERRLOG("send request fail");
671 perror("[LYNQ_CALL] send request fail:");
672 return -1;
673 }
lh42c1e572022-01-25 18:47:39 -0800674 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800675 JumpHeader(p,&resp_type,&request,&slot_id,&error);
676 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
677 lynq_call_id = updateAddr(addr);
678 if(error==0)
679 {
680 isDial = 1;
681 if(waitCallstateChange(3000)==ETIMEDOUT)//3000ms
682 {
683 error = LYNQ_E_TIME_OUT;
684 LYERRLOG("timeout:wait Call state fail!!!");
685 lynq_call_lists[lynq_call_id].hasTimeout = 1;
686 return error;
687 }
688 *handle = lynq_call_id;
689 }
690 return error;
691}
692int lynq_call_answer()
693{
694 Parcel p;
695 lynq_client_t client;
696 int resp_type = -1;
697 int request = -1;
698 int slot_id = -1;
699 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800700 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800701 client.request = 40;//RIL_REQUEST_DIAL
702 client.paramLen = 0;
703 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
704 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800705 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800706 {
707 LYERRLOG("send request fail");
708 perror("[LYNQ_CALL] send request fail:");
709 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 return error;
715}
716int lynq_call_hungup(int* handle)
717{
718 Parcel p;
719 lynq_client_t client;
720 int resp_type = -1;
721 int request = -1;
722 int slot_id = -1;
723 int error = -1;
724 int call_id = 0;
725 int lynq_call_id = 0;
lh42c1e572022-01-25 18:47:39 -0800726 if(handle==NULL||!((*handle>=0)&&(*handle<10)))
727 {
728 LYERRLOG("[%s] illegal input!!!!",__FUNCTION__);
729 return LYNQ_E_CONFLICT;
730 }
731 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800732 client.request = 12;//RIL_REQUEST_HUNGUP
733 client.paramLen = 1;
734 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
735 lynq_call_id = *handle;
736 call_id = lynq_call_lists[lynq_call_id].call_id;
737 sprintf(client.param,"%d",call_id);
738 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800739 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800740 {
741 LYERRLOG("send request fail");
742 perror("[LYNQ_CALL] send request fail:");
743 return -1;
744 }
lh42c1e572022-01-25 18:47:39 -0800745 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800746 JumpHeader(p,&resp_type,&request,&slot_id,&error);
747 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
748 if(error==0)
749 {
750 cleanCallList(lynq_call_id);
751 }
752 return error;
753}
754int lynq_call_hungup_all()
755{
756 Parcel p;
757 lynq_client_t client;
758 int resp_type = -1;
759 int request = -1;
760 int slot_id = -1;
761 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800762 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800763 client.request = 17;//RIL_REQUEST_UDUB
764 client.paramLen = 0;
765 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
766 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800767 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800768 {
769 LYERRLOG("send request fail");
770 perror("[LYNQ_CALL] send request fail:");
771 return -1;
772 }
lh42c1e572022-01-25 18:47:39 -0800773 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800774 JumpHeader(p,&resp_type,&request,&slot_id,&error);
775 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
776 return error;
777}
778int lynq_wait_incoming_call(int *handle)
779{
780 waitIncomingCall();
781 *handle = lynqIncomingCallId;
782 LYINFLOG("lynq incoming call id:%d",lynqIncomingCallId);
783 return 0;
784}
785
786int lynq_set_auto_answercall(const int mode)
787{
788 global_call_auto_answer = mode;
789 LYINFLOG("auto answer call mode =%d",mode);
790 return 0;
791}
792int lynq_get_mute_status(int *status)
793{
794 Parcel p;
795 lynq_client_t client;
796 int resp_type = -1;
797 int request = -1;
798 int slot_id = -1;
799 int error = -1;
800 if(status==NULL)
801 {
802 LYERRLOG("status is null");
803 return -1;
804 }
lh42c1e572022-01-25 18:47:39 -0800805 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800806 client.request = 54;//RIL_REQUEST_GET_MUTE
807 client.paramLen = 0;
808 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
809 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800810 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800811 {
812 LYERRLOG("send request fail");
813 perror("[LYNQ_CALL] send request fail:");
814 return -1;
815 }
lh42c1e572022-01-25 18:47:39 -0800816 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800817 JumpHeader(p,&resp_type,&request,&slot_id,&error);
818 p.readInt32(status);
819 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
820 return error;
821}
lldc99c9b2022-01-24 12:16:22 +0000822
823static int judge_mic(const int enable){
llf512fa32022-02-14 08:58:16 +0000824 switch(enable){
825 case 0:
826 return 1;
827 case 1:
828 return 1;
829 default:
830 return 0;
lldc99c9b2022-01-24 12:16:22 +0000831 }
832}
833
lh7b0674a2022-01-10 00:34:35 -0800834int lynq_set_mute_mic(const int enable)
lldc99c9b2022-01-24 12:16:22 +0000835{
836 if(!judge_mic(enable)){
837 return LYNQ_E_CONFLICT;
838 }
lh7b0674a2022-01-10 00:34:35 -0800839 Parcel p;
840 lynq_client_t client;
841 int resp_type = -1;
842 int request = -1;
843 int slot_id = -1;
844 int error = -1;
lh42c1e572022-01-25 18:47:39 -0800845 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800846 client.request = 53;//RIL_REQUEST_SET_MUTE
847 client.paramLen = 1;
848 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
849 sprintf(client.param,"%d",enable);
850 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800851 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800852 {
853 LYERRLOG("send request fail");
854 perror("[LYNQ_CALL] send request fail:");
855 return -1;
856 }
lh42c1e572022-01-25 18:47:39 -0800857 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800858 JumpHeader(p,&resp_type,&request,&slot_id,&error);
859 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
860 return error;
861}
862int lynq_set_DTMF(const char callnum)
863{
864 Parcel p;
865 lynq_client_t client;
866 int resp_type = -1;
867 int request = -1;
868 int slot_id = -1;
869 int error = -1;
870 if(!lynq_call_state)
871 {
872 LYERRLOG("LYNQ_E_CONFLICT");
873 return LYNQ_E_CONFLICT;
874 }
lh42c1e572022-01-25 18:47:39 -0800875 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800876 client.request = 24;//RIL_REQUEST_DTMF
877 client.paramLen = 1;
878 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
879 sprintf(client.param,"%c",callnum);
880 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800881 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800882 {
883 LYERRLOG("send request fail");
884 perror("[LYNQ_CALL] send request fail:");
885 return -1;
886 }
lh42c1e572022-01-25 18:47:39 -0800887 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800888 JumpHeader(p,&resp_type,&request,&slot_id,&error);
889 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
890 return error;
891
892}
lldc99c9b2022-01-24 12:16:22 +0000893
894static int judge_volume(const int volume){
895 if(volume < 0 ||volume >36){
896 return 0;
897 }
ll04ae4142022-01-27 05:54:38 +0000898 return 1;
lldc99c9b2022-01-24 12:16:22 +0000899}
900
lh7b0674a2022-01-10 00:34:35 -0800901int lynq_set_DTMF_volume(const int volume)
lldc99c9b2022-01-24 12:16:22 +0000902{
903 if(!judge_volume(volume)){
904 return LYNQ_E_CONFLICT;
905 }
lh7b0674a2022-01-10 00:34:35 -0800906 Parcel p;
907 lynq_client_t client;
908 int resp_type = -1;
909 int request = -1;
910 int slot_id = -1;
911 int error = -1;
912 //if(!lynq_call_state)
913 //{
914 // LYERRLOG("LYNQ_E_CONFLICT");
915 // return LYNQ_E_CONFLICT;
916 //}
lh42c1e572022-01-25 18:47:39 -0800917 client.uToken = Global_uToken_call;
lh7b0674a2022-01-10 00:34:35 -0800918 client.request = 8003;//LYNQ_REQUEST_SET_DTMF_VOLUME
919 client.paramLen = 1;
920 bzero(client.param,LYNQ_REQUEST_PARAM_BUF);
921 sprintf(client.param,"%d",volume);
922 LYDBGLOG("uToken=%d,request=%d,paralen=%d,param=%s\n",client.uToken,client.request,client.paramLen,client.param);
lh42c1e572022-01-25 18:47:39 -0800923 if(send_request(lynq_call_client_sockfd,&client)==-1)
lh7b0674a2022-01-10 00:34:35 -0800924 {
925 LYERRLOG("send request fail");
926 perror("[LYNQ_CALL] send request fail:");
927 return -1;
928 }
lh42c1e572022-01-25 18:47:39 -0800929 get_response(lynq_call_client_sockfd,p);
lh7b0674a2022-01-10 00:34:35 -0800930 JumpHeader(p,&resp_type,&request,&slot_id,&error);
931 LYINFLOG("resp_type=%d,request=%d,slot_id=%d,error_code=%d",resp_type,request,slot_id,error);
932 return 0;
933}
934int lynq_get_current_call_state(int *handle, int *call_state,int *toa,int *direction,char addr[])
935{
936 int lynq_call_id = 0;
937 if(handle==NULL)
938 {
939 return LYNQ_E_NULL_ANONALY;
940 }
941 lynq_call_id = *handle;
942 *call_state = lynq_call_lists[lynq_call_id].call_state;
943 *toa = lynq_call_lists[lynq_call_id].toa;
944 *direction = lynq_call_lists[lynq_call_id].direction;
945 memcpy(addr,lynq_call_lists[lynq_call_id].addr,strlen(lynq_call_lists[lynq_call_id].addr)+1);
946 return 0;
947}
948
949#if 0
950int main(int argc,char **argv)
951{
952 int n = 0;
953 n = lynq_init_call(lynq_call_state_change_test,2222);
954 if(n<0)
955 {
956 printf("lynq init call fail!!!\n");
957 return -1;
958 }
959 printf("lynq call init success!!!\n");
960 char phoneNum[LYNQ_PHONE_NUMBER_MAX];
961 sprintf(phoneNum,"18180053406 0",strlen("18180053406 0")+1);
962 lynq_call(phoneNum);
963 while(1)
964 {
965 sleep(1);
966 }
967 return 0;
968}
969#endif
970/*Warren add for T800 platform 2021/11/19 end*/