btstack with pico w configuration

283 Views Asked by At

I'm having trouble compiling my program that is intended to send and receive strings over bluetooth. From the documentation, I know for a fact that btstack_run_loop_embedded_get_instance() exists, but i get the error: 'btstack_run_loop_embedded_get_instance' was not declared in this scope.

I've been reading the documentation and asking chatgpt to help me understand it, and to my knowledge, bluetooth is built upon protocols like gatt etc, but specifically SPP, which is what I care about because that essentially is a serial port over bluetooth? Trying to compile gave me an error about missing btstack_config.h which is missing on purpose, because as the developer I'm supposed to configure that to my needs. I ripped the btstack_config.h from the pico w examples, but it is giving me the error: 'btstack_run_loop_embedded_get_instance' was not declared in this scope.

It seems like I'm not including btstack from the pico-sdk correctly.

// main.cpp
#include <iostream>
#include <cstring>
#include "btstack.h" // Include the BTstack header file
#include <btstack_run_loop_embedded.h>
#include <btstack_uart_block.h>
#include <hci_transport_h4.h>
#include "pico/cyw43_arch.h"
#include "pico/btstack_cyw43.h"
#include "btstack_memory.h"
#include "hci.h"
#include "rfcomm.h"
// Callback function for receiving data
void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);

int main() {
    // Initialize BTstack
    btstack_memory_init();
    btstack_run_loop_init(btstack_run_loop_embedded_get_instance());

    // Set up Bluetooth module
    const hci_transport_t *transport = hci_transport_h4_instance(btstack_uart_block_embedded_instance());
    remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
    hci_init((hci_transport_t *)transport, NULL, remote_device_db, NULL);
    hci_set_class_of_device(0x000508); // Set device class to 'Uncategorized'

    // Initialize L2CAP
    l2cap_init();

    // Initialize RFCOMM
    rfcomm_init();

    // Initialize SDP
    sdp_init();

    // Setup SPP service
    rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);

    // Set local device name
    gap_set_local_name("BTstack SPP Server");

    // Configure Bluetooth device
    hci_power_control(HCI_POWER_ON);

    // Run the event loop
    btstack_run_loop_execute();

    return 0;
}

// Callback function for receiving data
void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
    switch (packet_type) {
        case HCI_EVENT_PACKET:
            switch (packet[0]) {
                case RFCOMM_EVENT_INCOMING_CONNECTION:
                    // Accept incoming connection
                    rfcomm_event_incoming_connection_get_bd_addr(packet, remote_addr);
                    rfcomm_accept_connection(channel);
                    break;
                case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
                    if (packet[2]) {
                        std::cerr << "Connection failed" << std::endl;
                    } else {
                        std::cout << "Connection established" << std::endl;

                        // Send a string over Bluetooth
                        const char *message = "Hello, Bluetooth!";
                        rfcomm_send_internal(channel, (uint8_t *) message, strlen(message));
                    }
                    break;
                case RFCOMM_EVENT_CHANNEL_CLOSED:
                    std::cout << "Connection closed" << std::endl;
                    break;
                case RFCOMM_EVENT_DATA_PACKET:
                    // Received data
                    std::cout << "Received: ";
                    for (int i = 0; i < size; i++) {
                        std::cout << packet[i];
                    }
                    std::cout << std::endl;
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
}

//CmakeLists.txt
cmake_minimum_required(VERSION 3.13)

#include(pico_sdk_import.cmake)
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
include($ENV{PICO_EXTRAS_PATH}/external/pico_extras_import.cmake)

project(bt_uart_example)

set(PICO_BOARD pico_w)
pico_sdk_init()
add_executable(${PROJECT_NAME}
    main.cpp
)

# Add BTstack include directories

include_directories(/home/pi/pico/pico-sdk/lib/btstack/src/classic)
include_directories(/home/pi/pico/pico-sdk/lib/btstack/src)
include_directories(/home/pi/pico/pico-sdk/lib/btstack/platform/embedded)
# Add other include directories if needed

# Add BTstack source files
#set(BTSTACK_SOURCES
#    your_project_path/btstack/src/...   # List the necessary source files
#    your_project_path/btstack/platform/embedded/...   # List platform-specific source files
#)

target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}
)
#target_compile_definitions(CYW43_ENABLE_BLUETOOTH=1)

