blob: ce9cc9c8e7e0e85046762db2a36306895c7a3e65 [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/*
1593* Get current network IMS information.
1594*/
1595int mbtk_net_ims_get(mbtk_info_handle_t* handle, int* reg)
1596{
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) {
1604 LOG("reg type : %d", state);
1605 *reg = state;
1606 return 0;
1607 } else {
1608 return handle->info_err;
1609 }
1610
1611}
1612
1613/*
1614* Set current network IMS information.
1615*/
1616int mbtk_net_ims_set(mbtk_info_handle_t* handle, int reg)
1617{
1618 if(handle == NULL)
1619 {
1620 LOGE("ARG error.");
1621 return -1;
1622 }
1623
1624 return info_item_process(handle, MBTK_INFO_ID_NET_IMS_REQ, (uint8*)&reg, sizeof(uint8), NULL) ? handle->info_err : 0;
1625
1626}
1627
liubin281ac462023-07-19 14:22:54 +08001628
1629/*
1630* Get radio state.
1631*/
1632int mbtk_radio_state_get(mbtk_info_handle_t* handle, int *radio_state)
1633{
1634 uint8 state;
1635 if(handle == NULL || radio_state == NULL)
1636 {
1637 LOGE("ARG error.");
1638 return -1;
1639 }
1640 if(info_item_process(handle, MBTK_INFO_ID_NET_RADIO_REQ, NULL, 0, &state) > 0) {
1641 LOG("Radio state : %d", state);
1642 *radio_state = state;
1643 return 0;
1644 } else {
1645 return handle->info_err;
1646 }
1647}
1648
1649/*
1650* Set radio state.
1651*/
1652int mbtk_radio_state_set(mbtk_info_handle_t* handle, int radio_state)
1653{
1654 if(handle == NULL)
1655 {
1656 LOGE("ARG error.");
1657 return -1;
1658 }
1659 return info_item_process(handle, MBTK_INFO_ID_NET_RADIO_REQ, (uint8*)&radio_state, sizeof(uint8), NULL) ? handle->info_err : 0;
1660}
1661
1662/*
1663* Get time type.
1664*/
1665int mbtk_time_get(mbtk_info_handle_t* handle, int *time_type)
1666{
1667 uint8 state;
1668 if(handle == NULL || time_type == NULL)
1669 {
1670 LOGE("ARG error.");
1671 return -1;
1672 }
1673 if(info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ, NULL, 0, &state) > 0) {
1674 LOG("Time type : %d", state);
1675 *time_type = state;
1676 return 0;
1677 } else {
1678 return handle->info_err;
1679 }
1680}
1681
1682/*
1683*Get Absolute time
1684*"23/05/24 06:09:32 +32 00"
1685*/
1686int mbtk_get_abs_time(char *time_str, time_t *time_out)
1687{
1688 struct tm tm_;
1689
1690 char *ptr = strstr(time_str + 10, " ");
1691 *ptr = '\0';
1692
1693 LOGD("time : \"%s\"", time_str);
1694#if 1
1695 if(strptime(time_str, "%y/%m/%d %T", &tm_) == NULL) {
1696 LOGE("strptime() fail.");
1697 return -1;
1698 }
1699#else
1700 int year, month, day, hour, minute,second,time_zone;
1701 if(strstr(time_str, "+")) {
1702 sscanf(time_str, "%d/%d/%d %d:%d:%d +%d",&year,&month,&day,&hour,&minute,&second,&time_zone);
1703 } else 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 {
1706 LOGE("Time format error:%s", time_str);
1707 return -1;
1708 }
1709
1710 // 1970+
1711 if(year < 70) { // 20xx
1712 tm_.tm_year = 2000 + year;
1713 } else { // 19xx
1714 tm_.tm_year = 1900 + year;
1715 }
1716 tm_.tm_mon = month - 1;
1717 tm_.tm_mday = day;
1718 tm_.tm_hour = hour;
1719 tm_.tm_min = minute;
1720 tm_.tm_sec = second;
1721 tm_.tm_isdst = 0;
1722#endif
1723
1724 time_t timeStamp = mktime(&tm_);
1725 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);
1726 LOGD("time = %ld,%x", timeStamp,timeStamp);
1727 *time_out = timeStamp;
1728
1729 return 0;
1730}
1731
1732/*
1733* Get time type.
1734* "23/05/24,06:09:32+32" -> "23/05/24 06:09:32 +32 00"
1735*/
1736int mbtk_net_time_get(mbtk_info_handle_t* handle, char* time_str)
1737{
1738 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
1739 if(handle == NULL || time_str == NULL)
1740 {
1741 LOGE("ARG error.");
1742 return -1;
1743 }
1744 //printf("mbtk_net_time_get begin info_item_process\n");
1745 if(info_item_process(handle, MBTK_INFO_ID_NET_TIME_REQ, NULL, 0, buff) > 0) {
1746 memcpy(time_str,buff,strlen(buff));
1747
1748 uint8 *temp = strstr(time_str, ",");
1749 if(temp) {
1750 *temp = ' '; // ',' -> ' '
1751
1752 temp = strstr(time_str, "+");
1753 if(temp == NULL) {
1754 temp = strstr(time_str, "-");
1755 }
1756
1757 if(temp) {
1758 // Copy +XX or -XX
1759 uint8 *last_ptr = temp + strlen(temp) + 1;
1760 while(last_ptr > temp) {
1761 *last_ptr = *(last_ptr - 1);
1762 last_ptr--;
1763 }
1764
1765 *last_ptr = ' ';
1766
1767 memcpy(temp + strlen(temp), "00", 2);
1768
1769 LOGD("%s -> %s", buff, time_str);
1770 return 0;
1771 } else {
1772 LOGE("Time error:%s",buff);
1773 return MBTK_INFO_ERR_TIME_FORMAT;
1774 }
1775 } else {
1776 LOGE("Time error:%s",buff);
1777 return MBTK_INFO_ERR_TIME_FORMAT;
1778 }
1779 } else {
1780 return handle->info_err;
1781 }
1782}
1783
1784/*
1785* Set time.
1786*
1787* time_type:
1788* 0: Cell time
1789* 1: NTP time
1790* 2: User time
1791* time_str: "YYYY-MM-DD HH:MM:SS"
1792*/
1793int mbtk_time_set(mbtk_info_handle_t* handle, mbtk_time_type_enum time_type, char* time_str)
1794{
1795 if(handle == NULL)
1796 {
1797 LOGE("ARG error.");
1798 return -1;
1799 }
1800 uint8 buffer[100] = {0};
1801 buffer[0] = (uint8)time_type;
1802 if(time_type == MBTK_TIME_TYPE_USER) {
1803 if(!str_empty(time_str)) {
1804 memcpy(buffer + sizeof(uint8), time_str, strlen(time_str));
1805 return info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ,
1806 buffer, sizeof(uint8) + strlen(time_str), NULL) ? handle->info_err : 0;
1807 } else {
1808 return -1;
1809 }
1810 } else {
1811 return info_item_process(handle, MBTK_INFO_ID_DEV_TIME_REQ,
1812 buffer, sizeof(uint8), NULL) ? handle->info_err : 0;
1813 }
1814}
1815
1816/*
1817* Return sms cmgf.
1818*/
1819int mbtk_sms_cmgf_get(mbtk_info_handle_t* handle, int *volte_state)
1820{
1821 uint8 state;
1822 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGF_REQ, NULL, 0, &state) > 0) {
1823 LOG("mbtk_sms_cmgf_get()-----------sms cmgf : %d", state);
1824 *volte_state = state;
1825 return 0;
1826 } else {
1827 return handle->info_err;
1828 }
1829}
1830
1831/*
1832* Set sms cmgf.
1833*
1834* volte_state:
1835* 0 : PDU mode.
1836* 1 : text mode.
1837*
1838* Restarting takes effect after execution.
1839*/
1840int mbtk_sms_cmgf_set(mbtk_info_handle_t* handle, int mode)
1841{
1842 printf("mbtk_sms_cmgf_set()--------mode=:%d, len:%d", mode, sizeof(uint8));
1843 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGF_REQ, (uint8*)&mode, sizeof(uint8), NULL) ? handle->info_err : 0;
1844}
1845
1846/*
1847* Set sms cmgs.
1848*
1849if PDU mode (+CMGF=0):
1850+CMGS=<length><CR>
1851PDU is given<ctrl-Z/ESC>
1852
1853if text mode (+CMGF=1):
1854+CMGS=<da>[,<toda>]<CR>
1855text is entered<ctrl-Z/ESC>
1856
1857* Restarting takes effect after execution.
1858*/
1859int mbtk_sms_cmgs_set(mbtk_info_handle_t* handle, char * cmgs, char *resp)
1860{
1861 printf("mbtk_sms_cmgs_set(1)--------cmgs=:%s, len:%d", cmgs, strlen(cmgs));
1862// char req[20] = {0}
1863 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGS_REQ, cmgs, strlen(cmgs), resp) > 0){
1864 printf("resp:%s\n", resp);
1865 return 0;
1866 }else{
1867 return handle->info_err;
1868 }
1869}
1870
1871/*
1872* Set sms cmgw.
1873*
1874if text mode (+CMGF=1):
1875+CMGW=<oa/da>[,<tooa/toda>[,<stat>]]
1876<CR>
1877text is entered<ctrl-Z/ESC>
1878if PDU mode (+CMGF=0):
1879+CMGW=<length>[,<stat>]<CR>PDU is
1880given<ctrl-Z/ESC>
1881
1882*/
1883
1884int mbtk_sms_cmgw_set(mbtk_info_handle_t* handle, char * cmgw, char *resp)
1885{
1886 printf("mbtk_sms_cmgw_set() ----------cmgw:%s, len:%d", cmgw, strlen(cmgw));
1887 if(info_item_process(handle, MBTK_INFO_ID_SMS_CMGW_REQ, cmgw, strlen(cmgw), resp) > 0){
1888 printf("resp:%s\n", resp);
1889 return 0;
1890 }else{
1891 return handle->info_err;
1892 }
1893}
1894
1895/*
1896* Set sms cmgd.
1897*
1898* +CMGD=<index>[,<delflag>]
1899*
1900* Restarting takes effect after execution.
1901*/
1902int mbtk_sms_cmgd_set(mbtk_info_handle_t* handle, char * cmdg)
1903{
1904 printf("mbtk_sms_cmgd_set() cmdg:%s, len:%d",cmdg, strlen(cmdg));
1905 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGD_REQ, cmdg, strlen(cmdg), NULL) ? handle->info_err : 0;
1906}
1907
1908/*
r.xiaoeb9dba42024-02-07 02:16:13 -08001909* Get sms cmgd.
1910*
1911* +CMGD: (XXX,XXX)(0-4)
1912*
1913* Restarting takes effect after execution.
1914*/
1915int mbtk_sms_cmgd_get(mbtk_info_handle_t* handle, char * cmdg)
1916{
1917 return info_item_process(handle, MBTK_INFO_ID_SMS_CMGD_REQ, NULL, 0, cmdg) ? handle->info_err : 0;
1918}
1919
1920
1921/*
liubin281ac462023-07-19 14:22:54 +08001922* Set sms cmgl.
1923*
1924* AT+CMGL[=<stat>]
1925*
1926* Restarting takes effect after execution.
1927*/
1928int mbtk_sms_cmgl_set(mbtk_info_handle_t* handle, char * cmgl, char *resp)
1929{
1930 printf("0mbtk_sms_cmgl_set() cmgl:%s, len:%d\n",cmgl, strlen(cmgl));
1931 char reg[5*1024] ={0};
1932 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMGL_REQ, cmgl, strlen(cmgl), reg) > 0){
1933 printf("len:%d , reg:%s\n", strlen(reg), reg);
1934 // memcpy(resp, reg, strlen(reg));
1935 return 0;
1936 }else {
1937 return handle->info_err;
1938 }
1939}
1940
1941/*
1942* Return sms csca.
1943*/
1944int mbtk_sms_csca_get(mbtk_info_handle_t* handle, char *buf)
1945{
1946 // char state;
1947 if(info_item_process(handle, MBTK_INFO_ID_SMS_CSCA_REQ, NULL, 0, buf) > 0) {
1948 LOG("mbtk_sms_csca_get()-----------sms csca : %s", buf);
1949 // *volte_state = state;
1950 return 0;
1951 } else {
1952 return handle->info_err;
1953 }
1954}
1955
1956/*
1957* Set sms csca.
1958*
1959* AT+CSCA=<number> [,<type>]
1960*
1961* Restarting takes effect after execution.
1962*/
1963int mbtk_sms_csca_set(mbtk_info_handle_t* handle, char * csca)
1964{
1965 printf("mbtk_sms_csca_set() csca:%s, len:%d",csca, strlen(csca));
1966 return info_item_process(handle, MBTK_INFO_ID_SMS_CSCA_REQ, csca, strlen(csca), NULL) ? handle->info_err : 0;
1967}
1968
1969/*
1970* Set sms csmp.
1971*
1972* AT+CSMP=[<fo>[,<vp>[,<pid>[,<dcs>]]]]
1973*
1974* Restarting takes effect after execution.
1975*/
1976int mbtk_sms_csmp_set(mbtk_info_handle_t* handle, char * csmp)
1977{
1978 printf("mbtk_sms_csmp_set() csmp:%s, len:%d",csmp, strlen(csmp));
1979 return info_item_process(handle, MBTK_INFO_ID_SMS_CSMP_REQ, csmp, strlen(csmp), NULL) ? handle->info_err : 0;
1980}
1981
1982/*
1983* Set sms cscb.
1984*
1985* AT+CSCB=<[<mode>[,<mids>[,<dcss>]]]>
1986*
1987* Restarting takes effect after execution.
1988*/
1989int mbtk_sms_cscb_set(mbtk_info_handle_t* handle, char * cscb)
1990{
1991 printf("mbtk_sms_cscb_set() cscb:%s, len:%d",cscb, strlen(cscb));
1992 return info_item_process(handle, MBTK_INFO_ID_SMS_CSCB_REQ, cscb, strlen(cscb), NULL) ? handle->info_err : 0;
1993}
1994
1995/*
1996* Set sms cnmi.
1997*
1998at+cnmi=1,2
1999
2000OK
2001if sending fails:
2002+CMS ERROR: <err>
2003*/
2004int mbtk_sms_cnmi_set(mbtk_info_handle_t* handle)
2005{
2006 printf("mbtk_sms_cnmi_set()------------start\n");
2007
2008 return info_item_process(handle, MBTK_INFO_ID_SMS_CNMI_REQ, NULL, 0, NULL)? handle->info_err : 0;
2009}
2010
2011/*
2012* Set sms cmss.
2013*
2014+CMSS=<index>[,<da>[,<toda>]]
2015
2016if sending successful:
2017+CMSS: <mr>
2018OK
2019if sending fails:
2020+CMS ERROR: <err>
2021*/
2022int mbtk_sms_cmss_set(mbtk_info_handle_t* handle, char * cmss, char *resp)
2023{
2024 printf("mbtk_sms_cmss_set()------------cmss:%s, len:%d", cmss, strlen(cmss));
2025 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMSS_REQ, cmss, strlen(cmss), resp) > 0){
2026 printf("resp:%s\n", resp);
2027 return 0;
2028 }else{
2029 return handle->info_err;
2030 }
2031}
2032
2033/*
2034* Return sms cmgf.
2035*/
2036int mbtk_sms_cpms_get(mbtk_info_handle_t* handle, char * mem)
2037{
2038 char req[128] = {0};
2039 if(info_item_process(handle, MBTK_INFO_ID_SMS_CPMS_REQ, NULL, 0, &req) > 0) {
2040 LOG("mbtk_sms_cpms_get() req : %s, strlen(mem_ptr):%d\n", req, strlen(req));
2041 memcpy(mem, req, strlen(req));
2042 return 0;
2043 } else {
2044 return handle->info_err;
2045 }
2046}
2047
2048
2049/*
2050* Set sms cpms.
2051*
2052* AT+CPMS=<mem1>[,<mem2>[,<mem3>]]
2053*
2054* Restarting takes effect after execution.
2055*/
2056int mbtk_sms_cpms_set(mbtk_info_handle_t* handle, char * mem, char* response)
2057{
2058 printf("mbtk_sms_cpms_set() mem:%s, len:%d",mem, strlen(mem));
2059 char req[128] = {0};
2060 if( info_item_process(handle, MBTK_INFO_ID_SMS_CPMS_REQ, mem, strlen(mem), req) > 0){
2061 memcpy(response, req, strlen(req));
2062 return 0;
2063 }
2064 else{
2065 return handle->info_err;
2066 }
2067}
2068
2069/*
2070* Set sms cm.
2071*
2072* +CMGR=<index>
2073
2074if PDU mode (+CMGF=0) ��command successful:
2075+CMGR: <stat>,[<alpha>],<length><CR><LF><pdu>
2076OK
2077if text mode (+CMGF=1), command successful and SMS-DELIVER:
2078+CMGR:<stat>,<oa>,[<alpha>],<scts>[,<tooa>,<fo>,<pid>,<dcs
2079>, <sca>,<tosca>,<length>]<CR><LF><data>
2080OK
2081if text mode (+CMGF=1), command successful and SMS-SUBMIT:
2082+CMGR:
2083<stat>,<da>,[<alpha>][,<toda>,<fo>,<pid>,<dcs>,[<vp>],
2084<sca>,<tosca>,<length>]<CR><LF><data>
2085OK
2086otherwise:
2087+CMS ERROR: <err>
2088*
2089* Restarting takes effect after execution.
2090*/
2091int mbtk_sms_cmgr_set(mbtk_info_handle_t* handle, int index, char *resp)
2092{
2093 printf("mbtk_sms_cmgr_set() --------------index:%d",index);
2094 if( info_item_process(handle, MBTK_INFO_ID_SMS_CMGR_REQ, (uint8*)&index, sizeof(uint8), resp) > 0){
2095 printf("resp:%s\n", resp);
2096 return 0;
2097 }else{
2098 return handle->info_err;
2099 }
2100}
2101
2102
2103/*
2104* Get sim state.
2105*/
2106int mbtk_sim_state_get(mbtk_info_handle_t* handle, mbtk_sim_state_enum *sim_state)
2107{
2108 uint8 state;
2109 if(handle == NULL || sim_state == NULL)
2110 {
2111 LOGE("ARG error.");
2112 return -1;
2113 }
2114 if(info_item_process(handle, MBTK_INFO_ID_SIM_STATE_REQ, NULL, 0, &state) > 0) {
2115 *sim_state = (mbtk_sim_state_enum)state;
2116 LOG("Sim state : %d", *sim_state);
2117 return 0;
2118 } else {
2119 return handle->info_err;
2120 }
2121}
2122
2123/*
2124* Get sim card type.
2125*/
2126int mbtk_sim_card_type_get(mbtk_info_handle_t* handle, mbtk_sim_card_type_enum *sim_card_type)
2127{
2128 uint8 state;
2129 if(handle == NULL || sim_card_type == NULL)
2130 {
2131 LOGE("ARG error.");
2132 return -1;
2133 }
2134 if(info_item_process(handle, MBTK_INFO_ID_SIM_STYPE_REQ, NULL, 0, &state) > 0) {
2135 *sim_card_type = (mbtk_sim_card_type_enum)state;
2136 LOG("Sim sim_card_type : %d", *sim_card_type);
2137 return 0;
2138 } else {
2139 return handle->info_err;
2140 }
2141}
2142
2143/*
2144* Get system temperature.
2145*
2146* type[IN]:
2147* 0: Soc temperature.
2148* 1: RF temperature.
2149* temp[OUT]:
2150* temperature in celsius.
2151*/
2152int mbtk_temp_get(mbtk_info_handle_t* handle, int type, int* temp)
2153{
2154 if(handle == NULL)
2155 {
2156 LOGE("ARG error.");
2157 return -1;
2158 }
2159 if(type != 0 && type != 1 || temp == NULL)
2160 {
2161 return -1;
2162 }
2163
2164 uint8 temp_type = (uint8)type;
2165 uint8 temp_ptr;
2166 if(info_item_process(handle, MBTK_INFO_ID_DEV_TEMP_REQ, &temp_type, sizeof(uint8), &temp_ptr) > 0) {
2167 *temp = temp_ptr;
2168 LOG("Temperature : %d", *temp);
2169 return 0;
2170 } else {
2171 return handle->info_err;
2172 }
2173
2174 return 0;
2175}
2176
2177
2178/*
2179* Set sim power state.
2180* power:
2181* 0: Sim power off.
2182* 1: Sim power on.
2183*/
2184int mbtk_sim_power_set(int power)
2185{
2186 if(power != 0 && power != 1)
2187 {
2188 return -1;
2189 }
2190
2191 // /sys/devices/virtual/usim_event/usim0/send_event
2192 char cmd[100] = {0};
2193 sprintf(cmd, "echo %d > /sys/devices/virtual/usim_event/usim0/send_event", power ? 0 : 1);
2194 system(cmd);
2195
2196 return 0;
2197}
2198
2199/*
2200* System power.
2201* type:
2202* 0: Reboot system.
2203* 1: Poweroff system.
2204* 2: Halt system.
2205*/
2206int mbtk_system_reboot(int type)
2207{
2208 if(type != 0 && type != 1 && type != 2)
2209 {
2210 return -1;
2211 }
2212
2213 switch(type) {
2214 case 0: {
2215 system("reboot");
2216 break;
2217 }
2218 case 1: {
2219 system("poweroff");
2220 break;
2221 }
2222 case 2: {
2223 system("halt");
2224 break;
2225 }
2226 default: {
2227 break;
2228 }
2229 }
2230
2231 return 0;
2232}
2233
2234/*
2235* set modem fun
2236*
2237*/
2238int mbtk_set_modem_fun(mbtk_info_handle_t* handle, mbtk_modem_info_t *info)
2239{
2240 if(handle == NULL)
2241 {
2242 LOGE("ARG error.");
2243 return -1;
2244 }
2245 return info_item_process(handle, MBTK_INFO_ID_DEV_MODEM_REQ, info, sizeof(mbtk_modem_info_t), NULL) ? handle->info_err : 0;
2246}
2247
2248/*
2249* get modem fun
2250*
2251*/
2252int mbtk_get_modem_fun(mbtk_info_handle_t* handle, int* fun)
2253{
2254 uint8 state;
2255 if(handle == NULL || fun == NULL)
2256 {
2257 LOGE("ARG error.");
2258 return -1;
2259 }
2260 if(info_item_process(handle, MBTK_INFO_ID_DEV_MODEM_REQ, NULL, 0, &state) > 0) {
2261 LOG("modem type : %d", state);
2262 *fun = state;
2263 return 0;
2264 } else {
2265 return handle->info_err;
2266 }
2267}
2268
2269/*
2270* call_start
2271*
2272*/
2273int mbtk_call_start(mbtk_info_handle_t* handle, char* phone_number)
2274{
2275 if(handle == NULL)
2276 {
2277 LOGE("ARG error.");
2278 return -1;
2279 }
2280 if(str_empty(phone_number))
2281 return -1;
2282
2283 return info_item_process(handle, MBTK_INFO_ID_CALL_START_REQ,
2284 phone_number, strlen(phone_number), NULL) ? handle->info_err : 0;
2285}
2286/*
2287* Answer the phone call.
2288*
2289*/
2290int mbtk_call_answer(mbtk_info_handle_t* handle)
2291{
2292 return info_item_process(handle, MBTK_INFO_ID_CALL_ANSWER_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2293}
2294
2295/*
2296* Hang up all call.
2297*
2298*/
2299int mbtk_call_hang(mbtk_info_handle_t* handle)
2300{
2301 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2302}
2303
2304/*
2305* Hang up a call.
2306*
2307*/
2308int mbtk_a_call_hang(mbtk_info_handle_t* handle, int phone_id)
2309{
2310 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_A_REQ, (uint8*)&phone_id, sizeof(uint8), NULL) ? handle->info_err : 0;
2311}
2312
2313/*
2314* Hang up waiting or background call.
2315*
2316*/
2317int mbtk_waiting_or_background_call_hang(mbtk_info_handle_t* handle)
2318{
2319 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_B_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2320}
2321
2322/*
2323* Hang up foreground resume background call.
2324*
2325*/
2326int mbtk_foreground_resume_background_call_hang(mbtk_info_handle_t* handle)
2327{
2328 return info_item_process(handle, MBTK_INFO_ID_CALL_HANGUP_C_REQ, NULL, 0, NULL) ? handle->info_err : 0;
2329}
2330
2331/*
2332* Get current call phone number.
2333*/
2334int mbtk_call_reg_get(mbtk_info_handle_t* handle, mbtk_call_info_t *reg)
2335{
2336 if(info_item_process(handle, MBTK_INFO_ID_CALL_WAITIN_REQ, NULL, 0, reg) > 0) {
2337 LOG("CLCC : %d, %d, %d, %d, %d, %s, %d", reg->dir1, reg->dir, reg->state, reg->mode, reg->mpty, reg->phone_number, reg->type);
2338 return 0;
2339 } else {
2340 return handle->info_err;
2341 }
2342}
2343
2344/*
2345* Return mute state.
2346*/
2347int mbtk_mute_state_get(mbtk_info_handle_t* handle, int *mute_state)
2348{
2349 uint8 state;
2350 if(info_item_process(handle, MBTK_INFO_ID_CALL_MUTE_REQ, NULL, 0, &state) > 0) {
2351 LOG("Mute State : %d", state);
2352 *mute_state = state;
2353 return 0;
2354 } else {
2355 return handle->info_err;
2356 }
2357}
2358
2359/*
2360* Set mute state.
2361*
2362* mute_state:
2363* 0 : of mute.
2364* 1 : on mute.
2365*
2366* Restarting takes effect after execution.
2367*/
2368int mbtk_mute_state_set(mbtk_info_handle_t* handle, int mute_state)
2369{
2370 return info_item_process(handle, MBTK_INFO_ID_CALL_MUTE_REQ, (uint8*)&mute_state, sizeof(uint8), NULL) ? handle->info_err : 0;
2371}
2372
2373/*
r.xiaoec113d12024-01-12 02:13:28 -08002374* Set wakeup state.
2375*
2376* wakeup_state:(0~31)
2377* 0 : means resume all
2378* 1~31 means suspend
2379* Control the active reporting of some platform modems to reduce wakeup
2380*/
2381
2382int mbtk_wakeup_state_set(mbtk_info_handle_t* handle, uint32 wakeup_state)
2383{
2384 return info_item_process(handle, MBTK_INFO_ID_WAKEUP_STA_REQ, (uint32*)&wakeup_state, sizeof(uint32), NULL) ? handle->info_err : 0;
2385}
2386
2387/*
2388* oos get.
2389*/
2390int mbtk_oos_get(mbtk_info_handle_t* handle, mbtk_oos_info *oos_info)
2391{
2392 if(info_item_process(handle, MBTK_INFO_ID_OOS_STA_REQ, NULL, 0, oos_info) > 0) {
2393 return 0;
2394 } else {
2395 return handle->info_err;
2396 }
2397}
2398
2399/*
2400* oos set .
2401*/
r.xiaocfd7c682024-01-22 03:59:46 -08002402int mbtk_oos_set(mbtk_info_handle_t* handle, mbtk_oos_info *oos_info)
r.xiaoec113d12024-01-12 02:13:28 -08002403{
2404 if(handle == NULL)
2405 {
2406 LOGE("ARG error.");
2407 return -1;
2408 }
r.xiaoec113d12024-01-12 02:13:28 -08002409
r.xiaocfd7c682024-01-22 03:59:46 -08002410 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 -08002411}
2412
2413
2414/*
liubin281ac462023-07-19 14:22:54 +08002415* Set DTMF character.
2416*
2417*/
2418int mbtk_dtmf_send(mbtk_info_handle_t* handle, mbtk_call_dtmf_info_t *dtmf_character)
2419{
2420 if(handle == NULL)
2421 {
2422 LOGE("ARG error.");
2423 return -1;
2424 }
2425 return info_item_process(handle, MBTK_INFO_ID_CALL_DTMF_REQ,
2426 dtmf_character, sizeof(mbtk_call_dtmf_info_t), NULL) ? handle->info_err : 0;
2427}
2428
2429/*
wangyouqiang38e53362024-01-23 10:53:48 +08002430* Set net led.
2431*
2432*/
2433int mbtk_led_set(mbtk_info_handle_t* handle, mbtk_led_type type, mbtk_led_status status)
2434{
2435 if(handle == NULL)
2436 {
2437 LOGE("ARG error.");
2438 return -1;
2439 }
2440
2441 char buff[3] = {0};
2442 if(type == MBTK_LED_TYPE_NET)
2443 {
2444 buff[0] = 0;
2445 }
2446 else
2447 {
2448 buff[0] = 1;
2449 }
2450
2451 if(status == MBTK_LED_STATUS_CLOSE)
2452 {
2453 buff[1] = 0;
2454 }
2455 else
2456 {
2457 buff[1] = 1;
2458 }
2459 return info_item_process(handle, MBTK_INFO_ID_LED_REQ, buff, 2, NULL) ? handle->info_err : 0;
2460}
2461
2462/*
liubin281ac462023-07-19 14:22:54 +08002463* Set pdp state change callback function.
2464*/
2465int mbtk_pdp_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2466{
2467 if(handle == NULL)
2468 {
2469 LOGE("ARG error.");
2470 return -1;
2471 }
2472 if(info_item_process(handle, MBTK_INFO_ID_IND_PDP_STATE_CHANGE, NULL, 0, NULL) < 0) {
2473 return handle->info_err;
2474 } else {
2475 handle->pdp_state_cb = cb;
2476 return 0;
2477 }
2478}
2479
2480/*
2481* Set network state change callback function.
2482*/
2483int mbtk_net_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2484{
2485 if(handle == NULL)
2486 {
2487 LOGE("ARG error.");
2488 return -1;
2489 }
b.liu27b91e42024-01-17 20:02:30 +08002490 if(info_item_process(handle, MBTK_INFO_ID_IND_NET_STATE_CHANGE, NULL, 0, NULL) < 0) {
2491 return handle->info_err;
2492 } else {
liubin281ac462023-07-19 14:22:54 +08002493 handle->net_state_cb = cb;
2494 return 0;
liubin281ac462023-07-19 14:22:54 +08002495 }
2496}
2497
2498/*
2499* Set call state change callback function.
2500*/
2501int mbtk_call_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2502{
2503 if(handle == NULL)
2504 {
2505 LOGE("ARG error.");
2506 return -1;
2507 }
2508 if(info_item_process(handle, MBTK_INFO_ID_IND_CALL_STATE_CHANGE, NULL, 0, NULL) < 0) {
2509 return handle->info_err;
2510 } else {
2511 handle->call_state_cb = cb;
2512 return 0;
2513 }
2514}
2515
2516/*
2517* Set sms state change callback function.
2518*/
2519int mbtk_sms_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2520{
2521 if(handle == NULL)
2522 {
2523 LOGE("ARG error.");
2524 return -1;
2525 }
2526 if(info_item_process(handle, MBTK_INFO_ID_IND_SMS_STATE_CHANGE, NULL, 0, NULL) < 0) {
2527 return handle->info_err;
2528 } else {
2529 handle->sms_state_cb = cb;
2530 return 0;
2531 }
2532}
2533
2534/*
2535* Set radio state change callback function.
2536*/
2537int mbtk_radio_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2538{
2539 if(handle == NULL)
2540 {
2541 LOGE("ARG error.");
2542 return -1;
2543 }
2544 if(info_item_process(handle, MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, NULL, 0, NULL) < 0) {
2545 return handle->info_err;
2546 } else {
2547 handle->radio_state_cb = cb;
2548 return 0;
2549 }
2550}
2551
2552/*
2553* Set sim state change callback function.
2554*/
2555int mbtk_sim_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2556{
2557 if(handle == NULL)
2558 {
2559 LOGE("ARG error.");
2560 return -1;
2561 }
2562 if(info_item_process(handle, MBTK_INFO_ID_IND_SIM_STATE_CHANGE, NULL, 0, NULL) < 0) {
2563 return handle->info_err;
2564 } else {
2565 handle->sim_state_cb = cb;
2566 return 0;
2567 }
2568}
r.xiaofca7c472024-04-24 01:00:23 -07002569
2570/*
2571* Set signal state change callback function.
2572*/
2573int mbtk_signal_state_change_cb_reg(mbtk_info_handle_t* handle, mbtk_info_callback_func cb)
2574{
2575 if(handle == NULL)
2576 {
2577 LOGE("ARG error.");
2578 return -1;
2579 }
2580 if(info_item_process(handle, MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, NULL, 0, NULL) < 0) {
2581 return handle->info_err;
2582 } else {
2583 handle->signal_state_cb = cb;
2584 return 0;
2585 }
2586}
2587