diff --git a/CMakeLists.txt b/CMakeLists.txt index b6c03b6..7d7b97a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,11 @@ add_library(sim-base STATIC sim/nrfx/mdk/nrf52_bitfields.h # nrf/components/libraries/timer sim/libraries/gpiote/app_gpiote.h # includes hal/nrf_gpio.h + # nibmle + sim/host/ble_gap.h + sim/host/ble_gatt.h + sim/host/ble_gatt.cpp + sim/host/ble_uuid.h ) # include the generated lv_conf.h file before anything else target_include_directories(sim-base PUBLIC "${CMAKE_CURRENT_BINARY_DIR}") # lv_conf.h @@ -128,8 +133,6 @@ target_sources(infinisim PUBLIC sim/components/ble/NavigationService.cpp sim/components/ble/NimbleController.h sim/components/ble/NimbleController.cpp - sim/components/ble/SimpleWeatherService.h - sim/components/ble/SimpleWeatherService.cpp sim/components/brightness/BrightnessController.h sim/components/brightness/BrightnessController.cpp sim/components/firmwarevalidator/FirmwareValidator.h @@ -196,6 +199,8 @@ target_sources(infinisim PUBLIC ${InfiniTime_DIR}/src/components/settings/Settings.cpp ${InfiniTime_DIR}/src/components/ble/NotificationManager.h ${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp + ${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.h + ${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.cpp ${InfiniTime_DIR}/src/components/fs/FS.h ${InfiniTime_DIR}/src/components/fs/FS.cpp ${InfiniTime_DIR}/src/components/motor/MotorController.h diff --git a/main.cpp b/main.cpp index f410c73..ffc4299 100644 --- a/main.cpp +++ b/main.cpp @@ -25,6 +25,10 @@ #include #include +# // be sure to get the sim headers for SimpleWeatherService.h +#include "host/ble_gatt.h" +#include "host/ble_uuid.h" + #include "BootloaderVersion.h" #include "components/battery/BatteryController.h" #include "components/ble/BleController.h" @@ -57,6 +61,9 @@ #include #include #include +#include +#include +#include #include // std::pow // additional includes for 'saveScreenshot()' function @@ -806,11 +813,77 @@ class Framework { batteryController.voltage = batteryController.percentRemaining * 50; } + void write_uint64(std::span data, uint64_t val) + { + assert(data.size() >= 8); + for (int i=0; i<8; i++) + { + data[i] = (val >> (i*8)) & 0xff; + } + } + void write_int16(std::span data, int16_t val) + { + assert(data.size() >= 2); + data[0] = val & 0xff; + data[1] = (val >> 8) & 0xff; + } + void set_current_weather(uint64_t timestamp, int16_t temperature, int iconId) + { + std::array dataBuffer {}; + std::span data(dataBuffer); + os_mbuf buffer; + ble_gatt_access_ctxt ctxt; + ctxt.om = &buffer; + buffer.om_data = dataBuffer.data(); + + // fill buffer with specified format + int16_t minTemperature = temperature; + int16_t maxTemperature = temperature; + dataBuffer.at(0) = 0; // MessageType::CurrentWeather + dataBuffer.at(1) = 0; // Vesion 0 + write_uint64(data.subspan(2), timestamp); + write_int16(data.subspan(10), temperature); + write_int16(data.subspan(12), minTemperature); + write_int16(data.subspan(14), maxTemperature); + dataBuffer.at(48) = static_cast(iconId); + + // send weather to SimpleWeatherService + systemTask.nimble().weather().OnCommand(&ctxt); + } + void set_forecast( + uint64_t timestamp, + std::array< + Pinetime::Controllers::SimpleWeatherService::Forecast::Day, + Pinetime::Controllers::SimpleWeatherService::MaxNbForecastDays> days) + { + std::array dataBuffer {}; + std::span data(dataBuffer); + os_mbuf buffer; + ble_gatt_access_ctxt ctxt; + ctxt.om = &buffer; + buffer.om_data = dataBuffer.data(); + + // fill buffer with specified format + dataBuffer.at(0) = 1; // MessageType::Forecast + dataBuffer.at(1) = 0; // Vesion 0 + write_uint64(data.subspan(2), timestamp); + dataBuffer.at(10) = static_cast(days.size()); + for (int i = 0; i < days.size(); i++) + { + const Pinetime::Controllers::SimpleWeatherService::Forecast::Day &day = days.at(i); + write_int16(data.subspan(11+(i*5)), day.minTemperature); + write_int16(data.subspan(13+(i*5)), day.maxTemperature); + dataBuffer.at(15+(i*5)) = static_cast(day.iconId); + } + // send Forecast to SimpleWeatherService + systemTask.nimble().weather().OnCommand(&ctxt); + } + void generate_weather_data(bool clear) { if (clear) { - systemTask.nimble().weather().SetCurrentWeather(0, 0, 0); + set_current_weather(0, 0, 0); std::array days; - systemTask.nimble().weather().SetForecast(0, days); + set_forecast(0, days); return; } auto timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); @@ -818,7 +891,7 @@ class Framework { // Generate current weather data int16_t temperature = (rand() % 81 - 40) * 100; - systemTask.nimble().weather().SetCurrentWeather((uint64_t)timestamp, temperature, rand() % 9); + set_current_weather((uint64_t)timestamp, temperature, rand() % 9); // Generate forecast data std::array days; @@ -827,7 +900,7 @@ class Framework { (int16_t)(temperature - rand() % 10 * 100), (int16_t)(temperature + rand() % 10 * 100), Pinetime::Controllers::SimpleWeatherService::Icons(rand() % 9) }; } - systemTask.nimble().weather().SetForecast((uint64_t)timestamp, days); + set_forecast((uint64_t)timestamp, days); } void handle_touch_and_button() { diff --git a/sim/components/ble/SimpleWeatherService.cpp b/sim/components/ble/SimpleWeatherService.cpp deleted file mode 100644 index 66e22fb..0000000 --- a/sim/components/ble/SimpleWeatherService.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include "components/ble/SimpleWeatherService.h" - -#include -#include -#include -#include - -using namespace Pinetime::Controllers; - -namespace { -enum class MessageType : uint8_t { CurrentWeather, Forecast, Unknown }; - -uint64_t ToUInt64(const uint8_t* data) { - return data[0] + (data[1] << 8) + (data[2] << 16) + (data[3] << 24) + (static_cast(data[4]) << 32) + - (static_cast(data[5]) << 40) + (static_cast(data[6]) << 48) + (static_cast(data[7]) << 56); -} - -int16_t ToInt16(const uint8_t* data) { - return data[0] + (data[1] << 8); -} - -SimpleWeatherService::CurrentWeather CreateCurrentWeather(const uint8_t* dataBuffer) { - SimpleWeatherService::Location cityName; - std::memcpy(cityName.data(), &dataBuffer[16], 32); - cityName[32] = '\0'; - return SimpleWeatherService::CurrentWeather(ToUInt64(&dataBuffer[2]), - ToInt16(&dataBuffer[10]), - ToInt16(&dataBuffer[12]), - ToInt16(&dataBuffer[14]), - SimpleWeatherService::Icons {dataBuffer[16 + 32]}, - std::move(cityName)); -} - -SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) { - auto timestamp = static_cast(ToUInt64(&dataBuffer[2])); - - std::array days; - const uint8_t nbDaysInBuffer = dataBuffer[10]; - const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer); - for (int i = 0; i < nbDays; i++) { - days[i] = SimpleWeatherService::Forecast::Day {ToInt16(&dataBuffer[11 + (i * 5)]), - ToInt16(&dataBuffer[13 + (i * 5)]), - SimpleWeatherService::Icons {dataBuffer[15 + (i * 5)]}}; - } - return SimpleWeatherService::Forecast {timestamp, nbDays, days}; -} - -MessageType GetMessageType(const uint8_t* data) { - auto messageType = static_cast(*data); - if (messageType > MessageType::Unknown) { - return MessageType::Unknown; - } - return messageType; -} - -uint8_t GetVersion(const uint8_t* dataBuffer) { - return dataBuffer[1]; -} -} - -int WeatherCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble_gatt_access_ctxt* ctxt, void* arg) { - return static_cast(arg)->OnCommand(ctxt); -} - -SimpleWeatherService::SimpleWeatherService(DateTime& dateTimeController) : dateTimeController(dateTimeController) { -} - -void SimpleWeatherService::Init() { - //ble_gatts_count_cfg(serviceDefinition); - //ble_gatts_add_svcs(serviceDefinition); -} - -void SimpleWeatherService::SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId) { - SimpleWeatherService::Location cityName; - cityName[32] = '\0'; - currentWeather = SimpleWeatherService::CurrentWeather((uint64_t)timestamp, temperature, temperature, temperature, SimpleWeatherService::Icons(iconId), std::move(cityName)); - printf("currentWeather: timestamp=%d, temperature=%d, icon=%d\n", currentWeather->timestamp, currentWeather->temperature, currentWeather->iconId); -} - -void SimpleWeatherService::SetForecast(uint64_t timestamp, std::array days) { - forecast = SimpleWeatherService::Forecast {timestamp, SimpleWeatherService::MaxNbForecastDays, days}; - for (int i = 0; i < SimpleWeatherService::MaxNbForecastDays; i++) { - printf("forecast: day=%d. min=%d, max=%d icon=%d\n", i, days[i].minTemperature, days[i].maxTemperature, days[i].iconId); - } -} - -int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) { - - return 0; -} - -std::optional SimpleWeatherService::Current() const { - if (currentWeather) { - auto currentTime = dateTimeController.UTCDateTime().time_since_epoch(); - auto weatherTpSecond = std::chrono::seconds {currentWeather->timestamp}; - auto weatherTp = std::chrono::duration_cast(weatherTpSecond); - auto delta = currentTime - weatherTp; - - if (delta < std::chrono::hours {24}) { - return currentWeather; - } - } - return {}; -} - -std::optional SimpleWeatherService::GetForecast() const { - if (forecast) { - auto currentTime = dateTimeController.UTCDateTime().time_since_epoch(); - auto weatherTpSecond = std::chrono::seconds {forecast->timestamp}; - auto weatherTp = std::chrono::duration_cast(weatherTpSecond); - auto delta = currentTime - weatherTp; - - if (delta < std::chrono::hours {24}) { - return this->forecast; - } - } - return {}; -} - -bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService::CurrentWeather& other) const { - return this->iconId == other.iconId && this->temperature == other.temperature && this->timestamp == other.timestamp && - this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature && - std::strcmp(this->location.data(), other.location.data()) == 0; -} - -bool SimpleWeatherService::Forecast::Day::operator==(const SimpleWeatherService::Forecast::Day& other) const { - return this->iconId == other.iconId && - this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature; -} - -bool SimpleWeatherService::Forecast::operator==(const SimpleWeatherService::Forecast& other) const { - for (int i = 0; i < this->nbDays; i++) { - if (this->days[i] != other.days[i]) { - return false; - } - } - return this->timestamp == other.timestamp && this->nbDays == other.nbDays; -} diff --git a/sim/components/ble/SimpleWeatherService.h b/sim/components/ble/SimpleWeatherService.h deleted file mode 100644 index 1f123c1..0000000 --- a/sim/components/ble/SimpleWeatherService.h +++ /dev/null @@ -1,136 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -//#define min // workaround: nimble's min/max macros conflict with libstdc++ -//#define max -//#include -//#include -#include -#include -#include -//#undef max -//#undef min - -#include "components/datetime/DateTimeController.h" - -int WeatherCallback(uint16_t connHandle, uint16_t attrHandle, struct ble_gatt_access_ctxt* ctxt, void* arg); - -namespace Pinetime { -namespace Controllers { - -class SimpleWeatherService { -public: - explicit SimpleWeatherService(DateTime& dateTimeController); - - void Init(); - - int OnCommand(struct ble_gatt_access_ctxt* ctxt); - - static constexpr uint8_t MaxNbForecastDays = 5; - - enum class Icons : uint8_t { - Sun = 0, // ClearSky - CloudsSun = 1, // FewClouds - Clouds = 2, // Scattered clouds - BrokenClouds = 3, - CloudShowerHeavy = 4, // shower rain - CloudSunRain = 5, // rain - Thunderstorm = 6, - Snow = 7, - Smog = 8, // Mist - Unknown = 255 - }; - - using Location = std::array; // 32 char + \0 (end of string) - - struct CurrentWeather { - CurrentWeather(uint64_t timestamp, - int16_t temperature, - int16_t minTemperature, - int16_t maxTemperature, - Icons iconId, - Location&& location) - : timestamp {timestamp}, - temperature {temperature}, - minTemperature {minTemperature}, - maxTemperature {maxTemperature}, - iconId {iconId}, - location {std::move(location)} { - } - - uint64_t timestamp; - int16_t temperature; - int16_t minTemperature; - int16_t maxTemperature; - Icons iconId; - Location location; - - bool operator==(const CurrentWeather& other) const; - }; - - struct Forecast { - uint64_t timestamp; - uint8_t nbDays; - - struct Day { - int16_t minTemperature; - int16_t maxTemperature; - Icons iconId; - - bool operator==(const Day& other) const; - }; - - std::array days; - - bool operator==(const Forecast& other) const; - }; - - void SetCurrentWeather(uint64_t timestamp, int16_t temperature, int iconId); - void SetForecast(uint64_t timestamp, std::array days); - - std::optional Current() const; - std::optional GetForecast() const; - - static int16_t CelsiusToFahrenheit(int16_t celsius) { - return celsius * 9 / 5 + 3200; - } - -private: - // 00050000-78fc-48fe-8e23-433b3a1942d0 - //static constexpr ble_uuid128_t BaseUuid() { - // return CharUuid(0x00, 0x00); - //} - - // 0005yyxx-78fc-48fe-8e23-433b3a1942d0 - //static constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) { - // return ble_uuid128_t {.u = {.type = BLE_UUID_TYPE_128}, - // .value = {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, y, x, 0x05, 0x00}}; - //} - - //ble_uuid128_t weatherUuid {BaseUuid()}; - - //ble_uuid128_t weatherDataCharUuid {CharUuid(0x00, 0x01)}; - - //const struct ble_gatt_chr_def characteristicDefinition[2] = {{.uuid = &weatherDataCharUuid.u, - // .access_cb = WeatherCallback, - // .arg = this, - // .flags = BLE_GATT_CHR_F_WRITE, - // .val_handle = &eventHandle}, - // {0}}; - //const struct ble_gatt_svc_def serviceDefinition[2] = { - // {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &weatherUuid.u, .characteristics = characteristicDefinition}, - // {0}}; - - uint16_t eventHandle {}; - - Pinetime::Controllers::DateTime& dateTimeController; - - std::optional currentWeather; - std::optional forecast; -}; -} -} diff --git a/sim/host/ble_gap.h b/sim/host/ble_gap.h new file mode 100644 index 0000000..59a73dd --- /dev/null +++ b/sim/host/ble_gap.h @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_BLE_GAP_ +#define H_BLE_GAP_ + +/** + * @brief Bluetooth Host Generic Access Profile (GAP) + * @defgroup bt_host_gap Bluetooth Host Generic Access Profile (GAP) + * @ingroup bt_host + * @{ + */ + +#include +#include "host/ble_gatt.h" + +#ifdef __cplusplus +extern "C" { +#endif + + + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif diff --git a/sim/host/ble_gatt.cpp b/sim/host/ble_gatt.cpp new file mode 100644 index 0000000..e2bf35c --- /dev/null +++ b/sim/host/ble_gatt.cpp @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "host/ble_gatt.h" + +int +ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs) +{ + //struct ble_gatt_resources res = { 0 }; + //int rc; + + //rc = ble_gatts_count_resources(defs, &res); + //if (rc != 0) { + // return rc; + //} + + //ble_hs_max_services += res.svcs; + //ble_hs_max_attrs += res.attrs; + + ///* Reserve an extra CCCD for the cache. */ + //ble_hs_max_client_configs += + // res.cccds * (MYNEWT_VAL(BLE_MAX_CONNECTIONS) + 1); + + return 0; +} + +int +ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs) +{ +// void *p; +// int rc; +// +// ble_hs_lock(); +// if (!ble_gatts_mutable()) { +// rc = BLE_HS_EBUSY; +// goto done; +// } +// +// p = realloc(ble_gatts_svc_defs, +// (ble_gatts_num_svc_defs + 1) * sizeof *ble_gatts_svc_defs); +// if (p == NULL) { +// rc = BLE_HS_ENOMEM; +// goto done; +// } +// +// ble_gatts_svc_defs = p; +// ble_gatts_svc_defs[ble_gatts_num_svc_defs] = svcs; +// ble_gatts_num_svc_defs++; +// +// rc = 0; +// +//done: +// ble_hs_unlock(); +// return rc; + return 0; +} \ No newline at end of file diff --git a/sim/host/ble_gatt.h b/sim/host/ble_gatt.h new file mode 100644 index 0000000..402ffdd --- /dev/null +++ b/sim/host/ble_gatt.h @@ -0,0 +1,933 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_BLE_GATT_ +#define H_BLE_GATT_ + +/** + * @brief Bluetooth Generic Attribute Profile (GATT) + * @defgroup bt_gatt Bluetooth Generic Attribute Profile (GATT) + * @ingroup bt_host + * @{ + */ + +#include +//#include "host/ble_att.h" +#include "host/ble_uuid.h" +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_hs_conn; +struct ble_att_error_rsp; +struct ble_hs_cfg; + +#define BLE_GATT_REGISTER_OP_SVC 1 +#define BLE_GATT_REGISTER_OP_CHR 2 +#define BLE_GATT_REGISTER_OP_DSC 3 + +#define BLE_GATT_SVC_UUID16 0x1801 +#define BLE_GATT_DSC_CLT_CFG_UUID16 0x2902 + +#define BLE_GATT_CHR_PROP_BROADCAST 0x01 +#define BLE_GATT_CHR_PROP_READ 0x02 +#define BLE_GATT_CHR_PROP_WRITE_NO_RSP 0x04 +#define BLE_GATT_CHR_PROP_WRITE 0x08 +#define BLE_GATT_CHR_PROP_NOTIFY 0x10 +#define BLE_GATT_CHR_PROP_INDICATE 0x20 +#define BLE_GATT_CHR_PROP_AUTH_SIGN_WRITE 0x40 +#define BLE_GATT_CHR_PROP_EXTENDED 0x80 + +#define BLE_GATT_ACCESS_OP_READ_CHR 0 +#define BLE_GATT_ACCESS_OP_WRITE_CHR 1 +#define BLE_GATT_ACCESS_OP_READ_DSC 2 +#define BLE_GATT_ACCESS_OP_WRITE_DSC 3 + +#define BLE_GATT_CHR_F_BROADCAST 0x0001 +#define BLE_GATT_CHR_F_READ 0x0002 +#define BLE_GATT_CHR_F_WRITE_NO_RSP 0x0004 +#define BLE_GATT_CHR_F_WRITE 0x0008 +#define BLE_GATT_CHR_F_NOTIFY 0x0010 +#define BLE_GATT_CHR_F_INDICATE 0x0020 +#define BLE_GATT_CHR_F_AUTH_SIGN_WRITE 0x0040 +#define BLE_GATT_CHR_F_RELIABLE_WRITE 0x0080 +#define BLE_GATT_CHR_F_AUX_WRITE 0x0100 +#define BLE_GATT_CHR_F_READ_ENC 0x0200 +#define BLE_GATT_CHR_F_READ_AUTHEN 0x0400 +#define BLE_GATT_CHR_F_READ_AUTHOR 0x0800 +#define BLE_GATT_CHR_F_WRITE_ENC 0x1000 +#define BLE_GATT_CHR_F_WRITE_AUTHEN 0x2000 +#define BLE_GATT_CHR_F_WRITE_AUTHOR 0x4000 + +#define BLE_GATT_SVC_TYPE_END 0 +#define BLE_GATT_SVC_TYPE_PRIMARY 1 +#define BLE_GATT_SVC_TYPE_SECONDARY 2 + +/*** @client. */ +struct ble_gatt_error { + uint16_t status; + uint16_t att_handle; +}; + +struct ble_gatt_svc { + uint16_t start_handle; + uint16_t end_handle; + ble_uuid_any_t uuid; +}; + +struct ble_gatt_attr { + uint16_t handle; + uint16_t offset; + struct os_mbuf *om; +}; + +struct ble_gatt_chr { + uint16_t def_handle; + uint16_t val_handle; + uint8_t properties; + ble_uuid_any_t uuid; +}; + +struct ble_gatt_dsc { + uint16_t handle; + ble_uuid_any_t uuid; +}; + +typedef int ble_gatt_mtu_fn(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t mtu, void *arg); +typedef int ble_gatt_disc_svc_fn(uint16_t conn_handle, + const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, + void *arg); + +/** + * The host will free the attribute mbuf automatically after the callback is + * executed. The application can take ownership of the mbuf and prevent it + * from being freed by assigning NULL to attr->om. + */ +typedef int ble_gatt_attr_fn(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg); + +/** + * The host will free the attribute mbufs automatically after the callback is + * executed. The application can take ownership of the mbufs and prevent them + * from being freed by assigning NULL to each attribute's om field. + */ +typedef int ble_gatt_reliable_attr_fn(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attrs, + uint8_t num_attrs, void *arg); + +typedef int ble_gatt_chr_fn(uint16_t conn_handle, + const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg); + +typedef int ble_gatt_dsc_fn(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t chr_val_handle, + const struct ble_gatt_dsc *dsc, + void *arg); + +/** + * Initiates GATT procedure: Exchange MTU. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_exchange_mtu(uint16_t conn_handle, + ble_gatt_mtu_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Discover All Primary Services. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + */ +int ble_gattc_disc_all_svcs(uint16_t conn_handle, + ble_gatt_disc_svc_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Discover Primary Service by Service UUID. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param service_uuid128 The 128-bit UUID of the service to discover. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, const ble_uuid_t *uuid, + ble_gatt_disc_svc_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Find Included Services. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param start_handle The handle to begin the search at (generally + * the service definition handle). + * @param end_handle The handle to end the search at (generally the + * last handle in the service). + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, + ble_gatt_disc_svc_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Discover All Characteristics of a Service. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param start_handle The handle to begin the search at (generally + * the service definition handle). + * @param end_handle The handle to end the search at (generally the + * last handle in the service). + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, ble_gatt_chr_fn *cb, + void *cb_arg); + +/** + * Initiates GATT procedure: Discover Characteristics by UUID. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param start_handle The handle to begin the search at (generally + * the service definition handle). + * @param end_handle The handle to end the search at (generally the + * last handle in the service). + * @param chr_uuid128 The 128-bit UUID of the characteristic to + * discover. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, const ble_uuid_t *uuid, + ble_gatt_chr_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Discover All Characteristic Descriptors. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param chr_val_handle The handle of the characteristic value + * attribute. + * @param chr_end_handle The last handle in the characteristic + * definition. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_disc_all_dscs(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, + ble_gatt_dsc_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Read Characteristic Value. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param attr_handle The handle of the characteristic value to read. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_read(uint16_t conn_handle, uint16_t attr_handle, + ble_gatt_attr_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Read Using Characteristic UUID. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param start_handle The first handle to search (generally the + * handle of the service definition). + * @param end_handle The last handle to search (generally the + * last handle in the service definition). + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_read_by_uuid(uint16_t conn_handle, uint16_t start_handle, + uint16_t end_handle, const ble_uuid_t *uuid, + ble_gatt_attr_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Read Long Characteristic Values. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param handle The handle of the characteristic value to read. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_read_long(uint16_t conn_handle, uint16_t handle, uint16_t offset, + ble_gatt_attr_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Read Multiple Characteristic Values. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param handles An array of 16-bit attribute handles to read. + * @param num_handles The number of entries in the "handles" array. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_read_mult(uint16_t conn_handle, const uint16_t *handles, + uint8_t num_handles, ble_gatt_attr_fn *cb, + void *cb_arg); + +/** + * Initiates GATT procedure: Write Without Response. This function consumes + * the supplied mbuf regardless of the outcome. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param attr_handle The handle of the characteristic value to write + * to. + * @param txom The value to write to the characteristic. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle, + struct os_mbuf *om); + +/** + * Initiates GATT procedure: Write Without Response. This function consumes + * the supplied mbuf regardless of the outcome. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param attr_handle The handle of the characteristic value to write + * to. + * @param value The value to write to the characteristic. + * @param value_len The number of bytes to write. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_write_no_rsp_flat(uint16_t conn_handle, uint16_t attr_handle, + const void *data, uint16_t data_len); + +/** + * Initiates GATT procedure: Write Characteristic Value. This function + * consumes the supplied mbuf regardless of the outcome. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param attr_handle The handle of the characteristic value to write + * to. + * @param txom The value to write to the characteristic. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle, + struct os_mbuf *om, + ble_gatt_attr_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Write Characteristic Value (flat buffer version). + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param attr_handle The handle of the characteristic value to write + * to. + * @param value The value to write to the characteristic. + * @param value_len The number of bytes to write. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_write_flat(uint16_t conn_handle, uint16_t attr_handle, + const void *data, uint16_t data_len, + ble_gatt_attr_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Write Long Characteristic Values. This function + * consumes the supplied mbuf regardless of the outcome. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param attr_handle The handle of the characteristic value to write + * to. + * @param txom The value to write to the characteristic. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_write_long(uint16_t conn_handle, uint16_t attr_handle, + uint16_t offset, struct os_mbuf *om, + ble_gatt_attr_fn *cb, void *cb_arg); + +/** + * Initiates GATT procedure: Reliable Writes. This function consumes the + * supplied mbufs regardless of the outcome. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param attrs An array of attribute descriptors; specifies + * which characteristics to write to and what + * data to write to them. The mbuf pointer in + * each attribute is set to NULL by this + * function. + * @param num_attrs The number of characteristics to write; equal + * to the number of elements in the 'attrs' + * array. + * @param cb The function to call to report procedure status + * updates; null for no callback. + * @param cb_arg The optional argument to pass to the callback + * function. + */ +int ble_gattc_write_reliable(uint16_t conn_handle, + struct ble_gatt_attr *attrs, + int num_attrs, ble_gatt_reliable_attr_fn *cb, + void *cb_arg); + +/** + * Sends a "free-form" characteristic notification. This function consumes the + * supplied mbuf regardless of the outcome. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param chr_val_handle The attribute handle to indicate in the + * outgoing notification. + * @param txom The value to write to the characteristic. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_notify_custom(uint16_t conn_handle, uint16_t att_handle, + struct os_mbuf *om); + +/** + * Sends a characteristic notification. The content of the message is read + * from the specified characteristic. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param chr_val_handle The value attribute handle of the + * characteristic to include in the outgoing + * notification. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle); + +/** + * Sends a "free-form" characteristic indication. The provided mbuf contains + * the indication payload. This function consumes the supplied mbuf regardless + * of the outcome. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param chr_val_handle The value attribute handle of the + * characteristic to include in the outgoing + * indication. + * @param txom The data to include in the indication. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, + struct os_mbuf *txom); + +/** + * Sends a characteristic indication. The content of the message is read from + * the specified characteristic. + * + * @param conn_handle The connection over which to execute the + * procedure. + * @param chr_val_handle The value attribute handle of the + * characteristic to include in the outgoing + * indication. + * + * @return 0 on success; nonzero on failure. + */ +int ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle); + +int ble_gattc_init(void); + +/*** @server. */ + +struct ble_gatt_access_ctxt; +typedef int ble_gatt_access_fn(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +typedef uint16_t ble_gatt_chr_flags; + +struct ble_gatt_chr_def { + /** + * Pointer to characteristic UUID; use BLE_UUIDxx_DECLARE macros to declare + * proper UUID; NULL if there are no more characteristics in the service. + */ + const ble_uuid_t *uuid; + + /** + * Callback that gets executed when this characteristic is read or + * written. + */ + ble_gatt_access_fn *access_cb; + + /** Optional argument for callback. */ + void *arg; + + /** + * Array of this characteristic's descriptors. NULL if no descriptors. + * Do not include CCCD; it gets added automatically if this + * characteristic's notify or indicate flag is set. + */ + struct ble_gatt_dsc_def *descriptors; + + /** Specifies the set of permitted operations for this characteristic. */ + ble_gatt_chr_flags flags; + + /** Specifies minimum required key size to access this characteristic. */ + uint8_t min_key_size; + + /** + * At registration time, this is filled in with the characteristic's value + * attribute handle. + */ + uint16_t *val_handle; +}; + +struct ble_gatt_svc_def { + /** + * One of the following: + * o BLE_GATT_SVC_TYPE_PRIMARY - primary service + * o BLE_GATT_SVC_TYPE_SECONDARY - secondary service + * o 0 - No more services in this array. + */ + uint8_t type; + + /** + * Pointer to service UUID; use BLE_UUIDxx_DECLARE macros to declare + * proper UUID; NULL if there are no more characteristics in the service. + */ + const ble_uuid_t *uuid; + + /** + * Array of pointers to other service definitions. These services are + * reported as "included services" during service discovery. Terminate the + * array with NULL. + */ + const struct ble_gatt_svc_def **includes; + + /** + * Array of characteristic definitions corresponding to characteristics + * belonging to this service. + */ + const struct ble_gatt_chr_def *characteristics; +}; + +struct ble_gatt_dsc_def { + /** + * Pointer to descriptor UUID; use BLE_UUIDxx_DECLARE macros to declare + * proper UUID; NULL if there are no more characteristics in the service. + */ + const ble_uuid_t *uuid; + + /** Specifies the set of permitted operations for this descriptor. */ + uint8_t att_flags; + + /** Specifies minimum required key size to access this descriptor. */ + uint8_t min_key_size; + + /** Callback that gets executed when the descriptor is read or written. */ + ble_gatt_access_fn *access_cb; + + /** Optional argument for callback. */ + void *arg; +}; + +// buffer from os_mbuf.h +/** + * Chained memory buffer. + */ +struct os_mbuf { + /** + * Current pointer to data in the structure + */ + uint8_t *om_data; + /** + * Flags associated with this buffer, see OS_MBUF_F_* defintions + */ + uint8_t om_flags; + /** + * Length of packet header + */ + uint8_t om_pkthdr_len; + /** + * Length of data in this buffer + */ + uint16_t om_len; + + // sim: disabled, not used by InfiniSim + ///** + // * The mbuf pool this mbuf was allocated out of + // */ + //struct os_mbuf_pool *om_omp; + + //SLIST_ENTRY(os_mbuf) om_next; + + /** + * Pointer to the beginning of the data, after this buffer + */ + uint8_t om_databuf[0]; +}; + + +/** + * Context for an access to a GATT characteristic or descriptor. When a client + * reads or writes a locally registered characteristic or descriptor, an + * instance of this struct gets passed to the application callback. + */ +struct ble_gatt_access_ctxt { + /** + * Indicates the gatt operation being performed. This is equal to one of + * the following values: + * o BLE_GATT_ACCESS_OP_READ_CHR + * o BLE_GATT_ACCESS_OP_WRITE_CHR + * o BLE_GATT_ACCESS_OP_READ_DSC + * o BLE_GATT_ACCESS_OP_WRITE_DSC + */ + uint8_t op; + + /** + * A container for the GATT access data. + * o For reads: The application populates this with the value of the + * characteristic or descriptor being read. + * o For writes: This is already populated with the value being written + * by the peer. If the application wishes to retain this mbuf for + * later use, the access callback must set this pointer to NULL to + * prevent the stack from freeing it. + */ + struct os_mbuf *om; + + /** + * The GATT operation being performed dictates which field in this union is + * valid. If a characteristic is being accessed, the chr field is valid. + * Otherwise a descriptor is being accessed, in which case the dsc field + * is valid. + */ + union { + /** + * The characteristic definition corresponding to the characteristic + * being accessed. This is what the app registered at startup. + */ + const struct ble_gatt_chr_def *chr; + + /** + * The descriptor definition corresponding to the descriptor being + * accessed. This is what the app registered at startup. + */ + const struct ble_gatt_dsc_def *dsc; + }; +}; + +/** + * Context passed to the registration callback; represents the GATT service, + * characteristic, or descriptor being registered. + */ +struct ble_gatt_register_ctxt { + /** + * Indicates the gatt registration operation just performed. This is + * equal to one of the following values: + * o BLE_GATT_REGISTER_OP_SVC + * o BLE_GATT_REGISTER_OP_CHR + * o BLE_GATT_REGISTER_OP_DSC + */ + uint8_t op; + + /** + * The value of the op field determines which field in this union is valid. + */ + union { + /** Service; valid if op == BLE_GATT_REGISTER_OP_SVC. */ + struct { + /** The ATT handle of the service definition attribute. */ + uint16_t handle; + + /** + * The service definition representing the service being + * registered. + */ + const struct ble_gatt_svc_def *svc_def; + } svc; + + /** Characteristic; valid if op == BLE_GATT_REGISTER_OP_CHR. */ + struct { + /** The ATT handle of the characteristic definition attribute. */ + uint16_t def_handle; + + /** The ATT handle of the characteristic value attribute. */ + uint16_t val_handle; + + /** + * The characteristic definition representing the characteristic + * being registered. + */ + const struct ble_gatt_chr_def *chr_def; + + /** + * The service definition corresponding to the characteristic's + * parent service. + */ + const struct ble_gatt_svc_def *svc_def; + } chr; + + /** Descriptor; valid if op == BLE_GATT_REGISTER_OP_DSC. */ + struct { + /** The ATT handle of the descriptor definition attribute. */ + uint16_t handle; + + /** + * The descriptor definition corresponding to the descriptor being + * registered. + */ + const struct ble_gatt_dsc_def *dsc_def; + + /** + * The characteristic definition corresponding to the descriptor's + * parent characteristic. + */ + const struct ble_gatt_chr_def *chr_def; + + /** + * The service definition corresponding to the descriptor's + * grandparent service + */ + const struct ble_gatt_svc_def *svc_def; + } dsc; + }; +}; + +typedef void ble_gatt_register_fn(struct ble_gatt_register_ctxt *ctxt, + void *arg); + +/** + * Queues a set of service definitions for registration. All services queued + * in this manner get registered when ble_gatts_start() is called. + * + * @param svcs An array of service definitions to queue for + * registration. This array must be + * terminated with an entry whose 'type' + * equals 0. + * + * @return 0 on success; + * BLE_HS_ENOMEM on heap exhaustion. + */ +int ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs); + +/** + * Set visibility of local GATT service. Invisible services are not removed + * from database but are not discoverable by peer devices. Service Changed + * should be handled by application when needed by calling + * ble_svc_gatt_changed(). + * + * @param handle Handle of service + * @param visible non-zero if service should be visible + * + * @return 0 on success; + * BLE_HS_ENOENT if service wasn't found. + */ +int ble_gatts_svc_set_visibility(uint16_t handle, int visible); + +/** + * Adjusts a host configuration object's settings to accommodate the specified + * service definition array. This function adds the counts to the appropriate + * fields in the supplied configuration object without clearing them first, so + * it can be called repeatedly with different inputs to calculate totals. Be + * sure to zero the GATT server settings prior to the first call to this + * function. + * + * @param defs The service array containing the resource + * definitions to be counted. + * + * @return 0 on success; + * BLE_HS_EINVAL if the svcs array contains an + * invalid resource definition. + */ +int ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs); + +/** + * Send notification (or indication) to any connected devices that have + * subscribed for notification (or indication) for specified characteristic. + * + * @param chr_val_handle Characteristic value handle + */ +void ble_gatts_chr_updated(uint16_t chr_val_handle); + +/** + * Retrieves the attribute handle associated with a local GATT service. + * + * @param uuid The UUID of the service to look up. + * @param out_handle On success, populated with the handle of the + * service attribute. Pass null if you don't + * need this value. + * + * @return 0 on success; + * BLE_HS_ENOENT if the specified service could + * not be found. + */ +int ble_gatts_find_svc(const ble_uuid_t *uuid, uint16_t *out_handle); + +/** + * Retrieves the pair of attribute handles associated with a local GATT + * characteristic. + * + * @param svc_uuid The UUID of the parent service. + * @param chr_uuid The UUID of the characteristic to look up. + * @param out_def_handle On success, populated with the handle + * of the characteristic definition attribute. + * Pass null if you don't need this value. + * @param out_val_handle On success, populated with the handle + * of the characteristic value attribute. + * Pass null if you don't need this value. + * + * @return 0 on success; + * BLE_HS_ENOENT if the specified service or + * characteristic could not be found. + */ +int ble_gatts_find_chr(const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, + uint16_t *out_def_handle, uint16_t *out_val_handle); + +/** + * Retrieves the attribute handle associated with a local GATT descriptor. + * + * @param svc_uuid The UUID of the grandparent service. + * @param chr_uuid The UUID of the parent characteristic. + * @param dsc_uuid The UUID of the descriptor ro look up. + * @param out_handle On success, populated with the handle + * of the descriptor attribute. Pass null if + * you don't need this value. + * + * @return 0 on success; + * BLE_HS_ENOENT if the specified service, + * characteristic, or descriptor could not be + * found. + */ +int ble_gatts_find_dsc(const ble_uuid_t *svc_uuid, const ble_uuid_t *chr_uuid, + const ble_uuid_t *dsc_uuid, uint16_t *out_dsc_handle); + +typedef void (*ble_gatt_svc_foreach_fn)(const struct ble_gatt_svc_def *svc, + uint16_t handle, + uint16_t end_group_handle, + void *arg); + +/** + * Prints dump of local GATT database. This is useful to log local state of + * database in human readable form. + */ +void ble_gatts_show_local(void); + +/** + * Resets the GATT server to its initial state. On success, this function + * removes all supported services, characteristics, and descriptors. This + * function requires that: + * o No peers are connected, and + * o No GAP operations are active (advertise, discover, or connect). + * + * @return 0 on success; + * BLE_HS_EBUSY if the GATT server could not be + * reset due to existing connections or active + * GAP procedures. + */ +int ble_gatts_reset(void); + +/** + * Makes all registered services available to peers. This function gets called + * automatically by the NimBLE host on startup; manual calls are only necessary + * for replacing the set of supported services with a new one. This function + * requires that: + * o No peers are connected, and + * o No GAP operations are active (advertise, discover, or connect). + * + * @return 0 on success; + * A BLE host core return code on unexpected + * error. + */ +int ble_gatts_start(void); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif diff --git a/sim/host/ble_uuid.h b/sim/host/ble_uuid.h new file mode 100644 index 0000000..80af9f3 --- /dev/null +++ b/sim/host/ble_uuid.h @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_BLE_UUID_ +#define H_BLE_UUID_ + +/** + * @brief Bluetooth UUID + * @defgroup bt_uuid Bluetooth UUID + * @ingroup bt_host + * @{ + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct os_mbuf; + +/** Type of UUID */ +enum { + /** 16-bit UUID (BT SIG assigned) */ + BLE_UUID_TYPE_16 = 16, + + /** 32-bit UUID (BT SIG assigned) */ + BLE_UUID_TYPE_32 = 32, + + /** 128-bit UUID */ + BLE_UUID_TYPE_128 = 128, +}; + +/** Generic UUID type, to be used only as a pointer */ +typedef struct { + /** Type of the UUID */ + uint8_t type; +} ble_uuid_t; + +/** 16-bit UUID */ +typedef struct { + ble_uuid_t u; + uint16_t value; +} ble_uuid16_t; + +/** 32-bit UUID */ +typedef struct { + ble_uuid_t u; + uint32_t value; +} ble_uuid32_t; + +/** 128-bit UUID */ +typedef struct { + ble_uuid_t u; + uint8_t value[16]; +} ble_uuid128_t; + +/** Universal UUID type, to be used for any-UUID static allocation */ +typedef union { + ble_uuid_t u; + ble_uuid16_t u16; + ble_uuid32_t u32; + ble_uuid128_t u128; +} ble_uuid_any_t; + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* _BLE_HOST_UUID_H */