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