flipagotchi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <notification/notification.h>
  4. #include <notification/notification_messages.h>
  5. #include <gui/elements.h>
  6. #include <furi_hal_uart.h>
  7. #include <furi_hal_console.h>
  8. #include <gui/view_dispatcher.h>
  9. #include <gui/modules/dialog_ex.h>
  10. #include "../include/pwnagotchi.h"
  11. #include "../include/protocol.h"
  12. #include "../include/constants.h"
  13. #include "../include/message_queue.h"
  14. #define LINES_ON_SCREEN 6
  15. #define COLUMNS_ON_SCREEN 21
  16. typedef struct PwnDumpModel PwnDumpModel;
  17. typedef struct {
  18. Gui* gui;
  19. NotificationApp* notification;
  20. ViewDispatcher* view_dispatcher;
  21. View* view;
  22. FuriThread* worker_thread;
  23. FuriStreamBuffer* rx_stream;
  24. } FlipagotchiApp;
  25. typedef struct {
  26. FuriString* text;
  27. } ListElement;
  28. struct PwnDumpModel {
  29. MessageQueue* queue;
  30. Pwnagotchi* pwn;
  31. };
  32. typedef enum {
  33. WorkerEventReserved = (1 << 0), // Reserved for StreamBuffer internal event
  34. WorkerEventStop = (1 << 1),
  35. WorkerEventRx = (1 << 2),
  36. } WorkerEventFlags;
  37. #define WORKER_EVENTS_MASK (WorkerEventStop | WorkerEventRx)
  38. const NotificationSequence sequence_notification = {
  39. &message_display_backlight_on,
  40. &message_green_255,
  41. &message_delay_10,
  42. NULL,
  43. };
  44. static bool flipagotchi_exec_cmd(PwnDumpModel* model) {
  45. if(message_queue_has_message(model->queue)) {
  46. PwnCommand cmd;
  47. message_queue_pop_message(model->queue, &cmd);
  48. FURI_LOG_D("PWN", "Has message (code: %d), processing...", cmd.parameterCode);
  49. // See what the cmd wants
  50. switch(cmd.parameterCode) {
  51. // Process Face
  52. case 0x04: {
  53. // Adding 4 to account for the offset that is required above 0x03
  54. int face = cmd.arguments[0] - 4;
  55. if(face < 0) {
  56. face = 0;
  57. }
  58. model->pwn->face = cmd.arguments[0] - 4;
  59. break;
  60. }
  61. // Process Name
  62. case 0x05: {
  63. // Write over hostname with nothing
  64. strncpy(model->pwn->hostname, "", PWNAGOTCHI_MAX_HOSTNAME_LEN);
  65. for(size_t i = 0; i < PWNAGOTCHI_MAX_HOSTNAME_LEN; i++) {
  66. // Break if we hit the end of the name
  67. if(cmd.arguments[i] == 0x00) {
  68. break;
  69. }
  70. model->pwn->hostname[i] = cmd.arguments[i];
  71. }
  72. break;
  73. }
  74. // Process channel
  75. case 0x06: {
  76. // Write over channel with nothing
  77. strncpy(model->pwn->channel, "", PWNAGOTCHI_MAX_CHANNEL_LEN);
  78. for(size_t i = 0; i < PWNAGOTCHI_MAX_CHANNEL_LEN; i++) {
  79. // Break if we hit the end of the name
  80. if(cmd.arguments[i] == 0x00) {
  81. break;
  82. }
  83. model->pwn->channel[i] = cmd.arguments[i];
  84. }
  85. break;
  86. }
  87. // Process APS (Access Points)
  88. case 0x07: {
  89. // Write over APS with nothing
  90. strncpy(model->pwn->apStat, "", PWNAGOTCHI_MAX_APS_LEN);
  91. for(size_t i = 0; i < PWNAGOTCHI_MAX_APS_LEN; i++) {
  92. // Break if we hit the end of the name
  93. if(cmd.arguments[i] == 0x00) {
  94. break;
  95. }
  96. model->pwn->apStat[i] = cmd.arguments[i];
  97. }
  98. break;
  99. }
  100. // Process uptime
  101. case 0x08: {
  102. // Write over uptime with nothing
  103. strncpy(model->pwn->uptime, "", PWNAGOTCHI_MAX_UPTIME_LEN);
  104. for(size_t i = 0; i < PWNAGOTCHI_MAX_UPTIME_LEN; i++) {
  105. // Break if we hit the end of the name
  106. if(cmd.arguments[i] == 0x00) {
  107. break;
  108. }
  109. model->pwn->uptime[i] = cmd.arguments[i];
  110. }
  111. break;
  112. }
  113. // Process friend
  114. case 0x09: {
  115. // Friend not implemented yet
  116. break;
  117. }
  118. // Process mode
  119. case 0x0a: {
  120. enum PwnagotchiMode mode;
  121. switch(cmd.arguments[0]) {
  122. case 0x04:
  123. mode = PwnMode_Manual;
  124. break;
  125. case 0x05:
  126. mode = PwnMode_Auto;
  127. break;
  128. case 0x06:
  129. mode = PwnMode_Ai;
  130. break;
  131. default:
  132. mode = PwnMode_Manual;
  133. break;
  134. }
  135. model->pwn->mode = mode;
  136. break;
  137. }
  138. // Process Handshakes
  139. case 0x0b: {
  140. // Write over handshakes with nothing
  141. strncpy(model->pwn->handshakes, "", PWNAGOTCHI_MAX_HANDSHAKES_LEN);
  142. for(size_t i = 0; i < PWNAGOTCHI_MAX_HANDSHAKES_LEN; i++) {
  143. // Break if we hit the end of the name
  144. if(cmd.arguments[i] == 0x00) {
  145. break;
  146. }
  147. model->pwn->handshakes[i] = cmd.arguments[i];
  148. }
  149. break;
  150. }
  151. // Process message
  152. case 0x0c: {
  153. // Write over the message with nothing
  154. strncpy(model->pwn->message, "", PWNAGOTCHI_MAX_MESSAGE_LEN);
  155. for(size_t i = 0; i < PWNAGOTCHI_MAX_MESSAGE_LEN; i++) {
  156. // Break if we hit the end of the name
  157. if(cmd.arguments[i] == 0x00) {
  158. break;
  159. }
  160. model->pwn->message[i] = cmd.arguments[i];
  161. }
  162. break;
  163. }
  164. }
  165. }
  166. return false;
  167. }
  168. static void flipagotchi_view_draw_callback(Canvas* canvas, void* _model) {
  169. PwnDumpModel* model = _model;
  170. pwnagotchi_draw_all(model->pwn, canvas);
  171. }
  172. static bool flipagotchi_view_input_callback(InputEvent* event, void* context) {
  173. UNUSED(event);
  174. UNUSED(context);
  175. return false;
  176. }
  177. static uint32_t flipagotchi_exit(void* context) {
  178. UNUSED(context);
  179. return VIEW_NONE;
  180. }
  181. static void flipagotchi_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  182. furi_assert(context);
  183. FlipagotchiApp* app = context;
  184. if(ev == UartIrqEventRXNE) {
  185. furi_stream_buffer_send(app->rx_stream, &data, 1, 0);
  186. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventRx);
  187. }
  188. }
  189. static void flipagotchi_push_to_list(PwnDumpModel* model, const char data) {
  190. message_queue_push_byte(model->queue, data);
  191. }
  192. static int32_t flipagotchi_worker(void* context) {
  193. furi_assert(context);
  194. FlipagotchiApp* app = context;
  195. while(true) {
  196. bool update = false;
  197. uint32_t events =
  198. furi_thread_flags_wait(WORKER_EVENTS_MASK, FuriFlagWaitAny, FuriWaitForever);
  199. furi_check((events & FuriFlagError) == 0);
  200. if(events & WorkerEventStop) break;
  201. if(events & WorkerEventRx) {
  202. size_t length = 0;
  203. do {
  204. uint8_t data[1];
  205. length = furi_stream_buffer_receive(app->rx_stream, data, 1, 0);
  206. if(length > 0) {
  207. with_view_model(
  208. app->view,
  209. PwnDumpModel * model,
  210. {
  211. for(size_t i = 0; i < length; i++) {
  212. flipagotchi_push_to_list(model, data[i]);
  213. }
  214. update = flipagotchi_exec_cmd(model);
  215. },
  216. update);
  217. }
  218. } while(length > 0);
  219. notification_message(app->notification, &sequence_notification);
  220. // with_view_model(
  221. // app->view, PwnDumpModel * model, { UNUSED(model); }, true);
  222. }
  223. }
  224. return 0;
  225. }
  226. static FlipagotchiApp* flipagotchi_app_alloc() {
  227. FlipagotchiApp* app = malloc(sizeof(FlipagotchiApp));
  228. app->rx_stream = furi_stream_buffer_alloc(2048, 1);
  229. // Gui
  230. app->gui = furi_record_open(RECORD_GUI);
  231. app->notification = furi_record_open(RECORD_NOTIFICATION);
  232. // View dispatcher
  233. app->view_dispatcher = view_dispatcher_alloc();
  234. view_dispatcher_enable_queue(app->view_dispatcher);
  235. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  236. // Views
  237. app->view = view_alloc();
  238. view_set_draw_callback(app->view, flipagotchi_view_draw_callback);
  239. view_set_input_callback(app->view, flipagotchi_view_input_callback);
  240. view_allocate_model(app->view, ViewModelTypeLocking, sizeof(PwnDumpModel));
  241. with_view_model(
  242. app->view,
  243. PwnDumpModel * model,
  244. {
  245. model->queue = message_queue_alloc();
  246. model->pwn = pwnagotchi_alloc();
  247. },
  248. true);
  249. view_set_previous_callback(app->view, flipagotchi_exit);
  250. view_dispatcher_add_view(app->view_dispatcher, 0, app->view);
  251. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  252. // Enable uart listener
  253. furi_hal_console_disable();
  254. furi_hal_uart_set_br(PWNAGOTCHI_UART_CHANNEL, PWNAGOTCHI_UART_BAUD);
  255. furi_hal_uart_set_irq_cb(PWNAGOTCHI_UART_CHANNEL, flipagotchi_on_irq_cb, app);
  256. app->worker_thread = furi_thread_alloc();
  257. furi_thread_set_name(app->worker_thread, "UsbUartWorker");
  258. furi_thread_set_stack_size(app->worker_thread, 1024);
  259. furi_thread_set_context(app->worker_thread, app);
  260. furi_thread_set_callback(app->worker_thread, flipagotchi_worker);
  261. furi_thread_start(app->worker_thread);
  262. return app;
  263. }
  264. static void flipagotchi_app_free(FlipagotchiApp* app) {
  265. furi_assert(app);
  266. furi_thread_flags_set(furi_thread_get_id(app->worker_thread), WorkerEventStop);
  267. furi_thread_join(app->worker_thread);
  268. furi_thread_free(app->worker_thread);
  269. furi_hal_console_enable();
  270. // Free views
  271. view_dispatcher_remove_view(app->view_dispatcher, 0);
  272. with_view_model(
  273. app->view,
  274. PwnDumpModel * model,
  275. {
  276. message_queue_free(model->queue);
  277. pwnagotchi_free(model->pwn);
  278. },
  279. true);
  280. view_free(app->view);
  281. view_dispatcher_free(app->view_dispatcher);
  282. // Close gui record
  283. furi_record_close(RECORD_GUI);
  284. furi_record_close(RECORD_NOTIFICATION);
  285. app->gui = NULL;
  286. furi_stream_buffer_free(app->rx_stream);
  287. // Free rest
  288. free(app);
  289. }
  290. int32_t flipagotchi_app(void* p) {
  291. UNUSED(p);
  292. FlipagotchiApp* app = flipagotchi_app_alloc();
  293. view_dispatcher_run(app->view_dispatcher);
  294. flipagotchi_app_free(app);
  295. return 0;
  296. }