antfs.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  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. #include "antfs.h"
  8. #include <string.h>
  9. #include "defines.h"
  10. #include "app_error.h"
  11. #include "app_timer.h"
  12. #include "ant_error.h"
  13. #include "ant_parameters.h"
  14. #include "ant_interface.h"
  15. #include "ant_key_manager.h"
  16. #include "crc.h"
  17. #include "app_util.h"
  18. #ifdef LEDDRIVER_ACTIVE
  19. #include "bsp.h"
  20. #endif // LEDDRIVER_ACTIVE
  21. #define BURST_PACKET_SIZE 8u /**< The burst packet size. */
  22. #define ANTFS_CONNECTION_TYPE_OFFSET 0x00u /**< The connection type offset within ANT-FS message. */
  23. #define ANTFS_COMMAND_OFFSET 0x01u /**< The command offset within ANT-FS message. */
  24. #define ANTFS_RESPONSE_OFFSET 0x01u /**< The response offset within ANT-FS message. */
  25. #define ANTFS_CONTROL_OFFSET 0x02u /**< The control offset within ANT-FS message. */
  26. #define ANTFS_DATA_OFFSET 0x03u /**< The data offset within ANT-FS message. */
  27. #define ANTFS_BEACON_ID 0x43u /**< The ANT-FS beacon ID. */
  28. #define ANTFS_COMMAND_ID 0x44u /**< The ANT-FS command ID. */
  29. // Beacon definitions.
  30. #define STATUS1_OFFSET 0x01u /**< The beacon status1 field offset. */
  31. #define STATUS2_OFFSET 0x02u /**< The beacon status2 field offset. */
  32. #define DEVICE_STATE_SHIFT 0x00u /**< Shift value for device state bitfield. */
  33. #define DEVICE_STATE_MASK (0x0Fu << DEVICE_STATE_SHIFT) /**< Device state bitmask. */
  34. #define DEVICE_STATE_LINK (0x00u << DEVICE_STATE_SHIFT) /**< Device is in link state. */
  35. #define DEVICE_STATE_AUTHENTICATE (0x01u << DEVICE_STATE_SHIFT) /**< Device is in authenticate state. */
  36. #define DEVICE_STATE_TRANSPORT (0x02u << DEVICE_STATE_SHIFT) /**< Device is in transport state. */
  37. #define DEVICE_STATE_BUSY (0x03u << DEVICE_STATE_SHIFT) /**< Device is in busy state. */
  38. #define DEVICE_DESCRIPTOR_OFFSET_0 0x04u /**< Beacon ANT-FS device descriptor LSB position. */
  39. #define DEVICE_DESCRIPTOR_OFFSET_1 0x05u /**< Beacon ANT-FS device descriptor LSB + 1 position. */
  40. #define DEVICE_DESCRIPTOR_OFFSET_2 0x06u /**< Beacon ANT-FS device descriptor LSB + 2 position. */
  41. #define DEVICE_DESCRIPTOR_OFFSET_3 0x07u /**< Beacon ANT-FS device descriptor MSB position. */
  42. // Commands.
  43. #define ANTFS_CMD_NONE 0x00u /**< Used to identify that no ANT-FS command is in progress. */
  44. #define ANTFS_CMD_LINK_ID 0x02u /**< ANT-FS link command ID. */
  45. #define ANTFS_CMD_DISCONNECT_ID 0x03u /**< ANT-FS disconnect command ID. */
  46. #define ANTFS_CMD_AUTHENTICATE_ID 0x04u /**< ANT-FS authenticate command ID. */
  47. #define ANTFS_CMD_PING_ID 0x05u /**< ANT-FS ping command ID. */
  48. #define ANTFS_CMD_DOWNLOAD_ID 0x09u /**< ANT-FS download request command ID. */
  49. #define ANTFS_CMD_UPLOAD_REQUEST_ID 0x0Au /**< ANT-FS upload request command ID. */
  50. #define ANTFS_CMD_ERASE_ID 0x0Bu /**< ANT-FS erase request command ID. */
  51. #define ANTFS_CMD_UPLOAD_DATA_ID 0x0Cu /**< ANT-FS upload command ID. */
  52. // Responses.
  53. #define ANTFS_RSP_AUTHENTICATE_ID 0x84u /**< ANT-FS authenticate response command ID. */
  54. #define ANTFS_RSP_DOWNLOAD_ID 0x89u /**< ANT-FS download request response command ID. */
  55. #define ANTFS_RSP_UPLOAD_REQ_ID 0x8Au /**< ANT-FS upload request response command ID. */
  56. #define ANTFS_RSP_ERASE_ID 0x8Bu /**< ANT-FS erase response command ID. */
  57. #define ANTFS_RSP_UPLOAD_DATA_ID 0x8Cu /**< ANT-FS upload data response command ID. */
  58. // Link command.
  59. #define TRANSPORT_CHANNEL_FREQUENCY_OFFSET 0x02u /**< Channel frequency field offset within link command. */
  60. #define TRANSPORT_MESSAGE_PERIOD_OFFSET 0x03u /**< Channel period field offset within link command. */
  61. #define HOST_ID_OFFSET_0 0x04u /**< Host serial number period field LSB offset within link command. */
  62. #define HOST_ID_OFFSET_1 0x05u /**< Host serial number period field LSB + 1 offset within link command. */
  63. #define HOST_ID_OFFSET_2 0x06u /**< Host serial number period field LSB + 2 offset within link command. */
  64. #define HOST_ID_OFFSET_3 0x07u /**< Host serial number period field MSB offset within link command. */
  65. // Authenticate command.
  66. #define COMMAND_TYPE_OFFSET 0x02u /**< Command type field offset within authenticate command. */
  67. #define COMMAND_TYPE_PROCEED 0x00u /**< Command type proceed to transport in the authenticate command. */
  68. #define COMMAND_TYPE_REQUEST_SERIAL 0x01u /**< Command type request client device serial number in the authenticate command. */
  69. #define COMMAND_TYPE_REQUEST_PAIR 0x02u /**< Command type request pairing in the authenticate command. */
  70. #define COMMAND_TYPE_REQUEST_PASSKEY 0x03u /**< Command type request passkey exchange in the authenticate command. */
  71. // Authenticate response.
  72. #define RESPONSE_TYPE_OFFSET 0x02u /**< Command type field offset within authenticate response command. */
  73. #define AUTH_RESPONSE_N_A 0x00u /**< Command response type N/A (response for client serial number request). */
  74. #define AUTH_RESPONSE_ACCEPT 0x01u /**< Command response type accept. */
  75. #define AUTH_RESPONSE_REJECT 0x02u /**< Command response type reject. */
  76. // Authenticate command/response.
  77. #define AUTH_STRING_LENGTH_OFFSET 0x03u /**< Authenticate Command/Response authentication string length offset. */
  78. #define SERIAL_NUMBER_OFFSET_0 0x04u /**< Authenticate Command/Response client/host serial number LSB offset. */
  79. #define SERIAL_NUMBER_OFFSET_1 0x05u /**< Authenticate Command/Response client/host serial number LSB + 1 offset. */
  80. #define SERIAL_NUMBER_OFFSET_2 0x06u /**< Authenticate Command/Response client/host serial number LSB + 2 offset. */
  81. #define SERIAL_NUMBER_OFFSET_3 0x07u /**< Authenticate Command/Response client/host serial number MSB offset. */
  82. // Download/Upload/Erase commands.
  83. #define INITIAL_REQUEST_OFFSET 0x01u /**< Download/Upload/Erase command initial request command offset. */
  84. #define DATA_INDEX_OFFSET_LOW 0x02u /**< Download/Upload/Erase command offset index low. */
  85. #define DATA_INDEX_OFFSET_HIGH 0x03u /**< Download/Upload/Erase command offset index high. */
  86. #define ADDRESS_PARAMETER_OFFSET_0 0x04u /**< Download/Upload command parameter LSB offset. */
  87. #define ADDRESS_PARAMETER_OFFSET_1 0x05u /**< Download/Upload command parameter LSB + 1 offset. */
  88. #define ADDRESS_PARAMETER_OFFSET_2 0x06u /**< Download/Upload command parameter LSB + 2 offset. */
  89. #define ADDRESS_PARAMETER_OFFSET_3 0x07u /**< Download/Upload command parameter MSB offset. */
  90. #define UPLOAD_CRC_OFFSET_LOW 0x06u /**< Upload data CRC offset low. */
  91. #define UPLOAD_CRC_OFFSET_HIGH 0x07u /**< Upload data CRC offset high. */
  92. // Below compile switches should be provided by antfs_config.h:
  93. // - ANTFS_AUTH_TYPE_PAIRING /**< Use pairing and key exchange authentication. */
  94. // - ANTFS_AUTH_TYPE_PASSKEY /**< Use passkey authentication. */
  95. // - ANTFS_AUTH_TYPE_PASSTHROUGH /**< Allow host to bypass authentication. */
  96. // - ANTFS_INCLUDE_UPLOAD /**< Support upload operation. */
  97. // Authentication type. The highest level of authentication available is included in the beacon.
  98. #if defined (ANTFS_AUTH_TYPE_PASSKEY)
  99. #define AUTHENTICATION_TYPE COMMAND_TYPE_REQUEST_PASSKEY /**< Passkey and pairing only mode set as authentication type in beacon. */
  100. #elif defined (ANTFS_AUTH_TYPE_PAIRING)
  101. #define AUTHENTICATION_TYPE COMMAND_TYPE_REQUEST_PAIR /**< Pairing only mode set as authentication type in beacon. */
  102. #elif defined (ANTFS_AUTH_TYPE_PASSTHROUGH)
  103. #define AUTHENTICATION_TYPE COMMAND_TYPE_PROCEED /**< Pass-through mode set as authentication type in beacon. */
  104. #else
  105. #error "No valid auth type defined"
  106. #endif
  107. #define AUTHENTICATION_RETRIES 0x05u /**< Max number of retries for authentication responses */
  108. #define ANTFS_EVENT_QUEUE_SIZE 0x04u /**< ANT-FS event queue size. */
  109. #define SAVE_DISTANCE 256u /**< Save distance required because of nRF buffer to line up data offset on retry. */
  110. // Buffer Indices.
  111. #define BUFFER_INDEX_MESG_SIZE 0x00u /**< ANT message buffer index length offset. */
  112. #define BUFFER_INDEX_MESG_ID 0x01u /**< ANT message buffer index ID offset. */
  113. #define BUFFER_INDEX_CHANNEL_NUM 0x02u /**< ANT message buffer index channel number offset. */
  114. #define BUFFER_INDEX_MESG_DATA 0x03u /**< ANT message buffer index begin of data offset. */
  115. #define BUFFER_INDEX_RESPONSE_CODE 0x04u /**< ANT message buffer index response code offset. */
  116. typedef struct
  117. {
  118. char friendly_name[ANTFS_FRIENDLY_NAME_MAX]; /**< Friendly Name. */
  119. bool is_name_set; /**< Is the name set. */
  120. uint32_t index; /**< Current index (for reading the friendly name). */
  121. uint32_t friendly_name_size; /**< Friendly name size. */
  122. } friendly_name_t;
  123. typedef union
  124. {
  125. antfs_link_substate_t link_sub_state; /**< Sub-state (Link layer). */
  126. antfs_authenticate_substate_t auth_sub_state; /**< Sub-state (Authentication layer). */
  127. antfs_transport_substate_t trans_sub_state; /**< Sub-state (Transport layer). */
  128. } antfs_substate_t;
  129. typedef struct
  130. {
  131. antfs_state_t state; /**< ANT-FS state. */
  132. antfs_substate_t sub_state; /**< ANT-FS sub-state. */
  133. } antfs_states_t;
  134. typedef struct
  135. {
  136. antfs_event_return_t * p_queue; /**< ANT-FS event queue. */
  137. uint32_t head; /**< ANT-FS event queue head index. */
  138. uint32_t tail; /**< ANT-FS event queue tail index. */
  139. } antfs_event_queue_t;
  140. static antfs_params_t m_initial_parameters; /**< Initial parameters. */
  141. static antfs_beacon_status_byte1_t m_active_beacon_status1_field; /**< Status 1 field in beacon. */
  142. static uint32_t m_active_beacon_frequency; /**< Active beacon frequency. */
  143. static antfs_states_t m_current_state; /**< Current state. */
  144. static friendly_name_t m_friendly_name; /**< Host's friendly name. */
  145. static ulong_union_t m_link_host_serial_number; /**< Host's serial number. */
  146. static uint32_t m_link_command_in_progress; /**< ANT-FS command in progress. */
  147. static uint32_t m_authenticate_command_type; /**< Authenticate command type in progress. */
  148. static volatile uint8_t m_burst_wait; /**< Polling status flag for data unlock on burst handler input. */
  149. static uint8_t m_retry; /**< Retry counter */
  150. APP_TIMER_DEF(m_timer_id); /**< Timer ID used with the timer module. */
  151. #if defined (ANTFS_AUTH_TYPE_PASSKEY)
  152. static uint32_t m_passkey_index; /**< Current location of Tx block (auth string). */
  153. #endif // ANTFS_AUTH_TYPE_PASSKEY
  154. // Download/upload.
  155. static bool m_is_data_request_pending; /**< Requested data pending. */
  156. static bool m_is_crc_pending; /**< CRC for data packets pending. */
  157. static ushort_union_t m_file_index; /**< File index of current upload/download. */
  158. static ulong_union_t m_file_size; /**< File size of current upload/download (bytes). */
  159. static ulong_union_t m_max_block_size; /**< Maximum number of bytes expected to be downloaded in a single burst block of data (set by host). */
  160. static ulong_union_t m_link_burst_index; /**< Current location of Tx block (bytes). */
  161. static ulong_union_t m_bytes_remaining; /**< Total remaining data length (bytes). */
  162. static ulong_union_t m_max_transfer_index; /**< Upper limit of the current Tx burst block (bytes). */
  163. static uint32_t m_bytes_to_write; /**< Number of bytes to write to file (upload). */
  164. static const uint8_t * mp_upload_data; /**< Address of begin of the buffer that holds data received from upload. */
  165. #ifdef ANTFS_INCLUDE_UPLOAD
  166. static ulong_union_t m_block_size; /**< Number of bytes the client can receive in a single burst. */
  167. #endif // ANTFS_INCLUDE_UPLOAD
  168. // CRC verification.
  169. static uint32_t m_saved_crc_offset; /**< CRC data offset (bytes) saved at last CRC update (save point). */
  170. static uint32_t m_saved_buffer_crc_offset; /**< Data offset to track how much data has been buffered into nRF */
  171. static uint32_t m_temp_crc_offset; /**< Temporary CRC data offset used in CRC calculation. */
  172. static uint16_t m_compared_crc; /**< 16-bit CRC for all data packets in the block (provided by download request). */
  173. static uint16_t m_transfer_crc; /**< 16-bit CRC for all data packets in the block (calculated by client). */
  174. static uint16_t m_saved_transfer_crc; /**< 16-bit CRC saved at last CRC update (save point). */
  175. static uint16_t m_saved_buffer_crc; /**< 16-bit CRC saved at last CRC update (save point) for buffering the nRF */
  176. // ANT-FS event handling.
  177. static antfs_event_return_t m_event_queue_buffer[ANTFS_EVENT_QUEUE_SIZE]; /**< Event queue storage. */
  178. static antfs_event_queue_t m_event_queue; /**< Event queue. */
  179. static antfs_burst_wait_handler_t m_burst_wait_handler = NULL; /**< Burst wait handler */
  180. const char * antfs_hostname_get(void)
  181. {
  182. if (m_friendly_name.is_name_set)
  183. {
  184. return (m_friendly_name.friendly_name);
  185. }
  186. return NULL;
  187. }
  188. /**@brief Function for waiting for the burst transmission request to complete.
  189. */
  190. static void wait_burst_request_to_complete(void)
  191. {
  192. while (m_burst_wait != 0)
  193. {
  194. if(m_burst_wait_handler != NULL)
  195. {
  196. m_burst_wait_handler();
  197. }
  198. };
  199. }
  200. /**@brief Function for stopping ANT-FS timeout, which is possibly currently running.
  201. */
  202. static void timeout_disable(void)
  203. {
  204. m_link_command_in_progress = ANTFS_CMD_NONE;
  205. const uint32_t err_code = app_timer_stop(m_timer_id);
  206. APP_ERROR_CHECK(err_code);
  207. }
  208. /**@brief Function for transmitting a beacon.
  209. *
  210. * Transmits a beacon either using a broadcast or burst transmission mode.
  211. *
  212. * @param[in] message_type Defines the used transmission mode.
  213. */
  214. static void beacon_transmit(uint32_t message_type)
  215. {
  216. uint8_t beacon_status_byte2;
  217. // Set beacon status byte 2.
  218. if (m_link_command_in_progress == ANTFS_CMD_NONE)
  219. {
  220. switch (m_current_state.state)
  221. {
  222. case ANTFS_STATE_AUTH:
  223. beacon_status_byte2 = DEVICE_STATE_AUTHENTICATE;
  224. break;
  225. case ANTFS_STATE_TRANS:
  226. beacon_status_byte2 = DEVICE_STATE_TRANSPORT;
  227. break;
  228. default:
  229. beacon_status_byte2 = DEVICE_STATE_LINK;
  230. break;
  231. }
  232. }
  233. else
  234. {
  235. beacon_status_byte2 = DEVICE_STATE_BUSY;
  236. }
  237. // Set remaining beacon fields.
  238. uint8_t tx_buffer[BURST_PACKET_SIZE];
  239. tx_buffer[0] = ANTFS_BEACON_ID;
  240. tx_buffer[1] = m_active_beacon_status1_field.status;
  241. tx_buffer[2] = beacon_status_byte2;
  242. tx_buffer[3] = AUTHENTICATION_TYPE;
  243. if ((m_current_state.state == ANTFS_STATE_AUTH) ||
  244. (m_current_state.state == ANTFS_STATE_TRANS))
  245. {
  246. tx_buffer[4] = m_link_host_serial_number.bytes.byte0;
  247. tx_buffer[5] = m_link_host_serial_number.bytes.byte1;
  248. tx_buffer[6] = m_link_host_serial_number.bytes.byte2;
  249. tx_buffer[7] = m_link_host_serial_number.bytes.byte3;
  250. }
  251. else
  252. {
  253. tx_buffer[4] = m_initial_parameters.beacon_device_type;
  254. tx_buffer[5] = (m_initial_parameters.beacon_device_type >> 8u);
  255. tx_buffer[6] = m_initial_parameters.beacon_device_manufacturing_id;
  256. tx_buffer[7] = (m_initial_parameters.beacon_device_manufacturing_id >> 8u);
  257. }
  258. if (message_type == MESG_BROADCAST_DATA_ID)
  259. {
  260. if (sd_ant_broadcast_message_tx(ANTFS_CHANNEL, sizeof(tx_buffer), tx_buffer) != NRF_SUCCESS)
  261. {
  262. // @note: No implementation needed, as it is a valid and accepted use case for this call
  263. // to fail. This call can fail if we are in middle of bursting.
  264. }
  265. }
  266. else if(message_type == MESG_BURST_DATA_ID)
  267. {
  268. // Send as the first packet of a burst.
  269. const uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  270. sizeof(tx_buffer),
  271. tx_buffer,
  272. BURST_SEGMENT_START);
  273. APP_ERROR_CHECK(err_code);
  274. wait_burst_request_to_complete();
  275. // This is the first packet of a burst response, disable command timeout while bursting.
  276. timeout_disable();
  277. }
  278. else
  279. {
  280. // This should not happen.
  281. APP_ERROR_HANDLER(message_type);
  282. }
  283. }
  284. /**@brief Function for transmitting a authenticate response message.
  285. *
  286. * @param[in] response_type Authenticate response code.
  287. * @param[in] password_length Length of authentication string.
  288. * @param[in] p_password Authentication string transmitted.
  289. */
  290. static void authenticate_response_transmit(uint8_t response_type,
  291. uint32_t password_length,
  292. const uint8_t * p_password)
  293. {
  294. ulong_union_t serial_number;
  295. serial_number.data = m_initial_parameters.client_serial_number;
  296. // First packet is beacon.
  297. beacon_transmit(MESG_BURST_DATA_ID);
  298. uint8_t tx_buffer[BURST_PACKET_SIZE];
  299. tx_buffer[ANTFS_CONNECTION_TYPE_OFFSET] = ANTFS_COMMAND_ID;
  300. tx_buffer[ANTFS_RESPONSE_OFFSET] = ANTFS_RSP_AUTHENTICATE_ID;
  301. tx_buffer[RESPONSE_TYPE_OFFSET] = response_type;
  302. tx_buffer[AUTH_STRING_LENGTH_OFFSET] = password_length;
  303. tx_buffer[SERIAL_NUMBER_OFFSET_0] = serial_number.bytes.byte0;
  304. tx_buffer[SERIAL_NUMBER_OFFSET_1] = serial_number.bytes.byte1;
  305. tx_buffer[SERIAL_NUMBER_OFFSET_2] = serial_number.bytes.byte2;
  306. tx_buffer[SERIAL_NUMBER_OFFSET_3] = serial_number.bytes.byte3;
  307. uint32_t err_code;
  308. if ((m_current_state.state == ANTFS_STATE_AUTH) &&
  309. (
  310. (response_type != AUTH_RESPONSE_REJECT) &&
  311. password_length &&
  312. (password_length <= ANTFS_AUTH_STRING_MAX)
  313. )
  314. )
  315. {
  316. // Send second packet (auth response).
  317. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  318. sizeof(tx_buffer),
  319. tx_buffer,
  320. BURST_SEGMENT_CONTINUE);
  321. APP_ERROR_CHECK(err_code);
  322. wait_burst_request_to_complete();
  323. // Round size to a multiple of 8 bytes.
  324. uint8_t tx_buffer_authenticate[ANTFS_AUTH_STRING_MAX + 1u];
  325. memset(tx_buffer_authenticate, 0, ANTFS_AUTH_STRING_MAX + 1u);
  326. memcpy(tx_buffer_authenticate, p_password, password_length);
  327. // Round up total number bytes to a multiple of 8 to be sent to burst handler.
  328. if (password_length & (BURST_PACKET_SIZE - 1u))
  329. {
  330. password_length &= ~(BURST_PACKET_SIZE - 1u);
  331. password_length += BURST_PACKET_SIZE;
  332. }
  333. // Send auth string (last packets of the burst).
  334. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  335. password_length,
  336. tx_buffer_authenticate,
  337. BURST_SEGMENT_END);
  338. APP_ERROR_CHECK(err_code);
  339. wait_burst_request_to_complete();
  340. m_link_command_in_progress = ANTFS_RSP_AUTHENTICATE_ID;
  341. }
  342. else
  343. {
  344. // If the authorization is rejected or there is no valid password, the auth response is the
  345. // last packet.
  346. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  347. sizeof(tx_buffer),
  348. tx_buffer,
  349. BURST_SEGMENT_END);
  350. APP_ERROR_CHECK(err_code);
  351. wait_burst_request_to_complete();
  352. }
  353. // Switch to appropiate state.
  354. if (response_type == AUTH_RESPONSE_REJECT)
  355. {
  356. m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_REJECT;
  357. }
  358. else if (response_type == AUTH_RESPONSE_ACCEPT)
  359. {
  360. m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_ACCEPT;
  361. }
  362. else
  363. {
  364. // No implementation needed.
  365. }
  366. }
  367. bool antfs_pairing_resp_transmit(bool accept)
  368. {
  369. #if defined(ANTFS_AUTH_TYPE_PAIRING)
  370. // This function should only be called when ANT-FS is in PAIRING mode.
  371. if ((m_current_state.state != ANTFS_STATE_AUTH) ||
  372. (m_current_state.sub_state.auth_sub_state != ANTFS_AUTH_SUBSTATE_PAIR))
  373. {
  374. return false;
  375. }
  376. m_link_command_in_progress = ANTFS_CMD_AUTHENTICATE_ID;
  377. if (accept)
  378. {
  379. // Accept request and send passkey if authentication passed.
  380. authenticate_response_transmit(AUTH_RESPONSE_ACCEPT,
  381. ANTFS_PASSKEY_SIZE,
  382. m_initial_parameters.p_pass_key);
  383. }
  384. else
  385. {
  386. // Reject authentication request.
  387. authenticate_response_transmit(AUTH_RESPONSE_REJECT, 0, NULL);
  388. }
  389. return true;
  390. #else
  391. return false;
  392. #endif // ANTFS_AUTH_TYPE_PAIRING
  393. }
  394. /**@brief Function for adding an ANT-FS event to the event queue.
  395. *
  396. * @param[in] event_code The event to be added.
  397. */
  398. static void event_queue_write(antfs_event_t event_code)
  399. {
  400. antfs_event_return_t * p_event = NULL;
  401. #ifdef LEDDRIVER_ACTIVE
  402. uint32_t err_code;
  403. #endif
  404. // Check if there is room in the queue for a new event.
  405. if (((m_event_queue.head + 1u) & (ANTFS_EVENT_QUEUE_SIZE - 1u)) != m_event_queue.tail)
  406. {
  407. p_event = &(m_event_queue.p_queue[m_event_queue.head]);
  408. }
  409. if (p_event != NULL)
  410. {
  411. // Initialize event parameters.
  412. p_event->event = event_code;
  413. // Set parameters depending on event type.
  414. switch (event_code)
  415. {
  416. case ANTFS_EVENT_ERASE_REQUEST:
  417. p_event->file_index = m_file_index.data;
  418. p_event->offset = 0;
  419. p_event->bytes = 0;
  420. p_event->crc = 0;
  421. break;
  422. case ANTFS_EVENT_DOWNLOAD_REQUEST:
  423. p_event->file_index = m_file_index.data;
  424. // Requested offset for the download.
  425. p_event->offset = m_link_burst_index.data;
  426. p_event->bytes = 0;
  427. p_event->crc = 0;
  428. break;
  429. case ANTFS_EVENT_DOWNLOAD_REQUEST_DATA:
  430. p_event->file_index = m_file_index.data;
  431. // Current offset.
  432. p_event->offset = m_link_burst_index.data;
  433. if (m_bytes_remaining.data > (ANTFS_BURST_BLOCK_SIZE * BURST_PACKET_SIZE))
  434. {
  435. // If remaining bytes > burst block size then grab one block at a time.
  436. p_event->bytes = ANTFS_BURST_BLOCK_SIZE * BURST_PACKET_SIZE;
  437. }
  438. else
  439. {
  440. p_event->bytes = m_bytes_remaining.data;
  441. }
  442. p_event->crc = 0;
  443. break;
  444. case ANTFS_EVENT_UPLOAD_REQUEST:
  445. p_event->file_index = m_file_index.data;
  446. // Requested offset for the upload.
  447. p_event->offset = m_link_burst_index.data;
  448. // Upper limit of the download (offset + remaining bytes).
  449. p_event->bytes = m_max_transfer_index.data;
  450. // CRC Seed (from last save point if resuming).
  451. p_event->crc = m_transfer_crc;
  452. break;
  453. case ANTFS_EVENT_UPLOAD_DATA:
  454. p_event->file_index = m_file_index.data;
  455. // Current offset.
  456. p_event->offset = m_link_burst_index.data;
  457. // Current CRC.
  458. p_event->crc = m_transfer_crc;
  459. // Number of bytes to write.
  460. p_event->bytes = m_bytes_to_write;
  461. // Upload to appication data buffer.
  462. memcpy(p_event->data, mp_upload_data, m_bytes_to_write);
  463. break;
  464. case ANTFS_EVENT_PAIRING_REQUEST:
  465. #ifdef LEDDRIVER_ACTIVE
  466. err_code = bsp_indication_set(BSP_INDICATE_BONDING);
  467. APP_ERROR_CHECK(err_code);
  468. #endif // LEDDRIVER_ACTIVE
  469. break;
  470. default:
  471. // No parameters need to be set.
  472. p_event->file_index = 0;
  473. p_event->offset = 0;
  474. p_event->bytes = 0;
  475. p_event->crc = 0;
  476. break;
  477. }
  478. // Put the event in the queue.
  479. m_event_queue.head = ((m_event_queue.head + 1u) & (ANTFS_EVENT_QUEUE_SIZE - 1u));
  480. }
  481. else
  482. {
  483. // No free space left in the queue.
  484. APP_ERROR_HANDLER(0);
  485. }
  486. }
  487. /**@brief Function for transmitting download request response message.
  488. *
  489. * @param[in] response Download response code.
  490. */
  491. static void download_request_response_transmit(uint8_t response)
  492. {
  493. // First burst packet is beacon.
  494. beacon_transmit(MESG_BURST_DATA_ID);
  495. uint8_t tx_buffer[BURST_PACKET_SIZE];
  496. // Next send the first part of the download response.
  497. tx_buffer[0] = ANTFS_COMMAND_ID;
  498. tx_buffer[1] = ANTFS_RSP_DOWNLOAD_ID;
  499. tx_buffer[2] = response;
  500. tx_buffer[3] = 0;
  501. // Total number of bytes remaining in the data block.
  502. tx_buffer[4] = m_bytes_remaining.bytes.byte0;
  503. tx_buffer[5] = m_bytes_remaining.bytes.byte1;
  504. tx_buffer[6] = m_bytes_remaining.bytes.byte2;
  505. tx_buffer[7] = m_bytes_remaining.bytes.byte3;
  506. uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  507. sizeof(tx_buffer),
  508. tx_buffer,
  509. BURST_SEGMENT_CONTINUE);
  510. APP_ERROR_CHECK(err_code);
  511. wait_burst_request_to_complete();
  512. // Second part of the download response.
  513. // The offset the data will start from in this block.
  514. tx_buffer[0] = m_link_burst_index.bytes.byte0;
  515. tx_buffer[1] = m_link_burst_index.bytes.byte1;
  516. tx_buffer[2] = m_link_burst_index.bytes.byte2;
  517. tx_buffer[3] = m_link_burst_index.bytes.byte3;
  518. // The file size in the client device.
  519. tx_buffer[4] = m_file_size.bytes.byte0;
  520. tx_buffer[5] = m_file_size.bytes.byte1;
  521. tx_buffer[6] = m_file_size.bytes.byte2;
  522. tx_buffer[7] = m_file_size.bytes.byte3;
  523. if (response || (m_bytes_remaining.data == 0))
  524. {
  525. // If the download was rejected or there is no data to send.
  526. // Set response to end since we're not downloading any data.
  527. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  528. sizeof(tx_buffer),
  529. tx_buffer,
  530. BURST_SEGMENT_END);
  531. APP_ERROR_CHECK(err_code);
  532. wait_burst_request_to_complete();
  533. }
  534. else
  535. {
  536. // Response will continue (data packets + CRC footer to follow).
  537. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  538. sizeof(tx_buffer),
  539. tx_buffer,
  540. BURST_SEGMENT_CONTINUE);
  541. APP_ERROR_CHECK(err_code);
  542. wait_burst_request_to_complete();
  543. }
  544. m_link_command_in_progress = ANTFS_CMD_DOWNLOAD_ID;
  545. if (response == 0)
  546. {
  547. // If we are going to download (response OK), enter the downloading substate.
  548. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_DOWNLOADING;
  549. event_queue_write(ANTFS_EVENT_DOWNLOAD_START);
  550. }
  551. else
  552. {
  553. // Download rejected.
  554. event_queue_write(ANTFS_EVENT_DOWNLOAD_FAIL);
  555. }
  556. }
  557. void antfs_download_req_resp_prepare(uint8_t response,
  558. const antfs_request_info_t * const p_request_info)
  559. {
  560. // This function should only be called after receiving a download request.
  561. APP_ERROR_CHECK_BOOL((m_current_state.state == ANTFS_STATE_TRANS) &&
  562. (m_link_command_in_progress == ANTFS_CMD_DOWNLOAD_ID));
  563. if (response == 0)
  564. {
  565. // Download request OK.
  566. // File size of the requested download.
  567. m_file_size.data = p_request_info->file_size.data;
  568. if (m_link_burst_index.data > m_file_size.data)
  569. {
  570. // Offset should not exceed file size.
  571. m_link_burst_index.data = m_file_size.data;
  572. }
  573. // If the host is not limiting download size or the file size does not exceed the host's
  574. // download size limit.
  575. if ((m_max_block_size.data == 0) || (m_file_size.data < m_max_block_size.data))
  576. {
  577. // Number of bytes remaining to be downloaded in this block is the file size.
  578. m_bytes_remaining.data = m_file_size.data;
  579. }
  580. if ((m_file_size.data - m_link_burst_index.data) < m_bytes_remaining.data)
  581. {
  582. // Calculate number of remaining bytes in this block based on the offset.
  583. m_bytes_remaining.data = m_file_size.data - m_link_burst_index.data;
  584. }
  585. // If the application is limiting the Tx block size.
  586. if (m_bytes_remaining.data > p_request_info->max_burst_block_size.data)
  587. {
  588. // Number of remaining bytes in this block is the application defined block size.
  589. m_bytes_remaining.data = p_request_info->max_burst_block_size.data;
  590. }
  591. // Find upper limit of the burst Tx.
  592. m_max_transfer_index.data = m_link_burst_index.data + m_bytes_remaining.data;
  593. if (m_saved_crc_offset == ANTFS_MAX_FILE_SIZE)
  594. {
  595. // CRC checking was set as invalid. An invalid download was requested, so reject it.
  596. response = RESPONSE_INVALID_OPERATION;
  597. }
  598. }
  599. if ((response != 0) || (m_file_size.data == 0))
  600. {
  601. // Send the response right away if the download request was rejected or there is no data to
  602. // send.
  603. download_request_response_transmit(response);
  604. }
  605. else
  606. {
  607. // Proceed to download data.
  608. if (m_link_burst_index.data != m_saved_crc_offset)
  609. {
  610. uint32_t temp;
  611. // If requesting to resume exactly where we left off, we can start from the same block.
  612. if (m_link_burst_index.data == m_temp_crc_offset)
  613. {
  614. // Move last save point to end of last block sent.
  615. m_saved_crc_offset = m_link_burst_index.data;
  616. m_saved_transfer_crc = m_transfer_crc;
  617. }
  618. // To resume the download, request a block of data starting from the last save point.
  619. // Update the remaining number of bytes per the last save point.
  620. m_bytes_remaining.data += (m_link_burst_index.data - m_saved_crc_offset);
  621. // Swap the current burst Tx index with the saved CRC index, to make sure we do not
  622. // start updating the CRC until we get to the requested index.
  623. temp = m_link_burst_index.data;
  624. m_link_burst_index.data = m_saved_crc_offset;
  625. m_saved_crc_offset = temp;
  626. // Set CRC to previous save point, to check the CRC provided by the host.
  627. m_transfer_crc = m_saved_transfer_crc;
  628. }
  629. m_temp_crc_offset = m_saved_crc_offset;
  630. m_is_data_request_pending = true;
  631. // Request data from application.
  632. event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST_DATA);
  633. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_VERIFY_CRC;
  634. }
  635. }
  636. uint32_t antfs_input_data_download(uint16_t index,
  637. uint32_t offset,
  638. uint32_t num_bytes,
  639. const uint8_t * const p_message)
  640. {
  641. // Verify that this is the requested data.
  642. APP_ERROR_CHECK_BOOL((offset == m_link_burst_index.data) && (index == m_file_index.data));
  643. // If file offset is greater than the upper limit, this is not data we need.
  644. APP_ERROR_CHECK_BOOL(offset <= m_max_transfer_index.data);
  645. if ((m_current_state.state == ANTFS_STATE_TRANS) &&
  646. (
  647. (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_VERIFY_CRC) ||
  648. // Only send data if we were processing a download request.
  649. (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_DOWNLOADING)
  650. )
  651. )
  652. {
  653. uint32_t block_offset = 0;
  654. if (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_VERIFY_CRC)
  655. {
  656. // Make sure download_request_response_transmit defaults to RESPONSE_INVALID_CRC.
  657. uint32_t response = RESPONSE_MESSAGE_OK;
  658. // Check CRC.
  659. if (m_link_burst_index.data == m_saved_crc_offset)
  660. {
  661. // If indexes match, we can compare CRC directly.
  662. if (m_transfer_crc != m_compared_crc)
  663. {
  664. response = RESPONSE_INVALID_CRC;
  665. }
  666. else
  667. {
  668. // Set up the save point
  669. m_temp_crc_offset = m_link_burst_index.data; // Reset save point counter
  670. m_saved_crc_offset = m_link_burst_index.data;
  671. m_saved_buffer_crc_offset = m_link_burst_index.data;
  672. // Set up the CRC save points
  673. m_saved_transfer_crc = m_compared_crc;
  674. m_saved_buffer_crc = m_compared_crc;
  675. m_is_crc_pending = true;
  676. }
  677. // Start bursting beacon and the download response (3 burst packets).
  678. download_request_response_transmit(response);
  679. }
  680. // If the data is in this block, advance to the requested offset
  681. else if ((m_link_burst_index.data < m_saved_crc_offset) &&
  682. ((m_saved_crc_offset - m_link_burst_index.data) < num_bytes))
  683. {
  684. // Update the offset within this block for the requested transmission.
  685. block_offset = m_saved_crc_offset - m_link_burst_index.data;
  686. // Update the number of bytes that will actually be transmitted.
  687. num_bytes -= block_offset;
  688. // Update CRC calculation up to requested index.
  689. m_transfer_crc = crc_crc16_update(m_transfer_crc, p_message, block_offset);
  690. // Update the remaining number of bytes.
  691. m_bytes_remaining.data -= block_offset;
  692. // Check CRC
  693. if (m_transfer_crc != m_compared_crc)
  694. {
  695. response = RESPONSE_INVALID_CRC;
  696. }
  697. else
  698. {
  699. // Move index back to point where transmission will resume.
  700. m_link_burst_index.data = m_saved_crc_offset;
  701. // Set up the save point
  702. m_temp_crc_offset = m_link_burst_index.data; // Reset save point counter
  703. m_saved_buffer_crc_offset = m_link_burst_index.data;
  704. // Set up the CRC save points
  705. m_saved_transfer_crc = m_compared_crc;
  706. m_saved_buffer_crc = m_compared_crc;
  707. m_is_crc_pending = true;
  708. }
  709. download_request_response_transmit(response);
  710. }
  711. // Data index has gone too far and it is not possible to check CRC, fail and let host retry
  712. else if(m_link_burst_index.data > m_saved_crc_offset)
  713. {
  714. response = RESPONSE_INVALID_CRC;
  715. download_request_response_transmit(response);
  716. }
  717. // Keep getting data and recalculate the CRC until the indexes match
  718. else
  719. {
  720. m_is_data_request_pending = false;
  721. // Update the current burst index and bytes remaining
  722. m_link_burst_index.data += num_bytes;
  723. m_bytes_remaining.data -= num_bytes;
  724. // Update CRC
  725. m_transfer_crc = crc_crc16_update(m_transfer_crc, p_message, num_bytes);
  726. // Request more data.
  727. event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST_DATA);
  728. }
  729. }
  730. // Append data.
  731. if (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_DOWNLOADING)
  732. {
  733. uint32_t num_of_bytes_to_burst = num_bytes;
  734. if (num_of_bytes_to_burst & (BURST_PACKET_SIZE - 1u))
  735. {
  736. // Round up total number bytes to a multiple of BURST_PACKET_SIZE to be sent to
  737. // burst handler.
  738. num_of_bytes_to_burst &= ~(BURST_PACKET_SIZE - 1u);
  739. num_of_bytes_to_burst += BURST_PACKET_SIZE;
  740. }
  741. uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  742. num_of_bytes_to_burst,
  743. (uint8_t*)&(p_message[block_offset]),
  744. BURST_SEGMENT_CONTINUE);
  745. if(err_code != NRF_ANT_ERROR_TRANSFER_SEQUENCE_NUMBER_ERROR)
  746. {
  747. // If burst failed before we are able to catch it, we will get a TRANSFER_SEQUENCE_NUMBER_ERROR
  748. // The message processing will send client back to correct state
  749. APP_ERROR_CHECK(err_code);
  750. }
  751. wait_burst_request_to_complete();
  752. // Update current burst index.
  753. m_link_burst_index.data += num_bytes;
  754. // Update remaining bytes.
  755. m_bytes_remaining.data -= num_bytes;
  756. m_is_data_request_pending = false;
  757. m_transfer_crc = crc_crc16_update(m_transfer_crc,
  758. &(p_message[block_offset]),
  759. num_bytes);
  760. if((m_link_burst_index.data - m_temp_crc_offset) > SAVE_DISTANCE)
  761. {
  762. // Set CRC save point
  763. m_saved_transfer_crc = m_saved_buffer_crc; // Set CRC at buffer save point (will always be one behind to account for buffering)
  764. m_saved_buffer_crc = m_transfer_crc; // Set CRC at save point
  765. // Set offset save point
  766. m_saved_crc_offset = m_saved_buffer_crc_offset; // Set offset at buffer save point (will always be one behind to account for buffering)
  767. m_saved_buffer_crc_offset = m_link_burst_index.data; // Set buffer offset to current data offset
  768. // Reset save counter offset
  769. m_temp_crc_offset = m_link_burst_index.data; // Set to current location; next save point will take place after SAVE_DISTANCE bytes
  770. }
  771. if (!m_is_data_request_pending && (m_link_burst_index.data < m_max_transfer_index.data))
  772. {
  773. // If we have not finished the download.
  774. // Request more data.
  775. event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST_DATA);
  776. m_is_data_request_pending = true;
  777. }
  778. else if (m_link_burst_index.data >= m_max_transfer_index.data && m_is_crc_pending)
  779. {
  780. // We are done, send CRC footer.
  781. uint8_t tx_buffer[BURST_PACKET_SIZE];
  782. tx_buffer[0] = 0;
  783. tx_buffer[1] = 0;
  784. tx_buffer[2] = 0;
  785. tx_buffer[3] = 0;
  786. tx_buffer[4] = 0;
  787. tx_buffer[5] = 0;
  788. tx_buffer[6] = (uint8_t)m_transfer_crc;
  789. tx_buffer[7] = (uint8_t)(m_transfer_crc >> 8u);
  790. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  791. sizeof(tx_buffer),
  792. tx_buffer,
  793. BURST_SEGMENT_END);
  794. if(err_code != NRF_ANT_ERROR_TRANSFER_SEQUENCE_NUMBER_ERROR)
  795. {
  796. // If burst failed before we are able to catch it, we will get a TRANSFER_SEQUENCE_NUMBER_ERROR
  797. // The message processing will send client back to correct state
  798. APP_ERROR_CHECK(err_code);
  799. }
  800. wait_burst_request_to_complete();
  801. m_is_crc_pending = false;
  802. m_max_transfer_index.data = 0;
  803. }
  804. // Return the number of bytes we accepted.
  805. return num_bytes;
  806. }
  807. }
  808. // No bytes were accepted.
  809. return 0;
  810. }
  811. bool antfs_upload_req_resp_transmit(uint8_t response,
  812. const antfs_request_info_t * const p_request_info)
  813. {
  814. #if defined(ANTFS_INCLUDE_UPLOAD)
  815. if (m_current_state.state != ANTFS_STATE_TRANS ||
  816. // Only send the response if we were processing an upload request.
  817. (m_link_command_in_progress != ANTFS_CMD_UPLOAD_REQUEST_ID))
  818. {
  819. return false;
  820. }
  821. // If the application is sending a response for a different file than requested, the upload
  822. // will fail.
  823. if (p_request_info->file_index.data != m_file_index.data)
  824. {
  825. event_queue_write(ANTFS_EVENT_UPLOAD_FAIL);
  826. return false;
  827. }
  828. ulong_union_t max_mem_size;
  829. // Set maximum number of bytes that can be written to the file.
  830. max_mem_size.data = p_request_info->max_file_size;
  831. if (p_request_info->max_burst_block_size.data != 0)
  832. {
  833. // If the client is limiting the block size set the block size requested by the client.
  834. m_block_size.data = p_request_info->max_burst_block_size.data;
  835. }
  836. else
  837. {
  838. // Try to get the entire file in a single block.
  839. m_block_size.data = max_mem_size.data;
  840. }
  841. if (response == 0)
  842. {
  843. if (m_max_transfer_index.data > max_mem_size.data)
  844. {
  845. // Not enough space to write file, reject download.
  846. response = RESPONSE_MESSAGE_NOT_ENOUGH_SPACE;
  847. }
  848. }
  849. // Get last valid CRC and last valid offset.
  850. m_transfer_crc = p_request_info->file_crc;
  851. m_link_burst_index.data = p_request_info->file_size.data;
  852. // First packet to transmit is the beacon.
  853. beacon_transmit(MESG_BURST_DATA_ID);
  854. // Second packet.
  855. uint8_t tx_buffer[BURST_PACKET_SIZE];
  856. tx_buffer[0] = ANTFS_COMMAND_ID;
  857. tx_buffer[1] = ANTFS_RSP_UPLOAD_REQ_ID;
  858. tx_buffer[2] = response;
  859. tx_buffer[3] = 0;
  860. // Last valid data offset written to the file.
  861. tx_buffer[4] = m_link_burst_index.bytes.byte0;
  862. tx_buffer[5] = m_link_burst_index.bytes.byte1;
  863. tx_buffer[6] = m_link_burst_index.bytes.byte2;
  864. tx_buffer[7] = m_link_burst_index.bytes.byte3;
  865. uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  866. sizeof(tx_buffer),
  867. tx_buffer,
  868. BURST_SEGMENT_CONTINUE);
  869. APP_ERROR_CHECK(err_code);
  870. wait_burst_request_to_complete();
  871. // Third packet.
  872. // Maximum number of bytes that can be written to the file.
  873. tx_buffer[0] = max_mem_size.bytes.byte0;
  874. tx_buffer[1] = max_mem_size.bytes.byte1;
  875. tx_buffer[2] = max_mem_size.bytes.byte2;
  876. tx_buffer[3] = max_mem_size.bytes.byte3;
  877. // Maximum upload block size.
  878. tx_buffer[4] = m_block_size.bytes.byte0;
  879. tx_buffer[5] = m_block_size.bytes.byte1;
  880. tx_buffer[6] = m_block_size.bytes.byte2;
  881. tx_buffer[7] = m_block_size.bytes.byte3;
  882. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  883. sizeof(tx_buffer),
  884. tx_buffer,
  885. BURST_SEGMENT_CONTINUE);
  886. APP_ERROR_CHECK(err_code);
  887. wait_burst_request_to_complete();
  888. // Fourth packet.
  889. tx_buffer[0] = 0;
  890. tx_buffer[1] = 0;
  891. tx_buffer[2] = 0;
  892. tx_buffer[3] = 0;
  893. tx_buffer[4] = 0;
  894. tx_buffer[5] = 0;
  895. // Value of CRC at last data offset.
  896. tx_buffer[6] = (uint8_t) m_transfer_crc;
  897. tx_buffer[7] = (uint8_t)(m_transfer_crc >> 8);
  898. err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  899. sizeof(tx_buffer),
  900. tx_buffer,
  901. BURST_SEGMENT_END);
  902. APP_ERROR_CHECK(err_code);
  903. wait_burst_request_to_complete();
  904. m_link_command_in_progress = ANTFS_CMD_UPLOAD_REQUEST_ID;
  905. if (response != 0)
  906. {
  907. // Failed upload request. Reset max transfer index to 0 (do not accept any data if the host
  908. // sends it anyway).
  909. m_max_transfer_index.data = 0;
  910. }
  911. else
  912. {
  913. // Wait for upload data request.
  914. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA;
  915. }
  916. return true;
  917. #else
  918. return false;
  919. #endif // ANTFS_INCLUDE_UPLOAD
  920. }
  921. bool antfs_upload_data_resp_transmit(bool data_upload_success)
  922. {
  923. #if defined(ANTFS_INCLUDE_UPLOAD)
  924. // Should be in TRANSPORT layer to send this response.
  925. if (m_current_state.state != ANTFS_STATE_TRANS)
  926. {
  927. return false;
  928. }
  929. uint8_t tx_buffer[BURST_PACKET_SIZE];
  930. // Response.
  931. tx_buffer[0] = ANTFS_COMMAND_ID;
  932. tx_buffer[1] = ANTFS_RSP_UPLOAD_DATA_ID;
  933. tx_buffer[2] = (data_upload_success) ? RESPONSE_MESSAGE_OK : RESPONSE_MESSAGE_FAIL;
  934. tx_buffer[3] = 0;
  935. tx_buffer[4] = 0;
  936. tx_buffer[5] = 0;
  937. tx_buffer[6] = 0;
  938. tx_buffer[7] = 0;
  939. // First packet is beacon.
  940. beacon_transmit(MESG_BURST_DATA_ID);
  941. // Send last packet.
  942. uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  943. sizeof(tx_buffer),
  944. tx_buffer,
  945. BURST_SEGMENT_END);
  946. APP_ERROR_CHECK(err_code);
  947. wait_burst_request_to_complete();
  948. m_link_command_in_progress = ANTFS_CMD_UPLOAD_REQUEST_ID;
  949. // Reset maximum index.
  950. m_max_transfer_index.data = 0;
  951. return true;
  952. #else
  953. return false;
  954. #endif // ANTFS_INCLUDE_UPLOAD
  955. }
  956. void antfs_erase_req_resp_transmit(uint8_t response)
  957. {
  958. // This function should only be called after receiving an erase request.
  959. APP_ERROR_CHECK_BOOL((m_current_state.state == ANTFS_STATE_TRANS) &&
  960. (m_link_command_in_progress == ANTFS_CMD_ERASE_ID));
  961. beacon_transmit(MESG_BURST_DATA_ID);
  962. uint8_t tx_buffer[BURST_PACKET_SIZE];
  963. // Erase response.
  964. tx_buffer[0] = ANTFS_COMMAND_ID;
  965. tx_buffer[1] = ANTFS_RSP_ERASE_ID;
  966. tx_buffer[2] = response;
  967. tx_buffer[3] = 0;
  968. tx_buffer[4] = 0;
  969. tx_buffer[5] = 0;
  970. tx_buffer[6] = 0;
  971. tx_buffer[7] = 0;
  972. uint32_t err_code = sd_ant_burst_handler_request(ANTFS_CHANNEL,
  973. sizeof(tx_buffer),
  974. tx_buffer,
  975. BURST_SEGMENT_END);
  976. APP_ERROR_CHECK(err_code);
  977. wait_burst_request_to_complete();
  978. }
  979. bool antfs_event_extract(antfs_event_return_t * const p_event)
  980. {
  981. bool return_value = false;
  982. if (m_event_queue.head != m_event_queue.tail)
  983. {
  984. // Pending events exist. Copy event parameters into return event.
  985. p_event->event = m_event_queue.p_queue[m_event_queue.tail].event;
  986. p_event->file_index = m_event_queue.p_queue[m_event_queue.tail].file_index;
  987. p_event->offset = m_event_queue.p_queue[m_event_queue.tail].offset;
  988. p_event->bytes = m_event_queue.p_queue[m_event_queue.tail].bytes;
  989. p_event->crc = m_event_queue.p_queue[m_event_queue.tail].crc;
  990. memcpy(p_event->data,
  991. m_event_queue.p_queue[m_event_queue.tail].data,
  992. sizeof(p_event->data));
  993. // Release the event queue.
  994. m_event_queue.tail = ((m_event_queue.tail + 1u) & (ANTFS_EVENT_QUEUE_SIZE - 1u));
  995. return_value = true;
  996. }
  997. return return_value;
  998. }
  999. /**@brief Function for setting the channel period.
  1000. *
  1001. * Sets the channel period. The only allowed frequencies are 0.5, 1, 2, 4 and 8 Hz.
  1002. *
  1003. * @param[in] link_period Link period for the beacon transmission.
  1004. */
  1005. static void channel_period_set(uint32_t link_period)
  1006. {
  1007. uint32_t period;
  1008. switch (link_period)
  1009. {
  1010. default:
  1011. // Shouldn't happen, but just in case default to 0,5Hz.
  1012. case BEACON_PERIOD_0_5_HZ:
  1013. period = 65535u;
  1014. break;
  1015. case BEACON_PERIOD_1_HZ:
  1016. period = 32768u;
  1017. break;
  1018. case BEACON_PERIOD_2_HZ:
  1019. period = 16384u;
  1020. break;
  1021. case BEACON_PERIOD_4_HZ:
  1022. period = 8192u;
  1023. break;
  1024. case BEACON_PERIOD_8_HZ:
  1025. period = 4096u;
  1026. break;
  1027. }
  1028. const uint32_t err_code = sd_ant_channel_period_set(ANTFS_CHANNEL, period);
  1029. APP_ERROR_CHECK(err_code);
  1030. }
  1031. /**@brief Function for starting ANT-FS timeout.
  1032. *
  1033. * @param[in] timeout_in_secs Timeout requested in unit of seconds.
  1034. */
  1035. static void timeout_start(uint32_t timeout_in_secs)
  1036. {
  1037. uint32_t err_code = app_timer_stop(m_timer_id);
  1038. APP_ERROR_CHECK(err_code);
  1039. err_code = app_timer_start(m_timer_id,
  1040. APP_TIMER_TICKS((uint32_t)(timeout_in_secs * 1000u),
  1041. APP_TIMER_PRESCALER),
  1042. NULL);
  1043. APP_ERROR_CHECK(err_code);
  1044. }
  1045. /**@brief Function for switching to authentication layer.
  1046. */
  1047. static void authenticate_layer_transit(void)
  1048. {
  1049. if (m_current_state.state != ANTFS_STATE_OFF)
  1050. {
  1051. m_current_state.state = ANTFS_STATE_AUTH;
  1052. m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_NONE;
  1053. m_link_command_in_progress = ANTFS_CMD_NONE;
  1054. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1055. uint32_t err_code = sd_ant_channel_radio_freq_set(ANTFS_CHANNEL, m_active_beacon_frequency);
  1056. APP_ERROR_CHECK(err_code);
  1057. event_queue_write(ANTFS_EVENT_AUTH);
  1058. }
  1059. }
  1060. /**@brief Function for decoding an ANT-FS command received at the link layer.
  1061. *
  1062. * @param[in] p_command_buffer The ANT-FS command buffer.
  1063. */
  1064. static void link_layer_cmd_decode(const uint8_t * p_command_buffer)
  1065. {
  1066. if (p_command_buffer[ANTFS_CONNECTION_TYPE_OFFSET] != ANTFS_COMMAND_ID)
  1067. {
  1068. return;
  1069. }
  1070. switch (p_command_buffer[ANTFS_COMMAND_OFFSET])
  1071. {
  1072. case ANTFS_CMD_LINK_ID:
  1073. // Channel frequency.
  1074. m_active_beacon_frequency =
  1075. p_command_buffer[TRANSPORT_CHANNEL_FREQUENCY_OFFSET];
  1076. // Channel message period.
  1077. m_active_beacon_status1_field.parameters.link_period =
  1078. p_command_buffer[TRANSPORT_MESSAGE_PERIOD_OFFSET];
  1079. // Host serial Number.
  1080. m_link_host_serial_number.bytes.byte0 =
  1081. p_command_buffer[HOST_ID_OFFSET_0];
  1082. m_link_host_serial_number.bytes.byte1 =
  1083. p_command_buffer[HOST_ID_OFFSET_1];
  1084. m_link_host_serial_number.bytes.byte2 =
  1085. p_command_buffer[HOST_ID_OFFSET_2];
  1086. m_link_host_serial_number.bytes.byte3 =
  1087. p_command_buffer[HOST_ID_OFFSET_3];
  1088. // Move to the channel period issued by the host.
  1089. channel_period_set(m_active_beacon_status1_field.parameters.link_period);
  1090. authenticate_layer_transit();
  1091. break;
  1092. default:
  1093. break;
  1094. }
  1095. }
  1096. /**@brief Function for switching to link layer.
  1097. */
  1098. static void link_layer_transit(void)
  1099. {
  1100. if (m_current_state.state != ANTFS_STATE_OFF)
  1101. {
  1102. uint32_t err_code;
  1103. #ifdef LEDDRIVER_ACTIVE
  1104. err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  1105. APP_ERROR_CHECK(err_code);
  1106. #endif // LEDDRIVER_ACTIVE
  1107. m_current_state.state = ANTFS_STATE_LINK;
  1108. m_current_state.sub_state.link_sub_state = ANTFS_LINK_SUBSTATE_NONE;
  1109. m_link_command_in_progress = ANTFS_CMD_NONE;
  1110. m_active_beacon_status1_field = m_initial_parameters.beacon_status_byte1;
  1111. m_active_beacon_frequency = m_initial_parameters.beacon_frequency;
  1112. timeout_disable();
  1113. err_code = sd_ant_channel_radio_freq_set(ANTFS_CHANNEL, m_active_beacon_frequency);
  1114. APP_ERROR_CHECK(err_code);
  1115. event_queue_write(ANTFS_EVENT_LINK);
  1116. }
  1117. }
  1118. /**@brief Function for decoding an ANT-FS command received at the authenticate layer.
  1119. *
  1120. * @param[in] control_byte The command control byte.
  1121. * @param[in] p_command_buffer The ANT-FS command buffer.
  1122. */
  1123. static void authenticate_layer_cmd_decode(uint8_t control_byte,
  1124. const uint8_t * p_command_buffer)
  1125. {
  1126. // @note: Response variable must have a static storage allocation as it keeps track of the
  1127. // passkey authentication progress between multiple burst packets.
  1128. #if defined (ANTFS_AUTH_TYPE_PASSKEY)
  1129. static uint32_t response;
  1130. #endif // ANTFS_AUTH_TYPE_PASSKEY
  1131. if((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0 && m_link_command_in_progress != ANTFS_CMD_NONE)
  1132. {
  1133. // This is something new, and we're busy processing something already, so don't respond
  1134. return;
  1135. }
  1136. if (p_command_buffer[ANTFS_CONNECTION_TYPE_OFFSET] == ANTFS_COMMAND_ID &&
  1137. m_link_command_in_progress == ANTFS_CMD_NONE)
  1138. {
  1139. if (p_command_buffer[ANTFS_COMMAND_OFFSET] == ANTFS_CMD_AUTHENTICATE_ID)
  1140. {
  1141. // Make sure it is the correct host
  1142. if(m_link_host_serial_number.bytes.byte0 != p_command_buffer[HOST_ID_OFFSET_0] ||
  1143. m_link_host_serial_number.bytes.byte1 != p_command_buffer[HOST_ID_OFFSET_1] ||
  1144. m_link_host_serial_number.bytes.byte2 != p_command_buffer[HOST_ID_OFFSET_2] ||
  1145. m_link_host_serial_number.bytes.byte3 != p_command_buffer[HOST_ID_OFFSET_3])
  1146. return;
  1147. m_link_command_in_progress = ANTFS_CMD_AUTHENTICATE_ID;
  1148. m_authenticate_command_type = p_command_buffer[COMMAND_TYPE_OFFSET];
  1149. m_retry = AUTHENTICATION_RETRIES;
  1150. }
  1151. }
  1152. if (m_link_command_in_progress == ANTFS_CMD_AUTHENTICATE_ID)
  1153. {
  1154. switch (m_authenticate_command_type)
  1155. {
  1156. case COMMAND_TYPE_REQUEST_SERIAL:
  1157. if (control_byte & SEQUENCE_LAST_MESSAGE)
  1158. {
  1159. // Don't do anything before the burst completes (last burst message received).
  1160. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1161. authenticate_response_transmit(AUTH_RESPONSE_N_A,
  1162. ANTFS_REMOTE_FRIENDLY_NAME_MAX,
  1163. // Send device friendly name if it exists.
  1164. m_initial_parameters.p_remote_friendly_name);
  1165. }
  1166. break;
  1167. #if defined (ANTFS_AUTH_TYPE_PASSTHROUGH)
  1168. case COMMAND_TYPE_PROCEED:
  1169. if (control_byte & SEQUENCE_LAST_MESSAGE)
  1170. {
  1171. // Don't do anything before the burst completes (last burst message received).
  1172. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1173. // Proceed directly to transport layer (no authentication required).
  1174. authenticate_response_transmit(AUTH_RESPONSE_ACCEPT, 0, NULL);
  1175. }
  1176. break;
  1177. #endif // ANTFS_AUTH_TYPE_PASSTHROUGH
  1178. #if defined (ANTFS_AUTH_TYPE_PAIRING)
  1179. case COMMAND_TYPE_REQUEST_PAIR:
  1180. if ((control_byte & SEQUENCE_NUMBER_ROLLOVER) == 0)
  1181. {
  1182. // First burst packet.
  1183. // Friendly name length.
  1184. m_friendly_name.friendly_name_size =
  1185. p_command_buffer[AUTH_STRING_LENGTH_OFFSET];
  1186. if (m_friendly_name.friendly_name_size > 0)
  1187. {
  1188. if (m_friendly_name.friendly_name_size > ANTFS_FRIENDLY_NAME_MAX)
  1189. {
  1190. m_friendly_name.friendly_name_size = ANTFS_FRIENDLY_NAME_MAX;
  1191. }
  1192. m_friendly_name.index = 0;
  1193. }
  1194. }
  1195. else
  1196. {
  1197. // Next burst packets: read host friendly name.
  1198. if (m_friendly_name.index < ANTFS_FRIENDLY_NAME_MAX)
  1199. {
  1200. uint32_t num_of_bytes = ANTFS_FRIENDLY_NAME_MAX - m_friendly_name.index;
  1201. if (num_of_bytes > 8u)
  1202. {
  1203. num_of_bytes = 8u;
  1204. }
  1205. memcpy((uint8_t*)&m_friendly_name.friendly_name[m_friendly_name.index],
  1206. p_command_buffer,
  1207. num_of_bytes);
  1208. m_friendly_name.index += num_of_bytes;
  1209. }
  1210. }
  1211. if (control_byte & SEQUENCE_LAST_MESSAGE)
  1212. {
  1213. // Last burst packet.
  1214. timeout_start(ANTFS_PAIRING_TIMEOUT);
  1215. if (m_friendly_name.friendly_name_size > 0)
  1216. {
  1217. m_friendly_name.is_name_set = true;
  1218. }
  1219. m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_PAIR;
  1220. // If pairing is supported, send request to UI.
  1221. event_queue_write(ANTFS_EVENT_PAIRING_REQUEST);
  1222. }
  1223. break;
  1224. #endif // ANTFS_AUTH_TYPE_PAIRING
  1225. #if defined (ANTFS_AUTH_TYPE_PASSKEY)
  1226. case COMMAND_TYPE_REQUEST_PASSKEY:
  1227. if ((control_byte & SEQUENCE_NUMBER_ROLLOVER) == 0)
  1228. {
  1229. // First burst packet.
  1230. // Passkey length.
  1231. const uint32_t passkey_size = p_command_buffer[AUTH_STRING_LENGTH_OFFSET];
  1232. // Default the algorithm to accept.
  1233. response = AUTH_RESPONSE_ACCEPT;
  1234. // Check if the passkey length is valid.
  1235. if (passkey_size == ANTFS_PASSKEY_SIZE)
  1236. {
  1237. m_passkey_index = 0;
  1238. }
  1239. else
  1240. {
  1241. // Invalid lenght supplied - the authentication will be rejected.
  1242. response = AUTH_RESPONSE_REJECT;
  1243. }
  1244. }
  1245. else
  1246. {
  1247. // Next burst packets: read host friendly name.
  1248. if ((response == AUTH_RESPONSE_ACCEPT) &&
  1249. // Prevent buffer overrun.
  1250. (m_passkey_index != ANTFS_PASSKEY_SIZE))
  1251. {
  1252. // Passkey length was valid and the host supplied key matches so far.
  1253. uint32_t idx = 0;
  1254. // Check the current received burst packet for passkey match.
  1255. do
  1256. {
  1257. if (m_initial_parameters.p_pass_key[m_passkey_index++] !=
  1258. p_command_buffer[idx])
  1259. {
  1260. // Reject the authentication request and further processing of
  1261. // passkey matching if a mismatch is found.
  1262. response = AUTH_RESPONSE_REJECT;
  1263. break;
  1264. }
  1265. ++idx;
  1266. }
  1267. while (idx < BURST_PACKET_SIZE);
  1268. }
  1269. }
  1270. if (control_byte & SEQUENCE_LAST_MESSAGE)
  1271. {
  1272. // Last burst packet.
  1273. if (m_passkey_index < ANTFS_PASSKEY_SIZE)
  1274. {
  1275. // We did not get the complete passkey, reject authentication request.
  1276. response = AUTH_RESPONSE_REJECT;
  1277. }
  1278. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1279. m_current_state.sub_state.auth_sub_state = ANTFS_AUTH_SUBSTATE_PASSKEY;
  1280. authenticate_response_transmit(response, 0, NULL);
  1281. }
  1282. break;
  1283. #endif // ANTFS_AUTH_TYPE_PASSKEY
  1284. default:
  1285. break;
  1286. }
  1287. }
  1288. else if (p_command_buffer[ANTFS_COMMAND_OFFSET] == ANTFS_CMD_DISCONNECT_ID)
  1289. {
  1290. if (control_byte & SEQUENCE_LAST_MESSAGE)
  1291. {
  1292. // Don't do anything before the burst completes (last burst message received).
  1293. link_layer_transit();
  1294. }
  1295. }
  1296. else if (p_command_buffer[ANTFS_COMMAND_OFFSET] == ANTFS_CMD_PING_ID)
  1297. {
  1298. // Reset timeout.
  1299. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1300. m_link_command_in_progress = ANTFS_CMD_NONE;
  1301. }
  1302. else
  1303. {
  1304. // No implementation needed.
  1305. }
  1306. }
  1307. /**@brief Function for decoding an ANT-FS command received at the transport layer.
  1308. *
  1309. * @param[in] control_byte The command control byte.
  1310. * @param[in] p_command_buffer The ANT-FS command buffer.
  1311. */
  1312. static void transport_layer_cmd_decode(uint8_t control_byte, const uint8_t * p_command_buffer)
  1313. {
  1314. ulong_union_t host_serial_number = {0};
  1315. if (p_command_buffer[ANTFS_CONNECTION_TYPE_OFFSET] == ANTFS_COMMAND_ID)
  1316. {
  1317. m_link_command_in_progress = p_command_buffer[ANTFS_COMMAND_OFFSET];
  1318. }
  1319. switch (m_link_command_in_progress)
  1320. {
  1321. case ANTFS_CMD_PING_ID:
  1322. // Reset timeout.
  1323. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1324. m_link_command_in_progress = ANTFS_CMD_NONE;
  1325. break;
  1326. case ANTFS_CMD_DISCONNECT_ID:
  1327. if(control_byte & SEQUENCE_LAST_MESSAGE)
  1328. {
  1329. // Don't do anything before the burst completes (last burst message received).
  1330. link_layer_transit();
  1331. }
  1332. break;
  1333. case ANTFS_CMD_ERASE_ID:
  1334. if(control_byte & SEQUENCE_LAST_MESSAGE)
  1335. {
  1336. // Don't do anything before the burst completes (last burst message received).
  1337. // Requested index.
  1338. m_file_index.bytes.low = p_command_buffer[DATA_INDEX_OFFSET_LOW];
  1339. m_file_index.bytes.high = p_command_buffer[DATA_INDEX_OFFSET_HIGH];
  1340. // Send erase request to the application.
  1341. event_queue_write(ANTFS_EVENT_ERASE_REQUEST);
  1342. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1343. m_link_command_in_progress = ANTFS_CMD_ERASE_ID;
  1344. }
  1345. break;
  1346. case ANTFS_CMD_DOWNLOAD_ID:
  1347. if (m_current_state.sub_state.trans_sub_state != ANTFS_TRANS_SUBSTATE_NONE)
  1348. {
  1349. // Ignore the command if we are busy.
  1350. break;
  1351. }
  1352. if ((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0x00)
  1353. {
  1354. // First burst packet.
  1355. if ((m_file_index.bytes.low != p_command_buffer[DATA_INDEX_OFFSET_LOW]) ||
  1356. (m_file_index.bytes.high != p_command_buffer[DATA_INDEX_OFFSET_HIGH]))
  1357. {
  1358. // This is a new index, so we can not check the CRC against the previous saved
  1359. // CRC.
  1360. // CRC seed checking is made invalid by setting the last saved offset to the
  1361. // maximum file size.
  1362. m_saved_crc_offset = ANTFS_MAX_FILE_SIZE;
  1363. }
  1364. // Requested data file index.
  1365. m_file_index.bytes.low = p_command_buffer[DATA_INDEX_OFFSET_LOW];
  1366. m_file_index.bytes.high = p_command_buffer[DATA_INDEX_OFFSET_HIGH];
  1367. // Initialize current position in the TX burst to the requested offset.
  1368. m_link_burst_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
  1369. m_link_burst_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
  1370. m_link_burst_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
  1371. m_link_burst_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];
  1372. }
  1373. else if (control_byte & SEQUENCE_LAST_MESSAGE)
  1374. {
  1375. // Last burst packet (download command should be two packets long).
  1376. // Get CRC seed from host.
  1377. m_compared_crc = (uint16_t)p_command_buffer[DATA_INDEX_OFFSET_LOW];
  1378. m_compared_crc |= ((uint16_t)p_command_buffer[DATA_INDEX_OFFSET_HIGH] << 8u);
  1379. // Maximum block size allowed by host.
  1380. m_max_block_size.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
  1381. m_max_block_size.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
  1382. m_max_block_size.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
  1383. m_max_block_size.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];
  1384. // Initialize number of remaining bytes for this block to the maximum block size.
  1385. m_bytes_remaining.data = m_max_block_size.data;
  1386. if (p_command_buffer[INITIAL_REQUEST_OFFSET])
  1387. {
  1388. // This request is the start of a new transfer.
  1389. // Initialize data offset for CRC calculation to the requested data offset.
  1390. m_saved_crc_offset = m_link_burst_index.data;
  1391. m_saved_buffer_crc_offset = m_link_burst_index.data;
  1392. // Use CRC seed provided by host for CRC checking of the data.
  1393. m_transfer_crc = m_compared_crc;
  1394. m_saved_transfer_crc = m_compared_crc;
  1395. m_saved_buffer_crc = m_compared_crc;
  1396. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_VERIFY_CRC;
  1397. }
  1398. else
  1399. {
  1400. // This is a request to resume a partially completed transfer.
  1401. if (m_saved_crc_offset > m_link_burst_index.data)
  1402. {
  1403. // We can not check the received CRC seed as the requested offset is before
  1404. // our last save point.
  1405. // Set CRC checking as invalid.
  1406. m_saved_crc_offset = ANTFS_MAX_FILE_SIZE;
  1407. }
  1408. else
  1409. {
  1410. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_VERIFY_CRC;
  1411. }
  1412. }
  1413. m_is_data_request_pending = false;
  1414. // Send download request to the application for further handling.
  1415. event_queue_write(ANTFS_EVENT_DOWNLOAD_REQUEST);
  1416. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1417. m_link_command_in_progress = ANTFS_CMD_DOWNLOAD_ID;
  1418. }
  1419. break;
  1420. case ANTFS_CMD_UPLOAD_REQUEST_ID:
  1421. #if defined(ANTFS_INCLUDE_UPLOAD)
  1422. if ((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0x00)
  1423. {
  1424. // First burst packet.
  1425. if ((m_file_index.bytes.low != p_command_buffer[DATA_INDEX_OFFSET_LOW]) ||
  1426. (
  1427. (m_file_index.bytes.high != p_command_buffer[DATA_INDEX_OFFSET_HIGH]) ||
  1428. (m_current_state.sub_state.trans_sub_state == ANTFS_TRANS_SUBSTATE_NONE)
  1429. )
  1430. )
  1431. {
  1432. // If it is a new index or we completed the last upload.
  1433. // Get the file index.
  1434. m_file_index.bytes.low = p_command_buffer[DATA_INDEX_OFFSET_LOW];
  1435. m_file_index.bytes.high = p_command_buffer[DATA_INDEX_OFFSET_HIGH];
  1436. // As this is a new upload, reset save point to the beginning of the file.
  1437. // Set CRC to zero.
  1438. m_saved_crc_offset = 0;
  1439. m_saved_transfer_crc = 0;
  1440. }
  1441. // Get the upper limit of upload from request message.
  1442. m_max_transfer_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
  1443. m_max_transfer_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
  1444. m_max_transfer_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
  1445. m_max_transfer_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];
  1446. }
  1447. else if (control_byte & SEQUENCE_LAST_MESSAGE)
  1448. {
  1449. // Last burst (second) packet.
  1450. // Get data offset the requested upload will start at.
  1451. m_link_burst_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
  1452. m_link_burst_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
  1453. m_link_burst_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
  1454. m_link_burst_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];
  1455. if (m_link_burst_index.data != ANTFS_MAX_FILE_SIZE)
  1456. {
  1457. // If this is a new upload.
  1458. // The data offset specified in the upload request will be used.
  1459. m_saved_crc_offset = m_link_burst_index.data;
  1460. m_saved_transfer_crc = 0;
  1461. }
  1462. m_transfer_crc = m_saved_transfer_crc;
  1463. // Send upload request to the application for further handling.
  1464. event_queue_write(ANTFS_EVENT_UPLOAD_REQUEST);
  1465. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1466. m_link_command_in_progress = ANTFS_CMD_UPLOAD_REQUEST_ID;
  1467. }
  1468. #endif // ANTFS_INCLUDE_UPLOAD
  1469. break;
  1470. case ANTFS_CMD_UPLOAD_DATA_ID:
  1471. #if defined(ANTFS_INCLUDE_UPLOAD)
  1472. if ((control_byte & ~SEQUENCE_LAST_MESSAGE) == 0x00)
  1473. {
  1474. // First burst packet.
  1475. if (m_current_state.sub_state.trans_sub_state ==
  1476. ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA)
  1477. {
  1478. antfs_event_t event;
  1479. // Get CRC Seed from host.
  1480. m_compared_crc = (uint16_t)p_command_buffer[DATA_INDEX_OFFSET_LOW];
  1481. m_compared_crc |= ((uint16_t)p_command_buffer[DATA_INDEX_OFFSET_HIGH] << 8u);
  1482. // Set download offset.
  1483. m_link_burst_index.bytes.byte0 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_0];
  1484. m_link_burst_index.bytes.byte1 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_1];
  1485. m_link_burst_index.bytes.byte2 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_2];
  1486. m_link_burst_index.bytes.byte3 = p_command_buffer[ADDRESS_PARAMETER_OFFSET_3];
  1487. if ((m_link_burst_index.data + m_block_size.data) < m_max_transfer_index.data)
  1488. {
  1489. // Adjust block size as set by client.
  1490. m_max_transfer_index.data = m_link_burst_index.data + m_block_size.data;
  1491. }
  1492. if (m_compared_crc != m_transfer_crc)
  1493. {
  1494. // Check that the request matches the CRC sent on the upload response.
  1495. // Do not accept any data.
  1496. m_max_transfer_index.data = 0;
  1497. // Failure will be reported when upload is done.
  1498. event = (antfs_event_t)0;
  1499. }
  1500. // Set ready to receive a file.
  1501. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_UPLOADING;
  1502. event = ANTFS_EVENT_UPLOAD_START;
  1503. m_transfer_crc = m_compared_crc;
  1504. if (m_link_burst_index.data > m_max_transfer_index.data)
  1505. {
  1506. // If the requested offset is too high.
  1507. // Clear the max transfer index, so we'll report fail when the transfer
  1508. // finishes.
  1509. m_max_transfer_index.data = 0;
  1510. // Clear the event because we normally would not send an event at this point
  1511. // in this case.
  1512. event = (antfs_event_t)0;
  1513. }
  1514. if (control_byte & SEQUENCE_LAST_MESSAGE)
  1515. {
  1516. // If this upload contains no data.
  1517. // Leave the upload state.
  1518. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_NONE;
  1519. // if it was a valid index, report it as a successful upload, otherwise
  1520. // report it as a failure.
  1521. if (event == 0)
  1522. {
  1523. event = ANTFS_EVENT_UPLOAD_FAIL;
  1524. }
  1525. else
  1526. {
  1527. event = ANTFS_EVENT_UPLOAD_COMPLETE;
  1528. }
  1529. }
  1530. if (event != 0)
  1531. {
  1532. event_queue_write(event);
  1533. }
  1534. }
  1535. }
  1536. #endif // ANTFS_INCLUDE_UPLOAD
  1537. break;
  1538. case ANTFS_CMD_LINK_ID:
  1539. host_serial_number.bytes.byte0 = p_command_buffer[HOST_ID_OFFSET_0];
  1540. host_serial_number.bytes.byte1 = p_command_buffer[HOST_ID_OFFSET_1];
  1541. host_serial_number.bytes.byte2 = p_command_buffer[HOST_ID_OFFSET_2];
  1542. host_serial_number.bytes.byte3 = p_command_buffer[HOST_ID_OFFSET_3];
  1543. if (m_link_host_serial_number.data == host_serial_number.data)
  1544. {
  1545. m_active_beacon_frequency = p_command_buffer[TRANSPORT_CHANNEL_FREQUENCY_OFFSET];
  1546. m_active_beacon_status1_field.parameters.link_period =
  1547. p_command_buffer[TRANSPORT_MESSAGE_PERIOD_OFFSET];
  1548. const uint32_t err_code = sd_ant_channel_radio_freq_set(ANTFS_CHANNEL,
  1549. m_active_beacon_frequency);
  1550. APP_ERROR_CHECK(err_code);
  1551. channel_period_set(m_active_beacon_status1_field.parameters.link_period);
  1552. }
  1553. m_link_command_in_progress = 0;
  1554. break;
  1555. default:
  1556. // Don't do anything, this is an invalid message.
  1557. m_link_command_in_progress = 0;
  1558. break;
  1559. }
  1560. }
  1561. /**@brief Function for handling data upload.
  1562. *
  1563. * @param[in] control_byte The command control byte.
  1564. * @param[in] p_buffer The data buffer.
  1565. */
  1566. static void upload_data_process(uint8_t control_byte, const uint8_t * p_buffer)
  1567. {
  1568. #if defined(ANTFS_INCLUDE_UPLOAD)
  1569. if (control_byte & SEQUENCE_LAST_MESSAGE)
  1570. {
  1571. // Last burst packet: upload complete.
  1572. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_NONE;
  1573. // CRC for data packets contained in this upload block.
  1574. m_compared_crc = p_buffer[UPLOAD_CRC_OFFSET_LOW ];
  1575. m_compared_crc |= (p_buffer[UPLOAD_CRC_OFFSET_HIGH] << 8u);
  1576. if (m_max_transfer_index.data && (m_compared_crc == m_transfer_crc))
  1577. {
  1578. // CRC OK, upload was completed successfully.
  1579. event_queue_write(ANTFS_EVENT_UPLOAD_COMPLETE);
  1580. }
  1581. else
  1582. {
  1583. // CRC mismatch, upload failed.
  1584. event_queue_write(ANTFS_EVENT_UPLOAD_FAIL);
  1585. }
  1586. m_max_transfer_index.data = 0;
  1587. }
  1588. else
  1589. {
  1590. // Not the last burst packet: upload not complete.
  1591. // Set initial number of bytes to 8 (size of burst packet).
  1592. m_bytes_to_write = BURST_PACKET_SIZE;
  1593. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1594. if (m_link_burst_index.data > m_max_transfer_index.data)
  1595. {
  1596. // We are past the main index, we do not need to write any more data.
  1597. m_bytes_to_write = 0;
  1598. }
  1599. else
  1600. {
  1601. if ((m_bytes_to_write + m_link_burst_index.data) > m_max_transfer_index.data)
  1602. {
  1603. // if we're less than 8 bytes away from the end, adjust the number of bytes to write
  1604. // in this block.
  1605. m_bytes_to_write = m_max_transfer_index.data - m_link_burst_index.data;
  1606. }
  1607. }
  1608. if (m_bytes_to_write != 0)
  1609. {
  1610. APP_ERROR_CHECK_BOOL(m_bytes_to_write <= BURST_PACKET_SIZE);
  1611. // Store begin of upload data.
  1612. mp_upload_data = p_buffer;
  1613. m_transfer_crc = crc_crc16_update(m_transfer_crc, p_buffer, m_bytes_to_write);
  1614. // Send data to application.
  1615. event_queue_write(ANTFS_EVENT_UPLOAD_DATA);
  1616. // Update current offset.
  1617. m_link_burst_index.data += m_bytes_to_write;
  1618. // Store save point.
  1619. m_saved_crc_offset = m_link_burst_index.data;
  1620. m_saved_transfer_crc = m_transfer_crc;
  1621. }
  1622. }
  1623. #endif // ANTFS_INCLUDE_UPLOAD
  1624. }
  1625. /**@brief Function for switching to transport layer.
  1626. */
  1627. static void transport_layer_transit(void)
  1628. {
  1629. if (m_current_state.state != ANTFS_STATE_OFF)
  1630. {
  1631. #ifdef LEDDRIVER_ACTIVE
  1632. uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  1633. APP_ERROR_CHECK(err_code);
  1634. #endif // LEDDRIVER_ACTIVE
  1635. m_current_state.state = ANTFS_STATE_TRANS;
  1636. m_current_state.sub_state.trans_sub_state = ANTFS_TRANS_SUBSTATE_NONE;
  1637. timeout_start(ANTFS_LINK_COMMAND_TIMEOUT);
  1638. beacon_transmit(MESG_BROADCAST_DATA_ID);
  1639. event_queue_write(ANTFS_EVENT_TRANS);
  1640. }
  1641. }
  1642. void antfs_message_process(uint8_t * p_message)
  1643. {
  1644. #ifdef LEDDRIVER_ACTIVE
  1645. uint32_t err_code;
  1646. #endif
  1647. if (p_message != NULL)
  1648. {
  1649. if ((p_message[BUFFER_INDEX_CHANNEL_NUM] & CHANNEL_NUMBER_MASK) != ANTFS_CHANNEL)
  1650. {
  1651. // Only process messages corresponding to the ANT-FS channel here.
  1652. return;
  1653. }
  1654. if ((m_current_state.state == ANTFS_STATE_OFF) &&
  1655. (
  1656. !(
  1657. (p_message[BUFFER_INDEX_MESG_ID] == MESG_RESPONSE_EVENT_ID) &&
  1658. (p_message[BUFFER_INDEX_RESPONSE_CODE] == NO_EVENT)
  1659. )
  1660. )
  1661. )
  1662. {
  1663. return;
  1664. }
  1665. switch (p_message[BUFFER_INDEX_MESG_ID])
  1666. {
  1667. case MESG_BROADCAST_DATA_ID:
  1668. // We are not going to process broadcast messages or pass them to the app to handle.
  1669. break;
  1670. case MESG_ACKNOWLEDGED_DATA_ID:
  1671. // Mark it as being the last message if it's an ack message.
  1672. p_message[ANTFS_CONTROL_OFFSET] |= SEQUENCE_LAST_MESSAGE;
  1673. /* fall-through */
  1674. case MESG_BURST_DATA_ID:
  1675. switch (m_current_state.state)
  1676. {
  1677. case ANTFS_STATE_LINK:
  1678. link_layer_cmd_decode(&p_message[ANTFS_DATA_OFFSET]);
  1679. break;
  1680. case ANTFS_STATE_AUTH:
  1681. authenticate_layer_cmd_decode(p_message[ANTFS_CONTROL_OFFSET],
  1682. &p_message[ANTFS_DATA_OFFSET]);
  1683. break;
  1684. case ANTFS_STATE_TRANS:
  1685. if (m_current_state.sub_state.trans_sub_state !=
  1686. ANTFS_TRANS_SUBSTATE_UPLOADING)
  1687. {
  1688. transport_layer_cmd_decode(p_message[ANTFS_CONTROL_OFFSET],
  1689. &p_message[ANTFS_DATA_OFFSET]);
  1690. }
  1691. else
  1692. {
  1693. upload_data_process(p_message[ANTFS_CONTROL_OFFSET],
  1694. &p_message[ANTFS_DATA_OFFSET]);
  1695. }
  1696. break;
  1697. default:
  1698. // If in any other state or sub-state, do nothing.
  1699. break;
  1700. }
  1701. break;
  1702. case MESG_RESPONSE_EVENT_ID:
  1703. // Branch on event ID.
  1704. switch (p_message[BUFFER_INDEX_RESPONSE_CODE])
  1705. {
  1706. case EVENT_TRANSFER_TX_FAILED:
  1707. m_link_command_in_progress = ANTFS_CMD_NONE;
  1708. // Switch into the appropriate state after the failure. Must be ready for
  1709. // the host to do a retry.
  1710. switch (m_current_state.state)
  1711. {
  1712. case ANTFS_STATE_LINK:
  1713. link_layer_transit();
  1714. break;
  1715. case ANTFS_STATE_AUTH:
  1716. // Burst failed, retry sending the response
  1717. if(!m_retry)
  1718. {
  1719. authenticate_layer_transit(); // Reload beacon
  1720. }
  1721. else
  1722. {
  1723. if(m_current_state.sub_state.auth_sub_state == ANTFS_AUTH_SUBSTATE_ACCEPT)
  1724. {
  1725. if(m_authenticate_command_type == COMMAND_TYPE_REQUEST_PAIR)
  1726. {
  1727. authenticate_response_transmit(AUTH_RESPONSE_ACCEPT, ANTFS_PASSKEY_SIZE,
  1728. m_initial_parameters.p_pass_key);
  1729. }
  1730. else
  1731. {
  1732. authenticate_response_transmit(AUTH_RESPONSE_ACCEPT, 0, NULL);
  1733. }
  1734. }
  1735. else if(m_current_state.sub_state.auth_sub_state == ANTFS_AUTH_SUBSTATE_REJECT)
  1736. {
  1737. authenticate_response_transmit(AUTH_RESPONSE_REJECT, 0, NULL);
  1738. }
  1739. else if(m_authenticate_command_type == COMMAND_TYPE_REQUEST_SERIAL)
  1740. {
  1741. authenticate_response_transmit(AUTH_RESPONSE_N_A,
  1742. ANTFS_REMOTE_FRIENDLY_NAME_MAX,
  1743. // Send device friendly name if it exists.
  1744. m_initial_parameters.p_remote_friendly_name);
  1745. }
  1746. else
  1747. {
  1748. // No implementation needed
  1749. }
  1750. m_retry--;
  1751. }
  1752. break;
  1753. case ANTFS_STATE_TRANS:
  1754. if (m_current_state.sub_state.trans_sub_state ==
  1755. ANTFS_TRANS_SUBSTATE_DOWNLOADING)
  1756. {
  1757. event_queue_write(ANTFS_EVENT_DOWNLOAD_FAIL);
  1758. }
  1759. transport_layer_transit();
  1760. break;
  1761. default:
  1762. // No implementation needed.
  1763. break;
  1764. }
  1765. break;
  1766. case EVENT_TRANSFER_RX_FAILED:
  1767. m_link_command_in_progress = ANTFS_CMD_NONE;
  1768. if (m_current_state.sub_state.trans_sub_state ==
  1769. ANTFS_TRANS_SUBSTATE_UPLOADING)
  1770. {
  1771. event_queue_write(ANTFS_EVENT_UPLOAD_FAIL);
  1772. m_current_state.sub_state.trans_sub_state =
  1773. ANTFS_TRANS_SUBSTATE_UPLOAD_RESUME;
  1774. }
  1775. else
  1776. {
  1777. // No implementation needed
  1778. }
  1779. break;
  1780. case EVENT_TRANSFER_TX_COMPLETED:
  1781. m_link_command_in_progress = ANTFS_CMD_NONE;
  1782. // Switch into appropiate state after successful command.
  1783. switch (m_current_state.state)
  1784. {
  1785. case ANTFS_STATE_AUTH:
  1786. if (m_current_state.sub_state.auth_sub_state ==
  1787. ANTFS_AUTH_SUBSTATE_ACCEPT)
  1788. {
  1789. // We passed authentication, so go to transport state.
  1790. transport_layer_transit();
  1791. }
  1792. else if (m_current_state.sub_state.auth_sub_state ==
  1793. ANTFS_AUTH_SUBSTATE_REJECT)
  1794. {
  1795. // We failed authentication, so go to link state.
  1796. link_layer_transit();
  1797. }
  1798. else
  1799. {
  1800. // Reload beacon.
  1801. authenticate_layer_transit();
  1802. }
  1803. break;
  1804. case ANTFS_STATE_TRANS:
  1805. if (m_current_state.sub_state.trans_sub_state ==
  1806. ANTFS_TRANS_SUBSTATE_DOWNLOADING)
  1807. {
  1808. event_queue_write(ANTFS_EVENT_DOWNLOAD_COMPLETE);
  1809. }
  1810. if (m_current_state.sub_state.trans_sub_state !=
  1811. ANTFS_TRANS_SUBSTATE_UPLOAD_WAIT_FOR_DATA)
  1812. {
  1813. transport_layer_transit(); // Reload beacon.
  1814. }
  1815. break;
  1816. default:
  1817. link_layer_transit(); // Reload beacon.
  1818. break;
  1819. }
  1820. break;
  1821. case EVENT_TX:
  1822. #ifdef LEDDRIVER_ACTIVE
  1823. err_code = bsp_indication_set(BSP_INDICATE_SENT_OK);
  1824. APP_ERROR_CHECK(err_code);
  1825. #endif // LEDDRIVER_ACTIVE
  1826. // Load beacon.
  1827. beacon_transmit(MESG_BROADCAST_DATA_ID);
  1828. break;
  1829. case EVENT_CHANNEL_CLOSED:
  1830. event_queue_write(ANTFS_EVENT_CLOSE_COMPLETE);
  1831. break;
  1832. case NO_EVENT:
  1833. // This shouldn't happen... command responses should not occur.
  1834. APP_ERROR_HANDLER(p_message[BUFFER_INDEX_RESPONSE_CODE]);
  1835. break;
  1836. default:
  1837. // No implementation needed.
  1838. return;
  1839. }
  1840. break;
  1841. default:
  1842. // No implementation needed.
  1843. return;
  1844. }
  1845. }
  1846. }
  1847. void antfs_channel_setup(void)
  1848. {
  1849. // Start channel configuration.
  1850. uint32_t err_code = ant_fs_key_set(ANTFS_NETWORK_NUMBER);
  1851. APP_ERROR_CHECK(err_code);
  1852. err_code = sd_ant_channel_assign(ANTFS_CHANNEL, ANTFS_CHANNEL_TYPE, ANTFS_NETWORK_NUMBER, 0);
  1853. APP_ERROR_CHECK(err_code);
  1854. // Use the lower 2 bytes of the ESN for device number.
  1855. uint16_t device_number = (uint16_t)(m_initial_parameters.client_serial_number & 0x0000FFFFu);
  1856. if (device_number == 0)
  1857. {
  1858. // Device number of 0 is not allowed.
  1859. device_number = 2;
  1860. }
  1861. err_code = sd_ant_channel_id_set(ANTFS_CHANNEL,
  1862. device_number,
  1863. ANTFS_DEVICE_TYPE,
  1864. ANTFS_TRANS_TYPE);
  1865. APP_ERROR_CHECK(err_code);
  1866. // Remain in initialization state until channel is open.
  1867. m_current_state.state = ANTFS_STATE_INIT;
  1868. // @note: Channel frequency is set by function below.
  1869. link_layer_transit();
  1870. m_current_state.state = ANTFS_STATE_INIT;
  1871. channel_period_set(m_active_beacon_status1_field.parameters.link_period);
  1872. err_code = sd_ant_channel_open(ANTFS_CHANNEL);
  1873. APP_ERROR_CHECK(err_code);
  1874. err_code = sd_ant_channel_radio_tx_power_set(ANTFS_CHANNEL,
  1875. ANTFS_TRANSMIT_POWER,
  1876. ANTFS_CUSTOM_TRANSMIT_POWER);
  1877. APP_ERROR_CHECK(err_code);
  1878. m_current_state.state = ANTFS_STATE_LINK;
  1879. m_current_state.sub_state.link_sub_state = ANTFS_LINK_SUBSTATE_NONE;
  1880. event_queue_write(ANTFS_EVENT_OPEN_COMPLETE);
  1881. // Start beacon broadcast.
  1882. beacon_transmit(MESG_BROADCAST_DATA_ID);
  1883. }
  1884. /**@brief Function for resetting the ANT-FS state machine.
  1885. */
  1886. static void state_machine_reset(void)
  1887. {
  1888. m_current_state.state = ANTFS_STATE_OFF;
  1889. m_link_command_in_progress = ANTFS_CMD_NONE;
  1890. timeout_disable();
  1891. // Reset the ANT-FS event queue.
  1892. m_event_queue.p_queue = m_event_queue_buffer;
  1893. m_event_queue.head = 0;
  1894. m_event_queue.tail = 0;
  1895. // Set as invalid.
  1896. m_authenticate_command_type = 0xFFu;
  1897. m_retry = 0;
  1898. m_saved_crc_offset = 0xFFFFFFFFu;
  1899. m_max_transfer_index.data = 0;
  1900. m_is_crc_pending = false;
  1901. m_is_data_request_pending = false;
  1902. m_friendly_name.is_name_set = false;
  1903. m_friendly_name.index = 0;
  1904. memset(m_friendly_name.friendly_name, 0, ANTFS_FRIENDLY_NAME_MAX);
  1905. }
  1906. /**@brief Function for ANT-FS timer event.
  1907. *
  1908. * Handles pairing and command timeouts.
  1909. *
  1910. * @param[in] p_context The callback context.
  1911. */
  1912. static void timeout_handle(void * p_context)
  1913. {
  1914. if (m_current_state.state == ANTFS_STATE_OFF)
  1915. {
  1916. return;
  1917. }
  1918. if ((m_current_state.state == ANTFS_STATE_AUTH) &&
  1919. // Pairing timeout.
  1920. (m_current_state.sub_state.auth_sub_state == ANTFS_AUTH_SUBSTATE_PAIR))
  1921. {
  1922. // Reject authentication request and send pairing timeout event.
  1923. authenticate_response_transmit(AUTH_RESPONSE_REJECT, 0, NULL);
  1924. event_queue_write(ANTFS_EVENT_PAIRING_TIMEOUT);
  1925. }
  1926. // Fall back to link layer when an ANT-FS event times out.
  1927. link_layer_transit();
  1928. }
  1929. void antfs_init(const antfs_params_t * const p_params,
  1930. antfs_burst_wait_handler_t burst_wait_handler)
  1931. {
  1932. m_initial_parameters = *p_params;
  1933. m_burst_wait_handler = burst_wait_handler;
  1934. m_active_beacon_status1_field = m_initial_parameters.beacon_status_byte1;
  1935. uint32_t err_code = app_timer_create(&m_timer_id, APP_TIMER_MODE_SINGLE_SHOT, timeout_handle);
  1936. APP_ERROR_CHECK(err_code);
  1937. state_machine_reset();
  1938. err_code = sd_ant_burst_handler_wait_flag_enable((uint8_t *)(&m_burst_wait));
  1939. APP_ERROR_CHECK(err_code);
  1940. }