bleio: Add a new Address class
Use the new in the Adapter singleton.
This commit is contained in:
parent
d5f942a971
commit
345334aaf1
@ -185,6 +185,7 @@ SRC_BINDINGS_ENUMS = \
|
||||
|
||||
ifneq ($(SD), )
|
||||
SRC_BINDINGS_ENUMS += \
|
||||
bleio/Address.c \
|
||||
bleio/AddressType.c \
|
||||
bleio/UUIDType.c
|
||||
endif
|
||||
|
@ -26,11 +26,12 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ble_drv.h"
|
||||
#include "nrfx.h"
|
||||
#include "nrf_error.h"
|
||||
#include "py/misc.h"
|
||||
#include "shared-module/bleio/Address.h"
|
||||
|
||||
void common_hal_bleio_adapter_set_enabled(bool enabled) {
|
||||
if (enabled) {
|
||||
@ -49,12 +50,10 @@ bool common_hal_bleio_adapter_get_enabled(void) {
|
||||
return ble_drv_stack_enabled();
|
||||
}
|
||||
|
||||
void common_hal_bleio_adapter_get_address(vstr_t *vstr) {
|
||||
ble_drv_addr_t address;
|
||||
ble_drv_address_get(&address);
|
||||
void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address) {
|
||||
ble_drv_addr_t drv_addr;
|
||||
ble_drv_address_get(&drv_addr);
|
||||
|
||||
vstr_printf(vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
|
||||
HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
|
||||
address.addr[5], address.addr[4], address.addr[3],
|
||||
address.addr[2], address.addr[1], address.addr[0]);
|
||||
address->type = drv_addr.addr_type;
|
||||
memcpy(address->value, drv_addr.addr, BLEIO_ADDRESS_BYTES);
|
||||
}
|
||||
|
@ -90,10 +90,8 @@ typedef enum {
|
||||
typedef enum {
|
||||
UBLUEPY_ADDR_TYPE_PUBLIC = 0,
|
||||
UBLUEPY_ADDR_TYPE_RANDOM_STATIC = 1,
|
||||
#if 0
|
||||
UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE = 2,
|
||||
UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = 3,
|
||||
#endif
|
||||
} ubluepy_addr_type_t;
|
||||
|
||||
typedef enum {
|
||||
|
@ -82,9 +82,6 @@ STATIC const mp_rom_map_elem_t ubluepy_constants_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_EVT_GATTS_WRITE), MP_ROM_INT(80) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_UUID_CCCD), MP_ROM_INT(0x2902) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_ADDR_TYPE_PUBLIC), MP_ROM_INT(UBLUEPY_ADDR_TYPE_PUBLIC) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ADDR_TYPE_RANDOM_STATIC), MP_ROM_INT(UBLUEPY_ADDR_TYPE_RANDOM_STATIC) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_ad_types), MP_ROM_PTR(&ubluepy_constants_ad_types_type) },
|
||||
};
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
*/
|
||||
|
||||
#include "py/objproperty.h"
|
||||
#include "shared-bindings/bleio/Address.h"
|
||||
#include "shared-bindings/bleio/Adapter.h"
|
||||
|
||||
//| .. currentmodule:: bleio
|
||||
@ -57,8 +58,6 @@
|
||||
//| MAC address of the BLE adapter. (read-only)
|
||||
//|
|
||||
|
||||
#define BLE_ADDRESS_LEN 17
|
||||
|
||||
STATIC mp_obj_t bleio_adapter_get_enabled(mp_obj_t self) {
|
||||
return mp_obj_new_bool(common_hal_bleio_adapter_get_enabled());
|
||||
}
|
||||
@ -81,16 +80,12 @@ const mp_obj_property_t bleio_adapter_enabled_obj = {
|
||||
};
|
||||
|
||||
STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) {
|
||||
vstr_t vstr;
|
||||
vstr_init(&vstr, BLE_ADDRESS_LEN);
|
||||
mp_obj_t obj = bleio_address_type.make_new(&bleio_address_type, 1, 0, mp_const_none);
|
||||
bleio_address_obj_t *address = MP_OBJ_TO_PTR(obj);
|
||||
|
||||
common_hal_bleio_adapter_get_address(&vstr);
|
||||
common_hal_bleio_adapter_get_address(address);
|
||||
|
||||
const mp_obj_t mac_str = mp_obj_new_str(vstr.buf, vstr.len);
|
||||
|
||||
vstr_clear(&vstr);
|
||||
|
||||
return mac_str;
|
||||
return obj;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address);
|
||||
|
||||
|
@ -27,12 +27,12 @@
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "shared-module/bleio/Address.h"
|
||||
|
||||
const mp_obj_type_t bleio_adapter_type;
|
||||
|
||||
extern bool common_hal_bleio_adapter_get_enabled(void);
|
||||
extern void common_hal_bleio_adapter_set_enabled(bool enabled);
|
||||
extern void common_hal_bleio_adapter_get_address(vstr_t *address);
|
||||
extern void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H
|
||||
|
167
shared-bindings/bleio/Address.c
Normal file
167
shared-bindings/bleio/Address.c
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* 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 <stdio.h>
|
||||
|
||||
#include "py/objproperty.h"
|
||||
#include "py/objstr.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/bleio/Address.h"
|
||||
#include "shared-module/bleio/Address.h"
|
||||
|
||||
#define ADDRESS_LONG_LEN 17 // XX:XX:XX:XX:XX:XX
|
||||
#define ADDRESS_SHORT_LEN 12 // XXXXXXXXXXXX
|
||||
|
||||
//| .. currentmodule:: bleio
|
||||
//|
|
||||
//| :class:`Address` -- BLE address
|
||||
//| =========================================================
|
||||
//|
|
||||
//| Encapsulates the address of a BLE device.
|
||||
//|
|
||||
|
||||
//| .. class:: Address(address)
|
||||
//|
|
||||
//| Create a new Address object encapsulating the address value.
|
||||
//| The value itself can be one of:
|
||||
//|
|
||||
//| - a `str` value in the format of 'XXXXXXXXXXXX' or 'XX:XX:XX:XX:XX'
|
||||
//| - a `bytes` or `bytearray` containing 6 bytes
|
||||
//| - another Address object
|
||||
//|
|
||||
//| :param address: The address to encapsulate
|
||||
//|
|
||||
|
||||
//| .. attribute:: type
|
||||
//|
|
||||
//| The address type. One of:
|
||||
//|
|
||||
//| - `bleio.AddressType.PUBLIC`
|
||||
//| - `bleio.AddressType.RANDOM_STATIC`
|
||||
//| - `bleio.AddressType.RANDOM_PRIVATE_RESOLVABLE`
|
||||
//| - `bleio.AddressType.RANDOM_PRIVATE_NON_RESOLVABLE`
|
||||
//|
|
||||
STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
|
||||
mp_arg_check_num(n_args, n_kw, 1, 1, true);
|
||||
bleio_address_obj_t *self = m_new_obj(bleio_address_obj_t);
|
||||
self->base.type = &bleio_address_type;
|
||||
self->type = ADDRESS_PUBLIC;
|
||||
|
||||
mp_map_t kw_args;
|
||||
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
|
||||
|
||||
enum { ARG_address };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ ARG_address, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
};
|
||||
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
const mp_obj_t address = args[ARG_address].u_obj;
|
||||
|
||||
if (MP_OBJ_IS_STR(address)) {
|
||||
GET_STR_DATA_LEN(address, str_data, str_len);
|
||||
const bool is_long = (str_len == ADDRESS_LONG_LEN);
|
||||
const bool is_short = (str_len == ADDRESS_SHORT_LEN);
|
||||
|
||||
if (is_long || is_short) {
|
||||
size_t i = str_len - 1;
|
||||
for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) {
|
||||
self->value[b] = unichar_xdigit_value(str_data[i]) |
|
||||
unichar_xdigit_value(str_data[i - 1]) << 4;
|
||||
|
||||
i -= is_long ? 3 : 2;
|
||||
}
|
||||
} else {
|
||||
mp_raise_ValueError("Wrong address length");
|
||||
}
|
||||
} else if (MP_OBJ_IS_TYPE(address, &mp_type_bytearray) || MP_OBJ_IS_TYPE(address, &mp_type_bytes)) {
|
||||
mp_buffer_info_t buf_info;
|
||||
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
|
||||
if (buf_info.len != BLEIO_ADDRESS_BYTES) {
|
||||
mp_raise_ValueError("Wrong number of bytes provided");
|
||||
}
|
||||
|
||||
for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) {
|
||||
self->value[BLEIO_ADDRESS_BYTES - b - 1] = ((uint8_t*)buf_info.buf)[b];
|
||||
}
|
||||
} else if (MP_OBJ_IS_TYPE(address, &bleio_address_type)) {
|
||||
// deep copy
|
||||
bleio_address_obj_t *other = MP_OBJ_TO_PTR(address);
|
||||
self->type = other->type;
|
||||
memcpy(self->value, other->value, BLEIO_ADDRESS_BYTES);
|
||||
}
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
mp_printf(print, "Address('"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT"')",
|
||||
self->value[5], self->value[4], self->value[3],
|
||||
self->value[2], self->value[1], self->value[0]);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t bleio_address_get_type(mp_obj_t self_in) {
|
||||
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
if (self->type == ADDRESS_PUBLIC) {
|
||||
return (mp_obj_t)&bleio_addresstype_public_obj;
|
||||
} else if (self->type == ADDRESS_RANDOM_STATIC) {
|
||||
return (mp_obj_t)&bleio_addresstype_random_static_obj;
|
||||
} else if (self->type == ADDRESS_RANDOM_PRIVATE_RESOLVABLE) {
|
||||
return (mp_obj_t)&bleio_addresstype_random_private_resolvable_obj;
|
||||
} else if (self->type == ADDRESS_RANDOM_PRIVATE_NON_RESOLVABLE) {
|
||||
return (mp_obj_t)&bleio_addresstype_random_private_non_resolvable_obj;
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_type_obj, bleio_address_get_type);
|
||||
|
||||
const mp_obj_property_t bleio_address_type_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&bleio_address_get_type_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(bleio_address_locals_dict, bleio_address_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t bleio_address_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_Address,
|
||||
.print = bleio_address_print,
|
||||
.make_new = bleio_address_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&bleio_address_locals_dict
|
||||
};
|
34
shared-bindings/bleio/Address.h
Normal file
34
shared-bindings/bleio/Address.h
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* 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_BINDINGS_BLEIO_ADDRESS_H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H
|
||||
|
||||
#include "py/objtype.h"
|
||||
|
||||
extern const mp_obj_type_t bleio_address_type;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H
|
@ -33,7 +33,7 @@
|
||||
//|
|
||||
//| .. class:: bleio.AddressType
|
||||
//|
|
||||
//| Enum-like class to define the type of a BLE address.
|
||||
//| Enum-like class to define the type of a BLE address, see also `bleio.Address`.
|
||||
//|
|
||||
//| .. data:: PUBLIC
|
||||
//|
|
||||
|
@ -27,6 +27,7 @@
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "shared-bindings/bleio/__init__.h"
|
||||
#include "shared-bindings/bleio/Address.h"
|
||||
#include "shared-bindings/bleio/AddressType.h"
|
||||
#include "shared-bindings/bleio/Descriptor.h"
|
||||
#include "shared-bindings/bleio/UUID.h"
|
||||
@ -46,6 +47,7 @@
|
||||
//| .. toctree::
|
||||
//| :maxdepth: 3
|
||||
//|
|
||||
//| Address
|
||||
//| AddressType
|
||||
//| Adapter
|
||||
//| Descriptor
|
||||
@ -61,6 +63,7 @@
|
||||
|
||||
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Address), MP_ROM_PTR(&bleio_address_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_AddressType), MP_ROM_PTR(&bleio_addresstype_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },
|
||||
|
40
shared-module/bleio/Address.h
Normal file
40
shared-module/bleio/Address.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* 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_ATMEL_SAMD_SHARED_MODULE_BLEIO_ADDRESS_H
|
||||
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BLEIO_ADDRESS_H
|
||||
|
||||
#include "shared-bindings/bleio/AddressType.h"
|
||||
|
||||
#define BLEIO_ADDRESS_BYTES 6
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
bleio_address_type_t type;
|
||||
uint8_t value[BLEIO_ADDRESS_BYTES];
|
||||
} bleio_address_obj_t;
|
||||
|
||||
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BLEIO_ADDRESS_H
|
Loading…
x
Reference in New Issue
Block a user