From bdeb2b85e7af515fe625ffa1c0af2480b1d60bff Mon Sep 17 00:00:00 2001 From: embr Date: Mon, 24 Apr 2023 16:23:41 +0200 Subject: [PATCH] clang-format --- .clang-format | 11 +++++ shell.nix | 2 +- src/btstack_config.h | 2 +- src/main.c | 62 ++++++++++++------------ src/stdio_rtt.c | 16 +++---- src/vibe_bt.c | 112 ++++++++++++++++++++++++------------------- src/vibe_bt.h | 24 +++++----- 7 files changed, 127 insertions(+), 102 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..bd98d7e --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +--- +BasedOnStyle: WebKit +IndentWidth: 4 +TabWidth: 4 +UseTab: Always +--- +Language: Cpp +BreakBeforeBraces: Attach + +# this doesn't come out until clang-format 15 :( +#InsertBraces: true diff --git a/shell.nix b/shell.nix index 8f45b57..b6ec5a2 100644 --- a/shell.nix +++ b/shell.nix @@ -2,6 +2,6 @@ pkgs.mkShell { buildInputs = with pkgs; [ cmake cmakeCurses ninja python3 gcc-arm-embedded - picotool + picotool clang-tools ]; } diff --git a/src/btstack_config.h b/src/btstack_config.h index a5386e0..a132ad9 100644 --- a/src/btstack_config.h +++ b/src/btstack_config.h @@ -28,7 +28,7 @@ // Enable and configure HCI Controller to Host Flow Control to avoid cyw43 shared bus overrun #define ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL -#define HCI_HOST_ACL_PACKET_LEN (255+4) +#define HCI_HOST_ACL_PACKET_LEN (255 + 4) #define HCI_HOST_ACL_PACKET_NUM 3 #define HCI_HOST_SCO_PACKET_LEN 120 #define HCI_HOST_SCO_PACKET_NUM 3 diff --git a/src/main.c b/src/main.c index 1371ba5..741e019 100644 --- a/src/main.c +++ b/src/main.c @@ -1,15 +1,15 @@ -#include #include "btstack.h" #include "hci_dump.h" #include "hci_dump_segger_rtt_stdout.h" -#include "pico/cyw43_arch.h" #include "pico/btstack_cyw43.h" +#include "pico/cyw43_arch.h" #include "pico/stdlib.h" #include "stdio_rtt.h" #include "vibe_bt.h" +#include #ifndef CYW43_WL_GPIO_LED_PIN - #error no WiFi LED on board, wrong -DPICO_BOARD? +#error no WiFi LED on board, wrong -DPICO_BOARD? #endif #define HEARTBEAT_PERIOD_MS 100 @@ -17,42 +17,42 @@ static btstack_timer_source_t heartbeat; static btstack_packet_callback_registration_t hci_event_callback_registration; -static void heartbeat_handler(struct btstack_timer_source *ts) { - cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, bt_connected); +static void heartbeat_handler(struct btstack_timer_source* ts) { + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, bt_connected); - // Restart timer - btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); - btstack_run_loop_add_timer(ts); + // Restart timer + btstack_run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS); + btstack_run_loop_add_timer(ts); } int main() { - stdio_init_all(); - stdio_rtt_init(); + stdio_init_all(); + stdio_rtt_init(); - if (cyw43_arch_init()) { - printf("CYW43 init failed"); - return -1; - } + if (cyw43_arch_init()) { + printf("CYW43 init failed"); + return -1; + } - // hci_dump_init(hci_dump_segger_rtt_stdout_get_instance()); - l2cap_init(); - sm_init(); + // hci_dump_init(hci_dump_segger_rtt_stdout_get_instance()); + l2cap_init(); + sm_init(); - att_server_init(profile_data, bt_att_read_callback, bt_att_write_callback); - - hci_event_callback_registration.callback = &bt_packet_handler; - hci_add_event_handler(&hci_event_callback_registration); - // register for ATT event - att_server_register_packet_handler(bt_packet_handler); + att_server_init(profile_data, bt_att_read_callback, bt_att_write_callback); - // set one-shot btstack timer - heartbeat.process = &heartbeat_handler; - btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); - btstack_run_loop_add_timer(&heartbeat); + hci_event_callback_registration.callback = &bt_packet_handler; + hci_add_event_handler(&hci_event_callback_registration); + // register for ATT event + att_server_register_packet_handler(bt_packet_handler); - // turn on bluetooth! - hci_power_control(HCI_POWER_ON); + // set one-shot btstack timer + heartbeat.process = &heartbeat_handler; + btstack_run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS); + btstack_run_loop_add_timer(&heartbeat); - btstack_run_loop_execute(); - return 0; + // turn on bluetooth! + hci_power_control(HCI_POWER_ON); + + btstack_run_loop_execute(); + return 0; } diff --git a/src/stdio_rtt.c b/src/stdio_rtt.c index 8cbb060..c010a78 100644 --- a/src/stdio_rtt.c +++ b/src/stdio_rtt.c @@ -1,20 +1,20 @@ -#include "pico/stdio/driver.h" #include "stdio_rtt.h" #include "SEGGER_RTT.h" +#include "pico/stdio/driver.h" void stdio_rtt_init(void) { - stdio_set_driver_enabled(&stdio_rtt, true); + stdio_set_driver_enabled(&stdio_rtt, true); } -static void stdio_rtt_out_chars(const char *buf, int length) { - SEGGER_RTT_Write(0, buf, length); +static void stdio_rtt_out_chars(const char* buf, int length) { + SEGGER_RTT_Write(0, buf, length); } -static int stdio_rtt_in_chars(char *buf, int length) { - return SEGGER_RTT_Read(0, buf, length); +static int stdio_rtt_in_chars(char* buf, int length) { + return SEGGER_RTT_Read(0, buf, length); } stdio_driver_t stdio_rtt = { - .out_chars = stdio_rtt_out_chars, - .in_chars = stdio_rtt_in_chars, + .out_chars = stdio_rtt_out_chars, + .in_chars = stdio_rtt_in_chars, }; diff --git a/src/vibe_bt.c b/src/vibe_bt.c index eea4590..84d7adc 100644 --- a/src/vibe_bt.c +++ b/src/vibe_bt.c @@ -1,48 +1,58 @@ -#include #include "vibe_bt.h" #include "vibe.h" +#include #define APP_AD_FLAGS 0x06 + +/* clang-format off */ static const uint8_t adv_data[] = { - // Flags general discoverable - 0x02, BLUETOOTH_DATA_TYPE_FLAGS, APP_AD_FLAGS, - // Name - 0x0E, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, 's', 'a', 'k', 'u', 'r', 'a', 'n', 'e', 'k', 'o', '-', '0', '3', + // Flags general discoverable + 0x02, + BLUETOOTH_DATA_TYPE_FLAGS, + APP_AD_FLAGS, + + // Name + 0x0E, + BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME, + 's', 'a', 'k', 'u', 'r', 'a', 'n', 'e', 'k', 'o', '-', '0', '3', }; + static const uint8_t adv_data_len = sizeof(adv_data); bool bt_connected = false; void bt_packet_handler( - uint8_t packet_type, - uint16_t channel, - uint8_t *packet, + uint8_t packet_type, + uint16_t channel, + uint8_t* packet, uint16_t size) { - UNUSED(size); - UNUSED(channel); - bd_addr_t local_addr; - if (packet_type != HCI_EVENT_PACKET) return; + UNUSED(size); + UNUSED(channel); + bd_addr_t local_addr; + if (packet_type != HCI_EVENT_PACKET) + return; - uint8_t event_type = hci_event_packet_get_type(packet); - switch(event_type){ - case BTSTACK_EVENT_STATE: - if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return; - gap_local_bd_addr(local_addr); - printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); + uint8_t event_type = hci_event_packet_get_type(packet); + switch (event_type) { + case BTSTACK_EVENT_STATE: + if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) + return; + gap_local_bd_addr(local_addr); + printf("BTstack up and running on %s.\n", bd_addr_to_str(local_addr)); - // setup advertisements - uint16_t adv_int_min = 800; - uint16_t adv_int_max = 800; - uint8_t adv_type = 0; - bd_addr_t null_addr; - memset(null_addr, 0, 6); - gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); - assert(adv_data_len <= 31); // ble limitation - gap_advertisements_set_data(adv_data_len, (uint8_t*) adv_data); - gap_advertisements_enable(1); - break; - case HCI_EVENT_LE_META: + // setup advertisements + uint16_t adv_int_min = 800; + uint16_t adv_int_max = 800; + uint8_t adv_type = 0; + bd_addr_t null_addr; + memset(null_addr, 0, 6); + gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); + assert(adv_data_len <= 31); // ble limitation + gap_advertisements_set_data(adv_data_len, (uint8_t*)adv_data); + gap_advertisements_enable(1); + break; + case HCI_EVENT_LE_META: switch (hci_event_le_meta_get_subevent_code(packet)) { case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: bt_connected = true; @@ -51,19 +61,19 @@ void bt_packet_handler( break; } break; - case HCI_EVENT_DISCONNECTION_COMPLETE: - bt_connected = false; - break; - default: - break; - } + case HCI_EVENT_DISCONNECTION_COMPLETE: + bt_connected = false; + break; + default: + break; + } } uint16_t bt_att_read_callback( - hci_con_handle_t connection_handle, - uint16_t att_handle, - uint16_t offset, - uint8_t * buffer, + hci_con_handle_t connection_handle, + uint16_t att_handle, + uint16_t offset, + uint8_t* buffer, uint16_t buffer_size) { printf("GATT Read %d\n", att_handle); @@ -71,11 +81,11 @@ uint16_t bt_att_read_callback( } int bt_att_write_callback( - hci_con_handle_t connection_handle, - uint16_t att_handle, - uint16_t transaction_mode, - uint16_t offset, - uint8_t *buffer, + hci_con_handle_t connection_handle, + uint16_t att_handle, + uint16_t transaction_mode, + uint16_t offset, + uint8_t* buffer, uint16_t buffer_size) { printf("GATT Write %d, %db: ", att_handle, buffer_size); @@ -83,13 +93,17 @@ int bt_att_write_callback( printf("%02x ", buffer[i]); putchar('\n'); - if (att_handle != ATT_CHARACTERISTIC_0000ffe1_0000_1000_8000_00805f9b34fb_01_VALUE_HANDLE) return 0; + if (att_handle != ATT_CHARACTERISTIC_0000ffe1_0000_1000_8000_00805f9b34fb_01_VALUE_HANDLE) + return 0; // Buttplug.io always sends // 0xa1, 0x08, 0x01, 0x00, 0x00, 0x00, 0x64, PERCENTAGE, 0x00, 0x64, 0xdf, 0x55, - if (buffer_size != 12) return 0; - if (memcmp(buffer, "\xA1\x08\x01\x00\x00\x00\x64", 7) != 0) return 0; - if (memcmp(buffer + 8, "\x00\x64\xDF\x55", 4) != 0) return 0; + if (buffer_size != 12) + return 0; + if (memcmp(buffer, "\xA1\x08\x01\x00\x00\x00\x64", 7) != 0) + return 0; + if (memcmp(buffer + 8, "\x00\x64\xDF\x55", 4) != 0) + return 0; // In case it somehow came unset bt_connected = true; diff --git a/src/vibe_bt.h b/src/vibe_bt.h index 23db0a1..01023c8 100644 --- a/src/vibe_bt.h +++ b/src/vibe_bt.h @@ -6,24 +6,24 @@ extern bool bt_connected; extern const uint8_t profile_data[]; void bt_packet_handler( - uint8_t packet_type, - uint16_t channel, - uint8_t *packet, + uint8_t packet_type, + uint16_t channel, + uint8_t* packet, uint16_t size); uint16_t bt_att_read_callback( - hci_con_handle_t connection_handle, - uint16_t att_handle, - uint16_t offset, - uint8_t * buffer, + hci_con_handle_t connection_handle, + uint16_t att_handle, + uint16_t offset, + uint8_t* buffer, uint16_t buffer_size); int bt_att_write_callback( - hci_con_handle_t connection_handle, - uint16_t att_handle, - uint16_t transaction_mode, - uint16_t offset, - uint8_t *buffer, + hci_con_handle_t connection_handle, + uint16_t att_handle, + uint16_t transaction_mode, + uint16_t offset, + uint8_t* buffer, uint16_t buffer_size); #endif