|
34 | 34 | #include "nrfx_power.h" |
35 | 35 | #include "nrf_nvic.h" |
36 | 36 | #include "nrf_sdm.h" |
| 37 | +#include "py/objstr.h" |
37 | 38 | #include "py/runtime.h" |
38 | | -#include "shared-bindings/bleio/Adapter.h" |
39 | | - |
40 | 39 | #include "supervisor/usb.h" |
| 40 | +#include "shared-bindings/bleio/Adapter.h" |
| 41 | +#include "shared-bindings/bleio/Address.h" |
41 | 42 |
|
42 | 43 | STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) { |
43 | 44 | mp_raise_msg_varg(&mp_type_AssertionError, |
@@ -132,20 +133,42 @@ bool common_hal_bleio_adapter_get_enabled(void) { |
132 | 133 | return is_enabled; |
133 | 134 | } |
134 | 135 |
|
135 | | -void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address) { |
136 | | - ble_gap_addr_t local_address; |
| 136 | +void get_address(ble_gap_addr_t *address) { |
137 | 137 | uint32_t err_code; |
138 | 138 |
|
139 | 139 | common_hal_bleio_adapter_set_enabled(true); |
140 | | - err_code = sd_ble_gap_addr_get(&local_address); |
| 140 | + err_code = sd_ble_gap_addr_get(address); |
141 | 141 |
|
142 | 142 | if (err_code != NRF_SUCCESS) { |
143 | 143 | mp_raise_OSError_msg(translate("Failed to get local address")); |
144 | 144 | } |
| 145 | +} |
| 146 | + |
| 147 | +bleio_address_obj_t *common_hal_bleio_adapter_get_address(void) { |
| 148 | + common_hal_bleio_adapter_set_enabled(true); |
| 149 | + |
| 150 | + ble_gap_addr_t local_address; |
| 151 | + get_address(&local_address); |
| 152 | + |
| 153 | + bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t); |
| 154 | + address->base.type = &bleio_address_type; |
| 155 | + |
| 156 | + common_hal_bleio_address_construct(address, local_address.addr, local_address.addr_type); |
| 157 | + return address; |
| 158 | +} |
| 159 | + |
| 160 | +mp_obj_t common_hal_bleio_adapter_get_default_name(void) { |
| 161 | + common_hal_bleio_adapter_set_enabled(true); |
| 162 | + |
| 163 | + ble_gap_addr_t local_address; |
| 164 | + get_address(&local_address); |
| 165 | + |
| 166 | + char name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0, 0 }; |
145 | 167 |
|
146 | | - address->type = local_address.addr_type; |
| 168 | + name[sizeof(name) - 4] = nibble_to_hex_lower[local_address.addr[1] >> 4 & 0xf]; |
| 169 | + name[sizeof(name) - 3] = nibble_to_hex_lower[local_address.addr[1] & 0xf]; |
| 170 | + name[sizeof(name) - 2] = nibble_to_hex_lower[local_address.addr[0] >> 4 & 0xf]; |
| 171 | + name[sizeof(name) - 1] = nibble_to_hex_lower[local_address.addr[0] & 0xf]; |
147 | 172 |
|
148 | | - mp_buffer_info_t buf_info; |
149 | | - mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ); |
150 | | - memcpy(address->bytes, buf_info.buf, NUM_BLEIO_ADDRESS_BYTES); |
| 173 | + return mp_obj_new_str(name, sizeof(name)); |
151 | 174 | } |
0 commit comments