ESP32 Bluetooth example code convert to c++

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

ESP32 Bluetooth example code convert to c++

Postby zazas321 » Thu Sep 23, 2021 10:30 am

Hello. I am writing code for my project on ESP32. I mostly use c language but I put .cpp extension for all of my components. I want to know whether that is normal to do and whether that is not going to cause any issues?

The reason why I put .cpp for all of my components is because for one particular component, I use c++ code (classes) and I have found a lot of issues when trying to include this component and its header in other non cpp components. Therefore I changed all the extensions to .cpp and added:

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif


HEADER CODE

#ifdef __cplusplus
}
#endif

Inside all headers.


Now everything seemed to work fine. However, I am now trying to implement BLE funcionality for my device. I have used one of the example code provided by esp-idf and put this code inside a BLE_Custom component.


Initially, I had this Bluetooth component with .c extension and I could include it in my main.cpp and use it without any issues.

I am now trying to include a header from the cpp component to the bluetooth component and this is the error I am getting:

Code: Select all

In file included from ../components/BLE/BLE_custom.h:23,
                 from ../components/BLE/BLE_custom.c:3:
../components/Thermostat/Thermostat.h:18:1: error: unknown type name 'class'
 class Thermostat
 ^~~~~
../components/Thermostat/Thermostat.h:19:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 {


I have then tried to change the BLE_custom extension to .cpp and of course added the :

Code: Select all

#ifdef __cplusplus
extern "C" {
#endif

to BLE_custom.h

Now I am getting a lot of errors! :

Code: Select all

../components/BLE/BLE_custom.cpp:159:1: warning: missing initializer for member 'esp_ble_adv_params_t::peer_addr' [-Wmissing-field-initializers]
 };
 ^
../components/BLE/BLE_custom.cpp:159:1: warning: missing initializer for member 'esp_ble_adv_params_t::peer_addr_type' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::app_id' [-Wmissing-field-initializers]
 };
 ^
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::conn_id' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::service_handle' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::service_id' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::char_handle' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::char_uuid' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::perm' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::property' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::descr_handle' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:185:1: warning: missing initializer for member 'gatts_profile_inst::descr_uuid' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:262:1: sorry, unimplemented: non-trivial designated initializers not supported
 };
 ^
../components/BLE/BLE_custom.cpp:262:1: sorry, unimplemented: non-trivial designated initializers not supported
../components/BLE/BLE_custom.cpp:262:1: sorry, unimplemented: non-trivial designated initializers not supported
../components/BLE/BLE_custom.cpp: In function 'void gatts_profile_event_handler(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t*)':
../components/BLE/BLE_custom.cpp:499:32: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
                     char * c = "Hello world";
                                ^~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:58: warning: missing initializer for member 'esp_ble_conn_update_params_t::min_int' [-Wmissing-field-initializers]
             esp_ble_conn_update_params_t conn_params = {0};
                                                          ^
../components/BLE/BLE_custom.cpp:544:58: warning: missing initializer for member 'esp_ble_conn_update_params_t::max_int' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:544:58: warning: missing initializer for member 'esp_ble_conn_update_params_t::latency' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:544:58: warning: missing initializer for member 'esp_ble_conn_update_params_t::timeout' [-Wmissing-field-initializers]
../components/BLE/BLE_custom.cpp:554:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_DISCONNECT_EVT:
              ^~~~~~~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:558:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_CREAT_ATTR_TAB_EVT:{
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:573:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_STOP_EVT:
              ^~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:575:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_OPEN_EVT:
              ^~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:577:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_CANCEL_OPEN_EVT:
              ^~~~~~~~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:579:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_CLOSE_EVT:
              ^~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:581:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_LISTEN_EVT:
              ^~~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:583:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_CONGEST_EVT:
              ^~~~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:585:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_UNREG_EVT:
              ^~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:587:14: error: jump to case label [-fpermissive]
         case ESP_GATTS_DELETE_EVT:
              ^~~~~~~~~~~~~~~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:589:9: error: jump to case label [-fpermissive]
         default:
         ^~~~~~~
../components/BLE/BLE_custom.cpp:544:42: note:   crosses initialization of 'esp_ble_conn_update_params_t conn_params'
             esp_ble_conn_update_params_t conn_params = {0};
                                          ^~~~~~~~~~~
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_UNREG_EVT' not handled in switch [-Werror=switch]
     switch (event) {
            ^
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_CREATE_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_ADD_INCL_SRVC_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_ADD_CHAR_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_ADD_CHAR_DESCR_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_DELETE_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_STOP_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_DISCONNECT_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_OPEN_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_CANCEL_OPEN_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_CLOSE_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_LISTEN_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_CONGEST_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_RESPONSE_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_CREAT_ATTR_TAB_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_SET_ATTR_VAL_EVT' not handled in switch [-Werror=switch]
../components/BLE/BLE_custom.cpp:385:12: error: enumeration value 'ESP_GATTS_SEND_SERVICE_CHANGE_EVT' not handled in switch [-Werror=switch]


Can someone suggest me a way to include a c++ component to my Bluetooth component?

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 Bluetooth example code convert to c++

Postby chegewara » Fri Sep 24, 2021 9:38 pm

In file included from ../components/BLE/BLE_custom.h:23,
from ../components/BLE/BLE_custom.c:3:
../components/Thermostat/Thermostat.h:18:1: error: unknown type name 'class'
Do you have by any chance that file with .c extension?

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 99 guests