HID scanner demo

Dependencies:   BLE_API WIFI_API_32kRAM nRF51822 mbed

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 * @class Human Interface Device Service
00007 * @brief BLE Human Interface Device Service. This service displays the Glucose measurement value represented as a 16bit Float format.<br>
00008 * @Author: Marco.Hsu  
00009 * @Email: marco.missyou@gmail.com  
00010 */
00011 
00012 extern const uint8_t KeyboardReportMap[76];
00013         
00014 class HIDService {
00015 public:
00016     HIDService(BLEDevice &_ble, const uint8_t* key = &KeyboardReportMap[0]):
00017         ble(_ble),
00018         protocol_modeValue(1),  // Report Protocol Mode(1), Boot Protocol Mode(0)
00019         KeyboardMap(key),
00020         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),
00021         ReportMap(GattCharacteristic::UUID_REPORT_MAP_CHAR, KeyboardMap.getPointer(), 76, sizeof(KeyboardMap), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00022         Report(GattCharacteristic::UUID_REPORT_CHAR, reportValue.getPointer(), 8, 8, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
00023         HID_Information(GattCharacteristic::UUID_HID_INFORMATION_CHAR, hidInformation.getPointer(), 4, 4, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
00024         HID_Control_Point(GattCharacteristic::UUID_HID_CONTROL_POINT_CHAR, &hidcontrolPointer, 1, 1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE)
00025         {
00026             static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
00027             if (serviceAdded) {
00028             return;
00029             }
00030             //Report.requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK);
00031             GattCharacteristic *charTable[] = {&Protocol_Mode, &ReportMap, &Report, &HID_Information, &HID_Control_Point};
00032             GattService         HIDGattService(GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00033             ble.addService(HIDGattService);
00034             serviceAdded = true;
00035             ble.onDataWritten(this, &HIDService::onDataWritten);
00036         }
00037 public:
00038     void updateReport(uint8_t modifydata, uint8_t data) {
00039         reportValue.updateReportValue(modifydata, data);
00040         ble.updateCharacteristicValue(Report.getValueAttribute().getHandle(), reportValue.getPointer(), 8);
00041     }
00042     
00043     virtual void onDataWritten(const GattWriteCallbackParams *params) {
00044         if (params->handle == HID_Control_Point.getValueAttribute().getHandle()) {
00045             uint16_t bytesRead = params->len;
00046             if (bytesRead == 1) {
00047                 memcpy(&hidcontrolPointer, params->data, bytesRead);
00048             }
00049         }
00050         if (params->handle == Report.getValueAttribute().getHandle()) {
00051             uint16_t bytesRead = params->len;
00052             if (bytesRead <= 4) {
00053                 memcpy(&reportValue, params->data, bytesRead);
00054             }
00055         }
00056     }
00057 
00058 private:
00059     struct ReportMapStructure{
00060             uint8_t KeyboardMap[76];
00061             ReportMapStructure(const uint8_t* data): KeyboardMap() {
00062             memcpy(&KeyboardMap[0], data, 76);
00063             }
00064             uint8_t     *getPointer(void) {
00065             return      KeyboardMap;
00066             }
00067     };
00068 
00069 private:
00070    struct ReportStructure {
00071             // Initial setting report value
00072             ReportStructure(): reportValue() {
00073                 uint8_t data= 0x00;
00074                 updateReportValue(data, data);
00075             }
00076             
00077             void updateReportValue(uint8_t modifyKey, uint8_t data){
00078                 memset(&reportValue[0], 0 ,8);
00079                 memcpy(&reportValue[0], &modifyKey, 1);
00080                 memcpy(&reportValue[2], &data, 1);
00081             }
00082         
00083             uint8_t     *getPointer(void) {
00084             return      reportValue;
00085             }
00086 
00087             uint8_t reportValue[8];
00088         };
00089         
00090 private:
00091     struct HIDInforStructure{
00092             uint16_t    bcdHID;
00093             uint8_t     bCountryCode;
00094             uint8_t     Flags;
00095             
00096             HIDInforStructure():bcdHID(0),bCountryCode(0),Flags(0){
00097                     memcpy(&hidInformation[0], &bcdHID, 2);
00098                     memcpy(&hidInformation[2], &bCountryCode, 1);
00099                     memcpy(&hidInformation[3], &Flags, 1);
00100                 }
00101             uint8_t     *getPointer(void) {
00102             return      hidInformation;
00103             }
00104             
00105             uint8_t hidInformation[4];
00106         };
00107         
00108 private:
00109     BLEDevice           &ble;
00110     uint8_t             protocol_modeValue;
00111     ReportStructure     reportValue;
00112     uint8_t             hidcontrolPointer;
00113     ReportMapStructure  KeyboardMap;
00114     HIDInforStructure   hidInformation;
00115     GattCharacteristic      Protocol_Mode;
00116     GattCharacteristic      ReportMap;
00117     GattCharacteristic      Report;
00118 //    ReadOnlyGattCharacteristic         Boot_Keyboard_Input_Report;
00119 //    ReadWriteGattCharacteristic        Boot_Keyboard_Output_Report;
00120 //    ReadOnlyGattCharacteristic         Boot_Mouse_Input_Report;
00121     GattCharacteristic      HID_Information;
00122     GattCharacteristic      HID_Control_Point;
00123 };
00124 #endif /* #ifndef __BLE_GLUCOSE_SERVICE_H__*/