![](/media/cache/group/logo.jpg.50x50_q85.jpg)
HID scanner demo
Dependencies: BLE_API WIFI_API_32kRAM nRF51822 mbed
BLE HID function in this demo is temporary working with Android phone ONLY. Will be fixed to support iOS device in next update.
main.cpp@2:be04a220392e, 2015-08-14 (annotated)
- Committer:
- wgd8700
- Date:
- Fri Aug 14 09:37:24 2015 +0000
- Revision:
- 2:be04a220392e
- Parent:
- 1:51535675abf4
Update BLE_API, nRF51822, and mbed libs to sync throughout all sample code, work for 32KB RAM revision platform; Update main.cpp to add RF switch lock mechanism to prevent packet losses for BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Marcomissyou | 0:b0fc0661c081 | 1 | #include "mbed.h" |
Marcomissyou | 0:b0fc0661c081 | 2 | #include "BLE.h" |
Marcomissyou | 0:b0fc0661c081 | 3 | #include "BatteryService.h" |
Marcomissyou | 0:b0fc0661c081 | 4 | #include "DeviceInformationService.h" |
Marcomissyou | 0:b0fc0661c081 | 5 | #include "HIDService.h" |
Marcomissyou | 1:51535675abf4 | 6 | #include "WIFIDevice.h" |
Marcomissyou | 1:51535675abf4 | 7 | #include "EthernetInterface.h" |
Marcomissyou | 0:b0fc0661c081 | 8 | |
Marcomissyou | 0:b0fc0661c081 | 9 | BLEDevice ble; |
Marcomissyou | 1:51535675abf4 | 10 | DigitalOut led01(p7); |
Marcomissyou | 0:b0fc0661c081 | 11 | Serial uart(p25,p23); |
Marcomissyou | 1:51535675abf4 | 12 | DigitalOut RFSWIO(p19); |
Marcomissyou | 1:51535675abf4 | 13 | Ticker flipper; |
Marcomissyou | 1:51535675abf4 | 14 | WIFIDevice wifi; |
Marcomissyou | 1:51535675abf4 | 15 | EthernetInterface eth; |
Marcomissyou | 1:51535675abf4 | 16 | |
wgd8700 | 2:be04a220392e | 17 | bool flip_lock = false; |
wgd8700 | 2:be04a220392e | 18 | |
Marcomissyou | 1:51535675abf4 | 19 | void flip() { |
wgd8700 | 2:be04a220392e | 20 | if (!flip_lock) RFSWIO = !RFSWIO; |
Marcomissyou | 1:51535675abf4 | 21 | } |
Marcomissyou | 0:b0fc0661c081 | 22 | |
Marcomissyou | 0:b0fc0661c081 | 23 | unsigned char keyData; |
Marcomissyou | 0:b0fc0661c081 | 24 | bool is_input = false; |
wgd8700 | 2:be04a220392e | 25 | char* ap_ssid = "Tsungta_iPhone"; |
wgd8700 | 2:be04a220392e | 26 | char* ap_pw = "icq87001"; |
wgd8700 | 2:be04a220392e | 27 | const char* PC_SERVER_ADDRESS = "172.20.10.4"; |
Marcomissyou | 1:51535675abf4 | 28 | uint16_t PC_PORT = 5222; |
Marcomissyou | 1:51535675abf4 | 29 | |
Marcomissyou | 0:b0fc0661c081 | 30 | static const char DEVICE_NAME[] = "HID_Keyboard"; |
Marcomissyou | 0:b0fc0661c081 | 31 | static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE}; |
Marcomissyou | 0:b0fc0661c081 | 32 | static volatile bool triggerSensorPolling = false; |
Marcomissyou | 0:b0fc0661c081 | 33 | |
Marcomissyou | 0:b0fc0661c081 | 34 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
Marcomissyou | 0:b0fc0661c081 | 35 | { |
Marcomissyou | 0:b0fc0661c081 | 36 | ble.startAdvertising(); // restart advertising |
Marcomissyou | 0:b0fc0661c081 | 37 | } |
Marcomissyou | 0:b0fc0661c081 | 38 | |
Marcomissyou | 1:51535675abf4 | 39 | void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) |
Marcomissyou | 1:51535675abf4 | 40 | { |
Marcomissyou | 1:51535675abf4 | 41 | printf("Input passKey: "); |
Marcomissyou | 1:51535675abf4 | 42 | for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { |
Marcomissyou | 1:51535675abf4 | 43 | printf("%c ", passkey[i]); |
Marcomissyou | 1:51535675abf4 | 44 | } |
Marcomissyou | 1:51535675abf4 | 45 | printf("\r\n"); |
Marcomissyou | 1:51535675abf4 | 46 | } |
Marcomissyou | 1:51535675abf4 | 47 | |
Marcomissyou | 1:51535675abf4 | 48 | void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) |
Marcomissyou | 1:51535675abf4 | 49 | { |
Marcomissyou | 1:51535675abf4 | 50 | if (status == SecurityManager::SEC_STATUS_SUCCESS) { |
Marcomissyou | 1:51535675abf4 | 51 | printf("Security success\r\n", status); |
Marcomissyou | 1:51535675abf4 | 52 | } else { |
Marcomissyou | 1:51535675abf4 | 53 | printf("Security failed\r\n", status); |
Marcomissyou | 1:51535675abf4 | 54 | } |
Marcomissyou | 1:51535675abf4 | 55 | } |
Marcomissyou | 0:b0fc0661c081 | 56 | |
Marcomissyou | 1:51535675abf4 | 57 | |
Marcomissyou | 1:51535675abf4 | 58 | static uint8_t key_press_scan_buff[50]; |
Marcomissyou | 1:51535675abf4 | 59 | static uint8_t modifyKey[50]; |
Marcomissyou | 1:51535675abf4 | 60 | char msg[25] = "NNN40 CONNECTED\n"; |
Marcomissyou | 0:b0fc0661c081 | 61 | int main(void) |
wgd8700 | 2:be04a220392e | 62 | { |
Marcomissyou | 1:51535675abf4 | 63 | uart.baud(115200); |
Marcomissyou | 0:b0fc0661c081 | 64 | uart.printf("Starting HID Service\n"); |
Marcomissyou | 1:51535675abf4 | 65 | RFSWIO = 1; |
Marcomissyou | 1:51535675abf4 | 66 | EthernetInterface eth; |
Marcomissyou | 1:51535675abf4 | 67 | eth.init(); //Use DHCP |
Marcomissyou | 1:51535675abf4 | 68 | wifi.setNetwork(ap_ssid, ap_pw, 0); // set given SSID and PW as the highest priority |
Marcomissyou | 1:51535675abf4 | 69 | eth.connect(40000); |
Marcomissyou | 0:b0fc0661c081 | 70 | led01 = 1; |
Marcomissyou | 1:51535675abf4 | 71 | TCPSocketConnection socket; |
Marcomissyou | 1:51535675abf4 | 72 | socket.connect(PC_SERVER_ADDRESS,PC_PORT); |
Marcomissyou | 1:51535675abf4 | 73 | socket.send(msg, sizeof(msg)); |
Marcomissyou | 1:51535675abf4 | 74 | memset(msg, 0, 25); |
Marcomissyou | 1:51535675abf4 | 75 | /*======BLE setup========*/ |
Marcomissyou | 0:b0fc0661c081 | 76 | ble.init(); |
Marcomissyou | 1:51535675abf4 | 77 | bool enableBonding = true; |
Marcomissyou | 1:51535675abf4 | 78 | bool requireMITM = true; |
Marcomissyou | 1:51535675abf4 | 79 | //const uint8_t passkeyValue[6] = {0x00,0x00,0x00,0x00,0x00,0x00}; |
Marcomissyou | 1:51535675abf4 | 80 | ble.initializeSecurity(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY); //IO_CAPS_DISPLAY_ONLY, IO_CAPS_NONE |
Marcomissyou | 0:b0fc0661c081 | 81 | ble.onDisconnection(disconnectionCallback); |
Marcomissyou | 1:51535675abf4 | 82 | ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); |
Marcomissyou | 1:51535675abf4 | 83 | ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); |
Marcomissyou | 0:b0fc0661c081 | 84 | |
Marcomissyou | 0:b0fc0661c081 | 85 | /* Setup primary service. */ |
Marcomissyou | 0:b0fc0661c081 | 86 | HIDService hidService(ble); |
Marcomissyou | 0:b0fc0661c081 | 87 | /* Setup auxiliary service. */ |
Marcomissyou | 0:b0fc0661c081 | 88 | DeviceInformationService deviceInfo(ble, "ARM", "CYNTEC", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
Marcomissyou | 0:b0fc0661c081 | 89 | /* Setup advertising. */ |
Marcomissyou | 0:b0fc0661c081 | 90 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Marcomissyou | 0:b0fc0661c081 | 91 | ble.accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); |
Marcomissyou | 0:b0fc0661c081 | 92 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Marcomissyou | 0:b0fc0661c081 | 93 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Marcomissyou | 0:b0fc0661c081 | 94 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Marcomissyou | 0:b0fc0661c081 | 95 | ble.setAdvertisingInterval(1000); |
Marcomissyou | 1:51535675abf4 | 96 | |
Marcomissyou | 1:51535675abf4 | 97 | RFSWIO = 1; |
Marcomissyou | 0:b0fc0661c081 | 98 | ble.startAdvertising(); |
Marcomissyou | 0:b0fc0661c081 | 99 | uart.printf("Starting advertising\n"); |
Marcomissyou | 1:51535675abf4 | 100 | wait(5); |
Marcomissyou | 1:51535675abf4 | 101 | flipper.attach(&flip, 0.15); |
Marcomissyou | 1:51535675abf4 | 102 | int index_b = 0; |
Marcomissyou | 1:51535675abf4 | 103 | int index_w = 0; |
Marcomissyou | 1:51535675abf4 | 104 | |
Marcomissyou | 0:b0fc0661c081 | 105 | while (1) { |
Marcomissyou | 0:b0fc0661c081 | 106 | if (uart.readable() == 1) { |
Marcomissyou | 0:b0fc0661c081 | 107 | keyData = uart.getc(); |
Marcomissyou | 0:b0fc0661c081 | 108 | uart.putc(keyData); |
Marcomissyou | 1:51535675abf4 | 109 | msg[index_w++] = keyData; |
Marcomissyou | 0:b0fc0661c081 | 110 | if(keyData <= 0x39 && keyData >= 0x30){ //number |
Marcomissyou | 0:b0fc0661c081 | 111 | if(keyData == 0x30){ |
Marcomissyou | 1:51535675abf4 | 112 | modifyKey[index_b] = 0x00; |
Marcomissyou | 1:51535675abf4 | 113 | key_press_scan_buff[index_b] = 0x27; |
Marcomissyou | 1:51535675abf4 | 114 | index_b++; |
Marcomissyou | 1:51535675abf4 | 115 | key_press_scan_buff[index_b] = 0x73; |
Marcomissyou | 0:b0fc0661c081 | 116 | } else { |
Marcomissyou | 1:51535675abf4 | 117 | modifyKey[index_b] = 0x00; |
Marcomissyou | 1:51535675abf4 | 118 | key_press_scan_buff[index_b] = keyData-0x13; |
Marcomissyou | 1:51535675abf4 | 119 | index_b++; |
Marcomissyou | 1:51535675abf4 | 120 | key_press_scan_buff[index_b] = 0x73; |
Marcomissyou | 0:b0fc0661c081 | 121 | } |
Marcomissyou | 0:b0fc0661c081 | 122 | } else if(keyData <= 0x7a && keyData >= 0x61 ){ //lowercase letters |
Marcomissyou | 1:51535675abf4 | 123 | modifyKey[index_b] = 0x00; |
Marcomissyou | 1:51535675abf4 | 124 | key_press_scan_buff[index_b] = keyData-0x5d; |
Marcomissyou | 1:51535675abf4 | 125 | index_b++; |
Marcomissyou | 1:51535675abf4 | 126 | key_press_scan_buff[index_b] = 0x73; |
Marcomissyou | 0:b0fc0661c081 | 127 | } else if(keyData <= 0x5a && keyData >= 0x41){ //uppercase letters |
Marcomissyou | 1:51535675abf4 | 128 | modifyKey[index_b] = 0x02; |
Marcomissyou | 1:51535675abf4 | 129 | key_press_scan_buff[index_b] = keyData-0x3d; |
Marcomissyou | 1:51535675abf4 | 130 | index_b++; |
Marcomissyou | 1:51535675abf4 | 131 | key_press_scan_buff[index_b] = 0x73; |
Marcomissyou | 1:51535675abf4 | 132 | } else if (keyData == 0x20) { //space |
Marcomissyou | 1:51535675abf4 | 133 | modifyKey[index_b] = 0x00; |
Marcomissyou | 1:51535675abf4 | 134 | key_press_scan_buff[index_b] = 0x2c; |
Marcomissyou | 1:51535675abf4 | 135 | index_b++; |
Marcomissyou | 1:51535675abf4 | 136 | key_press_scan_buff[index_b] = 0x73; |
Marcomissyou | 0:b0fc0661c081 | 137 | } else { |
Marcomissyou | 1:51535675abf4 | 138 | modifyKey[index_b] = 0x00; |
Marcomissyou | 1:51535675abf4 | 139 | //key_press_scan_buff[index_b] = 0x73; //this is dummy data. |
Marcomissyou | 1:51535675abf4 | 140 | //msg[index_w+1] = '\0'; |
Marcomissyou | 0:b0fc0661c081 | 141 | } |
Marcomissyou | 1:51535675abf4 | 142 | index_b++; |
Marcomissyou | 0:b0fc0661c081 | 143 | if(keyData == 0x0a && ble.getGapState().connected){ |
wgd8700 | 2:be04a220392e | 144 | flip_lock = true; |
wgd8700 | 2:be04a220392e | 145 | RFSWIO = 0;// RF Switch to BLE and lockup temporary |
Marcomissyou | 1:51535675abf4 | 146 | for(int i = 0; i < index_b ; i++){ |
Marcomissyou | 1:51535675abf4 | 147 | hidService.updateReport(modifyKey[i], key_press_scan_buff[i]); |
Marcomissyou | 1:51535675abf4 | 148 | wait(0.03); |
Marcomissyou | 0:b0fc0661c081 | 149 | } |
wgd8700 | 2:be04a220392e | 150 | flip_lock = false; |
wgd8700 | 2:be04a220392e | 151 | |
wgd8700 | 2:be04a220392e | 152 | flip_lock = true; |
wgd8700 | 2:be04a220392e | 153 | RFSWIO = 1;// RF Switch to WiFi and lockup temporary |
wgd8700 | 2:be04a220392e | 154 | socket.send(msg, sizeof(msg)); |
wgd8700 | 2:be04a220392e | 155 | flip_lock = false; |
Marcomissyou | 1:51535675abf4 | 156 | |
Marcomissyou | 1:51535675abf4 | 157 | index_b = 0; |
Marcomissyou | 1:51535675abf4 | 158 | index_w = 0; |
Marcomissyou | 1:51535675abf4 | 159 | memset(modifyKey, 0, 50); |
Marcomissyou | 1:51535675abf4 | 160 | memset(msg, 0, 25); |
Marcomissyou | 1:51535675abf4 | 161 | memset(key_press_scan_buff, 0, 50); |
Marcomissyou | 0:b0fc0661c081 | 162 | } |
Marcomissyou | 0:b0fc0661c081 | 163 | |
Marcomissyou | 0:b0fc0661c081 | 164 | } |
Marcomissyou | 0:b0fc0661c081 | 165 | } |
Marcomissyou | 0:b0fc0661c081 | 166 | } |