bt.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if(!furi_hal_bt_wait_startup()) {
  55. FURI_LOG_E(BT_SERVICE_TAG, "Core2 startup failed");
  56. } else {
  57. view_port_enabled_set(bt->statusbar_view_port, true);
  58. if(bt->bt_settings.enabled) {
  59. bool bt_app_started = furi_hal_bt_start_app();
  60. if(!bt_app_started) {
  61. FURI_LOG_E(BT_SERVICE_TAG, "BT App start failed");
  62. } else {
  63. FURI_LOG_I(BT_SERVICE_TAG, "BT App started");
  64. }
  65. }
  66. }
  67. BtMessage message;
  68. while(1) {
  69. furi_check(osMessageQueueGet(bt->message_queue, &message, NULL, osWaitForever) == osOK);
  70. if(message.type == BtMessageTypeUpdateStatusbar) {
  71. // Update statusbar
  72. view_port_enabled_set(bt->statusbar_view_port, furi_hal_bt_is_alive());
  73. } else if(message.type == BtMessageTypeUpdateBatteryLevel) {
  74. // Update battery level
  75. if(furi_hal_bt_is_alive()) {
  76. battery_svc_update_level(message.data.battery_level);
  77. }
  78. } else if(message.type == BtMessageTypePinCodeShow) {
  79. // Display PIN code
  80. bt_pin_code_show_event_handler(bt, message.data.pin_code);
  81. }
  82. }
  83. return 0;
  84. }