ble_serial.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This code is based on the Willy-JL's (https://github.com/Willy-JL) BLE fix.
  3. *
  4. * Thank you to Willy-JL for providing this code and making it available under the https://github.com/Flipper-XFW/Xtreme-Apps repository.
  5. * Your contribution has been invaluable for this project.
  6. */
  7. #include "ble_serial.h"
  8. #include <gap.h>
  9. #include <furi_ble/profile_interface.h>
  10. #include <services/serial_service.h>
  11. #include <furi.h>
  12. #include <ble/core/ble_defs.h>
  13. typedef struct {
  14. FuriHalBleProfileBase base;
  15. BleServiceSerial* serial_svc;
  16. } BleProfileSerial;
  17. _Static_assert(offsetof(BleProfileSerial, base) == 0, "Wrong layout");
  18. static FuriHalBleProfileBase* ble_profile_serial_start(FuriHalBleProfileParams profile_params) {
  19. UNUSED(profile_params);
  20. BleProfileSerial* profile = malloc(sizeof(BleProfileSerial));
  21. profile->base.config = ble_profile_serial;
  22. profile->serial_svc = ble_svc_serial_start();
  23. return &profile->base;
  24. }
  25. static void ble_profile_serial_stop(FuriHalBleProfileBase* profile) {
  26. furi_check(profile);
  27. furi_check(profile->config == ble_profile_serial);
  28. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  29. ble_svc_serial_stop(serial_profile->serial_svc);
  30. }
  31. static GapConfig serial_template_config = {
  32. .adv_service =
  33. {
  34. .UUID_Type = UUID_TYPE_16,
  35. .Service_UUID_16 = 0x3080,
  36. },
  37. .appearance_char = 0x8600,
  38. .bonding_mode = true,
  39. .pairing_method = GapPairingPinCodeShow,
  40. .conn_param = {
  41. .conn_int_min = 0x18, // 30 ms
  42. .conn_int_max = 0x24, // 45 ms
  43. .slave_latency = 0,
  44. .supervisor_timeout = 0,
  45. }};
  46. static void
  47. ble_profile_serial_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  48. BleProfileSerialParams* serial_profile_params = profile_params;
  49. furi_check(config);
  50. memcpy(config, &serial_template_config, sizeof(GapConfig));
  51. // Set mac address
  52. memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
  53. // Change MAC address for HID profile
  54. config->mac_address[2]++;
  55. if(serial_profile_params) {
  56. config->mac_address[0] ^= serial_profile_params->mac_xor;
  57. config->mac_address[1] ^= serial_profile_params->mac_xor >> 8;
  58. }
  59. // Set advertise name
  60. memset(config->adv_name, 0, sizeof(config->adv_name));
  61. const char* clicker_str = "Serial";
  62. if(serial_profile_params && serial_profile_params->device_name_prefix) {
  63. clicker_str = serial_profile_params->device_name_prefix;
  64. }
  65. // We don't have Flipper in BLE name, use printf instead of replace
  66. FuriString* name = furi_string_alloc_printf(
  67. "%c%s %s",
  68. furi_hal_version_get_ble_local_device_name_ptr()[0],
  69. clicker_str,
  70. furi_hal_version_get_ble_local_device_name_ptr() + 1);
  71. if(furi_string_size(name) >= sizeof(config->adv_name)) {
  72. furi_string_left(name, sizeof(config->adv_name) - 1);
  73. }
  74. memcpy(config->adv_name, furi_string_get_cstr(name), furi_string_size(name));
  75. furi_string_free(name);
  76. config->adv_service.UUID_Type = UUID_TYPE_16;
  77. config->adv_service.Service_UUID_16 |= furi_hal_version_get_hw_color();
  78. }
  79. static const FuriHalBleProfileTemplate profile_callbacks = {
  80. .start = ble_profile_serial_start,
  81. .stop = ble_profile_serial_stop,
  82. .get_gap_config = ble_profile_serial_get_config,
  83. };
  84. const FuriHalBleProfileTemplate* ble_profile_serial = &profile_callbacks;
  85. void ble_profile_serial_set_event_callback(
  86. FuriHalBleProfileBase* profile,
  87. uint16_t buff_size,
  88. FuriHalBtSerialCallback callback,
  89. void* context) {
  90. furi_check(profile && (profile->config == ble_profile_serial));
  91. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  92. ble_svc_serial_set_callbacks(serial_profile->serial_svc, buff_size, callback, context);
  93. }
  94. bool ble_profile_serial_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
  95. furi_check(profile && (profile->config == ble_profile_serial));
  96. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  97. if(size > BLE_PROFILE_SERIAL_PACKET_SIZE_MAX) {
  98. return false;
  99. }
  100. return ble_svc_serial_update_tx(serial_profile->serial_svc, data, size);
  101. }