|
| 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 <stdio.h> |
| 28 | + |
| 29 | +#include "ble_drv.h" |
| 30 | +#include "ble_gap.h" |
| 31 | +#include "ble_gatt.h" |
| 32 | +#include "ble_types.h" |
| 33 | +#include "py/runtime.h" |
| 34 | +#include "shared-bindings/bleio/Characteristic.h" |
| 35 | +#include "shared-bindings/bleio/Device.h" |
| 36 | +#include "shared-bindings/bleio/Service.h" |
| 37 | +#include "shared-bindings/bleio/UUID.h" |
| 38 | + |
| 39 | +static volatile bool m_disc_evt_received; |
| 40 | + |
| 41 | +STATIC void gap_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) { |
| 42 | + if (event_id == BLE_GAP_EVT_CONNECTED) { |
| 43 | + device->conn_handle = conn_handle; |
| 44 | + } else if (event_id == BLE_GAP_EVT_DISCONNECTED) { |
| 45 | + device->conn_handle = BLE_CONN_HANDLE_INVALID; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +STATIC void gatts_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) { |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +STATIC void gattc_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) { |
| 54 | + m_disc_evt_received = true; |
| 55 | +} |
| 56 | + |
| 57 | +STATIC void disc_add_service(bleio_device_obj_t *device, ble_drv_service_data_t * service_data) { |
| 58 | + bleio_service_obj_t *service = m_new_obj(bleio_service_obj_t); |
| 59 | + service->base.type = &bleio_service_type; |
| 60 | + |
| 61 | + bleio_uuid_obj_t *uuid = m_new_obj(bleio_uuid_obj_t); |
| 62 | + uuid->base.type = &bleio_uuid_type; |
| 63 | + uuid->type = (service_data->uuid_type == BLE_UUID_TYPE_BLE) ? UUID_TYPE_16BIT : UUID_TYPE_128BIT; |
| 64 | + uuid->value[0] = service_data->uuid & 0xFF; |
| 65 | + uuid->value[1] = service_data->uuid >> 8; |
| 66 | + |
| 67 | + service->char_list = mp_obj_new_list(0, NULL); |
| 68 | + service->uuid = uuid; |
| 69 | + service->device = device; |
| 70 | + service->handle = service_data->start_handle; |
| 71 | + service->start_handle = service_data->start_handle; |
| 72 | + service->end_handle = service_data->end_handle; |
| 73 | + |
| 74 | + mp_obj_list_append(device->service_list, service); |
| 75 | +} |
| 76 | + |
| 77 | +STATIC void disc_add_char(bleio_service_obj_t *service, ble_drv_char_data_t *chara_data) { |
| 78 | + bleio_characteristic_obj_t *chara = m_new_obj(bleio_characteristic_obj_t); |
| 79 | + chara->base.type = &bleio_characteristic_type; |
| 80 | + |
| 81 | + bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t); |
| 82 | + p_uuid->base.type = &bleio_uuid_type; |
| 83 | + |
| 84 | + chara->uuid = p_uuid; |
| 85 | + |
| 86 | + p_uuid->type = chara_data->uuid_type; |
| 87 | + p_uuid->value[0] = chara_data->uuid & 0xFF; |
| 88 | + p_uuid->value[1] = chara_data->uuid >> 8; |
| 89 | + |
| 90 | + // add characteristic specific data from discovery |
| 91 | + chara->props.broadcast = chara_data->props.broadcast; |
| 92 | + chara->props.indicate = chara_data->props.indicate; |
| 93 | + chara->props.notify = chara_data->props.notify; |
| 94 | + chara->props.read = chara_data->props.read; |
| 95 | + chara->props.write = chara_data->props.write; |
| 96 | + chara->props.write_wo_resp = chara_data->props.write_wo_resp; |
| 97 | + chara->handle = chara_data->value_handle; |
| 98 | + |
| 99 | + chara->service_handle = service->handle; |
| 100 | + chara->service = service; |
| 101 | + |
| 102 | + mp_obj_list_append(service->char_list, MP_OBJ_FROM_PTR(chara)); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +void common_hal_bleio_device_start_advertising(bleio_device_obj_t *device, bleio_advertisement_data_t *adv_data) { |
| 107 | + if (adv_data->connectable) { |
| 108 | + ble_drv_gap_event_handler_set(device, gap_event_handler); |
| 109 | + ble_drv_gatts_event_handler_set(device, gatts_event_handler); |
| 110 | + } |
| 111 | + |
| 112 | + ble_drv_advertise_data(adv_data); |
| 113 | +} |
| 114 | + |
| 115 | +void common_hal_bleio_device_stop_advertising(bleio_device_obj_t *device) { |
| 116 | + (void)device; |
| 117 | + |
| 118 | + ble_drv_advertise_stop(); |
| 119 | +} |
| 120 | + |
| 121 | +void common_hal_bleio_device_connect(bleio_device_obj_t *device) { |
| 122 | + ble_drv_gap_event_handler_set(device, gap_event_handler); |
| 123 | + |
| 124 | + ble_drv_connect(device); |
| 125 | + |
| 126 | + while (device->conn_handle == BLE_CONN_HANDLE_INVALID) { |
| 127 | + run_background_tasks(); |
| 128 | +// __asm volatile ("wfi"); |
| 129 | + } |
| 130 | + |
| 131 | + ble_drv_gattc_event_handler_set(device, gattc_event_handler); |
| 132 | + |
| 133 | + // TODO: read name |
| 134 | + |
| 135 | + // find services |
| 136 | + bool found_service = ble_drv_discover_services(device, BLE_GATT_HANDLE_START, disc_add_service); |
| 137 | + while (found_service) { |
| 138 | + const mp_obj_list_t *service_list = MP_OBJ_TO_PTR(device->service_list); |
| 139 | + const bleio_service_obj_t *service = service_list->items[service_list->len - 1]; |
| 140 | + |
| 141 | + found_service = ble_drv_discover_services(device, service->end_handle + 1, disc_add_service); |
| 142 | + } |
| 143 | + |
| 144 | + // find characteristics in each service |
| 145 | + const mp_obj_list_t *service_list = MP_OBJ_TO_PTR(device->service_list); |
| 146 | + for (size_t i = 0; i < service_list->len; ++i) { |
| 147 | + bleio_service_obj_t *service = service_list->items[i]; |
| 148 | + |
| 149 | + bool found_char = ble_drv_discover_characteristic(device, service, service->start_handle, disc_add_char); |
| 150 | + while (found_char) { |
| 151 | + const mp_obj_list_t *char_list = MP_OBJ_TO_PTR(service->char_list); |
| 152 | + const bleio_characteristic_obj_t *characteristic = char_list->items[char_list->len - 1]; |
| 153 | + |
| 154 | + const uint16_t next_handle = characteristic->handle + 1; |
| 155 | + if (next_handle >= service->end_handle) { |
| 156 | + break; |
| 157 | + } |
| 158 | + |
| 159 | + found_char = ble_drv_discover_characteristic(device, service, next_handle, disc_add_char); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +void common_hal_bleio_device_disconnect(bleio_device_obj_t *device) { |
| 165 | + ble_drv_disconnect(device); |
| 166 | +} |
0 commit comments