flipagotchi.c 11 KB

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