fbs.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "fbs.h"
  2. const uint16_t BT_SERIAL_BUFFER_SIZE = 128;
  3. void draw_callback(Canvas* canvas, void* ctx) {
  4. FBS* app = ctx;
  5. furi_check(furi_mutex_acquire(app->app_mutex, FuriWaitForever) == FuriStatusOk);
  6. canvas_draw_str_aligned(canvas, 64, 17, AlignCenter, AlignCenter, "App is running");
  7. canvas_draw_str_aligned(canvas, 64, 27, AlignCenter, AlignCenter, "Please open CLI");
  8. canvas_draw_str_aligned(canvas, 64, 37, AlignCenter, AlignCenter, "and type: log info");
  9. furi_mutex_release(app->app_mutex);
  10. }
  11. void input_callback(InputEvent* input, void* ctx) {
  12. FBS* app = ctx;
  13. furi_message_queue_put(app->event_queue, input, FuriWaitForever);
  14. }
  15. FBS* fbs_alloc() {
  16. FBS* app = malloc(sizeof(FBS));
  17. app->app_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  18. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  19. app->gui = furi_record_open(RECORD_GUI);
  20. app->view_port = view_port_alloc();
  21. view_port_draw_callback_set(app->view_port, draw_callback, app);
  22. view_port_input_callback_set(app->view_port, input_callback, app);
  23. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  24. app->bt_connected = false;
  25. app->bt = furi_record_open(RECORD_BT);
  26. return app;
  27. }
  28. void fbs_free(FBS* app) {
  29. view_port_enabled_set(app->view_port, false);
  30. gui_remove_view_port(app->gui, app->view_port);
  31. furi_record_close(RECORD_GUI);
  32. app->gui = NULL;
  33. view_port_free(app->view_port);
  34. furi_mutex_free(app->app_mutex);
  35. furi_message_queue_free(app->event_queue);
  36. furi_record_close(RECORD_BT);
  37. app->bt = NULL;
  38. free(app);
  39. }
  40. static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context) {
  41. furi_assert(context);
  42. Bt* bt = context;
  43. UNUSED(bt);
  44. uint16_t ret = 0;
  45. if(event.event == SerialServiceEventTypeDataReceived) {
  46. FURI_LOG_I(TAG, "SerialServiceEventTypeDataReceived");
  47. FURI_LOG_I(TAG, "Size: %u", event.data.size);
  48. FURI_LOG_I(TAG, "Data: ");
  49. for(size_t i = 0; i < event.data.size; i++) {
  50. printf("%X ", event.data.buffer[i]);
  51. }
  52. printf("\r\n");
  53. } else if(event.event == SerialServiceEventTypeDataSent) {
  54. FURI_LOG_I(TAG, "SerialServiceEventTypeDataSent");
  55. FURI_LOG_I(TAG, "Size: %u", event.data.size);
  56. FURI_LOG_I(TAG, "Data: ");
  57. for(size_t i = 0; i < event.data.size; i++) {
  58. printf("%X ", event.data.buffer[i]);
  59. }
  60. printf("\r\n");
  61. }
  62. return ret;
  63. }
  64. int32_t fbs_app(void* p) {
  65. UNUSED(p);
  66. FBS* app = fbs_alloc();
  67. if(furi_hal_bt_is_active()) {
  68. FURI_LOG_I(TAG, "BT is working, hijacking the serial connection...");
  69. furi_hal_bt_serial_set_event_callback(
  70. BT_SERIAL_BUFFER_SIZE, bt_serial_event_callback, app);
  71. furi_hal_bt_start_advertising();
  72. } else {
  73. FURI_LOG_I(TAG, "Please, enable the Bluetooth and restart the app");
  74. }
  75. InputEvent event;
  76. for(bool processing = true; processing;) {
  77. int status = furi_message_queue_get(app->event_queue, &event, 100);
  78. furi_check(furi_mutex_acquire(app->app_mutex, FuriWaitForever) == FuriStatusOk);
  79. if(status == FuriStatusOk && event.type == InputTypePress && event.key == InputKeyBack) {
  80. processing = false;
  81. }
  82. furi_mutex_release(app->app_mutex);
  83. view_port_update(app->view_port);
  84. }
  85. furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
  86. fbs_free(app);
  87. FURI_LOG_I(TAG, "Released everything");
  88. return 0;
  89. }