antfs.c 97 KB

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