| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- #include <furi.h>
- #include <gui/gui.h>
- #include <notification/notification.h>
- #include <notification/notification_messages.h>
- #include <gui/elements.h>
- #include <furi_hal.h>
- #include <gui/view_dispatcher.h>
- #include <gui/modules/dialog_ex.h>
- #include <expansion/expansion.h>
- #include "../include/pwnagotchi.h"
- #include "../include/protocol.h"
- #include "../include/constants.h"
- #include "../include/message_queue.h"
- #define LINES_ON_SCREEN 6
- #define COLUMNS_ON_SCREEN 21
- typedef struct PwnDumpModel PwnDumpModel;
- typedef struct {
- Gui* gui;
- NotificationApp* notification;
- ViewDispatcher* view_dispatcher;
- View* view;
- FuriThread* worker_thread;
- FuriStreamBuffer* rx_stream;
- FuriHalSerialHandle* serial_handle;
- } FlipagotchiApp;
- typedef struct {
- FuriString* text;
- } ListElement;
- struct PwnDumpModel {
- MessageQueue* queue;
- Pwnagotchi* pwn;
- };
- typedef enum {
- WorkerEventReserved = (1 << 0), // Reserved for StreamBuffer internal event
- WorkerEventStop = (1 << 1),
- WorkerEventRx = (1 << 2),
- } WorkerEventFlags;
- #define WORKER_EVENTS_MASK (WorkerEventStop | WorkerEventRx)
- const NotificationSequence sequence_notification = {
- &message_display_backlight_on,
- &message_green_255,
- &message_delay_10,
- NULL,
- };
- static bool flipagotchi_exec_cmd(PwnDumpModel* model) {
- if(message_queue_has_message(model->queue)) {
- PwnCommand cmd;
- message_queue_pop_message(model->queue, &cmd);
- FURI_LOG_D("PWN", "Has message (code: %d), processing...", cmd.parameterCode);
- // See what the cmd wants
- switch(cmd.parameterCode) {
- // Process Face
- case 0x04: {
- // Adding 4 to account for the offset that is required above 0x03
- int face = cmd.arguments[0] - 4;
- if(face < 0) {
- face = 0;
- }
- model->pwn->face = cmd.arguments[0] - 4;
- break;
- }
- // Process Name
- case 0x05: {
- // Write over hostname with nothing
- strncpy(model->pwn->hostname, "", PWNAGOTCHI_MAX_HOSTNAME_LEN);
- for(size_t i = 0; i < PWNAGOTCHI_MAX_HOSTNAME_LEN; i++) {
- // Break if we hit the end of the name
- if(cmd.arguments[i] == 0x00) {
- break;
- }
- model->pwn->hostname[i] = cmd.arguments[i];
- }
- break;
- }
- // Process channel
- case 0x06: {
- // Write over channel with nothing
- strncpy(model->pwn->channel, "", PWNAGOTCHI_MAX_CHANNEL_LEN);
- for(size_t i = 0; i < PWNAGOTCHI_MAX_CHANNEL_LEN; i++) {
- // Break if we hit the end of the name
- if(cmd.arguments[i] == 0x00) {
- break;
- }
- model->pwn->channel[i] = cmd.arguments[i];
- }
- break;
- }
- // Process APS (Access Points)
- case 0x07: {
- // Write over APS with nothing
- strncpy(model->pwn->apStat, "", PWNAGOTCHI_MAX_APS_LEN);
- for(size_t i = 0; i < PWNAGOTCHI_MAX_APS_LEN; i++) {
- // Break if we hit the end of the name
- if(cmd.arguments[i] == 0x00) {
- break;
- }
- model->pwn->apStat[i] = cmd.arguments[i];
- }
- break;
- }
- // Process uptime
- case 0x08: {
- // Write over uptime with nothing
- strncpy(model->pwn->uptime, "", PWNAGOTCHI_MAX_UPTIME_LEN);
- for(size_t i = 0; i < PWNAGOTCHI_MAX_UPTIME_LEN; i++) {
- // Break if we hit the end of the name
- if(cmd.arguments[i] == 0x00) {
- break;
- }
- model->pwn->uptime[i] = cmd.arguments[i];
- }
- break;
- }
- // Process friend
- case 0x09: {
- // Friend not implemented yet
- break;
- }
- // Process mode
- case 0x0a: {
- enum PwnagotchiMode mode;
- switch(cmd.arguments[0]) {
- case 0x04:
- mode = PwnMode_Manual;
- break;
- case 0x05:
- mode = PwnMode_Auto;
- break;
- case 0x06:
- mode = PwnMode_Ai;
- break;
- default:
- mode = PwnMode_Manual;
- break;
- }
- model->pwn->mode = mode;
- break;
- }
- // Process Handshakes
- case 0x0b: {
- // Write over handshakes with nothing
- strncpy(model->pwn->handshakes, "", PWNAGOTCHI_MAX_HANDSHAKES_LEN);
- for(size_t i = 0; i < PWNAGOTCHI_MAX_HANDSHAKES_LEN; i++) {
- // Break if we hit the end of the name
- if(cmd.arguments[i] == 0x00) {
- break;
- }
- model->pwn->handshakes[i] = cmd.arguments[i];
- }
- break;
- }
- // Process message
- case 0x0c: {
- // Write over the message with nothing
- strncpy(model->pwn->message, "", PWNAGOTCHI_MAX_MESSAGE_LEN);
- for(size_t i = 0; i < PWNAGOTCHI_MAX_MESSAGE_LEN; i++) {
- // Break if we hit the end of the name
- if(cmd.arguments[i] == 0x00) {
- break;
- }
- model->pwn->message[i] = cmd.arguments[i];
- }
- break;
- }
- }
- }
- return false;
- }
- static void flipagotchi_view_draw_callback(Canvas* canvas, void* _model) {
- PwnDumpModel* model = _model;
- pwnagotchi_draw_all(model->pwn, canvas);
- }
- static bool flipagotchi_view_input_callback(InputEvent* event, void* context) {
- UNUSED(event);
- UNUSED(context);
- return false;
- }
- static uint32_t flipagotchi_exit(void* context) {
- UNUSED(context);
- return VIEW_NONE;
- }
- static void
- flipagotchi_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
- furi_assert(context);
- FlipagotchiApp* app = context;
- if(event == FuriHalSerialRxEventData) {
- uint8_t data = furi_hal_serial_async_rx(handle);
- furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
- furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
- }
- }
- static void flipagotchi_push_to_list(PwnDumpModel* model, const char data) {
- message_queue_push_byte(model->queue, data);
- }
- static int32_t flipagotchi_worker(void* context) {
- furi_assert(context);
- FlipagotchiApp* app = context;
- while(true) {
- bool update = false;
- uint32_t events =
- furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
- furi_check((events & FuriFlagError) == 0);
- if(events & WorkerEventStop) break;
- if(events & WorkerEventRx) {
- size_t length = 0;
- do {
- uint8_t data[1];
- length = furi_stream_buffer_receive(app->rx_stream, data, 1, 0);
- if(length > 0) {
- with_view_model(
- app->view,
- PwnDumpModel * model,
- {
- for(size_t i = 0; i < length; i++) {
- flipagotchi_push_to_list(model, data[i]);
- }
- update = flipagotchi_exec_cmd(model);
- },
- update);
- }
- } while(length > 0);
- notification_message(app->notification, &sequence_notification);
- // with_view_model(
- // app->view, PwnDumpModel * model, { UNUSED(model); }, true);
- }
- }
- return 0;
- }
- static FlipagotchiApp* flipagotchi_app_alloc() {
- FlipagotchiApp* app = malloc(sizeof(FlipagotchiApp));
- app->rx_stream = furi_stream_buffer_alloc(2048, 1);
- // Gui
- app->gui = furi_record_open(RECORD_GUI);
- app->notification = furi_record_open(RECORD_NOTIFICATION);
- // View dispatcher
- app->view_dispatcher = view_dispatcher_alloc();
- view_dispatcher_enable_queue(app->view_dispatcher);
- view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
- // Views
- app->view = view_alloc();
- view_set_draw_callback(app->view, flipagotchi_view_draw_callback);
- view_set_input_callback(app->view, flipagotchi_view_input_callback);
- view_allocate_model(app->view, ViewModelTypeLocking, sizeof(PwnDumpModel));
- with_view_model(
- app->view,
- PwnDumpModel * model,
- {
- model->queue = message_queue_alloc();
- model->pwn = pwnagotchi_alloc();
- },
- true);
- view_set_previous_callback(app->view, flipagotchi_exit);
- view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
- view_dispatcher_switch_to_view(app->view_dispatcher, 0);
- // Enable uart listener
- app->serial_handle = furi_hal_serial_control_acquire(PWNAGOTCHI_UART_CHANNEL);
- furi_check(app->serial_handle);
- furi_hal_serial_init(app->serial_handle, PWNAGOTCHI_UART_BAUD);
- furi_hal_serial_async_rx_start(app->serial_handle, flipagotchi_on_irq_cb, app, false);
- app->worker_thread = furi_thread_alloc();
- furi_thread_set_name(app->worker_thread, "UsbUartWorker");
- furi_thread_set_stack_size(app->worker_thread, 1024);
- furi_thread_set_context(app->worker_thread, app);
- furi_thread_set_callback(app->worker_thread, flipagotchi_worker);
- furi_thread_start(app->worker_thread);
- return app;
- }
- static void flipagotchi_app_free(FlipagotchiApp* app) {
- furi_assert(app);
- 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_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
- furi_thread_join(app->worker_thread);
- furi_thread_free(app->worker_thread);
- // Free views
- view_dispatcher_remove_view(app->view_dispatcher, 0);
- with_view_model(
- app->view,
- PwnDumpModel * model,
- {
- message_queue_free(model->queue);
- pwnagotchi_free(model->pwn);
- },
- true);
- view_free(app->view);
- view_dispatcher_free(app->view_dispatcher);
- // Close gui record
- furi_record_close(RECORD_GUI);
- furi_record_close(RECORD_NOTIFICATION);
- app->gui = NULL;
- furi_stream_buffer_free(app->rx_stream);
- // Free rest
- free(app);
- }
- int32_t flipagotchi_app(void* p) {
- UNUSED(p);
- // Disable expansion protocol to avoid interference with UART Handle
- Expansion* expansion = furi_record_open(RECORD_EXPANSION);
- expansion_disable(expansion);
- FlipagotchiApp* app = flipagotchi_app_alloc();
- view_dispatcher_run(app->view_dispatcher);
- flipagotchi_app_free(app);
- // Return previous state of expansion
- expansion_enable(expansion);
- furi_record_close(RECORD_EXPANSION);
- return 0;
- }
|