2018-07-17 05:44:55 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-18 23:46:20 -04:00
|
|
|
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
|
2018-07-17 05:44:55 -04:00
|
|
|
* 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"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "shared-bindings/_bleio/Address.h"
|
|
|
|
#include "shared-module/_bleio/Address.h"
|
2018-07-17 05:44:55 -04:00
|
|
|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| class Address:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Encapsulates the address of a BLE device."""
|
2018-07-17 05:44:55 -04:00
|
|
|
|
2020-07-03 14:23:34 -04:00
|
|
|
//| def __init__(self, address: ReadableBuffer, address_type: int) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Create a new Address object encapsulating the address value.
|
|
|
|
//| The value itself can be one of:
|
2018-07-17 05:44:55 -04:00
|
|
|
//|
|
2021-12-21 15:28:49 -05:00
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer address: The address value to encapsulate. A buffer object (bytearray, bytes) of 6 bytes.
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param int address_type: one of the integer values: `PUBLIC`, `RANDOM_STATIC`,
|
|
|
|
//| `RANDOM_PRIVATE_RESOLVABLE`, or `RANDOM_PRIVATE_NON_RESOLVABLE`."""
|
|
|
|
//| ...
|
2021-10-15 14:43:12 -04:00
|
|
|
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 *all_args) {
|
2019-06-13 21:55:07 -04:00
|
|
|
enum { ARG_address, ARG_address_type };
|
2018-07-17 05:44:55 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-06-13 21:55:07 -04:00
|
|
|
{ MP_QSTR_address, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
|
|
|
{ MP_QSTR_address_type, MP_ARG_INT, {.u_int = BLEIO_ADDRESS_TYPE_PUBLIC } },
|
2018-07-17 05:44:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2019-01-14 21:08:45 -05:00
|
|
|
|
|
|
|
bleio_address_obj_t *self = m_new_obj(bleio_address_obj_t);
|
|
|
|
self->base.type = &bleio_address_type;
|
2018-07-17 05:44:55 -04:00
|
|
|
|
|
|
|
const mp_obj_t address = args[ARG_address].u_obj;
|
2019-06-10 07:18:28 -04:00
|
|
|
mp_buffer_info_t buf_info;
|
|
|
|
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
|
2019-06-13 21:55:07 -04:00
|
|
|
if (buf_info.len != NUM_BLEIO_ADDRESS_BYTES) {
|
|
|
|
mp_raise_ValueError_varg(translate("Address must be %d bytes long"), NUM_BLEIO_ADDRESS_BYTES);
|
2018-07-17 05:44:55 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:55:07 -04:00
|
|
|
const mp_int_t address_type = args[ARG_address_type].u_int;
|
|
|
|
if (address_type < BLEIO_ADDRESS_TYPE_MIN || address_type > BLEIO_ADDRESS_TYPE_MAX) {
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_arg_error_invalid(MP_QSTR_address_type);
|
2019-06-10 07:18:28 -04:00
|
|
|
}
|
2019-06-18 23:46:20 -04:00
|
|
|
|
2019-06-22 22:10:15 -04:00
|
|
|
common_hal_bleio_address_construct(self, buf_info.buf, address_type);
|
2018-07-17 05:44:55 -04:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| address_bytes: bytes
|
2020-07-02 14:11:25 -04:00
|
|
|
//| """The bytes that make up the device address (read-only).
|
2019-08-06 22:55:25 -04:00
|
|
|
//|
|
|
|
|
//| Note that the ``bytes`` object returned is in little-endian order:
|
|
|
|
//| The least significant byte is ``address_bytes[0]``. So the address will
|
|
|
|
//| appear to be reversed if you print the raw ``bytes`` object. If you print
|
2019-08-29 18:44:27 -04:00
|
|
|
//| or use `str()` on the :py:class:`~_bleio.Attribute` object itself, the address will be printed
|
2019-08-06 22:55:25 -04:00
|
|
|
//| in the expected order. For example:
|
|
|
|
//|
|
2020-09-13 12:56:11 -04:00
|
|
|
//| .. code-block:: python
|
2019-08-06 22:55:25 -04:00
|
|
|
//|
|
2019-08-29 18:44:27 -04:00
|
|
|
//| >>> import _bleio
|
|
|
|
//| >>> _bleio.adapter.address
|
2019-08-06 22:55:25 -04:00
|
|
|
//| <Address c8:1d:f5:ed:a8:35>
|
2019-08-29 18:44:27 -04:00
|
|
|
//| >>> _bleio.adapter.address.address_bytes
|
2020-05-12 20:15:28 -04:00
|
|
|
//| b'5\\xa8\\xed\\xf5\\x1d\\xc8'"""
|
2019-06-13 21:55:07 -04:00
|
|
|
STATIC mp_obj_t bleio_address_get_address_bytes(mp_obj_t self_in) {
|
2018-07-17 05:44:55 -04:00
|
|
|
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2019-06-18 23:46:20 -04:00
|
|
|
return common_hal_bleio_address_get_address_bytes(self);
|
2018-07-17 05:44:55 -04:00
|
|
|
}
|
2019-06-13 21:55:07 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_address_bytes_obj, bleio_address_get_address_bytes);
|
2018-07-17 05:44:55 -04:00
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_address_address_bytes_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_address_get_address_bytes_obj);
|
2019-06-22 22:10:15 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| type: int
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """The address type (read-only).
|
2019-09-14 15:40:24 -04:00
|
|
|
//|
|
|
|
|
//| One of the integer values: `PUBLIC`, `RANDOM_STATIC`, `RANDOM_PRIVATE_RESOLVABLE`,
|
2020-05-01 18:23:27 -04:00
|
|
|
//| or `RANDOM_PRIVATE_NON_RESOLVABLE`."""
|
2018-07-17 05:44:55 -04:00
|
|
|
STATIC mp_obj_t bleio_address_get_type(mp_obj_t self_in) {
|
|
|
|
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2019-06-18 23:46:20 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_address_get_type(self));
|
2018-07-17 05:44:55 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_type_obj, bleio_address_get_type);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_address_type_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_address_get_type_obj);
|
2018-07-17 05:44:55 -04:00
|
|
|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| def __eq__(self, other: object) -> bool:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Two Address objects are equal if their addresses and address types are equal."""
|
|
|
|
//| ...
|
2019-06-22 22:10:15 -04:00
|
|
|
STATIC mp_obj_t bleio_address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|
|
|
switch (op) {
|
|
|
|
// Two Addresses are equal if their address bytes and address_type are equal
|
|
|
|
case MP_BINARY_OP_EQUAL:
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_type(rhs_in, &bleio_address_type)) {
|
2019-06-22 22:10:15 -04:00
|
|
|
bleio_address_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in);
|
|
|
|
bleio_address_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in);
|
|
|
|
return mp_obj_new_bool(
|
|
|
|
mp_obj_equal(common_hal_bleio_address_get_address_bytes(lhs),
|
2021-03-15 09:57:36 -04:00
|
|
|
common_hal_bleio_address_get_address_bytes(rhs)) &&
|
2019-06-22 22:10:15 -04:00
|
|
|
common_hal_bleio_address_get_type(lhs) ==
|
|
|
|
common_hal_bleio_address_get_type(rhs));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 13:49:00 -04:00
|
|
|
//| def __hash__(self) -> int:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Returns a hash for the Address data."""
|
|
|
|
//| ...
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_address_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|
|
|
switch (op) {
|
|
|
|
// Two Addresses are equal if their address bytes and address_type are equal
|
|
|
|
case MP_UNARY_OP_HASH: {
|
|
|
|
mp_obj_t bytes = common_hal_bleio_address_get_address_bytes(MP_OBJ_TO_PTR(self_in));
|
|
|
|
GET_STR_HASH(bytes, h);
|
|
|
|
if (h == 0) {
|
|
|
|
GET_STR_DATA_LEN(bytes, data, len);
|
|
|
|
h = qstr_compute_hash(data, len);
|
|
|
|
}
|
|
|
|
h ^= common_hal_bleio_address_get_type(MP_OBJ_TO_PTR(self_in));
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(h);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:10:15 -04:00
|
|
|
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);
|
2019-07-31 00:30:24 -04:00
|
|
|
mp_buffer_info_t buf_info;
|
|
|
|
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
|
|
|
|
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
const uint8_t *buf = (uint8_t *)buf_info.buf;
|
2019-07-31 00:30:24 -04:00
|
|
|
mp_printf(print, "<Address %02x:%02x:%02x:%02x:%02x:%02x>",
|
2021-03-15 09:57:36 -04:00
|
|
|
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
|
2019-06-22 22:10:15 -04:00
|
|
|
}
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| PUBLIC: int
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """A publicly known address, with a company ID (high 24 bits)and company-assigned part (low 24 bits)."""
|
2019-07-16 19:53:36 -04:00
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| RANDOM_STATIC: int
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """A randomly generated address that does not change often. It may never change or may change after
|
|
|
|
//| a power cycle."""
|
2019-07-16 19:53:36 -04:00
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| RANDOM_PRIVATE_RESOLVABLE: int
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """An address that is usable when the peer knows the other device's secret Identity Resolving Key (IRK)."""
|
2019-07-16 19:53:36 -04:00
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| RANDOM_PRIVATE_NON_RESOLVABLE: int
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """A randomly generated address that changes on every connection."""
|
2018-07-17 05:44:55 -04:00
|
|
|
STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = {
|
2019-08-02 17:56:44 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_address_bytes_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) },
|
2019-06-13 21:55:07 -04:00
|
|
|
// These match the BLE_GAP_ADDR_TYPES values used by the nRF library.
|
2019-08-02 17:56:44 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_ROM_INT(0) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_RANDOM_STATIC), MP_ROM_INT(1) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_RESOLVABLE), MP_ROM_INT(2) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE), MP_ROM_INT(3) },
|
2019-06-13 21:55:07 -04:00
|
|
|
|
2018-07-17 05:44:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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 },
|
2021-07-09 15:59:37 -04:00
|
|
|
.flags = MP_TYPE_FLAG_EXTENDED,
|
2018-07-17 05:44:55 -04:00
|
|
|
.name = MP_QSTR_Address,
|
|
|
|
.make_new = bleio_address_make_new,
|
2019-06-22 22:10:15 -04:00
|
|
|
.print = bleio_address_print,
|
2021-07-06 11:57:35 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&bleio_address_locals_dict,
|
2021-07-12 07:57:59 -04:00
|
|
|
MP_TYPE_EXTENDED_FIELDS(
|
2021-07-06 11:57:35 -04:00
|
|
|
.unary_op = bleio_address_unary_op,
|
|
|
|
.binary_op = bleio_address_binary_op,
|
|
|
|
),
|
2018-07-17 05:44:55 -04:00
|
|
|
};
|