Airoha M0 BLE API  1.0.5.4
sm_hal.h
1 /******************************************************************************
2 Copyright (c) Airoha 2016 - All rights reserved
3 
4 FILE NAME
5  sm_hal.h
6 DESCRIPTION
7 NOTES
8 ********************************************************************************/
9 #ifndef __SM_HAL_H__
10 #define __SM_HAL_H__
11 
12 #define SM_HAL_AB1122_ENABLED (0)
13 #define SM_HAL_AB1600_ENABLED (1)
14 
15 #define SM_MASTER_SUPPORTED (1)
16 #define SM_SECURE_CONNECTION_SUPPORTED (1)
17 
18 /*********************************************************************************************************
19 * SM_HAL_AB1600_ENABLED
20 *********************************************************************************************************/
21 #include <stdint.h>
22 #include "ble_gap.h"
23 #include "ble_gap_links.h"
24 //-----------------------------------------------------------------------/
25 // Common
26 //-----------------------------------------------------------------------/
27 #include "Drv_Trng.h"
28 #include "Ab_MMITimer.h"
29 
30 //-----------------------------------------------------------------------/
31 // Sm_protocol.c
32 //-----------------------------------------------------------------------/
33 #include "ble_l2cap.h"
34 
35 //-----------------------------------------------------------------------/
36 // sm_cryptotool.c
37 //-----------------------------------------------------------------------/
38 #include "hci_cmd_evt_dat.h"
39 
40 //-----------------------------------------------------------------------/
41 // Flash data
42 //-----------------------------------------------------------------------/
43 #include "ab_flash.h"
44 
45 //-----------------------------------------------------------------------/
46 // sm_slave.c
47 //-----------------------------------------------------------------------/
48 #include "bt.h"
49 
50 /*********************************************************************************************************
51 * Type & Macro Definition
52 *********************************************************************************************************/
53 
54 //-----------------------------------------------------------------------/
55 // Common
56 //-----------------------------------------------------------------------/
57 #ifndef PACKED_STRUCT
58  #define PACKED_STRUCT __attribute__((packed))
59 #endif
60 
61 #define SM_INVALID_INDEX 0xFF
62 
63 #define SM_FLASH_INIT_FLAG 0xAA
64 #define SM_FLASH_SPACE_AVAILABLE 0xFF
65 #define SM_FLASH_START_TO_WRITE 0xCF
66 #define SM_FLASH_VALID_DATA 0xF
67 #define SM_FLASH_INVALID_DATA 0x0
68 
69 #if (SM_SECURE_CONNECTION_SUPPORTED)
70 #define SM_PROTOCOL_PAYLOAD_MAX_LEN 65
71 #define SM_PROTOCOL_PARAM_MAX_LEN 64
72 #else
73 #define SM_PROTOCOL_PAYLOAD_MAX_LEN 24
74 #define SM_PROTOCOL_PARAM_MAX_LEN 23
75 #endif
76 
77 #define SM_HAL_DEFAULT_TIMEOUT 30000
78 
79 #define sm_hal_malloc(x) malloc(x)
80 #define sm_hal_mfree(x) free(x)
81 #define sm_hal_memset(x, y, z) memset((uint8_t *)x, y, z)
82 #define sm_hal_get_role(x) ble_gap_link_GetAttr_Role(x)
83 #define sm_hal_get_idx_byHandle(x) ble_gap_link_GetIdx_ByHandle(x)
84 #define sm_hal_get_handle_byIdx(x) ble_gap_link_GetAttr_Handle(x)
85 #define sm_hal_get_peer_addr(x) ble_gap_link_GetAttr_BdAddr(x)
86 #define sm_hal_get_peer_addr_type(x) ble_gap_link_GetAttr_BdAddrType(x)
87 
88 //-----------------------------------------------------------------------/
89 // Sm_protocol.c
90 //-----------------------------------------------------------------------/
91 typedef union
92 {
93  struct
94  {
95  uint8_t cmd;
96  uint8_t param[SM_PROTOCOL_PARAM_MAX_LEN];
97  }packet;
98  uint8_t payload[SM_PROTOCOL_PAYLOAD_MAX_LEN];
99 }sm_protocol_data_t;
100 
101 typedef struct
102 {
103  sm_protocol_data_t pdu;
104 }sm_hal_l2cap_tx_pkt_t;
105 
106 //-----------------------------------------------------------------------/
107 // sm_cryptotool.c
108 //-----------------------------------------------------------------------/
109 typedef void (*EncryptionCB)(uint8_t *encData, uint8_t status, uint8_t idx);
110 
111 typedef struct
112 {
113  EncryptionCB cb;
114  uint8_t linkidx;
115 }sm_cryptotool_cb_t;
116 
117 //-----------------------------------------------------------------------/
118 // sm_slave.c
119 //-----------------------------------------------------------------------/
120 
121 //-----------------------------------------------------------------------/
122 // sm_master.c
123 //-----------------------------------------------------------------------/
124 
125 //-----------------------------------------------------------------------/
126 // sm.c
127 //-----------------------------------------------------------------------/
128 
129 //-----------------------------------------------------------------------/
130 // Flash data
131 //-----------------------------------------------------------------------/
132 typedef void (*FlashDataCB)(uint8_t *flashData, uint8_t status, uint8_t idx);
133 
134 /*********************************************************************************************************
135 * prototype
136 *********************************************************************************************************/
137 
138 //-----------------------------------------------------------------------/
139 // Common
140 //-----------------------------------------------------------------------/
141 //Get link information
142 uint8_t *sm_hal_get_local_addr(uint8_t idx);
143 uint8_t sm_hal_get_local_addr_Type(uint8_t idx);
144 //Set link status
145 void sm_hal_set_link_authenticated(uint8_t idx, uint8_t auth);
146 void sm_hal_set_link_encrypted(uint8_t idx, uint8_t enc);
147 //Get random number
148 void sm_hal_get_random_number(uint8_t *ptr, uint8_t len);
149 //Timer
150 void sm_hal_set_timer(uint8_t idx, uint32_t ms);
151 void sm_hal_release_timer(uint8_t idx);
152 //infom app
153 void sm_hal_infom_set_sec_param_req(uint8_t idx);
154 void sm_hal_infom_passkey_display_req(uint8_t idx, uint8_t matchReq, uint8_t *ltk);
155 void sm_hal_infom_passkey_input_req(uint8_t idx, uint8_t keyType);
156 void sm_hal_infom_recv_security_req(uint8_t idx, uint8_t authReq);
157 void sm_hal_infom_conn_sec_update(uint8_t idx, uint8_t lv, uint8_t status);
158 void sm_hal_infom_auth_status(uint8_t idx, uint8_t status);
159 
160 //-----------------------------------------------------------------------/
161 // Sm_protocol.c
162 //-----------------------------------------------------------------------/
163 sm_hal_l2cap_tx_pkt_t *sm_hal_protocol_cmd_factory(uint8_t cmdCode, uint8_t cmdLen);
164 void sm_hal_protocol_send_l2cap_packet(uint8_t idx, sm_hal_l2cap_tx_pkt_t *smPtr, uint16_t len);
165 
166 //-----------------------------------------------------------------------/
167 // sm_cryptotool.c
168 //-----------------------------------------------------------------------/
169 void sm_hal_cryptoTool_hci_send_cmd(uint16_t opcode, uint8_t *parm, uint8_t len);
170 void sm_hal_cryptoTool_hci_send_cmd_ex(uint16_t opcode, uint8_t *parm, uint8_t len, sm_cryptotool_cb_t *cb);
171 
172 //-----------------------------------------------------------------------/
173 // sm_slave.c
174 //-----------------------------------------------------------------------/
175 
176 //-----------------------------------------------------------------------/
177 // sm_master.c
178 //-----------------------------------------------------------------------/
179 
180 //-----------------------------------------------------------------------/
181 // sm.c
182 //-----------------------------------------------------------------------/
183 
184 //-----------------------------------------------------------------------/
185 // Flash data
186 //-----------------------------------------------------------------------/
187 void sm_hal_flash_get_ltk(uint8_t *ediv, uint8_t *rand, uint8_t idx, FlashDataCB cb);
188 bool sm_hal_flash_get_last_paired_device(ble_addr_t *deviceAddr);
189 bool sm_hal_flash_isDevice_bonded(ble_addr_t *addr, uint8_t *idx);
190 void sm_hal_flash_delete_paired_record(ble_addr_t *addr);
191 bool sm_hal_flash_save_data(uint8_t idx);
192 void sm_hal_flash_init_data(void);
193 bool sm_hal_flash_set_passkey(uint32_t passkey);
194 void sm_hal_flash_get_passkey(uint8_t *passkey);
195 uint8_t sm_hal_verify_resolvable_addr(uint8_t *pAddr, uint8_t flashIdx, void (*cb)(bool, uint8_t));
196 
197 //-----------------------------------------------------------------------/
198 // sm_hal
199 //-----------------------------------------------------------------------/
200 void sm_hal_init(void);
201 void sm_hal_cryptoTool_init(void);
202 
203 
204 #endif
BLE address structure.
Definition: ble_gap.h:460