1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
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 #include "smp_cmd.h"
17
18 #include "l2cap_le_if.h"
19 #include "log.h"
20 #include "packet.h"
21 #include "smp_def.h"
22
23 static int SMP_SendData(uint16_t handle, Packet *pkt, SMP_SendDataCb cb);
24
SMP_SendPairingRequest(uint16_t handle,const SMP_PairParam * pairReqParam,SMP_SendDataCb cb)25 int SMP_SendPairingRequest(uint16_t handle, const SMP_PairParam *pairReqParam, SMP_SendDataCb cb)
26 {
27 Packet *pkt = NULL;
28 size_t offset = 0x00;
29 uint8_t code = SMP_CODE_PAIRING_REQ;
30
31 LOG_INFO("%{public}s", __FUNCTION__);
32 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_PAIR_REQ_DATA_LEN);
33 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
34 offset += sizeof(code);
35 PacketPayloadWrite(pkt, &pairReqParam->ioCapability, offset, sizeof(pairReqParam->ioCapability));
36 offset += sizeof(pairReqParam->ioCapability);
37 PacketPayloadWrite(pkt, &pairReqParam->oobDataFlag, offset, sizeof(pairReqParam->oobDataFlag));
38 offset += sizeof(pairReqParam->oobDataFlag);
39 PacketPayloadWrite(pkt, &pairReqParam->authReq, offset, sizeof(pairReqParam->authReq));
40 offset += sizeof(pairReqParam->authReq);
41 PacketPayloadWrite(pkt, &pairReqParam->maxEncKeySize, offset, sizeof(pairReqParam->maxEncKeySize));
42 offset += sizeof(pairReqParam->maxEncKeySize);
43 PacketPayloadWrite(pkt, &pairReqParam->initKeyDist, offset, sizeof(pairReqParam->initKeyDist));
44 offset += sizeof(pairReqParam->initKeyDist);
45 PacketPayloadWrite(pkt, &pairReqParam->respKeyDist, offset, sizeof(pairReqParam->respKeyDist));
46
47 int ret = SMP_SendData(handle, pkt, cb);
48 if (ret != SMP_SUCCESS) {
49 LOG_ERROR("Send LE Packet Error: %{public}d.", ret);
50 }
51 PacketFree(pkt);
52
53 return ret;
54 }
55
SMP_SendPairingResponse(uint16_t handle,const SMP_PairParam * pairRspParam,SMP_SendDataCb cb)56 int SMP_SendPairingResponse(uint16_t handle, const SMP_PairParam *pairRspParam, SMP_SendDataCb cb)
57 {
58 Packet *pkt = NULL;
59 size_t offset = 0x00;
60 uint8_t code = SMP_CODE_PAIRING_RSP;
61
62 LOG_INFO("%{public}s", __FUNCTION__);
63 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_PAIR_RSP_DATA_LEN);
64 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
65 offset += sizeof(code);
66 PacketPayloadWrite(pkt, &pairRspParam->ioCapability, offset, sizeof(pairRspParam->ioCapability));
67 offset += sizeof(pairRspParam->ioCapability);
68 PacketPayloadWrite(pkt, &pairRspParam->oobDataFlag, offset, sizeof(pairRspParam->oobDataFlag));
69 offset += sizeof(pairRspParam->oobDataFlag);
70 PacketPayloadWrite(pkt, &pairRspParam->authReq, offset, sizeof(pairRspParam->authReq));
71 offset += sizeof(pairRspParam->authReq);
72 PacketPayloadWrite(pkt, &pairRspParam->maxEncKeySize, offset, sizeof(pairRspParam->maxEncKeySize));
73 offset += sizeof(pairRspParam->maxEncKeySize);
74 PacketPayloadWrite(pkt, &pairRspParam->initKeyDist, offset, sizeof(pairRspParam->initKeyDist));
75 offset += sizeof(pairRspParam->initKeyDist);
76 PacketPayloadWrite(pkt, &pairRspParam->respKeyDist, offset, sizeof(pairRspParam->respKeyDist));
77
78 int ret = SMP_SendData(handle, pkt, cb);
79 if (ret != SMP_SUCCESS) {
80 LOG_ERROR("Send LE Packet Error: %{public}d. ", ret);
81 }
82 PacketFree(pkt);
83
84 return ret;
85 }
86
SMP_SendPairingConfirm(uint16_t handle,const uint8_t * confirm,SMP_SendDataCb cb)87 int SMP_SendPairingConfirm(uint16_t handle, const uint8_t *confirm, SMP_SendDataCb cb)
88 {
89 Packet *pkt = NULL;
90 size_t offset = 0x00;
91 uint8_t code = SMP_CODE_PAIRING_CFM;
92
93 LOG_INFO("%{public}s", __FUNCTION__);
94
95 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_PAIR_CFM_DATA_LEN);
96
97 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
98 offset += sizeof(code);
99 PacketPayloadWrite(pkt, confirm, offset, SMP_CONFIRM_DATA_LEN);
100
101 int ret = SMP_SendData(handle, pkt, cb);
102 if (ret != SMP_SUCCESS) {
103 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
104 }
105
106 PacketFree(pkt);
107
108 return ret;
109 }
110
SMP_SendPairingRandom(uint16_t handle,const uint8_t * random,SMP_SendDataCb cb)111 int SMP_SendPairingRandom(uint16_t handle, const uint8_t *random, SMP_SendDataCb cb)
112 {
113 Packet *pkt = NULL;
114 size_t offset = 0x00;
115 uint8_t code = SMP_CODE_PAIRING_RAND;
116
117 LOG_INFO("%{public}s", __FUNCTION__);
118
119 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_PAIR_RAND_DATA_LEN);
120
121 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
122 offset += sizeof(code);
123 PacketPayloadWrite(pkt, random, offset, SMP_RANDOM_DATA_LEN);
124
125 int ret = SMP_SendData(handle, pkt, cb);
126 if (ret != SMP_SUCCESS) {
127 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
128 }
129
130 PacketFree(pkt);
131
132 return ret;
133 }
134
SMP_SendPairingFailed(uint16_t handle,uint8_t reason,SMP_SendDataCb cb)135 int SMP_SendPairingFailed(uint16_t handle, uint8_t reason, SMP_SendDataCb cb)
136 {
137 Packet *pkt = NULL;
138 size_t offset = 0x00;
139 uint8_t code = SMP_CODE_PAIRING_FAIL;
140
141 LOG_INFO("%{public}s", __FUNCTION__);
142
143 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_PAIR_FAIL_DATA_LEN);
144
145 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
146 offset += sizeof(code);
147 PacketPayloadWrite(pkt, &reason, offset, sizeof(reason));
148
149 int ret = SMP_SendData(handle, pkt, cb);
150 if (ret != SMP_SUCCESS) {
151 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
152 }
153
154 PacketFree(pkt);
155
156 return ret;
157 }
158
SMP_SendPairingPublicKey(uint16_t handle,const uint8_t * publicKey,SMP_SendDataCb cb)159 int SMP_SendPairingPublicKey(uint16_t handle, const uint8_t *publicKey, SMP_SendDataCb cb)
160 {
161 Packet *pkt = NULL;
162 size_t offset = 0x00;
163 uint8_t code = SMP_CODE_PAIRING_PUBLIC_KEY;
164
165 LOG_INFO("%{public}s", __FUNCTION__);
166
167 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_PAIR_PUBLIC_KEY_DATA_LEN);
168
169 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
170 offset += sizeof(code);
171 PacketPayloadWrite(pkt, publicKey, offset, SMP_PUBLICKEY_LEN);
172
173 int ret = SMP_SendData(handle, pkt, cb);
174 if (ret != SMP_SUCCESS) {
175 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
176 }
177
178 PacketFree(pkt);
179
180 return ret;
181 }
182
SMP_SendPairingDHKeyCheck(uint16_t handle,const uint8_t * dhKeyCheck,SMP_SendDataCb cb)183 int SMP_SendPairingDHKeyCheck(uint16_t handle, const uint8_t *dhKeyCheck, SMP_SendDataCb cb)
184 {
185 Packet *pkt = NULL;
186 size_t offset = 0x00;
187 uint8_t code = SMP_CODE_PAIRING_DHKEY_CHECK;
188
189 LOG_INFO("%{public}s", __FUNCTION__);
190
191 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_PAIR_DHK_CHECK_DATA_LEN);
192
193 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
194 offset += sizeof(code);
195 PacketPayloadWrite(pkt, dhKeyCheck, offset, SMP_DHKEY_CHECK_LEN);
196
197 int ret = SMP_SendData(handle, pkt, cb);
198 if (ret != SMP_SUCCESS) {
199 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
200 }
201
202 PacketFree(pkt);
203
204 return ret;
205 }
206
SMP_SendEncryptionInformation(uint16_t handle,const uint8_t * ltk,SMP_SendDataCb cb)207 int SMP_SendEncryptionInformation(uint16_t handle, const uint8_t *ltk, SMP_SendDataCb cb)
208 {
209 Packet *pkt = NULL;
210 size_t offset = 0x00;
211 uint8_t code = SMP_CODE_ENCRYPTION_INFO;
212
213 LOG_INFO("%{public}s", __FUNCTION__);
214
215 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_ENCRYPTION_INFO_DATA_LEN);
216
217 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
218 offset += sizeof(code);
219 PacketPayloadWrite(pkt, ltk, offset, SMP_LTK_LEN);
220
221 int ret = SMP_SendData(handle, pkt, cb);
222 if (ret != SMP_SUCCESS) {
223 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
224 }
225
226 PacketFree(pkt);
227
228 return ret;
229 }
230
SMP_SendMasterIdentification(uint16_t handle,uint16_t ediv,const uint8_t * rand,SMP_SendDataCb cb)231 int SMP_SendMasterIdentification(uint16_t handle, uint16_t ediv, const uint8_t *rand, SMP_SendDataCb cb)
232 {
233 Packet *pkt = NULL;
234 size_t offset = 0x00;
235 uint8_t code = SMP_CODE_MASTER_IDENTITY;
236 uint8_t edivBuf[sizeof(ediv)] = {0x00};
237
238 LOG_INFO("%{public}s", __FUNCTION__);
239
240 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_MASTER_IDENTITY_DATA_LEN);
241
242 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
243 offset += sizeof(code);
244 edivBuf[0] = (uint8_t)(ediv & 0x00FFu);
245 edivBuf[1] = (uint8_t)((ediv >> 0x08) & 0x00FFu);
246 PacketPayloadWrite(pkt, edivBuf, offset, sizeof(edivBuf));
247 offset += sizeof(edivBuf);
248 PacketPayloadWrite(pkt, rand, offset, SMP_MASTER_RAND_LEN);
249
250 int ret = SMP_SendData(handle, pkt, cb);
251 if (ret != SMP_SUCCESS) {
252 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
253 }
254
255 PacketFree(pkt);
256
257 return ret;
258 }
259
SMP_SendIdentityInformation(uint16_t handle,const uint8_t * irk,SMP_SendDataCb cb)260 int SMP_SendIdentityInformation(uint16_t handle, const uint8_t *irk, SMP_SendDataCb cb)
261 {
262 Packet *pkt = NULL;
263 size_t offset = 0x00;
264 uint8_t code = SMP_CODE_IDENTITY_INFO;
265
266 LOG_INFO("%{public}s", __FUNCTION__);
267
268 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_IDENTITY_INFO_DATA_LEN);
269
270 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
271 offset += sizeof(code);
272 PacketPayloadWrite(pkt, irk, offset, SMP_IRK_LEN);
273
274 int ret = SMP_SendData(handle, pkt, cb);
275 if (ret != SMP_SUCCESS) {
276 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
277 }
278
279 PacketFree(pkt);
280
281 return ret;
282 }
283
SMP_SendIdentityAddressInformation(uint16_t handle,const BtAddr * addr,SMP_SendDataCb cb)284 int SMP_SendIdentityAddressInformation(uint16_t handle, const BtAddr *addr, SMP_SendDataCb cb)
285 {
286 Packet *pkt = NULL;
287 size_t offset = 0x00;
288 uint8_t code = SMP_CODE_IDENTITY_ADDR_INFO;
289
290 LOG_INFO("%{public}s", __FUNCTION__);
291
292 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_IDENTITY_ADDR_INFO_DATA_LEN);
293
294 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
295 offset += sizeof(code);
296 PacketPayloadWrite(pkt, &addr->type, offset, sizeof(addr->type));
297 offset += sizeof(addr->type);
298 PacketPayloadWrite(pkt, addr->addr, offset, BT_ADDRESS_SIZE);
299
300 int ret = SMP_SendData(handle, pkt, cb);
301 if (ret != SMP_SUCCESS) {
302 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
303 }
304
305 PacketFree(pkt);
306
307 return ret;
308 }
309
SMP_SendSigningInformation(uint16_t handle,const uint8_t * csrk,SMP_SendDataCb cb)310 int SMP_SendSigningInformation(uint16_t handle, const uint8_t *csrk, SMP_SendDataCb cb)
311 {
312 Packet *pkt = NULL;
313 size_t offset = 0x00;
314 uint8_t code = SMP_CODE_SIGNING_INFO;
315
316 LOG_INFO("%{public}s", __FUNCTION__);
317
318 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_SIGNING_INFO_DATA_LEN);
319
320 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
321 offset += sizeof(code);
322 PacketPayloadWrite(pkt, csrk, offset, SMP_CSRK_LEN);
323
324 int ret = SMP_SendData(handle, pkt, cb);
325 if (ret != SMP_SUCCESS) {
326 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
327 }
328
329 PacketFree(pkt);
330
331 return ret;
332 }
333
SMP_SendSecurityRequest(uint16_t handle,uint8_t authReq,SMP_SendDataCb cb)334 int SMP_SendSecurityRequest(uint16_t handle, uint8_t authReq, SMP_SendDataCb cb)
335 {
336 Packet *pkt = NULL;
337 size_t offset = 0x00;
338 uint8_t code = SMP_CODE_SECURITY_REQ;
339
340 LOG_INFO("%{public}s", __FUNCTION__);
341
342 pkt = PacketMalloc(0x00, 0x00, SMP_CMD_SECURITY_REQ_DATA_LEN);
343
344 PacketPayloadWrite(pkt, &code, offset, sizeof(code));
345 offset += sizeof(code);
346 PacketPayloadWrite(pkt, &authReq, offset, sizeof(authReq));
347
348 int ret = SMP_SendData(handle, pkt, cb);
349 if (ret != SMP_SUCCESS) {
350 LOG_ERROR("Send LE Packet Error: %{public}d", ret);
351 }
352
353 PacketFree(pkt);
354
355 return ret;
356 }
357
SMP_SendData(uint16_t handle,Packet * pkt,SMP_SendDataCb cb)358 static int SMP_SendData(uint16_t handle, Packet *pkt, SMP_SendDataCb cb)
359 {
360 LOG_INFO("%{public}s: size(%u)", __FUNCTION__, PacketSize(pkt));
361 int ret = L2CIF_LeSendFixChannelData(handle, L2CAP_LE_SMP_CHANNEL, pkt, cb);
362
363 return ret;
364 }