|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Dan Halbert for Adafruit Industries |
| 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 <string.h> |
| 29 | +#include <stdio.h> |
| 30 | + |
| 31 | +#include "ble.h" |
| 32 | +#include "ble_drv.h" |
| 33 | +#include "ble_hci.h" |
| 34 | +#include "nrf_soc.h" |
| 35 | +#include "py/objstr.h" |
| 36 | +#include "py/runtime.h" |
| 37 | +#include "shared-bindings/bleio/Adapter.h" |
| 38 | +#include "shared-bindings/bleio/Characteristic.h" |
| 39 | +#include "shared-bindings/bleio/Central.h" |
| 40 | +#include "shared-bindings/bleio/Service.h" |
| 41 | +#include "shared-bindings/bleio/UUID.h" |
| 42 | + |
| 43 | +static bleio_service_obj_t *m_char_discovery_service; |
| 44 | +static volatile bool m_discovery_successful; |
| 45 | + |
| 46 | +STATIC bool discover_next_services(bleio_central_obj_t *self, uint16_t start_handle) { |
| 47 | + m_discovery_successful = false; |
| 48 | + |
| 49 | + uint32_t err_code = sd_ble_gattc_primary_services_discover(self->conn_handle, start_handle, NULL); |
| 50 | + if (err_code != NRF_SUCCESS) { |
| 51 | + mp_raise_OSError_msg(translate("Failed to discover services")); |
| 52 | + } |
| 53 | + |
| 54 | + // Serialize discovery. |
| 55 | + err_code = sd_mutex_acquire(&m_discovery_mutex); |
| 56 | + if (err_code != NRF_SUCCESS) { |
| 57 | + mp_raise_OSError_msg(translate("Failed to acquire mutex")); |
| 58 | + } |
| 59 | + |
| 60 | + // Wait for someone else to release m_discovery_mutex. |
| 61 | + while (sd_mutex_acquire(&m_discovery_mutex) == NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN) { |
| 62 | +#ifdef MICROPY_VM_HOOK_LOOP |
| 63 | + MICROPY_VM_HOOK_LOOP |
| 64 | +#endif |
| 65 | + } |
| 66 | + |
| 67 | + err_code = sd_mutex_release(&m_discovery_mutex); |
| 68 | + if (err_code != NRF_SUCCESS) { |
| 69 | + mp_raise_OSError_msg(translate("Failed to release mutex")); |
| 70 | + } |
| 71 | + |
| 72 | + return m_discovery_successful; |
| 73 | +} |
| 74 | + |
| 75 | +STATIC bool discover_next_characteristics(bleio_central_obj_t *self, bleio_service_obj_t *service, uint16_t start_handle) { |
| 76 | + m_char_discovery_service = service; |
| 77 | + |
| 78 | + ble_gattc_handle_range_t handle_range; |
| 79 | + handle_range.start_handle = start_handle; |
| 80 | + handle_range.end_handle = service->end_handle; |
| 81 | + |
| 82 | + m_discovery_successful = false; |
| 83 | + |
| 84 | + uint32_t err_code = sd_ble_gattc_characteristics_discover(self->conn_handle, &handle_range); |
| 85 | + if (err_code != NRF_SUCCESS) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + err_code = sd_mutex_acquire(&m_discovery_mutex); |
| 90 | + if (err_code != NRF_SUCCESS) { |
| 91 | + mp_raise_OSError_msg(translate("Failed to acquire mutex")); |
| 92 | + } |
| 93 | + |
| 94 | + while (sd_mutex_acquire(&m_discovery_mutex) == NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN) { |
| 95 | + MICROPY_VM_HOOK_LOOP; |
| 96 | + } |
| 97 | + |
| 98 | + err_code = sd_mutex_release(&m_discovery_mutex); |
| 99 | + if (err_code != NRF_SUCCESS) { |
| 100 | + mp_raise_OSError_msg(translate("Failed to release mutex")); |
| 101 | + } |
| 102 | + |
| 103 | + return m_discovery_successful; |
| 104 | +} |
| 105 | + |
| 106 | +STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *response, bleio_central_obj_t *central) { |
| 107 | + for (size_t i = 0; i < response->count; ++i) { |
| 108 | + ble_gattc_service_t *gattc_service = &response->services[i]; |
| 109 | + |
| 110 | + bleio_service_obj_t *service = m_new_obj(bleio_service_obj_t); |
| 111 | + service->base.type = &bleio_service_type; |
| 112 | + service->device = central; |
| 113 | + service->characteristic_list = mp_obj_new_list(0, NULL); |
| 114 | + service->start_handle = gattc_service->handle_range.start_handle; |
| 115 | + service->end_handle = gattc_service->handle_range.end_handle; |
| 116 | + service->handle = gattc_service->handle_range.start_handle; |
| 117 | + |
| 118 | + bleio_uuid_obj_t *uuid = m_new_obj(bleio_uuid_obj_t); |
| 119 | + bleio_uuid_construct_from_nrf_ble_uuid(uuid, &gattc_service->uuid); |
| 120 | + service->uuid = uuid; |
| 121 | + |
| 122 | + mp_obj_list_append(central->service_list, service); |
| 123 | + } |
| 124 | + |
| 125 | + if (response->count > 0) { |
| 126 | + m_discovery_successful = true; |
| 127 | + } |
| 128 | + |
| 129 | + const uint32_t err_code = sd_mutex_release(&m_discovery_mutex); |
| 130 | + if (err_code != NRF_SUCCESS) { |
| 131 | + mp_raise_OSError_msg(translate("Failed to release mutex")); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, bleio_central_obj_t *central) { |
| 136 | + for (size_t i = 0; i < response->count; ++i) { |
| 137 | + ble_gattc_char_t *gattc_char = &response->chars[i]; |
| 138 | + |
| 139 | + bleio_characteristic_obj_t *characteristic = m_new_obj(bleio_characteristic_obj_t); |
| 140 | + characteristic->base.type = &bleio_characteristic_type; |
| 141 | + |
| 142 | + bleio_uuid_obj_t *uuid = m_new_obj(bleio_uuid_obj_t); |
| 143 | + uuid->base.type = &bleio_uuid_type; |
| 144 | + bleio_uuid_construct_from_nrf_ble_uuid(uuid, &gattc_char->uuid); |
| 145 | + characteristic->uuid = uuid; |
| 146 | + |
| 147 | + characteristic->props.broadcast = gattc_char->char_props.broadcast; |
| 148 | + characteristic->props.indicate = gattc_char->char_props.indicate; |
| 149 | + characteristic->props.notify = gattc_char->char_props.notify; |
| 150 | + characteristic->props.read = gattc_char->char_props.read; |
| 151 | + characteristic->props.write = gattc_char->char_props.write; |
| 152 | + characteristic->props.write_no_response = gattc_char->char_props.write_wo_resp; |
| 153 | + characteristic->handle = gattc_char->handle_value; |
| 154 | + characteristic->service = m_char_discovery_service; |
| 155 | + |
| 156 | + mp_obj_list_append(m_char_discovery_service->characteristic_list, MP_OBJ_FROM_PTR(characteristic)); |
| 157 | + } |
| 158 | + |
| 159 | + if (response->count > 0) { |
| 160 | + m_discovery_successful = true; |
| 161 | + } |
| 162 | + |
| 163 | + const uint32_t err_code = sd_mutex_release(&m_discovery_mutex); |
| 164 | + if (err_code != NRF_SUCCESS) { |
| 165 | + mp_raise_OSError_msg(translate("Failed to release mutex")); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +STATIC void on_ble_evt(ble_evt_t *ble_evt, void *central_in) { |
| 170 | + bleio_central_obj_t *central = (bleio_central_obj_t*)central_in; |
| 171 | + |
| 172 | + switch (ble_evt->header.evt_id) { |
| 173 | + case BLE_GAP_EVT_CONNECTED: |
| 174 | + { |
| 175 | + ble_gap_conn_params_t conn_params; |
| 176 | + central->conn_handle = ble_evt->evt.gap_evt.conn_handle; |
| 177 | + |
| 178 | + sd_ble_gap_ppcp_get(&conn_params); |
| 179 | + sd_ble_gap_conn_param_update(ble_evt->evt.gap_evt.conn_handle, &conn_params); |
| 180 | + break; |
| 181 | + } |
| 182 | + |
| 183 | + case BLE_GAP_EVT_TIMEOUT: |
| 184 | + if (central->attempting_to_connect) { |
| 185 | + // Signal that connection attempt has timed out. |
| 186 | + central->attempting_to_connect = false; |
| 187 | + } |
| 188 | + break; |
| 189 | + |
| 190 | + case BLE_GAP_EVT_DISCONNECTED: |
| 191 | + central->conn_handle = BLE_CONN_HANDLE_INVALID; |
| 192 | + break; |
| 193 | + |
| 194 | + case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: |
| 195 | + on_primary_srv_discovery_rsp(&ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp, central); |
| 196 | + break; |
| 197 | + |
| 198 | + case BLE_GATTC_EVT_CHAR_DISC_RSP: |
| 199 | + on_char_discovery_rsp(&ble_evt->evt.gattc_evt.params.char_disc_rsp, central); |
| 200 | + break; |
| 201 | + |
| 202 | + case BLE_GATTS_EVT_SYS_ATTR_MISSING: |
| 203 | + sd_ble_gatts_sys_attr_set(ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0); |
| 204 | + break; |
| 205 | + |
| 206 | + case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: |
| 207 | + sd_ble_gatts_exchange_mtu_reply(central->conn_handle, BLE_GATT_ATT_MTU_DEFAULT); |
| 208 | + break; |
| 209 | + |
| 210 | + case BLE_GAP_EVT_SEC_PARAMS_REQUEST: |
| 211 | + sd_ble_gap_sec_params_reply(central->conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL); |
| 212 | + break; |
| 213 | + |
| 214 | + case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: |
| 215 | + { |
| 216 | + ble_gap_evt_conn_param_update_request_t *request = &ble_evt->evt.gap_evt.params.conn_param_update_request; |
| 217 | + sd_ble_gap_conn_param_update(central->conn_handle, &request->conn_params); |
| 218 | + break; |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +void common_hal_bleio_central_connect(bleio_central_obj_t *self, mp_float_t timeout) { |
| 224 | + common_hal_bleio_adapter_set_enabled(true); |
| 225 | + ble_drv_add_event_handler(on_ble_evt, self); |
| 226 | + |
| 227 | + ble_gap_scan_params_t scan_params = { |
| 228 | + .interval = MSEC_TO_UNITS(100, UNIT_0_625_MS), |
| 229 | + .window = MSEC_TO_UNITS(100, UNIT_0_625_MS), |
| 230 | + .scan_phys = BLE_GAP_PHY_1MBPS, |
| 231 | + // timeout of 0 means no timeout |
| 232 | + .timeout = SEC_TO_UNITS(timeout, UNIT_10_MS), |
| 233 | + }; |
| 234 | + |
| 235 | + ble_gap_conn_params_t conn_params = { |
| 236 | + .conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS), |
| 237 | + .min_conn_interval = MSEC_TO_UNITS(15, UNIT_1_25_MS), |
| 238 | + .max_conn_interval = MSEC_TO_UNITS(300, UNIT_1_25_MS), |
| 239 | + .slave_latency = 0, // number of conn events |
| 240 | + }; |
| 241 | + |
| 242 | + self->attempting_to_connect = true; |
| 243 | + |
| 244 | + uint32_t err_code = sd_ble_gap_connect(&scan_params, &m_scan_buffer); |
| 245 | + |
| 246 | + if (err_code != NRF_SUCCESS) { |
| 247 | + mp_raise_OSError_msg_varg(translate("Failed to start connecting, error 0x%04x"), err_code); |
| 248 | + } |
| 249 | + |
| 250 | + while (self->conn_handle == BLE_CONN_HANDLE_INVALID && self->attempting_to_connect) { |
| 251 | + #ifdef MICROPY_VM_HOOK_LOOP |
| 252 | + MICROPY_VM_HOOK_LOOP |
| 253 | + #endif |
| 254 | + } |
| 255 | + |
| 256 | + if (!self->attempting_to_connect) { |
| 257 | + mp_raise_OSError_msg(translate("Failed to connect: timeout")); |
| 258 | + } |
| 259 | + |
| 260 | + // Conenction successful. |
| 261 | + // Now discover all services on the remote peripheral. Ask for services repeatedly |
| 262 | + // until no more are left. |
| 263 | + |
| 264 | + uint16_t next_start_handle; |
| 265 | + |
| 266 | + next_start_handle = BLE_GATT_HANDLE_START; |
| 267 | + |
| 268 | + while (1) { |
| 269 | + if(!discover_next_services(self, discovery_start_handle)) { |
| 270 | + break; |
| 271 | + } |
| 272 | + |
| 273 | + // discover_next_services() appends to service_list. |
| 274 | + const mp_obj_list_t *service_list = MP_OBJ_TO_PTR(self->service_list); |
| 275 | + |
| 276 | + // Get the most recently discovered service, and ask for services with handles |
| 277 | + // starting after the last attribute handle of t |
| 278 | + const bleio_service_obj_t *service = service_list->items[service_list->len - 1]; |
| 279 | + next_start_handle = service->end_handle + 1; |
| 280 | + } |
| 281 | + |
| 282 | + // Now, for each service, discover its characteristics. |
| 283 | + // find characteristics in each service |
| 284 | + const mp_obj_list_t *service_list = MP_OBJ_TO_PTR(self->service_list); |
| 285 | + for (size_t i = 0; i < service_list->len; ++i) { |
| 286 | + bleio_service_obj_t *service = service_list->items[i]; |
| 287 | + |
| 288 | + next_start_handle = service->start_handle; |
| 289 | + |
| 290 | + while (1) { |
| 291 | + if (!discover_next_characteristics(self, service, service->start_handle)) { |
| 292 | + break; |
| 293 | + } |
| 294 | + |
| 295 | + // discover_next_characteristics() appends to the characteristic_list. |
| 296 | + const mp_obj_list_t *characteristic_list = MP_OBJ_TO_PTR(service->characteristic_list); |
| 297 | + |
| 298 | + // Get the most recently discovered characteristic. |
| 299 | + const bleio_characteristic_obj_t *characteristic = |
| 300 | + characteristic_list->items[characteristic_list->len - 1]; |
| 301 | + next_start_handle = characteristic->handle + 1; |
| 302 | + if (next_start_handle >= service->end_handle) { |
| 303 | + // Went past the end of the range of handles for this service. |
| 304 | + break; |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | +} |
| 309 | + |
| 310 | +void common_hal_bleio_central_disconnect(bleio_central_obj_t *self) { |
| 311 | + sd_ble_gap_disconnect(self->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); |
| 312 | +} |
0 commit comments