Skip to content

Commit

Permalink
esp32-s3: more changes
Browse files Browse the repository at this point in the history
This is still WIP.
UNI_ENABLE_BLE / UNI_ENABLE_BREDR constants are used
  • Loading branch information
ricardoquesada committed Apr 6, 2023
1 parent dcc77e2 commit b7d547c
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/components/bluepad32/include/uni_bt_bredr.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void uni_bt_bredr_scan_stop(void);
// Called from uni_hid_device_disconnect()
void uni_bt_bredr_disconnect(uni_bt_conn_t* conn);

void uni_bt_bredr_list_bonded_keys(void);
void uni_bt_bredr_delete_bonded_keys(void);
void uni_bt_bredr_setup(void);

Expand Down
1 change: 1 addition & 0 deletions src/components/bluepad32/include/uni_bt_le.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void uni_bt_le_scan_stop(void);
// Called from uni_hid_device_disconnect()
void uni_bt_le_disconnect(uni_bt_conn_t* conn);

void uni_bt_le_list_bonded_keys(void);
void uni_bt_le_delete_bonded_keys(void);
void uni_bt_le_setup(void);

Expand Down
15 changes: 15 additions & 0 deletions src/components/bluepad32/include/uni_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ limitations under the License.
#ifndef UNI_CONFIG_H
#define UNI_CONFIG_H

#include "sdkconfig.h"

#if defined(CONFIG_TARGET_LIBUSB)
// Linux + libusb
#define UNI_ENABLE_BREDR
#define UNI_ENABLE_BLE
#elif defined(CONFIG_IDF_TARGET_ESP32)
// ESP32
#define UNI_ENABLE_BREDR
#define UNI_ENABLE_BLE
#else
// ESP32-S3
#define UNI_ENABLE_BLE
#endif

// For more configurations, please look at the Kconfig file, or just do:
// "idf.py menuconfig" -> "Component config" -> "Bluepad32"

Expand Down
48 changes: 14 additions & 34 deletions src/components/bluepad32/uni_bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ static void on_l2cap_channel_opened(uint16_t channel, const uint8_t* packet, uin
logi("Probably GAP-security-related issues. Set GAP security to 2\n");
}
logi("Removing key for device: %s.\n", bd_addr_to_str(address));
#ifdef UNI_ENABLE_BREDR
// TODO: Move to uni_bt_bredr.c
gap_drop_link_key_for_bd_addr(device->conn.btaddr);
#endif // UNI_ENABLE_BREDR
uni_hid_device_disconnect(device);
uni_hid_device_delete(device);
/* 'device' is destroyed, don't use */
Expand Down Expand Up @@ -470,43 +473,13 @@ static void inquiry_remote_name_timeout_callback(btstack_timer_source_t* ts) {
}

static void bluetooth_del_keys(void) {
bd_addr_t addr;
link_key_t link_key;
link_key_type_t type;
btstack_link_key_iterator_t it;

int ok = gap_link_key_iterator_init(&it);
if (!ok) {
loge("Link key iterator not implemented\n");
return;
}

while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)) {
logi("Deleting key: %s - type %u - key: ", bd_addr_to_str(addr), (int)type);
printf_hexdump(link_key, 16);
gap_drop_link_key_for_bd_addr(addr);
}
gap_link_key_iterator_done(&it);
uni_bt_bredr_delete_bonded_keys();
uni_bt_le_delete_bonded_keys();
}

static void bluetooth_list_keys(void) {
bd_addr_t addr;
link_key_t link_key;
link_key_type_t type;
btstack_link_key_iterator_t it;

int ok = gap_link_key_iterator_init(&it);
if (!ok) {
loge("Link key iterator not implemented\n");
return;
}

logi("Bluetooth keys:\n");
while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)) {
logi("%s - type %u - key: ", bd_addr_to_str(addr), (int)type);
printf_hexdump(link_key, 16);
}
gap_link_key_iterator_done(&it);
uni_bt_bredr_list_bonded_keys();
uni_bt_le_list_bonded_keys();
}

