Skip to content

Commit

Permalink
increased app partition, since enabling BT exceedes 1mb of binary, sh…
Browse files Browse the repository at this point in the history
…ow BLE status in GUI yay
  • Loading branch information
szymek156 committed Apr 21, 2022
1 parent 790bcf8 commit bf01c65
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 39 deletions.
48 changes: 38 additions & 10 deletions components/for_esp32/ble_wrap/ble_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ static esp_ble_adv_data_t scan_rsp_data = {
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};

static void sendEvent(BLEStatus status) {
auto queue = BLEService::instance().getQueue();

auto status_event = BLEStatusData{.status = status};

if (xQueueSendToBack(queue, &status_event, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send BLE event");
}
}

static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
ESP_LOGI(TAG, "gap event %x", event);
switch (event) {
Expand All @@ -114,6 +124,8 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
ESP_LOGE(TAG, "advertising start failed");
} else {
ESP_LOGI(TAG, "advertising start successfully");

sendEvent(BLEStatus::Advertising);
}
break;
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
Expand Down Expand Up @@ -203,17 +215,14 @@ static void gatts_event_handler(esp_gatts_cb_event_t event,
}
}


void BLEService::enable() {
ESP_LOGI(TAG, "Enabling BT...");
// esp_err_t ret;
if (enabled_) {
ESP_LOGW(TAG, "BT already enabled");

// /* Initialize NVS. */
// ret = nvs_flash_init();
// if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// ESP_ERROR_CHECK(nvs_flash_erase());
// ret = nvs_flash_init();
// }
// ESP_ERROR_CHECK(ret);
return;
}

// If the app calls esp_bt_controller_enable(ESP_BT_MODE_BLE) to use BLE only then it is safe to
// call esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT) at initialization time to free
Expand Down Expand Up @@ -243,12 +252,24 @@ void BLEService::enable() {
ESP_ERROR_CHECK(esp_ble_gatt_set_local_mtu(500));

ESP_LOGI(TAG, "BT enabled.");

BLEStatusData status_event{.status = BLEStatus::On};

if (xQueueSendToBack(getQueue(), &status_event, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send BLE enable event");
}
}

BLEService& BLEService::instance() {
// As long as it's called only from event dispatcher thread, no synchronization is fine.
static BLEService service;

return service;
}

BLEService::BLEService() : AbstractTask(sizeof(BLEStatusData)) {
BLEService::BLEService() : AbstractTask(sizeof(BLEStatusData), 10), enabled_(false) {
// There is no clean way to do that, because of static callbacks,
// so yeah, inject the queue to the static object

gatts_.setEventQueue(this->getQueue());
}

Expand All @@ -261,7 +282,14 @@ void BLEService::run() {
}

void BLEService::disable() {
BLEStatusData status_event{.status = BLEStatus::Off};

if (xQueueSendToBack(getQueue(), &status_event, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send BLE disable event");
}

// TODO: Implement
// enabled_ = false;
}


Expand Down
18 changes: 17 additions & 1 deletion components/for_esp32/ble_wrap/ble_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@ namespace bk {
/** @brief Abstraction over bluetooth low energy */
class BLEService : public AbstractTask {
public:
BLEService();
// Huh that's second singleton in the system.
// Does it converge to total singularity?
// Some may say that's antipattern, but I opt to have it in
// deeply embedded device, like this one.
// Alternativley components could use messaging, but amount
// of boilerplate that is required to write it is not worth
// my PRECIOUS time.
// Apart from that, BLE API uses static entities all over the place
// anyway, so not much loss adding another one
static BLEService& instance();

void start() override;
void run() override;
void enable();
void disable();

private:
BLEService();
// TODO: operators, cpy ctors...

bool enabled_;

};
} // namespace bk
6 changes: 4 additions & 2 deletions components/for_esp32/ble_wrap/file_transfer_gatts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ void FileTransferGATTS::gatts_profile_event_handler(esp_gatts_cb_event_t event,
conn_params.timeout = 400; // timeout = 400*10ms = 4000ms
// start sent the update connection parameters to the peer device.
esp_ble_gap_update_conn_params(&conn_params);

sendEvent(BLEStatusData{.status = BLEStatus::Connected});
break;
}
case ESP_GATTS_CREAT_ATTR_TAB_EVT: {
Expand Down Expand Up @@ -357,10 +359,10 @@ void FileTransferGATTS::setEventQueue(QueueHandle_t queue) {
this->queue_ = queue;
}

void FileTransferGATTS::sendEvent(BLEStatusData data) {
void FileTransferGATTS::sendEvent(const BLEStatusData &data) {
if (this->queue_ != nullptr) {
// TODO: sooner or later mutex will be needed here
if (xQueueOverwrite(queue_, &data) != pdPASS) {
if (xQueueSendToBack(queue_, &data, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send data on the queue");
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/for_esp32/ble_wrap/file_transfer_gatts.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FileTransferGATTS {
void setEventQueue(QueueHandle_t queue);

private:
void sendEvent(BLEStatusData event);
void sendEvent(const BLEStatusData &event);
// TODO: that will require a mutex to sync up between BT event thread, and fetching thread
std::vector<FileInfo> files_to_sync_;
std::unique_ptr<FileTransferTask> file_transfer_;
Expand Down
2 changes: 1 addition & 1 deletion components/gnss/NEO-7M-C/gnss.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "gnss.h"

#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
#include <esp_log.h>
#include <string.h>
#include <uart_wrapper.h>
Expand Down
23 changes: 10 additions & 13 deletions main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@

static const char* TAG = "Application";

void btTesting() {
bk::BLEService ble;
ble.enable();
// void btTesting() {
// bk::BLEService ble;
// ble.enable();

ESP_LOGI(TAG, "BLE enabled...");
// ESP_LOGI(TAG, "BLE enabled...");

bk::HealthService::reportAll();
// bk::HealthService::reportAll();

while (true) {
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
// while (true) {
// vTaskDelay(pdMS_TO_TICKS(2000));
// }
// }

void initializeNVS() {
esp_err_t ret = nvs_flash_init();
Expand All @@ -49,8 +49,6 @@ void StartApplication() {

initializeNVS();

// btTesting();

bk::Display display;

display.start();
Expand All @@ -59,10 +57,9 @@ void StartApplication() {
bk::GNSS gnss;
bk::Keypad keypad;
bk::TimeService time_service;
bk::BLEService ble;

bk::EventDispatcher dispatcher(
weather.getQueue(), gnss.getQueue(), keypad.getQueue(), time_service.getQueue(), ble.getQueue());
weather.getQueue(), gnss.getQueue(), keypad.getQueue(), time_service.getQueue(), bk::BLEService::instance().getQueue());

time_service.setEventDispatcher(&dispatcher);
// TODO: This is a dangling pointer - in theory
Expand Down
9 changes: 0 additions & 9 deletions main/event_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ void EventDispatcher::EventDispatcher::listenForEvents() {
if (xQueueReceive(time_q_, &time_data, 0) == pdPASS) {
ESP_LOGV(TAG, "Got TimeService event");

// TODO: remove
static int fake_event = 0;
BLEStatusData fake {.status = static_cast<BLEStatus>(fake_event)};
fake_event = (fake_event + 1) % 4;

if (xQueueSendToBack(bluetooth_q_, &fake, 0) != pdPASS) {
ESP_LOGE(TAG, "Failed to send fake data");
}

notifyTime(time_data);
}

Expand Down
7 changes: 6 additions & 1 deletion main/presenter/synchronize.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "synchronize.h"

#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "ble_service.h"
#include <paint.h>

#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include <esp_log.h>

namespace bk {
Expand Down Expand Up @@ -89,9 +91,12 @@ void SynchronizePresenter::onButtonPressed(const KeypadData &data) {
} else if (data.rd_pressed) {
// events_->widgetEvent(WidgetData{.new_widget = WidgetData::more});
// TODO: enable bluetooth
BLEService::instance().enable();
} else if (data.ld_pressed) {
// events_->widgetEvent(WidgetData{.new_widget = WidgetData::less});
// TODO: disable bluetooth
BLEService::instance().disable();

}
}
} // namespace bk
2 changes: 1 addition & 1 deletion partitions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# TODO: add OTA support (2 partitions)
nvs,data,nvs,0x9000,24K,
phy_init,data,phy,0xf000,4K,
factory,app,factory,0x10000,1M,
factory,app,factory,0x10000,2M,
# TODO: Decide on the size of the partition, now size is out of thin air.
storage, data, spiffs, , 1M,

0 comments on commit bf01c65

Please sign in to comment.