api-hal-version.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <api-hal-version.h>
  2. #include <stm32wbxx.h>
  3. #include <stm32wbxx_ll_rtc.h>
  4. #include <stdio.h>
  5. #include "ble.h"
  6. #define FLIPPER_NAME_LENGTH 8
  7. typedef struct {
  8. uint8_t version;
  9. uint8_t target;
  10. uint8_t body;
  11. uint8_t connect;
  12. uint32_t timestamp;
  13. char name[FLIPPER_NAME_LENGTH];
  14. } ApiHalVersionOTP;
  15. #define FLIPPER_ARRAY_NAME_LENGTH (FLIPPER_NAME_LENGTH + 1)
  16. // BLE symbol + "Flipper " + name
  17. #define FLIPPER_DEVICE_NAME_LENGTH (1 + 8 + FLIPPER_ARRAY_NAME_LENGTH)
  18. // Initialiazed from OTP, used to guarantee zero terminated C string
  19. static char flipper_name[FLIPPER_ARRAY_NAME_LENGTH];
  20. static char flipper_device_name[FLIPPER_DEVICE_NAME_LENGTH];
  21. static uint8_t api_hal_version_ble_mac[6];
  22. void api_hal_version_init() {
  23. char* name = ((ApiHalVersionOTP*)OTP_AREA_BASE)->name;
  24. strlcpy(flipper_name, name, FLIPPER_ARRAY_NAME_LENGTH);
  25. if(api_hal_version_get_name_ptr() != NULL) {
  26. snprintf(
  27. flipper_device_name,
  28. FLIPPER_DEVICE_NAME_LENGTH,
  29. "xFlipper %s",
  30. flipper_name);
  31. } else {
  32. snprintf(
  33. flipper_device_name,
  34. FLIPPER_DEVICE_NAME_LENGTH,
  35. "xFlipper");
  36. }
  37. flipper_device_name[0] = AD_TYPE_COMPLETE_LOCAL_NAME;
  38. // BLE Mac address
  39. uint32_t udn = LL_FLASH_GetUDN();
  40. uint32_t company_id = LL_FLASH_GetSTCompanyID();
  41. uint32_t device_id = LL_FLASH_GetDeviceID();
  42. api_hal_version_ble_mac[0] = (uint8_t)(udn & 0x000000FF);
  43. api_hal_version_ble_mac[1] = (uint8_t)( (udn & 0x0000FF00) >> 8 );
  44. api_hal_version_ble_mac[2] = (uint8_t)( (udn & 0x00FF0000) >> 16 );
  45. api_hal_version_ble_mac[3] = (uint8_t)device_id;
  46. api_hal_version_ble_mac[4] = (uint8_t)(company_id & 0x000000FF);;
  47. api_hal_version_ble_mac[5] = (uint8_t)( (company_id & 0x0000FF00) >> 8 );
  48. }
  49. bool api_hal_version_do_i_belong_here() {
  50. return api_hal_version_get_hw_target() == 6;
  51. }
  52. const char* api_hal_version_get_model_name() {
  53. return "Flipper Zero";
  54. }
  55. const uint8_t api_hal_version_get_hw_version() {
  56. return ((ApiHalVersionOTP*)OTP_AREA_BASE)->version;
  57. }
  58. const uint8_t api_hal_version_get_hw_target() {
  59. return ((ApiHalVersionOTP*)OTP_AREA_BASE)->target;
  60. }
  61. const uint8_t api_hal_version_get_hw_body() {
  62. return ((ApiHalVersionOTP*)OTP_AREA_BASE)->body;
  63. }
  64. const uint8_t api_hal_version_get_hw_color() {
  65. return 0;
  66. }
  67. const uint8_t api_hal_version_get_hw_connect() {
  68. return ((ApiHalVersionOTP*)OTP_AREA_BASE)->connect;
  69. }
  70. const uint8_t api_hal_version_get_hw_region() {
  71. return 0;
  72. }
  73. const uint32_t api_hal_version_get_hw_timestamp() {
  74. return ((ApiHalVersionOTP*)OTP_AREA_BASE)->timestamp;
  75. }
  76. const char* api_hal_version_get_name_ptr() {
  77. return *flipper_name == 0xFFU ? NULL : flipper_name;
  78. }
  79. const char* api_hal_version_get_device_name_ptr() {
  80. return flipper_device_name + 1;
  81. }
  82. const char* api_hal_version_get_ble_local_device_name_ptr() {
  83. return flipper_device_name;
  84. }
  85. const uint8_t* api_hal_version_get_ble_mac() {
  86. return api_hal_version_ble_mac;
  87. }
  88. const struct Version* api_hal_version_get_firmware_version(void) {
  89. return version_get();
  90. }
  91. const struct Version* api_hal_version_get_boot_version(void) {
  92. #ifdef NO_BOOTLOADER
  93. return 0;
  94. #else
  95. /* Backup register which points to structure in flash memory */
  96. return (const struct Version*)LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR1);
  97. #endif
  98. }
  99. size_t api_hal_version_uid_size() {
  100. return 64/8;
  101. }
  102. const uint8_t* api_hal_version_uid() {
  103. return (const uint8_t *)UID64_BASE;
  104. }