static void enable_new_connections(bool enabled) {
Expand Down Expand Up @@ -647,6 +620,8 @@ void uni_bluetooth_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t
break;
}
case HCI_EVENT_PIN_CODE_REQUEST: {
#ifdef UNI_ENABLE_BREDR
// TODO: Move to uni_bt_bredr.c
bool is_mouse = false;

logi("--> HCI_EVENT_PIN_CODE_REQUEST\n");
Expand Down Expand Up @@ -682,6 +657,7 @@ void uni_bluetooth_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t
printf_hexdump(pin_code, sizeof(pin_code));
gap_pin_code_response_binary(event_addr, pin_code, sizeof(pin_code));
}
#endif // UNI_ENABLE_BREDR
break;
}
case HCI_EVENT_USER_CONFIRMATION_REQUEST:
Expand Down Expand Up @@ -822,6 +798,9 @@ void uni_bluetooth_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t
}

void uni_bluetooth_process_fsm(uni_hid_device_t* d) {
#ifdef UNI_ENABLE_BREDR
// TODO: Move to uni_bt_bredr.c

// A device can be in the following states:
// - discovered (which might or might not have a name)
// - received incoming connection
Expand Down Expand Up @@ -947,4 +926,5 @@ void uni_bluetooth_process_fsm(uni_hid_device_t* d) {
}
}
}
#endif // UNI_ENABLE_BREDR
}
37 changes: 37 additions & 0 deletions src/components/bluepad32/uni_bt_bredr.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,37 @@ limitations under the License.
#include "uni_bt_defines.h"
#include "uni_bt_sdp.h"
#include "uni_bt_setup.h"
#include "uni_config.h"
#include "uni_log.h"
#include "uni_platform.h"

static bool bt_bredr_enabled = true;

void uni_bt_bredr_scan_start(void) {
#ifdef UNI_ENABLE_BREDR
uint8_t status;

status =
gap_inquiry_periodic_start(uni_bt_setup_get_gap_inquiry_lenght(), uni_bt_setup_get_gap_max_periodic_lenght(),
uni_bt_setup_get_gap_min_periodic_lenght());
if (status)
loge("Failed to start period inquiry, error=0x%02x\n", status);
#endif // UNI_ENABLE_BREDR
}

void uni_bt_bredr_scan_stop(void) {
#ifdef UNI_ENABLE_BREDR
uint8_t status;

status = gap_inquiry_stop();
if (status)
loge("Error: cannot stop inquiry (0x%02x), please try again\n", status);
#endif // UNI_ENABLE_BREDR
}

// Called from uni_hid_device_disconnect()
void uni_bt_bredr_disconnect(uni_bt_conn_t* conn) {
#ifdef UNI_ENABLE_BREDR
if (gap_get_connection_type(conn->handle) != GAP_CONNECTION_INVALID) {
gap_disconnect(conn->handle);
conn->handle = UNI_BT_CONN_HANDLE_INVALID;
Expand All @@ -70,9 +76,11 @@ void uni_bt_bredr_disconnect(uni_bt_conn_t* conn) {
conn->interrupt_cid = 0;
}
}
#endif // UNI_ENABLE_BREDR
}

void uni_bt_bredr_delete_bonded_keys(void) {
#ifdef UNI_ENABLE_BREDR
bd_addr_t addr;
link_key_t link_key;
link_key_type_t type;
Expand All @@ -98,9 +106,33 @@ void uni_bt_bredr_delete_bonded_keys(void) {

logi(".\n");
gap_link_key_iterator_done(&it);
#endif // UNI_ENABLE_BREDR
}

void uni_bt_bredr_list_bonded_keys(void) {
#ifdef UNI_ENABLE_BREDR
bd_addr_t addr;
link_key_t link_key;
link_key_type_t type;
btstack_link_key_iterator_t it;

int ok = gap_link_key_iterator_init(&it);
if (!ok) {
loge("Link key iterator not implemented\n");
return;
}

logi("Bluetooth BR/EDR keys:\n");
while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)) {
logi("%s - type %u - key: ", bd_addr_to_str(addr), (int)type);
printf_hexdump(link_key, 16);
}
gap_link_key_iterator_done(&it);
#endif // UNI_ENABLE_BREDR
}

