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@4:a10e2c94c8c8, 2018-05-16 (annotated)
- Committer:
- Davidroid
- Date:
- Wed May 16 17:20:48 2018 +0000
- Revision:
- 4:a10e2c94c8c8
- Parent:
- 0:6b34c49b5285
- Child:
- 5:445024130101
Add sensors
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 | 0:6b34c49b5285 | 44 | #include "ble_utils.h" |
Davidroid | 0:6b34c49b5285 | 45 | |
Davidroid | 4:a10e2c94c8c8 | 46 | #define TIMESTAMP_LENGTH (sizeof(uint16_t)) |
Davidroid | 4:a10e2c94c8c8 | 47 | #define STATE_LENGTH (sizeof(uint8_t)) |
Davidroid | 4:a10e2c94c8c8 | 48 | #define COMMAND_LENGTH (sizeof(uint8_t)) |
Davidroid | 4:a10e2c94c8c8 | 49 | #define PRESSURE_LENGTH (sizeof(uint32_t)) |
Davidroid | 4:a10e2c94c8c8 | 50 | #define HUMIDITY_LENGTH (sizeof(uint16_t)) |
Davidroid | 4:a10e2c94c8c8 | 51 | #define TEMPERATURE_LENGTH (sizeof(uint16_t)) |
Davidroid | 0:6b34c49b5285 | 52 | |
Davidroid | 4:a10e2c94c8c8 | 53 | #define STATE_DATA_LENGTH (TIMESTAMP_LENGTH + STATE_LENGTH) |
Davidroid | 4:a10e2c94c8c8 | 54 | #define COMMAND_DATA_LENGTH (COMMAND_LENGTH) |
Davidroid | 4:a10e2c94c8c8 | 55 | #define SENSORS_DATA_LENGTH (TIMESTAMP_LENGTH + PRESSURE_LENGTH + HUMIDITY_LENGTH + TEMPERATURE_LENGTH) |
Davidroid | 4:a10e2c94c8c8 | 56 | |
Davidroid | 4:a10e2c94c8c8 | 57 | const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 4:a10e2c94c8c8 | 58 | 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 | 4:a10e2c94c8c8 | 59 | const UUID::LongUUIDBytes_t CUSTOM_SENSORS_CHARACTERISTIC_UUID = {0x00,0x1c,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
Davidroid | 0:6b34c49b5285 | 60 | |
Davidroid | 0:6b34c49b5285 | 61 | class CustomService { |
Davidroid | 0:6b34c49b5285 | 62 | public: |
Davidroid | 0:6b34c49b5285 | 63 | |
Davidroid | 0:6b34c49b5285 | 64 | /* Switch states. */ |
Davidroid | 0:6b34c49b5285 | 65 | typedef enum |
Davidroid | 0:6b34c49b5285 | 66 | { |
Davidroid | 0:6b34c49b5285 | 67 | SWITCH_OFF = 0, |
Davidroid | 0:6b34c49b5285 | 68 | SWITCH_ON |
Davidroid | 0:6b34c49b5285 | 69 | } switch_state_t; |
Davidroid | 0:6b34c49b5285 | 70 | |
Davidroid | 0:6b34c49b5285 | 71 | CustomService(BLEDevice &_ble) : |
Davidroid | 0:6b34c49b5285 | 72 | ble(_ble), |
Davidroid | 4:a10e2c94c8c8 | 73 | state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, STATE_DATA_LENGTH, STATE_DATA_LENGTH, |
Davidroid | 4:a10e2c94c8c8 | 74 | /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
Davidroid | 4:a10e2c94c8c8 | 75 | sensors(CUSTOM_SENSORS_CHARACTERISTIC_UUID, packed_sensors, SENSORS_DATA_LENGTH, SENSORS_DATA_LENGTH, |
Davidroid | 4:a10e2c94c8c8 | 76 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
Davidroid | 0:6b34c49b5285 | 77 | { |
Davidroid | 4:a10e2c94c8c8 | 78 | GattCharacteristic *char_table[] = {&state_command, &sensors}; |
Davidroid | 4:a10e2c94c8c8 | 79 | GattService switch_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *)); |
Davidroid | 0:6b34c49b5285 | 80 | ble.addService(switch_service); |
Davidroid | 4:a10e2c94c8c8 | 81 | memset (packed_state_command, 0, STATE_DATA_LENGTH); |
Davidroid | 4:a10e2c94c8c8 | 82 | memset (packed_sensors, 0, SENSORS_DATA_LENGTH); |
Davidroid | 0:6b34c49b5285 | 83 | } |
Davidroid | 0:6b34c49b5285 | 84 | |
Davidroid | 0:6b34c49b5285 | 85 | void send_state(uint16_t time_stamp, uint8_t current_state) { |
Davidroid | 4:a10e2c94c8c8 | 86 | memset (packed_state_command, 0, STATE_DATA_LENGTH); |
Davidroid | 0:6b34c49b5285 | 87 | STORE_LE_16(packed_state_command, time_stamp); |
Davidroid | 4:a10e2c94c8c8 | 88 | packed_state_command[TIMESTAMP_LENGTH] = current_state; |
Davidroid | 4:a10e2c94c8c8 | 89 | ble.gattServer().write(get_state_command_handle(), (uint8_t *) &packed_state_command, STATE_DATA_LENGTH, 0); |
Davidroid | 0:6b34c49b5285 | 90 | } |
Davidroid | 0:6b34c49b5285 | 91 | |
Davidroid | 4:a10e2c94c8c8 | 92 | void send_sensors(uint16_t time_stamp, uint32_t pressure, uint16_t humidity, uint16_t temperature) { |
Davidroid | 4:a10e2c94c8c8 | 93 | memset (packed_sensors, 0, SENSORS_DATA_LENGTH); |
Davidroid | 4:a10e2c94c8c8 | 94 | int p = 0; |
Davidroid | 4:a10e2c94c8c8 | 95 | STORE_LE_16(&packed_sensors[p], time_stamp); |
Davidroid | 4:a10e2c94c8c8 | 96 | STORE_LE_32(&packed_sensors[p += TIMESTAMP_LENGTH], pressure); |
Davidroid | 4:a10e2c94c8c8 | 97 | STORE_LE_16(&packed_sensors[p += PRESSURE_LENGTH], humidity); |
Davidroid | 4:a10e2c94c8c8 | 98 | STORE_LE_16(&packed_sensors[p += HUMIDITY_LENGTH], temperature); |
Davidroid | 4:a10e2c94c8c8 | 99 | ble.gattServer().write(get_sensors_handle(), (uint8_t *) &packed_sensors, SENSORS_DATA_LENGTH, 0); |
Davidroid | 4:a10e2c94c8c8 | 100 | } |
Davidroid | 4:a10e2c94c8c8 | 101 | |
Davidroid | 4:a10e2c94c8c8 | 102 | GattAttribute::Handle_t get_state_command_handle() const |
Davidroid | 0:6b34c49b5285 | 103 | { |
Davidroid | 0:6b34c49b5285 | 104 | return state_command.getValueAttribute().getHandle(); |
Davidroid | 0:6b34c49b5285 | 105 | } |
Davidroid | 0:6b34c49b5285 | 106 | |
Davidroid | 4:a10e2c94c8c8 | 107 | GattAttribute::Handle_t get_sensors_handle() const |
Davidroid | 4:a10e2c94c8c8 | 108 | { |
Davidroid | 4:a10e2c94c8c8 | 109 | return sensors.getValueAttribute().getHandle(); |
Davidroid | 4:a10e2c94c8c8 | 110 | } |
Davidroid | 4:a10e2c94c8c8 | 111 | |
Davidroid | 0:6b34c49b5285 | 112 | private: |
Davidroid | 0:6b34c49b5285 | 113 | BLEDevice &ble; |
Davidroid | 0:6b34c49b5285 | 114 | GattCharacteristic state_command; |
Davidroid | 4:a10e2c94c8c8 | 115 | GattCharacteristic sensors; |
Davidroid | 4:a10e2c94c8c8 | 116 | uint8_t packed_state_command[STATE_DATA_LENGTH]; |
Davidroid | 4:a10e2c94c8c8 | 117 | uint8_t packed_sensors[SENSORS_DATA_LENGTH]; |
Davidroid | 0:6b34c49b5285 | 118 | }; |
Davidroid | 0:6b34c49b5285 | 119 | |
Davidroid | 0:6b34c49b5285 | 120 | #endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */ |