Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions firmware/main/ble.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct BleLocationData {
uint32_t timestamp;
uint8_t valid;
};
static_assert(sizeof(BleLocationData) == 17, "BleLocationData must be 17 bytes for BLE");

struct BleAlertData {
uint8_t alert_type;
Expand All @@ -37,19 +38,20 @@ struct BleAlertData {
int32_t altitude;
uint32_t timestamp;
};
static_assert(sizeof(BleAlertData) == 18, "BleAlertData must be 18 bytes for BLE");
#pragma pack(pop)

class BleServer {
public:
BleServer ();
~BleServer ();

esp_err_t init ();
esp_err_t start ();
esp_err_t stop ();
esp_err_t update_location (const BleLocationData& location);
esp_err_t send_alert (const BleAlertData& alert);
esp_err_t set_device_name (const char* name);
[[nodiscard]] esp_err_t init ();
[[nodiscard]] esp_err_t start ();
[[nodiscard]] esp_err_t stop ();
[[nodiscard]] esp_err_t update_location (const BleLocationData& location);
[[nodiscard]] esp_err_t send_alert (const BleAlertData& alert);
[[nodiscard]] esp_err_t set_device_name (const char* name);

bool is_connected () const;

Expand Down
9 changes: 9 additions & 0 deletions firmware/main/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class Config {
static esp_err_t get_spreading_factor (uint8_t& sf);
static esp_err_t set_spreading_factor (uint8_t sf);

/**
* @brief Get device name from NVS
* @param name Buffer to store device name
* @param max_len Maximum buffer size
* @return ESP_OK on success, error code otherwise
* @note If key not found in NVS, returns default name "PetTracker" and ESP_OK.
* Callers who need to distinguish "stored value" vs "default" should use
* nvs_get_str() directly.
*/
static esp_err_t get_device_name (char* name, size_t max_len);
static esp_err_t set_device_name (const char* name);

Expand Down
5 changes: 4 additions & 1 deletion firmware/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ app_main (void) {
}

static TrackerStateMachine state_machine (gps, lora, accel, ble, led, battery);
state_machine.init ();
esp_err_t sm_err = state_machine.init ();
if (sm_err != ESP_OK) {
ESP_LOGE (TAG, "State machine init failed: %s", esp_err_to_name (sm_err));
}

ESP_LOGI (TAG, "Pet tracker initialized, entering main loop");

Expand Down
7 changes: 4 additions & 3 deletions firmware/main/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ TrackerStateMachine::TrackerStateMachine (Gps& gps, LoRaDriver& lora, Accelerome
lora_ (lora), accel_ (accel), ble_ (ble), led_ (led), battery_ (battery),
geofence_breach_ (false) {}

void
esp_err_t
TrackerStateMachine::init () {
esp_err_t err = Config::init ();
if (err != ESP_OK) {
ESP_LOGE (TAG, "Config::init() failed: %s", esp_err_to_name (err));
transition_to (TrackerState::ERROR);
return;
return err;
}

err = Config::load (config_);
Expand All @@ -34,6 +34,7 @@ TrackerStateMachine::init () {
lora_.set_spreading_factor (config_.spreading_factor);

transition_to (TrackerState::IDLE);
return ESP_OK;
}

void
Expand Down Expand Up @@ -221,7 +222,7 @@ TrackerStateMachine::check_geofence () {
alert.longitude = (int32_t)(data.longitude * COORD_SCALE);
alert.altitude = (int32_t)(data.altitude * 100);
alert.timestamp = (uint32_t)(esp_timer_get_time () / 1000000);
ble_.send_alert (alert);
ESP_ERROR_CHECK (ble_.send_alert (alert));
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/main/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TrackerStateMachine {
TrackerStateMachine (Gps& gps, LoRaDriver& lora, Accelerometer& accel, BleServer& ble,
LedDriver& led, BatteryDriver& battery);

void init ();
esp_err_t init ();
void run ();

TrackerState
Expand Down
Loading