seos_profile.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "seos_profile.h"
  2. #include <gap.h>
  3. #include <furi_ble/profile_interface.h>
  4. #include "seos_service.h"
  5. #include "seos_service_uuid.inc"
  6. #include <ble/core/ble_defs.h>
  7. #include <furi.h>
  8. #define TAG "SeosProfile"
  9. typedef struct {
  10. FuriHalBleProfileBase base;
  11. BleServiceSeos* seos_svc;
  12. } BleProfileSeos;
  13. _Static_assert(offsetof(BleProfileSeos, base) == 0, "Wrong layout");
  14. static FuriHalBleProfileBase* ble_profile_seos_start(FuriHalBleProfileParams profile_params) {
  15. FURI_LOG_D(TAG, "ble_profile_seos_start");
  16. UNUSED(profile_params);
  17. BleProfileSeos* profile = malloc(sizeof(BleProfileSeos));
  18. profile->base.config = ble_profile_seos;
  19. profile->seos_svc = ble_svc_seos_start();
  20. return &profile->base;
  21. }
  22. static void ble_profile_seos_stop(FuriHalBleProfileBase* profile) {
  23. furi_check(profile);
  24. furi_check(profile->config == ble_profile_seos);
  25. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  26. ble_svc_seos_stop(seos_profile->seos_svc);
  27. }
  28. // AN5289: 4.7, in order to use flash controller interval must be at least 25ms + advertisement, which is 30 ms
  29. // Since we don't use flash controller anymore interval can be lowered to 7.5ms
  30. #define CONNECTION_INTERVAL_MIN (0x06)
  31. // Up to 45 ms
  32. #define CONNECTION_INTERVAL_MAX (0x24)
  33. static GapConfig seos_template_config = {
  34. .adv_service.UUID_Type = UUID_TYPE_128,
  35. .adv_service.Service_UUID_128 = BLE_SVC_SEOS_UUID,
  36. .mfg_data =
  37. {0x2e,
  38. 0x01,
  39. 0x15,
  40. 0x4b,
  41. 0xe2,
  42. 0xb6,
  43. 0xb6,
  44. 0xb6,
  45. 0x2a,
  46. 0x46,
  47. 0x4c,
  48. 0x30,
  49. 0x4b,
  50. 0x37,
  51. 0x5a,
  52. 0x30,
  53. 0x31,
  54. 0x55,
  55. 0x31},
  56. .mfg_data_len = 19,
  57. .adv_name = "Seos",
  58. .bonding_mode = false,
  59. .pairing_method = GapPairingNone,
  60. .conn_param = {
  61. .conn_int_min = CONNECTION_INTERVAL_MIN,
  62. .conn_int_max = CONNECTION_INTERVAL_MAX,
  63. .slave_latency = 0,
  64. .supervisor_timeout = 0,
  65. }};
  66. static void
  67. ble_profile_seos_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  68. UNUSED(profile_params);
  69. furi_check(config);
  70. memcpy(config, &seos_template_config, sizeof(GapConfig));
  71. // Set mac address
  72. memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
  73. }
  74. static const FuriHalBleProfileTemplate profile_callbacks = {
  75. .start = ble_profile_seos_start,
  76. .stop = ble_profile_seos_stop,
  77. .get_gap_config = ble_profile_seos_get_config,
  78. };
  79. const FuriHalBleProfileTemplate* ble_profile_seos = &profile_callbacks;
  80. void ble_profile_seos_set_event_callback(
  81. FuriHalBleProfileBase* profile,
  82. uint16_t buff_size,
  83. FuriHalBtSeosCallback callback,
  84. void* context) {
  85. FURI_LOG_D(TAG, "ble_profile_seos_set_event_callback");
  86. furi_check(profile && (profile->config == ble_profile_seos));
  87. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  88. ble_svc_seos_set_callbacks(seos_profile->seos_svc, buff_size, callback, context);
  89. }
  90. void ble_profile_seos_notify_buffer_is_empty(FuriHalBleProfileBase* profile) {
  91. furi_check(profile && (profile->config == ble_profile_seos));
  92. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  93. ble_svc_seos_notify_buffer_is_empty(seos_profile->seos_svc);
  94. }
  95. bool ble_profile_seos_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
  96. furi_check(profile && (profile->config == ble_profile_seos));
  97. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  98. if(size > BLE_PROFILE_SEOS_PACKET_SIZE_MAX) {
  99. return false;
  100. }
  101. return ble_svc_seos_update_tx(seos_profile->seos_svc, data, size);
  102. }