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
- Committer:
- Marcomissyou
- Date:
- 2015-07-16
- Revision:
- 1:51535675abf4
- Parent:
- 0:b0fc0661c081
- Child:
- 2:be04a220392e
File content as of revision 1:51535675abf4:
#include "mbed.h"
#include "BLE.h"
#include "BatteryService.h"
#include "DeviceInformationService.h"
#include "HIDService.h"
#include "WIFIDevice.h"
#include "EthernetInterface.h"
BLEDevice ble;
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;
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
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[50];
static uint8_t modifyKey[50];
char msg[25] = "NNN40 CONNECTED\n";
int main(void)
{ 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");
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 = 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);
/* Setup auxiliary service. */
DeviceInformationService deviceInfo(ble, "ARM", "CYNTEC", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
/* Setup advertising. */
ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
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");
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_b] = 0x00;
key_press_scan_buff[index_b] = 0x27;
index_b++;
key_press_scan_buff[index_b] = 0x73;
} else {
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_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_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_b] = 0x00;
//key_press_scan_buff[index_b] = 0x73; //this is dummy data.
//msg[index_w+1] = '\0';
}
index_b++;
if(keyData == 0x0a && ble.getGapState().connected){
for(int i = 0; i < index_b ; i++){
hidService.updateReport(modifyKey[i], key_press_scan_buff[i]);
wait(0.03);
}
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);
}
}
}
}