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

CustomConsoleService.h

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CustomConsoleService.h
00004   * @author  Central Labs / AST
00005   * @version V0.9.0
00006   * @date    23-Dec-2015
00007   * @brief   BLE console 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_CONSOLE_SERVICE_H__
00039 #define __CUSTOM_BLE_CONSOLE_SERVICE_H__
00040 
00041 #include "BLE.h"
00042 #include "ble_debug.h"
00043 
00044 const UUID::LongUUIDBytes_t CONS_SERVICE_UUID_128 =                   { 0x00,0x00,0x00,0x00,0x00,0x0E,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b }; 
00045 const UUID::LongUUIDBytes_t CONS_TERM_CHAR_UUID_128 =                 { 0x00,0x00,0x00,0x01,0x00,0x0E,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b };  
00046 const UUID::LongUUIDBytes_t CONS_STDERR_CHAR_UUID_128 =               { 0x00,0x00,0x00,0x02,0x00,0x0E,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b };  
00047 
00048 #define W2ST_CONSOLE_MAX_CHAR_LEN   20
00049 
00050 /* Custom Sensors Service */
00051 class CustomConsoleService  {
00052 public:
00053     CustomConsoleService(BLE &_ble) :
00054         ble(_ble),
00055         consTermCharacteristic(CONS_TERM_CHAR_UUID_128,LastTermBuffer, W2ST_CONSOLE_MAX_CHAR_LEN, W2ST_CONSOLE_MAX_CHAR_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
00056                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00057         consStderrCharacteristic(CONS_STDERR_CHAR_UUID_128, LastStderrBuffer, W2ST_CONSOLE_MAX_CHAR_LEN, W2ST_CONSOLE_MAX_CHAR_LEN,
00058                                  GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00059         {
00060  
00061             static bool serviceAdded = false; /* We should only ever need to add the env service once. */
00062             if (serviceAdded) {
00063             return;
00064             }
00065 
00066             GattCharacteristic *charTable[] = {&consTermCharacteristic, &consStderrCharacteristic};        
00067                         
00068             GattService   consService(CONS_SERVICE_UUID_128, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));               
00069                 
00070             ble.gattServer().addService(consService);             
00071 
00072             memset (LastTermBuffer, 0, W2ST_CONSOLE_MAX_CHAR_LEN);  
00073             memset (LastStderrBuffer, 0, W2ST_CONSOLE_MAX_CHAR_LEN);           
00074                                                                          
00075             isBTLEConnected                     = DISCONNECTED;  
00076             isEnabledTermNotify                 = false;
00077             isEnabledStderrNotify               = false;                                                                            
00078             serviceAdded                        = true;                                                                                                                                  
00079         }
00080 
00081     tBleStatus updateTerm (uint8_t *data,uint8_t length) {
00082         tBleStatus ret;
00083         uint8_t Offset;
00084         uint8_t DataToSend;
00085 //        uint8_t BufferToWrite[W2ST_CONSOLE_MAX_CHAR_LEN];
00086 //        uint8_t BytesToWrite;    
00087 
00088         if (isEnabledTermNotify && isBTLEConnected==CONNECTED) {
00089         /* Split the code in packages */
00090             for(Offset =0; Offset<length; Offset +=W2ST_CONSOLE_MAX_CHAR_LEN){
00091                 DataToSend = (length-Offset);
00092                 DataToSend = (DataToSend>W2ST_CONSOLE_MAX_CHAR_LEN) ?  W2ST_CONSOLE_MAX_CHAR_LEN : DataToSend;
00093 
00094                 /* keep a copy */
00095                 memcpy(LastTermBuffer,data+Offset,DataToSend);
00096                 LastTermLen = DataToSend;       
00097                 PRINTF("updateTerm handle: %d string: %s\n\r", consTermCharacteristic.getValueAttribute().getHandle(), LastTermBuffer);
00098                 ret = ble.gattServer().write(consTermCharacteristic.getValueAttribute().getHandle(), data+Offset, DataToSend, 0);
00099                 if (ret != BLE_ERROR_NONE) {  /* FIXME the wrong errcode from BLE requires BLE fix */
00100 //                    BytesToWrite = sprintf((char *)BufferToWrite, "Error Updating Stdout Char\r\n");
00101 //                    updateStderr(BufferToWrite,BytesToWrite);               
00102                 }
00103                 return ret;               
00104             }
00105         }
00106         return BLE_STATUS_SUCCESS;
00107     }
00108        
00109     tBleStatus updateStderr (uint8_t *data,uint8_t length) {
00110 
00111         uint8_t Offset;
00112         uint8_t DataToSend;
00113         
00114         if (isEnabledStderrNotify && isBTLEConnected==CONNECTED) {
00115         /* Split the code in packages */
00116             for(Offset =0; Offset<length; Offset +=W2ST_CONSOLE_MAX_CHAR_LEN){
00117                 DataToSend = (length-Offset);
00118                 DataToSend = (DataToSend>W2ST_CONSOLE_MAX_CHAR_LEN) ?  W2ST_CONSOLE_MAX_CHAR_LEN : DataToSend;
00119 
00120                 /* keep a copy */
00121                 memcpy(LastStderrBuffer,data+Offset,DataToSend);
00122                 LastStderrLen = DataToSend;
00123                 PRINTF("updateStderr handle: %d string: %s\n\r", consStderrCharacteristic.getValueAttribute().getHandle(), LastStderrBuffer);
00124                 return (ble.gattServer().write(consStderrCharacteristic.getValueAttribute().getHandle(), data+Offset, DataToSend, 0));                          
00125             }
00126         }
00127         return BLE_STATUS_SUCCESS;
00128     }
00129 
00130     void updateConnectionStatus (ConnectionStatus_t status){
00131             isBTLEConnected = status;                      
00132             isEnabledTermNotify                 = false;
00133             isEnabledStderrNotify               = false;                 
00134     }             
00135 
00136     bool isTermHandle (Gap::Handle_t handle) {
00137             if (handle == consTermCharacteristic.getValueAttribute().getHandle() - BLE_HANDLE_VALUE_OFFSET) return true;
00138             return false;
00139     }
00140 
00141     bool isStderrHandle (Gap::Handle_t handle) {
00142             if (handle == consStderrCharacteristic.getValueAttribute().getHandle() - BLE_HANDLE_VALUE_OFFSET) return true;
00143             return false;
00144     }
00145     
00146     void enNotify (Gap::Handle_t handle)  {
00147             if (isTermHandle(handle - BLE_HANDLE_EN_DIS_OFFSET))   { isEnabledTermNotify = true; return; }    
00148             if (isStderrHandle(handle - BLE_HANDLE_EN_DIS_OFFSET)) { isEnabledStderrNotify = true; return; }                
00149     }
00150         
00151     void disNotify (Gap::Handle_t handle) {
00152             if (isTermHandle(handle - BLE_HANDLE_EN_DIS_OFFSET))   { isEnabledTermNotify = false; return; }    
00153             if (isStderrHandle(handle - BLE_HANDLE_EN_DIS_OFFSET)) { isEnabledStderrNotify = false; return; }                            
00154     }
00155 
00156 
00157 private:
00158     BLE                 &ble;
00159     uint8_t             LastTermBuffer     [W2ST_CONSOLE_MAX_CHAR_LEN]; 
00160     uint8_t             LastTermLen;
00161     uint8_t             LastStderrBuffer   [W2ST_CONSOLE_MAX_CHAR_LEN];   
00162     uint8_t             LastStderrLen;                   
00163      
00164     GattCharacteristic  consTermCharacteristic;     
00165     GattCharacteristic  consStderrCharacteristic;   
00166     ConnectionStatus_t  isBTLEConnected;
00167     bool                isEnabledTermNotify;
00168     bool                isEnabledStderrNotify;    
00169 };
00170 
00171 #endif /* #ifndef __CUSTOM_BLE_CONSOLE_SERVICE_H__*/