pmdroid / tesla-ble

This CPP library facilitates direct communication with Tesla vehicles via the BLE API.
MIT License
16 stars 5 forks source link

Support for mbedtls v2.x #1

Closed robin-thoni closed 3 months ago

robin-thoni commented 9 months ago

I'm trying to use your library in ESPHome, unfortunately, the mbedtls version included in the Espressif Arduino framework (/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/mbedtls/mbedtls) seems to be 2.x, which is not compatible:

src/esphome/components/tesla_ble/client.cpp: In member function 'int TeslaBLE::Client::LoadPrivateKey(const uint8_t*, size_t)':
src/esphome/components/tesla_ble/client.cpp:92:65: error: too many arguments to function 'int mbedtls_pk_parse_key(mbedtls_pk_context*, const unsigned char*, size_t, const unsigned char*, size_t)'
       password, 0, mbedtls_ctr_drbg_random, &this->drbg_context_);
                                                                 ^
In file included from src/esphome/components/tesla_ble/client.h:10,
                 from src/esphome/components/tesla_ble/client.cpp:1:
/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/mbedtls/mbedtls/include/mbedtls/pk.h:712:5: note: declared here
 int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
     ^~~~~~~~~~~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp: In member function 'int TeslaBLE::Client::GeneratePublicKey()':
src/esphome/components/tesla_ble/client.cpp:131:51: error: 'mbedtls_ecp_keypair' {aka 'struct mbedtls_ecp_keypair'} has no member named 'private_grp'
       &mbedtls_pk_ec(this->private_key_context_)->private_grp,
                                                   ^~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:132:51: error: 'mbedtls_ecp_keypair' {aka 'struct mbedtls_ecp_keypair'} has no member named 'private_Q'
       &mbedtls_pk_ec(this->private_key_context_)->private_Q,
                                                   ^~~~~~~~~
src/esphome/components/tesla_ble/client.cpp: In member function 'int TeslaBLE::Client::GenerateKeyId()':
src/esphome/components/tesla_ble/client.cpp:153:54: error: void value not ignored as it ought to be
   int return_code = mbedtls_sha1_starts(&sha1_context);
                                                      ^
src/esphome/components/tesla_ble/client.cpp:160:59: error: void value not ignored as it ought to be
                                     this->public_key_size_);
                                                           ^
src/esphome/components/tesla_ble/client.cpp:167:58: error: void value not ignored as it ought to be
   return_code = mbedtls_sha1_finish(&sha1_context, buffer);
                                                          ^
src/esphome/components/tesla_ble/client.cpp: In member function 'int TeslaBLE::Client::LoadTeslaKey(const uint8_t*, size_t)':
src/esphome/components/tesla_ble/client.cpp:193:62: error: 'mbedtls_ecp_keypair' {aka 'struct mbedtls_ecp_keypair'} has no member named 'private_grp'
   int return_code = mbedtls_ecp_group_load(&this->tesla_key_.private_grp,
                                                              ^~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:201:25: error: 'mbedtls_ecp_keypair' {aka 'struct mbedtls_ecp_keypair'} has no member named 'private_grp'
       &this->tesla_key_.private_grp, &this->tesla_key_.private_Q,
                         ^~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:201:56: error: 'mbedtls_ecp_keypair' {aka 'struct mbedtls_ecp_keypair'} has no member named 'private_Q'
       &this->tesla_key_.private_grp, &this->tesla_key_.private_Q,
                                                        ^~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:241:50: error: void value not ignored as it ought to be
   return_code = mbedtls_sha1_starts(&sha1_context);
                                                  ^
src/esphome/components/tesla_ble/client.cpp:248:62: error: void value not ignored as it ought to be
                                     temp_shared_secret_length);
                                                              ^
src/esphome/components/tesla_ble/client.cpp:254:72: error: void value not ignored as it ought to be
   return_code = mbedtls_sha1_finish(&sha1_context, this->shared_secret_);
                                                                        ^
src/esphome/components/tesla_ble/client.cpp: In member function 'int TeslaBLE::Client::Encrypt(unsigned char*, size_t, unsigned char*, size_t, size_t*, unsigned char*)':
src/esphome/components/tesla_ble/client.cpp:297:49: error: too few arguments to function 'int mbedtls_gcm_starts(mbedtls_gcm_context*, int, const unsigned char*, size_t, const unsigned char*, size_t)'
                                    sizeof(nonce));
                                                 ^
In file included from src/esphome/components/tesla_ble/client.cpp:7:
/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/mbedtls/mbedtls/include/mbedtls/gcm.h:244:5: note: declared here
 int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
     ^~~~~~~~~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:304:40: error: invalid conversion from 'unsigned char*' to 'size_t' {aka 'unsigned int'} [-fpermissive]
       mbedtls_gcm_update(&aes_context, input_buffer, input_buffer_length,
                                        ^~~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:304:54: error: invalid conversion from 'size_t' {aka 'unsigned int'} to 'const unsigned char*' [-fpermissive]
       mbedtls_gcm_update(&aes_context, input_buffer, input_buffer_length,
                                                      ^~~~~~~~~~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:305:76: error: too many arguments to function 'int mbedtls_gcm_update(mbedtls_gcm_context*, size_t, const unsigned char*, unsigned char*)'
                          output_buffer, output_buffer_length, output_length);
                                                                            ^
In file included from src/esphome/components/tesla_ble/client.cpp:7:
/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/mbedtls/mbedtls/include/mbedtls/gcm.h:276:5: note: declared here
 int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
     ^~~~~~~~~~~~~~~~~~
src/esphome/components/tesla_ble/client.cpp:317:69: error: too many arguments to function 'int mbedtls_gcm_finish(mbedtls_gcm_context*, unsigned char*, size_t)'
                          &finish_buffer_length, signature_buffer, 16);
                                                                     ^
In file included from src/esphome/components/tesla_ble/client.cpp:7:
/data/cache/platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/mbedtls/mbedtls/include/mbedtls/gcm.h:297:5: note: declared here
 int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
     ^~~~~~~~~~~~~~~~~~
*** [.pioenvs/watchy-01/src/esphome/components/tesla_ble/client.cpp.o] Error 1

I quickly had a look at it, but some changes do not seem that trivial to me. Any chance you could make the library compatible with both versions (2.x and 3.x)? I doubt adding 3.x on top of 2.x in my work tree would do any good.

Thanks!

MrBLJ commented 4 months ago

@robin-thoni sorry to hijack your thread, but did you manage to fix your issue ? With the recent limitation imposed by Tesla on the number of command we can send using the API each day, BLE support would be most welcome.

robin-thoni commented 4 months ago

I managed to make it build inside ESPHome w/ a few tweaks, however, the ESP never bothered to connect to the car, so I'm not sure my mod is correct :/

MrBLJ commented 4 months ago

I managed to make it build inside ESPHome w/ a few tweaks, however, the ESP never bothered to connect to the car, so I'm not sure my mod is correct :/

Thanks a lot for you reply. Would you mind sharing your work ?

robin-thoni commented 4 months ago

Here it is: https://github.com/robin-thoni/tesla-ble-esphome

MrBLJ commented 4 months ago

Here it is: https://github.com/robin-thoni/tesla-ble-esphome

Thanks a lot. Would it be possible to describe what you achieved, and the issues that you faced ?

robin-thoni commented 4 months ago

The current blocker is that the ESP doesn't want to connect to the car, for some reason, it's like it's ignoring it. I didn't do much test yet to understand why.

robin-thoni commented 3 months ago

Closing since I got it working: https://github.com/esphome/feature-requests/issues/2630#issuecomment-2210962105