seos_profile.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Char_UUID_128 = BLE_SVC_SEOS_CHAR_UUID,
  36. .appearance_char = 0x8600,
  37. .bonding_mode = false,
  38. .pairing_method = GapPairingNone,
  39. .conn_param = {
  40. .conn_int_min = CONNECTION_INTERVAL_MIN,
  41. .conn_int_max = CONNECTION_INTERVAL_MAX,
  42. .slave_latency = 0,
  43. .supervisor_timeout = 0,
  44. }};
  45. static void
  46. ble_profile_seos_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  47. UNUSED(profile_params);
  48. furi_check(config);
  49. memcpy(config, &seos_template_config, sizeof(GapConfig));
  50. // Set mac address
  51. memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
  52. // Set advertise name
  53. strlcpy(
  54. config->adv_name,
  55. furi_hal_version_get_ble_local_device_name_ptr(),
  56. FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
  57. }
  58. static const FuriHalBleProfileTemplate profile_callbacks = {
  59. .start = ble_profile_seos_start,
  60. .stop = ble_profile_seos_stop,
  61. .get_gap_config = ble_profile_seos_get_config,
  62. };
  63. const FuriHalBleProfileTemplate* ble_profile_seos = &profile_callbacks;
  64. void ble_profile_seos_set_event_callback(
  65. FuriHalBleProfileBase* profile,
  66. uint16_t buff_size,
  67. FuriHalBtSeosCallback callback,
  68. void* context) {
  69. furi_check(profile && (profile->config == ble_profile_seos));
  70. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  71. ble_svc_seos_set_callbacks(seos_profile->seos_svc, buff_size, callback, context);
  72. }
  73. void ble_profile_seos_notify_buffer_is_empty(FuriHalBleProfileBase* profile) {
  74. furi_check(profile && (profile->config == ble_profile_seos));
  75. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  76. ble_svc_seos_notify_buffer_is_empty(seos_profile->seos_svc);
  77. }
  78. bool ble_profile_seos_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
  79. furi_check(profile && (profile->config == ble_profile_seos));
  80. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  81. if(size > BLE_PROFILE_SEOS_PACKET_SIZE_MAX) {
  82. return false;
  83. }
  84. return ble_svc_seos_update_tx(seos_profile->seos_svc, data, size);
  85. }