2018-07-18 19:01:41 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
|
2018-07-18 19:01:41 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ble_drv.h"
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "ble.h"
|
2018-11-20 23:04:58 -05:00
|
|
|
#include "py/runtime.h"
|
2018-12-27 00:04:04 -05:00
|
|
|
#include "common-hal/bleio/__init__.h"
|
2019-06-18 23:46:20 -04:00
|
|
|
#include "shared-bindings/bleio/Characteristic.h"
|
2019-08-02 17:56:44 -04:00
|
|
|
#include "shared-bindings/bleio/Descriptor.h"
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "shared-bindings/bleio/Service.h"
|
|
|
|
#include "shared-bindings/bleio/Adapter.h"
|
2018-07-18 19:01:41 -04:00
|
|
|
|
2019-06-18 23:46:20 -04:00
|
|
|
void common_hal_bleio_service_construct(bleio_service_obj_t *self, bleio_uuid_obj_t *uuid, mp_obj_list_t *characteristic_list, bool is_secondary) {
|
|
|
|
self->device = mp_const_none;
|
|
|
|
self->handle = 0xFFFF;
|
|
|
|
self->uuid = uuid;
|
|
|
|
self->characteristic_list = characteristic_list;
|
2019-07-27 13:20:59 -04:00
|
|
|
self->is_remote = false;
|
2019-06-18 23:46:20 -04:00
|
|
|
self->is_secondary = is_secondary;
|
|
|
|
|
|
|
|
for (size_t characteristic_idx = 0; characteristic_idx < characteristic_list->len; ++characteristic_idx) {
|
2019-07-07 00:07:47 -04:00
|
|
|
bleio_characteristic_obj_t *characteristic =
|
|
|
|
MP_OBJ_TO_PTR(characteristic_list->items[characteristic_idx]);
|
2019-07-09 00:21:46 -04:00
|
|
|
characteristic->service = self;
|
2019-06-18 23:46:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bleio_uuid_obj_t *common_hal_bleio_service_get_uuid(bleio_service_obj_t *self) {
|
|
|
|
return self->uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_list_t *common_hal_bleio_service_get_characteristic_list(bleio_service_obj_t *self) {
|
|
|
|
return self->characteristic_list;
|
|
|
|
}
|
|
|
|
|
2019-07-27 13:20:59 -04:00
|
|
|
bool common_hal_bleio_service_get_is_remote(bleio_service_obj_t *self) {
|
|
|
|
return self->is_remote;
|
|
|
|
}
|
|
|
|
|
2019-06-18 23:46:20 -04:00
|
|
|
bool common_hal_bleio_service_get_is_secondary(bleio_service_obj_t *self) {
|
|
|
|
return self->is_secondary;
|
|
|
|
}
|
|
|
|
|
2018-12-29 13:55:10 -05:00
|
|
|
// Call this after the Service has been added to the Peripheral.
|
2018-12-27 00:04:04 -05:00
|
|
|
void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self) {
|
|
|
|
// Add all the characteristics.
|
2019-07-07 00:07:47 -04:00
|
|
|
for (size_t characteristic_idx = 0; characteristic_idx < self->characteristic_list->len; ++characteristic_idx) {
|
|
|
|
bleio_characteristic_obj_t *characteristic =
|
|
|
|
MP_OBJ_TO_PTR(self->characteristic_list->items[characteristic_idx]);
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-08-07 23:49:09 -04:00
|
|
|
if (characteristic->handle != BLE_GATT_HANDLE_INVALID) {
|
|
|
|
mp_raise_ValueError(translate("Characteristic already in use by another Service."));
|
|
|
|
}
|
|
|
|
|
2018-12-27 00:04:04 -05:00
|
|
|
ble_gatts_char_md_t char_md = {
|
2019-08-07 11:10:21 -04:00
|
|
|
.char_props.broadcast = (characteristic->props & CHAR_PROP_BROADCAST) ? 1 : 0,
|
|
|
|
.char_props.read = (characteristic->props & CHAR_PROP_READ) ? 1 : 0,
|
|
|
|
.char_props.write_wo_resp = (characteristic->props & CHAR_PROP_WRITE_NO_RESPONSE) ? 1 : 0,
|
|
|
|
.char_props.write = (characteristic->props & CHAR_PROP_WRITE) ? 1 : 0,
|
|
|
|
.char_props.notify = (characteristic->props & CHAR_PROP_NOTIFY) ? 1 : 0,
|
|
|
|
.char_props.indicate = (characteristic->props & CHAR_PROP_INDICATE) ? 1 : 0,
|
2018-12-27 00:04:04 -05:00
|
|
|
};
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2018-12-27 00:04:04 -05:00
|
|
|
ble_gatts_attr_md_t cccd_md = {
|
|
|
|
.vloc = BLE_GATTS_VLOC_STACK,
|
|
|
|
};
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-08-02 17:56:44 -04:00
|
|
|
ble_uuid_t char_uuid;
|
|
|
|
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &char_uuid);
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-08-02 17:56:44 -04:00
|
|
|
ble_gatts_attr_md_t char_attr_md = {
|
2018-12-27 00:04:04 -05:00
|
|
|
.vloc = BLE_GATTS_VLOC_STACK,
|
2019-08-07 23:49:09 -04:00
|
|
|
.vlen = !characteristic->fixed_length,
|
2018-12-27 00:04:04 -05:00
|
|
|
};
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-08-25 21:38:13 -04:00
|
|
|
if (char_md.char_props.notify || char_md.char_props.indicate) {
|
|
|
|
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
|
|
|
|
// Make CCCD write permission match characteristic read permission.
|
|
|
|
bleio_attribute_gatts_set_security_mode(&cccd_md.write_perm, characteristic->read_perm);
|
|
|
|
|
|
|
|
char_md.p_cccd_md = &cccd_md;
|
|
|
|
}
|
|
|
|
|
|
|
|
bleio_attribute_gatts_set_security_mode(&char_attr_md.read_perm, characteristic->read_perm);
|
|
|
|
bleio_attribute_gatts_set_security_mode(&char_attr_md.write_perm, characteristic->write_perm);
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-08-07 23:49:09 -04:00
|
|
|
mp_buffer_info_t char_value_bufinfo;
|
|
|
|
mp_get_buffer_raise(characteristic->value, &char_value_bufinfo, MP_BUFFER_READ);
|
|
|
|
|
2019-08-02 17:56:44 -04:00
|
|
|
ble_gatts_attr_t char_attr = {
|
|
|
|
.p_uuid = &char_uuid,
|
|
|
|
.p_attr_md = &char_attr_md,
|
2019-08-07 23:49:09 -04:00
|
|
|
.init_len = char_value_bufinfo.len,
|
|
|
|
.p_value = char_value_bufinfo.buf,
|
|
|
|
.init_offs = 0,
|
|
|
|
.max_len = characteristic->max_length,
|
2018-12-27 00:04:04 -05:00
|
|
|
};
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-08-02 17:56:44 -04:00
|
|
|
ble_gatts_char_handles_t char_handles;
|
2018-12-27 00:04:04 -05:00
|
|
|
|
|
|
|
uint32_t err_code;
|
2019-08-02 17:56:44 -04:00
|
|
|
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &char_attr, &char_handles);
|
2018-12-27 00:04:04 -05:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
mp_raise_OSError_msg_varg(translate("Failed to add characteristic, err 0x%04x"), err_code);
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:56:44 -04:00
|
|
|
characteristic->user_desc_handle = char_handles.user_desc_handle;
|
|
|
|
characteristic->cccd_handle = char_handles.cccd_handle;
|
|
|
|
characteristic->sccd_handle = char_handles.sccd_handle;
|
|
|
|
characteristic->handle = char_handles.value_handle;
|
|
|
|
|
|
|
|
// Add the descriptors for this characteristic.
|
|
|
|
for (size_t descriptor_idx = 0; descriptor_idx < characteristic->descriptor_list->len; ++descriptor_idx) {
|
|
|
|
bleio_descriptor_obj_t *descriptor =
|
|
|
|
MP_OBJ_TO_PTR(characteristic->descriptor_list->items[descriptor_idx]);
|
|
|
|
|
|
|
|
ble_uuid_t desc_uuid;
|
|
|
|
bleio_uuid_convert_to_nrf_ble_uuid(descriptor->uuid, &desc_uuid);
|
|
|
|
|
|
|
|
ble_gatts_attr_md_t desc_attr_md = {
|
|
|
|
// Data passed is not in a permanent location and should be copied.
|
|
|
|
.vloc = BLE_GATTS_VLOC_STACK,
|
2019-08-07 23:49:09 -04:00
|
|
|
.vlen = !descriptor->fixed_length,
|
2019-08-02 17:56:44 -04:00
|
|
|
};
|
|
|
|
|
2019-08-25 21:38:13 -04:00
|
|
|
bleio_attribute_gatts_set_security_mode(&desc_attr_md.read_perm, descriptor->read_perm);
|
|
|
|
bleio_attribute_gatts_set_security_mode(&desc_attr_md.write_perm, descriptor->write_perm);
|
2019-08-02 17:56:44 -04:00
|
|
|
|
2019-08-07 23:49:09 -04:00
|
|
|
mp_buffer_info_t desc_value_bufinfo;
|
|
|
|
mp_get_buffer_raise(descriptor->value, &desc_value_bufinfo, MP_BUFFER_READ);
|
2019-08-02 17:56:44 -04:00
|
|
|
|
|
|
|
ble_gatts_attr_t desc_attr = {
|
|
|
|
.p_uuid = &desc_uuid,
|
|
|
|
.p_attr_md = &desc_attr_md,
|
2019-08-07 23:49:09 -04:00
|
|
|
.init_len = desc_value_bufinfo.len,
|
|
|
|
.p_value = desc_value_bufinfo.buf,
|
2019-08-02 17:56:44 -04:00
|
|
|
.init_offs = 0,
|
2019-08-07 23:49:09 -04:00
|
|
|
.max_len = descriptor->max_length,
|
2019-08-02 17:56:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
err_code = sd_ble_gatts_descriptor_add(characteristic->handle, &desc_attr, &descriptor->handle);
|
2019-08-07 23:49:09 -04:00
|
|
|
|
|
|
|
} // loop over descriptors
|
|
|
|
|
|
|
|
} // loop over characteristics
|
2018-07-18 19:01:41 -04:00
|
|
|
}
|