|
25 | 25 | * THE SOFTWARE. |
26 | 26 | */ |
27 | 27 |
|
28 | | -#include "ble.h" |
29 | | -#include "ble_drv.h" |
30 | | -#include "common-hal/bleio/UUID.h" |
31 | | -#include "nrf_error.h" |
32 | | -#include "py/objstr.h" |
| 28 | +#include <string.h> |
| 29 | + |
33 | 30 | #include "py/runtime.h" |
| 31 | +#include "common-hal/bleio/UUID.h" |
34 | 32 | #include "shared-bindings/bleio/Adapter.h" |
35 | | -#include "shared-bindings/bleio/UUID.h" |
36 | | - |
37 | | -#define UUID_STR_16BIT_LEN 6 |
38 | | -#define UUID_STR_128BIT_LEN 36 |
39 | | - |
40 | | -static uint8_t xdigit_8b_value(byte nibble1, byte nibble2) { |
41 | | - return unichar_xdigit_value(nibble1) | |
42 | | - (unichar_xdigit_value(nibble2) << 4); |
43 | | -} |
44 | | - |
45 | | -void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, const mp_obj_t *uuid) { |
46 | | - if (MP_OBJ_IS_INT(*uuid)) { |
47 | | - self->type = UUID_TYPE_16BIT; |
48 | | - |
49 | | - self->value[1] = (mp_obj_get_int(*uuid) >> 8) & 0xFF; |
50 | | - self->value[0] = (mp_obj_get_int(*uuid) >> 0) & 0xFF; |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - if (MP_OBJ_IS_STR(*uuid)) { |
55 | | - GET_STR_DATA_LEN(*uuid, str_data, str_len); |
56 | | - |
57 | | - if (str_len == UUID_STR_16BIT_LEN) { |
58 | | - self->type = UUID_TYPE_16BIT; |
59 | | - |
60 | | - self->value[0] = xdigit_8b_value(str_data[5], str_data[4]); |
61 | | - self->value[1] = xdigit_8b_value(str_data[3], str_data[2]); |
62 | | - } else if (str_len == UUID_STR_128BIT_LEN) { |
63 | | - self->type = UUID_TYPE_128BIT; |
64 | 33 |
|
65 | | - ble_uuid128_t vs_uuid; |
66 | | - vs_uuid.uuid128[0] = xdigit_8b_value(str_data[35], str_data[34]); |
67 | | - vs_uuid.uuid128[1] = xdigit_8b_value(str_data[33], str_data[32]); |
68 | | - vs_uuid.uuid128[2] = xdigit_8b_value(str_data[31], str_data[30]); |
69 | | - vs_uuid.uuid128[3] = xdigit_8b_value(str_data[29], str_data[28]); |
70 | | - vs_uuid.uuid128[4] = xdigit_8b_value(str_data[27], str_data[26]); |
71 | | - vs_uuid.uuid128[5] = xdigit_8b_value(str_data[25], str_data[24]); |
72 | | - |
73 | | - // 23 '-' |
74 | | - vs_uuid.uuid128[6] = xdigit_8b_value(str_data[22], str_data[21]); |
75 | | - vs_uuid.uuid128[7] = xdigit_8b_value(str_data[20], str_data[19]); |
76 | | - |
77 | | - // 18 '-' |
78 | | - vs_uuid.uuid128[8] = xdigit_8b_value(str_data[17], str_data[16]); |
79 | | - vs_uuid.uuid128[9] = xdigit_8b_value(str_data[15], str_data[14]); |
80 | | - |
81 | | - // 13 '-' |
82 | | - vs_uuid.uuid128[10] = xdigit_8b_value(str_data[12], str_data[11]); |
83 | | - vs_uuid.uuid128[11] = xdigit_8b_value(str_data[10], str_data[9]); |
84 | | - |
85 | | - // 8 '-' |
86 | | - self->value[0] = xdigit_8b_value(str_data[7], str_data[6]); |
87 | | - self->value[1] = xdigit_8b_value(str_data[5], str_data[4]); |
88 | | - |
89 | | - vs_uuid.uuid128[14] = xdigit_8b_value(str_data[3], str_data[2]); |
90 | | - vs_uuid.uuid128[15] = xdigit_8b_value(str_data[1], str_data[0]); |
91 | | - |
92 | | - common_hal_bleio_adapter_set_enabled(true); |
93 | | - |
94 | | - const uint32_t err_code = sd_ble_uuid_vs_add(&vs_uuid, &self->uuid_vs_idx); |
95 | | - if (err_code != NRF_SUCCESS) { |
96 | | - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, |
97 | | - translate("Failed to add Vendor Specific UUID, status: 0x%08lX"), err_code)); |
98 | | - } |
99 | | - |
100 | | - } else { |
101 | | - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
102 | | - translate("Invalid UUID string length"))); |
103 | | - } |
| 34 | +#include "ble.h" |
| 35 | +#include "ble_drv.h" |
| 36 | +#include "nrf_error.h" |
104 | 37 |
|
105 | | - return; |
| 38 | +// If uuid128 is NULL, this is a Bluetooth SIG 16-bit UUID. |
| 39 | +// If uuid128 is not NULL, it's a 128-bit (16-byte) UUID, with bytes 12 and 13 zero'd out, where |
| 40 | +// the 16-bit part goes. Those 16 bits are passed in uuid16. |
| 41 | +void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, uint32_t uuid16, uint8_t uuid128[]) { |
| 42 | + self->uuid16 = uuid16; |
| 43 | + self->uuid_vs_idx = 0; |
| 44 | + if (uuid128 != NULL) { |
| 45 | + ble_uuid128_t vs_uuid; |
| 46 | + memcpy(vs_uuid.uuid128, uuid128, sizeof(vs_uuid.uuid128)); |
| 47 | + |
| 48 | + // Register this vendor-specific UUID. Bytes 12 and 13 will be zero. |
| 49 | + common_hal_bleio_adapter_set_enabled(true); |
| 50 | + const uint32_t err_code = sd_ble_uuid_vs_add(&vs_uuid, &self->uuid_vs_idx); |
| 51 | + if (err_code != NRF_SUCCESS) { |
| 52 | + mp_raise_OSError(&mp_type_OSError, translate("Could not register Vendor-Specific UUID")); |
106 | 53 | } |
107 | | - |
108 | | - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
109 | | - translate("Invalid UUID parameter"))); |
110 | 54 | } |
111 | 55 |
|
112 | 56 | void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print) { |
113 | | - if (self->type == UUID_TYPE_16BIT) { |
114 | | - mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ")", |
115 | | - self->value[1], self->value[0]); |
| 57 | + if (self->uuid_vs_idx != 0) { |
| 58 | + mp_printf(print, "UUID(uuid16: 0x%04x, Vendor-Specific index: " HEX2_FMT ")", |
| 59 | + self->uuid16, self->uuid_vs_idx); |
116 | 60 | } else { |
117 | | - mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ", VS idx: " HEX2_FMT ")", |
118 | | - self->value[1], self->value[0], self->uuid_vs_idx); |
| 61 | + mp_printf(print, "UUID16(0x%04x)", self->uuid16); |
119 | 62 | } |
120 | 63 | } |
121 | 64 |
|
122 | | -bleio_uuid_type_t common_hal_bleio_uuid_get_type(bleio_uuid_obj_t *self) { |
123 | | - return self->type; |
| 65 | +bool common_hal_bleio_uuid_get_vendor_specific(bleio_uuid_obj_t *self) { |
| 66 | + return self->uuid_vs_idx != 0; |
| 67 | +} |
| 68 | + |
| 69 | +uint32_t common_hal_bleio_uuid_get_uuid16(bleio_uuid_obj_t *self) { |
| 70 | + return self->uuid16; |
124 | 71 | } |
0 commit comments