target_compile_options(${PROJECT_NAME} PRIVATE -std=c++17)

target_link_libraries(${PROJECT_NAME} PRIVATE
    pico_stdlib
    pico_btstack_ble # Needed for ble
    pico_btstack_cyw43 # Needed for ble
    pico_cyw43_arch_none # Needed for pico_w
    #pico_bluetooth
    hardware_adc
)
pico_enable_stdio_usb(${PROJECT_NAME} 1)
pico_enable_stdio_uart(${PROJECT_NAME} 0)

pico_add_extra_outputs(${PROJECT_NAME})

// btstack_config.h
#ifndef _PICO_BTSTACK_BTSTACK_CONFIG_H
#define _PICO_BTSTACK_BTSTACK_CONFIG_H

#ifndef ENABLE_BLE
#error Please link to pico_btstack_ble
#endif

// BTstack features that can be enabled
#define ENABLE_LE_PERIPHERAL
#define ENABLE_LOG_INFO
#define ENABLE_LOG_ERROR
#define ENABLE_PRINTF_HEXDUMP

// for the client
#if RUNNING_AS_CLIENT
#define ENABLE_LE_CENTRAL
#define MAX_NR_GATT_CLIENTS 1
#else
#define MAX_NR_GATT_CLIENTS 0
#endif

// BTstack configuration. buffers, sizes, ...
#define HCI_OUTGOING_PRE_BUFFER_SIZE 4
#define HCI_ACL_PAYLOAD_SIZE (255 + 4)
#define HCI_ACL_CHUNK_SIZE_ALIGNMENT 4
#define MAX_NR_HCI_CONNECTIONS 1
#define MAX_NR_SM_LOOKUP_ENTRIES 3
#define MAX_NR_WHITELIST_ENTRIES 16
#define MAX_NR_LE_DEVICE_DB_ENTRIES 16

// Limit number of ACL/SCO Buffer to use by stack to avoid cyw43 shared bus overrun
#define MAX_NR_CONTROLLER_ACL_BUFFERS 3
#define MAX_NR_CONTROLLER_SCO_PACKETS 3

// 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_NUM 3
#define HCI_HOST_SCO_PACKET_LEN 120
#define HCI_HOST_SCO_PACKET_NUM 3

// Link Key DB and LE Device DB using TLV on top of Flash Sector interface
#define NVM_NUM_DEVICE_DB_ENTRIES 16
#define NVM_NUM_LINK_KEYS 16

// We don't give btstack a malloc, so use a fixed-size ATT DB.
#define MAX_ATT_DB_SIZE 512

// BTstack HAL configuration
#define HAVE_EMBEDDED_TIME_MS
#define HAVE_EMBEDDED_TICK
#define HAVE_EMBEDDED_TICK_ELAPSED
#define HAVE_EMBEDDED_SLEEP
// map btstack_assert onto Pico SDK assert()
#define HAVE_ASSERT
// Some USB dongles take longer to respond to HCI reset (e.g. BCM20702A).
#define HCI_RESET_RESEND_TIMEOUT_MS 1000
#define ENABLE_SOFTWARE_AES128
#define ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONS

#endif // MICROPY_INCLUDED_EXTMOD_BTSTACK_BTSTACK_CONFIG_H

make output:

/home/pi/pico/phonk/branch5/main.cpp: In function 'int main()':
/home/pi/pico/phonk/branch5/main.cpp:22:5: error: 'remote_device_db_t' was not declared in this scope
     remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
     ^~~~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:22:5: note: suggested alternative: 'le_device_db_add'
     remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
     ^~~~~~~~~~~~~~~~~~
     le_device_db_add
/home/pi/pico/phonk/branch5/main.cpp:22:25: error: 'remote_device_db' was not declared in this scope
     remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
                         ^~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:22:25: note: suggested alternative: 'rfcomm_service_t'
     remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
                         ^~~~~~~~~~~~~~~~
                         rfcomm_service_t
