aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-module/_bleio
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
commit4fd287655a72b9aea14cdac715ad5b90ed082ed2 (patch)
tree65d393bc0e699dd12d05b29ba568e04cea666207 /circuitpython/shared-module/_bleio
parent0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff)
add circuitpython code
Diffstat (limited to 'circuitpython/shared-module/_bleio')
-rw-r--r--circuitpython/shared-module/_bleio/Address.c45
-rw-r--r--circuitpython/shared-module/_bleio/Address.h41
-rw-r--r--circuitpython/shared-module/_bleio/Attribute.c46
-rw-r--r--circuitpython/shared-module/_bleio/Attribute.h41
-rw-r--r--circuitpython/shared-module/_bleio/Characteristic.h56
-rw-r--r--circuitpython/shared-module/_bleio/ScanEntry.c97
-rw-r--r--circuitpython/shared-module/_bleio/ScanEntry.h47
-rw-r--r--circuitpython/shared-module/_bleio/ScanResults.c139
-rw-r--r--circuitpython/shared-module/_bleio/ScanResults.h64
9 files changed, 576 insertions, 0 deletions
diff --git a/circuitpython/shared-module/_bleio/Address.c b/circuitpython/shared-module/_bleio/Address.c
new file mode 100644
index 0000000..8c8b043
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/Address.c
@@ -0,0 +1,45 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <string.h>
+
+#include "py/objstr.h"
+#include "shared-bindings/_bleio/Address.h"
+#include "shared-module/_bleio/Address.h"
+
+void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, uint8_t address_type) {
+ self->bytes = mp_obj_new_bytes(bytes, NUM_BLEIO_ADDRESS_BYTES);
+ self->type = address_type;
+}
+
+mp_obj_t common_hal_bleio_address_get_address_bytes(bleio_address_obj_t *self) {
+ return self->bytes;
+}
+
+uint8_t common_hal_bleio_address_get_type(bleio_address_obj_t *self) {
+ return self->type;
+}
diff --git a/circuitpython/shared-module/_bleio/Address.h b/circuitpython/shared-module/_bleio/Address.h
new file mode 100644
index 0000000..3978984
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/Address.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ADDRESS_H
+#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ADDRESS_H
+
+#include "py/obj.h"
+
+#define NUM_BLEIO_ADDRESS_BYTES 6
+
+typedef struct {
+ mp_obj_base_t base;
+ uint8_t type;
+ mp_obj_t bytes; // a bytes() object
+} bleio_address_obj_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ADDRESS_H
diff --git a/circuitpython/shared-module/_bleio/Attribute.c b/circuitpython/shared-module/_bleio/Attribute.c
new file mode 100644
index 0000000..3acfcf1
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/Attribute.c
@@ -0,0 +1,46 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <string.h>
+
+#include "py/runtime.h"
+#include "shared-bindings/_bleio/Attribute.h"
+
+void common_hal_bleio_attribute_security_mode_check_valid(bleio_attribute_security_mode_t security_mode) {
+ switch (security_mode) {
+ case SECURITY_MODE_NO_ACCESS:
+ case SECURITY_MODE_OPEN:
+ case SECURITY_MODE_ENC_NO_MITM:
+ case SECURITY_MODE_ENC_WITH_MITM:
+ case SECURITY_MODE_LESC_ENC_WITH_MITM:
+ case SECURITY_MODE_SIGNED_NO_MITM:
+ case SECURITY_MODE_SIGNED_WITH_MITM:
+ break;
+ default:
+ mp_raise_ValueError(translate("Invalid security_mode"));
+ break;
+ }
+}
diff --git a/circuitpython/shared-module/_bleio/Attribute.h b/circuitpython/shared-module/_bleio/Attribute.h
new file mode 100644
index 0000000..a498a14
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/Attribute.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ATTRIBUTE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ATTRIBUTE_H
+
+// BLE security modes: 0x<level><mode>
+typedef enum {
+ SECURITY_MODE_NO_ACCESS = 0x00,
+ SECURITY_MODE_OPEN = 0x11,
+ SECURITY_MODE_ENC_NO_MITM = 0x21,
+ SECURITY_MODE_ENC_WITH_MITM = 0x31,
+ SECURITY_MODE_LESC_ENC_WITH_MITM = 0x41,
+ SECURITY_MODE_SIGNED_NO_MITM = 0x12,
+ SECURITY_MODE_SIGNED_WITH_MITM = 0x22,
+} bleio_attribute_security_mode_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ATTRIBUTE_H
diff --git a/circuitpython/shared-module/_bleio/Characteristic.h b/circuitpython/shared-module/_bleio/Characteristic.h
new file mode 100644
index 0000000..298592b
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/Characteristic.h
@@ -0,0 +1,56 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H
+#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H
+
+// These are not the Bluetooth spec values. They are what is used by the Nordic SoftDevice.
+// The bit values are in different positions.
+
+typedef enum {
+ CHAR_PROP_NONE = 0,
+ CHAR_PROP_BROADCAST = 1u << 0,
+ CHAR_PROP_INDICATE = 1u << 1,
+ CHAR_PROP_NOTIFY = 1u << 2,
+ CHAR_PROP_READ = 1u << 3,
+ CHAR_PROP_WRITE = 1u << 4,
+ CHAR_PROP_WRITE_NO_RESPONSE = 1u << 5,
+ CHAR_PROP_ALL = (CHAR_PROP_BROADCAST | CHAR_PROP_INDICATE | CHAR_PROP_NOTIFY |
+ CHAR_PROP_READ | CHAR_PROP_WRITE | CHAR_PROP_WRITE_NO_RESPONSE)
+} bleio_characteristic_properties_enum_t;
+typedef uint8_t bleio_characteristic_properties_t;
+
+// Bluetooth spec property values
+#define BT_GATT_CHRC_BROADCAST 0x01
+#define BT_GATT_CHRC_READ 0x02
+#define BT_GATT_CHRC_WRITE_WITHOUT_RESP 0x04
+#define BT_GATT_CHRC_WRITE 0x08
+#define BT_GATT_CHRC_NOTIFY 0x10
+#define BT_GATT_CHRC_INDICATE 0x20
+#define BT_GATT_CHRC_AUTH 0x40
+#define BT_GATT_CHRC_EXT_PROP 0x80
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H
diff --git a/circuitpython/shared-module/_bleio/ScanEntry.c b/circuitpython/shared-module/_bleio/ScanEntry.c
new file mode 100644
index 0000000..7a57d8a
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/ScanEntry.c
@@ -0,0 +1,97 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ * Copyright (c) 2018 Artur Pacholec
+ * Copyright (c) 2017 Glenn Ruben Bakke
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <string.h>
+
+#include "shared-bindings/_bleio/Address.h"
+#include "shared-bindings/_bleio/ScanEntry.h"
+#include "shared-module/_bleio/Address.h"
+#include "shared-module/_bleio/ScanEntry.h"
+
+mp_obj_t common_hal_bleio_scanentry_get_address(bleio_scanentry_obj_t *self) {
+ return MP_OBJ_FROM_PTR(self->address);
+}
+
+mp_obj_t common_hal_bleio_scanentry_get_advertisement_bytes(bleio_scanentry_obj_t *self) {
+ return MP_OBJ_FROM_PTR(self->data);
+}
+
+mp_int_t common_hal_bleio_scanentry_get_rssi(bleio_scanentry_obj_t *self) {
+ return self->rssi;
+}
+
+bool common_hal_bleio_scanentry_get_connectable(bleio_scanentry_obj_t *self) {
+ return self->connectable;
+}
+
+bool common_hal_bleio_scanentry_get_scan_response(bleio_scanentry_obj_t *self) {
+ return self->scan_response;
+}
+
+bool bleio_scanentry_data_matches(const uint8_t *data, size_t len, const uint8_t *prefixes, size_t prefixes_length, bool any) {
+ if (prefixes_length == 0) {
+ return true;
+ }
+ if (len == 0) {
+ // Prefixes exist, but no data.
+ return false;
+ }
+ size_t i = 0;
+ while (i < prefixes_length) {
+ uint8_t prefix_length = prefixes[i];
+ i += 1;
+ size_t j = 0;
+ bool prefix_matched = false;
+ while (j < len) {
+ uint8_t structure_length = data[j];
+ j += 1;
+ if (structure_length == 0) {
+ break;
+ }
+ if (structure_length >= prefix_length && memcmp(data + j, prefixes + i, prefix_length) == 0) {
+ if (any) {
+ return true;
+ }
+ prefix_matched = true;
+ break;
+ }
+ j += structure_length;
+ }
+ // If all (!any), the current prefix must have matched at least one field.
+ if (!prefix_matched && !any) {
+ return false;
+ }
+ i += prefix_length;
+ }
+ // All prefixes matched some field (if !any), or none did (if any).
+ return !any;
+}
+
+bool common_hal_bleio_scanentry_matches(bleio_scanentry_obj_t *self, const uint8_t *prefixes, size_t prefixes_len, bool match_all) {
+ return bleio_scanentry_data_matches(self->data->data, self->data->len, prefixes, prefixes_len, !match_all);
+}
diff --git a/circuitpython/shared-module/_bleio/ScanEntry.h b/circuitpython/shared-module/_bleio/ScanEntry.h
new file mode 100644
index 0000000..9e142fd
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/ScanEntry.h
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANENTRY_H
+#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANENTRY_H
+
+#include "py/obj.h"
+#include "py/objstr.h"
+#include "shared-bindings/_bleio/Address.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ bool connectable;
+ bool scan_response;
+ int8_t rssi;
+ bleio_address_obj_t *address;
+ mp_obj_str_t *data;
+ uint64_t time_received;
+} bleio_scanentry_obj_t;
+
+bool bleio_scanentry_data_matches(const uint8_t *data, size_t len, const uint8_t *prefixes, size_t prefix_length, bool any);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANENTRY_H
diff --git a/circuitpython/shared-module/_bleio/ScanResults.c b/circuitpython/shared-module/_bleio/ScanResults.c
new file mode 100644
index 0000000..1f4dbff
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/ScanResults.c
@@ -0,0 +1,139 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ * Copyright (c) 2018 Artur Pacholec
+ * Copyright (c) 2017 Glenn Ruben Bakke
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <string.h>
+
+#include "shared/runtime/interrupt_char.h"
+#include "py/objstr.h"
+#include "py/runtime.h"
+#include "shared-bindings/_bleio/ScanEntry.h"
+#include "shared-bindings/_bleio/ScanResults.h"
+
+bleio_scanresults_obj_t *shared_module_bleio_new_scanresults(size_t buffer_size, uint8_t *prefixes, size_t prefixes_len, mp_int_t minimum_rssi) {
+ bleio_scanresults_obj_t *self = m_new_obj(bleio_scanresults_obj_t);
+ self->base.type = &bleio_scanresults_type;
+ ringbuf_alloc(&self->buf, buffer_size, false);
+ self->prefixes = prefixes;
+ self->prefix_length = prefixes_len;
+ self->minimum_rssi = minimum_rssi;
+ return self;
+}
+
+mp_obj_t common_hal_bleio_scanresults_next(bleio_scanresults_obj_t *self) {
+ while (ringbuf_num_filled(&self->buf) == 0 && !self->done && !mp_hal_is_interrupted()) {
+ RUN_BACKGROUND_TASKS;
+ }
+ if (ringbuf_num_filled(&self->buf) == 0 || mp_hal_is_interrupted()) {
+ return mp_const_none;
+ }
+
+ // Create a ScanEntry out of the data on the buffer.
+ uint8_t type = ringbuf_get(&self->buf);
+ bool connectable = (type & (1 << 0)) != 0;
+ bool scan_response = (type & (1 << 1)) != 0;
+ uint64_t ticks_ms;
+ ringbuf_get_n(&self->buf, (uint8_t *)&ticks_ms, sizeof(ticks_ms));
+ uint8_t rssi = ringbuf_get(&self->buf);
+ uint8_t peer_addr[NUM_BLEIO_ADDRESS_BYTES];
+ ringbuf_get_n(&self->buf, peer_addr, sizeof(peer_addr));
+ uint8_t addr_type = ringbuf_get(&self->buf);
+ uint16_t len;
+ ringbuf_get_n(&self->buf, (uint8_t *)&len, sizeof(len));
+
+ mp_obj_str_t *o = MP_OBJ_TO_PTR(mp_obj_new_bytes_of_zeros(len));
+ ringbuf_get_n(&self->buf, (uint8_t *)o->data, len);
+
+ bleio_scanentry_obj_t *entry = m_new_obj(bleio_scanentry_obj_t);
+ entry->base.type = &bleio_scanentry_type;
+ entry->rssi = rssi;
+
+ bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
+ address->base.type = &bleio_address_type;
+ common_hal_bleio_address_construct(MP_OBJ_TO_PTR(address), peer_addr, addr_type);
+ entry->address = address;
+
+ entry->data = o;
+ entry->time_received = ticks_ms;
+ entry->connectable = connectable;
+ entry->scan_response = scan_response;
+
+ return MP_OBJ_FROM_PTR(entry);
+}
+
+
+void shared_module_bleio_scanresults_append(bleio_scanresults_obj_t *self,
+ uint64_t ticks_ms,
+ bool connectable,
+ bool scan_response,
+ int8_t rssi,
+ const uint8_t *peer_addr,
+ uint8_t addr_type,
+ const uint8_t *data,
+ uint16_t len) {
+ int32_t packet_size = sizeof(uint8_t) + sizeof(ticks_ms) + sizeof(rssi) + NUM_BLEIO_ADDRESS_BYTES +
+ sizeof(addr_type) + sizeof(len) + len;
+ int32_t empty_space = self->buf.size - ringbuf_num_filled(&self->buf);
+ if (packet_size >= empty_space) {
+ // We can't fit the packet so skip it.
+ return;
+ }
+ // Filter the packet.
+ if (rssi < self->minimum_rssi) {
+ return;
+ }
+
+ // If any prefixes are provided, then only include packets that include at least one of them.
+ if (!bleio_scanentry_data_matches(data, len, self->prefixes, self->prefix_length, true)) {
+ return;
+ }
+ uint8_t type = 0;
+ if (connectable) {
+ type |= 1 << 0;
+ }
+ if (scan_response) {
+ type |= 1 << 1;
+ }
+
+ // Add the packet to the buffer.
+ ringbuf_put(&self->buf, type);
+ ringbuf_put_n(&self->buf, (uint8_t *)&ticks_ms, sizeof(ticks_ms));
+ ringbuf_put(&self->buf, rssi);
+ ringbuf_put_n(&self->buf, peer_addr, NUM_BLEIO_ADDRESS_BYTES);
+ ringbuf_put(&self->buf, addr_type);
+ ringbuf_put_n(&self->buf, (uint8_t *)&len, sizeof(len));
+ ringbuf_put_n(&self->buf, data, len);
+}
+
+bool shared_module_bleio_scanresults_get_done(bleio_scanresults_obj_t *self) {
+ return self->done;
+}
+
+void shared_module_bleio_scanresults_set_done(bleio_scanresults_obj_t *self, bool done) {
+ self->done = done;
+ self->common_hal_data = NULL;
+}
diff --git a/circuitpython/shared-module/_bleio/ScanResults.h b/circuitpython/shared-module/_bleio/ScanResults.h
new file mode 100644
index 0000000..945809c
--- /dev/null
+++ b/circuitpython/shared-module/_bleio/ScanResults.h
@@ -0,0 +1,64 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Dan Halbert for Adafruit Industries
+ * Copyright (c) 2018 Artur Pacholec
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANRESULTS_H
+#define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANRESULTS_H
+
+#include <stdint.h>
+
+#include "py/obj.h"
+#include "py/ringbuf.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ // Pointers that needs to live until the scan is done.
+ void *common_hal_data;
+ ringbuf_t buf;
+ // Prefixes is a length encoded array of prefixes.
+ uint8_t *prefixes;
+ size_t prefix_length;
+ mp_int_t minimum_rssi;
+ bool active;
+ bool done;
+} bleio_scanresults_obj_t;
+
+bleio_scanresults_obj_t *shared_module_bleio_new_scanresults(size_t buffer_size, uint8_t *prefixes, size_t prefixes_len, mp_int_t minimum_rssi);
+
+bool shared_module_bleio_scanresults_get_done(bleio_scanresults_obj_t *self);
+void shared_module_bleio_scanresults_set_done(bleio_scanresults_obj_t *self, bool done);
+
+void shared_module_bleio_scanresults_append(bleio_scanresults_obj_t *self,
+ uint64_t ticks_ms,
+ bool connectable,
+ bool scan_result,
+ int8_t rssi,
+ const uint8_t *peer_addr,
+ uint8_t addr_type,
+ const uint8_t *data,
+ uint16_t len);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_SCANRESULTS_H