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 CustomSensorsService.h Source File

CustomSensorsService.h

00001 /******************************************************************************
00002   * @file    CustomSensorsService.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 /* BLE Services: Primary + 3 Secondary (Char Desk) */
00041 //const LongUUIDBytes_t SENS_SERVICE_UUID           = {0x00,0x00,0x00,0x00,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B}; //Primary 
00042 //const LongUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID   = {0x00,0x00,0x00,0x01,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B}; //Difference Light/Mems
00043 //const LongUUIDBytes_t SENS_POSITION_CHAR_UUID     = {0x00,0x00,0x00,0x02,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B}; //Motor Position
00044 //const LongUUIDBytes_t SENS_SUNPANEL_CHAR_UUID     = {0x00,0x00,0x00,0x03,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B}; //SunPanel Voltage
00045 //const ShortUUIDBytes_t SENS_SERVICE_UUID            = 0xB000; //Primary 
00046 //const ShortUUIDBytes_t SENS_STATUS_CHAR_UUID        = 0xB001; //Status
00047 //const ShortUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID    = 0xB002; //Difference Light/Mems Sensors
00048 //const ShortUUIDBytes_t SENS_POSITION_CHAR_UUID      = 0xB003; //Motor Position
00049 //const ShortUUIDBytes_t SENS_SUNPANEL_CHAR_UUID      = 0xB004; //SunPanel Voltage    
00050 
00051 /* BLE Services: Primary + 4 Secondary (Char Desk) */
00052 const UUID::LongUUIDBytes_t SENS_SERVICE_UUID           = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Primary 
00053 const UUID::LongUUIDBytes_t SENS_STATUS_CHAR_UUID       = {0x00,0x00,0x08,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Status                 0x00000800
00054 const UUID::LongUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID   = {0x00,0x00,0x10,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Difference Light/Mems  0x00001000
00055 const UUID::LongUUIDBytes_t SENS_POSITION_CHAR_UUID     = {0x00,0x00,0x20,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Motor Position         0x00002000
00056 const UUID::LongUUIDBytes_t SENS_SUNPANEL_CHAR_UUID     = {0x00,0x00,0x40,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //SunPanel Voltage       0x00004000
00057                       
00058 //#define DIFFERENCE_DATA_LEN     2
00059 //#define POSITION_DATA_LEN       2
00060 //#define SUNPANEL_DATA_LEN       2
00061 // 1 byte   0xFF     8 bit      int8
00062 // 2 byte   0xFFFF   16 bit     int16
00063 
00064 #define STATUS_DATA_LEN         2+2 // TimeStamp + Status
00065 #define DIFFERENCE_DATA_LEN     2+2 // TimeStamp + Difference
00066 #define POSITION_DATA_LEN       2+2 // TimeStamp + Position
00067 #define SUNPANEL_DATA_LEN       2+2 // TimeStamp + SunPanel
00068 
00069 /* Custom Sensors Service */
00070 class CustomSensorService  {
00071 public:
00072     CustomSensorService(BLEDevice &_ble) :
00073         ble(_ble),
00074                 statusCharacteristic(SENS_STATUS_CHAR_UUID, envStatus, STATUS_DATA_LEN, STATUS_DATA_LEN,
00075                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00076                 differenceCharacteristic(SENS_DIFFERENCE_CHAR_UUID, envDifference, DIFFERENCE_DATA_LEN, DIFFERENCE_DATA_LEN,
00077                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00078                 positionCharacteristic(SENS_POSITION_CHAR_UUID, envPosition, POSITION_DATA_LEN, POSITION_DATA_LEN,
00079                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00080                 sunpanelCharacteristic(SENS_SUNPANEL_CHAR_UUID, envSunpanel, SUNPANEL_DATA_LEN, SUNPANEL_DATA_LEN,
00081                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)         
00082         {  
00083         static bool serviceAdded = false; // To be sure to define just one time
00084         if (serviceAdded) {
00085             return;
00086         }
00087 
00088         GattCharacteristic *charTable[] = {&statusCharacteristic, &differenceCharacteristic, &positionCharacteristic, &sunpanelCharacteristic};                                                                                                                          
00089         GattService   envService(SENS_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00090         ble.gattServer().addService(envService);             
00091 
00092         isEnabledStatusNotify           = false;
00093         isEnabledDifferenceNotify       = false;
00094         isEnabledPositionNotify         = false;
00095         isEnabledSunpanelNotify         = false;
00096         memset (envStatus,     0, STATUS_DATA_LEN);          
00097         memset (envDifference, 0, DIFFERENCE_DATA_LEN);  
00098         memset (envPosition,   0, POSITION_DATA_LEN);  
00099         memset (envSunpanel,   0, SUNPANEL_DATA_LEN);                                                       
00100         isBTLEConnected                 = DISCONNECTED;                                                                  
00101         serviceAdded                    = true;                                                                                                                                  
00102     }
00103 
00104 // Tests Method
00105 
00106     void sendEnvStatus (uint16_t Sta, uint16_t TimeStamp) {
00107             STORE_LE_16(envStatus,TimeStamp);
00108             STORE_LE_16(envStatus+2,Sta);
00109             PRINTF("sendEnvStatus handle: %d\n\r", statusCharacteristic.getValueAttribute().getHandle());
00110             memcpy (pastenvStatus, envStatus, STATUS_DATA_LEN);
00111             ble.gattServer().write(statusCharacteristic.getValueAttribute().getHandle(), envStatus, STATUS_DATA_LEN, 0);         
00112             #ifdef Debug
00113                 printf(" (Update)");
00114             #endif
00115     } 
00116 
00117     void updateEnvStatus (uint16_t Sta, uint16_t TimeStamp) {
00118             if (ble.getGapState().connected && isEnabledStatusNotify ) { 
00119                 if (memcmp (&pastenvStatus[2], &Sta, 2) != 0) {
00120                     sendEnvStatus (Sta, TimeStamp);
00121                 }
00122             }
00123     }
00124 
00125 //--
00126 
00127     void sendEnvDifference (uint16_t Dif, uint16_t TimeStamp) {
00128             STORE_LE_16(envDifference,TimeStamp);
00129             STORE_LE_16(envDifference+2,Dif);            
00130             PRINTF("sendEnvDifference handle: %d\n\r", differenceCharacteristic.getValueAttribute().getHandle());
00131             memcpy (pastenvDifference, envDifference, DIFFERENCE_DATA_LEN);
00132             ble.gattServer().write(differenceCharacteristic.getValueAttribute().getHandle(), envDifference, DIFFERENCE_DATA_LEN, 0);         
00133             #ifdef Debug
00134                 printf(" (Update)");
00135             #endif
00136     } 
00137 
00138     void updateEnvDifference (uint16_t Dif, uint16_t TimeStamp) {
00139             if (ble.getGapState().connected && isEnabledDifferenceNotify ) { 
00140                 if (memcmp (&pastenvDifference[2], &Dif, 2) != 0) {
00141                     sendEnvDifference (Dif, TimeStamp);
00142                 }
00143             }
00144     }
00145         
00146 //--
00147 
00148     void sendEnvPosition (uint16_t Pos, uint16_t TimeStamp) {
00149             STORE_LE_16(envPosition,TimeStamp);
00150             STORE_LE_16(envPosition+2,Pos);             
00151             PRINTF("sendEnvPosition handle: %d\n\r", positionCharacteristic.getValueAttribute().getHandle());
00152             memcpy (pastenvPosition, envPosition, POSITION_DATA_LEN);
00153             ble.gattServer().write(positionCharacteristic.getValueAttribute().getHandle(), envPosition, POSITION_DATA_LEN, 0);         
00154             #ifdef Debug
00155                 printf(" (Update)");
00156             #endif
00157     } 
00158 
00159     void updateEnvPosition (uint16_t Pos, uint16_t TimeStamp) {
00160             if (ble.getGapState().connected && isEnabledPositionNotify ) { 
00161                 if (memcmp (&pastenvPosition[2], &Pos, 2) != 0) {
00162                     sendEnvPosition (Pos, TimeStamp);
00163                 }
00164             }
00165     }
00166 
00167 //--
00168 
00169     void sendEnvSunpanel (uint16_t Sun, uint16_t TimeStamp) {
00170             STORE_LE_16(envSunpanel,TimeStamp);
00171             STORE_LE_16(envSunpanel+2,Sun);              
00172             PRINTF("sendEnvSunpanel handle: %d\n\r", sunpanelCharacteristic.getValueAttribute().getHandle());
00173             memcpy (pastenvSunpanel, envSunpanel, SUNPANEL_DATA_LEN);
00174             ble.gattServer().write(sunpanelCharacteristic.getValueAttribute().getHandle(), envSunpanel, SUNPANEL_DATA_LEN, 0);         
00175             #ifdef Debug
00176                 printf(" (Update)");
00177             #endif
00178     } 
00179 
00180     void updateEnvSunpanel (uint16_t Sun, uint16_t TimeStamp) {
00181             if (ble.getGapState().connected && isEnabledSunpanelNotify ) { 
00182                 if (memcmp (&pastenvSunpanel[2], &Sun, 2) != 0) {
00183                     sendEnvSunpanel (Sun, TimeStamp);            
00184                 }
00185             }
00186     }
00187 
00188 //---- 
00189                                         
00190     void enNotify (Gap::Handle_t handle) {
00191             printf("\n\r\n\renNotify (handle %d)", handle);
00192             if (isStatusHandle(handle)) { isEnabledStatusNotify = true; memset(envStatus,0,STATUS_DATA_LEN); return; }
00193             if (isDifferenceHandle(handle)) { isEnabledDifferenceNotify = true; memset(envDifference,0,DIFFERENCE_DATA_LEN); return; }
00194             if (isPositionHandle(handle)) { isEnabledPositionNotify = true; memset(envPosition,0,POSITION_DATA_LEN); return; }
00195             if (isSunpanelHandle(handle)) { isEnabledSunpanelNotify = true; memset(envSunpanel,0,SUNPANEL_DATA_LEN); return; }                     
00196     }
00197 
00198     void disNotify (Gap::Handle_t handle) {
00199             printf("\n\r\n\rdisNotify (handle %d)", handle);
00200             if (isStatusHandle(handle)) { isEnabledStatusNotify = false; memset(envStatus,0,STATUS_DATA_LEN); return; }
00201             if (isDifferenceHandle(handle)) { isEnabledDifferenceNotify = false; memset(envDifference,0,DIFFERENCE_DATA_LEN); return; }
00202             if (isPositionHandle(handle)) { isEnabledPositionNotify = false; memset(envPosition,0,POSITION_DATA_LEN); return; }        
00203             if (isSunpanelHandle(handle)) { isEnabledSunpanelNotify = false; memset(envSunpanel,0,SUNPANEL_DATA_LEN); return; }                   
00204     }
00205     
00206 //----    
00207       
00208     bool isStatusNotificationEn (void) {
00209             return isEnabledStatusNotify;
00210     }  
00211         
00212     bool isDifferenceNotificationEn (void) {
00213             return isEnabledDifferenceNotify;
00214     }
00215 
00216     bool isPositionNotificationEn (void) {
00217             return isEnabledPositionNotify;
00218     }
00219         
00220     bool isSunpanelNotificationEn (void) {
00221             return isEnabledSunpanelNotify;
00222     }
00223 
00224 //----
00225            
00226     bool isStatusHandle (Gap::Handle_t handle) {
00227             if (handle == statusCharacteristic.getValueAttribute().getHandle()) return true;
00228             return false;
00229     }
00230 
00231     bool isDifferenceHandle (Gap::Handle_t handle) {
00232             if (handle == differenceCharacteristic.getValueAttribute().getHandle()) return true;
00233             return false;
00234     }
00235 
00236     bool isPositionHandle (Gap::Handle_t handle) {
00237             if (handle == positionCharacteristic.getValueAttribute().getHandle()) return true;
00238             return false;
00239     }
00240 
00241     bool isSunpanelHandle (Gap::Handle_t handle) {
00242             if (handle == sunpanelCharacteristic.getValueAttribute().getHandle()) return true;
00243             return false;
00244     }
00245         
00246     void updateConnectionStatus(ConnectionStatus_t status) {
00247             isEnabledStatusNotify = false; 
00248             isEnabledDifferenceNotify = false;
00249             isEnabledPositionNotify = false;
00250             isEnabledSunpanelNotify = false;
00251             memset (envStatus, 0, STATUS_DATA_LEN);  
00252             memset (envDifference, 0, DIFFERENCE_DATA_LEN);  
00253             memset (envPosition, 0, POSITION_DATA_LEN);  
00254             memset (envSunpanel, 0, SUNPANEL_DATA_LEN);                             
00255             isBTLEConnected = status; 
00256     }
00257                 
00258 // Variables Initialization     
00259 private:
00260 
00261     BLEDevice            &ble;
00262     uint8_t              envStatus [STATUS_DATA_LEN];
00263     uint8_t              envDifference [DIFFERENCE_DATA_LEN];
00264     uint8_t              envPosition [POSITION_DATA_LEN];
00265     uint8_t              envSunpanel [SUNPANEL_DATA_LEN];    
00266     uint8_t              pastenvStatus [STATUS_DATA_LEN];   
00267     uint8_t              pastenvDifference [POSITION_DATA_LEN];
00268     uint8_t              pastenvPosition [POSITION_DATA_LEN];
00269     uint8_t              pastenvSunpanel [SUNPANEL_DATA_LEN];
00270     
00271     GattCharacteristic   statusCharacteristic;
00272     GattCharacteristic   differenceCharacteristic;   
00273     GattCharacteristic   positionCharacteristic;     
00274     GattCharacteristic   sunpanelCharacteristic;   
00275     
00276     ConnectionStatus_t   isBTLEConnected;
00277 
00278     bool                 isEnabledStatusNotify;
00279     bool                 isEnabledDifferenceNotify;
00280     bool                 isEnabledPositionNotify;
00281     bool                 isEnabledSunpanelNotify;
00282                
00283 };