2020-09-14 20:32:07 +00:00
|
|
|
/* This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
Unless required by applicable law or agreed to in writing, this software is
|
|
|
|
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "esp_bt.h"
|
|
|
|
#include "esp_bt_defs.h"
|
|
|
|
#include "esp_gap_ble_api.h"
|
|
|
|
#include "esp_gatts_api.h"
|
|
|
|
#include "esp_gatt_defs.h"
|
|
|
|
#include "esp_bt_main.h"
|
|
|
|
#include "esp_bt_device.h"
|
|
|
|
|
|
|
|
#include "esp_hidh.h"
|
|
|
|
#include "esp_hid_gap.h"
|
|
|
|
|
|
|
|
#include "blue.h"
|
2020-12-21 22:06:33 +00:00
|
|
|
#include "gpio.h"
|
2020-12-16 20:00:00 +00:00
|
|
|
#include "led.h"
|
2020-09-14 20:32:07 +00:00
|
|
|
|
|
|
|
#define BTMOUSE_BUTTON1 (1 << 0)
|
|
|
|
#define BTMOUSE_BUTTON2 (1 << 1)
|
|
|
|
#define BTMOUSE_BUTTON3 (1 << 2)
|
2020-12-16 20:00:00 +00:00
|
|
|
#define BTMOUSE_BUTTONE 248
|
2020-09-14 20:32:07 +00:00
|
|
|
|
2020-12-18 10:08:52 +00:00
|
|
|
/* debug tag */
|
|
|
|
static const char *TAG = "blue";
|
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
/* private functions */
|
|
|
|
static esp_hid_raw_report_map_t *blue_hid_rm_get(esp_hidh_dev_t *dev);
|
|
|
|
static esp_hid_report_item_t blue_hid_ri_find(esp_hidh_dev_t *d, esp_hid_usage_t u, uint8_t t, uint8_t p);
|
|
|
|
|
2020-09-14 20:32:07 +00:00
|
|
|
void hidh_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data)
|
|
|
|
{
|
|
|
|
esp_hidh_event_t event = (esp_hidh_event_t)id;
|
|
|
|
esp_hidh_event_data_t *param = (esp_hidh_event_data_t *)event_data;
|
|
|
|
|
2020-12-18 10:08:52 +00:00
|
|
|
const uint8_t *bda = NULL;
|
|
|
|
|
2020-09-14 20:32:07 +00:00
|
|
|
/*
|
2020-12-30 18:51:03 +00:00
|
|
|
* esp_hidh_event_data_t = union
|
2020-12-18 10:08:52 +00:00
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* esp_hidh_dev_t *dev; // HID Remote bluetooth device
|
|
|
|
* } open;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* esp_hidh_dev_t *dev; // HID Remote bluetooth device
|
|
|
|
* int reason; // Reason why the connection was closed. BLE Only
|
|
|
|
* } close;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* esp_hidh_dev_t *dev; // HID Remote bluetooth device
|
|
|
|
* uint8_t level; // Battery Level (0-100%)
|
|
|
|
* } battery;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* esp_hidh_dev_t *dev; // HID Remote bluetooth device
|
|
|
|
* esp_hid_usage_t usage; // HID report usage
|
|
|
|
* uint16_t report_id; // HID report index
|
|
|
|
* uint16_t length; // HID data length
|
|
|
|
* uint8_t *data; // The pointer to the HID data
|
|
|
|
* uint8_t map_index; // HID report map index
|
|
|
|
* } input;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* esp_hidh_dev_t *dev; // HID Remote bluetooth device
|
|
|
|
* esp_hid_usage_t usage; // HID report usage
|
|
|
|
* uint16_t report_id; // HID report index
|
|
|
|
* uint16_t length; // HID data length
|
|
|
|
* uint8_t *data; // The pointer to the HID data
|
|
|
|
* uint8_t map_index; // HID report map index
|
|
|
|
* } feature;
|
|
|
|
*/
|
2020-09-14 20:32:07 +00:00
|
|
|
|
|
|
|
char click;
|
|
|
|
short x, y;
|
|
|
|
|
|
|
|
switch (event) {
|
2020-12-18 10:08:52 +00:00
|
|
|
case ESP_HIDH_OPEN_EVENT: {
|
2020-12-21 22:06:33 +00:00
|
|
|
blue_open(param);
|
2020-09-14 20:32:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDH_BATTERY_EVENT: {
|
2020-12-18 10:08:52 +00:00
|
|
|
bda = esp_hidh_dev_bda_get(param->battery.dev);
|
|
|
|
ESP_LOGI(TAG, ESP_BD_ADDR_STR " battery: %d%%", ESP_BD_ADDR_HEX(bda), param->battery.level);
|
2020-09-14 20:32:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDH_INPUT_EVENT: {
|
2020-12-18 10:08:52 +00:00
|
|
|
bda = esp_hidh_dev_bda_get(param->input.dev);
|
2020-09-14 20:32:07 +00:00
|
|
|
ESP_LOGD(TAG, ESP_BD_ADDR_STR " INPUT: %8s, MAP: %2u, ID: %3u, Len: %d, Data:", ESP_BD_ADDR_HEX(bda), esp_hid_usage_str(param->input.usage), param->input.map_index, param->input.report_id, param->input.length);
|
|
|
|
ESP_LOG_BUFFER_HEX(TAG, param->input.data, param->input.length);
|
2020-12-16 20:00:00 +00:00
|
|
|
xTaskNotify(t_yellow, LED_ONCE, eSetValueWithOverwrite);
|
2020-09-14 20:32:07 +00:00
|
|
|
memcpy (&click, param->input.data, sizeof(uint8_t));
|
|
|
|
click = click & (BTMOUSE_BUTTON1 | BTMOUSE_BUTTON2 | BTMOUSE_BUTTON3);
|
|
|
|
if (click)
|
|
|
|
ESP_LOGI(TAG, "CLICK: %d", click);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDH_FEATURE_EVENT: {
|
2020-12-18 10:08:52 +00:00
|
|
|
bda = esp_hidh_dev_bda_get(param->feature.dev);
|
2020-09-14 20:32:07 +00:00
|
|
|
ESP_LOGI(TAG, ESP_BD_ADDR_STR " FEATURE: %8s, MAP: %2u, ID: %3u, Len: %d", ESP_BD_ADDR_HEX(bda), esp_hid_usage_str(param->feature.usage), param->feature.map_index, param->feature.report_id, param->feature.length);
|
|
|
|
ESP_LOG_BUFFER_HEX(TAG, param->feature.data, param->feature.length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDH_CLOSE_EVENT: {
|
2020-12-30 18:51:03 +00:00
|
|
|
blue_close(param);
|
2020-09-14 20:32:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2020-12-18 10:08:52 +00:00
|
|
|
ESP_LOGI(TAG, "Unknwown event: %d", event);
|
2020-09-14 20:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void blue_init(void)
|
|
|
|
{
|
|
|
|
esp_err_t ret;
|
|
|
|
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
2020-12-18 10:08:52 +00:00
|
|
|
|
2021-04-18 11:54:31 +00:00
|
|
|
ESP_LOGD(TAG, "Starting Bluetooth init");
|
2020-09-14 20:32:07 +00:00
|
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
ESP_ERROR_CHECK(esp_hid_gap_init(ESP_BT_MODE_BTDM));
|
|
|
|
ESP_ERROR_CHECK(esp_ble_gattc_register_callback(esp_hidh_gattc_event_handler));
|
|
|
|
|
|
|
|
esp_hidh_config_t config = {
|
|
|
|
.callback = hidh_callback,
|
2021-04-18 11:54:31 +00:00
|
|
|
.event_stack_size = 4096
|
2020-09-14 20:32:07 +00:00
|
|
|
};
|
|
|
|
|
2020-12-16 20:00:00 +00:00
|
|
|
ESP_ERROR_CHECK(esp_hidh_init(&config));
|
2021-04-20 22:33:42 +00:00
|
|
|
esp_log_level_set("event", ESP_LOG_INFO);
|
2020-09-14 20:32:07 +00:00
|
|
|
|
2020-12-18 10:08:52 +00:00
|
|
|
/*
|
|
|
|
* at this point, everything but bluetooth is started.
|
2020-12-16 20:00:00 +00:00
|
|
|
* put green steady, start blinking blue and keep scanning until a device is found
|
|
|
|
*/
|
2021-04-20 22:33:42 +00:00
|
|
|
|
2020-12-16 20:00:00 +00:00
|
|
|
xTaskNotify(t_green, LED_ON, eSetValueWithOverwrite);
|
2021-04-20 22:33:42 +00:00
|
|
|
xTaskNotify(t_blue, LED_FAST, eSetValueWithOverwrite);
|
2021-04-18 11:54:31 +00:00
|
|
|
xTaskCreatePinnedToCore(blue_scan, "blue_scan", 6 * 1024, NULL, 2, NULL, 0);
|
2020-12-30 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void blue_close(esp_hidh_event_data_t *p) {
|
|
|
|
const uint8_t *bda = NULL;
|
|
|
|
|
|
|
|
configASSERT(p != NULL);
|
|
|
|
bda = esp_hidh_dev_bda_get(p->close.dev);
|
2021-04-20 22:33:42 +00:00
|
|
|
ESP_LOGI(TAG, "closed connection with device " ESP_BD_ADDR_STR, ESP_BD_ADDR_HEX(bda));
|
2020-12-30 18:51:03 +00:00
|
|
|
esp_hidh_dev_free(p->close.dev);
|
2021-04-20 22:33:42 +00:00
|
|
|
|
2020-12-30 18:51:03 +00:00
|
|
|
xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite);
|
2020-12-18 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 22:06:33 +00:00
|
|
|
void blue_open(esp_hidh_event_data_t *p) {
|
|
|
|
const uint8_t *bda = NULL;
|
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
esp_hid_report_item_t mir; /* MOUSE INPUT REPORT */
|
|
|
|
|
2020-12-30 18:51:03 +00:00
|
|
|
configASSERT(p != NULL);
|
2020-12-21 22:06:33 +00:00
|
|
|
bda = esp_hidh_dev_bda_get(p->open.dev);
|
|
|
|
ESP_LOGI(TAG, "opened connection with " ESP_BD_ADDR_STR, ESP_BD_ADDR_HEX(bda));
|
2020-12-30 18:51:03 +00:00
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
/* search for MIR section */
|
|
|
|
esp_hid_raw_report_map_t *maps;
|
|
|
|
maps = blue_hid_rm_get(p->open.dev);
|
|
|
|
mir = blue_hid_ri_find(p->open.dev, ESP_HID_USAGE_MOUSE, ESP_HID_REPORT_TYPE_INPUT, ESP_HID_PROTOCOL_MODE_REPORT);
|
2020-12-30 18:51:03 +00:00
|
|
|
|
2020-12-21 22:06:33 +00:00
|
|
|
xTaskNotify(t_blue, LED_ON, eSetValueWithOverwrite);
|
|
|
|
gpio_output_enable();
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
/* get specific report from report map matching specified usage + type + protocol */
|
|
|
|
static esp_hid_report_item_t blue_hid_ri_find(esp_hidh_dev_t *d, esp_hid_usage_t u, uint8_t t, uint8_t p) {
|
|
|
|
size_t num_reports = 0;
|
|
|
|
esp_hid_report_item_t *reports;
|
|
|
|
esp_hid_report_item_t search;
|
|
|
|
|
|
|
|
configASSERT(d != NULL);
|
|
|
|
configASSERT(p <= ESP_HID_REPORT_TYPE_FEATURE);
|
|
|
|
configASSERT(t > 0);
|
|
|
|
|
|
|
|
esp_hidh_dev_reports_get(d, &num_reports, &reports);
|
|
|
|
memset(&search, 0, sizeof(esp_hid_report_item_t));
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < num_reports; i++) {
|
|
|
|
if (reports[i].protocol_mode == p && reports[i].report_type == t && reports[i].usage == u) {
|
|
|
|
memcpy(&search, &reports[i], sizeof(esp_hid_report_item_t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(reports);
|
|
|
|
if (search.value_len == 0) {
|
|
|
|
ESP_LOGW(TAG, "%s %s %s not found",
|
|
|
|
esp_hid_usage_str(u),
|
|
|
|
esp_hid_report_type_str(t),
|
|
|
|
esp_hid_protocol_mode_str(p));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ESP_LOGD(TAG, "%s %s %s is id %i size %i byte(s)",
|
|
|
|
esp_hid_usage_str(u),
|
|
|
|
esp_hid_report_type_str(t),
|
|
|
|
esp_hid_protocol_mode_str(p),
|
|
|
|
search.report_id,
|
|
|
|
search.value_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return search;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get report map raw pointer. Also dump it on JTAG */
|
|
|
|
static esp_hid_raw_report_map_t *blue_hid_rm_get(esp_hidh_dev_t *dev) {
|
|
|
|
size_t num_maps = 0;
|
|
|
|
esp_hid_raw_report_map_t *maps;
|
|
|
|
|
|
|
|
configASSERT(dev != NULL);
|
|
|
|
ESP_LOGI(TAG, BLUE_SNIP);
|
|
|
|
esp_hidh_dev_report_maps_get(dev, &num_maps, &maps);
|
|
|
|
ESP_LOG_BUFFER_HEX(TAG, maps[0].data, maps[0].len);
|
|
|
|
ESP_LOGI(TAG, BLUE_SNIP);
|
|
|
|
|
|
|
|
/* looking at 4.2 SDK source, seems there is always only one map */
|
|
|
|
configASSERT(num_maps == 1);
|
|
|
|
return maps;
|
|
|
|
}
|
|
|
|
|
2020-12-21 22:06:33 +00:00
|
|
|
void blue_scan(void *pvParameters) {
|
2020-12-18 10:08:52 +00:00
|
|
|
size_t len = 0;
|
|
|
|
esp_hid_scan_result_t *mouse = NULL;
|
|
|
|
esp_hid_scan_result_t *results = NULL;
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "starting scan on core %d…", xPortGetCoreID());
|
|
|
|
esp_hid_scan(BLUE_SCAN_DURATION, &len, &results);
|
|
|
|
ESP_LOGI(TAG, "scan returned %u result(s)", len);
|
2021-04-20 22:33:42 +00:00
|
|
|
xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite);
|
2020-12-18 10:08:52 +00:00
|
|
|
|
|
|
|
if (len) {
|
|
|
|
esp_hid_scan_result_t *r = results;
|
2021-04-20 22:33:42 +00:00
|
|
|
|
2020-12-18 10:08:52 +00:00
|
|
|
while (r) {
|
2021-04-20 22:33:42 +00:00
|
|
|
ESP_LOGI(TAG, "found %s %s device: " ESP_BD_ADDR_STR ", RSSI: %d, NAME: %s",
|
|
|
|
(r->transport == ESP_HID_TRANSPORT_BLE) ? "BLE" : "BT",
|
|
|
|
esp_hid_cod_major_str(r->bt.cod.major),
|
|
|
|
ESP_BD_ADDR_HEX(r->bda), r->rssi, r->name ? r->name : "");
|
|
|
|
|
|
|
|
/* search for something that looks like Bluetooth Classic mouse */
|
|
|
|
if (r->transport == ESP_HID_TRANSPORT_BT &&
|
|
|
|
strcmp("PERIPHERAL", esp_hid_cod_major_str(r->bt.cod.major)) == 0
|
|
|
|
&& (r->bt.cod.minor & ESP_HID_COD_MIN_MOUSE)) {
|
2020-12-18 10:08:52 +00:00
|
|
|
mouse = r;
|
|
|
|
}
|
|
|
|
r = r->next;
|
|
|
|
}
|
2020-12-21 22:06:33 +00:00
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
/* try to connect to the last candidate found */
|
2020-12-21 22:06:33 +00:00
|
|
|
if (mouse)
|
2020-12-18 10:08:52 +00:00
|
|
|
esp_hidh_dev_open(mouse->bda, mouse->transport, mouse->ble.addr_type);
|
2020-12-21 22:06:33 +00:00
|
|
|
else
|
2020-12-18 10:08:52 +00:00
|
|
|
ESP_LOGI(TAG, "devices found but no mouse detected");
|
|
|
|
|
|
|
|
esp_hid_scan_results_free(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelete(NULL);
|
2020-09-14 20:32:07 +00:00
|
|
|
}
|