blob: b226408832eeaaaee65c00f3fe15300faeb1815f [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
wangyouqiang80487e42024-05-24 15:06:20 +0800397#ifdef MBTK_SG_SUPPORT
398 if(handle->handle_state)
399 {
400 LOG("hangdle BUSY.");
401 return -2;
402 }
403#endif
404
liubin281ac462023-07-19 14:22:54 +0800405 mbtk_info_pack_t* pack = mbtk_info_pack_creat(id);
406 if(pack == NULL) {
407 return -1;
408 }
409 if(send_buff && send_buff_len > 0) { // Set the data to be sent.
410 // log_hex("data", send_buff, send_buff_len);
411 // mbtk_info_pack_data_set(pack, data, data_len);
412 pack->data_len = (uint16)send_buff_len;
413 pack->data = (const uint8*)send_buff;
414 }
wangyouqiang962740a2023-11-02 19:27:22 +0800415
wangyouqiang80487e42024-05-24 15:06:20 +0800416#ifndef MBTK_SG_SUPPORT
wangyouqiang962740a2023-11-02 19:27:22 +0800417 pthread_mutex_lock(&handle->send_mutex);
wangyouqiang80487e42024-05-24 15:06:20 +0800418#endif
wangyouqiang962740a2023-11-02 19:27:22 +0800419 pthread_mutex_lock(&handle->mutex);
wangyouqiang80487e42024-05-24 15:06:20 +0800420#ifdef MBTK_SG_SUPPORT
421 handle->handle_state = 1;
422#endif
wangyouqiang962740a2023-11-02 19:27:22 +0800423 handle->is_waitting = true;
b.liu27b91e42024-01-17 20:02:30 +0800424
liubin281ac462023-07-19 14:22:54 +0800425 mbtk_info_pack_send(handle->client_fd, pack);
426 mbtk_info_pack_free(&pack);
427
428 if(recv_buff != NULL)
429 handle->data = recv_buff;
liubin281ac462023-07-19 14:22:54 +0800430 // Wait for server response.
liubin281ac462023-07-19 14:22:54 +0800431 pthread_cond_wait(&handle->cond, &handle->mutex);
432 handle->is_waitting = false;
433 pthread_mutex_unlock(&handle->mutex);
liubin281ac462023-07-19 14:22:54 +0800434 if(handle->info_err == MBTK_INFO_ERR_SUCCESS)
435 {
436 LOG("REQ %s success.", id2str(id));
437 int recv_len = 0;
438 if(recv_buff && handle->data_len > 0) {
439 recv_len = handle->data_len;
440 handle->data_len = 0;
441 handle->data = NULL;
442 }
wangyouqiang80487e42024-05-24 15:06:20 +0800443#ifdef MBTK_SG_SUPPORT
444 handle->handle_state = 0;
445#else
wangyouqiang962740a2023-11-02 19:27:22 +0800446 pthread_mutex_unlock(&handle->send_mutex);
wangyouqiang80487e42024-05-24 15:06:20 +0800447#endif
448
liubin281ac462023-07-19 14:22:54 +0800449 return recv_len;
450 } else {
451 LOG("REQ %s fail : %s", id2str(id), err2str(handle->info_err));
wangyouqiang80487e42024-05-24 15:06:20 +0800452#ifdef MBTK_SG_SUPPORT
453 handle->handle_state = 0;
454#else
455 pthread_mutex_unlock(&handle->send_mutex);
456#endif
457
liubin281ac462023-07-19 14:22:54 +0800458 return -1;
459 }
460}
461
462
463
464mbtk_info_handle_t* mbtk_info_handle_get()
465{
466 mbtk_info_handle_t* handle = (mbtk_info_handle_t*)malloc(sizeof(mbtk_info_handle_t));
467 if(!handle)
468 {
469 LOG("malloc() error[%d].", errno);
470 return NULL;
471 }
472 memset(handle, 0, sizeof(mbtk_info_handle_t));
473 handle->client_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
474 if(handle->client_fd < 0)
475 {
476 LOG("socket() fail[%d].", errno);
477 goto error;
478 }
479
480 // Set O_NONBLOCK
481 int flags = fcntl(handle->client_fd, F_GETFL, 0);
482 if (flags < 0)
483 {
484 LOG("Get flags error:%d", errno);
485 goto error;
486 }
487 flags |= O_NONBLOCK;
488 if (fcntl(handle->client_fd, F_SETFL, flags) < 0)
489 {
490 LOG("Set flags error:%d", errno);
491 goto error;
492 }
493
494 struct sockaddr_un cli_addr;
495 memset(&cli_addr, 0, sizeof(cli_addr));
496 cli_addr.sun_family = AF_LOCAL;
497 strcpy(cli_addr.sun_path, SOCK_INFO_PATH);
498 if(connect(handle->client_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
499 {
500 LOG("connect() fail[%d].", errno);
501 goto error;
502 }
503
504 if(pipe(handle->exit_fd)) {
505 LOG("pipe() fail[%d].", errno);
506 goto error;
507 }
508#if 0
509 pthread_attr_t thread_attr;
510 pthread_attr_init(&thread_attr);
511 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
512 {
513 LOG("pthread_attr_setdetachstate() fail.");
514 goto error;
515 }
516
517 if(pthread_create(&(handle->read_thread_id), &thread_attr, info_read_run, handle))
518 {
519 LOG("pthread_create() fail.");
520 goto error;
521 }
522 pthread_attr_destroy(&thread_attr);
523#else
524 if(pthread_create(&(handle->read_thread_id), NULL, info_read_run, handle))
525 {
526 LOG("pthread_create() fail.");
527 goto error;
528 }
529#endif
530
531 pthread_mutex_init(&handle->mutex, NULL);
wangyouqiang80487e42024-05-24 15:06:20 +0800532#ifndef MBTK_SG_SUPPORT
wangyouqiang962740a2023-11-02 19:27:22 +0800533 pthread_mutex_init(&handle->send_mutex, NULL);
wangyouqiang80487e42024-05-24 15:06:20 +0800534#endif
liubin281ac462023-07-19 14:22:54 +0800535 pthread_cond_init(&handle->cond, NULL);
536 handle->is_waitting = false;
537
538 //mbtk wyq for server_ready_status add start
539 int timeout = 5;//The wait server timeout by default
540 LOG("wait server handshake message--->.");
541 while(timeout)
542 {
543 if(handle->server_ready_status)
544 {
545 break;
546 }
547 else
548 {
549 sleep(1);
550 timeout--;
551 }
552 }
553
554 if(timeout <= 0)
555 {
556 if(handle->exit_fd[1] > 0)
557 {
558 write(handle->exit_fd[1], "EXIT", 4);
559 }
560 pthread_join(handle->read_thread_id,NULL);
561 LOG("mbtk_info_handle_get() server not ready.");
562 goto error;
563 }
564 else
565 {
566 LOG("mbtk_info_handle_get() server ready ok.");
567 }
568 //mbtk wyq for server_ready_status add end
569 return handle;
570error:
571 if(handle)
572 {
573 if(handle->client_fd > 0)
574 {
575 close(handle->client_fd);
576 }
577 if(handle->exit_fd[0] > 0) {
578 close(handle->exit_fd[0]);
579 }
580 if(handle->exit_fd[1] > 0) {
581 close(handle->exit_fd[1]);
582 }
583 free(handle);
584 handle = NULL;
585 }
586
587 return NULL;
588}
589
590int mbtk_info_handle_free(mbtk_info_handle_t** handle)
591{
592 if(handle == NULL || *handle == NULL)
593 {
594 LOG("Handle is NULL.");
595 return -1;
596 }
597
598 if((*handle)->exit_fd[1] > 0) {
599 write((*handle)->exit_fd[1], "EXIT", 4);
600 }
601
602 // Wait read_thread exit.
603 pthread_join((*handle)->read_thread_id,NULL);
604
605 if((*handle)->exit_fd[0] > 0) {
606 close((*handle)->exit_fd[0]);
607 (*handle)->exit_fd[0] = -1;
608 }
609
610 if((*handle)->exit_fd[1] > 0) {
611 close((*handle)->exit_fd[1]);
612 (*handle)->exit_fd[1] = -1;
613 }
614
615 if((*handle)->client_fd > 0)
616 {
617 close((*handle)->client_fd);
618 (*handle)->client_fd = -1;
619 }
620 free(*handle);
621 *handle = NULL;
622 return 0;
623}
624
625/*
626* Get platform version.
627*/
628int mbtk_version_get(mbtk_info_handle_t* handle, void *version)
629{
630 if(handle == NULL || version == NULL)
631 {
632 LOGE("ARG error.");
633 return -1;
634 }
635
636 if(info_item_process(handle, MBTK_INFO_ID_DEV_VERSION_REQ, NULL, 0, version) > 0) {
637 LOG("Version : %s", version);
638 return 0;
639 } else {
640 return handle->info_err;
641 }
642}
643
644/*
645* Get platform version.
646*/
647int mbtk_model_get(mbtk_info_handle_t* handle, void *model)
648{
649 if(handle == NULL || model == NULL)
650 {
651 LOGE("ARG error.");
652 return -1;
653 }
654
655 if(info_item_process(handle, MBTK_INFO_ID_DEV_MODEL_REQ, NULL, 0, model) > 0) {
656 LOG("Version : %s", model);
657 return 0;
658 } else {
659 return handle->info_err;
660 }
661}
662
663/*
664* Get platform IMEI.
665*/
666int mbtk_imei_get(mbtk_info_handle_t* handle, void *imei)
667{
668 if(handle == NULL || imei == NULL)
669 {
670 LOGE("ARG error.");
671 return -1;
672 }
673 if(info_item_process(handle, MBTK_INFO_ID_DEV_IMEI_REQ, NULL, 0, imei) > 0) {
674 LOG("IMEI : %s", imei);
675 return 0;
676 } else {
677 return handle->info_err;
678 }
679}
680
681/*
682* Get platform SN.
683*/
684int mbtk_sn_get(mbtk_info_handle_t* handle, void *sn)
685{
686 if(handle == NULL || sn == NULL)
687 {
688 LOGE("ARG error.");
689 return -1;
690 }
691 if(info_item_process(handle, MBTK_INFO_ID_DEV_SN_REQ, NULL, 0, sn) > 0) {
692 LOG("SN : %s", sn);
693 return 0;
694 } else {
695 return handle->info_err;
696 }
697}
698
699/*
700* Get platform MEID.
701*/
702int mbtk_meid_get(mbtk_info_handle_t* handle, void *meid)
703{
704 if(handle == NULL || meid == NULL)
705 {
706 LOGE("ARG error.");
707 return -1;
708 }
709 if(info_item_process(handle, MBTK_INFO_ID_DEV_MEID_REQ, NULL, 0, meid) > 0) {
710 LOG("MEID : %s", meid);
711 return 0;
712 } else {
713 return handle->info_err;
714 }
715}
716
717/*
718* Return VoLTE state.
719*/
720int mbtk_volte_state_get(mbtk_info_handle_t* handle, int *volte_state)
721{
722 uint8 state;
723 if(handle == NULL || volte_state == NULL)
724 {
725 LOGE("ARG error.");
726 return -1;
727 }
728 if(info_item_process(handle, MBTK_INFO_ID_DEV_VOLTE_REQ, NULL, 0, &state) > 0) {
729 LOG("VoLTE State : %d", state);
730 *volte_state = state;
731 return 0;
732 } else {
733 return handle->info_err;
734 }
735}
736
737/*
738* Set VoLTE state.
739*
740* volte_state:
741* 0 : Close VoLTE.
742* 1 : Open VoLTE.
743*
744* Restarting takes effect after execution.
745*/
746int mbtk_volte_state_set(mbtk_info_handle_t* handle, int volte_state)
747{
748 if(handle == NULL)
749 {
750 LOGE("ARG error.");
751 return -1;
752 }
753 return info_item_process(handle, MBTK_INFO_ID_DEV_VOLTE_REQ, (uint8*)&volte_state, sizeof(uint8), NULL) ? handle->info_err : 0;
754}
755
756/*
757* Get platform IMSI.
758*/
759int mbtk_imsi_get(mbtk_info_handle_t* handle, void *imsi)
760{
761 if(handle == NULL || imsi == NULL)
762 {
763 LOGE("ARG error.");
764 return -1;
765 }
766 if(info_item_process(handle, MBTK_INFO_ID_SIM_IMSI_REQ, NULL, 0, imsi) > 0) {
767 LOG("IMSI : %s", imsi);
768 return 0;
769 } else {
770 return handle->info_err;
771 }
772}
773
774/*
775* Get platform ICCID.
776*/
777int mbtk_iccid_get(mbtk_info_handle_t* handle, void *iccid)
778{
779 if(handle == NULL || iccid == NULL)
780 {
781 LOGE("ARG error.");
782 return -1;
783 }
784 if(info_item_process(handle, MBTK_INFO_ID_SIM_ICCID_REQ, NULL, 0, iccid) > 0) {
785 LOG("ICCID : %s", iccid);
786 return 0;
787 } else {
788 return handle->info_err;
789 }
790}
791
792/*
793* Get current phone number.
794*/
795int mbtk_phone_number_get(mbtk_info_handle_t* handle, void *phone_number)
796{
797 if(handle == NULL || phone_number == NULL)
798 {
799 LOGE("ARG error.");
800 return -1;
801 }
802 if(info_item_process(handle, MBTK_INFO_ID_SIM_PN_REQ, NULL, 0, phone_number) > 0) {
803 LOG("Phone Number : %s", phone_number);
804 return 0;
805 } else {
806 return handle->info_err;
807 }
808}
809
810/*
811* Get PIN’s number of remaining retry
812*/
813int mbtk_pin_last_num_get(mbtk_info_handle_t* handle, mbtk_pin_puk_last_times *last_times)
814{
815 if(handle == NULL || last_times == NULL)
816 {
817 LOGE("ARG error.");
818 return -1;
819 }
820 if(info_item_process(handle, MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ, NULL, 0, last_times) > 0) {
821 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);
822 return 0;
823 } else {
824 return handle->info_err;
825 }
826}
827
828/*
829* emable PIN
830*/
831int mbtk_enable_pin(mbtk_info_handle_t* handle, mbtk_enable_pin_info *pin)
832{
833 if(handle == NULL)
834 {
835 LOGE("ARG error.");
836 return -1;
837 }
838 if(info_item_process(handle, MBTK_INFO_ID_SIM_ENABLE_PIN_REQ, pin, sizeof(mbtk_enable_pin_info), NULL) >= 0) {
839 LOG("pin Number : %s", pin->pin_value);
840 return 0;
841 } else {
842 return handle->info_err;
843 }
844
845}
846
847/*
848* Verify PIN
849*/
850int mbtk_verify_pin(mbtk_info_handle_t* handle, char *pin)
851{
852 if(handle == NULL)
853 {
854 LOGE("ARG error.");
855 return -1;
856 }
857 if(info_item_process(handle, MBTK_INFO_ID_SIM_PIN_REQ, pin, strlen(pin), NULL) >= 0) {
858 LOG("pin Number : %s", pin);
859 return 0;
860 } else {
861 return handle->info_err;
862 }
863
864}
865
866/*
867* Verify PIN
868*/
869int mbtk_change_pin(mbtk_info_handle_t* handle, mbtk_change_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_CHANGE_PIN_REQ, pin, sizeof(mbtk_change_pin_info), NULL) >= 0) {
877 LOG("Change PIN : %s -> %s", pin->old_pin_value, pin->new_pin_value);
878 return 0;
879 } else {
880 return handle->info_err;
881 }
882}
883
884/*
885* unblock_pin
886*/
887int mbtk_unlock_pin(mbtk_info_handle_t* handle, mbtk_unlock_pin_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_PUK_REQ, pin, sizeof(mbtk_unlock_pin_info), NULL) >= 0) {
895 LOG("Unlock : %s , %s", pin->pin_value , pin->puk_value);
896 return 0;
897 } else {
898 return handle->info_err;
899 }
900}
901
902/*
903* Get plmn list
904*/
905int mbtk_get_plmn_list(mbtk_info_handle_t* handle, mbtk_plmn_info *pin)
906{
907 if(handle == NULL)
908 {
909 LOGE("ARG error.");
910 return -1;
911 }
912 if(info_item_process(handle, MBTK_INFO_ID_SIM_PLMN_REQ, NULL, 0, pin) >= 0) {
913 //LOG("pin Number : %s", pin);
914 return 0;
915 } else {
916 return handle->info_err;
917 }
918}
919
920
921/*
922* Get available network.
923*/
924int mbtk_available_net_get(mbtk_info_handle_t* handle, list_node_t **net_list)
925{
926 if(handle == NULL)
927 {
928 LOGE("ARG error.");
929 return -1;
930 }
931 *net_list = list_create(NULL);
932 if(*net_list == NULL)
933 {
934 LOG("list_create() fail.");
935 return MBTK_INFO_ERR_MEMORY;
936 }
937
938 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
939 int buff_len;
940 if((buff_len = info_item_process(handle, MBTK_INFO_ID_NET_AVAILABLE_REQ, NULL, 0, buff)) > 0) {
941 int i = 0;
942 while (i < buff_len / sizeof(mbtk_net_info_t))
943 {
944 mbtk_net_info_t* net = (mbtk_net_info_t*)malloc(sizeof(mbtk_net_info_t));
945 if(net == NULL)
946 {
947 LOG("malloc() fail.");
948 list_free(*net_list);
949 return MBTK_INFO_ERR_MEMORY;
950 }
951 memcpy(net, buff + i * sizeof(mbtk_net_info_t), sizeof(mbtk_net_info_t));
952 list_add(*net_list, net);
953
954 LOG("NET-%d: %d, %d, %d, %d", i + 1, net->net_sel_mode, net->net_type, net->net_state, net->plmn);
955 i++;
956 }
957
958 return 0;
959 } else {
960 list_free(*net_list);
961 return handle->info_err;
962 }
963}
964
965/*
966* Set network select mode. (+COPS=...)
967*/
968int mbtk_net_sel_mode_set(mbtk_info_handle_t* handle, const mbtk_net_info_t *net)
969{
970 if(handle == NULL || net == NULL)
971 {
972 LOGE("ARG error.");
973 return -1;
974 }
975 return info_item_process(handle, MBTK_INFO_ID_NET_SEL_MODE_REQ, net, sizeof(mbtk_net_info_t), NULL) ? handle->info_err : 0;
976}
977
978/*
979* Get network select mode. (+COPS?)
980*/
981int mbtk_net_sel_mode_get(mbtk_info_handle_t* handle, mbtk_net_info_t *net)
982{
983 if(handle == NULL || net == NULL)
984 {
985 LOGE("ARG error.");
986 return -1;
987 }
988 if(info_item_process(handle, MBTK_INFO_ID_NET_SEL_MODE_REQ, NULL, 0, net) > 0) {
989 LOG("NET : %d, %d, %d, %d", net->net_sel_mode, net->net_type, net->net_state, net->plmn);
990 return 0;
991 } else {
992 return handle->info_err;
993 }
994}
995
996/*
997* Get platform support bands.
998*/
999int mbtk_support_band_get(mbtk_info_handle_t* handle, mbtk_band_info_t *band)
1000{
1001 uint8 type = 0; // Get support bands.
1002 if(handle == NULL || band == NULL)
1003 {
1004 LOGE("ARG error.");
1005 return -1;
1006 }
1007 if(info_item_process(handle, MBTK_INFO_ID_NET_BAND_REQ, &type, sizeof(uint8), band) > 0) {
1008 LOG("BAND : %d, %d, %d, %d, %d", band->net_pref, band->gsm_band, band->umts_band, band->tdlte_band, band->fddlte_band);
1009 return 0;
1010 } else {
1011 return handle->info_err;
1012 }
1013}
1014
1015/*
1016* Get platform current bands.
1017*/
1018int mbtk_current_band_get(mbtk_info_handle_t* handle, mbtk_band_info_t *band)
1019{
1020 uint8 type = 1; // Get current bands.
1021 if(handle == NULL || band == NULL)
1022 {
1023 LOGE("ARG error.");
1024 return -1;
1025 }
1026 if(info_item_process(handle, MBTK_INFO_ID_NET_BAND_REQ, &type, sizeof(uint8), band) > 0) {
1027 LOG("BAND : %d, %d, %d, %d, %d", band->net_pref, band->gsm_band, band->umts_band, band->tdlte_band, band->fddlte_band);
1028 return 0;
1029 } else {
1030 return handle->info_err;
1031 }
1032}
1033
1034/*
1035* Set platform current bands.
1036*/
1037int mbtk_current_band_set(mbtk_info_handle_t* handle, const mbtk_band_info_t *band)
1038{
1039 if(handle == NULL || band == NULL)
1040 {
1041 LOGE("ARG error.");
1042 return -1;
1043 }
1044 return info_item_process(handle, MBTK_INFO_ID_NET_BAND_REQ, band, sizeof(mbtk_band_info_t), NULL) ? handle->info_err : 0;
1045}
1046
1047/*
1048* Get current cell infomation.
1049*/
1050int mbtk_cell_get(mbtk_info_handle_t* handle, mbtk_cell_type_enum *type, list_node_t **cell_list)
1051{
1052 if(handle == NULL)
1053 {
1054 LOGE("ARG error.");
1055 return -1;
1056 }
1057 *cell_list = list_create(NULL);
1058 if(*cell_list == NULL)
1059 {
1060 LOG("list_create() fail.");
1061 return MBTK_INFO_ERR_MEMORY;
1062 }
1063
1064 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
1065 int buff_len;
1066 if((buff_len = info_item_process(handle, MBTK_INFO_ID_NET_CELL_REQ, NULL, 0, buff)) > 0) {
1067 int i = 0;
1068 *type = buff[0]; // Set network type.
1069 while (i < (buff_len - sizeof(uint8)) / sizeof(mbtk_cell_info_t))
1070 {
1071 mbtk_cell_info_t* cell = (mbtk_cell_info_t*)malloc(sizeof(mbtk_cell_info_t));
1072 if(cell == NULL)
1073 {
1074 LOG("malloc() fail.");
1075 list_free(*cell_list);
1076 return MBTK_INFO_ERR_MEMORY;
1077 }
1078 memcpy(cell, buff + sizeof(uint8) + i * sizeof(mbtk_cell_info_t), sizeof(mbtk_cell_info_t));
1079 list_add(*cell_list, cell);
1080
1081 // LOG("Cell-%d: %d, %d, %d, %d, %d", i + 1, cell->value1, cell->value2, cell->value3, cell->value4, cell->value5);
1082 i++;
1083 }
1084
1085 return 0;
1086 } else {
1087 list_free(*cell_list);
1088 return handle->info_err;
1089 }
1090}
1091/*
1092* Set cell info.
1093*
1094* at*CELL=<mode>,<act>,< band>,<freq>,<cellId>
1095* at*cell=2,3,,40936,429 //
1096* at*cell=0 //
1097*
1098* Restarting takes effect after execution.
1099*/
1100int mbtk_cell_set(mbtk_info_handle_t* handle, char * info, char* response)
1101{
1102 printf("mbtk_cell_set() info:%s, len:%d",info, strlen(info));
1103 char req[128] = {0};
1104 if( info_item_process(handle, MBTK_INFO_ID_NET_CELL_REQ, info, strlen(info), req) > 0){
1105 memcpy(response, req, strlen(req));
1106 return 0;
1107 }
1108 else{
1109 return 0;
1110 // return handle->info_err;
1111 }
1112}
1113
1114/*
1115* Get all APN informations.
1116*/
1117int mbtk_apn_get(mbtk_info_handle_t* handle, int *apn_num, mbtk_apn_info_t apns[])
1118{
1119 int len;
1120 if(handle == NULL || apn_num == NULL || apns == NULL)
1121 {
1122 LOGE("ARG error.");
1123 return -1;
1124 }
1125 uint8 data[SOCK_MSG_LEN_MAX];
1126 if((len = info_item_process(handle, MBTK_INFO_ID_NET_APN_REQ, NULL, 0, data)) > 0) {
1127 /*
1128 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
1129 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
1130 */
1131 uint8* ptr = data;
1132 if(apn_num == NULL || apns == NULL || *apn_num < *ptr) {
1133 *apn_num = 0;
1134 LOGE("APN array size to not enough.");
1135 return -1;
1136 }
1137 *apn_num = *ptr++;
1138 LOG("APN Number : %d", *apn_num);
1139 int i = 0;
1140 while(i < *apn_num) {
1141 memset(&(apns[i]), 0 ,sizeof(mbtk_apn_info_t));
1142 apns[i].cid = *ptr++;
1143 apns[i].ip_type = (mbtk_ip_type_enum)(*ptr++);
1144
1145 // apn
1146 len = byte_2_uint16(ptr, false);
1147 ptr += sizeof(uint16);
1148 if(len > 0) { // Has APN
1149 memcpy(apns[i].apn, ptr, len);
1150 ptr += len;
1151 }
1152 // user
1153 len = byte_2_uint16(ptr, false);
1154 ptr += sizeof(uint16);
1155 if(len > 0) { // Has APN
1156 memcpy(apns[i].user, ptr, len);
1157 ptr += len;
1158 }
1159
1160 // pass
1161 len = byte_2_uint16(ptr, false);
1162 ptr += sizeof(uint16);
1163 if(len > 0) { // Has APN
1164 memcpy(apns[i].pass, ptr, len);
1165 ptr += len;
1166 }
1167 // auth
1168 len = byte_2_uint16(ptr, false);
1169 ptr += sizeof(uint16);
1170 if(len > 0) { // Has APN
1171 memcpy(apns[i].auth, ptr, len);
1172 ptr += len;
1173 }
1174
1175 i++;
1176 }
1177 return 0;
1178 } else {
1179 return handle->info_err;
1180 }
1181}
1182
1183/*
wangyouqiang80487e42024-05-24 15:06:20 +08001184* Get all APN informations.
1185*/
1186int mbtk_qser_apn_get(mbtk_info_handle_t* handle, int *apn_num, mbtk_qser_apn_info_s apns[])
1187{
1188 int len;
1189 if(handle == NULL || apn_num == NULL || apns == NULL)
1190 {
1191 LOGE("ARG error.");
1192 return -1;
1193 }
1194 uint8 data[SOCK_MSG_LEN_MAX];
1195 if((len = info_item_process(handle, MBTK_INFO_ID_NET_QSER_APN_REQ, NULL, 0, data)) > 0) {
1196 /*
1197 <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>...
1198 <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>
1199 */
1200 uint8* ptr = data;
1201 if(apn_num == NULL || apns == NULL || *apn_num < *ptr) {
1202 *apn_num = 0;
1203 LOGE("APN array size to not enough.");
1204 return -1;
1205 }
1206 *apn_num = *ptr++;
1207 LOGD("APN Number : %d", *apn_num);
1208 int i = 0;
1209 while(i < *apn_num) {
1210 memset(&(apns[i]), 0x0 ,sizeof(mbtk_qser_apn_info_s));
1211 apns[i].cid = *ptr++;
1212 apns[i].ip_type = (mbtk_ip_type_enum)(*ptr++);
1213
1214 // apn
1215 len = byte_2_uint16(ptr, false);
1216 ptr += sizeof(uint16);
1217 if(len > 0) { // Has APN
1218 memcpy(apns[i].apn_name, ptr, len);
1219 ptr += len;
1220 }
1221 // user
1222 len = byte_2_uint16(ptr, false);
1223 ptr += sizeof(uint16);
1224 if(len > 0) { // Has APN
1225 memcpy(apns[i].user_name, ptr, len);
1226 ptr += len;
1227 }
1228
1229 // pass
1230 len = byte_2_uint16(ptr, false);
1231 ptr += sizeof(uint16);
1232 if(len > 0) { // Has APN
1233 memcpy(apns[i].user_pass, ptr, len);
1234 ptr += len;
1235 }
1236 // auth
1237 apns[i].auth_proto = (mbtk_apn_auth_proto_enum)(*ptr++);
1238
1239 //apn_type
1240 len = byte_2_uint16(ptr, false);
1241 ptr += sizeof(uint16);
1242 if(len > 0) { // Has APN
1243 memcpy(apns[i].apn_type, ptr, len);
1244 ptr += len;
1245 }
1246
1247 i++;
1248 }
1249 }
1250 else if(len == 0)
1251 {
1252 LOGD("get data len : 0.");
1253 *apn_num = 0;
1254 return 0;
1255 }
1256 else
1257 {
1258 return handle->info_err;
1259 }
1260
1261 return 0;
1262}
1263
1264/*
1265* qser Set current APN informations.
1266*/
1267int mbtk_qser_apn_set(mbtk_info_handle_t* handle, mbtk_qser_apn_info_s *apninfo, unsigned char *cid)
1268{
1269 if(handle == NULL)
1270 {
1271 LOGE("ARG error.");
1272 return -1;
1273 }
1274
1275 uint8 data[SOCK_MSG_LEN_MAX];
1276 memset(data, 0, SOCK_MSG_LEN_MAX);
1277 // cid : 2 - 7
1278 if(apninfo->req_type != MBTK_APN_REQ_TYPE_ADD && (apninfo->cid < MBTK_APN_CID_MIN || apninfo->cid > MBTK_APN_CID_MAX)) {
1279 LOGE("CID error.");
1280 return -1;
1281 }
1282
1283 uint8* ptr = data;
1284 // <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]>
1285 *ptr++ = (uint8)apninfo->cid;
1286 *ptr++ = (uint8)apninfo->ip_type;
1287 *ptr++ = (uint8)apninfo->req_type;
1288 *ptr++ = (uint8)apninfo->auth_proto;
1289 if(str_empty(apninfo->apn_name)) {
1290 uint16_2_byte((uint16)0, ptr, false);
1291 ptr += sizeof(uint16);
1292 } else {
1293 uint16_2_byte((uint16)strlen((char *)apninfo->apn_name), ptr, false);
1294 ptr += sizeof(uint16);
1295 memcpy(ptr, apninfo->apn_name, strlen((char *)apninfo->apn_name));
1296 ptr += strlen((char *)apninfo->apn_name);
1297 }
1298 if(str_empty(apninfo->user_name)) {
1299 uint16_2_byte((uint16)0, ptr, false);
1300 ptr += sizeof(uint16);
1301 } else {
1302 uint16_2_byte((uint16)strlen((char *)apninfo->user_name), ptr, false);
1303 ptr += sizeof(uint16);
1304 memcpy(ptr, apninfo->user_name, strlen((char *)apninfo->user_name));
1305 ptr += strlen((char *)apninfo->user_name);
1306 }
1307
1308 if(str_empty(apninfo->user_pass)) {
1309 uint16_2_byte((uint16)0, ptr, false);
1310 ptr += sizeof(uint16);
1311 } else {
1312 uint16_2_byte((uint16)strlen((char *)apninfo->user_pass), ptr, false);
1313 ptr += sizeof(uint16);
1314 memcpy(ptr, apninfo->user_pass, strlen((char *)apninfo->user_pass));
1315 ptr += strlen((char *)apninfo->user_pass);
1316 }
1317
1318 if(str_empty(apninfo->apn_type)) {
1319 uint16_2_byte((uint16)0, ptr, false);
1320 ptr += sizeof(uint16);
1321 }
1322 else
1323 {
1324 uint16_2_byte((uint16)strlen((char *)apninfo->apn_type), ptr, false);
1325 ptr += sizeof(uint16);
1326 memcpy(ptr, apninfo->apn_type, strlen((char *)apninfo->apn_type));
1327 ptr += strlen((char *)apninfo->apn_type);
1328 }
1329
1330 if(info_item_process(handle, MBTK_INFO_ID_NET_QSER_APN_REQ, data, ptr - data, (void *)cid) < 0)
1331 {
1332 return handle->info_err;
1333 }
1334
1335 return 0;
1336}
1337
1338/*
liubin281ac462023-07-19 14:22:54 +08001339* Set current APN informations.
1340*/
1341int mbtk_apn_set(mbtk_info_handle_t* handle, int cid, mbtk_ip_type_enum ip_type, const void* apn_name,
1342 const void *user_name, const void *user_pass, const void *auth)
1343{
1344 if(handle == NULL)
1345 {
1346 LOGE("ARG error.");
1347 return -1;
1348 }
1349 uint8 data[SOCK_MSG_LEN_MAX];
1350 memset(data, 0, SOCK_MSG_LEN_MAX);
1351 // cid : 2 - 7
1352 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1353 LOGE("CID error.");
1354 return -1;
1355 }
1356 uint8* ptr = data;
1357 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
1358 *ptr++ = (uint8)cid;
1359 *ptr++ = (uint8)ip_type;
1360 if(str_empty(apn_name)) {
1361 uint16_2_byte((uint16)0, ptr, false);
1362 ptr += sizeof(uint16);
1363 } else {
1364 uint16_2_byte((uint16)strlen(apn_name), ptr, false);
1365 ptr += sizeof(uint16);
1366 memcpy(ptr, apn_name, strlen(apn_name));
1367 ptr += strlen(apn_name);
1368 }
1369 if(str_empty(user_name)) {
1370 uint16_2_byte((uint16)0, ptr, false);
1371 ptr += sizeof(uint16);
1372 } else {
1373 uint16_2_byte((uint16)strlen(user_name), ptr, false);
1374 ptr += sizeof(uint16);
1375 memcpy(ptr, user_name, strlen(user_name));
1376 ptr += strlen(user_name);
1377 }
1378
1379 if(str_empty(user_pass)) {
1380 uint16_2_byte((uint16)0, ptr, false);
1381 ptr += sizeof(uint16);
1382 } else {
1383 uint16_2_byte((uint16)strlen(user_pass), ptr, false);
1384 ptr += sizeof(uint16);
1385 memcpy(ptr, user_pass, strlen(user_pass));
1386 ptr += strlen(user_pass);
1387 }
1388
1389 if(str_empty(auth)) {
1390 uint16_2_byte((uint16)0, ptr, false);
1391 ptr += sizeof(uint16);
1392 } else {
1393 uint16_2_byte((uint16)strlen(auth), ptr, false);
1394 ptr += sizeof(uint16);
1395 memcpy(ptr, auth, strlen(auth));
1396 ptr += strlen(auth);
1397 }
1398
1399 return info_item_process(handle, MBTK_INFO_ID_NET_APN_REQ, data, ptr - data, NULL) ? handle->info_err : 0;
1400}
1401
liuyang0e49d9a2024-04-23 21:04:54 +08001402int mbtk_apn_del(mbtk_info_handle_t* handle, unsigned char profile_idx)
1403{
liuyang0e49d9a2024-04-23 21:04:54 +08001404 if(handle == NULL)
1405 {
1406 LOGE("ARG error.");
1407 return -1;
1408 }
liuyang1cefd852024-04-24 18:30:53 +08001409
1410 profile_idx++;
1411 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 +08001412 {
liuyang1cefd852024-04-24 18:30:53 +08001413 LOG("profile_idx Number : %d", profile_idx);
liuyang0e49d9a2024-04-23 21:04:54 +08001414 return 0;
1415 }
1416 else
1417 {
1418 return handle->info_err;
1419 }
1420
1421}
1422
liubin281ac462023-07-19 14:22:54 +08001423/*
1424* Start data call.
1425*/
1426int mbtk_data_call_start(mbtk_info_handle_t* handle, int cid, int auto_conn_interval, bool boot_conn, int timeout)
1427{
1428 uint8 data[10];
1429 if(handle == NULL)
1430 {
1431 LOGE("ARG error.");
1432 return -1;
1433 }
1434 memset(data, 0, 10);
1435 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1436 LOGE("CID error.");
1437 return -1;
1438 }
1439 uint8* ptr = data;
1440 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
1441 call_type : mbtk_data_call_type_enum
1442 cid : 2 - 7
1443 timeout : second
1444 */
1445 *ptr++ = (uint8)MBTK_DATA_CALL_START;
1446 *ptr++ = (uint8)cid;
1447 *ptr++ = (uint8)(auto_conn_interval > 0 ? auto_conn_interval : 0); // 拨号失败后重拨间隔(s)
1448 *ptr++ = (uint8)(boot_conn ? 1 : 0); // 开机自动拨号
1449 if(timeout <= 0) {
1450 *ptr++ = (uint8)MBTK_DATA_CALL_TIMEOUT_DEFAULT;
1451 } else {
1452 *ptr++ = (uint8)timeout;
1453 }
1454
1455 return info_item_process(handle, MBTK_INFO_ID_NET_DATA_CALL_REQ, data, 5, NULL) ? handle->info_err : 0;
1456}
1457
1458/*
1459* Stop data call.
1460*/
1461int mbtk_data_call_stop(mbtk_info_handle_t* handle, int cid, int timeout)
1462{
1463 uint8 data[10];
1464 if(handle == NULL)
1465 {
1466 LOGE("ARG error.");
1467 return -1;
1468 }
1469 memset(data, 0, 10);
1470 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1471 LOGE("CID error.");
1472 return -1;
1473 }
1474 uint8* ptr = data;
1475 /* <call_type[1]><cid[1]><timeout[1]>
1476 call_type : mbtk_data_call_type_enum
1477 cid : 2 - 7
1478 timeout : second
1479 */
1480 *ptr++ = (uint8)MBTK_DATA_CALL_STOP;
1481 *ptr++ = (uint8)cid;
1482 *ptr++ = (uint8)timeout;
1483
1484 return info_item_process(handle, MBTK_INFO_ID_NET_DATA_CALL_REQ, data, 3, NULL) ? handle->info_err : 0;
1485}
1486
1487/*
1488* Get data call state.
1489*/
1490int mbtk_data_call_state_get(mbtk_info_handle_t* handle, int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6)
1491{
1492 uint8 data[10];
1493 if(handle == NULL)
1494 {
1495 LOGE("ARG error.");
1496 return -1;
1497 }
1498 uint8 recv_buff[SOCK_MSG_LEN_MAX]={0};
1499 memset(data, 0, 10);
1500 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
1501 LOGE("CID error.");
1502 return -1;
1503 }
1504 uint8* ptr = data;
1505 /* <call_type[1]><cid[1]><timeout[1]>
1506 call_type : mbtk_data_call_type_enum
1507 cid : 2 - 7
1508 timeout : second
1509 */
1510 *ptr++ = (uint8)MBTK_DATA_CALL_STATE;
1511 *ptr++ = (uint8)cid;
1512 *ptr++ = (uint8)0;
1513
1514 if(ipv4) {
1515 memset(ipv4, 0, sizeof(mbtk_ipv4_info_t));
1516 }
1517
1518 if(ipv6) {
1519 memset(ipv6, 0, sizeof(mbtk_ipv6_info_t));
1520 }
1521
1522 if(info_item_process(handle, MBTK_INFO_ID_NET_DATA_CALL_REQ, data, 3, recv_buff) > 0) {
1523 if(recv_buff[0] == 0) { // IPv4 Only.
1524 if(ipv4) {
1525 memcpy(ipv4, recv_buff + sizeof(uint8), sizeof(mbtk_ipv4_info_t));
1526 }
1527 } else if(recv_buff[0] == 1) { // IPv6 Only.
1528 if(ipv6) {
1529 memcpy(ipv6, recv_buff + sizeof(uint8), sizeof(mbtk_ipv6_info_t));
1530 }
1531 } else if(recv_buff[0] == 2) { // IPv4 and IPv6.
1532 if(ipv4) {
1533 memcpy(ipv4, recv_buff + sizeof(uint8), sizeof(mbtk_ipv4_info_t));
1534 }
1535
1536 if(ipv6) {
1537 memcpy(ipv6, recv_buff + sizeof(uint8) + sizeof(mbtk_ipv4_info_t), sizeof(mbtk_ipv6_info_t));
1538 }
1539 } else {
1540 LOGE("Unknown IP type : %d", recv_buff[0]);
1541 return -1;
1542 }
1543 return 0;
1544 } else {
1545 return handle->info_err;
1546 }
1547}
1548
1549
1550
1551/*
1552* Get current network signal.
1553*/
1554int mbtk_net_signal_get(mbtk_info_handle_t* handle, mbtk_signal_info_t *signal)
1555{
1556 if(handle == NULL || signal == NULL)
1557 {
1558 LOGE("ARG error.");
1559 return -1;
1560 }
1561 if(info_item_process(handle, MBTK_INFO_ID_NET_SIGNAL_REQ, NULL, 0, signal) > 0) {
1562 LOG("Signal : %d, %d, %d, %d, %d, %d, %d", signal->rssi, signal->rxlev, signal->ber, signal->rscp, signal->ecno,
1563 signal->rsrq, signal->rsrp);
1564 return 0;
1565 } else {
1566 return handle->info_err;
1567 }
1568}
1569
1570/*
1571* Get current network register information.
1572*/
1573int mbtk_net_reg_get(mbtk_info_handle_t* handle, mbtk_net_reg_info_t *reg)
1574{
1575 if(handle == NULL || reg == NULL)
1576 {
1577 LOGE("ARG error.");
1578 return -1;
1579 }
1580 if(info_item_process(handle, MBTK_INFO_ID_NET_REG_REQ, NULL, 0, reg) > 0) {
1581 if(reg->call_state || reg->data_state || reg->ims_state) {
1582 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);
1583 } else {
1584 LOGE("Net not reg.");
1585 }
1586 return 0;
1587 } else {
1588 return handle->info_err;
1589 }
1590}
1591
r.xiao06db9a12024-04-14 18:51:15 -07001592/*
b.liufdf03172024-06-07 15:01:29 +08001593* Get current IMS enable or not.
r.xiao06db9a12024-04-14 18:51:15 -07001594*/
b.liufdf03172024-06-07 15:01:29 +08001595int mbtk_net_ims_get(mbtk_info_handle_t* handle, int* enable)
r.xiao06db9a12024-04-14 18:51:15 -07001596{
1597 uint8 state;
1598 if(handle == NULL)
1599 {
1600 LOGE("ARG error.");
1601 return -1;
1602 }
1603 if(info_item_process(handle, MBTK_INFO_ID_NET_IMS_REQ, NULL, 0, &state) > 0) {
b.liufdf03172024-06-07 15:01:29 +08001604 LOG("IMS enable : %d", state);
1605 *enable = state;
r.xiao06db9a12024-04-14 18:51:15 -07001606 return 0;
1607 } else {
1608 return handle->info_err;
1609 }
1610
1611}
1612
1613/*
b.liufdf03172024-06-07 15:01:29 +08001614* Set IMS enable or not. This function takes effect after starting the device.
r.xiao06db9a12024-04-14 18:51:15 -07001615*/
b.liufdf03172024-06-07 15:01:29 +08001616int mbtk_net_ims_set(mbtk_info_handle_t* handle, int enable)
r.xiao06db9a12024-04-14 18:51:15 -07001617{
1618 if(handle == NULL)
1619 {
1620 LOGE("ARG error.");
1621 return -1;
1622 }
1623
b.liufdf03172024-06-07 15:01:29 +08001624 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 -07001625
1626}
1627
b.liufdf03172024-06-07 15:01:29 +08001628/*
1629* Get current network IMS register state.
1630*/
1631int mbtk_net_ims_reg_state_get(mbtk_info_handle_t* handle, int* reg)
1632{
1633 uint8 state;
1634 if(handle == NULL)
1635 {
1636 LOGE("ARG error.");
1637 return -1;
1638 }
1639 if(info_item_process(handle, MBTK_INFO_ID_NET_IMS_REG_STATE_REQ, NULL, 0, &state) > 0) {
1640 LOG("reg type : %d", state);
1641 *reg = state;
1642 return 0;
1643 } else {
1644 return handle->info_err;
1645 }
1646}
1647
liubin281ac462023-07-19 14:22:54 +08001648
1649/*
1650* Get radio state.
1651*/
1652int mbtk_radio_state_get(mbtk_info_handle_t* handle, int *radio_state)
1653{
1654 uint8 state;
1655 if(handle == NULL || radio_state == NULL)
1656 {
1657 LOGE("ARG error.");
1658 return -1;
1659 }
1660 if(info_item_process(handle, MBTK_INFO_ID_NET_RADIO_REQ, NULL, 0, &state) > 0) {
1661 LOG("Radio state : %d", state);
1662 *radio_state = state;
1663 return 0;
1664 } else {
1665 return handle->info_err;
1666 }
1667}
1668
1669/*
1670* Set radio state.
1671*/
1672int mbtk_radio_state_set(mbtk_info_handle_t* handle, int radio_state)
1673{
1674 if(handle == NULL)
1675 {
1676 LOGE("ARG error.");
1677 return -1;
1678 }
1679 return info_item_process(handle, MBTK_INFO_ID_NET_RADIO_REQ, (uint8*)&radio_state, sizeof(uint8), NULL) ? handle->info_err : 0;
1680}
1681
1682/*
1683* Get time type.
1684*/
1685int mbtk_time_get(mbtk_info_handle_t* handle, int *time_type)
1686{
1687 uint8 state;
1688 if(handle == NULL || time_type == NULL)
1689 {
1690 LOGE("ARG error.");
1691 return -1;
1692 }
1693 if(info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ, NULL, 0, &state) > 0) {
1694 LOG("Time type : %d", state);
1695 *time_type = state;
1696 return 0;
1697 } else {
1698 return handle->info_err;
1699 }
1700}
1701
1702/*
1703*Get Absolute time
1704*"23/05/24 06:09:32 +32 00"
1705*/
1706int mbtk_get_abs_time(char *time_str, time_t *time_out)
1707{
1708 struct tm tm_;
1709
1710 char *ptr = strstr(time_str + 10, " ");
1711 *ptr = '\0';
1712
1713 LOGD("time : \"%s\"", time_str);
1714#if 1
1715 if(strptime(time_str, "%y/%m/%d %T", &tm_) == NULL) {
1716 LOGE("strptime() fail.");
1717 return -1;
1718 }
1719#else
1720 int year, month, day, hour, minute,second,time_zone;
1721 if(strstr(time_str, "+")) {
1722 sscanf(time_str, "%d/%d/%d %d:%d:%d +%d",&year,&month,&day,&hour,&minute,&second,&time_zone);
1723 } else if(strstr(time_str, "-")) {
1724 sscanf(time_str, "%d/%d/%d %d:%d:%d -%d",&year,&month,&day,&hour,&minute,&second,&time_zone);
1725 } else {
1726 LOGE("Time format error:%s", time_str);
1727 return -1;
1728 }
1729
1730 // 1970+
1731 if(year < 70) { // 20xx
1732 tm_.tm_year = 2000 + year;
1733 } else { // 19xx
1734 tm_.tm_year = 1900 + year;
1735 }
1736 tm_.tm_mon = month - 1;
1737 tm_.tm_mday = day;
1738 tm_.tm_hour = hour;
1739 tm_.tm_min = minute;
1740 tm_.tm_sec = second;
1741 tm_.tm_isdst = 0;
1742#endif
1743
1744 time_t timeStamp = mktime(&tm_);
1745 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);
1746 LOGD("time = %ld,%x", timeStamp,timeStamp);
1747 *time_out = timeStamp;
1748
1749 return 0;
1750}
1751
1752/*
1753* Get time type.
1754* "23/05/24,06:09:32+32" -> "23/05/24 06:09:32 +32 00"
1755*/
1756int mbtk_net_time_get(mbtk_info_handle_t* handle, char* time_str)
1757{
1758 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
1759 if(handle == NULL || time_str == NULL)
1760 {
1761 LOGE("ARG error.");
1762 return -1;
1763 }
1764 //printf("mbtk_net_time_get begin info_item_process\n");
b.liubaa41e12024-07-19 15:07:24 +08001765 if(info_item_process(handle, MBTK_INFO_ID_DEV_CELL_TIME_REQ, NULL, 0, buff) > 0) {
liubin281ac462023-07-19 14:22:54 +08001766 memcpy(time_str,buff,strlen(buff));
1767
1768 uint8 *temp = strstr(time_str, ",");
1769 if(temp) {
1770 *temp = ' '; // ',' -> ' '
1771
1772 temp = strstr(time_str, "+");
1773 if(temp == NULL) {
1774 temp = strstr(time_str, "-");
1775 }
1776
1777 if(temp) {
1778 // Copy +XX or -XX
1779 uint8 *last_ptr = temp + strlen(temp) + 1;
1780 while(last_ptr > temp) {
1781 *last_ptr = *(last_ptr - 1);
1782 last_ptr--;
1783 }
1784
1785 *last_ptr = ' ';
1786
1787 memcpy(temp + strlen(temp), "00", 2);
1788
1789 LOGD("%s -> %s", buff, time_str);
1790 return 0;
1791 } else {
1792 LOGE("Time error:%s",buff);
1793 return MBTK_INFO_ERR_TIME_FORMAT;
1794 }
1795 } else {
1796 LOGE("Time error:%s",buff);
1797 return MBTK_INFO_ERR_TIME_FORMAT;
1798 }
1799 } else {
1800 return handle->info_err;
1801 }
1802}
1803
1804/*
1805* Set time.
1806*
1807* time_type:
1808* 0: Cell time
1809* 1: NTP time
1810* 2: User time
1811* time_str: "YYYY-MM-DD HH:MM:SS"
1812*/
1813int mbtk_time_set(mbtk_info_handle_t* handle, mbtk_time_type_enum time_type, char* time_str)
1814{
1815 if(handle == NULL)
1816 {
1817 LOGE("ARG error.");
1818 return -1;
1819 }
1820 uint8 buffer[100] = {0};
1821 buffer[0] = (uint8)time_type;
1822 if(time_type == MBTK_TIME_TYPE_USER) {
1823 if(!str_empty(time_str)) {
1824 memcpy(buffer + sizeof(uint8), time_str, strlen(time_str));
1825 return info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ,
1826 buffer, sizeof(uint8) + strlen(time_str), NULL) ? handle->info_err : 0;
1827 } else {
1828 return -1;
1829 }
1830 } else {
1831 return info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ,
1832 buffer, sizeof(uint8), NULL) ? handle->info_err : 0;
1833 }
1834}
1835
1836/*
1837* Return sms cmgf.
1838*/
1839int mbtk_sms_cmgf_get(mbtk_info_handle_t* handle, int *volte_state)
1840{
1841 uint8 state;
1842 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGF_REQ, NULL, 0, &state) > 0) {
1843 LOG("mbtk_sms_cmgf_get()-----------sms cmgf : %d", state);
1844 *volte_state = state;
1845 return 0;
1846 } else {
1847 return handle->info_err;
1848 }
1849}
1850
1851/*
1852* Set sms cmgf.
1853*
1854* volte_state:
1855* 0 : PDU mode.
1856* 1 : text mode.
1857*
1858* Restarting takes effect after execution.
1859*/
1860int mbtk_sms_cmgf_set(mbtk_info_handle_t* handle, int mode)
1861{
1862 printf("mbtk_sms_cmgf_set()--------mode=:%d, len:%d", mode, sizeof(uint8));
1863 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGF_REQ, (uint8*)&mode, sizeof(uint8), NULL) ? handle->info_err : 0;
1864}
1865
1866/*
1867* Set sms cmgs.
1868*
1869if PDU mode (+CMGF=0):
1870+CMGS=<length><CR>
1871PDU is given<ctrl-Z/ESC>
1872
1873if text mode (+CMGF=1):
1874+CMGS=<da>[,<toda>]<CR>
1875text is entered<ctrl-Z/ESC>
1876
1877* Restarting takes effect after execution.
1878*/
1879int mbtk_sms_cmgs_set(mbtk_info_handle_t* handle, char * cmgs, char *resp)
1880{
1881 printf("mbtk_sms_cmgs_set(1)--------cmgs=:%s, len:%d", cmgs, strlen(cmgs));
1882// char req[20] = {0}
1883 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGS_REQ, cmgs, strlen(cmgs), resp) > 0){
1884 printf("resp:%s\n", resp);
1885 return 0;
1886 }else{
1887 return handle->info_err;
1888 }
1889}
1890
1891/*
1892* Set sms cmgw.
1893*
1894if text mode (+CMGF=1):
1895+CMGW=<oa/da>[,<tooa/toda>[,<stat>]]
1896<CR>
1897text is entered<ctrl-Z/ESC>
1898if PDU mode (+CMGF=0):
1899+CMGW=<length>[,<stat>]<CR>PDU is
1900given<ctrl-Z/ESC>
1901
1902*/
1903
1904int mbtk_sms_cmgw_set(mbtk_info_handle_t* handle, char * cmgw, char *resp)
1905{
1906 printf("mbtk_sms_cmgw_set() ----------cmgw:%s, len:%d", cmgw, strlen(cmgw));
1907 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGW_REQ, cmgw, strlen(cmgw), resp) > 0){
1908 printf("resp:%s\n", resp);
1909 return 0;
1910 }else{
1911 return handle->info_err;
1912 }
1913}
1914
1915/*
1916* Set sms cmgd.
1917*
1918* +CMGD=<index>[,<delflag>]
1919*
1920* Restarting takes effect after execution.
1921*/
1922int mbtk_sms_cmgd_set(mbtk_info_handle_t* handle, char * cmdg)
1923{
1924 printf("mbtk_sms_cmgd_set() cmdg:%s, len:%d",cmdg, strlen(cmdg));
1925 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGD_REQ, cmdg, strlen(cmdg), NULL) ? handle->info_err : 0;
1926}
1927
1928/*
r.xiaoeb9dba42024-02-07 02:16:13 -08001929* Get sms cmgd.
1930*
1931* +CMGD: (XXX,XXX)(0-4)
1932*
1933* Restarting takes effect after execution.
1934*/
1935int mbtk_sms_cmgd_get(mbtk_info_handle_t* handle, char * cmdg)
1936{
1937 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGD_REQ, NULL, 0, cmdg) ? handle->info_err : 0;
1938}
1939
1940
1941/*
liubin281ac462023-07-19 14:22:54 +08001942* Set sms cmgl.
1943*
1944* AT+CMGL[=<stat>]
1945*
1946* Restarting takes effect after execution.
1947*/
1948int mbtk_sms_cmgl_set(mbtk_info_handle_t* handle, char * cmgl, char *resp)
1949{
1950 printf("0mbtk_sms_cmgl_set() cmgl:%s, len:%d\n",cmgl, strlen(cmgl));
1951 char reg[5*1024] ={0};
1952 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMGL_REQ, cmgl, strlen(cmgl), reg) > 0){
1953 printf("len:%d , reg:%s\n", strlen(reg), reg);
1954 // memcpy(resp, reg, strlen(reg));
1955 return 0;
1956 }else {
1957 return handle->info_err;
1958 }
1959}
1960
1961/*
1962* Return sms csca.
1963*/
1964int mbtk_sms_csca_get(mbtk_info_handle_t* handle, char *buf)
1965{
1966 // char state;
1967 if(info_item_process(handle, MBTK_INFO_ID_SMS_CSCA_REQ, NULL, 0, buf) > 0) {
1968 LOG("mbtk_sms_csca_get()-----------sms csca : %s", buf);
1969 // *volte_state = state;
1970 return 0;
1971 } else {
1972 return handle->info_err;
1973 }
1974}
1975
1976/*
1977* Set sms csca.
1978*
1979* AT+CSCA=<number> [,<type>]
1980*
1981* Restarting takes effect after execution.
1982*/
1983int mbtk_sms_csca_set(mbtk_info_handle_t* handle, char * csca)
1984{
1985 printf("mbtk_sms_csca_set() csca:%s, len:%d",csca, strlen(csca));
1986 return info_item_process(handle, MBTK_INFO_ID_SMS_CSCA_REQ, csca, strlen(csca), NULL) ? handle->info_err : 0;
1987}
1988
1989/*
1990* Set sms csmp.
1991*
1992* AT+CSMP=[<fo>[,<vp>[,<pid>[,<dcs>]]]]
1993*
1994* Restarting takes effect after execution.
1995*/
1996int mbtk_sms_csmp_set(mbtk_info_handle_t* handle, char * csmp)
1997{
1998 printf("mbtk_sms_csmp_set() csmp:%s, len:%d",csmp, strlen(csmp));
1999 return info_item_process(handle, MBTK_INFO_ID_SMS_CSMP_REQ, csmp, strlen(csmp), NULL) ? handle->info_err : 0;
2000}
2001
2002/*
2003* Set sms cscb.
2004*
2005* AT+CSCB=<[<mode>[,<mids>[,<dcss>]]]>
2006*
2007* Restarting takes effect after execution.
2008*/
2009int mbtk_sms_cscb_set(mbtk_info_handle_t* handle, char * cscb)
2010{
2011 printf("mbtk_sms_cscb_set() cscb:%s, len:%d",cscb, strlen(cscb));
2012 return info_item_process(handle, MBTK_INFO_ID_SMS_CSCB_REQ, cscb, strlen(cscb), NULL) ? handle->info_err : 0;
2013}
2014
2015/*
2016* Set sms cnmi.
2017*
2018at+cnmi=1,2
2019
2020OK
2021if sending fails:
2022+CMS ERROR: <err>
2023*/
2024int mbtk_sms_cnmi_set(mbtk_info_handle_t* handle)
2025{
2026 printf("mbtk_sms_cnmi_set()------------start\n");
2027
2028 return info_item_process(handle, MBTK_INFO_ID_SMS_CNMI_REQ, NULL, 0, NULL)? handle->info_err : 0;
2029}
2030
2031/*
2032* Set sms cmss.
2033*
2034+CMSS=<index>[,<da>[,<toda>]]
2035
2036if sending successful:
2037+CMSS: <mr>
2038OK
2039if sending fails:
2040+CMS ERROR: <err>
2041*/
2042int mbtk_sms_cmss_set(mbtk_info_handle_t* handle, char * cmss, char *resp)
2043{
2044 printf("mbtk_sms_cmss_set()------------cmss:%s, len:%d", cmss, strlen(cmss));
2045 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMSS_REQ, cmss, strlen(cmss), resp) > 0){
2046 printf("resp:%s\n", resp);
2047 return 0;
2048 }else{
2049 return handle->info_err;
2050 }
2051}
2052
2053/*
2054* Return sms cmgf.
2055*/
2056int mbtk_sms_cpms_get(mbtk_info_handle_t* handle, char * mem)
2057{
2058 char req[128] = {0};
2059 if(info_item_process(handle, MBTK_INFO_ID_SMS_CPMS_REQ, NULL, 0, &req) > 0) {
2060 LOG("mbtk_sms_cpms_get() req : %s, strlen(mem_ptr):%d\n", req, strlen(req));
2061 memcpy(mem, req, strlen(req));
2062 return 0;
2063 } else {
2064 return handle->info_err;
2065 }
2066}
2067
2068
2069/*
2070* Set sms cpms.
2071*
2072* AT+CPMS=<mem1>[,<mem2>[,<mem3>]]
2073*
2074* Restarting takes effect after execution.
2075*/
2076int mbtk_sms_cpms_set(mbtk_info_handle_t* handle, char * mem, char* response)
2077{
2078 printf("mbtk_sms_cpms_set() mem:%s, len:%d",mem, strlen(mem));
2079 char req[128] = {0};
2080 if( info_item_process(handle, MBTK_INFO_ID_SMS_CPMS_REQ, mem, strlen(mem), req) > 0){
2081 memcpy(response, req, strlen(req));
2082 return 0;
2083 }
2084 else{
2085 return handle->info_err;
2086 }
2087}
2088
2089/*
2090* Set sms cm.
2091*
2092* +CMGR=<index>
2093
2094if PDU mode (+CMGF=0) ��command successful:
2095+CMGR: <stat>,[<alpha>],<length><CR><LF><pdu>
2096OK
2097if text mode (+CMGF=1), command successful and SMS-DELIVER:
2098+CMGR:<stat>,<oa>,[<alpha>],<scts>[,<tooa>,<fo>,<pid>,<dcs
2099>, <sca>,<tosca>,<length>]<CR><LF><data>
2100OK
2101if text mode (+CMGF=1), command successful and SMS-SUBMIT:
2102+CMGR:
2103<stat>,<da>,[<alpha>][,<toda>,<fo>,<pid>,<dcs>,[<vp>],
2104<sca>,<tosca>,<length>]<CR><LF><data>
2105OK
2106otherwise:
2107+CMS ERROR: <err>
2108*
2109* Restarting takes effect after execution.
2110*/
2111int mbtk_sms_cmgr_set(mbtk_info_handle_t* handle, int index, char *resp)
2112{
2113 printf("mbtk_sms_cmgr_set() --------------index:%d",index);
2114 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMGR_REQ, (uint8*)&index, sizeof(uint8), resp) > 0){
2115 printf("resp:%s\n", resp);
2116 return 0;
2117 }else{
2118 return handle->info_err;
2119 }
2120}
2121
2122
2123/*
2124* Get sim state.
2125*/
2126int mbtk_sim_state_get(mbtk_info_handle_t* handle, mbtk_sim_state_enum *sim_state)
2127{
2128 uint8 state;
2129 if(handle == NULL || sim_state == NULL)
2130 {
2131 LOGE("ARG error.");
2132 return -1;
2133 }
2134 if(info_item_process(handle, MBTK_INFO_ID_SIM_STATE_REQ, NULL, 0, &state) > 0) {
2135 *sim_state = (mbtk_sim_state_enum)state;
2136 LOG("Sim state : %d", *sim_state);
2137 return 0;
2138 } else {
2139 return handle->info_err;
2140 }
2141}
2142
2143/*
2144* Get sim card type.
2145*/
2146int mbtk_sim_card_type_get(mbtk_info_handle_t* handle, mbtk_sim_card_type_enum *sim_card_type)
2147{
2148 uint8 state;
2149 if(handle == NULL || sim_card_type == NULL)
2150 {
2151 LOGE("ARG error.");
2152 return -1;
2153 }
2154 if(info_item_process(handle, MBTK_INFO_ID_SIM_STYPE_REQ, NULL, 0, &state) > 0) {
2155 *sim_card_type = (mbtk_sim_card_type_enum)state;
2156 LOG("Sim sim_card_type : %d", *sim_card_type);
2157 return 0;
2158 } else {
2159 return handle->info_err;
2160 }
2161}
2162
2163/*
2164* Get system temperature.
2165*
2166* type[IN]:
2167* 0: Soc temperature.
2168* 1: RF temperature.
2169* temp[OUT]:
2170* temperature in celsius.
2171*/
r.xiao2102d762024-06-07 03:10:38 -07002172int mbtk_temp_get(mbtk_info_handle_t* handle, int type, mbtk_thermal_info_t* temp)
liubin281ac462023-07-19 14:22:54 +08002173{
2174 if(handle == NULL)
2175 {
2176 LOGE("ARG error.");
2177 return -1;
2178 }
r.xiao2102d762024-06-07 03:10:38 -07002179 if(type != 0 && type != 1)
liubin281ac462023-07-19 14:22:54 +08002180 {
2181 return -1;
2182 }
2183
2184 uint8 temp_type = (uint8)type;
r.xiao2102d762024-06-07 03:10:38 -07002185
2186 if(info_item_process(handle, MBTK_INFO_ID_DEV_TEMP_REQ, &temp_type, sizeof(uint8), temp) > 0) {
2187
2188 LOG("Temperature : %d", temp->ther);
liubin281ac462023-07-19 14:22:54 +08002189 return 0;
2190 } else {
2191 return handle->info_err;
2192 }
2193
2194 return 0;
2195}
2196
2197
2198/*
2199* Set sim power state.
2200* power:
2201* 0: Sim power off.
2202* 1: Sim power on.
2203*/
2204int mbtk_sim_power_set(int power)
2205{
2206 if(power != 0 && power != 1)
2207 {
2208 return -1;
2209 }
2210
2211 // /sys/devices/virtual/usim_event/usim0/send_event
2212 char cmd[100] = {0};
2213 sprintf(cmd, "echo %d > /sys/devices/virtual/usim_event/usim0/send_event", power ? 0 : 1);
2214 system(cmd);
2215
2216 return 0;
2217}
2218
2219/*
2220* System power.
2221* type:
2222* 0: Reboot system.
2223* 1: Poweroff system.
2224* 2: Halt system.
2225*/
2226int mbtk_system_reboot(int type)
2227{
2228 if(type != 0 && type != 1 && type != 2)
2229 {
2230 return -1;
2231 }
2232
2233 switch(type) {
2234 case 0: {
2235 system("reboot");
2236 break;
2237 }
2238 case 1: {
2239 system("poweroff");
2240 break;
2241 }
2242 case 2: {
2243 system("halt");
2244 break;
2245 }
2246 default: {
2247 break;
2248 }
2249 }
2250
2251 return 0;
2252}
2253
2254/*
2255* set modem fun
2256*
2257*/
2258int mbtk_set_modem_fun(mbtk_info_handle_t* handle, mbtk_modem_info_t *info)
2259{
2260 if(handle == NULL)
2261 {
2262 LOGE("ARG error.");
2263 return -1;
2264 }
2265 return info_item_process(handle, MBTK_INFO_ID_DEV_MODEM_REQ, info, sizeof(mbtk_modem_info_t), NULL) ? handle->info_err : 0;
2266}
2267
2268/*
2269* get modem fun
2270*
2271*/
2272int mbtk_get_modem_fun(mbtk_info_handle_t* handle, int* fun)
2273{
2274 uint8 state;
2275 if(handle == NULL || fun == NULL)
2276 {
2277 LOGE("ARG error.");
2278 return -1;
2279 }
2280 if(info_item_process(handle, MBTK_INFO_ID_DEV_MODEM_REQ, NULL, 0, &state) > 0) {
2281 LOG("modem type : %d", state);
2282 *fun = state;
2283 return 0;
2284 } else {
2285 return handle->info_err;
2286 }
2287}
2288
2289/*
2290* call_start
2291*
2292*/
2293int mbtk_call_start(mbtk_info_handle_t* handle, char* phone_number)
2294{
2295 if(handle == NULL)
2296 {
2297 LOGE("ARG error.");
2298 return -1;
2299 }
2300 if(str_empty(phone_number))
2301 return -1;
2302
2303 return info_item_process(handle, MBTK_INFO_ID_CALL_START_REQ,
2304 phone_number, strlen(phone_number), NULL) ? handle->info_err : 0;
2305}
2306/*
2307* Answer the phone call.
2308*
2309*/
2310int mbtk_call_answer(mbtk_info_handle_t* handle)
2311{
2312 return info_item_process(handle, MBTK_INFO_ID_CALL_ANSWER_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2313}
2314
2315/*
2316* Hang up all call.
2317*
2318*/
2319int mbtk_call_hang(mbtk_info_handle_t* handle)
2320{
2321 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2322}
2323
2324/*
2325* Hang up a call.
2326*
2327*/
2328int mbtk_a_call_hang(mbtk_info_handle_t* handle, int phone_id)
2329{
2330 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_A_REQ, (uint8*)&phone_id, sizeof(uint8), NULL) ? handle->info_err : 0;
2331}
2332
2333/*
2334* Hang up waiting or background call.
2335*
2336*/
2337int mbtk_waiting_or_background_call_hang(mbtk_info_handle_t* handle)
2338{
2339 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_B_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2340}
2341
2342/*
2343* Hang up foreground resume background call.
2344*
2345*/
2346int mbtk_foreground_resume_background_call_hang(mbtk_info_handle_t* handle)
2347{
2348 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_C_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2349}
2350
2351/*
2352* Get current call phone number.
2353*/
2354int mbtk_call_reg_get(mbtk_info_handle_t* handle, mbtk_call_info_t *reg)
2355{
2356 if(info_item_process(handle, MBTK_INFO_ID_CALL_WAITIN_REQ, NULL, 0, reg) > 0) {
2357 LOG("CLCC : %d, %d, %d, %d, %d, %s, %d", reg->dir1, reg->dir, reg->state, reg->mode, reg->mpty, reg->phone_number, reg->type);
2358 return 0;
2359 } else {
2360 return handle->info_err;
2361 }
2362}
2363
2364/*
2365* Return mute state.
2366*/
2367int mbtk_mute_state_get(mbtk_info_handle_t* handle, int *mute_state)
2368{
2369 uint8 state;
2370 if(info_item_process(handle, MBTK_INFO_ID_CALL_MUTE_REQ, NULL, 0, &state) > 0) {
2371 LOG("Mute State : %d", state);
2372 *mute_state = state;
2373 return 0;
2374 } else {
2375 return handle->info_err;
2376 }
2377}
2378
2379/*
2380* Set mute state.
2381*
2382* mute_state:
2383* 0 : of mute.
2384* 1 : on mute.
2385*
2386* Restarting takes effect after execution.
2387*/
2388int mbtk_mute_state_set(mbtk_info_handle_t* handle, int mute_state)
2389{
2390 return info_item_process(handle, MBTK_INFO_ID_CALL_MUTE_REQ, (uint8*)&mute_state, sizeof(uint8), NULL) ? handle->info_err : 0;
2391}
2392
2393/*
r.xiaoec113d12024-01-12 02:13:28 -08002394* Set wakeup state.
2395*
2396* wakeup_state:(0~31)
2397* 0 : means resume all
2398* 1~31 means suspend
2399* Control the active reporting of some platform modems to reduce wakeup
2400*/
2401
2402int mbtk_wakeup_state_set(mbtk_info_handle_t* handle, uint32 wakeup_state)
2403{
2404 return info_item_process(handle, MBTK_INFO_ID_WAKEUP_STA_REQ, (uint32*)&wakeup_state, sizeof(uint32), NULL) ? handle->info_err : 0;
2405}
2406
2407/*
2408* oos get.
2409*/
2410int mbtk_oos_get(mbtk_info_handle_t* handle, mbtk_oos_info *oos_info)
2411{
2412 if(info_item_process(handle, MBTK_INFO_ID_OOS_STA_REQ, NULL, 0, oos_info) > 0) {
2413 return 0;
2414 } else {
2415 return handle->info_err;
2416 }
2417}
2418
2419/*
2420* oos set .
2421*/
r.xiaocfd7c682024-01-22 03:59:46 -08002422int mbtk_oos_set(mbtk_info_handle_t* handle, mbtk_oos_info *oos_info)
r.xiaoec113d12024-01-12 02:13:28 -08002423{
2424 if(handle == NULL)
2425 {
2426 LOGE("ARG error.");
2427 return -1;
2428 }
r.xiaoec113d12024-01-12 02:13:28 -08002429
r.xiaocfd7c682024-01-22 03:59:46 -08002430 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 -08002431}
2432
2433
2434/*
liubin281ac462023-07-19 14:22:54 +08002435* Set DTMF character.
2436*
2437*/
2438int mbtk_dtmf_send(mbtk_info_handle_t* handle, mbtk_call_dtmf_info_t *dtmf_character)
2439{
2440 if(handle == NULL)
2441 {
2442 LOGE("ARG error.");
2443 return -1;
2444 }
2445 return info_item_process(handle, MBTK_INFO_ID_CALL_DTMF_REQ,
2446 dtmf_character, sizeof(mbtk_call_dtmf_info_t), NULL) ? handle->info_err : 0;
2447}
2448
2449/*
wangyouqiang38e53362024-01-23 10:53:48 +08002450* Set net led.
2451*
2452*/
2453int mbtk_led_set(mbtk_info_handle_t* handle, mbtk_led_type type, mbtk_led_status status)
2454{
2455 if(handle == NULL)
2456 {
2457 LOGE("ARG error.");
2458 return -1;
2459 }
2460
2461 char buff[3] = {0};
2462 if(type == MBTK_LED_TYPE_NET)
2463 {
2464 buff[0] = 0;
2465 }
2466 else
2467 {
2468 buff[0] = 1;
2469 }
2470
2471 if(status == MBTK_LED_STATUS_CLOSE)
2472 {
2473 buff[1] = 0;
2474 }
2475 else
2476 {
2477 buff[1] = 1;
2478 }
2479 return info_item_process(handle, MBTK_INFO_ID_LED_REQ, buff, 2, NULL) ? handle->info_err : 0;
2480}
2481
2482/*
liubin281ac462023-07-19 14:22:54 +08002483* Set pdp state change callback function.
2484*/
2485int mbtk_pdp_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 }
2492 if(info_item_process(handle, MBTK_INFO_ID_IND_PDP_STATE_CHANGE, NULL, 0, NULL) < 0) {
2493 return handle->info_err;
2494 } else {
2495 handle->pdp_state_cb = cb;
2496 return 0;
2497 }
2498}
2499
2500/*
2501* Set network state change callback function.
2502*/
2503int mbtk_net_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 }
b.liu27b91e42024-01-17 20:02:30 +08002510 if(info_item_process(handle, MBTK_INFO_ID_IND_NET_STATE_CHANGE, NULL, 0, NULL) < 0) {
2511 return handle->info_err;
2512 } else {
liubin281ac462023-07-19 14:22:54 +08002513 handle->net_state_cb = cb;
2514 return 0;
liubin281ac462023-07-19 14:22:54 +08002515 }
2516}
2517
2518/*
2519* Set call state change callback function.
2520*/
2521int mbtk_call_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_CALL_STATE_CHANGE, NULL, 0, NULL) < 0) {
2529 return handle->info_err;
2530 } else {
2531 handle->call_state_cb = cb;
2532 return 0;
2533 }
2534}
2535
2536/*
2537* Set sms state change callback function.
2538*/
2539int mbtk_sms_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_SMS_STATE_CHANGE, NULL, 0, NULL) < 0) {
2547 return handle->info_err;
2548 } else {
2549 handle->sms_state_cb = cb;
2550 return 0;
2551 }
2552}
2553
2554/*
2555* Set radio state change callback function.
2556*/
2557int mbtk_radio_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_RADIO_STATE_CHANGE, NULL, 0, NULL) < 0) {
2565 return handle->info_err;
2566 } else {
2567 handle->radio_state_cb = cb;
2568 return 0;
2569 }
2570}
2571
2572/*
2573* Set sim state change callback function.
2574*/
2575int mbtk_sim_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_SIM_STATE_CHANGE, NULL, 0, NULL) < 0) {
2583 return handle->info_err;
2584 } else {
2585 handle->sim_state_cb = cb;
2586 return 0;
2587 }
2588}
r.xiaofca7c472024-04-24 01:00:23 -07002589
2590/*
2591* Set signal state change callback function.
2592*/
2593int mbtk_signal_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2594{
2595 if(handle == NULL)
2596 {
2597 LOGE("ARG error.");
2598 return -1;
2599 }
2600 if(info_item_process(handle, MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, NULL, 0, NULL) < 0) {
2601 return handle->info_err;
2602 } else {
2603 handle->signal_state_cb = cb;
2604 return 0;
2605 }
2606}
2607