dev_info_service.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "dev_info_service.h"
  2. #include "app_common.h"
  3. #include "ble.h"
  4. #include <furi.h>
  5. #include <m-string.h>
  6. #include <protobuf_version.h>
  7. #include <lib/toolbox/version.h>
  8. #define TAG "BtDevInfoSvc"
  9. typedef struct {
  10. uint16_t service_handle;
  11. uint16_t man_name_char_handle;
  12. uint16_t serial_num_char_handle;
  13. uint16_t firmware_rev_char_handle;
  14. uint16_t software_rev_char_handle;
  15. uint16_t rpc_version_char_handle;
  16. string_t version_string;
  17. char hardware_revision[4];
  18. } DevInfoSvc;
  19. static DevInfoSvc* dev_info_svc = NULL;
  20. static const char dev_info_man_name[] = "Flipper Devices Inc.";
  21. static const char dev_info_serial_num[] = "1.0";
  22. static const char dev_info_rpc_version[] = TOSTRING(PROTOBUF_MAJOR_VERSION.PROTOBUF_MINOR_VERSION);
  23. static const uint8_t dev_info_rpc_version_uuid[] =
  24. {0x33, 0xa9, 0xb5, 0x3e, 0x87, 0x5d, 0x1a, 0x8e, 0xc8, 0x47, 0x5e, 0xae, 0x6d, 0x66, 0xf6, 0x03};
  25. void dev_info_svc_start() {
  26. dev_info_svc = malloc(sizeof(DevInfoSvc));
  27. string_init_printf(
  28. dev_info_svc->version_string,
  29. "%s %s %s %s",
  30. version_get_githash(NULL),
  31. version_get_gitbranch(NULL),
  32. version_get_gitbranchnum(NULL),
  33. version_get_builddate(NULL));
  34. snprintf(
  35. dev_info_svc->hardware_revision,
  36. sizeof(dev_info_svc->hardware_revision),
  37. "%d",
  38. version_get_target(NULL));
  39. tBleStatus status;
  40. // Add Device Information Service
  41. uint16_t uuid = DEVICE_INFORMATION_SERVICE_UUID;
  42. status = aci_gatt_add_service(
  43. UUID_TYPE_16, (Service_UUID_t*)&uuid, PRIMARY_SERVICE, 11, &dev_info_svc->service_handle);
  44. if(status) {
  45. FURI_LOG_E(TAG, "Failed to add Device Information Service: %d", status);
  46. }
  47. // Add characteristics
  48. uuid = MANUFACTURER_NAME_UUID;
  49. status = aci_gatt_add_char(
  50. dev_info_svc->service_handle,
  51. UUID_TYPE_16,
  52. (Char_UUID_t*)&uuid,
  53. strlen(dev_info_man_name),
  54. CHAR_PROP_READ,
  55. ATTR_PERMISSION_AUTHEN_READ,
  56. GATT_DONT_NOTIFY_EVENTS,
  57. 10,
  58. CHAR_VALUE_LEN_CONSTANT,
  59. &dev_info_svc->man_name_char_handle);
  60. if(status) {
  61. FURI_LOG_E(TAG, "Failed to add manufacturer name char: %d", status);
  62. }
  63. uuid = SERIAL_NUMBER_UUID;
  64. status = aci_gatt_add_char(
  65. dev_info_svc->service_handle,
  66. UUID_TYPE_16,
  67. (Char_UUID_t*)&uuid,
  68. strlen(dev_info_serial_num),
  69. CHAR_PROP_READ,
  70. ATTR_PERMISSION_AUTHEN_READ,
  71. GATT_DONT_NOTIFY_EVENTS,
  72. 10,
  73. CHAR_VALUE_LEN_CONSTANT,
  74. &dev_info_svc->serial_num_char_handle);
  75. if(status) {
  76. FURI_LOG_E(TAG, "Failed to add serial number char: %d", status);
  77. }
  78. uuid = FIRMWARE_REVISION_UUID;
  79. status = aci_gatt_add_char(
  80. dev_info_svc->service_handle,
  81. UUID_TYPE_16,
  82. (Char_UUID_t*)&uuid,
  83. strlen(dev_info_svc->hardware_revision),
  84. CHAR_PROP_READ,
  85. ATTR_PERMISSION_AUTHEN_READ,
  86. GATT_DONT_NOTIFY_EVENTS,
  87. 10,
  88. CHAR_VALUE_LEN_CONSTANT,
  89. &dev_info_svc->firmware_rev_char_handle);
  90. if(status) {
  91. FURI_LOG_E(TAG, "Failed to add firmware revision char: %d", status);
  92. }
  93. uuid = SOFTWARE_REVISION_UUID;
  94. status = aci_gatt_add_char(
  95. dev_info_svc->service_handle,
  96. UUID_TYPE_16,
  97. (Char_UUID_t*)&uuid,
  98. string_size(dev_info_svc->version_string),
  99. CHAR_PROP_READ,
  100. ATTR_PERMISSION_AUTHEN_READ,
  101. GATT_DONT_NOTIFY_EVENTS,
  102. 10,
  103. CHAR_VALUE_LEN_CONSTANT,
  104. &dev_info_svc->software_rev_char_handle);
  105. if(status) {
  106. FURI_LOG_E(TAG, "Failed to add software revision char: %d", status);
  107. }
  108. status = aci_gatt_add_char(
  109. dev_info_svc->service_handle,
  110. UUID_TYPE_128,
  111. (const Char_UUID_t*)dev_info_rpc_version_uuid,
  112. strlen(dev_info_rpc_version),
  113. CHAR_PROP_READ,
  114. ATTR_PERMISSION_AUTHEN_READ,
  115. GATT_DONT_NOTIFY_EVENTS,
  116. 10,
  117. CHAR_VALUE_LEN_CONSTANT,
  118. &dev_info_svc->rpc_version_char_handle);
  119. if(status) {
  120. FURI_LOG_E(TAG, "Failed to add rpc version characteristic: %d", status);
  121. }
  122. // Update characteristics
  123. status = aci_gatt_update_char_value(
  124. dev_info_svc->service_handle,
  125. dev_info_svc->man_name_char_handle,
  126. 0,
  127. strlen(dev_info_man_name),
  128. (uint8_t*)dev_info_man_name);
  129. if(status) {
  130. FURI_LOG_E(TAG, "Failed to update manufacturer name char: %d", status);
  131. }
  132. status = aci_gatt_update_char_value(
  133. dev_info_svc->service_handle,
  134. dev_info_svc->serial_num_char_handle,
  135. 0,
  136. strlen(dev_info_serial_num),
  137. (uint8_t*)dev_info_serial_num);
  138. if(status) {
  139. FURI_LOG_E(TAG, "Failed to update serial number char: %d", status);
  140. }
  141. status = aci_gatt_update_char_value(
  142. dev_info_svc->service_handle,
  143. dev_info_svc->firmware_rev_char_handle,
  144. 0,
  145. strlen(dev_info_svc->hardware_revision),
  146. (uint8_t*)dev_info_svc->hardware_revision);
  147. if(status) {
  148. FURI_LOG_E(TAG, "Failed to update firmware revision char: %d", status);
  149. }
  150. status = aci_gatt_update_char_value(
  151. dev_info_svc->service_handle,
  152. dev_info_svc->software_rev_char_handle,
  153. 0,
  154. string_size(dev_info_svc->version_string),
  155. (uint8_t*)string_get_cstr(dev_info_svc->version_string));
  156. if(status) {
  157. FURI_LOG_E(TAG, "Failed to update software revision char: %d", status);
  158. }
  159. status = aci_gatt_update_char_value(
  160. dev_info_svc->service_handle,
  161. dev_info_svc->rpc_version_char_handle,
  162. 0,
  163. strlen(dev_info_rpc_version),
  164. (uint8_t*)dev_info_rpc_version);
  165. if(status) {
  166. FURI_LOG_E(TAG, "Failed to update rpc version char: %d", status);
  167. }
  168. }
  169. void dev_info_svc_stop() {
  170. tBleStatus status;
  171. if(dev_info_svc) {
  172. string_clear(dev_info_svc->version_string);
  173. // Delete service characteristics
  174. status =
  175. aci_gatt_del_char(dev_info_svc->service_handle, dev_info_svc->man_name_char_handle);
  176. if(status) {
  177. FURI_LOG_E(TAG, "Failed to delete manufacturer name char: %d", status);
  178. }
  179. status =
  180. aci_gatt_del_char(dev_info_svc->service_handle, dev_info_svc->serial_num_char_handle);
  181. if(status) {
  182. FURI_LOG_E(TAG, "Failed to delete serial number char: %d", status);
  183. }
  184. status = aci_gatt_del_char(
  185. dev_info_svc->service_handle, dev_info_svc->firmware_rev_char_handle);
  186. if(status) {
  187. FURI_LOG_E(TAG, "Failed to delete firmware revision char: %d", status);
  188. }
  189. status = aci_gatt_del_char(
  190. dev_info_svc->service_handle, dev_info_svc->software_rev_char_handle);
  191. if(status) {
  192. FURI_LOG_E(TAG, "Failed to delete software revision char: %d", status);
  193. }
  194. status =
  195. aci_gatt_del_char(dev_info_svc->service_handle, dev_info_svc->rpc_version_char_handle);
  196. if(status) {
  197. FURI_LOG_E(TAG, "Failed to delete rpc version char: %d", status);
  198. }
  199. // Delete service
  200. status = aci_gatt_del_service(dev_info_svc->service_handle);
  201. if(status) {
  202. FURI_LOG_E(TAG, "Failed to delete device info service: %d", status);
  203. }
  204. free(dev_info_svc);
  205. dev_info_svc = NULL;
  206. }
  207. }
  208. bool dev_info_svc_is_started() {
  209. return dev_info_svc != NULL;
  210. }