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 base source \n
16 */
17
18 #include "at_base.h"
19
20 static at_base_api_t g_at_base_api = {0};
21 static bool g_at_init_status = false;
22 #ifdef CONFIG_AT_SUPPORT_ASYNCHRONOUS
23 static void *g_at_timer_handler = NULL;
24 #endif
25
at_base_toupper(char * str,uint32_t len)26 void at_base_toupper(char *str, uint32_t len)
27 {
28 char *ch = str;
29 for (uint32_t index = 0; index < len; index++) {
30 if (*ch <= 'z' && *ch >= 'a') {
31 *ch = (char)((uint8_t)*ch - (uint8_t)'a' + (uint8_t)'A');
32 }
33 ch++;
34 }
35 }
36
at_base_api_register_base_api_check(at_base_api_t base_api)37 static errcode_t at_base_api_register_base_api_check(at_base_api_t base_api)
38 {
39 if ((base_api.msg_queue_create_func == NULL) ||
40 (base_api.msg_queue_read_func == NULL) ||
41 (base_api.msg_queue_write_func == NULL) ||
42 (base_api.malloc_func == NULL) ||
43 (base_api.free_func == NULL) ||
44 (base_api.task_pause_func == NULL)) {
45 return ERRCODE_INVALID_PARAM;
46 }
47
48 #ifdef CONFIG_AT_SUPPORT_CMD_ATTR
49 if (base_api.cmd_attr_func == NULL) {
50 return ERRCODE_INVALID_PARAM;
51 }
52 #endif
53 return ERRCODE_SUCC;
54 }
55
56 #ifdef CONFIG_AT_SUPPORT_ASYNCHRONOUS
at_base_api_register_time_api_check(at_base_api_t * base_api)57 static errcode_t at_base_api_register_time_api_check(at_base_api_t *base_api)
58 {
59 if ((base_api->timer_create_func == NULL) ||
60 (base_api->timer_delete_func == NULL) ||
61 (base_api->timer_start_func == NULL)) {
62 return ERRCODE_INVALID_PARAM;
63 }
64 return ERRCODE_SUCC;
65 }
66 #endif
67
68 #ifdef CONFIG_AT_SUPPORT_NOTIFY_REPORT
at_base_api_register_notify_api_check(at_base_api_t * base_api)69 static errcode_t at_base_api_register_notify_api_check(at_base_api_t *base_api)
70 {
71 if ((base_api->create_mutex_func == NULL) ||
72 (base_api->acquire_mutex_func == NULL) ||
73 (base_api->release_mutex_func == NULL)) {
74 return ERRCODE_INVALID_PARAM;
75 }
76 return ERRCODE_SUCC;
77 }
78 #endif
79
uapi_at_base_api_register(at_base_api_t base_api)80 errcode_t uapi_at_base_api_register(at_base_api_t base_api)
81 {
82 errcode_t err = ERRCODE_SUCC;
83 err = at_base_api_register_base_api_check(base_api);
84 if (err != ERRCODE_SUCC) {
85 return err;
86 }
87
88 #ifdef CONFIG_AT_SUPPORT_ASYNCHRONOUS
89 err = at_base_api_register_time_api_check(&base_api);
90 if (err != ERRCODE_SUCC) {
91 return err;
92 }
93 #endif
94
95 #ifdef CONFIG_AT_SUPPORT_NOTIFY_REPORT
96 err = at_base_api_register_notify_api_check(&base_api);
97 if (err != ERRCODE_SUCC) {
98 return err;
99 }
100 #endif
101
102 if (memcpy_s(&g_at_base_api, sizeof(at_base_api_t), &base_api, sizeof(at_base_api_t)) != EOK) {
103 return ERRCODE_MEMCPY;
104 }
105 g_at_init_status = true;
106 return ERRCODE_SUCC;
107 }
108
at_base_is_at_init(void)109 bool at_base_is_at_init(void)
110 {
111 return g_at_init_status;
112 }
113
at_malloc(uint32_t size)114 void* at_malloc(uint32_t size)
115 {
116 if (g_at_base_api.malloc_func != NULL) {
117 return g_at_base_api.malloc_func(size);
118 }
119 return NULL;
120 }
121
at_free(void * addr)122 void at_free(void *addr)
123 {
124 if (g_at_base_api.free_func != NULL) {
125 g_at_base_api.free_func(addr);
126 }
127 return;
128 }
129
at_msg_queue_create(uint32_t msg_count,uint32_t msg_size,unsigned long * queue_id)130 void at_msg_queue_create(uint32_t msg_count, uint32_t msg_size, unsigned long *queue_id)
131 {
132 if (g_at_base_api.msg_queue_create_func != NULL) {
133 g_at_base_api.msg_queue_create_func(msg_count, msg_size, queue_id);
134 }
135 return;
136 }
137
at_msg_queue_write(unsigned long queue_id,void * msg_ptr,uint32_t msg_size,uint32_t timeout)138 uint32_t at_msg_queue_write(unsigned long queue_id, void *msg_ptr, uint32_t msg_size, uint32_t timeout)
139 {
140 if (g_at_base_api.msg_queue_write_func != NULL) {
141 return g_at_base_api.msg_queue_write_func(queue_id, msg_ptr, msg_size, timeout);
142 }
143 return 0;
144 }
145
at_msg_queue_read(unsigned long queue_id,void * buf_ptr,uint32_t * buf_size,uint32_t timeout)146 uint32_t at_msg_queue_read(unsigned long queue_id, void *buf_ptr, uint32_t *buf_size, uint32_t timeout)
147 {
148 if (g_at_base_api.msg_queue_read_func != NULL) {
149 return g_at_base_api.msg_queue_read_func(queue_id, buf_ptr, buf_size, timeout);
150 }
151 return 0;
152 }
153
at_yield(void)154 void at_yield(void)
155 {
156 if (g_at_base_api.task_pause_func != NULL) {
157 g_at_base_api.task_pause_func();
158 }
159 return;
160 }
161
at_log(const char * buf,uint16_t buf_size,uint8_t level)162 void at_log(const char *buf, uint16_t buf_size, uint8_t level)
163 {
164 #ifndef CONFIG_AT_SUPPORT_LOG
165 unused(buf);
166 unused(buf_size);
167 unused(level);
168 #else
169 if (g_at_base_api.log_func != NULL) {
170 g_at_base_api.log_func(buf, buf_size, level);
171 }
172 return;
173 #endif
174 }
175
at_cmd_attr(uint16_t attr)176 bool at_cmd_attr(uint16_t attr)
177 {
178 #ifdef CONFIG_AT_SUPPORT_CMD_ATTR
179 if (g_at_base_api.cmd_attr_func != NULL) {
180 return g_at_base_api.cmd_attr_func(attr);
181 }
182 return true;
183 #else
184 unused(attr);
185 return true;
186 #endif
187 }
188
189 #ifdef CONFIG_AT_SUPPORT_ASYNCHRONOUS
at_timer_delete(void)190 void at_timer_delete(void)
191 {
192 if ((g_at_timer_handler != NULL) && (g_at_base_api.timer_delete_func != NULL)) {
193 g_at_base_api.timer_delete_func(g_at_timer_handler);
194 g_at_timer_handler = NULL;
195 }
196 return;
197 }
198
at_timer_start(uint32_t time_us,at_timer_callback_func_t call_back,void * argument)199 void at_timer_start(uint32_t time_us, at_timer_callback_func_t call_back, void *argument)
200 {
201 at_timer_delete();
202 if ((g_at_base_api.timer_create_func != NULL) && (g_at_base_api.timer_start_func != NULL)) {
203 g_at_timer_handler = g_at_base_api.timer_create_func(call_back, argument);
204 g_at_base_api.timer_start_func(g_at_timer_handler, time_us);
205 }
206 return;
207 }
208 #endif
209
210 #ifdef CONFIG_AT_SUPPORT_NOTIFY_REPORT
at_mutex_create(void)211 void* at_mutex_create(void)
212 {
213 if (g_at_base_api.create_mutex_func != NULL) {
214 return g_at_base_api.create_mutex_func();
215 }
216 return NULL;
217 }
218
at_mutex_acquire(void * handle)219 void at_mutex_acquire(void *handle)
220 {
221 if (g_at_base_api.acquire_mutex_func != NULL) {
222 g_at_base_api.acquire_mutex_func(handle);
223 }
224 return;
225 }
226
at_mutex_release(void * handle)227 void at_mutex_release(void *handle)
228 {
229 if (g_at_base_api.release_mutex_func != NULL) {
230 g_at_base_api.release_mutex_func(handle);
231 }
232 return;
233 }
234 #endif
235