/home/pi/pico/phonk/branch5/main.cpp:22:65: error: expected primary-expression before ')' token
     remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
                                                                 ^
/home/pi/pico/phonk/branch5/main.cpp:22:68: error: 'remote_device_db_memory' was not declared in this scope
     remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
                                                                    ^~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:22:68: note: suggested alternative: 'le_device_db_remove'
     remote_device_db_t *remote_device_db = (remote_device_db_t *) &remote_device_db_memory;
                                                                    ^~~~~~~~~~~~~~~~~~~~~~~
                                                                    le_device_db_remove
/home/pi/pico/phonk/branch5/main.cpp:24:5: error: 'hci_set_class_of_device' was not declared in this scope
     hci_set_class_of_device(0x000508); // Set device class to 'Uncategorized'
     ^~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:24:5: note: suggested alternative: 'gap_set_class_of_device'
     hci_set_class_of_device(0x000508); // Set device class to 'Uncategorized'
     ^~~~~~~~~~~~~~~~~~~~~~~
     gap_set_class_of_device
/home/pi/pico/phonk/branch5/main.cpp:33:5: error: 'sdp_init' was not declared in this scope
     sdp_init();
     ^~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:33:5: note: suggested alternative: 'sm_init'
     sdp_init();
     ^~~~~~~~
     sm_init
/home/pi/pico/phonk/branch5/main.cpp:36:45: error: 'RFCOMM_SERVER_CHANNEL' was not declared in this scope
     rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
                                             ^~~~~~~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:36:45: note: suggested alternative: 'RFCOMM_CREATE_CHANNEL'
     rfcomm_register_service(packet_handler, RFCOMM_SERVER_CHANNEL, 0xffff);
                                             ^~~~~~~~~~~~~~~~~~~~~
                                             RFCOMM_CREATE_CHANNEL
/home/pi/pico/phonk/branch5/main.cpp: In function 'void packet_handler(uint8_t, uint16_t, uint8_t*, uint16_t)':
/home/pi/pico/phonk/branch5/main.cpp:57:74: error: 'remote_addr' was not declared in this scope
                     rfcomm_event_incoming_connection_get_bd_addr(packet, remote_addr);
                                                                          ^~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:57:74: note: suggested alternative: '_remove_r'
                     rfcomm_event_incoming_connection_get_bd_addr(packet, remote_addr);
                                                                          ^~~~~~~~~~~
                                                                          _remove_r
/home/pi/pico/phonk/branch5/main.cpp:60:22: error: 'RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE' was not declared in this scope
                 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:60:22: note: suggested alternative: 'RFCOMM_EVENT_CHANNEL_OPENED'
                 case RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE:
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                      RFCOMM_EVENT_CHANNEL_OPENED
/home/pi/pico/phonk/branch5/main.cpp:68:25: error: 'rfcomm_send_internal' was not declared in this scope
                         rfcomm_send_internal(channel, (uint8_t *) message, strlen(message));
                         ^~~~~~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:68:25: note: suggested alternative: 'rfcomm_send_prepared'
                         rfcomm_send_internal(channel, (uint8_t *) message, strlen(message));
                         ^~~~~~~~~~~~~~~~~~~~
                         rfcomm_send_prepared
/home/pi/pico/phonk/branch5/main.cpp:74:22: error: 'RFCOMM_EVENT_DATA_PACKET' was not declared in this scope
                 case RFCOMM_EVENT_DATA_PACKET:
                      ^~~~~~~~~~~~~~~~~~~~~~~~
/home/pi/pico/phonk/branch5/main.cpp:74:22: note: suggested alternative: 'RFCOMM_DATA_PACKET'
                 case RFCOMM_EVENT_DATA_PACKET:
                      ^~~~~~~~~~~~~~~~~~~~~~~~
                      RFCOMM_DATA_PACKET
make[2]: *** [CMakeFiles/bt_uart_example.dir/build.make:63: CMakeFiles/bt_uart_example.dir/main.cpp.obj] Error 1
make[1]: *** [CMakeFiles/Makefile2:2181: CMakeFiles/bt_uart_example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

0

There are 0 best solutions below