Commit 18cf4287 authored by schneider's avatar schneider
Browse files

feat(ble): Add error handling to HCI logging

parent d6ea08a7
...@@ -95,6 +95,7 @@ void WsfPDump(wsfPDumpType_t pdType, uint16_t length, uint8_t *pBuffer) ...@@ -95,6 +95,7 @@ void WsfPDump(wsfPDumpType_t pdType, uint16_t length, uint8_t *pBuffer)
{ {
uint32_t direction; uint32_t direction;
uint8_t type; uint8_t type;
int ret;
if (log_enabled) { if (log_enabled) {
switch (pdType) { switch (pdType) {
...@@ -131,11 +132,29 @@ void WsfPDump(wsfPDumpType_t pdType, uint16_t length, uint8_t *pBuffer) ...@@ -131,11 +132,29 @@ void WsfPDump(wsfPDumpType_t pdType, uint16_t length, uint8_t *pBuffer)
.timestamp_us_l = __htonl(timestamp_us & 0xFFFFFFFF) .timestamp_us_l = __htonl(timestamp_us & 0xFFFFFFFF)
}; };
epic_file_write(log_fd, &header, sizeof(header)); ret = epic_file_write(log_fd, &header, sizeof(header));
epic_file_write(log_fd, &type, sizeof(type)); if (ret != sizeof(header)) {
epic_file_write(log_fd, pBuffer, length); goto out_err;
}
ret = epic_file_write(log_fd, &type, sizeof(type));
if (ret != sizeof(type)) {
goto out_err;
}
ret = epic_file_write(log_fd, pBuffer, length);
if (ret != length) {
goto out_err;
}
log_dirty = true; log_dirty = true;
} }
return;
out_err:
LOG_WARN("ble", "Log file write failed. Logging diabled");
log_enabled = false;
} }
/*************************************************************************************************/ /*************************************************************************************************/
static void WsfInit(void) static void WsfInit(void)
...@@ -319,15 +338,25 @@ static void log_flush(void) ...@@ -319,15 +338,25 @@ static void log_flush(void)
} }
} }
/*************************************************************************************************/ /*************************************************************************************************/
static void log_rotate(void) static int log_rotate(void)
{ {
int i; int i;
char filename_old[16]; char filename_old[16];
char filename_new[16]; char filename_new[16];
struct epic_stat stat; struct epic_stat stat;
int ret;
if (epic_file_stat("logs/", &stat) != 0) { epic_file_stat("logs", &stat);
epic_file_mkdir("logs");
if (stat.type == EPICSTAT_FILE) {
return -1;
}
if (stat.type == EPICSTAT_NONE) {
ret = epic_file_mkdir("logs");
if (ret < 0) {
return ret;
}
} }
if (epic_file_stat("logs/ble9.log", &stat) == 0) { if (epic_file_stat("logs/ble9.log", &stat) == 0) {
...@@ -346,6 +375,43 @@ static void log_rotate(void) ...@@ -346,6 +375,43 @@ static void log_rotate(void)
if (epic_file_stat("logs/ble.log", &stat) == 0) { if (epic_file_stat("logs/ble.log", &stat) == 0) {
epic_file_rename("logs/ble.log", "logs/ble1.log"); epic_file_rename("logs/ble.log", "logs/ble1.log");
} }
return 0;
}
/*************************************************************************************************/
static void log_init(void)
{
int ret;
log_enabled = config_get_boolean_with_default("ble_log_enable", false);
if (!log_enabled) {
return;
}
LOG_INFO("ble", "Log is enabled");
if (log_rotate() < 0) {
log_enabled = false;
LOG_WARN("ble", "Can not rotate logs. Logging disabled.");
return;
}
log_fd = epic_file_open("logs/ble.log", "w");
if (log_fd < 0) {
log_enabled = false;
LOG_WARN("ble", "Can not create log file. Logging disabled.");
return;
}
ret = epic_file_write(log_fd, log_header, sizeof(log_header));
if (ret != sizeof(log_header)) {
log_enabled = false;
LOG_WARN(
"ble",
"Can not create log file header. Logging disabled."
);
return;
}
} }
/*************************************************************************************************/ /*************************************************************************************************/
void vBleTask(void *pvParameters) void vBleTask(void *pvParameters)
...@@ -357,14 +423,7 @@ void vBleTask(void *pvParameters) ...@@ -357,14 +423,7 @@ void vBleTask(void *pvParameters)
*/ */
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
log_enabled = config_get_boolean_with_default("ble_log_enable", false); log_init();
if (log_enabled) {
LOG_INFO("ble", "Log is enabled");
log_rotate();
log_fd = epic_file_open("logs/ble.log", "w");
epic_file_write(log_fd, log_header, sizeof(log_header));
}
/* We are going to execute FreeRTOS functions from callbacks /* We are going to execute FreeRTOS functions from callbacks
* coming from this interrupt. Its priority needs to be * coming from this interrupt. Its priority needs to be
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment