2018-07-16 08:44:20 -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) 2018 Dan Halbert for Adafruit Industries
|
2018-07-16 08:44:20 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2016 Glenn Ruben Bakke
|
2018-07-16 08:44:20 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-11-15 17:00:12 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-07-16 08:44:20 -04:00
|
|
|
#include "py/runtime.h"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "common-hal/_bleio/UUID.h"
|
2019-09-14 15:40:24 -04:00
|
|
|
#include "shared-bindings/_bleio/__init__.h"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "shared-bindings/_bleio/Adapter.h"
|
2018-07-16 08:44:20 -04:00
|
|
|
|
2018-11-15 17:00:12 -05:00
|
|
|
#include "ble.h"
|
|
|
|
#include "ble_drv.h"
|
|
|
|
#include "nrf_error.h"
|
2018-07-16 08:44:20 -04:00
|
|
|
|
2018-11-15 17:00:12 -05:00
|
|
|
// If uuid128 is NULL, this is a Bluetooth SIG 16-bit UUID.
|
|
|
|
// If uuid128 is not NULL, it's a 128-bit (16-byte) UUID, with bytes 12 and 13 zero'd out, where
|
|
|
|
// the 16-bit part goes. Those 16 bits are passed in uuid16.
|
2020-08-08 00:29:37 -04:00
|
|
|
void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, mp_int_t uuid16, const uint8_t uuid128[16]) {
|
2018-11-26 21:09:17 -05:00
|
|
|
self->nrf_ble_uuid.uuid = uuid16;
|
|
|
|
if (uuid128 == NULL) {
|
|
|
|
self->nrf_ble_uuid.type = BLE_UUID_TYPE_BLE;
|
|
|
|
} else {
|
2018-11-15 17:00:12 -05:00
|
|
|
ble_uuid128_t vs_uuid;
|
|
|
|
memcpy(vs_uuid.uuid128, uuid128, sizeof(vs_uuid.uuid128));
|
|
|
|
|
|
|
|
// Register this vendor-specific UUID. Bytes 12 and 13 will be zero.
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(sd_ble_uuid_vs_add(&vs_uuid, &self->nrf_ble_uuid.type));
|
2019-09-14 15:40:24 -04:00
|
|
|
vm_used_ble = true;
|
2018-07-16 08:44:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 23:04:58 -05:00
|
|
|
uint32_t common_hal_bleio_uuid_get_size(bleio_uuid_obj_t *self) {
|
2018-11-26 21:09:17 -05:00
|
|
|
return self->nrf_ble_uuid.type == BLE_UUID_TYPE_BLE ? 16 : 128;
|
2018-11-15 17:00:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t common_hal_bleio_uuid_get_uuid16(bleio_uuid_obj_t *self) {
|
2018-11-26 21:09:17 -05:00
|
|
|
return self->nrf_ble_uuid.uuid;
|
2018-07-16 08:44:20 -04:00
|
|
|
}
|
2018-11-20 23:04:58 -05:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
void common_hal_bleio_uuid_get_uuid128(bleio_uuid_obj_t *self, uint8_t uuid128[16]) {
|
2018-11-26 21:09:17 -05:00
|
|
|
uint8_t length;
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(sd_ble_uuid_encode(&self->nrf_ble_uuid, &length, uuid128));
|
2018-11-20 23:04:58 -05:00
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
void common_hal_bleio_uuid_pack_into(bleio_uuid_obj_t *self, uint8_t* buf) {
|
|
|
|
if (self->nrf_ble_uuid.type == BLE_UUID_TYPE_BLE) {
|
|
|
|
buf[0] = self->nrf_ble_uuid.uuid & 0xff;
|
|
|
|
buf[1] = self->nrf_ble_uuid.uuid >> 8;
|
|
|
|
} else {
|
|
|
|
common_hal_bleio_uuid_get_uuid128(self, buf);
|
|
|
|
}
|
2018-11-26 21:09:17 -05:00
|
|
|
}
|
2018-11-20 23:04:58 -05:00
|
|
|
|
2018-11-26 21:09:17 -05:00
|
|
|
void bleio_uuid_construct_from_nrf_ble_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_ble_uuid) {
|
|
|
|
if (nrf_ble_uuid->type == BLE_UUID_TYPE_UNKNOWN) {
|
2019-11-20 16:53:50 -05:00
|
|
|
mp_raise_bleio_BluetoothError(translate("Unexpected nrfx uuid type"));
|
2018-11-20 23:04:58 -05:00
|
|
|
}
|
2018-11-26 21:09:17 -05:00
|
|
|
self->nrf_ble_uuid.uuid = nrf_ble_uuid->uuid;
|
|
|
|
self->nrf_ble_uuid.type = nrf_ble_uuid->type;
|
2018-11-20 23:04:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in a ble_uuid_t from my values.
|
2018-11-26 21:09:17 -05:00
|
|
|
void bleio_uuid_convert_to_nrf_ble_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_ble_uuid) {
|
|
|
|
nrf_ble_uuid->uuid = self->nrf_ble_uuid.uuid;
|
|
|
|
nrf_ble_uuid->type = self->nrf_ble_uuid.type;
|
2018-11-20 23:04:58 -05:00
|
|
|
}
|