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.
Diff: main.cpp
- Revision:
- 1:51535675abf4
- Parent:
- 0:b0fc0661c081
- Child:
- 2:be04a220392e
diff -r b0fc0661c081 -r 51535675abf4 main.cpp --- a/main.cpp Thu Jul 02 01:35:08 2015 +0000 +++ b/main.cpp Thu Jul 16 01:18:16 2015 +0000 @@ -3,13 +3,28 @@ #include "BatteryService.h" #include "DeviceInformationService.h" #include "HIDService.h" +#include "WIFIDevice.h" +#include "EthernetInterface.h" BLEDevice ble; -DigitalOut led01(LED1); +DigitalOut led01(p7); Serial uart(p25,p23); +DigitalOut RFSWIO(p19); +Ticker flipper; +WIFIDevice wifi; +EthernetInterface eth; + +void flip() { + RFSWIO = !RFSWIO; +} unsigned char keyData; bool is_input = false; +char ap_ssid[10] = {'H','T','C','8','2','0','\0'}; +char ap_pw[10] = {'1','1','1','1','1','1','1','1','\0'}; +const char* PC_SERVER_ADDRESS = "192.168.1.234"; +uint16_t PC_PORT = 5222; + static const char DEVICE_NAME[] = "HID_Keyboard"; static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE}; static volatile bool triggerSensorPolling = false; @@ -19,19 +34,51 @@ ble.startAdvertising(); // restart advertising } +void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) +{ + printf("Input passKey: "); + for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { + printf("%c ", passkey[i]); + } + printf("\r\n"); +} + +void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) +{ + if (status == SecurityManager::SEC_STATUS_SUCCESS) { + printf("Security success\r\n", status); + } else { + printf("Security failed\r\n", status); + } +} -static uint8_t key_press_scan_buff[30]; -static uint8_t modifyKey[30]; + +static uint8_t key_press_scan_buff[50]; +static uint8_t modifyKey[50]; +char msg[25] = "NNN40 CONNECTED\n"; int main(void) -{ uart.baud(9600); +{ NRF_CLOCK->XTALFREQ = 0x00; //Tsungta, Used to active radio correctly external when XTAL 32MHz is loaded + uart.baud(115200); uart.printf("Starting HID Service\n"); - //uart.attach(&uart_rx); + RFSWIO = 1; + EthernetInterface eth; + eth.init(); //Use DHCP + wifi.setNetwork(ap_ssid, ap_pw, 0); // set given SSID and PW as the highest priority + eth.connect(40000); led01 = 1; + TCPSocketConnection socket; + socket.connect(PC_SERVER_ADDRESS,PC_PORT); + socket.send(msg, sizeof(msg)); + memset(msg, 0, 25); + /*======BLE setup========*/ ble.init(); - bool enableBonding = false; - bool requireMITM = false; - ble.initializeSecurity(enableBonding, requireMITM); + bool enableBonding = true; + bool requireMITM = true; + //const uint8_t passkeyValue[6] = {0x00,0x00,0x00,0x00,0x00,0x00}; + ble.initializeSecurity(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY); //IO_CAPS_DISPLAY_ONLY, IO_CAPS_NONE ble.onDisconnection(disconnectionCallback); + ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); + ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); /* Setup primary service. */ HIDService hidService(ble); @@ -44,46 +91,65 @@ ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.setAdvertisingInterval(1000); + + RFSWIO = 1; ble.startAdvertising(); uart.printf("Starting advertising\n"); - int index = 0; + wait(5); + flipper.attach(&flip, 0.15); + int index_b = 0; + int index_w = 0; + while (1) { if (uart.readable() == 1) { keyData = uart.getc(); uart.putc(keyData); + msg[index_w++] = keyData; if(keyData <= 0x39 && keyData >= 0x30){ //number if(keyData == 0x30){ - modifyKey[index] = 0x00; - key_press_scan_buff[index] = 0x27; + modifyKey[index_b] = 0x00; + key_press_scan_buff[index_b] = 0x27; + index_b++; + key_press_scan_buff[index_b] = 0x73; } else { - modifyKey[index] = 0x00; - key_press_scan_buff[index] = keyData-0x13; + modifyKey[index_b] = 0x00; + key_press_scan_buff[index_b] = keyData-0x13; + index_b++; + key_press_scan_buff[index_b] = 0x73; } } else if(keyData <= 0x7a && keyData >= 0x61 ){ //lowercase letters - modifyKey[index] = 0x00; - key_press_scan_buff[index] = keyData-0x5d; + modifyKey[index_b] = 0x00; + key_press_scan_buff[index_b] = keyData-0x5d; + index_b++; + key_press_scan_buff[index_b] = 0x73; } else if(keyData <= 0x5a && keyData >= 0x41){ //uppercase letters - modifyKey[index] = 0x02; - key_press_scan_buff[index] = keyData-0x3d; - } else if (keyData == 0x20) { - modifyKey[index] = 0x00; - key_press_scan_buff[index] = 0x2c; + modifyKey[index_b] = 0x02; + key_press_scan_buff[index_b] = keyData-0x3d; + index_b++; + key_press_scan_buff[index_b] = 0x73; + } else if (keyData == 0x20) { //space + modifyKey[index_b] = 0x00; + key_press_scan_buff[index_b] = 0x2c; + index_b++; + key_press_scan_buff[index_b] = 0x73; } else { - modifyKey[index] = 0x00; - //key_press_scan_buff[index] = 0x28; - //key_press_scan_buff[index++] = 0x73; - key_press_scan_buff[index] = 0x73; + modifyKey[index_b] = 0x00; + //key_press_scan_buff[index_b] = 0x73; //this is dummy data. + //msg[index_w+1] = '\0'; } - index++; + index_b++; if(keyData == 0x0a && ble.getGapState().connected){ - for(int i = 0; i < index; i++){ - //uart.putc(key_press_scan_buff[i]); - hidService.updateReport(modifyKey[i], key_press_scan_buff[i]); - wait(0.1); + for(int i = 0; i < index_b ; i++){ + hidService.updateReport(modifyKey[i], key_press_scan_buff[i]); + wait(0.03); } - index = 0; - memset(modifyKey, 0, 30); - memset(key_press_scan_buff, 0, 30); + + socket.send(msg, sizeof(msg)); + index_b = 0; + index_w = 0; + memset(modifyKey, 0, 50); + memset(msg, 0, 25); + memset(key_press_scan_buff, 0, 50); } }