antfs.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /**
  2. * This software is subject to the ANT+ Shared Source License
  3. * www.thisisant.com/swlicenses
  4. * Copyright (c) Garmin Canada Inc. 2012
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * 1) Redistributions of source code must retain the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer.
  14. *
  15. * 2) Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * 3) Neither the name of Garmin nor the names of its
  21. * contributors may be used to endorse or promote products
  22. * derived from this software without specific prior
  23. * written permission.
  24. *
  25. * The following actions are prohibited:
  26. *
  27. * 1) Redistribution of source code containing the ANT+ Network
  28. * Key. The ANT+ Network Key is available to ANT+ Adopters.
  29. * Please refer to http://thisisant.com to become an ANT+
  30. * Adopter and access the key.
  31. *
  32. * 2) Reverse engineering, decompilation, and/or disassembly of
  33. * software provided in binary form under this license.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE HEREBY
  39. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
  42. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; DAMAGE TO ANY DEVICE, LOSS OF USE, DATA, OR
  44. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  45. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  47. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE. SOME STATES DO NOT ALLOW
  49. * THE EXCLUSION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THE
  50. * ABOVE LIMITATIONS MAY NOT APPLY TO YOU.
  51. *
  52. */
  53. /**@file
  54. * @brief The ANT-FS client protocol interface.
  55. * This file is based on implementation originally made in August 2012 by Garmin Canada Inc.
  56. * (former Dynastream Innovations Inc.)
  57. * @defgroup ant_fs ANT-FS client device simulator
  58. * @{
  59. * @ingroup ant_sdk_utils
  60. *
  61. * @brief The ANT-FS client device simulator.
  62. *
  63. * @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.
  64. */
  65. #ifndef ANTFS_H__
  66. #define ANTFS_H__
  67. #include <stdint.h>
  68. #include <stdbool.h>
  69. #include "defines.h"
  70. #include "sdk_config.h"
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. #define ANTFS_VERSION_MAJOR 1u /**< Version major number. */
  75. #define ANTFS_VERSION_MINOR 0 /**< Version minor number. */
  76. #define ANTFS_VERSION_ITERATION 0 /**< Version iteration. */
  77. #define ANTFS_VERSION_TYPE 'R' /**< Version type is release. */
  78. #define ANTFS_VERSION_SPEC '0.AK' /**< Version of the ANT-FS Technology Specification. */
  79. #define ANTFS_DIR_STRUCT_VERSION 1u /**< Version of the directory file structure. */
  80. #define ANTFS_VERSION_DATE 20090522u /**< Version date. */
  81. // ANT-FS options.
  82. #define ANTFS_LINK_FREQ 50u /**< RF Frequency (+2400MHz). */
  83. #define ANTFS_CHANNEL_TYPE CHANNEL_TYPE_MASTER /**< ANT-FS Client Channel Type. */
  84. #define ANTFS_AUTH_STRING_MAX 255u /**< Maximum size of authentication strings (passkey/friendly name). */
  85. #define ANTFS_PASSKEY_SIZE 16u /**< Passkey size. */
  86. #define ANTFS_FRIENDLY_NAME_MAX 16u /**< Maximum size of friendly name received from host. */
  87. #define ANTFS_REMOTE_FRIENDLY_NAME_MAX 16u /**< Maximum size of client's friendly name. */
  88. // Beacon definitions.
  89. #define BEACON_PERIOD_SHIFT 0x00 /**< Shift value for masking out beacon period. */
  90. #define BEACON_PERIOD_MASK (0x07u << BEACON_PERIOD_SHIFT) /**< Beacon period bitmask. */
  91. #define BEACON_PERIOD_0_5_HZ (0x00 << BEACON_PERIOD_SHIFT) /**< Value for 0,5Hz beacon period. */
  92. #define BEACON_PERIOD_1_HZ (0x01u << BEACON_PERIOD_SHIFT) /**< Value for 1Hz beacon period. */
  93. #define BEACON_PERIOD_2_HZ (0x02u << BEACON_PERIOD_SHIFT) /**< Value for 2Hz beacon period. */
  94. #define BEACON_PERIOD_4_HZ (0x03u << BEACON_PERIOD_SHIFT) /**< Value for 4Hz beacon period. */
  95. #define BEACON_PERIOD_8_HZ (0x04u << BEACON_PERIOD_SHIFT) /**< Value for 8Hz beacon period. */
  96. #define PAIRING_AVAILABLE_FLAG_SHIFT 0x03u /**< Shift value for masking out pairing enabled bit. */
  97. #define PAIRING_AVAILABLE_FLAG_MASK (0x01u << PAIRING_AVAILABLE_FLAG_SHIFT) /**< Pairing enabled bitmask. */
  98. #define UPLOAD_ENABLED_FLAG_SHIFT 0x04u /**< Shift value for masking out upload enabled bit. */
  99. #define UPLOAD_ENABLED_FLAG_MASK (0x01u << UPLOAD_ENABLED_FLAG_SHIFT) /**< Upload enabled bitmask. */
  100. #define DATA_AVAILABLE_FLAG_SHIFT 0x05u /**< Shift value for masking out data available bit. */
  101. #define DATA_AVAILABLE_FLAG_MASK (0x01u << DATA_AVAILABLE_FLAG_SHIFT) /**< Data available bitmask. */
  102. #if ANTFS_ENABLED
  103. // Build the default beacon settings.
  104. #if ANTFS_CONFIG_AUTH_TYPE_PAIRING_ENABLED
  105. #define ANTFS_PAIRING_BIT PAIRING_AVAILABLE_FLAG_MASK /**< Build pairing enabled default beacon setting. */
  106. #else
  107. #define ANTFS_PAIRING_BIT 0x00u /**< Build pairing disabled default beacon setting. */
  108. #endif // ANTFS_CONFIG_AUTH_TYPE_PAIRING_ENABLED
  109. #if ANTFS_CONFIG_UPLOAD_ENABLED
  110. #define ANTFS_UPLOAD_BIT UPLOAD_ENABLED_FLAG_MASK /**< Build upload enabled default beacon setting. */
  111. #else
  112. #define ANTFS_UPLOAD_BIT 0x00u /**< Build upload disabled default beacon setting. */
  113. #endif // ANTFS_CONFIG_UPLOAD_ENABLED
  114. #define ANTFS_DEFAULT_BEACON (ANTFS_CONFIG_BEACON_STATUS_PERIOD | ANTFS_UPLOAD_BIT | ANTFS_PAIRING_BIT | DATA_AVAILABLE_FLAG_MASK) /**< Define the default beacon setting. */
  115. #endif // ANTFS_ENABLED
  116. // Download/Upload responses.
  117. #define RESPONSE_MESSAGE_OK 0x00u /**< Download request ok. */
  118. #define RESPONSE_MESSAGE_NOT_EXIST 0x01u /**< File does not exist. */
  119. #define RESPONSE_MESSAGE_NOT_AVAILABLE 0x02u /**< File can not be read/written to (download/upload respectively). */
  120. #define RESPONSE_INVALID_OPERATION 0x04u /**< Request invalid. */
  121. // Download responses.
  122. #define RESPONSE_MESSAGE_NOT_READY 0x03u /**< Not ready to download. */
  123. #define RESPONSE_INVALID_CRC 0x05u /**< CRC incorrect. */
  124. // Upload responses.
  125. #define RESPONSE_MESSAGE_NOT_ENOUGH_SPACE 0x03u /**< Not enough space to to complete write. */
  126. #define RESPONSE_MESSAGE_UPLOAD_NOT_READY 0x05u /**< Not ready to upload */
  127. // Upload/Erase responses.
  128. #define RESPONSE_MESSAGE_FAIL 0x01u /**< Data File Index does not exist / Erase failed. */
  129. // Directory general file flags.
  130. #define ANTFS_DIR_READ_MASK 0x80u /**< Read (can download). */
  131. #define ANTFS_DIR_WRITE_MASK 0x40u /**< Write (can upload). */
  132. #define ANTFS_DIR_ERASE_MASK 0x20u /**< Erase (can erase). */
  133. #define ANTFS_DIR_ARCHIVE_MASK 0x10u /**< Archive (has been downloaded). */
  134. #define ANTFS_DIR_APPEND_MASK 0x08u /**< Append (can append to file only). */
  135. #define ANTFS_MAX_FILE_SIZE 0xFFFFFFFFu /**< Maximum file size, as specified by directory structure. */
  136. #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. */
  137. /**@brief ANT-FS beacon status. */
  138. typedef union
  139. {
  140. uint32_t status; /**< Beacon status byte 1. */
  141. struct
  142. {
  143. uint8_t link_period : 3; /**< Beacon period (0.5 - 8 Hz). */
  144. bool is_pairing_enabled : 1; /**< Pairing is enabled/disabled. */
  145. bool is_upload_enabled : 1; /**< Upload is enabled/disabled. */
  146. bool is_data_available : 1; /**< Data is available for download / no data available. */
  147. uint8_t reserved : 2; /**< Reserved. */
  148. } parameters;
  149. } antfs_beacon_status_byte1_t;
  150. // ANT-FS states.
  151. typedef enum
  152. {
  153. ANTFS_STATE_OFF, /**< Off state. */
  154. ANTFS_STATE_INIT, /**< Init state. */
  155. ANTFS_STATE_LINK, /**< Link state. */
  156. ANTFS_STATE_AUTH, /**< Authenticate state. */
  157. ANTFS_STATE_TRANS /**< Transport state. */
  158. } antfs_state_t;
  159. // ANT-FS link layer substates.
  160. typedef enum
  161. {
  162. ANTFS_LINK_SUBSTATE_NONE /**< None state. */
  163. } antfs_link_substate_t;
  164. // ANT-FS authenticate layer substates. */
  165. typedef enum
  166. {
  167. ANTFS_AUTH_SUBSTATE_NONE, /**< None state. */
  168. ANTFS_AUTH_SUBSTATE_PAIR, /**< Pairing state. */
  169. ANTFS_AUTH_SUBSTATE_PASSKEY, /**< Passkey state. */
  170. ANTFS_AUTH_SUBSTATE_ACCEPT, /**< Authenticate accept state. */
  171. ANTFS_AUTH_SUBSTATE_REJECT /**< Authenticate reject state. */
  172. } antfs_authenticate_substate_t;
  173. // ANT-FS transport layer substates. */
  174. typedef enum
  175. {
  176. ANTFS_TRANS_SUBSTATE_NONE, /**< None state. */
  177. ANTFS_TRANS_SUBSTATE_VERIFY_CRC, /**< Verify CRC state. */
  178. ANTFS_TRANS_SUBSTATE_DOWNLOADING, /**< Downloading state. */
  179. ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA, /**< Wait for upload data request state. */
  180. ANTFS_TRANS_SUBSTATE_UPLOADING, /**< Ready / receiving upload data state. */
  181. ANTFS_TRANS_SUBSTATE_UPLOAD_RESUME /**< RX failure upon receiving upload data state. */
  182. } antfs_transport_substate_t;
  183. // ANT-FS Events.
  184. typedef enum
  185. {
  186. ANTFS_EVENT_PAIRING_REQUEST = 0xB0, /**< Pairing request event. */
  187. ANTFS_EVENT_PAIRING_TIMEOUT = 0xB1, /**< Pairing timeout event. */
  188. ANTFS_EVENT_OPEN_COMPLETE = 0xB2, /**< Channel setup complete event. */
  189. ANTFS_EVENT_CLOSE_COMPLETE = 0xB4, /**< Channel closed event. */
  190. ANTFS_EVENT_LINK = 0xB6, /**< Enter link layer event. */
  191. ANTFS_EVENT_AUTH = 0xB7, /**< Enter authenticate layer event. */
  192. ANTFS_EVENT_TRANS = 0xB8, /**< Enter transport layer event. */
  193. ANTFS_EVENT_DOWNLOAD_REQUEST = 0xB9, /**< Download request event. */
  194. ANTFS_EVENT_DOWNLOAD_REQUEST_DATA = 0xBA, /**< Download request data event. */
  195. ANTFS_EVENT_DOWNLOAD_START = 0xBB, /**< Download started event. */
  196. ANTFS_EVENT_DOWNLOAD_COMPLETE = 0xBC, /**< Download completed event. */
  197. ANTFS_EVENT_DOWNLOAD_FAIL = 0xBD, /**< Download failed event. */
  198. ANTFS_EVENT_UPLOAD_REQUEST = 0xBE, /**< Upload request event. */
  199. ANTFS_EVENT_UPLOAD_DATA = 0xBF, /**< Upload data available for read event. */
  200. ANTFS_EVENT_UPLOAD_START = 0xC0, /**< Upload begin event. */
  201. ANTFS_EVENT_UPLOAD_COMPLETE = 0xC1, /**< Upload completed event. */
  202. ANTFS_EVENT_UPLOAD_FAIL = 0xC2, /**< Upload process failed event. */
  203. ANTFS_EVENT_ERASE_REQUEST = 0xC3 /**< Erase request event. */
  204. } antfs_event_t;
  205. /**@brief ANT-FS <-> application event communication object. */
  206. typedef struct
  207. {
  208. antfs_event_t event; /**< ANT-FS event. */
  209. uint16_t file_index; /**< File index (download/upload/erase). */
  210. uint32_t offset; /**< Current offset (download/upload). */
  211. uint32_t bytes; /**< Number of bytes in block (download/upload). */
  212. uint16_t crc; /**< Current CRC (upload). */
  213. uint8_t data[8]; /**< Block of data (upload). */
  214. } antfs_event_return_t;
  215. /**@brief ANT-FS parameters. */
  216. typedef struct
  217. {
  218. uint32_t client_serial_number; /**< Client serial number. */
  219. uint16_t beacon_device_type; /**< Client device type. */
  220. uint16_t beacon_device_manufacturing_id; /**< Client manufacturing ID. */
  221. uint8_t beacon_frequency; /**< Beacon RF Frequency. */
  222. antfs_beacon_status_byte1_t beacon_status_byte1; /**< Beacon status byte 1. */
  223. const uint8_t * p_pass_key; /**< Pass Key. */
  224. const uint8_t * p_remote_friendly_name; /**< Friendly Name. */
  225. } antfs_params_t;
  226. /**@brief ANT-FS directory header. */
  227. typedef struct
  228. {
  229. uint8_t version; /**< Version of the directory file structure. */
  230. uint8_t length; /**< Length of each structure, in bytes. */
  231. uint8_t time_format; /**< Defines how system keeps track of date/time stamps. */
  232. uint8_t reserved01;
  233. uint8_t reserved02;
  234. uint8_t reserved03;
  235. uint8_t reserved04;
  236. uint8_t reserved05;
  237. uint32_t system_time; /**< Number of seconds elapsed since system power up. */
  238. uint32_t date; /**< Number of seconds elapsed since 00:00 hrs Dec 31, 1989. If system time is unknown, used as counter. */
  239. } antfs_dir_header_t;
  240. /**@brief ANT-FS directory entry. */
  241. typedef struct
  242. {
  243. uint16_t data_file_index; /**< Data file index. */
  244. uint8_t file_data_type; /**< File data type. */
  245. uint8_t user_defined1; /**< Identifier, first byte (structure defined by data type). */
  246. uint16_t user_defined2; /**< Identifier, last two bytes (structure defined by data type). */
  247. uint8_t user_flags; /**< File data type specific flags (bits defined by data type). */
  248. uint8_t general_flags; /**< Bit mapped flags of flag permissions. */
  249. uint32_t file_size_in_bytes; /**< File size, in bytes. */
  250. uint32_t date; /**< Number of seconds elapsed since 00:00 hrs Dec 31, 1980, if supported. */
  251. } antfs_dir_struct_t;
  252. /**@brief ANT-FS download/upload request context. */
  253. typedef struct
  254. {
  255. ulong_union_t file_size; /**< Size of a file to download when reading, or the size of a partially completed upload when writing. */
  256. 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. */
  257. ulong_union_t max_burst_block_size; /**< Maximum burst block size. */
  258. ushort_union_t file_index; /**< File index. */
  259. uint16_t file_crc; /**< CRC (uploads). */
  260. } antfs_request_info_t;
  261. /**@brief The burst wait handler can be configured by the application to customize the code that is
  262. * executed while waiting for the burst busy flag. */
  263. typedef void(*antfs_burst_wait_handler_t)(void);
  264. /**@brief Function for setting initial ANT-FS configuration parameters.
  265. *
  266. * @param[in] p_params The initial ANT-FS configuration parameters.
  267. * @param[in] burst_wait_handler Burst wait handler.
  268. */
  269. void antfs_init(const antfs_params_t * const p_params,
  270. antfs_burst_wait_handler_t burst_wait_handler);
  271. /**@brief Function for getting host name if received.
  272. *
  273. * @return Pointer to host name buffer if a host name was recieved, NULL otherwise.
  274. */
  275. const char * antfs_hostname_get(void);
  276. /**@brief Function for transmitting a response to a pairing request issued by ANT-FS host.
  277. *
  278. * @param[in] accept The pairing response, true if pairing accepted.
  279. *
  280. * @retval true Operation success. Response to a pairing request was transmitted.
  281. * @retval false Operation failure. Not in pairing mode or pairing not supported by the
  282. * implementation.
  283. */
  284. bool antfs_pairing_resp_transmit(bool accept);
  285. /**@brief Function for doing calculations prior downloading the data to the ANT-FS host.
  286. *
  287. * Function does the necessary pre processing calculations, which are required prior downloading the
  288. * data, and also transmits the download request response right away in case of the download request
  289. * was rejected or there is no data to send.
  290. *
  291. * @param[in] response The download request response code.
  292. * @param[in] p_request_info ANT-FS request info structure.
  293. */
  294. void antfs_download_req_resp_prepare(uint8_t response,
  295. const antfs_request_info_t * const p_request_info);
  296. /**@brief Function for downloading requested data.
  297. *
  298. * @param[in] index Index of the current file downloaded.
  299. * @param[in] offset Offset specified by client.
  300. * @param[in] num_bytes Number of bytes requested to be transmitted from the buffer.
  301. * @param[in] p_message Data buffer to be transmitted.
  302. *
  303. * @return Number of data bytes transmitted.
  304. */
  305. uint32_t antfs_input_data_download(uint16_t index,
  306. uint32_t offset,
  307. uint32_t num_bytes,
  308. const uint8_t * const p_message);
  309. /**@brief Function for transmitting upload request response to a upload request command by ANT-FS
  310. * host.
  311. *
  312. * @param[in] response The upload response code.
  313. * @param[in] p_request_info ANT-FS request info structure.
  314. *
  315. * @retval true Operation success. Response to upload request command was transmitted.
  316. * @retval false Operation failure. Upload not supported by the implementation or not in correct
  317. * state or application is sending a response for a different file
  318. * than requested.
  319. */
  320. bool antfs_upload_req_resp_transmit(uint8_t response,
  321. const antfs_request_info_t * const p_request_info);
  322. /**@brief Function for transmitting upload data response to a upload data command by ANT-FS host.
  323. *
  324. * @param[in] data_upload_success The upload response code, true for success.
  325. *
  326. * @retval true Operation success. Response to upload data command was transmitted.
  327. * @retval false Operation failure. Upload not supported by the implementation or not in correct
  328. * state.
  329. */
  330. bool antfs_upload_data_resp_transmit(bool data_upload_success);
  331. /**@brief Function for transmitting erase response to a erase request.
  332. *
  333. * @param[in] response The erase response code.
  334. */
  335. void antfs_erase_req_resp_transmit(uint8_t response);
  336. /**@brief Function for extracting possible pending ANT-FS event.
  337. *
  338. * @param[out] p_event The output event structure.
  339. *
  340. * @retval true Operation success. Pending ANT-FS event available and it was copied to the output
  341. * event structure.
  342. * @retval false Operation failure. No pending ANT-FS event available.
  343. */
  344. bool antfs_event_extract(antfs_event_return_t * const p_event);
  345. /**@brief Function for processing ANT events and data received from the ANT-FS channel.
  346. *
  347. * @param[in] p_message The message buffer containing the message received from the ANT-FS
  348. * channel.
  349. */
  350. void antfs_message_process(uint8_t * p_message);
  351. /**@brief Function for setting up the ANT-FS channel.
  352. */
  353. void antfs_channel_setup(void);
  354. #ifdef __cplusplus
  355. }
  356. #endif
  357. #endif // ANTFS_H__
  358. /**
  359. *@}
  360. **/