updating demo
This commit is contained in:
1 parent
f661313702
commit
ffae8db151
8 files changed
+340
-242
No files matched your search
@@ -91,6 +91,7 @@
|
||||
|
||||
int cprintf(const char * format, ...);
|
||||
#define PRINT_LOCATION() cprintf("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__)
|
||||
#define PRINT_MESS(x) cprintf("%s: %d: " x " \n" , __PRETTY_FUNCTION__, __LINE__)
|
||||
#define PRTNT_HEAP() if (CFG_DEBUG == 3) cprintf("\n%s: %d: Heap free: %d\n", __PRETTY_FUNCTION__, __LINE__, util_heap_get_free_size())
|
||||
#define PRINT_INT(x) cprintf("%s: %d: " #x " = %ld\n" , __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
|
||||
#define PRINT_HEX(x) cprintf("%s: %d: " #x " = %08lx\n", __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
|
||||
|
||||
@@ -26,6 +26,8 @@ void setup()
|
||||
Serial.println("Bluefruit52 HID Keyboard Example");
|
||||
|
||||
Bluefruit.begin();
|
||||
// HID Device can have min connection interval up to 9*1.25 = 11.25 ms
|
||||
Bluefruit.setConnInterval(9, 16); // min = 11.25 ms, max = 20ms
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
|
||||
// Configure and Start Device Information Service
|
||||
|
||||
@@ -43,10 +43,13 @@
|
||||
#define BLE_CENTRAL_MAX_CONN 0
|
||||
#define BLE_CENTRAL_MAX_SECURE_CONN 0
|
||||
|
||||
#define CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS 3000
|
||||
#define CFG_GAP_CONNECTION_SLAVE_LATENCY 0
|
||||
#define CFG_GAP_ADV_INTERVAL_MS 20
|
||||
#define CFG_GAP_ADV_TIMEOUT_S 30
|
||||
#define GAP_CONN_SUPERVISION_TIMEOUT_MS 3000
|
||||
#define GAP_CONN_SLAVE_LATENCY 0
|
||||
#define GAP_CONN_MIN_INTERVAL_MS 20
|
||||
#define GAP_CONN_MAX_INTERVAL_MS 40
|
||||
|
||||
#define GAP_ADV_INTERVAL_MS 20
|
||||
#define GAP_ADV_TIMEOUT_S 30
|
||||
|
||||
#define CFG_BLE_TX_POWER_LEVEL 0
|
||||
#define CFG_DEFAULT_NAME "Bluefruit52"
|
||||
@@ -94,6 +97,7 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
_txbuf_sem = NULL;
|
||||
|
||||
_conn_hdl = BLE_GATT_HANDLE_INVALID;
|
||||
_bonded = false;
|
||||
|
||||
_chars_count = 0;
|
||||
for(uint8_t i=0; i<BLE_MAX_CHARS; i++) _chars_list[i] = NULL;
|
||||
@@ -143,13 +147,13 @@ err_t AdafruitBluefruit::begin(void)
|
||||
// Connection Parameters
|
||||
ble_gap_conn_params_t gap_conn_params =
|
||||
{
|
||||
.min_conn_interval = MS100TO125(20) , // in 1.25ms unit
|
||||
.max_conn_interval = MS100TO125(40) , // in 1.25ms unit
|
||||
.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
.min_conn_interval = MS100TO125(GAP_CONN_MIN_INTERVAL_MS) , // in 1.25ms unit
|
||||
.max_conn_interval = MS100TO125(GAP_CONN_MAX_INTERVAL_MS) , // in 1.25ms unit
|
||||
.slave_latency = GAP_CONN_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
};
|
||||
|
||||
(void) sd_ble_gap_ppcp_set(&gap_conn_params);
|
||||
VERIFY_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params) );
|
||||
|
||||
// Default device name
|
||||
ble_gap_conn_sec_mode_t sec_mode = BLE_SECMODE_OPEN;
|
||||
@@ -175,6 +179,25 @@ err_t AdafruitBluefruit::begin(void)
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
err_t AdafruitBluefruit::setConnInterval(uint16_t min, uint16_t max)
|
||||
{
|
||||
ble_gap_conn_params_t gap_conn_params =
|
||||
{
|
||||
.min_conn_interval = min, // in 1.25ms unit
|
||||
.max_conn_interval = max, // in 1.25ms unit
|
||||
.slave_latency = GAP_CONN_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
};
|
||||
|
||||
return sd_ble_gap_ppcp_set(&gap_conn_params);
|
||||
}
|
||||
|
||||
err_t AdafruitBluefruit::setConnIntervalMS(uint16_t min_ms, uint16_t max_ms)
|
||||
{
|
||||
return setConnInterval( MS100TO125(min_ms), MS100TO125(max_ms) );
|
||||
}
|
||||
|
||||
|
||||
void AdafruitBluefruit::setName(const char* str)
|
||||
{
|
||||
strncpy(_name, str, 32);
|
||||
@@ -388,8 +411,8 @@ err_t AdafruitBluefruit::startAdvertising(void)
|
||||
.p_peer_addr = NULL , // Undirected advertisement
|
||||
.fp = BLE_GAP_ADV_FP_ANY ,
|
||||
.p_whitelist = NULL ,
|
||||
.interval = MS1000TO625(CFG_GAP_ADV_INTERVAL_MS), // advertising interval (in units of 0.625 ms)
|
||||
.timeout = CFG_GAP_ADV_TIMEOUT_S
|
||||
.interval = MS1000TO625(GAP_ADV_INTERVAL_MS), // advertising interval (in units of 0.625 ms)
|
||||
.timeout = GAP_ADV_TIMEOUT_S
|
||||
};
|
||||
|
||||
VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para) );
|
||||
@@ -442,6 +465,14 @@ bool AdafruitBluefruit::txbuf_get(uint32_t ms)
|
||||
return xSemaphoreTake(_txbuf_sem, ms2tick(ms));
|
||||
}
|
||||
|
||||
err_t AdafruitBluefruit::saveAllCCCD(void)
|
||||
{
|
||||
uint16_t len=0;
|
||||
sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS);
|
||||
|
||||
PRINT_INT(len);
|
||||
}
|
||||
|
||||
void adafruit_bluefruit_task(void* arg)
|
||||
{
|
||||
(void) arg;
|
||||
@@ -520,6 +551,8 @@ void AdafruitBluefruit::_poll(void)
|
||||
switch ( evt->header.evt_id )
|
||||
{
|
||||
case BLE_GAP_EVT_CONNECTED:
|
||||
PRINT_MESS("Connected");
|
||||
|
||||
if (_led_conn) digitalWrite(LED_CONN, HIGH);
|
||||
|
||||
_conn_hdl = evt->evt.gap_evt.conn_handle;
|
||||
@@ -530,9 +563,13 @@ void AdafruitBluefruit::_poll(void)
|
||||
_txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
|
||||
if ( _connect_cb ) _connect_cb(0);
|
||||
|
||||
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
PRINT_MESS("Disconnected");
|
||||
|
||||
if (_led_conn) digitalWrite(LED_CONN, LOW);
|
||||
|
||||
_conn_hdl = BLE_GATT_HANDLE_INVALID;
|
||||
@@ -560,16 +597,27 @@ void AdafruitBluefruit::_poll(void)
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONN_PARAM_UPDATE:
|
||||
{
|
||||
ble_gap_conn_params_t* param = &evt->evt.gap_evt.params.conn_param_update.conn_params;
|
||||
PRINT_INT(param->min_conn_interval);
|
||||
PRINT_INT(param->max_conn_interval);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_INFO_REQUEST:
|
||||
{
|
||||
PRINT_MESS("BLE_GAP_EVT_SEC_INFO_REQUEST");
|
||||
// If bonded previously, return information. Otherwise NULL
|
||||
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
|
||||
|
||||
if (_enc_key.master_id.ediv == sec_request->master_id.ediv)
|
||||
{
|
||||
PRINT_LOCATION();
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_enc_key.enc_info, &_peer_id.id_info, NULL);
|
||||
} else
|
||||
{
|
||||
PRINT_LOCATION();
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
|
||||
}
|
||||
}
|
||||
@@ -577,10 +625,14 @@ void AdafruitBluefruit::_poll(void)
|
||||
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
{
|
||||
PRINT_LOCATION();
|
||||
ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
|
||||
PRINT_INT(peer->bond);
|
||||
PRINT_INT(peer->mitm);
|
||||
PRINT_INT(peer->lesc);
|
||||
|
||||
ble_gap_sec_params_t sec_para =
|
||||
{
|
||||
.bond = 1 ,
|
||||
.bond = 1,
|
||||
.mitm = 0, //CFG_PIN_ENABLED ? nvm_data.core.passkey_enable : 0,
|
||||
.lesc = 0,
|
||||
.keypress = 0,
|
||||
@@ -607,33 +659,59 @@ void AdafruitBluefruit::_poll(void)
|
||||
}
|
||||
};
|
||||
|
||||
VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_para, &keyset),
|
||||
RETURN_VOID);
|
||||
VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &sec_para, &keyset), RETURN_VOID);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_PASSKEY_DISPLAY:
|
||||
PRINT_LOCATION();
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
|
||||
PRINT_LOCATION();
|
||||
break;
|
||||
|
||||
|
||||
case BLE_GAP_EVT_CONN_SEC_UPDATE:
|
||||
{
|
||||
ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
|
||||
PRINT_LOCATION();
|
||||
(void) conn_sec;
|
||||
PRINT_INT(conn_sec->sec_mode.sm);
|
||||
PRINT_INT(conn_sec->sec_mode.lv);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_AUTH_STATUS:
|
||||
PRINT_LOCATION();
|
||||
|
||||
// Bonding succeeded --> save encryption keys
|
||||
PRINT_HEX(evt->evt.gap_evt.params.auth_status.auth_status);
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == evt->evt.gap_evt.params.auth_status.auth_status)
|
||||
{
|
||||
|
||||
_bonded = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
|
||||
PRINT_LOCATION();
|
||||
PRINT_MESS("BLE_GATTS_EVT_SYS_ATTR_MISSING");
|
||||
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
|
||||
break;
|
||||
|
||||
// Save CCCD when enabled/disabled if bonded
|
||||
case BLE_GATTS_EVT_WRITE:
|
||||
if ( _bonded )
|
||||
{
|
||||
ble_gatts_evt_write_t * write = &evt->evt.gatts_evt.params.write;
|
||||
|
||||
for(int i=0; i<_chars_count; i++)
|
||||
{
|
||||
if (_chars_list[i]->handles().cccd_handle == write->handle )
|
||||
{
|
||||
saveAllCCCD();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ class AdafruitBluefruit
|
||||
void setName(const char* str);
|
||||
// bool setTxPower(int8_t power);
|
||||
|
||||
// Advertising Packet
|
||||
/*------------- Advertising -------------*/
|
||||
uint8_t getAdvLen(void);
|
||||
uint8_t getAdvData(uint8_t* buffer);
|
||||
bool setAdvData(const uint8_t* data, uint8_t count);
|
||||
@@ -105,13 +105,17 @@ class AdafruitBluefruit
|
||||
err_t addService(uint16_t uuid16);
|
||||
err_t addService(uint8_t const uuid128[]);
|
||||
|
||||
void disconnect(void);
|
||||
bool connected(void);
|
||||
|
||||
err_t setConnInterval(uint16_t min, uint16_t max);
|
||||
err_t setConnIntervalMS(uint16_t min_ms, uint16_t max_ms);
|
||||
|
||||
uint16_t connHandle(void);
|
||||
ble_gap_addr_t peerAddr(void);
|
||||
|
||||
bool txbuf_get(uint32_t ms);
|
||||
|
||||
void disconnect(void);
|
||||
bool connected(void);
|
||||
err_t saveAllCCCD(void);
|
||||
|
||||
/*------------- Callback -------------*/
|
||||
void setConnectCallback ( void (*fp) (uint32_t hostID) );
|
||||
@@ -141,6 +145,7 @@ class AdafruitBluefruit
|
||||
uint8_t _chars_count;
|
||||
|
||||
uint16_t _conn_hdl;
|
||||
bool _bonded;
|
||||
|
||||
// TODO move to bonding place
|
||||
ble_gap_enc_key_t _enc_key;
|
||||
@@ -148,6 +153,9 @@ class AdafruitBluefruit
|
||||
|
||||
ble_gap_addr_t _peer_addr;
|
||||
|
||||
uint8_t* _sys_attr;
|
||||
// uint16_t
|
||||
|
||||
// Transmission Buffer Count for HVX notification, max is seen at 7
|
||||
SemaphoreHandle_t _txbuf_sem;
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ uint8_t const hid_report_descriptor[] =
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ),
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_KEYBOARD ),
|
||||
HID_COLLECTION ( HID_COLLECTION_APPLICATION ),
|
||||
// HID_REPORT_ID ( REPORT_ID_KEYBOARD + 0 ),
|
||||
HID_REPORT_ID ( REPORT_ID_KEYBOARD + 0 ),
|
||||
HID_USAGE_PAGE( HID_USAGE_PAGE_KEYBOARD ),
|
||||
// 8 bits Modifier Keys (Shfit, Control, Alt)
|
||||
HID_USAGE_MIN ( 224 ),
|
||||
@@ -81,6 +81,17 @@ uint8_t const hid_report_descriptor[] =
|
||||
HID_REPORT_SIZE ( 8 ),
|
||||
HID_INPUT ( HID_CONSTANT ),
|
||||
|
||||
// 6-byte Keycodes
|
||||
HID_USAGE_PAGE (HID_USAGE_PAGE_KEYBOARD),
|
||||
HID_USAGE_MIN ( 0 ),
|
||||
HID_USAGE_MAX ( 101 ),
|
||||
HID_LOGICAL_MIN ( 0 ),
|
||||
HID_LOGICAL_MAX ( 101 ),
|
||||
|
||||
HID_REPORT_COUNT ( 6 ),
|
||||
HID_REPORT_SIZE ( 8 ),
|
||||
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ),
|
||||
|
||||
// LED Indicator Kana | Compose | Scroll Lock | CapsLock | NumLock
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_LED ),
|
||||
/* 5-bit Led report */
|
||||
@@ -93,20 +104,8 @@ uint8_t const hid_report_descriptor[] =
|
||||
HID_REPORT_COUNT ( 1 ),
|
||||
HID_REPORT_SIZE ( 3 ),
|
||||
HID_OUTPUT ( HID_CONSTANT ),
|
||||
|
||||
// 6-byte Keycodes
|
||||
HID_USAGE_PAGE (HID_USAGE_PAGE_KEYBOARD),
|
||||
HID_USAGE_MIN ( 0 ),
|
||||
HID_USAGE_MAX ( 101 ),
|
||||
HID_LOGICAL_MIN ( 0 ),
|
||||
HID_LOGICAL_MAX ( 101 ),
|
||||
|
||||
HID_REPORT_COUNT ( 6 ),
|
||||
HID_REPORT_SIZE ( 8 ),
|
||||
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ),
|
||||
HID_COLLECTION_END,
|
||||
|
||||
#if 0
|
||||
//------------- Consumer Control Report -------------//
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ),
|
||||
HID_USAGE ( HID_USAGE_CONSUMER_CONTROL ),
|
||||
@@ -121,6 +120,7 @@ uint8_t const hid_report_descriptor[] =
|
||||
HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ),
|
||||
HID_COLLECTION_END,
|
||||
|
||||
#if 0
|
||||
//------------- Mouse Report: buttons + dx + dy + scroll + pan -------------//
|
||||
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ),
|
||||
HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ),
|
||||
@@ -213,15 +213,14 @@ uint8_t const hid_report_descriptor[] =
|
||||
};
|
||||
|
||||
BLEHidAdafruit::BLEHidAdafruit(void)
|
||||
: BLEHidGeneric(1, 1, 0)
|
||||
: BLEHidGeneric(2, 1, 0)
|
||||
{
|
||||
_kbd_leds = 0;
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::start(void)
|
||||
{
|
||||
uint16_t input_len[1] = { sizeof(hid_keyboard_report_t) };
|
||||
uint16_t output_len[1] = { sizeof(_kbd_leds) } ;
|
||||
uint16_t input_len[] = { sizeof(hid_keyboard_report_t), 2 };
|
||||
uint16_t output_len[] = { 1 };
|
||||
|
||||
setReportLen(input_len, output_len, NULL);
|
||||
|
||||
@@ -247,6 +246,18 @@ err_t BLEHidAdafruit::keyboardReport(uint8_t modifier, uint8_t keycode[6])
|
||||
return keyboardReport(&report);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::keyHIDCode(uint8_t modifier, uint8_t keycode0, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5)
|
||||
{
|
||||
hid_keyboard_report_t report =
|
||||
{
|
||||
.modifier = modifier,
|
||||
.reserved = 0,
|
||||
.keycode = { keycode0, keycode1, keycode2, keycode3, keycode4, keycode5 }
|
||||
};
|
||||
|
||||
return keyboardReport(&report);
|
||||
}
|
||||
|
||||
err_t BLEHidAdafruit::keyPress(char ch)
|
||||
{
|
||||
hid_keyboard_report_t report;
|
||||
|
||||
@@ -47,7 +47,6 @@
|
||||
class BLEHidAdafruit : public BLEHidGeneric
|
||||
{
|
||||
protected:
|
||||
uint8_t _kbd_leds;
|
||||
|
||||
public:
|
||||
BLEHidAdafruit(void);
|
||||
@@ -58,6 +57,8 @@ class BLEHidAdafruit : public BLEHidGeneric
|
||||
err_t keyboardReport(hid_keyboard_report_t* report);
|
||||
err_t keyboardReport(uint8_t modifier, uint8_t keycode[6]);
|
||||
|
||||
err_t keyHIDCode(uint8_t modifier, uint8_t keycode0, uint8_t keycode1=0, uint8_t keycode2=0, uint8_t keycode3=0, uint8_t keycode4=0, uint8_t keycode5=0);
|
||||
|
||||
err_t keyPress(char ch);
|
||||
err_t keyRelease(void);
|
||||
err_t keySequence(const char* str, int interal=5);
|
||||
|
||||
@@ -42,6 +42,201 @@ enum {
|
||||
REPORT_TYPE_FEATURE
|
||||
};
|
||||
|
||||
BLEHidGeneric::BLEHidGeneric(uint8_t num_input, uint8_t num_output, uint8_t num_feature)
|
||||
: BLEService(UUID16_SVC_HUMAN_INTERFACE_DEVICE), _chr_control(UUID16_CHR_HID_CONTROL_POINT)
|
||||
{
|
||||
_boot_keyboard = _boot_mouse = false;
|
||||
_report_map = NULL;
|
||||
_report_map_len = 0;
|
||||
|
||||
_input_len = _output_len = _feature_len = NULL;
|
||||
|
||||
_num_input = num_input;
|
||||
_num_output = num_output;
|
||||
_num_feature = num_feature;
|
||||
|
||||
// HID Information
|
||||
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.hid_information.xml
|
||||
// bcd 1.1, country = 0, flag = normal connect
|
||||
_hid_info[0] = 0x01;
|
||||
_hid_info[1] = 0x01;
|
||||
_hid_info[2] = 0x00;
|
||||
_hid_info[3] = bit(1);
|
||||
|
||||
_chr_protocol = NULL;
|
||||
_chr_inputs = _chr_outputs = _chr_features = NULL;
|
||||
_chr_boot_keyboard_input = _chr_boot_keyboard_output = _chr_boot_mouse_input = NULL;
|
||||
|
||||
_output_cbs = NULL;
|
||||
|
||||
if ( _num_input )
|
||||
{
|
||||
_chr_inputs = new BLECharacteristic[_num_input];
|
||||
}
|
||||
|
||||
if ( _num_output )
|
||||
{
|
||||
_chr_outputs = new BLECharacteristic[_num_output];
|
||||
_output_cbs = new output_report_cb_t[_num_output];
|
||||
|
||||
for (uint8_t i=0; i<_num_output; i++) _output_cbs[i] = NULL;
|
||||
}
|
||||
|
||||
if ( _num_feature )
|
||||
{
|
||||
_chr_features = new BLECharacteristic[_num_feature];
|
||||
}
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setBootProtocol(bool bootKeyboard, bool bootMouse)
|
||||
{
|
||||
_boot_keyboard = bootKeyboard;
|
||||
_boot_mouse = bootMouse;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setHidInfo(uint16_t bcd, uint8_t country, uint8_t flags)
|
||||
{
|
||||
memcpy(_hid_info, &bcd, 2);
|
||||
_hid_info[2] = country;
|
||||
_hid_info[3] = flags;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setReportMap(const uint8_t* report_map, size_t len)
|
||||
{
|
||||
_report_map = report_map;
|
||||
_report_map_len = len;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setReportLen(uint16_t input_len[], uint16_t output_len[], uint16_t feature_len[])
|
||||
{
|
||||
_input_len = input_len;
|
||||
_output_len = output_len;
|
||||
_feature_len = feature_len;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setOutputReportCallback(uint8_t reportID, output_report_cb_t fp)
|
||||
{
|
||||
_output_cbs[reportID] = fp;
|
||||
}
|
||||
|
||||
void blehidgeneric_output_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request)
|
||||
{
|
||||
BLEHidGeneric& hid = (BLEHidGeneric&) chr.parentService();
|
||||
PRINT_BUFFER(request->data, request->len);
|
||||
}
|
||||
|
||||
err_t BLEHidGeneric::start(void)
|
||||
{
|
||||
VERIFY ( (_report_map != NULL) && _report_map_len, NRF_ERROR_INVALID_PARAM);
|
||||
|
||||
VERIFY_STATUS( this->addToGatt() );
|
||||
|
||||
// Protocol Mode
|
||||
if ( _boot_keyboard || _boot_mouse )
|
||||
{
|
||||
_chr_protocol = new BLECharacteristic(UUID16_CHR_PROTOCOL_MODE);
|
||||
VERIFY(_chr_protocol, NRF_ERROR_NO_MEM);
|
||||
|
||||
_chr_protocol->setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_protocol->setFixedLen(1);
|
||||
VERIFY_STATUS( _chr_protocol->start() );
|
||||
_chr_protocol->write( (uint8_t) 1);
|
||||
}
|
||||
|
||||
// Input reports
|
||||
for(uint8_t i=0; i<_num_input; i++)
|
||||
{
|
||||
_chr_inputs[i].setUuid(UUID16_CHR_REPORT);
|
||||
_chr_inputs[i].setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
|
||||
_chr_inputs[i].setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
_chr_inputs[i].setReportRefDescriptor(i+0, REPORT_TYPE_INPUT);
|
||||
|
||||
// Input report len is configured, else variable len up to 255
|
||||
if ( _input_len ) _chr_inputs[i].setFixedLen( _input_len[i] );
|
||||
|
||||
VERIFY_STATUS( _chr_inputs[i].start() );
|
||||
}
|
||||
|
||||
// Output reports
|
||||
for(uint8_t i=0; i<_num_output; i++)
|
||||
{
|
||||
_chr_outputs[i].setUuid(UUID16_CHR_REPORT);
|
||||
_chr_outputs[i].setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_outputs[i].setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
_chr_outputs[i].setReportRefDescriptor(i+0, REPORT_TYPE_OUTPUT);
|
||||
// _chr_outputs[i].setWriteCallback(blehidgeneric_output_cb);
|
||||
|
||||
// Input report len is configured, else variable len up to 255
|
||||
if ( _output_len ) _chr_outputs[i].setFixedLen( _output_len[i] );
|
||||
|
||||
VERIFY_STATUS( _chr_outputs[i].start() );
|
||||
|
||||
_chr_outputs[i].write( (uint8_t) 0 );
|
||||
}
|
||||
|
||||
// Report Map (HID Report Descriptor)
|
||||
BLECharacteristic report_map(UUID16_CHR_REPORT_MAP);
|
||||
report_map.setTempMemory();
|
||||
report_map.setProperties(CHR_PROPS_READ);
|
||||
report_map.setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
report_map.setFixedLen(_report_map_len);
|
||||
VERIFY_STATUS( report_map.start() );
|
||||
report_map.write(_report_map, _report_map_len);
|
||||
|
||||
// Boot Keyboard Input Report
|
||||
if ( _boot_keyboard )
|
||||
{
|
||||
_chr_boot_keyboard_input = new BLECharacteristic(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT);
|
||||
_chr_boot_keyboard_input->setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
|
||||
_chr_boot_keyboard_input->setFixedLen(8); // boot keyboard is 8 bytes
|
||||
_chr_boot_keyboard_input->setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
VERIFY_STATUS(_chr_boot_keyboard_input->start());
|
||||
|
||||
_chr_boot_keyboard_output = new BLECharacteristic(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT);
|
||||
_chr_boot_keyboard_output->setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_boot_keyboard_output->setFixedLen(1); // boot keyboard is 1 byte
|
||||
_chr_boot_keyboard_output->setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
VERIFY_STATUS(_chr_boot_keyboard_output->start());
|
||||
_chr_boot_keyboard_output->write((uint8_t) 0);
|
||||
}
|
||||
|
||||
// Boot Mouse Input Report
|
||||
if ( _boot_mouse )
|
||||
{
|
||||
_chr_boot_mouse_input = new BLECharacteristic(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT);
|
||||
_chr_boot_mouse_input->setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
|
||||
_chr_boot_mouse_input->setFixedLen(sizeof(hid_mouse_report_t));
|
||||
_chr_boot_mouse_input->setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
VERIFY_STATUS(_chr_boot_mouse_input->start());
|
||||
}
|
||||
|
||||
// HID Info
|
||||
BLECharacteristic hid_info(UUID16_CHR_HID_INFORMATION);
|
||||
hid_info.setTempMemory();
|
||||
hid_info.setProperties(CHR_PROPS_READ);
|
||||
hid_info.setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
hid_info.setFixedLen(sizeof(_hid_info));
|
||||
VERIFY_STATUS( hid_info.start() );
|
||||
hid_info.write(_hid_info, sizeof(_hid_info));
|
||||
|
||||
// HID Control Point
|
||||
_chr_control.setProperties(CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_control.setPermission(SECMODE_NO_ACCESS, SECMODE_ENC_NO_MITM);
|
||||
_chr_control.setFixedLen(1);
|
||||
VERIFY_STATUS( _chr_control.start() );
|
||||
_chr_control.write( (uint8_t) 0 );
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
err_t BLEHidGeneric::inputReport(uint8_t reportID, void const* data, int len)
|
||||
{
|
||||
return _chr_inputs[reportID].notify( (uint8_t const*) data, len);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128] =
|
||||
{
|
||||
{0, 0 }, // 0x00 Null
|
||||
@@ -177,201 +372,3 @@ const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128] =
|
||||
{0, HID_KEY_DELETE } // 0x7F Delete
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
BLEHidGeneric::BLEHidGeneric(uint8_t num_input, uint8_t num_output, uint8_t num_feature)
|
||||
: BLEService(UUID16_SVC_HUMAN_INTERFACE_DEVICE), _chr_control(UUID16_CHR_HID_CONTROL_POINT)
|
||||
{
|
||||
_boot_keyboard = _boot_mouse = false;
|
||||
_report_map = NULL;
|
||||
_report_map_len = 0;
|
||||
|
||||
_input_len = _output_len = _feature_len = NULL;
|
||||
|
||||
_num_input = num_input;
|
||||
_num_output = num_output;
|
||||
_num_feature = num_feature;
|
||||
|
||||
// HID Information
|
||||
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.hid_information.xml
|
||||
// bcd 1.1, country = 0, flag = normal connect
|
||||
_hid_info[0] = 0x01;
|
||||
_hid_info[1] = 0x01;
|
||||
_hid_info[2] = 0x00;
|
||||
_hid_info[3] = bit(1);
|
||||
|
||||
_chr_protocol = NULL;
|
||||
_chr_inputs = _chr_outputs = _chr_features = NULL;
|
||||
_chr_boot_keyboard_input = _chr_boot_keyboard_output = _chr_boot_mouse_input = NULL;
|
||||
|
||||
_output_cbs = NULL;
|
||||
|
||||
if ( _num_input )
|
||||
{
|
||||
_chr_inputs = new BLECharacteristic[_num_input];
|
||||
}
|
||||
|
||||
if ( _num_output )
|
||||
{
|
||||
_chr_outputs = new BLECharacteristic[_num_output];
|
||||
_output_cbs = new output_report_cb_t[_num_output];
|
||||
|
||||
for (uint8_t i=0; i<_num_output; i++) _output_cbs[i] = NULL;
|
||||
}
|
||||
|
||||
if ( _num_feature )
|
||||
{
|
||||
_chr_features = new BLECharacteristic[_num_feature];
|
||||
}
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setBootProtocol(bool bootKeyboard, bool bootMouse)
|
||||
{
|
||||
_boot_keyboard = bootKeyboard;
|
||||
_boot_mouse = bootMouse;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setHidInfo(uint16_t bcd, uint8_t country, uint8_t flags)
|
||||
{
|
||||
memcpy(_hid_info, &bcd, 2);
|
||||
_hid_info[2] = country;
|
||||
_hid_info[3] = flags;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setReportMap(const uint8_t* report_map, size_t len)
|
||||
{
|
||||
_report_map = report_map;
|
||||
_report_map_len = len;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setReportLen(uint16_t input_len[], uint16_t output_len[], uint16_t feature_len[])
|
||||
{
|
||||
_input_len = input_len;
|
||||
_output_len = output_len;
|
||||
_feature_len = feature_len;
|
||||
}
|
||||
|
||||
void BLEHidGeneric::setOutputReportCallback(uint8_t reportID, output_report_cb_t fp)
|
||||
{
|
||||
_output_cbs[reportID] = fp;
|
||||
}
|
||||
|
||||
void blehidgeneric_output_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request)
|
||||
{
|
||||
BLEHidGeneric& hid = (BLEHidGeneric&) chr.parentService();
|
||||
|
||||
PRINT_BUFFER(request->data, request->len);
|
||||
}
|
||||
|
||||
err_t BLEHidGeneric::start(void)
|
||||
{
|
||||
VERIFY ( (_report_map != NULL) && _report_map_len, NRF_ERROR_INVALID_PARAM);
|
||||
|
||||
VERIFY_STATUS( this->addToGatt() );
|
||||
|
||||
// Protocol Mode
|
||||
if ( _boot_keyboard || _boot_mouse )
|
||||
{
|
||||
_chr_protocol = new BLECharacteristic(UUID16_CHR_PROTOCOL_MODE);
|
||||
VERIFY(_chr_protocol, NRF_ERROR_NO_MEM);
|
||||
|
||||
_chr_protocol->setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_protocol->setFixedLen(1);
|
||||
VERIFY_STATUS( _chr_protocol->start() );
|
||||
_chr_protocol->write( (uint8_t) 1);
|
||||
}
|
||||
|
||||
// Input reports
|
||||
for(uint8_t i=0; i<_num_input; i++)
|
||||
{
|
||||
_chr_inputs[i].setUuid(UUID16_CHR_REPORT);
|
||||
_chr_inputs[i].setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
|
||||
_chr_inputs[i].setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
_chr_inputs[i].setReportRefDescriptor(i+0, REPORT_TYPE_INPUT);
|
||||
|
||||
// Input report len is configured, else variable len up to 255
|
||||
if ( _input_len ) _chr_inputs[i].setFixedLen( _input_len[i] );
|
||||
|
||||
VERIFY_STATUS( _chr_inputs[i].start() );
|
||||
}
|
||||
|
||||
// Output reports
|
||||
for(uint8_t i=0; i<_num_output; i++)
|
||||
{
|
||||
_chr_outputs[i].setUuid(UUID16_CHR_REPORT);
|
||||
_chr_outputs[i].setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_outputs[i].setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
_chr_outputs[i].setReportRefDescriptor(i+0, REPORT_TYPE_OUTPUT);
|
||||
_chr_outputs[i].setWriteCallback(blehidgeneric_output_cb);
|
||||
|
||||
// Input report len is configured, else variable len up to 255
|
||||
if ( _output_len ) _chr_outputs[i].setFixedLen( _output_len[i] );
|
||||
|
||||
VERIFY_STATUS( _chr_outputs[i].start() );
|
||||
|
||||
_chr_outputs[i].write( (uint8_t) 0 );
|
||||
}
|
||||
|
||||
// Report Map (HID Report Descriptor)
|
||||
BLECharacteristic report_map(UUID16_CHR_REPORT_MAP);
|
||||
report_map.setTempMemory();
|
||||
report_map.setProperties(CHR_PROPS_READ);
|
||||
report_map.setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
report_map.setFixedLen(_report_map_len);
|
||||
VERIFY_STATUS( report_map.start() );
|
||||
report_map.write(_report_map, _report_map_len);
|
||||
|
||||
// Boot Keyboard Input Report
|
||||
if ( _boot_keyboard )
|
||||
{
|
||||
_chr_boot_keyboard_input = new BLECharacteristic(UUID16_CHR_BOOT_KEYBOARD_INPUT_REPORT);
|
||||
_chr_boot_keyboard_input->setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
|
||||
_chr_boot_keyboard_input->setFixedLen(8); // boot keyboard is 8 bytes
|
||||
_chr_boot_keyboard_input->setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
VERIFY_STATUS(_chr_boot_keyboard_input->start());
|
||||
|
||||
_chr_boot_keyboard_output = new BLECharacteristic(UUID16_CHR_BOOT_KEYBOARD_OUTPUT_REPORT);
|
||||
_chr_boot_keyboard_output->setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_boot_keyboard_output->setFixedLen(1); // boot keyboard is 1 byte
|
||||
_chr_boot_keyboard_output->setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
VERIFY_STATUS(_chr_boot_keyboard_output->start());
|
||||
_chr_boot_keyboard_output->write((uint8_t) 0);
|
||||
}
|
||||
|
||||
// Boot Mouse Input Report
|
||||
if ( _boot_mouse )
|
||||
{
|
||||
_chr_boot_mouse_input = new BLECharacteristic(UUID16_CHR_BOOT_MOUSE_INPUT_REPORT);
|
||||
_chr_boot_mouse_input->setProperties(CHR_PROPS_READ | CHR_PROPS_NOTIFY);
|
||||
_chr_boot_mouse_input->setFixedLen(sizeof(hid_mouse_report_t));
|
||||
_chr_boot_mouse_input->setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
VERIFY_STATUS(_chr_boot_mouse_input->start());
|
||||
}
|
||||
|
||||
// HID Info
|
||||
BLECharacteristic hid_info(UUID16_CHR_HID_INFORMATION);
|
||||
hid_info.setTempMemory();
|
||||
hid_info.setProperties(CHR_PROPS_READ);
|
||||
hid_info.setPermission(SECMODE_ENC_NO_MITM, SECMODE_NO_ACCESS);
|
||||
hid_info.setFixedLen(sizeof(_hid_info));
|
||||
VERIFY_STATUS( hid_info.start() );
|
||||
hid_info.write(_hid_info, sizeof(_hid_info));
|
||||
|
||||
// HID Control Point
|
||||
_chr_control.setProperties(CHR_PROPS_WRITE_WO_RESP);
|
||||
_chr_control.setPermission(SECMODE_NO_ACCESS, SECMODE_ENC_NO_MITM);
|
||||
_chr_control.setFixedLen(1);
|
||||
VERIFY_STATUS( _chr_control.start() );
|
||||
_chr_control.write( (uint8_t) 0 );
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
err_t BLEHidGeneric::inputReport(uint8_t reportID, void const* data, int len)
|
||||
{
|
||||
return _chr_inputs[reportID].notify( (uint8_t const*) data, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -297,8 +297,8 @@ typedef enum {
|
||||
//------------- ITEM & TAG -------------//
|
||||
#define HID_REPORT_DATA_0(data)
|
||||
#define HID_REPORT_DATA_1(data) , (data)
|
||||
#define HID_REPORT_DATA_2(data) , U16_TO_U8S_LE(data)
|
||||
#define HID_REPORT_DATA_3(data) , U32_TO_U8S_LE(data)
|
||||
#define HID_REPORT_DATA_2(data) , U16_BYTES_LE(data)
|
||||
#define HID_REPORT_DATA_3(data) , U32_BYTES_LE(data)
|
||||
|
||||
#define HID_REPORT_ITEM(data, tag, type, size) \
|
||||
(((tag) << 4) | ((type) << 2) | (size)) HID_REPORT_DATA_##size(data)
|
||||
|
||||
Reference in new issue
Block a user