Update flipper and can_sniffer
This commit is contained in:
@@ -1,206 +1,325 @@
|
||||
/**
|
||||
* CAN Monitor for Flipper Zero
|
||||
*
|
||||
* Receives CAN sniffer statistics from RPI5 via UART.
|
||||
* Displays: IP address, total frames, pending frames, processed frames.
|
||||
* Multi-page application with handshake protocol.
|
||||
*
|
||||
* UART Configuration:
|
||||
* - TX: GPIO 13 (pin 13)
|
||||
* - RX: GPIO 14 (pin 14)
|
||||
* - Baud: 115200
|
||||
* - 8N1
|
||||
* Handshake:
|
||||
* 1. User presses OK on welcome screen
|
||||
* 2. Flipper sends: INIT:flipper\n
|
||||
* 3. RPI5 responds: ACK:rpi5,ip=x.x.x.x\n
|
||||
* 4. Flipper allows navigation to stats page
|
||||
* 5. RPI5 starts sending: STATS:ip=...,total=...,pending=...,processed=...\n
|
||||
*
|
||||
* Protocol: Text-based
|
||||
* Format: STATS:ip=<ip>,total=<n>,pending=<n>,processed=<n>\n
|
||||
* Example: STATS:ip=192.168.1.100,total=12345,pending=100,processed=12245\n
|
||||
* Wiring:
|
||||
* RPI5 TX (GPIO14, Pin 8) -> Flipper RX (Pin 14)
|
||||
* RPI5 RX (GPIO15, Pin 10) <- Flipper TX (Pin 13)
|
||||
* RPI5 GND (Pin 6) -> Flipper GND (Pin 8/11/18)
|
||||
*/
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <gui/gui.h>
|
||||
#include <gui/view_port.h>
|
||||
#include <notification/notification.h>
|
||||
#include <notification/notification_messages.h>
|
||||
|
||||
#include <expansion/expansion.h>
|
||||
#include <furi_hal_serial.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TAG "CANMonitor"
|
||||
|
||||
// UART configuration
|
||||
#define UART_CH FuriHalSerialIdUsart
|
||||
#define UART_BAUD 115200
|
||||
|
||||
// Buffer sizes
|
||||
#define RX_BUFFER_SIZE 256
|
||||
#define IP_BUFFER_SIZE 32
|
||||
|
||||
// Statistics structure
|
||||
// Connection state
|
||||
typedef enum {
|
||||
StateDisconnected,
|
||||
StateConnecting,
|
||||
StateConnected,
|
||||
} ConnectionState;
|
||||
|
||||
// Pages
|
||||
typedef enum {
|
||||
PageWelcome,
|
||||
PageStats,
|
||||
} AppPage;
|
||||
|
||||
// Statistics
|
||||
typedef struct {
|
||||
char ip_address[IP_BUFFER_SIZE];
|
||||
uint32_t total_frames;
|
||||
uint32_t pending_frames;
|
||||
uint32_t processed_frames;
|
||||
bool connected;
|
||||
bool data_received;
|
||||
uint32_t last_update_tick;
|
||||
} CanStats;
|
||||
|
||||
// Application context
|
||||
// App context
|
||||
typedef struct {
|
||||
Gui* gui;
|
||||
ViewPort* view_port;
|
||||
FuriMessageQueue* event_queue;
|
||||
FuriMutex* mutex;
|
||||
|
||||
FuriHalSerialHandle* serial_handle;
|
||||
FuriHalSerialHandle* serial;
|
||||
FuriStreamBuffer* rx_stream;
|
||||
FuriThread* rx_thread;
|
||||
FuriThread* worker_thread;
|
||||
|
||||
CanStats stats;
|
||||
bool running;
|
||||
AppPage current_page;
|
||||
ConnectionState conn_state;
|
||||
volatile bool running;
|
||||
volatile bool send_init;
|
||||
} CanMonitorApp;
|
||||
|
||||
// Event types
|
||||
typedef enum {
|
||||
EventTypeKey,
|
||||
EventTypeStats,
|
||||
} EventType;
|
||||
// Send data via UART
|
||||
static void uart_send(CanMonitorApp* app, const char* data) {
|
||||
if(app->serial) {
|
||||
furi_hal_serial_tx(app->serial, (uint8_t*)data, strlen(data));
|
||||
FURI_LOG_I(TAG, "TX: %s", data);
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
EventType type;
|
||||
InputEvent input;
|
||||
} CanMonitorEvent;
|
||||
// Parse ACK response: ACK:rpi5,ip=x.x.x.x
|
||||
static bool parse_ack(const char* line, CanStats* stats) {
|
||||
if(strncmp(line, "ACK:", 4) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse statistics from received line
|
||||
static bool parse_stats_line(const char* line, CanStats* stats) {
|
||||
// Format: STATS:ip=192.168.1.100,total=12345,pending=100,processed=12245
|
||||
const char* p = line + 4;
|
||||
|
||||
// Check for rpi5 identifier
|
||||
if(strncmp(p, "rpi5", 4) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse ip=
|
||||
const char* ip = strstr(p, "ip=");
|
||||
if(ip) {
|
||||
ip += 3;
|
||||
// Find end (comma, newline, or end of string)
|
||||
size_t len = 0;
|
||||
while(ip[len] && ip[len] != ',' && ip[len] != '\n' && ip[len] != '\r') {
|
||||
len++;
|
||||
}
|
||||
if(len > 0 && len < IP_BUFFER_SIZE) {
|
||||
memcpy(stats->ip_address, ip, len);
|
||||
stats->ip_address[len] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Parse STATS: STATS:ip=x.x.x.x,total=N,pending=N,processed=N
|
||||
static bool parse_stats(const char* line, CanStats* stats) {
|
||||
if(strncmp(line, "STATS:", 6) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* data = line + 6;
|
||||
const char* p = line + 6;
|
||||
|
||||
// Parse ip=
|
||||
const char* ip_start = strstr(data, "ip=");
|
||||
if(ip_start) {
|
||||
ip_start += 3;
|
||||
const char* ip_end = strchr(ip_start, ',');
|
||||
if(ip_end) {
|
||||
size_t len = ip_end - ip_start;
|
||||
if(len < IP_BUFFER_SIZE) {
|
||||
strncpy(stats->ip_address, ip_start, len);
|
||||
stats->ip_address[len] = '\0';
|
||||
}
|
||||
const char* ip = strstr(p, "ip=");
|
||||
if(ip) {
|
||||
ip += 3;
|
||||
const char* end = strchr(ip, ',');
|
||||
if(end && (size_t)(end - ip) < IP_BUFFER_SIZE) {
|
||||
size_t len = end - ip;
|
||||
memcpy(stats->ip_address, ip, len);
|
||||
stats->ip_address[len] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
// Parse total=
|
||||
const char* total_start = strstr(data, "total=");
|
||||
if(total_start) {
|
||||
stats->total_frames = strtoul(total_start + 6, NULL, 10);
|
||||
const char* total = strstr(p, "total=");
|
||||
if(total) {
|
||||
stats->total_frames = strtoul(total + 6, NULL, 10);
|
||||
}
|
||||
|
||||
// Parse pending=
|
||||
const char* pending_start = strstr(data, "pending=");
|
||||
if(pending_start) {
|
||||
stats->pending_frames = strtoul(pending_start + 8, NULL, 10);
|
||||
const char* pending = strstr(p, "pending=");
|
||||
if(pending) {
|
||||
stats->pending_frames = strtoul(pending + 8, NULL, 10);
|
||||
}
|
||||
|
||||
// Parse processed=
|
||||
const char* processed_start = strstr(data, "processed=");
|
||||
if(processed_start) {
|
||||
stats->processed_frames = strtoul(processed_start + 10, NULL, 10);
|
||||
const char* processed = strstr(p, "processed=");
|
||||
if(processed) {
|
||||
stats->processed_frames = strtoul(processed + 10, NULL, 10);
|
||||
}
|
||||
|
||||
stats->connected = true;
|
||||
stats->data_received = true;
|
||||
stats->last_update_tick = furi_get_tick();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// UART receive callback
|
||||
static void uart_rx_callback(
|
||||
FuriHalSerialHandle* handle,
|
||||
FuriHalSerialRxEvent event,
|
||||
void* context) {
|
||||
CanMonitorApp* app = context;
|
||||
UNUSED(handle);
|
||||
// Process received line
|
||||
static void process_line(CanMonitorApp* app, const char* line) {
|
||||
FURI_LOG_I(TAG, "RX: %s", line);
|
||||
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
|
||||
if(app->conn_state == StateConnecting) {
|
||||
// Waiting for ACK
|
||||
if(parse_ack(line, &app->stats)) {
|
||||
FURI_LOG_I(TAG, "ACK received, IP: %s", app->stats.ip_address);
|
||||
app->conn_state = StateConnected;
|
||||
}
|
||||
} else if(app->conn_state == StateConnected) {
|
||||
// Parse stats
|
||||
parse_stats(line, &app->stats);
|
||||
}
|
||||
|
||||
furi_mutex_release(app->mutex);
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
|
||||
// UART RX callback
|
||||
static void uart_callback(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* ctx) {
|
||||
CanMonitorApp* app = ctx;
|
||||
|
||||
if(event == FuriHalSerialRxEventData) {
|
||||
uint8_t data = furi_hal_serial_async_rx(handle);
|
||||
furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
|
||||
uint8_t byte = furi_hal_serial_async_rx(handle);
|
||||
furi_stream_buffer_send(app->rx_stream, &byte, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// UART receive thread
|
||||
static int32_t uart_rx_thread(void* context) {
|
||||
CanMonitorApp* app = context;
|
||||
char rx_buffer[RX_BUFFER_SIZE];
|
||||
size_t rx_index = 0;
|
||||
// Worker thread
|
||||
static int32_t worker_thread(void* ctx) {
|
||||
CanMonitorApp* app = ctx;
|
||||
char buffer[RX_BUFFER_SIZE];
|
||||
size_t idx = 0;
|
||||
uint32_t last_rx_tick = 0;
|
||||
|
||||
FURI_LOG_I(TAG, "Worker started");
|
||||
|
||||
while(app->running) {
|
||||
uint8_t data;
|
||||
size_t len = furi_stream_buffer_receive(app->rx_stream, &data, 1, 100);
|
||||
// Check if we need to send INIT
|
||||
if(app->send_init) {
|
||||
app->send_init = false;
|
||||
uart_send(app, "INIT:flipper\n");
|
||||
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
app->conn_state = StateConnecting;
|
||||
furi_mutex_release(app->mutex);
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
|
||||
// Receive data
|
||||
uint8_t byte;
|
||||
size_t len = furi_stream_buffer_receive(app->rx_stream, &byte, 1, 50);
|
||||
|
||||
if(len > 0) {
|
||||
if(data == '\n' || data == '\r') {
|
||||
if(rx_index > 0) {
|
||||
rx_buffer[rx_index] = '\0';
|
||||
last_rx_tick = furi_get_tick();
|
||||
|
||||
// Parse the received line
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
if(parse_stats_line(rx_buffer, &app->stats)) {
|
||||
// Notify view to redraw
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
furi_mutex_release(app->mutex);
|
||||
|
||||
rx_index = 0;
|
||||
if(byte == '\n' || byte == '\r') {
|
||||
if(idx > 0) {
|
||||
buffer[idx] = '\0';
|
||||
process_line(app, buffer);
|
||||
idx = 0;
|
||||
}
|
||||
} else if(rx_index < RX_BUFFER_SIZE - 1) {
|
||||
rx_buffer[rx_index++] = data;
|
||||
} else if(idx < RX_BUFFER_SIZE - 1) {
|
||||
buffer[idx++] = byte;
|
||||
}
|
||||
} else {
|
||||
// Timeout: parse partial data after 500ms
|
||||
if(idx > 0 && (furi_get_tick() - last_rx_tick) > 500) {
|
||||
buffer[idx] = '\0';
|
||||
process_line(app, buffer);
|
||||
idx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Check connection timeout (5 seconds)
|
||||
// Check data timeout (5 sec) - only when connected
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
if(app->stats.connected &&
|
||||
(furi_get_tick() - app->stats.last_update_tick) > 5000) {
|
||||
app->stats.connected = false;
|
||||
view_port_update(app->view_port);
|
||||
if(app->conn_state == StateConnected && app->stats.data_received) {
|
||||
if((furi_get_tick() - app->stats.last_update_tick) > 5000) {
|
||||
app->stats.data_received = false;
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
}
|
||||
furi_mutex_release(app->mutex);
|
||||
}
|
||||
|
||||
FURI_LOG_I(TAG, "Worker stopped");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Draw callback
|
||||
static void draw_callback(Canvas* canvas, void* context) {
|
||||
CanMonitorApp* app = context;
|
||||
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
|
||||
// Draw welcome page
|
||||
static void draw_welcome(Canvas* canvas, CanMonitorApp* app) {
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
|
||||
// Title
|
||||
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "CAN Monitor");
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str_aligned(canvas, 64, 8, AlignCenter, AlignCenter, "CAN Monitor");
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
|
||||
// Connection status
|
||||
if(app->stats.connected) {
|
||||
canvas_draw_str(canvas, 0, 14, "Status: Connected");
|
||||
// Subtitle
|
||||
canvas_draw_str_aligned(canvas, 64, 22, AlignCenter, AlignCenter, "RPI5 CAN Sniffer");
|
||||
|
||||
// Status based on connection state
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
ConnectionState state = app->conn_state;
|
||||
char ip_buf[IP_BUFFER_SIZE];
|
||||
strncpy(ip_buf, app->stats.ip_address, IP_BUFFER_SIZE);
|
||||
furi_mutex_release(app->mutex);
|
||||
|
||||
switch(state) {
|
||||
case StateDisconnected:
|
||||
canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, "Press OK to connect");
|
||||
canvas_draw_str_aligned(canvas, 64, 50, AlignCenter, AlignCenter, "[OK]");
|
||||
break;
|
||||
|
||||
case StateConnecting:
|
||||
canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, "Connecting...");
|
||||
canvas_draw_str_aligned(canvas, 64, 50, AlignCenter, AlignCenter, "Waiting for RPI5");
|
||||
break;
|
||||
|
||||
case StateConnected:
|
||||
canvas_draw_str_aligned(canvas, 64, 34, AlignCenter, AlignCenter, "Connected!");
|
||||
if(strlen(ip_buf) > 0) {
|
||||
char buf[48];
|
||||
snprintf(buf, sizeof(buf), "RPI5: %s", ip_buf);
|
||||
canvas_draw_str_aligned(canvas, 64, 46, AlignCenter, AlignCenter, buf);
|
||||
}
|
||||
canvas_draw_str_aligned(canvas, 64, 58, AlignCenter, AlignCenter, "Press RIGHT >");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw stats page
|
||||
static void draw_stats(Canvas* canvas, CanMonitorApp* app) {
|
||||
char buf[64];
|
||||
|
||||
canvas_clear(canvas);
|
||||
|
||||
// Header
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 10, 10, "CAN Monitor");
|
||||
|
||||
// Navigation hint
|
||||
canvas_draw_str(canvas, 0, 10, "<");
|
||||
|
||||
// Connection indicator
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
bool data_ok = app->stats.data_received;
|
||||
furi_mutex_release(app->mutex);
|
||||
|
||||
if(data_ok) {
|
||||
canvas_draw_disc(canvas, 120, 6, 4);
|
||||
} else {
|
||||
canvas_draw_str(canvas, 0, 14, "Status: Waiting...");
|
||||
canvas_draw_circle(canvas, 120, 6, 4);
|
||||
}
|
||||
|
||||
// Separator
|
||||
canvas_draw_line(canvas, 0, 14, 128, 14);
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
|
||||
// IP Address
|
||||
char buf[64];
|
||||
if(strlen(app->stats.ip_address) > 0) {
|
||||
snprintf(buf, sizeof(buf), "IP: %s", app->stats.ip_address);
|
||||
} else {
|
||||
@@ -209,7 +328,7 @@ static void draw_callback(Canvas* canvas, void* context) {
|
||||
canvas_draw_str(canvas, 0, 26, buf);
|
||||
|
||||
// Total frames
|
||||
snprintf(buf, sizeof(buf), "Total: %lu", (unsigned long)app->stats.total_frames);
|
||||
snprintf(buf, sizeof(buf), "Total frames: %lu", (unsigned long)app->stats.total_frames);
|
||||
canvas_draw_str(canvas, 0, 38, buf);
|
||||
|
||||
// Pending frames
|
||||
@@ -218,110 +337,158 @@ static void draw_callback(Canvas* canvas, void* context) {
|
||||
|
||||
// Processed frames
|
||||
snprintf(buf, sizeof(buf), "Processed: %lu", (unsigned long)app->stats.processed_frames);
|
||||
canvas_draw_str(canvas, 0, 62, buf);
|
||||
canvas_draw_str(canvas, 4, 62, buf);
|
||||
|
||||
furi_mutex_release(app->mutex);
|
||||
}
|
||||
|
||||
// Input callback
|
||||
static void input_callback(InputEvent* input_event, void* context) {
|
||||
CanMonitorApp* app = context;
|
||||
CanMonitorEvent event = {.type = EventTypeKey, .input = *input_event};
|
||||
furi_message_queue_put(app->event_queue, &event, FuriWaitForever);
|
||||
// Draw callback
|
||||
static void draw_callback(Canvas* canvas, void* ctx) {
|
||||
CanMonitorApp* app = ctx;
|
||||
|
||||
switch(app->current_page) {
|
||||
case PageWelcome:
|
||||
draw_welcome(canvas, app);
|
||||
break;
|
||||
case PageStats:
|
||||
draw_stats(canvas, app);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize application
|
||||
static CanMonitorApp* can_monitor_app_alloc(void) {
|
||||
// Input callback
|
||||
static void input_callback(InputEvent* event, void* ctx) {
|
||||
CanMonitorApp* app = ctx;
|
||||
|
||||
if(event->type == InputTypeShort) {
|
||||
furi_mutex_acquire(app->mutex, FuriWaitForever);
|
||||
|
||||
switch(event->key) {
|
||||
case InputKeyOk:
|
||||
if(app->current_page == PageWelcome && app->conn_state == StateDisconnected) {
|
||||
// Start handshake
|
||||
app->send_init = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case InputKeyRight:
|
||||
// Only allow if connected
|
||||
if(app->current_page == PageWelcome && app->conn_state == StateConnected) {
|
||||
app->current_page = PageStats;
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
break;
|
||||
|
||||
case InputKeyLeft:
|
||||
if(app->current_page == PageStats) {
|
||||
app->current_page = PageWelcome;
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
break;
|
||||
|
||||
case InputKeyBack:
|
||||
if(app->current_page == PageWelcome) {
|
||||
app->running = false;
|
||||
} else {
|
||||
app->current_page = PageWelcome;
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
furi_mutex_release(app->mutex);
|
||||
}
|
||||
}
|
||||
|
||||
// Main entry point
|
||||
int32_t can_monitor_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
FURI_LOG_I(TAG, "Starting CAN Monitor");
|
||||
|
||||
// Allocate app
|
||||
CanMonitorApp* app = malloc(sizeof(CanMonitorApp));
|
||||
memset(app, 0, sizeof(CanMonitorApp));
|
||||
|
||||
app->running = true;
|
||||
app->current_page = PageWelcome;
|
||||
app->conn_state = StateDisconnected;
|
||||
app->send_init = false;
|
||||
|
||||
// Mutex
|
||||
app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
app->event_queue = furi_message_queue_alloc(8, sizeof(CanMonitorEvent));
|
||||
|
||||
// Initialize stats
|
||||
memset(&app->stats, 0, sizeof(CanStats));
|
||||
strcpy(app->stats.ip_address, "");
|
||||
|
||||
// Disable expansion protocol to use UART
|
||||
// Disable expansion protocol
|
||||
Expansion* expansion = furi_record_open(RECORD_EXPANSION);
|
||||
expansion_disable(expansion);
|
||||
furi_record_close(RECORD_EXPANSION);
|
||||
|
||||
// Initialize UART
|
||||
FURI_LOG_I(TAG, "Expansion disabled");
|
||||
|
||||
// Init UART
|
||||
app->rx_stream = furi_stream_buffer_alloc(RX_BUFFER_SIZE, 1);
|
||||
app->serial_handle = furi_hal_serial_control_acquire(UART_CH);
|
||||
furi_hal_serial_init(app->serial_handle, UART_BAUD);
|
||||
furi_hal_serial_async_rx_start(app->serial_handle, uart_rx_callback, app, false);
|
||||
app->serial = furi_hal_serial_control_acquire(FuriHalSerialIdUsart);
|
||||
|
||||
// Start RX thread
|
||||
app->rx_thread = furi_thread_alloc_ex("CANMonitorRx", 1024, uart_rx_thread, app);
|
||||
furi_thread_start(app->rx_thread);
|
||||
if(app->serial) {
|
||||
furi_hal_serial_init(app->serial, UART_BAUD);
|
||||
furi_hal_serial_async_rx_start(app->serial, uart_callback, app, false);
|
||||
FURI_LOG_I(TAG, "UART initialized at %d baud", UART_BAUD);
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to acquire UART");
|
||||
}
|
||||
|
||||
// Initialize GUI
|
||||
// Start worker
|
||||
app->worker_thread = furi_thread_alloc_ex("CanMonitorWorker", 1024, worker_thread, app);
|
||||
furi_thread_start(app->worker_thread);
|
||||
|
||||
// Init GUI
|
||||
app->gui = furi_record_open(RECORD_GUI);
|
||||
app->view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(app->view_port, draw_callback, app);
|
||||
view_port_input_callback_set(app->view_port, input_callback, app);
|
||||
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
|
||||
|
||||
return app;
|
||||
}
|
||||
FURI_LOG_I(TAG, "GUI initialized, waiting for user input");
|
||||
|
||||
// Free application
|
||||
static void can_monitor_app_free(CanMonitorApp* app) {
|
||||
// Stop RX thread
|
||||
app->running = false;
|
||||
furi_thread_join(app->rx_thread);
|
||||
furi_thread_free(app->rx_thread);
|
||||
// Main loop
|
||||
while(app->running) {
|
||||
furi_delay_ms(100);
|
||||
}
|
||||
|
||||
// Deinitialize UART
|
||||
furi_hal_serial_async_rx_stop(app->serial_handle);
|
||||
furi_hal_serial_deinit(app->serial_handle);
|
||||
furi_hal_serial_control_release(app->serial_handle);
|
||||
FURI_LOG_I(TAG, "Shutting down");
|
||||
|
||||
// Send disconnect signal
|
||||
if(app->conn_state == StateConnected) {
|
||||
uart_send(app, "STOP:flipper\n");
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
furi_thread_join(app->worker_thread);
|
||||
furi_thread_free(app->worker_thread);
|
||||
|
||||
if(app->serial) {
|
||||
furi_hal_serial_async_rx_stop(app->serial);
|
||||
furi_hal_serial_deinit(app->serial);
|
||||
furi_hal_serial_control_release(app->serial);
|
||||
}
|
||||
furi_stream_buffer_free(app->rx_stream);
|
||||
|
||||
// Re-enable expansion protocol
|
||||
Expansion* expansion = furi_record_open(RECORD_EXPANSION);
|
||||
// Re-enable expansion
|
||||
expansion = furi_record_open(RECORD_EXPANSION);
|
||||
expansion_enable(expansion);
|
||||
furi_record_close(RECORD_EXPANSION);
|
||||
|
||||
// Free GUI
|
||||
view_port_enabled_set(app->view_port, false);
|
||||
gui_remove_view_port(app->gui, app->view_port);
|
||||
view_port_free(app->view_port);
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
furi_message_queue_free(app->event_queue);
|
||||
furi_mutex_free(app->mutex);
|
||||
|
||||
free(app);
|
||||
}
|
||||
|
||||
// Main application entry point
|
||||
int32_t can_monitor_app(void* p) {
|
||||
UNUSED(p);
|
||||
|
||||
CanMonitorApp* app = can_monitor_app_alloc();
|
||||
|
||||
CanMonitorEvent event;
|
||||
bool running = true;
|
||||
|
||||
// Main event loop
|
||||
while(running) {
|
||||
if(furi_message_queue_get(app->event_queue, &event, 100) == FuriStatusOk) {
|
||||
if(event.type == EventTypeKey) {
|
||||
if(event.input.type == InputTypeShort ||
|
||||
event.input.type == InputTypeLong) {
|
||||
if(event.input.key == InputKeyBack) {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
can_monitor_app_free(app);
|
||||
FURI_LOG_I(TAG, "Bye!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user