blob: 99fb91ff171bad41129f0b61027bd27b2f15fee5 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +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 <string.h>
11#include <fcntl.h>
12#include <signal.h>
13
14#include "mbtk_info.h"
15#include "mbtk_list.h"
16#include "mbtk_utils.h"
17
18#include "time.h"
19
20#define EPOLL_LISTEN_MAX 100
21#define EPOLL_LISTEN_MAX 100
22
b.liuc41882f2024-10-23 17:54:44 +080023static mbtk_info_callback_func ril_server_state_cb = NULL;
24
liubin281ac462023-07-19 14:22:54 +080025#if 0
26struct
27{
28 uint8 operator[128];
29 uint8 operator[128];
30 uint8 mcc_mnc[10];
31} operator_mcc_mnc =
32{
33 {"China Mobile","CMCC","46000"},
34 {"China Unicom","CU","46001"},
35 {"China Mobile","CMCC","46002"},
36 {"China Telecom","CT","46003"},
37 {"China Mobile","CMCC","46004"},
38 {"China Telecom","CT","46005"},
39 {"China Unicom","CU","46006"},
40 {"China Mobile","CMCC","46007"},
41 {"China Mobile","CMCC","46008"},
42 {"China Unicom","CU","46009"},
43 {"China Telecom","CT","46011"},
44 {NULL, NULL, NULL}
45};
46#endif
47
48static int pack_process(mbtk_info_handle_t* handle, mbtk_info_pack_t* pack)
49{
50 mbtk_info_type_enum info_type = mbtk_info_type_get(pack->info_id);
51 LOG("Type : %s, ID : %s, Result : %s ,Length : %d", type2str(info_type),
52 id2str(pack->info_id),
53 err2str(pack->info_err),
54 pack->data_len);
55 if(0 && pack->data_len > 0)
56 {
57 log_hex("DATA", pack->data, pack->data_len);
58 }
59 // IND Message.
60 if(info_type == MBTK_INFO_TYPE_IND)
61 {
62 if(pack->data_len > 0 && pack->data != NULL) // IND message.
63 {
64 log_hex(id2str(pack->info_id), pack->data, pack->data_len);
65 switch(pack->info_id)
66 {
67 case MBTK_INFO_ID_IND_NET_STATE_CHANGE:
68 {
69 if(handle->net_state_cb)
70 handle->net_state_cb(pack->data, pack->data_len);
71 break;
72 }
73 case MBTK_INFO_ID_IND_CALL_STATE_CHANGE:
74 {
75 if(handle->call_state_cb)
76 handle->call_state_cb(pack->data, pack->data_len);
77 break;
78 }
79 case MBTK_INFO_ID_IND_SMS_STATE_CHANGE:
80 {
81 if(handle->sms_state_cb)
82 handle->sms_state_cb(pack->data, pack->data_len);
83 break;
84 }
85 case MBTK_INFO_ID_IND_RADIO_STATE_CHANGE:
86 {
87 if(handle->radio_state_cb)
88 handle->radio_state_cb(pack->data, pack->data_len);
89 break;
90 }
91 case MBTK_INFO_ID_IND_SIM_STATE_CHANGE:
92 {
93 if(handle->sim_state_cb)
94 handle->sim_state_cb(pack->data, pack->data_len);
95 break;
96 }
97 case MBTK_INFO_ID_IND_PDP_STATE_CHANGE:
98 {
99 if(handle->pdp_state_cb)
100 handle->pdp_state_cb(pack->data, pack->data_len);
101 break;
102 }
103 //mbtk wyq for server_ready_status add start
104 case MBTK_INFO_ID_IND_SERVER_STATE_CHANGE:
105 {
106 handle->server_ready_status = 1;
107 LOG("handshake message recv ok.");
108 break;
109 }
r.xiaofca7c472024-04-24 01:00:23 -0700110 //mbtk xr for signal_status add start
111 case MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE:
112 {
113 if(handle->signal_state_cb)
114 handle->signal_state_cb(pack->data, pack->data_len);
115 break;
116 }
117
liubin281ac462023-07-19 14:22:54 +0800118 //mbtk wyq for server_ready_status add end
119 default:
120 {
121 LOG("Unknown IND : %d", pack->info_id);
122 break;
123 }
124 }
125 }
126 else // Register IND response.
127 {
128 handle->info_err = pack->info_err;
129 if(pack->info_err == MBTK_INFO_ERR_SUCCESS)
130 {
131 LOG("IND %s register success.", id2str(pack->info_id));
132 }
133 else
134 {
135 LOG("IND %s register fail : %s", id2str(pack->info_id), err2str(pack->info_err));
136 }
137
138 if(handle->is_waitting) {
139 pthread_mutex_lock(&handle->mutex);
140 pthread_cond_signal(&handle->cond);
141 pthread_mutex_unlock(&handle->mutex);
142 }
143 }
144 }
145 else // Response Information.
146 {
147 handle->info_err = pack->info_err;
148
149 // Set data length.
150 // If data change,will change this lenght in mbtk_info_pack_data_get().
151 handle->data_len = pack->data_len;
152 // Copy data buffer,because it will be released.
153 if(handle->data && pack->data && pack->data_len > 0) {
154 memcpy(handle->data, pack->data, handle->data_len);
155 }
156
157 if(handle->is_waitting) {
158 pthread_mutex_lock(&handle->mutex);
159 pthread_cond_signal(&handle->cond);
160 pthread_mutex_unlock(&handle->mutex);
161 }
162
163
164#if 0
165 if(pack->info_err == MBTK_INFO_ERR_SUCCESS)
166 {
167 LOG("REQ %s success.", id2str(pack->info_id));
168#if 0
169 if(pack->data_len > 0)
170 {
171 log_hex("DATA", pack->data, pack->data_len);
172 }
173#endif
174 switch(pack->info_id)
175 {
176 case MBTK_INFO_ID_NET_AVAILABLE_RSP:
177 {
178 mbtk_net_array_info_t* nets = (mbtk_net_array_info_t*)mbtk_info_pack_data_get(pack);
179 if(nets)
180 {
181 mbtk_net_info_t *net = NULL;
182 list_first(nets->net_list);
183 while ((net = (mbtk_net_info_t*) list_next(nets->net_list)))
184 {
185 LOG("NET : %d, %d, %s", net->net_sel_mode, net->net_type, net->plmn);
186 }
187 list_free(nets->net_list);
188 free(nets);
189 }
190 else
191 {
192 LOG("mbtk_info_pack_data_get() fail.");
193 }
194
195 break;
196 }
197 case MBTK_INFO_ID_NET_SEL_MODE_RSP:
198 {
199 mbtk_net_info_t* net = (mbtk_net_info_t*)mbtk_info_pack_data_get(pack);
200 if(net)
201 {
202 LOG("NET : %d, %d, %d", net->net_sel_mode, net->net_type, net->plmn);
203 free(net);
204 }
205 else
206 {
207 LOG("mbtk_info_pack_data_get() fail.");
208 }
209
210 break;
211 }
212 case MBTK_INFO_ID_NET_BAND_RSP:
213 {
214 mbtk_band_info_t* band = (mbtk_band_info_t*)mbtk_info_pack_data_get(pack);
215 if(band) {
216 LOG("BAND : %d, %d, %d, %d, %d", band->net_pref,
217 band->gsm_band,
218 band->umts_band,
219 band->tdlte_band,
220 band->fddlte_band);
221 } else {
222 LOG("mbtk_info_pack_data_get() fail.");
223 }
224
225 break;
226 }
227 default:
228 {
229 break;
230 }
231 }
232 }
233 else
234 {
235 LOG("REQ %s fail : %s", id2str(pack->info_id), err2str(pack->info_err));
236 }
237#endif
238 }
239 return 0;
240}
241
242
243static void* info_read_run(void* arg)
244{
245 int epoll_fd = epoll_create(5);
246 if(epoll_fd < 0)
247 {
248 LOG("epoll_create() fail[%d].", errno);
249 return NULL;
250 }
251 mbtk_info_handle_t* handle = (mbtk_info_handle_t*)arg;
252
253 uint32 event = EPOLLIN | EPOLLET;
254 struct epoll_event ev_cli, ev_exit;
255 ev_cli.data.fd = handle->client_fd;
256 ev_cli.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
257 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,handle->client_fd,&ev_cli);
258
259 ev_exit.data.fd = handle->exit_fd[0];
260 ev_exit.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
261 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,handle->exit_fd[0],&ev_exit);
262
263 int nready = -1;
264 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
265 while(1)
266 {
267 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
268 if(nready > 0)
269 {
270 int i;
271 for(i = 0; i < nready; i++)
272 {
b.liuc41882f2024-10-23 17:54:44 +0800273 LOGD("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
liubin281ac462023-07-19 14:22:54 +0800274 if(epoll_events[i].events & EPOLLHUP) // Closed by server.
275 {
b.liuc41882f2024-10-23 17:54:44 +0800276 LOGD("Closed by server.");
277 if(ril_server_state_cb) {
278 int state = 1;
279 ril_server_state_cb(&state, sizeof(int));
280 }
liubin281ac462023-07-19 14:22:54 +0800281 }
282 else if(epoll_events[i].events & EPOLLIN)
283 {
284 if(handle->client_fd == epoll_events[i].data.fd) // Server data arrive.
285 {
286 // Read and process every message.
287 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
288 mbtk_info_pack_t **pack = mbtk_info_pack_recv(handle->client_fd, false, &err);
289
290 // Parse packet error,send error response to client.
291 if(pack == NULL)
292 {
293 if(err != MBTK_INFO_ERR_SUCCESS)
294 {
295 LOG("RSP packet error[%s].", err2str(err));
296 }
297 }
298 else
299 {
300 mbtk_info_pack_t** pack_ptr = pack;
301 while(*pack_ptr)
302 {
303 pack_process(handle, *pack_ptr);
304 mbtk_info_pack_free(pack_ptr);
305 pack_ptr++;
306 }
307 free(pack);
308 }
309 }
310 else if(handle->exit_fd[0] == epoll_events[i].data.fd) //
311 {
312 char buff[100] = {0};
313 int len = read(handle->exit_fd[0], buff, 100);
314 if(len > 0) {
315 LOGI("CMD : %s", buff);
316 if(strcmp(buff, "EXIT") == 0) {
317 goto read_thread_exit;
318 } else {
319 LOGD("Unkonw cmd : %s", buff);
320 }
321 } else {
322 LOGE("sock_read() fail.");
323 }
324 }
325 else
326 {
327 LOG("Unknown socket : %d", epoll_events[i].data.fd);
328 }
329 }
330 else
331 {
332 LOG("Unknown event : %x", epoll_events[i].events);
333 }
334 }
335 }
336 else
337 {
338 LOG("epoll_wait() fail[%d].", errno);
339 }
340 }
341
342read_thread_exit:
343 LOGD("info_read thread exit.");
344 return NULL;
345}
346
347#if 0
348static int info_item_get(mbtk_info_handle_t* handle, mbtk_info_id_enum id, void* data)
349{
350 int data_len = 0;
351 if(data == NULL) {
352 LOG("data is null.");
353 return -1;
354 }
355 mbtk_info_pack_t* pack = mbtk_info_pack_creat(id);
356 if(pack == NULL) {
357 LOG("mbtk_info_item_get() fail.");
358 return -1;
359 }
360
361 mbtk_info_pack_send(handle->client_fd, pack);
362 mbtk_info_pack_free(&pack);
363 handle->data = data;
364
365 // Wait for server response.
366 pthread_mutex_lock(&handle->mutex);
367 handle->is_waitting = true;
368 pthread_cond_wait(&handle->cond, &handle->mutex);
369 handle->is_waitting = false;
370 pthread_mutex_unlock(&handle->mutex);
371
372 if(handle->info_err == MBTK_INFO_ERR_SUCCESS)
373 {
374 LOG("REQ %s success.", id2str(id));
375 if(data && handle->data_len > 0) {
376 data_len = handle->data_len;
377 handle->data_len = 0;
378 handle->data = NULL;
379 }
380 return data_len;
381 } else {
382 LOG("REQ %s fail : %s", id2str(id), err2str(handle->info_err));
383 return -1;
384 }
385}
386#endif
387
388/*
389* Return recv data length.
390* -1 : fail.
391*/
392static int info_item_process(mbtk_info_handle_t *handle,
393 mbtk_info_id_enum id,
394 const void *send_buff,
395 int send_buff_len,
396 void *recv_buff)
397{
398 if(handle == NULL/* || ((send_buff == NULL || send_buff_len == 0) && recv_buff == NULL)*/) {
399 LOG("ARG error.");
400 return -1;
401 }
402
403 mbtk_info_pack_t* pack = mbtk_info_pack_creat(id);
404 if(pack == NULL) {
405 return -1;
406 }
407 if(send_buff && send_buff_len > 0) { // Set the data to be sent.
408 // log_hex("data", send_buff, send_buff_len);
409 // mbtk_info_pack_data_set(pack, data, data_len);
410 pack->data_len = (uint16)send_buff_len;
411 pack->data = (const uint8*)send_buff;
412 }
wangyouqiang962740a2023-11-02 19:27:22 +0800413
414 pthread_mutex_lock(&handle->send_mutex);
415 pthread_mutex_lock(&handle->mutex);
416 handle->is_waitting = true;
b.liu27b91e42024-01-17 20:02:30 +0800417
liubin281ac462023-07-19 14:22:54 +0800418 mbtk_info_pack_send(handle->client_fd, pack);
419 mbtk_info_pack_free(&pack);
420
421 if(recv_buff != NULL)
422 handle->data = recv_buff;
liubin281ac462023-07-19 14:22:54 +0800423 // Wait for server response.
liubin281ac462023-07-19 14:22:54 +0800424 pthread_cond_wait(&handle->cond, &handle->mutex);
425 handle->is_waitting = false;
426 pthread_mutex_unlock(&handle->mutex);
liubin281ac462023-07-19 14:22:54 +0800427 if(handle->info_err == MBTK_INFO_ERR_SUCCESS)
428 {
429 LOG("REQ %s success.", id2str(id));
430 int recv_len = 0;
431 if(recv_buff && handle->data_len > 0) {
432 recv_len = handle->data_len;
433 handle->data_len = 0;
434 handle->data = NULL;
435 }
wangyouqiang962740a2023-11-02 19:27:22 +0800436 pthread_mutex_unlock(&handle->send_mutex);
b.liubcf86c92024-08-19 19:48:28 +0800437
liubin281ac462023-07-19 14:22:54 +0800438 return recv_len;
439 } else {
440 LOG("REQ %s fail : %s", id2str(id), err2str(handle->info_err));
yq.wangb783cc32024-08-13 23:23:06 -0700441 pthread_mutex_unlock(&handle->send_mutex);
wangyouqiang80487e42024-05-24 15:06:20 +0800442
liubin281ac462023-07-19 14:22:54 +0800443 return -1;
444 }
445}
446
b.liubcf86c92024-08-19 19:48:28 +0800447void mbtk_ril_lib_info_print()
448{
449 MBTK_SOURCE_INFO_PRINT("mbtk_ril_lib");
450}
liubin281ac462023-07-19 14:22:54 +0800451
452mbtk_info_handle_t* mbtk_info_handle_get()
453{
454 mbtk_info_handle_t* handle = (mbtk_info_handle_t*)malloc(sizeof(mbtk_info_handle_t));
455 if(!handle)
456 {
457 LOG("malloc() error[%d].", errno);
458 return NULL;
459 }
460 memset(handle, 0, sizeof(mbtk_info_handle_t));
461 handle->client_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
462 if(handle->client_fd < 0)
463 {
464 LOG("socket() fail[%d].", errno);
465 goto error;
466 }
467
468 // Set O_NONBLOCK
469 int flags = fcntl(handle->client_fd, F_GETFL, 0);
470 if (flags < 0)
471 {
472 LOG("Get flags error:%d", errno);
473 goto error;
474 }
475 flags |= O_NONBLOCK;
476 if (fcntl(handle->client_fd, F_SETFL, flags) < 0)
477 {
478 LOG("Set flags error:%d", errno);
479 goto error;
480 }
481
482 struct sockaddr_un cli_addr;
483 memset(&cli_addr, 0, sizeof(cli_addr));
484 cli_addr.sun_family = AF_LOCAL;
485 strcpy(cli_addr.sun_path, SOCK_INFO_PATH);
486 if(connect(handle->client_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
487 {
488 LOG("connect() fail[%d].", errno);
489 goto error;
490 }
491
492 if(pipe(handle->exit_fd)) {
493 LOG("pipe() fail[%d].", errno);
494 goto error;
495 }
496#if 0
497 pthread_attr_t thread_attr;
498 pthread_attr_init(&thread_attr);
499 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
500 {
501 LOG("pthread_attr_setdetachstate() fail.");
502 goto error;
503 }
504
505 if(pthread_create(&(handle->read_thread_id), &thread_attr, info_read_run, handle))
506 {
507 LOG("pthread_create() fail.");
508 goto error;
509 }
510 pthread_attr_destroy(&thread_attr);
511#else
512 if(pthread_create(&(handle->read_thread_id), NULL, info_read_run, handle))
513 {
514 LOG("pthread_create() fail.");
515 goto error;
516 }
517#endif
518
519 pthread_mutex_init(&handle->mutex, NULL);
wangyouqiang80487e42024-05-24 15:06:20 +0800520#ifndef MBTK_SG_SUPPORT
wangyouqiang962740a2023-11-02 19:27:22 +0800521 pthread_mutex_init(&handle->send_mutex, NULL);
wangyouqiang80487e42024-05-24 15:06:20 +0800522#endif
liubin281ac462023-07-19 14:22:54 +0800523 pthread_cond_init(&handle->cond, NULL);
524 handle->is_waitting = false;
525
526 //mbtk wyq for server_ready_status add start
527 int timeout = 5;//The wait server timeout by default
528 LOG("wait server handshake message--->.");
529 while(timeout)
530 {
531 if(handle->server_ready_status)
532 {
533 break;
534 }
535 else
536 {
537 sleep(1);
538 timeout--;
539 }
540 }
541
542 if(timeout <= 0)
543 {
544 if(handle->exit_fd[1] > 0)
545 {
546 write(handle->exit_fd[1], "EXIT", 4);
547 }
548 pthread_join(handle->read_thread_id,NULL);
549 LOG("mbtk_info_handle_get() server not ready.");
550 goto error;
551 }
552 else
553 {
554 LOG("mbtk_info_handle_get() server ready ok.");
555 }
556 //mbtk wyq for server_ready_status add end
557 return handle;
558error:
559 if(handle)
560 {
561 if(handle->client_fd > 0)
562 {
563 close(handle->client_fd);
564 }
565 if(handle->exit_fd[0] > 0) {
566 close(handle->exit_fd[0]);
567 }
568 if(handle->exit_fd[1] > 0) {
569 close(handle->exit_fd[1]);
570 }
571 free(handle);
572 handle = NULL;
573 }
574
575 return NULL;
576}
577
578int mbtk_info_handle_free(mbtk_info_handle_t** handle)
579{
580 if(handle == NULL || *handle == NULL)
581 {
582 LOG("Handle is NULL.");
583 return -1;
584 }
585
586 if((*handle)->exit_fd[1] > 0) {
587 write((*handle)->exit_fd[1], "EXIT", 4);
588 }
589
590 // Wait read_thread exit.
591 pthread_join((*handle)->read_thread_id,NULL);
592
593 if((*handle)->exit_fd[0] > 0) {
594 close((*handle)->exit_fd[0]);
595 (*handle)->exit_fd[0] = -1;
596 }
597
598 if((*handle)->exit_fd[1] > 0) {
599 close((*handle)->exit_fd[1]);
600 (*handle)->exit_fd[1] = -1;
601 }
602
603 if((*handle)->client_fd > 0)
604 {
605 close((*handle)->client_fd);
606 (*handle)->client_fd = -1;
607 }
608 free(*handle);
609 *handle = NULL;
610 return 0;
611}
612
613/*
614* Get platform version.
615*/
616int mbtk_version_get(mbtk_info_handle_t* handle, void *version)
617{
618 if(handle == NULL || version == NULL)
619 {
620 LOGE("ARG error.");
621 return -1;
622 }
623
624 if(info_item_process(handle, MBTK_INFO_ID_DEV_VERSION_REQ, NULL, 0, version) > 0) {
625 LOG("Version : %s", version);
626 return 0;
627 } else {
628 return handle->info_err;
629 }
630}
631
632/*
633* Get platform version.
634*/
635int mbtk_model_get(mbtk_info_handle_t* handle, void *model)
636{
637 if(handle == NULL || model == NULL)
638 {
639 LOGE("ARG error.");
640 return -1;
641 }
642
643 if(info_item_process(handle, MBTK_INFO_ID_DEV_MODEL_REQ, NULL, 0, model) > 0) {
644 LOG("Version : %s", model);
645 return 0;
646 } else {
647 return handle->info_err;
648 }
649}
650
651/*
652* Get platform IMEI.
653*/
654int mbtk_imei_get(mbtk_info_handle_t* handle, void *imei)
655{
656 if(handle == NULL || imei == NULL)
657 {
658 LOGE("ARG error.");
659 return -1;
660 }
661 if(info_item_process(handle, MBTK_INFO_ID_DEV_IMEI_REQ, NULL, 0, imei) > 0) {
662 LOG("IMEI : %s", imei);
663 return 0;
664 } else {
665 return handle->info_err;
666 }
667}
668
669/*
670* Get platform SN.
671*/
672int mbtk_sn_get(mbtk_info_handle_t* handle, void *sn)
673{
674 if(handle == NULL || sn == NULL)
675 {
676 LOGE("ARG error.");
677 return -1;
678 }
679 if(info_item_process(handle, MBTK_INFO_ID_DEV_SN_REQ, NULL, 0, sn) > 0) {
680 LOG("SN : %s", sn);
681 return 0;
682 } else {
683 return handle->info_err;
684 }
685}
686
687/*
688* Get platform MEID.
689*/
690int mbtk_meid_get(mbtk_info_handle_t* handle, void *meid)
691{
692 if(handle == NULL || meid == NULL)
693 {
694 LOGE("ARG error.");
695 return -1;
696 }
697 if(info_item_process(handle, MBTK_INFO_ID_DEV_MEID_REQ, NULL, 0, meid) > 0) {
698 LOG("MEID : %s", meid);
699 return 0;
700 } else {
701 return handle->info_err;
702 }
703}
704
705/*
706* Return VoLTE state.
707*/
708int mbtk_volte_state_get(mbtk_info_handle_t* handle, int *volte_state)
709{
710 uint8 state;
711 if(handle == NULL || volte_state == NULL)
712 {
713 LOGE("ARG error.");
714 return -1;
715 }
716 if(info_item_process(handle, MBTK_INFO_ID_DEV_VOLTE_REQ, NULL, 0, &state) > 0) {
717 LOG("VoLTE State : %d", state);
718 *volte_state = state;
719 return 0;
720 } else {
721 return handle->info_err;
722 }
723}
724
725/*
726* Set VoLTE state.
727*
728* volte_state:
729* 0 : Close VoLTE.
730* 1 : Open VoLTE.
731*
732* Restarting takes effect after execution.
733*/
734int mbtk_volte_state_set(mbtk_info_handle_t* handle, int volte_state)
735{
736 if(handle == NULL)
737 {
738 LOGE("ARG error.");
739 return -1;
740 }
741 return info_item_process(handle, MBTK_INFO_ID_DEV_VOLTE_REQ, (uint8*)&volte_state, sizeof(uint8), NULL) ? handle->info_err : 0;
742}
743
744/*
745* Get platform IMSI.
746*/
747int mbtk_imsi_get(mbtk_info_handle_t* handle, void *imsi)
748{
749 if(handle == NULL || imsi == NULL)
750 {
751 LOGE("ARG error.");
752 return -1;
753 }
754 if(info_item_process(handle, MBTK_INFO_ID_SIM_IMSI_REQ, NULL, 0, imsi) > 0) {
755 LOG("IMSI : %s", imsi);
756 return 0;
757 } else {
758 return handle->info_err;
759 }
760}
761
762/*
763* Get platform ICCID.
764*/
765int mbtk_iccid_get(mbtk_info_handle_t* handle, void *iccid)
766{
767 if(handle == NULL || iccid == NULL)
768 {
769 LOGE("ARG error.");
770 return -1;
771 }
772 if(info_item_process(handle, MBTK_INFO_ID_SIM_ICCID_REQ, NULL, 0, iccid) > 0) {
773 LOG("ICCID : %s", iccid);
774 return 0;
775 } else {
776 return handle->info_err;
777 }
778}
779
780/*
781* Get current phone number.
782*/
783int mbtk_phone_number_get(mbtk_info_handle_t* handle, void *phone_number)
784{
785 if(handle == NULL || phone_number == NULL)
786 {
787 LOGE("ARG error.");
788 return -1;
789 }
790 if(info_item_process(handle, MBTK_INFO_ID_SIM_PN_REQ, NULL, 0, phone_number) > 0) {
791 LOG("Phone Number : %s", phone_number);
792 return 0;
793 } else {
794 return handle->info_err;
795 }
796}
797
798/*
799* Get PIN’s number of remaining retry
800*/
801int mbtk_pin_last_num_get(mbtk_info_handle_t* handle, mbtk_pin_puk_last_times *last_times)
802{
803 if(handle == NULL || last_times == NULL)
804 {
805 LOGE("ARG error.");
806 return -1;
807 }
808 if(info_item_process(handle, MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ, NULL, 0, last_times) > 0) {
809 LOG("Sim sim_pin_puk_last_times : %d, %d, %d, %d", last_times->p1_retry,last_times->p2_retry,last_times->puk1_retry,last_times->puk2_retry);
810 return 0;
811 } else {
812 return handle->info_err;
813 }
814}
815
816/*
817* emable PIN
818*/
819int mbtk_enable_pin(mbtk_info_handle_t* handle, mbtk_enable_pin_info *pin)
820{
821 if(handle == NULL)
822 {
823 LOGE("ARG error.");
824 return -1;
825 }
826 if(info_item_process(handle, MBTK_INFO_ID_SIM_ENABLE_PIN_REQ, pin, sizeof(mbtk_enable_pin_info), NULL) >= 0) {
827 LOG("pin Number : %s", pin->pin_value);
828 return 0;
829 } else {
830 return handle->info_err;
831 }
832
833}
834
835/*
yq.wang586a0df2024-10-24 20:10:37 -0700836* GET PIN STATE
837*/
838int mbtk_get_pin_state(mbtk_info_handle_t* handle, mbtk_pin_state_enum *pin_state)
839{
840 if(handle == NULL)
841 {
842 LOGE("ARG error.");
843 return -1;
844 }
845 if(info_item_process(handle, MBTK_INFO_ID_SIM_ENABLE_PIN_REQ, NULL, 0, pin_state) >= 0) {
846 LOGD("pin state: %d", *pin_state);
847 return 0;
848 } else {
849 return handle->info_err;
850 }
851
852}
853
854
855/*
liubin281ac462023-07-19 14:22:54 +0800856* Verify PIN
857*/
858int mbtk_verify_pin(mbtk_info_handle_t* handle, char *pin)
859{
860 if(handle == NULL)
861 {
862 LOGE("ARG error.");
863 return -1;
864 }
865 if(info_item_process(handle, MBTK_INFO_ID_SIM_PIN_REQ, pin, strlen(pin), NULL) >= 0) {
866 LOG("pin Number : %s", pin);
867 return 0;
868 } else {
869 return handle->info_err;
870 }
871
872}
873
874/*
875* Verify PIN
876*/
877int mbtk_change_pin(mbtk_info_handle_t* handle, mbtk_change_pin_info *pin)
878{
879 if(handle == NULL)
880 {
881 LOGE("ARG error.");
882 return -1;
883 }
884 if(info_item_process(handle, MBTK_INFO_ID_SIM_CHANGE_PIN_REQ, pin, sizeof(mbtk_change_pin_info), NULL) >= 0) {
885 LOG("Change PIN : %s -> %s", pin->old_pin_value, pin->new_pin_value);
886 return 0;
887 } else {
888 return handle->info_err;
889 }
890}
891
892/*
893* unblock_pin
894*/
895int mbtk_unlock_pin(mbtk_info_handle_t* handle, mbtk_unlock_pin_info *pin)
896{
897 if(handle == NULL)
898 {
899 LOGE("ARG error.");
900 return -1;
901 }
902 if(info_item_process(handle, MBTK_INFO_ID_SIM_PUK_REQ, pin, sizeof(mbtk_unlock_pin_info), NULL) >= 0) {
903 LOG("Unlock : %s , %s", pin->pin_value , pin->puk_value);
904 return 0;
905 } else {
906 return handle->info_err;
907 }
908}
909
910/*
911* Get plmn list
912*/
913int mbtk_get_plmn_list(mbtk_info_handle_t* handle, mbtk_plmn_info *pin)
914{
915 if(handle == NULL)
916 {
917 LOGE("ARG error.");
918 return -1;
919 }
920 if(info_item_process(handle, MBTK_INFO_ID_SIM_PLMN_REQ, NULL, 0, pin) >= 0) {
921 //LOG("pin Number : %s", pin);
922 return 0;
923 } else {
924 return handle->info_err;
925 }
926}
927
928
929/*
930* Get available network.
931*/
932int mbtk_available_net_get(mbtk_info_handle_t* handle, list_node_t **net_list)
933{
934 if(handle == NULL)
935 {
936 LOGE("ARG error.");
937 return -1;
938 }
939 *net_list = list_create(NULL);
940 if(*net_list == NULL)
941 {
942 LOG("list_create() fail.");
943 return MBTK_INFO_ERR_MEMORY;
944 }
945
946 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
947 int buff_len;
948 if((buff_len = info_item_process(handle, MBTK_INFO_ID_NET_AVAILABLE_REQ, NULL, 0, buff)) > 0) {
949 int i = 0;
950 while (i < buff_len / sizeof(mbtk_net_info_t))
951 {
952 mbtk_net_info_t* net = (mbtk_net_info_t*)malloc(sizeof(mbtk_net_info_t));
953 if(net == NULL)
954 {
955 LOG("malloc() fail.");
956 list_free(*net_list);
957 return MBTK_INFO_ERR_MEMORY;
958 }
959 memcpy(net, buff + i * sizeof(mbtk_net_info_t), sizeof(mbtk_net_info_t));
960 list_add(*net_list, net);
961
962 LOG("NET-%d: %d, %d, %d, %d", i + 1, net->net_sel_mode, net->net_type, net->net_state, net->plmn);
963 i++;
964 }
965
966 return 0;
967 } else {
968 list_free(*net_list);
969 return handle->info_err;
970 }
971}
972
973/*
974* Set network select mode. (+COPS=...)
975*/
976int mbtk_net_sel_mode_set(mbtk_info_handle_t* handle, const mbtk_net_info_t *net)
977{
978 if(handle == NULL || net == NULL)
979 {
980 LOGE("ARG error.");
981 return -1;
982 }
983 return info_item_process(handle, MBTK_INFO_ID_NET_SEL_MODE_REQ, net, sizeof(mbtk_net_info_t), NULL) ? handle->info_err : 0;
984}
985
986/*
987* Get network select mode. (+COPS?)
988*/
989int mbtk_net_sel_mode_get(mbtk_info_handle_t* handle, mbtk_net_info_t *net)
990{
991 if(handle == NULL || net == NULL)
992 {
993 LOGE("ARG error.");
994 return -1;
995 }
996 if(info_item_process(handle, MBTK_INFO_ID_NET_SEL_MODE_REQ, NULL, 0, net) > 0) {
997 LOG("NET : %d, %d, %d, %d", net->net_sel_mode, net->net_type, net->net_state, net->plmn);
998 return 0;
999 } else {
1000 return handle->info_err;
1001 }
1002}
1003
1004/*
1005* Get platform support bands.
1006*/
1007int mbtk_support_band_get(mbtk_info_handle_t* handle, mbtk_band_info_t *band)
1008{
1009 uint8 type = 0; // Get support bands.
1010 if(handle == NULL || band == NULL)
1011 {
1012 LOGE("ARG error.");
1013 return -1;
1014 }
1015 if(info_item_process(handle, MBTK_INFO_ID_NET_BAND_REQ, &type, sizeof(uint8), band) > 0) {
1016 LOG("BAND : %d, %d, %d, %d, %d", band->net_pref, band->gsm_band, band->umts_band, band->tdlte_band, band->fddlte_band);
1017 return 0;
1018 } else {
1019 return handle->info_err;
1020 }
1021}
1022
1023/*
1024* Get platform current bands.
1025*/
1026int mbtk_current_band_get(mbtk_info_handle_t* handle, mbtk_band_info_t *band)
1027{
1028 uint8 type = 1; // Get current bands.
1029 if(handle == NULL || band == NULL)
1030 {
1031 LOGE("ARG error.");
1032 return -1;
1033 }
1034 if(info_item_process(handle, MBTK_INFO_ID_NET_BAND_REQ, &type, sizeof(uint8), band) > 0) {
1035 LOG("BAND : %d, %d, %d, %d, %d", band->net_pref, band->gsm_band, band->umts_band, band->tdlte_band, band->fddlte_band);
1036 return 0;
1037 } else {
1038 return handle->info_err;
1039 }
1040}
1041
1042/*
1043* Set platform current bands.
1044*/
1045int mbtk_current_band_set(mbtk_info_handle_t* handle, const mbtk_band_info_t *band)
1046{
1047 if(handle == NULL || band == NULL)
1048 {
1049 LOGE("ARG error.");
1050 return -1;
1051 }
1052 return info_item_process(handle, MBTK_INFO_ID_NET_BAND_REQ, band, sizeof(mbtk_band_info_t), NULL) ? handle->info_err : 0;
1053}
1054
1055/*
1056* Get current cell infomation.
1057*/
1058int mbtk_cell_get(mbtk_info_handle_t* handle, mbtk_cell_type_enum *type, list_node_t **cell_list)
1059{
1060 if(handle == NULL)
1061 {
1062 LOGE("ARG error.");
1063 return -1;
1064 }
1065 *cell_list = list_create(NULL);
1066 if(*cell_list == NULL)
1067 {
1068 LOG("list_create() fail.");
1069 return MBTK_INFO_ERR_MEMORY;
1070 }
1071
1072 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
1073 int buff_len;
1074 if((buff_len = info_item_process(handle, MBTK_INFO_ID_NET_CELL_REQ, NULL, 0, buff)) > 0) {
1075 int i = 0;
1076 *type = buff[0]; // Set network type.
1077 while (i < (buff_len - sizeof(uint8)) / sizeof(mbtk_cell_info_t))
1078 {
1079 mbtk_cell_info_t* cell = (mbtk_cell_info_t*)malloc(sizeof(mbtk_cell_info_t));
1080 if(cell == NULL)
1081 {
1082 LOG("malloc() fail.");
1083 list_free(*cell_list);
1084 return MBTK_INFO_ERR_MEMORY;
1085 }
1086 memcpy(cell, buff + sizeof(uint8) + i * sizeof(mbtk_cell_info_t), sizeof(mbtk_cell_info_t));
1087 list_add(*cell_list, cell);
1088
1089 // LOG("Cell-%d: %d, %d, %d, %d, %d", i + 1, cell->value1, cell->value2, cell->value3, cell->value4, cell->value5);
1090 i++;
1091 }
1092
1093 return 0;
1094 } else {
1095 list_free(*cell_list);
1096 return handle->info_err;
1097 }
1098}
1099/*
1100* Set cell info.
1101*
1102* at*CELL=<mode>,<act>,< band>,<freq>,<cellId>
1103* at*cell=2,3,,40936,429 //
1104* at*cell=0 //
1105*
1106* Restarting takes effect after execution.
1107*/
1108int mbtk_cell_set(mbtk_info_handle_t* handle, char * info, char* response)
1109{
1110 printf("mbtk_cell_set() info:%s, len:%d",info, strlen(info));
1111 char req[128] = {0};
1112 if( info_item_process(handle, MBTK_INFO_ID_NET_CELL_REQ, info, strlen(info), req) > 0){
1113 memcpy(response, req, strlen(req));
1114 return 0;
1115 }
1116 else{
1117 return 0;
1118 // return handle->info_err;
1119 }
1120}
1121
1122/*
1123* Get all APN informations.
1124*/
1125int mbtk_apn_get(mbtk_info_handle_t* handle, int *apn_num, mbtk_apn_info_t apns[])
1126{
1127 int len;
1128 if(handle == NULL || apn_num == NULL || apns == NULL)
1129 {
1130 LOGE("ARG error.");
1131 return -1;
1132 }
1133 uint8 data[SOCK_MSG_LEN_MAX];
1134 if((len = info_item_process(handle, MBTK_INFO_ID_NET_APN_REQ, NULL, 0, data)) > 0) {
1135 /*
1136 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
1137 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
1138 */
1139 uint8* ptr = data;
1140 if(apn_num == NULL || apns == NULL || *apn_num < *ptr) {
1141 *apn_num = 0;
1142 LOGE("APN array size to not enough.");
1143 return -1;
1144 }
1145 *apn_num = *ptr++;
1146 LOG("APN Number : %d", *apn_num);
1147 int i = 0;
1148 while(i < *apn_num) {
1149 memset(&(apns[i]), 0 ,sizeof(mbtk_apn_info_t));
1150 apns[i].cid = *ptr++;
1151 apns[i].ip_type = (mbtk_ip_type_enum)(*ptr++);
1152
1153 // apn
1154 len = byte_2_uint16(ptr, false);
1155 ptr += sizeof(uint16);
1156 if(len > 0) { // Has APN
1157 memcpy(apns[i].apn, ptr, len);
1158 ptr += len;
1159 }
1160 // user
1161 len = byte_2_uint16(ptr, false);
1162 ptr += sizeof(uint16);
1163 if(len > 0) { // Has APN
1164 memcpy(apns[i].user, ptr, len);
1165 ptr += len;
1166 }
1167
1168 // pass
1169 len = byte_2_uint16(ptr, false);
1170 ptr += sizeof(uint16);
1171 if(len > 0) { // Has APN
1172 memcpy(apns[i].pass, ptr, len);
1173 ptr += len;
1174 }
1175 // auth
1176 len = byte_2_uint16(ptr, false);
1177 ptr += sizeof(uint16);
1178 if(len > 0) { // Has APN
1179 memcpy(apns[i].auth, ptr, len);
1180 ptr += len;
1181 }
1182
1183 i++;
1184 }
1185 return 0;
1186 } else {
1187 return handle->info_err;
1188 }
1189}
1190
1191/*
wangyouqiang80487e42024-05-24 15:06:20 +08001192* Get all APN informations.
1193*/
1194int mbtk_qser_apn_get(mbtk_info_handle_t* handle, int *apn_num, mbtk_qser_apn_info_s apns[])
1195{
1196 int len;
1197 if(handle == NULL || apn_num == NULL || apns == NULL)
1198 {
1199 LOGE("ARG error.");
1200 return -1;
1201 }
1202 uint8 data[SOCK_MSG_LEN_MAX];
1203 if((len = info_item_process(handle, MBTK_INFO_ID_NET_QSER_APN_REQ, NULL, 0, data)) > 0) {
1204 /*
1205 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth[1]><apn_type_len[2]><apn_type_len>...
1206 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth[1]><apn_type_len[2]><apn_type_len>
1207 */
1208 uint8* ptr = data;
1209 if(apn_num == NULL || apns == NULL || *apn_num < *ptr) {
1210 *apn_num = 0;
1211 LOGE("APN array size to not enough.");
1212 return -1;
1213 }
1214 *apn_num = *ptr++;
1215 LOGD("APN Number : %d", *apn_num);
1216 int i = 0;
1217 while(i < *apn_num) {
1218 memset(&(apns[i]), 0x0 ,sizeof(mbtk_qser_apn_info_s));
1219 apns[i].cid = *ptr++;
1220 apns[i].ip_type = (mbtk_ip_type_enum)(*ptr++);
1221
1222 // apn
1223 len = byte_2_uint16(ptr, false);
1224 ptr += sizeof(uint16);
1225 if(len > 0) { // Has APN
1226 memcpy(apns[i].apn_name, ptr, len);
1227 ptr += len;
1228 }
1229 // user
1230 len = byte_2_uint16(ptr, false);
1231 ptr += sizeof(uint16);
1232 if(len > 0) { // Has APN
1233 memcpy(apns[i].user_name, ptr, len);
1234 ptr += len;
1235 }
1236
1237 // pass
1238 len = byte_2_uint16(ptr, false);
1239 ptr += sizeof(uint16);
1240 if(len > 0) { // Has APN
1241 memcpy(apns[i].user_pass, ptr, len);
1242 ptr += len;
1243 }
1244 // auth
1245 apns[i].auth_proto = (mbtk_apn_auth_proto_enum)(*ptr++);
1246
1247 //apn_type
1248 len = byte_2_uint16(ptr, false);
1249 ptr += sizeof(uint16);
1250 if(len > 0) { // Has APN
1251 memcpy(apns[i].apn_type, ptr, len);
1252 ptr += len;
1253 }
1254
1255 i++;
1256 }
b.liubcf86c92024-08-19 19:48:28 +08001257 }
wangyouqiang80487e42024-05-24 15:06:20 +08001258 else if(len == 0)
1259 {
1260 LOGD("get data len : 0.");
1261 *apn_num = 0;
1262 return 0;
1263 }
1264 else
1265 {
1266 return handle->info_err;
1267 }
1268
1269 return 0;
1270}
1271
1272/*
1273* qser Set current APN informations.
1274*/
1275int mbtk_qser_apn_set(mbtk_info_handle_t* handle, mbtk_qser_apn_info_s *apninfo, unsigned char *cid)
1276{
1277 if(handle == NULL)
1278 {
1279 LOGE("ARG error.");
1280 return -1;
1281 }
b.liubcf86c92024-08-19 19:48:28 +08001282
wangyouqiang80487e42024-05-24 15:06:20 +08001283 uint8 data[SOCK_MSG_LEN_MAX];
1284 memset(data, 0, SOCK_MSG_LEN_MAX);
1285 // cid : 2 - 7
1286 if(apninfo->req_type != MBTK_APN_REQ_TYPE_ADD && (apninfo->cid < MBTK_APN_CID_MIN || apninfo->cid > MBTK_APN_CID_MAX)) {
1287 LOGE("CID error.");
1288 return -1;
1289 }
1290
1291 uint8* ptr = data;
1292 // <cid[1]><ip_type[1]><req_type[1]><auth[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><apn_type[2]>
1293 *ptr++ = (uint8)apninfo->cid;
1294 *ptr++ = (uint8)apninfo->ip_type;
1295 *ptr++ = (uint8)apninfo->req_type;
1296 *ptr++ = (uint8)apninfo->auth_proto;
1297 if(str_empty(apninfo->apn_name)) {
1298 uint16_2_byte((uint16)0, ptr, false);
1299 ptr += sizeof(uint16);
1300 } else {
1301 uint16_2_byte((uint16)strlen((char *)apninfo->apn_name), ptr, false);
1302 ptr += sizeof(uint16);
1303 memcpy(ptr, apninfo->apn_name, strlen((char *)apninfo->apn_name));
1304 ptr += strlen((char *)apninfo->apn_name);
1305 }
1306 if(str_empty(apninfo->user_name)) {
1307 uint16_2_byte((uint16)0, ptr, false);
1308 ptr += sizeof(uint16);
1309 } else {
1310 uint16_2_byte((uint16)strlen((char *)apninfo->user_name), ptr, false);
1311 ptr += sizeof(uint16);
1312 memcpy(ptr, apninfo->user_name, strlen((char *)apninfo->user_name));
1313 ptr += strlen((char *)apninfo->user_name);
1314 }
1315
1316 if(str_empty(apninfo->user_pass)) {
1317 uint16_2_byte((uint16)0, ptr, false);
1318 ptr += sizeof(uint16);
1319 } else {
1320 uint16_2_byte((uint16)strlen((char *)apninfo->user_pass), ptr, false);
1321 ptr += sizeof(uint16);
1322 memcpy(ptr, apninfo->user_pass, strlen((char *)apninfo->user_pass));
1323 ptr += strlen((char *)apninfo->user_pass);
1324 }
1325
1326 if(str_empty(apninfo->apn_type)) {
1327 uint16_2_byte((uint16)0, ptr, false);
1328 ptr += sizeof(uint16);
1329 }
1330 else
1331 {
1332 uint16_2_byte((uint16)strlen((char *)apninfo->apn_type), ptr, false);
1333 ptr += sizeof(uint16);
1334 memcpy(ptr, apninfo->apn_type, strlen((char *)apninfo->apn_type));
1335 ptr += strlen((char *)apninfo->apn_type);
1336 }
1337
1338 if(info_item_process(handle, MBTK_INFO_ID_NET_QSER_APN_REQ, data, ptr - data, (void *)cid) < 0)
1339 {
1340 return handle->info_err;
1341 }
1342
1343 return 0;
1344}
1345
1346/*
liubin281ac462023-07-19 14:22:54 +08001347* Set current APN informations.
1348*/
1349int mbtk_apn_set(mbtk_info_handle_t* handle, int cid, mbtk_ip_type_enum ip_type, const void* apn_name,
1350 const void *user_name, const void *user_pass, const void *auth)
1351{
1352 if(handle == NULL)
1353 {
1354 LOGE("ARG error.");
1355 return -1;
1356 }
1357 uint8 data[SOCK_MSG_LEN_MAX];
1358 memset(data, 0, SOCK_MSG_LEN_MAX);
1359 // cid : 2 - 7
1360 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1361 LOGE("CID error.");
1362 return -1;
1363 }
1364 uint8* ptr = data;
1365 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
1366 *ptr++ = (uint8)cid;
1367 *ptr++ = (uint8)ip_type;
1368 if(str_empty(apn_name)) {
1369 uint16_2_byte((uint16)0, ptr, false);
1370 ptr += sizeof(uint16);
1371 } else {
1372 uint16_2_byte((uint16)strlen(apn_name), ptr, false);
1373 ptr += sizeof(uint16);
1374 memcpy(ptr, apn_name, strlen(apn_name));
1375 ptr += strlen(apn_name);
1376 }
1377 if(str_empty(user_name)) {
1378 uint16_2_byte((uint16)0, ptr, false);
1379 ptr += sizeof(uint16);
1380 } else {
1381 uint16_2_byte((uint16)strlen(user_name), ptr, false);
1382 ptr += sizeof(uint16);
1383 memcpy(ptr, user_name, strlen(user_name));
1384 ptr += strlen(user_name);
1385 }
1386
1387 if(str_empty(user_pass)) {
1388 uint16_2_byte((uint16)0, ptr, false);
1389 ptr += sizeof(uint16);
1390 } else {
1391 uint16_2_byte((uint16)strlen(user_pass), ptr, false);
1392 ptr += sizeof(uint16);
1393 memcpy(ptr, user_pass, strlen(user_pass));
1394 ptr += strlen(user_pass);
1395 }
1396
1397 if(str_empty(auth)) {
1398 uint16_2_byte((uint16)0, ptr, false);
1399 ptr += sizeof(uint16);
1400 } else {
1401 uint16_2_byte((uint16)strlen(auth), ptr, false);
1402 ptr += sizeof(uint16);
1403 memcpy(ptr, auth, strlen(auth));
1404 ptr += strlen(auth);
1405 }
1406
1407 return info_item_process(handle, MBTK_INFO_ID_NET_APN_REQ, data, ptr - data, NULL) ? handle->info_err : 0;
1408}
1409
liuyang0e49d9a2024-04-23 21:04:54 +08001410int mbtk_apn_del(mbtk_info_handle_t* handle, unsigned char profile_idx)
1411{
liuyang0e49d9a2024-04-23 21:04:54 +08001412 if(handle == NULL)
1413 {
1414 LOGE("ARG error.");
1415 return -1;
1416 }
liuyang1cefd852024-04-24 18:30:53 +08001417
1418 profile_idx++;
b.liubcf86c92024-08-19 19:48:28 +08001419 if(info_item_process(handle, MBTK_INFO_ID_NET_APN_DEL_REQ, &profile_idx, sizeof(profile_idx), NULL) >= 0)
liuyang0e49d9a2024-04-23 21:04:54 +08001420 {
liuyang1cefd852024-04-24 18:30:53 +08001421 LOG("profile_idx Number : %d", profile_idx);
liuyang0e49d9a2024-04-23 21:04:54 +08001422 return 0;
b.liubcf86c92024-08-19 19:48:28 +08001423 }
1424 else
liuyang0e49d9a2024-04-23 21:04:54 +08001425 {
1426 return handle->info_err;
1427 }
1428
1429}
1430
liubin281ac462023-07-19 14:22:54 +08001431/*
1432* Start data call.
1433*/
1434int mbtk_data_call_start(mbtk_info_handle_t* handle, int cid, int auto_conn_interval, bool boot_conn, int timeout)
1435{
1436 uint8 data[10];
1437 if(handle == NULL)
1438 {
1439 LOGE("ARG error.");
1440 return -1;
1441 }
1442 memset(data, 0, 10);
1443 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1444 LOGE("CID error.");
1445 return -1;
1446 }
1447 uint8* ptr = data;
1448 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
1449 call_type : mbtk_data_call_type_enum
1450 cid : 2 - 7
1451 timeout : second
1452 */
1453 *ptr++ = (uint8)MBTK_DATA_CALL_START;
1454 *ptr++ = (uint8)cid;
1455 *ptr++ = (uint8)(auto_conn_interval > 0 ? auto_conn_interval : 0); // 拨号失败后重拨间隔(s)
1456 *ptr++ = (uint8)(boot_conn ? 1 : 0); // 开机自动拨号
1457 if(timeout <= 0) {
1458 *ptr++ = (uint8)MBTK_DATA_CALL_TIMEOUT_DEFAULT;
1459 } else {
1460 *ptr++ = (uint8)timeout;
1461 }
1462
1463 return info_item_process(handle, MBTK_INFO_ID_NET_DATA_CALL_REQ, data, 5, NULL) ? handle->info_err : 0;
1464}
1465
1466/*
1467* Stop data call.
1468*/
1469int mbtk_data_call_stop(mbtk_info_handle_t* handle, int cid, int timeout)
1470{
1471 uint8 data[10];
1472 if(handle == NULL)
1473 {
1474 LOGE("ARG error.");
1475 return -1;
1476 }
1477 memset(data, 0, 10);
1478 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1479 LOGE("CID error.");
1480 return -1;
1481 }
1482 uint8* ptr = data;
1483 /* <call_type[1]><cid[1]><timeout[1]>
1484 call_type : mbtk_data_call_type_enum
1485 cid : 2 - 7
1486 timeout : second
1487 */
1488 *ptr++ = (uint8)MBTK_DATA_CALL_STOP;
1489 *ptr++ = (uint8)cid;
1490 *ptr++ = (uint8)timeout;
1491
1492 return info_item_process(handle, MBTK_INFO_ID_NET_DATA_CALL_REQ, data, 3, NULL) ? handle->info_err : 0;
1493}
1494
1495/*
1496* Get data call state.
1497*/
1498int mbtk_data_call_state_get(mbtk_info_handle_t* handle, int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6)
1499{
1500 uint8 data[10];
1501 if(handle == NULL)
1502 {
1503 LOGE("ARG error.");
1504 return -1;
1505 }
1506 uint8 recv_buff[SOCK_MSG_LEN_MAX]={0};
1507 memset(data, 0, 10);
1508 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1509 LOGE("CID error.");
1510 return -1;
1511 }
1512 uint8* ptr = data;
1513 /* <call_type[1]><cid[1]><timeout[1]>
1514 call_type : mbtk_data_call_type_enum
1515 cid : 2 - 7
1516 timeout : second
1517 */
1518 *ptr++ = (uint8)MBTK_DATA_CALL_STATE;
1519 *ptr++ = (uint8)cid;
1520 *ptr++ = (uint8)0;
1521
1522 if(ipv4) {
1523 memset(ipv4, 0, sizeof(mbtk_ipv4_info_t));
1524 }
1525
1526 if(ipv6) {
1527 memset(ipv6, 0, sizeof(mbtk_ipv6_info_t));
1528 }
1529
1530 if(info_item_process(handle, MBTK_INFO_ID_NET_DATA_CALL_REQ, data, 3, recv_buff) > 0) {
1531 if(recv_buff[0] == 0) { // IPv4 Only.
1532 if(ipv4) {
1533 memcpy(ipv4, recv_buff + sizeof(uint8), sizeof(mbtk_ipv4_info_t));
1534 }
1535 } else if(recv_buff[0] == 1) { // IPv6 Only.
1536 if(ipv6) {
1537 memcpy(ipv6, recv_buff + sizeof(uint8), sizeof(mbtk_ipv6_info_t));
1538 }
1539 } else if(recv_buff[0] == 2) { // IPv4 and IPv6.
1540 if(ipv4) {
1541 memcpy(ipv4, recv_buff + sizeof(uint8), sizeof(mbtk_ipv4_info_t));
1542 }
1543
1544 if(ipv6) {
1545 memcpy(ipv6, recv_buff + sizeof(uint8) + sizeof(mbtk_ipv4_info_t), sizeof(mbtk_ipv6_info_t));
1546 }
1547 } else {
1548 LOGE("Unknown IP type : %d", recv_buff[0]);
1549 return -1;
1550 }
1551 return 0;
1552 } else {
1553 return handle->info_err;
1554 }
1555}
1556
1557
1558
1559/*
1560* Get current network signal.
1561*/
1562int mbtk_net_signal_get(mbtk_info_handle_t* handle, mbtk_signal_info_t *signal)
1563{
1564 if(handle == NULL || signal == NULL)
1565 {
1566 LOGE("ARG error.");
1567 return -1;
1568 }
1569 if(info_item_process(handle, MBTK_INFO_ID_NET_SIGNAL_REQ, NULL, 0, signal) > 0) {
1570 LOG("Signal : %d, %d, %d, %d, %d, %d, %d", signal->rssi, signal->rxlev, signal->ber, signal->rscp, signal->ecno,
1571 signal->rsrq, signal->rsrp);
1572 return 0;
1573 } else {
1574 return handle->info_err;
1575 }
1576}
1577
1578/*
1579* Get current network register information.
1580*/
1581int mbtk_net_reg_get(mbtk_info_handle_t* handle, mbtk_net_reg_info_t *reg)
1582{
1583 if(handle == NULL || reg == NULL)
1584 {
1585 LOGE("ARG error.");
1586 return -1;
1587 }
1588 if(info_item_process(handle, MBTK_INFO_ID_NET_REG_REQ, NULL, 0, reg) > 0) {
1589 if(reg->call_state || reg->data_state || reg->ims_state) {
1590 LOGD("REG : call_state=%d, data_state=%d, ims_state=%d, net_type=%d, %04x, %08x", reg->call_state, reg->data_state, reg->ims_state, reg->type, reg->lac, reg->ci);
1591 } else {
1592 LOGE("Net not reg.");
1593 }
1594 return 0;
1595 } else {
1596 return handle->info_err;
1597 }
1598}
1599
r.xiao06db9a12024-04-14 18:51:15 -07001600/*
b.liufdf03172024-06-07 15:01:29 +08001601* Get current IMS enable or not.
r.xiao06db9a12024-04-14 18:51:15 -07001602*/
b.liufdf03172024-06-07 15:01:29 +08001603int mbtk_net_ims_get(mbtk_info_handle_t* handle, int* enable)
r.xiao06db9a12024-04-14 18:51:15 -07001604{
1605 uint8 state;
1606 if(handle == NULL)
1607 {
1608 LOGE("ARG error.");
1609 return -1;
1610 }
1611 if(info_item_process(handle, MBTK_INFO_ID_NET_IMS_REQ, NULL, 0, &state) > 0) {
b.liufdf03172024-06-07 15:01:29 +08001612 LOG("IMS enable : %d", state);
1613 *enable = state;
r.xiao06db9a12024-04-14 18:51:15 -07001614 return 0;
1615 } else {
1616 return handle->info_err;
1617 }
1618
1619}
1620
1621/*
b.liufdf03172024-06-07 15:01:29 +08001622* Set IMS enable or not. This function takes effect after starting the device.
r.xiao06db9a12024-04-14 18:51:15 -07001623*/
b.liufdf03172024-06-07 15:01:29 +08001624int mbtk_net_ims_set(mbtk_info_handle_t* handle, int enable)
r.xiao06db9a12024-04-14 18:51:15 -07001625{
1626 if(handle == NULL)
1627 {
1628 LOGE("ARG error.");
1629 return -1;
1630 }
1631
b.liufdf03172024-06-07 15:01:29 +08001632 return info_item_process(handle, MBTK_INFO_ID_NET_IMS_REQ, (uint8*)&enable, sizeof(uint8), NULL) ? handle->info_err : 0;
r.xiao06db9a12024-04-14 18:51:15 -07001633
1634}
1635
b.liufdf03172024-06-07 15:01:29 +08001636/*
1637* Get current network IMS register state.
1638*/
1639int mbtk_net_ims_reg_state_get(mbtk_info_handle_t* handle, int* reg)
1640{
1641 uint8 state;
1642 if(handle == NULL)
1643 {
1644 LOGE("ARG error.");
1645 return -1;
1646 }
1647 if(info_item_process(handle, MBTK_INFO_ID_NET_IMS_REG_STATE_REQ, NULL, 0, &state) > 0) {
1648 LOG("reg type : %d", state);
1649 *reg = state;
1650 return 0;
1651 } else {
1652 return handle->info_err;
1653 }
1654}
1655
liubin281ac462023-07-19 14:22:54 +08001656
1657/*
1658* Get radio state.
1659*/
1660int mbtk_radio_state_get(mbtk_info_handle_t* handle, int *radio_state)
1661{
1662 uint8 state;
1663 if(handle == NULL || radio_state == NULL)
1664 {
1665 LOGE("ARG error.");
1666 return -1;
1667 }
1668 if(info_item_process(handle, MBTK_INFO_ID_NET_RADIO_REQ, NULL, 0, &state) > 0) {
1669 LOG("Radio state : %d", state);
1670 *radio_state = state;
1671 return 0;
1672 } else {
1673 return handle->info_err;
1674 }
1675}
1676
1677/*
1678* Set radio state.
1679*/
1680int mbtk_radio_state_set(mbtk_info_handle_t* handle, int radio_state)
1681{
1682 if(handle == NULL)
1683 {
1684 LOGE("ARG error.");
1685 return -1;
1686 }
1687 return info_item_process(handle, MBTK_INFO_ID_NET_RADIO_REQ, (uint8*)&radio_state, sizeof(uint8), NULL) ? handle->info_err : 0;
1688}
1689
1690/*
1691* Get time type.
1692*/
1693int mbtk_time_get(mbtk_info_handle_t* handle, int *time_type)
1694{
1695 uint8 state;
1696 if(handle == NULL || time_type == NULL)
1697 {
1698 LOGE("ARG error.");
1699 return -1;
1700 }
1701 if(info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ, NULL, 0, &state) > 0) {
1702 LOG("Time type : %d", state);
1703 *time_type = state;
1704 return 0;
1705 } else {
1706 return handle->info_err;
1707 }
1708}
1709
1710/*
1711*Get Absolute time
1712*"23/05/24 06:09:32 +32 00"
1713*/
1714int mbtk_get_abs_time(char *time_str, time_t *time_out)
1715{
1716 struct tm tm_;
1717
1718 char *ptr = strstr(time_str + 10, " ");
1719 *ptr = '\0';
1720
1721 LOGD("time : \"%s\"", time_str);
1722#if 1
1723 if(strptime(time_str, "%y/%m/%d %T", &tm_) == NULL) {
1724 LOGE("strptime() fail.");
1725 return -1;
1726 }
1727#else
1728 int year, month, day, hour, minute,second,time_zone;
1729 if(strstr(time_str, "+")) {
1730 sscanf(time_str, "%d/%d/%d %d:%d:%d +%d",&year,&month,&day,&hour,&minute,&second,&time_zone);
1731 } else if(strstr(time_str, "-")) {
1732 sscanf(time_str, "%d/%d/%d %d:%d:%d -%d",&year,&month,&day,&hour,&minute,&second,&time_zone);
1733 } else {
1734 LOGE("Time format error:%s", time_str);
1735 return -1;
1736 }
1737
1738 // 1970+
1739 if(year < 70) { // 20xx
1740 tm_.tm_year = 2000 + year;
1741 } else { // 19xx
1742 tm_.tm_year = 1900 + year;
1743 }
1744 tm_.tm_mon = month - 1;
1745 tm_.tm_mday = day;
1746 tm_.tm_hour = hour;
1747 tm_.tm_min = minute;
1748 tm_.tm_sec = second;
1749 tm_.tm_isdst = 0;
1750#endif
1751
1752 time_t timeStamp = mktime(&tm_);
1753 LOGD("tm_.tm_year = %d,tm_.tm_mon = %d,tm_.tm_mday = %d,tm_.tm_hour = %d,tm_.tm_min = %d,tm_.tm_sec = %d,tm_.tm_isdst = %d",tm_.tm_year,tm_.tm_mon,tm_.tm_mday,tm_.tm_hour,tm_.tm_min,tm_.tm_sec,tm_.tm_isdst);
1754 LOGD("time = %ld,%x", timeStamp,timeStamp);
1755 *time_out = timeStamp;
1756
1757 return 0;
1758}
1759
1760/*
1761* Get time type.
1762* "23/05/24,06:09:32+32" -> "23/05/24 06:09:32 +32 00"
1763*/
1764int mbtk_net_time_get(mbtk_info_handle_t* handle, char* time_str)
1765{
1766 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
1767 if(handle == NULL || time_str == NULL)
1768 {
1769 LOGE("ARG error.");
1770 return -1;
1771 }
1772 //printf("mbtk_net_time_get begin info_item_process\n");
b.liubaa41e12024-07-19 15:07:24 +08001773 if(info_item_process(handle, MBTK_INFO_ID_DEV_CELL_TIME_REQ, NULL, 0, buff) > 0) {
liubin281ac462023-07-19 14:22:54 +08001774 memcpy(time_str,buff,strlen(buff));
1775
1776 uint8 *temp = strstr(time_str, ",");
1777 if(temp) {
1778 *temp = ' '; // ',' -> ' '
1779
1780 temp = strstr(time_str, "+");
1781 if(temp == NULL) {
1782 temp = strstr(time_str, "-");
1783 }
1784
1785 if(temp) {
1786 // Copy +XX or -XX
1787 uint8 *last_ptr = temp + strlen(temp) + 1;
1788 while(last_ptr > temp) {
1789 *last_ptr = *(last_ptr - 1);
1790 last_ptr--;
1791 }
1792
1793 *last_ptr = ' ';
1794
1795 memcpy(temp + strlen(temp), "00", 2);
1796
1797 LOGD("%s -> %s", buff, time_str);
1798 return 0;
1799 } else {
1800 LOGE("Time error:%s",buff);
1801 return MBTK_INFO_ERR_TIME_FORMAT;
1802 }
1803 } else {
1804 LOGE("Time error:%s",buff);
1805 return MBTK_INFO_ERR_TIME_FORMAT;
1806 }
1807 } else {
1808 return handle->info_err;
1809 }
1810}
1811
1812/*
1813* Set time.
1814*
1815* time_type:
1816* 0: Cell time
1817* 1: NTP time
1818* 2: User time
1819* time_str: "YYYY-MM-DD HH:MM:SS"
1820*/
1821int mbtk_time_set(mbtk_info_handle_t* handle, mbtk_time_type_enum time_type, char* time_str)
1822{
1823 if(handle == NULL)
1824 {
1825 LOGE("ARG error.");
1826 return -1;
1827 }
1828 uint8 buffer[100] = {0};
1829 buffer[0] = (uint8)time_type;
1830 if(time_type == MBTK_TIME_TYPE_USER) {
1831 if(!str_empty(time_str)) {
1832 memcpy(buffer + sizeof(uint8), time_str, strlen(time_str));
1833 return info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ,
1834 buffer, sizeof(uint8) + strlen(time_str), NULL) ? handle->info_err : 0;
1835 } else {
1836 return -1;
1837 }
1838 } else {
1839 return info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ,
1840 buffer, sizeof(uint8), NULL) ? handle->info_err : 0;
1841 }
1842}
1843
1844/*
1845* Return sms cmgf.
1846*/
1847int mbtk_sms_cmgf_get(mbtk_info_handle_t* handle, int *volte_state)
1848{
1849 uint8 state;
1850 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGF_REQ, NULL, 0, &state) > 0) {
1851 LOG("mbtk_sms_cmgf_get()-----------sms cmgf : %d", state);
1852 *volte_state = state;
1853 return 0;
1854 } else {
1855 return handle->info_err;
1856 }
1857}
1858
1859/*
1860* Set sms cmgf.
1861*
1862* volte_state:
1863* 0 : PDU mode.
1864* 1 : text mode.
1865*
1866* Restarting takes effect after execution.
1867*/
1868int mbtk_sms_cmgf_set(mbtk_info_handle_t* handle, int mode)
1869{
yq.wanga9b5dd02024-10-16 00:25:14 -07001870 LOGD("mbtk_sms_cmgf_set()--------mode=:%d, len:%d", mode, sizeof(uint8));
liubin281ac462023-07-19 14:22:54 +08001871 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGF_REQ, (uint8*)&mode, sizeof(uint8), NULL) ? handle->info_err : 0;
1872}
1873
1874/*
1875* Set sms cmgs.
1876*
1877if PDU mode (+CMGF=0):
1878+CMGS=<length><CR>
1879PDU is given<ctrl-Z/ESC>
1880
1881if text mode (+CMGF=1):
1882+CMGS=<da>[,<toda>]<CR>
1883text is entered<ctrl-Z/ESC>
1884
1885* Restarting takes effect after execution.
1886*/
1887int mbtk_sms_cmgs_set(mbtk_info_handle_t* handle, char * cmgs, char *resp)
1888{
yq.wanga9b5dd02024-10-16 00:25:14 -07001889 LOGD("mbtk_sms_cmgs_set(1)--------cmgs=:%s, len:%d", cmgs, strlen(cmgs));
liubin281ac462023-07-19 14:22:54 +08001890// char req[20] = {0}
1891 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGS_REQ, cmgs, strlen(cmgs), resp) > 0){
yq.wanga9b5dd02024-10-16 00:25:14 -07001892 LOGD("resp:%s", resp);
liubin281ac462023-07-19 14:22:54 +08001893 return 0;
1894 }else{
1895 return handle->info_err;
1896 }
1897}
1898
1899/*
1900* Set sms cmgw.
1901*
1902if text mode (+CMGF=1):
1903+CMGW=<oa/da>[,<tooa/toda>[,<stat>]]
1904<CR>
1905text is entered<ctrl-Z/ESC>
1906if PDU mode (+CMGF=0):
1907+CMGW=<length>[,<stat>]<CR>PDU is
1908given<ctrl-Z/ESC>
1909
1910*/
1911
1912int mbtk_sms_cmgw_set(mbtk_info_handle_t* handle, char * cmgw, char *resp)
1913{
1914 printf("mbtk_sms_cmgw_set() ----------cmgw:%s, len:%d", cmgw, strlen(cmgw));
1915 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGW_REQ, cmgw, strlen(cmgw), resp) > 0){
1916 printf("resp:%s\n", resp);
1917 return 0;
1918 }else{
1919 return handle->info_err;
1920 }
1921}
1922
1923/*
1924* Set sms cmgd.
1925*
1926* +CMGD=<index>[,<delflag>]
1927*
1928* Restarting takes effect after execution.
1929*/
1930int mbtk_sms_cmgd_set(mbtk_info_handle_t* handle, char * cmdg)
1931{
1932 printf("mbtk_sms_cmgd_set() cmdg:%s, len:%d",cmdg, strlen(cmdg));
1933 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGD_REQ, cmdg, strlen(cmdg), NULL) ? handle->info_err : 0;
1934}
1935
1936/*
r.xiaoeb9dba42024-02-07 02:16:13 -08001937* Get sms cmgd.
1938*
1939* +CMGD: (XXX,XXX)(0-4)
1940*
1941* Restarting takes effect after execution.
1942*/
1943int mbtk_sms_cmgd_get(mbtk_info_handle_t* handle, char * cmdg)
1944{
1945 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGD_REQ, NULL, 0, cmdg) ? handle->info_err : 0;
1946}
1947
1948
1949/*
liubin281ac462023-07-19 14:22:54 +08001950* Set sms cmgl.
1951*
1952* AT+CMGL[=<stat>]
1953*
1954* Restarting takes effect after execution.
1955*/
1956int mbtk_sms_cmgl_set(mbtk_info_handle_t* handle, char * cmgl, char *resp)
1957{
1958 printf("0mbtk_sms_cmgl_set() cmgl:%s, len:%d\n",cmgl, strlen(cmgl));
1959 char reg[5*1024] ={0};
1960 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMGL_REQ, cmgl, strlen(cmgl), reg) > 0){
1961 printf("len:%d , reg:%s\n", strlen(reg), reg);
1962 // memcpy(resp, reg, strlen(reg));
1963 return 0;
1964 }else {
1965 return handle->info_err;
1966 }
1967}
1968
1969/*
1970* Return sms csca.
1971*/
1972int mbtk_sms_csca_get(mbtk_info_handle_t* handle, char *buf)
1973{
1974 // char state;
1975 if(info_item_process(handle, MBTK_INFO_ID_SMS_CSCA_REQ, NULL, 0, buf) > 0) {
1976 LOG("mbtk_sms_csca_get()-----------sms csca : %s", buf);
1977 // *volte_state = state;
1978 return 0;
1979 } else {
1980 return handle->info_err;
1981 }
1982}
1983
1984/*
1985* Set sms csca.
1986*
1987* AT+CSCA=<number> [,<type>]
1988*
1989* Restarting takes effect after execution.
1990*/
1991int mbtk_sms_csca_set(mbtk_info_handle_t* handle, char * csca)
1992{
1993 printf("mbtk_sms_csca_set() csca:%s, len:%d",csca, strlen(csca));
1994 return info_item_process(handle, MBTK_INFO_ID_SMS_CSCA_REQ, csca, strlen(csca), NULL) ? handle->info_err : 0;
1995}
1996
1997/*
1998* Set sms csmp.
1999*
2000* AT+CSMP=[<fo>[,<vp>[,<pid>[,<dcs>]]]]
2001*
2002* Restarting takes effect after execution.
2003*/
2004int mbtk_sms_csmp_set(mbtk_info_handle_t* handle, char * csmp)
2005{
2006 printf("mbtk_sms_csmp_set() csmp:%s, len:%d",csmp, strlen(csmp));
2007 return info_item_process(handle, MBTK_INFO_ID_SMS_CSMP_REQ, csmp, strlen(csmp), NULL) ? handle->info_err : 0;
2008}
2009
2010/*
2011* Set sms cscb.
2012*
2013* AT+CSCB=<[<mode>[,<mids>[,<dcss>]]]>
2014*
2015* Restarting takes effect after execution.
2016*/
2017int mbtk_sms_cscb_set(mbtk_info_handle_t* handle, char * cscb)
2018{
2019 printf("mbtk_sms_cscb_set() cscb:%s, len:%d",cscb, strlen(cscb));
2020 return info_item_process(handle, MBTK_INFO_ID_SMS_CSCB_REQ, cscb, strlen(cscb), NULL) ? handle->info_err : 0;
2021}
2022
2023/*
2024* Set sms cnmi.
2025*
2026at+cnmi=1,2
2027
2028OK
2029if sending fails:
2030+CMS ERROR: <err>
2031*/
2032int mbtk_sms_cnmi_set(mbtk_info_handle_t* handle)
2033{
2034 printf("mbtk_sms_cnmi_set()------------start\n");
2035
2036 return info_item_process(handle, MBTK_INFO_ID_SMS_CNMI_REQ, NULL, 0, NULL)? handle->info_err : 0;
2037}
2038
2039/*
2040* Set sms cmss.
2041*
2042+CMSS=<index>[,<da>[,<toda>]]
2043
2044if sending successful:
2045+CMSS: <mr>
2046OK
2047if sending fails:
2048+CMS ERROR: <err>
2049*/
2050int mbtk_sms_cmss_set(mbtk_info_handle_t* handle, char * cmss, char *resp)
2051{
2052 printf("mbtk_sms_cmss_set()------------cmss:%s, len:%d", cmss, strlen(cmss));
2053 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMSS_REQ, cmss, strlen(cmss), resp) > 0){
2054 printf("resp:%s\n", resp);
2055 return 0;
2056 }else{
2057 return handle->info_err;
2058 }
2059}
2060
2061/*
2062* Return sms cmgf.
2063*/
2064int mbtk_sms_cpms_get(mbtk_info_handle_t* handle, char * mem)
2065{
2066 char req[128] = {0};
2067 if(info_item_process(handle, MBTK_INFO_ID_SMS_CPMS_REQ, NULL, 0, &req) > 0) {
2068 LOG("mbtk_sms_cpms_get() req : %s, strlen(mem_ptr):%d\n", req, strlen(req));
2069 memcpy(mem, req, strlen(req));
2070 return 0;
2071 } else {
2072 return handle->info_err;
2073 }
2074}
2075
2076
2077/*
2078* Set sms cpms.
2079*
2080* AT+CPMS=<mem1>[,<mem2>[,<mem3>]]
2081*
2082* Restarting takes effect after execution.
2083*/
2084int mbtk_sms_cpms_set(mbtk_info_handle_t* handle, char * mem, char* response)
2085{
2086 printf("mbtk_sms_cpms_set() mem:%s, len:%d",mem, strlen(mem));
2087 char req[128] = {0};
2088 if( info_item_process(handle, MBTK_INFO_ID_SMS_CPMS_REQ, mem, strlen(mem), req) > 0){
2089 memcpy(response, req, strlen(req));
2090 return 0;
2091 }
2092 else{
2093 return handle->info_err;
2094 }
2095}
2096
2097/*
2098* Set sms cm.
2099*
2100* +CMGR=<index>
2101
2102if PDU mode (+CMGF=0) ��command successful:
2103+CMGR: <stat>,[<alpha>],<length><CR><LF><pdu>
2104OK
2105if text mode (+CMGF=1), command successful and SMS-DELIVER:
2106+CMGR:<stat>,<oa>,[<alpha>],<scts>[,<tooa>,<fo>,<pid>,<dcs
2107>, <sca>,<tosca>,<length>]<CR><LF><data>
2108OK
2109if text mode (+CMGF=1), command successful and SMS-SUBMIT:
2110+CMGR:
2111<stat>,<da>,[<alpha>][,<toda>,<fo>,<pid>,<dcs>,[<vp>],
2112<sca>,<tosca>,<length>]<CR><LF><data>
2113OK
2114otherwise:
2115+CMS ERROR: <err>
2116*
2117* Restarting takes effect after execution.
2118*/
2119int mbtk_sms_cmgr_set(mbtk_info_handle_t* handle, int index, char *resp)
2120{
2121 printf("mbtk_sms_cmgr_set() --------------index:%d",index);
2122 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMGR_REQ, (uint8*)&index, sizeof(uint8), resp) > 0){
2123 printf("resp:%s\n", resp);
2124 return 0;
2125 }else{
2126 return handle->info_err;
2127 }
2128}
2129
2130
2131/*
2132* Get sim state.
2133*/
2134int mbtk_sim_state_get(mbtk_info_handle_t* handle, mbtk_sim_state_enum *sim_state)
2135{
2136 uint8 state;
2137 if(handle == NULL || sim_state == NULL)
2138 {
2139 LOGE("ARG error.");
2140 return -1;
2141 }
2142 if(info_item_process(handle, MBTK_INFO_ID_SIM_STATE_REQ, NULL, 0, &state) > 0) {
2143 *sim_state = (mbtk_sim_state_enum)state;
2144 LOG("Sim state : %d", *sim_state);
2145 return 0;
2146 } else {
2147 return handle->info_err;
2148 }
2149}
2150
2151/*
2152* Get sim card type.
2153*/
2154int mbtk_sim_card_type_get(mbtk_info_handle_t* handle, mbtk_sim_card_type_enum *sim_card_type)
2155{
2156 uint8 state;
2157 if(handle == NULL || sim_card_type == NULL)
2158 {
2159 LOGE("ARG error.");
2160 return -1;
2161 }
2162 if(info_item_process(handle, MBTK_INFO_ID_SIM_STYPE_REQ, NULL, 0, &state) > 0) {
2163 *sim_card_type = (mbtk_sim_card_type_enum)state;
2164 LOG("Sim sim_card_type : %d", *sim_card_type);
2165 return 0;
2166 } else {
2167 return handle->info_err;
2168 }
2169}
2170
2171/*
2172* Get system temperature.
2173*
2174* type[IN]:
2175* 0: Soc temperature.
2176* 1: RF temperature.
2177* temp[OUT]:
2178* temperature in celsius.
2179*/
r.xiao2102d762024-06-07 03:10:38 -07002180int mbtk_temp_get(mbtk_info_handle_t* handle, int type, mbtk_thermal_info_t* temp)
liubin281ac462023-07-19 14:22:54 +08002181{
2182 if(handle == NULL)
2183 {
2184 LOGE("ARG error.");
2185 return -1;
2186 }
r.xiao2102d762024-06-07 03:10:38 -07002187 if(type != 0 && type != 1)
liubin281ac462023-07-19 14:22:54 +08002188 {
2189 return -1;
2190 }
2191
2192 uint8 temp_type = (uint8)type;
r.xiao2102d762024-06-07 03:10:38 -07002193
2194 if(info_item_process(handle, MBTK_INFO_ID_DEV_TEMP_REQ, &temp_type, sizeof(uint8), temp) > 0) {
2195
2196 LOG("Temperature : %d", temp->ther);
liubin281ac462023-07-19 14:22:54 +08002197 return 0;
2198 } else {
2199 return handle->info_err;
2200 }
2201
2202 return 0;
2203}
2204
2205
2206/*
2207* Set sim power state.
2208* power:
2209* 0: Sim power off.
2210* 1: Sim power on.
2211*/
2212int mbtk_sim_power_set(int power)
2213{
2214 if(power != 0 && power != 1)
2215 {
2216 return -1;
2217 }
2218
2219 // /sys/devices/virtual/usim_event/usim0/send_event
2220 char cmd[100] = {0};
2221 sprintf(cmd, "echo %d > /sys/devices/virtual/usim_event/usim0/send_event", power ? 0 : 1);
2222 system(cmd);
2223
2224 return 0;
2225}
2226
2227/*
2228* System power.
2229* type:
2230* 0: Reboot system.
2231* 1: Poweroff system.
2232* 2: Halt system.
2233*/
2234int mbtk_system_reboot(int type)
2235{
2236 if(type != 0 && type != 1 && type != 2)
2237 {
2238 return -1;
2239 }
2240
2241 switch(type) {
2242 case 0: {
2243 system("reboot");
2244 break;
2245 }
2246 case 1: {
2247 system("poweroff");
2248 break;
2249 }
2250 case 2: {
2251 system("halt");
2252 break;
2253 }
2254 default: {
2255 break;
2256 }
2257 }
2258
2259 return 0;
2260}
2261
2262/*
2263* set modem fun
2264*
2265*/
2266int mbtk_set_modem_fun(mbtk_info_handle_t* handle, mbtk_modem_info_t *info)
2267{
2268 if(handle == NULL)
2269 {
2270 LOGE("ARG error.");
2271 return -1;
2272 }
2273 return info_item_process(handle, MBTK_INFO_ID_DEV_MODEM_REQ, info, sizeof(mbtk_modem_info_t), NULL) ? handle->info_err : 0;
2274}
2275
2276/*
2277* get modem fun
2278*
2279*/
2280int mbtk_get_modem_fun(mbtk_info_handle_t* handle, int* fun)
2281{
2282 uint8 state;
2283 if(handle == NULL || fun == NULL)
2284 {
2285 LOGE("ARG error.");
2286 return -1;
2287 }
2288 if(info_item_process(handle, MBTK_INFO_ID_DEV_MODEM_REQ, NULL, 0, &state) > 0) {
2289 LOG("modem type : %d", state);
2290 *fun = state;
2291 return 0;
2292 } else {
2293 return handle->info_err;
2294 }
2295}
2296
2297/*
2298* call_start
2299*
2300*/
2301int mbtk_call_start(mbtk_info_handle_t* handle, char* phone_number)
2302{
2303 if(handle == NULL)
2304 {
2305 LOGE("ARG error.");
2306 return -1;
2307 }
2308 if(str_empty(phone_number))
2309 return -1;
2310
2311 return info_item_process(handle, MBTK_INFO_ID_CALL_START_REQ,
2312 phone_number, strlen(phone_number), NULL) ? handle->info_err : 0;
2313}
2314/*
2315* Answer the phone call.
2316*
2317*/
2318int mbtk_call_answer(mbtk_info_handle_t* handle)
2319{
2320 return info_item_process(handle, MBTK_INFO_ID_CALL_ANSWER_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2321}
2322
2323/*
2324* Hang up all call.
2325*
2326*/
2327int mbtk_call_hang(mbtk_info_handle_t* handle)
2328{
2329 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2330}
2331
2332/*
2333* Hang up a call.
2334*
2335*/
2336int mbtk_a_call_hang(mbtk_info_handle_t* handle, int phone_id)
2337{
2338 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_A_REQ, (uint8*)&phone_id, sizeof(uint8), NULL) ? handle->info_err : 0;
2339}
2340
2341/*
2342* Hang up waiting or background call.
2343*
2344*/
2345int mbtk_waiting_or_background_call_hang(mbtk_info_handle_t* handle)
2346{
2347 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_B_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2348}
2349
2350/*
2351* Hang up foreground resume background call.
2352*
2353*/
2354int mbtk_foreground_resume_background_call_hang(mbtk_info_handle_t* handle)
2355{
2356 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_C_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2357}
2358
2359/*
2360* Get current call phone number.
2361*/
2362int mbtk_call_reg_get(mbtk_info_handle_t* handle, mbtk_call_info_t *reg)
2363{
2364 if(info_item_process(handle, MBTK_INFO_ID_CALL_WAITIN_REQ, NULL, 0, reg) > 0) {
2365 LOG("CLCC : %d, %d, %d, %d, %d, %s, %d", reg->dir1, reg->dir, reg->state, reg->mode, reg->mpty, reg->phone_number, reg->type);
2366 return 0;
2367 } else {
2368 return handle->info_err;
2369 }
2370}
2371
2372/*
2373* Return mute state.
2374*/
2375int mbtk_mute_state_get(mbtk_info_handle_t* handle, int *mute_state)
2376{
2377 uint8 state;
2378 if(info_item_process(handle, MBTK_INFO_ID_CALL_MUTE_REQ, NULL, 0, &state) > 0) {
2379 LOG("Mute State : %d", state);
2380 *mute_state = state;
2381 return 0;
2382 } else {
2383 return handle->info_err;
2384 }
2385}
2386
2387/*
2388* Set mute state.
2389*
2390* mute_state:
2391* 0 : of mute.
2392* 1 : on mute.
2393*
2394* Restarting takes effect after execution.
2395*/
2396int mbtk_mute_state_set(mbtk_info_handle_t* handle, int mute_state)
2397{
2398 return info_item_process(handle, MBTK_INFO_ID_CALL_MUTE_REQ, (uint8*)&mute_state, sizeof(uint8), NULL) ? handle->info_err : 0;
2399}
2400
2401/*
r.xiaoec113d12024-01-12 02:13:28 -08002402* Set wakeup state.
2403*
2404* wakeup_state:(0~31)
2405* 0 : means resume all
2406* 1~31 means suspend
2407* Control the active reporting of some platform modems to reduce wakeup
2408*/
2409
2410int mbtk_wakeup_state_set(mbtk_info_handle_t* handle, uint32 wakeup_state)
2411{
2412 return info_item_process(handle, MBTK_INFO_ID_WAKEUP_STA_REQ, (uint32*)&wakeup_state, sizeof(uint32), NULL) ? handle->info_err : 0;
2413}
2414
2415/*
2416* oos get.
2417*/
2418int mbtk_oos_get(mbtk_info_handle_t* handle, mbtk_oos_info *oos_info)
2419{
2420 if(info_item_process(handle, MBTK_INFO_ID_OOS_STA_REQ, NULL, 0, oos_info) > 0) {
2421 return 0;
2422 } else {
2423 return handle->info_err;
2424 }
2425}
2426
2427/*
2428* oos set .
2429*/
r.xiaocfd7c682024-01-22 03:59:46 -08002430int mbtk_oos_set(mbtk_info_handle_t* handle, mbtk_oos_info *oos_info)
r.xiaoec113d12024-01-12 02:13:28 -08002431{
2432 if(handle == NULL)
2433 {
2434 LOGE("ARG error.");
2435 return -1;
2436 }
r.xiaoec113d12024-01-12 02:13:28 -08002437
r.xiaocfd7c682024-01-22 03:59:46 -08002438 return info_item_process(handle, MBTK_INFO_ID_OOS_STA_REQ, oos_info, sizeof(mbtk_oos_info), NULL) ? handle->info_err : 0;
r.xiaoec113d12024-01-12 02:13:28 -08002439}
2440
2441
2442/*
liubin281ac462023-07-19 14:22:54 +08002443* Set DTMF character.
2444*
2445*/
2446int mbtk_dtmf_send(mbtk_info_handle_t* handle, mbtk_call_dtmf_info_t *dtmf_character)
2447{
2448 if(handle == NULL)
2449 {
2450 LOGE("ARG error.");
2451 return -1;
2452 }
2453 return info_item_process(handle, MBTK_INFO_ID_CALL_DTMF_REQ,
2454 dtmf_character, sizeof(mbtk_call_dtmf_info_t), NULL) ? handle->info_err : 0;
2455}
2456
2457/*
wangyouqiang38e53362024-01-23 10:53:48 +08002458* Set net led.
2459*
2460*/
2461int mbtk_led_set(mbtk_info_handle_t* handle, mbtk_led_type type, mbtk_led_status status)
2462{
2463 if(handle == NULL)
2464 {
2465 LOGE("ARG error.");
2466 return -1;
2467 }
2468
2469 char buff[3] = {0};
2470 if(type == MBTK_LED_TYPE_NET)
2471 {
2472 buff[0] = 0;
2473 }
2474 else
2475 {
2476 buff[0] = 1;
2477 }
2478
2479 if(status == MBTK_LED_STATUS_CLOSE)
2480 {
2481 buff[1] = 0;
2482 }
2483 else
2484 {
2485 buff[1] = 1;
2486 }
2487 return info_item_process(handle, MBTK_INFO_ID_LED_REQ, buff, 2, NULL) ? handle->info_err : 0;
2488}
2489
2490/*
liubin281ac462023-07-19 14:22:54 +08002491* Set pdp state change callback function.
2492*/
2493int mbtk_pdp_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2494{
2495 if(handle == NULL)
2496 {
2497 LOGE("ARG error.");
2498 return -1;
2499 }
2500 if(info_item_process(handle, MBTK_INFO_ID_IND_PDP_STATE_CHANGE, NULL, 0, NULL) < 0) {
2501 return handle->info_err;
2502 } else {
2503 handle->pdp_state_cb = cb;
2504 return 0;
2505 }
2506}
2507
2508/*
2509* Set network state change callback function.
2510*/
2511int mbtk_net_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2512{
2513 if(handle == NULL)
2514 {
2515 LOGE("ARG error.");
2516 return -1;
2517 }
b.liu27b91e42024-01-17 20:02:30 +08002518 if(info_item_process(handle, MBTK_INFO_ID_IND_NET_STATE_CHANGE, NULL, 0, NULL) < 0) {
2519 return handle->info_err;
2520 } else {
liubin281ac462023-07-19 14:22:54 +08002521 handle->net_state_cb = cb;
2522 return 0;
liubin281ac462023-07-19 14:22:54 +08002523 }
2524}
2525
2526/*
2527* Set call state change callback function.
2528*/
2529int mbtk_call_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2530{
2531 if(handle == NULL)
2532 {
2533 LOGE("ARG error.");
2534 return -1;
2535 }
2536 if(info_item_process(handle, MBTK_INFO_ID_IND_CALL_STATE_CHANGE, NULL, 0, NULL) < 0) {
2537 return handle->info_err;
2538 } else {
2539 handle->call_state_cb = cb;
2540 return 0;
2541 }
2542}
2543
2544/*
2545* Set sms state change callback function.
2546*/
2547int mbtk_sms_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2548{
2549 if(handle == NULL)
2550 {
2551 LOGE("ARG error.");
2552 return -1;
2553 }
2554 if(info_item_process(handle, MBTK_INFO_ID_IND_SMS_STATE_CHANGE, NULL, 0, NULL) < 0) {
2555 return handle->info_err;
2556 } else {
2557 handle->sms_state_cb = cb;
2558 return 0;
2559 }
2560}
2561
2562/*
2563* Set radio state change callback function.
2564*/
2565int mbtk_radio_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2566{
2567 if(handle == NULL)
2568 {
2569 LOGE("ARG error.");
2570 return -1;
2571 }
2572 if(info_item_process(handle, MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, NULL, 0, NULL) < 0) {
2573 return handle->info_err;
2574 } else {
2575 handle->radio_state_cb = cb;
2576 return 0;
2577 }
2578}
2579
2580/*
2581* Set sim state change callback function.
2582*/
2583int mbtk_sim_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2584{
2585 if(handle == NULL)
2586 {
2587 LOGE("ARG error.");
2588 return -1;
2589 }
2590 if(info_item_process(handle, MBTK_INFO_ID_IND_SIM_STATE_CHANGE, NULL, 0, NULL) < 0) {
2591 return handle->info_err;
2592 } else {
2593 handle->sim_state_cb = cb;
2594 return 0;
2595 }
2596}
r.xiaofca7c472024-04-24 01:00:23 -07002597
2598/*
2599* Set signal state change callback function.
2600*/
2601int mbtk_signal_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2602{
2603 if(handle == NULL)
2604 {
2605 LOGE("ARG error.");
2606 return -1;
2607 }
2608 if(info_item_process(handle, MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, NULL, 0, NULL) < 0) {
2609 return handle->info_err;
2610 } else {
2611 handle->signal_state_cb = cb;
2612 return 0;
2613 }
2614}
2615
b.liuc41882f2024-10-23 17:54:44 +08002616/*
2617* Set ril server state change callback function.
2618*/
2619int mbtk_ril_server_state_change_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2620{
2621 if(handle == NULL)
2622 {
2623 LOGE("ARG error.");
2624 return -1;
2625 }
2626
2627 ril_server_state_cb = cb;
2628 return 0;
2629}
2630
2631