11
11
12
12
CREATE_FLAG (flag_is_connected );
13
13
CREATE_FLAG (flag_discover_complete );
14
+ CREATE_FLAG (flag_security_changed );
14
15
CREATE_FLAG (flag_write_complete );
15
16
CREATE_FLAG (flag_read_complete );
16
17
17
18
static struct bt_conn * g_conn ;
18
19
static uint16_t chrc_handle ;
19
20
static uint16_t long_chrc_handle ;
21
+ static uint16_t enc_chrc_handle ;
22
+ static uint16_t lesc_chrc_handle ;
23
+ static uint8_t att_err ;
20
24
static const struct bt_uuid * test_svc_uuid = TEST_SERVICE_UUID ;
21
25
22
26
#define ARRAY_ITEM (i , _ ) i
@@ -61,9 +65,19 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
61
65
UNSET_FLAG (flag_is_connected );
62
66
}
63
67
68
+ static void security_changed (struct bt_conn * conn , bt_security_t level , enum bt_security_err err )
69
+ {
70
+ if (err != BT_SECURITY_ERR_SUCCESS ) {
71
+ FAIL ("Security failed (err %d)\n" , err );
72
+ } else {
73
+ SET_FLAG (flag_security_changed );
74
+ }
75
+ }
76
+
64
77
BT_CONN_CB_DEFINE (conn_callbacks ) = {
65
78
.connected = connected ,
66
79
.disconnected = disconnected ,
80
+ .security_changed = security_changed ,
67
81
};
68
82
69
83
void device_found (const bt_addr_le_t * addr , int8_t rssi , uint8_t type ,
@@ -141,6 +155,12 @@ static uint8_t discover_func(struct bt_conn *conn,
141
155
} else if (bt_uuid_cmp (chrc -> uuid , TEST_LONG_CHRC_UUID ) == 0 ) {
142
156
printk ("Found long_chrc\n" );
143
157
long_chrc_handle = chrc -> value_handle ;
158
+ } else if (bt_uuid_cmp (chrc -> uuid , TEST_ENC_CHRC_UUID ) == 0 ) {
159
+ printk ("Found enc_chrc_handle\n" );
160
+ enc_chrc_handle = chrc -> value_handle ;
161
+ } else if (bt_uuid_cmp (chrc -> uuid , TEST_LESC_CHRC_UUID ) == 0 ) {
162
+ printk ("Found lesc_chrc_handle\n" );
163
+ lesc_chrc_handle = chrc -> value_handle ;
144
164
}
145
165
}
146
166
@@ -169,31 +189,49 @@ static void gatt_discover(void)
169
189
printk ("Discover complete\n" );
170
190
}
171
191
172
- static void gatt_write_cb (struct bt_conn * conn , uint8_t err ,
173
- struct bt_gatt_write_params * params )
192
+ static void update_security (void )
174
193
{
175
- if (err != BT_ATT_ERR_SUCCESS ) {
176
- FAIL ("Write failed: 0x%02X\n" , err );
194
+ int err ;
195
+
196
+ printk ("Updating security\n" );
197
+ err = bt_conn_set_security (g_conn , BT_SECURITY_L2 );
198
+ if (err != 0 ) {
199
+ FAIL ("Set security failed (err %d)\n" , err );
177
200
}
178
201
202
+ WAIT_FOR_FLAG (flag_security_changed );
203
+ printk ("Security changed\n" );
204
+ }
205
+
206
+ static void gatt_write_cb (struct bt_conn * conn , uint8_t err , struct bt_gatt_write_params * params )
207
+ {
179
208
(void )memset (params , 0 , sizeof (* params ));
209
+ att_err = err ;
180
210
181
211
SET_FLAG (flag_write_complete );
182
212
}
183
213
184
- static void gatt_write (uint16_t handle )
214
+ static void gatt_write (uint16_t handle , uint8_t expect_att_err )
185
215
{
186
216
static struct bt_gatt_write_params write_params ;
187
217
int err ;
188
218
189
219
if (handle == chrc_handle ) {
190
- printk ("Writing to chrc\n" );
220
+ printk ("Writing to chrc and expecting 0x%02X \n" , expect_att_err );
191
221
write_params .data = chrc_data ;
192
222
write_params .length = sizeof (chrc_data );
193
223
} else if (handle == long_chrc_handle ) {
194
- printk ("Writing to long_chrc\n" );
224
+ printk ("Writing to long_chrc and expecting 0x%02X \n" , expect_att_err );
195
225
write_params .data = long_chrc_data ;
196
226
write_params .length = sizeof (long_chrc_data );
227
+ } else if (handle == enc_chrc_handle ) {
228
+ printk ("Writing to enc_chrc and expecting 0x%02X\n" , expect_att_err );
229
+ write_params .data = chrc_data ;
230
+ write_params .length = sizeof (chrc_data );
231
+ } else if (handle == lesc_chrc_handle ) {
232
+ printk ("Writing to lesc_chrc and expecting 0x%02X\n" , expect_att_err );
233
+ write_params .data = chrc_data ;
234
+ write_params .length = sizeof (chrc_data );
197
235
}
198
236
199
237
write_params .func = gatt_write_cb ;
@@ -207,15 +245,25 @@ static void gatt_write(uint16_t handle)
207
245
}
208
246
209
247
WAIT_FOR_FLAG (flag_write_complete );
248
+
249
+ if (att_err != expect_att_err ) {
250
+ FAIL ("Write failed: 0x%02X\n" , att_err );
251
+ }
252
+
210
253
printk ("success\n" );
211
254
}
212
255
213
256
static uint8_t gatt_read_cb (struct bt_conn * conn , uint8_t err ,
214
257
struct bt_gatt_read_params * params ,
215
258
const void * data , uint16_t length )
216
259
{
260
+ att_err = err ;
261
+
217
262
if (err != BT_ATT_ERR_SUCCESS ) {
218
- FAIL ("Read failed: 0x%02X\n" , err );
263
+ printk ("Read failed: 0x%02X\n" , err );
264
+
265
+ (void )memset (params , 0 , sizeof (* params ));
266
+ SET_FLAG (flag_read_complete );
219
267
220
268
return BT_GATT_ITER_STOP ;
221
269
}
@@ -242,16 +290,25 @@ static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err,
242
290
FAIL ("long_chrc data different than expected (%u %u)\n" , length ,
243
291
LONG_CHRC_SIZE );
244
292
}
293
+ } else if (params -> single .handle == enc_chrc_handle ) {
294
+ if (data_received_size != CHRC_SIZE ||
295
+ memcmp (data_received , chrc_data , data_received_size ) != 0 ) {
296
+ FAIL ("enc_chrc data different than expected (%u %u)\n" , length , CHRC_SIZE );
297
+ }
298
+ } else if (params -> single .handle == lesc_chrc_handle ) {
299
+ if (data_received_size != CHRC_SIZE ||
300
+ memcmp (data_received , chrc_data , data_received_size ) != 0 ) {
301
+ FAIL ("lesc_chrc data different than expected (%u %u)\n" , length , CHRC_SIZE );
302
+ }
245
303
}
246
304
247
305
(void )memset (params , 0 , sizeof (* params ));
248
-
249
306
SET_FLAG (flag_read_complete );
250
307
251
308
return BT_GATT_ITER_STOP ;
252
309
}
253
310
254
- static void gatt_read (uint16_t handle )
311
+ static void gatt_read (uint16_t handle , uint8_t expect_att_err )
255
312
{
256
313
static struct bt_gatt_read_params read_params ;
257
314
int err ;
@@ -260,9 +317,13 @@ static void gatt_read(uint16_t handle)
260
317
memset (data_received , 0 , sizeof (data_received ));
261
318
262
319
if (handle == chrc_handle ) {
263
- printk ("Reading chrc\n" );
320
+ printk ("Reading chrc and expecting 0x%02X \n" , expect_att_err );
264
321
} else if (handle == long_chrc_handle ) {
265
- printk ("Reading long_chrc\n" );
322
+ printk ("Reading long_chrc and expecting 0x%02X\n" , expect_att_err );
323
+ } else if (handle == enc_chrc_handle ) {
324
+ printk ("Reading enc_chrc and expecting 0x%02X\n" , expect_att_err );
325
+ } else if (handle == lesc_chrc_handle ) {
326
+ printk ("Reading lesc_chrc and expecting 0x%02X\n" , expect_att_err );
266
327
}
267
328
268
329
read_params .func = gatt_read_cb ;
@@ -278,6 +339,11 @@ static void gatt_read(uint16_t handle)
278
339
}
279
340
280
341
WAIT_FOR_FLAG (flag_read_complete );
342
+
343
+ if (att_err != expect_att_err ) {
344
+ FAIL ("Read failed: 0x%02X\n" , att_err );
345
+ }
346
+
281
347
printk ("success\n" );
282
348
}
283
349
@@ -303,12 +369,24 @@ static void test_main(void)
303
369
304
370
/* Write and read a few times to ensure stateless behavior */
305
371
for (size_t i = 0 ; i < 3 ; i ++ ) {
306
- gatt_write (chrc_handle );
307
- gatt_read (chrc_handle );
308
- gatt_write (long_chrc_handle );
309
- gatt_read (long_chrc_handle );
372
+ gatt_write (chrc_handle , BT_ATT_ERR_SUCCESS );
373
+ gatt_read (chrc_handle , BT_ATT_ERR_SUCCESS );
374
+ gatt_write (long_chrc_handle , BT_ATT_ERR_SUCCESS );
375
+ gatt_read (long_chrc_handle , BT_ATT_ERR_SUCCESS );
310
376
}
311
377
378
+ gatt_write (enc_chrc_handle , BT_ATT_ERR_AUTHENTICATION );
379
+ gatt_read (enc_chrc_handle , BT_ATT_ERR_AUTHENTICATION );
380
+ gatt_write (lesc_chrc_handle , BT_ATT_ERR_AUTHENTICATION );
381
+ gatt_read (lesc_chrc_handle , BT_ATT_ERR_AUTHENTICATION );
382
+
383
+ update_security ();
384
+
385
+ gatt_write (enc_chrc_handle , BT_ATT_ERR_SUCCESS );
386
+ gatt_read (enc_chrc_handle , BT_ATT_ERR_SUCCESS );
387
+ gatt_write (lesc_chrc_handle , BT_ATT_ERR_SUCCESS );
388
+ gatt_read (lesc_chrc_handle , BT_ATT_ERR_SUCCESS );
389
+
312
390
PASS ("GATT client Passed\n" );
313
391
}
314
392
0 commit comments