2021-08-18 21:13:40 +00:00
|
|
|
/*
|
|
|
|
* blue.c
|
|
|
|
* quack
|
|
|
|
*
|
|
|
|
* Created by Michel DEPEIGE on 18/07/2021.
|
|
|
|
* Copyright (c) 2021 Michel DEPEIGE.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the Apache License, Version 2.0 (the "License");
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
2020-09-14 20:32:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2021-08-18 21:13:40 +00:00
|
|
|
#include "esp_hidd.h"
|
2020-09-14 20:32:07 +00:00
|
|
|
#include "esp_hidh.h"
|
|
|
|
#include "esp_hid_gap.h"
|
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
#include "adb.h"
|
2020-09-14 20:32:07 +00:00
|
|
|
#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"
|
2021-08-05 17:03:15 +00:00
|
|
|
#include "m4848.h"
|
2020-09-14 20:32:07 +00:00
|
|
|
|
2020-12-18 10:08:52 +00:00
|
|
|
/* debug tag */
|
|
|
|
static const char *TAG = "blue";
|
|
|
|
|
2021-05-09 21:16:14 +00:00
|
|
|
/* globals */
|
2021-08-18 21:13:40 +00:00
|
|
|
TaskHandle_t t_adb2hid;
|
2021-08-21 08:52:54 +00:00
|
|
|
extern TaskHandle_t t_click;
|
2021-08-18 21:13:40 +00:00
|
|
|
static esp_hidd_dev_t *hid_dev = NULL;
|
2021-08-21 08:52:54 +00:00
|
|
|
extern QueueHandle_t q_qx, q_qy;
|
2021-09-20 22:09:09 +00:00
|
|
|
uint8_t blue_pointers; // mouse count
|
2021-05-09 21:16:14 +00:00
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
/* private functions */
|
2021-08-21 08:52:54 +00:00
|
|
|
static void blue_d_connect(void);
|
|
|
|
static void blue_d_disconnect(esp_hidd_event_data_t *dev);
|
2021-08-18 21:13:40 +00:00
|
|
|
static void blue_d_init(void);
|
|
|
|
static void blue_d_start(void);
|
2021-08-18 11:39:21 +00:00
|
|
|
void blue_h_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data);
|
2021-08-18 21:13:40 +00:00
|
|
|
static void blue_h_init(void);
|
|
|
|
static esp_hid_report_item_t blue_h_ri_find(esp_hidh_dev_t *d, esp_hid_usage_t u, uint8_t t, uint8_t p);
|
2021-05-09 21:16:14 +00:00
|
|
|
static void blue_handle_button(uint8_t buttons);
|
2021-04-20 22:33:42 +00:00
|
|
|
static esp_hid_raw_report_map_t *blue_hid_rm_get(esp_hidh_dev_t *dev);
|
2021-08-18 21:13:40 +00:00
|
|
|
void blue_set_boot_protocol(esp_hidh_dev_t *dev);
|
2021-08-18 11:39:21 +00:00
|
|
|
static bool blue_support_boot(esp_hidh_dev_t *dev);
|
2021-05-09 21:16:14 +00:00
|
|
|
|
|
|
|
/* direct calls to bluedroid */
|
|
|
|
extern void BTA_HhSetProtoMode(uint8_t handle, uint8_t t_type);
|
2021-04-20 22:33:42 +00:00
|
|
|
|
2021-08-18 21:13:40 +00:00
|
|
|
/* Device specific functions blue_d_* */
|
|
|
|
static void blue_d_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data)
|
|
|
|
{
|
|
|
|
esp_hidd_event_t event = (esp_hidd_event_t)id;
|
|
|
|
esp_hidd_event_data_t *param = (esp_hidd_event_data_t *)event_data;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case ESP_HIDD_START_EVENT: {
|
|
|
|
blue_d_start();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDD_CONNECT_EVENT: {
|
2021-08-21 08:52:54 +00:00
|
|
|
blue_d_connect();
|
2021-08-18 21:13:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDD_PROTOCOL_MODE_EVENT: {
|
|
|
|
ESP_LOGI(TAG, "PROTOCOL MODE[%u]: %s", param->protocol_mode.map_index, param->protocol_mode.protocol_mode ? "REPORT" : "BOOT");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDD_CONTROL_EVENT: {
|
|
|
|
ESP_LOGI(TAG, "CONTROL[%u]: %sSUSPEND", param->control.map_index, param->control.control ? "EXIT_" : "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDD_OUTPUT_EVENT: {
|
|
|
|
ESP_LOGI(TAG, "OUTPUT[%u]: %8s ID: %2u, Len: %d, Data:", param->output.map_index, esp_hid_usage_str(param->output.usage), param->output.report_id, param->output.length);
|
|
|
|
ESP_LOG_BUFFER_HEX(TAG, param->output.data, param->output.length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDD_FEATURE_EVENT: {
|
|
|
|
ESP_LOGI(TAG, "FEATURE[%u]: %8s ID: %2u, Len: %d, Data:", param->feature.map_index, esp_hid_usage_str(param->feature.usage), param->feature.report_id, param->feature.length);
|
|
|
|
ESP_LOG_BUFFER_HEX(TAG, param->feature.data, param->feature.length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDD_DISCONNECT_EVENT: {
|
2021-08-21 08:52:54 +00:00
|
|
|
blue_d_disconnect(param);
|
2021-08-18 21:13:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_HIDD_STOP_EVENT: {
|
|
|
|
ESP_LOGI(TAG, "STOP");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-21 08:52:54 +00:00
|
|
|
static void blue_d_connect() {
|
|
|
|
ESP_LOGI(TAG, "Host connected");
|
|
|
|
xTaskNotify(t_blue, LED_ON, eSetValueWithOverwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blue_d_disconnect(esp_hidd_event_data_t *dev) {
|
|
|
|
ESP_LOGI(TAG, "Host disconnected, reason: %s",
|
|
|
|
esp_hid_disconnect_reason_str(esp_hidd_dev_transport_get(dev->disconnect.dev), dev->disconnect.reason));
|
|
|
|
esp_hid_ble_gap_adv_start();
|
|
|
|
xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite);
|
|
|
|
}
|
|
|
|
|
2021-08-18 21:13:40 +00:00
|
|
|
static void blue_d_init() {
|
|
|
|
esp_err_t ret;
|
|
|
|
|
|
|
|
ret = esp_hid_ble_gap_adv_init(ESP_HID_APPEARANCE_MOUSE, m4848_config.device_name);
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
|
|
|
if ((ret = esp_ble_gatts_register_callback(esp_hidd_gatts_event_handler)) != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "GATTS register callback failed: %d", ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_hidd_dev_init(&m4848_config, ESP_HID_TRANSPORT_BLE, blue_d_callback, &hid_dev));
|
|
|
|
xTaskCreate(blue_adb2hid, "ADB2BT", 2 * 1024, NULL, tskIDLE_PRIORITY + 1, &t_adb2hid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when the ESP HIS stack is started
|
|
|
|
* Start advertising our Bluetooth stuff
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void blue_d_start()
|
|
|
|
{
|
2021-08-20 09:33:17 +00:00
|
|
|
ESP_LOGD(TAG, "Bluetooth stack started");
|
2021-08-18 21:13:40 +00:00
|
|
|
xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite);
|
|
|
|
esp_hid_ble_gap_adv_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called by the ADB stack from adb_task_host on mouse activity
|
|
|
|
* Convert the 16bit ADB data to a 3 bytes HID INPUT REPORT matching the m4848
|
|
|
|
*/
|
|
|
|
|
|
|
|
void blue_adb2hid(void *pvParameters) {
|
|
|
|
uint16_t data = 0;
|
|
|
|
uint8_t buffer[3] = {0, 0, 0};
|
|
|
|
int8_t move = 0;
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "ADB2BT started on core %d", xPortGetCoreID());
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
xTaskNotifyWait(0, 0, &tmp, portMAX_DELAY);
|
|
|
|
data = (uint16_t)tmp;
|
|
|
|
|
2021-08-20 09:33:17 +00:00
|
|
|
if (! esp_hidd_dev_connected(hid_dev))
|
|
|
|
continue;
|
|
|
|
|
2021-08-18 21:13:40 +00:00
|
|
|
/* button pressed */
|
|
|
|
if (! (data & ADB_CMP_B1) || (! (data & ADB_CMP_B2)))
|
|
|
|
buffer[0] = (1<<0);
|
|
|
|
else
|
|
|
|
buffer[0] = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cast negative signed 7 bits to signed 8 bits
|
|
|
|
* then apply a slight acceleration (x^1.2)
|
|
|
|
*/
|
|
|
|
|
|
|
|
move = (data & ADB_CMP_MX) >> 0;
|
|
|
|
if (move & 0x40) {
|
|
|
|
move &= ~0x40;
|
|
|
|
move |= 0x80;
|
|
|
|
move += 64;
|
|
|
|
move *= -1;
|
|
|
|
buffer[1] = m4848_accel[move];
|
|
|
|
buffer[1] = buffer[1] * -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer[1] = m4848_accel[move];
|
|
|
|
}
|
|
|
|
|
|
|
|
move = (data & ADB_CMP_MY) >> 8;
|
|
|
|
if (move & 0x40) {
|
|
|
|
move &= ~0x40;
|
|
|
|
move |= 0x80;
|
|
|
|
move += 64;
|
|
|
|
move *= -1;
|
|
|
|
buffer[2] = m4848_accel[move];
|
|
|
|
buffer[2] = buffer[2] * -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer[2] = m4848_accel[move];
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_hidd_dev_input_set(hid_dev, 1, 0, buffer, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Host specific functions blue_h_* */
|
2021-08-18 11:39:21 +00:00
|
|
|
void blue_h_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data)
|
2020-09-14 20:32:07 +00:00
|
|
|
{
|
|
|
|
esp_hidh_event_t event = (esp_hidh_event_t)id;
|
|
|
|
esp_hidh_event_data_t *param = (esp_hidh_event_data_t *)event_data;
|
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
const uint8_t *bda = NULL;
|
2020-09-14 20:32:07 +00:00
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
/* union described in include/esp_hidh.h */
|
2020-09-14 20:32:07 +00:00
|
|
|
switch (event) {
|
2021-08-18 11:39:21 +00:00
|
|
|
case ESP_HIDH_OPEN_EVENT: {
|
2021-05-09 21:16:14 +00:00
|
|
|
esp_hidh_dev_dump(param->open.dev, stdout);
|
2021-08-18 11:39:21 +00:00
|
|
|
blue_h_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: {
|
2021-08-20 09:33:17 +00:00
|
|
|
//ESP_LOG_BUFFER_HEX(TAG, param->input.data, param->input.length);
|
2021-08-18 21:13:40 +00:00
|
|
|
xTaskNotify(t_yellow, LED_ONCE, eSetValueWithOverwrite);
|
2021-08-18 11:39:21 +00:00
|
|
|
blue_h_input(param->input.dev, param->input.data, param->input.length);
|
2021-05-09 21:16:14 +00:00
|
|
|
break;
|
2020-09-14 20:32:07 +00:00
|
|
|
}
|
|
|
|
case ESP_HIDH_FEATURE_EVENT: {
|
2021-08-18 11:39:21 +00:00
|
|
|
bda = esp_hidh_dev_bda_get(param->feature.dev);
|
|
|
|
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;
|
|
|
|
}
|
2020-09-14 20:32:07 +00:00
|
|
|
case ESP_HIDH_CLOSE_EVENT: {
|
2021-08-18 11:39:21 +00:00
|
|
|
blue_h_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
void blue_h_close(esp_hidh_event_data_t *p) {
|
2021-05-09 21:16:14 +00:00
|
|
|
const uint8_t *bda = NULL;
|
|
|
|
|
|
|
|
configASSERT(p != NULL);
|
|
|
|
bda = esp_hidh_dev_bda_get(p->close.dev);
|
|
|
|
ESP_LOGI(TAG, "closed connection with device " ESP_BD_ADDR_STR, ESP_BD_ADDR_HEX(bda));
|
|
|
|
esp_hidh_dev_free(p->close.dev);
|
|
|
|
|
2021-09-20 22:09:09 +00:00
|
|
|
blue_pointers--;
|
|
|
|
if (! blue_pointers)
|
|
|
|
xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite);
|
2021-05-09 21:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blue_handle_button(uint8_t buttons) {
|
|
|
|
static bool status = BLUE_BUTTON_N; // Keep state
|
|
|
|
|
|
|
|
buttons = buttons & (BLUE_BUTTON_1 | BLUE_BUTTON_2 | BLUE_BUTTON_3);
|
|
|
|
|
|
|
|
if (status && buttons)
|
|
|
|
return ;
|
|
|
|
if (status == BLUE_BUTTON_N && buttons == BLUE_BUTTON_N)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
/* release button */
|
|
|
|
if (status && buttons == BLUE_BUTTON_N) {
|
|
|
|
xTaskNotify(t_click, 0, eSetValueWithOverwrite);
|
|
|
|
status = 0;
|
|
|
|
ESP_LOGD(TAG, "button released");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* press button */
|
|
|
|
if (status == BLUE_BUTTON_N && buttons) {
|
|
|
|
xTaskNotify(t_click, 1, eSetValueWithOverwrite);
|
|
|
|
status = 1;
|
|
|
|
ESP_LOGD(TAG, "button pressed");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-18 21:13:40 +00:00
|
|
|
static void blue_h_init(void) {
|
|
|
|
esp_hidh_config_t config = {
|
|
|
|
.callback = blue_h_callback,
|
|
|
|
.event_stack_size = 4096
|
|
|
|
};
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_hidh_init(&config));
|
|
|
|
|
2021-09-20 22:09:09 +00:00
|
|
|
blue_pointers = 0;
|
2021-08-18 21:13:40 +00:00
|
|
|
xTaskCreatePinnedToCore(blue_scan, "blue_scan", 6 * 1024, NULL, 2, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bluetooth common init: init module and various stuff
|
|
|
|
* Host or Device specific inits go in blue_d_init() or blue_h_init()
|
|
|
|
*/
|
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-05-09 21:16:14 +00:00
|
|
|
ESP_LOGD(TAG, "Starting Bluetooth init on core %d", xPortGetCoreID());
|
2020-09-14 20:32:07 +00:00
|
|
|
ESP_ERROR_CHECK(ret);
|
2021-08-18 11:39:21 +00:00
|
|
|
/* ESP_BT_MODE_CLASSIC_BT doesn't work, it freezes esp_hidh_init */
|
2020-09-14 20:32:07 +00:00
|
|
|
ESP_ERROR_CHECK(esp_hid_gap_init(ESP_BT_MODE_BTDM));
|
|
|
|
ESP_ERROR_CHECK(esp_ble_gattc_register_callback(esp_hidh_gattc_event_handler));
|
|
|
|
|
2021-04-20 22:33:42 +00:00
|
|
|
esp_log_level_set("event", ESP_LOG_INFO);
|
2021-05-09 21:16:14 +00:00
|
|
|
/* complains about wrong data len on BOOT mode and CCONTROL */
|
|
|
|
esp_log_level_set("BT_HIDH", ESP_LOG_ERROR);
|
|
|
|
|
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-08-18 21:13:40 +00:00
|
|
|
|
|
|
|
if (adb_is_host())
|
|
|
|
blue_d_init();
|
|
|
|
else
|
|
|
|
blue_h_init();
|
2020-12-30 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 21:16:14 +00:00
|
|
|
/*
|
|
|
|
* data follows HID Mouse boot protocol format
|
|
|
|
*
|
|
|
|
* Byte | Bits | Description
|
|
|
|
* -----+------+---------------------------------------------------------------
|
|
|
|
* 0 | 0 | Button 1 (left)
|
|
|
|
* 0 | 1 | Button 2 (right)
|
|
|
|
* 0 | 2 | Button 3 (middle)
|
|
|
|
* 0 | 3-7 | Device specific, usually unused (0)
|
|
|
|
* 1 | 0-7 | X Displacement
|
|
|
|
* 2 | 0-7 | Y Displacement
|
|
|
|
* 3+ | 0-7 | Device specific, usually 3 is scroll wheel, usually unused (0)
|
|
|
|
*/
|
2020-12-30 18:51:03 +00:00
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
void blue_h_input(esp_hidh_dev_t *dev, uint8_t *data, uint16_t length) {
|
2021-05-09 21:16:14 +00:00
|
|
|
uint8_t buttons;
|
|
|
|
uint8_t i;
|
|
|
|
int8_t x, y;
|
2021-04-20 22:33:42 +00:00
|
|
|
|
2021-05-09 21:16:14 +00:00
|
|
|
buttons = data[0];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do some checks before parsing data. We sould't get anything wrong in theory,
|
|
|
|
* but we sometimes to get shit from either mouse or bluetooth stack
|
|
|
|
*
|
|
|
|
* Stack issue: https://github.com/espressif/esp-idf/issues/6985
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (length < 3) {
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (buttons & BLUE_BUTTON_E) {
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we also want to ignore anything that contains scroll wheel & co for now
|
|
|
|
* statistically theses are corrupted packets
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 3; i < length; i++) {
|
|
|
|
if (data[i]) {
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blue_handle_button(buttons);
|
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
/* quadrature use 7 bits x/y offsets, bluetooth BOOT uses 8 bits */
|
2021-05-09 21:16:14 +00:00
|
|
|
x = data[1];
|
|
|
|
y = data[2];
|
|
|
|
|
2021-08-21 08:52:54 +00:00
|
|
|
/* reduce bluetooth movement speed as well before notifying */
|
2021-05-09 21:16:14 +00:00
|
|
|
if (x != 0) {
|
2021-08-21 08:52:54 +00:00
|
|
|
if (x == 1 || x == -1) {
|
|
|
|
xQueueSendToBack(q_qx, &x, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x /= 2;
|
|
|
|
xQueueSendToBack(q_qx, &x, 0);
|
|
|
|
}
|
2021-05-09 21:16:14 +00:00
|
|
|
}
|
|
|
|
if (y != 0) {
|
2021-08-21 08:52:54 +00:00
|
|
|
if (y == 1 || y == -1) {
|
|
|
|
xQueueSendToBack(q_qy, &y, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
y /= 2;
|
|
|
|
xQueueSendToBack(q_qy, &y, 0);
|
|
|
|
}
|
2021-05-09 21:16:14 +00:00
|
|
|
}
|
2020-12-18 10:08:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
void blue_h_open(esp_hidh_event_data_t *p) {
|
2020-12-21 22:06:33 +00:00
|
|
|
const uint8_t *bda = NULL;
|
|
|
|
|
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-08-18 11:39:21 +00:00
|
|
|
/* Dump various info on console */
|
|
|
|
blue_hid_rm_get(p->open.dev);
|
2021-05-09 21:16:14 +00:00
|
|
|
blue_set_boot_protocol(p->open.dev);
|
2021-08-18 11:39:21 +00:00
|
|
|
if (blue_support_boot(p->open.dev) == false) {
|
|
|
|
xTaskNotify(t_red, LED_ONCE, eSetValueWithOverwrite);
|
|
|
|
ESP_LOGE(TAG, "Mouse does not support boot protocol !");
|
|
|
|
}
|
2020-12-30 18:51:03 +00:00
|
|
|
|
2021-09-20 22:09:09 +00:00
|
|
|
blue_pointers++;
|
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 */
|
2021-08-18 11:39:21 +00:00
|
|
|
static esp_hid_report_item_t blue_h_ri_find(esp_hidh_dev_t *d, esp_hid_usage_t u, uint8_t t, uint8_t p) {
|
2021-04-20 22:33:42 +00:00
|
|
|
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
|
|
|
}
|
2021-05-09 21:16:14 +00:00
|
|
|
|
2021-08-18 21:13:40 +00:00
|
|
|
void blue_set_boot_protocol(esp_hidh_dev_t *dev) {
|
2021-05-09 21:16:14 +00:00
|
|
|
configASSERT(dev != NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* /!\ Disclaimer /!\
|
|
|
|
* This is ugly. We are accessing directly bluedroid and need the hidden handle to do that
|
|
|
|
* Extract it from a private esp_hidh_dev_s struct and call BTA_HhSetProtoMode directly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct decoy_dev_s {
|
|
|
|
struct esp_hidh_dev_s *next;
|
|
|
|
|
|
|
|
esp_hid_device_config_t config;
|
|
|
|
esp_hid_usage_t usage;
|
|
|
|
esp_hid_transport_t transport;
|
|
|
|
bool connected;
|
|
|
|
bool opened;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
size_t reports_len;
|
|
|
|
void *reports;
|
|
|
|
|
|
|
|
void *tmp;
|
|
|
|
size_t tmp_len;
|
|
|
|
|
|
|
|
xSemaphoreHandle semaphore;
|
|
|
|
|
|
|
|
esp_err_t (*close) (esp_hidh_dev_t *dev);
|
|
|
|
esp_err_t (*report_write) (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t len);
|
|
|
|
esp_err_t (*report_read) (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, size_t max_length, uint8_t *value, size_t *value_len);
|
|
|
|
void (*dump) (esp_hidh_dev_t *dev, FILE *fp);
|
|
|
|
|
|
|
|
esp_bd_addr_t bda;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
esp_bt_cod_t cod;
|
|
|
|
int handle;
|
|
|
|
uint8_t sub_class;
|
|
|
|
uint8_t app_id;
|
|
|
|
uint16_t attr_mask;
|
|
|
|
} bt;
|
|
|
|
TAILQ_ENTRY(esp_hidh_dev_s) devices;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct decoy_dev_s *pass_that_handle;
|
|
|
|
pass_that_handle = (struct decoy_dev_s *)dev;
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "switching " ESP_BD_ADDR_STR " (%i) to protocol mode boot" ,
|
2021-08-18 11:39:21 +00:00
|
|
|
ESP_BD_ADDR_HEX(pass_that_handle->bda), pass_that_handle->bt.handle);
|
2021-05-09 21:16:14 +00:00
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
//ESP_LOG_BUFFER_HEX(TAG, dev, sizeof(struct decoy_dev_s));
|
2021-05-09 21:16:14 +00:00
|
|
|
/* bluedroid/bta/include/bta_hh_api.h */
|
|
|
|
BTA_HhSetProtoMode(pass_that_handle->bt.handle, 0x01);
|
|
|
|
}
|
|
|
|
|
2021-08-18 11:39:21 +00:00
|
|
|
static bool blue_support_boot(esp_hidh_dev_t *dev) {
|
|
|
|
esp_hid_report_item_t mib;
|
|
|
|
|
2021-05-09 21:16:14 +00:00
|
|
|
configASSERT(dev != NULL);
|
2021-08-18 11:39:21 +00:00
|
|
|
mib = blue_h_ri_find(dev, ESP_HID_USAGE_MOUSE, ESP_HID_REPORT_TYPE_INPUT, ESP_HID_PROTOCOL_MODE_BOOT);
|
|
|
|
if (mib.value_len == 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
2021-05-09 21:16:14 +00:00
|
|
|
}
|
|
|
|
|