hci.c (63671e69528ce01470ba0f26b596b5fc63797376) hci.c (a0698cb4c696b74e4f10b8dcffd80c5327fa6c05)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 1234 unchanged lines hidden (view full) ---

1243 */
1244void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){
1245 *addr_type = hci_stack->le_connection_own_addr_type;
1246 hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, addr);
1247}
1248
1249void le_handle_advertisement_report(uint8_t *packet, uint16_t size){
1250
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright

--- 1234 unchanged lines hidden (view full) ---

1243 */
1244void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){
1245 *addr_type = hci_stack->le_connection_own_addr_type;
1246 hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, addr);
1247}
1248
1249void le_handle_advertisement_report(uint8_t *packet, uint16_t size){
1250
1251 int offset = 3;
1252 int num_reports = packet[offset];
1251 uint16_t offset = 3;
1252 uint8_t num_reports = packet[offset];
1253 offset += 1;
1254
1253 offset += 1;
1254
1255 int i;
1256 // log_info("HCI: handle adv report with num reports: %d", num_reports);
1255 uint16_t i;
1257 uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
1258 for (i=0; (i<num_reports) && (offset < size);i++){
1259 // sanity checks on data_length:
1260 uint8_t data_length = packet[offset + 8];
1261 if (data_length > LE_ADVERTISING_DATA_SIZE) return;
1262 if ((offset + 9u + data_length + 1u) > size) return;
1263 // setup event
1264 uint8_t event_size = 10u + data_length;
1256 uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
1257 for (i=0; (i<num_reports) && (offset < size);i++){
1258 // sanity checks on data_length:
1259 uint8_t data_length = packet[offset + 8];
1260 if (data_length > LE_ADVERTISING_DATA_SIZE) return;
1261 if ((offset + 9u + data_length + 1u) > size) return;
1262 // setup event
1263 uint8_t event_size = 10u + data_length;
1265 int pos = 0;
1264 uint16_t pos = 0;
1266 event[pos++] = GAP_EVENT_ADVERTISING_REPORT;
1267 event[pos++] = event_size;
1268 (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address
1269 offset += 8;
1270 pos += 8;
1271 event[pos++] = packet[offset + 1 + data_length]; // rssi
1272 event[pos++] = data_length;
1273 offset++;
1274 (void)memcpy(&event[pos], &packet[offset], data_length);
1275 pos += data_length;
1276 offset += data_length + 1u; // rssi
1277 hci_emit_event(event, pos, 1);
1278 }
1279}
1265 event[pos++] = GAP_EVENT_ADVERTISING_REPORT;
1266 event[pos++] = event_size;
1267 (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address
1268 offset += 8;
1269 pos += 8;
1270 event[pos++] = packet[offset + 1 + data_length]; // rssi
1271 event[pos++] = data_length;
1272 offset++;
1273 (void)memcpy(&event[pos], &packet[offset], data_length);
1274 pos += data_length;
1275 offset += data_length + 1u; // rssi
1276 hci_emit_event(event, pos, 1);
1277 }
1278}
1279
1280#ifdef ENABLE_LE_EXTENDED_ADVERTISING
1281void le_handle_extended_advertisement_report(uint8_t *packet, uint16_t size) {
1282 uint16_t offset = 3;
1283 uint8_t num_reports = packet[offset++];
1284
1285 uint8_t i;
1286 uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var
1287 for (i=0; (i<num_reports) && (offset < size);i++){
1288 // sanity checks on data_length:
1289 uint16_t data_length = packet[offset + 23];
1290 if (data_length > LE_ADVERTISING_DATA_SIZE) return;
1291 if ((offset + 24u + data_length) > size) return;
1292 uint16_t event_type = little_endian_read_16(packet, offset);
1293 offset += 2;
1294 if ((event_type & 0x10) != 0) {
1295 // setup legacy event
1296 uint8_t legacy_event_type;
1297 switch (event_type){
1298 case 0b0010011:
1299 // ADV_IND
1300 legacy_event_type = 0;
1301 break;
1302 case 0b0010101:
1303 // ADV_DIRECT_IND
1304 legacy_event_type = 1;
1305 break;
1306 case 0b0010010:
1307 // ADV_SCAN_IND
1308 legacy_event_type = 2;
1309 break;
1310 case 0b0010000:
1311 // ADV_NONCONN_IND
1312 legacy_event_type = 3;
1313 break;
1314 case 0b0011011:
1315 case 0b0011010:
1316 // SCAN_RSP
1317 legacy_event_type = 4;
1318 break;
1319 default:
1320 legacy_event_type = 0;
1321 break;
1322 }
1323 uint16_t pos = 0;
1324 event[pos++] = GAP_EVENT_ADVERTISING_REPORT;
1325 event[pos++] = 10u + data_length;
1326 event[pos++] = legacy_event_type;
1327 // copy address type + address
1328 (void) memcpy(&event[pos], &packet[offset], 1 + 6);
1329 offset += 7;
1330 pos += 7;
1331 // skip primary_phy, secondary_phy, advertising_sid, tx_power
1332 offset += 4;
1333 // copy rssi
1334 event[pos++] = packet[offset++];
1335 // skip periodic advertising interval and direct address
1336 offset += 9;
1337 // copy data len + data;
1338 (void) memcpy(&event[pos], &packet[offset], 1 + data_length);
1339 pos += 1 +data_length;
1340 offset += 1+ data_length;
1341 hci_emit_event(event, pos, 1);
1342 } else {
1343 // TODO: process new events
1344 }
1345 }
1346}
1280#endif
1347#endif
1348
1281#endif
1349#endif
1350#endif
1282
1283#ifdef ENABLE_BLE
1284#ifdef ENABLE_LE_PERIPHERAL
1285static void hci_update_advertisements_enabled_for_current_roles(void){
1286 if (hci_stack->le_advertisements_enabled){
1287 // get number of active le slave connections
1288 int num_slave_connections = 0;
1289 btstack_linked_list_iterator_t it;

--- 1998 unchanged lines hidden (view full) ---

3288 break;
3289#endif
3290
3291#ifdef ENABLE_BLE
3292 case HCI_EVENT_LE_META:
3293 switch (packet[2]){
3294#ifdef ENABLE_LE_CENTRAL
3295 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
1351
1352#ifdef ENABLE_BLE
1353#ifdef ENABLE_LE_PERIPHERAL
1354static void hci_update_advertisements_enabled_for_current_roles(void){
1355 if (hci_stack->le_advertisements_enabled){
1356 // get number of active le slave connections
1357 int num_slave_connections = 0;
1358 btstack_linked_list_iterator_t it;

--- 1998 unchanged lines hidden (view full) ---

3357 break;
3358#endif
3359
3360#ifdef ENABLE_BLE
3361 case HCI_EVENT_LE_META:
3362 switch (packet[2]){
3363#ifdef ENABLE_LE_CENTRAL
3364 case HCI_SUBEVENT_LE_ADVERTISING_REPORT:
3296 // log_info("advertising report received");
3297 if (!hci_stack->le_scanning_enabled) break;
3298 le_handle_advertisement_report(packet, size);
3299 break;
3365 if (!hci_stack->le_scanning_enabled) break;
3366 le_handle_advertisement_report(packet, size);
3367 break;
3368#ifdef ENABLE_LE_EXTENDED_ADVERTISING
3369 case HCI_SUBEVENT_LE_EXTENDED_ADVERTISING_REPORT:
3370 if (!hci_stack->le_scanning_enabled) break;
3371 le_handle_extended_advertisement_report(packet, size);
3372 break;
3300#endif
3373#endif
3374#endif
3301 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
3302 event_handle_le_connection_complete(packet);
3303 break;
3304
3305 // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]);
3306 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
3307 handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
3308 conn = hci_connection_for_handle(handle);

--- 3849 unchanged lines hidden ---
3375 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
3376 event_handle_le_connection_complete(packet);
3377 break;
3378
3379 // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]);
3380 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
3381 handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet);
3382 conn = hci_connection_for_handle(handle);

--- 3849 unchanged lines hidden ---