1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. \n
14 *
15 * Description: Provides AT message source \n
16 */
17
18 #include "at_base.h"
19 #include "at_channel.h"
20 #include "at_process.h"
21 #ifdef CONFIG_AT_SUPPORT_NOTIFY_REPORT
22 #include "at_notify.h"
23 #endif
24 #include "at_msg.h"
25
26 static unsigned long g_at_msg_queue;
27 #define AT_NO_WAIT 0x0
28 #define AT_WAIT_FOREVER 0xFFFFFFFF
29
at_msg_process(at_msg_block_t * msg)30 void at_msg_process(at_msg_block_t *msg)
31 {
32 switch (msg->type) {
33 case AT_CMD_MSG:
34 at_proc_cmd_handle(msg->sub_msg.cmd.channel_id);
35 break;
36 #ifdef CONFIG_AT_SUPPORT_ASYNCHRONOUS
37 case AT_CMD_RESULT_MSG:
38 at_proc_cmd_result_handle(msg->sub_msg.result.err_code);
39 break;
40 case AT_CMD_TIMEOUT_MSG:
41 at_proc_timeout_handle();
42 break;
43 case AT_CMD_INTERACTIVITY_MSG:
44 at_proc_interactivity_handle(msg->sub_msg.cmd.channel_id);
45 break;
46 #endif
47 #ifdef CONFIG_AT_SUPPORT_NOTIFY_REPORT
48 case AT_CMD_URC_REPORT_MSG:
49 at_proc_cmd_urc_handle();
50 break;
51 #endif
52 default:
53 break;
54 }
55 }
56
at_msg_send(at_msg_block_t * msg)57 errcode_t at_msg_send(at_msg_block_t *msg)
58 {
59 if (at_msg_queue_write(g_at_msg_queue, msg, sizeof(at_msg_block_t), AT_NO_WAIT) != 0) {
60 return ERRCODE_AT_MSG_SEND_ERROR;
61 }
62 return ERRCODE_SUCC;
63 }
64
65 #ifdef CONFIG_AT_SUPPORT_ASYNCHRONOUS
uapi_at_send_async_result(uint16_t err)66 errcode_t uapi_at_send_async_result(uint16_t err)
67 {
68 at_msg_block_t msg;
69 msg.type = AT_CMD_RESULT_MSG;
70 msg.sub_msg.result.err_code = err;
71
72 return at_msg_send(&msg);
73 }
74 #endif
75
uapi_at_msg_main(void * unused)76 void uapi_at_msg_main(void *unused)
77 {
78 unused(unused);
79 at_msg_block_t msg;
80 uint32_t buffer_size = sizeof(at_msg_block_t);
81
82 if (at_base_is_at_init() != true) {
83 return;
84 }
85
86 at_msg_queue_create(AT_MSG_MAX_NUM, sizeof(at_msg_block_t), &g_at_msg_queue);
87 #ifdef CONFIG_AT_SUPPORT_NOTIFY_REPORT
88 at_notify_init();
89 #endif
90 at_channel_check_and_enable();
91
92 for (;;) {
93 if (at_msg_queue_read(g_at_msg_queue, &msg, &buffer_size, AT_WAIT_FOREVER) == 0) {
94 at_msg_process(&msg);
95 }
96 (void)at_yield();
97 #ifdef UT_TEST
98 break;
99 #endif
100 }
101 }
102