Hirofumi Otori / Mbed 2 deprecated BLE_AndroidShortCutKey

Dependencies:   BLE_API mbed nRF51822

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