HID scanner demo
Dependencies: BLE_API WIFI_API_32kRAM nRF51822 mbed
main.cpp
00001 #include "mbed.h" 00002 #include "BLE.h" 00003 #include "BatteryService.h" 00004 #include "DeviceInformationService.h" 00005 #include "HIDService.h" 00006 #include "WIFIDevice.h" 00007 #include "EthernetInterface.h" 00008 00009 BLEDevice ble; 00010 DigitalOut led01(p7); 00011 Serial uart(p25,p23); 00012 DigitalOut RFSWIO(p19); 00013 Ticker flipper; 00014 WIFIDevice wifi; 00015 EthernetInterface eth; 00016 00017 bool flip_lock = false; 00018 00019 void flip() { 00020 if (!flip_lock) RFSWIO = !RFSWIO; 00021 } 00022 00023 unsigned char keyData; 00024 bool is_input = false; 00025 char* ap_ssid = "Tsungta_iPhone"; 00026 char* ap_pw = "icq87001"; 00027 const char* PC_SERVER_ADDRESS = "172.20.10.4"; 00028 uint16_t PC_PORT = 5222; 00029 00030 static const char DEVICE_NAME[] = "HID_Keyboard"; 00031 static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE}; 00032 static volatile bool triggerSensorPolling = false; 00033 00034 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00035 { 00036 ble.startAdvertising(); // restart advertising 00037 } 00038 00039 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) 00040 { 00041 printf("Input passKey: "); 00042 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { 00043 printf("%c ", passkey[i]); 00044 } 00045 printf("\r\n"); 00046 } 00047 00048 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) 00049 { 00050 if (status == SecurityManager::SEC_STATUS_SUCCESS) { 00051 printf("Security success\r\n", status); 00052 } else { 00053 printf("Security failed\r\n", status); 00054 } 00055 } 00056 00057 00058 static uint8_t key_press_scan_buff[50]; 00059 static uint8_t modifyKey[50]; 00060 char msg[25] = "NNN40 CONNECTED\n"; 00061 int main(void) 00062 { 00063 uart.baud(115200); 00064 uart.printf("Starting HID Service\n"); 00065 RFSWIO = 1; 00066 EthernetInterface eth; 00067 eth.init(); //Use DHCP 00068 wifi.setNetwork(ap_ssid, ap_pw, 0); // set given SSID and PW as the highest priority 00069 eth.connect(40000); 00070 led01 = 1; 00071 TCPSocketConnection socket; 00072 socket.connect(PC_SERVER_ADDRESS,PC_PORT); 00073 socket.send(msg, sizeof(msg)); 00074 memset(msg, 0, 25); 00075 /*======BLE setup========*/ 00076 ble.init(); 00077 bool enableBonding = true; 00078 bool requireMITM = true; 00079 //const uint8_t passkeyValue[6] = {0x00,0x00,0x00,0x00,0x00,0x00}; 00080 ble.initializeSecurity(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY); //IO_CAPS_DISPLAY_ONLY, IO_CAPS_NONE 00081 ble.onDisconnection(disconnectionCallback); 00082 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); 00083 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); 00084 00085 /* Setup primary service. */ 00086 HIDService hidService(ble); 00087 /* Setup auxiliary service. */ 00088 DeviceInformationService deviceInfo(ble, "ARM", "CYNTEC", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00089 /* Setup advertising. */ 00090 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00091 ble.accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); 00092 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00093 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00094 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00095 ble.setAdvertisingInterval(1000); 00096 00097 RFSWIO = 1; 00098 ble.startAdvertising(); 00099 uart.printf("Starting advertising\n"); 00100 wait(5); 00101 flipper.attach(&flip, 0.15); 00102 int index_b = 0; 00103 int index_w = 0; 00104 00105 while (1) { 00106 if (uart.readable() == 1) { 00107 keyData = uart.getc(); 00108 uart.putc(keyData); 00109 msg[index_w++] = keyData; 00110 if(keyData <= 0x39 && keyData >= 0x30){ //number 00111 if(keyData == 0x30){ 00112 modifyKey[index_b] = 0x00; 00113 key_press_scan_buff[index_b] = 0x27; 00114 index_b++; 00115 key_press_scan_buff[index_b] = 0x73; 00116 } else { 00117 modifyKey[index_b] = 0x00; 00118 key_press_scan_buff[index_b] = keyData-0x13; 00119 index_b++; 00120 key_press_scan_buff[index_b] = 0x73; 00121 } 00122 } else if(keyData <= 0x7a && keyData >= 0x61 ){ //lowercase letters 00123 modifyKey[index_b] = 0x00; 00124 key_press_scan_buff[index_b] = keyData-0x5d; 00125 index_b++; 00126 key_press_scan_buff[index_b] = 0x73; 00127 } else if(keyData <= 0x5a && keyData >= 0x41){ //uppercase letters 00128 modifyKey[index_b] = 0x02; 00129 key_press_scan_buff[index_b] = keyData-0x3d; 00130 index_b++; 00131 key_press_scan_buff[index_b] = 0x73; 00132 } else if (keyData == 0x20) { //space 00133 modifyKey[index_b] = 0x00; 00134 key_press_scan_buff[index_b] = 0x2c; 00135 index_b++; 00136 key_press_scan_buff[index_b] = 0x73; 00137 } else { 00138 modifyKey[index_b] = 0x00; 00139 //key_press_scan_buff[index_b] = 0x73; //this is dummy data. 00140 //msg[index_w+1] = '\0'; 00141 } 00142 index_b++; 00143 if(keyData == 0x0a && ble.getGapState().connected){ 00144 flip_lock = true; 00145 RFSWIO = 0;// RF Switch to BLE and lockup temporary 00146 for(int i = 0; i < index_b ; i++){ 00147 hidService.updateReport(modifyKey[i], key_press_scan_buff[i]); 00148 wait(0.03); 00149 } 00150 flip_lock = false; 00151 00152 flip_lock = true; 00153 RFSWIO = 1;// RF Switch to WiFi and lockup temporary 00154 socket.send(msg, sizeof(msg)); 00155 flip_lock = false; 00156 00157 index_b = 0; 00158 index_w = 0; 00159 memset(modifyKey, 0, 50); 00160 memset(msg, 0, 25); 00161 memset(key_press_scan_buff, 0, 50); 00162 } 00163 00164 } 00165 } 00166 }
Generated on Wed Jul 13 2022 20:02:04 by 1.7.2