Skip to content

Commit ded1ea4

Browse files
authored
tesla ble 4.0 (#41)
* move log.cpp from esphome-tesla-ble into tesla-ble * feat: Add `Vehicle` class to manage Tesla BLE communication, command queuing, and authentication, supported by new adapter interfaces. * feat(logging): Introduce logging framework with log levels and adapters * add line number parameter to log functions for better traceability * sort and reorganize imports * feat(vehicle): Enhance command processing for wake commands and improve logging on timeouts * bump to v4.0.0
1 parent cb816b2 commit ded1ea4

23 files changed

Lines changed: 2455 additions & 75 deletions

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ set(TESLABLE_SRCS
99
"src/errors.cpp"
1010
"src/message_builders.cpp"
1111
"src/peer.cpp"
12+
"src/tb_logging.cpp"
1213
"src/tb_utils.cpp"
14+
"src/vehicle.cpp"
1315
"src/vin_utils.cpp"
16+
"src/logging_bridge.cpp"
1417
)
1518

1619
# Protobuf generated source files
@@ -30,9 +33,12 @@ set(TESLABLE_PROTO_SRCS
3033
# Public headers
3134
set(TESLABLE_PUBLIC_HEADERS
3235
include/client.h
36+
include/adapters.h
3337
include/errors.h
3438
include/peer.h
39+
include/tb_logging.h
3540
include/tb_utils.h
41+
include/vehicle.h
3642
include/vin_utils.h
3743
)
3844

@@ -104,7 +110,7 @@ else()
104110

105111
include(FetchContent)
106112
project(TeslaBLE
107-
VERSION 3.4.0
113+
VERSION 4.0.0
108114
DESCRIPTION "CPP Tesla BLE Library"
109115
LANGUAGES CXX C
110116
)

idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "3.4.0"
1+
version: "4.0.0"
22
description: "C++ library for Tesla BLE API communication"
33
url: "https://github.com/yoziru/tesla-ble"
44
dependencies:

include/adapters.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include <cstdint>
6+
7+
namespace TeslaBLE {
8+
9+
/**
10+
* @brief Abstract interface for BLE operations.
11+
*
12+
* Platforms (ESPHome, Python/Bleak) must implement this to handle
13+
* low-level BLE communication.
14+
*/
15+
class BleAdapter {
16+
public:
17+
virtual ~BleAdapter() = default;
18+
19+
/**
20+
* @brief Connect to a specific BLE device.
21+
* @param address The MAC address or identifier of the device.
22+
*/
23+
virtual void connect(const std::string& address) = 0;
24+
25+
/**
26+
* @brief Disconnect from the current device.
27+
*/
28+
virtual void disconnect() = 0;
29+
30+
/**
31+
* @brief Write data to the vehicle.
32+
*
33+
* The implementation should handle writing to the correct RX characteristic
34+
* of the Tesla service.
35+
*
36+
* @param data The raw bytes to send.
37+
* @return true if written successfully (enqueued), false otherwise.
38+
*/
39+
virtual bool write(const std::vector<uint8_t>& data) = 0;
40+
41+
// Callbacks for data reception and connection state should be handled
42+
// by registering the Vehicle instance with the platform's BLE handler,
43+
// or by the platform calling Vehicle::on_rx_data().
44+
};
45+
46+
/**
47+
* @brief Abstract interface for persistent storage.
48+
*
49+
* Used to securely store session keys, counters, and tokens.
50+
*/
51+
class StorageAdapter {
52+
public:
53+
virtual ~StorageAdapter() = default;
54+
55+
/**
56+
* @brief Load a value from storage.
57+
* @param key The unique key identifier.
58+
* @param buffer Output buffer to store the loaded data.
59+
* @return true if found and loaded, false otherwise.
60+
*/
61+
virtual bool load(const std::string& key, std::vector<uint8_t>& buffer) = 0;
62+
63+
/**
64+
* @brief Save a value to storage.
65+
* @param key The unique key identifier.
66+
* @param buffer The data to save.
67+
* @return true if saved successfully, false otherwise.
68+
*/
69+
virtual bool save(const std::string& key, const std::vector<uint8_t>& buffer) = 0;
70+
71+
/**
72+
* @brief Remove a value from storage.
73+
* @param key The unique key identifier.
74+
* @return true if removed successfully, false otherwise.
75+
*/
76+
virtual bool remove(const std::string& key) = 0;
77+
};
78+
79+
} // namespace TeslaBLE

include/defs.h

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,36 @@
11
#pragma once
22

3-
#ifdef ESP_PLATFORM
4-
5-
#ifndef LOG_LOCAL_LEVEL
6-
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
3+
#ifndef TESLA_LOG_TAG
4+
#define TESLA_LOG_TAG "TeslaBLE"
75
#endif
8-
#include "esp_log.h"
9-
static const char *const TAG = "tesla_ble";
10-
#define LOG(...) ESP_LOGI(TAG, __VA_ARGS__)
11-
#define LOG_INFO(...) ESP_LOGI(TAG, __VA_ARGS__)
12-
#define LOG_DEBUG(...) ESP_LOGD(TAG, __VA_ARGS__)
13-
#define LOG_ERROR(...) ESP_LOGE(TAG, __VA_ARGS__)
14-
#define LOG_WARNING(...) ESP_LOGW(TAG, __VA_ARGS__)
15-
16-
#else
17-
18-
#include <cstdio>
19-
#include <cstring>
20-
#include <iostream>
21-
22-
#define RESET_COLOR "\x1B[0m"
23-
#define INFO_COLOR "\x1B[1;34m"
24-
#define DEBUG_COLOR "\x1B[1;30m"
25-
#define WARNING_COLOR "\x1B[1;33m"
26-
#define ERROR_COLOR "\x1B[31m"
27-
#define LOG(...) log("[LOG]", RESET_COLOR, __VA_ARGS__)
28-
#define LOG_INFO(...) log("[INFO]", INFO_COLOR, __VA_ARGS__)
29-
#define LOG_DEBUG(...) log("[DEBUG]", DEBUG_COLOR, __VA_ARGS__)
30-
#define LOG_WARNING(...) log("[WARNING]", WARNING_COLOR, __VA_ARGS__)
31-
#define LOG_ERROR(...) log("[ERROR]", ERROR_COLOR, __VA_ARGS__)
32-
template <typename... Args>
33-
void log(const char *type, const char *color, const char *s, Args... args)
34-
{
35-
printf("%s%s - ", color, type);
36-
if constexpr (sizeof...(args) > 0) {
37-
printf(s, args...);
38-
} else {
39-
printf("%s", s);
40-
}
41-
printf("%s\n", RESET_COLOR);
42-
}
436

7+
#include <cstdarg>
8+
#include "adapters.h"
9+
10+
#ifdef ESP_PLATFORM
11+
#include <esp_log.h>
4412
#endif
13+
14+
namespace TeslaBLE {
15+
16+
enum class LogLevel {
17+
ERROR,
18+
WARN,
19+
INFO,
20+
DEBUG,
21+
VERBOSE
22+
};
23+
24+
typedef void (*LogCallback)(LogLevel level, const char* tag, int line, const char* format, va_list args);
25+
26+
extern LogCallback g_log_callback;
27+
28+
void log_internal(LogLevel level, const char* tag, int line, const char* format, ...);
29+
30+
} // namespace TeslaBLE
31+
32+
#define LOG_ERROR(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::ERROR, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__)
33+
#define LOG_WARNING(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::WARN, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__)
34+
#define LOG_INFO(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::INFO, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__)
35+
#define LOG_DEBUG(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::DEBUG, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__)
36+
#define LOG_VERBOSE(format, ...) TeslaBLE::log_internal(TeslaBLE::LogLevel::VERBOSE, TESLA_LOG_TAG, __LINE__, format, ##__VA_ARGS__)

include/tb_logging.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include "car_server.pb.h"
4+
#include "signatures.pb.h"
5+
#include "universal_message.pb.h"
6+
#include "vcsec.pb.h"
7+
#include "errors.pb.h"
8+
#include <string>
9+
#include <cstdint>
10+
11+
namespace TeslaBLE {
12+
13+
const char *domain_to_string(UniversalMessage_Domain domain);
14+
const char *information_request_type_to_string(VCSEC_InformationRequestType request_type);
15+
const char *message_fault_to_string(UniversalMessage_MessageFault_E fault);
16+
const char *operation_status_to_string(UniversalMessage_OperationStatus_E status);
17+
const char *vcsec_operation_status_to_string(VCSEC_OperationStatus_E status);
18+
const char *vssec_signed_message_information_to_string(VCSEC_SignedMessage_information_E information);
19+
const char *closure_state_to_string(VCSEC_ClosureState_E state);
20+
const char *vehicle_lock_state_to_string(VCSEC_VehicleLockState_E state);
21+
const char *vehicle_sleep_status_to_string(VCSEC_VehicleSleepStatus_E state);
22+
const char *user_presence_to_string(VCSEC_UserPresence_E state);
23+
const char *generic_error_to_string(Errors_GenericError_E error);
24+
const char *carserver_operation_status_to_string(CarServer_OperationStatus_E status);
25+
26+
void log_aes_gcm_personalized_signature_data(const char *tag, const Signatures_AES_GCM_Personalized_Signature_Data *data);
27+
void log_destination(const char *tag, const char *prefix, const UniversalMessage_Destination *dest);
28+
void log_information_request(const char *tag, const VCSEC_InformationRequest *msg);
29+
void log_message_status(const char *tag, const UniversalMessage_MessageStatus *status);
30+
void log_routable_message(const char *tag, const UniversalMessage_RoutableMessage *msg);
31+
void log_session_info_request(const char *tag, const UniversalMessage_SessionInfoRequest *req);
32+
void log_session_info(const char *tag, const Signatures_SessionInfo *req);
33+
void log_signature_data(const char *tag, const Signatures_SignatureData *sig);
34+
void log_vehicle_status(const char *tag, const VCSEC_VehicleStatus *msg);
35+
void log_vssec_signed_message_status(const char *tag, const VCSEC_SignedMessage_status *status);
36+
void log_vssec_whitelist_operation_status(const char *tag, const VCSEC_WhitelistOperation_status *status);
37+
void log_vcsec_command_status(const char *tag, const VCSEC_CommandStatus *msg);
38+
void log_carserver_response(const char *tag, const CarServer_Response *msg);
39+
40+
} // namespace TeslaBLE

include/tb_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
#include <string>
44
#include "pb.h"
5+
#include <vector>
56

67
namespace TeslaBLE
78
{
9+
std::string format_hex(const uint8_t* data, size_t length);
10+
811
int pb_encode_fields(
912
pb_byte_t *output_buffer,
1013
size_t *output_length,

0 commit comments

Comments
 (0)