blob: 0fe2d2f600d263a2032f9612b9589d6834fea60a [file] [log] [blame]
b.liu87afc4c2024-08-14 17:33:45 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <sys/socket.h>
6#include <sys/un.h>
7#include <netinet/in.h>
8#include <pthread.h>
9#include <sys/epoll.h>
10#include <fcntl.h>
11#include <signal.h>
12
13#include "mbtk_type.h"
14#include "mbtk_ril.h"
15#include "atchannel.h"
16#include "at_tok.h"
17#include "mbtk_utils.h"
18#include "ril_info.h"
19
20void ril_rsp_pack_send(int fd, int ril_id, int msg_index, const void* data, int data_len);
21
22/*
23AT+CGSN
24864788050901201
25
26OK
27*/
28static int req_imei_get(void *data, int *cme_err)
29{
30 ATResponse *response = NULL;
31 int err = at_send_command_numeric("AT+CGSN", &response);
32
33 if (err < 0 || response->success == 0 || !response->p_intermediates) {
34 *cme_err = at_get_cme_error(response);
35 goto exit;
36 }
37
38 memcpy(data, response->p_intermediates->line, strlen(response->p_intermediates->line));
39exit:
40 at_response_free(response);
41 return err;
42}
43
44/*
45AT+MRD_SN=R
46+MRD_SN:0101,Thu Nov 12 00:00:00 2020,G4M32301020006
47
48OK
49
50*/
51static int req_sn_get(void *data, int *cme_err)
52{
53 ATResponse *response = NULL;
54 char *tmp_ptr = NULL;
55 int err = at_send_command_singleline("AT+MRD_SN=R", "+MRD_SN:", &response);
56
57 if (err < 0 || response->success == 0 || !response->p_intermediates){
58 *cme_err = at_get_cme_error(response);
59 goto exit;
60 }
61
62 char *line = response->p_intermediates->line;
63 err = at_tok_start(&line);
64 if (err < 0)
65 {
66 goto exit;
67 }
68
69 err = at_tok_nextstr(&line, &tmp_ptr);
70 if (err < 0)
71 {
72 goto exit;
73 }
74
75 err = at_tok_nextstr(&line, &tmp_ptr);
76 if (err < 0)
77 {
78 goto exit;
79 }
80
81 err = at_tok_nextstr(&line, &tmp_ptr);
82 if (err < 0)
83 {
84 goto exit;
85 }
86
87 memcpy(data, tmp_ptr, strlen(tmp_ptr));
88
89 goto exit;
90exit:
91 at_response_free(response);
92 return err;
93}
94
95/*
96ATI
97Manufacturer:"LYNQ"
98Model:"LYNQ_L508TLC"
99Revision:L508TLCv02.01b01.00
100IMEI:864788050901201
101
102OK
103
104*/
105static int req_version_get(void *data, int *cme_err)
106{
107 ATResponse *response = NULL;
108 int err = at_send_command_multiline("ATI", "", &response);
109
110 if (err < 0 || response->success == 0 || !response->p_intermediates){
111 *cme_err = at_get_cme_error(response);
112 goto exit;
113 }
114
115 ATLine* lines_ptr = response->p_intermediates;
116 char *line = NULL;
117 while(lines_ptr)
118 {
119 line = lines_ptr->line;
120 if(strStartsWith(line, "Revision"))
121 {
122 err = at_tok_start(&line);
123 if (err < 0)
124 {
125 goto exit;
126 }
127 memcpy(data, line, strlen(line));
128 break;
129 }
130 lines_ptr = lines_ptr->p_next;
131 }
132
133 goto exit;
134exit:
135 at_response_free(response);
136 return err;
137}
138
139/*
140ATI
141Manufacturer:"LYNQ"
142Model:"LYNQ_L508TLC"
143Revision:L508TLCv02.01b01.00
144IMEI:864788050901201
145
146OK
147
148*/
149static int req_model_get(void *data, int *cme_err)
150{
151 ATResponse *response = NULL;
152 int err = at_send_command_multiline("ATI", "", &response);
153
154 if (err < 0 || response->success == 0 || !response->p_intermediates){
155 *cme_err = at_get_cme_error(response);
156 goto exit;
157 }
158
159 ATLine* lines_ptr = response->p_intermediates;
160 char *line = NULL;
161 while(lines_ptr)
162 {
163 line = lines_ptr->line;
164 if(strStartsWith(line, "Model"))
165 {
166 err = at_tok_start(&line);
167 if (err < 0)
168 {
169 goto exit;
170 }
171 memcpy(data, line, strlen(line));
172 break;
173 }
174 lines_ptr = lines_ptr->p_next;
175 }
176
177 goto exit;
178exit:
179 at_response_free(response);
180 return err;
181}
182
183/*
184AT+ACONFIG="IMSD=1"
185or
186AT+ACONFIG="IMSD=0"
187
188OK
189*/
190static int req_volte_set(int state, int *cme_err)
191{
192 ATResponse *response = NULL;
193 char cmd[30] = {0};
194 if(state)
195 {
196 strcpy(cmd, "AT+ACONFIG=\"IMSD=1\"");
197 }
198 else
199 {
200 strcpy(cmd, "AT+ACONFIG=\"IMSD=0\"");
201 }
202 int err = at_send_command(cmd, &response);
203
204 if (err < 0 || response->success == 0) {
205 *cme_err = at_get_cme_error(response);
206 goto exit;
207 }
208
209 err = 0;
210exit:
211 at_response_free(response);
212 return err;
213}
214
215/*
216AT+ACONFIG?
217PID=0,VID=0,IMSD=1,PIPE=0,FAST=0,RDUP=1,NOCP=0,GEFL=-1237040617
218
219OK
220*/
221static int req_volte_get(int *state, int *cme_err)
222{
223 ATResponse *response = NULL;
224 char *tmp_ptr = NULL;
225 int err = at_send_command_singleline("AT+ACONFIG?", "", &response);
226
227 if (err < 0 || response->success == 0 || !response->p_intermediates){
228 *cme_err = at_get_cme_error(response);
229 goto exit;
230 }
231
232 char *line = response->p_intermediates->line;
233 char* ptr = strstr(line, "IMSD=");
234 if(ptr)
235 {
236 *state = atoi(ptr + strlen("IMSD="));
237 }
238 else
239 {
240 err = -1;
241 }
242exit:
243 at_response_free(response);
244 return err;
245}
246
247
248/*
249* Get system temperature.
250*
251* type[IN]:
252* 0: Soc temperature.
253* 1: RF temperature.
254* temp[OUT]:
255* temperature in celsius.
256*
257
258AT*RFTEMP
259*RFTEMP:0,28
260OK
261
262AT*SOCTEMP
263*SOCTEMP:24000
264OK
265
266*/
267static int req_temp_get(mbtk_temp_type_enum type, int16 *temp, int *cme_err)
268{
269 ATResponse *response = NULL;
270 int err = -1;
271 int tmp_int;
272 if(type == MBTK_TEMP_TYPE_SOC) { // Soc
273 err = at_send_command_singleline("AT*SOCTEMP", "*SOCTEMP:", &response);
274 } else { // RF
275 err = at_send_command_singleline("AT*RFTEMP", "*RFTEMP:", &response);
276 }
277
278 if (err < 0 || response->success == 0 || !response->p_intermediates){
279 *cme_err = at_get_cme_error(response);
280 goto exit;
281 }
282
283 char *line = response->p_intermediates->line;
284 err = at_tok_start(&line);
285 if (err < 0)
286 {
287 goto exit;
288 }
289 err = at_tok_nextint(&line, &tmp_int);
290 if (err < 0)
291 {
292 goto exit;
293 }
294
295 if(type == MBTK_TEMP_TYPE_RF) { // RF
296 err = at_tok_nextint(&line, &tmp_int);
297 if (err < 0)
298 {
299 goto exit;
300 }
301 *temp = (int16)tmp_int;
302 } else {
303 tmp_int = tmp_int / 1000;
304 *temp = (int16)tmp_int;
305 }
306
307exit:
308 at_response_free(response);
309 return err;
310}
311
312/*
313AT+CCLK?
314+CCLK: "23/03/20,01:58:00+32"
315
316OK
317
318*/
319static int req_cell_time_get(char *data, int *cme_err)
320{
321 ATResponse *response = NULL;
322 char *tmp_ptr = NULL;
323 int err = at_send_command_singleline("AT+CCLK?", "+CCLK:", &response);
324
325 if (err < 0 || response->success == 0 || !response->p_intermediates){
326 *cme_err = at_get_cme_error(response);
327 goto exit;
328 }
329
330 char *line = response->p_intermediates->line;
331 err = at_tok_start(&line);
332 if (err < 0)
333 {
334 goto exit;
335 }
336 err = at_tok_nextstr(&line, &tmp_ptr);
337 if (err < 0)
338 {
339 goto exit;
340 }
341 memcpy(data, tmp_ptr, strlen(tmp_ptr));
342
343exit:
344 at_response_free(response);
345 return err;
346}
347
348/*
3490: minimum functionality
3501: full functionality
3513: disable phone receive RF circuits.
3524: disable phone both transmit and receive RF circuits
3535: disable SIM
3546: turn off full secondary receive.
355-1: fail
356*/
357mbtk_radio_state_enum ril_radio_state_get()
358{
359 ATResponse *p_response = NULL;
360 int err;
361 char *line;
362 int ret;
363
364 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
365
366 if (err < 0 || p_response->success == 0 || !p_response->p_intermediates)
367 {
368 // assume radio is off
369 goto done;
370 }
371
372 line = p_response->p_intermediates->line;
373
374 err = at_tok_start(&line);
375 if (err < 0) goto done;
376
377 err = at_tok_nextint(&line, &ret);
378 if (err < 0) goto done;
379
380 at_response_free(p_response);
381
382 ril_info.radio_state = (mbtk_radio_state_enum)ret;
383
384 return ril_info.radio_state;
385done:
386 at_response_free(p_response);
387 return MBTK_RADIO_STATE_UNKNOWN;
388}
389
390mbtk_ril_err_enum ril_radio_state_set(mbtk_radio_state_enum state, bool reset)
391{
392 int err;
393 ATResponse *p_response = NULL;
394 mbtk_ril_err_enum ret = MBTK_RIL_ERR_UNKNOWN;
395
396 if(state == ril_info.radio_state) {
397 LOGE("Radio state is same.");
398 //return MBTK_RIL_ERR_PARAMETER;
399 return MBTK_RIL_ERR_SUCCESS;
400 }
401
402 LOGI("Set radio state - %d", state);
403
404 char cmd[64] = {0};
405 if(reset) {
406 snprintf(cmd, sizeof(cmd), "AT+CFUN=%d,1", state);
407 } else {
408 snprintf(cmd, sizeof(cmd), "AT+CFUN=%d", state);
409 }
410 err = at_send_command(cmd, &p_response);
411 if (err || !p_response->success) {
412 goto done;
413 }
414
415 if(state == MBTK_RADIO_STATE_FULL_FUNC) { // +CFUN=1
416 if(ril_radio_state_get() == MBTK_RADIO_STATE_FULL_FUNC) { // success
417 ril_info.radio_state = MBTK_RADIO_STATE_FULL_FUNC;
418 ret = MBTK_RIL_ERR_SUCCESS;
419 LOGD("Radio open success.");
420 } else {
421 LOGW("Radio open fail.");
422 }
423 } else {
424 ril_info.radio_state = state;
425 ret = MBTK_RIL_ERR_SUCCESS;
426 LOGD("Set radio state to %d success.", state);
427 }
428
429done:
430 at_response_free(p_response);
431 return ret;
432}
433
434
435//void net_list_free(void *data);
436// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
437// Otherwise, do not call pack_error_send().
438mbtk_ril_err_enum dev_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack)
439{
440 mbtk_ril_err_enum err = MBTK_RIL_ERR_SUCCESS;
441 int cme_err = MBTK_RIL_ERR_CME_NON;
442 switch(pack->msg_id)
443 {
444 case RIL_MSG_ID_DEV_IMEI:
445 {
446 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
447 {
448 char imei[20] = {0};
449 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_RIL_ERR_CME_NON)
450 {
451 if(cme_err != MBTK_RIL_ERR_CME_NON) {
452 err = MBTK_RIL_ERR_CME + cme_err;
453 } else {
454 err = MBTK_RIL_ERR_UNKNOWN;
455 }
456 LOG("Get IMEI fail.");
457 }
458 else
459 {
460 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, imei, strlen(imei));
461 }
462 }
463 else // Set IMEI(Unsupport).
464 {
465 err = MBTK_RIL_ERR_UNSUPPORTED;
466 LOG("Unsupport set IMEI.");
467 }
468 break;
469 }
470 case RIL_MSG_ID_DEV_SN:
471 {
472 if(pack->data_len == 0 || pack->data == NULL)
473 {
474 char sn[20] = {0};
475 if(req_sn_get(sn, &cme_err) || strlen(sn) == 0 || cme_err != MBTK_RIL_ERR_CME_NON)
476 {
477 if(cme_err != MBTK_RIL_ERR_CME_NON) {
478 err = MBTK_RIL_ERR_CME + cme_err;
479 } else {
480 err = MBTK_RIL_ERR_UNKNOWN;
481 }
482 LOG("Get SN fail.");
483 }
484 else
485 {
486 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, sn, strlen(sn));
487 }
488 }
489 else // Set
490 {
491 err = MBTK_RIL_ERR_UNSUPPORTED;
492 LOG("Unsupport set SN.");
493 }
494 break;
495 }
496 case RIL_MSG_ID_DEV_MEID:
497 {
498 if(pack->data_len == 0 || pack->data == NULL)
499 {
500 err = MBTK_RIL_ERR_UNSUPPORTED;
501 LOG("Support only for CDMA.");
502 }
503 else // Set
504 {
505 err = MBTK_RIL_ERR_UNSUPPORTED;
506 LOG("Unsupport set MEID.");
507 }
508 break;
509 }
510 case RIL_MSG_ID_DEV_VERSION:
511 {
512 if(pack->data_len == 0 || pack->data == NULL)
513 {
514 char version[50] = {0};
515 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_RIL_ERR_CME_NON)
516 {
517 if(cme_err != MBTK_RIL_ERR_CME_NON) {
518 err = MBTK_RIL_ERR_CME + cme_err;
519 } else {
520 err = MBTK_RIL_ERR_UNKNOWN;
521 }
522 LOG("Get Version fail.");
523 }
524 else
525 {
526 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, version, strlen(version));
527 }
528 }
529 else // Set
530 {
531 err = MBTK_RIL_ERR_UNSUPPORTED;
532 LOG("Unsupport set Version.");
533 }
534 break;
535 }
536 case RIL_MSG_ID_DEV_MODEL:
537 {
538 if(pack->data_len == 0 || pack->data == NULL)
539 {
540 char model[50] = {0};
541 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_RIL_ERR_CME_NON)
542 {
543 if(cme_err != MBTK_RIL_ERR_CME_NON) {
544 err = MBTK_RIL_ERR_CME + cme_err;
545 } else {
546 err = MBTK_RIL_ERR_UNKNOWN;
547 }
548 LOG("Get Version fail.");
549 }
550 else
551 {
552 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, model, strlen(model));
553 }
554 }
555 else // Set
556 {
557 err = MBTK_RIL_ERR_UNSUPPORTED;
558 LOG("Unsupport set Model.");
559 }
560 break;
561 }
562 case RIL_MSG_ID_DEV_VOLTE: // <uint8> 0:Close 1:Open
563 {
564 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
565 {
566 int state;
567 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON)
568 {
569 if(cme_err != MBTK_RIL_ERR_CME_NON) {
570 err = MBTK_RIL_ERR_CME + cme_err;
571 } else {
572 err = MBTK_RIL_ERR_UNKNOWN;
573 }
574 LOG("Get VoLTE state fail.");
575 }
576 else
577 {
578 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, &state, sizeof(uint8));
579 }
580 }
581 else // Set VoLTE state.
582 {
583 if(pack->data == NULL || pack->data_len != sizeof(uint8)
584 || (*(pack->data) != 0 && *(pack->data) != 1))
585 {
586 err = MBTK_RIL_ERR_REQ_PARAMETER;
587 LOG("Set VOLTE parameter error.");
588 break;
589 }
590
591 uint8 on = *(pack->data);
592 if(req_volte_set(on, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON)
593 {
594 if(cme_err != MBTK_RIL_ERR_CME_NON) {
595 err = MBTK_RIL_ERR_CME + cme_err;
596 } else {
597 err = MBTK_RIL_ERR_UNKNOWN;
598 }
599 LOG("Set VoLTE state fail.");
600 }
601 else
602 {
603 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0);
604
605 // Restart is required to take effect.
606 // LOG("Will reboot system...");
607 }
608 }
609 break;
610 }
611 case RIL_MSG_ID_DEV_TEMP:
612 {
613 if(pack->data && pack->data_len == sizeof(uint8)) {
614 int16 temp;
615 mbtk_temp_type_enum type = (mbtk_temp_type_enum)(*(pack->data));
616 if(req_temp_get(type, &temp, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON)
617 {
618 if(cme_err != MBTK_RIL_ERR_CME_NON) {
619 err = MBTK_RIL_ERR_CME + cme_err;
620 } else {
621 err = MBTK_RIL_ERR_UNKNOWN;
622 }
623 LOG("Get temperature fail.");
624 }
625 else
626 {
627 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, &temp, sizeof(int16));
628 }
629 } else {
630 err = MBTK_RIL_ERR_UNSUPPORTED;
631 LOG("Unsupport set Temperature.");
632 }
633 break;
634 }
635 case RIL_MSG_ID_DEV_CELL_TIME:
636 {
637 if(pack->data_len == 0 || pack->data == NULL) // Get Time
638 {
639 char time[100] = {0};
640 if(req_cell_time_get(time, &cme_err) || strlen(time) == 0 || cme_err != MBTK_RIL_ERR_CME_NON)
641 {
642 if(cme_err != MBTK_RIL_ERR_CME_NON) {
643 err = MBTK_RIL_ERR_CME + cme_err;
644 } else {
645 err = MBTK_RIL_ERR_UNKNOWN;
646 }
647 LOG("Get cell time fail.");
648 }
649 else
650 {
651 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, time, strlen(time));
652 }
653 }
654 else // Set Time
655 {
656 err = MBTK_RIL_ERR_UNSUPPORTED;
657 LOG("Unsupport set cell time.");
658 }
659 break;
660 }
661 case RIL_MSG_ID_DEV_MODEM:
662 {
663 if(pack->data_len == 0 || pack->data == NULL)
664 {
665 mbtk_radio_state_enum radio_state = ril_radio_state_get();
666 if(radio_state == MBTK_RADIO_STATE_UNKNOWN)
667 {
668 err = MBTK_RIL_ERR_UNKNOWN;
669 LOG("Get Version fail.");
670 }
671 else
672 {
673 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, &radio_state, sizeof(uint8));
674 }
675 }
676 else // Set
677 {
678 // <func><reset>
679 if(pack->data && pack->data_len == 2) {
680 mbtk_radio_state_enum radio_state = (mbtk_radio_state_enum)(*(pack->data));
681 bool reset = *(pack->data + 1);
682 err = ril_radio_state_set(radio_state, reset);
683 if(MBTK_RIL_ERR_SUCCESS == err) {
684 ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0);
685 }
686 } else {
687 err = MBTK_RIL_ERR_FORMAT;
688 }
689 }
690 break;
691 }
692 default:
693 {
694 err = MBTK_RIL_ERR_REQ_UNKNOWN;
695 LOG("Unknown request : %s", id2str(pack->msg_id));
696 break;
697 }
698 }
699
700 return err;
701}
702
703
704
705