bt.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "bt_i.h"
  2. #include "battery_service.h"
  3. #define BT_SERVICE_TAG "BT"
  4. // static void bt_update_statusbar(void* arg) {
  5. // furi_assert(arg);
  6. // Bt* bt = arg;
  7. // BtMessage m = {.type = BtMessageTypeUpdateStatusbar};
  8. // furi_check(osMessageQueuePut(bt->message_queue, &m, 0, osWaitForever) == osOK);
  9. // }
  10. static void bt_draw_statusbar_callback(Canvas* canvas, void* context) {
  11. canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_5x8);
  12. }
  13. static ViewPort* bt_statusbar_view_port_alloc() {
  14. ViewPort* statusbar_view_port = view_port_alloc();
  15. view_port_set_width(statusbar_view_port, 5);
  16. view_port_draw_callback_set(statusbar_view_port, bt_draw_statusbar_callback, NULL);
  17. view_port_enabled_set(statusbar_view_port, false);
  18. return statusbar_view_port;
  19. }
  20. static void bt_pin_code_show_event_handler(Bt* bt, uint32_t pin) {
  21. furi_assert(bt);
  22. string_t pin_str;
  23. string_init_printf(pin_str, "%06d", pin);
  24. dialog_message_set_text(
  25. bt->dialog_message, string_get_cstr(pin_str), 64, 32, AlignCenter, AlignCenter);
  26. dialog_message_set_buttons(bt->dialog_message, "Back", NULL, NULL);
  27. dialog_message_show(bt->dialogs, bt->dialog_message);
  28. string_clear(pin_str);
  29. }
  30. Bt* bt_alloc() {
  31. Bt* bt = furi_alloc(sizeof(Bt));
  32. // Load settings
  33. if(!bt_settings_load(&bt->bt_settings)) {
  34. bt_settings_save(&bt->bt_settings);
  35. }
  36. // Alloc queue
  37. bt->message_queue = osMessageQueueNew(8, sizeof(BtMessage), NULL);
  38. // doesn't make sense if we waiting for transition on service start
  39. // bt->update_status_timer = osTimerNew(bt_update_statusbar, osTimerPeriodic, bt, NULL);
  40. // osTimerStart(bt->update_status_timer, 4000);
  41. // Setup statusbar view port
  42. bt->statusbar_view_port = bt_statusbar_view_port_alloc();
  43. // Gui
  44. bt->gui = furi_record_open("gui");
  45. gui_add_view_port(bt->gui, bt->statusbar_view_port, GuiLayerStatusBarLeft);
  46. // Dialogs
  47. bt->dialogs = furi_record_open("dialogs");
  48. bt->dialog_message = dialog_message_alloc();
  49. return bt;
  50. }
  51. int32_t bt_srv() {
  52. Bt* bt = bt_alloc();
  53. furi_record_create("bt", bt);
  54. furi_hal_bt_init();
  55. if(!furi_hal_bt_wait_startup()) {
  56. FURI_LOG_E(BT_SERVICE_TAG, "Core2 startup failed");
  57. } else {
  58. view_port_enabled_set(bt->statusbar_view_port, true);
  59. if(bt->bt_settings.enabled) {
  60. bool bt_app_started = furi_hal_bt_start_app();
  61. if(!bt_app_started) {
  62. FURI_LOG_E(BT_SERVICE_TAG, "BT App start failed");
  63. } else {
  64. FURI_LOG_I(BT_SERVICE_TAG, "BT App started");
  65. }
  66. }
  67. }
  68. BtMessage message;
  69. while(1) {
  70. furi_check(osMessageQueueGet(bt->message_queue, &message, NULL, osWaitForever) == osOK);
  71. if(message.type == BtMessageTypeUpdateStatusbar) {
  72. // Update statusbar
  73. view_port_enabled_set(bt->statusbar_view_port, furi_hal_bt_is_alive());
  74. } else if(message.type == BtMessageTypeUpdateBatteryLevel) {
  75. // Update battery level
  76. if(furi_hal_bt_is_alive()) {
  77. battery_svc_update_level(message.data.battery_level);
  78. }
  79. } else if(message.type == BtMessageTypePinCodeShow) {
  80. // Display PIN code
  81. bt_pin_code_show_event_handler(bt, message.data.pin_code);
  82. }
  83. }
  84. return 0;
  85. }