ble_serial.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * Based on <targets/f7/ble_glue/profiles/serial_profile.c>
  8. * and on <lib/ble_profile/extra_profiles/hid_profile.c>
  9. */
  10. #include "ble_serial.h"
  11. #include <gap.h>
  12. #include <furi_ble/profile_interface.h>
  13. #include <services/serial_service.h>
  14. #include <furi.h>
  15. #include <ble/core/ble_defs.h>
  16. typedef struct {
  17. FuriHalBleProfileBase base;
  18. BleServiceSerial* serial_svc;
  19. } BleProfileSerial;
  20. _Static_assert(offsetof(BleProfileSerial, base) == 0, "Wrong layout");
  21. static FuriHalBleProfileBase* ble_profile_serial_start(FuriHalBleProfileParams profile_params) {
  22. UNUSED(profile_params);
  23. BleProfileSerial* profile = malloc(sizeof(BleProfileSerial));
  24. profile->base.config = ble_profile_serial;
  25. profile->serial_svc = ble_svc_serial_start();
  26. return &profile->base;
  27. }
  28. static void ble_profile_serial_stop(FuriHalBleProfileBase* profile) {
  29. furi_check(profile);
  30. furi_check(profile->config == ble_profile_serial);
  31. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  32. ble_svc_serial_stop(serial_profile->serial_svc);
  33. }
  34. // AN5289: 4.7, in order to use flash controller interval must be at least 25ms + advertisement, which is 30 ms
  35. // Since we don't use flash controller anymore interval can be lowered to 7.5ms
  36. #define CONNECTION_INTERVAL_MIN (0x06)
  37. // Up to 45 ms
  38. #define CONNECTION_INTERVAL_MAX (0x24)
  39. static const GapConfig serial_template_config = {
  40. .adv_service =
  41. {
  42. .UUID_Type = UUID_TYPE_16,
  43. .Service_UUID_16 = 0x3080,
  44. },
  45. .appearance_char = 0x8600,
  46. .bonding_mode = true,
  47. .pairing_method = GapPairingPinCodeShow,
  48. .conn_param = {
  49. .conn_int_min = CONNECTION_INTERVAL_MIN,
  50. .conn_int_max = CONNECTION_INTERVAL_MAX,
  51. .slave_latency = 0,
  52. .supervisor_timeout = 0,
  53. }};
  54. static void
  55. ble_profile_serial_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  56. BleProfileSerialParams* serial_profile_params = profile_params;
  57. furi_check(config);
  58. memcpy(config, &serial_template_config, sizeof(GapConfig));
  59. // Set mac address
  60. memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
  61. // Change MAC address for Serial profile
  62. config->mac_address[2]++;
  63. if(serial_profile_params) {
  64. config->mac_address[0] ^= serial_profile_params->mac_xor;
  65. config->mac_address[1] ^= serial_profile_params->mac_xor >> 8;
  66. }
  67. // Set advertise name
  68. const char* clicker_str = "Serial";
  69. if(serial_profile_params && serial_profile_params->device_name_prefix) {
  70. clicker_str = serial_profile_params->device_name_prefix;
  71. }
  72. snprintf(
  73. config->adv_name,
  74. sizeof(config->adv_name),
  75. "%c%s %s",
  76. furi_hal_version_get_ble_local_device_name_ptr()[0],
  77. clicker_str,
  78. furi_hal_version_get_name_ptr());
  79. config->adv_service.UUID_Type = UUID_TYPE_16;
  80. config->adv_service.Service_UUID_16 |= furi_hal_version_get_hw_color();
  81. }
  82. static const FuriHalBleProfileTemplate profile_callbacks = {
  83. .start = ble_profile_serial_start,
  84. .stop = ble_profile_serial_stop,
  85. .get_gap_config = ble_profile_serial_get_config,
  86. };
  87. const FuriHalBleProfileTemplate* const ble_profile_serial = &profile_callbacks;
  88. void ble_profile_serial_set_event_callback(
  89. FuriHalBleProfileBase* profile,
  90. uint16_t buff_size,
  91. FuriHalBtSerialCallback callback,
  92. void* context) {
  93. furi_check(profile && (profile->config == ble_profile_serial));
  94. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  95. ble_svc_serial_set_callbacks(serial_profile->serial_svc, buff_size, callback, context);
  96. }
  97. bool ble_profile_serial_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
  98. furi_check(profile && (profile->config == ble_profile_serial));
  99. BleProfileSerial* serial_profile = (BleProfileSerial*)profile;
  100. if(size > BLE_PROFILE_SERIAL_PACKET_SIZE_MAX) {
  101. return false;
  102. }
  103. return ble_svc_serial_update_tx(serial_profile->serial_svc, data, size);
  104. }