tile_storage.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * NOTICE
  3. *
  4. * Copyright 2016 Tile Inc. All Rights Reserved.
  5. * All code or other information included in the accompanying files ("Tile Source Material")
  6. * is PROPRIETARY information of Tile Inc. ("Tile") and access and use of the Tile Source Material
  7. * is subject to these terms. The Tile Source Material may only be used for demonstration purposes,
  8. * and may not be otherwise distributed or made available to others, including for commercial purposes.
  9. * Without limiting the foregoing , you understand and agree that no production use
  10. * of the Tile Source Material is allowed without a Tile ID properly obtained under a separate
  11. * agreement with Tile.
  12. * You also understand and agree that Tile may terminate the limited rights granted under these terms
  13. * at any time in its discretion.
  14. * All Tile Source Material is provided AS-IS without warranty of any kind.
  15. * Tile does not warrant that the Tile Source Material will be error-free or fit for your purposes.
  16. * Tile will not be liable for any damages resulting from your use of or inability to use
  17. * the Tile Source Material.
  18. *
  19. * Support: firmware_support@tile.com
  20. */
  21. /**
  22. * @file tile_storage.h
  23. * @brief Tile storage system
  24. */
  25. #ifndef TILE_STORAGE_H_
  26. #define TILE_STORAGE_H_
  27. #include <stdint.h>
  28. #include "tile_lib.h"
  29. #include "nrf_fstorage_sd.h"
  30. #include "tile_tdt_module.h"
  31. #include "ble_gap.h"
  32. /*****************************************/
  33. /* Copied from nordic14, TO DO: find correct definitions for Nordic 15.2 */
  34. #define PAGE_SIZE 4096
  35. /* These addresses should be the two pages directly before the default bootloader location */
  36. #define APP_DATA_BANK0_ADDRESS 0x76000
  37. #define APP_DATA_BANK1_ADDRESS 0x77000
  38. #define APP_DATA_NUM_PAGES 1
  39. /****************************************/
  40. #define DEFAULT_ADVERTISING_INTERVAL 160
  41. #define PERSIST_SIGNATURE 0xA5A5
  42. #define CHECKED_SIZE 128
  43. #define UNCHECKED_SIZE 256
  44. #define CHECKED_STRUCTURE_VERSION_1 1
  45. #define CHECKED_STRUCTURE_VERSION_2 2
  46. #define CHECKED_STRUCTURE_VERSION_3 3
  47. #define CHECKED_STRUCTURE_VERSION_4 4
  48. #define CHECKED_STRUCTURE_VERSION CHECKED_STRUCTURE_VERSION_1
  49. extern nrf_fstorage_t app_data_bank0;
  50. extern nrf_fstorage_t app_data_bank1;
  51. extern uint8_t bdaddr[BLE_GAP_ADDR_LEN];
  52. extern const uint8_t interim_tile_id[];
  53. extern const uint8_t interim_tile_key[];
  54. extern const char tile_model_number[];
  55. extern const char tile_hw_version[];
  56. struct tile_checked_tag
  57. {
  58. /**************************************************************************************************/
  59. /*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ****/
  60. /*** THIS STRUCTURE IS SAVED TO FLASH AND RETRIEVED AFTER TOFU ****/
  61. /*** THIS MEANS STUFF SHOULD NOT BE MODIFIED BUT ONLY AT THE END TO MAINTAIN COMPATIBILITY ****/
  62. /**************************************************************************************************/
  63. uint16_t version;
  64. uint8_t id;
  65. uint8_t bank;
  66. uint8_t mode;
  67. uint16_t adv_int;
  68. tdt_config_t tdt_configuration;
  69. uint8_t tile_id[TILE_ID_LEN];
  70. uint8_t tile_auth_key[TILE_AUTH_KEY_LEN];
  71. char model_number[TILE_MODEL_NUMBER_LEN];
  72. char hardware_version[TILE_HARDWARE_VERSION_LEN];
  73. uint8_t bdaddr[TILE_BDADDR_LEN];
  74. uint8_t tileIDkey[TILEID_KEY_LEN];
  75. };
  76. struct tile_unchecked_tag
  77. {
  78. /**************************************************************************************************/
  79. /*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ****/
  80. /*** THIS STRUCTURE IS SAVED TO FLASH AND RETRIEVED AFTER TOFU ****/
  81. /*** THIS MEANS STUFF SHOULD NOT BE MODIFIED BUT ONLY AT THE END TO MAINTAIN COMPATIBILITY ****/
  82. /**************************************************************************************************/
  83. // Activity tracking
  84. uint32_t connection_count; /**< number of connections */
  85. uint32_t disconnect_count; /**< Number of disconnections */
  86. uint8_t auth_fail_count; /**< authentication failures count */
  87. uint8_t micFailures; /**< mic failures */
  88. uint8_t reset_count; /**< Reset Count */
  89. uint32_t piezoMs; /**< time for which piezo was active in '10 ms' units */
  90. // TOA Activity monitoring
  91. uint32_t toa_channel_open_count; /**< Number of successfull TOA Channel Open (with a successfull authentication) */
  92. uint32_t toa_authenticate_count; /**< number of TOA Authenticate Commands received */
  93. uint16_t tka_closed_channel_count; /**< number of TOA Channel close triggered by TKA */
  94. uint16_t auth_disconnect_count; /**< number of disconnections triggered by Auth Timer */
  95. //Counter for private ID
  96. uint16_t tileIDcounter; /**< Counter used for PrivateID */
  97. };
  98. struct tile_persist_tag
  99. {
  100. uint16_t crc;
  101. uint16_t signature;
  102. union
  103. {
  104. struct tile_checked_tag s;
  105. uint8_t d[CHECKED_SIZE-4]; /* -4 for CRC + signature */
  106. } checked __attribute__ ((aligned (4)));
  107. union
  108. {
  109. struct tile_unchecked_tag s;
  110. uint8_t d[UNCHECKED_SIZE];
  111. } unchecked __attribute__ ((aligned (4)));
  112. };
  113. /**
  114. * @brief Persistent structure, which is saved to flash. Does not need to be
  115. * accessed directly. Access elements with tile_checked and tile_unchecked.
  116. */
  117. extern struct tile_persist_tag tile_persist;
  118. /**
  119. * @brief CRC checked portion of persistent data.
  120. */
  121. extern struct tile_checked_tag * const tile_checked;
  122. /**
  123. * @brief Non-CRC portion of persistent data. This get reinitialized when
  124. * the CRC of the checked portion fails.
  125. */
  126. extern struct tile_unchecked_tag * const tile_unchecked;
  127. /**
  128. * @brief Tile environment data. Lost at reboot.
  129. */
  130. struct tile_env_tag
  131. {
  132. uint16_t last_reset_reason; ///> Contains the reason for the last reset
  133. uint8_t authorized;
  134. uint8_t hashedTileID[TILE_HASHED_TILEID_LEN];
  135. };
  136. extern struct tile_env_tag tile_env;
  137. void tile_storage_init(void);
  138. void tile_store_app_data(void);
  139. #endif