103 lines
2.8 KiB
C
103 lines
2.8 KiB
C
#include <zephyr/kernel.h>
|
|
#include <zephyr/logging/log.h>
|
|
#include <string.h>
|
|
|
|
#include "buzz_proto.h"
|
|
#include "settings_mgmt.h"
|
|
#include "batt_mgmt.h"
|
|
// #include "fw_mgmt.h"
|
|
// #include "audio.h"
|
|
|
|
LOG_MODULE_REGISTER(main);
|
|
|
|
static const char *battery_state_to_str(batt_mgmt_state_t state)
|
|
{
|
|
switch (state) {
|
|
case BATT_STATE_DISCHARGING:
|
|
return "discharging";
|
|
case BATT_STATE_FULL:
|
|
return "full";
|
|
case BATT_STATE_CHARGING:
|
|
return "charging";
|
|
case BATT_STATE_ERROR:
|
|
return "error";
|
|
default:
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
#if IS_ENABLED(CONFIG_BLE_MGMT)
|
|
#include "ble_mgmt.h"
|
|
void ble_rx_cb(const uint8_t *data, uint16_t len)
|
|
{
|
|
uint8_t *buf;
|
|
|
|
/* 1. Länge prüfen (darf SLAB_BLOCK_SIZE = 256 nicht überschreiten) */
|
|
if (len > CONFIG_BUZZ_PROTO_SLAB_SIZE) {
|
|
LOG_ERR("Received data too large for proto buf (%u > %u)", len, CONFIG_BUZZ_PROTO_SLAB_SIZE);
|
|
return;
|
|
}
|
|
|
|
/* 2. Speicher aus dem Protokoll-Slab-Pool anfordern (Zero-Wait) */
|
|
if (buzz_proto_buf_alloc(&buf) != 0) {
|
|
LOG_ERR("No free memory slabs for incoming BLE frame!");
|
|
return;
|
|
}
|
|
|
|
/* 3. Daten in den allokierten Slab kopieren */
|
|
memcpy(buf, data, len);
|
|
|
|
/* 4. Nachrichten-Struktur für den Protokoll-Thread füllen */
|
|
struct buzz_frame_msg msg = {
|
|
.data_ptr = buf,
|
|
.length = len,
|
|
.reply_cb = ble_mgmt_send,
|
|
.max_payload = ble_mgmt_get_max_payload(),
|
|
};
|
|
|
|
/* 5. Frame asynchron an den Protokoll-Thread übergeben */
|
|
if (buzz_proto_submit_frame(&msg) != 0) {
|
|
LOG_ERR("Failed to submit frame to proto thread (Queue full)");
|
|
buzz_proto_buf_free(&buf); /* Speicher bei Fehler sofort wieder freigeben */
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
int main(void)
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_BLE_MGMT)
|
|
/* BLE-Subsystem initialisieren und RX-Callback registrieren */
|
|
int rc = ble_mgmt_init(ble_rx_cb, app_cfg.dev_name);
|
|
if (rc < 0) {
|
|
LOG_ERR("Failed to initialize BLE management: %d", rc);
|
|
return rc;
|
|
}
|
|
#else
|
|
LOG_WRN("BLE not enabled");
|
|
#endif
|
|
|
|
#if IS_ENABLED(CONFIG_LOG)
|
|
#if IS_ENABLED(CONFIG_BATT_MGMT)
|
|
k_sleep(K_SECONDS(1));
|
|
batt_mgmt_info_t batt_info;
|
|
int batt_rc = batt_mgmt_get_info(&batt_info);
|
|
if (batt_rc == 0) {
|
|
LOG_INF("Battery after 1s: %d mV, %u%%, level=%u, state=%s (%d)",
|
|
batt_info.voltage_mv,
|
|
batt_info.percent,
|
|
batt_info.level,
|
|
battery_state_to_str(batt_info.state),
|
|
batt_info.state);
|
|
} else {
|
|
LOG_WRN("Battery info read failed: %d", batt_rc);
|
|
}
|
|
#endif // CONFIG_BATT_MGMT
|
|
#endif // CONFIG_LOG
|
|
|
|
for (;;) {
|
|
int32_t rem_ms = k_sleep(K_FOREVER);
|
|
LOG_WRN("main woke unexpectedly (remaining=%d ms)", rem_ms);
|
|
}
|
|
} |