diff --git a/components/esp_hid/include/esp_private/esp_hidd_private.h b/components/esp_hid/include/esp_private/esp_hidd_private.h index bac88d1e16..fccf4e22ee 100644 --- a/components/esp_hid/include/esp_private/esp_hidd_private.h +++ b/components/esp_hid/include/esp_private/esp_hidd_private.h @@ -41,15 +41,15 @@ struct esp_hidd_dev_s { void *dev; esp_hid_transport_t transport; - bool (*connected) (void *dev); - esp_err_t (*deinit) (void *dev); - esp_err_t (*disconnect) (void *dev); - esp_err_t (*virtual_unplug) (void *dev); - esp_err_t (*battery_set) (void *dev, uint8_t level); - esp_err_t (*input_set) (void *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length); - esp_err_t (*feature_set) (void *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length); - esp_err_t (*event_handler_register) (void *dev, esp_event_handler_t callback, esp_hidd_event_t event); - esp_err_t (*event_handler_unregister) (void *dev, esp_event_handler_t callback, esp_hidd_event_t event); + bool (*connected)(void *dev); + esp_err_t (*deinit)(void *dev); + esp_err_t (*disconnect)(void *dev); + esp_err_t (*virtual_unplug)(void *dev); + esp_err_t (*battery_set)(void *dev, uint8_t level); + esp_err_t (*input_set)(void *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length); + esp_err_t (*feature_set)(void *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length); + esp_err_t (*event_handler_register)(void *dev, esp_event_handler_t callback, esp_hidd_event_t event); + esp_err_t (*event_handler_unregister)(void *dev, esp_event_handler_t callback, esp_hidd_event_t event); }; typedef struct esp_hidd_dev_s esp_hidd_dev_t; diff --git a/components/esp_hid/include/esp_private/esp_hidh_private.h b/components/esp_hid/include/esp_private/esp_hidh_private.h index 41fad3dce7..086000ec9f 100644 --- a/components/esp_hid/include/esp_private/esp_hidh_private.h +++ b/components/esp_hid/include/esp_private/esp_hidh_private.h @@ -69,15 +69,15 @@ struct esp_hidh_dev_s { SemaphoreHandle_t semaphore; SemaphoreHandle_t mutex; - esp_err_t (*close) (esp_hidh_dev_t *dev); - esp_err_t (*report_write) (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t len); - esp_err_t (*report_read) (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, size_t max_length, uint8_t *value, size_t *value_len); - esp_err_t (*set_report) (esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t len); - esp_err_t (*get_idle) (esp_hidh_dev_t *dev); - esp_err_t (*set_idle) (esp_hidh_dev_t *dev, uint8_t idle_time); - esp_err_t (*get_protocol) (esp_hidh_dev_t *dev); - esp_err_t (*set_protocol) (esp_hidh_dev_t *dev, uint8_t protocol_mode); - void (*dump) (esp_hidh_dev_t *dev, FILE *fp); + esp_err_t (*close)(esp_hidh_dev_t *dev); + esp_err_t (*report_write)(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t len); + esp_err_t (*report_read)(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, size_t max_length, uint8_t *value, size_t *value_len); + esp_err_t (*set_report)(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type, uint8_t *data, size_t len); + esp_err_t (*get_idle)(esp_hidh_dev_t *dev); + esp_err_t (*set_idle)(esp_hidh_dev_t *dev, uint8_t idle_time); + esp_err_t (*get_protocol)(esp_hidh_dev_t *dev); + esp_err_t (*set_protocol)(esp_hidh_dev_t *dev, uint8_t protocol_mode); + void (*dump)(esp_hidh_dev_t *dev, FILE *fp); union { #if CONFIG_BT_HID_HOST_ENABLED diff --git a/components/esp_hid/src/ble_hidd.c b/components/esp_hid/src/ble_hidd.c index 743b6c35ba..ec8d9fd29b 100644 --- a/components/esp_hid/src/ble_hidd.c +++ b/components/esp_hid/src/ble_hidd.c @@ -141,7 +141,6 @@ struct esp_ble_hidd_dev_s { esp_hid_device_config_t config; uint16_t appearance; - bool connected; uint16_t conn_id; esp_bd_addr_t remote_bda; @@ -158,7 +157,6 @@ struct esp_ble_hidd_dev_s { uint16_t bat_level_handle; uint16_t bat_ccc_handle; - uint8_t pnp[7]; hidd_dev_map_t *devices; @@ -173,7 +171,6 @@ static const uint8_t hidInfo[4] = { ESP_HID_FLAGS_REMOTE_WAKE | ESP_HID_FLAGS_NORMALLY_CONNECTABLE // Flags }; - #define WAIT_CB(d) xSemaphoreTake(d->sem, portMAX_DELAY) #define SEND_CB(d) xSemaphoreGive(d->sem) @@ -181,7 +178,7 @@ static const char *gatts_evt_names[25] = { "REG", "READ", "WRITE", "EXEC_WRITE", static const char *gatts_evt_str(uint8_t event) { - if (event >= (sizeof(gatts_evt_names)/sizeof(*gatts_evt_names))) { + if (event >= (sizeof(gatts_evt_names) / sizeof(*gatts_evt_names))) { return "UNKNOWN"; } return gatts_evt_names[event]; @@ -230,8 +227,8 @@ static esp_err_t create_info_db(esp_ble_hidd_dev_t *dev) uint8_t pnp_val[7] = { 0x02, //0x1=BT, 0x2=USB dev->config.vendor_id & 0xFF, (dev->config.vendor_id >> 8) & 0xFF, //VID - dev->config.product_id & 0xFF, (dev->config.product_id >> 8) & 0xFF, //PID - dev->config.version & 0xFF, (dev->config.version >> 8) & 0xFF //VERSION + dev->config.product_id & 0xFF, (dev->config.product_id >> 8) & 0xFF, //PID + dev->config.version & 0xFF, (dev->config.version >> 8) & 0xFF //VERSION }; memcpy(dev->pnp, pnp_val, 7); add_db_record(_last_db, index++, (uint8_t *)&s_character_declaration_uuid, ESP_GATT_PERM_READ, 1, 1, (uint8_t *)&s_char_prop_read); @@ -335,7 +332,6 @@ static esp_err_t create_hid_db(esp_ble_hidd_dev_t *dev, int device_index) return err; } - static void link_report_handles(hidd_dev_map_t *dev, uint16_t *handles) { @@ -601,7 +597,7 @@ static void hid_event_handler(esp_ble_hidd_dev_t *dev, int device_index, esp_gat } } -static int get_device_map_index_by_gatts_if (esp_ble_hidd_dev_t *dev, esp_gatt_if_t gatts_if) +static int get_device_map_index_by_gatts_if(esp_ble_hidd_dev_t *dev, esp_gatt_if_t gatts_if) { for (uint8_t d = 0; d < dev->devices_len; d++) { if (dev->devices[d].hid_svc.gatt_if && gatts_if == dev->devices[d].hid_svc.gatt_if) { @@ -780,7 +776,7 @@ void esp_hidd_gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatt } else if (s_dev->info_svc.gatt_if && gatts_if == s_dev->info_svc.gatt_if) { info_event_handler(s_dev, event, gatts_if, param); } else { - int devi = get_device_map_index_by_gatts_if (s_dev, gatts_if); + int devi = get_device_map_index_by_gatts_if(s_dev, gatts_if); if (devi >= 0) { hid_event_handler(s_dev, devi, event, gatts_if, param); } else { @@ -971,7 +967,6 @@ esp_err_t esp_ble_hidd_dev_init(esp_hidd_dev_t *dev_p, const esp_hid_device_conf return ESP_FAIL; } - esp_event_loop_args_t event_task_args = { .queue_size = 5, .task_name = "ble_hidd_events", diff --git a/components/esp_hid/src/ble_hidh.c b/components/esp_hid/src/ble_hidh.c index 8179726239..616bd27846 100644 --- a/components/esp_hid/src/ble_hidh.c +++ b/components/esp_hid/src/ble_hidh.c @@ -28,7 +28,7 @@ static const char *s_gattc_evt_names[] = {"REG", "UNREG", "OPEN", "READ_CHAR", " const char *gattc_evt_str(uint8_t event) { - if (event >= (sizeof(s_gattc_evt_names)/sizeof(*s_gattc_evt_names))) { + if (event >= (sizeof(s_gattc_evt_names) / sizeof(*s_gattc_evt_names))) { return "UNKNOWN"; } return s_gattc_evt_names[event]; @@ -396,7 +396,6 @@ void esp_hidh_gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gatt SEND_CB();//return from open break; - case ESP_GATTC_READ_CHAR_EVT: case ESP_GATTC_READ_DESCR_EVT: { dev = esp_hidh_dev_get_by_conn_id(p_data->read.conn_id); @@ -607,8 +606,8 @@ static void esp_ble_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp) while (report) { if (report->map_index == d) { fprintf(fp, " %8s %7s %6s, ID: %2u, Length: %3u, Permissions: 0x%02x, Handle: %3u, CCC Handle: %3u\n", - esp_hid_usage_str(report->usage), esp_hid_report_type_str(report->report_type), esp_hid_protocol_mode_str(report->protocol_mode), - report->report_id, report->value_len, report->permissions, report->handle, report->ccc_handle); + esp_hid_usage_str(report->usage), esp_hid_report_type_str(report->report_type), esp_hid_protocol_mode_str(report->protocol_mode), + report->report_id, report->value_len, report->permissions, report->handle, report->ccc_handle); } report = report->next; } diff --git a/components/esp_hid/src/bt_hidd.c b/components/esp_hid/src/bt_hidd.c index ec888267ff..beb05a0eff 100644 --- a/components/esp_hid/src/bt_hidd.c +++ b/components/esp_hid/src/bt_hidd.c @@ -43,8 +43,7 @@ typedef struct { uint8_t devices_len; } esp_bt_hidd_dev_t; -typedef struct -{ +typedef struct { osi_mutex_t mutex; esp_bt_hidd_dev_t *dev; esp_hidd_app_param_t app_param; @@ -313,7 +312,7 @@ static bool esp_bt_hidd_dev_connected(void *devp) ret = false; break; } - } while(0); + } while (0); if (ret) { ret = dev->connected; } @@ -337,7 +336,7 @@ static esp_err_t esp_bt_hidd_dev_deinit(void *devp) ESP_LOGE(TAG, "Wrong HID device provided"); ret = ESP_FAIL; } - } while(0); + } while (0); osi_mutex_unlock(&s_hidd_param.mutex); if (ret == ESP_OK) { @@ -369,7 +368,7 @@ static esp_err_t esp_bt_hidd_dev_disconnect(void *devp) ESP_LOGW(TAG, "already disconnected"); return ESP_OK; } - } while(0); + } while (0); osi_mutex_unlock(&s_hidd_param.mutex); if (ret == ESP_OK) { @@ -423,7 +422,7 @@ static esp_err_t esp_bt_hidd_dev_input_set(void *devp, size_t index, size_t id, ret = ESP_FAIL; break; } - } while(0); + } while (0); osi_mutex_unlock(&s_hidd_param.mutex); if (ret == ESP_OK) { @@ -469,7 +468,7 @@ static esp_err_t esp_bt_hidd_dev_feature_set(void *devp, size_t index, size_t id ret = ESP_FAIL; break; } - } while(0); + } while (0); osi_mutex_unlock(&s_hidd_param.mutex); if (ret == ESP_OK) { diff --git a/components/esp_hid/src/bt_hidh.c b/components/esp_hid/src/bt_hidh.c index 8193b38c07..59e641f573 100644 --- a/components/esp_hid/src/bt_hidh.c +++ b/components/esp_hid/src/bt_hidh.c @@ -55,7 +55,8 @@ static const char *s_esp_hh_status_names[] = {"OK", "NO_DATA", "NEED_INIT", "NEED_DEINIT", - "NO_CONNECTION"}; + "NO_CONNECTION" + }; static esp_hidh_dev_t *hidh_dev_ctor(esp_bd_addr_t bda); @@ -418,7 +419,7 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) esp_hidh_dev_lock(dev); dev->added = param->add_dev.status == ESP_HIDH_OK ? true : false; esp_hidh_dev_unlock(dev); - } while(0); + } while (0); if (param->add_dev.status != ESP_HIDH_OK) { ESP_LOGE(TAG, "ADD_DEV ERROR: %s", s_esp_hh_status_names[param->add_dev.status]); @@ -460,7 +461,7 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) // free the device in the wrapper event handler dev->in_use = false; esp_hidh_dev_unlock(dev); - } while(0); + } while (0); if (param->close.status != ESP_HIDH_OK) { ESP_LOGE(TAG, "CLOSE ERROR: %s", s_esp_hh_status_names[param->close.status]); @@ -535,7 +536,7 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) reset_trans(dev); break; } - case ESP_HIDH_GET_IDLE_EVT:{ + case ESP_HIDH_GET_IDLE_EVT: { if (param->get_idle.status != ESP_HIDH_OK) { ESP_LOGE(TAG, "GET_IDLE ERROR: handle: %d, status: %s", param->get_idle.handle, s_esp_hh_status_names[param->get_idle.status]); @@ -684,7 +685,7 @@ static void esp_hh_cb(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param) } } else { report = esp_hidh_dev_get_input_report_by_proto_and_data( - dev, ESP_HID_PROTOCOL_MODE_REPORT, param->data_ind.len, param->data_ind.data, &has_report_id); + dev, ESP_HID_PROTOCOL_MODE_REPORT, param->data_ind.len, param->data_ind.data, &has_report_id); if (report == NULL) { esp_hidh_dev_unlock(dev); ESP_LOGE(TAG, "Not find report handle: %d mode: %s", param->data_ind.handle, @@ -903,7 +904,7 @@ static esp_err_t esp_bt_hidh_dev_get_idle(esp_hidh_dev_t *dev) if (ret == ESP_OK) { set_trans(dev, ESP_HID_TRANS_GET_IDLE); } - } while(0); + } while (0); return ret; } @@ -926,7 +927,7 @@ static esp_err_t esp_bt_hidh_dev_set_idle(esp_hidh_dev_t *dev, uint8_t idle_time if (ret == ESP_OK) { set_trans(dev, ESP_HID_TRANS_SET_IDLE); } - } while(0); + } while (0); return ret; } @@ -949,7 +950,7 @@ static esp_err_t esp_bt_hidh_dev_get_protocol(esp_hidh_dev_t *dev) if (ret == ESP_OK) { set_trans(dev, ESP_HID_TRANS_GET_PROTOCOL); } - } while(0); + } while (0); return ret; } @@ -973,7 +974,7 @@ static esp_err_t esp_bt_hidh_dev_set_protocol(esp_hidh_dev_t *dev, uint8_t proto if (ret == ESP_OK) { set_trans(dev, ESP_HID_TRANS_SET_PROTOCOL); } - } while(0); + } while (0); return ret; } @@ -987,8 +988,8 @@ static void esp_bt_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp) esp_hidh_dev_report_t *report = dev->reports; while (report) { fprintf(fp, " %8s %7s %6s, ID: %3u, Length: %3u\n", - esp_hid_usage_str(report->usage), esp_hid_report_type_str(report->report_type), get_protocol_mode(report->protocol_mode), - report->report_id, report->value_len); + esp_hid_usage_str(report->usage), esp_hid_report_type_str(report->report_type), get_protocol_mode(report->protocol_mode), + report->report_id, report->value_len); report = report->next; } } diff --git a/components/esp_hid/src/esp_hid_common.c b/components/esp_hid/src/esp_hid_common.c index ed4afc8947..364073ed9c 100644 --- a/components/esp_hid/src/esp_hid_common.c +++ b/components/esp_hid/src/esp_hid_common.c @@ -45,7 +45,6 @@ typedef enum { PARSE_WAIT_USAGE_PAGE, PARSE_WAIT_USAGE, PARSE_WAIT_COLLECTION_APPLICATION, PARSE_WAIT_END_COLLECTION } s_parse_step_t; - static s_parse_step_t s_parse_step = PARSE_WAIT_USAGE_PAGE; static uint8_t s_collection_depth = 0; static hid_report_params_t s_report_params = {0,}; @@ -192,7 +191,6 @@ static int handle_report(hid_report_params_t *report, bool first) return 0; } - static int parse_cmd(const uint8_t *data, size_t len, size_t index, hid_report_cmd_t **out) { if (index == len) { @@ -334,7 +332,6 @@ static int handle_cmd(hid_report_cmd_t *cmd) return 0; } - esp_hid_report_map_t *esp_hid_parse_report_map(const uint8_t *hid_rm, size_t hid_rm_len) { size_t index = 0; @@ -405,7 +402,7 @@ esp_hid_report_map_t *esp_hid_parse_report_map(const uint8_t *hid_rm, size_t hid void esp_hid_free_report_map(esp_hid_report_map_t *map) { - if (map != NULL){ + if (map != NULL) { free(map->reports); free(map); } @@ -444,7 +441,7 @@ const char *esp_hid_usage_str(esp_hid_usage_t usage) const char *esp_hid_protocol_mode_str(uint8_t protocol) { - if (protocol >= (sizeof(s_hid_protocol_names)/sizeof(s_hid_protocol_names[0]))) { + if (protocol >= (sizeof(s_hid_protocol_names) / sizeof(s_hid_protocol_names[0]))) { return s_unknown_str; } return s_hid_protocol_names[protocol]; @@ -452,7 +449,7 @@ const char *esp_hid_protocol_mode_str(uint8_t protocol) const char *esp_hid_report_type_str(uint8_t report_type) { - if (report_type >= (sizeof(s_hid_report_type_names)/sizeof(s_hid_report_type_names[0]))) { + if (report_type >= (sizeof(s_hid_report_type_names) / sizeof(s_hid_report_type_names[0]))) { return s_unknown_str; } return s_hid_report_type_names[report_type]; @@ -460,7 +457,7 @@ const char *esp_hid_report_type_str(uint8_t report_type) const char *esp_hid_cod_major_str(uint8_t cod_major) { - if (cod_major >= (sizeof(s_hid_cod_major_names)/sizeof(s_hid_cod_major_names[0]))) { + if (cod_major >= (sizeof(s_hid_cod_major_names) / sizeof(s_hid_cod_major_names[0]))) { return s_unknown_str; } return s_hid_cod_major_names[cod_major]; diff --git a/components/esp_hid/src/esp_hidh.c b/components/esp_hid/src/esp_hidh.c index 9f926746bf..1bbbf89bd9 100644 --- a/components/esp_hid/src/esp_hidh.c +++ b/components/esp_hid/src/esp_hidh.c @@ -37,7 +37,6 @@ static inline void unlock_devices(void) } } - /* * Public Functions * */ @@ -504,7 +503,6 @@ esp_err_t esp_hidh_dev_report_maps_get(esp_hidh_dev_t *dev, size_t *num_maps, es return ESP_OK; } - /* * Private Functions * */ @@ -557,7 +555,7 @@ esp_hidh_dev_report_t *esp_hidh_dev_get_report_by_id_type_proto(esp_hidh_dev_t * esp_hidh_dev_report_t *r = dev->reports; while (r) { if (r->map_index == map_index && r->report_type == report_type && r->report_id == report_id && - r->protocol_mode == protocol_mode) { + r->protocol_mode == protocol_mode) { return r; } r = r->next; @@ -614,7 +612,7 @@ esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_proto_and_data(esp_hidh_ // first, assume data not include report id while (r) { if (r->value_len == len && r->report_id == 0 && (r->report_type & ESP_HID_REPORT_TYPE_INPUT) && - r->protocol_mode == protocol_mode) { + r->protocol_mode == protocol_mode) { *has_report_id = false; break; } @@ -630,7 +628,7 @@ esp_hidh_dev_report_t *esp_hidh_dev_get_input_report_by_proto_and_data(esp_hidh_ r = dev->reports; while (r) { if (r->value_len == len - 1 && r->report_id == *data && (r->report_type & ESP_HID_REPORT_TYPE_INPUT) && - r->protocol_mode == protocol_mode) { + r->protocol_mode == protocol_mode) { *has_report_id = true; break; } diff --git a/components/esp_hid/src/nimble_hidd.c b/components/esp_hid/src/nimble_hidd.c index f9c9cadf5c..eec5fc4abf 100644 --- a/components/esp_hid/src/nimble_hidd.c +++ b/components/esp_hid/src/nimble_hidd.c @@ -32,7 +32,6 @@ static const char *TAG = "NIMBLE_HIDD"; #define BLE_SVC_BAS_UUID16 0x180F - typedef struct esp_ble_hidd_dev_s esp_ble_hidd_dev_t; // there can be only one BLE HID device static esp_ble_hidd_dev_t *s_dev = NULL; @@ -48,8 +47,6 @@ typedef struct { uint16_t hid_protocol_handle; } hidd_dev_map_t; - - struct esp_ble_hidd_dev_s { esp_hidd_dev_t *dev; esp_event_loop_handle_t event_loop_handle; @@ -119,7 +116,7 @@ static int create_hid_db(int device_index) if (report->usage == ESP_HID_USAGE_KEYBOARD) { //Boot Keyboard Input hparams.kbd_inp_present = 1; } else { //Boot Mouse Input - hparams.mouse_inp_present = 1; + hparams.mouse_inp_present = 1; } } else { //Boot Keyboard Output hparams.kbd_out_present = 1; @@ -129,14 +126,14 @@ static int create_hid_db(int device_index) hparams.rpts_len = report_mode_rpts; /* Add service */ rc = ble_svc_hid_add(hparams); - if(rc != 0) { + if (rc != 0) { return rc; } return rc; } - -static int ble_hid_create_info_db() { +static int ble_hid_create_info_db() +{ int rc; rc = 0; @@ -144,8 +141,8 @@ static int ble_hid_create_info_db() { uint8_t pnp_val[7] = { 0x02, //0x1=BT, 0x2=USB s_dev->config.vendor_id & 0xFF, (s_dev->config.vendor_id >> 8) & 0xFF, //VID - s_dev->config.product_id & 0xFF, (s_dev->config.product_id >> 8) & 0xFF, //PID - s_dev->config.version & 0xFF, (s_dev->config.version >> 8) & 0xFF //VERSION + s_dev->config.product_id & 0xFF, (s_dev->config.product_id >> 8) & 0xFF, //PID + s_dev->config.version & 0xFF, (s_dev->config.version >> 8) & 0xFF //VERSION }; memcpy(s_dev->pnp, pnp_val, 7); ble_svc_dis_pnp_id_set((char *)s_dev->pnp); @@ -170,7 +167,7 @@ static int nimble_hid_start_gatts(void) for (uint8_t d = 0; d < s_dev->devices_len; d++) { rc = create_hid_db(d); - if(rc != 0) { + if (rc != 0) { return rc; } } @@ -273,7 +270,8 @@ static int ble_hid_free_config(esp_ble_hidd_dev_t *dev) return ESP_OK; } -static int nimble_hidd_dev_deinit(void *devp) { +static int nimble_hidd_dev_deinit(void *devp) +{ esp_ble_hidd_dev_t *dev = (esp_ble_hidd_dev_t *)devp; if (!s_dev) { ESP_LOGE(TAG, "HID device profile already uninitialized"); @@ -322,24 +320,26 @@ static int nimble_hidd_dev_battery_set(void *devp, uint8_t level) } /* if mode is NULL, find the first matching report */ -static hidd_le_report_item_t* find_report(uint8_t id, uint8_t type, uint8_t *mode) { +static hidd_le_report_item_t* find_report(uint8_t id, uint8_t type, uint8_t *mode) +{ hidd_le_report_item_t *rpt; for (uint8_t d = 0; d < s_dev->devices_len; d++) { for (uint8_t i = 0; i < s_dev->devices[d].reports_len; i++) { rpt = &s_dev->devices[d].reports[i]; - if(rpt->report_id == id && rpt->report_type == type && (!mode || (mode && *mode == rpt->protocol_mode))) { + if (rpt->report_id == id && rpt->report_type == type && (!mode || (mode && *mode == rpt->protocol_mode))) { return rpt; } } } return NULL; } -static hidd_le_report_item_t* find_report_by_usage_and_type(uint8_t usage, uint8_t type, uint8_t *mode) { +static hidd_le_report_item_t* find_report_by_usage_and_type(uint8_t usage, uint8_t type, uint8_t *mode) +{ hidd_le_report_item_t *rpt; for (uint8_t d = 0; d < s_dev->devices_len; d++) { for (uint8_t i = 0; i < s_dev->devices[d].reports_len; i++) { rpt = &s_dev->devices[d].reports[i]; - if(rpt->usage == usage && rpt->report_type == type && (!mode || (mode && *mode == rpt->protocol_mode))) { + if (rpt->usage == usage && rpt->report_type == type && (!mode || (mode && *mode == rpt->protocol_mode))) { return rpt; } } @@ -365,12 +365,12 @@ static int nimble_hidd_dev_input_set(void *devp, size_t index, size_t id, uint8_ /* check the protocol mode */ /* as the protocol mode is always present, its safe to read the characteristic */ rc = ble_att_svr_read_local(s_dev->devices[index].hid_protocol_handle, &om); - if(rc != 0) { + if (rc != 0) { ESP_LOGE(TAG, "Unable to fetch protocol_mode\n"); return ESP_FAIL; } rc = ble_hs_mbuf_to_flat(om, &dev->protocol, sizeof(dev->protocol), NULL); - if(rc != 0) { + if (rc != 0) { return ESP_FAIL; } /* free the mbuf */ @@ -409,12 +409,12 @@ static int nimble_hidd_dev_feature_set(void *devp, size_t index, size_t id, uint /* check the protocol mode */ /* as the protocol mode is always present, its safe to read the characteristic */ rc = ble_att_svr_read_local(s_dev->devices[index].hid_protocol_handle, &om); - if(rc != 0) { + if (rc != 0) { ESP_LOGE(TAG, "Unable to fetch protocol_mode\n"); return ESP_FAIL; } rc = ble_hs_mbuf_to_flat(om, &dev->protocol, sizeof(dev->protocol), NULL); - if(rc != 0) { + if (rc != 0) { return ESP_FAIL; } /* free the mbuf */ @@ -488,11 +488,11 @@ static int nimble_hid_gap_event(struct ble_gap_event *event, void *arg) /* reset the protocol mode value */ data = ESP_HID_PROTOCOL_MODE_REPORT; om = ble_hs_mbuf_from_flat(&data, 1); - if(om == NULL) { + if (om == NULL) { ESP_LOGD(TAG, "No memory to allocate mbuf"); } /* NOTE : om is freed by stack */ - for(int i = 0; i < s_dev->devices_len; i++) { + for (int i = 0; i < s_dev->devices_len; i++) { rc = ble_att_svr_write_local(s_dev->devices[i].hid_protocol_handle, om); if (rc != 0) { ESP_LOGE(TAG, "Write on Protocol Mode Failed: %d", rc); @@ -541,7 +541,7 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf), ctxt->svc.handle); uuid16 = ble_uuid_u16(ctxt->svc.svc_def->uuid); - if(uuid16 == BLE_SVC_HID_UUID16) { + if (uuid16 == BLE_SVC_HID_UUID16) { ++service_index; s_dev->devices[service_index].hid_svc = ctxt->svc.handle; } @@ -555,36 +555,36 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi ctxt->chr.def_handle, ctxt->chr.val_handle); uuid16 = ble_uuid_u16(ctxt->chr.chr_def->uuid); - if(uuid16 == BLE_SVC_HID_CHR_UUID16_HID_CTRL_PT) { + if (uuid16 == BLE_SVC_HID_CHR_UUID16_HID_CTRL_PT) { /* assuming this characteristic is from the last registered hid service */ s_dev->devices[service_index].hid_control_handle = ctxt->chr.val_handle; } - if(uuid16 == BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE) { + if (uuid16 == BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE) { /* assuming this characteristic is from the last registered hid service */ s_dev->devices[service_index].hid_protocol_handle = ctxt->chr.val_handle; } - if(uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_KBD_INP) { + if (uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_KBD_INP) { protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT; rpt = find_report_by_usage_and_type(ESP_HID_USAGE_KEYBOARD, ESP_HID_REPORT_TYPE_INPUT, &protocol_mode); - if(rpt == NULL) { + if (rpt == NULL) { ESP_LOGE(TAG, "Unknown boot kbd input report registration"); return; } rpt->handle = ctxt->chr.val_handle; } - if(uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_KBD_OUT) { + if (uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_KBD_OUT) { protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT; rpt = find_report_by_usage_and_type(ESP_HID_USAGE_KEYBOARD, ESP_HID_REPORT_TYPE_OUTPUT, &protocol_mode); - if(rpt == NULL) { + if (rpt == NULL) { ESP_LOGE(TAG, "Unknown boot kbd output report registration"); return; } rpt->handle = ctxt->chr.val_handle; } - if(uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INP) { + if (uuid16 == BLE_SVC_HID_CHR_UUID16_BOOT_MOUSE_INP) { protocol_mode = ESP_HID_PROTOCOL_MODE_BOOT; rpt = find_report_by_usage_and_type(ESP_HID_USAGE_MOUSE, ESP_HID_REPORT_TYPE_INPUT, &protocol_mode); - if(rpt == NULL) { + if (rpt == NULL) { ESP_LOGE(TAG, "Unknown boot mouse input report registration"); return; } @@ -597,7 +597,7 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf), ctxt->dsc.handle); uuid16 = ble_uuid_u16(ctxt->dsc.dsc_def->uuid); - if(uuid16 == BLE_SVC_HID_DSC_UUID16_RPT_REF) { + if (uuid16 == BLE_SVC_HID_DSC_UUID16_RPT_REF) { rc = ble_att_svr_read_local(ctxt->dsc.handle, &om); assert(rc == 0); @@ -621,7 +621,8 @@ static void nimble_gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, voi } } -static void nimble_host_synced(void) { +static void nimble_host_synced(void) +{ esp_event_post_to(s_dev->event_loop_handle, ESP_HIDD_EVENTS, ESP_HIDD_START_EVENT, NULL, 0, portMAX_DELAY); } @@ -699,11 +700,11 @@ esp_err_t esp_ble_hidd_dev_init(esp_hidd_dev_t *dev_p, const esp_hid_device_conf ble_hs_cfg.sync_cb = nimble_host_synced; ble_hs_cfg.gatts_register_cb = nimble_gatt_svr_register_cb; rc = nimble_hid_start_gatts(); - if(rc != ESP_OK) { + if (rc != ESP_OK) { return rc; } ble_gap_event_listener_register(&nimble_gap_event_listener, - nimble_hid_gap_event, NULL); + nimble_hid_gap_event, NULL); return rc; } diff --git a/components/esp_hid/src/nimble_hidh.c b/components/esp_hid/src/nimble_hidh.c index 55a2125559..d86398119a 100644 --- a/components/esp_hid/src/nimble_hidh.c +++ b/components/esp_hid/src/nimble_hidh.c @@ -93,28 +93,28 @@ print_mbuf(const struct os_mbuf *om) static int nimble_on_read(uint16_t conn_handle, - const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, - void *arg) + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg) { int old_offset; MODLOG_DFLT(INFO, "Read complete; status=%d conn_handle=%d", error->status, conn_handle); s_read_status = error->status; - switch(s_read_status) { - case 0: - MODLOG_DFLT(DEBUG, " attr_handle=%d value=", attr->handle); - old_offset = s_read_data_len; - s_read_data_len += OS_MBUF_PKTLEN(attr->om); - s_read_data_val = realloc(s_read_data_val, s_read_data_len + 1); // 1 extra byte to store null char - ble_hs_mbuf_to_flat(attr->om, s_read_data_val + old_offset, OS_MBUF_PKTLEN(attr->om), NULL); - print_mbuf(attr->om); - return 0; - case BLE_HS_EDONE: - s_read_data_val[s_read_data_len] = 0; // to insure strings are ended with \0 */ - s_read_status = 0; - SEND_CB(); - return 0; + switch (s_read_status) { + case 0: + MODLOG_DFLT(DEBUG, " attr_handle=%d value=", attr->handle); + old_offset = s_read_data_len; + s_read_data_len += OS_MBUF_PKTLEN(attr->om); + s_read_data_val = realloc(s_read_data_val, s_read_data_len + 1); // 1 extra byte to store null char + ble_hs_mbuf_to_flat(attr->om, s_read_data_val + old_offset, OS_MBUF_PKTLEN(attr->om), NULL); + print_mbuf(attr->om); + return 0; + case BLE_HS_EDONE: + s_read_data_val[s_read_data_len] = 0; // to insure strings are ended with \0 */ + s_read_status = 0; + SEND_CB(); + return 0; } return 0; } @@ -161,7 +161,7 @@ static int read_descr(uint16_t conn_handle, uint16_t handle, uint8_t **out, uint static int svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg) + const struct ble_gatt_svc *service, void *arg) { int rc; struct ble_gatt_svc *service_result; @@ -211,7 +211,7 @@ svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, static int chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) + const struct ble_gatt_chr *chr, void *arg) { struct ble_gatt_chr *chrs; int rc; @@ -221,8 +221,8 @@ chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, status = error->status; switch (error->status) { case 0: - ESP_LOGD(TAG,"Char discovered : def handle : %04x, val_handle : %04x, properties : %02x\n, uuid : %04x", - chr->def_handle, chr->val_handle,chr->properties, ble_uuid_u16(&chr->uuid.u)); + ESP_LOGD(TAG, "Char discovered : def handle : %04x, val_handle : %04x, properties : %02x\n, uuid : %04x", + chr->def_handle, chr->val_handle, chr->properties, ble_uuid_u16(&chr->uuid.u)); memcpy(chrs + chrs_discovered, chr, sizeof(struct ble_gatt_chr)); chrs_discovered++; break; @@ -249,8 +249,8 @@ chr_disced(uint16_t conn_handle, const struct ble_gatt_error *error, static int desc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, - uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, - void *arg) + uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc, + void *arg) { int rc; struct ble_gatt_dsc *dscr; @@ -260,8 +260,8 @@ desc_disced(uint16_t conn_handle, const struct ble_gatt_error *error, status = error->status; switch (error->status) { case 0: - ESP_LOGD(TAG,"DISC discovered : handle : %04x, uuid : %04x", - dsc->handle, ble_uuid_u16(&dsc->uuid.u)); + ESP_LOGD(TAG, "DISC discovered : handle : %04x, uuid : %04x", + dsc->handle, ble_uuid_u16(&dsc->uuid.u)); memcpy(dscr + dscs_discovered, dsc, sizeof(struct ble_gatt_dsc)); dscs_discovered++; break; @@ -307,12 +307,12 @@ static void read_device_services(esp_hidh_dev_t *dev) int rc; rc = ble_gattc_disc_all_svcs(dev->ble.conn_id, svc_disced, service_result); - if(rc != 0) { + if (rc != 0) { ESP_LOGD(TAG, "Error discovering services : %d", rc); assert(rc != 0); } WAIT_CB(); - if(status != 0) { + if (status != 0) { ESP_LOGE(TAG, "failed to find services"); assert(status == 0); } @@ -351,8 +351,8 @@ static void read_device_services(esp_hidh_dev_t *dev) rc = ble_gattc_disc_all_chrs(dev->ble.conn_id, service_result[s].start_handle, service_result[s].end_handle, chr_disced, char_result); WAIT_CB(); - if(status != 0) { - ESP_LOGE(TAG, "failed to find chars for service : %d",s); + if (status != 0) { + ESP_LOGE(TAG, "failed to find chars for service : %d", s); assert(status == 0); } ccount = chrs_discovered; @@ -365,8 +365,8 @@ static void read_device_services(esp_hidh_dev_t *dev) if (suuid == BLE_SVC_GAP_UUID16) { if (dev->config.device_name == NULL && cuuid == BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME - && (char_result[c].properties & BLE_GATT_CHR_PROP_READ) != 0) { - if (read_char(dev->ble.conn_id, chandle,&rdata, &rlen) == 0 && rlen) { + && (char_result[c].properties & BLE_GATT_CHR_PROP_READ) != 0) { + if (read_char(dev->ble.conn_id, chandle, &rdata, &rlen) == 0 && rlen) { dev->config.device_name = (const char *)rdata; } } else { @@ -374,7 +374,7 @@ static void read_device_services(esp_hidh_dev_t *dev) } } else if (suuid == BLE_SVC_BAS_UUID16) { if (cuuid == BLE_SVC_BAS_CHR_UUID16_BATTERY_LEVEL && - (char_result[c].properties & BLE_GATT_CHR_PROP_READ) != 0) { + (char_result[c].properties & BLE_GATT_CHR_PROP_READ) != 0) { dev->ble.battery_handle = chandle; } else { continue; @@ -400,7 +400,7 @@ static void read_device_services(esp_hidh_dev_t *dev) } continue; } else { - if(cuuid == BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE) { + if (cuuid == BLE_SVC_HID_CHR_UUID16_PROTOCOL_MODE) { if (char_result[c].properties & BLE_GATT_CHR_PROP_READ) { if (read_char(dev->ble.conn_id, chandle, &rdata, &rlen) == 0 && rlen) { dev->protocol_mode[hidindex] = *((uint8_t *)rdata); @@ -457,17 +457,16 @@ static void read_device_services(esp_hidh_dev_t *dev) struct ble_gatt_dsc descr_result[20]; uint16_t dcount = 20; uint16_t chr_end_handle; - if(c + 1 < ccount) { + if (c + 1 < ccount) { chr_end_handle = char_result[c + 1].def_handle; - } - else { + } else { chr_end_handle = service_result[s].end_handle; } rc = ble_gattc_disc_all_dscs(dev->ble.conn_id, char_result[c].val_handle, chr_end_handle, desc_disced, descr_result); WAIT_CB(); - if(status != 0) { - ESP_LOGE(TAG, "failed to find discriptors for characteristic : %d",c); + if (status != 0) { + ESP_LOGE(TAG, "failed to find discriptors for characteristic : %d", c); assert(status == 0); } dcount = dscs_discovered; @@ -479,7 +478,7 @@ static void read_device_services(esp_hidh_dev_t *dev) if (suuid == BLE_SVC_BAS_UUID16) { if (duuid == BLE_GATT_DSC_CLT_CFG_UUID16 && - (char_result[c].properties & BLE_GATT_CHR_PROP_NOTIFY) != 0) { + (char_result[c].properties & BLE_GATT_CHR_PROP_NOTIFY) != 0) { dev->ble.battery_ccc_handle = dhandle; } } else if (suuid == BLE_SVC_HID_UUID16 && report != NULL) { @@ -549,12 +548,12 @@ static void read_device_services(esp_hidh_dev_t *dev) static int on_subscribe(uint16_t conn_handle, - const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, - void *arg) + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg) { uint16_t conn_id; - conn_id =*((uint16_t*) arg); + conn_id = *((uint16_t*) arg); assert(conn_id == conn_handle); @@ -572,22 +571,22 @@ static void register_for_notify(uint16_t conn_handle, uint16_t handle) int rc; value[0] = 1; value[1] = 0; - rc = ble_gattc_write_flat(conn_handle, handle, value, sizeof value, on_subscribe,(void *)&conn_handle); + rc = ble_gattc_write_flat(conn_handle, handle, value, sizeof value, on_subscribe, (void *)&conn_handle); if (rc != 0) { ESP_LOGE(TAG, "Error: Failed to subscribe to characteristic; " - "rc=%d\n", rc); + "rc=%d\n", rc); } WAIT_CB(); } static int on_write(uint16_t conn_handle, - const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, - void *arg) + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg) { uint16_t conn_id; - conn_id =*((uint16_t*) arg); + conn_id = *((uint16_t*) arg); assert(conn_id == conn_handle); @@ -652,7 +651,7 @@ esp_hidh_gattc_event_handler(struct ble_gap_event *event, void *arg) rc = ble_gap_conn_find(event->connect.conn_handle, &desc); assert(rc == 0); dev = esp_hidh_dev_get_by_bda(desc.peer_ota_addr.val); - if(!dev) { + if (!dev) { ESP_LOGE(TAG, "Connect received for unknown device"); } dev->status = -1; // set to not found and clear if HID service is found @@ -660,7 +659,7 @@ esp_hidh_gattc_event_handler(struct ble_gap_event *event, void *arg) /* Try to set the mtu to the max value */ rc = ble_att_set_preferred_mtu(BLE_ATT_MTU_MAX); - if(rc != 0) { + if (rc != 0) { ESP_LOGE(TAG, "att preferred mtu set failed"); } rc = ble_gattc_exchange_mtu(event->connect.conn_handle, NULL, NULL); @@ -671,9 +670,9 @@ esp_hidh_gattc_event_handler(struct ble_gap_event *event, void *arg) } else { MODLOG_DFLT(ERROR, "Error: Connection failed; status=%d\n", event->connect.status); - dev->status = event->connect.status; // ESP_GATT_CONN_FAIL_ESTABLISH; - dev->ble.conn_id = -1; - SEND_CB(); // return from connection + dev->status = event->connect.status; // ESP_GATT_CONN_FAIL_ESTABLISH; + dev->ble.conn_id = -1; + SEND_CB(); // return from connection } return 0; @@ -738,7 +737,7 @@ esp_hidh_gattc_event_handler(struct ble_gap_event *event, void *arg) esp_hidh_event_data_t *p_param = NULL; size_t event_data_size = sizeof(esp_hidh_event_data_t); - if(report->protocol_mode != dev->protocol_mode[report->map_index]) { + if (report->protocol_mode != dev->protocol_mode[report->map_index]) { /* only pass the notifications in the current protocol mode */ ESP_LOGD(TAG, "Wrong protocol mode, dropping notification"); return 0; @@ -861,9 +860,9 @@ static void esp_ble_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp) while (report) { if (report->map_index == d) { fprintf(fp, " %8s %7s %6s, ID: %2u, Length: %3u, Permissions: 0x%02x, Handle: %3u, CCC Handle: %3u\n", - esp_hid_usage_str(report->usage), esp_hid_report_type_str(report->report_type), - esp_hid_protocol_mode_str(report->protocol_mode), report->report_id, report->value_len, - report->permissions, report->handle, report->ccc_handle); + esp_hid_usage_str(report->usage), esp_hid_report_type_str(report->report_type), + esp_hid_protocol_mode_str(report->protocol_mode), report->report_id, report->value_len, + report->permissions, report->handle, report->ccc_handle); } report = report->next; } @@ -883,10 +882,11 @@ static void esp_ble_hidh_event_handler_wrapper(void *event_handler_arg, esp_even esp_hidh_post_process_event_handler(event_handler_arg, event_base, event_id, event_data); } -static void nimble_host_synced(void) { -/* - no need to perform anything here -*/ +static void nimble_host_synced(void) +{ + /* + no need to perform anything here + */ } static void nimble_host_reset(int reason) diff --git a/tools/ci/astyle-rules.yml b/tools/ci/astyle-rules.yml index ebc9be7134..bf49a32925 100644 --- a/tools/ci/astyle-rules.yml +++ b/tools/ci/astyle-rules.yml @@ -55,7 +55,6 @@ components_not_formatted_temporary: - "/components/esp_coex/" - "/components/esp_eth/" - "/components/esp_gdbstub/" - - "/components/esp_hid/" - "/components/esp_http_client/" - "/components/esp_http_server/" - "/components/esp_https_ota/"