From ede3d24e7ec57ea59bed10e284908ecdee01229a Mon Sep 17 00:00:00 2001 From: dlktdr Date: Mon, 27 Jun 2022 05:43:51 -0700 Subject: [PATCH 01/17] initial --- src/{bt.c => bt.cpp} | 10 +- src/bt.h | 3 + src/bt_client.c | 34 +++--- src/bt_server.c | 33 ++++-- src/cb.h | 45 ++++---- src/cobs.h | 60 +++++++++++ src/crc.cpp | 127 ++++++++++++++++++++++ src/crc.h | 107 ++++++++++++++++++ src/defines.h | 2 +- src/{frskybt.c => frskybt.cpp} | 0 src/{main.c => main.cpp} | 2 + src/{settings.c => settings.cpp} | 0 src/{terminal.c => terminal.cpp} | 180 ++++++++----------------------- src/terminal.h | 92 ++++------------ 14 files changed, 442 insertions(+), 253 deletions(-) rename src/{bt.c => bt.cpp} (98%) create mode 100644 src/cobs.h create mode 100644 src/crc.cpp create mode 100644 src/crc.h rename src/{frskybt.c => frskybt.cpp} (100%) rename src/{main.c => main.cpp} (99%) rename src/{settings.c => settings.cpp} (100%) rename src/{terminal.c => terminal.cpp} (59%) diff --git a/src/bt.c b/src/bt.cpp similarity index 98% rename from src/bt.c rename to src/bt.cpp index 4d04512..82e50cf 100644 --- a/src/bt.c +++ b/src/bt.cpp @@ -9,9 +9,12 @@ #include "esp_bt.h" #include "esp_bt_main.h" #include "esp_log.h" -#include "bt.h" #include "freertos/task.h" +extern "C" { +#include "bt.h" +}; + #define LOG_BT "BT" #define MAX_BTNAME_LEN 50 @@ -20,6 +23,7 @@ esp_bd_addr_t rmtbtaddress; char btname[MAX_BTNAME_LEN] = "Hello"; +extern "C" { void strtobtaddr(esp_bd_addr_t dest, char *src) { for(int i=0; i < 6; i++) { @@ -46,7 +50,6 @@ char *btaddrtostr(char dest[13], esp_bd_addr_t src) bool memreleased=false; - void bt_init() { esp_err_t ret; @@ -101,4 +104,5 @@ void btSetName(const char *name) strncpy(btname, name, sizeof(btname)); btname[sizeof(btname)-1] = '\0'; ESP_LOGI(LOG_BT,"Setting BT Name %s", name); -} \ No newline at end of file +} +}; \ No newline at end of file diff --git a/src/bt.h b/src/bt.h index 4cce4a3..4f1a9c9 100644 --- a/src/bt.h +++ b/src/bt.h @@ -4,6 +4,8 @@ #define BT_PAUSE_BEFORE_RESTART 250 //ms + + extern esp_bd_addr_t rmtbtaddress; extern esp_bd_addr_t localbtaddress; extern char btname[]; @@ -13,3 +15,4 @@ char *btaddrtostr(char dest[13], esp_bd_addr_t src); void bt_disable(); void bt_init(); void btSetName(const char *name); + diff --git a/src/bt_client.c b/src/bt_client.c index 979a9f8..052f19e 100644 --- a/src/bt_client.c +++ b/src/bt_client.c @@ -47,8 +47,6 @@ #define PROFILE_A_APP_ID 0 #define INVALID_HANDLE 0 -char *str_ble_board_types[BLE_BOARD_COUNT] = {"Unknown","CC2540","PARA","HeadTracker","FlySky"}; - static bool get_server = false; static esp_gattc_char_elem_t *char_elem_result = NULL; static esp_gattc_descr_elem_t *descr_elem_result = NULL; @@ -139,13 +137,14 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param; switch (event) { - case ESP_GATTC_REG_EVT: + case ESP_GATTC_REG_EVT: { ESP_LOGI(GATTC_TAG, "REG_EVT"); esp_err_t scan_ret = esp_ble_gap_set_scan_params(&ble_scan_params); if (scan_ret){ ESP_LOGE(GATTC_TAG, "set scan params error, error code = %x", scan_ret); } break; + } case ESP_GATTC_CONNECT_EVT:{ btc_connected = true; esp_ble_conn_update_params_t conn_params = {0}; @@ -171,14 +170,15 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ break; } - case ESP_GATTC_OPEN_EVT: + case ESP_GATTC_OPEN_EVT: { if (param->open.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "open failed, status %d", p_data->open.status); break; } ESP_LOGI(GATTC_TAG, "open success"); break; - case ESP_GATTC_DIS_SRVC_CMPL_EVT: + } + case ESP_GATTC_DIS_SRVC_CMPL_EVT: { if (param->dis_srvc_cmpl.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "discover service failed, status %d", param->dis_srvc_cmpl.status); break; @@ -186,12 +186,14 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ ESP_LOGI(GATTC_TAG, "discover service complete conn_id %d", param->dis_srvc_cmpl.conn_id); esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_service_uuid); break; - case ESP_GATTC_CFG_MTU_EVT: + } + case ESP_GATTC_CFG_MTU_EVT: { if (param->cfg_mtu.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG,"config mtu failed, error status = %x", param->cfg_mtu.status); } ESP_LOGI(GATTC_TAG, "ESP_GATTC_CFG_MTU_EVT, Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id); break; + } case ESP_GATTC_SEARCH_RES_EVT: { ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x is primary service %d", p_data->search_res.conn_id, p_data->search_res.is_primary); ESP_LOGI(GATTC_TAG, "start handle %d end handle %d current handle value %d", p_data->search_res.start_handle, p_data->search_res.end_handle, p_data->search_res.srvc_id.inst_id); @@ -204,7 +206,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } break; } - case ESP_GATTC_SEARCH_CMPL_EVT: + case ESP_GATTC_SEARCH_CMPL_EVT: { if (p_data->search_cmpl.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "search service failed, error status = %x", p_data->search_cmpl.status); break; @@ -287,6 +289,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } } break; + } case ESP_GATTC_REG_FOR_NOTIFY_EVT: { ESP_LOGI(GATTC_TAG, "ESP_GATTC_REG_FOR_NOTIFY_EVT"); if (p_data->reg_for_notify.status != ESP_GATT_OK){ @@ -320,7 +323,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){ - ret_status = esp_ble_gattc_write_char_descr( gattc_if, + ret_status = (esp_gatt_status_t)esp_ble_gattc_write_char_descr( gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, descr_elem_result[0].handle, sizeof(notify_en), @@ -344,7 +347,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } break; } - case ESP_GATTC_NOTIFY_EVT: + case ESP_GATTC_NOTIFY_EVT: { if (p_data->notify.is_notify) { // TODO, verify what characteristic is being notified #ifdef DEBUG_TIMERS @@ -360,7 +363,8 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } break; - case ESP_GATTC_WRITE_DESCR_EVT: + } + case ESP_GATTC_WRITE_DESCR_EVT: { if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write descr failed, error status = %x", p_data->write.status); break; @@ -379,6 +383,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); break; + } case ESP_GATTC_SRVC_CHG_EVT: { esp_bd_addr_t bda; memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t)); @@ -386,21 +391,24 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ esp_log_buffer_hex(GATTC_TAG, bda, sizeof(esp_bd_addr_t)); break; } - case ESP_GATTC_WRITE_CHAR_EVT: + case ESP_GATTC_WRITE_CHAR_EVT: { if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write char failed, error status = %x", p_data->write.status); break; } ESP_LOGI(GATTC_TAG, "write char success "); break; - case ESP_GATTC_DISCONNECT_EVT: + } + case ESP_GATTC_DISCONNECT_EVT: { btc_connected = false; get_server = false; ESP_LOGI(GATTC_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason); break; - default: + } + default: { break; } + } } static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) diff --git a/src/bt_server.c b/src/bt_server.c index 4d2f673..6f6e02e 100644 --- a/src/bt_server.c +++ b/src/bt_server.c @@ -220,7 +220,7 @@ void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { switch (event) { - case ESP_GATTS_REG_EVT: + case ESP_GATTS_REG_EVT: { ESP_LOGI(GATTS_TAG, "REGISTER_APP_EVT, status %d, app_id %d\n", param->reg.status, param->reg.app_id); gl_profile_tab[PROFILE_TRAINER_SL_ID].service_id.is_primary = true; gl_profile_tab[PROFILE_TRAINER_SL_ID].service_id.id.inst_id = 0x00; @@ -244,6 +244,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i esp_ble_gatts_create_service(gatts_if, &gl_profile_tab[PROFILE_TRAINER_SL_ID].service_id, GATTS_NUM_HANDLE_TEST_A); break; + } case ESP_GATTS_READ_EVT: { ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle); esp_gatt_rsp_t rsp; @@ -301,17 +302,20 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i example_write_event_env(gatts_if, &a_prepare_write_env, param);*/ break; } - case ESP_GATTS_EXEC_WRITE_EVT: + case ESP_GATTS_EXEC_WRITE_EVT: { ESP_LOGI(GATTS_TAG,"ESP_GATTS_EXEC_WRITE_EVT"); esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL); example_exec_write_event_env(&a_prepare_write_env, param); break; - case ESP_GATTS_MTU_EVT: + } + case ESP_GATTS_MTU_EVT: { ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu); break; - case ESP_GATTS_UNREG_EVT: + } + case ESP_GATTS_UNREG_EVT: { break; - case ESP_GATTS_CREATE_EVT: + } + case ESP_GATTS_CREATE_EVT: { ESP_LOGI(GATTS_TAG, "CREATE_SERVICE_EVT, status %d, service_handle %d\n", param->create.status, param->create.service_handle); gl_profile_tab[PROFILE_TRAINER_SL_ID].service_handle = param->create.service_handle; gl_profile_tab[PROFILE_TRAINER_SL_ID].char_uuid.len = ESP_UUID_LEN_16; @@ -327,8 +331,10 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i ESP_LOGE(GATTS_TAG, "add char failed, error code =%x",add_char_ret); } break; - case ESP_GATTS_ADD_INCL_SRVC_EVT: + } + case ESP_GATTS_ADD_INCL_SRVC_EVT: { break; + } case ESP_GATTS_ADD_CHAR_EVT: { uint16_t length = 0; const uint8_t *prf_char; @@ -354,19 +360,23 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i } break; } - case ESP_GATTS_ADD_CHAR_DESCR_EVT: + case ESP_GATTS_ADD_CHAR_DESCR_EVT: { gl_profile_tab[PROFILE_TRAINER_SL_ID].descr_handle = param->add_char_descr.attr_handle; ESP_LOGI(GATTS_TAG, "ADD_DESCR_EVT, status %d, attr_handle %d, service_handle %d\n", param->add_char_descr.status, param->add_char_descr.attr_handle, param->add_char_descr.service_handle); break; - case ESP_GATTS_DELETE_EVT: + } + case ESP_GATTS_DELETE_EVT: { break; - case ESP_GATTS_START_EVT: + } + case ESP_GATTS_START_EVT: { ESP_LOGI(GATTS_TAG, "SERVICE_START_EVT, status %d, service_handle %d\n", param->start.status, param->start.service_handle); break; - case ESP_GATTS_STOP_EVT: + } + case ESP_GATTS_STOP_EVT: { break; + } case ESP_GATTS_CONNECT_EVT: { esp_ble_conn_update_params_t conn_params = {0}; memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t)); @@ -387,11 +397,12 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i btp_connected = true; break; } - case ESP_GATTS_DISCONNECT_EVT: + case ESP_GATTS_DISCONNECT_EVT: { ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT, disconnect reason 0x%x", param->disconnect.reason); esp_ble_gap_start_advertising(&adv_params); btp_connected = false; break; + } case ESP_GATTS_CONF_EVT: case ESP_GATTS_OPEN_EVT: case ESP_GATTS_CANCEL_OPEN_EVT: diff --git a/src/cb.h b/src/cb.h index 564d36f..3420f82 100644 --- a/src/cb.h +++ b/src/cb.h @@ -15,34 +15,43 @@ typedef struct circular_buffer void cb_init(circular_buffer *cb, size_t capacity) { - cb->buffer = malloc(capacity); - if(cb->buffer == NULL) { - // TODO: handle error - } - cb->buffer_end = cb->buffer + capacity; - cb->capacity = capacity; - cb->count = 0; - cb->head = cb->buffer; - cb->tail = cb->buffer; + cb->buffer = (char*)malloc(capacity); + if(cb->buffer == NULL) { + // TODO: handle error + } + cb->buffer_end = cb->buffer + capacity; + cb->capacity = capacity; + cb->count = 0; + cb->head = cb->buffer; + cb->tail = cb->buffer; +} + +void cb_clear(circular_buffer *cb) +{ + cb->buffer_end = cb->buffer + cb->capacity; + cb->capacity = cb->capacity; + cb->count = 0; + cb->head = cb->buffer; + cb->tail = cb->buffer; } void cb_free(circular_buffer *cb) { - free(cb->buffer); + free(cb->buffer); } int cb_push_back(circular_buffer *cb, char *item) { - if(cb->count == cb->capacity) - return -1; + if(cb->count == cb->capacity) + return -1; - *cb->head = *item; + *cb->head = *item; - cb->head++; - if(cb->head == cb->buffer_end) - cb->head = cb->buffer; - cb->count++; - return 0; + cb->head++; + if(cb->head == cb->buffer_end) + cb->head = cb->buffer; + cb->count++; + return 0; } int cb_pop_front(circular_buffer *cb, char *item) diff --git a/src/cobs.h b/src/cobs.h new file mode 100644 index 0000000..9d07eb0 --- /dev/null +++ b/src/cobs.h @@ -0,0 +1,60 @@ +#pragma once + +class COBS +{ +public: + static int encode(const uint8_t* buffer, + int size, + uint8_t* encodedBuffer) + { + int read_index = 0; + int write_index = 1; + int code_index = 0; + uint8_t code = 1; + + while (read_index < size) { + if (buffer[read_index] == 0) { + encodedBuffer[code_index] = code; + code = 1; + code_index = write_index++; + read_index++; + } else { + encodedBuffer[write_index++] = buffer[read_index++]; + code++; + if (code == 0xFF) { + encodedBuffer[code_index] = code; + code = 1; + code_index = write_index++; + } + } + } + encodedBuffer[code_index] = code; + return write_index; + } + + static int decode(const uint8_t* encodedBuffer, + int size, + uint8_t* decodedBuffer) { + if (size == 0) + return 0; + int read_index = 0; + int write_index = 0; + uint8_t code = 0; + uint8_t i = 0; + while (read_index < size) { + code = encodedBuffer[read_index]; + if (read_index + code > size && code != 1) { + return 0; + } + read_index++; + for (i = 1; i < code; i++) { + decodedBuffer[write_index++] = encodedBuffer[read_index++]; + } + if (code != 0xFF && read_index != size) { + decodedBuffer[write_index++] = '\0'; + } + } + + return write_index; + } +}; \ No newline at end of file diff --git a/src/crc.cpp b/src/crc.cpp new file mode 100644 index 0000000..485851b --- /dev/null +++ b/src/crc.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "crc.h" + +static const unsigned short * crc16tab[] = { + crc16tab_1021, + crc16tab_1189 +}; + +uint16_t crc16(uint8_t index, const uint8_t * buf, uint32_t len, uint16_t start) +{ + uint16_t crc = start; + const unsigned short * tab = crc16tab[index]; + for (uint32_t i=0; i>8) ^ *buf++) & 0x00FF]; + } + return crc; +} + +// CRC8 implementation with polynom = x^8+x^7+x^6+x^4+x^2+1 (0xD5) +const unsigned char crc8tab[256] = { + 0x00, 0xD5, 0x7F, 0xAA, 0xFE, 0x2B, 0x81, 0x54, + 0x29, 0xFC, 0x56, 0x83, 0xD7, 0x02, 0xA8, 0x7D, + 0x52, 0x87, 0x2D, 0xF8, 0xAC, 0x79, 0xD3, 0x06, + 0x7B, 0xAE, 0x04, 0xD1, 0x85, 0x50, 0xFA, 0x2F, + 0xA4, 0x71, 0xDB, 0x0E, 0x5A, 0x8F, 0x25, 0xF0, + 0x8D, 0x58, 0xF2, 0x27, 0x73, 0xA6, 0x0C, 0xD9, + 0xF6, 0x23, 0x89, 0x5C, 0x08, 0xDD, 0x77, 0xA2, + 0xDF, 0x0A, 0xA0, 0x75, 0x21, 0xF4, 0x5E, 0x8B, + 0x9D, 0x48, 0xE2, 0x37, 0x63, 0xB6, 0x1C, 0xC9, + 0xB4, 0x61, 0xCB, 0x1E, 0x4A, 0x9F, 0x35, 0xE0, + 0xCF, 0x1A, 0xB0, 0x65, 0x31, 0xE4, 0x4E, 0x9B, + 0xE6, 0x33, 0x99, 0x4C, 0x18, 0xCD, 0x67, 0xB2, + 0x39, 0xEC, 0x46, 0x93, 0xC7, 0x12, 0xB8, 0x6D, + 0x10, 0xC5, 0x6F, 0xBA, 0xEE, 0x3B, 0x91, 0x44, + 0x6B, 0xBE, 0x14, 0xC1, 0x95, 0x40, 0xEA, 0x3F, + 0x42, 0x97, 0x3D, 0xE8, 0xBC, 0x69, 0xC3, 0x16, + 0xEF, 0x3A, 0x90, 0x45, 0x11, 0xC4, 0x6E, 0xBB, + 0xC6, 0x13, 0xB9, 0x6C, 0x38, 0xED, 0x47, 0x92, + 0xBD, 0x68, 0xC2, 0x17, 0x43, 0x96, 0x3C, 0xE9, + 0x94, 0x41, 0xEB, 0x3E, 0x6A, 0xBF, 0x15, 0xC0, + 0x4B, 0x9E, 0x34, 0xE1, 0xB5, 0x60, 0xCA, 0x1F, + 0x62, 0xB7, 0x1D, 0xC8, 0x9C, 0x49, 0xE3, 0x36, + 0x19, 0xCC, 0x66, 0xB3, 0xE7, 0x32, 0x98, 0x4D, + 0x30, 0xE5, 0x4F, 0x9A, 0xCE, 0x1B, 0xB1, 0x64, + 0x72, 0xA7, 0x0D, 0xD8, 0x8C, 0x59, 0xF3, 0x26, + 0x5B, 0x8E, 0x24, 0xF1, 0xA5, 0x70, 0xDA, 0x0F, + 0x20, 0xF5, 0x5F, 0x8A, 0xDE, 0x0B, 0xA1, 0x74, + 0x09, 0xDC, 0x76, 0xA3, 0xF7, 0x22, 0x88, 0x5D, + 0xD6, 0x03, 0xA9, 0x7C, 0x28, 0xFD, 0x57, 0x82, + 0xFF, 0x2A, 0x80, 0x55, 0x01, 0xD4, 0x7E, 0xAB, + 0x84, 0x51, 0xFB, 0x2E, 0x7A, 0xAF, 0x05, 0xD0, + 0xAD, 0x78, 0xD2, 0x07, 0x53, 0x86, 0x2C, 0xF9 +}; + +uint8_t crc8(const uint8_t * ptr, uint32_t len) +{ + uint8_t crc = 0; + for (uint32_t i=0; i + +enum { + CRC_1021, + CRC_1189, +}; + +uint8_t crc8(const uint8_t * ptr, uint32_t len); +uint8_t crc8_BA(const uint8_t * ptr, uint32_t len); +uint16_t crc16(uint8_t index, const uint8_t * buf, uint32_t len, uint16_t start = 0); + +// CRC16 implementation according to CCITT standards +static const unsigned short crc16tab_1021[256] = { + 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, + 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, + 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, + 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, + 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, + 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, + 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, + 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, + 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, + 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, + 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, + 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, + 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, + 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, + 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, + 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, + 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, + 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, + 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, + 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, + 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, + 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, + 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, + 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, + 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, + 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, + 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, + 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, + 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, + 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, + 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, + 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 +}; + +static const unsigned short crc16tab_1189[256] = { + 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf, + 0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7, + 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e, + 0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876, + 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd, + 0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5, + 0x3183,0x200a,0x1291,0x0318,0x77a7,0x662e,0x54b5,0x453c, + 0xbdcb,0xac42,0x9ed9,0x8f50,0xfbef,0xea66,0xd8fd,0xc974, + 0x4204,0x538d,0x6116,0x709f,0x0420,0x15a9,0x2732,0x36bb, + 0xce4c,0xdfc5,0xed5e,0xfcd7,0x8868,0x99e1,0xab7a,0xbaf3, + 0x5285,0x430c,0x7197,0x601e,0x14a1,0x0528,0x37b3,0x263a, + 0xdecd,0xcf44,0xfddf,0xec56,0x98e9,0x8960,0xbbfb,0xaa72, + 0x6306,0x728f,0x4014,0x519d,0x2522,0x34ab,0x0630,0x17b9, + 0xef4e,0xfec7,0xcc5c,0xddd5,0xa96a,0xb8e3,0x8a78,0x9bf1, + 0x7387,0x620e,0x5095,0x411c,0x35a3,0x242a,0x16b1,0x0738, + 0xffcf,0xee46,0xdcdd,0xcd54,0xb9eb,0xa862,0x9af9,0x8b70, + 0x8408,0x9581,0xa71a,0xb693,0xc22c,0xd3a5,0xe13e,0xf0b7, + 0x0840,0x19c9,0x2b52,0x3adb,0x4e64,0x5fed,0x6d76,0x7cff, + 0x9489,0x8500,0xb79b,0xa612,0xd2ad,0xc324,0xf1bf,0xe036, + 0x18c1,0x0948,0x3bd3,0x2a5a,0x5ee5,0x4f6c,0x7df7,0x6c7e, + 0xa50a,0xb483,0x8618,0x9791,0xe32e,0xf2a7,0xc03c,0xd1b5, + 0x2942,0x38cb,0x0a50,0x1bd9,0x6f66,0x7eef,0x4c74,0x5dfd, + 0xb58b,0xa402,0x9699,0x8710,0xf3af,0xe226,0xd0bd,0xc134, + 0x39c3,0x284a,0x1ad1,0x0b58,0x7fe7,0x6e6e,0x5cf5,0x4d7c, + 0xc60c,0xd785,0xe51e,0xf497,0x8028,0x91a1,0xa33a,0xb2b3, + 0x4a44,0x5bcd,0x6956,0x78df,0x0c60,0x1de9,0x2f72,0x3efb, + 0xd68d,0xc704,0xf59f,0xe416,0x90a9,0x8120,0xb3bb,0xa232, + 0x5ac5,0x4b4c,0x79d7,0x685e,0x1ce1,0x0d68,0x3ff3,0x2e7a, + 0xe70e,0xf687,0xc41c,0xd595,0xa12a,0xb0a3,0x8238,0x93b1, + 0x6b46,0x7acf,0x4854,0x59dd,0x2d62,0x3ceb,0x0e70,0x1ff9, + 0xf78f,0xe606,0xd49d,0xc514,0xb1ab,0xa022,0x92b9,0x8330, + 0x7bc7,0x6a4e,0x58d5,0x495c,0x3de3,0x2c6a,0x1ef1,0x0f78 +}; + +#endif diff --git a/src/defines.h b/src/defines.h index 46df342..ee860e0 100644 --- a/src/defines.h +++ b/src/defines.h @@ -30,7 +30,7 @@ #define UART_RX_BUFFER 512 #define UART_TX_BUFFER 512 #define BAUD_RESET_TIMER 1000000 // us -#define BAUD_DEFAULT 115200 +#define BAUD_DEFAULT 57600 #define BAUD_MAXIMUM 921600 // Optional Debugging Options diff --git a/src/frskybt.c b/src/frskybt.cpp similarity index 100% rename from src/frskybt.c rename to src/frskybt.cpp diff --git a/src/main.c b/src/main.cpp similarity index 99% rename from src/main.c rename to src/main.cpp index 2185f3a..e0d2297 100644 --- a/src/main.c +++ b/src/main.cpp @@ -44,6 +44,7 @@ void runBlinky() { } #endif +extern "C" { void app_main(void) { TaskHandle_t tUartHnd = NULL; @@ -71,3 +72,4 @@ void app_main(void) { loadSettings(); } +} \ No newline at end of file diff --git a/src/settings.c b/src/settings.cpp similarity index 100% rename from src/settings.c rename to src/settings.cpp diff --git a/src/terminal.c b/src/terminal.cpp similarity index 59% rename from src/terminal.c rename to src/terminal.cpp index 35abfad..a29147b 100644 --- a/src/terminal.c +++ b/src/terminal.cpp @@ -12,6 +12,8 @@ #include "bt_server.h" #include "defines.h" #include "settings.h" +#include "cobs.h" +#include "crc.h" #define LOG_UART "UART" @@ -55,110 +57,10 @@ char reusablebuff[REUSABLE_BUFFER]; void sendBTMode() { - char lcladdress[13] = "000000000000"; - btaddrtostr(lcladdress, localbtaddress); - ESP_LOGI(LOG_UART,"Local Addr %s", lcladdress); - if(curMode == ROLE_BLE_PERIPHERAL) { - snprintf(reusablebuff, sizeof(reusablebuff), "Peripheral:%s\r\n", lcladdress); - uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); - } else if(curMode == ROLE_BLE_CENTRAL) { - snprintf(reusablebuff, sizeof(reusablebuff), "Central:%s\r\n", lcladdress); - uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); - } -} - -void parserATCommand(char atcommand[]) -{ - // Strip trailing whitespace - bool done=false; - while(!done) { - int len = strlen(atcommand); - if(len > 0 && (atcommand[len-1] == '\n' || atcommand[len-1] == '\r')) - atcommand[len-1] = '\0'; - else - done = true; - } - if(strncmp(atcommand, "+ROLE0", 6) == 0) { - ESP_LOGI(LOG_UART, "Setting role as Peripheral"); - UART_WRITE_STRING(uart_num, "OK+Role:0\r\n"); - setRole(ROLE_BLE_PERIPHERAL); - sendBTMode(); - - } else if (strncmp(atcommand, "+ROLE1", 6) == 0) { - ESP_LOGI(LOG_UART, "Setting role as Central"); - UART_WRITE_STRING(uart_num, "OK+Role:1\r\n"); - setRole(ROLE_BLE_CENTRAL); - sendBTMode(); - - } else if (strncmp(atcommand, "+CON", 4) == 0) { - if(curMode == ROLE_BLE_CENTRAL) { - // Connect to device specified - snprintf(reusablebuff, sizeof(reusablebuff), "OK+CONNA\r\nConnecting to:%s\r\n", atcommand + 4); - uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); - // Store Remote Address to Connect to - strcpy(rmtaddress, atcommand + 4); - // Start connection - btCentralState = CENTRAL_STATE_CONNECT; - } else { - UART_WRITE_STRING(uart_num, "ERROR"); - } - - } else if (strncmp(atcommand, "+NAME", 5) == 0) { - ESP_LOGI(LOG_UART,"Setting Name to %s", atcommand + 5); - btSetName(atcommand + 5); - snprintf(reusablebuff, sizeof(reusablebuff), "OK+Name:%s\r\n", atcommand +5); - uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); - sendBTMode(); - - } else if (strncmp(atcommand, "+TXPW", 5) == 0) { - ESP_LOGI(LOG_UART, "Setting Power to %s", atcommand + 5); - UART_WRITE_STRING(uart_num, "OK+Txpw:0\r\n"); - sendBTMode(); - - } else if (strncmp(atcommand, "+DISC?", 6) == 0) { - if(curMode == ROLE_BLE_CENTRAL) { - ESP_LOGI(LOG_UART, "Discovery Requested"); - UART_WRITE_STRING(uart_num, "OK+DISCS\r\n"); - laddcnt = 0; - if(btCentralState != CENTRAL_STATE_SCAN_START && - btCentralState != CENTRAL_STATE_SCANNING) - btCentralState = CENTRAL_STATE_SCAN_START; - } - - } else if (strncmp(atcommand, "+CLEAR", 6) == 0) { - if(curMode == ROLE_BLE_CENTRAL) { - btCentralState = CENTRAL_STATE_DISCONNECT; - UART_WRITE_STRING(uart_num, "OK+CLEAR\r\n"); - } - - } else if (strncmp(atcommand, "+BAUD", 5) == 0) { - strncpy(reusablebuff, &atcommand[6], sizeof(reusablebuff)); - int baudrate = atoi(reusablebuff); - ESP_LOGI(LOG_UART, "Baud Rate Change Requested to %d", baudrate); - - baudTimer = esp_timer_get_time() + BAUD_RESET_TIMER; - //setBaudRate(baudrate); TODO - //UART_WRITE_STRING(uart_num, "OK+BAUD\r\n"); - - // TO DO: We need to check if the new baud worked. If the above timer elapses - // before an AT+ACK or something is seen. Then revert back to the default - // baud - - } else if (strncmp(atcommand, "+HTRESET", 8) == 0) { - if(btc_board_type == BLE_BOARD_HEADTRACKER) { - ESP_LOGI(LOG_UART, "Reset Head Tracker Requested"); - UART_WRITE_STRING(uart_num, "OK+HTRESET\r\n"); - btc_dohtreset(); - } else { - UART_WRITE_STRING(uart_num, "ERROR"); - } - - } else { - ESP_LOGE(LOG_UART, "Unknown AT Cmd: %s", atcommand); - } } + // Ticks to wait for data stream to come in uart_config_t uart_config = { @@ -170,19 +72,18 @@ uart_config_t uart_config = { .source_clk = UART_SCLK_APB, }; -void setBaudRate(uint32_t baudRate) -{ - if(baudRate < BAUD_DEFAULT || baudRate > BAUD_MAXIMUM) return; - uart_config.baud_rate = baudRate; - ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); -} char atcommand[AT_CMD_MAX_LEN]; int atcommandlen=-1; circular_buffer uartinbuf; -void runUARTHead() { +void processPacket(packet_s &packet) +{ + +} + +void runUARTHead(void *stuff) { // Setup UART Port ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); ESP_ERROR_CHECK(uart_driver_install(uart_num, UART_RX_BUFFER * 2, @@ -209,38 +110,29 @@ void runUARTHead() { for(;;) {} } + packet_s packet; while (1) { + uint8_t encodedbuffer[270]; + int cnt = uart_read_bytes(uart_num, data, UART_RX_BUFFER, 0); - for (int i = 0; i < cnt; i++) + for (int i = 0; i < cnt; i++) { cb_push_back(&uartinbuf, &data[i]); - - char c; - while (!cb_pop_front(&uartinbuf, &c)) { - if (atcommandlen >= 0) { - atcommand[atcommandlen++] = c; - // Check for buffer overflow - if(atcommandlen >= sizeof(atcommand)-1) { - ESP_LOGE(LOG_UART, "AT Command Buffer Overflow"); - atcommandlen = -1; - continue; - } - // AT Command Termination - if(c == '\n') { - atcommand[atcommandlen] = '\0'; - parserATCommand(atcommand); - atcommandlen = -1; - } - } else { - // Scan for characters AT in the byte stream - static char lc=0; - if(lc == 'A' && c == 'T') { - atcommandlen = 0; - } else { - frSkyProcessByte(c); + if(&data[i] == 0) { + ESP_LOGI(LOG_UART,"PacketFound"); + + // Pull the packet out of the buffer + char c; + char *ptr = (char*)encodedbuffer; + int len=0; + while (!cb_pop_front(&uartinbuf, &c)) { + *ptr++ = c; + len++; + if(len == sizeof(encodedbuffer)) + break; } - - lc = c; + COBS::decode(encodedbuffer,len,(uint8_t *)&packet); } + cb_clear(&uartinbuf); // Clear the buffer } runBT(); @@ -406,4 +298,22 @@ void runBT() default: break; } +} + +void write(const uint8_t *dat, int len, bool iscmd, int id) +{ + uint8_t encodedbuffer[sizeof(packet_s) + 1]; + packet_s packet; + packet.type = id; + packet.type |= (iscmd << ESP_PACKET_CMD_BIT); + packet.len = len; + packet.crc = 0xAABB; + memcpy(packet.data, dat, len); // TODO, Remove me, extra copy for just the crc calc. + packet.crc = crc16(0,(uint8_t *)&packet,len + PACKET_OVERHEAD); + int wl = COBS::encode((uint8_t *)&packet, packet.len + PACKET_OVERHEAD, encodedbuffer); + + encodedbuffer[wl] = '\0'; // Null terminate packet, used for detection of packet end + + // Write the packet + uart_write_bytes(uart_num, (uint8_t *)&packet, wl+1); } \ No newline at end of file diff --git a/src/terminal.h b/src/terminal.h index b2f25eb..53f8930 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -2,86 +2,34 @@ #include "defines.h" -// NOTE:: Not used at the moment. -// Future planning/thinking to enable more robust packet -// format for features that could be added while keeping -// interface cleaner -// - // Packet Format typedef struct { uint8_t len; // Data length. Max 255 per packet - uint8_t type; // Packet Type - uint8_t data[256]; // User Data + uint8_t type; uint16_t crc; // CRC16:xxxx of the packet + uint8_t data[257]; // User Data } packet_s; -// Struct for a PACKET_TYPE_8_CHANNEL -// channelmask is the bitmask of the valid channels -// Keeping SBUS Style 11 Bits of channel info - -typedef struct PACKED { - uint16_t ch01 : 11; - uint16_t ch02 : 11; - uint16_t ch03 : 11; - uint16_t ch04 : 11; - uint16_t ch05 : 11; - uint16_t ch06 : 11; - uint16_t ch07 : 11; - uint16_t ch08 : 11; - uint16_t ch09 : 11; - uint16_t ch11 : 11; - uint16_t ch12 : 11; - uint16_t ch13 : 11; - uint16_t ch14 : 11; - uint16_t ch15 : 11; - uint16_t ch16 : 11; - uint16_t ch17 : 11; - uint16_t ch18 : 11; - uint16_t ch19 : 11; - uint16_t ch20 : 11; - uint16_t ch21 : 11; - uint16_t ch22 : 11; - uint16_t ch23 : 11; - uint16_t ch24 : 11; - uint16_t ch26 : 11; - uint16_t ch27 : 11; - uint16_t ch28 : 11; - uint16_t ch29 : 11; - uint16_t ch30 : 11; - uint16_t ch31 : 11; - uint16_t ch32 : 11; // 352 bits, 44 bytes - uint32_t channelmask; -} channeldata_s; // 48 bytes total - -// Packet Type Field -// Bit [0:4] - Packet Type -// Bit [5] - Require an acknowledge to be returned -// Bit [6] - Continuation Packet - -// 0 1 2 3 4 5 6 7 -// 1 (0:6) Packets Remaining -// 1 (6:7) ACK required -// m m m m m 0 0 0 (0:5) Packet Type - +#define PACKET_OVERHEAD 4 +/* Packet Type Format + * Bits 0:4 - Type(32 Possible ESPModes, Mode 0=Base Module) + * 5 - PartialPacket=1 + * 6 - Command=1/Data=0 + * 7 - Require Ack=1 + * If Command Bit(6) = 1 + * data[0] = Command + * data[1:255] = UserData + * If command bit(6) = 0 + * data[0:255] = DataStream + */ -// x x x x x x 1 x - -// If Bit7 clear (type&^0x80) then, -// Type is defines as the below ennums -// If Bit7 set then Bits0:6 = Remaining Packets to receive +#define ESP_PACKET_CMD_BIT 6 +#define ESP_PACKET_ACK_BIT 7 +#define ESP_PACKET_ISCMD(t) (t&(1< Date: Sun, 3 Jul 2022 02:35:02 -0700 Subject: [PATCH 02/17] Packet receiving Possible memory issue --- platformio.ini | 4 +- sdkconfig.C3Mini | 2 +- src/{bt.cpp => bt.c} | 5 -- src/cobs.c | 53 +++++++++++++++++ src/cobs.h | 60 +------------------ src/{crc.cpp => crc.c} | 0 src/crc.h | 2 +- src/defines.h | 2 +- src/esproot.c | 29 ++++++++++ src/esproot.h | 8 +++ src/esptrainer.c | 14 +++++ src/esptrainer.h | 12 ++++ src/{frskybt.cpp => frskybt.c} | 0 src/{main.cpp => main.c} | 6 +- src/{settings.cpp => settings.c} | 0 src/{terminal.cpp => terminal.c} | 99 ++++++++++++++++++++++++++------ src/terminal.h | 9 +-- 17 files changed, 212 insertions(+), 93 deletions(-) rename src/{bt.cpp => bt.c} (98%) create mode 100644 src/cobs.c rename src/{crc.cpp => crc.c} (100%) create mode 100644 src/esproot.c create mode 100644 src/esproot.h create mode 100644 src/esptrainer.c create mode 100644 src/esptrainer.h rename src/{frskybt.cpp => frskybt.c} (100%) rename src/{main.cpp => main.c} (98%) rename src/{settings.cpp => settings.c} (100%) rename src/{terminal.cpp => terminal.c} (74%) diff --git a/platformio.ini b/platformio.ini index 44be824..49cd8bf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -78,8 +78,8 @@ build_flags = board = esp32-c3-devkitm-1 upload_protocol = esptool monitor_speed = 115200 -#upload_port = COM33 -#monitor_port = COM41 +upload_port = COM21 +monitor_port = COM21 #debug_tool = esptool build_flags = -DRTOS_FREERTOS=y diff --git a/sdkconfig.C3Mini b/sdkconfig.C3Mini index e1e3afa..5f5ebf0 100644 --- a/sdkconfig.C3Mini +++ b/sdkconfig.C3Mini @@ -885,7 +885,7 @@ CONFIG_LOG_DEFAULT_LEVEL_INFO=y # CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set # CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set CONFIG_LOG_DEFAULT_LEVEL=3 -CONFIG_LOG_COLORS=y +# CONFIG_LOG_COLORS is not set CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y # CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set # end of Log output diff --git a/src/bt.cpp b/src/bt.c similarity index 98% rename from src/bt.cpp rename to src/bt.c index 82e50cf..23798cd 100644 --- a/src/bt.cpp +++ b/src/bt.c @@ -10,10 +10,7 @@ #include "esp_bt_main.h" #include "esp_log.h" #include "freertos/task.h" - -extern "C" { #include "bt.h" -}; #define LOG_BT "BT" #define MAX_BTNAME_LEN 50 @@ -23,7 +20,6 @@ esp_bd_addr_t rmtbtaddress; char btname[MAX_BTNAME_LEN] = "Hello"; -extern "C" { void strtobtaddr(esp_bd_addr_t dest, char *src) { for(int i=0; i < 6; i++) { @@ -105,4 +101,3 @@ void btSetName(const char *name) btname[sizeof(btname)-1] = '\0'; ESP_LOGI(LOG_BT,"Setting BT Name %s", name); } -}; \ No newline at end of file diff --git a/src/cobs.c b/src/cobs.c new file mode 100644 index 0000000..4c734f5 --- /dev/null +++ b/src/cobs.c @@ -0,0 +1,53 @@ +#include "cobs.h" + +int cobs_encode(const uint8_t* buffer, int size, uint8_t* encodedBuffer) +{ + int read_index = 0; + int write_index = 0; + int code_index = 0; + uint8_t code = 1; + + while (read_index < size) { + if (buffer[read_index] == 0) { + encodedBuffer[code_index] = code; + code = 1; + code_index = write_index++; + read_index++; + } else { + encodedBuffer[write_index++] = buffer[read_index++]; + code++; + if (code == 0xFF) { + encodedBuffer[code_index] = code; + code = 1; + code_index = write_index++; + } + } + } + encodedBuffer[code_index] = code; + return write_index; +} + +int cobs_decode(const uint8_t* encodedBuffer, int size, uint8_t* decodedBuffer) +{ + if (size == 0) + return 0; + int read_index = 0; + int write_index = 0; + uint8_t code = 0; + uint8_t i = 0; + while (read_index < size) { + code = encodedBuffer[read_index]; + if (read_index + code > size && code != 1) { + return 0; + } + read_index++; + for (i = 1; i < code; i++) { + decodedBuffer[write_index++] = encodedBuffer[read_index++]; + } + if (code != 0xFF && read_index != size) { + decodedBuffer[write_index++] = '\0'; + } + } + + return write_index; +} diff --git a/src/cobs.h b/src/cobs.h index 9d07eb0..6caf565 100644 --- a/src/cobs.h +++ b/src/cobs.h @@ -1,60 +1,6 @@ #pragma once -class COBS -{ -public: - static int encode(const uint8_t* buffer, - int size, - uint8_t* encodedBuffer) - { - int read_index = 0; - int write_index = 1; - int code_index = 0; - uint8_t code = 1; +#include - while (read_index < size) { - if (buffer[read_index] == 0) { - encodedBuffer[code_index] = code; - code = 1; - code_index = write_index++; - read_index++; - } else { - encodedBuffer[write_index++] = buffer[read_index++]; - code++; - if (code == 0xFF) { - encodedBuffer[code_index] = code; - code = 1; - code_index = write_index++; - } - } - } - encodedBuffer[code_index] = code; - return write_index; - } - - static int decode(const uint8_t* encodedBuffer, - int size, - uint8_t* decodedBuffer) { - if (size == 0) - return 0; - int read_index = 0; - int write_index = 0; - uint8_t code = 0; - uint8_t i = 0; - while (read_index < size) { - code = encodedBuffer[read_index]; - if (read_index + code > size && code != 1) { - return 0; - } - read_index++; - for (i = 1; i < code; i++) { - decodedBuffer[write_index++] = encodedBuffer[read_index++]; - } - if (code != 0xFF && read_index != size) { - decodedBuffer[write_index++] = '\0'; - } - } - - return write_index; - } -}; \ No newline at end of file +int cobs_encode(const uint8_t* buffer, int size, uint8_t* encodedBuffer); +int cobs_decode(const uint8_t* encodedBuffer, int size, uint8_t* decodedBuffer); diff --git a/src/crc.cpp b/src/crc.c similarity index 100% rename from src/crc.cpp rename to src/crc.c diff --git a/src/crc.h b/src/crc.h index f79a56e..df5c969 100644 --- a/src/crc.h +++ b/src/crc.h @@ -31,7 +31,7 @@ enum { uint8_t crc8(const uint8_t * ptr, uint32_t len); uint8_t crc8_BA(const uint8_t * ptr, uint32_t len); -uint16_t crc16(uint8_t index, const uint8_t * buf, uint32_t len, uint16_t start = 0); +uint16_t crc16(uint8_t index, const uint8_t * buf, uint32_t len, uint16_t start); // CRC16 implementation according to CCITT standards static const unsigned short crc16tab_1021[256] = { diff --git a/src/defines.h b/src/defines.h index ee860e0..ea89d48 100644 --- a/src/defines.h +++ b/src/defines.h @@ -30,7 +30,7 @@ #define UART_RX_BUFFER 512 #define UART_TX_BUFFER 512 #define BAUD_RESET_TIMER 1000000 // us -#define BAUD_DEFAULT 57600 +#define BAUD_DEFAULT 576000 #define BAUD_MAXIMUM 921600 // Optional Debugging Options diff --git a/src/esproot.c b/src/esproot.c new file mode 100644 index 0000000..654a1c1 --- /dev/null +++ b/src/esproot.c @@ -0,0 +1,29 @@ +#include "esproot.h" + +enum ESPRootCmds { + ESP_ROOTCMD_START_MODE, + ESP_ROOTCMD_STOP_MODE, + ESP_ROOTCMD_RESTART, + ESP_ROOTCMD_GET_VER, + ESP_ROOTCMD_COUNT, +}; + +void espRootData(const char *data, uint8_t len) { + +} + +void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) +{ + switch(command) { + case ESP_ROOTCMD_START_MODE: + break; + case ESP_ROOTCMD_STOP_MODE: + break; + case ESP_ROOTCMD_RESTART: + break; + case ESP_ROOTCMD_GET_VER: + break; + case ESP_ROOTCMD_COUNT: + break; + } +} diff --git a/src/esproot.h b/src/esproot.h new file mode 100644 index 0000000..5e3ea29 --- /dev/null +++ b/src/esproot.h @@ -0,0 +1,8 @@ +#pragma once + +#include + + + +void espRootData(const char *data, uint8_t len); +void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/esptrainer.c b/src/esptrainer.c new file mode 100644 index 0000000..ef91a8e --- /dev/null +++ b/src/esptrainer.c @@ -0,0 +1,14 @@ +#include "esptrainer.h" +#include "esp_log.h" + +#define LOGS "TRNR" + +void espTrainerData(const char *data, uint8_t len) +{ + ESP_LOGI(LOGS, "p"); +} + +void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len) +{ + ESP_LOGI(LOGS, "I got a command %d", command); +} diff --git a/src/esptrainer.h b/src/esptrainer.h new file mode 100644 index 0000000..d502f67 --- /dev/null +++ b/src/esptrainer.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +enum ESPTrainerCmds { + ESP_TRAINERCMD_SET_MASTER, + ESP_TRAINERCMD_SET_SLAVE, + ESP_TRAINERCMD_COUNT, +}; + +void espTrainerData(const char *data, uint8_t len); +void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/frskybt.cpp b/src/frskybt.c similarity index 100% rename from src/frskybt.cpp rename to src/frskybt.c diff --git a/src/main.cpp b/src/main.c similarity index 98% rename from src/main.cpp rename to src/main.c index e0d2297..6b8a8e8 100644 --- a/src/main.cpp +++ b/src/main.c @@ -44,9 +44,8 @@ void runBlinky() { } #endif -extern "C" { -void app_main(void) { - +void app_main(void) +{ TaskHandle_t tUartHnd = NULL; xTaskCreate(runUARTHead, "UART", 4096, NULL, tskIDLE_PRIORITY+2, &tUartHnd); configASSERT(tUartHnd); @@ -72,4 +71,3 @@ void app_main(void) { loadSettings(); } -} \ No newline at end of file diff --git a/src/settings.cpp b/src/settings.c similarity index 100% rename from src/settings.cpp rename to src/settings.c diff --git a/src/terminal.cpp b/src/terminal.c similarity index 74% rename from src/terminal.cpp rename to src/terminal.c index a29147b..a071593 100644 --- a/src/terminal.cpp +++ b/src/terminal.c @@ -15,6 +15,9 @@ #include "cobs.h" #include "crc.h" +#include "esproot.h" +#include "esptrainer.h" + #define LOG_UART "UART" #define UART_RX_BUFFER 512 @@ -78,11 +81,55 @@ int atcommandlen=-1; circular_buffer uartinbuf; -void processPacket(packet_s &packet) -{ +enum ESPModes { + ESP_ROOT, + ESP_TELEMETRY, + ESP_TRAINER, + ESP_JOYSTICK, + ESP_AUDIO, + ESP_FTP, + ESP_IMU, + ESP_MAX +}; +#define ESP_BASE 0 +#define ESP_PACKET_TYPE_MSK 0x0F +#define ESP_PACKET_CMD_BIT 6 +#define ESP_PACKET_ACK_BIT 7 +#define ESP_PACKET_ISCMD(t) (t&(1<type & ESP_PACKET_TYPE_MSK) { + case ESP_ROOT: + if(ESP_PACKET_ISCMD(packet->type)) + espRootCommand(packet->data[0], packet->data + 1, packet->len -1); + else + espRootData(packet->data, packet->len); + break; + case ESP_TELEMETRY: + break; + case ESP_TRAINER: + if(ESP_PACKET_ISCMD(packet->type)) + espTrainerCommand(packet->data[0], packet->data + 1, packet->len -1); + else + espTrainerData(packet->data, packet->len); + break; + break; + case ESP_JOYSTICK: + break; + case ESP_AUDIO: + break; + case ESP_FTP: + break; + case ESP_IMU: + break; + } } +#define PACKED_BUFFERS 5 + void runUARTHead(void *stuff) { // Setup UART Port ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); @@ -112,28 +159,42 @@ void runUARTHead(void *stuff) { packet_s packet; while (1) { - uint8_t encodedbuffer[270]; - + uint8_t encodedbuffer[270] = "\0"; int cnt = uart_read_bytes(uart_num, data, UART_RX_BUFFER, 0); + // Room for improvment, don't write to the cb for (int i = 0; i < cnt; i++) { - cb_push_back(&uartinbuf, &data[i]); - if(&data[i] == 0) { - ESP_LOGI(LOG_UART,"PacketFound"); - - // Pull the packet out of the buffer + cb_push_back(&uartinbuf, data + i); + if(data[i] == 0) { char c; char *ptr = (char*)encodedbuffer; int len=0; while (!cb_pop_front(&uartinbuf, &c)) { *ptr++ = c; len++; - if(len == sizeof(encodedbuffer)) + if(len == sizeof(encodedbuffer)) { + ESP_LOGE("UartRX", "Buffer overflow"); break; + } } - COBS::decode(encodedbuffer,len,(uint8_t *)&packet); +// ESP_LOG_BUFFER_HEX("EBL", encodedbuffer, len); + int lenout = cobs_decode(encodedbuffer,len,(uint8_t *)&packet); +// ESP_LOG_BUFFER_HEX("P", (uint8_t *)&packet, lenout); + uint16_t packetcrc = packet.crcl | (packet.crch << 8); // Store transmitted packet + packet.crcl = 0xBB; + packet.crch = 0xAA; + uint16_t calccrc = crc16(0,(uint8_t *)&packet,lenout - 1, 0); + packet.len = lenout - PACKET_OVERHEAD - 1; +// printf("in %d out %d data %d\r\n", len, lenout, packet.len); + if(packetcrc == calccrc) { + // Successful packet, parse it + processPacket(&packet); + } else { + ESP_LOGE("PM", "CRC Fault"); + } + cb_clear(&uartinbuf); // Clear the buffer } - cb_clear(&uartinbuf); // Clear the buffer } + //uart_write_bytes(uart_num,"Hello\n",7); runBT(); vTaskDelay(1); @@ -239,8 +300,8 @@ void runBTCentral() uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); sprintf(reusablebuff, "MTU Size:65\r\nMTU Size: 65\r\nPHT Update Complete\r\nCurrent PHY:2M\r\n"); // Fix me uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); - sprintf(reusablebuff, "Board:%s\r\n", str_ble_board_types[btc_board_type]); - uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); +// sprintf(reusablebuff, "Board:%s\r\n", str_ble_board_types[btc_board_type]); +// uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); btCentralState = CENTRAL_STATE_CONNECTED; // TODO: Add @@ -306,11 +367,13 @@ void write(const uint8_t *dat, int len, bool iscmd, int id) packet_s packet; packet.type = id; packet.type |= (iscmd << ESP_PACKET_CMD_BIT); - packet.len = len; - packet.crc = 0xAABB; + packet.crcl = 0xBB; + packet.crch = 0xAA; memcpy(packet.data, dat, len); // TODO, Remove me, extra copy for just the crc calc. - packet.crc = crc16(0,(uint8_t *)&packet,len + PACKET_OVERHEAD); - int wl = COBS::encode((uint8_t *)&packet, packet.len + PACKET_OVERHEAD, encodedbuffer); + uint16_t crc = crc16(0, (uint8_t *)&packet,len + PACKET_OVERHEAD, 0); + packet.crcl = crc & 0xFF; + packet.crch = (crc & 0xFF00) >> 8; + int wl = cobs_encode((uint8_t *)&packet, packet.len + PACKET_OVERHEAD, encodedbuffer); encodedbuffer[wl] = '\0'; // Null terminate packet, used for detection of packet end diff --git a/src/terminal.h b/src/terminal.h index 53f8930..ff6ffa3 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -3,14 +3,15 @@ #include "defines.h" // Packet Format -typedef struct { - uint8_t len; // Data length. Max 255 per packet +typedef struct PACKED { uint8_t type; - uint16_t crc; // CRC16:xxxx of the packet + uint8_t crcl; // CRC16:low byte + uint8_t crch; // CRC16:high byte uint8_t data[257]; // User Data + uint8_t len; // Data length, not transmitted } packet_s; -#define PACKET_OVERHEAD 4 +#define PACKET_OVERHEAD 3 /* Packet Type Format * Bits 0:4 - Type(32 Possible ESPModes, Mode 0=Base Module) From 0d0557362eb912d9c76d0c424bdef73fa7baa09d Mon Sep 17 00:00:00 2001 From: dlktdr Date: Mon, 4 Jul 2022 06:07:13 -0700 Subject: [PATCH 03/17] More packet mode changes --- src/defines.h | 2 -- src/esproot.c | 2 +- src/esproot.h | 2 +- src/esptrainer.c | 15 ++++++++++++--- src/esptrainer.h | 2 +- src/main.c | 2 +- src/terminal.c | 18 +++++++++++------- 7 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/defines.h b/src/defines.h index ea89d48..f37a33d 100644 --- a/src/defines.h +++ b/src/defines.h @@ -27,8 +27,6 @@ #endif -#define UART_RX_BUFFER 512 -#define UART_TX_BUFFER 512 #define BAUD_RESET_TIMER 1000000 // us #define BAUD_DEFAULT 576000 #define BAUD_MAXIMUM 921600 diff --git a/src/esproot.c b/src/esproot.c index 654a1c1..faee284 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -8,7 +8,7 @@ enum ESPRootCmds { ESP_ROOTCMD_COUNT, }; -void espRootData(const char *data, uint8_t len) { +void espRootData(const uint8_t *data, uint8_t len) { } diff --git a/src/esproot.h b/src/esproot.h index 5e3ea29..e3a28ba 100644 --- a/src/esproot.h +++ b/src/esproot.h @@ -4,5 +4,5 @@ -void espRootData(const char *data, uint8_t len); +void espRootData(const uint8_t *data, uint8_t len); void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/esptrainer.c b/src/esptrainer.c index ef91a8e..08a3f0c 100644 --- a/src/esptrainer.c +++ b/src/esptrainer.c @@ -3,12 +3,21 @@ #define LOGS "TRNR" -void espTrainerData(const char *data, uint8_t len) +// Channel Format +typedef struct { + int16_t ch[32]; + uint32_t channelmask; // Valid Channels +} channeldata; + + +void espTrainerData(const uint8_t *data, uint8_t len) { - ESP_LOGI(LOGS, "p"); + printf("d\r\n"); +// ESP_LOGI(LOGS, "%d", len); } void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len) { - ESP_LOGI(LOGS, "I got a command %d", command); + printf("c\r\n"); +// ESP_LOGI(LOGS, "%d", command); } diff --git a/src/esptrainer.h b/src/esptrainer.h index d502f67..343754c 100644 --- a/src/esptrainer.h +++ b/src/esptrainer.h @@ -8,5 +8,5 @@ enum ESPTrainerCmds { ESP_TRAINERCMD_COUNT, }; -void espTrainerData(const char *data, uint8_t len); +void espTrainerData(const uint8_t *data, uint8_t len); void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/main.c b/src/main.c index 6b8a8e8..d8227e1 100644 --- a/src/main.c +++ b/src/main.c @@ -47,7 +47,7 @@ void runBlinky() { void app_main(void) { TaskHandle_t tUartHnd = NULL; - xTaskCreate(runUARTHead, "UART", 4096, NULL, tskIDLE_PRIORITY+2, &tUartHnd); + xTaskCreate(runUARTHead, "UART", 4096, NULL, tskIDLE_PRIORITY+12, &tUartHnd); configASSERT(tUartHnd); #if defined(LEDPIN) diff --git a/src/terminal.c b/src/terminal.c index a071593..061fbf5 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -133,8 +133,8 @@ void processPacket(const packet_s *packet) void runUARTHead(void *stuff) { // Setup UART Port ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); - ESP_ERROR_CHECK(uart_driver_install(uart_num, UART_RX_BUFFER * 2, - UART_RX_BUFFER * 2, 0, NULL, 0)); + ESP_ERROR_CHECK(uart_driver_install(uart_num, UART_RX_BUFFER * 4, + UART_RX_BUFFER * 4, 0, NULL, 0)); ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_TXPIN, UART_RXPIN, UART_PIN_NO_CHANGE, @@ -159,11 +159,13 @@ void runUARTHead(void *stuff) { packet_s packet; while (1) { - uint8_t encodedbuffer[270] = "\0"; + uint8_t encodedbuffer[UART_RX_BUFFER]; int cnt = uart_read_bytes(uart_num, data, UART_RX_BUFFER, 0); - // Room for improvment, don't write to the cb + for (int i = 0; i < cnt; i++) { - cb_push_back(&uartinbuf, data + i); + if(!cb_push_back(&uartinbuf, data + i)) { + ESP_LOGE("UartRX", "Circular buffer full"); + } if(data[i] == 0) { char c; char *ptr = (char*)encodedbuffer; @@ -176,6 +178,8 @@ void runUARTHead(void *stuff) { break; } } + cb_clear(&uartinbuf); // Clear the buffer + // ESP_LOG_BUFFER_HEX("EBL", encodedbuffer, len); int lenout = cobs_decode(encodedbuffer,len,(uint8_t *)&packet); // ESP_LOG_BUFFER_HEX("P", (uint8_t *)&packet, lenout); @@ -191,12 +195,12 @@ void runUARTHead(void *stuff) { } else { ESP_LOGE("PM", "CRC Fault"); } - cb_clear(&uartinbuf); // Clear the buffer + } } //uart_write_bytes(uart_num,"Hello\n",7); - runBT(); +// runBT(); vTaskDelay(1); } free(data); From ffd184ad1793944f097c9dec91fca632665066eb Mon Sep 17 00:00:00 2001 From: Cliff Blackburn Date: Sun, 10 Jul 2022 21:10:02 -0600 Subject: [PATCH 04/17] Uart receive, mode files --- src/defines.h | 1 + src/espaudio.c | 31 +++++++++ src/espaudio.h | 9 +++ src/espdefs.h | 47 +++++++++++++ src/espjoystick.c | 31 +++++++++ src/espjoystick.h | 9 +++ src/esproot.c | 111 ++++++++++++++++++++++++++--- src/esproot.h | 3 +- src/esptrainer.c | 49 +++++++++++-- src/esptrainer.h | 11 ++- src/main.c | 9 ++- src/terminal.c | 174 ++++++++++++++++++++++++++++------------------ src/terminal.h | 14 ++-- 13 files changed, 397 insertions(+), 102 deletions(-) create mode 100644 src/espaudio.c create mode 100644 src/espaudio.h create mode 100644 src/espdefs.h create mode 100644 src/espjoystick.c create mode 100644 src/espjoystick.h diff --git a/src/defines.h b/src/defines.h index f37a33d..d2ac925 100644 --- a/src/defines.h +++ b/src/defines.h @@ -43,6 +43,7 @@ typedef enum { ROLE_UNKNOWN=0, ROLE_BLE_PERIPHERAL, ROLE_BLE_CENTRAL, + ROLE_BLE_JOYSTICK, ROLE_ADVANCE, ROLE_COUNT } role_t; diff --git a/src/espaudio.c b/src/espaudio.c new file mode 100644 index 0000000..3c503eb --- /dev/null +++ b/src/espaudio.c @@ -0,0 +1,31 @@ +#include +#include "espaudio.h" +#include "esp_log.h" + +#define AUDIO_TAG "AUD" + +int espAudioStart() +{ + return -1; +} + +void espAudioStop() +{ + +} + +bool espAudioRunning() +{ + return false; +} + +void espAudioData(const uint8_t *data, uint8_t len) +{ + // Read audio data, resample and write to bluetooth + +} + +void espAudioCommand(uint8_t command, const uint8_t *data, uint8_t len) +{ + +} diff --git a/src/espaudio.h b/src/espaudio.h new file mode 100644 index 0000000..a1c6e9a --- /dev/null +++ b/src/espaudio.h @@ -0,0 +1,9 @@ +#pragma once + +#include "espdefs.h" + +int espAudioStart(); +void espAudioStop(); +bool espAudioRunning(); +void espAudioData(const uint8_t *data, uint8_t len); +void espAudioCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/espdefs.h b/src/espdefs.h new file mode 100644 index 0000000..e9a35fa --- /dev/null +++ b/src/espdefs.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include + +#define ESP_PACKET_TYPE_MSK 0x0F +#define ESP_PACKET_CMD_BIT 6 +#define ESP_PACKET_ACK_BIT 7 +#define ESP_PACKET_ISCMD(t) (t&(1< +#include "espjoystick.h" +#include "esp_log.h" + +#define JOYSTICK_TAG "JOY" + +int espJoystickStart() +{ + return -1; +} + +void espJoystickStop() +{ + +} + +bool espJoystickRunning() +{ + return false; +} + +void espJoystickData(const uint8_t *data, uint8_t len) +{ + // Read audio data, resample and write to bluetooth + +} + +void espJoystickCommand(uint8_t command, const uint8_t *data, uint8_t len) +{ + +} diff --git a/src/espjoystick.h b/src/espjoystick.h new file mode 100644 index 0000000..1790aa0 --- /dev/null +++ b/src/espjoystick.h @@ -0,0 +1,9 @@ +#pragma once + +#include "espdefs.h" + +int espJoystickStart(); +void espJoystickStop(); +bool espJoystickRunning(); +void espJoystickData(const uint8_t *data, uint8_t len); +void espJoystickCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/esproot.c b/src/esproot.c index faee284..7146508 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -1,29 +1,118 @@ +#include #include "esproot.h" +#include "esptrainer.h" +#include "espjoystick.h" +#include "espaudio.h" +#include "terminal.h" +#include "espdefs.h" -enum ESPRootCmds { - ESP_ROOTCMD_START_MODE, - ESP_ROOTCMD_STOP_MODE, - ESP_ROOTCMD_RESTART, - ESP_ROOTCMD_GET_VER, - ESP_ROOTCMD_COUNT, -}; +#define LOG_ESPR "ESPROOT" -void espRootData(const uint8_t *data, uint8_t len) { +int rv; +#define STARTBLE_MODE(m,x) rv=x; writeAckNak(m,rv,""); if(rv==0) radioused=true; +#define STARTBTEDR_MODE(m,x) rv=x; writeAckNak(m,rv,""); if(rv==0) radioused=true; +#define STARTWIFI_MODE(m,x) rv=x; writeAckNak(m,rv,""); if(rv==0) radioused=true; +#define START_MODE(m,x) writeAckNak(m,x,""); +uint8_t runningModes; +bool radioused=false; + +void espRootData(const uint8_t *data, uint8_t len) +{ + } void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { + uint8_t mode = data[0]; switch(command) { + + // Startup Command case ESP_ROOTCMD_START_MODE: + ESP_LOGI(LOG_ESPR, "Starting Mode %d", data[0]); + if(radioused && ((mode == ESP_TELEMETRY || + mode == ESP_TRAINER || + mode == ESP_JOYSTICK || + mode == ESP_AUDIO || + mode == ESP_FTP))) { + ESP_LOGE(LOG_ESPR, "Cannot start Radio already used"); + writeAckNak(mode,false, "Radio already used"); // Write error + } else { + switch (mode) + { + case ESP_TELEMETRY: + //STARTRADIO_MODE(ESP_TELEMETRY, espTelemetryStart()); + break; + case ESP_TRAINER: + STARTBLE_MODE(ESP_TRAINER, espTrainerStart()); + break; + case ESP_JOYSTICK: + STARTBLE_MODE(ESP_JOYSTICK, espJoystickStart()); + break; + case ESP_AUDIO: + STARTBTEDR_MODE(ESP_AUDIO, espAudioStart()); + break; + case ESP_FTP: + //STARTWIFI_MODE(ESP_FTP, espFTPStart()); + break; + case ESP_IMU: + //START_MODE(ESP_IMU, espIMUStart()); + break; + default: + break; + } + } break; + case ESP_ROOTCMD_STOP_MODE: + ESP_LOGI(LOG_ESPR, "Stopping Mode %d", data[0]); + switch (mode) { + case ESP_TELEMETRY: + // if(espTrainerRunning()) { + // espTrainerStop(); + // radioused = false; + // } + break; + case ESP_TRAINER: + if(espTrainerRunning()) { + espTrainerStop(); + radioused = false; + } + break; + case ESP_JOYSTICK: + if(espJoystickRunning()) { + espJoystickStop(); + radioused = false; + } + break; + case ESP_AUDIO: + if(espAudioRunning()) { + espAudioStop(); + radioused = false; + } + break; + case ESP_FTP: + // if(espTrainerRunning()) { + // espTrainerStop(); + // radioused = false; + // } + break; + case ESP_IMU: + // if(espIMURunning()) { + // espTIMUStop(); + // } + break; + default: + break; + } break; + case ESP_ROOTCMD_RESTART: + ESP_LOGI(LOG_ESPR, "Rebooting"); + esp_restart(); break; - case ESP_ROOTCMD_GET_VER: - break; - case ESP_ROOTCMD_COUNT: + case ESP_ROOTCMD_GET_VER: break; } } + diff --git a/src/esproot.h b/src/esproot.h index e3a28ba..71ed625 100644 --- a/src/esproot.h +++ b/src/esproot.h @@ -1,8 +1,7 @@ #pragma once #include - - +#include "espdefs.h" void espRootData(const uint8_t *data, uint8_t len); void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/esptrainer.c b/src/esptrainer.c index 08a3f0c..c19416e 100644 --- a/src/esptrainer.c +++ b/src/esptrainer.c @@ -1,23 +1,60 @@ +#include #include "esptrainer.h" #include "esp_log.h" -#define LOGS "TRNR" +#define TRAINER_TAG "TRNR" +#define MAX_OUTPUT_CHANNELS 32 // Channel Format typedef struct { - int16_t ch[32]; + int16_t ch[MAX_OUTPUT_CHANNELS]; uint32_t channelmask; // Valid Channels } channeldata; +bool trainerstarted=false; +int espTrainerStart() +{ + if(trainerstarted) return -1; + + return 0; +} + +void espTrainerStop() +{ + if(!trainerstarted) return; + + trainerstarted = false; +} + +bool espTrainerRunning() +{ + return trainerstarted; +} + +// Trainer data received void espTrainerData(const uint8_t *data, uint8_t len) { - printf("d\r\n"); -// ESP_LOGI(LOGS, "%d", len); + if(len == sizeof(channeldata)) { + const channeldata *chdata = (const channeldata *)data; + for(int i=0; i < MAX_OUTPUT_CHANNELS; i++) { + if(chdata->channelmask & 1<ch[i]); + } + printf("\r\n"); + + } else { + ESP_LOGE(TRAINER_TAG, "Unknown Data"); + } } void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len) { - printf("c\r\n"); -// ESP_LOGI(LOGS, "%d", command); + ESP_LOGI(TRAINER_TAG, "Got A Command %d", command); + switch(command) { + case ESP_TRAINERCMD_SET_MASTER: + break; + case ESP_TRAINERCMD_SET_SLAVE: + break; + } } diff --git a/src/esptrainer.h b/src/esptrainer.h index 343754c..f58c051 100644 --- a/src/esptrainer.h +++ b/src/esptrainer.h @@ -1,12 +1,9 @@ #pragma once -#include - -enum ESPTrainerCmds { - ESP_TRAINERCMD_SET_MASTER, - ESP_TRAINERCMD_SET_SLAVE, - ESP_TRAINERCMD_COUNT, -}; +#include "espdefs.h" +int espTrainerStart(); +void espTrainerStop(); +bool espTrainerRunning(); void espTrainerData(const uint8_t *data, uint8_t len); void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/main.c b/src/main.c index d8227e1..aecf0c0 100644 --- a/src/main.c +++ b/src/main.c @@ -47,8 +47,13 @@ void runBlinky() { void app_main(void) { TaskHandle_t tUartHnd = NULL; - xTaskCreate(runUARTHead, "UART", 4096, NULL, tskIDLE_PRIORITY+12, &tUartHnd); - configASSERT(tUartHnd); + xTaskCreate(mainTask, "UART", 65535, NULL, tskIDLE_PRIORITY+1, &tUartHnd); + //configASSERT(tUartHnd); + + TaskHandle_t tUartRxHnd = NULL; + xTaskCreate(uartRXTask, "UARTRX", 2048, NULL, tskIDLE_PRIORITY+8, &tUartRxHnd); + //configASSERT(tUartHnd); + #if defined(LEDPIN) TaskHandle_t tBlinkHnd = NULL; diff --git a/src/terminal.c b/src/terminal.c index 061fbf5..ae84dc0 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -17,18 +17,24 @@ #include "esproot.h" #include "esptrainer.h" +#include "espaudio.h" +#include "freertos/stream_buffer.h" #define LOG_UART "UART" -#define UART_RX_BUFFER 512 +#define UART_RX_BUFFER 1024 #define REUSABLE_BUFFER 250 #define AT_CMD_MAX_LEN 40 #define BT_CMD_MAX_LEN 30 +StreamBufferHandle_t uartrxstreamhndl; + void runBT(); void setBaudRate(uint32_t baudRate); void setRole(role_t role); +volatile bool uartRXTaskStarted=false; + const uart_port_t uart_num = UART_NUM; #define UART_WRITE_STRING(x,y) uart_write_bytes(x, y, sizeof(y)-1) @@ -81,17 +87,6 @@ int atcommandlen=-1; circular_buffer uartinbuf; -enum ESPModes { - ESP_ROOT, - ESP_TELEMETRY, - ESP_TRAINER, - ESP_JOYSTICK, - ESP_AUDIO, - ESP_FTP, - ESP_IMU, - ESP_MAX -}; - #define ESP_BASE 0 #define ESP_PACKET_TYPE_MSK 0x0F #define ESP_PACKET_CMD_BIT 6 @@ -116,10 +111,17 @@ void processPacket(const packet_s *packet) else espTrainerData(packet->data, packet->len); break; - break; case ESP_JOYSTICK: + if(ESP_PACKET_ISCMD(packet->type)) + espAudioCommand(packet->data[0], packet->data + 1, packet->len -1); + else + espAudioData(packet->data, packet->len); break; case ESP_AUDIO: + if(ESP_PACKET_ISCMD(packet->type)) + espAudioCommand(packet->data[0], packet->data + 1, packet->len -1); + else + espAudioData(packet->data, packet->len); break; case ESP_FTP: break; @@ -130,18 +132,7 @@ void processPacket(const packet_s *packet) #define PACKED_BUFFERS 5 -void runUARTHead(void *stuff) { - // Setup UART Port - ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); - ESP_ERROR_CHECK(uart_driver_install(uart_num, UART_RX_BUFFER * 4, - UART_RX_BUFFER * 4, 0, NULL, 0)); - ESP_ERROR_CHECK(uart_set_pin(uart_num, UART_TXPIN, - UART_RXPIN, - UART_PIN_NO_CHANGE, - UART_PIN_NO_CHANGE)); - - cb_init(&uartinbuf, UART_RX_BUFFER*2); - +void mainTask(void *stuff) { ESP_LOGI(LOG_UART, "Waiting for settings to be read"); while(!settings_ok) {vTaskDelay(50);}; // Pause until settings are read ESP_LOGI(LOG_UART, "Setting initial role"); @@ -151,59 +142,41 @@ void runUARTHead(void *stuff) { } setRole(settings.role); - char* data = (char*) malloc(UART_RX_BUFFER+1); - if(data == NULL) { - ESP_LOGE(LOG_UART, "No Memory!!!!!\nHALT"); - for(;;) {} - } + ESP_LOGI(LOG_UART, "Waiting for UART RX Task to start"); + while(!uartRXTaskStarted); packet_s packet; - while (1) { - uint8_t encodedbuffer[UART_RX_BUFFER]; - int cnt = uart_read_bytes(uart_num, data, UART_RX_BUFFER, 0); - - for (int i = 0; i < cnt; i++) { - if(!cb_push_back(&uartinbuf, data + i)) { - ESP_LOGE("UartRX", "Circular buffer full"); - } - if(data[i] == 0) { - char c; - char *ptr = (char*)encodedbuffer; - int len=0; - while (!cb_pop_front(&uartinbuf, &c)) { - *ptr++ = c; - len++; - if(len == sizeof(encodedbuffer)) { - ESP_LOGE("UartRX", "Buffer overflow"); - break; - } - } - cb_clear(&uartinbuf); // Clear the buffer - -// ESP_LOG_BUFFER_HEX("EBL", encodedbuffer, len); - int lenout = cobs_decode(encodedbuffer,len,(uint8_t *)&packet); -// ESP_LOG_BUFFER_HEX("P", (uint8_t *)&packet, lenout); + uint8_t buffer[sizeof(packet_s)+1]; + int bufferpos =0; + + while (1) { + char inb; + int rec = xStreamBufferReceive(uartrxstreamhndl, &inb, 1, 10); + if(rec) { + if(inb == 0 && bufferpos != 0) { + int lenout = cobs_decode(buffer,bufferpos,(uint8_t *)&packet); + //ESP_LOG_BUFFER_HEX("P", (uint8_t *)&packet, lenout); uint16_t packetcrc = packet.crcl | (packet.crch << 8); // Store transmitted packet packet.crcl = 0xBB; packet.crch = 0xAA; - uint16_t calccrc = crc16(0,(uint8_t *)&packet,lenout - 1, 0); - packet.len = lenout - PACKET_OVERHEAD - 1; -// printf("in %d out %d data %d\r\n", len, lenout, packet.len); + uint16_t calccrc = crc16(0,(uint8_t *)&packet,lenout, 0); + packet.len = lenout - PACKET_OVERHEAD; + // printf("in %d out %d data %d\r\n", len, lenout, packet.len); if(packetcrc == calccrc) { - // Successful packet, parse it - processPacket(&packet); + processPacket(&packet); } else { ESP_LOGE("PM", "CRC Fault"); } - + bufferpos = 0; + } else { + buffer[bufferpos++] = inb; + if(bufferpos == sizeof(buffer)) { + printf("Buffer Overflow\r\n"); + bufferpos = 0; + } } } - //uart_write_bytes(uart_num,"Hello\n",7); - -// runBT(); - vTaskDelay(1); } - free(data); vTaskDelete(NULL); } @@ -236,6 +209,11 @@ void setRole(role_t role) bt_init(); btpInit(); break; + case ROLE_BLE_JOYSTICK: + btPeripherialState = PERIPHERIAL_STATE_DISCONNECTED; + bt_init(); + //btjInit(); // TOOD + default: break; } @@ -365,11 +343,12 @@ void runBT() } } -void write(const uint8_t *dat, int len, bool iscmd, int id) +// Builds a packet +void writePacket(const uint8_t *dat, int len, bool iscmd, int mode) { uint8_t encodedbuffer[sizeof(packet_s) + 1]; packet_s packet; - packet.type = id; + packet.type = mode; packet.type |= (iscmd << ESP_PACKET_CMD_BIT); packet.crcl = 0xBB; packet.crch = 0xAA; @@ -383,4 +362,63 @@ void write(const uint8_t *dat, int len, bool iscmd, int id) // Write the packet uart_write_bytes(uart_num, (uint8_t *)&packet, wl+1); +} + +// Sends some data +void writeData(int mode, const uint8_t *dat, int len) +{ + writePacket(dat, len, false, mode); +} + +// Send a command +void writeCommand(int mode, uint8_t command, const uint8_t *dat, int len) +{ + uint8_t *data = malloc(len+1); + memcpy(data + 1, dat, len); + data[0] = command; + writePacket(data, len+1, true, mode); + free(data); +} + +// Writes an acknowledge/ not-acknowledge and a optional message +void writeAckNak(int mode, bool ack, const char *message) +{ + uint8_t command = ESP_ROOTCMD_ACKNAK | (ack?1< Date: Fri, 24 Jun 2022 18:57:24 -0700 Subject: [PATCH 05/17] Joystick Initial Commit --- src/joystick/bt_joystick.c | 205 ++++++++ src/joystick/bt_joystick.h | 5 + src/joystick/esp_hidd_prf_api.c | 240 +++++++++ src/joystick/esp_hidd_prf_api.h | 167 +++++++ src/joystick/hid_dev.c | 137 ++++++ src/joystick/hid_dev.h | 260 ++++++++++ src/joystick/hid_device_le_prf.c | 815 +++++++++++++++++++++++++++++++ src/joystick/hidd_le_prf_int.h | 343 +++++++++++++ src/terminal.c | 8 +- 9 files changed, 2176 insertions(+), 4 deletions(-) create mode 100644 src/joystick/bt_joystick.c create mode 100644 src/joystick/bt_joystick.h create mode 100644 src/joystick/esp_hidd_prf_api.c create mode 100644 src/joystick/esp_hidd_prf_api.h create mode 100644 src/joystick/hid_dev.c create mode 100644 src/joystick/hid_dev.h create mode 100644 src/joystick/hid_device_le_prf.c create mode 100644 src/joystick/hidd_le_prf_int.h diff --git a/src/joystick/bt_joystick.c b/src/joystick/bt_joystick.c new file mode 100644 index 0000000..df303ae --- /dev/null +++ b/src/joystick/bt_joystick.c @@ -0,0 +1,205 @@ +#include +#include +#include +#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_hidd_prf_api.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 "driver/gpio.h" +#include "joystick/hid_dev.h" + +/** + * Brief: + * This example Implemented BLE HID device profile related functions, in which the HID device + * has 4 Reports (1 is mouse, 2 is keyboard and LED, 3 is Consumer Devices, 4 is Vendor devices). + * Users can choose different reports according to their own application scenarios. + * BLE HID profile inheritance and USB HID class. + */ + +/** + * Note: + * 1. Win10 does not support vendor report , So SUPPORT_REPORT_VENDOR is always set to FALSE, it defines in hidd_le_prf_int.h + * 2. Update connection parameters are not allowed during iPhone HID encryption, slave turns + * off the ability to automatically update connection parameters during encryption. + * 3. After our HID device is connected, the iPhones write 1 to the Report Characteristic Configuration Descriptor, + * even if the HID encryption is not completed. This should actually be written 1 after the HID encryption is completed. + * we modify the permissions of the Report Characteristic Configuration Descriptor to `ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE_ENCRYPTED`. + * if you got `GATT_INSUF_ENCRYPTION` error, please ignore. + */ + +#define HID_DEMO_TAG "HID_DEMO" + + +static uint16_t hid_conn_id = 0; +static bool sec_conn = false; +static bool send_volum_up = false; +#define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) + +static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param); + +#define HIDD_DEVICE_NAME "HID" +static uint8_t hidd_service_uuid128[] = { + /* LSB <--------------------------------------------------------------------------------> MSB */ + //first uuid, 16bit, [12],[13] is the value + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x12, 0x18, 0x00, 0x00, +}; + +static esp_ble_adv_data_t hidd_adv_data = { + .set_scan_rsp = false, + .include_name = true, + .include_txpower = true, + .min_interval = 0x0006, //slave connection min interval, Time = min_interval * 1.25 msec + .max_interval = 0x0010, //slave connection max interval, Time = max_interval * 1.25 msec + .appearance = 0x03c0, //HID Generic, + .manufacturer_len = 0, + .p_manufacturer_data = NULL, + .service_data_len = 0, + .p_service_data = NULL, + .service_uuid_len = sizeof(hidd_service_uuid128), + .p_service_uuid = hidd_service_uuid128, + .flag = 0x6, +}; + +static esp_ble_adv_params_t hidd_adv_params = { + .adv_int_min = 0x20, + .adv_int_max = 0x30, + .adv_type = ADV_TYPE_IND, + .own_addr_type = BLE_ADDR_TYPE_PUBLIC, + //.peer_addr = + //.peer_addr_type = + .channel_map = ADV_CHNL_ALL, + .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, +}; + + +static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param) +{ + switch(event) { + case ESP_HIDD_EVENT_REG_FINISH: { + if (param->init_finish.state == ESP_HIDD_INIT_OK) { + //esp_bd_addr_t rand_addr = {0x04,0x11,0x11,0x11,0x11,0x05}; + esp_ble_gap_set_device_name(HIDD_DEVICE_NAME); + esp_ble_gap_config_adv_data(&hidd_adv_data); + + } + break; + } + case ESP_BAT_EVENT_REG: { + break; + } + case ESP_HIDD_EVENT_DEINIT_FINISH: + break; + case ESP_HIDD_EVENT_BLE_CONNECT: { + ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_CONNECT"); + hid_conn_id = param->connect.conn_id; + break; + } + case ESP_HIDD_EVENT_BLE_DISCONNECT: { + sec_conn = false; + ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_DISCONNECT"); + esp_ble_gap_start_advertising(&hidd_adv_params); + break; + } + case ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT: { + ESP_LOGI(HID_DEMO_TAG, "%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", __func__); + ESP_LOG_BUFFER_HEX(HID_DEMO_TAG, param->vendor_write.data, param->vendor_write.length); + } + default: + break; + } + return; +} + +static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) +{ + switch (event) { + case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: + esp_ble_gap_start_advertising(&hidd_adv_params); + break; + case ESP_GAP_BLE_SEC_REQ_EVT: + for(int i = 0; i < ESP_BD_ADDR_LEN; i++) { + ESP_LOGD(HID_DEMO_TAG, "%x:",param->ble_security.ble_req.bd_addr[i]); + } + esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true); + break; + case ESP_GAP_BLE_AUTH_CMPL_EVT: + sec_conn = true; + esp_bd_addr_t bd_addr; + memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t)); + ESP_LOGI(HID_DEMO_TAG, "remote BD_ADDR: %08x%04x",\ + (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3], + (bd_addr[4] << 8) + bd_addr[5]); + ESP_LOGI(HID_DEMO_TAG, "address type = %d", param->ble_security.auth_cmpl.addr_type); + ESP_LOGI(HID_DEMO_TAG, "pair status = %s",param->ble_security.auth_cmpl.success ? "success" : "fail"); + if(!param->ble_security.auth_cmpl.success) { + ESP_LOGE(HID_DEMO_TAG, "fail reason = 0x%x",param->ble_security.auth_cmpl.fail_reason); + } + break; + default: + break; + } +} + +void hid_demo_task(void *pvParameters) +{ + vTaskDelay(1000 / portTICK_PERIOD_MS); + while(1) { + + //vTaskDelay(2000 / portTICK_PERIOD_MS); + if (sec_conn) { + esp_hidd_send_joystick_value(hid_conn_id, ) + + //uint8_t key_vaule = {HID_KEY_A}; + //esp_hidd_send_keyboard_value(hid_conn_id, 0, &key_vaule, 1); + esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_UP, true); + vTaskDelay(3000 / portTICK_PERIOD_MS); + if (send_volum_up) { + send_volum_up = false; + esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_UP, false); + esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_DOWN, true); + vTaskDelay(3000 / portTICK_PERIOD_MS); + esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_DOWN, false); + } + } + } +} + +#include "bt_joystick.h" + +void btjoyInit() +{ + ///register the callback function to the gap module + esp_ble_gap_register_callback(gap_event_handler); + esp_hidd_register_callbacks(hidd_event_callback); + + /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/ + esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND; //bonding with peer device after authentication + esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE; //set the IO capability to No output No input + uint8_t key_size = 16; //the key size should be 7~16 bytes + uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; + uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; + esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t)); + esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t)); + esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t)); + /* If your BLE device act as a Slave, the init_key means you hope which types of key of the master should distribute to you, + and the response key means which key you can distribute to the Master; + If your BLE device act as a master, the response key means you hope which types of key of the slave should distribute to you, + and the init key means which key you can distribute to the slave. */ + esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)); + esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)); + + xTaskCreate(&hid_demo_task, "hid_task", 2048, NULL, 5, NULL); +} \ No newline at end of file diff --git a/src/joystick/bt_joystick.h b/src/joystick/bt_joystick.h new file mode 100644 index 0000000..a7c0f94 --- /dev/null +++ b/src/joystick/bt_joystick.h @@ -0,0 +1,5 @@ +#pragma once + + +void btjoyInit(); + diff --git a/src/joystick/esp_hidd_prf_api.c b/src/joystick/esp_hidd_prf_api.c new file mode 100644 index 0000000..3e7079b --- /dev/null +++ b/src/joystick/esp_hidd_prf_api.c @@ -0,0 +1,240 @@ +// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with 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. + +#include "esp_hidd_prf_api.h" +#include "hidd_le_prf_int.h" +#include "hid_dev.h" +#include +#include +#include "esp_log.h" + +// HID keyboard input report length +#define HID_KEYBOARD_IN_RPT_LEN 8 + +// HID LED output report length +#define HID_LED_OUT_RPT_LEN 1 + +// HID mouse input report length +#define HID_MOUSE_IN_RPT_LEN 5 + +// HID consumer control input report length +#define HID_CC_IN_RPT_LEN 2 + +// HID gamepad input report length +#define HID_GAMEPAD_IN_RPT_LEN 6 + +esp_err_t esp_hidd_register_callbacks(esp_hidd_event_cb_t callbacks) +{ + esp_err_t hidd_status; + + if(callbacks != NULL) { + hidd_le_env.hidd_cb = callbacks; + } else { + return ESP_FAIL; + } + + if((hidd_status = hidd_register_cb()) != ESP_OK) { + return hidd_status; + } + + esp_ble_gatts_app_register(BATTRAY_APP_ID); + + if((hidd_status = esp_ble_gatts_app_register(HIDD_APP_ID)) != ESP_OK) { + return hidd_status; + } + + return hidd_status; +} + +esp_err_t esp_hidd_profile_init(void) +{ + if (hidd_le_env.enabled) { + ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized"); + return ESP_FAIL; + } + // Reset the hid device target environment + memset(&hidd_le_env, 0, sizeof(hidd_le_env_t)); + hidd_le_env.enabled = true; + return ESP_OK; +} + +esp_err_t esp_hidd_profile_deinit(void) +{ + uint16_t hidd_svc_hdl = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC]; + if (!hidd_le_env.enabled) { + ESP_LOGE(HID_LE_PRF_TAG, "HID device profile already initialized"); + return ESP_OK; + } + + if(hidd_svc_hdl != 0) { + esp_ble_gatts_stop_service(hidd_svc_hdl); + esp_ble_gatts_delete_service(hidd_svc_hdl); + } else { + return ESP_FAIL; + } + + /* register the HID device profile to the BTA_GATTS module*/ + esp_ble_gatts_app_unregister(hidd_le_env.gatt_if); + + return ESP_OK; +} + +uint16_t esp_hidd_get_version(void) +{ + return HIDD_VERSION; +} + +void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pressed) +{ + uint8_t buffer[HID_CC_IN_RPT_LEN] = {0, 0}; + if (key_pressed) { + ESP_LOGD(HID_LE_PRF_TAG, "hid_consumer_build_report"); + hid_consumer_build_report(buffer, key_cmd); + } + ESP_LOGD(HID_LE_PRF_TAG, "buffer[0] = %x, buffer[1] = %x", buffer[0], buffer[1]); + hid_dev_send_report(hidd_le_env.gatt_if, conn_id, + HID_RPT_ID_CC_IN, HID_REPORT_TYPE_INPUT, HID_CC_IN_RPT_LEN, buffer); + return; +} + +void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask, uint8_t *keyboard_cmd, uint8_t num_key) +{ + if (num_key > HID_KEYBOARD_IN_RPT_LEN - 2) { + ESP_LOGE(HID_LE_PRF_TAG, "%s(), the number key should not be more than %d", __func__, HID_KEYBOARD_IN_RPT_LEN); + return; + } + + uint8_t buffer[HID_KEYBOARD_IN_RPT_LEN] = {0}; + + buffer[0] = special_key_mask; + + for (int i = 0; i < num_key; i++) { + buffer[i+2] = keyboard_cmd[i]; + } + + ESP_LOGD(HID_LE_PRF_TAG, "the key vaule = %d,%d,%d, %d, %d, %d,%d, %d", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7]); + hid_dev_send_report(hidd_le_env.gatt_if, conn_id, + HID_RPT_ID_KEY_IN, HID_REPORT_TYPE_INPUT, HID_KEYBOARD_IN_RPT_LEN, buffer); + return; +} + +void esp_hidd_send_mouse_value(uint16_t conn_id, uint8_t mouse_button, int8_t mickeys_x, int8_t mickeys_y) +{ + uint8_t buffer[HID_MOUSE_IN_RPT_LEN]; + + buffer[0] = mouse_button; // Buttons + buffer[1] = mickeys_x; // X + buffer[2] = mickeys_y; // Y + buffer[3] = 0; // Wheel + buffer[4] = 0; // AC Pan + + hid_dev_send_report(hidd_le_env.gatt_if, conn_id, + HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, HID_MOUSE_IN_RPT_LEN, buffer); + return; +} + +// ------------------------------------- +// From - Head Tracker Code joystick.cpp + +#define JOYSTICK_BUTTONS +#define JOYSTICK_BUTTON_HIGH 1750 +#define JOYSTICK_BUTTON_LOW 1250 + +struct { +#ifdef JOYSTICK_BUTTONS + uint8_t but[2]; +#endif + uint16_t channels[8]; +} report; + +void set_JoystickChannels(uint16_t chans[16]) +{ + memcpy(report.channels, chans, sizeof(report.channels)); + +#ifdef JOYSTICK_BUTTONS + report.but[0] = 0; + report.but[1] = 0; +#endif + + for(int i=0; i < 8 ; i++) { + if(report.channels[i] == 0) // If disabled, center it + report.channels[i] = 1500; + +#ifdef JOYSTICK_BUTTONS + if(report.channels[i] >= JOYSTICK_BUTTON_HIGH) { + report.but[0] |= 1<<(i * 2); + report.but[1] |= 1<<((i - 4) * 2); + } + + if(report.channels[i] <= JOYSTICK_BUTTON_LOW) { + report.but[0] |= 1<<((i * 2) + 1); + report.but[1] |= 1<<(((i - 4) * 2) + 1); + } +#endif + + report.channels[i] -= 988; // Shift from center so it's 0-1024 + } + + hid_int_ep_write(hdev, (uint8_t*)&report, sizeof(report), NULL); +} + +static const uint8_t hid_report_desc[] = { + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x05, // USAGE (Game Pad) + 0xa1, 0x01, // COLLECTION (Application) + 0xa1, 0x00, // COLLECTION (Physical) +#ifdef JOYSTICK_BUTTONS + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x10, // USAGE_MAXIMUM (Button 8) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x95, 0x10, // REPORT_COUNT (8) + 0x75, 0x01, // REPORT_SIZE (1) + 0x81, 0x02, // INPUT (Data,Var,Abs) +#endif + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x09, 0x32, // USAGE (Z) + 0x09, 0x33, // USAGE (Rx) + 0x09, 0x34, // USAGE (Ry) + 0x09, 0x35, // USAGE (Rz) + 0x09, 0x36, // USAGE (Slider) + 0x09, 0x36, // USAGE (Slider) + 0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0) + 0x26, 0xFF, 0x03, // LOGICAL_MAXIMUM (1024) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x08, // REPORT_COUNT (8) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0xc0, // END_COLLECTION + 0xc0 // END_COLLECTION +}; + +void esp_hidd_send_joystick_value(uint16_t conn_id, uint16_t joystick_buttons, uint8_t joystick_x, uint8_t joystick_y, uint8_t joystick_z, uint8_t joystick_rx) +{ + uint8_t buffer[HID_GAMEPAD_IN_RPT_LEN]; + ESP_LOGD(HID_LE_PRF_TAG, "the buttons value = %d js1 = %d, %d js2 = %d, %d", joystick_buttons, joystick_x, joystick_y, joystick_z, joystick_rx); + + buffer[0]=joystick_buttons & 0xff; + buffer[1] = ( joystick_buttons >> 8); + buffer[2] = ( joystick_x ^ 0x80 ); // X + buffer[3] = (( joystick_y ^ 0x80 ) * -1) - 1; // Y + buffer[4] = ( joystick_z ^ 0x80 ); // X + buffer[5] = (( joystick_rx ^ 0x80 ) * -1) - 1; // Y + + hid_dev_send_report(hidd_le_env.gatt_if, conn_id, + HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, HID_GAMEPAD_IN_RPT_LEN, buffer); + return; +} \ No newline at end of file diff --git a/src/joystick/esp_hidd_prf_api.h b/src/joystick/esp_hidd_prf_api.h new file mode 100644 index 0000000..e7a1932 --- /dev/null +++ b/src/joystick/esp_hidd_prf_api.h @@ -0,0 +1,167 @@ +// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with 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. + +#ifndef __ESP_HIDD_API_H__ +#define __ESP_HIDD_API_H__ + +#include "esp_bt_defs.h" +#include "esp_gatt_defs.h" +#include "esp_err.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + ESP_HIDD_EVENT_REG_FINISH = 0, + ESP_BAT_EVENT_REG, + ESP_HIDD_EVENT_DEINIT_FINISH, + ESP_HIDD_EVENT_BLE_CONNECT, + ESP_HIDD_EVENT_BLE_DISCONNECT, + ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT, +} esp_hidd_cb_event_t; + +/// HID config status +typedef enum { + ESP_HIDD_STA_CONN_SUCCESS = 0x00, + ESP_HIDD_STA_CONN_FAIL = 0x01, +} esp_hidd_sta_conn_state_t; + +/// HID init status +typedef enum { + ESP_HIDD_INIT_OK = 0, + ESP_HIDD_INIT_FAILED = 1, +} esp_hidd_init_state_t; + +/// HID deinit status +typedef enum { + ESP_HIDD_DEINIT_OK = 0, + ESP_HIDD_DEINIT_FAILED = 0, +} esp_hidd_deinit_state_t; + +#define LEFT_CONTROL_KEY_MASK (1 << 0) +#define LEFT_SHIFT_KEY_MASK (1 << 1) +#define LEFT_ALT_KEY_MASK (1 << 2) +#define LEFT_GUI_KEY_MASK (1 << 3) +#define RIGHT_CONTROL_KEY_MASK (1 << 4) +#define RIGHT_SHIFT_KEY_MASK (1 << 5) +#define RIGHT_ALT_KEY_MASK (1 << 6) +#define RIGHT_GUI_KEY_MASK (1 << 7) + +typedef uint8_t key_mask_t; +/** + * @brief HIDD callback parameters union + */ +typedef union { + /** + * @brief ESP_HIDD_EVENT_INIT_FINISH + */ + struct hidd_init_finish_evt_param { + esp_hidd_init_state_t state; /*!< Initial status */ + esp_gatt_if_t gatts_if; + } init_finish; /*!< HID callback param of ESP_HIDD_EVENT_INIT_FINISH */ + + /** + * @brief ESP_HIDD_EVENT_DEINIT_FINISH + */ + struct hidd_deinit_finish_evt_param { + esp_hidd_deinit_state_t state; /*!< De-initial status */ + } deinit_finish; /*!< HID callback param of ESP_HIDD_EVENT_DEINIT_FINISH */ + + /** + * @brief ESP_HIDD_EVENT_CONNECT + */ + struct hidd_connect_evt_param { + uint16_t conn_id; + esp_bd_addr_t remote_bda; /*!< HID Remote bluetooth connection index */ + } connect; /*!< HID callback param of ESP_HIDD_EVENT_CONNECT */ + + /** + * @brief ESP_HIDD_EVENT_DISCONNECT + */ + struct hidd_disconnect_evt_param { + esp_bd_addr_t remote_bda; /*!< HID Remote bluetooth device address */ + } disconnect; /*!< HID callback param of ESP_HIDD_EVENT_DISCONNECT */ + + /** + * @brief ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT + */ + struct hidd_vendor_write_evt_param { + uint16_t conn_id; /*!< HID connection index */ + uint16_t report_id; /*!< HID report index */ + uint16_t length; /*!< data length */ + uint8_t *data; /*!< The pointer to the data */ + } vendor_write; /*!< HID callback param of ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT */ + +} esp_hidd_cb_param_t; + + +/** + * @brief HID device event callback function type + * @param event : Event type + * @param param : Point to callback parameter, currently is union type + */ +typedef void (*esp_hidd_event_cb_t) (esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param); + + + +/** + * + * @brief This function is called to receive hid device callback event + * + * @param[in] callbacks: callback functions + * + * @return ESP_OK - success, other - failed + * + */ +esp_err_t esp_hidd_register_callbacks(esp_hidd_event_cb_t callbacks); + +/** + * + * @brief This function is called to initialize hid device profile + * + * @return ESP_OK - success, other - failed + * + */ +esp_err_t esp_hidd_profile_init(void); + +/** + * + * @brief This function is called to de-initialize hid device profile + * + * @return ESP_OK - success, other - failed + * + */ +esp_err_t esp_hidd_profile_deinit(void); + +/** + * + * @brief Get hidd profile version + * + * @return Most 8bit significant is Great version, Least 8bit is Sub version + * + */ +uint16_t esp_hidd_get_version(void); + +void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pressed); + +void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask, uint8_t *keyboard_cmd, uint8_t num_key); + +void esp_hidd_send_mouse_value(uint16_t conn_id, uint8_t mouse_button, int8_t mickeys_x, int8_t mickeys_y); + +#ifdef __cplusplus +} +#endif + +#endif /* __ESP_HIDD_API_H__ */ diff --git a/src/joystick/hid_dev.c b/src/joystick/hid_dev.c new file mode 100644 index 0000000..92d3572 --- /dev/null +++ b/src/joystick/hid_dev.c @@ -0,0 +1,137 @@ +// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with 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. + +#include "hid_dev.h" +#include +#include +#include +#include +#include "esp_log.h" + +static hid_report_map_t *hid_dev_rpt_tbl; +static uint8_t hid_dev_rpt_tbl_Len; + +static hid_report_map_t *hid_dev_rpt_by_id(uint8_t id, uint8_t type) +{ + hid_report_map_t *rpt = hid_dev_rpt_tbl; + + for (uint8_t i = hid_dev_rpt_tbl_Len; i > 0; i--, rpt++) { + if (rpt->id == id && rpt->type == type && rpt->mode == hidProtocolMode) { + return rpt; + } + } + + return NULL; +} + +void hid_dev_register_reports(uint8_t num_reports, hid_report_map_t *p_report) +{ + hid_dev_rpt_tbl = p_report; + hid_dev_rpt_tbl_Len = num_reports; + return; +} + +void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id, + uint8_t id, uint8_t type, uint8_t length, uint8_t *data) +{ + hid_report_map_t *p_rpt; + + // get att handle for report + if ((p_rpt = hid_dev_rpt_by_id(id, type)) != NULL) { + // if notifications are enabled + ESP_LOGD(HID_LE_PRF_TAG, "%s(), send the report, handle = %d", __func__, p_rpt->handle); + esp_ble_gatts_send_indicate(gatts_if, conn_id, p_rpt->handle, length, data, false); + } + + return; +} + +void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd) +{ + if (!buffer) { + ESP_LOGE(HID_LE_PRF_TAG, "%s(), the buffer is NULL, hid build report failed.", __func__); + return; + } + + switch (cmd) { + case HID_CONSUMER_CHANNEL_UP: + HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_UP); + break; + + case HID_CONSUMER_CHANNEL_DOWN: + HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_DOWN); + break; + + case HID_CONSUMER_VOLUME_UP: + HID_CC_RPT_SET_VOLUME_UP(buffer); + break; + + case HID_CONSUMER_VOLUME_DOWN: + HID_CC_RPT_SET_VOLUME_DOWN(buffer); + break; + + case HID_CONSUMER_MUTE: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_MUTE); + break; + + case HID_CONSUMER_POWER: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_POWER); + break; + + case HID_CONSUMER_RECALL_LAST: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_LAST); + break; + + case HID_CONSUMER_ASSIGN_SEL: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_ASSIGN_SEL); + break; + + case HID_CONSUMER_PLAY: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PLAY); + break; + + case HID_CONSUMER_PAUSE: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PAUSE); + break; + + case HID_CONSUMER_RECORD: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_RECORD); + break; + + case HID_CONSUMER_FAST_FORWARD: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_FAST_FWD); + break; + + case HID_CONSUMER_REWIND: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_REWIND); + break; + + case HID_CONSUMER_SCAN_NEXT_TRK: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_NEXT_TRK); + break; + + case HID_CONSUMER_SCAN_PREV_TRK: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_PREV_TRK); + break; + + case HID_CONSUMER_STOP: + HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_STOP); + break; + + default: + break; + } + + return; +} diff --git a/src/joystick/hid_dev.h b/src/joystick/hid_dev.h new file mode 100644 index 0000000..d41b872 --- /dev/null +++ b/src/joystick/hid_dev.h @@ -0,0 +1,260 @@ +// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with 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. + +#ifndef HID_DEV_H__ +#define HID_DEV_H__ + +#include "hidd_le_prf_int.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/* HID Report type */ +#define HID_TYPE_INPUT 1 +#define HID_TYPE_OUTPUT 2 +#define HID_TYPE_FEATURE 3 + +// HID Keyboard/Keypad Usage IDs (subset of the codes available in the USB HID Usage Tables spec) +#define HID_KEY_RESERVED 0 // No event inidicated +#define HID_KEY_A 4 // Keyboard a and A +#define HID_KEY_B 5 // Keyboard b and B +#define HID_KEY_C 6 // Keyboard c and C +#define HID_KEY_D 7 // Keyboard d and D +#define HID_KEY_E 8 // Keyboard e and E +#define HID_KEY_F 9 // Keyboard f and F +#define HID_KEY_G 10 // Keyboard g and G +#define HID_KEY_H 11 // Keyboard h and H +#define HID_KEY_I 12 // Keyboard i and I +#define HID_KEY_J 13 // Keyboard j and J +#define HID_KEY_K 14 // Keyboard k and K +#define HID_KEY_L 15 // Keyboard l and L +#define HID_KEY_M 16 // Keyboard m and M +#define HID_KEY_N 17 // Keyboard n and N +#define HID_KEY_O 18 // Keyboard o and O +#define HID_KEY_P 19 // Keyboard p and p +#define HID_KEY_Q 20 // Keyboard q and Q +#define HID_KEY_R 21 // Keyboard r and R +#define HID_KEY_S 22 // Keyboard s and S +#define HID_KEY_T 23 // Keyboard t and T +#define HID_KEY_U 24 // Keyboard u and U +#define HID_KEY_V 25 // Keyboard v and V +#define HID_KEY_W 26 // Keyboard w and W +#define HID_KEY_X 27 // Keyboard x and X +#define HID_KEY_Y 28 // Keyboard y and Y +#define HID_KEY_Z 29 // Keyboard z and Z +#define HID_KEY_1 30 // Keyboard 1 and ! +#define HID_KEY_2 31 // Keyboard 2 and @ +#define HID_KEY_3 32 // Keyboard 3 and # +#define HID_KEY_4 33 // Keyboard 4 and % +#define HID_KEY_5 34 // Keyboard 5 and % +#define HID_KEY_6 35 // Keyboard 6 and ^ +#define HID_KEY_7 36 // Keyboard 7 and & +#define HID_KEY_8 37 // Keyboard 8 and * +#define HID_KEY_9 38 // Keyboard 9 and ( +#define HID_KEY_0 39 // Keyboard 0 and ) +#define HID_KEY_RETURN 40 // Keyboard Return (ENTER) +#define HID_KEY_ESCAPE 41 // Keyboard ESCAPE +#define HID_KEY_DELETE 42 // Keyboard DELETE (Backspace) +#define HID_KEY_TAB 43 // Keyboard Tab +#define HID_KEY_SPACEBAR 44 // Keyboard Spacebar +#define HID_KEY_MINUS 45 // Keyboard - and (underscore) +#define HID_KEY_EQUAL 46 // Keyboard = and + +#define HID_KEY_LEFT_BRKT 47 // Keyboard [ and { +#define HID_KEY_RIGHT_BRKT 48 // Keyboard ] and } +#define HID_KEY_BACK_SLASH 49 // Keyboard \ and | +#define HID_KEY_SEMI_COLON 51 // Keyboard ; and : +#define HID_KEY_SGL_QUOTE 52 // Keyboard ' and " +#define HID_KEY_GRV_ACCENT 53 // Keyboard Grave Accent and Tilde +#define HID_KEY_COMMA 54 // Keyboard , and < +#define HID_KEY_DOT 55 // Keyboard . and > +#define HID_KEY_FWD_SLASH 56 // Keyboard / and ? +#define HID_KEY_CAPS_LOCK 57 // Keyboard Caps Lock +#define HID_KEY_F1 58 // Keyboard F1 +#define HID_KEY_F2 59 // Keyboard F2 +#define HID_KEY_F3 60 // Keyboard F3 +#define HID_KEY_F4 61 // Keyboard F4 +#define HID_KEY_F5 62 // Keyboard F5 +#define HID_KEY_F6 63 // Keyboard F6 +#define HID_KEY_F7 64 // Keyboard F7 +#define HID_KEY_F8 65 // Keyboard F8 +#define HID_KEY_F9 66 // Keyboard F9 +#define HID_KEY_F10 67 // Keyboard F10 +#define HID_KEY_F11 68 // Keyboard F11 +#define HID_KEY_F12 69 // Keyboard F12 +#define HID_KEY_PRNT_SCREEN 70 // Keyboard Print Screen +#define HID_KEY_SCROLL_LOCK 71 // Keyboard Scroll Lock +#define HID_KEY_PAUSE 72 // Keyboard Pause +#define HID_KEY_INSERT 73 // Keyboard Insert +#define HID_KEY_HOME 74 // Keyboard Home +#define HID_KEY_PAGE_UP 75 // Keyboard PageUp +#define HID_KEY_DELETE_FWD 76 // Keyboard Delete Forward +#define HID_KEY_END 77 // Keyboard End +#define HID_KEY_PAGE_DOWN 78 // Keyboard PageDown +#define HID_KEY_RIGHT_ARROW 79 // Keyboard RightArrow +#define HID_KEY_LEFT_ARROW 80 // Keyboard LeftArrow +#define HID_KEY_DOWN_ARROW 81 // Keyboard DownArrow +#define HID_KEY_UP_ARROW 82 // Keyboard UpArrow +#define HID_KEY_NUM_LOCK 83 // Keypad Num Lock and Clear +#define HID_KEY_DIVIDE 84 // Keypad / +#define HID_KEY_MULTIPLY 85 // Keypad * +#define HID_KEY_SUBTRACT 86 // Keypad - +#define HID_KEY_ADD 87 // Keypad + +#define HID_KEY_ENTER 88 // Keypad ENTER +#define HID_KEYPAD_1 89 // Keypad 1 and End +#define HID_KEYPAD_2 90 // Keypad 2 and Down Arrow +#define HID_KEYPAD_3 91 // Keypad 3 and PageDn +#define HID_KEYPAD_4 92 // Keypad 4 and Lfet Arrow +#define HID_KEYPAD_5 93 // Keypad 5 +#define HID_KEYPAD_6 94 // Keypad 6 and Right Arrow +#define HID_KEYPAD_7 95 // Keypad 7 and Home +#define HID_KEYPAD_8 96 // Keypad 8 and Up Arrow +#define HID_KEYPAD_9 97 // Keypad 9 and PageUp +#define HID_KEYPAD_0 98 // Keypad 0 and Insert +#define HID_KEYPAD_DOT 99 // Keypad . and Delete +#define HID_KEY_MUTE 127 // Keyboard Mute +#define HID_KEY_VOLUME_UP 128 // Keyboard Volume up +#define HID_KEY_VOLUME_DOWN 129 // Keyboard Volume down +#define HID_KEY_LEFT_CTRL 224 // Keyboard LeftContorl +#define HID_KEY_LEFT_SHIFT 225 // Keyboard LeftShift +#define HID_KEY_LEFT_ALT 226 // Keyboard LeftAlt +#define HID_KEY_LEFT_GUI 227 // Keyboard LeftGUI +#define HID_KEY_RIGHT_CTRL 228 // Keyboard RightContorl +#define HID_KEY_RIGHT_SHIFT 229 // Keyboard RightShift +#define HID_KEY_RIGHT_ALT 230 // Keyboard RightAlt +#define HID_KEY_RIGHT_GUI 231 // Keyboard RightGUI +typedef uint8_t keyboard_cmd_t; + +#define HID_MOUSE_LEFT 253 +#define HID_MOUSE_MIDDLE 254 +#define HID_MOUSE_RIGHT 255 +typedef uint8_t mouse_cmd_t; + +// HID Consumer Usage IDs (subset of the codes available in the USB HID Usage Tables spec) +#define HID_CONSUMER_POWER 48 // Power +#define HID_CONSUMER_RESET 49 // Reset +#define HID_CONSUMER_SLEEP 50 // Sleep + +#define HID_CONSUMER_MENU 64 // Menu +#define HID_CONSUMER_SELECTION 128 // Selection +#define HID_CONSUMER_ASSIGN_SEL 129 // Assign Selection +#define HID_CONSUMER_MODE_STEP 130 // Mode Step +#define HID_CONSUMER_RECALL_LAST 131 // Recall Last +#define HID_CONSUMER_QUIT 148 // Quit +#define HID_CONSUMER_HELP 149 // Help +#define HID_CONSUMER_CHANNEL_UP 156 // Channel Increment +#define HID_CONSUMER_CHANNEL_DOWN 157 // Channel Decrement + +#define HID_CONSUMER_PLAY 176 // Play +#define HID_CONSUMER_PAUSE 177 // Pause +#define HID_CONSUMER_RECORD 178 // Record +#define HID_CONSUMER_FAST_FORWARD 179 // Fast Forward +#define HID_CONSUMER_REWIND 180 // Rewind +#define HID_CONSUMER_SCAN_NEXT_TRK 181 // Scan Next Track +#define HID_CONSUMER_SCAN_PREV_TRK 182 // Scan Previous Track +#define HID_CONSUMER_STOP 183 // Stop +#define HID_CONSUMER_EJECT 184 // Eject +#define HID_CONSUMER_RANDOM_PLAY 185 // Random Play +#define HID_CONSUMER_SELECT_DISC 186 // Select Disk +#define HID_CONSUMER_ENTER_DISC 187 // Enter Disc +#define HID_CONSUMER_REPEAT 188 // Repeat +#define HID_CONSUMER_STOP_EJECT 204 // Stop/Eject +#define HID_CONSUMER_PLAY_PAUSE 205 // Play/Pause +#define HID_CONSUMER_PLAY_SKIP 206 // Play/Skip + +#define HID_CONSUMER_VOLUME 224 // Volume +#define HID_CONSUMER_BALANCE 225 // Balance +#define HID_CONSUMER_MUTE 226 // Mute +#define HID_CONSUMER_BASS 227 // Bass +#define HID_CONSUMER_VOLUME_UP 233 // Volume Increment +#define HID_CONSUMER_VOLUME_DOWN 234 // Volume Decrement +typedef uint8_t consumer_cmd_t; + +#define HID_CC_RPT_MUTE 1 +#define HID_CC_RPT_POWER 2 +#define HID_CC_RPT_LAST 3 +#define HID_CC_RPT_ASSIGN_SEL 4 +#define HID_CC_RPT_PLAY 5 +#define HID_CC_RPT_PAUSE 6 +#define HID_CC_RPT_RECORD 7 +#define HID_CC_RPT_FAST_FWD 8 +#define HID_CC_RPT_REWIND 9 +#define HID_CC_RPT_SCAN_NEXT_TRK 10 +#define HID_CC_RPT_SCAN_PREV_TRK 11 +#define HID_CC_RPT_STOP 12 + +#define HID_CC_RPT_CHANNEL_UP 0x01 +#define HID_CC_RPT_CHANNEL_DOWN 0x03 +#define HID_CC_RPT_VOLUME_UP 0x40 +#define HID_CC_RPT_VOLUME_DOWN 0x80 + +// HID Consumer Control report bitmasks +#define HID_CC_RPT_NUMERIC_BITS 0xF0 +#define HID_CC_RPT_CHANNEL_BITS 0xCF +#define HID_CC_RPT_VOLUME_BITS 0x3F +#define HID_CC_RPT_BUTTON_BITS 0xF0 +#define HID_CC_RPT_SELECTION_BITS 0xCF + + +// Macros for the HID Consumer Control 2-byte report +#define HID_CC_RPT_SET_NUMERIC(s, x) (s)[0] &= HID_CC_RPT_NUMERIC_BITS; \ + (s)[0] = (x) +#define HID_CC_RPT_SET_CHANNEL(s, x) (s)[0] &= HID_CC_RPT_CHANNEL_BITS; \ + (s)[0] |= ((x) & 0x03) << 4 +#define HID_CC_RPT_SET_VOLUME_UP(s) (s)[0] &= HID_CC_RPT_VOLUME_BITS; \ + (s)[0] |= 0x40 +#define HID_CC_RPT_SET_VOLUME_DOWN(s) (s)[0] &= HID_CC_RPT_VOLUME_BITS; \ + (s)[0] |= 0x80 +#define HID_CC_RPT_SET_BUTTON(s, x) (s)[1] &= HID_CC_RPT_BUTTON_BITS; \ + (s)[1] |= (x) +#define HID_CC_RPT_SET_SELECTION(s, x) (s)[1] &= HID_CC_RPT_SELECTION_BITS; \ + (s)[1] |= ((x) & 0x03) << 4 + + +// HID report mapping table +typedef struct +{ + uint16_t handle; // Handle of report characteristic + uint16_t cccdHandle; // Handle of CCCD for report characteristic + uint8_t id; // Report ID + uint8_t type; // Report type + uint8_t mode; // Protocol mode (report or boot) +} hid_report_map_t; + +// HID dev configuration structure +typedef struct +{ + uint32_t idleTimeout; // Idle timeout in milliseconds + uint8_t hidFlags; // HID feature flags + +} hid_dev_cfg_t; + +void hid_dev_register_reports(uint8_t num_reports, hid_report_map_t *p_report); + +void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id, + uint8_t id, uint8_t type, uint8_t length, uint8_t *data); + +void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd); + +void hid_keyboard_build_report(uint8_t *buffer, keyboard_cmd_t cmd); + +void hid_mouse_build_report(uint8_t *buffer, mouse_cmd_t cmd); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* HID_DEV_H__ */ diff --git a/src/joystick/hid_device_le_prf.c b/src/joystick/hid_device_le_prf.c new file mode 100644 index 0000000..db697b0 --- /dev/null +++ b/src/joystick/hid_device_le_prf.c @@ -0,0 +1,815 @@ +// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with 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. + +#include "hidd_le_prf_int.h" +#include +#include "esp_log.h" + +/// characteristic presentation information +struct prf_char_pres_fmt +{ + /// Unit (The Unit is a UUID) + uint16_t unit; + /// Description + uint16_t description; + /// Format + uint8_t format; + /// Exponent + uint8_t exponent; + /// Name space + uint8_t name_space; +}; + +// HID report mapping table +static hid_report_map_t hid_rpt_map[HID_NUM_REPORTS]; + +// HID Report Map characteristic value +// Keyboard report descriptor (using format for Boot interface descriptor) +static const uint8_t hidReportMap[] = { + 0x05, 0x01, // Usage Page (Generic Desktop) + 0x09, 0x02, // Usage (Mouse) + 0xA1, 0x01, // Collection (Application) + 0x85, 0x01, // Report Id (1) + 0x09, 0x01, // Usage (Pointer) + 0xA1, 0x00, // Collection (Physical) + 0x05, 0x09, // Usage Page (Buttons) + 0x19, 0x01, // Usage Minimum (01) - Button 1 + 0x29, 0x03, // Usage Maximum (03) - Button 3 + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x01, // Logical Maximum (1) + 0x75, 0x01, // Report Size (1) + 0x95, 0x03, // Report Count (3) + 0x81, 0x02, // Input (Data, Variable, Absolute) - Button states + 0x75, 0x05, // Report Size (5) + 0x95, 0x01, // Report Count (1) + 0x81, 0x01, // Input (Constant) - Padding or Reserved bits + 0x05, 0x01, // Usage Page (Generic Desktop) + 0x09, 0x30, // Usage (X) + 0x09, 0x31, // Usage (Y) + 0x09, 0x38, // Usage (Wheel) + 0x15, 0x81, // Logical Minimum (-127) + 0x25, 0x7F, // Logical Maximum (127) + 0x75, 0x08, // Report Size (8) + 0x95, 0x03, // Report Count (3) + 0x81, 0x06, // Input (Data, Variable, Relative) - X & Y coordinate + 0xC0, // End Collection + 0xC0, // End Collection + + 0x05, 0x01, // Usage Pg (Generic Desktop) + 0x09, 0x06, // Usage (Keyboard) + 0xA1, 0x01, // Collection: (Application) + 0x85, 0x02, // Report Id (2) + // + 0x05, 0x07, // Usage Pg (Key Codes) + 0x19, 0xE0, // Usage Min (224) + 0x29, 0xE7, // Usage Max (231) + 0x15, 0x00, // Log Min (0) + 0x25, 0x01, // Log Max (1) + // + // Modifier byte + 0x75, 0x01, // Report Size (1) + 0x95, 0x08, // Report Count (8) + 0x81, 0x02, // Input: (Data, Variable, Absolute) + // + // Reserved byte + 0x95, 0x01, // Report Count (1) + 0x75, 0x08, // Report Size (8) + 0x81, 0x01, // Input: (Constant) + // + // LED report + 0x95, 0x05, // Report Count (5) + 0x75, 0x01, // Report Size (1) + 0x05, 0x08, // Usage Pg (LEDs) + 0x19, 0x01, // Usage Min (1) + 0x29, 0x05, // Usage Max (5) + 0x91, 0x02, // Output: (Data, Variable, Absolute) + // + // LED report padding + 0x95, 0x01, // Report Count (1) + 0x75, 0x03, // Report Size (3) + 0x91, 0x01, // Output: (Constant) + // + // Key arrays (6 bytes) + 0x95, 0x06, // Report Count (6) + 0x75, 0x08, // Report Size (8) + 0x15, 0x00, // Log Min (0) + 0x25, 0x65, // Log Max (101) + 0x05, 0x07, // Usage Pg (Key Codes) + 0x19, 0x00, // Usage Min (0) + 0x29, 0x65, // Usage Max (101) + 0x81, 0x00, // Input: (Data, Array) + // + 0xC0, // End Collection + // + 0x05, 0x0C, // Usage Pg (Consumer Devices) + 0x09, 0x01, // Usage (Consumer Control) + 0xA1, 0x01, // Collection (Application) + 0x85, 0x03, // Report Id (3) + 0x09, 0x02, // Usage (Numeric Key Pad) + 0xA1, 0x02, // Collection (Logical) + 0x05, 0x09, // Usage Pg (Button) + 0x19, 0x01, // Usage Min (Button 1) + 0x29, 0x0A, // Usage Max (Button 10) + 0x15, 0x01, // Logical Min (1) + 0x25, 0x0A, // Logical Max (10) + 0x75, 0x04, // Report Size (4) + 0x95, 0x01, // Report Count (1) + 0x81, 0x00, // Input (Data, Ary, Abs) + 0xC0, // End Collection + 0x05, 0x0C, // Usage Pg (Consumer Devices) + 0x09, 0x86, // Usage (Channel) + 0x15, 0xFF, // Logical Min (-1) + 0x25, 0x01, // Logical Max (1) + 0x75, 0x02, // Report Size (2) + 0x95, 0x01, // Report Count (1) + 0x81, 0x46, // Input (Data, Var, Rel, Null) + 0x09, 0xE9, // Usage (Volume Up) + 0x09, 0xEA, // Usage (Volume Down) + 0x15, 0x00, // Logical Min (0) + 0x75, 0x01, // Report Size (1) + 0x95, 0x02, // Report Count (2) + 0x81, 0x02, // Input (Data, Var, Abs) + 0x09, 0xE2, // Usage (Mute) + 0x09, 0x30, // Usage (Power) + 0x09, 0x83, // Usage (Recall Last) + 0x09, 0x81, // Usage (Assign Selection) + 0x09, 0xB0, // Usage (Play) + 0x09, 0xB1, // Usage (Pause) + 0x09, 0xB2, // Usage (Record) + 0x09, 0xB3, // Usage (Fast Forward) + 0x09, 0xB4, // Usage (Rewind) + 0x09, 0xB5, // Usage (Scan Next) + 0x09, 0xB6, // Usage (Scan Prev) + 0x09, 0xB7, // Usage (Stop) + 0x15, 0x01, // Logical Min (1) + 0x25, 0x0C, // Logical Max (12) + 0x75, 0x04, // Report Size (4) + 0x95, 0x01, // Report Count (1) + 0x81, 0x00, // Input (Data, Ary, Abs) + 0x09, 0x80, // Usage (Selection) + 0xA1, 0x02, // Collection (Logical) + 0x05, 0x09, // Usage Pg (Button) + 0x19, 0x01, // Usage Min (Button 1) + 0x29, 0x03, // Usage Max (Button 3) + 0x15, 0x01, // Logical Min (1) + 0x25, 0x03, // Logical Max (3) + 0x75, 0x02, // Report Size (2) + 0x81, 0x00, // Input (Data, Ary, Abs) + 0xC0, // End Collection + 0x81, 0x03, // Input (Const, Var, Abs) + 0xC0, // End Collectionq + +#if (SUPPORT_REPORT_VENDOR == true) + 0x06, 0xFF, 0xFF, // Usage Page(Vendor defined) + 0x09, 0xA5, // Usage(Vendor Defined) + 0xA1, 0x01, // Collection(Application) + 0x85, 0x04, // Report Id (4) + 0x09, 0xA6, // Usage(Vendor defined) + 0x09, 0xA9, // Usage(Vendor defined) + 0x75, 0x08, // Report Size + 0x95, 0x7F, // Report Count = 127 Btyes + 0x91, 0x02, // Output(Data, Variable, Absolute) + 0xC0, // End Collection +#endif + +}; + +/// Battery Service Attributes Indexes +enum +{ + BAS_IDX_SVC, + + BAS_IDX_BATT_LVL_CHAR, + BAS_IDX_BATT_LVL_VAL, + BAS_IDX_BATT_LVL_NTF_CFG, + BAS_IDX_BATT_LVL_PRES_FMT, + + BAS_IDX_NB, +}; + +#define HI_UINT16(a) (((a) >> 8) & 0xFF) +#define LO_UINT16(a) ((a) & 0xFF) +#define PROFILE_NUM 1 +#define PROFILE_APP_IDX 0 + +struct gatts_profile_inst { + esp_gatts_cb_t gatts_cb; + uint16_t gatts_if; + uint16_t app_id; + uint16_t conn_id; +}; + +hidd_le_env_t hidd_le_env; + +// HID report map length +uint8_t hidReportMapLen = sizeof(hidReportMap); +uint8_t hidProtocolMode = HID_PROTOCOL_MODE_REPORT; + +// HID report mapping table +//static hidRptMap_t hidRptMap[HID_NUM_REPORTS]; + +// HID Information characteristic value +static const uint8_t hidInfo[HID_INFORMATION_LEN] = { + LO_UINT16(0x0111), HI_UINT16(0x0111), // bcdHID (USB HID version) + 0x00, // bCountryCode + HID_KBD_FLAGS // Flags +}; + + +// HID External Report Reference Descriptor +static uint16_t hidExtReportRefDesc = ESP_GATT_UUID_BATTERY_LEVEL; + +// HID Report Reference characteristic descriptor, mouse input +static uint8_t hidReportRefMouseIn[HID_REPORT_REF_LEN] = + { HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT }; + + +// HID Report Reference characteristic descriptor, key input +static uint8_t hidReportRefKeyIn[HID_REPORT_REF_LEN] = + { HID_RPT_ID_KEY_IN, HID_REPORT_TYPE_INPUT }; + +// HID Report Reference characteristic descriptor, LED output +static uint8_t hidReportRefLedOut[HID_REPORT_REF_LEN] = + { HID_RPT_ID_LED_OUT, HID_REPORT_TYPE_OUTPUT }; + +#if (SUPPORT_REPORT_VENDOR == true) + +static uint8_t hidReportRefVendorOut[HID_REPORT_REF_LEN] = + {HID_RPT_ID_VENDOR_OUT, HID_REPORT_TYPE_OUTPUT}; +#endif + +// HID Report Reference characteristic descriptor, Feature +static uint8_t hidReportRefFeature[HID_REPORT_REF_LEN] = + { HID_RPT_ID_FEATURE, HID_REPORT_TYPE_FEATURE }; + +// HID Report Reference characteristic descriptor, consumer control input +static uint8_t hidReportRefCCIn[HID_REPORT_REF_LEN] = + { HID_RPT_ID_CC_IN, HID_REPORT_TYPE_INPUT }; + + +/* + * Heart Rate PROFILE ATTRIBUTES + **************************************************************************************** + */ + +/// hid Service uuid +static uint16_t hid_le_svc = ATT_SVC_HID; +uint16_t hid_count = 0; +esp_gatts_incl_svc_desc_t incl_svc = {0}; + +#define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) +///the uuid definition +static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE; +static const uint16_t include_service_uuid = ESP_GATT_UUID_INCLUDE_SERVICE; +static const uint16_t character_declaration_uuid = ESP_GATT_UUID_CHAR_DECLARE; +static const uint16_t character_client_config_uuid = ESP_GATT_UUID_CHAR_CLIENT_CONFIG; +static const uint16_t hid_info_char_uuid = ESP_GATT_UUID_HID_INFORMATION; +static const uint16_t hid_report_map_uuid = ESP_GATT_UUID_HID_REPORT_MAP; +static const uint16_t hid_control_point_uuid = ESP_GATT_UUID_HID_CONTROL_POINT; +static const uint16_t hid_report_uuid = ESP_GATT_UUID_HID_REPORT; +static const uint16_t hid_proto_mode_uuid = ESP_GATT_UUID_HID_PROTO_MODE; +static const uint16_t hid_kb_input_uuid = ESP_GATT_UUID_HID_BT_KB_INPUT; +static const uint16_t hid_kb_output_uuid = ESP_GATT_UUID_HID_BT_KB_OUTPUT; +static const uint16_t hid_mouse_input_uuid = ESP_GATT_UUID_HID_BT_MOUSE_INPUT; +static const uint16_t hid_repot_map_ext_desc_uuid = ESP_GATT_UUID_EXT_RPT_REF_DESCR; +static const uint16_t hid_report_ref_descr_uuid = ESP_GATT_UUID_RPT_REF_DESCR; +///the propoty definition +static const uint8_t char_prop_notify = ESP_GATT_CHAR_PROP_BIT_NOTIFY; +static const uint8_t char_prop_read = ESP_GATT_CHAR_PROP_BIT_READ; +static const uint8_t char_prop_write_nr = ESP_GATT_CHAR_PROP_BIT_WRITE_NR; +static const uint8_t char_prop_read_write = ESP_GATT_CHAR_PROP_BIT_WRITE|ESP_GATT_CHAR_PROP_BIT_READ; +static const uint8_t char_prop_read_notify = ESP_GATT_CHAR_PROP_BIT_READ|ESP_GATT_CHAR_PROP_BIT_NOTIFY; +static const uint8_t char_prop_read_write_notify = ESP_GATT_CHAR_PROP_BIT_READ|ESP_GATT_CHAR_PROP_BIT_WRITE|ESP_GATT_CHAR_PROP_BIT_NOTIFY; + +/// battary Service +static const uint16_t battary_svc = ESP_GATT_UUID_BATTERY_SERVICE_SVC; + +static const uint16_t bat_lev_uuid = ESP_GATT_UUID_BATTERY_LEVEL; +static const uint8_t bat_lev_ccc[2] ={ 0x00, 0x00}; +static const uint16_t char_format_uuid = ESP_GATT_UUID_CHAR_PRESENT_FORMAT; + +static uint8_t battary_lev = 50; +/// Full HRS Database Description - Used to add attributes into the database +static const esp_gatts_attr_db_t bas_att_db[BAS_IDX_NB] = +{ + // Battary Service Declaration + [BAS_IDX_SVC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, + sizeof(uint16_t), sizeof(battary_svc), (uint8_t *)&battary_svc}}, + + // Battary level Characteristic Declaration + [BAS_IDX_BATT_LVL_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_notify}}, + + // Battary level Characteristic Value + [BAS_IDX_BATT_LVL_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&bat_lev_uuid, ESP_GATT_PERM_READ, + sizeof(uint8_t),sizeof(uint8_t), &battary_lev}}, + + // Battary level Characteristic - Client Characteristic Configuration Descriptor + [BAS_IDX_BATT_LVL_NTF_CFG] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, + sizeof(uint16_t),sizeof(bat_lev_ccc), (uint8_t *)bat_lev_ccc}}, + + // Battary level report Characteristic Declaration + [BAS_IDX_BATT_LVL_PRES_FMT] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&char_format_uuid, ESP_GATT_PERM_READ, + sizeof(struct prf_char_pres_fmt), 0, NULL}}, +}; + + +/// Full Hid device Database Description - Used to add attributes into the database +static esp_gatts_attr_db_t hidd_le_gatt_db[HIDD_LE_IDX_NB] = +{ + // HID Service Declaration + [HIDD_LE_IDX_SVC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, + ESP_GATT_PERM_READ_ENCRYPTED, sizeof(uint16_t), sizeof(hid_le_svc), + (uint8_t *)&hid_le_svc}}, + + // HID Service Declaration + [HIDD_LE_IDX_INCL_SVC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&include_service_uuid, + ESP_GATT_PERM_READ, + sizeof(esp_gatts_incl_svc_desc_t), sizeof(esp_gatts_incl_svc_desc_t), + (uint8_t *)&incl_svc}}, + + // HID Information Characteristic Declaration + [HIDD_LE_IDX_HID_INFO_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read}}, + // HID Information Characteristic Value + [HIDD_LE_IDX_HID_INFO_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_info_char_uuid, + ESP_GATT_PERM_READ, + sizeof(hids_hid_info_t), sizeof(hidInfo), + (uint8_t *)&hidInfo}}, + + // HID Control Point Characteristic Declaration + [HIDD_LE_IDX_HID_CTNL_PT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_write_nr}}, + // HID Control Point Characteristic Value + [HIDD_LE_IDX_HID_CTNL_PT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_control_point_uuid, + ESP_GATT_PERM_WRITE, + sizeof(uint8_t), 0, + NULL}}, + + // Report Map Characteristic Declaration + [HIDD_LE_IDX_REPORT_MAP_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read}}, + // Report Map Characteristic Value + [HIDD_LE_IDX_REPORT_MAP_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_map_uuid, + ESP_GATT_PERM_READ, + HIDD_LE_REPORT_MAP_MAX_LEN, sizeof(hidReportMap), + (uint8_t *)&hidReportMap}}, + + // Report Map Characteristic - External Report Reference Descriptor + [HIDD_LE_IDX_REPORT_MAP_EXT_REP_REF] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_repot_map_ext_desc_uuid, + ESP_GATT_PERM_READ, + sizeof(uint16_t), sizeof(uint16_t), + (uint8_t *)&hidExtReportRefDesc}}, + + // Protocol Mode Characteristic Declaration + [HIDD_LE_IDX_PROTO_MODE_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_write}}, + // Protocol Mode Characteristic Value + [HIDD_LE_IDX_PROTO_MODE_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_proto_mode_uuid, + (ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE), + sizeof(uint8_t), sizeof(hidProtocolMode), + (uint8_t *)&hidProtocolMode}}, + + [HIDD_LE_IDX_REPORT_MOUSE_IN_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_notify}}, + + [HIDD_LE_IDX_REPORT_MOUSE_IN_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_uuid, + ESP_GATT_PERM_READ, + HIDD_LE_REPORT_MAX_LEN, 0, + NULL}}, + + [HIDD_LE_IDX_REPORT_MOUSE_IN_CCC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, + (ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE), + sizeof(uint16_t), 0, + NULL}}, + + [HIDD_LE_IDX_REPORT_MOUSE_REP_REF] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_ref_descr_uuid, + ESP_GATT_PERM_READ, + sizeof(hidReportRefMouseIn), sizeof(hidReportRefMouseIn), + hidReportRefMouseIn}}, + // Report Characteristic Declaration + [HIDD_LE_IDX_REPORT_KEY_IN_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_notify}}, + // Report Characteristic Value + [HIDD_LE_IDX_REPORT_KEY_IN_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_uuid, + ESP_GATT_PERM_READ, + HIDD_LE_REPORT_MAX_LEN, 0, + NULL}}, + // Report KEY INPUT Characteristic - Client Characteristic Configuration Descriptor + [HIDD_LE_IDX_REPORT_KEY_IN_CCC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, + (ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE), + sizeof(uint16_t), 0, + NULL}}, + // Report Characteristic - Report Reference Descriptor + [HIDD_LE_IDX_REPORT_KEY_IN_REP_REF] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_ref_descr_uuid, + ESP_GATT_PERM_READ, + sizeof(hidReportRefKeyIn), sizeof(hidReportRefKeyIn), + hidReportRefKeyIn}}, + + // Report Characteristic Declaration + [HIDD_LE_IDX_REPORT_LED_OUT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_write}}, + + [HIDD_LE_IDX_REPORT_LED_OUT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_uuid, + ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, + HIDD_LE_REPORT_MAX_LEN, 0, + NULL}}, + [HIDD_LE_IDX_REPORT_LED_OUT_REP_REF] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_ref_descr_uuid, + ESP_GATT_PERM_READ, + sizeof(hidReportRefLedOut), sizeof(hidReportRefLedOut), + hidReportRefLedOut}}, +#if (SUPPORT_REPORT_VENDOR == true) + // Report Characteristic Declaration + [HIDD_LE_IDX_REPORT_VENDOR_OUT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_write_notify}}, + [HIDD_LE_IDX_REPORT_VENDOR_OUT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_uuid, + ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE, + HIDD_LE_REPORT_MAX_LEN, 0, + NULL}}, + [HIDD_LE_IDX_REPORT_VENDOR_OUT_REP_REF] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_ref_descr_uuid, + ESP_GATT_PERM_READ, + sizeof(hidReportRefVendorOut), sizeof(hidReportRefVendorOut), + hidReportRefVendorOut}}, +#endif + // Report Characteristic Declaration + [HIDD_LE_IDX_REPORT_CC_IN_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_notify}}, + // Report Characteristic Value + [HIDD_LE_IDX_REPORT_CC_IN_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_uuid, + ESP_GATT_PERM_READ, + HIDD_LE_REPORT_MAX_LEN, 0, + NULL}}, + // Report KEY INPUT Characteristic - Client Characteristic Configuration Descriptor + [HIDD_LE_IDX_REPORT_CC_IN_CCC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, + (ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE_ENCRYPTED), + sizeof(uint16_t), 0, + NULL}}, + // Report Characteristic - Report Reference Descriptor + [HIDD_LE_IDX_REPORT_CC_IN_REP_REF] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_ref_descr_uuid, + ESP_GATT_PERM_READ, + sizeof(hidReportRefCCIn), sizeof(hidReportRefCCIn), + hidReportRefCCIn}}, + + // Boot Keyboard Input Report Characteristic Declaration + [HIDD_LE_IDX_BOOT_KB_IN_REPORT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_notify}}, + // Boot Keyboard Input Report Characteristic Value + [HIDD_LE_IDX_BOOT_KB_IN_REPORT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_kb_input_uuid, + ESP_GATT_PERM_READ, + HIDD_LE_BOOT_REPORT_MAX_LEN, 0, + NULL}}, + // Boot Keyboard Input Report Characteristic - Client Characteristic Configuration Descriptor + [HIDD_LE_IDX_BOOT_KB_IN_REPORT_NTF_CFG] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, + (ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE), + sizeof(uint16_t), 0, + NULL}}, + + // Boot Keyboard Output Report Characteristic Declaration + [HIDD_LE_IDX_BOOT_KB_OUT_REPORT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_write}}, + // Boot Keyboard Output Report Characteristic Value + [HIDD_LE_IDX_BOOT_KB_OUT_REPORT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_kb_output_uuid, + (ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE), + HIDD_LE_BOOT_REPORT_MAX_LEN, 0, + NULL}}, + + // Boot Mouse Input Report Characteristic Declaration + [HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_notify}}, + // Boot Mouse Input Report Characteristic Value + [HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_mouse_input_uuid, + ESP_GATT_PERM_READ, + HIDD_LE_BOOT_REPORT_MAX_LEN, 0, + NULL}}, + // Boot Mouse Input Report Characteristic - Client Characteristic Configuration Descriptor + [HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_NTF_CFG] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, + (ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE), + sizeof(uint16_t), 0, + NULL}}, + + // Report Characteristic Declaration + [HIDD_LE_IDX_REPORT_CHAR] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, + ESP_GATT_PERM_READ, + CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, + (uint8_t *)&char_prop_read_write}}, + // Report Characteristic Value + [HIDD_LE_IDX_REPORT_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_uuid, + ESP_GATT_PERM_READ, + HIDD_LE_REPORT_MAX_LEN, 0, + NULL}}, + // Report Characteristic - Report Reference Descriptor + [HIDD_LE_IDX_REPORT_REP_REF] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&hid_report_ref_descr_uuid, + ESP_GATT_PERM_READ, + sizeof(hidReportRefFeature), sizeof(hidReportRefFeature), + hidReportRefFeature}}, +}; + +static void hid_add_id_tbl(void); + +void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, + esp_ble_gatts_cb_param_t *param) +{ + switch(event) { + case ESP_GATTS_REG_EVT: { + esp_ble_gap_config_local_icon (ESP_BLE_APPEARANCE_GENERIC_HID); + esp_hidd_cb_param_t hidd_param; + hidd_param.init_finish.state = param->reg.status; + if(param->reg.app_id == HIDD_APP_ID) { + hidd_le_env.gatt_if = gatts_if; + if(hidd_le_env.hidd_cb != NULL) { + (hidd_le_env.hidd_cb)(ESP_HIDD_EVENT_REG_FINISH, &hidd_param); + hidd_le_create_service(hidd_le_env.gatt_if); + } + } + if(param->reg.app_id == BATTRAY_APP_ID) { + hidd_param.init_finish.gatts_if = gatts_if; + if(hidd_le_env.hidd_cb != NULL) { + (hidd_le_env.hidd_cb)(ESP_BAT_EVENT_REG, &hidd_param); + } + + } + + break; + } + case ESP_GATTS_CONF_EVT: { + break; + } + case ESP_GATTS_CREATE_EVT: + break; + case ESP_GATTS_CONNECT_EVT: { + esp_hidd_cb_param_t cb_param = {0}; + ESP_LOGI(HID_LE_PRF_TAG, "HID connection establish, conn_id = %x",param->connect.conn_id); + memcpy(cb_param.connect.remote_bda, param->connect.remote_bda, sizeof(esp_bd_addr_t)); + cb_param.connect.conn_id = param->connect.conn_id; + hidd_clcb_alloc(param->connect.conn_id, param->connect.remote_bda); + esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_NO_MITM); + if(hidd_le_env.hidd_cb != NULL) { + (hidd_le_env.hidd_cb)(ESP_HIDD_EVENT_BLE_CONNECT, &cb_param); + } + break; + } + case ESP_GATTS_DISCONNECT_EVT: { + if(hidd_le_env.hidd_cb != NULL) { + (hidd_le_env.hidd_cb)(ESP_HIDD_EVENT_BLE_DISCONNECT, NULL); + } + hidd_clcb_dealloc(param->disconnect.conn_id); + break; + } + case ESP_GATTS_CLOSE_EVT: + break; + case ESP_GATTS_WRITE_EVT: { +#if (SUPPORT_REPORT_VENDOR == true) + esp_hidd_cb_param_t cb_param = {0}; + if (param->write.handle == hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_VENDOR_OUT_VAL] && + hidd_le_env.hidd_cb != NULL) { + cb_param.vendor_write.conn_id = param->write.conn_id; + cb_param.vendor_write.report_id = HID_RPT_ID_VENDOR_OUT; + cb_param.vendor_write.length = param->write.len; + cb_param.vendor_write.data = param->write.value; + (hidd_le_env.hidd_cb)(ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT, &cb_param); + } +#endif + break; + } + case ESP_GATTS_CREAT_ATTR_TAB_EVT: { + if (param->add_attr_tab.num_handle == BAS_IDX_NB && + param->add_attr_tab.svc_uuid.uuid.uuid16 == ESP_GATT_UUID_BATTERY_SERVICE_SVC && + param->add_attr_tab.status == ESP_GATT_OK) { + incl_svc.start_hdl = param->add_attr_tab.handles[BAS_IDX_SVC]; + incl_svc.end_hdl = incl_svc.start_hdl + BAS_IDX_NB -1; + ESP_LOGI(HID_LE_PRF_TAG, "%s(), start added the hid service to the stack database. incl_handle = %d", + __func__, incl_svc.start_hdl); + esp_ble_gatts_create_attr_tab(hidd_le_gatt_db, gatts_if, HIDD_LE_IDX_NB, 0); + } + if (param->add_attr_tab.num_handle == HIDD_LE_IDX_NB && + param->add_attr_tab.status == ESP_GATT_OK) { + memcpy(hidd_le_env.hidd_inst.att_tbl, param->add_attr_tab.handles, + HIDD_LE_IDX_NB*sizeof(uint16_t)); + ESP_LOGI(HID_LE_PRF_TAG, "hid svc handle = %x",hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC]); + hid_add_id_tbl(); + esp_ble_gatts_start_service(hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_SVC]); + } else { + esp_ble_gatts_start_service(param->add_attr_tab.handles[0]); + } + break; + } + + default: + break; + } +} + +void hidd_le_create_service(esp_gatt_if_t gatts_if) +{ + /* Here should added the battery service first, because the hid service should include the battery service. + After finish to added the battery service then can added the hid service. */ + esp_ble_gatts_create_attr_tab(bas_att_db, gatts_if, BAS_IDX_NB, 0); + +} + +void hidd_le_init(void) +{ + + // Reset the hid device target environment + memset(&hidd_le_env, 0, sizeof(hidd_le_env_t)); +} + +void hidd_clcb_alloc (uint16_t conn_id, esp_bd_addr_t bda) +{ + uint8_t i_clcb = 0; + hidd_clcb_t *p_clcb = NULL; + + for (i_clcb = 0, p_clcb= hidd_le_env.hidd_clcb; i_clcb < HID_MAX_APPS; i_clcb++, p_clcb++) { + if (!p_clcb->in_use) { + p_clcb->in_use = true; + p_clcb->conn_id = conn_id; + p_clcb->connected = true; + memcpy (p_clcb->remote_bda, bda, ESP_BD_ADDR_LEN); + break; + } + } + return; +} + +bool hidd_clcb_dealloc (uint16_t conn_id) +{ + uint8_t i_clcb = 0; + hidd_clcb_t *p_clcb = NULL; + + for (i_clcb = 0, p_clcb= hidd_le_env.hidd_clcb; i_clcb < HID_MAX_APPS; i_clcb++, p_clcb++) { + memset(p_clcb, 0, sizeof(hidd_clcb_t)); + return true; + } + + return false; +} + +static struct gatts_profile_inst heart_rate_profile_tab[PROFILE_NUM] = { + [PROFILE_APP_IDX] = { + .gatts_cb = esp_hidd_prf_cb_hdl, + .gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */ + }, + +}; + +static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, + esp_ble_gatts_cb_param_t *param) +{ + /* If event is register event, store the gatts_if for each profile */ + if (event == ESP_GATTS_REG_EVT) { + if (param->reg.status == ESP_GATT_OK) { + heart_rate_profile_tab[PROFILE_APP_IDX].gatts_if = gatts_if; + } else { + ESP_LOGI(HID_LE_PRF_TAG, "Reg app failed, app_id %04x, status %d\n", + param->reg.app_id, + param->reg.status); + return; + } + } + + do { + int idx; + for (idx = 0; idx < PROFILE_NUM; idx++) { + if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */ + gatts_if == heart_rate_profile_tab[idx].gatts_if) { + if (heart_rate_profile_tab[idx].gatts_cb) { + heart_rate_profile_tab[idx].gatts_cb(event, gatts_if, param); + } + } + } + } while (0); +} + + +esp_err_t hidd_register_cb(void) +{ + esp_err_t status; + status = esp_ble_gatts_register_callback(gatts_event_handler); + return status; +} + +void hidd_set_attr_value(uint16_t handle, uint16_t val_len, const uint8_t *value) +{ + hidd_inst_t *hidd_inst = &hidd_le_env.hidd_inst; + if(hidd_inst->att_tbl[HIDD_LE_IDX_HID_INFO_VAL] <= handle && + hidd_inst->att_tbl[HIDD_LE_IDX_REPORT_REP_REF] >= handle) { + esp_ble_gatts_set_attr_value(handle, val_len, value); + } else { + ESP_LOGE(HID_LE_PRF_TAG, "%s error:Invalid handle value.",__func__); + } + return; +} + +void hidd_get_attr_value(uint16_t handle, uint16_t *length, uint8_t **value) +{ + hidd_inst_t *hidd_inst = &hidd_le_env.hidd_inst; + if(hidd_inst->att_tbl[HIDD_LE_IDX_HID_INFO_VAL] <= handle && + hidd_inst->att_tbl[HIDD_LE_IDX_REPORT_REP_REF] >= handle){ + esp_ble_gatts_get_attr_value(handle, length, (const uint8_t **)value); + } else { + ESP_LOGE(HID_LE_PRF_TAG, "%s error:Invalid handle value.", __func__); + } + + return; +} + +static void hid_add_id_tbl(void) +{ + // Mouse input report + hid_rpt_map[0].id = hidReportRefMouseIn[0]; + hid_rpt_map[0].type = hidReportRefMouseIn[1]; + hid_rpt_map[0].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_MOUSE_IN_VAL]; + hid_rpt_map[0].cccdHandle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_MOUSE_IN_VAL]; + hid_rpt_map[0].mode = HID_PROTOCOL_MODE_REPORT; + + // Key input report + hid_rpt_map[1].id = hidReportRefKeyIn[0]; + hid_rpt_map[1].type = hidReportRefKeyIn[1]; + hid_rpt_map[1].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_KEY_IN_VAL]; + hid_rpt_map[1].cccdHandle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_KEY_IN_CCC]; + hid_rpt_map[1].mode = HID_PROTOCOL_MODE_REPORT; + + // Consumer Control input report + hid_rpt_map[2].id = hidReportRefCCIn[0]; + hid_rpt_map[2].type = hidReportRefCCIn[1]; + hid_rpt_map[2].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_CC_IN_VAL]; + hid_rpt_map[2].cccdHandle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_CC_IN_CCC]; + hid_rpt_map[2].mode = HID_PROTOCOL_MODE_REPORT; + + // LED output report + hid_rpt_map[3].id = hidReportRefLedOut[0]; + hid_rpt_map[3].type = hidReportRefLedOut[1]; + hid_rpt_map[3].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_LED_OUT_VAL]; + hid_rpt_map[3].cccdHandle = 0; + hid_rpt_map[3].mode = HID_PROTOCOL_MODE_REPORT; + + // Boot keyboard input report + // Use same ID and type as key input report + hid_rpt_map[4].id = hidReportRefKeyIn[0]; + hid_rpt_map[4].type = hidReportRefKeyIn[1]; + hid_rpt_map[4].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_BOOT_KB_IN_REPORT_VAL]; + hid_rpt_map[4].cccdHandle = 0; + hid_rpt_map[4].mode = HID_PROTOCOL_MODE_BOOT; + + // Boot keyboard output report + // Use same ID and type as LED output report + hid_rpt_map[5].id = hidReportRefLedOut[0]; + hid_rpt_map[5].type = hidReportRefLedOut[1]; + hid_rpt_map[5].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_BOOT_KB_OUT_REPORT_VAL]; + hid_rpt_map[5].cccdHandle = 0; + hid_rpt_map[5].mode = HID_PROTOCOL_MODE_BOOT; + + // Boot mouse input report + // Use same ID and type as mouse input report + hid_rpt_map[6].id = hidReportRefMouseIn[0]; + hid_rpt_map[6].type = hidReportRefMouseIn[1]; + hid_rpt_map[6].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_VAL]; + hid_rpt_map[6].cccdHandle = 0; + hid_rpt_map[6].mode = HID_PROTOCOL_MODE_BOOT; + + // Feature report + hid_rpt_map[7].id = hidReportRefFeature[0]; + hid_rpt_map[7].type = hidReportRefFeature[1]; + hid_rpt_map[7].handle = hidd_le_env.hidd_inst.att_tbl[HIDD_LE_IDX_REPORT_VAL]; + hid_rpt_map[7].cccdHandle = 0; + hid_rpt_map[7].mode = HID_PROTOCOL_MODE_REPORT; + + + // Setup report ID map + hid_dev_register_reports(HID_NUM_REPORTS, hid_rpt_map); +} diff --git a/src/joystick/hidd_le_prf_int.h b/src/joystick/hidd_le_prf_int.h new file mode 100644 index 0000000..7f81e3f --- /dev/null +++ b/src/joystick/hidd_le_prf_int.h @@ -0,0 +1,343 @@ +// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with 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. + + + +#ifndef __HID_DEVICE_LE_PRF__ +#define __HID_DEVICE_LE_PRF__ +#include +#include "esp_gatts_api.h" +#include "esp_gatt_defs.h" +#include "esp_hidd_prf_api.h" +#include "esp_gap_ble_api.h" +#include "hid_dev.h" + +#define SUPPORT_REPORT_VENDOR false +//HID BLE profile log tag +#define HID_LE_PRF_TAG "HID_LE_PRF" + +/// Maximal number of HIDS that can be added in the DB +#ifndef USE_ONE_HIDS_INSTANCE +#define HIDD_LE_NB_HIDS_INST_MAX (2) +#else +#define HIDD_LE_NB_HIDS_INST_MAX (1) +#endif + +#define HIDD_GREAT_VER 0x01 //Version + Subversion +#define HIDD_SUB_VER 0x00 //Version + Subversion +#define HIDD_VERSION ((HIDD_GREAT_VER<<8)|HIDD_SUB_VER) //Version + Subversion + +#define HID_MAX_APPS 1 + +// Number of HID reports defined in the service +#define HID_NUM_REPORTS 9 + +// HID Report IDs for the service +#define HID_RPT_ID_MOUSE_IN 1 // Mouse input report ID +#define HID_RPT_ID_KEY_IN 2 // Keyboard input report ID +#define HID_RPT_ID_CC_IN 3 //Consumer Control input report ID +#define HID_RPT_ID_VENDOR_OUT 4 // Vendor output report ID +#define HID_RPT_ID_LED_OUT 0 // LED output report ID +#define HID_RPT_ID_FEATURE 0 // Feature report ID + +#define HIDD_APP_ID 0x1812//ATT_SVC_HID + +#define BATTRAY_APP_ID 0x180f + + +#define ATT_SVC_HID 0x1812 + +/// Maximal number of Report Char. that can be added in the DB for one HIDS - Up to 11 +#define HIDD_LE_NB_REPORT_INST_MAX (5) + +/// Maximal length of Report Char. Value +#define HIDD_LE_REPORT_MAX_LEN (255) +/// Maximal length of Report Map Char. Value +#define HIDD_LE_REPORT_MAP_MAX_LEN (512) + +/// Length of Boot Report Char. Value Maximal Length +#define HIDD_LE_BOOT_REPORT_MAX_LEN (8) + +/// Boot KB Input Report Notification Configuration Bit Mask +#define HIDD_LE_BOOT_KB_IN_NTF_CFG_MASK (0x40) +/// Boot KB Input Report Notification Configuration Bit Mask +#define HIDD_LE_BOOT_MOUSE_IN_NTF_CFG_MASK (0x80) +/// Boot Report Notification Configuration Bit Mask +#define HIDD_LE_REPORT_NTF_CFG_MASK (0x20) + + +/* HID information flags */ +#define HID_FLAGS_REMOTE_WAKE 0x01 // RemoteWake +#define HID_FLAGS_NORMALLY_CONNECTABLE 0x02 // NormallyConnectable + +/* Control point commands */ +#define HID_CMD_SUSPEND 0x00 // Suspend +#define HID_CMD_EXIT_SUSPEND 0x01 // Exit Suspend + +/* HID protocol mode values */ +#define HID_PROTOCOL_MODE_BOOT 0x00 // Boot Protocol Mode +#define HID_PROTOCOL_MODE_REPORT 0x01 // Report Protocol Mode + +/* Attribute value lengths */ +#define HID_PROTOCOL_MODE_LEN 1 // HID Protocol Mode +#define HID_INFORMATION_LEN 4 // HID Information +#define HID_REPORT_REF_LEN 2 // HID Report Reference Descriptor +#define HID_EXT_REPORT_REF_LEN 2 // External Report Reference Descriptor + +// HID feature flags +#define HID_KBD_FLAGS HID_FLAGS_REMOTE_WAKE + +/* HID Report type */ +#define HID_REPORT_TYPE_INPUT 1 +#define HID_REPORT_TYPE_OUTPUT 2 +#define HID_REPORT_TYPE_FEATURE 3 + + +/// HID Service Attributes Indexes +enum { + HIDD_LE_IDX_SVC, + + // Included Service + HIDD_LE_IDX_INCL_SVC, + + // HID Information + HIDD_LE_IDX_HID_INFO_CHAR, + HIDD_LE_IDX_HID_INFO_VAL, + + // HID Control Point + HIDD_LE_IDX_HID_CTNL_PT_CHAR, + HIDD_LE_IDX_HID_CTNL_PT_VAL, + + // Report Map + HIDD_LE_IDX_REPORT_MAP_CHAR, + HIDD_LE_IDX_REPORT_MAP_VAL, + HIDD_LE_IDX_REPORT_MAP_EXT_REP_REF, + + // Protocol Mode + HIDD_LE_IDX_PROTO_MODE_CHAR, + HIDD_LE_IDX_PROTO_MODE_VAL, + + // Report mouse input + HIDD_LE_IDX_REPORT_MOUSE_IN_CHAR, + HIDD_LE_IDX_REPORT_MOUSE_IN_VAL, + HIDD_LE_IDX_REPORT_MOUSE_IN_CCC, + HIDD_LE_IDX_REPORT_MOUSE_REP_REF, + //Report Key input + HIDD_LE_IDX_REPORT_KEY_IN_CHAR, + HIDD_LE_IDX_REPORT_KEY_IN_VAL, + HIDD_LE_IDX_REPORT_KEY_IN_CCC, + HIDD_LE_IDX_REPORT_KEY_IN_REP_REF, + ///Report Led output + HIDD_LE_IDX_REPORT_LED_OUT_CHAR, + HIDD_LE_IDX_REPORT_LED_OUT_VAL, + HIDD_LE_IDX_REPORT_LED_OUT_REP_REF, + +#if (SUPPORT_REPORT_VENDOR == true) + /// Report Vendor + HIDD_LE_IDX_REPORT_VENDOR_OUT_CHAR, + HIDD_LE_IDX_REPORT_VENDOR_OUT_VAL, + HIDD_LE_IDX_REPORT_VENDOR_OUT_REP_REF, +#endif + HIDD_LE_IDX_REPORT_CC_IN_CHAR, + HIDD_LE_IDX_REPORT_CC_IN_VAL, + HIDD_LE_IDX_REPORT_CC_IN_CCC, + HIDD_LE_IDX_REPORT_CC_IN_REP_REF, + + // Boot Keyboard Input Report + HIDD_LE_IDX_BOOT_KB_IN_REPORT_CHAR, + HIDD_LE_IDX_BOOT_KB_IN_REPORT_VAL, + HIDD_LE_IDX_BOOT_KB_IN_REPORT_NTF_CFG, + + // Boot Keyboard Output Report + HIDD_LE_IDX_BOOT_KB_OUT_REPORT_CHAR, + HIDD_LE_IDX_BOOT_KB_OUT_REPORT_VAL, + + // Boot Mouse Input Report + HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_CHAR, + HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_VAL, + HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_NTF_CFG, + + // Report + HIDD_LE_IDX_REPORT_CHAR, + HIDD_LE_IDX_REPORT_VAL, + HIDD_LE_IDX_REPORT_REP_REF, + //HIDD_LE_IDX_REPORT_NTF_CFG, + + HIDD_LE_IDX_NB, +}; + + +/// Attribute Table Indexes +enum { + HIDD_LE_INFO_CHAR, + HIDD_LE_CTNL_PT_CHAR, + HIDD_LE_REPORT_MAP_CHAR, + HIDD_LE_REPORT_CHAR, + HIDD_LE_PROTO_MODE_CHAR, + HIDD_LE_BOOT_KB_IN_REPORT_CHAR, + HIDD_LE_BOOT_KB_OUT_REPORT_CHAR, + HIDD_LE_BOOT_MOUSE_IN_REPORT_CHAR, + HIDD_LE_CHAR_MAX //= HIDD_LE_REPORT_CHAR + HIDD_LE_NB_REPORT_INST_MAX, +}; + +///att read event table Indexs +enum { + HIDD_LE_READ_INFO_EVT, + HIDD_LE_READ_CTNL_PT_EVT, + HIDD_LE_READ_REPORT_MAP_EVT, + HIDD_LE_READ_REPORT_EVT, + HIDD_LE_READ_PROTO_MODE_EVT, + HIDD_LE_BOOT_KB_IN_REPORT_EVT, + HIDD_LE_BOOT_KB_OUT_REPORT_EVT, + HIDD_LE_BOOT_MOUSE_IN_REPORT_EVT, + + HID_LE_EVT_MAX +}; + +/// Client Characteristic Configuration Codes +enum { + HIDD_LE_DESC_MASK = 0x10, + + HIDD_LE_BOOT_KB_IN_REPORT_CFG = HIDD_LE_BOOT_KB_IN_REPORT_CHAR | HIDD_LE_DESC_MASK, + HIDD_LE_BOOT_MOUSE_IN_REPORT_CFG = HIDD_LE_BOOT_MOUSE_IN_REPORT_CHAR | HIDD_LE_DESC_MASK, + HIDD_LE_REPORT_CFG = HIDD_LE_REPORT_CHAR | HIDD_LE_DESC_MASK, +}; + +/// Features Flag Values +enum { + HIDD_LE_CFG_KEYBOARD = 0x01, + HIDD_LE_CFG_MOUSE = 0x02, + HIDD_LE_CFG_PROTO_MODE = 0x04, + HIDD_LE_CFG_MAP_EXT_REF = 0x08, + HIDD_LE_CFG_BOOT_KB_WR = 0x10, + HIDD_LE_CFG_BOOT_MOUSE_WR = 0x20, +}; + +/// Report Char. Configuration Flag Values +enum { + HIDD_LE_CFG_REPORT_IN = 0x01, + HIDD_LE_CFG_REPORT_OUT = 0x02, + //HOGPD_CFG_REPORT_FEAT can be used as a mask to check Report type + HIDD_LE_CFG_REPORT_FEAT = 0x03, + HIDD_LE_CFG_REPORT_WR = 0x10, +}; + +/// Pointer to the connection clean-up function +#define HIDD_LE_CLEANUP_FNCT (NULL) + +/* + * TYPE DEFINITIONS + **************************************************************************************** + */ + +/// HIDD Features structure +typedef struct { + /// Service Features + uint8_t svc_features; + /// Number of Report Char. instances to add in the database + uint8_t report_nb; + /// Report Char. Configuration + uint8_t report_char_cfg[HIDD_LE_NB_REPORT_INST_MAX]; +} hidd_feature_t; + + +typedef struct { + bool in_use; + bool congest; + uint16_t conn_id; + bool connected; + esp_bd_addr_t remote_bda; + uint32_t trans_id; + uint8_t cur_srvc_id; + +} hidd_clcb_t; + +// HID report mapping table +typedef struct { + uint16_t handle; // Handle of report characteristic + uint16_t cccdHandle; // Handle of CCCD for report characteristic + uint8_t id; // Report ID + uint8_t type; // Report type + uint8_t mode; // Protocol mode (report or boot) +} hidRptMap_t; + + +typedef struct { + /// hidd profile id + uint8_t app_id; + /// Notified handle + uint16_t ntf_handle; + ///Attribute handle Table + uint16_t att_tbl[HIDD_LE_IDX_NB]; + /// Supported Features + hidd_feature_t hidd_feature[HIDD_LE_NB_HIDS_INST_MAX]; + /// Current Protocol Mode + uint8_t proto_mode[HIDD_LE_NB_HIDS_INST_MAX]; + /// Number of HIDS added in the database + uint8_t hids_nb; + uint8_t pending_evt; + uint16_t pending_hal; +} hidd_inst_t; + +/// Report Reference structure +typedef struct +{ + ///Report ID + uint8_t report_id; + ///Report Type + uint8_t report_type; +}hids_report_ref_t; + +/// HID Information structure +typedef struct +{ + /// bcdHID + uint16_t bcdHID; + /// bCountryCode + uint8_t bCountryCode; + /// Flags + uint8_t flags; +}hids_hid_info_t; + + +/* service engine control block */ +typedef struct { + hidd_clcb_t hidd_clcb[HID_MAX_APPS]; /* connection link*/ + esp_gatt_if_t gatt_if; + bool enabled; + bool is_take; + bool is_primery; + hidd_inst_t hidd_inst; + esp_hidd_event_cb_t hidd_cb; + uint8_t inst_id; +} hidd_le_env_t; + +extern hidd_le_env_t hidd_le_env; +extern uint8_t hidProtocolMode; + + +void hidd_clcb_alloc (uint16_t conn_id, esp_bd_addr_t bda); + +bool hidd_clcb_dealloc (uint16_t conn_id); + +void hidd_le_create_service(esp_gatt_if_t gatts_if); + +void hidd_set_attr_value(uint16_t handle, uint16_t val_len, const uint8_t *value); + +void hidd_get_attr_value(uint16_t handle, uint16_t *length, uint8_t **value); + +esp_err_t hidd_register_cb(void); + + +#endif ///__HID_DEVICE_LE_PRF__ diff --git a/src/terminal.c b/src/terminal.c index ae84dc0..5673d02 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -140,6 +140,7 @@ void mainTask(void *stuff) { ESP_LOGE(LOG_UART, "Invalid role loaded, defaulting to central"); settings.role = ROLE_BLE_CENTRAL; } + settings.role = ROLE_BLE_HID_JOYSTICK; setRole(settings.role); ESP_LOGI(LOG_UART, "Waiting for UART RX Task to start"); @@ -209,11 +210,10 @@ void setRole(role_t role) bt_init(); btpInit(); break; - case ROLE_BLE_JOYSTICK: - btPeripherialState = PERIPHERIAL_STATE_DISCONNECTED; + case ROLE_BLE_HID_JOYSTICK: bt_init(); - //btjInit(); // TOOD - + btjoyInit(); + break; default: break; } From a6f836633d3495950728b8745cca661b8f386285 Mon Sep 17 00:00:00 2001 From: Cliff Blackburn Date: Tue, 5 Jul 2022 21:40:09 -0600 Subject: [PATCH 06/17] Connects, comes up as a joystick. But not getting reports --- src/frskybt.c | 7 +- src/joystick/bt_joystick.c | 37 ++-------- src/joystick/bt_joystick.h | 3 +- src/joystick/esp_hidd_prf_api.c | 114 +++---------------------------- src/joystick/esp_hidd_prf_api.h | 6 +- src/joystick/hid_dev.c | 78 --------------------- src/joystick/hid_dev.h | 4 -- src/joystick/hid_device_le_prf.c | 42 +++++++++++- src/terminal.c | 16 ++--- 9 files changed, 72 insertions(+), 235 deletions(-) diff --git a/src/frskybt.c b/src/frskybt.c index ee43cdb..0976c35 100644 --- a/src/frskybt.c +++ b/src/frskybt.c @@ -4,6 +4,8 @@ #include #include "frskybt.h" #include "bt_server.h" +#include "joystick/bt_joystick.h" +#include "joystick/esp_hidd_prf_api.h" #include "settings.h" #include "esp_log.h" @@ -125,6 +127,9 @@ void processTrainerFrame(const uint8_t * otxbuffer) // If the data came from the radio, send it out over bluetooth. Send same data but add the START_STOP if(settings.role == ROLE_BLE_PERIPHERAL) { + if(btjoystickconnected) { + hid_SendJoystickChannels(channeldata); + } btp_sendChannelData(_otxbuffer, otxbufferIndex+1); } } @@ -195,7 +200,7 @@ void frSkyProcessByte(uint8_t data) if (crc == otxbuffer[BLUETOOTH_PACKET_SIZE - 1]) { if (otxbuffer[0] == TRAINER_FRAME) { processTrainerFrame(otxbuffer); - logBTFrame(true, ""); + //logBTFrame(true, ""); } else { logBTFrame(false, "Not a trainer frame"); } diff --git a/src/joystick/bt_joystick.c b/src/joystick/bt_joystick.c index df303ae..060747c 100644 --- a/src/joystick/bt_joystick.c +++ b/src/joystick/bt_joystick.c @@ -21,6 +21,7 @@ #include "driver/gpio.h" #include "joystick/hid_dev.h" + /** * Brief: * This example Implemented BLE HID device profile related functions, in which the HID device @@ -42,15 +43,15 @@ #define HID_DEMO_TAG "HID_DEMO" - -static uint16_t hid_conn_id = 0; +uint16_t btj_conn_id = 0; static bool sec_conn = false; static bool send_volum_up = false; +volatile bool btjoystickconnected=false; #define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param); -#define HIDD_DEVICE_NAME "HID" +#define HIDD_DEVICE_NAME "BTWifiGamePad" static uint8_t hidd_service_uuid128[] = { /* LSB <--------------------------------------------------------------------------------> MSB */ //first uuid, 16bit, [12],[13] is the value @@ -104,11 +105,13 @@ static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t * break; case ESP_HIDD_EVENT_BLE_CONNECT: { ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_CONNECT"); - hid_conn_id = param->connect.conn_id; + btjoystickconnected = true; + btj_conn_id = param->connect.conn_id; break; } case ESP_HIDD_EVENT_BLE_DISCONNECT: { sec_conn = false; + btjoystickconnected = false; ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_DISCONNECT"); esp_ble_gap_start_advertising(&hidd_adv_params); break; @@ -153,30 +156,6 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param } } -void hid_demo_task(void *pvParameters) -{ - vTaskDelay(1000 / portTICK_PERIOD_MS); - while(1) { - - //vTaskDelay(2000 / portTICK_PERIOD_MS); - if (sec_conn) { - esp_hidd_send_joystick_value(hid_conn_id, ) - - //uint8_t key_vaule = {HID_KEY_A}; - //esp_hidd_send_keyboard_value(hid_conn_id, 0, &key_vaule, 1); - esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_UP, true); - vTaskDelay(3000 / portTICK_PERIOD_MS); - if (send_volum_up) { - send_volum_up = false; - esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_UP, false); - esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_DOWN, true); - vTaskDelay(3000 / portTICK_PERIOD_MS); - esp_hidd_send_consumer_value(hid_conn_id, HID_CONSUMER_VOLUME_DOWN, false); - } - } - } -} - #include "bt_joystick.h" void btjoyInit() @@ -200,6 +179,4 @@ void btjoyInit() and the init key means which key you can distribute to the slave. */ esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)); esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)); - - xTaskCreate(&hid_demo_task, "hid_task", 2048, NULL, 5, NULL); } \ No newline at end of file diff --git a/src/joystick/bt_joystick.h b/src/joystick/bt_joystick.h index a7c0f94..c94d19f 100644 --- a/src/joystick/bt_joystick.h +++ b/src/joystick/bt_joystick.h @@ -1,5 +1,6 @@ #pragma once - +extern volatile bool btjoystickconnected; +extern uint16_t btj_conn_id; void btjoyInit(); diff --git a/src/joystick/esp_hidd_prf_api.c b/src/joystick/esp_hidd_prf_api.c index 3e7079b..b3f2ea9 100644 --- a/src/joystick/esp_hidd_prf_api.c +++ b/src/joystick/esp_hidd_prf_api.c @@ -18,6 +18,7 @@ #include #include #include "esp_log.h" +#include "bt_joystick.h" // HID keyboard input report length #define HID_KEYBOARD_IN_RPT_LEN 8 @@ -95,55 +96,6 @@ uint16_t esp_hidd_get_version(void) return HIDD_VERSION; } -void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pressed) -{ - uint8_t buffer[HID_CC_IN_RPT_LEN] = {0, 0}; - if (key_pressed) { - ESP_LOGD(HID_LE_PRF_TAG, "hid_consumer_build_report"); - hid_consumer_build_report(buffer, key_cmd); - } - ESP_LOGD(HID_LE_PRF_TAG, "buffer[0] = %x, buffer[1] = %x", buffer[0], buffer[1]); - hid_dev_send_report(hidd_le_env.gatt_if, conn_id, - HID_RPT_ID_CC_IN, HID_REPORT_TYPE_INPUT, HID_CC_IN_RPT_LEN, buffer); - return; -} - -void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask, uint8_t *keyboard_cmd, uint8_t num_key) -{ - if (num_key > HID_KEYBOARD_IN_RPT_LEN - 2) { - ESP_LOGE(HID_LE_PRF_TAG, "%s(), the number key should not be more than %d", __func__, HID_KEYBOARD_IN_RPT_LEN); - return; - } - - uint8_t buffer[HID_KEYBOARD_IN_RPT_LEN] = {0}; - - buffer[0] = special_key_mask; - - for (int i = 0; i < num_key; i++) { - buffer[i+2] = keyboard_cmd[i]; - } - - ESP_LOGD(HID_LE_PRF_TAG, "the key vaule = %d,%d,%d, %d, %d, %d,%d, %d", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7]); - hid_dev_send_report(hidd_le_env.gatt_if, conn_id, - HID_RPT_ID_KEY_IN, HID_REPORT_TYPE_INPUT, HID_KEYBOARD_IN_RPT_LEN, buffer); - return; -} - -void esp_hidd_send_mouse_value(uint16_t conn_id, uint8_t mouse_button, int8_t mickeys_x, int8_t mickeys_y) -{ - uint8_t buffer[HID_MOUSE_IN_RPT_LEN]; - - buffer[0] = mouse_button; // Buttons - buffer[1] = mickeys_x; // X - buffer[2] = mickeys_y; // Y - buffer[3] = 0; // Wheel - buffer[4] = 0; // AC Pan - - hid_dev_send_report(hidd_le_env.gatt_if, conn_id, - HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, HID_MOUSE_IN_RPT_LEN, buffer); - return; -} - // ------------------------------------- // From - Head Tracker Code joystick.cpp @@ -151,16 +103,18 @@ void esp_hidd_send_mouse_value(uint16_t conn_id, uint8_t mouse_button, int8_t mi #define JOYSTICK_BUTTON_HIGH 1750 #define JOYSTICK_BUTTON_LOW 1250 -struct { +struct PACKED { #ifdef JOYSTICK_BUTTONS uint8_t but[2]; #endif - uint16_t channels[8]; + uint8_t channels[16]; } report; -void set_JoystickChannels(uint16_t chans[16]) +void hid_SendJoystickChannels(uint16_t chans[8]) { - memcpy(report.channels, chans, sizeof(report.channels)); + uint16_t conn_id = btj_conn_id; + ESP_LOGI("JOY","Sending HID Report ch0 - %d", chans[0]); + memcpy(report.channels, chans, sizeof(report.channels)); #ifdef JOYSTICK_BUTTONS report.but[0] = 0; @@ -183,58 +137,10 @@ void set_JoystickChannels(uint16_t chans[16]) } #endif - report.channels[i] -= 988; // Shift from center so it's 0-1024 + report.channels[i] -= 988; // Shift from center so it's 0-1024 } - hid_int_ep_write(hdev, (uint8_t*)&report, sizeof(report), NULL); + hid_dev_send_report(hidd_le_env.gatt_if, conn_id, + HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, sizeof(report), (uint8_t*)&report); } -static const uint8_t hid_report_desc[] = { - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x05, // USAGE (Game Pad) - 0xa1, 0x01, // COLLECTION (Application) - 0xa1, 0x00, // COLLECTION (Physical) -#ifdef JOYSTICK_BUTTONS - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x10, // USAGE_MAXIMUM (Button 8) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x95, 0x10, // REPORT_COUNT (8) - 0x75, 0x01, // REPORT_SIZE (1) - 0x81, 0x02, // INPUT (Data,Var,Abs) -#endif - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x09, 0x32, // USAGE (Z) - 0x09, 0x33, // USAGE (Rx) - 0x09, 0x34, // USAGE (Ry) - 0x09, 0x35, // USAGE (Rz) - 0x09, 0x36, // USAGE (Slider) - 0x09, 0x36, // USAGE (Slider) - 0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0) - 0x26, 0xFF, 0x03, // LOGICAL_MAXIMUM (1024) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x08, // REPORT_COUNT (8) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0xc0, // END_COLLECTION - 0xc0 // END_COLLECTION -}; - -void esp_hidd_send_joystick_value(uint16_t conn_id, uint16_t joystick_buttons, uint8_t joystick_x, uint8_t joystick_y, uint8_t joystick_z, uint8_t joystick_rx) -{ - uint8_t buffer[HID_GAMEPAD_IN_RPT_LEN]; - ESP_LOGD(HID_LE_PRF_TAG, "the buttons value = %d js1 = %d, %d js2 = %d, %d", joystick_buttons, joystick_x, joystick_y, joystick_z, joystick_rx); - - buffer[0]=joystick_buttons & 0xff; - buffer[1] = ( joystick_buttons >> 8); - buffer[2] = ( joystick_x ^ 0x80 ); // X - buffer[3] = (( joystick_y ^ 0x80 ) * -1) - 1; // Y - buffer[4] = ( joystick_z ^ 0x80 ); // X - buffer[5] = (( joystick_rx ^ 0x80 ) * -1) - 1; // Y - - hid_dev_send_report(hidd_le_env.gatt_if, conn_id, - HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, HID_GAMEPAD_IN_RPT_LEN, buffer); - return; -} \ No newline at end of file diff --git a/src/joystick/esp_hidd_prf_api.h b/src/joystick/esp_hidd_prf_api.h index e7a1932..e8dd898 100644 --- a/src/joystick/esp_hidd_prf_api.h +++ b/src/joystick/esp_hidd_prf_api.h @@ -154,11 +154,7 @@ esp_err_t esp_hidd_profile_deinit(void); */ uint16_t esp_hidd_get_version(void); -void esp_hidd_send_consumer_value(uint16_t conn_id, uint8_t key_cmd, bool key_pressed); - -void esp_hidd_send_keyboard_value(uint16_t conn_id, key_mask_t special_key_mask, uint8_t *keyboard_cmd, uint8_t num_key); - -void esp_hidd_send_mouse_value(uint16_t conn_id, uint8_t mouse_button, int8_t mickeys_x, int8_t mickeys_y); +void hid_SendJoystickChannels(uint16_t chans[8]); #ifdef __cplusplus } diff --git a/src/joystick/hid_dev.c b/src/joystick/hid_dev.c index 92d3572..c8c99ae 100644 --- a/src/joystick/hid_dev.c +++ b/src/joystick/hid_dev.c @@ -57,81 +57,3 @@ void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id, return; } -void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd) -{ - if (!buffer) { - ESP_LOGE(HID_LE_PRF_TAG, "%s(), the buffer is NULL, hid build report failed.", __func__); - return; - } - - switch (cmd) { - case HID_CONSUMER_CHANNEL_UP: - HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_UP); - break; - - case HID_CONSUMER_CHANNEL_DOWN: - HID_CC_RPT_SET_CHANNEL(buffer, HID_CC_RPT_CHANNEL_DOWN); - break; - - case HID_CONSUMER_VOLUME_UP: - HID_CC_RPT_SET_VOLUME_UP(buffer); - break; - - case HID_CONSUMER_VOLUME_DOWN: - HID_CC_RPT_SET_VOLUME_DOWN(buffer); - break; - - case HID_CONSUMER_MUTE: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_MUTE); - break; - - case HID_CONSUMER_POWER: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_POWER); - break; - - case HID_CONSUMER_RECALL_LAST: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_LAST); - break; - - case HID_CONSUMER_ASSIGN_SEL: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_ASSIGN_SEL); - break; - - case HID_CONSUMER_PLAY: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PLAY); - break; - - case HID_CONSUMER_PAUSE: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_PAUSE); - break; - - case HID_CONSUMER_RECORD: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_RECORD); - break; - - case HID_CONSUMER_FAST_FORWARD: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_FAST_FWD); - break; - - case HID_CONSUMER_REWIND: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_REWIND); - break; - - case HID_CONSUMER_SCAN_NEXT_TRK: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_NEXT_TRK); - break; - - case HID_CONSUMER_SCAN_PREV_TRK: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_SCAN_PREV_TRK); - break; - - case HID_CONSUMER_STOP: - HID_CC_RPT_SET_BUTTON(buffer, HID_CC_RPT_STOP); - break; - - default: - break; - } - - return; -} diff --git a/src/joystick/hid_dev.h b/src/joystick/hid_dev.h index d41b872..fdadb3e 100644 --- a/src/joystick/hid_dev.h +++ b/src/joystick/hid_dev.h @@ -247,10 +247,6 @@ void hid_dev_register_reports(uint8_t num_reports, hid_report_map_t *p_report); void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id, uint8_t id, uint8_t type, uint8_t length, uint8_t *data); -void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd); - -void hid_keyboard_build_report(uint8_t *buffer, keyboard_cmd_t cmd); - void hid_mouse_build_report(uint8_t *buffer, mouse_cmd_t cmd); #ifdef __cplusplus diff --git a/src/joystick/hid_device_le_prf.c b/src/joystick/hid_device_le_prf.c index db697b0..c6274fa 100644 --- a/src/joystick/hid_device_le_prf.c +++ b/src/joystick/hid_device_le_prf.c @@ -34,9 +34,45 @@ struct prf_char_pres_fmt // HID report mapping table static hid_report_map_t hid_rpt_map[HID_NUM_REPORTS]; +#define JOYSTICK_BUTTONS + +static const uint8_t hidReportMap[] = { + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x05, // USAGE (Game Pad) + 0xa1, 0x01, // COLLECTION (Application) + 0xa1, 0x00, // COLLECTION (Physical) +#ifdef JOYSTICK_BUTTONS + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x10, // USAGE_MAXIMUM (Button 8) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x95, 0x10, // REPORT_COUNT (8) + 0x75, 0x01, // REPORT_SIZE (1) + 0x81, 0x02, // INPUT (Data,Var,Abs) +#endif + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x09, 0x32, // USAGE (Z) + 0x09, 0x33, // USAGE (Rx) + 0x09, 0x34, // USAGE (Ry) + 0x09, 0x35, // USAGE (Rz) + 0x09, 0x36, // USAGE (Slider) + 0x09, 0x36, // USAGE (Slider) + 0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0) + 0x26, 0xFF, 0x03, // LOGICAL_MAXIMUM (1024) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x08, // REPORT_COUNT (8) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0xc0, // END_COLLECTION + 0xc0 // END_COLLECTION +}; + + // HID Report Map characteristic value // Keyboard report descriptor (using format for Boot interface descriptor) -static const uint8_t hidReportMap[] = { +/*static const uint8_t hidReportMap[] = { 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x02, // Usage (Mouse) 0xA1, 0x01, // Collection (Application) @@ -183,7 +219,7 @@ static const uint8_t hidReportMap[] = { 0xC0, // End Collection #endif -}; +};*/ /// Battery Service Attributes Indexes enum @@ -299,7 +335,7 @@ static const uint16_t bat_lev_uuid = ESP_GATT_UUID_BATTERY_LEVEL; static const uint8_t bat_lev_ccc[2] ={ 0x00, 0x00}; static const uint16_t char_format_uuid = ESP_GATT_UUID_CHAR_PRESENT_FORMAT; -static uint8_t battary_lev = 50; +static uint8_t battary_lev = 98; /// Full HRS Database Description - Used to add attributes into the database static const esp_gatts_attr_db_t bas_att_db[BAS_IDX_NB] = { diff --git a/src/terminal.c b/src/terminal.c index 5673d02..1644edb 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -10,6 +10,7 @@ #include "bt.h" #include "bt_client.h" #include "bt_server.h" +#include "joystick/bt_joystick.h" #include "defines.h" #include "settings.h" #include "cobs.h" @@ -138,9 +139,8 @@ void mainTask(void *stuff) { ESP_LOGI(LOG_UART, "Setting initial role"); if(settings.role == ROLE_UNKNOWN) { ESP_LOGE(LOG_UART, "Invalid role loaded, defaulting to central"); - settings.role = ROLE_BLE_CENTRAL; + settings.role = ROLE_BLE_PERIPHERAL; } - settings.role = ROLE_BLE_HID_JOYSTICK; setRole(settings.role); ESP_LOGI(LOG_UART, "Waiting for UART RX Task to start"); @@ -186,11 +186,13 @@ void setRole(role_t role) ESP_LOGI(LOG_UART,"Switching from mode %d to %d", curMode, role); if(role == curMode) return; + if(role > 1) + role = 0; // Shutdown switch(curMode) { case ROLE_BLE_CENTRAL: case ROLE_BLE_PERIPHERAL: - default: + default: break; } @@ -207,10 +209,6 @@ void setRole(role_t role) break; case ROLE_BLE_PERIPHERAL: btPeripherialState = PERIPHERIAL_STATE_DISCONNECTED; - bt_init(); - btpInit(); - break; - case ROLE_BLE_HID_JOYSTICK: bt_init(); btjoyInit(); break; @@ -306,7 +304,7 @@ void runBTPeripherial() { switch(btPeripherialState) { case PERIPHERIAL_STATE_DISCONNECTED: - if(btp_connected) { + if(btjoystickconnected) { // Save Remote Address btaddrtostr(rmtaddress, rmtbtaddress); sprintf(reusablebuff, "Connected:%s\r\n", rmtaddress); @@ -315,7 +313,7 @@ void runBTPeripherial() } break; case PERIPHERIAL_STATE_CONNECTED: - if(!btp_connected) { + if(!btjoystickconnected) { btPeripherialState = PERIPHERIAL_STATE_DISCONNECTED; uart_write_bytes(uart_num, "DisConnected\r\nERROR\r\nERROR\r\n",28); } From bae2cefef502ba9ad994efa039609d46dc13bee9 Mon Sep 17 00:00:00 2001 From: Cliff Blackburn Date: Thu, 7 Jul 2022 23:57:09 -0600 Subject: [PATCH 07/17] BT Joystick Works --- src/frskybt.c | 2 +- src/joystick/esp_hidd_prf_api.c | 68 ++++++---- src/joystick/hid_device_le_prf.c | 217 +++++-------------------------- 3 files changed, 76 insertions(+), 211 deletions(-) diff --git a/src/frskybt.c b/src/frskybt.c index 0976c35..8e316dd 100644 --- a/src/frskybt.c +++ b/src/frskybt.c @@ -127,7 +127,7 @@ void processTrainerFrame(const uint8_t * otxbuffer) // If the data came from the radio, send it out over bluetooth. Send same data but add the START_STOP if(settings.role == ROLE_BLE_PERIPHERAL) { - if(btjoystickconnected) { + if(btjoystickconnected) { hid_SendJoystickChannels(channeldata); } btp_sendChannelData(_otxbuffer, otxbufferIndex+1); diff --git a/src/joystick/esp_hidd_prf_api.c b/src/joystick/esp_hidd_prf_api.c index b3f2ea9..46d5467 100644 --- a/src/joystick/esp_hidd_prf_api.c +++ b/src/joystick/esp_hidd_prf_api.c @@ -104,43 +104,61 @@ uint16_t esp_hidd_get_version(void) #define JOYSTICK_BUTTON_LOW 1250 struct PACKED { -#ifdef JOYSTICK_BUTTONS uint8_t but[2]; -#endif - uint8_t channels[16]; + uint16_t channels[8]; } report; void hid_SendJoystickChannels(uint16_t chans[8]) { uint16_t conn_id = btj_conn_id; - ESP_LOGI("JOY","Sending HID Report ch0 - %d", chans[0]); memcpy(report.channels, chans, sizeof(report.channels)); -#ifdef JOYSTICK_BUTTONS - report.but[0] = 0; - report.but[1] = 0; -#endif + report.but[0] = 0; + report.but[1] = 0; - for(int i=0; i < 8 ; i++) { - if(report.channels[i] == 0) // If disabled, center it - report.channels[i] = 1500; + for(int i=0; i < 8 ; i++) { + if(report.channels[i] == 0) // If disabled, center it + report.channels[i] = 1500; -#ifdef JOYSTICK_BUTTONS - if(report.channels[i] >= JOYSTICK_BUTTON_HIGH) { - report.but[0] |= 1<<(i * 2); - report.but[1] |= 1<<((i - 4) * 2); - } - - if(report.channels[i] <= JOYSTICK_BUTTON_LOW) { - report.but[0] |= 1<<((i * 2) + 1); - report.but[1] |= 1<<(((i - 4) * 2) + 1); - } -#endif + if(report.channels[i] >= JOYSTICK_BUTTON_HIGH) { + report.but[0] |= 1<<(i * 2); + report.but[1] |= 1<<((i - 4) * 2); + } - report.channels[i] -= 988; // Shift from center so it's 0-1024 + if(report.channels[i] <= JOYSTICK_BUTTON_LOW) { + report.but[0] |= 1<<((i * 2) + 1); + report.but[1] |= 1<<(((i - 4) * 2) + 1); } - hid_dev_send_report(hidd_le_env.gatt_if, conn_id, - HID_RPT_ID_MOUSE_IN, HID_REPORT_TYPE_INPUT, sizeof(report), (uint8_t*)&report); + report.channels[i] -= 988; // Shift from center so it's 0-1024 + } + + hid_dev_send_report(hidd_le_env.gatt_if, + conn_id, + HID_RPT_ID_MOUSE_IN, + HID_REPORT_TYPE_INPUT, + sizeof(report), + (uint8_t *)&report); } +void esp_hidd_send_joystick_value(uint16_t joystick_buttons, uint8_t joystick_x, uint8_t joystick_y, uint8_t joystick_z, uint8_t joystick_rx) +{ + uint16_t conn_id = btj_conn_id; + uint8_t buffer[HID_GAMEPAD_IN_RPT_LEN]; + ESP_LOGI(HID_LE_PRF_TAG, "the buttons value = %d js1 = %d, %d js2 = %d, %d", joystick_buttons, joystick_x, joystick_y, joystick_z, joystick_rx); + + buffer[0]= joystick_buttons & 0xff; + buffer[1] = ( joystick_buttons >> 8); + buffer[2] = ( joystick_x ^ 0x80 ); // X + buffer[3] = (( joystick_y ^ 0x80 ) * -1) - 1; // Y + buffer[4] = ( joystick_z ^ 0x80 ); // X + buffer[5] = (( joystick_rx ^ 0x80 ) * -1) - 1; // Y + + hid_dev_send_report(hidd_le_env.gatt_if, + conn_id, + HID_RPT_ID_MOUSE_IN, + HID_REPORT_TYPE_INPUT, + 6, + buffer); + return; +} \ No newline at end of file diff --git a/src/joystick/hid_device_le_prf.c b/src/joystick/hid_device_le_prf.c index c6274fa..f9e7e5f 100644 --- a/src/joystick/hid_device_le_prf.c +++ b/src/joystick/hid_device_le_prf.c @@ -34,193 +34,40 @@ struct prf_char_pres_fmt // HID report mapping table static hid_report_map_t hid_rpt_map[HID_NUM_REPORTS]; -#define JOYSTICK_BUTTONS - static const uint8_t hidReportMap[] = { - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x05, // USAGE (Game Pad) - 0xa1, 0x01, // COLLECTION (Application) - 0xa1, 0x00, // COLLECTION (Physical) -#ifdef JOYSTICK_BUTTONS - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x10, // USAGE_MAXIMUM (Button 8) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x95, 0x10, // REPORT_COUNT (8) - 0x75, 0x01, // REPORT_SIZE (1) - 0x81, 0x02, // INPUT (Data,Var,Abs) -#endif - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x09, 0x32, // USAGE (Z) - 0x09, 0x33, // USAGE (Rx) - 0x09, 0x34, // USAGE (Ry) - 0x09, 0x35, // USAGE (Rz) - 0x09, 0x36, // USAGE (Slider) - 0x09, 0x36, // USAGE (Slider) - 0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0) - 0x26, 0xFF, 0x03, // LOGICAL_MAXIMUM (1024) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x08, // REPORT_COUNT (8) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0xc0, // END_COLLECTION - 0xc0 // END_COLLECTION +0x05, 0x01, // Usage Page (Generic Desktop Ctrls) +0x09, 0x05, // Usage (Game Pad) +0xA1, 0x01, // Collection (Application) +0x85, 0x01, // Report Id (1) +0xA1, 0x00, // Collection (Physical) +0x05, 0x09, // Usage Page (Button) +0x19, 0x01, // Usage Minimum (0x01) +0x29, 0x10, // Usage Maximum (0x10) +0x15, 0x00, // Logical Minimum (0) +0x25, 0x01, // Logical Maximum (1) +0x95, 0x10, // Report Count (16) +0x75, 0x01, // Report Size (1) +0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) +0x05, 0x01, // Usage Page (Generic Desktop Ctrls) +0x09, 0x30, // Usage (X) +0x09, 0x31, // Usage (Y) +0x09, 0x32, // Usage (Z) +0x09, 0x33, // Usage (Rx) +0x09, 0x34, // Usage (Ry) +0x09, 0x35, // Usage (Rz) +0x09, 0x36, // Usage (Slider) +0x09, 0x36, // Usage (Slider) +0x16, 0x00, 0x00, // Logical Minimum (0) +0x26, 0xFF, 0x03, // Logical Maximum (1023) +0x75, 0x10, // Report Size (16) +0x95, 0x08, // Report Count (8) +0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) +0xC0, // End Collection +0xC0 // End Collection + +// 56 bytes }; - -// HID Report Map characteristic value -// Keyboard report descriptor (using format for Boot interface descriptor) -/*static const uint8_t hidReportMap[] = { - 0x05, 0x01, // Usage Page (Generic Desktop) - 0x09, 0x02, // Usage (Mouse) - 0xA1, 0x01, // Collection (Application) - 0x85, 0x01, // Report Id (1) - 0x09, 0x01, // Usage (Pointer) - 0xA1, 0x00, // Collection (Physical) - 0x05, 0x09, // Usage Page (Buttons) - 0x19, 0x01, // Usage Minimum (01) - Button 1 - 0x29, 0x03, // Usage Maximum (03) - Button 3 - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x03, // Report Count (3) - 0x81, 0x02, // Input (Data, Variable, Absolute) - Button states - 0x75, 0x05, // Report Size (5) - 0x95, 0x01, // Report Count (1) - 0x81, 0x01, // Input (Constant) - Padding or Reserved bits - 0x05, 0x01, // Usage Page (Generic Desktop) - 0x09, 0x30, // Usage (X) - 0x09, 0x31, // Usage (Y) - 0x09, 0x38, // Usage (Wheel) - 0x15, 0x81, // Logical Minimum (-127) - 0x25, 0x7F, // Logical Maximum (127) - 0x75, 0x08, // Report Size (8) - 0x95, 0x03, // Report Count (3) - 0x81, 0x06, // Input (Data, Variable, Relative) - X & Y coordinate - 0xC0, // End Collection - 0xC0, // End Collection - - 0x05, 0x01, // Usage Pg (Generic Desktop) - 0x09, 0x06, // Usage (Keyboard) - 0xA1, 0x01, // Collection: (Application) - 0x85, 0x02, // Report Id (2) - // - 0x05, 0x07, // Usage Pg (Key Codes) - 0x19, 0xE0, // Usage Min (224) - 0x29, 0xE7, // Usage Max (231) - 0x15, 0x00, // Log Min (0) - 0x25, 0x01, // Log Max (1) - // - // Modifier byte - 0x75, 0x01, // Report Size (1) - 0x95, 0x08, // Report Count (8) - 0x81, 0x02, // Input: (Data, Variable, Absolute) - // - // Reserved byte - 0x95, 0x01, // Report Count (1) - 0x75, 0x08, // Report Size (8) - 0x81, 0x01, // Input: (Constant) - // - // LED report - 0x95, 0x05, // Report Count (5) - 0x75, 0x01, // Report Size (1) - 0x05, 0x08, // Usage Pg (LEDs) - 0x19, 0x01, // Usage Min (1) - 0x29, 0x05, // Usage Max (5) - 0x91, 0x02, // Output: (Data, Variable, Absolute) - // - // LED report padding - 0x95, 0x01, // Report Count (1) - 0x75, 0x03, // Report Size (3) - 0x91, 0x01, // Output: (Constant) - // - // Key arrays (6 bytes) - 0x95, 0x06, // Report Count (6) - 0x75, 0x08, // Report Size (8) - 0x15, 0x00, // Log Min (0) - 0x25, 0x65, // Log Max (101) - 0x05, 0x07, // Usage Pg (Key Codes) - 0x19, 0x00, // Usage Min (0) - 0x29, 0x65, // Usage Max (101) - 0x81, 0x00, // Input: (Data, Array) - // - 0xC0, // End Collection - // - 0x05, 0x0C, // Usage Pg (Consumer Devices) - 0x09, 0x01, // Usage (Consumer Control) - 0xA1, 0x01, // Collection (Application) - 0x85, 0x03, // Report Id (3) - 0x09, 0x02, // Usage (Numeric Key Pad) - 0xA1, 0x02, // Collection (Logical) - 0x05, 0x09, // Usage Pg (Button) - 0x19, 0x01, // Usage Min (Button 1) - 0x29, 0x0A, // Usage Max (Button 10) - 0x15, 0x01, // Logical Min (1) - 0x25, 0x0A, // Logical Max (10) - 0x75, 0x04, // Report Size (4) - 0x95, 0x01, // Report Count (1) - 0x81, 0x00, // Input (Data, Ary, Abs) - 0xC0, // End Collection - 0x05, 0x0C, // Usage Pg (Consumer Devices) - 0x09, 0x86, // Usage (Channel) - 0x15, 0xFF, // Logical Min (-1) - 0x25, 0x01, // Logical Max (1) - 0x75, 0x02, // Report Size (2) - 0x95, 0x01, // Report Count (1) - 0x81, 0x46, // Input (Data, Var, Rel, Null) - 0x09, 0xE9, // Usage (Volume Up) - 0x09, 0xEA, // Usage (Volume Down) - 0x15, 0x00, // Logical Min (0) - 0x75, 0x01, // Report Size (1) - 0x95, 0x02, // Report Count (2) - 0x81, 0x02, // Input (Data, Var, Abs) - 0x09, 0xE2, // Usage (Mute) - 0x09, 0x30, // Usage (Power) - 0x09, 0x83, // Usage (Recall Last) - 0x09, 0x81, // Usage (Assign Selection) - 0x09, 0xB0, // Usage (Play) - 0x09, 0xB1, // Usage (Pause) - 0x09, 0xB2, // Usage (Record) - 0x09, 0xB3, // Usage (Fast Forward) - 0x09, 0xB4, // Usage (Rewind) - 0x09, 0xB5, // Usage (Scan Next) - 0x09, 0xB6, // Usage (Scan Prev) - 0x09, 0xB7, // Usage (Stop) - 0x15, 0x01, // Logical Min (1) - 0x25, 0x0C, // Logical Max (12) - 0x75, 0x04, // Report Size (4) - 0x95, 0x01, // Report Count (1) - 0x81, 0x00, // Input (Data, Ary, Abs) - 0x09, 0x80, // Usage (Selection) - 0xA1, 0x02, // Collection (Logical) - 0x05, 0x09, // Usage Pg (Button) - 0x19, 0x01, // Usage Min (Button 1) - 0x29, 0x03, // Usage Max (Button 3) - 0x15, 0x01, // Logical Min (1) - 0x25, 0x03, // Logical Max (3) - 0x75, 0x02, // Report Size (2) - 0x81, 0x00, // Input (Data, Ary, Abs) - 0xC0, // End Collection - 0x81, 0x03, // Input (Const, Var, Abs) - 0xC0, // End Collectionq - -#if (SUPPORT_REPORT_VENDOR == true) - 0x06, 0xFF, 0xFF, // Usage Page(Vendor defined) - 0x09, 0xA5, // Usage(Vendor Defined) - 0xA1, 0x01, // Collection(Application) - 0x85, 0x04, // Report Id (4) - 0x09, 0xA6, // Usage(Vendor defined) - 0x09, 0xA9, // Usage(Vendor defined) - 0x75, 0x08, // Report Size - 0x95, 0x7F, // Report Count = 127 Btyes - 0x91, 0x02, // Output(Data, Variable, Absolute) - 0xC0, // End Collection -#endif - -};*/ - /// Battery Service Attributes Indexes enum { @@ -582,7 +429,7 @@ void esp_hidd_prf_cb_hdl(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, { switch(event) { case ESP_GATTS_REG_EVT: { - esp_ble_gap_config_local_icon (ESP_BLE_APPEARANCE_GENERIC_HID); + esp_ble_gap_config_local_icon (ESP_BLE_APPEARANCE_HID_GAMEPAD); esp_hidd_cb_param_t hidd_param; hidd_param.init_finish.state = param->reg.status; if(param->reg.app_id == HIDD_APP_ID) { From d0d0a98aeab25077e54b3742741ed359f19cd494 Mon Sep 17 00:00:00 2001 From: Cliff Blackburn Date: Sun, 10 Jul 2022 23:36:13 -0600 Subject: [PATCH 08/17] Joystick working --- platformio.ini | 4 ++-- src/bt.h | 4 +--- src/espaudio.c | 2 ++ src/espdefs.h | 15 +++++++++++++-- src/espjoystick.c | 36 ++++++++++++++++++++++++++++++++---- src/esproot.c | 4 +++- src/esptrainer.c | 12 ++++-------- src/frskybt.c | 30 +++++++++++++++--------------- src/settings.c | 5 +++-- src/settings.h | 3 ++- src/terminal.c | 16 +++++++--------- 11 files changed, 84 insertions(+), 47 deletions(-) diff --git a/platformio.ini b/platformio.ini index 49cd8bf..f8a95f1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -78,8 +78,8 @@ build_flags = board = esp32-c3-devkitm-1 upload_protocol = esptool monitor_speed = 115200 -upload_port = COM21 -monitor_port = COM21 +upload_port = COM30 +monitor_port = COM30 #debug_tool = esptool build_flags = -DRTOS_FREERTOS=y diff --git a/src/bt.h b/src/bt.h index 4f1a9c9..861894c 100644 --- a/src/bt.h +++ b/src/bt.h @@ -2,9 +2,7 @@ #include "esp_bt_defs.h" -#define BT_PAUSE_BEFORE_RESTART 250 //ms - - +#define BT_PAUSE_BEFORE_RESTART 500 //ms extern esp_bd_addr_t rmtbtaddress; extern esp_bd_addr_t localbtaddress; diff --git a/src/espaudio.c b/src/espaudio.c index 3c503eb..69017f7 100644 --- a/src/espaudio.c +++ b/src/espaudio.c @@ -6,11 +6,13 @@ int espAudioStart() { + ESP_LOGI(AUDIO_TAG, "Audio Starting"); return -1; } void espAudioStop() { + ESP_LOGI(AUDIO_TAG, "Audio Stop"); } diff --git a/src/espdefs.h b/src/espdefs.h index e9a35fa..85e674b 100644 --- a/src/espdefs.h +++ b/src/espdefs.h @@ -22,14 +22,15 @@ enum ESPModes { }; enum ESPRootCmds { + ESP_ROOTCMD_ACKNAK=0, ESP_ROOTCMD_START_MODE, ESP_ROOTCMD_STOP_MODE, ESP_ROOTCMD_RESTART, ESP_ROOTCMD_GET_VER, ESP_ROOTCMD_EVENT, - ESP_ROOTCMD_ACKNAK, }; + enum ESPEvents { ESP_EVT_DISCOVER_STARTED, ESP_EVT_DISCOVER_COMPLETE, @@ -44,4 +45,14 @@ enum ESPTrainerCmds { ESP_TRAINERCMD_SET_MASTER, ESP_TRAINERCMD_SET_SLAVE, ESP_TRAINERCMD_COUNT, -}; \ No newline at end of file +}; + +// Channel Data (Joystick, Trainer) + +#define MAX_OUTPUT_CHANNELS 32 + +// Channel Format +typedef struct { + int16_t ch[MAX_OUTPUT_CHANNELS]; + uint32_t channelmask; // Valid Channels +} channeldata; diff --git a/src/espjoystick.c b/src/espjoystick.c index 6276696..bc4b345 100644 --- a/src/espjoystick.c +++ b/src/espjoystick.c @@ -1,27 +1,55 @@ #include #include "espjoystick.h" #include "esp_log.h" +#include "joystick/bt_joystick.h" +#include "joystick/hidd_le_prf_int.h" +#include "bt.h" #define JOYSTICK_TAG "JOY" +bool joystickstarted=false; + int espJoystickStart() { - return -1; + ESP_LOGI(JOYSTICK_TAG, "Joystick Starting"); + if(joystickstarted) return -1; + joystickstarted = true; + bt_init(); + btjoyInit(); + return 0; } void espJoystickStop() { - + ESP_LOGI(JOYSTICK_TAG, "Joystick Stop"); + if(joystickstarted) { + bt_disable(); + } + joystickstarted = false; } bool espJoystickRunning() { - return false; + + return joystickstarted; } void espJoystickData(const uint8_t *data, uint8_t len) { - // Read audio data, resample and write to bluetooth + if(len == sizeof(channeldata)) { + const channeldata *chdata = (const channeldata *)data; + for(int i=0; i < 8; i++) { + if(chdata->channelmask & 1<ch[i]); + } + printf("\r\n"); + + hid_SendJoystickChannels((uint16_t *)data); + + } else { + ESP_LOGE(JOYSTICK_TAG, "Unknown Data"); + } + } diff --git a/src/esproot.c b/src/esproot.c index 7146508..c3084ad 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -24,6 +24,7 @@ void espRootData(const uint8_t *data, uint8_t len) void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { + ESP_LOGI(LOG_ESPR, "Root Command %d, Extra %d", command, data[0]); uint8_t mode = data[0]; switch(command) { @@ -101,7 +102,8 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) // if(espIMURunning()) { // espTIMUStop(); // } - break; + break; + default: break; } diff --git a/src/esptrainer.c b/src/esptrainer.c index c19416e..fa48d68 100644 --- a/src/esptrainer.c +++ b/src/esptrainer.c @@ -2,26 +2,22 @@ #include "esptrainer.h" #include "esp_log.h" -#define TRAINER_TAG "TRNR" -#define MAX_OUTPUT_CHANNELS 32 - -// Channel Format -typedef struct { - int16_t ch[MAX_OUTPUT_CHANNELS]; - uint32_t channelmask; // Valid Channels -} channeldata; +#define TRAINER_TAG "TRAINER" bool trainerstarted=false; int espTrainerStart() { + ESP_LOGI(TRAINER_TAG, "Trainer Start"); if(trainerstarted) return -1; + trainerstarted = true; return 0; } void espTrainerStop() { + ESP_LOGI(TRAINER_TAG, "Trainer Stop"); if(!trainerstarted) return; trainerstarted = false; diff --git a/src/frskybt.c b/src/frskybt.c index 8e316dd..70844c9 100644 --- a/src/frskybt.c +++ b/src/frskybt.c @@ -15,7 +15,7 @@ #define BLUETOOTH_LINE_LENGTH 32 #define BLUETOOTH_PACKET_SIZE 14 -uint16_t channeldata[BT_CHANNELS]; +uint16_t frchanneldata[BT_CHANNELS]; /** * @brief Displays the decoded channel values and time since last receive @@ -34,14 +34,14 @@ void logBTFrame(bool valid, char message[]) } else { ESP_LOGI(FRSKYBT_TAG, "(%05lld)Ch1[%04d] Ch2[%04d] Ch3[%04d] Ch4[%04d] Ch5[%04d] Ch6[%04d] Ch7[%04d] Ch8[%04d]", timestamp, - channeldata[0], - channeldata[1], - channeldata[2], - channeldata[3], - channeldata[4], - channeldata[5], - channeldata[6], - channeldata[7]); + frchanneldata[0], + frchanneldata[1], + frchanneldata[2], + frchanneldata[3], + frchanneldata[4], + frchanneldata[5], + frchanneldata[6], + frchanneldata[7]); } } @@ -121,17 +121,17 @@ void processTrainerFrame(const uint8_t * otxbuffer) { for (uint8_t channel=0, i=1; channel> 4) + ((otxbuffer[i+2] & 0x0f) << 8); + frchanneldata[channel] = otxbuffer[i] + ((otxbuffer[i+1] & 0xf0) << 4); + frchanneldata[channel+1] = ((otxbuffer[i+1] & 0x0f) << 4) + ((otxbuffer[i+2] & 0xf0) >> 4) + ((otxbuffer[i+2] & 0x0f) << 8); } // If the data came from the radio, send it out over bluetooth. Send same data but add the START_STOP - if(settings.role == ROLE_BLE_PERIPHERAL) { +/* if(settings.mode == ROLE_BLE_PERIPHERAL) { if(btjoystickconnected) { - hid_SendJoystickChannels(channeldata); + hid_SendJoystickChannels(frchanneldata); } - btp_sendChannelData(_otxbuffer, otxbufferIndex+1); - } + btp_sendfrchanneldata(_otxbuffer, otxbufferIndex+1); + }*/ } void frSkyProcessByte(uint8_t data) diff --git a/src/settings.c b/src/settings.c index 0a4eecb..4e6399a 100644 --- a/src/settings.c +++ b/src/settings.c @@ -2,6 +2,7 @@ #include "nvs_flash.h" #include "settings.h" + #define LOG_SET "SETTINGS" #define NVS_STRUCT_KEY "skey" @@ -9,7 +10,7 @@ void _loadSettings(settings_t *set); settings_t settings = { - .role = ROLE_UNKNOWN + .mode = ESP_ROOT, }; extern nvs_handle_t nvs_flsh_btw; volatile bool settings_ok = false; @@ -24,7 +25,7 @@ void saveSettings() ESP_LOGI(LOG_SET, "No data differs, not writing to flash"); return; } else { - ESP_LOGI(LOG_SET, "Data is different, FR=%d, MR=%d", flashsettings.role, settings.role); + ESP_LOGI(LOG_SET, "Data is different, FR=%d, MR=%d", flashsettings.mode, settings.mode); } esp_err_t err = nvs_set_blob(nvs_flsh_btw, NVS_STRUCT_KEY, (void *)&settings, sizeof(settings_t)); diff --git a/src/settings.h b/src/settings.h index b414f9b..fe41786 100644 --- a/src/settings.h +++ b/src/settings.h @@ -2,11 +2,12 @@ #include "defines.h" #include "bt_client.h" +#include "espdefs.h" typedef struct { //uint8_t version // Todo the version info here, check on load it matches otherwise ignore char rmtbtaddr[13]; - role_t role; + uint8_t mode; } settings_t; extern settings_t settings; diff --git a/src/terminal.c b/src/terminal.c index 1644edb..a0e9336 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -32,7 +32,6 @@ StreamBufferHandle_t uartrxstreamhndl; void runBT(); void setBaudRate(uint32_t baudRate); -void setRole(role_t role); volatile bool uartRXTaskStarted=false; @@ -114,9 +113,9 @@ void processPacket(const packet_s *packet) break; case ESP_JOYSTICK: if(ESP_PACKET_ISCMD(packet->type)) - espAudioCommand(packet->data[0], packet->data + 1, packet->len -1); + espJoystickCommand(packet->data[0], packet->data + 1, packet->len -1); else - espAudioData(packet->data, packet->len); + espJoystickData(packet->data, packet->len); break; case ESP_AUDIO: if(ESP_PACKET_ISCMD(packet->type)) @@ -137,11 +136,10 @@ void mainTask(void *stuff) { ESP_LOGI(LOG_UART, "Waiting for settings to be read"); while(!settings_ok) {vTaskDelay(50);}; // Pause until settings are read ESP_LOGI(LOG_UART, "Setting initial role"); - if(settings.role == ROLE_UNKNOWN) { - ESP_LOGE(LOG_UART, "Invalid role loaded, defaulting to central"); - settings.role = ROLE_BLE_PERIPHERAL; + if(settings.mode == ESP_ROOT) { + ESP_LOGE(LOG_UART, "No Role Loaded, Leaving off.. for now"); } - setRole(settings.role); +// setRole(settings.role); ESP_LOGI(LOG_UART, "Waiting for UART RX Task to start"); while(!uartRXTaskStarted); @@ -181,7 +179,7 @@ void mainTask(void *stuff) { vTaskDelete(NULL); } -void setRole(role_t role) +/*void setRole(role_t role) { ESP_LOGI(LOG_UART,"Switching from mode %d to %d", curMode, role); if(role == curMode) return; @@ -218,7 +216,7 @@ void setRole(role_t role) // Save new role to flash saveSettings(); -} +}*/ void runBTCentral() From 85f6cf030cd24f389eb269f58ab7a4f9b3bde013 Mon Sep 17 00:00:00 2001 From: Cliff Blackburn Date: Sun, 10 Jul 2022 23:56:35 -0600 Subject: [PATCH 09/17] Only send on connected --- src/espjoystick.c | 8 ++++---- src/terminal.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/espjoystick.c b/src/espjoystick.c index bc4b345..930bdaf 100644 --- a/src/espjoystick.c +++ b/src/espjoystick.c @@ -37,14 +37,14 @@ bool espJoystickRunning() void espJoystickData(const uint8_t *data, uint8_t len) { if(len == sizeof(channeldata)) { - const channeldata *chdata = (const channeldata *)data; + /*const channeldata *chdata = (const channeldata *)data; for(int i=0; i < 8; i++) { if(chdata->channelmask & 1<ch[i]); } - printf("\r\n"); - - hid_SendJoystickChannels((uint16_t *)data); + printf("\r\n");*/ + if(btjoystickconnected) + hid_SendJoystickChannels((uint16_t *)data); } else { ESP_LOGE(JOYSTICK_TAG, "Unknown Data"); diff --git a/src/terminal.c b/src/terminal.c index a0e9336..0010cf6 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -396,7 +396,7 @@ void uartRXTask(void *n) UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); - uartrxstreamhndl = xStreamBufferCreate(UART_RX_BUFFER,1); + uartrxstreamhndl = xStreamBufferCreate(UART_RX_BUFFER*2,1); if( uartrxstreamhndl == NULL ) { ESP_LOGE("UARTRX","NOT ENOUGH HEAP!"); From e43ce50638331ed11dd8df05c34479c5994e0298 Mon Sep 17 00:00:00 2001 From: Cliff Blackburn Date: Wed, 13 Jul 2022 00:41:28 -0600 Subject: [PATCH 10/17] Packet sending, connection mngr events --- src/cobs.c | 2 +- src/espaudio.c | 2 +- src/espconmngr.c | 62 ++++++++++++++++++++++++++++++++++ src/espconmngr.h | 7 ++++ src/espdefs.h | 44 +++++++++++++++++++++---- src/espjoystick.c | 1 - src/esproot.c | 84 ++++++++++++++++++++++++++++++----------------- src/esptrainer.c | 52 ++++++++++++++++++++++++++--- src/esptrainer.h | 1 + src/terminal.c | 78 +++++++++++++++++-------------------------- src/terminal.h | 7 ++-- 11 files changed, 244 insertions(+), 96 deletions(-) create mode 100644 src/espconmngr.c create mode 100644 src/espconmngr.h diff --git a/src/cobs.c b/src/cobs.c index 4c734f5..15b51bb 100644 --- a/src/cobs.c +++ b/src/cobs.c @@ -3,7 +3,7 @@ int cobs_encode(const uint8_t* buffer, int size, uint8_t* encodedBuffer) { int read_index = 0; - int write_index = 0; + int write_index = 1; int code_index = 0; uint8_t code = 1; diff --git a/src/espaudio.c b/src/espaudio.c index 69017f7..863dcf4 100644 --- a/src/espaudio.c +++ b/src/espaudio.c @@ -23,7 +23,7 @@ bool espAudioRunning() void espAudioData(const uint8_t *data, uint8_t len) { - // Read audio data, resample and write to bluetooth + // Read audio data, resample and write to bluetooth } diff --git a/src/espconmngr.c b/src/espconmngr.c new file mode 100644 index 0000000..21a7926 --- /dev/null +++ b/src/espconmngr.c @@ -0,0 +1,62 @@ +#include +#include "espconmngr.h" +#include "esp_log.h" +#include "terminal.h" + +#define CON_MGR_TAG "CONMGR" + +void connectionCommandRX(int conCmd, const uint8_t * data, int len) +{ + switch (conCmd) + { + case ESP_CON_DISCOVER_START: + ESP_LOGI(CON_MGR_TAG, "Discovery Start Req"); + break; + case ESP_CON_DISCOVER_STOP: + ESP_LOGI(CON_MGR_TAG, "Discovery Stop Req"); + break; + case ESP_CON_CONNECT: + ESP_LOGI(CON_MGR_TAG, "Connect to device %s", (const char *)data); + break; + case ESP_CON_DISCONNECT: + ESP_LOGI(CON_MGR_TAG, "Disconnect"); + break; + case ESP_CON_SET_NAME: + ESP_LOGI(CON_MGR_TAG, "Setting name=%s", (const char *)data); + break; + case ESP_CON_SET_MAC: + ESP_LOGI(CON_MGR_TAG, "Setting mac=%s", (const char *)data); + break; + case ESP_CON_SET_SSID: + ESP_LOGI(CON_MGR_TAG, "Setting ssid=%s", (const char *)data); + break; + case ESP_CON_SET_IP: + ESP_LOGI(CON_MGR_TAG, "Setting id=%s", (const char *)data); + break; + case ESP_CON_SET_SUBNET_MASK: + ESP_LOGI(CON_MGR_TAG, "Setting subnetmask=%s", (const char *)data); + break; + case ESP_CON_SET_STATIC_IP: + ESP_LOGI(CON_MGR_TAG, "Setting to static ip mode"); + break; + case ESP_CON_SET_DHCP: + ESP_LOGI(CON_MGR_TAG, "Setting DHCP=%s", (const char *)data); + break; + case ESP_CON_SET_WIFI_STATION: + ESP_LOGI(CON_MGR_TAG, "Setting WIFI to station mode"); + break; + case ESP_CON_SET_WIFI_AP: + ESP_LOGI(CON_MGR_TAG, "Setting WIFI to ap mode"); + break; + } +} + +void connectionEventRX(int event, const uint8_t *data, int len) +{ + +} + +void sendEvent(int event, const uint8_t *data, int len) +{ + writeCommand(ESP_ROOT, ESP_ROOTCMD_CON_EVENT, data, len); +} \ No newline at end of file diff --git a/src/espconmngr.h b/src/espconmngr.h new file mode 100644 index 0000000..40bc517 --- /dev/null +++ b/src/espconmngr.h @@ -0,0 +1,7 @@ +#pragma once + +#include "espdefs.h" + +void connectionCommandRX(int conCmd, const uint8_t * data, int len); +void connectionEventRX(int event, const uint8_t *data, int len); +void sendEvent(int event, const uint8_t *data, int len); diff --git a/src/espdefs.h b/src/espdefs.h index 85e674b..defbe46 100644 --- a/src/espdefs.h +++ b/src/espdefs.h @@ -26,25 +26,40 @@ enum ESPRootCmds { ESP_ROOTCMD_START_MODE, ESP_ROOTCMD_STOP_MODE, ESP_ROOTCMD_RESTART, - ESP_ROOTCMD_GET_VER, - ESP_ROOTCMD_EVENT, + ESP_ROOTCMD_VERSION, + ESP_ROOTCMD_CON_EVENT, + ESP_ROOTCMD_CON_MGMNT, }; - -enum ESPEvents { - ESP_EVT_DISCOVER_STARTED, +enum ESPConnectionEvents { + ESP_EVT_DISCOVER_STARTED, ESP_EVT_DISCOVER_COMPLETE, ESP_EVT_DEVICE_FOUND, ESP_EVT_CONNECTED, - ESP_EVT_DISCONNECTED, + ESP_EVT_DISCONNECTED, ESP_EVT_PIN_REQUEST, ESP_EVT_IP_OBTAINED }; +enum ESPConnectionManagment { + ESP_CON_DISCOVER_START, + ESP_CON_DISCOVER_STOP, + ESP_CON_CONNECT, + ESP_CON_DISCONNECT, + ESP_CON_SET_NAME, + ESP_CON_SET_MAC, + ESP_CON_SET_SSID, + ESP_CON_SET_IP, + ESP_CON_SET_SUBNET_MASK, + ESP_CON_SET_STATIC_IP, + ESP_CON_SET_DHCP, + ESP_CON_SET_WIFI_STATION, + ESP_CON_SET_WIFI_AP, +}; + enum ESPTrainerCmds { ESP_TRAINERCMD_SET_MASTER, ESP_TRAINERCMD_SET_SLAVE, - ESP_TRAINERCMD_COUNT, }; // Channel Data (Joystick, Trainer) @@ -56,3 +71,18 @@ typedef struct { int16_t ch[MAX_OUTPUT_CHANNELS]; uint32_t channelmask; // Valid Channels } channeldata; + +typedef struct { + uint8_t bteaddr[6]; + uint8_t rssi; + char name[30]; +} btscanresult; + +typedef struct { + uint8_t maj; + uint8_t min; + uint8_t rev; + uint8_t sha[10]; +} espversion; + +extern espversion espVersion; \ No newline at end of file diff --git a/src/espjoystick.c b/src/espjoystick.c index 930bdaf..08fc884 100644 --- a/src/espjoystick.c +++ b/src/espjoystick.c @@ -45,7 +45,6 @@ void espJoystickData(const uint8_t *data, uint8_t len) printf("\r\n");*/ if(btjoystickconnected) hid_SendJoystickChannels((uint16_t *)data); - } else { ESP_LOGE(JOYSTICK_TAG, "Unknown Data"); } diff --git a/src/esproot.c b/src/esproot.c index c3084ad..d96fcc6 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -8,14 +8,21 @@ #define LOG_ESPR "ESPROOT" -int rv; -#define STARTBLE_MODE(m,x) rv=x; writeAckNak(m,rv,""); if(rv==0) radioused=true; -#define STARTBTEDR_MODE(m,x) rv=x; writeAckNak(m,rv,""); if(rv==0) radioused=true; -#define STARTWIFI_MODE(m,x) rv=x; writeAckNak(m,rv,""); if(rv==0) radioused=true; +// TODO, Replace me with git SHA +espversion espVersion = {1,0,0,"GITTAG"}; + +int g_rv; +bool g_radioIsBTEDR=false; +bool g_radioIsBLE=false; +bool g_radioIsWIFI=false; + +#define STARTBLE_MODE(m,x) g_rv=x; writeAckNak(m,g_rv,""); if(g_rv==0) g_radioIsBLE=true; +#define STARTBTEDR_MODE(m,x) g_rv=x; writeAckNak(m,g_rv,""); if(g_rv==0) g_radioIsBTEDR=true; +#define STARTWIFI_MODE(m,x) g_rv=x; writeAckNak(m,g_rv,""); if(g_rv==0) g_radioIsWIFI=true; #define START_MODE(m,x) writeAckNak(m,x,""); +#define RADIO_USED() (g_radioIsBTEDR || g_radioIsBLE || g_radioIsWIFI) uint8_t runningModes; -bool radioused=false; void espRootData(const uint8_t *data, uint8_t len) { @@ -24,20 +31,19 @@ void espRootData(const uint8_t *data, uint8_t len) void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { - ESP_LOGI(LOG_ESPR, "Root Command %d, Extra %d", command, data[0]); - uint8_t mode = data[0]; - switch(command) { + ESP_LOGI(LOG_ESPR, "Root Command %d", command, data[0]); - // Startup Command + switch(command) { case ESP_ROOTCMD_START_MODE: + if(len != 1) break; ESP_LOGI(LOG_ESPR, "Starting Mode %d", data[0]); - if(radioused && ((mode == ESP_TELEMETRY || - mode == ESP_TRAINER || - mode == ESP_JOYSTICK || - mode == ESP_AUDIO || - mode == ESP_FTP))) { + if(RADIO_USED() && ((data[0] == ESP_TELEMETRY || + data[0] == ESP_TRAINER || + data[0] == ESP_JOYSTICK || + data[0] == ESP_AUDIO || + data[0] == ESP_FTP))) { ESP_LOGE(LOG_ESPR, "Cannot start Radio already used"); - writeAckNak(mode,false, "Radio already used"); // Write error + writeAckNak(mode,false, "Radio already used"); } else { switch (mode) { @@ -64,43 +70,45 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) } } break; - + case ESP_ROOTCMD_STOP_MODE: + if(len != 1) break; ESP_LOGI(LOG_ESPR, "Stopping Mode %d", data[0]); - switch (mode) { + switch (data[0]) { case ESP_TELEMETRY: - // if(espTrainerRunning()) { - // espTrainerStop(); - // radioused = false; + // if(espTelemetryRunning()) { + // espTelemetryStop(); + // g_radioIsBTEDR = false; // WHAT TO DO, I COULD SEE BOTH.. + // probably EDR with a serial port tho // } break; case ESP_TRAINER: if(espTrainerRunning()) { espTrainerStop(); - radioused = false; + g_radioIsBLE = false; } break; case ESP_JOYSTICK: if(espJoystickRunning()) { espJoystickStop(); - radioused = false; + g_radioIsBLE = false; } break; case ESP_AUDIO: if(espAudioRunning()) { espAudioStop(); - radioused = false; + g_radioIsBTEDR = false; } break; case ESP_FTP: - // if(espTrainerRunning()) { - // espTrainerStop(); - // radioused = false; + // if(espFTPRunning()) { + // espFTPStop(); + // radioUsed = false; // } break; case ESP_IMU: - // if(espIMURunning()) { - // espTIMUStop(); + // if(espFTPRunning()) { + // espFTPStop(); // } break; @@ -110,11 +118,27 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) break; case ESP_ROOTCMD_RESTART: - ESP_LOGI(LOG_ESPR, "Rebooting"); + ESP_LOGI(LOG_ESPR, "Rebooting..."); esp_restart(); break; - case ESP_ROOTCMD_GET_VER: + case ESP_ROOTCMD_VERSION: + writeCommand(ESP_ROOT, + ESP_ROOTCMD_VERSION, + (const uint8_t *)&espVersion, + sizeof(espversion)); + break; + case ESP_ROOTCMD_CON_EVENT: // Shouldn't be much here, events are generated here + if(len < 1) break; + ESP_LOGI(LOG_ESPR, "Con Mgr. Event %d", data[0]); + connectionEventRX(data[0], data + 1, len -1); + break; + case ESP_ROOTCMD_CON_MGMNT: + if(len < 1) break; + ESP_LOGI(LOG_ESPR, "Con Mgr. Cmd %d", data[0]); + connectionCommandRX(data[0], data + 1, len -1); break; } + } + diff --git a/src/esptrainer.c b/src/esptrainer.c index fa48d68..8813422 100644 --- a/src/esptrainer.c +++ b/src/esptrainer.c @@ -1,25 +1,50 @@ #include #include "esptrainer.h" +#include "terminal.h" #include "esp_log.h" +#include "bt.h" +#include "bt_client.h" +#include "bt_server.h" #define TRAINER_TAG "TRAINER" bool trainerstarted=false; +bool trainerismaster=false; int espTrainerStart() { - ESP_LOGI(TRAINER_TAG, "Trainer Start"); - if(trainerstarted) return -1; - trainerstarted = true; + if(trainerstarted) + return -1; + + if(trainerismaster) { + ESP_LOGI(TRAINER_TAG, "Trainer Master Start"); + // Start Master Mode + bt_init(); + btpInit(); + /*writeCommand(ESP_TRAINER, + ESP_TRAINERCMD_SET_MASTER, + NULL, + 0);*/ + } else { + ESP_LOGI(TRAINER_TAG, "Trainer Slave Start"); + // Start Slave Mode + bt_init(); + btcInit(); + /*writeCommand(ESP_TRAINER, + ESP_TRAINERCMD_SET_MASTER, + NULL, + 0);*/ + } + trainerstarted = true; return 0; } void espTrainerStop() { ESP_LOGI(TRAINER_TAG, "Trainer Stop"); - if(!trainerstarted) return; - + if(trainerstarted) + bt_disable(); trainerstarted = false; } @@ -47,10 +72,27 @@ void espTrainerData(const uint8_t *data, uint8_t len) void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len) { ESP_LOGI(TRAINER_TAG, "Got A Command %d", command); + bool switchmodes=false; switch(command) { case ESP_TRAINERCMD_SET_MASTER: + if(!trainerismaster) + switchmodes = true; break; case ESP_TRAINERCMD_SET_SLAVE: + if(!trainerismaster) + switchmodes = true; break; } + + if(switchmodes) { + trainerismaster = !trainerismaster; + espTrainerStop(); + espTrainerStart(); + } +} + +void espTrainerSend(const channeldata *chans) +{ + writePacket((const uint8_t *)chans, sizeof(channeldata), false, ESP_TRAINER); } + diff --git a/src/esptrainer.h b/src/esptrainer.h index f58c051..5cb20a1 100644 --- a/src/esptrainer.h +++ b/src/esptrainer.h @@ -7,3 +7,4 @@ void espTrainerStop(); bool espTrainerRunning(); void espTrainerData(const uint8_t *data, uint8_t len); void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len); +void espTrainerSend(const channeldata *chans); \ No newline at end of file diff --git a/src/terminal.c b/src/terminal.c index 0010cf6..b9fb7e0 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -19,6 +19,7 @@ #include "esproot.h" #include "esptrainer.h" #include "espaudio.h" +#include "espjoystick.h" #include "freertos/stream_buffer.h" #define LOG_UART "UART" @@ -30,9 +31,6 @@ StreamBufferHandle_t uartrxstreamhndl; -void runBT(); -void setBaudRate(uint32_t baudRate); - volatile bool uartRXTaskStarted=false; const uart_port_t uart_num = UART_NUM; @@ -134,7 +132,7 @@ void processPacket(const packet_s *packet) void mainTask(void *stuff) { ESP_LOGI(LOG_UART, "Waiting for settings to be read"); - while(!settings_ok) {vTaskDelay(50);}; // Pause until settings are read + while(!settings_ok) {vTaskDelay(50);}; ESP_LOGI(LOG_UART, "Setting initial role"); if(settings.mode == ESP_ROOT) { ESP_LOGE(LOG_UART, "No Role Loaded, Leaving off.. for now"); @@ -142,14 +140,14 @@ void mainTask(void *stuff) { // setRole(settings.role); ESP_LOGI(LOG_UART, "Waiting for UART RX Task to start"); - while(!uartRXTaskStarted); + while(!uartRXTaskStarted) {vTaskDelay(20);} packet_s packet; uint8_t buffer[sizeof(packet_s)+1]; int bufferpos =0; while (1) { - char inb; + uint8_t inb; int rec = xStreamBufferReceive(uartrxstreamhndl, &inb, 1, 10); if(rec) { if(inb == 0 && bufferpos != 0) { @@ -160,7 +158,6 @@ void mainTask(void *stuff) { packet.crch = 0xAA; uint16_t calccrc = crc16(0,(uint8_t *)&packet,lenout, 0); packet.len = lenout - PACKET_OVERHEAD; - // printf("in %d out %d data %d\r\n", len, lenout, packet.len); if(packetcrc == calccrc) { processPacket(&packet); } else { @@ -175,6 +172,14 @@ void mainTask(void *stuff) { } } } + + // TEST SENDING PACKETS +/* static uint count=0; + if(count++ % 20 == 0) { + printf("Writting: Hello, Mode=2\r\n"); + writePacket("Hello",5,false,2); + writeCommand(1,3,"DATA",4); + }*/ } vTaskDelete(NULL); } @@ -219,7 +224,7 @@ void mainTask(void *stuff) { }*/ -void runBTCentral() +/*void runBTCentral() { switch(btCentralState) { case CENTRAL_STATE_DISCONNECT: { @@ -317,74 +322,51 @@ void runBTPeripherial() } break; } -} - -void runBT() -{ - switch(curMode) { - case ROLE_BLE_CENTRAL: - runBTCentral(); - break; - case ROLE_BLE_PERIPHERAL: - runBTPeripherial(); - break; - /*case ROLE_BTEDR_AUDIO_SOURCE: - break; - case ROLE_ESPNOW_CENTRAL: - break; - case ROLE_ESPNOW_PERIPHERAL: - break;*/ - default: - break; - } -} +}*/ // Builds a packet -void writePacket(const uint8_t *dat, int len, bool iscmd, int mode) +void writePacket(const uint8_t *dat, int len, bool iscmd, uint8_t mode) { uint8_t encodedbuffer[sizeof(packet_s) + 1]; packet_s packet; packet.type = mode; - packet.type |= (iscmd << ESP_PACKET_CMD_BIT); + if(iscmd) + packet.type |= (1 << ESP_PACKET_CMD_BIT); packet.crcl = 0xBB; packet.crch = 0xAA; memcpy(packet.data, dat, len); // TODO, Remove me, extra copy for just the crc calc. uint16_t crc = crc16(0, (uint8_t *)&packet,len + PACKET_OVERHEAD, 0); packet.crcl = crc & 0xFF; packet.crch = (crc & 0xFF00) >> 8; - int wl = cobs_encode((uint8_t *)&packet, packet.len + PACKET_OVERHEAD, encodedbuffer); - - encodedbuffer[wl] = '\0'; // Null terminate packet, used for detection of packet end - - // Write the packet - uart_write_bytes(uart_num, (uint8_t *)&packet, wl+1); + + int wl = cobs_encode((uint8_t *)&packet, len + PACKET_OVERHEAD, encodedbuffer); + encodedbuffer[wl] = '\0'; + uart_write_bytes(uart_num, (void *)&encodedbuffer, wl+1); } // Sends some data -void writeData(int mode, const uint8_t *dat, int len) +void writeData(uint8_t mode, const uint8_t *dat, int len) { writePacket(dat, len, false, mode); } // Send a command -void writeCommand(int mode, uint8_t command, const uint8_t *dat, int len) +void writeCommand(uint8_t mode, uint8_t command, const uint8_t *dat, int len) { - uint8_t *data = malloc(len+1); - memcpy(data + 1, dat, len); - data[0] = command; - writePacket(data, len+1, true, mode); - free(data); + uint8_t buffer[256]; + buffer[0] = command; // First byte of a command is the command, data follows + memcpy(buffer+1, dat, len); + writePacket(buffer, len+1, true, mode); } -// Writes an acknowledge/ not-acknowledge and a optional message -void writeAckNak(int mode, bool ack, const char *message) +// Writes an acknowledge / not-acknowledge and a optional message +void writeAckNak(uint8_t mode, bool ack, const char *message) { uint8_t command = ESP_ROOTCMD_ACKNAK | (ack?1< Date: Wed, 13 Jul 2022 00:47:05 -0600 Subject: [PATCH 11/17] Fix Root Cmd mode var --- src/esproot.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/esproot.c b/src/esproot.c index d96fcc6..1dd17e3 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -5,6 +5,7 @@ #include "espaudio.h" #include "terminal.h" #include "espdefs.h" +#include "espconmngr.h" #define LOG_ESPR "ESPROOT" @@ -43,9 +44,9 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) data[0] == ESP_AUDIO || data[0] == ESP_FTP))) { ESP_LOGE(LOG_ESPR, "Cannot start Radio already used"); - writeAckNak(mode,false, "Radio already used"); + writeAckNak(data[0],false, "Radio already used"); } else { - switch (mode) + switch (data[0]) { case ESP_TELEMETRY: //STARTRADIO_MODE(ESP_TELEMETRY, espTelemetryStart()); From e4b58e80d870ca179c604cbe6aa7ef5cbe240eca Mon Sep 17 00:00:00 2001 From: Cliff Date: Wed, 13 Jul 2022 02:04:25 -0600 Subject: [PATCH 12/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ffdfc5d..433c8d7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # BTWifiModule Frsky Bluetooth Emulator -This code is for use on a ESP Wroom 32, it emulates a FRSky Bluetooth module but with way more future possibilities. +This code is for use on a ESP Wroom 32/Pico/C3, it emulates a FRSky Bluetooth module but with way more future possibilities. # Supported Boards From acc0e1928a35c2fdc6837add3cc20e8d7579a19a Mon Sep 17 00:00:00 2001 From: dlktdr Date: Tue, 19 Jul 2022 12:30:15 -0700 Subject: [PATCH 13/17] Cleanup Folders, auto format --- src/bt.c | 62 +++---- src/bt.h | 5 +- src/{ => bt_client}/bt_client.c | 0 src/{ => bt_client}/bt_client.h | 0 src/{ => bt_server}/bt_server.c | 0 src/{ => bt_server}/bt_server.h | 0 src/espconmngr.c | 19 +-- src/espdefs.h | 29 ++-- src/espjoystick.c | 57 +++---- src/esproot.c | 146 +++++++++-------- src/esptrainer.c | 87 +++++----- src/frskybt.c | 242 +++++++++++++-------------- src/joystick/bt_joystick.c | 282 +++++++++++++++++--------------- src/joystick/bt_joystick.h | 3 +- src/settings.h | 5 +- src/terminal.c | 247 ++++++++++++++-------------- src/terminal.h | 2 +- 17 files changed, 587 insertions(+), 599 deletions(-) rename src/{ => bt_client}/bt_client.c (100%) rename src/{ => bt_client}/bt_client.h (100%) rename src/{ => bt_server}/bt_server.c (100%) rename src/{ => bt_server}/bt_server.h (100%) diff --git a/src/bt.c b/src/bt.c index 23798cd..9b7aa56 100644 --- a/src/bt.c +++ b/src/bt.c @@ -4,13 +4,13 @@ #include - -#include "esp_err.h" +#include "bt.h" #include "esp_bt.h" #include "esp_bt_main.h" +#include "esp_err.h" #include "esp_log.h" #include "freertos/task.h" -#include "bt.h" + #define LOG_BT "BT" #define MAX_BTNAME_LEN 50 @@ -20,37 +20,27 @@ esp_bd_addr_t rmtbtaddress; char btname[MAX_BTNAME_LEN] = "Hello"; -void strtobtaddr(esp_bd_addr_t dest, char *src) -{ - for(int i=0; i < 6; i++) { +void strtobtaddr(esp_bd_addr_t dest, char *src) { + for (int i = 0; i < 6; i++) { char str[3] = " "; memcpy(str, src, 2); src += 2; - dest[i] = strtoul(str,NULL, 16); + dest[i] = strtoul(str, NULL, 16); } } -char *btaddrtostr(char dest[13], esp_bd_addr_t src) -{ - sprintf(dest, "%02X%02X%02X%02X%02X%02X\r\n", - src[0], - src[1], - src[2], - src[3], - src[4], - src[5] - ); +char *btaddrtostr(char dest[13], esp_bd_addr_t src) { + sprintf(dest, "%02X%02X%02X%02X%02X%02X\r\n", src[0], src[1], src[2], src[3], + src[4], src[5]); dest[12] = '\0'; return dest; } - -bool memreleased=false; -void bt_init() -{ +bool memreleased = false; +void BTInit() { esp_err_t ret; - if(!memreleased) { + if (!memreleased) { ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); memreleased = true; } @@ -58,46 +48,48 @@ void bt_init() esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); ret = esp_bt_controller_init(&bt_cfg); if (ret) { - ESP_LOGE(LOG_BT, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret)); + ESP_LOGE(LOG_BT, "%s initialize controller failed: %s\n", __func__, + esp_err_to_name(ret)); return; } ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); if (ret) { - ESP_LOGE(LOG_BT, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret)); + ESP_LOGE(LOG_BT, "%s enable controller failed: %s\n", __func__, + esp_err_to_name(ret)); return; } ret = esp_bluedroid_init(); if (ret) { - ESP_LOGE(LOG_BT, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret)); + ESP_LOGE(LOG_BT, "%s init bluetooth failed: %s\n", __func__, + esp_err_to_name(ret)); return; } ret = esp_bluedroid_enable(); if (ret) { - ESP_LOGE(LOG_BT, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret)); + ESP_LOGE(LOG_BT, "%s enable bluetooth failed: %s\n", __func__, + esp_err_to_name(ret)); return; } - esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9 ); + esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9); } -void bt_disable() -{ - ESP_LOGI(LOG_BT,"Disabling Bluetooth"); +void bt_disable() { + ESP_LOGI(LOG_BT, "Disabling Bluetooth"); esp_bluedroid_disable(); esp_bluedroid_deinit(); esp_bt_controller_disable(); esp_bt_controller_deinit(); - ESP_LOGI(LOG_BT,"Pausing to shutdown"); + ESP_LOGI(LOG_BT, "Pausing to shutdown"); vTaskDelay(pdMS_TO_TICKS(BT_PAUSE_BEFORE_RESTART)); } -void btSetName(const char *name) -{ +void btSetName(const char *name) { strncpy(btname, name, sizeof(btname)); - btname[sizeof(btname)-1] = '\0'; - ESP_LOGI(LOG_BT,"Setting BT Name %s", name); + btname[sizeof(btname) - 1] = '\0'; + ESP_LOGI(LOG_BT, "Setting BT Name %s", name); } diff --git a/src/bt.h b/src/bt.h index 861894c..ff86197 100644 --- a/src/bt.h +++ b/src/bt.h @@ -2,7 +2,7 @@ #include "esp_bt_defs.h" -#define BT_PAUSE_BEFORE_RESTART 500 //ms +#define BT_PAUSE_BEFORE_RESTART 500 // ms extern esp_bd_addr_t rmtbtaddress; extern esp_bd_addr_t localbtaddress; @@ -11,6 +11,5 @@ extern char btname[]; void strtobtaddr(esp_bd_addr_t dest, char *src); char *btaddrtostr(char dest[13], esp_bd_addr_t src); void bt_disable(); -void bt_init(); +void BTInit(); void btSetName(const char *name); - diff --git a/src/bt_client.c b/src/bt_client/bt_client.c similarity index 100% rename from src/bt_client.c rename to src/bt_client/bt_client.c diff --git a/src/bt_client.h b/src/bt_client/bt_client.h similarity index 100% rename from src/bt_client.h rename to src/bt_client/bt_client.h diff --git a/src/bt_server.c b/src/bt_server/bt_server.c similarity index 100% rename from src/bt_server.c rename to src/bt_server/bt_server.c diff --git a/src/bt_server.h b/src/bt_server/bt_server.h similarity index 100% rename from src/bt_server.h rename to src/bt_server/bt_server.h diff --git a/src/espconmngr.c b/src/espconmngr.c index 21a7926..a486478 100644 --- a/src/espconmngr.c +++ b/src/espconmngr.c @@ -1,21 +1,20 @@ -#include #include "espconmngr.h" #include "esp_log.h" +#include "espdefs.h" #include "terminal.h" +#include #define CON_MGR_TAG "CONMGR" -void connectionCommandRX(int conCmd, const uint8_t * data, int len) -{ - switch (conCmd) - { +void connectionCommandRX(int conCmd, const uint8_t *data, int len) { + switch (conCmd) { case ESP_CON_DISCOVER_START: ESP_LOGI(CON_MGR_TAG, "Discovery Start Req"); break; case ESP_CON_DISCOVER_STOP: ESP_LOGI(CON_MGR_TAG, "Discovery Stop Req"); break; - case ESP_CON_CONNECT: + case ESP_CON_CONNECT: ESP_LOGI(CON_MGR_TAG, "Connect to device %s", (const char *)data); break; case ESP_CON_DISCONNECT: @@ -51,12 +50,8 @@ void connectionCommandRX(int conCmd, const uint8_t * data, int len) } } -void connectionEventRX(int event, const uint8_t *data, int len) -{ - -} +void connectionEventRX(int event, const uint8_t *data, int len) {} -void sendEvent(int event, const uint8_t *data, int len) -{ +void sendEvent(int event, const uint8_t *data, int len) { writeCommand(ESP_ROOT, ESP_ROOTCMD_CON_EVENT, data, len); } \ No newline at end of file diff --git a/src/espdefs.h b/src/espdefs.h index defbe46..648c4d5 100644 --- a/src/espdefs.h +++ b/src/espdefs.h @@ -1,14 +1,14 @@ #pragma once -#include -#include #include +#include +#include #define ESP_PACKET_TYPE_MSK 0x0F #define ESP_PACKET_CMD_BIT 6 #define ESP_PACKET_ACK_BIT 7 -#define ESP_PACKET_ISCMD(t) (t&(1< #include "espjoystick.h" +#include "bt.h" #include "esp_log.h" #include "joystick/bt_joystick.h" #include "joystick/hidd_le_prf_int.h" -#include "bt.h" +#include #define JOYSTICK_TAG "JOY" -bool joystickstarted=false; +bool joystickstarted = false; -int espJoystickStart() -{ +int espJoystickStart() { ESP_LOGI(JOYSTICK_TAG, "Joystick Starting"); - if(joystickstarted) return -1; + if (joystickstarted) + return -1; joystickstarted = true; - bt_init(); - btjoyInit(); + BTInit(); + BTJoyInit(); return 0; } -void espJoystickStop() -{ +void espJoystickStop() { ESP_LOGI(JOYSTICK_TAG, "Joystick Stop"); - if(joystickstarted) { + if (joystickstarted) { bt_disable(); } joystickstarted = false; } -bool espJoystickRunning() -{ - - return joystickstarted; -} - -void espJoystickData(const uint8_t *data, uint8_t len) -{ - if(len == sizeof(channeldata)) { - /*const channeldata *chdata = (const channeldata *)data; - for(int i=0; i < 8; i++) { - if(chdata->channelmask & 1<ch[i]); - } - printf("\r\n");*/ - if(btjoystickconnected) - hid_SendJoystickChannels((uint16_t *)data); +bool espJoystickRunning() { return joystickstarted; } + +void espJoystickData(const uint8_t *data, uint8_t len) { + if (len == sizeof(channeldata)) { + /*const channeldata *chdata = (const channeldata *)data; + for(int i=0; i < 8; i++) { + if(chdata->channelmask & 1<ch[i]); + } + printf("\r\n");*/ + if (btjoystickconnected) + hid_SendJoystickChannels((uint16_t *)data); } else { ESP_LOGE(JOYSTICK_TAG, "Unknown Data"); } - - } -void espJoystickCommand(uint8_t command, const uint8_t *data, uint8_t len) -{ - -} +void espJoystickCommand(uint8_t command, const uint8_t *data, uint8_t len) {} diff --git a/src/esproot.c b/src/esproot.c index 1dd17e3..27a38be 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -1,55 +1,64 @@ -#include #include "esproot.h" -#include "esptrainer.h" -#include "espjoystick.h" #include "espaudio.h" -#include "terminal.h" -#include "espdefs.h" #include "espconmngr.h" +#include "espdefs.h" +#include "espjoystick.h" +#include "esptrainer.h" +#include "terminal.h" +#include #define LOG_ESPR "ESPROOT" // TODO, Replace me with git SHA -espversion espVersion = {1,0,0,"GITTAG"}; +espversion espVersion = {1, 0, 0, "GITTAG"}; int g_rv; -bool g_radioIsBTEDR=false; -bool g_radioIsBLE=false; -bool g_radioIsWIFI=false; +uint8_t g_m; +bool g_radioIsBTEDR = false; +bool g_radioIsBLE = false; +bool g_radioIsWIFI = false; + +#define STARTBLE_MODE(m, x) \ + g_rv = x; \ + if (g_rv == 0) \ + g_radioIsBLE = true; +#define STARTBTEDR_MODE(m, x) \ + g_rv = x; \ + if (g_rv == 0) \ + g_radioIsBTEDR = true; +#define STARTWIFI_MODE(m, x) \ + g_rv = x; \ + g_m = m; + if (g_rv == 0) { \ + g_radioIsWIFI = true; \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_START_MODE, &m, 1); \ + } else { \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_STOP_MODE, &m, 1); \ + } -#define STARTBLE_MODE(m,x) g_rv=x; writeAckNak(m,g_rv,""); if(g_rv==0) g_radioIsBLE=true; -#define STARTBTEDR_MODE(m,x) g_rv=x; writeAckNak(m,g_rv,""); if(g_rv==0) g_radioIsBTEDR=true; -#define STARTWIFI_MODE(m,x) g_rv=x; writeAckNak(m,g_rv,""); if(g_rv==0) g_radioIsWIFI=true; -#define START_MODE(m,x) writeAckNak(m,x,""); +#define START_MODE(m, x) writeAckNak(m, x, ""); #define RADIO_USED() (g_radioIsBTEDR || g_radioIsBLE || g_radioIsWIFI) uint8_t runningModes; -void espRootData(const uint8_t *data, uint8_t len) -{ - -} +void espRootData(const uint8_t *data, uint8_t len) {} -void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) -{ - ESP_LOGI(LOG_ESPR, "Root Command %d", command, data[0]); +void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { + ESP_LOGI(LOG_ESPR, "Root Command %d", command); - switch(command) { + switch (command) { case ESP_ROOTCMD_START_MODE: - if(len != 1) break; - ESP_LOGI(LOG_ESPR, "Starting Mode %d", data[0]); - if(RADIO_USED() && ((data[0] == ESP_TELEMETRY || - data[0] == ESP_TRAINER || - data[0] == ESP_JOYSTICK || - data[0] == ESP_AUDIO || - data[0] == ESP_FTP))) { + if (len != 1) + break; + ESP_LOGI(LOG_ESPR, "Starting Mode %d", data[0]); + if (RADIO_USED() && ((data[0] == ESP_TELEMETRY || data[0] == ESP_TRAINER || + data[0] == ESP_JOYSTICK || data[0] == ESP_AUDIO || + data[0] == ESP_FTP))) { ESP_LOGE(LOG_ESPR, "Cannot start Radio already used"); - writeAckNak(data[0],false, "Radio already used"); } else { - switch (data[0]) - { + switch (data[0]) { case ESP_TELEMETRY: - //STARTRADIO_MODE(ESP_TELEMETRY, espTelemetryStart()); + // STARTRADIO_MODE(ESP_TELEMETRY, espTelemetryStart()); break; case ESP_TRAINER: STARTBLE_MODE(ESP_TRAINER, espTrainerStart()); @@ -58,60 +67,61 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) STARTBLE_MODE(ESP_JOYSTICK, espJoystickStart()); break; case ESP_AUDIO: - STARTBTEDR_MODE(ESP_AUDIO, espAudioStart()); + STARTBTEDR_MODE(ESP_AUDIO, espAudioStart()); break; case ESP_FTP: - //STARTWIFI_MODE(ESP_FTP, espFTPStart()); + // STARTWIFI_MODE(ESP_FTP, espFTPStart()); + break; + case ESP_IMU: + // START_MODE(ESP_IMU, espIMUStart()); break; - case ESP_IMU: - //START_MODE(ESP_IMU, espIMUStart()); - break; default: break; } } break; - + case ESP_ROOTCMD_STOP_MODE: - if(len != 1) break; + if (len != 1) + break; ESP_LOGI(LOG_ESPR, "Stopping Mode %d", data[0]); switch (data[0]) { case ESP_TELEMETRY: - // if(espTelemetryRunning()) { - // espTelemetryStop(); - // g_radioIsBTEDR = false; // WHAT TO DO, I COULD SEE BOTH.. - // probably EDR with a serial port tho - // } + // if(espTelemetryRunning()) { + // espTelemetryStop(); + // g_radioIsBTEDR = false; // WHAT TO DO, I COULD SEE BOTH.. + // probably EDR with a serial port tho + // } break; case ESP_TRAINER: - if(espTrainerRunning()) { + if (espTrainerRunning()) { espTrainerStop(); g_radioIsBLE = false; } break; case ESP_JOYSTICK: - if(espJoystickRunning()) { + if (espJoystickRunning()) { espJoystickStop(); g_radioIsBLE = false; } break; case ESP_AUDIO: - if(espAudioRunning()) { + if (espAudioRunning()) { espAudioStop(); g_radioIsBTEDR = false; } break; case ESP_FTP: - // if(espFTPRunning()) { - // espFTPStop(); - // radioUsed = false; - // } + // if(espFTPRunning()) { + // espFTPStop(); + // radioUsed = false; + // } + break; + case ESP_IMU: + // if(espFTPRunning()) { + // espFTPStop(); + // } break; - case ESP_IMU: - // if(espFTPRunning()) { - // espFTPStop(); - // } - break; default: break; @@ -122,24 +132,22 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) ESP_LOGI(LOG_ESPR, "Rebooting..."); esp_restart(); break; - case ESP_ROOTCMD_VERSION: - writeCommand(ESP_ROOT, - ESP_ROOTCMD_VERSION, - (const uint8_t *)&espVersion, + case ESP_ROOTCMD_VERSION: + writeCommand(ESP_ROOT, ESP_ROOTCMD_VERSION, (const uint8_t *)&espVersion, sizeof(espversion)); break; - case ESP_ROOTCMD_CON_EVENT: // Shouldn't be much here, events are generated here - if(len < 1) break; + case ESP_ROOTCMD_CON_EVENT: // Shouldn't be much here, events are generated + // here + if (len < 1) + break; ESP_LOGI(LOG_ESPR, "Con Mgr. Event %d", data[0]); - connectionEventRX(data[0], data + 1, len -1); - break; + connectionEventRX(data[0], data + 1, len - 1); + break; case ESP_ROOTCMD_CON_MGMNT: - if(len < 1) break; + if (len < 1) + break; ESP_LOGI(LOG_ESPR, "Con Mgr. Cmd %d", data[0]); - connectionCommandRX(data[0], data + 1, len -1); + connectionCommandRX(data[0], data + 1, len - 1); break; } - } - - diff --git a/src/esptrainer.c b/src/esptrainer.c index 8813422..6f70c93 100644 --- a/src/esptrainer.c +++ b/src/esptrainer.c @@ -1,38 +1,37 @@ -#include #include "esptrainer.h" -#include "terminal.h" -#include "esp_log.h" #include "bt.h" -#include "bt_client.h" -#include "bt_server.h" +#include "bt_client/bt_client.h" +#include "bt_server/bt_server.h" +#include "esp_log.h" +#include "terminal.h" +#include #define TRAINER_TAG "TRAINER" -bool trainerstarted=false; -bool trainerismaster=false; +bool trainerstarted = false; +bool trainerismaster = false; -int espTrainerStart() -{ - if(trainerstarted) +int espTrainerStart() { + if (trainerstarted) return -1; - - if(trainerismaster) { + + if (trainerismaster) { ESP_LOGI(TRAINER_TAG, "Trainer Master Start"); // Start Master Mode - bt_init(); + BTInit(); btpInit(); /*writeCommand(ESP_TRAINER, - ESP_TRAINERCMD_SET_MASTER, - NULL, + ESP_TRAINERCMD_SET_MASTER, + NULL, 0);*/ } else { ESP_LOGI(TRAINER_TAG, "Trainer Slave Start"); // Start Slave Mode - bt_init(); + BTInit(); btcInit(); /*writeCommand(ESP_TRAINER, - ESP_TRAINERCMD_SET_MASTER, - NULL, + ESP_TRAINERCMD_SET_MASTER, + NULL, 0);*/ } @@ -40,27 +39,22 @@ int espTrainerStart() return 0; } -void espTrainerStop() -{ +void espTrainerStop() { ESP_LOGI(TRAINER_TAG, "Trainer Stop"); - if(trainerstarted) + if (trainerstarted) bt_disable(); trainerstarted = false; } -bool espTrainerRunning() -{ - return trainerstarted; -} +bool espTrainerRunning() { return trainerstarted; } -// Trainer data received -void espTrainerData(const uint8_t *data, uint8_t len) -{ - if(len == sizeof(channeldata)) { +// Trainer data received +void espTrainerData(const uint8_t *data, uint8_t len) { + if (len == sizeof(channeldata)) { const channeldata *chdata = (const channeldata *)data; - for(int i=0; i < MAX_OUTPUT_CHANNELS; i++) { - if(chdata->channelmask & 1<ch[i]); + for (int i = 0; i < MAX_OUTPUT_CHANNELS; i++) { + if (chdata->channelmask & 1 << i) + printf("CH%d[%d] ", i + 1, chdata->ch[i]); } printf("\r\n"); @@ -69,30 +63,27 @@ void espTrainerData(const uint8_t *data, uint8_t len) } } -void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len) -{ +void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len) { ESP_LOGI(TRAINER_TAG, "Got A Command %d", command); - bool switchmodes=false; - switch(command) { - case ESP_TRAINERCMD_SET_MASTER: - if(!trainerismaster) - switchmodes = true; - break; - case ESP_TRAINERCMD_SET_SLAVE: - if(!trainerismaster) - switchmodes = true; - break; + bool switchmodes = false; + switch (command) { + case ESP_TRAINERCMD_SET_MASTER: + if (!trainerismaster) + switchmodes = true; + break; + case ESP_TRAINERCMD_SET_SLAVE: + if (!trainerismaster) + switchmodes = true; + break; } - if(switchmodes) { + if (switchmodes) { trainerismaster = !trainerismaster; espTrainerStop(); espTrainerStart(); } } -void espTrainerSend(const channeldata *chans) -{ +void espTrainerSend(const channeldata *chans) { writePacket((const uint8_t *)chans, sizeof(channeldata), false, ESP_TRAINER); } - diff --git a/src/frskybt.c b/src/frskybt.c index 70844c9..d4c6a5e 100644 --- a/src/frskybt.c +++ b/src/frskybt.c @@ -1,13 +1,13 @@ /* From OpenTX https://github.com/opentx */ -#include #include "frskybt.h" -#include "bt_server.h" +#include "bt_server/bt_server.h" +#include "esp_log.h" #include "joystick/bt_joystick.h" #include "joystick/esp_hidd_prf_api.h" #include "settings.h" -#include "esp_log.h" +#include #define FRSKYBT_TAG "FRSKYBT" @@ -24,172 +24,159 @@ uint16_t frchanneldata[BT_CHANNELS]; * @param len */ -void logBTFrame(bool valid, char message[]) -{ - static int64_t ltime =0; +void logBTFrame(bool valid, char message[]) { + static int64_t ltime = 0; int64_t timestamp = esp_timer_get_time() - ltime; ltime = esp_timer_get_time(); - if(!valid) { - ESP_LOGE(FRSKYBT_TAG, "(%05lld)Unable to decode data, %s", timestamp, message); + if (!valid) { + ESP_LOGE(FRSKYBT_TAG, "(%05lld)Unable to decode data, %s", timestamp, + message); } else { - ESP_LOGI(FRSKYBT_TAG, "(%05lld)Ch1[%04d] Ch2[%04d] Ch3[%04d] Ch4[%04d] Ch5[%04d] Ch6[%04d] Ch7[%04d] Ch8[%04d]", - timestamp, - frchanneldata[0], - frchanneldata[1], - frchanneldata[2], - frchanneldata[3], - frchanneldata[4], - frchanneldata[5], - frchanneldata[6], - frchanneldata[7]); + ESP_LOGI(FRSKYBT_TAG, + "(%05lld)Ch1[%04d] Ch2[%04d] Ch3[%04d] Ch4[%04d] Ch5[%04d] " + "Ch6[%04d] Ch7[%04d] Ch8[%04d]", + timestamp, frchanneldata[0], frchanneldata[1], frchanneldata[2], + frchanneldata[3], frchanneldata[4], frchanneldata[5], + frchanneldata[6], frchanneldata[7]); } } - -static uint8_t buffer[BLUETOOTH_LINE_LENGTH+1]; +static uint8_t buffer[BLUETOOTH_LINE_LENGTH + 1]; static uint8_t bufferIndex; static uint8_t crc; -void pushByte(uint8_t byte) -{ - crc ^= byte; - if (byte == START_STOP || byte == BYTE_STUFF) { - buffer[bufferIndex++] = BYTE_STUFF; - byte ^= STUFF_MASK; - } - buffer[bufferIndex++] = byte; +void pushByte(uint8_t byte) { + crc ^= byte; + if (byte == START_STOP || byte == BYTE_STUFF) { + buffer[bufferIndex++] = BYTE_STUFF; + byte ^= STUFF_MASK; + } + buffer[bufferIndex++] = byte; } /* Builds Trainer Data -* Returns the length of the encoded PPM + CRC -* Data saved into addr pointer -*/ - -int setTrainer(uint8_t *addr, uint16_t chan_vals[BT_CHANNELS]) -{ - // Allocate Channel Mappings, Set Default to all Center - uint8_t * cur = buffer; - bufferIndex = 0; - crc = 0x00; - - buffer[bufferIndex++] = START_STOP; // start byte - pushByte(TRAINER_FRAME); // trainer frame type? - for (int channel=0; channel < BT_CHANNELS; channel+=2, cur+=3) { - uint16_t channelValue1 = chan_vals[channel]; - uint16_t channelValue2 = chan_vals[channel+1]; - - pushByte(channelValue1 & 0x00ff); - pushByte(((channelValue1 & 0x0f00) >> 4) + ((channelValue2 & 0x00f0) >> 4)); - pushByte(((channelValue2 & 0x000f) << 4) + ((channelValue2 & 0x0f00) >> 8)); - } + * Returns the length of the encoded PPM + CRC + * Data saved into addr pointer + */ + +int setTrainer(uint8_t *addr, uint16_t chan_vals[BT_CHANNELS]) { + // Allocate Channel Mappings, Set Default to all Center + uint8_t *cur = buffer; + bufferIndex = 0; + crc = 0x00; + + buffer[bufferIndex++] = START_STOP; // start byte + pushByte(TRAINER_FRAME); // trainer frame type? + for (int channel = 0; channel < BT_CHANNELS; channel += 2, cur += 3) { + uint16_t channelValue1 = chan_vals[channel]; + uint16_t channelValue2 = chan_vals[channel + 1]; + + pushByte(channelValue1 & 0x00ff); + pushByte(((channelValue1 & 0x0f00) >> 4) + ((channelValue2 & 0x00f0) >> 4)); + pushByte(((channelValue2 & 0x000f) << 4) + ((channelValue2 & 0x0f00) >> 8)); + } - buffer[bufferIndex++] = crc; - buffer[bufferIndex++] = START_STOP; // end byte + buffer[bufferIndex++] = crc; + buffer[bufferIndex++] = START_STOP; // end byte - // Copy data to array - memcpy(addr,buffer,bufferIndex); + // Copy data to array + memcpy(addr, buffer, bufferIndex); - return bufferIndex; + return bufferIndex; } //---------------------------------- // Receieve Code //---------------------------------- -enum {STATE_DATA_IDLE, -STATE_DATA_START, -STATE_DATA_XOR, -STATE_DATA_IN_FRAME -}; +enum { STATE_DATA_IDLE, STATE_DATA_START, STATE_DATA_XOR, STATE_DATA_IN_FRAME }; -static uint8_t _otxbuffer[BLUETOOTH_LINE_LENGTH+2] = {START_STOP}; +static uint8_t _otxbuffer[BLUETOOTH_LINE_LENGTH + 2] = {START_STOP}; static uint8_t *otxbuffer = _otxbuffer + 1; uint8_t otxbufferIndex = 0; bool btprocessed = false; -void appendTrainerByte(uint8_t data) -{ +void appendTrainerByte(uint8_t data) { if (otxbufferIndex < BLUETOOTH_LINE_LENGTH) { otxbuffer[otxbufferIndex++] = data; } else { - ESP_LOGE(FRSKYBT_TAG,"OTX Buffer Overflow"); + ESP_LOGE(FRSKYBT_TAG, "OTX Buffer Overflow"); otxbufferIndex = 0; } } -void processTrainerFrame(const uint8_t * otxbuffer) -{ - for (uint8_t channel=0, i=1; channel> 4) + ((otxbuffer[i+2] & 0x0f) << 8); + frchanneldata[channel] = otxbuffer[i] + ((otxbuffer[i + 1] & 0xf0) << 4); + frchanneldata[channel + 1] = ((otxbuffer[i + 1] & 0x0f) << 4) + + ((otxbuffer[i + 2] & 0xf0) >> 4) + + ((otxbuffer[i + 2] & 0x0f) << 8); } - // If the data came from the radio, send it out over bluetooth. Send same data but add the START_STOP -/* if(settings.mode == ROLE_BLE_PERIPHERAL) { - if(btjoystickconnected) { - hid_SendJoystickChannels(frchanneldata); - } - btp_sendfrchanneldata(_otxbuffer, otxbufferIndex+1); - }*/ + // If the data came from the radio, send it out over bluetooth. Send same data + // but add the START_STOP + /* if(settings.mode == ROLE_BLE_PERIPHERAL) { + if(btjoystickconnected) { + hid_SendJoystickChannels(frchanneldata); + } + btp_sendfrchanneldata(_otxbuffer, otxbufferIndex+1); + }*/ } -void frSkyProcessByte(uint8_t data) -{ +void frSkyProcessByte(uint8_t data) { static uint8_t dataState = STATE_DATA_IDLE; switch (dataState) { - case STATE_DATA_START: - if (data == START_STOP) { - dataState = STATE_DATA_IN_FRAME; - otxbufferIndex = 0; - } - else { - appendTrainerByte(data); - } + case STATE_DATA_START: + if (data == START_STOP) { + dataState = STATE_DATA_IN_FRAME; + otxbufferIndex = 0; + } else { + appendTrainerByte(data); + } + break; + + case STATE_DATA_IN_FRAME: + if (data == BYTE_STUFF) { + dataState = STATE_DATA_XOR; // XOR next byte + } else if (data == START_STOP) { + dataState = STATE_DATA_IN_FRAME; + otxbufferIndex = 0; + } else { + appendTrainerByte(data); + } + break; + + case STATE_DATA_XOR: + switch (data) { + case BYTE_STUFF ^ STUFF_MASK: + case START_STOP ^ STUFF_MASK: + // Expected content, save the data + appendTrainerByte(data ^ STUFF_MASK); + dataState = STATE_DATA_IN_FRAME; break; - - case STATE_DATA_IN_FRAME: - if (data == BYTE_STUFF) { - dataState = STATE_DATA_XOR; // XOR next byte - } - else if (data == START_STOP) { - dataState = STATE_DATA_IN_FRAME; - otxbufferIndex = 0; - } - else { - appendTrainerByte(data); - } + case START_STOP: // Illegal situation, as we have START_STOP, try to start + // from the beginning + otxbufferIndex = 0; + dataState = STATE_DATA_IN_FRAME; break; - - case STATE_DATA_XOR: - switch (data) { - case BYTE_STUFF ^ STUFF_MASK: - case START_STOP ^ STUFF_MASK: - // Expected content, save the data - appendTrainerByte(data ^ STUFF_MASK); - dataState = STATE_DATA_IN_FRAME; - break; - case START_STOP: // Illegal situation, as we have START_STOP, try to start from the beginning - otxbufferIndex = 0; - dataState = STATE_DATA_IN_FRAME; - break; - default: - // Illegal situation, start looking for a new START_STOP byte - dataState = STATE_DATA_START; - break; - } + default: + // Illegal situation, start looking for a new START_STOP byte + dataState = STATE_DATA_START; break; + } + break; - case STATE_DATA_IDLE: - if (data == START_STOP) { - otxbufferIndex = 0; - dataState = STATE_DATA_START; - } - else { - appendTrainerByte(data); - } - break; + case STATE_DATA_IDLE: + if (data == START_STOP) { + otxbufferIndex = 0; + dataState = STATE_DATA_START; + } else { + appendTrainerByte(data); + } + break; } if (otxbufferIndex >= BLUETOOTH_PACKET_SIZE) { @@ -200,7 +187,7 @@ void frSkyProcessByte(uint8_t data) if (crc == otxbuffer[BLUETOOTH_PACKET_SIZE - 1]) { if (otxbuffer[0] == TRAINER_FRAME) { processTrainerFrame(otxbuffer); - //logBTFrame(true, ""); + // logBTFrame(true, ""); } else { logBTFrame(false, "Not a trainer frame"); } @@ -211,9 +198,8 @@ void frSkyProcessByte(uint8_t data) } } -void processFrame(const uint8_t *frame, uint8_t len) -{ - for(int i=0;i < len; i++) { +void processFrame(const uint8_t *frame, uint8_t len) { + for (int i = 0; i < len; i++) { frSkyProcessByte(frame[i]); } } \ No newline at end of file diff --git a/src/joystick/bt_joystick.c b/src/joystick/bt_joystick.c index 060747c..bc26472 100644 --- a/src/joystick/bt_joystick.c +++ b/src/joystick/bt_joystick.c @@ -1,44 +1,50 @@ -#include -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" -#include "esp_system.h" -#include "esp_wifi.h" +#include "esp_bt.h" #include "esp_event.h" #include "esp_log.h" +#include "esp_system.h" +#include "esp_wifi.h" +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "freertos/task.h" #include "nvs_flash.h" -#include "esp_bt.h" +#include +#include +#include -#include "esp_hidd_prf_api.h" + +#include "driver/gpio.h" #include "esp_bt_defs.h" +#include "esp_bt_device.h" +#include "esp_bt_main.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 "driver/gpio.h" +#include "esp_gatts_api.h" +#include "esp_hidd_prf_api.h" #include "joystick/hid_dev.h" - /** * Brief: - * This example Implemented BLE HID device profile related functions, in which the HID device - * has 4 Reports (1 is mouse, 2 is keyboard and LED, 3 is Consumer Devices, 4 is Vendor devices). - * Users can choose different reports according to their own application scenarios. - * BLE HID profile inheritance and USB HID class. + * This example Implemented BLE HID device profile related functions, in which + * the HID device has 4 Reports (1 is mouse, 2 is keyboard and LED, 3 is + * Consumer Devices, 4 is Vendor devices). Users can choose different reports + * according to their own application scenarios. BLE HID profile inheritance and + * USB HID class. */ /** * Note: - * 1. Win10 does not support vendor report , So SUPPORT_REPORT_VENDOR is always set to FALSE, it defines in hidd_le_prf_int.h - * 2. Update connection parameters are not allowed during iPhone HID encryption, slave turns - * off the ability to automatically update connection parameters during encryption. - * 3. After our HID device is connected, the iPhones write 1 to the Report Characteristic Configuration Descriptor, - * even if the HID encryption is not completed. This should actually be written 1 after the HID encryption is completed. - * we modify the permissions of the Report Characteristic Configuration Descriptor to `ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE_ENCRYPTED`. - * if you got `GATT_INSUF_ENCRYPTION` error, please ignore. + * 1. Win10 does not support vendor report , So SUPPORT_REPORT_VENDOR is always + * set to FALSE, it defines in hidd_le_prf_int.h + * 2. Update connection parameters are not allowed during iPhone HID encryption, + * slave turns off the ability to automatically update connection parameters + * during encryption. + * 3. After our HID device is connected, the iPhones write 1 to the Report + * Characteristic Configuration Descriptor, even if the HID encryption is not + * completed. This should actually be written 1 after the HID encryption is + * completed. we modify the permissions of the Report Characteristic + * Configuration Descriptor to `ESP_GATT_PERM_READ | + * ESP_GATT_PERM_WRITE_ENCRYPTED`. if you got `GATT_INSUF_ENCRYPTION` error, + * please ignore. */ #define HID_DEMO_TAG "HID_DEMO" @@ -46,27 +52,33 @@ uint16_t btj_conn_id = 0; static bool sec_conn = false; static bool send_volum_up = false; -volatile bool btjoystickconnected=false; -#define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) +volatile bool btjoystickconnected = false; +#define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) -static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param); +static void hidd_event_callback(esp_hidd_cb_event_t event, + esp_hidd_cb_param_t *param); -#define HIDD_DEVICE_NAME "BTWifiGamePad" +#define HIDD_DEVICE_NAME "BTWifiGamePad" static uint8_t hidd_service_uuid128[] = { - /* LSB <--------------------------------------------------------------------------------> MSB */ - //first uuid, 16bit, [12],[13] is the value - 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x12, 0x18, 0x00, 0x00, + /* LSB + <--------------------------------------------------------------------------------> + MSB */ + // first uuid, 16bit, [12],[13] is the value + 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, + 0x00, 0x10, 0x00, 0x00, 0x12, 0x18, 0x00, 0x00, }; static esp_ble_adv_data_t hidd_adv_data = { .set_scan_rsp = false, .include_name = true, .include_txpower = true, - .min_interval = 0x0006, //slave connection min interval, Time = min_interval * 1.25 msec - .max_interval = 0x0010, //slave connection max interval, Time = max_interval * 1.25 msec - .appearance = 0x03c0, //HID Generic, + .min_interval = 0x0006, // slave connection min interval, Time = + // min_interval * 1.25 msec + .max_interval = 0x0010, // slave connection max interval, Time = + // max_interval * 1.25 msec + .appearance = 0x03c0, // HID Generic, .manufacturer_len = 0, - .p_manufacturer_data = NULL, + .p_manufacturer_data = NULL, .service_data_len = 0, .p_service_data = NULL, .service_uuid_len = sizeof(hidd_service_uuid128), @@ -75,108 +87,122 @@ static esp_ble_adv_data_t hidd_adv_data = { }; static esp_ble_adv_params_t hidd_adv_params = { - .adv_int_min = 0x20, - .adv_int_max = 0x30, - .adv_type = ADV_TYPE_IND, - .own_addr_type = BLE_ADDR_TYPE_PUBLIC, + .adv_int_min = 0x20, + .adv_int_max = 0x30, + .adv_type = ADV_TYPE_IND, + .own_addr_type = BLE_ADDR_TYPE_PUBLIC, //.peer_addr = //.peer_addr_type = - .channel_map = ADV_CHNL_ALL, + .channel_map = ADV_CHNL_ALL, .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, }; - -static void hidd_event_callback(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param) -{ - switch(event) { - case ESP_HIDD_EVENT_REG_FINISH: { - if (param->init_finish.state == ESP_HIDD_INIT_OK) { - //esp_bd_addr_t rand_addr = {0x04,0x11,0x11,0x11,0x11,0x05}; - esp_ble_gap_set_device_name(HIDD_DEVICE_NAME); - esp_ble_gap_config_adv_data(&hidd_adv_data); - - } - break; - } - case ESP_BAT_EVENT_REG: { - break; - } - case ESP_HIDD_EVENT_DEINIT_FINISH: - break; - case ESP_HIDD_EVENT_BLE_CONNECT: { - ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_CONNECT"); - btjoystickconnected = true; - btj_conn_id = param->connect.conn_id; - break; - } - case ESP_HIDD_EVENT_BLE_DISCONNECT: { - sec_conn = false; - btjoystickconnected = false; - ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_DISCONNECT"); - esp_ble_gap_start_advertising(&hidd_adv_params); - break; - } - case ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT: { - ESP_LOGI(HID_DEMO_TAG, "%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", __func__); - ESP_LOG_BUFFER_HEX(HID_DEMO_TAG, param->vendor_write.data, param->vendor_write.length); - } - default: - break; +static void hidd_event_callback(esp_hidd_cb_event_t event, + esp_hidd_cb_param_t *param) { + switch (event) { + case ESP_HIDD_EVENT_REG_FINISH: { + if (param->init_finish.state == ESP_HIDD_INIT_OK) { + // esp_bd_addr_t rand_addr = {0x04,0x11,0x11,0x11,0x11,0x05}; + esp_ble_gap_set_device_name(HIDD_DEVICE_NAME); + esp_ble_gap_config_adv_data(&hidd_adv_data); } - return; + break; + } + case ESP_BAT_EVENT_REG: { + break; + } + case ESP_HIDD_EVENT_DEINIT_FINISH: + break; + case ESP_HIDD_EVENT_BLE_CONNECT: { + ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_CONNECT"); + btjoystickconnected = true; + btj_conn_id = param->connect.conn_id; + break; + } + case ESP_HIDD_EVENT_BLE_DISCONNECT: { + sec_conn = false; + btjoystickconnected = false; + ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_DISCONNECT"); + esp_ble_gap_start_advertising(&hidd_adv_params); + break; + } + case ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT: { + ESP_LOGI(HID_DEMO_TAG, "%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", + __func__); + ESP_LOG_BUFFER_HEX(HID_DEMO_TAG, param->vendor_write.data, + param->vendor_write.length); + } + default: + break; + } + return; } -static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) -{ - switch (event) { - case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: - esp_ble_gap_start_advertising(&hidd_adv_params); - break; - case ESP_GAP_BLE_SEC_REQ_EVT: - for(int i = 0; i < ESP_BD_ADDR_LEN; i++) { - ESP_LOGD(HID_DEMO_TAG, "%x:",param->ble_security.ble_req.bd_addr[i]); - } - esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true); - break; - case ESP_GAP_BLE_AUTH_CMPL_EVT: - sec_conn = true; - esp_bd_addr_t bd_addr; - memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t)); - ESP_LOGI(HID_DEMO_TAG, "remote BD_ADDR: %08x%04x",\ - (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3], - (bd_addr[4] << 8) + bd_addr[5]); - ESP_LOGI(HID_DEMO_TAG, "address type = %d", param->ble_security.auth_cmpl.addr_type); - ESP_LOGI(HID_DEMO_TAG, "pair status = %s",param->ble_security.auth_cmpl.success ? "success" : "fail"); - if(!param->ble_security.auth_cmpl.success) { - ESP_LOGE(HID_DEMO_TAG, "fail reason = 0x%x",param->ble_security.auth_cmpl.fail_reason); - } - break; - default: - break; +static void gap_event_handler(esp_gap_ble_cb_event_t event, + esp_ble_gap_cb_param_t *param) { + switch (event) { + case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: + esp_ble_gap_start_advertising(&hidd_adv_params); + break; + case ESP_GAP_BLE_SEC_REQ_EVT: + for (int i = 0; i < ESP_BD_ADDR_LEN; i++) { + ESP_LOGD(HID_DEMO_TAG, "%x:", param->ble_security.ble_req.bd_addr[i]); } + esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true); + break; + case ESP_GAP_BLE_AUTH_CMPL_EVT: + sec_conn = true; + esp_bd_addr_t bd_addr; + memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, + sizeof(esp_bd_addr_t)); + ESP_LOGI(HID_DEMO_TAG, "remote BD_ADDR: %08x%04x", + (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + + bd_addr[3], + (bd_addr[4] << 8) + bd_addr[5]); + ESP_LOGI(HID_DEMO_TAG, "address type = %d", + param->ble_security.auth_cmpl.addr_type); + ESP_LOGI(HID_DEMO_TAG, "pair status = %s", + param->ble_security.auth_cmpl.success ? "success" : "fail"); + if (!param->ble_security.auth_cmpl.success) { + ESP_LOGE(HID_DEMO_TAG, "fail reason = 0x%x", + param->ble_security.auth_cmpl.fail_reason); + } + break; + default: + break; + } } #include "bt_joystick.h" -void btjoyInit() -{ - ///register the callback function to the gap module - esp_ble_gap_register_callback(gap_event_handler); - esp_hidd_register_callbacks(hidd_event_callback); - - /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/ - esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND; //bonding with peer device after authentication - esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE; //set the IO capability to No output No input - uint8_t key_size = 16; //the key size should be 7~16 bytes - uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; - uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; - esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t)); - esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t)); - esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t)); - /* If your BLE device act as a Slave, the init_key means you hope which types of key of the master should distribute to you, - and the response key means which key you can distribute to the Master; - If your BLE device act as a master, the response key means you hope which types of key of the slave should distribute to you, - and the init key means which key you can distribute to the slave. */ - esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t)); - esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t)); +void BTJoyInit() { + /// register the callback function to the gap module + esp_ble_gap_register_callback(gap_event_handler); + esp_hidd_register_callbacks(hidd_event_callback); + + /* set the security iocap & auth_req & key size & init key response key + * parameters to the stack*/ + esp_ble_auth_req_t auth_req = + ESP_LE_AUTH_BOND; // bonding with peer device after authentication + esp_ble_io_cap_t iocap = + ESP_IO_CAP_NONE; // set the IO capability to No output No input + uint8_t key_size = 16; // the key size should be 7~16 bytes + uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; + uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK; + esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, + sizeof(uint8_t)); + esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, + sizeof(uint8_t)); + esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, + sizeof(uint8_t)); + /* If your BLE device act as a Slave, the init_key means you hope which types + of key of the master should distribute to you, and the response key means + which key you can distribute to the Master; If your BLE device act as a + master, the response key means you hope which types of key of the slave should + distribute to you, and the init key means which key you can distribute to the + slave. */ + esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, + sizeof(uint8_t)); + esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, + sizeof(uint8_t)); } \ No newline at end of file diff --git a/src/joystick/bt_joystick.h b/src/joystick/bt_joystick.h index c94d19f..13f56c6 100644 --- a/src/joystick/bt_joystick.h +++ b/src/joystick/bt_joystick.h @@ -2,5 +2,4 @@ extern volatile bool btjoystickconnected; extern uint16_t btj_conn_id; -void btjoyInit(); - +void BTJoyInit(); diff --git a/src/settings.h b/src/settings.h index fe41786..8b3bd44 100644 --- a/src/settings.h +++ b/src/settings.h @@ -1,11 +1,12 @@ #pragma once +#include "bt_client/bt_client.h" #include "defines.h" -#include "bt_client.h" #include "espdefs.h" typedef struct { - //uint8_t version // Todo the version info here, check on load it matches otherwise ignore + // uint8_t version // Todo the version info here, check on load it matches + // otherwise ignore char rmtbtaddr[13]; uint8_t mode; } settings_t; diff --git a/src/terminal.c b/src/terminal.c index b9fb7e0..288d6a8 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -3,23 +3,23 @@ #include #include +#include "bt.h" +#include "bt_client/bt_client.h" +#include "bt_server/bt_server.h" +#include "cb.h" +#include "cobs.h" +#include "crc.h" +#include "defines.h" #include "esp_log.h" -#include "terminal.h" #include "frskybt.h" -#include "cb.h" -#include "bt.h" -#include "bt_client.h" -#include "bt_server.h" #include "joystick/bt_joystick.h" -#include "defines.h" #include "settings.h" -#include "cobs.h" -#include "crc.h" +#include "terminal.h" -#include "esproot.h" -#include "esptrainer.h" #include "espaudio.h" #include "espjoystick.h" +#include "esproot.h" +#include "esptrainer.h" #include "freertos/stream_buffer.h" #define LOG_UART "UART" @@ -31,11 +31,11 @@ StreamBufferHandle_t uartrxstreamhndl; -volatile bool uartRXTaskStarted=false; +volatile bool uartRXTaskStarted = false; const uart_port_t uart_num = UART_NUM; -#define UART_WRITE_STRING(x,y) uart_write_bytes(x, y, sizeof(y)-1) +#define UART_WRITE_STRING(x, y) uart_write_bytes(x, y, sizeof(y) - 1) typedef enum { CENTRAL_STATE_DISCONNECT, @@ -56,17 +56,13 @@ typedef enum { role_t curMode = ROLE_UNKNOWN; btcentralstate btCentralState = CENTRAL_STATE_DISCONNECT; btperipheralstate btPeripherialState = PERIPHERIAL_STATE_DISCONNECTED; -int64_t baudTimer =0; +int64_t baudTimer = 0; int laddcnt = 0; char rmtaddress[13] = "000000000000"; char reusablebuff[REUSABLE_BUFFER]; -void sendBTMode() -{ - -} - +void sendBTMode() {} // Ticks to wait for data stream to come in @@ -79,9 +75,8 @@ uart_config_t uart_config = { .source_clk = UART_SCLK_APB, }; - char atcommand[AT_CMD_MAX_LEN]; -int atcommandlen=-1; +int atcommandlen = -1; circular_buffer uartinbuf; @@ -89,42 +84,46 @@ circular_buffer uartinbuf; #define ESP_PACKET_TYPE_MSK 0x0F #define ESP_PACKET_CMD_BIT 6 #define ESP_PACKET_ACK_BIT 7 -#define ESP_PACKET_ISCMD(t) (t&(1<type & ESP_PACKET_TYPE_MSK) { - case ESP_ROOT: - if(ESP_PACKET_ISCMD(packet->type)) - espRootCommand(packet->data[0], packet->data + 1, packet->len -1); - else - espRootData(packet->data, packet->len); - break; - case ESP_TELEMETRY: - break; - case ESP_TRAINER: - if(ESP_PACKET_ISCMD(packet->type)) - espTrainerCommand(packet->data[0], packet->data + 1, packet->len -1); - else - espTrainerData(packet->data, packet->len); - break; - case ESP_JOYSTICK: - if(ESP_PACKET_ISCMD(packet->type)) - espJoystickCommand(packet->data[0], packet->data + 1, packet->len -1); - else - espJoystickData(packet->data, packet->len); - break; - case ESP_AUDIO: - if(ESP_PACKET_ISCMD(packet->type)) - espAudioCommand(packet->data[0], packet->data + 1, packet->len -1); - else - espAudioData(packet->data, packet->len); - break; - case ESP_FTP: - break; - case ESP_IMU: - break; +void processPacket(const packet_s *packet) { + // Acknowledge we got the command, so radio knows we are running + if(ESP_PACKET_ISCMD(packet->type)) { + writeAck(); + } + + switch (packet->type & ESP_PACKET_TYPE_MSK) { + case ESP_ROOT: + if (ESP_PACKET_ISCMD(packet->type)) + espRootCommand(packet->data[0], packet->data + 1, packet->len - 1); + else + espRootData(packet->data, packet->len); + break; + case ESP_TELEMETRY: + break; + case ESP_TRAINER: + if (ESP_PACKET_ISCMD(packet->type)) + espTrainerCommand(packet->data[0], packet->data + 1, packet->len - 1); + else + espTrainerData(packet->data, packet->len); + break; + case ESP_JOYSTICK: + if (ESP_PACKET_ISCMD(packet->type)) + espJoystickCommand(packet->data[0], packet->data + 1, packet->len - 1); + else + espJoystickData(packet->data, packet->len); + break; + case ESP_AUDIO: + if (ESP_PACKET_ISCMD(packet->type)) + espAudioCommand(packet->data[0], packet->data + 1, packet->len - 1); + else + espAudioData(packet->data, packet->len); + break; + case ESP_FTP: + break; + case ESP_IMU: + break; } } @@ -132,41 +131,46 @@ void processPacket(const packet_s *packet) void mainTask(void *stuff) { ESP_LOGI(LOG_UART, "Waiting for settings to be read"); - while(!settings_ok) {vTaskDelay(50);}; + while (!settings_ok) { + vTaskDelay(50); + }; ESP_LOGI(LOG_UART, "Setting initial role"); - if(settings.mode == ESP_ROOT) { + if (settings.mode == ESP_ROOT) { ESP_LOGE(LOG_UART, "No Role Loaded, Leaving off.. for now"); } -// setRole(settings.role); + // setRole(settings.role); ESP_LOGI(LOG_UART, "Waiting for UART RX Task to start"); - while(!uartRXTaskStarted) {vTaskDelay(20);} + while (!uartRXTaskStarted) { + vTaskDelay(20); + } packet_s packet; - uint8_t buffer[sizeof(packet_s)+1]; - int bufferpos =0; + uint8_t buffer[sizeof(packet_s) + 1]; + int bufferpos = 0; - while (1) { + while (1) { uint8_t inb; int rec = xStreamBufferReceive(uartrxstreamhndl, &inb, 1, 10); - if(rec) { - if(inb == 0 && bufferpos != 0) { - int lenout = cobs_decode(buffer,bufferpos,(uint8_t *)&packet); - //ESP_LOG_BUFFER_HEX("P", (uint8_t *)&packet, lenout); - uint16_t packetcrc = packet.crcl | (packet.crch << 8); // Store transmitted packet + if (rec) { + if (inb == 0 && bufferpos != 0) { + int lenout = cobs_decode(buffer, bufferpos, (uint8_t *)&packet); + // ESP_LOG_BUFFER_HEX("P", (uint8_t *)&packet, lenout); + uint16_t packetcrc = + packet.crcl | (packet.crch << 8); // Store transmitted packet packet.crcl = 0xBB; packet.crch = 0xAA; - uint16_t calccrc = crc16(0,(uint8_t *)&packet,lenout, 0); + uint16_t calccrc = crc16(0, (uint8_t *)&packet, lenout, 0); packet.len = lenout - PACKET_OVERHEAD; - if(packetcrc == calccrc) { - processPacket(&packet); + if (packetcrc == calccrc) { + processPacket(&packet); } else { ESP_LOGE("PM", "CRC Fault"); } bufferpos = 0; } else { buffer[bufferpos++] = inb; - if(bufferpos == sizeof(buffer)) { + if (bufferpos == sizeof(buffer)) { printf("Buffer Overflow\r\n"); bufferpos = 0; } @@ -174,12 +178,12 @@ void mainTask(void *stuff) { } // TEST SENDING PACKETS -/* static uint count=0; - if(count++ % 20 == 0) { - printf("Writting: Hello, Mode=2\r\n"); - writePacket("Hello",5,false,2); - writeCommand(1,3,"DATA",4); - }*/ + /* static uint count=0; + if(count++ % 20 == 0) { + printf("Writting: Hello, Mode=2\r\n"); + writePacket("Hello",5,false,2); + writeCommand(1,3,"DATA",4); + }*/ } vTaskDelete(NULL); } @@ -195,7 +199,7 @@ void mainTask(void *stuff) { switch(curMode) { case ROLE_BLE_CENTRAL: case ROLE_BLE_PERIPHERAL: - default: + default: break; } @@ -207,13 +211,13 @@ void mainTask(void *stuff) { switch(curMode) { case ROLE_BLE_CENTRAL: btCentralState = CENTRAL_STATE_DISCONNECT; - bt_init(); + BTInit(); btcInit(); break; case ROLE_BLE_PERIPHERAL: btPeripherialState = PERIPHERIAL_STATE_DISCONNECTED; - bt_init(); - btjoyInit(); + BTInit(); + BTJoyInit(); break; default: break; @@ -223,7 +227,6 @@ void mainTask(void *stuff) { saveSettings(); }*/ - /*void runBTCentral() { switch(btCentralState) { @@ -243,7 +246,8 @@ void mainTask(void *stuff) { // New item(s) added for(int i=laddcnt; i < bt_scanned_address_cnt; i++) { char addr[13]; - sprintf(reusablebuff, "OK+DISC:%s\r\n",btaddrtostr(addr, btc_scanned_addresses[i])); + sprintf(reusablebuff, "OK+DISC:%s\r\n",btaddrtostr(addr, +btc_scanned_addresses[i])); //printf("%s",reusablebuff); uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); } @@ -281,9 +285,11 @@ void mainTask(void *stuff) { if(btc_validslavefound) { sprintf(reusablebuff, "Connected:%s\r\n", rmtaddress); uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); - sprintf(reusablebuff, "MTU Size:65\r\nMTU Size: 65\r\nPHT Update Complete\r\nCurrent PHY:2M\r\n"); // Fix me - uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); -// sprintf(reusablebuff, "Board:%s\r\n", str_ble_board_types[btc_board_type]); + sprintf(reusablebuff, "MTU Size:65\r\nMTU Size: 65\r\nPHT Update +Complete\r\nCurrent PHY:2M\r\n"); // Fix me uart_write_bytes(uart_num, +reusablebuff, strlen(reusablebuff)); +// sprintf(reusablebuff, "Board:%s\r\n", +str_ble_board_types[btc_board_type]); // uart_write_bytes(uart_num, reusablebuff, strlen(reusablebuff)); btCentralState = CENTRAL_STATE_CONNECTED; // TODO: Add @@ -325,74 +331,69 @@ void runBTPeripherial() }*/ // Builds a packet -void writePacket(const uint8_t *dat, int len, bool iscmd, uint8_t mode) -{ +void writePacket(const uint8_t *dat, int len, bool iscmd, uint8_t mode) { uint8_t encodedbuffer[sizeof(packet_s) + 1]; packet_s packet; packet.type = mode; - if(iscmd) + if (iscmd) packet.type |= (1 << ESP_PACKET_CMD_BIT); packet.crcl = 0xBB; packet.crch = 0xAA; - memcpy(packet.data, dat, len); // TODO, Remove me, extra copy for just the crc calc. - uint16_t crc = crc16(0, (uint8_t *)&packet,len + PACKET_OVERHEAD, 0); + memcpy(packet.data, dat, + len); // TODO, Remove me, extra copy for just the crc calc. + uint16_t crc = crc16(0, (uint8_t *)&packet, len + PACKET_OVERHEAD, 0); packet.crcl = crc & 0xFF; packet.crch = (crc & 0xFF00) >> 8; - - int wl = cobs_encode((uint8_t *)&packet, len + PACKET_OVERHEAD, encodedbuffer); + + int wl = + cobs_encode((uint8_t *)&packet, len + PACKET_OVERHEAD, encodedbuffer); encodedbuffer[wl] = '\0'; - uart_write_bytes(uart_num, (void *)&encodedbuffer, wl+1); + uart_write_bytes(uart_num, (void *)&encodedbuffer, wl + 1); } // Sends some data -void writeData(uint8_t mode, const uint8_t *dat, int len) -{ +void writeData(uint8_t mode, const uint8_t *dat, int len) { writePacket(dat, len, false, mode); } // Send a command -void writeCommand(uint8_t mode, uint8_t command, const uint8_t *dat, int len) -{ +void writeCommand(uint8_t mode, uint8_t command, const uint8_t *dat, int len) { uint8_t buffer[256]; buffer[0] = command; // First byte of a command is the command, data follows - memcpy(buffer+1, dat, len); - writePacket(buffer, len+1, true, mode); + memcpy(buffer + 1, dat, len); + writePacket(buffer, len + 1, true, mode); } // Writes an acknowledge / not-acknowledge and a optional message -void writeAckNak(uint8_t mode, bool ack, const char *message) -{ - uint8_t command = ESP_ROOTCMD_ACKNAK | (ack?1< Date: Tue, 19 Jul 2022 19:01:10 -0700 Subject: [PATCH 14/17] Get / Set Settings --- src/espconmngr.c | 27 ---------------- src/espdefs.h | 66 +++++++++++++++++++++++++++------------ src/esproot.c | 80 +++++++++++++++++++++++++++++++++++++++++++----- src/esproot.h | 2 ++ src/settings.c | 17 ++++++++-- src/settings.h | 9 ++++++ 6 files changed, 145 insertions(+), 56 deletions(-) diff --git a/src/espconmngr.c b/src/espconmngr.c index a486478..735b8e0 100644 --- a/src/espconmngr.c +++ b/src/espconmngr.c @@ -20,33 +20,6 @@ void connectionCommandRX(int conCmd, const uint8_t *data, int len) { case ESP_CON_DISCONNECT: ESP_LOGI(CON_MGR_TAG, "Disconnect"); break; - case ESP_CON_SET_NAME: - ESP_LOGI(CON_MGR_TAG, "Setting name=%s", (const char *)data); - break; - case ESP_CON_SET_MAC: - ESP_LOGI(CON_MGR_TAG, "Setting mac=%s", (const char *)data); - break; - case ESP_CON_SET_SSID: - ESP_LOGI(CON_MGR_TAG, "Setting ssid=%s", (const char *)data); - break; - case ESP_CON_SET_IP: - ESP_LOGI(CON_MGR_TAG, "Setting id=%s", (const char *)data); - break; - case ESP_CON_SET_SUBNET_MASK: - ESP_LOGI(CON_MGR_TAG, "Setting subnetmask=%s", (const char *)data); - break; - case ESP_CON_SET_STATIC_IP: - ESP_LOGI(CON_MGR_TAG, "Setting to static ip mode"); - break; - case ESP_CON_SET_DHCP: - ESP_LOGI(CON_MGR_TAG, "Setting DHCP=%s", (const char *)data); - break; - case ESP_CON_SET_WIFI_STATION: - ESP_LOGI(CON_MGR_TAG, "Setting WIFI to station mode"); - break; - case ESP_CON_SET_WIFI_AP: - ESP_LOGI(CON_MGR_TAG, "Setting WIFI to ap mode"); - break; } } diff --git a/src/espdefs.h b/src/espdefs.h index 648c4d5..ecb34d4 100644 --- a/src/espdefs.h +++ b/src/espdefs.h @@ -4,6 +4,8 @@ #include #include +#define MAX_OUTPUT_CHANNELS 32 + #define ESP_PACKET_TYPE_MSK 0x0F #define ESP_PACKET_CMD_BIT 6 #define ESP_PACKET_ACK_BIT 7 @@ -29,15 +31,17 @@ enum ESPRootCmds { ESP_ROOTCMD_VERSION, // Request Version ESP_ROOTCMD_CON_EVENT, // ESP Connection event ESP_ROOTCMD_CON_MGMNT, // Set ESP Connection Parameters + ESP_ROOTCMD_SET_VALUE, // Set a value + ESP_ROOTCMD_GET_VALUE, // Request a value }; enum ESPConnectionEvents { - ESP_EVT_MESSAGE, + ESP_EVT_MESSAGE, // String value of status ESP_EVT_DISCOVER_STARTED, ESP_EVT_DISCOVER_COMPLETE, - ESP_EVT_DEVICE_FOUND, - ESP_EVT_CONNECTED, - ESP_EVT_DISCONNECTED, + ESP_EVT_DEVICE_FOUND, // A connectable device was found + ESP_EVT_CONNECTED, // Periodically send this event. + ESP_EVT_DISCONNECTED, // Periodically send this event. ESP_EVT_PIN_REQUEST, ESP_EVT_IP_OBTAINED }; @@ -47,15 +51,11 @@ enum ESPConnectionManagment { ESP_CON_DISCOVER_STOP, ESP_CON_CONNECT, ESP_CON_DISCONNECT, - ESP_CON_SET_NAME, - ESP_CON_SET_MAC, - ESP_CON_SET_SSID, - ESP_CON_SET_IP, - ESP_CON_SET_SUBNET_MASK, - ESP_CON_SET_STATIC_IP, - ESP_CON_SET_DHCP, - ESP_CON_SET_WIFI_STATION, - ESP_CON_SET_WIFI_AP, +}; + +enum ESPWifiModes { + ESP_WIFI_STATION, + ESP_WIFI_AP, }; enum ESPTrainerCmds { @@ -63,21 +63,18 @@ enum ESPTrainerCmds { ESP_TRAINERCMD_SET_SLAVE, }; -// Channel Data (Joystick, Trainer) - -#define MAX_OUTPUT_CHANNELS 32 // Channel Format typedef struct { int16_t ch[MAX_OUTPUT_CHANNELS]; - uint32_t channelmask; // Valid Channels + uint32_t channelmask; // Valid Channels } channeldata; typedef struct { uint8_t bteaddr[6]; uint8_t rssi; char name[30]; -} btscanresult; +} scanresult; typedef struct { uint8_t maj; @@ -86,4 +83,35 @@ typedef struct { uint8_t sha[10]; } espversion; -extern espversion espVersion; \ No newline at end of file + +typedef struct { + uint8_t event; // Event ID + uint8_t data[50]; +} espevent; + +// ESP Settings +typedef struct { + char variable[5]; + void *ptr; + int len; +} espsettingslink; + +// ESP Settings +typedef struct { + char name[40]; + char wifimac[18]; + char blemac[18]; + char ssid[30]; + char ip[16]; + char subnet[16]; + char staticip[16]; + uint8_t dhcpMode; + uint8_t wifiStationMode; +} espsettings; + +#define SETTING_LINK_ARR(name, _array) {name, (void*)_array, sizeof(_array)} +#define SETTING_LINK_VAR(name, variable) {name, &variable, sizeof(variable)} +#define SETTINGS_COUNT (sizeof(espSettingsIndex)/sizeof(espsettingslink)) +#define SETTING_LEN 4 + +extern espsettings espSettings; \ No newline at end of file diff --git a/src/esproot.c b/src/esproot.c index 27a38be..af75b72 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -6,12 +6,27 @@ #include "esptrainer.h" #include "terminal.h" #include +#include #define LOG_ESPR "ESPROOT" // TODO, Replace me with git SHA espversion espVersion = {1, 0, 0, "GITTAG"}; +// Global ESP Settings +espsettings espSettings; +const espsettingslink espSettingsIndex[] = { + SETTING_LINK_ARR("name", + espSettings.name), // Settings must be 4 characters! + SETTING_LINK_ARR("wmac", espSettings.wifimac), + SETTING_LINK_ARR("btma", espSettings.blemac), + SETTING_LINK_ARR("ssid", espSettings.ssid), + SETTING_LINK_ARR("ip ", espSettings.ip), + SETTING_LINK_ARR("subn", espSettings.subnet), + SETTING_LINK_ARR("stip", espSettings.staticip), + SETTING_LINK_VAR("dhcp", espSettings.dhcpMode), + SETTING_LINK_VAR("wimd", espSettings.wifiStationMode)}; + int g_rv; uint8_t g_m; bool g_radioIsBTEDR = false; @@ -20,20 +35,32 @@ bool g_radioIsWIFI = false; #define STARTBLE_MODE(m, x) \ g_rv = x; \ - if (g_rv == 0) \ - g_radioIsBLE = true; + g_m = m; \ + if (g_rv == 0) { \ + g_radioIsBLE = true; \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_START_MODE, &g_m, 1); \ + } else { \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_STOP_MODE, &g_m, 1); \ + } + #define STARTBTEDR_MODE(m, x) \ g_rv = x; \ - if (g_rv == 0) \ - g_radioIsBTEDR = true; + g_m = m; \ + if (g_rv == 0) { \ + g_radioIsBTEDR = true; \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_START_MODE, &g_m, 1); \ + } else { \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_STOP_MODE, &g_m, 1); \ + } + #define STARTWIFI_MODE(m, x) \ g_rv = x; \ - g_m = m; + g_m = m; \ if (g_rv == 0) { \ g_radioIsWIFI = true; \ - writeCommand(ESP_ROOT, ESP_ROOTCMD_START_MODE, &m, 1); \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_START_MODE, &g_m, 1); \ } else { \ - writeCommand(ESP_ROOT, ESP_ROOTCMD_STOP_MODE, &m, 1); \ + writeCommand(ESP_ROOT, ESP_ROOTCMD_STOP_MODE, &g_m, 1); \ } #define START_MODE(m, x) writeAckNak(m, x, ""); @@ -149,5 +176,44 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { ESP_LOGI(LOG_ESPR, "Con Mgr. Cmd %d", data[0]); connectionCommandRX(data[0], data + 1, len - 1); break; + case ESP_ROOTCMD_SET_VALUE: { + // First 4 Characters are the Variable, Remainder is the Data + if (len > 5) { + char variable[5]; + memcpy(variable, data, 4); + variable[4] = '\0'; + ESP_LOGI("SETT", "Radio Set %s", variable); + for (unsigned int i = 0; i < SETTINGS_COUNT; i++) { + if (!strcmp(variable, espSettingsIndex[i].variable)) { + // Found the variable, make sure it's the same size + if (len - SETTING_LEN == espSettingsIndex[i].len) { + memcpy(espSettingsIndex[i].ptr, data + 4, len - 4); + ESP_LOGI("SETT", "Radio Set Success"); + } + break; + } + } + } + break; + } + case ESP_ROOTCMD_GET_VALUE: { + uint8_t buffer[50]; + // First 4 Characters are the Variable, Remainder is the Data + if (len == 4) { + char variable[5]; + memcpy(variable, data, 4); + variable[4] = '\0'; + ESP_LOGI("SETT", "Radio Requesting %s", variable); + for (unsigned int i = 0; i < SETTINGS_COUNT; i++) { + if (!strcmp(variable, espSettingsIndex[i].variable)) { + ESP_LOGI("SETT", "Found Variable.. Sending"); + memcpy(buffer, espSettingsIndex[i].variable, SETTING_LEN); + memcpy(buffer+4, espSettingsIndex[i].ptr, espSettingsIndex[i].len); + writeCommand(ESP_ROOT, ESP_ROOTCMD_SET_VALUE, buffer, espSettingsIndex[i].len+SETTING_LEN); + } + } + } + break; + } } } diff --git a/src/esproot.h b/src/esproot.h index 71ed625..09a8f3f 100644 --- a/src/esproot.h +++ b/src/esproot.h @@ -5,3 +5,5 @@ void espRootData(const uint8_t *data, uint8_t len); void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len); + + diff --git a/src/settings.c b/src/settings.c index 4e6399a..e271acd 100644 --- a/src/settings.c +++ b/src/settings.c @@ -42,13 +42,13 @@ void saveSettings() } } -void _loadSettings(settings_t *s) +void _loadSettings(settings_t *s) { ESP_LOGI(LOG_SET,"Reading settings"); size_t length = sizeof(settings_t); esp_err_t ret = nvs_get_blob(nvs_flsh_btw, NVS_STRUCT_KEY, (void*)s, &length); if(ret == ESP_OK && length == sizeof(settings_t)) { - ESP_LOGI(LOG_SET, "Settings Read Successfully"); + ESP_LOGI(LOG_SET, "Settings Read Successfully"); return; } else { if(ret == ESP_OK) { @@ -65,7 +65,18 @@ void loadSettings() { // Load settings into global settings struct. _loadSettings(&settings); - + // Will be filled properly here or nulled out settings_ok = true; + + // Fill the Radio Specific Settings + strcpy(espSettings.blemac, settings.blemac); + strcpy(espSettings.ip, settings.ip); + strcpy(espSettings.name, settings.name); + strcpy(espSettings.ssid, settings.ssid); + strcpy(espSettings.staticip, settings.staticip); + strcpy(espSettings.subnet, settings.subnet); + strcpy(espSettings.wifimac, settings.wifimac); + espSettings.dhcpMode = settings.dhcpMode; + espSettings.wifiStationMode = settings.wifiStationMode; } \ No newline at end of file diff --git a/src/settings.h b/src/settings.h index 8b3bd44..4582625 100644 --- a/src/settings.h +++ b/src/settings.h @@ -9,6 +9,15 @@ typedef struct { // otherwise ignore char rmtbtaddr[13]; uint8_t mode; + char name[40]; + char wifimac[18]; + char blemac[18]; + char ssid[30]; + char ip[16]; + char subnet[16]; + char staticip[16]; + uint8_t dhcpMode; + uint8_t wifiStationMode; } settings_t; extern settings_t settings; From d728481bc1323345136fe1bd38a5e56ce02930ab Mon Sep 17 00:00:00 2001 From: dlktdr Date: Wed, 20 Jul 2022 00:56:19 -0700 Subject: [PATCH 15/17] Add packet debugging --- src/espdefs.h | 22 +++++++++++----------- src/esproot.c | 22 ++++++++++++---------- src/terminal.c | 49 +++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/src/espdefs.h b/src/espdefs.h index ecb34d4..82ba62c 100644 --- a/src/espdefs.h +++ b/src/espdefs.h @@ -36,12 +36,12 @@ enum ESPRootCmds { }; enum ESPConnectionEvents { - ESP_EVT_MESSAGE, // String value of status + ESP_EVT_MESSAGE, // String value of status ESP_EVT_DISCOVER_STARTED, ESP_EVT_DISCOVER_COMPLETE, - ESP_EVT_DEVICE_FOUND, // A connectable device was found - ESP_EVT_CONNECTED, // Periodically send this event. - ESP_EVT_DISCONNECTED, // Periodically send this event. + ESP_EVT_DEVICE_FOUND, // A connectable device was found + ESP_EVT_CONNECTED, // Periodically send this event. + ESP_EVT_DISCONNECTED, // Periodically send this event. ESP_EVT_PIN_REQUEST, ESP_EVT_IP_OBTAINED }; @@ -63,11 +63,10 @@ enum ESPTrainerCmds { ESP_TRAINERCMD_SET_SLAVE, }; - // Channel Format typedef struct { int16_t ch[MAX_OUTPUT_CHANNELS]; - uint32_t channelmask; // Valid Channels + uint32_t channelmask; // Valid Channels } channeldata; typedef struct { @@ -83,9 +82,8 @@ typedef struct { uint8_t sha[10]; } espversion; - typedef struct { - uint8_t event; // Event ID + uint8_t event; // Event ID uint8_t data[50]; } espevent; @@ -109,9 +107,11 @@ typedef struct { uint8_t wifiStationMode; } espsettings; -#define SETTING_LINK_ARR(name, _array) {name, (void*)_array, sizeof(_array)} -#define SETTING_LINK_VAR(name, variable) {name, &variable, sizeof(variable)} -#define SETTINGS_COUNT (sizeof(espSettingsIndex)/sizeof(espsettingslink)) +#define SETTING_LINK_ARR(name, _array) \ + { name, (void *)_array, sizeof(_array) } +#define SETTING_LINK_VAR(name, variable) \ + { name, &variable, sizeof(variable) } +#define SETTINGS_COUNT (sizeof(espSettingsIndex) / sizeof(espsettingslink)) #define SETTING_LEN 4 extern espsettings espSettings; \ No newline at end of file diff --git a/src/esproot.c b/src/esproot.c index af75b72..b327d66 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -15,9 +15,10 @@ espversion espVersion = {1, 0, 0, "GITTAG"}; // Global ESP Settings espsettings espSettings; + +// Settings must be 4 characters! const espsettingslink espSettingsIndex[] = { - SETTING_LINK_ARR("name", - espSettings.name), // Settings must be 4 characters! + SETTING_LINK_ARR("name", espSettings.name), SETTING_LINK_ARR("wmac", espSettings.wifimac), SETTING_LINK_ARR("btma", espSettings.blemac), SETTING_LINK_ARR("ssid", espSettings.ssid), @@ -71,7 +72,7 @@ uint8_t runningModes; void espRootData(const uint8_t *data, uint8_t len) {} void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { - ESP_LOGI(LOG_ESPR, "Root Command %d", command); + //ESP_LOGI(LOG_ESPR, "Root Command %d", command); switch (command) { case ESP_ROOTCMD_START_MODE: @@ -178,17 +179,17 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { break; case ESP_ROOTCMD_SET_VALUE: { // First 4 Characters are the Variable, Remainder is the Data - if (len > 5) { + if (len > 4) { char variable[5]; memcpy(variable, data, 4); variable[4] = '\0'; - ESP_LOGI("SETT", "Radio Set %s", variable); + // ESP_LOGI("SETT", "Radio Set %s", variable); for (unsigned int i = 0; i < SETTINGS_COUNT; i++) { if (!strcmp(variable, espSettingsIndex[i].variable)) { // Found the variable, make sure it's the same size if (len - SETTING_LEN == espSettingsIndex[i].len) { memcpy(espSettingsIndex[i].ptr, data + 4, len - 4); - ESP_LOGI("SETT", "Radio Set Success"); + ESP_LOGI("ROOT", "Radio Set %s Success", espSettingsIndex[i].variable); } break; } @@ -203,13 +204,14 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { char variable[5]; memcpy(variable, data, 4); variable[4] = '\0'; - ESP_LOGI("SETT", "Radio Requesting %s", variable); + // ESP_LOGI("SETT", "Radio Requesting %s", variable); for (unsigned int i = 0; i < SETTINGS_COUNT; i++) { if (!strcmp(variable, espSettingsIndex[i].variable)) { - ESP_LOGI("SETT", "Found Variable.. Sending"); + ESP_LOGI("ROOT", "Found Variable %s.. Sending it", espSettingsIndex[i].variable); memcpy(buffer, espSettingsIndex[i].variable, SETTING_LEN); - memcpy(buffer+4, espSettingsIndex[i].ptr, espSettingsIndex[i].len); - writeCommand(ESP_ROOT, ESP_ROOTCMD_SET_VALUE, buffer, espSettingsIndex[i].len+SETTING_LEN); + memcpy(buffer + 4, espSettingsIndex[i].ptr, espSettingsIndex[i].len); + writeCommand(ESP_ROOT, ESP_ROOTCMD_SET_VALUE, buffer, + espSettingsIndex[i].len + SETTING_LEN); } } } diff --git a/src/terminal.c b/src/terminal.c index 288d6a8..71bab37 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -22,6 +22,35 @@ #include "esptrainer.h" #include "freertos/stream_buffer.h" +#define DEBUG_PACKETS + +#ifdef DEBUG_PACKETS +const char *ESPRootCmdsStr[] = {"Start Mode", "Stop Mode", "Active Modes", + "Restart", "Version", "ConEvent", + "ConMgmt", "SetValue", "GetValue"}; + +const char *ESPModesStr[] = {"Root", "Telemetry", "Trainer", "Joystick", + "Audio", "FTP", "IMU"}; + +#define DEBUG_PACKET(pkt) \ + if (!ESP_PACKET_ISCMD(pkt->type)) \ + printf("RX[D] Mode-%s Len-%d\r\n", \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], pkt->len); \ + else if (ESP_PACKET_ISACK(pkt->type)) \ + printf("TX[C] Mode-%s (Acknowledge)\r\n", \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK]); \ + else if (pkt->type == ESP_ROOT) \ + printf("TX[C] Mode-%s Cmd-%s Len-%d\r\n", \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], \ + ESPRootCmdsStr[pkt->data[0]], pkt->len - 1); \ + else \ + printf("RX[C] Mode-%s Cmd-0x%0.2x Len-%d\r\n", \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], pkt->data[0], \ + pkt->len - 1); +#else +#define DEBUG_PACKET(packet) +#endif + #define LOG_UART "UART" #define UART_RX_BUFFER 1024 @@ -80,17 +109,15 @@ int atcommandlen = -1; circular_buffer uartinbuf; -#define ESP_BASE 0 -#define ESP_PACKET_TYPE_MSK 0x0F -#define ESP_PACKET_CMD_BIT 6 -#define ESP_PACKET_ACK_BIT 7 -#define ESP_PACKET_ISCMD(t) (t & (1 << ESP_PACKET_CMD_BIT)) -#define ESP_PACKET_ISACKREQ(t) (t & (1 << ESP_PACKET_ACK_BIT)) - void processPacket(const packet_s *packet) { - // Acknowledge we got the command, so radio knows we are running - if(ESP_PACKET_ISCMD(packet->type)) { + DEBUG_PACKET(packet); + + // Acknowledge we got the command + if (ESP_PACKET_ISCMD(packet->type) && !ESP_PACKET_ISACK(packet->type)) { writeAck(); + } else if (ESP_PACKET_ISCMD(packet->type) && ESP_PACKET_ISACK(packet->type)) { + ESP_LOGD("PCKT", "ACK Rec"); + // TODO use it. } switch (packet->type & ESP_PACKET_TYPE_MSK) { @@ -349,6 +376,8 @@ void writePacket(const uint8_t *dat, int len, bool iscmd, uint8_t mode) { cobs_encode((uint8_t *)&packet, len + PACKET_OVERHEAD, encodedbuffer); encodedbuffer[wl] = '\0'; uart_write_bytes(uart_num, (void *)&encodedbuffer, wl + 1); + + DEBUG_PACKET((&packet)); } // Sends some data @@ -366,7 +395,7 @@ void writeCommand(uint8_t mode, uint8_t command, const uint8_t *dat, int len) { // Writes an acknowledge / not-acknowledge and a optional message void writeAck() { - writeCommand(ESP_ROOT, 1 << ESP_PACKET_ACK_BIT, NULL, 0); + writePacket(NULL, 0, true, ESP_ROOT | 1 << ESP_PACKET_ACK_BIT); } // Read from the UART RX, write to the stream if data available, this task From 215181eac6267171436ab0d2020b7ead5022916e Mon Sep 17 00:00:00 2001 From: dlktdr Date: Tue, 13 Sep 2022 19:55:16 -0700 Subject: [PATCH 16/17] Add Mode Modes & Start of Discovery --- platformio.ini | 4 +- src/bt_client/bt_client.c | 17 ++++++--- src/espaudio.c | 5 +++ src/espaudio.h | 1 + src/espconmngr.c | 31 ++++++++++++++++ src/espjoystick.c | 7 ++++ src/espjoystick.h | 3 ++ src/esproot.c | 37 ++++++++++++------- src/esptelemetry.c | 45 +++++++++++++++++++++++ src/esptelemetry.h | 12 ++++++ src/esptrainer.c | 55 +++++++++++++++++++++++++--- src/esptrainer.h | 11 +++++- src/terminal.c | 77 +++++++++++++++++++++++++-------------- src/terminal.h | 21 +++-------- 14 files changed, 255 insertions(+), 71 deletions(-) create mode 100644 src/esptelemetry.c create mode 100644 src/esptelemetry.h diff --git a/platformio.ini b/platformio.ini index f8a95f1..b77f152 100644 --- a/platformio.ini +++ b/platformio.ini @@ -78,8 +78,8 @@ build_flags = board = esp32-c3-devkitm-1 upload_protocol = esptool monitor_speed = 115200 -upload_port = COM30 -monitor_port = COM30 +upload_port = COM4 +monitor_port = COM4 #debug_tool = esptool build_flags = -DRTOS_FREERTOS=y diff --git a/src/bt_client/bt_client.c b/src/bt_client/bt_client.c index 052f19e..30cef85 100644 --- a/src/bt_client/bt_client.c +++ b/src/bt_client/bt_client.c @@ -39,6 +39,8 @@ #include "terminal.h" #include "defines.h" +#include "esptrainer.h" + #define GATTC_TAG "BTCLIENT" #define REMOTE_SERVICE_UUID 0xFFF0 #define REMOTE_FRSKY_CHAR_UUID 0xFFF6 @@ -353,7 +355,10 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ #ifdef DEBUG_TIMERS processFrame(p_data->notify.value,p_data->notify.value_len); // Used to decode the channel data for debugging #endif - uart_write_bytes(uart_num, (void*)p_data->notify.value, p_data->notify.value_len); // Write the received data to the UART port + + // TODO.. fixme + // espTrainerRFDataReceived() + // uart_write_bytes(uart_num, (void*)p_data->notify.value, p_data->notify.value_len); // Write the received data to the UART port // if(p_data->notify.handle == bt_datahandle) // If notify coming from the data handle, send it to the UART port // else @@ -450,10 +455,11 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par btaddrtostr(addr, scan_result->scan_rst.bda); printf("Disc BT Address %s, RSSI=%d, Addr Type=%d\n",addr, scan_result->scan_rst.rssi, scan_result->scan_rst.ble_addr_type); + espTrainerDeviceDiscovered(addr); break; } case ESP_GAP_SEARCH_INQ_CMPL_EVT: { - btc_scan_complete = true; + espTrainerDiscoverComplete(); break; } default: { @@ -606,10 +612,11 @@ void btcInit() uint8_t adrtype; esp_ble_gap_get_local_used_addr(localbtaddress, &adrtype); - vTaskDelay(pdMS_TO_TICKS(500)); - // Try to connect to saved address on startup + + /* + // Try to connect to saved address on startup.. // Leave me up to the radio esp_bd_addr_t addr; - strtobtaddr(addr, settings.rmtbtaddr); + strtobtaddr(addr, settings.rmtbtaddr);*/ //btc_connect(addr); } \ No newline at end of file diff --git a/src/espaudio.c b/src/espaudio.c index 863dcf4..66761c7 100644 --- a/src/espaudio.c +++ b/src/espaudio.c @@ -16,6 +16,11 @@ void espAudioStop() } +void espAudioExec() +{ + +} + bool espAudioRunning() { return false; diff --git a/src/espaudio.h b/src/espaudio.h index a1c6e9a..1d19eda 100644 --- a/src/espaudio.h +++ b/src/espaudio.h @@ -4,6 +4,7 @@ int espAudioStart(); void espAudioStop(); +void espAudioExec(); bool espAudioRunning(); void espAudioData(const uint8_t *data, uint8_t len); void espAudioCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/espconmngr.c b/src/espconmngr.c index 735b8e0..ce4140f 100644 --- a/src/espconmngr.c +++ b/src/espconmngr.c @@ -2,17 +2,28 @@ #include "esp_log.h" #include "espdefs.h" #include "terminal.h" + +#include "esptrainer.h" +#include "esptelemetry.h" +#include "espaudio.h" +#include "espjoystick.h" + #include #define CON_MGR_TAG "CONMGR" +static void discoverStart(); +static void discoverStop(); + void connectionCommandRX(int conCmd, const uint8_t *data, int len) { switch (conCmd) { case ESP_CON_DISCOVER_START: ESP_LOGI(CON_MGR_TAG, "Discovery Start Req"); + discoverStart(); break; case ESP_CON_DISCOVER_STOP: ESP_LOGI(CON_MGR_TAG, "Discovery Stop Req"); + void discoverStop(); break; case ESP_CON_CONNECT: ESP_LOGI(CON_MGR_TAG, "Connect to device %s", (const char *)data); @@ -27,4 +38,24 @@ void connectionEventRX(int event, const uint8_t *data, int len) {} void sendEvent(int event, const uint8_t *data, int len) { writeCommand(ESP_ROOT, ESP_ROOTCMD_CON_EVENT, data, len); +} + +void discoverStart() +{ + if(espTelemetryRunning()) + espTelemetryDiscoverStart(); + else if(espTrainerRunning()) + espTrainerDiscoverStart(); + else if(espJoystickRunning()) + espJoystickDiscoverStart(); +} + +void discoverStop() +{ + if(espTelemetryRunning()) + espTelemetryDiscoverStop(); + else if(espTrainerRunning()) + espTrainerDiscoverStop(); + else if(espJoystickRunning()) + espJoystickDiscoverStop(); } \ No newline at end of file diff --git a/src/espjoystick.c b/src/espjoystick.c index d485977..638d54e 100644 --- a/src/espjoystick.c +++ b/src/espjoystick.c @@ -29,6 +29,11 @@ void espJoystickStop() { bool espJoystickRunning() { return joystickstarted; } +void espJoystickExec() +{ + +} + void espJoystickData(const uint8_t *data, uint8_t len) { if (len == sizeof(channeldata)) { /*const channeldata *chdata = (const channeldata *)data; @@ -45,3 +50,5 @@ void espJoystickData(const uint8_t *data, uint8_t len) { } void espJoystickCommand(uint8_t command, const uint8_t *data, uint8_t len) {} +void espJoystickDiscoverStart() {} +void espJoystickDiscoverStop() {} \ No newline at end of file diff --git a/src/espjoystick.h b/src/espjoystick.h index 1790aa0..f6b1184 100644 --- a/src/espjoystick.h +++ b/src/espjoystick.h @@ -4,6 +4,9 @@ int espJoystickStart(); void espJoystickStop(); +void espJoystickExec(); bool espJoystickRunning(); void espJoystickData(const uint8_t *data, uint8_t len); void espJoystickCommand(uint8_t command, const uint8_t *data, uint8_t len); +void espJoystickDiscoverStart(); +void espJoystickDiscoverStop(); \ No newline at end of file diff --git a/src/esproot.c b/src/esproot.c index b327d66..3e236e9 100644 --- a/src/esproot.c +++ b/src/esproot.c @@ -65,7 +65,7 @@ bool g_radioIsWIFI = false; } #define START_MODE(m, x) writeAckNak(m, x, ""); -#define RADIO_USED() (g_radioIsBTEDR || g_radioIsBLE || g_radioIsWIFI) +#define RADIO_USED (g_radioIsBTEDR || g_radioIsBLE || g_radioIsWIFI) uint8_t runningModes; @@ -78,15 +78,15 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { case ESP_ROOTCMD_START_MODE: if (len != 1) break; - ESP_LOGI(LOG_ESPR, "Starting Mode %d", data[0]); - if (RADIO_USED() && ((data[0] == ESP_TELEMETRY || data[0] == ESP_TRAINER || + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Starting Mode %d", data[0]); + if (RADIO_USED && ((data[0] == ESP_TELEMETRY || data[0] == ESP_TRAINER || data[0] == ESP_JOYSTICK || data[0] == ESP_AUDIO || data[0] == ESP_FTP))) { ESP_LOGE(LOG_ESPR, "Cannot start Radio already used"); } else { switch (data[0]) { case ESP_TELEMETRY: - // STARTRADIO_MODE(ESP_TELEMETRY, espTelemetryStart()); + STARTBTEDR_MODE(ESP_TELEMETRY, espTelemetryStart()); break; case ESP_TRAINER: STARTBLE_MODE(ESP_TRAINER, espTrainerStart()); @@ -112,14 +112,13 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { case ESP_ROOTCMD_STOP_MODE: if (len != 1) break; - ESP_LOGI(LOG_ESPR, "Stopping Mode %d", data[0]); + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Stopping Mode %d", data[0]); switch (data[0]) { case ESP_TELEMETRY: - // if(espTelemetryRunning()) { - // espTelemetryStop(); - // g_radioIsBTEDR = false; // WHAT TO DO, I COULD SEE BOTH.. - // probably EDR with a serial port tho - // } + if(espTelemetryRunning()) { + espTelemetryStop(); + g_radioIsBTEDR = false; + } break; case ESP_TRAINER: if (espTrainerRunning()) { @@ -156,28 +155,37 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { } break; + case ESP_ROOTCMD_ACTIVE_MODES: + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Get Active Modes"); + case ESP_ROOTCMD_RESTART: - ESP_LOGI(LOG_ESPR, "Rebooting..."); + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Rebooting..."); esp_restart(); break; + case ESP_ROOTCMD_VERSION: writeCommand(ESP_ROOT, ESP_ROOTCMD_VERSION, (const uint8_t *)&espVersion, sizeof(espversion)); break; + case ESP_ROOTCMD_CON_EVENT: // Shouldn't be much here, events are generated - // here + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Connection Event"); if (len < 1) break; - ESP_LOGI(LOG_ESPR, "Con Mgr. Event %d", data[0]); + ESP_LOGI(LOG_ESPR, " Con Mgr. Event %d", data[0]); connectionEventRX(data[0], data + 1, len - 1); break; + case ESP_ROOTCMD_CON_MGMNT: + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Connection Command Received"); if (len < 1) break; - ESP_LOGI(LOG_ESPR, "Con Mgr. Cmd %d", data[0]); + ESP_LOGI(LOG_ESPR, " Con Mgr. Cmd %d", data[0]); connectionCommandRX(data[0], data + 1, len - 1); break; + case ESP_ROOTCMD_SET_VALUE: { + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Setting, Setting value"); // First 4 Characters are the Variable, Remainder is the Data if (len > 4) { char variable[5]; @@ -198,6 +206,7 @@ void espRootCommand(uint8_t command, const uint8_t *data, uint8_t len) { break; } case ESP_ROOTCMD_GET_VALUE: { + ESP_LOGI(LOG_ESPR, "Root Cmd RX: Setting, Requested value"); uint8_t buffer[50]; // First 4 Characters are the Variable, Remainder is the Data if (len == 4) { diff --git a/src/esptelemetry.c b/src/esptelemetry.c new file mode 100644 index 0000000..6e2c5b2 --- /dev/null +++ b/src/esptelemetry.c @@ -0,0 +1,45 @@ +#include "espTelemetry.h" +#include "bt.h" +#include "esp_log.h" +#include "joystick/bt_joystick.h" +#include "joystick/hidd_le_prf_int.h" +#include + +#define TELEMETRY_TAG "TELEM" + +bool telemetrystarted = false; + +int espTelemetryStart() { + ESP_LOGI(TELEMETRY_TAG, "Telemetry Starting"); + if (telemetrystarted) + return -1; + telemetrystarted = true; + //BTInit(); + //BTJoyInit(); + return 0; +} + +void espTelemetryStop() { + ESP_LOGI(TELEMETRY_TAG, "Telemetry Stop"); + if (telemetrystarted) { + //bt_disable(); + } + telemetrystarted = false; +} + +bool espTelemetryRunning() { return telemetrystarted; } + +bool espTelemetryExec() +{ + +} + +void espTelemetryData(const uint8_t *data, uint8_t len) +{ + +} + +void espTelemetryCommand(uint8_t command, const uint8_t *data, uint8_t len) {} + +void espTelemetryDiscoverStart() {} +void espTelemetryDiscoverStop() {} \ No newline at end of file diff --git a/src/esptelemetry.h b/src/esptelemetry.h new file mode 100644 index 0000000..6d3ac73 --- /dev/null +++ b/src/esptelemetry.h @@ -0,0 +1,12 @@ +#pragma once + +#include "espdefs.h" + +int espTelemetryStart(); +void espTelemetryStop(); +bool espTelemetryExec(); +bool espTelemetryRunning(); +void espTelemetryData(const uint8_t *data, uint8_t len); +void espTelemetryCommand(uint8_t command, const uint8_t *data, uint8_t len); +void espTelemetryDiscoverStart(); +void espTelemetryDiscoverStop(); diff --git a/src/esptrainer.c b/src/esptrainer.c index 6f70c93..55a1b21 100644 --- a/src/esptrainer.c +++ b/src/esptrainer.c @@ -10,6 +10,7 @@ bool trainerstarted = false; bool trainerismaster = false; +bool isDiscoveryStarted = false; int espTrainerStart() { if (trainerstarted) @@ -20,19 +21,19 @@ int espTrainerStart() { // Start Master Mode BTInit(); btpInit(); - /*writeCommand(ESP_TRAINER, + writeCommand(ESP_TRAINER, ESP_TRAINERCMD_SET_MASTER, NULL, - 0);*/ + 0); } else { ESP_LOGI(TRAINER_TAG, "Trainer Slave Start"); // Start Slave Mode BTInit(); btcInit(); - /*writeCommand(ESP_TRAINER, - ESP_TRAINERCMD_SET_MASTER, - NULL, - 0);*/ + writeCommand(ESP_TRAINER, + ESP_TRAINERCMD_SET_MASTER, + NULL, + 0); } trainerstarted = true; @@ -48,6 +49,11 @@ void espTrainerStop() { bool espTrainerRunning() { return trainerstarted; } +void espTrainerExec() +{ + +} + // Trainer data received void espTrainerData(const uint8_t *data, uint8_t len) { if (len == sizeof(channeldata)) { @@ -87,3 +93,40 @@ void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len) { void espTrainerSend(const channeldata *chans) { writePacket((const uint8_t *)chans, sizeof(channeldata), false, ESP_TRAINER); } + +void espTrainerDiscoverStart() +{ + ESP_LOGI(TRAINER_TAG, "Trainer Discovery Requested"); + if(isDiscoveryStarted || !trainerismaster) { + ESP_LOGE(TRAINER_TAG, "Discovery already started or not master mode"); + return; + } + btc_start_scan(); + writeEvent(ESP_EVT_DISCOVER_STARTED, NULL, 0); + isDiscoveryStarted = true; +} + +void espTrainerDeviceDiscovered(const char *address) +{ + ESP_LOGI(TRAINER_TAG, "Device discovered %s", address); + writeEvent(ESP_EVT_DEVICE_FOUND, (uint8_t*)address, strlen(address)+1); +} + +void espTrainerRFDataReceived(const channeldata *chans) +{ + +} + +void espTrainerDiscoverComplete() +{ + ESP_LOGI(TRAINER_TAG, "Discoverery Complete"); + writeEvent(ESP_EVT_DISCOVER_COMPLETE, NULL, 0); + isDiscoveryStarted = false; +} + +void espTrainerDiscoverStop() +{ + ESP_LOGI(TRAINER_TAG, "Trainer Discovery Stop Requested"); + isDiscoveryStarted = false; + +} \ No newline at end of file diff --git a/src/esptrainer.h b/src/esptrainer.h index 5cb20a1..b657404 100644 --- a/src/esptrainer.h +++ b/src/esptrainer.h @@ -2,9 +2,18 @@ #include "espdefs.h" +// Main functions to radio int espTrainerStart(); void espTrainerStop(); +void espTrainerExec(); bool espTrainerRunning(); void espTrainerData(const uint8_t *data, uint8_t len); void espTrainerCommand(uint8_t command, const uint8_t *data, uint8_t len); -void espTrainerSend(const channeldata *chans); \ No newline at end of file +void espTrainerSend(const channeldata *chans); +void espTrainerDiscoverStart(); +void espTrainerDiscoverStop(); + +// Internal Functions, Bluetooth +void espTrainerDeviceDiscovered(const char *address); +void espTrainerRFDataReceived(const channeldata *chans); +void espTrainerDiscoverComplete(); \ No newline at end of file diff --git a/src/terminal.c b/src/terminal.c index 71bab37..6f728fd 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -20,6 +20,8 @@ #include "espjoystick.h" #include "esproot.h" #include "esptrainer.h" +#include "esptelemetry.h" + #include "freertos/stream_buffer.h" #define DEBUG_PACKETS @@ -32,29 +34,28 @@ const char *ESPRootCmdsStr[] = {"Start Mode", "Stop Mode", "Active Modes", const char *ESPModesStr[] = {"Root", "Telemetry", "Trainer", "Joystick", "Audio", "FTP", "IMU"}; -#define DEBUG_PACKET(pkt) \ - if (!ESP_PACKET_ISCMD(pkt->type)) \ - printf("RX[D] Mode-%s Len-%d\r\n", \ - ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], pkt->len); \ - else if (ESP_PACKET_ISACK(pkt->type)) \ - printf("TX[C] Mode-%s (Acknowledge)\r\n", \ - ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK]); \ - else if (pkt->type == ESP_ROOT) \ - printf("TX[C] Mode-%s Cmd-%s Len-%d\r\n", \ - ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], \ - ESPRootCmdsStr[pkt->data[0]], pkt->len - 1); \ +#define DEBUG_PACKET(rxtx, pkt) \ + if (!ESP_PACKET_ISCMD(pkt->type)) \ + printf("%cX[D] Mode-%s Len-%d\r\n", rxtx, \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], pkt->len); \ + else if (ESP_PACKET_ISACK(pkt->type)) \ + printf("%cX[C] Mode-%s (Acknowledge)\r\n", rxtx, \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK]); \ + else if (pkt->type == ESP_ROOT) \ + printf("%cX[C] Mode-%s Cmd-%s Len-%d\r\n", rxtx, \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], \ + ESPRootCmdsStr[pkt->data[0]], pkt->len - 1); \ else \ - printf("RX[C] Mode-%s Cmd-0x%0.2x Len-%d\r\n", \ - ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], pkt->data[0], \ + printf("%cX[C] Mode-%s Cmd-0x%0.2x Len-%d\r\n", rxtx, \ + ESPModesStr[pkt->type & ESP_PACKET_TYPE_MSK], pkt->data[0], \ pkt->len - 1); #else -#define DEBUG_PACKET(packet) +#define DEBUG_PACKET(rxtx, packet) #endif #define LOG_UART "UART" #define UART_RX_BUFFER 1024 -#define REUSABLE_BUFFER 250 #define AT_CMD_MAX_LEN 40 #define BT_CMD_MAX_LEN 30 @@ -89,7 +90,6 @@ int64_t baudTimer = 0; int laddcnt = 0; char rmtaddress[13] = "000000000000"; -char reusablebuff[REUSABLE_BUFFER]; void sendBTMode() {} @@ -110,7 +110,7 @@ int atcommandlen = -1; circular_buffer uartinbuf; void processPacket(const packet_s *packet) { - DEBUG_PACKET(packet); + //DEBUG_PACKET('R', packet); // Acknowledge we got the command if (ESP_PACKET_ISCMD(packet->type) && !ESP_PACKET_ISACK(packet->type)) { @@ -204,13 +204,19 @@ void mainTask(void *stuff) { } } - // TEST SENDING PACKETS - /* static uint count=0; - if(count++ % 20 == 0) { - printf("Writting: Hello, Mode=2\r\n"); - writePacket("Hello",5,false,2); - writeCommand(1,3,"DATA",4); - }*/ + // Call all the functions to run + espTelemetryExec(); + espTrainerExec(); + espJoystickExec(); + espAudioExec(); + + // Test Sending Packets + static uint count = 0; + if (count++ % 20 == 0) { + //writeData(ESP_TRAINER, "HelloASDF", 9); + //uart_write_bytes(uart_num, "FEST", 4); + //writeCommand(ESP_TRAINER,ESP_TRAINERCMD_SET_MASTER,"DATA",4); + } } vTaskDelete(NULL); } @@ -359,11 +365,15 @@ void runBTPeripherial() // Builds a packet void writePacket(const uint8_t *dat, int len, bool iscmd, uint8_t mode) { + if(len > ESP_MAX_PACKET_DATA) {ESP_LOGE("TERM", "Packet too Big"); return;} uint8_t encodedbuffer[sizeof(packet_s) + 1]; packet_s packet; packet.type = mode; - if (iscmd) + packet.len = len; + if (iscmd) { packet.type |= (1 << ESP_PACKET_CMD_BIT); + packet.len -= 1; + } packet.crcl = 0xBB; packet.crch = 0xAA; memcpy(packet.data, dat, @@ -377,22 +387,35 @@ void writePacket(const uint8_t *dat, int len, bool iscmd, uint8_t mode) { encodedbuffer[wl] = '\0'; uart_write_bytes(uart_num, (void *)&encodedbuffer, wl + 1); - DEBUG_PACKET((&packet)); + //ESP_LOG_BUFFER_HEX("TX",encodedbuffer, wl+1); + //DEBUG_PACKET('T', (&packet)); } // Sends some data void writeData(uint8_t mode, const uint8_t *dat, int len) { + if(len > ESP_MAX_PACKET_DATA) return; writePacket(dat, len, false, mode); } // Send a command void writeCommand(uint8_t mode, uint8_t command, const uint8_t *dat, int len) { - uint8_t buffer[256]; + if(len > ESP_MAX_PACKET_DATA-1) return; + uint8_t buffer[ESP_MAX_PACKET_DATA-1]; buffer[0] = command; // First byte of a command is the command, data follows memcpy(buffer + 1, dat, len); writePacket(buffer, len + 1, true, mode); } +void writeEvent(uint8_t event, const uint8_t* dat, int len) +{ + if(len > ESP_MAX_PACKET_DATA-2) return; + uint8_t buffer[ESP_MAX_PACKET_DATA-1]; + buffer[0] = ESP_ROOTCMD_CON_EVENT; + buffer[1] = event; + memcpy(buffer + 2, dat, len); + writePacket(buffer, len + 2, true, ESP_ROOT); +} + // Writes an acknowledge / not-acknowledge and a optional message void writeAck() { writePacket(NULL, 0, true, ESP_ROOT | 1 << ESP_PACKET_ACK_BIT); diff --git a/src/terminal.h b/src/terminal.h index adadb45..60949e7 100644 --- a/src/terminal.h +++ b/src/terminal.h @@ -4,29 +4,17 @@ #include "espdefs.h" // Packet Format +#define PACKET_OVERHEAD 3 +#define ESP_MAX_PACKET_LEN 250 // COBS, without adding an extra byte +#define ESP_MAX_PACKET_DATA (ESP_MAX_PACKET_LEN - PACKET_OVERHEAD) typedef struct PACKED { uint8_t type; uint8_t crcl; // CRC16:low byte uint8_t crch; // CRC16:high byte - uint8_t data[257]; // User Data + uint8_t data[ESP_MAX_PACKET_DATA]; // User Data uint8_t len; // Data length, not transmitted } packet_s; -#define PACKET_OVERHEAD 3 - -/* Packet Type Format - * Bits 0:4 - Type(32 Possible ESPModes, Mode 0=Base Module) - * 5 - PartialPacket=1 - * 6 - Command=1/Data=0 - * 7 - Require Ack=1 - * If Command Bit(6) = 1 - * data[0] = Command - * data[1:255] = UserData - * If command bit(6) = 0 - * data[0:255] = DataStream - - */ - typedef uint8_t espmode; @@ -36,4 +24,5 @@ void logBTFrame(const char btdata[], int len); void writePacket(const uint8_t *dat, int len, bool iscmd, uint8_t mode); void writeData(uint8_t mode, const uint8_t *dat, int len); void writeCommand(uint8_t mode, uint8_t command, const uint8_t *dat, int len); +void writeEvent(uint8_t event, const uint8_t* dat, int len); void writeAck(); \ No newline at end of file From 27aa290e554e8c435da0196ee9e77cee2c02ebca Mon Sep 17 00:00:00 2001 From: dlktdr Date: Mon, 28 Nov 2022 23:34:21 -0800 Subject: [PATCH 17/17] Switch Most to C++ --- sdkconfig.C3Mini | 309 ++++++++++++++++------- sdkconfig.pico32_debug | 306 ++++++++++++++-------- src/{bt.c => bt.cpp} | 0 src/bt.h | 9 + src/bt_client/bt_client.c | 2 +- src/bt_client/bt_client.h | 11 +- src/bt_server/bt_server.h | 8 + src/{cobs.c => cobs.cpp} | 0 src/{crc.c => crc.cpp} | 0 src/{espaudio.c => espaudio.cpp} | 0 src/{espconmngr.c => espconmngr.cpp} | 0 src/{espjoystick.c => espjoystick.cpp} | 0 src/{esproot.c => esproot.cpp} | 1 + src/{esptelemetry.c => esptelemetry.cpp} | 2 +- src/esptelemetry.h | 2 +- src/{esptrainer.c => esptrainer.cpp} | 0 src/esptrainer.h | 11 +- src/{frskybt.c => frskybt.cpp} | 0 src/frskybt.h | 11 +- src/joystick/bt_joystick.c | 21 +- src/joystick/bt_joystick.h | 11 + src/{main.c => main.cpp} | 2 +- src/{settings.c => settings.cpp} | 0 src/settings.h | 10 +- src/{terminal.c => terminal.cpp} | 1 + 25 files changed, 499 insertions(+), 218 deletions(-) rename src/{bt.c => bt.cpp} (100%) rename src/{cobs.c => cobs.cpp} (100%) rename src/{crc.c => crc.cpp} (100%) rename src/{espaudio.c => espaudio.cpp} (100%) rename src/{espconmngr.c => espconmngr.cpp} (100%) rename src/{espjoystick.c => espjoystick.cpp} (100%) rename src/{esproot.c => esproot.cpp} (99%) rename src/{esptelemetry.c => esptelemetry.cpp} (97%) rename src/{esptrainer.c => esptrainer.cpp} (100%) rename src/{frskybt.c => frskybt.cpp} (100%) rename src/{main.c => main.cpp} (98%) rename src/{settings.c => settings.cpp} (100%) rename src/{terminal.c => terminal.cpp} (99%) diff --git a/sdkconfig.C3Mini b/sdkconfig.C3Mini index 5f5ebf0..60f7b8e 100644 --- a/sdkconfig.C3Mini +++ b/sdkconfig.C3Mini @@ -54,6 +54,7 @@ CONFIG_BOOTLOADER_LOG_LEVEL=3 CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y # CONFIG_BOOTLOADER_FACTORY_RESET is not set # CONFIG_BOOTLOADER_APP_TEST is not set +CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE=y CONFIG_BOOTLOADER_WDT_ENABLE=y # CONFIG_BOOTLOADER_WDT_DISABLE_IN_USER_CODE is not set CONFIG_BOOTLOADER_WDT_TIME_MS=9000 @@ -76,6 +77,15 @@ CONFIG_SECURE_TARGET_HAS_SECURE_ROM_DL_MODE=y # CONFIG_SECURE_FLASH_ENC_ENABLED is not set # end of Security features +# +# Boot ROM Behavior +# +CONFIG_BOOT_ROM_LOG_ALWAYS_ON=y +# CONFIG_BOOT_ROM_LOG_ALWAYS_OFF is not set +# CONFIG_BOOT_ROM_LOG_ON_GPIO_HIGH is not set +# CONFIG_BOOT_ROM_LOG_ON_GPIO_LOW is not set +# end of Boot ROM Behavior + # # Serial flasher config # @@ -85,6 +95,7 @@ CONFIG_ESPTOOLPY_BAUD_OTHER_VAL=115200 # CONFIG_ESPTOOLPY_FLASHMODE_QOUT is not set CONFIG_ESPTOOLPY_FLASHMODE_DIO=y # CONFIG_ESPTOOLPY_FLASHMODE_DOUT is not set +CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR=y CONFIG_ESPTOOLPY_FLASHMODE="dio" # CONFIG_ESPTOOLPY_FLASHFREQ_80M is not set CONFIG_ESPTOOLPY_FLASHFREQ_40M=y @@ -96,6 +107,9 @@ CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y # CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_32MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set CONFIG_ESPTOOLPY_FLASHSIZE="2MB" CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_BEFORE_RESET=y @@ -120,6 +134,7 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 # Partition Table # CONFIG_PARTITION_TABLE_SINGLE_APP=y +# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set # CONFIG_PARTITION_TABLE_TWO_OTA is not set # CONFIG_PARTITION_TABLE_CUSTOM is not set CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" @@ -138,6 +153,9 @@ CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y # CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set # CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE is not set +CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL=2 +# CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT is not set +CONFIG_COMPILER_HIDE_PATHS_MACROS=y # CONFIG_COMPILER_CXX_EXCEPTIONS is not set # CONFIG_COMPILER_CXX_RTTI is not set CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y @@ -145,6 +163,7 @@ CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y # CONFIG_COMPILER_STACK_CHECK_MODE_STRONG is not set # CONFIG_COMPILER_STACK_CHECK_MODE_ALL is not set # CONFIG_COMPILER_WARN_WRITE_STRINGS is not set +# CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS is not set # CONFIG_COMPILER_DISABLE_GCC8_WARNINGS is not set # CONFIG_COMPILER_DUMP_RTL_FILES is not set # end of Compiler options @@ -156,7 +175,7 @@ CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y # # Application Level Tracing # -# CONFIG_APPTRACE_DEST_TRAX is not set +# CONFIG_APPTRACE_DEST_JTAG is not set CONFIG_APPTRACE_DEST_NONE=y CONFIG_APPTRACE_LOCK_ENABLE=y # end of Application Level Tracing @@ -171,19 +190,10 @@ CONFIG_APPTRACE_LOCK_ENABLE=y # Bluetooth # CONFIG_BT_ENABLED=y -CONFIG_BT_CTRL_ESP32C3=y CONFIG_BT_SOC_SUPPORT_5_0=y -CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF=0 -CONFIG_BTDM_CTRL_PCM_ROLE_EFF=0 -CONFIG_BTDM_CTRL_PCM_POLAR_EFF=0 -CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF=0 -CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN_EFF=0 -CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF=0 -CONFIG_BTDM_CTRL_PINNED_TO_CORE=0 -CONFIG_BTDM_BLE_SLEEP_CLOCK_ACCURACY_INDEX_EFF=1 # -# Bluetooth controller(ESP32C3 Bluetooth Low Energy) +# Bluetooth controller # CONFIG_BT_CTRL_MODE_EFF=1 CONFIG_BT_CTRL_BLE_MAX_ACT=10 @@ -195,6 +205,7 @@ CONFIG_BT_CTRL_HCI_MODE_VHCI=y CONFIG_BT_CTRL_HCI_TL=1 CONFIG_BT_CTRL_ADV_DUP_FILT_MAX=30 # CONFIG_BT_CTRL_HW_CCA is not set +CONFIG_BT_CTRL_HW_CCA_VAL=20 CONFIG_BT_CTRL_HW_CCA_EFF=0 CONFIG_BT_CTRL_CE_LENGTH_TYPE_ORIG=y # CONFIG_BT_CTRL_CE_LENGTH_TYPE_CE is not set @@ -241,12 +252,7 @@ CONFIG_BT_CTRL_SLEEP_MODE_EFF=0 CONFIG_BT_CTRL_SLEEP_CLOCK_EFF=0 CONFIG_BT_CTRL_HCI_TL_EFF=1 # CONFIG_BT_CTRL_AGC_RECORRECT_EN is not set -# end of Bluetooth controller(ESP32C3 Bluetooth Low Energy) - -# -# MODEM SLEEP Options -# -# end of MODEM SLEEP Options +# end of Bluetooth controller CONFIG_BT_BLUEDROID_ENABLED=y # CONFIG_BT_NIMBLE_ENABLED is not set @@ -454,15 +460,10 @@ CONFIG_BT_MULTI_CONNECTION_ENBALE=y # CONFIG_BT_BLE_HOST_QUEUE_CONG_CHECK is not set CONFIG_BT_SMP_ENABLE=y CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT=30 -CONFIG_BT_MAX_DEVICE_NAME_LEN=32 CONFIG_BT_BLE_RPA_SUPPORTED=y CONFIG_BT_BLE_50_FEATURES_SUPPORTED=y CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y -CONFIG_BT_RESERVE_DRAM=0xdb5c # end of Bluedroid Options - -CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT=y -CONFIG_BT_NIMBLE_USE_ESP_TIMER=y # end of Bluetooth # CONFIG_BLE_MESH is not set @@ -487,6 +488,12 @@ CONFIG_COAP_LOG_DEFAULT_LEVEL=0 CONFIG_ADC_DISABLE_DAC=y # end of ADC configuration +# +# MCPWM configuration +# +# CONFIG_MCPWM_ISR_IN_IRAM is not set +# end of MCPWM configuration + # # SPI configuration # @@ -507,6 +514,13 @@ CONFIG_SPI_SLAVE_ISR_IN_IRAM=y # # CONFIG_UART_ISR_IN_IRAM is not set # end of UART configuration + +# +# GDMA Configuration +# +# CONFIG_GDMA_CTRL_FUNC_IN_IRAM is not set +# CONFIG_GDMA_ISR_IRAM_SAFE is not set +# end of GDMA Configuration # end of Driver configurations # @@ -522,6 +536,7 @@ CONFIG_EFUSE_MAX_BLK_LEN=256 # CONFIG_ESP_TLS_USING_MBEDTLS=y CONFIG_ESP_TLS_USE_DS_PERIPHERAL=y +# CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS is not set # CONFIG_ESP_TLS_SERVER is not set # CONFIG_ESP_TLS_PSK_VERIFICATION is not set # CONFIG_ESP_TLS_INSECURE is not set @@ -537,12 +552,9 @@ CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ=160 # CONFIG_ESP32C3_REV_MIN_1 is not set # CONFIG_ESP32C3_REV_MIN_2 is not set CONFIG_ESP32C3_REV_MIN_3=y +# CONFIG_ESP32C3_REV_MIN_4 is not set CONFIG_ESP32C3_REV_MIN=3 -# CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_TWO is not set -CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_FOUR=y -CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES=4 CONFIG_ESP32C3_DEBUG_OCDAWARE=y -# CONFIG_ESP32C3_DEBUG_STUBS_ENABLE is not set CONFIG_ESP32C3_BROWNOUT_DET=y CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_7=y # CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_6 is not set @@ -560,7 +572,6 @@ CONFIG_ESP32C3_RTC_CLK_SRC_INT_RC=y # CONFIG_ESP32C3_RTC_CLK_SRC_EXT_OSC is not set # CONFIG_ESP32C3_RTC_CLK_SRC_INT_8MD256 is not set CONFIG_ESP32C3_RTC_CLK_CAL_CYCLES=1024 -CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND=y # end of ESP32C3-Specific # @@ -572,29 +583,6 @@ CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND=y # Common ESP-related # CONFIG_ESP_ERR_TO_NAME_LOOKUP=y -CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 -CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 -CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584 -CONFIG_ESP_IPC_TASK_STACK_SIZE=1024 -CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048 -CONFIG_ESP_CONSOLE_UART_DEFAULT=y -# CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG is not set -# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set -# CONFIG_ESP_CONSOLE_NONE is not set -CONFIG_ESP_CONSOLE_UART=y -CONFIG_ESP_CONSOLE_UART_NUM=0 -CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200 -CONFIG_ESP_INT_WDT=y -CONFIG_ESP_INT_WDT_TIMEOUT_MS=300 -CONFIG_ESP_TASK_WDT=y -# CONFIG_ESP_TASK_WDT_PANIC is not set -CONFIG_ESP_TASK_WDT_TIMEOUT_S=5 -CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y -# CONFIG_ESP_PANIC_HANDLER_IRAM is not set -CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA=y -CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP=y -CONFIG_ESP_MAC_ADDR_UNIVERSE_BT=y -CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH=y # end of Common ESP-related # @@ -604,6 +592,7 @@ CONFIG_ETH_ENABLED=y CONFIG_ETH_USE_SPI_ETHERNET=y # CONFIG_ETH_SPI_ETHERNET_DM9051 is not set # CONFIG_ETH_SPI_ETHERNET_W5500 is not set +# CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL is not set # CONFIG_ETH_USE_OPENETH is not set # end of Ethernet @@ -625,6 +614,7 @@ CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=y # CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y # CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH is not set +CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH=y # end of ESP HTTP client # @@ -650,6 +640,54 @@ CONFIG_HTTPD_PURGE_BUF_LEN=32 # CONFIG_ESP_HTTPS_SERVER_ENABLE is not set # end of ESP HTTPS server +# +# Hardware Settings +# + +# +# MAC Config +# +CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_BT=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH=y +# CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_TWO is not set +CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_FOUR=y +CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES=4 +# end of MAC Config + +# +# Sleep Config +# +CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y +CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND=y +# CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND is not set +# end of Sleep Config + +# +# RTC Clock Config +# +CONFIG_RTC_CLOCK_BBPLL_POWER_ON_WITH_USB=y +# end of RTC Clock Config +# end of Hardware Settings + +# +# IPC (Inter-Processor Call) +# +CONFIG_ESP_IPC_TASK_STACK_SIZE=1024 +# end of IPC (Inter-Processor Call) + +# +# LCD and Touch Panel +# + +# +# LCD Peripheral Configuration +# +CONFIG_LCD_PANEL_IO_FORMAT_BUF_SIZE=32 +# end of LCD Peripheral Configuration +# end of LCD and Touch Panel + # # ESP NETIF Adapter # @@ -659,10 +697,21 @@ CONFIG_ESP_NETIF_TCPIP_LWIP=y CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y # end of ESP NETIF Adapter +# +# PHY +# +CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE=y +# CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION is not set +CONFIG_ESP_PHY_MAX_WIFI_TX_POWER=20 +CONFIG_ESP_PHY_MAX_TX_POWER=20 +CONFIG_ESP_PHY_ENABLE_USB=y +# end of PHY + # # Power Management # # CONFIG_PM_ENABLE is not set +CONFIG_PM_POWER_DOWN_CPU_IN_LIGHT_SLEEP=y # end of Power Management # @@ -672,19 +721,47 @@ CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y # CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT is not set # CONFIG_ESP_SYSTEM_PANIC_GDBSTUB is not set +# CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME is not set CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE=y CONFIG_ESP_SYSTEM_RTC_FAST_MEM_AS_HEAP_DEPCHECK=y CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP=y -CONFIG_ESP_SYSTEM_PD_FLASH=y -CONFIG_ESP_SYSTEM_PM_POWER_DOWN_CPU=y -# CONFIG_ESP_SYSTEM_FLASH_LEAKAGE_WORKAROUND is not set +# CONFIG_ESP_SYSTEM_USE_EH_FRAME is not set # # Memory protection # +CONFIG_ESP_SYSTEM_MEMPROT_DEPCHECK=y CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=y CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK=y +CONFIG_ESP_SYSTEM_MEMPROT_CPU_PREFETCH_PAD_SIZE=16 +CONFIG_ESP_SYSTEM_MEMPROT_MEM_ALIGN_SIZE=512 # end of Memory protection + +CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 +CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584 +CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y +# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set +CONFIG_ESP_MAIN_TASK_AFFINITY=0x0 +CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048 +CONFIG_ESP_CONSOLE_UART_DEFAULT=y +# CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG is not set +# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set +# CONFIG_ESP_CONSOLE_NONE is not set +# CONFIG_ESP_CONSOLE_SECONDARY_NONE is not set +CONFIG_ESP_CONSOLE_SECONDARY_USB_SERIAL_JTAG=y +CONFIG_ESP_CONSOLE_UART=y +CONFIG_ESP_CONSOLE_UART_NUM=0 +CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200 +CONFIG_ESP_INT_WDT=y +CONFIG_ESP_INT_WDT_TIMEOUT_MS=300 +CONFIG_ESP_TASK_WDT=y +# CONFIG_ESP_TASK_WDT_PANIC is not set +CONFIG_ESP_TASK_WDT_TIMEOUT_S=5 +CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y +# CONFIG_ESP_PANIC_HANDLER_IRAM is not set +# CONFIG_ESP_DEBUG_STUBS_ENABLE is not set +CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_4=y # end of ESP System Settings # @@ -694,12 +771,15 @@ CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK=y CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER=y CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER=y CONFIG_ESP_TIMER_TASK_STACK_SIZE=3584 +CONFIG_ESP_TIMER_INTERRUPT_LEVEL=1 +# CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD is not set CONFIG_ESP_TIMER_IMPL_SYSTIMER=y # end of High resolution timer (esp_timer) # # Wi-Fi # +CONFIG_ESP32_WIFI_ENABLED=y CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE=y CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=10 CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=32 @@ -715,29 +795,17 @@ CONFIG_ESP32_WIFI_RX_BA_WIN=6 CONFIG_ESP32_WIFI_NVS_ENABLED=y CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN=752 CONFIG_ESP32_WIFI_MGMT_SBUF_NUM=32 -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_NONE is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_ERROR is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_WARN is not set -CONFIG_WIFI_LOG_DEFAULT_LEVEL_INFO=y -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_DEBUG is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_VERBOSE is not set CONFIG_ESP32_WIFI_IRAM_OPT=y CONFIG_ESP32_WIFI_RX_IRAM_OPT=y CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y # CONFIG_ESP_WIFI_SLP_IRAM_OPT is not set # CONFIG_ESP_WIFI_FTM_ENABLE is not set # CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is not set +# CONFIG_ESP_WIFI_GCMP_SUPPORT is not set +# CONFIG_ESP_WIFI_GMAC_SUPPORT is not set +CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y # end of Wi-Fi -# -# PHY -# -CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y -# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set -CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20 -CONFIG_ESP32_PHY_MAX_TX_POWER=20 -# end of PHY - # # Core dump # @@ -818,8 +886,10 @@ CONFIG_FMB_MASTER_TIMER_INDEX=0 # CONFIG_FREERTOS_UNICORE=y CONFIG_FREERTOS_NO_AFFINITY=0x7FFFFFFF -CONFIG_FREERTOS_CORETIMER_0=y -# CONFIG_FREERTOS_CORETIMER_1 is not set +CONFIG_FREERTOS_TICK_SUPPORT_SYSTIMER=y +CONFIG_FREERTOS_CORETIMER_SYSTIMER_LVL1=y +# CONFIG_FREERTOS_CORETIMER_SYSTIMER_LVL3 is not set +CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER=y CONFIG_FREERTOS_OPTIMIZED_SCHEDULER=y CONFIG_FREERTOS_HZ=100 CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y @@ -849,8 +919,20 @@ CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER=y # CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE is not set # CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH is not set CONFIG_FREERTOS_DEBUG_OCDAWARE=y +CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT=y +# CONFIG_FREERTOS_PLACE_SNAPSHOT_FUNS_INTO_FLASH is not set # end of FreeRTOS +# +# Hardware Abstraction Layer (HAL) and Low Level (LL) +# +CONFIG_HAL_ASSERTION_EQUALS_SYSTEM=y +# CONFIG_HAL_ASSERTION_DISABLE is not set +# CONFIG_HAL_ASSERTION_SILIENT is not set +# CONFIG_HAL_ASSERTION_ENABLE is not set +CONFIG_HAL_DEFAULT_ASSERTION_LEVEL=2 +# end of Hardware Abstraction Layer (HAL) and Low Level (LL) + # # Heap memory debugging # @@ -885,6 +967,10 @@ CONFIG_LOG_DEFAULT_LEVEL_INFO=y # CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set # CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set CONFIG_LOG_DEFAULT_LEVEL=3 +CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y +# CONFIG_LOG_MAXIMUM_LEVEL_DEBUG is not set +# CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set +CONFIG_LOG_MAXIMUM_LEVEL=3 # CONFIG_LOG_COLORS is not set CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y # CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set @@ -894,6 +980,8 @@ CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y # LWIP # CONFIG_LWIP_LOCAL_HOSTNAME="espressif" +# CONFIG_LWIP_NETIF_API is not set +# CONFIG_LWIP_TCPIP_CORE_LOCKING is not set CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y # CONFIG_LWIP_L2_TO_L3_COPY is not set # CONFIG_LWIP_IRAM_OPTIMIZATION is not set @@ -917,7 +1005,9 @@ CONFIG_LWIP_GARP_TMR_INTERVAL=60 CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=32 CONFIG_LWIP_DHCP_DOES_ARP_CHECK=y # CONFIG_LWIP_DHCP_DISABLE_CLIENT_ID is not set +CONFIG_LWIP_DHCP_DISABLE_VENDOR_CLASS_ID=y # CONFIG_LWIP_DHCP_RESTORE_LAST_IP is not set +CONFIG_LWIP_DHCP_OPTIONS_LEN=68 # # DHCP server @@ -930,6 +1020,9 @@ CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8 # CONFIG_LWIP_AUTOIP is not set CONFIG_LWIP_IPV6=y # CONFIG_LWIP_IPV6_AUTOCONFIG is not set +CONFIG_LWIP_IPV6_NUM_ADDRESSES=3 +# CONFIG_LWIP_IPV6_FORWARD is not set +# CONFIG_LWIP_NETIF_STATUS_CALLBACK is not set CONFIG_LWIP_NETIF_LOOPBACK=y CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8 @@ -997,7 +1090,8 @@ CONFIG_LWIP_MAX_RAW_PCBS=16 # # SNTP # -CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1 +CONFIG_LWIP_SNTP_MAX_SERVERS=1 +# CONFIG_LWIP_DHCP_GET_NTP_SRV is not set CONFIG_LWIP_SNTP_UPDATE_DELAY=3600000 # end of SNTP @@ -1012,6 +1106,9 @@ CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT=y CONFIG_LWIP_HOOK_IP6_ROUTE_NONE=y # CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT is not set # CONFIG_LWIP_HOOK_IP6_ROUTE_CUSTOM is not set +CONFIG_LWIP_HOOK_ND6_GET_GW_NONE=y +# CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT is not set +# CONFIG_LWIP_HOOK_ND6_GET_GW_CUSTOM is not set CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_NONE=y # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_DEFAULT is not set # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_CUSTOM is not set @@ -1032,6 +1129,15 @@ CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 # CONFIG_MBEDTLS_DYNAMIC_BUFFER is not set # CONFIG_MBEDTLS_DEBUG is not set +# +# mbedTLS v2.28.x related +# +# CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH is not set +# CONFIG_MBEDTLS_X509_TRUSTED_CERT_CALLBACK is not set +# CONFIG_MBEDTLS_SSL_CONTEXT_SERIALIZATION is not set +CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=y +# end of mbedTLS v2.28.x related + # # Certificate Bundle # @@ -1040,11 +1146,13 @@ CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE is not set # CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE is not set +CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200 # end of Certificate Bundle # CONFIG_MBEDTLS_ECP_RESTARTABLE is not set # CONFIG_MBEDTLS_CMAC_C is not set CONFIG_MBEDTLS_HARDWARE_AES=y +CONFIG_MBEDTLS_AES_USE_INTERRUPT=y CONFIG_MBEDTLS_HARDWARE_MPI=y CONFIG_MBEDTLS_HARDWARE_SHA=y CONFIG_MBEDTLS_ROM_MD5=y @@ -1080,6 +1188,7 @@ CONFIG_MBEDTLS_SSL_RENEGOTIATION=y CONFIG_MBEDTLS_SSL_PROTO_TLS1=y CONFIG_MBEDTLS_SSL_PROTO_TLS1_1=y CONFIG_MBEDTLS_SSL_PROTO_TLS1_2=y +# CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1 is not set # CONFIG_MBEDTLS_SSL_PROTO_DTLS is not set CONFIG_MBEDTLS_SSL_ALPN=y CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS=y @@ -1151,6 +1260,8 @@ CONFIG_MDNS_TASK_AFFINITY=0x0 CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS=2000 # CONFIG_MDNS_STRICT_MODE is not set CONFIG_MDNS_TIMER_PERIOD_MS=100 +# CONFIG_MDNS_NETWORKING_SOCKET is not set +CONFIG_MDNS_MULTIPLE_INSTANCE=y # end of mDNS # @@ -1183,6 +1294,7 @@ CONFIG_NEWLIB_STDIN_LINE_ENDING_CR=y # # NVS # +# CONFIG_NVS_ASSERT_ERROR_CHECK is not set # end of NVS # @@ -1194,6 +1306,12 @@ CONFIG_OPENSSL_ERROR_STACK=y CONFIG_OPENSSL_ASSERT_EXIT=y # end of OpenSSL +# +# OpenThread +# +# CONFIG_OPENTHREAD_ENABLED is not set +# end of OpenThread + # # PThreads # @@ -1224,6 +1342,7 @@ CONFIG_SPI_FLASH_ERASE_YIELD_TICKS=1 CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE=8192 # CONFIG_SPI_FLASH_SIZE_OVERRIDE is not set # CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED is not set +# CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST is not set # # Auto-detect flash chips @@ -1233,6 +1352,7 @@ CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP=y CONFIG_SPI_FLASH_SUPPORT_GD_CHIP=y CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP=y CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP=y +CONFIG_SPI_FLASH_SUPPORT_TH_CHIP=y # end of Auto-detect flash chips CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE=y @@ -1286,16 +1406,12 @@ CONFIG_WS_BUFFER_SIZE=1024 # end of Websocket # end of TCP Transport -# -# TinyUSB -# -# end of TinyUSB - # # Unity unit testing library # CONFIG_UNITY_ENABLE_FLOAT=y CONFIG_UNITY_ENABLE_DOUBLE=y +# CONFIG_UNITY_ENABLE_64BIT is not set # CONFIG_UNITY_ENABLE_COLOR is not set CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y # CONFIG_UNITY_ENABLE_FIXTURE is not set @@ -1315,7 +1431,6 @@ CONFIG_VFS_SUPPORT_TERMIOS=y # Host File System I/O (Semihosting) # CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS=1 -CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN=128 # end of Host File System I/O (Semihosting) # end of Virtual file system @@ -1333,6 +1448,7 @@ CONFIG_WL_SECTOR_SIZE=4096 CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES=16 CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30 # CONFIG_WIFI_PROV_BLE_BONDING is not set +CONFIG_WIFI_PROV_BLE_FORCE_ENCRYPTION=y # end of Wi-Fi Provisioning Manager # @@ -1340,10 +1456,13 @@ CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30 # CONFIG_WPA_MBEDTLS_CRYPTO=y # CONFIG_WPA_WAPI_PSK is not set +# CONFIG_WPA_SUITE_B_192 is not set # CONFIG_WPA_DEBUG_PRINT is not set # CONFIG_WPA_TESTING_OPTIONS is not set # CONFIG_WPA_WPS_STRICT is not set # CONFIG_WPA_11KV_SUPPORT is not set +# CONFIG_WPA_MBO_SUPPORT is not set +# CONFIG_WPA_DPP_SUPPORT is not set # end of Supplicant # end of Component config @@ -1382,6 +1501,7 @@ CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y # CONFIG_OPTIMIZATION_ASSERTIONS_SILENT is not set # CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED is not set +CONFIG_OPTIMIZATION_ASSERTION_LEVEL=2 # CONFIG_CXX_EXCEPTIONS is not set CONFIG_STACK_CHECK_NONE=y # CONFIG_STACK_CHECK_NORM is not set @@ -1392,10 +1512,6 @@ CONFIG_STACK_CHECK_NONE=y # CONFIG_ESP32_APPTRACE_DEST_TRAX is not set CONFIG_ESP32_APPTRACE_DEST_NONE=y CONFIG_ESP32_APPTRACE_LOCK_ENABLE=y -CONFIG_BTDM_CONTROLLER_BLE_MAX_CONN_EFF=0 -CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_EFF=0 -CONFIG_BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_EFF=0 -CONFIG_BTDM_CONTROLLER_PINNED_TO_CORE=0 CONFIG_BLUEDROID_ENABLED=y # CONFIG_NIMBLE_ENABLED is not set CONFIG_BTC_TASK_STACK_SIZE=3072 @@ -1575,10 +1691,27 @@ CONFIG_BLUFI_INITIAL_TRACE_LEVEL=2 CONFIG_SMP_ENABLE=y CONFIG_BLE_ESTABLISH_LINK_CONNECTION_TIMEOUT=30 CONFIG_ADC2_DISABLE_DAC=y +# CONFIG_EVENT_LOOP_PROFILING is not set +CONFIG_POST_EVENTS_FROM_ISR=y +CONFIG_POST_EVENTS_FROM_IRAM_ISR=y +CONFIG_ESP_SYSTEM_PD_FLASH=y +CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND=y +CONFIG_IPC_TASK_STACK_SIZE=1024 +CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y +# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set +CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20 +CONFIG_ESP32_PHY_MAX_TX_POWER=20 +CONFIG_ESP_SYSTEM_PM_POWER_DOWN_CPU=y +# CONFIG_ESP32S2_PANIC_PRINT_HALT is not set +CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y +# CONFIG_ESP32S2_PANIC_SILENT_REBOOT is not set +# CONFIG_ESP32S2_PANIC_GDBSTUB is not set +CONFIG_ESP32S2_ALLOW_RTC_FAST_MEM_AS_HEAP=y +CONFIG_ESP32H2_MEMPROT_FEATURE=y +CONFIG_ESP32H2_MEMPROT_FEATURE_LOCK=y CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304 CONFIG_MAIN_TASK_STACK_SIZE=3584 -CONFIG_IPC_TASK_STACK_SIZE=1024 CONFIG_CONSOLE_UART_DEFAULT=y # CONFIG_CONSOLE_UART_CUSTOM is not set # CONFIG_ESP_CONSOLE_UART_NONE is not set @@ -1591,16 +1724,7 @@ CONFIG_TASK_WDT=y # CONFIG_TASK_WDT_PANIC is not set CONFIG_TASK_WDT_TIMEOUT_S=5 CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=y -# CONFIG_EVENT_LOOP_PROFILING is not set -CONFIG_POST_EVENTS_FROM_ISR=y -CONFIG_POST_EVENTS_FROM_IRAM_ISR=y -# CONFIG_ESP32S2_PANIC_PRINT_HALT is not set -CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y -# CONFIG_ESP32S2_PANIC_SILENT_REBOOT is not set -# CONFIG_ESP32S2_PANIC_GDBSTUB is not set -CONFIG_ESP32S2_ALLOW_RTC_FAST_MEM_AS_HEAP=y -CONFIG_ESP32C3_MEMPROT_FEATURE=y -CONFIG_ESP32C3_MEMPROT_FEATURE_LOCK=y +# CONFIG_ESP32_DEBUG_STUBS_ENABLE is not set CONFIG_TIMER_TASK_STACK_SIZE=3584 CONFIG_SW_COEXIST_ENABLE=y # CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set @@ -1659,5 +1783,4 @@ CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ABORTS=y CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y CONFIG_SUPPORT_TERMIOS=y CONFIG_SEMIHOSTFS_MAX_MOUNT_POINTS=1 -CONFIG_SEMIHOSTFS_HOST_PATH_MAX_LEN=128 # End of deprecated options diff --git a/sdkconfig.pico32_debug b/sdkconfig.pico32_debug index 519f076..1dcf255 100644 --- a/sdkconfig.pico32_debug +++ b/sdkconfig.pico32_debug @@ -54,6 +54,7 @@ CONFIG_BOOTLOADER_LOG_LEVEL=0 CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V=y # CONFIG_BOOTLOADER_FACTORY_RESET is not set # CONFIG_BOOTLOADER_APP_TEST is not set +CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE=y CONFIG_BOOTLOADER_WDT_ENABLE=y # CONFIG_BOOTLOADER_WDT_DISABLE_IN_USER_CODE is not set CONFIG_BOOTLOADER_WDT_TIME_MS=9000 @@ -83,6 +84,7 @@ CONFIG_ESPTOOLPY_BAUD_OTHER_VAL=115200 # CONFIG_ESPTOOLPY_FLASHMODE_QOUT is not set CONFIG_ESPTOOLPY_FLASHMODE_DIO=y # CONFIG_ESPTOOLPY_FLASHMODE_DOUT is not set +CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR=y CONFIG_ESPTOOLPY_FLASHMODE="dio" # CONFIG_ESPTOOLPY_FLASHFREQ_80M is not set CONFIG_ESPTOOLPY_FLASHFREQ_40M=y @@ -94,6 +96,9 @@ CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y # CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_32MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set +# CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set CONFIG_ESPTOOLPY_FLASHSIZE="2MB" CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_BEFORE_RESET=y @@ -118,6 +123,7 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 # Partition Table # # CONFIG_PARTITION_TABLE_SINGLE_APP is not set +# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set # CONFIG_PARTITION_TABLE_TWO_OTA is not set CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" @@ -136,6 +142,9 @@ CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y # CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT is not set # CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE is not set +CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL=2 +# CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT is not set +CONFIG_COMPILER_HIDE_PATHS_MACROS=y # CONFIG_COMPILER_CXX_EXCEPTIONS is not set # CONFIG_COMPILER_CXX_RTTI is not set CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y @@ -154,7 +163,7 @@ CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y # # Application Level Tracing # -# CONFIG_APPTRACE_DEST_TRAX is not set +# CONFIG_APPTRACE_DEST_JTAG is not set CONFIG_APPTRACE_DEST_NONE=y CONFIG_APPTRACE_LOCK_ENABLE=y # end of Application Level Tracing @@ -169,10 +178,9 @@ CONFIG_APPTRACE_LOCK_ENABLE=y # Bluetooth # CONFIG_BT_ENABLED=y -CONFIG_BT_CTRL_ESP32=y # -# Bluetooth controller(ESP32 Dual Mode Bluetooth) +# Bluetooth controller # CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y # CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY is not set @@ -206,37 +214,9 @@ CONFIG_BTDM_CTRL_FULL_SCAN_SUPPORTED=y CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y CONFIG_BTDM_BLE_ADV_REPORT_FLOW_CTRL_NUM=100 CONFIG_BTDM_BLE_ADV_REPORT_DISCARD_THRSHOLD=20 -# end of Bluetooth controller(ESP32 Dual Mode Bluetooth) - -CONFIG_BT_CTRL_MODE_EFF=1 -CONFIG_BT_CTRL_BLE_MAX_ACT=10 -CONFIG_BT_CTRL_BLE_MAX_ACT_EFF=10 -CONFIG_BT_CTRL_BLE_STATIC_ACL_TX_BUF_NB=0 -CONFIG_BT_CTRL_PINNED_TO_CORE=0 -CONFIG_BT_CTRL_HCI_TL=1 -CONFIG_BT_CTRL_ADV_DUP_FILT_MAX=30 -CONFIG_BT_CTRL_HW_CCA_EFF=0 -CONFIG_BT_CTRL_DFT_TX_POWER_LEVEL_EFF=0 -CONFIG_BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP=y -CONFIG_BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM=100 -CONFIG_BT_CTRL_BLE_ADV_REPORT_DISCARD_THRSHOLD=20 -CONFIG_BT_CTRL_BLE_SCAN_DUPL=y -CONFIG_BT_CTRL_SCAN_DUPL_TYPE=0 -CONFIG_BT_CTRL_SCAN_DUPL_CACHE_SIZE=100 - -# -# MODEM SLEEP Options -# -# end of MODEM SLEEP Options - -CONFIG_BT_CTRL_SLEEP_MODE_EFF=0 -CONFIG_BT_CTRL_SLEEP_CLOCK_EFF=0 -CONFIG_BT_CTRL_HCI_TL_EFF=1 - -# -# MODEM SLEEP Options -# -# end of MODEM SLEEP Options +CONFIG_BTDM_RESERVE_DRAM=0xdb5c +CONFIG_BTDM_CTRL_HLI=y +# end of Bluetooth controller CONFIG_BT_BLUEDROID_ENABLED=y # CONFIG_BT_NIMBLE_ENABLED is not set @@ -448,12 +428,8 @@ CONFIG_BT_MULTI_CONNECTION_ENBALE=y CONFIG_BT_SMP_ENABLE=y # CONFIG_BT_BLE_ACT_SCAN_REP_ADV_SCAN is not set CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT=30 -CONFIG_BT_MAX_DEVICE_NAME_LEN=32 # CONFIG_BT_BLE_RPA_SUPPORTED is not set -CONFIG_BT_RESERVE_DRAM=0xdb5c # end of Bluedroid Options - -CONFIG_BT_NIMBLE_USE_ESP_TIMER=y # end of Bluetooth # CONFIG_BLE_MESH is not set @@ -478,6 +454,12 @@ CONFIG_COAP_LOG_DEFAULT_LEVEL=0 CONFIG_ADC_DISABLE_DAC=y # end of ADC configuration +# +# MCPWM configuration +# +# CONFIG_MCPWM_ISR_IN_IRAM is not set +# end of MCPWM configuration + # # SPI configuration # @@ -514,6 +496,13 @@ CONFIG_SPI_SLAVE_ISR_IN_IRAM=y # # CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL is not set # end of GPIO Configuration + +# +# GDMA Configuration +# +# CONFIG_GDMA_CTRL_FUNC_IN_IRAM is not set +# CONFIG_GDMA_ISR_IRAM_SAFE is not set +# end of GDMA Configuration # end of Driver configurations # @@ -532,6 +521,7 @@ CONFIG_EFUSE_MAX_BLK_LEN=192 # CONFIG_ESP_TLS_USING_MBEDTLS=y # CONFIG_ESP_TLS_USE_SECURE_ELEMENT is not set +# CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS is not set # CONFIG_ESP_TLS_SERVER is not set # CONFIG_ESP_TLS_PSK_VERIFICATION is not set # CONFIG_ESP_TLS_INSECURE is not set @@ -553,9 +543,6 @@ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240 # CONFIG_ESP32_SPIRAM_SUPPORT is not set # CONFIG_ESP32_TRAX is not set CONFIG_ESP32_TRACEMEM_RESERVE_DRAM=0x0 -# CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_TWO is not set -CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR=y -CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES=4 # CONFIG_ESP32_ULP_COPROC_ENABLED is not set CONFIG_ESP32_ULP_COPROC_RESERVE_MEM=0 CONFIG_ESP32_DEBUG_OCDAWARE=y @@ -569,7 +556,6 @@ CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_0=y # CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_6 is not set # CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_7 is not set CONFIG_ESP32_BROWNOUT_DET_LVL=0 -CONFIG_ESP32_REDUCE_PHY_TX_POWER=y CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y # CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set # CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set @@ -603,32 +589,6 @@ CONFIG_ADC_CAL_LUT_ENABLE=y # Common ESP-related # CONFIG_ESP_ERR_TO_NAME_LOOKUP=y -CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 -CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 -CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584 -CONFIG_ESP_IPC_TASK_STACK_SIZE=1024 -CONFIG_ESP_IPC_USES_CALLERS_PRIORITY=y -CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048 -CONFIG_ESP_CONSOLE_UART_DEFAULT=y -# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set -# CONFIG_ESP_CONSOLE_NONE is not set -CONFIG_ESP_CONSOLE_UART=y -CONFIG_ESP_CONSOLE_MULTIPLE_UART=y -CONFIG_ESP_CONSOLE_UART_NUM=0 -CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200 -CONFIG_ESP_INT_WDT=y -CONFIG_ESP_INT_WDT_TIMEOUT_MS=300 -CONFIG_ESP_INT_WDT_CHECK_CPU1=y -CONFIG_ESP_TASK_WDT=y -# CONFIG_ESP_TASK_WDT_PANIC is not set -CONFIG_ESP_TASK_WDT_TIMEOUT_S=5 -CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y -CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=y -# CONFIG_ESP_PANIC_HANDLER_IRAM is not set -CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA=y -CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP=y -CONFIG_ESP_MAC_ADDR_UNIVERSE_BT=y -CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH=y # end of Common ESP-related # @@ -637,7 +597,6 @@ CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH=y CONFIG_ETH_ENABLED=y CONFIG_ETH_USE_ESP32_EMAC=y CONFIG_ETH_PHY_INTERFACE_RMII=y -# CONFIG_ETH_PHY_INTERFACE_MII is not set CONFIG_ETH_RMII_CLK_INPUT=y # CONFIG_ETH_RMII_CLK_OUTPUT is not set CONFIG_ETH_RMII_CLK_IN_GPIO=0 @@ -647,6 +606,7 @@ CONFIG_ETH_DMA_TX_BUFFER_NUM=10 CONFIG_ETH_USE_SPI_ETHERNET=y # CONFIG_ETH_SPI_ETHERNET_DM9051 is not set # CONFIG_ETH_SPI_ETHERNET_W5500 is not set +# CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL is not set # CONFIG_ETH_USE_OPENETH is not set # end of Ethernet @@ -668,6 +628,7 @@ CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=y # CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y # CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH is not set +CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH=y # end of ESP HTTP client # @@ -693,6 +654,56 @@ CONFIG_HTTPD_PURGE_BUF_LEN=32 # CONFIG_ESP_HTTPS_SERVER_ENABLE is not set # end of ESP HTTPS server +# +# Hardware Settings +# + +# +# MAC Config +# +CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_BT=y +CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH=y +# CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_TWO is not set +CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR=y +CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES=4 +# end of MAC Config + +# +# Sleep Config +# +CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y +CONFIG_ESP_SLEEP_RTC_BUS_ISO_WORKAROUND=y +# CONFIG_ESP_SLEEP_GPIO_RESET_WORKAROUND is not set +# CONFIG_ESP_SLEEP_FLASH_LEAKAGE_WORKAROUND is not set +# end of Sleep Config + +# +# RTC Clock Config +# +# end of RTC Clock Config +# end of Hardware Settings + +# +# IPC (Inter-Processor Call) +# +CONFIG_ESP_IPC_TASK_STACK_SIZE=1024 +CONFIG_ESP_IPC_USES_CALLERS_PRIORITY=y +CONFIG_ESP_IPC_ISR_ENABLE=y +# end of IPC (Inter-Processor Call) + +# +# LCD and Touch Panel +# + +# +# LCD Peripheral Configuration +# +CONFIG_LCD_PANEL_IO_FORMAT_BUF_SIZE=32 +# end of LCD Peripheral Configuration +# end of LCD and Touch Panel + # # ESP NETIF Adapter # @@ -702,6 +713,16 @@ CONFIG_ESP_NETIF_TCPIP_LWIP=y CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y # end of ESP NETIF Adapter +# +# PHY +# +CONFIG_ESP_PHY_CALIBRATION_AND_DATA_STORAGE=y +# CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION is not set +CONFIG_ESP_PHY_MAX_WIFI_TX_POWER=20 +CONFIG_ESP_PHY_MAX_TX_POWER=20 +CONFIG_ESP_PHY_REDUCE_TX_POWER=y +# end of PHY + # # Power Management # @@ -715,13 +736,39 @@ CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y # CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT is not set # CONFIG_ESP_SYSTEM_PANIC_GDBSTUB is not set -CONFIG_ESP_SYSTEM_PD_FLASH=y -# CONFIG_ESP_SYSTEM_FLASH_LEAKAGE_WORKAROUND is not set +# CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME is not set # # Memory protection # # end of Memory protection + +CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32 +CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584 +CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y +# CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set +# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set +CONFIG_ESP_MAIN_TASK_AFFINITY=0x0 +CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048 +CONFIG_ESP_CONSOLE_UART_DEFAULT=y +# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set +# CONFIG_ESP_CONSOLE_NONE is not set +CONFIG_ESP_CONSOLE_UART=y +CONFIG_ESP_CONSOLE_MULTIPLE_UART=y +CONFIG_ESP_CONSOLE_UART_NUM=0 +CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200 +CONFIG_ESP_INT_WDT=y +CONFIG_ESP_INT_WDT_TIMEOUT_MS=300 +CONFIG_ESP_INT_WDT_CHECK_CPU1=y +CONFIG_ESP_TASK_WDT=y +# CONFIG_ESP_TASK_WDT_PANIC is not set +CONFIG_ESP_TASK_WDT_TIMEOUT_S=5 +CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y +CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=y +# CONFIG_ESP_PANIC_HANDLER_IRAM is not set +# CONFIG_ESP_DEBUG_STUBS_ENABLE is not set +CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_5=y # end of ESP System Settings # @@ -731,6 +778,8 @@ CONFIG_ESP_SYSTEM_PD_FLASH=y CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER=y CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER=y CONFIG_ESP_TIMER_TASK_STACK_SIZE=3584 +CONFIG_ESP_TIMER_INTERRUPT_LEVEL=1 +# CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD is not set # CONFIG_ESP_TIMER_IMPL_FRC2 is not set CONFIG_ESP_TIMER_IMPL_TG0_LAC=y # end of High resolution timer (esp_timer) @@ -738,6 +787,7 @@ CONFIG_ESP_TIMER_IMPL_TG0_LAC=y # # Wi-Fi # +CONFIG_ESP32_WIFI_ENABLED=y CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE=y CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=10 CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=32 @@ -755,28 +805,15 @@ CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_0=y # CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1 is not set CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN=752 CONFIG_ESP32_WIFI_MGMT_SBUF_NUM=32 -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_NONE is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_ERROR is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_WARN is not set -CONFIG_WIFI_LOG_DEFAULT_LEVEL_INFO=y -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_DEBUG is not set -# CONFIG_WIFI_LOG_DEFAULT_LEVEL_VERBOSE is not set CONFIG_ESP32_WIFI_IRAM_OPT=y CONFIG_ESP32_WIFI_RX_IRAM_OPT=y CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE=y # CONFIG_ESP_WIFI_SLP_IRAM_OPT is not set # CONFIG_ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is not set +# CONFIG_ESP_WIFI_GMAC_SUPPORT is not set +CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y # end of Wi-Fi -# -# PHY -# -CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y -# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set -CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20 -CONFIG_ESP32_PHY_MAX_TX_POWER=20 -# end of PHY - # # Core dump # @@ -860,8 +897,10 @@ CONFIG_FMB_MASTER_TIMER_INDEX=0 # # CONFIG_FREERTOS_UNICORE is not set CONFIG_FREERTOS_NO_AFFINITY=0x7FFFFFFF +CONFIG_FREERTOS_TICK_SUPPORT_CORETIMER=y CONFIG_FREERTOS_CORETIMER_0=y # CONFIG_FREERTOS_CORETIMER_1 is not set +CONFIG_FREERTOS_SYSTICK_USES_CCOUNT=y CONFIG_FREERTOS_HZ=250 CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y # CONFIG_FREERTOS_CHECK_STACKOVERFLOW_NONE is not set @@ -891,8 +930,20 @@ CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER=y # CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH is not set CONFIG_FREERTOS_DEBUG_OCDAWARE=y # CONFIG_FREERTOS_FPU_IN_ISR is not set +CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT=y +# CONFIG_FREERTOS_PLACE_SNAPSHOT_FUNS_INTO_FLASH is not set # end of FreeRTOS +# +# Hardware Abstraction Layer (HAL) and Low Level (LL) +# +CONFIG_HAL_ASSERTION_EQUALS_SYSTEM=y +# CONFIG_HAL_ASSERTION_DISABLE is not set +# CONFIG_HAL_ASSERTION_SILIENT is not set +# CONFIG_HAL_ASSERTION_ENABLE is not set +CONFIG_HAL_DEFAULT_ASSERTION_LEVEL=2 +# end of Hardware Abstraction Layer (HAL) and Low Level (LL) + # # Heap memory debugging # @@ -927,6 +978,9 @@ CONFIG_HEAP_TRACING_OFF=y CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y # CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set CONFIG_LOG_DEFAULT_LEVEL=4 +CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y +# CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set +CONFIG_LOG_MAXIMUM_LEVEL=4 CONFIG_LOG_COLORS=y CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y # CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set @@ -936,6 +990,8 @@ CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y # LWIP # CONFIG_LWIP_LOCAL_HOSTNAME="espressif" +# CONFIG_LWIP_NETIF_API is not set +# CONFIG_LWIP_TCPIP_CORE_LOCKING is not set CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y # CONFIG_LWIP_L2_TO_L3_COPY is not set # CONFIG_LWIP_IRAM_OPTIMIZATION is not set @@ -959,7 +1015,9 @@ CONFIG_LWIP_GARP_TMR_INTERVAL=60 CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=32 CONFIG_LWIP_DHCP_DOES_ARP_CHECK=y # CONFIG_LWIP_DHCP_DISABLE_CLIENT_ID is not set +CONFIG_LWIP_DHCP_DISABLE_VENDOR_CLASS_ID=y # CONFIG_LWIP_DHCP_RESTORE_LAST_IP is not set +CONFIG_LWIP_DHCP_OPTIONS_LEN=68 # # DHCP server @@ -972,6 +1030,9 @@ CONFIG_LWIP_DHCPS_MAX_STATION_NUM=8 # CONFIG_LWIP_AUTOIP is not set CONFIG_LWIP_IPV6=y # CONFIG_LWIP_IPV6_AUTOCONFIG is not set +CONFIG_LWIP_IPV6_NUM_ADDRESSES=3 +# CONFIG_LWIP_IPV6_FORWARD is not set +# CONFIG_LWIP_NETIF_STATUS_CALLBACK is not set CONFIG_LWIP_NETIF_LOOPBACK=y CONFIG_LWIP_LOOPBACK_MAX_PBUFS=8 @@ -1040,7 +1101,8 @@ CONFIG_LWIP_MAX_RAW_PCBS=16 # # SNTP # -CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1 +CONFIG_LWIP_SNTP_MAX_SERVERS=1 +# CONFIG_LWIP_DHCP_GET_NTP_SRV is not set CONFIG_LWIP_SNTP_UPDATE_DELAY=3600000 # end of SNTP @@ -1055,6 +1117,9 @@ CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT=y CONFIG_LWIP_HOOK_IP6_ROUTE_NONE=y # CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT is not set # CONFIG_LWIP_HOOK_IP6_ROUTE_CUSTOM is not set +CONFIG_LWIP_HOOK_ND6_GET_GW_NONE=y +# CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT is not set +# CONFIG_LWIP_HOOK_ND6_GET_GW_CUSTOM is not set CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_NONE=y # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_DEFAULT is not set # CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_CUSTOM is not set @@ -1075,6 +1140,15 @@ CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 # CONFIG_MBEDTLS_DYNAMIC_BUFFER is not set # CONFIG_MBEDTLS_DEBUG is not set +# +# mbedTLS v2.28.x related +# +# CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH is not set +# CONFIG_MBEDTLS_X509_TRUSTED_CERT_CALLBACK is not set +# CONFIG_MBEDTLS_SSL_CONTEXT_SERIALIZATION is not set +CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE=y +# end of mbedTLS v2.28.x related + # # Certificate Bundle # @@ -1083,6 +1157,7 @@ CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE is not set # CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE is not set +CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200 # end of Certificate Bundle # CONFIG_MBEDTLS_ECP_RESTARTABLE is not set @@ -1123,6 +1198,7 @@ CONFIG_MBEDTLS_SSL_RENEGOTIATION=y CONFIG_MBEDTLS_SSL_PROTO_TLS1=y CONFIG_MBEDTLS_SSL_PROTO_TLS1_1=y CONFIG_MBEDTLS_SSL_PROTO_TLS1_2=y +# CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1 is not set # CONFIG_MBEDTLS_SSL_PROTO_DTLS is not set CONFIG_MBEDTLS_SSL_ALPN=y CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS=y @@ -1195,6 +1271,8 @@ CONFIG_MDNS_TASK_AFFINITY=0x0 CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS=2000 # CONFIG_MDNS_STRICT_MODE is not set CONFIG_MDNS_TIMER_PERIOD_MS=100 +# CONFIG_MDNS_NETWORKING_SOCKET is not set +CONFIG_MDNS_MULTIPLE_INSTANCE=y # end of mDNS # @@ -1227,6 +1305,7 @@ CONFIG_NEWLIB_STDIN_LINE_ENDING_CR=y # # NVS # +# CONFIG_NVS_ASSERT_ERROR_CHECK is not set # end of NVS # @@ -1238,6 +1317,12 @@ CONFIG_OPENSSL_ERROR_STACK=y CONFIG_OPENSSL_ASSERT_EXIT=y # end of OpenSSL +# +# OpenThread +# +# CONFIG_OPENTHREAD_ENABLED is not set +# end of OpenThread + # # PThreads # @@ -1269,6 +1354,7 @@ CONFIG_SPI_FLASH_ERASE_YIELD_TICKS=1 CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE=8192 # CONFIG_SPI_FLASH_SIZE_OVERRIDE is not set # CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED is not set +# CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST is not set # # Auto-detect flash chips @@ -1277,6 +1363,8 @@ CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP=y CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP=y CONFIG_SPI_FLASH_SUPPORT_GD_CHIP=y CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP=y +# CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP is not set +# CONFIG_SPI_FLASH_SUPPORT_TH_CHIP is not set # end of Auto-detect flash chips CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE=y @@ -1330,16 +1418,12 @@ CONFIG_WS_BUFFER_SIZE=1024 # end of Websocket # end of TCP Transport -# -# TinyUSB -# -# end of TinyUSB - # # Unity unit testing library # CONFIG_UNITY_ENABLE_FLOAT=y CONFIG_UNITY_ENABLE_DOUBLE=y +# CONFIG_UNITY_ENABLE_64BIT is not set # CONFIG_UNITY_ENABLE_COLOR is not set CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=y # CONFIG_UNITY_ENABLE_FIXTURE is not set @@ -1359,7 +1443,6 @@ CONFIG_VFS_SUPPORT_TERMIOS=y # Host File System I/O (Semihosting) # CONFIG_VFS_SEMIHOSTFS_MAX_MOUNT_POINTS=1 -CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN=128 # end of Host File System I/O (Semihosting) # end of Virtual file system @@ -1377,6 +1460,7 @@ CONFIG_WL_SECTOR_SIZE=4096 CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES=16 CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30 # CONFIG_WIFI_PROV_BLE_BONDING is not set +CONFIG_WIFI_PROV_BLE_FORCE_ENCRYPTION=y # end of Wi-Fi Provisioning Manager # @@ -1384,10 +1468,13 @@ CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30 # CONFIG_WPA_MBEDTLS_CRYPTO=y # CONFIG_WPA_WAPI_PSK is not set +# CONFIG_WPA_SUITE_B_192 is not set # CONFIG_WPA_DEBUG_PRINT is not set # CONFIG_WPA_TESTING_OPTIONS is not set # CONFIG_WPA_WPS_STRICT is not set # CONFIG_WPA_11KV_SUPPORT is not set +# CONFIG_WPA_MBO_SUPPORT is not set +# CONFIG_WPA_DPP_SUPPORT is not set # end of Supplicant # end of Component config @@ -1426,6 +1513,7 @@ CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y # CONFIG_OPTIMIZATION_ASSERTIONS_SILENT is not set # CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED is not set +CONFIG_OPTIMIZATION_ASSERTION_LEVEL=2 # CONFIG_CXX_EXCEPTIONS is not set CONFIG_STACK_CHECK_NONE=y # CONFIG_STACK_CHECK_NORM is not set @@ -1637,9 +1725,6 @@ CONFIG_BLE_ESTABLISH_LINK_CONNECTION_TIMEOUT=30 CONFIG_ADC2_DISABLE_DAC=y # CONFIG_SPIRAM_SUPPORT is not set CONFIG_TRACEMEM_RESERVE_DRAM=0x0 -# CONFIG_TWO_UNIVERSAL_MAC_ADDRESS is not set -CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS=y -CONFIG_NUMBER_OF_UNIVERSAL_MAC_ADDRESS=4 # CONFIG_ULP_COPROC_ENABLED is not set CONFIG_ULP_COPROC_RESERVE_MEM=0 CONFIG_BROWNOUT_DET=y @@ -1652,17 +1737,33 @@ CONFIG_BROWNOUT_DET_LVL_SEL_0=y # CONFIG_BROWNOUT_DET_LVL_SEL_6 is not set # CONFIG_BROWNOUT_DET_LVL_SEL_7 is not set CONFIG_BROWNOUT_DET_LVL=0 -CONFIG_REDUCE_PHY_TX_POWER=y CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC=y # CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL is not set # CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_OSC is not set # CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_8MD256 is not set # CONFIG_DISABLE_BASIC_ROM_CONSOLE is not set # CONFIG_COMPATIBLE_PRE_V2_1_BOOTLOADERS is not set +# CONFIG_EVENT_LOOP_PROFILING is not set +CONFIG_POST_EVENTS_FROM_ISR=y +CONFIG_POST_EVENTS_FROM_IRAM_ISR=y +# CONFIG_TWO_UNIVERSAL_MAC_ADDRESS is not set +CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS=y +CONFIG_NUMBER_OF_UNIVERSAL_MAC_ADDRESS=4 +CONFIG_ESP_SYSTEM_PD_FLASH=y +# CONFIG_ESP32C3_LIGHTSLEEP_GPIO_RESET_WORKAROUND is not set +CONFIG_IPC_TASK_STACK_SIZE=1024 +CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y +# CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION is not set +CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20 +CONFIG_ESP32_PHY_MAX_TX_POWER=20 +CONFIG_ESP32_REDUCE_PHY_TX_POWER=y +# CONFIG_ESP32S2_PANIC_PRINT_HALT is not set +CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y +# CONFIG_ESP32S2_PANIC_SILENT_REBOOT is not set +# CONFIG_ESP32S2_PANIC_GDBSTUB is not set CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304 CONFIG_MAIN_TASK_STACK_SIZE=3584 -CONFIG_IPC_TASK_STACK_SIZE=1024 CONFIG_CONSOLE_UART_DEFAULT=y # CONFIG_CONSOLE_UART_CUSTOM is not set # CONFIG_ESP_CONSOLE_UART_NONE is not set @@ -1677,13 +1778,7 @@ CONFIG_TASK_WDT=y CONFIG_TASK_WDT_TIMEOUT_S=5 CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=y CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1=y -# CONFIG_EVENT_LOOP_PROFILING is not set -CONFIG_POST_EVENTS_FROM_ISR=y -CONFIG_POST_EVENTS_FROM_IRAM_ISR=y -# CONFIG_ESP32S2_PANIC_PRINT_HALT is not set -CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y -# CONFIG_ESP32S2_PANIC_SILENT_REBOOT is not set -# CONFIG_ESP32S2_PANIC_GDBSTUB is not set +# CONFIG_ESP32_DEBUG_STUBS_ENABLE is not set CONFIG_TIMER_TASK_STACK_SIZE=3584 CONFIG_SW_COEXIST_ENABLE=y # CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set @@ -1746,5 +1841,4 @@ CONFIG_SPI_FLASH_WRITING_DANGEROUS_REGIONS_ABORTS=y CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT=y CONFIG_SUPPORT_TERMIOS=y CONFIG_SEMIHOSTFS_MAX_MOUNT_POINTS=1 -CONFIG_SEMIHOSTFS_HOST_PATH_MAX_LEN=128 # End of deprecated options diff --git a/src/bt.c b/src/bt.cpp similarity index 100% rename from src/bt.c rename to src/bt.cpp diff --git a/src/bt.h b/src/bt.h index ff86197..ddd020f 100644 --- a/src/bt.h +++ b/src/bt.h @@ -8,8 +8,17 @@ extern esp_bd_addr_t rmtbtaddress; extern esp_bd_addr_t localbtaddress; extern char btname[]; +#ifdef __cplusplus +extern "C" { +#endif + void strtobtaddr(esp_bd_addr_t dest, char *src); char *btaddrtostr(char dest[13], esp_bd_addr_t src); void bt_disable(); void BTInit(); void btSetName(const char *name); + +#ifdef __cplusplus +} +#endif + diff --git a/src/bt_client/bt_client.c b/src/bt_client/bt_client.c index 30cef85..8c243e8 100644 --- a/src/bt_client/bt_client.c +++ b/src/bt_client/bt_client.c @@ -34,7 +34,7 @@ #include "settings.h" #include "bt.h" -#include "bt_client.h" +#include "bt_client/bt_client.h" #include "frskybt.h" #include "terminal.h" #include "defines.h" diff --git a/src/bt_client/bt_client.h b/src/bt_client/bt_client.h index 00f6157..0daa86a 100644 --- a/src/bt_client/bt_client.h +++ b/src/bt_client/bt_client.h @@ -32,13 +32,22 @@ extern char *str_ble_board_types[BLE_BOARD_COUNT]; extern uint8_t bt_scanned_address_cnt; extern esp_bd_addr_t btc_scanned_addresses[MAX_BLE_ADDRESSES]; -void btc_connect(esp_bd_addr_t addr); extern volatile bool btc_connected; extern volatile bool btc_scan_complete; extern volatile bool btc_validslavefound; extern volatile bool btc_ht_reset; extern volatile ble_board_type btc_board_type; + +#ifdef __cplusplus +extern "C" { +#endif + +void btc_connect(esp_bd_addr_t addr); void btcInit(); void btc_disconnect(); void btc_start_scan(); void btc_dohtreset(); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/src/bt_server/bt_server.h b/src/bt_server/bt_server.h index 3a53370..efec18a 100644 --- a/src/bt_server/bt_server.h +++ b/src/bt_server/bt_server.h @@ -12,8 +12,16 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + void btpInit(); void btp_disconnect(); int btp_sendChannelData(uint8_t *data, int len); +#ifdef __cplusplus +} +#endif + extern volatile bool btp_connected; \ No newline at end of file diff --git a/src/cobs.c b/src/cobs.cpp similarity index 100% rename from src/cobs.c rename to src/cobs.cpp diff --git a/src/crc.c b/src/crc.cpp similarity index 100% rename from src/crc.c rename to src/crc.cpp diff --git a/src/espaudio.c b/src/espaudio.cpp similarity index 100% rename from src/espaudio.c rename to src/espaudio.cpp diff --git a/src/espconmngr.c b/src/espconmngr.cpp similarity index 100% rename from src/espconmngr.c rename to src/espconmngr.cpp diff --git a/src/espjoystick.c b/src/espjoystick.cpp similarity index 100% rename from src/espjoystick.c rename to src/espjoystick.cpp diff --git a/src/esproot.c b/src/esproot.cpp similarity index 99% rename from src/esproot.c rename to src/esproot.cpp index 3e236e9..dd86aaf 100644 --- a/src/esproot.c +++ b/src/esproot.cpp @@ -4,6 +4,7 @@ #include "espdefs.h" #include "espjoystick.h" #include "esptrainer.h" +#include "esptelemetry.h" #include "terminal.h" #include #include diff --git a/src/esptelemetry.c b/src/esptelemetry.cpp similarity index 97% rename from src/esptelemetry.c rename to src/esptelemetry.cpp index 6e2c5b2..9bbaf06 100644 --- a/src/esptelemetry.c +++ b/src/esptelemetry.cpp @@ -29,7 +29,7 @@ void espTelemetryStop() { bool espTelemetryRunning() { return telemetrystarted; } -bool espTelemetryExec() +void espTelemetryExec() { } diff --git a/src/esptelemetry.h b/src/esptelemetry.h index 6d3ac73..1b23dd5 100644 --- a/src/esptelemetry.h +++ b/src/esptelemetry.h @@ -4,7 +4,7 @@ int espTelemetryStart(); void espTelemetryStop(); -bool espTelemetryExec(); +void espTelemetryExec(); bool espTelemetryRunning(); void espTelemetryData(const uint8_t *data, uint8_t len); void espTelemetryCommand(uint8_t command, const uint8_t *data, uint8_t len); diff --git a/src/esptrainer.c b/src/esptrainer.cpp similarity index 100% rename from src/esptrainer.c rename to src/esptrainer.cpp diff --git a/src/esptrainer.h b/src/esptrainer.h index b657404..6d2b7ff 100644 --- a/src/esptrainer.h +++ b/src/esptrainer.h @@ -2,6 +2,10 @@ #include "espdefs.h" +#ifdef __cplusplus +extern "C" { +#endif + // Main functions to radio int espTrainerStart(); void espTrainerStop(); @@ -16,4 +20,9 @@ void espTrainerDiscoverStop(); // Internal Functions, Bluetooth void espTrainerDeviceDiscovered(const char *address); void espTrainerRFDataReceived(const channeldata *chans); -void espTrainerDiscoverComplete(); \ No newline at end of file +void espTrainerDiscoverComplete(); + +#ifdef __cplusplus +} +#endif + diff --git a/src/frskybt.c b/src/frskybt.cpp similarity index 100% rename from src/frskybt.c rename to src/frskybt.cpp diff --git a/src/frskybt.h b/src/frskybt.h index 0efbec1..7b1d805 100644 --- a/src/frskybt.h +++ b/src/frskybt.h @@ -15,6 +15,15 @@ enum { BT_SUCCESS=0, }; +#ifdef __cplusplus +extern "C" { +#endif + int setTrainer(uint8_t *addr, uint16_t chan_vals[BT_CHANNELS]); void frSkyProcessByte(uint8_t data); -void processFrame(const uint8_t *frame, uint8_t len); \ No newline at end of file +void processFrame(const uint8_t *frame, uint8_t len); + +#ifdef __cplusplus +} +#endif + diff --git a/src/joystick/bt_joystick.c b/src/joystick/bt_joystick.c index bc26472..16890a4 100644 --- a/src/joystick/bt_joystick.c +++ b/src/joystick/bt_joystick.c @@ -47,11 +47,10 @@ * please ignore. */ -#define HID_DEMO_TAG "HID_DEMO" +#define HID_JOYSTICK "HID_JOYSTICK" uint16_t btj_conn_id = 0; static bool sec_conn = false; -static bool send_volum_up = false; volatile bool btjoystickconnected = false; #define CHAR_DECLARATION_SIZE (sizeof(uint8_t)) @@ -114,7 +113,7 @@ static void hidd_event_callback(esp_hidd_cb_event_t event, case ESP_HIDD_EVENT_DEINIT_FINISH: break; case ESP_HIDD_EVENT_BLE_CONNECT: { - ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_CONNECT"); + ESP_LOGI(HID_JOYSTICK, "ESP_HIDD_EVENT_BLE_CONNECT"); btjoystickconnected = true; btj_conn_id = param->connect.conn_id; break; @@ -122,14 +121,14 @@ static void hidd_event_callback(esp_hidd_cb_event_t event, case ESP_HIDD_EVENT_BLE_DISCONNECT: { sec_conn = false; btjoystickconnected = false; - ESP_LOGI(HID_DEMO_TAG, "ESP_HIDD_EVENT_BLE_DISCONNECT"); + ESP_LOGI(HID_JOYSTICK, "ESP_HIDD_EVENT_BLE_DISCONNECT"); esp_ble_gap_start_advertising(&hidd_adv_params); break; } case ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT: { - ESP_LOGI(HID_DEMO_TAG, "%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", + ESP_LOGI(HID_JOYSTICK, "%s, ESP_HIDD_EVENT_BLE_VENDOR_REPORT_WRITE_EVT", __func__); - ESP_LOG_BUFFER_HEX(HID_DEMO_TAG, param->vendor_write.data, + ESP_LOG_BUFFER_HEX(HID_JOYSTICK, param->vendor_write.data, param->vendor_write.length); } default: @@ -146,7 +145,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, break; case ESP_GAP_BLE_SEC_REQ_EVT: for (int i = 0; i < ESP_BD_ADDR_LEN; i++) { - ESP_LOGD(HID_DEMO_TAG, "%x:", param->ble_security.ble_req.bd_addr[i]); + ESP_LOGD(HID_JOYSTICK, "%x:", param->ble_security.ble_req.bd_addr[i]); } esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true); break; @@ -155,16 +154,16 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_bd_addr_t bd_addr; memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t)); - ESP_LOGI(HID_DEMO_TAG, "remote BD_ADDR: %08x%04x", + ESP_LOGI(HID_JOYSTICK, "remote BD_ADDR: %08x%04x", (bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3], (bd_addr[4] << 8) + bd_addr[5]); - ESP_LOGI(HID_DEMO_TAG, "address type = %d", + ESP_LOGI(HID_JOYSTICK, "address type = %d", param->ble_security.auth_cmpl.addr_type); - ESP_LOGI(HID_DEMO_TAG, "pair status = %s", + ESP_LOGI(HID_JOYSTICK, "pair status = %s", param->ble_security.auth_cmpl.success ? "success" : "fail"); if (!param->ble_security.auth_cmpl.success) { - ESP_LOGE(HID_DEMO_TAG, "fail reason = 0x%x", + ESP_LOGE(HID_JOYSTICK, "fail reason = 0x%x", param->ble_security.auth_cmpl.fail_reason); } break; diff --git a/src/joystick/bt_joystick.h b/src/joystick/bt_joystick.h index 13f56c6..d3b91bb 100644 --- a/src/joystick/bt_joystick.h +++ b/src/joystick/bt_joystick.h @@ -2,4 +2,15 @@ extern volatile bool btjoystickconnected; extern uint16_t btj_conn_id; + +#ifdef __cplusplus +extern "C" { +#endif + void BTJoyInit(); + +#ifdef __cplusplus +} +#endif + + diff --git a/src/main.c b/src/main.cpp similarity index 98% rename from src/main.c rename to src/main.cpp index aecf0c0..395cdbf 100644 --- a/src/main.c +++ b/src/main.cpp @@ -44,7 +44,7 @@ void runBlinky() { } #endif -void app_main(void) +extern "C" void app_main(void) { TaskHandle_t tUartHnd = NULL; xTaskCreate(mainTask, "UART", 65535, NULL, tskIDLE_PRIORITY+1, &tUartHnd); diff --git a/src/settings.c b/src/settings.cpp similarity index 100% rename from src/settings.c rename to src/settings.cpp diff --git a/src/settings.h b/src/settings.h index 4582625..848b78f 100644 --- a/src/settings.h +++ b/src/settings.h @@ -23,5 +23,13 @@ typedef struct { extern settings_t settings; extern volatile bool settings_ok; +#ifdef __cplusplus +extern "C" { +#endif + void loadSettings(); -void saveSettings(); \ No newline at end of file +void saveSettings(); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/src/terminal.c b/src/terminal.cpp similarity index 99% rename from src/terminal.c rename to src/terminal.cpp index 6f728fd..0d6a630 100644 --- a/src/terminal.c +++ b/src/terminal.cpp @@ -101,6 +101,7 @@ uart_config_t uart_config = { .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .rx_flow_ctrl_thresh = 0, .source_clk = UART_SCLK_APB, };