seos_profile.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "seos_profile.h"
  2. #include <gap.h>
  3. #include <furi_ble/profile_interface.h>
  4. #include "seos_service.h"
  5. #include <furi.h>
  6. #define TAG "SeosProfile"
  7. typedef struct {
  8. FuriHalBleProfileBase base;
  9. BleServiceSeos* seos_svc;
  10. } BleProfileSeos;
  11. _Static_assert(offsetof(BleProfileSeos, base) == 0, "Wrong layout");
  12. static FuriHalBleProfileBase* ble_profile_seos_start(FuriHalBleProfileParams profile_params) {
  13. FURI_LOG_D(TAG, "ble_profile_seos_start");
  14. UNUSED(profile_params);
  15. BleProfileSeos* profile = malloc(sizeof(BleProfileSeos));
  16. profile->base.config = ble_profile_seos;
  17. profile->seos_svc = ble_svc_seos_start();
  18. return &profile->base;
  19. }
  20. static void ble_profile_seos_stop(FuriHalBleProfileBase* profile) {
  21. furi_check(profile);
  22. furi_check(profile->config == ble_profile_seos);
  23. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  24. ble_svc_seos_stop(seos_profile->seos_svc);
  25. }
  26. // AN5289: 4.7, in order to use flash controller interval must be at least 25ms + advertisement, which is 30 ms
  27. // Since we don't use flash controller anymore interval can be lowered to 7.5ms
  28. #define CONNECTION_INTERVAL_MIN (0x06)
  29. // Up to 45 ms
  30. #define CONNECTION_INTERVAL_MAX (0x24)
  31. static GapConfig seos_template_config = {
  32. .adv_service_uuid = 0x3080,
  33. .appearance_char = 0x8600,
  34. .bonding_mode = true,
  35. .pairing_method = GapPairingPinCodeShow,
  36. .conn_param = {
  37. .conn_int_min = CONNECTION_INTERVAL_MIN,
  38. .conn_int_max = CONNECTION_INTERVAL_MAX,
  39. .slave_latency = 0,
  40. .supervisor_timeout = 0,
  41. }};
  42. static void
  43. ble_profile_seos_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  44. UNUSED(profile_params);
  45. furi_check(config);
  46. memcpy(config, &seos_template_config, sizeof(GapConfig));
  47. // Set mac address
  48. memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
  49. // Set advertise name
  50. strlcpy(
  51. config->adv_name,
  52. furi_hal_version_get_ble_local_device_name_ptr(),
  53. FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
  54. config->adv_service_uuid |= furi_hal_version_get_hw_color();
  55. }
  56. static const FuriHalBleProfileTemplate profile_callbacks = {
  57. .start = ble_profile_seos_start,
  58. .stop = ble_profile_seos_stop,
  59. .get_gap_config = ble_profile_seos_get_config,
  60. };
  61. const FuriHalBleProfileTemplate* ble_profile_seos = &profile_callbacks;
  62. void ble_profile_seos_set_event_callback(
  63. FuriHalBleProfileBase* profile,
  64. uint16_t buff_size,
  65. FuriHalBtSeosCallback callback,
  66. void* context) {
  67. furi_check(profile && (profile->config == ble_profile_seos));
  68. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  69. ble_svc_seos_set_callbacks(seos_profile->seos_svc, buff_size, callback, context);
  70. }
  71. void ble_profile_seos_notify_buffer_is_empty(FuriHalBleProfileBase* profile) {
  72. furi_check(profile && (profile->config == ble_profile_seos));
  73. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  74. ble_svc_seos_notify_buffer_is_empty(seos_profile->seos_svc);
  75. }
  76. bool ble_profile_seos_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
  77. furi_check(profile && (profile->config == ble_profile_seos));
  78. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  79. if(size > BLE_PROFILE_SEOS_PACKET_SIZE_MAX) {
  80. return false;
  81. }
  82. return ble_svc_seos_update_tx(seos_profile->seos_svc, data, size);
  83. }