1 /*
2 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
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.
14 */
15
16 /**
17 ****************************************************************************************
18 *
19 * @file sonata_utils_api.h
20 *
21 * @brief header file - asr utilities
22 *
23 ****************************************************************************************
24 */
25
26 #ifndef _SONATA_UTILS_API_H_
27 #define _SONATA_UTILS_API_H_
28
29 /*
30 * INCLUDE FILES
31 ****************************************************************************************
32 */
33 #include "sonata_error_api.h"
34 #include "sonata_ble_hook.h"
35 #include "compiler.h" // for __INLINE
36 #include "stdbool.h"
37
38 /**
39 * @defgroup SONATA_UTILS_API UTILS_API
40 * @{
41 */
42
43 /*
44 * MACRO DEFINITIONS
45 ****************************************************************************************
46 */
47 /// maxium supported messages from application
48 #define SONATA_API_MAX_APP_MSG 32
49
50 /// debug trace
51 #ifdef SONATA_UTILS_API_DBG
52 #define SONATA_UTILS_API_TRC printf
53 #else
54 #define SONATA_UTILS_API_TRC(...)
55 #endif // SONATA_API_TASK_DBG
56
57 /// file system first tag id for application
58 #define APP_DATA_SAVE_TAG_FIRST (0x90)
59 /// file system last tag id for application
60 #define APP_DATA_SAVE_TAG_LAST (0xAF)
61 /// file system bluetooth device address tag id
62 #define SONATA_FS_TAG_BD_ADDR (0x01)
63 /// file system bluetooth device address tag length
64 #define SONATA_FS_TAG_BD_ADDR_LEN 6
65
66 #define PWR_ON_RST 0x00
67 #define HARDWARE_PIN_RST 0x01
68 #define SOFTWARE_RST 0x02
69 #define UNKNOWN_RST 0xFF
70
71 /*
72 * ENUM DEFINITIONS
73 ****************************************************************************************
74 */
75
76 /*!
77 * @brief app msg operation type
78 */
79 typedef enum {
80 /// at command operation
81 APP_MSG_AT_CMD = (1 << 0),
82 APP_MSG_UART_CMD = (5),
83
84 APP_MSG_HID_TIMER = (10),
85
86 APP_MSG_LAST,
87 } app_msg_op_t;
88
89 /*!
90 * @brief file system possible return status
91 */
92 typedef enum {
93 /// SONATA FILE SYSTEM status OK
94 SONATA_FS_OK,
95 /// generic SONATA FILE SYSTEM status KO
96 SONATA_FS_FAIL,
97 /// SONATA FILE SYSTEM TAG unrecognized
98 SONATA_FS_TAG_NOT_DEFINED,
99 /// No space for SONATA FILE SYSTEM
100 SONATA_FS_NO_SPACE_AVAILABLE,
101 /// Length violation
102 SONATA_FS_LENGTH_OUT_OF_RANGE,
103 /// SONATA FILE SYSTEM parameter locked
104 SONATA_FS_PARAM_LOCKED,
105 /// SONATA FILE SYSTEM corrupted
106 SONATA_FS_CORRUPT,
107 /// SONATA FILE TYPE OUT RANGE
108 SONATA_FS_OT_RANGE,
109 } SONATA_FS_STATUS;
110 typedef uint8_t sonata_fs_len_t;
111 typedef uint8_t sonata_fs_tag_t;
112
113 /*!
114 * @brief ble addr priority return result
115 */
116 typedef enum {
117 /// efuse > nvds
118 SONATA_MAC_USE_EFUSE,
119 /// nvds > efuse
120 SONATA_MAC_USE_NVDS,
121 } SONATA_MAC_PRIORITY;
122
123 /*
124 * Type Definition
125 ****************************************************************************************
126 */
127 /// application message handler
128 typedef uint8_t (*PF_SONATA_API_APP_MSG_HANDLER)(void *p_param);
129
130 /// asr api task messages
131 typedef struct sonata_api_app_msg {
132 uint8_t operation;
133 PF_SONATA_API_APP_MSG_HANDLER function;
134 } sonata_api_app_msg_t;
135
136 /// asr api task message array
137 typedef struct sonata_api_app_msg_array {
138 int msg_num;
139 sonata_api_app_msg_t *msg[SONATA_API_MAX_APP_MSG];
140 } sonata_api_app_msg_array_t;
141
142 /// asr api task message array
143 typedef struct sonata_api_app_ke_msg {
144 uint8_t operation;
145 void *p_param;
146 } sonata_api_app_ke_msg_t;
147
148 /// Ble bt addr priority callback
149 typedef struct {
150 SONATA_MAC_PRIORITY (*ble_addr_priority)(void);
151
152 } ble_addr_callback_t;
153 /**
154 ****************************************************************************************
155 * @brief Call back definition of the function that can handle result of an AES based algorithm
156 *
157 * @param[in] status Execution status
158 * @param[in] aes_res 16 bytes block result
159 * @param[in] src_info Information provided by requester
160 ****************************************************************************************
161 */
162 typedef void (*aes_func_result_cb) (uint8_t status, const uint8_t *aes_res, uint32_t src_info);
163
164 /*
165 * FUNCTION DECLARATIONS
166 ****************************************************************************************
167 */
168
169 /**
170 ***************************************************************************************
171 * @brief asr api send msg from app
172 * @param[in] uint8_t op : operation code
173 * [in] void *p_param
174 *
175 * @return uint16_t : api result
176 ***************************************************************************************
177 */
178 uint16_t sonata_api_send_app_msg(uint8_t op, void *p_param);
179
180 /**
181 ***************************************************************************************
182 * @brief asr api register message
183 * @param[in] const sonata_api_msg_t *cmd
184 *
185 *
186 * @return uint16_t : api result
187 ***************************************************************************************
188 */
189 uint16_t sonata_api_app_msg_register(const sonata_api_app_msg_t *msg);
190
191 /// @hide
192 uint8_t sonata_api_app_ke_msg_handler(void *p_param);
193
194 /// @hide
195 void sonata_api_util_gap_status_print(uint8_t status);
196
197 /// @hide
198 void sonata_api_util_gatt_status_print(uint8_t operation, uint8_t status);
199
200 /**
201 ***************************************************************************************
202 * @brief asr api create timer
203 * @param[in] uint8_t timer_id : timer id, should be uniquely in global
204 * @param[in] uint32_t delay : delay time
205 *
206 * @return uint16_t : api result
207 ***************************************************************************************
208 */
209 uint16_t sonata_api_app_timer_set(uint8_t timer_id, uint32_t delay);
210
211 /**
212 ***************************************************************************************
213 * @brief asr api clear timer
214 * @param[in] uint8_t timer_id : timer id, should be uniquely in global
215 *
216 *
217 * @return uint16_t : api result
218 ***************************************************************************************
219 */
220 void sonata_api_app_timer_clear(uint8_t timer_id);
221
222 /**
223 ***************************************************************************************
224 * @brief asr api to judge time active status
225 * @param[in] uint8_t timer_id : timer id, should be uniquely in global
226 *
227 *
228 * @return bool : true(active) or false(inactive)
229 ***************************************************************************************
230 */
231 bool sonata_api_app_timer_active(uint8_t timer_id);
232
233 /*****************************************************************************************
234
235 * @brief sonata malloc dynamic memory
236 * @param[in] uint16_t size : memory size
237 *
238 *
239 * @return void * : NULL or memory addr
240 ****************************************************************************************
241 */
242 void *sonata_api_malloc(uint16_t size);
243
244 /**
245 ***************************************************************************************
246 * @brief free memory alloced by sonata_api_malloc
247 * @param[in] void *p_ptr : memory addr
248 *
249 *
250 * @return void
251 ****************************************************************************************
252 */
253 void sonata_api_free(void *p_ptr);
254
255 /**
256 ***************************************************************************************
257 * @brief sonata file system write to flash
258 * @param[in] sonata_fs_tag_t tag : tag
259 * @param[in] sonata_fs_len_t length : write len
260 * @param[in] uint8_t *buf : write buf
261 * @note for example: write bluetooth device address
262 * @note sonata_fs_write(SONATA_FS_TAG_BD_ADDR,SONATA_FS_TAG_BD_ADDR_LEN,buf);
263 *
264 * @return uint8_t @see SONATA_FS_STATUS
265 ****************************************************************************************
266 */
267 uint8_t sonata_fs_write(sonata_fs_tag_t tag, sonata_fs_len_t length, uint8_t *buf);
268
269 /**
270 ***************************************************************************************
271 * @brief sonata file system read flash
272 * @param[in] sonata_fs_tag_t tag : tag
273 * @param[in] sonata_fs_len_t * lengthPtr: read length
274 * @param[in] uint8_t *buf : read buf
275 * @note for example: read bluetooth device address
276 * @note sonata_fs_len_t length = SONATA_FS_TAG_BD_ADDR_LEN;
277 * @note sonata_fs_read(SONATA_FS_TAG_BD_ADDR,&length,buf);
278 *
279 * @return uint8_t @see SONATA_FS_STATUS
280 ****************************************************************************************
281 */
282 uint8_t sonata_fs_read(sonata_fs_tag_t tag, sonata_fs_len_t *lengthPtr, uint8_t *buf);
283
284 /**
285 ***************************************************************************************
286 * @brief sonata file system erase flash
287 * @param[in] sonata_fs_tag_t tag : tag
288 *
289 *
290 * @return uint8_t @see SONATA_FS_STATUS
291 ****************************************************************************************
292 */
293 uint8_t sonata_fs_erase(sonata_fs_tag_t tag);
294
295 /*!
296 * @brief get BT address
297 * @return address
298 */
299 uint8_t *sonata_get_bt_address(void);
300
301 /**
302 ***************************************************************************************
303 * @brief sonata set bd addr
304 * @param[in] uint8_t* bd_addr : bd_addr value
305 * uint8_t length: addr length
306 *
307 *
308 *
309 * @return void
310 ****************************************************************************************
311 */
312 void sonata_set_bt_address(uint8_t *bd_addr, uint8_t length);
313
314 /**
315 ***************************************************************************************
316 * @brief sonata set bd addr but no save
317 * @param[in] uint8_t* bd_addr : bd_addr value
318 * uint8_t length: addr length
319 *
320 *
321 *
322 * @return void
323 ****************************************************************************************
324 */
325 void sonata_set_bt_address_no_save(uint8_t *bd_addr, uint8_t length);
326
327 /**
328 ****************************************************************************************
329 * @brief Perform an AES encryption form app - result within callback
330 * @param[in] key Key used for the encryption
331 * @param[in] val Value to encrypt using AES
332 * @param[in] res_cb Function that will handle the AES based result (16 bytes)
333 * @param[in] src_info Information used retrieve requester
334 *
335 * @return bool : true for encryption is ongoing, false for some error
336 ****************************************************************************************
337 */
338 bool sonata_aes_app_encrypt(uint8_t *key, uint8_t *val, aes_func_result_cb res_cb, uint32_t src_info);
339
340 /**
341 ****************************************************************************************
342 * @brief Perform an AES decryption form app - result within callback
343 * @param[in] key Key used for the decryption
344 * @param[in] val Value to decrypt using AES
345 * @param[in] res_cb Function that will handle the AES based result (16 bytes)
346 * @param[in] src_info Information used retrieve requester
347 *
348 * @return bool : true for decryption is ongoing, false for some error
349 ****************************************************************************************
350 */
351 bool sonata_aes_app_decrypt(uint8_t *key, uint8_t *val, aes_func_result_cb res_cb, uint32_t src_info);
352
353 /*!
354 * @brief change pin code to byte
355 * @param pin_code
356 * @param bytes
357 * @param byte_len
358 */
359 void sonata_passkey_pincode_to_byte(uint32_t pin_code, uint8_t *bytes, uint8_t byte_len);
360
361 /*!
362 * @brief change byte to pin code
363 * @param bytes
364 * @return
365 */
366 uint32_t sonata_passkey_byte_to_pincode(uint8_t *bytes);
367
368 /*!
369 * @brief Get information form advertising report
370 * @param type [in] GAP advertising flags, for example GAP_AD_TYPE_COMPLETE_NAME is the device's name in advertising report.
371 * @param info [in] save return value
372 * @param info_length [in] length of the info buffer
373 * @param report [in] advertising report data
374 * @param report_length [in] advertising report data length
375 * @param rel_length [out] for target information
376 * @return TRUE for target fond, FALSE for not found
377 */
378 bool sonata_get_adv_report_info(uint8_t type, uint8_t *info, uint16_t info_length, uint8_t *report,
379 uint16_t report_length, uint16_t *rel_length);
380
381 /*!
382 * @brief Get system tick value
383 * @return System tick
384 */
385 uint32_t sonata_get_sys_time(void);
386
387 /*!
388 * @brief Reset BLE stack
389 * @return
390 */
391 void sonata_ble_stack_reset(void);
392
393 /*!
394 * @brief Get boot type value
395 * @note in low power mode, if use this API should enable the peri_clk of RET_HCLK_EN
396 * @return 0x00: PWR_ON_RST, 0x01: HARDWARE_PIN_RST, 0x02: SOFTWARE_RST, 0xFF: UNKNOWN_RST
397 */
398 uint32_t sonata_get_boot_rst_type(void);
399
400 /*!
401 * @brief Get sonata rom version
402 * @return rom version
403 */
404 const char *sonata_get_rom_version(void);
405
406 /*!
407 * @brief Ble addr priority callback
408 */
409 void sonata_ble_register_bt_addr_callback(ble_addr_callback_t *cb);
410
411 extern int rand (void);
412
util_rand_byte(void)413 __INLINE uint8_t util_rand_byte(void)
414 {
415 return (uint8_t)(rand() & 0xFF);
416 }
util_rand_word(void)417 __INLINE uint32_t util_rand_word(void)
418 {
419 return (uint32_t)rand();
420 }
421
util_min(uint32_t a,uint32_t b)422 __INLINE uint32_t util_min(uint32_t a, uint32_t b)
423 {
424 return a < b ? a : b;
425 }
426
util_write16p(void const * ptr16,uint16_t value)427 __INLINE void util_write16p(void const *ptr16, uint16_t value)
428 {
429 uint8_t *ptr = (uint8_t *)ptr16;
430
431 *ptr++ = value & 0xff;
432 *ptr = (value & 0xff00) >> 8;
433 }
434
util_read16p(void const * ptr16)435 __INLINE uint16_t util_read16p(void const *ptr16)
436 {
437 uint16_t value = (((uint8_t *)ptr16)[0]) | (((uint8_t *)ptr16)[1] << 8);
438 return value;
439 }
440
util_read32p(void const * ptr32)441 __INLINE uint32_t util_read32p(void const *ptr32)
442 {
443 uint16_t addr_l, addr_h;
444 addr_l = util_read16p((uint16_t *)ptr32);
445 addr_h = util_read16p((uint16_t *)ptr32 + 1);
446 return (((uint32_t)addr_l) | (((uint32_t)addr_h) << 16));
447 }
448
449 void sonata_platform_reset(void);
450
451 /** @}*/
452 #endif // _SONATA_UTILS_API_H_
453
454