BLE HID Keyboard example for Delta BLE platform

Fork of BLE_HeartRate_DELTA by Delta

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HIDService.h Source File

HIDService.h

00001 #ifndef __BLE_HID_SERVICE_H__
00002 #define __BLE_HID_SERVICE_H__
00003 
00004 #include "BLE.h"
00005 
00006 #define BLE_UUID_DESCRIPTOR_REPORT_REFERENCE 0x2908
00007 
00008 /**
00009 * @class Human Interface Device Service
00010 * @brief BLE Human Interface Device Service. This service displays the Glucose measurement value represented as a 16bit Float format.<br>
00011 * @Author: Marco.Hsu  
00012 * @Email: marco.missyou@gmail.com  
00013 */
00014 
00015 typedef struct {
00016     uint8_t ID;
00017     uint8_t type;
00018 } report_reference_t;
00019 
00020 enum ReportType {
00021     INPUT_REPORT    = 0x1,
00022     OUTPUT_REPORT   = 0x2,
00023     FEATURE_REPORT  = 0x3,
00024 };
00025 
00026 extern const uint8_t KeyboardReportMap[76];
00027         
00028 class HIDService {
00029 public:
00030     HIDService(BLE &_ble, const uint8_t* key = &KeyboardReportMap[0]):
00031         ble(_ble),
00032         protocol_modeValue(1),  // Report Protocol Mode(1), Boot Protocol Mode(0)
00033         KeyboardMap(key),
00034         Protocol_Mode(GattCharacteristic::UUID_PROTOCOL_MODE_CHAR, &protocol_modeValue, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
00035         ReportMap(GattCharacteristic::UUID_REPORT_MAP_CHAR, KeyboardMap.getPointer(), 76, sizeof(KeyboardMap), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
00036         Report(GattCharacteristic::UUID_REPORT_CHAR, reportValue.getPointer(), 8, 8, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE, inputReportDescriptors(), 1),
00037         HID_Information(GattCharacteristic::UUID_HID_INFORMATION_CHAR, hidInformation.getPointer(), 4, 4, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
00038         HID_Control_Point(GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR, &hidcontrolPointer, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
00039         inputReportReferenceDescriptor(BLE_UUID_DESCRIPTOR_REPORT_REFERENCE,(uint8_t *)&inputReportReferenceData, 2, 2)
00040         {
00041             static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
00042             if (serviceAdded) {
00043             return;
00044             }
00045             
00046             //SecurityManager::SecurityMode_t securityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM;
00047             Protocol_Mode.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
00048             ReportMap.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
00049             Report.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
00050             HID_Information.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
00051             HID_Control_Point.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM);
00052             GattCharacteristic *charTable[] = {&Protocol_Mode, &ReportMap, &Report, &HID_Information, &HID_Control_Point};
00053             GattService         HIDGattService(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00054             ble.addService(HIDGattService);
00055             serviceAdded = true;
00056             ble.onDataWritten(this, &HIDService::onDataWritten);
00057         }
00058 public:
00059     void updateReport(uint8_t modifydata, uint8_t data) {
00060         reportValue.updateReportValue(modifydata, data);
00061         
00062         //ble.updateCharacteristicValue(Report.getValueAttribute().getHandle(), reportValue.getPointer(), 8);
00063         ble.gattServer().write(Report.getValueAttribute().getHandle(), reportValue.getPointer(), 8);
00064     }
00065     
00066     virtual void onDataWritten(const GattWriteCallbackParams *params) {
00067         if (params->handle == HID_Control_Point.getValueAttribute().getHandle()) {
00068             uint16_t bytesRead = params->len;
00069             if (bytesRead == 1) {
00070                 memcpy(&hidcontrolPointer, params->data, bytesRead);
00071             }
00072         }
00073         if (params->handle == Report.getValueAttribute().getHandle()) {
00074             uint16_t bytesRead = params->len;
00075             if (bytesRead <= 4) {
00076                 memcpy(&reportValue, params->data, bytesRead);
00077             }
00078         }
00079     }
00080 
00081 private:
00082     struct ReportMapStructure{
00083             uint8_t KeyboardMap[76];
00084             ReportMapStructure(const uint8_t* data): KeyboardMap() {
00085             memcpy(&KeyboardMap[0], data, 76);
00086             }
00087             uint8_t     *getPointer(void) {
00088             return      KeyboardMap;
00089             }
00090     };
00091     
00092     GattAttribute** HIDService::inputReportDescriptors() {
00093         inputReportReferenceData.ID = 0;
00094         inputReportReferenceData.type = INPUT_REPORT;
00095 
00096         static GattAttribute *descs[] = {
00097             &inputReportReferenceDescriptor,
00098         };
00099         return descs;
00100     }
00101 
00102 private:
00103    struct ReportStructure {
00104             // Initial setting report value
00105             ReportStructure(): reportValue() {
00106                 uint8_t data= 0x00;
00107                 updateReportValue(data, data);
00108             }
00109             
00110             void updateReportValue(uint8_t modifyKey, uint8_t data){
00111                 memset(&reportValue[0], 0 ,8);
00112                 memcpy(&reportValue[0], &modifyKey, 1);
00113                 memcpy(&reportValue[2], &data, 1);
00114             }
00115         
00116             uint8_t     *getPointer(void) {
00117             return      reportValue;
00118             }
00119 
00120             uint8_t reportValue[8];
00121         };
00122         
00123 private:
00124     struct HIDInforStructure{
00125             uint16_t    bcdHID;
00126             uint8_t     bCountryCode;
00127             uint8_t     Flags;
00128             
00129             HIDInforStructure():bcdHID(0),bCountryCode(0),Flags(0){
00130                     memcpy(&hidInformation[0], &bcdHID, 2);
00131                     memcpy(&hidInformation[2], &bCountryCode, 1);
00132                     memcpy(&hidInformation[3], &Flags, 1);
00133                 }
00134             uint8_t     *getPointer(void) {
00135             return      hidInformation;
00136             }
00137             
00138             uint8_t hidInformation[4];
00139         };
00140         
00141 private:
00142     BLE           &ble;
00143     uint8_t             protocol_modeValue;
00144     ReportStructure     reportValue;
00145     uint8_t             hidcontrolPointer;
00146     ReportMapStructure  KeyboardMap;
00147     HIDInforStructure   hidInformation;
00148     GattCharacteristic      Protocol_Mode;
00149     GattCharacteristic      ReportMap;
00150     GattCharacteristic      Report;
00151 //    ReadOnlyGattCharacteristic         Boot_Keyboard_Input_Report;
00152 //    ReadWriteGattCharacteristic        Boot_Keyboard_Output_Report;
00153 //    ReadOnlyGattCharacteristic         Boot_Mouse_Input_Report;
00154     GattCharacteristic      HID_Information;
00155     GattCharacteristic      HID_Control_Point;
00156     GattAttribute inputReportReferenceDescriptor;
00157     report_reference_t inputReportReferenceData;
00158 };
00159 #endif /* #ifndef __BLE_GLUCOSE_SERVICE_H__*/