Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CustomService.h Source File

CustomService.h

Go to the documentation of this file.
00001 /**
00002  *******************************************************************************
00003  * @file    CustomService.h
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    March 31st, 2018
00007  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IDB05A1
00008  *          Bluetooth Low energy Expansion Board.
00009  *******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright
00019  *      notice, this list of conditions and the following disclaimer in the
00020  *      documentation and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its
00022  *      contributors may be used to endorse or promote products derived from
00023  *      this software without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 
00029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  * POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  *******************************************************************************
00038  */
00039  
00040 
00041 #ifndef __BLE_CUSTOM_SERVICE_H__
00042 #define __BLE_CUSTOM_SERVICE_H__
00043 
00044 #include "ble_utils.h"
00045 
00046 #define TIMESTAMP_LENGTH   (sizeof(uint16_t))
00047 #define SWITCH_LENGTH      (sizeof(uint8_t))
00048 #define SWITCH_DATA_LENGTH (TIMESTAMP_LENGTH + SWITCH_LENGTH)
00049 #define SWITCH_DATA_INDEX  (TIMESTAMP_LENGTH)
00050 
00051 const UUID::LongUUIDBytes_t CUSTOM_SERVICE_UUID               = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
00052 const UUID::LongUUIDBytes_t CUSTOM_SWITCH_CHARACTERISTIC_UUID = {0x20,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
00053 
00054 class CustomService {
00055 public:
00056 
00057     /* Switch states. */
00058     typedef enum
00059     {
00060         SWITCH_OFF = 0,
00061         SWITCH_ON
00062     } switch_state_t;
00063 
00064     CustomService(BLEDevice &_ble) :
00065         ble(_ble),
00066         state_command(CUSTOM_SWITCH_CHARACTERISTIC_UUID, packed_state_command, SWITCH_DATA_LENGTH, SWITCH_DATA_LENGTH,
00067             /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | */GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00068     {
00069         GattCharacteristic *char_table[] = {&state_command};
00070         GattService switch_service(CUSTOM_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
00071         ble.addService(switch_service);
00072         memset (packed_state_command, 0, SWITCH_DATA_LENGTH);
00073     }
00074 
00075     void send_state(uint16_t time_stamp, uint8_t current_state) {
00076         memset (packed_state_command, 0, SWITCH_DATA_LENGTH);
00077         STORE_LE_16(packed_state_command, time_stamp);
00078         packed_state_command[SWITCH_DATA_INDEX] = current_state;
00079         ble.gattServer().write(state_command.getValueAttribute().getHandle(), (uint8_t *) &packed_state_command, SWITCH_DATA_LENGTH, 0);
00080     }
00081 
00082     GattAttribute::Handle_t getValueHandle() const
00083     {
00084         return state_command.getValueAttribute().getHandle();
00085     }
00086 
00087 private:
00088     BLEDevice &ble;
00089     GattCharacteristic state_command;
00090     uint8_t packed_state_command[SWITCH_DATA_LENGTH];
00091 };
00092 
00093 #endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */