|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Glenn Ruben Bakke |
| 7 | + * Copyright (c) 2018 Artur Pacholec |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "ble_drv.h" |
| 29 | +#include "common-hal/bleio/UUID.h" |
| 30 | +#include "py/objstr.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "shared-bindings/bleio/UUID.h" |
| 33 | + |
| 34 | +#define UUID_STR_16BIT_LEN 6 |
| 35 | +#define UUID_STR_128BIT_LEN 36 |
| 36 | + |
| 37 | +static uint8_t xdigit_8b_value(byte nibble1, byte nibble2) { |
| 38 | + return unichar_xdigit_value(nibble1) | |
| 39 | + (unichar_xdigit_value(nibble2) << 4); |
| 40 | +} |
| 41 | + |
| 42 | +void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, const mp_obj_t *uuid) { |
| 43 | + if (MP_OBJ_IS_INT(*uuid)) { |
| 44 | + self->type = UUID_TYPE_16BIT; |
| 45 | + |
| 46 | + self->value[1] = (mp_obj_get_int(*uuid) >> 8) & 0xFF; |
| 47 | + self->value[0] = (mp_obj_get_int(*uuid) >> 0) & 0xFF; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (MP_OBJ_IS_STR(*uuid)) { |
| 52 | + GET_STR_DATA_LEN(*uuid, str_data, str_len); |
| 53 | + |
| 54 | + if (str_len == UUID_STR_16BIT_LEN) { |
| 55 | + self->type = UUID_TYPE_16BIT; |
| 56 | + |
| 57 | + self->value[0] = xdigit_8b_value(str_data[5], str_data[4]); |
| 58 | + self->value[1] = xdigit_8b_value(str_data[3], str_data[2]); |
| 59 | + } else if (str_len == UUID_STR_128BIT_LEN) { |
| 60 | + self->type = UUID_TYPE_128BIT; |
| 61 | + |
| 62 | + uint8_t buffer[16]; |
| 63 | + buffer[0] = xdigit_8b_value(str_data[35], str_data[34]); |
| 64 | + buffer[1] = xdigit_8b_value(str_data[33], str_data[32]); |
| 65 | + buffer[2] = xdigit_8b_value(str_data[31], str_data[30]); |
| 66 | + buffer[3] = xdigit_8b_value(str_data[29], str_data[28]); |
| 67 | + buffer[4] = xdigit_8b_value(str_data[27], str_data[26]); |
| 68 | + buffer[5] = xdigit_8b_value(str_data[25], str_data[24]); |
| 69 | + |
| 70 | + // 23 '-' |
| 71 | + buffer[6] = xdigit_8b_value(str_data[22], str_data[21]); |
| 72 | + buffer[7] = xdigit_8b_value(str_data[20], str_data[19]); |
| 73 | + |
| 74 | + // 18 '-' |
| 75 | + buffer[8] = xdigit_8b_value(str_data[17], str_data[16]); |
| 76 | + buffer[9] = xdigit_8b_value(str_data[15], str_data[14]); |
| 77 | + |
| 78 | + // 13 '-' |
| 79 | + buffer[10] = xdigit_8b_value(str_data[12], str_data[11]); |
| 80 | + buffer[11] = xdigit_8b_value(str_data[10], str_data[9]); |
| 81 | + |
| 82 | + // 8 '-' |
| 83 | + self->value[0] = xdigit_8b_value(str_data[7], str_data[6]); |
| 84 | + self->value[1] = xdigit_8b_value(str_data[5], str_data[4]); |
| 85 | + |
| 86 | + buffer[14] = xdigit_8b_value(str_data[3], str_data[2]); |
| 87 | + buffer[15] = xdigit_8b_value(str_data[1], str_data[0]); |
| 88 | + |
| 89 | + ble_drv_uuid_add_vs(buffer, &self->uuid_vs_idx); |
| 90 | + } else { |
| 91 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
| 92 | + "Invalid UUID string length")); |
| 93 | + } |
| 94 | + |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // deep copy |
| 99 | + if (MP_OBJ_IS_TYPE(*uuid, &bleio_uuid_type)) { |
| 100 | + bleio_uuid_obj_t *other = MP_OBJ_TO_PTR(*uuid); |
| 101 | + self->type = other->type; |
| 102 | + self->uuid_vs_idx = other->uuid_vs_idx; |
| 103 | + self->value[0] = other->value[0]; |
| 104 | + self->value[1] = other->value[1]; |
| 105 | + |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
| 110 | + "Invalid UUID parameter")); |
| 111 | +} |
| 112 | + |
| 113 | +void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print) { |
| 114 | + if (self->type == UUID_TYPE_16BIT) { |
| 115 | + mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ")", |
| 116 | + self->value[1], self->value[0]); |
| 117 | + } else { |
| 118 | + mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ", VS idx: " HEX2_FMT ")", |
| 119 | + self->value[1], self->value[0], self->uuid_vs_idx); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +bleio_uuid_type_t common_hal_bleio_uuid_get_type(bleio_uuid_obj_t *self) { |
| 124 | + return self->type; |
| 125 | +} |
0 commit comments