antfs.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. This software is subject to the license described in the license.txt file included with this software distribution.
  3. You may not use this file except in compliance with this license.
  4. Copyright © Dynastream Innovations Inc. 2012
  5. All rights reserved.
  6. */
  7. /**@file
  8. * @brief The ANT-FS client protocol interface.
  9. * This file is based on implementation originally made by Dynastream Innovations Inc. - August 2012
  10. * @defgroup ant_fs_client_main ANT-FS client device simulator
  11. * @{
  12. * @ingroup ant_sdk_utils
  13. *
  14. * @brief The ANT-FS client device simulator.
  15. *
  16. * @note The ANT-FS Network Key is available for ANT+ Adopters. Please refer to http://thisisant.com to become an ANT+ Adopter and access the key.
  17. */
  18. #ifndef ANTFS_H__
  19. #define ANTFS_H__
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include "defines.h"
  23. #include "antfs_config.h"
  24. #define ANTFS_VERSION_MAJOR 1u /**< Version major number. */
  25. #define ANTFS_VERSION_MINOR 0 /**< Version minor number. */
  26. #define ANTFS_VERSION_ITERATION 0 /**< Version iteration. */
  27. #define ANTFS_VERSION_TYPE 'R' /**< Version type is release. */
  28. #define ANTFS_VERSION_SPEC '0.AK' /**< Version of the ANT-FS Technology Specification. */
  29. #define ANTFS_DIR_STRUCT_VERSION 1u /**< Version of the directory file structure. */
  30. #define ANTFS_VERSION_DATE 20090522u /**< Version date. */
  31. // ANT-FS options.
  32. #define ANTFS_NETWORK_NUMBER 0 /**< ANT-FS Network Number. */
  33. #define ANTFS_CHANNEL 0 /**< ANT Channel Number. */
  34. #define ANTFS_LINK_FREQ 50u /**< RF Frequency (+2400MHz). */
  35. #define ANTFS_PAIRING_TIMEOUT 120u /**< Pairing timeout (time the UI will wait for a response to the pairing request before switching to the link layer, in seconds). */
  36. #define ANTFS_LINK_COMMAND_TIMEOUT 10u /**< Command timeout (time the client will wait without receiving any commands before switching to the link layer, in seconds). */
  37. #define ANTFS_TRANS_TYPE 5u /**< ANT-FS Transmission Type. */
  38. #define ANTFS_CHANNEL_TYPE CHANNEL_TYPE_MASTER /**< ANT-FS Client Channel Type. */
  39. #define ANTFS_BEACON_PERIOD_STATUS BEACON_PERIOD_4_HZ /**< ANT-FS Beacon Message Period. */
  40. #define ANTFS_TRANSMIT_POWER 3u /**< ANT Transmit Power (0dBm). */
  41. #define ANTFS_CUSTOM_TRANSMIT_POWER 0u /**< ANT Custom Transmit Power (0dBm). */
  42. #define ANTFS_AUTH_STRING_MAX 255u /**< Maximum size of authentication strings (passkey/friendly name). */
  43. #define ANTFS_PASSKEY_SIZE 16u /**< Passkey size. */
  44. #define ANTFS_FRIENDLY_NAME_MAX 16u /**< Maximum size of friendly name received from host. */
  45. #define ANTFS_REMOTE_FRIENDLY_NAME_MAX 16u /**< Maximum size of client's friendly name. */
  46. // Beacon definitions.
  47. #define BEACON_PERIOD_SHIFT 0x00 /**< Shift value for masking out beacon period. */
  48. #define BEACON_PERIOD_MASK (0x07u << BEACON_PERIOD_SHIFT) /**< Beacon period bitmask. */
  49. #define BEACON_PERIOD_0_5_HZ (0x00 << BEACON_PERIOD_SHIFT) /**< Value for 0,5Hz beacon period. */
  50. #define BEACON_PERIOD_1_HZ (0x01u << BEACON_PERIOD_SHIFT) /**< Value for 1Hz beacon period. */
  51. #define BEACON_PERIOD_2_HZ (0x02u << BEACON_PERIOD_SHIFT) /**< Value for 2Hz beacon period. */
  52. #define BEACON_PERIOD_4_HZ (0x03u << BEACON_PERIOD_SHIFT) /**< Value for 4Hz beacon period. */
  53. #define BEACON_PERIOD_8_HZ (0x04u << BEACON_PERIOD_SHIFT) /**< Value for 8Hz beacon period. */
  54. #define PAIRING_AVAILABLE_FLAG_SHIFT 0x03u /**< Shift value for masking out pairing enabled bit. */
  55. #define PAIRING_AVAILABLE_FLAG_MASK (0x01u << PAIRING_AVAILABLE_FLAG_SHIFT) /**< Pairing enabled bitmask. */
  56. #define UPLOAD_ENABLED_FLAG_SHIFT 0x04u /**< Shift value for masking out upload enabled bit. */
  57. #define UPLOAD_ENABLED_FLAG_MASK (0x01u << UPLOAD_ENABLED_FLAG_SHIFT) /**< Upload enabled bitmask. */
  58. #define DATA_AVAILABLE_FLAG_SHIFT 0x05u /**< Shift value for masking out data available bit. */
  59. #define DATA_AVAILABLE_FLAG_MASK (0x01u << DATA_AVAILABLE_FLAG_SHIFT) /**< Data available bitmask. */
  60. // Build the default beacon settings.
  61. #if defined (ANTFS_AUTH_TYPE_PAIRING)
  62. #define ANTFS_PAIRING_BIT PAIRING_AVAILABLE_FLAG_MASK /**< Build pairing enabled default beacon setting. */
  63. #else
  64. #define ANTFS_PAIRING_BIT 0x00u /**< Build pairing disabled default beacon setting. */
  65. #endif // ANTFS_INCLUDE_PAIRING
  66. #if defined (ANTFS_INCLUDE_UPLOAD)
  67. #define ANTFS_UPLOAD_BIT UPLOAD_ENABLED_FLAG_MASK /**< Build upload enabled default beacon setting. */
  68. #else
  69. #define ANTFS_UPLOAD_BIT 0x00u /**< Build upload disabled default beacon setting. */
  70. #endif // ANTFS_INCLUDE_UPLOAD
  71. #define ANTFS_DEFAULT_BEACON (ANTFS_BEACON_PERIOD_STATUS | ANTFS_UPLOAD_BIT | ANTFS_PAIRING_BIT | DATA_AVAILABLE_FLAG_MASK) /**< Define the default beacon setting. */
  72. // Download/Upload responses.
  73. #define RESPONSE_MESSAGE_OK 0x00u /**< Download request ok. */
  74. #define RESPONSE_MESSAGE_NOT_EXIST 0x01u /**< File does not exist. */
  75. #define RESPONSE_MESSAGE_NOT_AVAILABLE 0x02u /**< File can not be read/written to (download/upload respectively). */
  76. #define RESPONSE_INVALID_OPERATION 0x04u /**< Request invalid. */
  77. // Download responses.
  78. #define RESPONSE_MESSAGE_NOT_READY 0x03u /**< Not ready to download. */
  79. #define RESPONSE_INVALID_CRC 0x05u /**< CRC incorrect. */
  80. // Upload responses.
  81. #define RESPONSE_MESSAGE_NOT_ENOUGH_SPACE 0x03u /**< Not enough space to to complete write. */
  82. #define RESPONSE_MESSAGE_UPLOAD_NOT_READY 0x05u /**< Not ready to upload */
  83. // Upload/Erase responses.
  84. #define RESPONSE_MESSAGE_FAIL 0x01u /**< Data File Index does not exist / Erase failed. */
  85. // Directory general file flags.
  86. #define ANTFS_DIR_READ_MASK 0x80u /**< Read (can download). */
  87. #define ANTFS_DIR_WRITE_MASK 0x40u /**< Write (can upload). */
  88. #define ANTFS_DIR_ERASE_MASK 0x20u /**< Erase (can erase). */
  89. #define ANTFS_DIR_ARCHIVE_MASK 0x10u /**< Archive (has been downloaded). */
  90. #define ANTFS_DIR_APPEND_MASK 0x08u /**< Append (can append to file only). */
  91. #define ANTFS_MAX_FILE_SIZE 0xFFFFFFFFu /**< Maximum file size, as specified by directory structure. */
  92. #define ANTFS_BURST_BLOCK_SIZE 16u /**< Size of each block of burst data that the client attempts to send when it processes a data request event. */
  93. /**@brief ANT-FS beacon status. */
  94. typedef union
  95. {
  96. uint32_t status; /**< Beacon status byte 1. */
  97. struct
  98. {
  99. uint32_t link_period : 3; /**< Beacon period (0.5 - 8 Hz). */
  100. bool is_pairing_enabled : 1; /**< Pairing is enabled/disabled. */
  101. bool is_upload_enabled : 1; /**< Upload is enabled/disabled. */
  102. bool is_data_available : 1; /**< Data is available for download / no data available. */
  103. bool reserved : 2; /**< Reserved. */
  104. } parameters;
  105. } antfs_beacon_status_byte1_t;
  106. // ANT-FS states.
  107. typedef enum
  108. {
  109. ANTFS_STATE_OFF, /**< Off state. */
  110. ANTFS_STATE_INIT, /**< Init state. */
  111. ANTFS_STATE_LINK, /**< Link state. */
  112. ANTFS_STATE_AUTH, /**< Authenticate state. */
  113. ANTFS_STATE_TRANS /**< Transport state. */
  114. } antfs_state_t;
  115. // ANT-FS link layer substates.
  116. typedef enum
  117. {
  118. ANTFS_LINK_SUBSTATE_NONE /**< None state. */
  119. } antfs_link_substate_t;
  120. // ANT-FS authenticate layer substates. */
  121. typedef enum
  122. {
  123. ANTFS_AUTH_SUBSTATE_NONE, /**< None state. */
  124. ANTFS_AUTH_SUBSTATE_PAIR, /**< Pairing state. */
  125. ANTFS_AUTH_SUBSTATE_PASSKEY, /**< Passkey state. */
  126. ANTFS_AUTH_SUBSTATE_ACCEPT, /**< Authenticate accept state. */
  127. ANTFS_AUTH_SUBSTATE_REJECT /**< Authenticate reject state. */
  128. } antfs_authenticate_substate_t;
  129. // ANT-FS transport layer substates. */
  130. typedef enum
  131. {
  132. ANTFS_TRANS_SUBSTATE_NONE, /**< None state. */
  133. ANTFS_TRANS_SUBSTATE_VERIFY_CRC, /**< Verify CRC state. */
  134. ANTFS_TRANS_SUBSTATE_DOWNLOADING, /**< Downloading state. */
  135. ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA, /**< Wait for upload data request state. */
  136. ANTFS_TRANS_SUBSTATE_UPLOADING, /**< Ready / receiving upload data state. */
  137. ANTFS_TRANS_SUBSTATE_UPLOAD_RESUME /**< RX failure upon receiving upload data state. */
  138. } antfs_transport_substate_t;
  139. // ANT-FS Events.
  140. typedef enum
  141. {
  142. ANTFS_EVENT_PAIRING_REQUEST = 0xB0, /**< Pairing request event. */
  143. ANTFS_EVENT_PAIRING_TIMEOUT = 0xB1, /**< Pairing timeout event. */
  144. ANTFS_EVENT_OPEN_COMPLETE = 0xB2, /**< Channel setup complete event. */
  145. ANTFS_EVENT_CLOSE_COMPLETE = 0xB4, /**< Channel closed event. */
  146. ANTFS_EVENT_LINK = 0xB6, /**< Enter link layer event. */
  147. ANTFS_EVENT_AUTH = 0xB7, /**< Enter authenticate layer event. */
  148. ANTFS_EVENT_TRANS = 0xB8, /**< Enter transport layer event. */
  149. ANTFS_EVENT_DOWNLOAD_REQUEST = 0xB9, /**< Download request event. */
  150. ANTFS_EVENT_DOWNLOAD_REQUEST_DATA = 0xBA, /**< Download request data event. */
  151. ANTFS_EVENT_DOWNLOAD_START = 0xBB, /**< Download started event. */
  152. ANTFS_EVENT_DOWNLOAD_COMPLETE = 0xBC, /**< Download completed event. */
  153. ANTFS_EVENT_DOWNLOAD_FAIL = 0xBD, /**< Download failed event. */
  154. ANTFS_EVENT_UPLOAD_REQUEST = 0xBE, /**< Upload request event. */
  155. ANTFS_EVENT_UPLOAD_DATA = 0xBF, /**< Upload data available for read event. */
  156. ANTFS_EVENT_UPLOAD_START = 0xC0, /**< Upload begin event. */
  157. ANTFS_EVENT_UPLOAD_COMPLETE = 0xC1, /**< Upload completed event. */
  158. ANTFS_EVENT_UPLOAD_FAIL = 0xC2, /**< Upload process failed event. */
  159. ANTFS_EVENT_ERASE_REQUEST = 0xC3 /**< Erase request event. */
  160. } antfs_event_t;
  161. /**@brief ANT-FS <-> application event communication object. */
  162. typedef struct
  163. {
  164. antfs_event_t event; /**< ANT-FS event. */
  165. uint16_t file_index; /**< File index (download/upload/erase). */
  166. uint32_t offset; /**< Current offset (download/upload). */
  167. uint32_t bytes; /**< Number of bytes in block (download/upload). */
  168. uint16_t crc; /**< Current CRC (upload). */
  169. uint8_t data[8]; /**< Block of data (upload). */
  170. } antfs_event_return_t;
  171. /**@brief ANT-FS parameters. */
  172. typedef struct
  173. {
  174. uint32_t client_serial_number; /**< Client serial number. */
  175. uint16_t beacon_device_type; /**< Client device type. */
  176. uint16_t beacon_device_manufacturing_id; /**< Client manufacturing ID. */
  177. uint8_t beacon_frequency; /**< Beacon RF Frequency. */
  178. antfs_beacon_status_byte1_t beacon_status_byte1; /**< Beacon status byte 1. */
  179. const uint8_t * p_pass_key; /**< Pass Key. */
  180. const uint8_t * p_remote_friendly_name; /**< Friendly Name. */
  181. } antfs_params_t;
  182. /**@brief ANT-FS directory header. */
  183. typedef struct
  184. {
  185. uint8_t version; /**< Version of the directory file structure. */
  186. uint8_t length; /**< Length of each structure, in bytes. */
  187. uint8_t time_format; /**< Defines how system keeps track of date/time stamps. */
  188. uint8_t reserved01;
  189. uint8_t reserved02;
  190. uint8_t reserved03;
  191. uint8_t reserved04;
  192. uint8_t reserved05;
  193. uint32_t system_time; /**< Number of seconds elapsed since system power up. */
  194. uint32_t date; /**< Number of seconds elapsed since 00:00 hrs Dec 31, 1989. If system time is unknown, used as counter. */
  195. } antfs_dir_header_t;
  196. /**@brief ANT-FS directory entry. */
  197. typedef struct
  198. {
  199. uint16_t data_file_index; /**< Data file index. */
  200. uint8_t file_data_type; /**< File data type. */
  201. uint8_t user_defined1; /**< Identifier, first byte (structure defined by data type). */
  202. uint16_t user_defined2; /**< Identifier, last two bytes (structure defined by data type). */
  203. uint8_t user_flags; /**< File data type specific flags (bits defined by data type). */
  204. uint8_t general_flags; /**< Bit mapped flags of flag permissions. */
  205. uint32_t file_size_in_bytes; /**< File size, in bytes. */
  206. uint32_t date; /**< Number of seconds elapsed since 00:00 hrs Dec 31, 1980, if supported. */
  207. } antfs_dir_struct_t;
  208. /**@brief ANT-FS download/upload request context. */
  209. typedef struct
  210. {
  211. ulong_union_t file_size; /**< Size of a file to download when reading, or the size of a partially completed upload when writing. */
  212. uint32_t max_file_size; /**< The maximum size of the file specified, this is the file size when reading, and the maximum allowed file size when writing. */
  213. ulong_union_t max_burst_block_size; /**< Maximum burst block size. */
  214. ushort_union_t file_index; /**< File index. */
  215. uint16_t file_crc; /**< CRC (uploads). */
  216. } antfs_request_info_t;
  217. /**@brief The burst wait handler can be configured by the application to customize the code that is
  218. * executed while waiting for the burst busy flag. */
  219. typedef void(*antfs_burst_wait_handler_t)(void);
  220. /**@brief Function for setting initial ANT-FS configuration parameters.
  221. *
  222. * @param[in] p_params The initial ANT-FS configuration parameters.
  223. * @param[in] burst_wait_handler Burst wait handler.
  224. */
  225. void antfs_init(const antfs_params_t * const p_params,
  226. antfs_burst_wait_handler_t burst_wait_handler);
  227. /**@brief Function for getting host name if received.
  228. *
  229. * @return Pointer to host name buffer if a host name was recieved, NULL otherwise.
  230. */
  231. const char * antfs_hostname_get(void);
  232. /**@brief Function for transmitting a response to a pairing request issued by ANT-FS host.
  233. *
  234. * @param[in] accept The pairing response, true if pairing accepted.
  235. *
  236. * @retval true Operation success. Response to a pairing request was transmitted.
  237. * @retval false Operation failure. Not in pairing mode or pairing not supported by the
  238. * implementation.
  239. */
  240. bool antfs_pairing_resp_transmit(bool accept);
  241. /**@brief Function for doing calculations prior downloading the data to the ANT-FS host.
  242. *
  243. * Function does the necessary pre processing calculations, which are required prior downloading the
  244. * data, and also transmits the download request response right away in case of the download request
  245. * was rejected or there is no data to send.
  246. *
  247. * @param[in] response The download request response code.
  248. * @param[in] p_request_info ANT-FS request info structure.
  249. */
  250. void antfs_download_req_resp_prepare(uint8_t response,
  251. const antfs_request_info_t * const p_request_info);
  252. /**@brief Function for downloading requested data.
  253. *
  254. * @param[in] index Index of the current file downloaded.
  255. * @param[in] offset Offset specified by client.
  256. * @param[in] num_bytes Number of bytes requested to be transmitted from the buffer.
  257. * @param[in] p_message Data buffer to be transmitted.
  258. *
  259. * @return Number of data bytes transmitted.
  260. */
  261. uint32_t antfs_input_data_download(uint16_t index,
  262. uint32_t offset,
  263. uint32_t num_bytes,
  264. const uint8_t * const p_message);
  265. /**@brief Function for transmitting upload request response to a upload request command by ANT-FS
  266. * host.
  267. *
  268. * @param[in] response The upload response code.
  269. * @param[in] p_request_info ANT-FS request info structure.
  270. *
  271. * @retval true Operation success. Response to upload request command was transmitted.
  272. * @retval false Operation failure. Upload not supported by the implementation or not in correct
  273. * state or application is sending a response for a different file
  274. * than requested.
  275. */
  276. bool antfs_upload_req_resp_transmit(uint8_t response,
  277. const antfs_request_info_t * const p_request_info);
  278. /**@brief Function for transmitting upload data response to a upload data command by ANT-FS host.
  279. *
  280. * @param[in] data_upload_success The upload response code, true for success.
  281. *
  282. * @retval true Operation success. Response to upload data command was transmitted.
  283. * @retval false Operation failure. Upload not supported by the implementation or not in correct
  284. * state.
  285. */
  286. bool antfs_upload_data_resp_transmit(bool data_upload_success);
  287. /**@brief Function for transmitting erase response to a erase request.
  288. *
  289. * @param[in] response The erase response code.
  290. */
  291. void antfs_erase_req_resp_transmit(uint8_t response);
  292. /**@brief Function for extracting possible pending ANT-FS event.
  293. *
  294. * @param[out] p_event The output event structure.
  295. *
  296. * @retval true Operation success. Pending ANT-FS event available and it was copied to the output
  297. * event structure.
  298. * @retval false Operation failure. No pending ANT-FS event available.
  299. */
  300. bool antfs_event_extract(antfs_event_return_t * const p_event);
  301. /**@brief Function for processing ANT events and data received from the ANT-FS channel.
  302. *
  303. * @param[in] p_message The message buffer containing the message received from the ANT-FS
  304. * channel.
  305. */
  306. void antfs_message_process(uint8_t * p_message);
  307. /**@brief Function for setting up the ANT-FS channel.
  308. */
  309. void antfs_channel_setup(void);
  310. #endif // ANTFS_H__
  311. /**
  312. *@}
  313. **/