Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_IKS01A2
Fork of Node_BLE_Switch_Device by
Bluetooth Low Energy enabled device with environmental and inertial features, i.e. "Pressure", "Humidity", "Temperature", "Accelerometer", "Gyroscope", and "Magnetometer", plus "Switch" feature, compatible with BlueST Protocol.
source/CustomService.h@7:2b5ed57b088c, 2018-08-06 (annotated)
- Committer:
- Davidroid
- Date:
- Mon Aug 06 09:57:49 2018 +0000
- Revision:
- 7:2b5ed57b088c
- Parent:
- 6:01d49589410e
Correct Custom Service UUID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:6b34c49b5285 | 1 | /** |
Davidroid | 0:6b34c49b5285 | 2 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 3 | * @file CustomService.h |
Davidroid | 0:6b34c49b5285 | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:6b34c49b5285 | 5 | * @version V1.0.0 |
Davidroid | 0:6b34c49b5285 | 6 | * @date March 31st, 2018 |
Davidroid | 0:6b34c49b5285 | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IDB05A1 |
Davidroid | 0:6b34c49b5285 | 8 | * Bluetooth Low energy Expansion Board. |
Davidroid | 0:6b34c49b5285 | 9 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 10 | * @attention |
Davidroid | 0:6b34c49b5285 | 11 | * |
Davidroid | 0:6b34c49b5285 | 12 | * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> |
Davidroid | 0:6b34c49b5285 | 13 | * |
Davidroid | 0:6b34c49b5285 | 14 | * Redistribution and use in source and binary forms, with or without |
Davidroid | 0:6b34c49b5285 | 15 | * modification, are permitted provided that the following conditions are met: |
Davidroid | 0:6b34c49b5285 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:6b34c49b5285 | 17 | * this list of conditions and the following disclaimer. |
Davidroid | 0:6b34c49b5285 | 18 | * 2. Redistributions in binary form must reproduce the above copyright |
Davidroid | 0:6b34c49b5285 | 19 | * notice, this list of conditions and the following disclaimer in the |
Davidroid | 0:6b34c49b5285 | 20 | * documentation and/or other materials provided with the distribution. |
Davidroid | 0:6b34c49b5285 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its |
Davidroid | 0:6b34c49b5285 | 22 | * contributors may be used to endorse or promote products derived from |
Davidroid | 0:6b34c49b5285 | 23 | * this software without specific prior written permission. |
Davidroid | 0:6b34c49b5285 | 24 | * |
Davidroid | 0:6b34c49b5285 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:6b34c49b5285 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:6b34c49b5285 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
Davidroid | 0:6b34c49b5285 | 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
Davidroid | 0:6b34c49b5285 | 29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
Davidroid | 0:6b34c49b5285 | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
Davidroid | 0:6b34c49b5285 | 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
Davidroid | 0:6b34c49b5285 | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
Davidroid | 0:6b34c49b5285 | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
Davidroid | 0:6b34c49b5285 | 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Davidroid | 0:6b34c49b5285 | 35 | * POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:6b34c49b5285 | 36 | * |
Davidroid | 0:6b34c49b5285 | 37 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 38 | */ |
Davidroid | 0:6b34c49b5285 | 39 | |
Davidroid | 0:6b34c49b5285 | 40 | |
Davidroid | 0:6b34c49b5285 | 41 | #ifndef __BLE_CUSTOM_SERVICE_H__ |
Davidroid | 0:6b34c49b5285 | 42 | #define __BLE_CUSTOM_SERVICE_H__ |
Davidroid | 0:6b34c49b5285 | 43 | |
Davidroid | 6:01d49589410e | 44 | |
Davidroid | 6:01d49589410e | 45 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 6:01d49589410e | 46 | |
Davidroid | 0:6b34c49b5285 | 47 | #include "ble_utils.h" |
Davidroid | 0:6b34c49b5285 | 48 | |
Davidroid | 6:01d49589410e | 49 | |
Davidroid | 6:01d49589410e | 50 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 6:01d49589410e | 51 | |
Davidroid | 6:01d49589410e | 52 | #define TIMESTAMP_LENGTH (sizeof(uint16_t)) |
Davidroid | 7:2b5ed57b088c | 53 | #define SWITCH_LENGTH (sizeof(uint8_t)) |
Davidroid | 6:01d49589410e | 54 | #define PRESSURE_LENGTH (sizeof(uint32_t)) |
Davidroid | 6:01d49589410e | 55 | #define HUMIDITY_LENGTH (sizeof(uint16_t)) |
Davidroid | 6:01d49589410e | 56 | #define TEMPERATURE_LENGTH (sizeof(uint16_t)) |
Davidroid | 6:01d49589410e | 57 | #define ACCELEROMETER_LENGTH (sizeof(uint16_t)) |
Davidroid | 6:01d49589410e | 58 | #define GYROSCOPE_LENGTH (sizeof(uint16_t)) |
Davidroid | 6:01d49589410e | 59 | #define MAGNETOMETER_LENGTH (sizeof(uint16_t)) |
Davidroid | 6:01d49589410e | 60 | |
Davidroid | 7:2b5ed57b088c | 61 | #define SWITCH_DATA_INDEX (TIMESTAMP_LENGTH) |
Davidroid | 7:2b5ed57b088c | 62 | #define SWITCH_DATA_LENGTH (TIMESTAMP_LENGTH + SWITCH_LENGTH) |
Davidroid | 6:01d49589410e | 63 | #define ENV_SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + PRESSURE_LENGTH + HUMIDITY_LENGTH + TEMPERATURE_LENGTH) |
Davidroid | 6:01d49589410e | 64 | #define INE_SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + (3 * ACCELEROMETER_LENGTH) + (3 * GYROSCOPE_LENGTH) + (3 * MAGNETOMETER_LENGTH)) |
Davidroid | 6:01d49589410e | 65 | |
Davidroid | 0:6b34c49b5285 | 66 | |
Davidroid | 6:01d49589410e | 67 | /* Types ---------------------------------------------------------------------*/ |
Davidroid | 6:01d49589410e | 68 | |
Davidroid | 6:01d49589410e | 69 | /* Axes. */ |
Davidroid | 6:01d49589410e | 70 | typedef struct |
Davidroid | 6:01d49589410e | 71 | { |
Davidroid | 6:01d49589410e | 72 | int32_t x; |
Davidroid | 6:01d49589410e | 73 | int32_t y; |
Davidroid | 6:01d49589410e | 74 | int32_t z; |
Davidroid | 6:01d49589410e | 75 | } axes_t; |
Davidroid | 4:a10e2c94c8c8 | 76 | |
Davidroid | 6:01d49589410e | 77 | |
Davidroid | 6:01d49589410e | 78 | /* Constants -----------------------------------------------------------------*/ |
Davidroid | 6:01d49589410e | 79 | |
Davidroid | 7:2b5ed57b088c | 80 | const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 6:01d49589410e | 81 | const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 6:01d49589410e | 82 | const UUID::LongUUIDBytes_t CUSTOM_ENV_SENSORS_CHARACTERISTIC_UUID = {0x00,0x1c,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 6:01d49589410e | 83 | const UUID::LongUUIDBytes_t CUSTOM_INE_SENSORS_CHARACTERISTIC_UUID = {0x00,0xe0,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 6:01d49589410e | 84 | |
Davidroid | 6:01d49589410e | 85 | |
Davidroid | 6:01d49589410e | 86 | /* Classes -------------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 87 | |
Davidroid | 0:6b34c49b5285 | 88 | class CustomService { |
Davidroid | 0:6b34c49b5285 | 89 | public: |
Davidroid | 0:6b34c49b5285 | 90 | |
Davidroid | 0:6b34c49b5285 | 91 | /* Switch states. */ |
Davidroid | 0:6b34c49b5285 | 92 | typedef enum |
Davidroid | 0:6b34c49b5285 | 93 | { |
Davidroid | 0:6b34c49b5285 | 94 | SWITCH_OFF = 0, |
Davidroid | 0:6b34c49b5285 | 95 | SWITCH_ON |
Davidroid | 0:6b34c49b5285 | 96 | } switch_state_t; |
Davidroid | 0:6b34c49b5285 | 97 | |
Davidroid | 0:6b34c49b5285 | 98 | CustomService(BLEDevice &_ble) : |
Davidroid | 0:6b34c49b5285 | 99 | ble(_ble), |
Davidroid | 7:2b5ed57b088c | 100 | state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, SWITCH_DATA_LENGTH, SWITCH_DATA_LENGTH, |
Davidroid | 4:a10e2c94c8c8 | 101 | /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
Davidroid | 6:01d49589410e | 102 | env_sensors(CUSTOM_ENV_SENSORS_CHARACTERISTIC_UUID, packed_env_sensors, ENV_SENSORS_DATA_LENGTH, ENV_SENSORS_DATA_LENGTH, |
Davidroid | 6:01d49589410e | 103 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
Davidroid | 6:01d49589410e | 104 | ine_sensors(CUSTOM_INE_SENSORS_CHARACTERISTIC_UUID, packed_ine_sensors, INE_SENSORS_DATA_LENGTH, INE_SENSORS_DATA_LENGTH, |
Davidroid | 4:a10e2c94c8c8 | 105 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
Davidroid | 0:6b34c49b5285 | 106 | { |
Davidroid | 6:01d49589410e | 107 | GattCharacteristic *char_table[] = {&state_command, &env_sensors, &ine_sensors}; |
Davidroid | 6:01d49589410e | 108 | GattService iot_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *)); |
Davidroid | 6:01d49589410e | 109 | ble.addService(iot_service); |
Davidroid | 7:2b5ed57b088c | 110 | memset (packed_state_command, 0, SWITCH_DATA_LENGTH); |
Davidroid | 6:01d49589410e | 111 | memset (packed_env_sensors, 0, ENV_SENSORS_DATA_LENGTH); |
Davidroid | 6:01d49589410e | 112 | memset (packed_ine_sensors, 0, INE_SENSORS_DATA_LENGTH); |
Davidroid | 0:6b34c49b5285 | 113 | } |
Davidroid | 0:6b34c49b5285 | 114 | |
Davidroid | 0:6b34c49b5285 | 115 | void send_state(uint16_t time_stamp, uint8_t current_state) { |
Davidroid | 7:2b5ed57b088c | 116 | memset (packed_state_command, 0, SWITCH_DATA_LENGTH); |
Davidroid | 0:6b34c49b5285 | 117 | STORE_LE_16(packed_state_command, time_stamp); |
Davidroid | 7:2b5ed57b088c | 118 | packed_state_command[SWITCH_DATA_INDEX] = current_state; |
Davidroid | 7:2b5ed57b088c | 119 | ble.gattServer().write(get_state_command_handle(), (uint8_t *) &packed_state_command, SWITCH_DATA_LENGTH, 0); |
Davidroid | 0:6b34c49b5285 | 120 | } |
Davidroid | 0:6b34c49b5285 | 121 | |
Davidroid | 6:01d49589410e | 122 | void send_env_sensors( |
Davidroid | 6:01d49589410e | 123 | uint16_t time_stamp, |
Davidroid | 6:01d49589410e | 124 | float pressure, float humidity, float temperature |
Davidroid | 6:01d49589410e | 125 | ) { |
Davidroid | 6:01d49589410e | 126 | memset (packed_env_sensors, 0, ENV_SENSORS_DATA_LENGTH); |
Davidroid | 4:a10e2c94c8c8 | 127 | int p = 0; |
Davidroid | 6:01d49589410e | 128 | STORE_LE_16(&packed_env_sensors[p], time_stamp); |
Davidroid | 5:445024130101 | 129 | p += TIMESTAMP_LENGTH; |
Davidroid | 6:01d49589410e | 130 | STORE_LE_32(&packed_env_sensors[p], (uint32_t) (pressure * 10)); |
Davidroid | 5:445024130101 | 131 | p += PRESSURE_LENGTH; |
Davidroid | 6:01d49589410e | 132 | STORE_LE_16(&packed_env_sensors[p], (uint16_t) (humidity * 10)); |
Davidroid | 5:445024130101 | 133 | p += HUMIDITY_LENGTH; |
Davidroid | 6:01d49589410e | 134 | STORE_LE_16(&packed_env_sensors[p], (uint16_t) (temperature * 10)); |
Davidroid | 6:01d49589410e | 135 | ble.gattServer().write(get_env_sensors_handle(), (uint8_t *) &packed_env_sensors, ENV_SENSORS_DATA_LENGTH, 0); |
Davidroid | 6:01d49589410e | 136 | } |
Davidroid | 6:01d49589410e | 137 | |
Davidroid | 6:01d49589410e | 138 | void send_ine_sensors( |
Davidroid | 6:01d49589410e | 139 | uint16_t time_stamp, |
Davidroid | 6:01d49589410e | 140 | axes_t *accelerometer, axes_t *gyroscope, axes_t *magnetometer |
Davidroid | 6:01d49589410e | 141 | ) { |
Davidroid | 6:01d49589410e | 142 | memset (packed_ine_sensors, 0, INE_SENSORS_DATA_LENGTH); |
Davidroid | 6:01d49589410e | 143 | int p = 0; |
Davidroid | 6:01d49589410e | 144 | STORE_LE_16(&packed_ine_sensors[p], time_stamp); |
Davidroid | 6:01d49589410e | 145 | p += TIMESTAMP_LENGTH; |
Davidroid | 6:01d49589410e | 146 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) accelerometer->x); |
Davidroid | 6:01d49589410e | 147 | p += ACCELEROMETER_LENGTH; |
Davidroid | 6:01d49589410e | 148 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) accelerometer->y); |
Davidroid | 6:01d49589410e | 149 | p += ACCELEROMETER_LENGTH; |
Davidroid | 6:01d49589410e | 150 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) accelerometer->z); |
Davidroid | 6:01d49589410e | 151 | p += ACCELEROMETER_LENGTH; |
Davidroid | 6:01d49589410e | 152 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) gyroscope->x); |
Davidroid | 6:01d49589410e | 153 | p += GYROSCOPE_LENGTH; |
Davidroid | 6:01d49589410e | 154 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) gyroscope->y); |
Davidroid | 6:01d49589410e | 155 | p += GYROSCOPE_LENGTH; |
Davidroid | 6:01d49589410e | 156 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) gyroscope->z); |
Davidroid | 6:01d49589410e | 157 | p += GYROSCOPE_LENGTH; |
Davidroid | 6:01d49589410e | 158 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) magnetometer->x); |
Davidroid | 6:01d49589410e | 159 | p += MAGNETOMETER_LENGTH; |
Davidroid | 6:01d49589410e | 160 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) magnetometer->y); |
Davidroid | 6:01d49589410e | 161 | p += MAGNETOMETER_LENGTH; |
Davidroid | 6:01d49589410e | 162 | STORE_LE_16(&packed_ine_sensors[p], (int16_t) magnetometer->z); |
Davidroid | 6:01d49589410e | 163 | ble.gattServer().write(get_ine_sensors_handle(), (uint8_t *) &packed_ine_sensors, INE_SENSORS_DATA_LENGTH, 0); |
Davidroid | 4:a10e2c94c8c8 | 164 | } |
Davidroid | 4:a10e2c94c8c8 | 165 | |
Davidroid | 4:a10e2c94c8c8 | 166 | GattAttribute::Handle_t get_state_command_handle() const |
Davidroid | 0:6b34c49b5285 | 167 | { |
Davidroid | 0:6b34c49b5285 | 168 | return state_command.getValueAttribute().getHandle(); |
Davidroid | 0:6b34c49b5285 | 169 | } |
Davidroid | 0:6b34c49b5285 | 170 | |
Davidroid | 6:01d49589410e | 171 | GattAttribute::Handle_t get_env_sensors_handle() const |
Davidroid | 4:a10e2c94c8c8 | 172 | { |
Davidroid | 6:01d49589410e | 173 | return env_sensors.getValueAttribute().getHandle(); |
Davidroid | 6:01d49589410e | 174 | } |
Davidroid | 6:01d49589410e | 175 | |
Davidroid | 6:01d49589410e | 176 | GattAttribute::Handle_t get_ine_sensors_handle() const |
Davidroid | 6:01d49589410e | 177 | { |
Davidroid | 6:01d49589410e | 178 | return ine_sensors.getValueAttribute().getHandle(); |
Davidroid | 4:a10e2c94c8c8 | 179 | } |
Davidroid | 4:a10e2c94c8c8 | 180 | |
Davidroid | 0:6b34c49b5285 | 181 | private: |
Davidroid | 0:6b34c49b5285 | 182 | BLEDevice &ble; |
Davidroid | 0:6b34c49b5285 | 183 | GattCharacteristic state_command; |
Davidroid | 6:01d49589410e | 184 | GattCharacteristic env_sensors; |
Davidroid | 6:01d49589410e | 185 | GattCharacteristic ine_sensors; |
Davidroid | 7:2b5ed57b088c | 186 | uint8_t packed_state_command[SWITCH_DATA_LENGTH]; |
Davidroid | 6:01d49589410e | 187 | uint8_t packed_env_sensors[ENV_SENSORS_DATA_LENGTH]; |
Davidroid | 6:01d49589410e | 188 | uint8_t packed_ine_sensors[INE_SENSORS_DATA_LENGTH]; |
Davidroid | 0:6b34c49b5285 | 189 | }; |
Davidroid | 0:6b34c49b5285 | 190 | |
Davidroid | 0:6b34c49b5285 | 191 | #endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */ |