From a8b2d58f88d64ad7cb07a8d90b8b4e5afe752c51 Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 01/10] Makefile: fix missing out dir in generate_raw --- proto/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/proto/Makefile b/proto/Makefile index 15de7af..e32cd12 100644 --- a/proto/Makefile +++ b/proto/Makefile @@ -1,5 +1,6 @@ generate_raw: - nanopb_generator *.proto --output-dir=out/ + mkdir -p out + nanopb_generator *.proto --output-dir=out/ fix: sed '/google\/protobuf\/timestamp/d' out/car_server.pb.h > out/car_server.pb.h.tmp From a55076558c9087fa8bdc4e5ee4151199924158a5 Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 02/10] add buildVCSECInformationRequestMessage --- include/client.h | 4 ++++ src/client.cpp | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/client.h b/include/client.h index e0cbe7d..93665fb 100644 --- a/include/client.h +++ b/include/client.h @@ -142,6 +142,10 @@ namespace TeslaBLE int buildVCSECActionMessage(const VCSEC_RKEAction_E action, pb_byte_t *output_buffer, size_t *output_length); + int buildVCSECInformationRequestMessage(VCSEC_InformationRequestType request_type, + pb_byte_t *output_buffer, + size_t *output_length, + uint32_t key_slot = 0); int buildCarServerActionMessage(const CarServer_VehicleAction *vehicle_action, pb_byte_t *output_buffer, diff --git a/src/client.cpp b/src/client.cpp index 11905dd..ce9d293 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -960,7 +960,7 @@ namespace TeslaBLE bool encryptPayload) { pb_byte_t payload_buffer; - size_t payload_length = 0; + size_t payload_length; printf("message: %p\n", message); printf("message.which_sub_message: %d\n", message->which_sub_message); pb_encode_fields(&payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, &message); @@ -1182,6 +1182,39 @@ namespace TeslaBLE return 0; } + int Client::buildVCSECInformationRequestMessage(VCSEC_InformationRequestType request_type, + pb_byte_t *output_buffer, + size_t *output_length, + uint32_t key_slot) + { + VCSEC_InformationRequest information_request = VCSEC_InformationRequest_init_default; + information_request.informationRequestType = request_type; + + if (key_slot != 0xFFFFFFFF) + { + printf("Adding key slot info"); + information_request.which_key = VCSEC_InformationRequest_slot_tag; + information_request.key.slot = key_slot; + } + + VCSEC_UnsignedMessage unsigned_message = VCSEC_UnsignedMessage_init_default; + unsigned_message.which_sub_message = VCSEC_UnsignedMessage_InformationRequest_tag; + unsigned_message.sub_message.InformationRequest = information_request; + + + size_t universal_encode_buffer_size = this->MAX_BLE_MESSAGE_SIZE - 2; + pb_byte_t universal_encode_buffer[universal_encode_buffer_size]; + int status = this->buildUnsignedMessagePayload(&unsigned_message, universal_encode_buffer, &universal_encode_buffer_size, false); + if (status != 0) + { + printf("Failed to build unsigned message\n"); + return status; + } + this->prependLength(universal_encode_buffer, universal_encode_buffer_size, + output_buffer, output_length); + return 0; + } + int Client::buildWakeVehicleMessage(pb_byte_t *output_buffer, size_t *output_length) { From d2b8d1fc32ac0392ffb755820eaa855964192131 Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 03/10] example: fix segmentation fault --- examples/simple/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple/main.cpp b/examples/simple/main.cpp index ab7ef34..3075c1d 100644 --- a/examples/simple/main.cpp +++ b/examples/simple/main.cpp @@ -29,7 +29,7 @@ int main() printf("Failed create private key\n"); } - unsigned char private_key_buffer[sizeof MOCK_PRIVATE_KEY]; + unsigned char private_key_buffer[sizeof MOCK_PRIVATE_KEY + 1]; size_t private_key_length; client.getPrivateKey(private_key_buffer, sizeof(private_key_buffer), &private_key_length); From 5fafeb639e217bc810f276c0d831439fb3503d93 Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 04/10] example: handle and return on errors --- examples/simple/main.cpp | 54 +++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/examples/simple/main.cpp b/examples/simple/main.cpp index 3075c1d..a34b5a1 100644 --- a/examples/simple/main.cpp +++ b/examples/simple/main.cpp @@ -31,8 +31,12 @@ int main() unsigned char private_key_buffer[sizeof MOCK_PRIVATE_KEY + 1]; size_t private_key_length; - client.getPrivateKey(private_key_buffer, sizeof(private_key_buffer), - &private_key_length); + status = client.getPrivateKey(private_key_buffer, sizeof(private_key_buffer), + &private_key_length); + if (status != 0) + { + printf("Failed to get private key\n"); + } printf("Private key length: %d\n", private_key_length); printf("Private key: "); for (int i = 0; i < private_key_length; i++) @@ -63,15 +67,25 @@ int main() printf("\n"); // mock received_message from VSSEC // 321212102fddc145caccca430566370df149855d3a0208027a5e0801124104c7a1f47138486aa4729971494878d33b1a24e39571f748a6e16c5955b3d877d3a6aaa0e955166474af5d32c410f439a2234137ad1bb085fd4e8813c958f11d971a104c463f9cc0d3d26906e982ed224adde625854a000030066a2432220a205a0d3c7cb02c04d912a3588bc2a6fd8c00f244091bdd9dfe46fcdc4706415b269203103ccce3d51a6f3c2aeea8913644a70584 - pb_byte_t received_bytes_vcsec[client.MAX_BLE_MESSAGE_SIZE] = {0x32, 0x12, 0x12, 0x10, 0x2f, 0xdd, 0xc1, 0x45, 0xca, 0xcc, 0xca, 0x43, 0x05, 0x66, 0x37, 0x0d, 0xf1, 0x49, 0x85, 0x5d, 0x3a, 0x02, 0x08, 0x02, 0x7a, 0x5e, 0x08, 0x01, 0x12, 0x41, 0x04, 0xc7, 0xa1, 0xf4, 0x71, 0x38, 0x48, 0x6a, 0xa4, 0x72, 0x99, 0x71, 0x49, 0x48, 0x78, 0xd3, 0x3b, 0x1a, 0x24, 0xe3, 0x95, 0x71, 0xf7, 0x48, 0xa6, 0xe1, 0x6c, 0x59, 0x55, 0xb3, 0xd8, 0x77, 0xd3, 0xa6, 0xaa, 0xa0, 0xe9, 0x55, 0x16, 0x64, 0x74, 0xaf, 0x5d, 0x32, 0xc4, 0x10, 0xf4, 0x39, 0xa2, 0x23, 0x41, 0x37, 0xad, 0x1b, 0xb0, 0x85, 0xfd, 0x4e, 0x88, 0x13, 0xc9, 0x58, 0xf1, 0x1d, 0x97, 0x1a, 0x10, 0x4c, 0x46, 0x3f, 0x9c, 0xc0, 0xd3, 0xd2, 0x69, 0x06, 0xe9, 0x82, 0xed, 0x22, 0x4a, 0xdd, 0xe6, 0x25, 0x85, 0x4a, 0x00, 0x00, 0x30, 0x06, 0x6a, 0x24, 0x32, 0x22, 0x0a, 0x20, 0x5a, 0x0d, 0x3c, 0x7c, 0xb0, 0x2c, 0x04, 0xd9, 0x12, 0xa3, 0x58, 0x8b, 0xc2, 0xa6, 0xfd, 0x8c, 0x00, 0xf2, 0x44, 0x09, 0x1b, 0xdd, 0x9d, 0xfe, 0x46, 0xfc, 0xdc, 0x47, 0x06, 0x41, 0x5b, 0x26, 0x92, 0x03, 0x10, 0x3c, 0xcc, 0xe3, 0xd5, 0x1a, 0x6f, 0x3c, 0x2a, 0xee, 0xa8, 0x91, 0x36, 0x44, 0xa7, 0x05, 0x84}; + pb_byte_t received_bytes_vcsec[177] = {0x32, 0x12, 0x12, 0x10, 0x2f, 0xdd, 0xc1, 0x45, 0xca, 0xcc, 0xca, 0x43, 0x05, 0x66, 0x37, 0x0d, 0xf1, 0x49, 0x85, 0x5d, 0x3a, 0x02, 0x08, 0x02, 0x7a, 0x5e, 0x08, 0x01, 0x12, 0x41, 0x04, 0xc7, 0xa1, 0xf4, 0x71, 0x38, 0x48, 0x6a, 0xa4, 0x72, 0x99, 0x71, 0x49, 0x48, 0x78, 0xd3, 0x3b, 0x1a, 0x24, 0xe3, 0x95, 0x71, 0xf7, 0x48, 0xa6, 0xe1, 0x6c, 0x59, 0x55, 0xb3, 0xd8, 0x77, 0xd3, 0xa6, 0xaa, 0xa0, 0xe9, 0x55, 0x16, 0x64, 0x74, 0xaf, 0x5d, 0x32, 0xc4, 0x10, 0xf4, 0x39, 0xa2, 0x23, 0x41, 0x37, 0xad, 0x1b, 0xb0, 0x85, 0xfd, 0x4e, 0x88, 0x13, 0xc9, 0x58, 0xf1, 0x1d, 0x97, 0x1a, 0x10, 0x4c, 0x46, 0x3f, 0x9c, 0xc0, 0xd3, 0xd2, 0x69, 0x06, 0xe9, 0x82, 0xed, 0x22, 0x4a, 0xdd, 0xe6, 0x25, 0x85, 0x4a, 0x00, 0x00, 0x30, 0x06, 0x6a, 0x24, 0x32, 0x22, 0x0a, 0x20, 0x5a, 0x0d, 0x3c, 0x7c, 0xb0, 0x2c, 0x04, 0xd9, 0x12, 0xa3, 0x58, 0x8b, 0xc2, 0xa6, 0xfd, 0x8c, 0x00, 0xf2, 0x44, 0x09, 0x1b, 0xdd, 0x9d, 0xfe, 0x46, 0xfc, 0xdc, 0x47, 0x06, 0x41, 0x5b, 0x26, 0x92, 0x03, 0x10, 0x3c, 0xcc, 0xe3, 0xd5, 0x1a, 0x6f, 0x3c, 0x2a, 0xee, 0xa8, 0x91, 0x36, 0x44, 0xa7, 0x05, 0x84}; // parse received universal message UniversalMessage_RoutableMessage received_message_vcsec = UniversalMessage_RoutableMessage_init_default; - client.parseUniversalMessage(received_bytes_vcsec, sizeof(received_bytes_vcsec), &received_message_vcsec); + return_code = client.parseUniversalMessage(received_bytes_vcsec, sizeof(received_bytes_vcsec), &received_message_vcsec); + if (return_code != 0) + { + printf("Failed to parse received message VSSEC\n"); + return -1; + } log_routable_message(&received_message_vcsec); Signatures_SessionInfo session_info_vcsec = Signatures_SessionInfo_init_default; - client.parsePayloadSessionInfo(&received_message_vcsec.payload.session_info, &session_info_vcsec); + return_code = client.parsePayloadSessionInfo(&received_message_vcsec.payload.session_info, &session_info_vcsec); + if (return_code != 0) + { + printf("Failed to parse session info VSSEC\n"); + return -1; + } log_session_info(&session_info_vcsec); uint32_t generated_at_vcsec = std::time(nullptr); @@ -82,8 +96,8 @@ int main() client.session_vcsec_.setTimeZero(&time_zero_vcsec); client.session_vcsec_.setIsAuthenticated(true); printf("Session authenticated: %s\n", client.session_vcsec_.isAuthenticated ? "true" : "false"); - int result_code_vcsec = client.loadTeslaKey(false, session_info_vcsec.publicKey.bytes, session_info_vcsec.publicKey.size); - if (result_code_vcsec != 0) + return_code = client.loadTeslaKey(false, session_info_vcsec.publicKey.bytes, session_info_vcsec.publicKey.size); + if (return_code != 0) { printf("Failed load vssec tesla key\n"); return 1; @@ -96,7 +110,7 @@ int main() } printf("\n"); - printf("Parsed session info response\n"); + printf("Parsed VCSEC session info response\n"); printf("Received new counter from the car: %" PRIu32, client.session_vcsec_.counter_); printf("\n"); printf("Received new expires at from the car: %" PRIu32, client.session_vcsec_.expires_at_); @@ -109,11 +123,11 @@ int main() printf("\n"); // build wake command + printf("Building wake command\n"); unsigned char action_message_buffer[client.MAX_BLE_MESSAGE_SIZE]; size_t action_message_buffer_length = 0; - int return_code_action = client.buildVCSECActionMessage(VCSEC_RKEAction_E_RKE_ACTION_WAKE_VEHICLE, action_message_buffer, &action_message_buffer_length); - - if (return_code_action != 0) + return_code = client.buildVCSECActionMessage(VCSEC_RKEAction_E_RKE_ACTION_WAKE_VEHICLE, action_message_buffer, &action_message_buffer_length); + if (return_code != 0) { printf("Failed to build action message \n"); return -1; @@ -128,15 +142,25 @@ int main() // mock received message from INFOTAINMENT // 321212108f3d244b50b07a9842cac108c928b5e73a0208037a5e0801124104c7a1f47138486aa4729971494878d33b1a24e39571f748a6e16c5955b3d877d3a6aaa0e955166474af5d32c410f439a2234137ad1bb085fd4e8813c958f11d971a104c463f9cc0d3d26906e982ed224adde6255f0a000030076a2432220a208e8dcd164ef361fd123c46c2b2bdfd1fc93056f4ef32c9311a275db908d4d23f9203100a404ec0fc9aa863aec3e50196fbf30b - pb_byte_t received_bytes_infotainment[client.MAX_BLE_MESSAGE_SIZE] = {0x32, 0x12, 0x12, 0x10, 0x8f, 0x3d, 0x24, 0x4b, 0x50, 0xb0, 0x7a, 0x98, 0x42, 0xca, 0xc1, 0x08, 0xc9, 0x28, 0xb5, 0xe7, 0x3a, 0x02, 0x08, 0x03, 0x7a, 0x5e, 0x08, 0x01, 0x12, 0x41, 0x04, 0xc7, 0xa1, 0xf4, 0x71, 0x38, 0x48, 0x6a, 0xa4, 0x72, 0x99, 0x71, 0x49, 0x48, 0x78, 0xd3, 0x3b, 0x1a, 0x24, 0xe3, 0x95, 0x71, 0xf7, 0x48, 0xa6, 0xe1, 0x6c, 0x59, 0x55, 0xb3, 0xd8, 0x77, 0xd3, 0xa6, 0xaa, 0xa0, 0xe9, 0x55, 0x16, 0x64, 0x74, 0xaf, 0x5d, 0x32, 0xc4, 0x10, 0xf4, 0x39, 0xa2, 0x23, 0x41, 0x37, 0xad, 0x1b, 0xb0, 0x85, 0xfd, 0x4e, 0x88, 0x13, 0xc9, 0x58, 0xf1, 0x1d, 0x97, 0x1a, 0x10, 0x4c, 0x46, 0x3f, 0x9c, 0xc0, 0xd3, 0xd2, 0x69, 0x06, 0xe9, 0x82, 0xed, 0x22, 0x4a, 0xdd, 0xe6, 0x25, 0x5f, 0x0a, 0x00, 0x00, 0x30, 0x07, 0x6a, 0x24, 0x32, 0x22, 0x0a, 0x20, 0x8e, 0x8d, 0xcd, 0x16, 0x4e, 0xf3, 0x61, 0xfd, 0x12, 0x3c, 0x46, 0xc2, 0xb2, 0xbd, 0xfd, 0x1f, 0xc9, 0x30, 0x56, 0xf4, 0xef, 0x32, 0xc9, 0x31, 0x1a, 0x27, 0x5d, 0xb9, 0x08, 0xd4, 0xd2, 0x3f, 0x92, 0x03, 0x10, 0x0a, 0x40, 0x4e, 0xc0, 0xfc, 0x9a, 0xa8, 0x63, 0xae, 0xc3, 0xe5, 0x01, 0x96, 0xfb, 0xf3, 0x0b}; + pb_byte_t received_bytes_infotainment[177] = {0x32, 0x12, 0x12, 0x10, 0x8f, 0x3d, 0x24, 0x4b, 0x50, 0xb0, 0x7a, 0x98, 0x42, 0xca, 0xc1, 0x08, 0xc9, 0x28, 0xb5, 0xe7, 0x3a, 0x02, 0x08, 0x03, 0x7a, 0x5e, 0x08, 0x01, 0x12, 0x41, 0x04, 0xc7, 0xa1, 0xf4, 0x71, 0x38, 0x48, 0x6a, 0xa4, 0x72, 0x99, 0x71, 0x49, 0x48, 0x78, 0xd3, 0x3b, 0x1a, 0x24, 0xe3, 0x95, 0x71, 0xf7, 0x48, 0xa6, 0xe1, 0x6c, 0x59, 0x55, 0xb3, 0xd8, 0x77, 0xd3, 0xa6, 0xaa, 0xa0, 0xe9, 0x55, 0x16, 0x64, 0x74, 0xaf, 0x5d, 0x32, 0xc4, 0x10, 0xf4, 0x39, 0xa2, 0x23, 0x41, 0x37, 0xad, 0x1b, 0xb0, 0x85, 0xfd, 0x4e, 0x88, 0x13, 0xc9, 0x58, 0xf1, 0x1d, 0x97, 0x1a, 0x10, 0x4c, 0x46, 0x3f, 0x9c, 0xc0, 0xd3, 0xd2, 0x69, 0x06, 0xe9, 0x82, 0xed, 0x22, 0x4a, 0xdd, 0xe6, 0x25, 0x5f, 0x0a, 0x00, 0x00, 0x30, 0x07, 0x6a, 0x24, 0x32, 0x22, 0x0a, 0x20, 0x8e, 0x8d, 0xcd, 0x16, 0x4e, 0xf3, 0x61, 0xfd, 0x12, 0x3c, 0x46, 0xc2, 0xb2, 0xbd, 0xfd, 0x1f, 0xc9, 0x30, 0x56, 0xf4, 0xef, 0x32, 0xc9, 0x31, 0x1a, 0x27, 0x5d, 0xb9, 0x08, 0xd4, 0xd2, 0x3f, 0x92, 0x03, 0x10, 0x0a, 0x40, 0x4e, 0xc0, 0xfc, 0x9a, 0xa8, 0x63, 0xae, 0xc3, 0xe5, 0x01, 0x96, 0xfb, 0xf3, 0x0b}; // parse received universal message UniversalMessage_RoutableMessage received_message = UniversalMessage_RoutableMessage_init_default; - client.parseUniversalMessage(received_bytes_infotainment, sizeof(received_bytes_infotainment), &received_message); + return_code = client.parseUniversalMessage(received_bytes_infotainment, sizeof(received_bytes_infotainment), &received_message); + if (return_code != 0) + { + printf("Failed to parse received message INFOTAINMENT\n"); + return -1; + } log_routable_message(&received_message); Signatures_SessionInfo session_info = Signatures_SessionInfo_init_default; - client.parsePayloadSessionInfo(&received_message.payload.session_info, &session_info); + return_code = client.parsePayloadSessionInfo(&received_message.payload.session_info, &session_info); + if (return_code != 0) + { + printf("Failed to parse session info INFOTAINMENT\n"); + return -1; + } log_session_info(&session_info); uint32_t generated_at = std::time(nullptr); @@ -149,7 +173,7 @@ int main() printf("Session authenticated: %s\n", client.session_infotainment_.isAuthenticated ? "true" : "false"); client.loadTeslaKey(true, session_info.publicKey.bytes, session_info.publicKey.size); - printf("Parsed session info response\n"); + printf("Parsed INFOTAINMENT session info response\n"); printf("Received new counter from the car: %" PRIu32, client.session_infotainment_.counter_); printf("\n"); printf("Received new counter from the car (hex): "); From ed04ab7367993762df503fb1e6eaf40d7706190c Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 05/10] example: add buildVCSECInformationRequestMessage --- examples/simple/main.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/simple/main.cpp b/examples/simple/main.cpp index a34b5a1..a8d3461 100644 --- a/examples/simple/main.cpp +++ b/examples/simple/main.cpp @@ -140,6 +140,24 @@ int main() } printf("\n"); + // build information request status + printf("Building information request status\n"); + pb_byte_t info_request_status_buffer[client.MAX_BLE_MESSAGE_SIZE]; + size_t info_request_status_length = 0; + return_code = client.buildVCSECInformationRequestMessage(VCSEC_InformationRequestType_INFORMATION_REQUEST_TYPE_GET_STATUS, info_request_status_buffer, &info_request_status_length); + if (return_code != 0) + { + printf("Failed to build action message \n"); + return -1; + } + printf("VCSEC InfoRequest status length: %d\n", info_request_status_length); + printf("VCSEC InfoRequest status hex: "); + for (int i = 0; i < info_request_status_length; i++) + { + printf("%02X", info_request_status_buffer[i]); + } + printf("\n"); + // mock received message from INFOTAINMENT // 321212108f3d244b50b07a9842cac108c928b5e73a0208037a5e0801124104c7a1f47138486aa4729971494878d33b1a24e39571f748a6e16c5955b3d877d3a6aaa0e955166474af5d32c410f439a2234137ad1bb085fd4e8813c958f11d971a104c463f9cc0d3d26906e982ed224adde6255f0a000030076a2432220a208e8dcd164ef361fd123c46c2b2bdfd1fc93056f4ef32c9311a275db908d4d23f9203100a404ec0fc9aa863aec3e50196fbf30b pb_byte_t received_bytes_infotainment[177] = {0x32, 0x12, 0x12, 0x10, 0x8f, 0x3d, 0x24, 0x4b, 0x50, 0xb0, 0x7a, 0x98, 0x42, 0xca, 0xc1, 0x08, 0xc9, 0x28, 0xb5, 0xe7, 0x3a, 0x02, 0x08, 0x03, 0x7a, 0x5e, 0x08, 0x01, 0x12, 0x41, 0x04, 0xc7, 0xa1, 0xf4, 0x71, 0x38, 0x48, 0x6a, 0xa4, 0x72, 0x99, 0x71, 0x49, 0x48, 0x78, 0xd3, 0x3b, 0x1a, 0x24, 0xe3, 0x95, 0x71, 0xf7, 0x48, 0xa6, 0xe1, 0x6c, 0x59, 0x55, 0xb3, 0xd8, 0x77, 0xd3, 0xa6, 0xaa, 0xa0, 0xe9, 0x55, 0x16, 0x64, 0x74, 0xaf, 0x5d, 0x32, 0xc4, 0x10, 0xf4, 0x39, 0xa2, 0x23, 0x41, 0x37, 0xad, 0x1b, 0xb0, 0x85, 0xfd, 0x4e, 0x88, 0x13, 0xc9, 0x58, 0xf1, 0x1d, 0x97, 0x1a, 0x10, 0x4c, 0x46, 0x3f, 0x9c, 0xc0, 0xd3, 0xd2, 0x69, 0x06, 0xe9, 0x82, 0xed, 0x22, 0x4a, 0xdd, 0xe6, 0x25, 0x5f, 0x0a, 0x00, 0x00, 0x30, 0x07, 0x6a, 0x24, 0x32, 0x22, 0x0a, 0x20, 0x8e, 0x8d, 0xcd, 0x16, 0x4e, 0xf3, 0x61, 0xfd, 0x12, 0x3c, 0x46, 0xc2, 0xb2, 0xbd, 0xfd, 0x1f, 0xc9, 0x30, 0x56, 0xf4, 0xef, 0x32, 0xc9, 0x31, 0x1a, 0x27, 0x5d, 0xb9, 0x08, 0xd4, 0xd2, 0x3f, 0x92, 0x03, 0x10, 0x0a, 0x40, 0x4e, 0xc0, 0xfc, 0x9a, 0xa8, 0x63, 0xae, 0xc3, 0xe5, 0x01, 0x96, 0xfb, 0xf3, 0x0b}; From 38292636beed598728869dc557371a47bb6b690c Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 06/10] pb_encode_fields: raise error when bytes written = 0 --- src/tb_utils.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/tb_utils.cpp b/src/tb_utils.cpp index 49c02a8..7ea40a6 100644 --- a/src/tb_utils.cpp +++ b/src/tb_utils.cpp @@ -95,6 +95,11 @@ namespace TeslaBLE return 1; } printf("Bytes written: %zu\n", unsigned_message_size_stream.bytes_written); + if (unsigned_message_size_stream.bytes_written == 0) + { + printf("\033[1;31mNo bytes written\033[0m\n"); + return 1; + } *output_length = unsigned_message_size_stream.bytes_written; // printf("Message size: %hhu\n", *output_length); From cd8cebd7454b3f8c530f8370a868b9de93202a01 Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 07/10] raise on pb_encode_fields errors --- src/client.cpp | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index ce9d293..10dd678 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -539,7 +539,12 @@ namespace TeslaBLE printf("Encoding whitelist message\n"); pb_byte_t payload_buffer[80]; size_t payload_length; - pb_encode_fields(payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, &payload); + int return_code = pb_encode_fields(payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, &payload); + if (return_code != 0) + { + printf("Failed to encode whitelist message\n"); + return 1; + } printf("Encoded whitelist message :"); for (int i = 0; i < payload_length; i++) @@ -563,7 +568,12 @@ namespace TeslaBLE printf("Encoding VCSEC to VCSEC message\n"); pb_byte_t vcsec_encode_buffer[86]; size_t vcsec_encode_buffer_size; - pb_encode_fields(vcsec_encode_buffer, &vcsec_encode_buffer_size, VCSEC_ToVCSECMessage_fields, &vcsec_message); + return_code = pb_encode_fields(vcsec_encode_buffer, &vcsec_encode_buffer_size, VCSEC_ToVCSECMessage_fields, &vcsec_message); + if (return_code != 0) + { + printf("Failed to encode VCSEC to VCSEC message\n"); + return 1; + } printf("Encoded VCSEC to VCSEC message length: %d\n", vcsec_encode_buffer_size); printf("Encoded VCSEC to VCSEC message :"); for (int i = 0; i < vcsec_encode_buffer_size; i++) @@ -891,7 +901,12 @@ namespace TeslaBLE } memcpy(universal_message.uuid, uuid, sizeof(uuid)); - pb_encode_fields(output_buffer, output_length, UniversalMessage_RoutableMessage_fields, &universal_message); + int return_code = pb_encode_fields(output_buffer, output_length, UniversalMessage_RoutableMessage_fields, &universal_message); + if (return_code != 0) + { + printf("\033[1;31mFailed to encode universal message\033[0m\n"); + return 1; + } return 0; } @@ -939,7 +954,12 @@ namespace TeslaBLE size_t universal_encode_buffer_size = this->MAX_BLE_MESSAGE_SIZE - 2; pb_byte_t universal_encode_buffer[universal_encode_buffer_size]; - pb_encode_fields(universal_encode_buffer, &universal_encode_buffer_size, UniversalMessage_RoutableMessage_fields, &universal_message); + int return_code = pb_encode_fields(universal_encode_buffer, &universal_encode_buffer_size, UniversalMessage_RoutableMessage_fields, &universal_message); + if (return_code != 0) + { + printf("\033[1;31mFailed to encode universal message\033[0m\n"); + return 1; + } this->prependLength(universal_encode_buffer, universal_encode_buffer_size, output_buffer, output_length); @@ -963,7 +983,12 @@ namespace TeslaBLE size_t payload_length; printf("message: %p\n", message); printf("message.which_sub_message: %d\n", message->which_sub_message); - pb_encode_fields(&payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, &message); + int return_code = pb_encode_fields(&payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, &message); + if (return_code != 0) + { + printf("\033[1;31mFailed to encode unsigned message\033[0m\n"); + return 1; + } // build universal message return this->buildUniversalMessageWithPayload( @@ -1000,7 +1025,12 @@ namespace TeslaBLE { pb_byte_t payload_buffer[100]; size_t payload_length = 0; - pb_encode_fields(payload_buffer, &payload_length, CarServer_Action_fields, action); + int return_code = pb_encode_fields(payload_buffer, &payload_length, CarServer_Action_fields, action); + if (return_code != 0) + { + printf("\033[1;31mFailed to encode car action message\033[0m\n"); + return 1; + } printf("payload length: %zu\n", payload_length); printf("payload: "); for (int i = 0; i < payload_length; i++) @@ -1187,7 +1217,7 @@ namespace TeslaBLE size_t *output_length, uint32_t key_slot) { - VCSEC_InformationRequest information_request = VCSEC_InformationRequest_init_default; + VCSEC_InformationRequest information_request = VCSEC_InformationRequest_init_zero; information_request.informationRequestType = request_type; if (key_slot != 0xFFFFFFFF) From 8e3ef05e2e75b20d593594d033478dea2a8c3dbd Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:08:22 +0200 Subject: [PATCH 08/10] fix buildUnsignedMessagePayload --- src/client.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 10dd678..b18ae25 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -979,11 +979,11 @@ namespace TeslaBLE size_t *output_length, bool encryptPayload) { - pb_byte_t payload_buffer; + pb_byte_t payload_buffer[100]; size_t payload_length; printf("message: %p\n", message); printf("message.which_sub_message: %d\n", message->which_sub_message); - int return_code = pb_encode_fields(&payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, &message); + int return_code = pb_encode_fields(payload_buffer, &payload_length, VCSEC_UnsignedMessage_fields, message); if (return_code != 0) { printf("\033[1;31mFailed to encode unsigned message\033[0m\n"); @@ -992,7 +992,7 @@ namespace TeslaBLE // build universal message return this->buildUniversalMessageWithPayload( - &payload_buffer, payload_length, UniversalMessage_Domain_DOMAIN_VEHICLE_SECURITY, + payload_buffer, payload_length, UniversalMessage_Domain_DOMAIN_VEHICLE_SECURITY, output_buffer, output_length, encryptPayload); } From 92da2094a7f949dd932daf25b97e8c8bbf8238e8 Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:09:19 +0200 Subject: [PATCH 09/10] get rid of broken CI step --- .github/workflows/cmake.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 607560c..7381712 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -19,10 +19,3 @@ jobs: - name: Configure and Build Project uses: threeal/cmake-action@main - - # run simple example as a test - - name: Build and run example - run: | - cd examples/simple - cmake --build . - ./simple From 9868d04bc500e48ff88bcd018c3d6a11fa3b5171 Mon Sep 17 00:00:00 2001 From: yoziru Date: Wed, 17 Jul 2024 20:09:44 +0200 Subject: [PATCH 10/10] bump version 2.0.2 --- CMakeLists.txt | 2 +- library.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d217b3..89f6c6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.22) include(FetchContent) project(TeslaBLE - VERSION 2.0.1 + VERSION 2.0.2 DESCRIPTION "CPP Tesla BLE Library" LANGUAGES CXX C ) diff --git a/library.json b/library.json index 5d17990..961b593 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "TeslaBLE", - "version": "2.0.1", + "version": "2.0.2", "description": "This CPP library facilitates direct communication with Tesla vehicles via the BLE API. It offers fundamental features such as unlocking/locking, opening the trunk, and more. The library's capabilities are contingent on the range of actions implemented by Tesla, which is the only limitation at present.", "keywords": "tesla, ble", "repository": {