SunTracker_BLE

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 X_NUCLEO_IHM01A1 X_NUCLEO_IKS01A1 mbed

Fork of SunTracker_BLE by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CustomControlService.h Source File

CustomControlService.h

00001 /******************************************************************************
00002   * @file    CustomControlService.h
00003   * @author  Fabio Brembilla
00004   * @version V1.0.0
00005   * @date    April, 2016
00006   * @brief   SunTracker Custom Service for BlueTooth (IDB0XA1 expansion board)
00007   *****************************************************************************
00008   * @attention
00009   *
00010   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00011   *
00012   * Redistribution and use in source and binary forms, with or without modification,
00013   * are permitted provided that the following conditions are met:
00014   *   1. Redistributions of source code must retain the above copyright notice,
00015   *      this list of conditions and the following disclaimer.
00016   *   2. Redistributions in binary form must reproduce the above copyright notice,
00017   *      this list of conditions and the following disclaimer in the documentation
00018   *      and/or other materials provided with the distribution.
00019   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00020   *      may be used to endorse or promote products derived from this software
00021   *      without specific prior written permission.
00022   *
00023   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00027   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00031   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033   *
00034   ******************************************************************************
00035 */
00036 
00037 #include "BLE.h"
00038 #include "UUID.h"
00039 
00040 #define STORE_BE_32(buf, val)    ( ((buf)[3] =  (uint8_t) (val)     ) , \
00041                                    ((buf)[2] =  (uint8_t) (val>>8)  ) , \
00042                                    ((buf)[1] =  (uint8_t) (val>>16) ) , \
00043                                    ((buf)[0] =  (uint8_t) (val>>24) ) )     
00044                 
00045 /* BLE Services: Primary + 1 Secondary (Char Desk) */
00046 //const LongUUIDBytes_t CONTROL_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A}; //Primary
00047 //const LongUUIDBytes_t USER_BUTTON_CHAR_UUID    = {0x00,0x00,0x00,0x01,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A}; //User Button Control
00048 //const ShortUUIDBytes_t CONTROL_SERVICE_UUID  = 0xA000;
00049 //const ShortUUIDBytes_t CONTROL_COMMAND_CHAR_UUID = 0xA001;                      
00050 
00051 /* BLE Services: Primary + 1 Secondary (Char Desk) */
00052 const UUID::LongUUIDBytes_t CONTROL_SERVICE_UUID        = {0x00,0x00,0x00,0x00,0x00,0x0F,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
00053 //const LongUUIDBytes_t CONTROL_REGISTER_CHAR_UUID  = {0x00,0x00,0x00,0x01,0x00,0x0F,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Not Used
00054 const UUID::LongUUIDBytes_t CONTROL_COMMAND_CHAR_UUID   = {0x00,0x00,0x00,0x02,0x00,0x0F,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
00055 
00056 //#define SIZEOF_CONTROL_DATA_LEN     2
00057 // 1 byte   0xFF        8 bit      int8
00058 // 2 byte   0xFFFF      16 bit     int16
00059 // 4 byte   0xFFFFFFFF  32 bit     int32
00060 #define COMMAND_DATA_LEN         2+4+1+2 // TimeStamp (only for Response) + Feature + Type + Data
00061 
00062 /* Custom Control Service */
00063 class CustomControlService  {
00064 public:
00065     CustomControlService(BLE &_ble) :
00066         ble(_ble),
00067                 commandCharacteristic(CONTROL_COMMAND_CHAR_UUID, commandData, COMMAND_DATA_LEN, COMMAND_DATA_LEN,                                        
00068                                          GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00069         {  
00070         static bool serviceAdded = false; // To be sure to define just one time
00071         if (serviceAdded) {
00072             return;
00073         }
00074 
00075         GattCharacteristic *charTable[] = {&commandCharacteristic};
00076         GattService   controlService(CONTROL_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));              
00077         ble.gattServer().addService(controlService);                              
00078                 
00079         isEnabledCommandNotify          = false;
00080         memset (commandData, 0, COMMAND_DATA_LEN);
00081         isBTLEConnected                 = DISCONNECTED;
00082         serviceAdded                    = true;
00083     }
00084 
00085 // Tests Method
00086 
00087 /* BlueMicrosystem1 (only for Response)
00088 
00089     uint32_t sendcontrolState(uint8_t val, uint8_t Command, uint32_t Feature, uint16_t TimeStamp) {      
00090             STORE_LE_16(commandData ,TimeStamp); 
00091             STORE_BE_32(commandData+2,Feature);          
00092             commandData[6] = Command;
00093             commandData[7] = (val==0x01) ? 100: val;                     
00094             return ble.gattServer().write(commandCharacteristic.getValueAttribute().getHandle(), commandData, COMMAND_DATA_LEN, 0);
00095     }
00096     
00097     uint32_t updatecontrolState(uint8_t val, uint8_t Command, uint32_t Feature, uint16_t TimeStamp) {
00098             if (ble.getGapState().connected && isEnabledCommandNotify ) {  
00099                 return sendcontrolState(val, Command, Feature, TimeStamp);
00100             }
00101             return 0;
00102     }
00103 */
00104 
00105 /* Old SunTracker
00106 
00107    void sendCommandState(uint8_t State) {
00108             STORE_LE_16(commandData,State);
00109             PRINTF("sendCommandState handle: %d\n\r", commandCharacteristic.getValueAttribute().getHandle());
00110             memcpy (pastcommandData, commandData, COMMAND_DATA_LEN);
00111             ble.gattServer().write(commandCharacteristic.getValueAttribute().getHandle(), commandData, COMMAND_DATA_LEN, 0);         
00112             printf(" (Update)"); 
00113     } 
00114 
00115     void updateCommandState(uint8_t State) {
00116             if (ble.getGapState().connected && isEnabledCommandNotify ) { 
00117                 if (memcmp (&pastcommandData[0], &State, COMMAND_DATA_LEN) != 0) {
00118                     sendCommandState(State);           
00119                 }
00120             }
00121     }
00122 
00123 */  
00124 
00125     void enNotify (Gap::Handle_t handle) {
00126             printf("\n\r\n\renNotify (handle %d)", handle);
00127             if (isCommandHandle(handle)) { isEnabledCommandNotify = true; return; }    
00128     }
00129     
00130     void disNotify (Gap::Handle_t handle) {
00131             printf("\n\r\n\rdisNotify (handle %d)", handle);
00132             if (isCommandHandle(handle)) { isEnabledCommandNotify = false; return; }    
00133     }   
00134 
00135     bool isCommandNotificationEn (void) {
00136             return isEnabledCommandNotify;
00137     }
00138     
00139     bool isCommandHandle (Gap::Handle_t handle) {
00140             if (handle == commandCharacteristic.getValueAttribute().getHandle()) return true;
00141             return false;
00142     }
00143     
00144     void updateConnectionStatus(ConnectionStatus_t status) {            
00145             isEnabledCommandNotify = false;
00146             memset (commandData, 0, COMMAND_DATA_LEN);
00147             isBTLEConnected = status;             
00148     }      
00149 
00150 // Variables Initialization
00151 private:
00152         BLE                              &ble;
00153         uint8_t                          commandData[COMMAND_DATA_LEN];                
00154         uint8_t                          pastcommandData[COMMAND_DATA_LEN]; 
00155         GattCharacteristic               commandCharacteristic;  
00156         ConnectionStatus_t               isBTLEConnected;
00157         bool                             isEnabledCommandNotify;
00158 
00159 };