void uni_bt_bredr_setup(void) {
#ifdef UNI_ENABLE_BREDR
int security_level = uni_bt_setup_get_gap_security_level();
gap_set_security_level(security_level);

Expand All @@ -127,6 +159,7 @@ void uni_bt_bredr_setup(void) {

// btstack_stdin_setup(stdin_process);
hci_set_master_slave_policy(HCI_ROLE_MASTER);
#endif // UNI_ENABLE_BREDR
}

void uni_bt_bredr_set_enabled(bool enabled) {
Expand All @@ -136,5 +169,9 @@ void uni_bt_bredr_set_enabled(bool enabled) {
}

bool uni_bt_bredr_is_enabled(void) {
#ifdef UNI_ENABLE_BREDR
return bt_bredr_enabled;
#else
return false;
#endif
}
22 changes: 22 additions & 0 deletions src/components/bluepad32/uni_bt_le.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,28 @@ void uni_bt_le_on_gap_event_advertising_report(const uint8_t* packet, uint16_t s
hog_connect(addr, addr_type);
}

void uni_bt_le_list_bonded_keys(void) {
bd_addr_t entry_address;
int i;

if (!uni_bt_le_is_enabled())
return;

logi("Bluetooth LE keys:\n");

for (i = 0; i < le_device_db_max_count(); i++) {
int entry_address_type = (int)BD_ADDR_TYPE_UNKNOWN;
le_device_db_info(i, &entry_address_type, entry_address, NULL);

// skip unused entries
if (entry_address_type == (int)BD_ADDR_TYPE_UNKNOWN)
continue;

logi("%s - type %u\n", bd_addr_to_str(entry_address), (int)entry_address_type);
}
logi(".\n");
}

void uni_bt_le_delete_bonded_keys(void) {
bd_addr_t entry_address;
int i;
Expand Down
18 changes: 18 additions & 0 deletions src/components/bluepad32/uni_bt_sdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
#include <btstack.h>
#include <inttypes.h>

#include "sdkconfig.h"
#include "uni_bluetooth.h"
#include "uni_common.h"
#include "uni_config.h"
#include "uni_log.h"

#define MAX_ATTRIBUTE_VALUE_SIZE 512 // Apparently PS4 has a 470-bytes report
Expand All @@ -75,6 +77,7 @@ static uint8_t device_id_sdp_service_buffer[100];

// HID results: HID descriptor, PSM interrupt, PSM control, etc.
static void handle_sdp_hid_query_result(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
#ifdef UNI_ENABLE_BREDR
ARG_UNUSED(packet_type);
ARG_UNUSED(channel);
ARG_UNUSED(size);
Expand Down Expand Up @@ -131,10 +134,12 @@ static void handle_sdp_hid_query_result(uint8_t packet_type, uint16_t channel, u
default:
break;
}
#endif // UNI_ENABLE_BREDR
}

// Device ID results: Vendor ID, Product ID, Version, etc...
static void handle_sdp_pid_query_result(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
#ifdef UNI_ENABLE_BREDR
ARG_UNUSED(packet_type);
ARG_UNUSED(channel);
ARG_UNUSED(size);
Expand Down Expand Up @@ -188,9 +193,11 @@ static void handle_sdp_pid_query_result(uint8_t packet_type, uint16_t channel, u
logd("TODO: handle_sdp_pid_query_result. switch->default triggered\n");
break;
}
#endif // UNI_ENABLE_BREDR
}

static void sdp_query_timeout(btstack_timer_source_t* ts) {
#ifdef UNI_ENABLE_BREDR
loge("<------- sdp_query_timeout()\n");
uni_hid_device_t* d = btstack_run_loop_get_timer_context(ts);
if (!sdp_device) {
Expand All @@ -205,11 +212,13 @@ static void sdp_query_timeout(btstack_timer_source_t* ts) {

logi("Failed to query SDP for %s, timeout\n", bd_addr_to_str(d->conn.btaddr));
sdp_device = NULL;
#endif // UNI_ENABLE_BREDR
}

// Public functions

void uni_bt_sdp_query_start(uni_hid_device_t* d) {
#ifdef UNI_ENABLE_BREDR
loge("-----------> sdp_query_start()\n");
// Needed for the SDP query since it only supports one SDP query at the time.
if (sdp_device != NULL) {
Expand All @@ -227,17 +236,21 @@ void uni_bt_sdp_query_start(uni_hid_device_t* d) {
btstack_run_loop_add_timer(&sdp_query_timer);

uni_bt_sdp_query_start_vid_pid(d);
#endif // UNI_ENABLE_BREDR
}

void uni_bt_sdp_query_end(uni_hid_device_t* d) {
#ifdef UNI_ENABLE_BREDR
loge("<----------- sdp_query_end()\n");
uni_bt_conn_set_state(&d->conn, UNI_BT_CONN_STATE_SDP_HID_DESCRIPTOR_FETCHED);
sdp_device = NULL;
btstack_run_loop_remove_timer(&sdp_query_timer);
uni_bluetooth_process_fsm(d);
#endif // UNI_ENABLE_BREDR
}

void uni_bt_sdp_query_start_vid_pid(uni_hid_device_t* d) {
#ifdef UNI_ENABLE_BREDR
logi("Starting SDP VID/PID query for %s\n", bd_addr_to_str(d->conn.btaddr));

uni_bt_conn_set_state(&d->conn, UNI_BT_CONN_STATE_SDP_VENDOR_REQUESTED);
Expand All @@ -250,9 +263,11 @@ void uni_bt_sdp_query_start_vid_pid(uni_hid_device_t* d) {
/* 'd' is destroyed after this call, don't use it */
return;
}
#endif // UNI_ENABLE_BREDR
}

void uni_bt_sdp_query_start_hid_descriptor(uni_hid_device_t* d) {
#ifdef UNI_ENABLE_BREDR
if (!uni_hid_device_does_require_hid_descriptor(d)) {
logi("Device %s does not need a HID descriptor, skipping query.\n", bd_addr_to_str(d->conn.btaddr));
uni_bt_sdp_query_end(d);
Expand All @@ -277,9 +292,11 @@ void uni_bt_sdp_query_start_hid_descriptor(uni_hid_device_t* d) {
uni_hid_device_delete(d);
/* 'd'' is destroyed after this call, don't use it */
}
#endif // UNI_ENABLE_BREDR
}

void uni_bt_sdp_server_init() {
#ifdef UNI_ENABLE_BREDR
// Only initialize the SDP record. Just needed for DualShock/DualSense to have
// a successful reconnect.
sdp_init();
Expand All @@ -288,4 +305,5 @@ void uni_bt_sdp_server_init() {
BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1, 1);
logi("Device ID SDP service record size: %u\n", de_get_len((uint8_t*)device_id_sdp_service_buffer));
sdp_register_service(device_id_sdp_service_buffer);
#endif // UNI_ENABLE_BREDR
}
6 changes: 3 additions & 3 deletions src/components/bluepad32/uni_platform_nina.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ static int spi_transfer(uint8_t out[], uint8_t in[], size_t len) {

spi_slave_transaction_t slv_trans = {.length = len * 8, .trans_len = 0, .tx_buffer = out, .rx_buffer = in};

esp_err_t ret = spi_slave_queue_trans(VSPI_HOST, &slv_trans, portMAX_DELAY);
esp_err_t ret = spi_slave_queue_trans(SPI3_HOST, &slv_trans, portMAX_DELAY);
if (ret != ESP_OK)
return -1;

xSemaphoreTake(_ready_semaphore, portMAX_DELAY);
gpio_set_level(GPIO_READY, 0);

ret = spi_slave_get_trans_result(VSPI_HOST, &slv_ret_trans, portMAX_DELAY);
ret = spi_slave_get_trans_result(SPI3_HOST, &slv_ret_trans, portMAX_DELAY);
if (ret != ESP_OK)
return -1;

Expand Down Expand Up @@ -903,7 +903,7 @@ static void spi_main_loop(void* arg) {
gpio_set_pull_mode(GPIO_SCLK, GPIO_PULLDOWN_ONLY);
gpio_set_pull_mode(GPIO_CS, GPIO_PULLUP_ONLY);

esp_err_t ret = spi_slave_initialize(VSPI_HOST, &buscfg, &slvcfg, DMA_CHANNEL);
esp_err_t ret = spi_slave_initialize(SPI3_HOST, &buscfg, &slvcfg, DMA_CHANNEL);
assert(ret == ESP_OK);

// Must be modulo 4 and word aligned.
Expand Down
Loading

0 comments on commit b7d547c

Please sign in to comment.