Skip to content

Commit f0e60da

Browse files
authored
Merge pull request #3310 from dhalbert/ble_hci
_bleio HCI implementation
2 parents ee1b142 + 4e3cb55 commit f0e60da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+10050
-229
lines changed

devices/ble_hci/common-hal/_bleio/Adapter.c

Lines changed: 958 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
7+
* Copyright (c) 2018 Artur Pacholec
8+
* Copyright (c) 2016 Glenn Ruben Bakke
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* in the Software without restriction, including without limitation the rights
13+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
* copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be included in
18+
* all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26+
* THE SOFTWARE.
27+
*/
28+
29+
#ifndef MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ADAPTER_H
30+
#define MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ADAPTER_H
31+
32+
#include "py/obj.h"
33+
#include "py/objtuple.h"
34+
35+
#include "shared-bindings/_bleio/Characteristic.h"
36+
#include "shared-bindings/_bleio/Connection.h"
37+
#include "shared-bindings/_bleio/ScanResults.h"
38+
#include "shared-bindings/busio/UART.h"
39+
#include "shared-bindings/digitalio/DigitalInOut.h"
40+
41+
#ifndef BLEIO_TOTAL_CONNECTION_COUNT
42+
#define BLEIO_TOTAL_CONNECTION_COUNT 5
43+
#endif
44+
45+
extern bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
46+
47+
typedef struct _bleio_adapter_obj_t {
48+
mp_obj_base_t base;
49+
bleio_scanresults_obj_t *scan_results;
50+
mp_obj_t name;
51+
mp_obj_tuple_t *connection_objs;
52+
busio_uart_obj_t* hci_uart;
53+
digitalio_digitalinout_obj_t *rts_digitalinout;
54+
digitalio_digitalinout_obj_t *cts_digitalinout;
55+
bool allocated; // True when in use.
56+
bool now_advertising;
57+
bool extended_advertising;
58+
bool circuitpython_advertising;
59+
bool enabled;
60+
61+
// HCI adapter version info.
62+
uint8_t hci_version;
63+
uint8_t lmp_version;
64+
uint16_t hci_revision;
65+
uint16_t manufacturer;
66+
uint16_t lmp_subversion;
67+
68+
// Used to monitor advertising timeout for legacy avertising.
69+
uint64_t advertising_start_ticks;
70+
uint64_t advertising_timeout_msecs; // If zero, do not check.
71+
72+
// Generic services characteristics.
73+
bleio_characteristic_obj_t *device_name_characteristic;
74+
bleio_characteristic_obj_t *appearance_characteristic;
75+
bleio_characteristic_obj_t * service_changed_characteristic;
76+
77+
uint16_t max_acl_buffer_len;
78+
uint16_t max_acl_num_buffers;
79+
uint16_t max_adv_data_len;
80+
uint8_t features[8]; // Supported BLE features.
81+
82+
// All the local attributes for this device. The index into the list
83+
// corresponds to the handle.
84+
mp_obj_list_t *attributes;
85+
// Handle for last added service. Characteristics can only be added immediately after
86+
// the service they belong to. This vets that.
87+
uint16_t last_added_service_handle;
88+
uint16_t last_added_characteristic_handle;
89+
} bleio_adapter_obj_t;
90+
91+
uint16_t bleio_adapter_add_attribute(bleio_adapter_obj_t *adapter, mp_obj_t *attribute);
92+
void bleio_adapter_advertising_was_stopped(bleio_adapter_obj_t *self);
93+
mp_obj_t* bleio_adapter_get_attribute(bleio_adapter_obj_t *adapter, uint16_t handle);
94+
uint16_t bleio_adapter_max_attribute_handle(bleio_adapter_obj_t *adapter);
95+
void bleio_adapter_background(bleio_adapter_obj_t* adapter);
96+
void bleio_adapter_gc_collect(bleio_adapter_obj_t* adapter);
97+
void bleio_adapter_reset(bleio_adapter_obj_t* adapter);
98+
99+
#endif // MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ADAPTER_H
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
29+
#include "shared-bindings/_bleio/Attribute.h"
30+
#include "shared-bindings/_bleio/Characteristic.h"
31+
#include "shared-bindings/_bleio/Descriptor.h"
32+
#include "shared-bindings/_bleio/Service.h"
33+
34+
35+
bleio_uuid_obj_t *bleio_attribute_get_uuid(mp_obj_t *attribute) {
36+
if (MP_OBJ_IS_TYPE(attribute, &bleio_characteristic_type)) {
37+
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(attribute);
38+
return characteristic->uuid;
39+
}
40+
if (MP_OBJ_IS_TYPE(attribute, &bleio_descriptor_type)) {
41+
bleio_descriptor_obj_t *descriptor = MP_OBJ_TO_PTR(attribute);
42+
return descriptor->uuid;
43+
}
44+
if (MP_OBJ_IS_TYPE(attribute, &bleio_service_type)) {
45+
bleio_service_obj_t *service = MP_OBJ_TO_PTR(attribute);
46+
return service->uuid;
47+
}
48+
mp_raise_RuntimeError(translate("Invalid BLE attribute"));
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ATTRIBUTE_H
28+
#define MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ATTRIBUTE_H
29+
30+
#include "shared-module/_bleio/Attribute.h"
31+
#include "shared-bindings/_bleio/UUID.h"
32+
33+
bleio_uuid_obj_t *bleio_attribute_get_uuid(mp_obj_t *attribute);
34+
35+
#endif // MICROPY_INCLUDED_BLE_HCI_COMMON_HAL_ATTRIBUTE_H

0 commit comments

Comments
 (0)