• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #ifndef SMP_STORAGE_H_
19 #define SMP_STORAGE_H_
20 
21 /*
22  *  Address resolution is not supported by default.
23  *  After pairing and binding, we need to obtain the central Address Resolution
24  *  feature value of the opposite end to determine whether the opposite end supports the address resolution function,
25  *  and write
26  *  the result to smp_bonding_flg. Currently, we leave it to the user to obtain this feature.
27  */
28 #define IS_PEER_ADDR_RES_SUPPORT(peerAddrResSuppFlg) (!(peerAddrResSuppFlg & BIT(7)))
29 
30 typedef enum {
31     Index_Update_by_Pairing_Order = 0,  // default value
32     Index_Update_by_Connect_Order = 1,
33 } index_updateMethod_t;
34 
35 /*
36  * smp parameter need save to flash.
37  */
38 typedef struct {
39     // 0x00
40     u8 flag;
41     u8 role_dev_idx;  // [7]:1 for master, 0 for slave;   [2:0] slave device index
42 
43     // peer_addr_type&peer_addr must be together(SiHui 20200916), cause using flash read packed "type&address" in code
44     u8 peer_addr_type;  // address used in link layer connection
45     u8 peer_addr[6];
46 
47     u8 peer_id_adrType;  // peer identity address information in key distribution, used to identify
48     u8 peer_id_addr[6];
49 
50     // 0x10
51     u8 local_peer_ltk[16];  // slave: local_ltk; master: peer_ltk
52 
53     // 0x20
54     u8 encryt_key_size;
55     u8 local_id_adrType;
56     u8 local_id_addr[6];
57 
58     u8 random[8];  // 8
59 
60     // 0x30
61     u8 peer_irk[16];
62 
63     // 0x40
64     u8 local_irk[16];  // local_csrk can be generated based on this key, to save flash area
65                        // (delete this note at last, customers can not see it)
66     // 0x50
67     u16 ediv;     // 2
68     u8 rsvd[14];  // 14  peer_csrk info address if needed(delete this note at last, customers can not see it)
69 } smp_param_save_t;
70 
71 /**
72  * @brief      This function is used to configure the bonding storage address and size.
73  * @param[in]  address - SMP bonding storage start address.
74  * @param[in]  size_byte - SMP bonding storage size(e.g.: 2*4096).
75  * @return     none.
76  */
77 void blc_smp_configPairingSecurityInfoStorageAddressAndSize(int address,
78                                                             int size_byte);  // address and size must be 4K aligned
79 
80 /**
81  * @brief      This function is used to configure the number of master and slave devices that can be bound.
82  * @param[in]  peer_slave_max - The number of slave devices that can be bound.
83  * @param[in]  peer_master_max - The number of master devices that can be bound.
84  * @return     none.
85  */
86 void blc_smp_setBondingDeviceMaxNumber(int peer_slave_max, int peer_master_max);
87 
88 /**
89  * @brief      This function is used to get the starting address of the current binding information storage area.
90  * @param[in]  none.
91  * @return     The starting address of the current binding information storage area.
92  */
93 u32 blc_smp_getBondingInfoCurStartAddr(void);
94 
95 // Search
96 // This API is for master only, to search if current slave device is already paired with master
97 /**
98  * @brief      This function is used to obtain binding information according to the slave's address and address type.
99  * @param[in]  peer_addr_type - Address type.
100  * @param[in]  peer_addr - Address.
101  * @return     0: Failed to get binding information;
102  *             others: FLASH address of the information area.
103  */
104 u32 blc_smp_searchBondingSlaveDevice_by_PeerMacAddress(u8 peer_addr_type, u8 *peer_addr);
105 
106 // Delete
107 /**
108  * @brief      This function is used to delete binding information according to the peer device address
109  *             and device address type.
110  * @param[in]  peer_addr_type - Address type.
111  * @param[in]  peer_addr - Address.
112  * @return     0: Failed to delete binding information;
113  *             others: FLASH address of the deleted information area.
114  */
115 int blc_smp_deleteBondingSlaveInfo_by_PeerMacAddress(u8 peer_addr_type, u8 *peer_addr);
116 
117 /**
118  * @brief      This function is used to configure the storage order of binding information.
119  * @param[in]  method - The storage order of binding info method value can refer to the structure 'index_updateMethod_t
120  *                      0: Index update by pairing order;
121  *                      1: Index update by connect order.
122  * @return     none.
123  */
124 void blc_smp_setBondingInfoIndexUpdateMethod(index_updateMethod_t method);
125 
126 /**
127  * @brief      This function is used to clear all binding information stored in the local FLASH.
128  * @param[in]  none.
129  * @return     none.
130  */
131 void blc_smp_eraseAllBondingInfo(void);
132 
133 /**
134  * @brief      This function is used to check whether the bound storage area reaches the almost full warning threshold.
135  * @param[in]  none.
136  * @return     0: The warning threshold is not reached.
137  *             1: The warning threshold is reached..
138  */
139 bool blc_smp_isBondingInfoStorageLowAlarmed(void);
140 
141 /**
142  * @brief      This function is used to load bonding information according
143  *             to the peer device address and device address type.
144  * @param[in]  isMaster - Is it a Master role: 0: slave role, others: master role.
145  * @param[in]  slaveDevIdx - Address.
146  * @param[in]  addr_type - Address type.
147  * @param[in]  addr - Address.
148  * @param[out] smp_param_load - bonding information.
149  * @return     0: Failed to find the binding information; others: FLASH address of the bonding information area.
150  */
151 u32 blc_smp_loadBondingInfoByAddr(u8 isMaster, u8 slaveDevIdx, u8 addr_type, u8 *addr,
152                                   smp_param_save_t *smp_param_load);
153 
154 /**
155  * @brief      This function is used to get the bonding information numbers.
156  * @param[in]  isMaster - Is it a Master role: 0: slave role, others: master role.
157  * @param[in]  slaveDevIdx - Address.
158  * @return     0: The number of bound devices is 0; others: Number of bound devices.
159  */
160 u8 blc_smp_param_getCurrentBondingDeviceNumber(u8 isMasterRole, u8 slaveDevIdx);
161 
162 /**
163  * @brief      This function is used to load bonding information according to the index.
164  * @param[in]  isMaster - Is it a Master role: 0: slave role, others: master role.
165  * @param[in]  slaveDevIdx - Address.
166  * @param[in]  index - bonding index.
167  * @param[out] smp_param_load - bonding information.
168  * @return     0: Failed to find the binding information; others: FLASH address of the bonding information area.
169  */
170 u32 blc_smp_loadBondingInfoFromFlashByIndex(u8 isMaster, u8 slaveDevIdx, u8 index, smp_param_save_t *smp_param_load);
171 
172 /**
173  * @brief      This function is used to delete binding information according
174  *             to the peer device address and device address type.
175  * @param[in]  peer_addr_type - Address type.
176  * @param[in]  peer_addr - Address.
177  * @return     0: Failed to delete binding information;
178  *             others: FLASH address of the deleted information area.
179  */
180 int blc_smp_setPeerAddrResSupportFlg(u32 flash_addr, u8 support);
181 
182 #endif /* SMP_STORAGE_H_ */
183