|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Artur Pacholec |
| 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 <string.h> |
| 28 | +#include <stdio.h> |
| 29 | + |
| 30 | +#include "py/objproperty.h" |
| 31 | +#include "py/objstr.h" |
| 32 | +#include "py/runtime.h" |
| 33 | +#include "shared-bindings/bleio/Address.h" |
| 34 | +#include "shared-module/bleio/Address.h" |
| 35 | + |
| 36 | +#define ADDRESS_LONG_LEN 17 // XX:XX:XX:XX:XX:XX |
| 37 | +#define ADDRESS_SHORT_LEN 12 // XXXXXXXXXXXX |
| 38 | + |
| 39 | +//| .. currentmodule:: bleio |
| 40 | +//| |
| 41 | +//| :class:`Address` -- BLE address |
| 42 | +//| ========================================================= |
| 43 | +//| |
| 44 | +//| Encapsulates the address of a BLE device. |
| 45 | +//| |
| 46 | + |
| 47 | +//| .. class:: Address(address) |
| 48 | +//| |
| 49 | +//| Create a new Address object encapsulating the address value. |
| 50 | +//| The value itself can be one of: |
| 51 | +//| |
| 52 | +//| - a `str` value in the format of 'XXXXXXXXXXXX' or 'XX:XX:XX:XX:XX' |
| 53 | +//| - a `bytes` or `bytearray` containing 6 bytes |
| 54 | +//| - another Address object |
| 55 | +//| |
| 56 | +//| :param address: The address to encapsulate |
| 57 | +//| |
| 58 | + |
| 59 | +//| .. attribute:: type |
| 60 | +//| |
| 61 | +//| The address type. One of: |
| 62 | +//| |
| 63 | +//| - `bleio.AddressType.PUBLIC` |
| 64 | +//| - `bleio.AddressType.RANDOM_STATIC` |
| 65 | +//| - `bleio.AddressType.RANDOM_PRIVATE_RESOLVABLE` |
| 66 | +//| - `bleio.AddressType.RANDOM_PRIVATE_NON_RESOLVABLE` |
| 67 | +//| |
| 68 | +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) { |
| 69 | + mp_arg_check_num(n_args, n_kw, 1, 1, true); |
| 70 | + bleio_address_obj_t *self = m_new_obj(bleio_address_obj_t); |
| 71 | + self->base.type = &bleio_address_type; |
| 72 | + self->type = ADDRESS_PUBLIC; |
| 73 | + |
| 74 | + mp_map_t kw_args; |
| 75 | + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); |
| 76 | + |
| 77 | + enum { ARG_address }; |
| 78 | + static const mp_arg_t allowed_args[] = { |
| 79 | + { ARG_address, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 80 | + }; |
| 81 | + |
| 82 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 83 | + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 84 | + |
| 85 | + const mp_obj_t address = args[ARG_address].u_obj; |
| 86 | + |
| 87 | + if (MP_OBJ_IS_STR(address)) { |
| 88 | + GET_STR_DATA_LEN(address, str_data, str_len); |
| 89 | + const bool is_long = (str_len == ADDRESS_LONG_LEN); |
| 90 | + const bool is_short = (str_len == ADDRESS_SHORT_LEN); |
| 91 | + |
| 92 | + if (is_long || is_short) { |
| 93 | + size_t i = str_len - 1; |
| 94 | + for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) { |
| 95 | + self->value[b] = unichar_xdigit_value(str_data[i]) | |
| 96 | + unichar_xdigit_value(str_data[i - 1]) << 4; |
| 97 | + |
| 98 | + i -= is_long ? 3 : 2; |
| 99 | + } |
| 100 | + } else { |
| 101 | + mp_raise_ValueError("Wrong address length"); |
| 102 | + } |
| 103 | + } else if (MP_OBJ_IS_TYPE(address, &mp_type_bytearray) || MP_OBJ_IS_TYPE(address, &mp_type_bytes)) { |
| 104 | + mp_buffer_info_t buf_info; |
| 105 | + mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ); |
| 106 | + if (buf_info.len != BLEIO_ADDRESS_BYTES) { |
| 107 | + mp_raise_ValueError("Wrong number of bytes provided"); |
| 108 | + } |
| 109 | + |
| 110 | + for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) { |
| 111 | + self->value[BLEIO_ADDRESS_BYTES - b - 1] = ((uint8_t*)buf_info.buf)[b]; |
| 112 | + } |
| 113 | + } else if (MP_OBJ_IS_TYPE(address, &bleio_address_type)) { |
| 114 | + // deep copy |
| 115 | + bleio_address_obj_t *other = MP_OBJ_TO_PTR(address); |
| 116 | + self->type = other->type; |
| 117 | + memcpy(self->value, other->value, BLEIO_ADDRESS_BYTES); |
| 118 | + } |
| 119 | + |
| 120 | + return MP_OBJ_FROM_PTR(self); |
| 121 | +} |
| 122 | + |
| 123 | +STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 124 | + bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 125 | + |
| 126 | + mp_printf(print, "Address('"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT"')", |
| 127 | + self->value[5], self->value[4], self->value[3], |
| 128 | + self->value[2], self->value[1], self->value[0]); |
| 129 | +} |
| 130 | + |
| 131 | +STATIC mp_obj_t bleio_address_get_type(mp_obj_t self_in) { |
| 132 | + bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 133 | + |
| 134 | + if (self->type == ADDRESS_PUBLIC) { |
| 135 | + return (mp_obj_t)&bleio_addresstype_public_obj; |
| 136 | + } else if (self->type == ADDRESS_RANDOM_STATIC) { |
| 137 | + return (mp_obj_t)&bleio_addresstype_random_static_obj; |
| 138 | + } else if (self->type == ADDRESS_RANDOM_PRIVATE_RESOLVABLE) { |
| 139 | + return (mp_obj_t)&bleio_addresstype_random_private_resolvable_obj; |
| 140 | + } else if (self->type == ADDRESS_RANDOM_PRIVATE_NON_RESOLVABLE) { |
| 141 | + return (mp_obj_t)&bleio_addresstype_random_private_non_resolvable_obj; |
| 142 | + } |
| 143 | + |
| 144 | + return mp_const_none; |
| 145 | +} |
| 146 | +MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_type_obj, bleio_address_get_type); |
| 147 | + |
| 148 | +const mp_obj_property_t bleio_address_type_obj = { |
| 149 | + .base.type = &mp_type_property, |
| 150 | + .proxy = {(mp_obj_t)&bleio_address_get_type_obj, |
| 151 | + (mp_obj_t)&mp_const_none_obj, |
| 152 | + (mp_obj_t)&mp_const_none_obj}, |
| 153 | +}; |
| 154 | + |
| 155 | +STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = { |
| 156 | + { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) }, |
| 157 | +}; |
| 158 | + |
| 159 | +STATIC MP_DEFINE_CONST_DICT(bleio_address_locals_dict, bleio_address_locals_dict_table); |
| 160 | + |
| 161 | +const mp_obj_type_t bleio_address_type = { |
| 162 | + { &mp_type_type }, |
| 163 | + .name = MP_QSTR_Address, |
| 164 | + .print = bleio_address_print, |
| 165 | + .make_new = bleio_address_make_new, |
| 166 | + .locals_dict = (mp_obj_dict_t*)&bleio_address_locals_dict |
| 167 | +}; |
0 commit comments