Motion and Environmental sensor reader application connected via BLE to ST BlueMS iOS/Android application.

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL

Fork of MOTENV_Mbed by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CustomConfigService.h Source File

CustomConfigService.h

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CustomConfigService.h
00004   * @author  Central Labs / AST
00005   * @version V0.9.0
00006   * @date    23-Dec-2015
00007   * @brief   BLE calibration service
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012   *
00013   * Redistribution and use in source and binary forms, with or without modification,
00014   * are permitted provided that the following conditions are met:
00015   *   1. Redistributions of source code must retain the above copyright notice,
00016   *      this list of conditions and the following disclaimer.
00017   *   2. Redistributions in binary form must reproduce the above copyright notice,
00018   *      this list of conditions and the following disclaimer in the documentation
00019   *      and/or other materials provided with the distribution.
00020   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021   *      may be used to endorse or promote products derived from this software
00022   *      without specific prior written permission.
00023   *
00024   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034   *
00035   ******************************************************************************
00036   */
00037 
00038 #ifndef __CUSTOM_BLE_CONFIG_SERVICE_H__
00039 #define __CUSTOM_BLE_CONFIG_SERVICE_H__
00040 #include "BLE.h"
00041 #include "UUID.h"
00042                               
00043 #define STORE_BE_32(buf, val)    ( ((buf)[3] =  (uint8_t) (val)     ) , \
00044                                    ((buf)[2] =  (uint8_t) (val>>8)  ) , \
00045                                    ((buf)[1] =  (uint8_t) (val>>16) ) , \
00046                                    ((buf)[0] =  (uint8_t) (val>>24) ) )                         
00047                             
00048 #define SIZEOF_CONFIG_DATA_LEN  2+4+1+1
00049 
00050 const UUID::LongUUIDBytes_t CONFIG_SERVICE_UUID_128 = {0x00,0x00,0x00,0x00,0x00,0x0F,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
00051 const UUID::LongUUIDBytes_t CONFIG_W2ST_CHAR_UUID   = {0x00,0x00,0x00,0x02,0x00,0x0F,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
00052 
00053 /* Custom Config Service */
00054 class CustomConfigService  {
00055 public:
00056     CustomConfigService(BLE &_ble) :
00057         ble(_ble),
00058                 configw2stCharacteristic(CONFIG_W2ST_CHAR_UUID, configData, SIZEOF_CONFIG_DATA_LEN, SIZEOF_CONFIG_DATA_LEN,
00059                                                                     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
00060                                                                     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE
00061 ) {  
00062 
00063         static bool serviceAdded = false; /* We should only ever need to add the service once. */
00064         if (serviceAdded) {
00065             return;
00066         }
00067 
00068         GattCharacteristic *charTable[] = {&configw2stCharacteristic};
00069 
00070         GattService   configService(CONFIG_SERVICE_UUID_128, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));              
00071                 
00072         ble.gattServer().addService(configService);                              
00073                 
00074         isEnabledConfNotify = false;
00075         memset (configData, 0, SIZEOF_CONFIG_DATA_LEN); 
00076         serviceAdded = true;
00077     }
00078 
00079     
00080     uint32_t sendConfigState(uint32_t Feature, uint8_t Command, uint8_t val, uint16_t TimeStamp) {      
00081             STORE_LE_16(configData ,TimeStamp); 
00082             STORE_BE_32(configData+2,Feature);          
00083             configData[6] = Command;
00084             configData[7] = (val==0x01) ? 100: val;            
00085             return ble.gattServer().write(configw2stCharacteristic.getValueAttribute().getHandle(), configData, SIZEOF_CONFIG_DATA_LEN, 0);
00086     }
00087     
00088   uint32_t updateConfigState(uint32_t Feature, uint8_t Command, uint8_t val, uint16_t TimeStamp) {
00089         if (ble.getGapState().connected && isEnabledConfNotify ) {  
00090             return sendConfigState(Feature, Command, val, TimeStamp);
00091         }
00092         return 0;
00093   }
00094 
00095     void updateConnectionStatus(ConnectionStatus_t status) {            
00096             isEnabledConfNotify = false;
00097             memset (configData, 0, SIZEOF_CONFIG_DATA_LEN);             
00098         }   
00099     
00100     bool isConfHandle (Gap::Handle_t handle) {
00101             if (handle == configw2stCharacteristic.getValueAttribute().getHandle() - BLE_HANDLE_VALUE_OFFSET) return true;
00102             return false;
00103     }           
00104 
00105     void enNotify (Gap::Handle_t handle) {
00106             if (isConfHandle(handle - BLE_HANDLE_EN_DIS_OFFSET)) { 
00107                 printf("enNotify! CustomConfigService %d\n\r", handle); isEnabledConfNotify = true; return; }    
00108     }
00109     
00110     void disNotify (Gap::Handle_t handle) {
00111             if (isConfHandle(handle - BLE_HANDLE_EN_DIS_OFFSET)) { 
00112                 printf("disNotify! CustomConfigService %d\n\r", handle); isEnabledConfNotify = false; return; }    
00113     }   
00114         
00115 private:
00116         BLE                              &ble;
00117         uint8_t                          configData[SIZEOF_CONFIG_DATA_LEN];                
00118         uint8_t                          configState;
00119         GattCharacteristic               configw2stCharacteristic;  
00120         bool                             isEnabledConfNotify;
00121 
00122 };
00123 
00124 #endif /* #ifndef __CUSTOM_BLE_CONFIG_SERVICE_H__*/