seos_profile.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. FuriHalBleProfileParams params;
  13. } BleProfileSeos;
  14. _Static_assert(offsetof(BleProfileSeos, base) == 0, "Wrong layout");
  15. static FuriHalBleProfileBase* ble_profile_seos_start(FuriHalBleProfileParams profile_params) {
  16. BleProfileSeos* profile = malloc(sizeof(BleProfileSeos));
  17. BleProfileParams* params = profile_params;
  18. profile->params = profile_params;
  19. profile->base.config = ble_profile_seos;
  20. profile->seos_svc = ble_svc_seos_start(params->mode);
  21. return &profile->base;
  22. }
  23. static void ble_profile_seos_stop(FuriHalBleProfileBase* profile) {
  24. furi_check(profile);
  25. furi_check(profile->config == ble_profile_seos);
  26. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  27. ble_svc_seos_stop(seos_profile->seos_svc);
  28. }
  29. // AN5289: 4.7, in order to use flash controller interval must be at least 25ms + advertisement, which is 30 ms
  30. // Since we don't use flash controller anymore interval can be lowered to 7.5ms
  31. #define CONNECTION_INTERVAL_MIN (0x06)
  32. // Up to 45 ms
  33. #define CONNECTION_INTERVAL_MAX (0x24)
  34. static GapConfig seos_reader_template_config = {
  35. .adv_service.UUID_Type = UUID_TYPE_128,
  36. .adv_service.Service_UUID_128 = BLE_SVC_SEOS_READER_UUID,
  37. .mfg_data =
  38. {0x2e,
  39. 0x01,
  40. 0x15,
  41. 0x4b,
  42. 0xe2,
  43. 0xb6,
  44. 0xb6,
  45. 0xb6,
  46. 0x2a,
  47. 0x46,
  48. 0x4c,
  49. 0x30,
  50. 0x4b,
  51. 0x37,
  52. 0x5a,
  53. 0x30,
  54. 0x31,
  55. 0x55,
  56. 0x31},
  57. .mfg_data_len = 19,
  58. .adv_name = "Seos",
  59. .bonding_mode = false,
  60. .pairing_method = GapPairingNone,
  61. .conn_param = {
  62. .conn_int_min = CONNECTION_INTERVAL_MIN,
  63. .conn_int_max = CONNECTION_INTERVAL_MAX,
  64. .slave_latency = 0,
  65. .supervisor_timeout = 0,
  66. }};
  67. static GapConfig seos_cred_template_config = {
  68. .adv_service.UUID_Type = UUID_TYPE_128,
  69. .adv_service.Service_UUID_128 = BLE_SVC_SEOS_CRED_UUID,
  70. .adv_name = "Flp",
  71. .bonding_mode = false,
  72. .pairing_method = GapPairingNone,
  73. .conn_param = {
  74. .conn_int_min = CONNECTION_INTERVAL_MIN,
  75. .conn_int_max = CONNECTION_INTERVAL_MAX,
  76. .slave_latency = 0,
  77. .supervisor_timeout = 0,
  78. }};
  79. static void
  80. ble_profile_seos_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  81. BleProfileParams* params = profile_params;
  82. FURI_LOG_D(TAG, "ble_profile_seos_get_config FlowMode %d", params->mode);
  83. furi_check(config);
  84. if(params->mode == FLOW_READER) {
  85. memcpy(config, &seos_reader_template_config, sizeof(GapConfig));
  86. } else if(params->mode == FLOW_CRED) {
  87. memcpy(config, &seos_cred_template_config, sizeof(GapConfig));
  88. }
  89. // Set mac address
  90. memcpy(config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
  91. }
  92. static const FuriHalBleProfileTemplate profile_callbacks = {
  93. .start = ble_profile_seos_start,
  94. .stop = ble_profile_seos_stop,
  95. .get_gap_config = ble_profile_seos_get_config,
  96. };
  97. const FuriHalBleProfileTemplate* ble_profile_seos = &profile_callbacks;
  98. void ble_profile_seos_set_event_callback(
  99. FuriHalBleProfileBase* profile,
  100. uint16_t buff_size,
  101. FuriHalBtSeosCallback callback,
  102. void* context) {
  103. furi_check(profile && (profile->config == ble_profile_seos));
  104. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  105. ble_svc_seos_set_callbacks(seos_profile->seos_svc, buff_size, callback, context);
  106. }
  107. bool ble_profile_seos_tx(FuriHalBleProfileBase* profile, uint8_t* data, uint16_t size) {
  108. furi_check(profile && (profile->config == ble_profile_seos));
  109. BleProfileSeos* seos_profile = (BleProfileSeos*)profile;
  110. if(size > BLE_PROFILE_SEOS_PACKET_SIZE_MAX) {
  111. return false;
  112. }
  113. return ble_svc_seos_update_tx(seos_profile->seos_svc, data, size);
  114. }