ble_serial.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. typedef struct {
  13. FuriHalBleProfileBase base;
  14. BleServiceSerial* serial_svc;
  15. } BleProfileSerial;
  16. _Static_assert(offsetof(BleProfileSerial, base) == 0, "Wrong layout");
  17. static FuriHalBleProfileBase* ble_profile_serial_start(FuriHalBleProfileParams profile_params) {
  18. UNUSED(profile_params);
  19. BleProfileSerial* profile = malloc(sizeof(BleProfileSerial));
  20. profile->base.config = ble_profile_serial;
  21. profile->serial_svc = ble_svc_serial_start();
  22. return &profile->base;
  23. }
  24. static void ble_profile_serial_stop(FuriHalBleProfileBase* profile) {
  25. furi_check(profile);
  26. furi_check(profile->config == ble_profile_serial);
  27. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  28. ble_svc_serial_stop(serial_profile->serial_svc);
  29. }
  30. static GapConfig serial_template_config = {
  31. .adv_service_uuid = 0x3080,
  32. .appearance_char = 0x8600,
  33. .bonding_mode = true,
  34. .pairing_method = GapPairingPinCodeShow,
  35. .conn_param = {
  36. .conn_int_min = 0x18, // 30 ms
  37. .conn_int_max = 0x24, // 45 ms
  38. .slave_latency = 0,
  39. .supervisor_timeout = 0,
  40. }};
  41. static void
  42. ble_profile_serial_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  43. BleProfileSerialParams* serial_profile_params = profile_params;
  44. furi_check(config);
  45. memcpy(config, &serial_template_config, sizeof(GapConfig));
  46. // Set mac address
  47. memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
  48. // Change MAC address for HID profile
  49. config->mac_address[2]++;
  50. if(serial_profile_params) {
  51. config->mac_address[0] ^= serial_profile_params->mac_xor;
  52. config->mac_address[1] ^= serial_profile_params->mac_xor >> 8;
  53. }
  54. // Set advertise name
  55. memset(config->adv_name, 0, sizeof(config->adv_name));
  56. const char* clicker_str = "Serial";
  57. if(serial_profile_params && serial_profile_params->device_name_prefix) {
  58. clicker_str = serial_profile_params->device_name_prefix;
  59. }
  60. // We don't have Flipper in BLE name, use printf instead of replace
  61. FuriString* name = furi_string_alloc_printf(
  62. "%c%s %s",
  63. furi_hal_version_get_ble_local_device_name_ptr()[0],
  64. clicker_str,
  65. furi_hal_version_get_ble_local_device_name_ptr() + 1);
  66. if(furi_string_size(name) >= sizeof(config->adv_name)) {
  67. furi_string_left(name, sizeof(config->adv_name) - 1);
  68. }
  69. memcpy(config->adv_name, furi_string_get_cstr(name), furi_string_size(name));
  70. furi_string_free(name);
  71. config->adv_service_uuid |= furi_hal_version_get_hw_color();
  72. }
  73. static const FuriHalBleProfileTemplate profile_callbacks = {
  74. .start = ble_profile_serial_start,
  75. .stop = ble_profile_serial_stop,
  76. .get_gap_config = ble_profile_serial_get_config,
  77. };
  78. const FuriHalBleProfileTemplate* ble_profile_serial = &profile_callbacks;
  79. void ble_profile_serial_set_event_callback(
  80. FuriHalBleProfileBase* profile,
  81. uint16_t buff_size,
  82. FuriHalBtSerialCallback callback,
  83. void* context) {
  84. furi_check(profile && (profile->config == ble_profile_serial));
  85. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  86. ble_svc_serial_set_callbacks(serial_profile->serial_svc, buff_size, callback, context);
  87. }
  88. bool ble_profile_serial_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
  89. furi_check(profile && (profile->config == ble_profile_serial));
  90. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  91. if(size > BLE_PROFILE_SERIAL_PACKET_SIZE_MAX) {
  92. return false;
  93. }
  94. return ble_svc_serial_update_tx(serial_profile->serial_svc, data, size);
  95. }