nrf_dfu_ant.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /**
  2. * This software is subject to the ANT+ Shared Source License
  3. * www.thisisant.com/swlicenses
  4. * Copyright (c) Garmin Canada Inc. 2018
  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 <stdbool.h>
  54. #include <stdint.h>
  55. #include "sdk_common.h"
  56. #include "ant_channel_config.h"
  57. #include "ant_interface.h"
  58. #include "ant_parameters.h"
  59. #include "nrf_assert.h"
  60. #include "nrf_balloc.h"
  61. #include "nrf_bootloader_info.h"
  62. #include "nrf_dfu_handling_error.h"
  63. #include "nrf_dfu_req_handler.h"
  64. #include "nrf_dfu_transport.h"
  65. #include "nrf_dfu_mbr.h"
  66. #include "nrf_sdh.h"
  67. #include "nrf_sdh_ant.h"
  68. #include "nrf_soc.h"
  69. #define NRF_LOG_MODULE_NAME nrf_dfu_ant
  70. #include "nrf_log.h"
  71. NRF_LOG_MODULE_REGISTER();
  72. /**@file
  73. *
  74. * @defgroup nrf_dfu_ant ANT transport for reference DFU.
  75. * @ingroup nrf_dfu
  76. * @brief Device Firmware Update (DFU) transport layer using ANT.
  77. *
  78. * Transport documentation:
  79. *
  80. * The ANT transport uses all of the same opcodes and payload formats as the
  81. * UART serial transport. The only differences are the packet header format and
  82. * some extra details to deal with retransmissions.
  83. *
  84. * The device receiving the update is the ANT master. The format of the
  85. * broadcast buffer is as follows;
  86. * Byte 0: Current Slave -> Master sequence number.
  87. * Byte 1: Current Master -> Slave sequence number.
  88. * Bytes 2-7: Reserved, set to 0.
  89. *
  90. * The sequence numbers are used to detect retransmissions, any messages sent
  91. * with a sequence number equivalent to the current sequence will be ignored.
  92. *
  93. * When the slave first connects to the master it should inspect the broadcast
  94. * data in order to synchronize its sequence counters.
  95. *
  96. * All commands/responses are padded out to the nearest 8-byte boundary after
  97. * framing, and then sent using either a burst or acknowledged data depending on
  98. * length (ack data is used for 8-byte messages). The message transmission is
  99. * retried until an EVENT_TRANSFER_TX_COMPLETE event is received.
  100. * All messages are framed using the following format:
  101. * Bytes 0-1: Message length before padding, little endian, includes header.
  102. * Byte 2: Sequence number. Increment for every new message.
  103. * Byte 3: Op code. Always 0x60 for responses.
  104. * Bytes 4-N: Command/Response payload. This follows the same format as the
  105. * UART serial transport, without any SLIP encoding.
  106. *
  107. * As a final note, the MTU for this protocol is the maximum size of a burst
  108. * that can be received.
  109. */
  110. /** Packet header is always 2 byte length + seq num + op code */
  111. #define PKT_HEADER_SIZE 4
  112. /** Maximum size of the payload in a write command. */
  113. #define MAX_WRITE_PAYLOAD (NRF_DFU_ANT_MTU - PKT_HEADER_SIZE)
  114. /** Bursts are always a multiple of the standard data size. */
  115. STATIC_ASSERT_MSG(
  116. ALIGN_NUM(ANT_STANDARD_DATA_PAYLOAD_SIZE, NRF_DFU_ANT_MTU) == NRF_DFU_ANT_MTU,
  117. "ANT MTU must be a multiple of " STRINGIFY(ANT_STANDARD_DATA_PAYLOAD_SIZE));
  118. /** Number of buffers to reserve space for with balloc. */
  119. #if (NRF_DFU_ANT_BUFFERS_OVERRIDE)
  120. #define NUM_BUFFERS NRF_DFU_ANT_BUFFERS
  121. #else
  122. #define NUM_BUFFERS CEIL_DIV(CODE_PAGE_SIZE, MAX_WRITE_PAYLOAD)
  123. #endif
  124. static uint32_t ant_dfu_init(nrf_dfu_observer_t observer);
  125. static uint32_t ant_dfu_close(nrf_dfu_transport_t const * p_exception);
  126. static ant_channel_config_t m_channel_config = {
  127. .channel_number = 0,
  128. .channel_type = CHANNEL_TYPE_MASTER,
  129. .rf_freq = NRF_DFU_ANT_RF_FREQ,
  130. .transmission_type = 1, // Non-shared, no global pages.
  131. .device_type = NRF_DFU_ANT_DEV_TYPE,
  132. .channel_period = NRF_DFU_ANT_CHANNEL_PERIOD,
  133. };
  134. static nrf_dfu_observer_t m_observer;
  135. /** Has transport been initialized by DFU core */
  136. static bool m_initialized = false;
  137. /** Has the channel started broadcasting */
  138. static bool m_started = false;
  139. /** Has some data been received on the transport. */
  140. static bool m_active = false;
  141. /** State tracking for rx transfers. */
  142. static struct
  143. {
  144. /** Buffer for holding the command. */
  145. uint8_t * buff;
  146. /** Amount of data written */
  147. size_t offset;
  148. /** Sequence of last processed command. */
  149. uint8_t seq;
  150. } m_rx;
  151. /** State tracking for tx transfers. */
  152. static struct
  153. {
  154. /** Raw data to send. */
  155. uint8_t resp[ALIGN_NUM(ANT_STANDARD_DATA_PAYLOAD_SIZE,
  156. PKT_HEADER_SIZE + sizeof(nrf_dfu_response_t))];
  157. /** Length of data to send. 0 Indicates no response queued. */
  158. size_t len;
  159. /** Sequence number of last queued response. */
  160. uint8_t seq;
  161. /**
  162. * Used as burst flag for softdevice, allows to busy loop until all data is
  163. * accepted by softdevice.
  164. */
  165. volatile bool buffering;
  166. /**
  167. * Indicate that a new response was generated before the last one was
  168. * confirmed.
  169. */
  170. bool response_overwritten;
  171. /** Data buffer used for broadcast messages. */
  172. uint8_t bcast_data[ANT_STANDARD_DATA_PAYLOAD_SIZE];
  173. } m_tx;
  174. /** State tracking for progress notifications. */
  175. static struct
  176. {
  177. /** Requested PRN */
  178. uint16_t limit;
  179. /** How many more write commands until a CRC should be sent back. */
  180. uint16_t remaining;
  181. } m_pkt_notify;
  182. DFU_TRANSPORT_REGISTER(nrf_dfu_transport_t const ant_dfu_transport) = {
  183. .init_func = ant_dfu_init,
  184. .close_func = ant_dfu_close,
  185. };
  186. NRF_BALLOC_DEF(m_buffer_pool, NRF_DFU_ANT_MTU, NUM_BUFFERS);
  187. static void release_rx_buff(void)
  188. {
  189. if (m_rx.buff != NULL)
  190. {
  191. nrf_balloc_free(&m_buffer_pool, m_rx.buff);
  192. m_rx.buff = NULL;
  193. }
  194. }
  195. static void transmit_response(void)
  196. {
  197. uint32_t err_code = NRF_SUCCESS;
  198. size_t full_len = ALIGN_NUM(ANT_STANDARD_DATA_PAYLOAD_SIZE, m_tx.len);
  199. // Pad out with 0's.
  200. memset(&m_tx.resp[m_tx.len], 0, full_len - m_tx.len);
  201. if (full_len > ANT_STANDARD_DATA_PAYLOAD_SIZE)
  202. {
  203. err_code = sd_ant_burst_handler_request(
  204. m_channel_config.channel_number,
  205. full_len, m_tx.resp,
  206. BURST_SEGMENT_START | BURST_SEGMENT_END);
  207. } else
  208. {
  209. err_code = sd_ant_acknowledge_message_tx(
  210. m_channel_config.channel_number,
  211. full_len, m_tx.resp);
  212. }
  213. // Wait for buffer to be consumed.
  214. // TODO: wait flag management needs to be improved if this will coexist with
  215. // other channels.
  216. while (err_code == NRF_SUCCESS && m_tx.buffering)
  217. {
  218. err_code = sd_app_evt_wait();
  219. }
  220. if (err_code != NRF_SUCCESS)
  221. {
  222. NRF_LOG_ERROR("Sending response failed with error %d", err_code);
  223. }
  224. }
  225. static void update_bcast_data(void)
  226. {
  227. memset(m_tx.bcast_data, 0, sizeof(m_tx.bcast_data));
  228. m_tx.bcast_data[0] = m_rx.seq;
  229. m_tx.bcast_data[1] = m_tx.seq;
  230. if (NRF_SUCCESS != sd_ant_broadcast_message_tx(
  231. m_channel_config.channel_number,
  232. sizeof(m_tx.bcast_data), m_tx.bcast_data))
  233. {
  234. NRF_LOG_WARNING("Unable to update broadcast data.");
  235. }
  236. }
  237. static void handle_write_complete(void * p_buf)
  238. {
  239. nrf_balloc_free(&m_buffer_pool, p_buf);
  240. }
  241. static void prepare_response(nrf_dfu_response_t * p_res)
  242. {
  243. if (m_tx.len)
  244. {
  245. NRF_LOG_WARNING("Overwriting previous response.");
  246. m_tx.response_overwritten = true;
  247. }
  248. // reserve first 2 bytes for length.
  249. m_tx.len = 2;
  250. m_tx.resp[m_tx.len++] = ++(m_tx.seq);
  251. m_tx.resp[m_tx.len++] = NRF_DFU_OP_RESPONSE;
  252. m_tx.resp[m_tx.len++] = p_res->request;
  253. m_tx.resp[m_tx.len++] = p_res->result;
  254. if (p_res->result == NRF_DFU_RES_CODE_SUCCESS)
  255. {
  256. switch(p_res->request)
  257. {
  258. case NRF_DFU_OP_PROTOCOL_VERSION:
  259. {
  260. m_tx.resp[m_tx.len++] = p_res->protocol.version;
  261. } break;
  262. case NRF_DFU_OP_CRC_GET:
  263. {
  264. m_tx.len += uint32_encode(
  265. p_res->crc.offset, &m_tx.resp[m_tx.len]);
  266. m_tx.len += uint32_encode(
  267. p_res->crc.crc, &m_tx.resp[m_tx.len]);
  268. } break;
  269. case NRF_DFU_OP_OBJECT_SELECT:
  270. {
  271. m_tx.len += uint32_encode(
  272. p_res->select.max_size, &m_tx.resp[m_tx.len]);
  273. m_tx.len += uint32_encode(
  274. p_res->select.offset, &m_tx.resp[m_tx.len]);
  275. m_tx.len += uint32_encode(
  276. p_res->select.crc, &m_tx.resp[m_tx.len]);
  277. } break;
  278. case NRF_DFU_OP_MTU_GET:
  279. {
  280. m_tx.len += uint16_encode(
  281. p_res->mtu.size, &m_tx.resp[m_tx.len]);
  282. } break;
  283. case NRF_DFU_OP_PING:
  284. {
  285. m_tx.resp[m_tx.len++] = p_res->ping.id;
  286. } break;
  287. case NRF_DFU_OP_HARDWARE_VERSION:
  288. {
  289. m_tx.len += uint32_encode(
  290. p_res->hardware.part, &m_tx.resp[m_tx.len]);
  291. m_tx.len += uint32_encode(
  292. p_res->hardware.variant, &m_tx.resp[m_tx.len]);
  293. m_tx.len += uint32_encode(
  294. p_res->hardware.memory.rom_size, &m_tx.resp[m_tx.len]);
  295. m_tx.len += uint32_encode(
  296. p_res->hardware.memory.rom_page_size, &m_tx.resp[m_tx.len]);
  297. m_tx.len += uint32_encode(
  298. p_res->hardware.memory.ram_size, &m_tx.resp[m_tx.len]);
  299. } break;
  300. case NRF_DFU_OP_FIRMWARE_VERSION:
  301. {
  302. m_tx.resp[m_tx.len++] = p_res->firmware.type;
  303. m_tx.len += uint32_encode(
  304. p_res->firmware.version, &m_tx.resp[m_tx.len]);
  305. m_tx.len += uint32_encode(
  306. p_res->firmware.addr, &m_tx.resp[m_tx.len]);
  307. m_tx.len += uint32_encode(
  308. p_res->firmware.len, &m_tx.resp[m_tx.len]);
  309. } break;
  310. default:
  311. break;
  312. }
  313. }
  314. else if (p_res->result == NRF_DFU_RES_CODE_EXT_ERROR)
  315. {
  316. m_tx.resp[m_tx.len++] = ext_error_get();
  317. UNUSED_RETURN_VALUE(ext_error_set(NRF_DFU_EXT_ERROR_NO_ERROR));
  318. }
  319. // Finally fill in the length.
  320. UNUSED_RETURN_VALUE(uint16_encode(m_tx.len, m_tx.resp));
  321. // Safety check buffer overflow.
  322. ASSERT(m_tx.len <= sizeof(m_tx.resp));
  323. if (!m_tx.response_overwritten)
  324. {
  325. // Can send out the response immediately if there wasn't a previous one
  326. // queued.
  327. transmit_response();
  328. }
  329. }
  330. static void handle_response(nrf_dfu_response_t * p_res, void * p_context)
  331. {
  332. UNUSED_PARAMETER(p_context);
  333. if (p_res->result != NRF_DFU_RES_CODE_SUCCESS)
  334. {
  335. NRF_LOG_WARNING("Operation %d had result %d",
  336. p_res->request, p_res->result);
  337. }
  338. if (p_res->request == NRF_DFU_OP_OBJECT_WRITE)
  339. {
  340. if (m_pkt_notify.limit == 0 ||
  341. --m_pkt_notify.remaining != 0)
  342. {
  343. // No packet notification needed, filter out response.
  344. return;
  345. }
  346. // Packet Notification time, send a CRC response.
  347. m_pkt_notify.remaining = m_pkt_notify.limit;
  348. p_res->request = NRF_DFU_OP_CRC_GET;
  349. uint32_t offset = p_res->write.offset;
  350. uint32_t crc = p_res->write.crc;
  351. p_res->crc.offset = offset;
  352. p_res->crc.crc = crc;
  353. }
  354. prepare_response(p_res);
  355. }
  356. static uint32_t handle_request(void)
  357. {
  358. uint16_t len = uint16_decode(m_rx.buff);
  359. uint16_t offset = sizeof(uint16_t);
  360. if (len < PKT_HEADER_SIZE || len > m_rx.offset)
  361. {
  362. NRF_LOG_WARNING("Ignoring command with invalid length.");
  363. return NRF_ERROR_DATA_SIZE;
  364. }
  365. uint8_t seq = m_rx.buff[offset++];
  366. if (!m_active)
  367. {
  368. m_active = true;
  369. // Close all other transports.
  370. UNUSED_RETURN_VALUE(nrf_dfu_transports_close(&ant_dfu_transport));
  371. }
  372. else if (seq == m_rx.seq)
  373. {
  374. NRF_LOG_DEBUG("Ignoring repeated command");
  375. return NRF_SUCCESS;
  376. }
  377. m_rx.seq = seq;
  378. nrf_dfu_request_t request = {
  379. .request = (nrf_dfu_op_t)m_rx.buff[offset++],
  380. .callback.response = handle_response,
  381. };
  382. switch(request.request)
  383. {
  384. case NRF_DFU_OP_OBJECT_CREATE:
  385. {
  386. request.create.object_type = m_rx.buff[offset++];
  387. request.create.object_size = uint32_decode(&m_rx.buff[offset]);
  388. offset += sizeof(uint32_t);
  389. } break;
  390. case NRF_DFU_OP_RECEIPT_NOTIF_SET:
  391. {
  392. request.prn.target = uint16_decode(&m_rx.buff[offset]);
  393. offset += sizeof(uint16_t);
  394. } break;
  395. case NRF_DFU_OP_OBJECT_SELECT:
  396. {
  397. request.select.object_type = m_rx.buff[offset++];
  398. } break;
  399. case NRF_DFU_OP_OBJECT_WRITE:
  400. {
  401. request.write.p_data = &m_rx.buff[offset];
  402. request.write.len = len - offset;
  403. offset = len;
  404. } break;
  405. case NRF_DFU_OP_PING:
  406. {
  407. request.ping.id = m_rx.buff[offset++];
  408. } break;
  409. case NRF_DFU_OP_FIRMWARE_VERSION:
  410. {
  411. request.firmware.image_number = m_rx.buff[offset++];
  412. } break;
  413. case NRF_DFU_OP_MTU_GET:
  414. {
  415. NRF_LOG_DEBUG("ANT DFU: Responding to MTU request with %d",
  416. NRF_DFU_ANT_MTU);
  417. request.mtu.size = NRF_DFU_ANT_MTU;
  418. } break;
  419. default:
  420. // Do nothing.
  421. break;
  422. }
  423. if (offset > len)
  424. {
  425. NRF_LOG_WARNING("Ignoring command with invalid length");
  426. return NRF_ERROR_DATA_SIZE;
  427. }
  428. // Some processing that is only safe to do if accepting the command.
  429. switch (request.request)
  430. {
  431. case NRF_DFU_OP_RECEIPT_NOTIF_SET:
  432. {
  433. m_pkt_notify.limit = request.prn.target;
  434. m_pkt_notify.remaining = m_pkt_notify.limit;
  435. } break;
  436. case NRF_DFU_OP_OBJECT_CREATE:
  437. case NRF_DFU_OP_OBJECT_SELECT:
  438. {
  439. m_pkt_notify.remaining = m_pkt_notify.limit;
  440. } break;
  441. case NRF_DFU_OP_OBJECT_WRITE:
  442. {
  443. // Ownership of buffer is transferred to the write command.
  444. request.callback.write = handle_write_complete;
  445. m_rx.buff = NULL;
  446. } break;
  447. default:
  448. break;
  449. }
  450. return nrf_dfu_req_handler_on_req(&request);
  451. }
  452. static void handle_tx_transfer_complete(bool success)
  453. {
  454. if (m_tx.response_overwritten)
  455. {
  456. // By treating the result as a failure the retransmission will send out
  457. // the new response.
  458. success = false;
  459. m_tx.response_overwritten = false;
  460. }
  461. if (success)
  462. {
  463. m_tx.len = 0;
  464. update_bcast_data();
  465. }
  466. else
  467. {
  468. transmit_response();
  469. }
  470. }
  471. static void handle_rx_transfer_start()
  472. {
  473. if (m_rx.buff == NULL)
  474. {
  475. m_rx.buff = nrf_balloc_alloc(&m_buffer_pool);
  476. if (m_rx.buff != NULL)
  477. {
  478. NRF_LOG_INFO("Allocated buffer %x", m_rx.buff);
  479. }
  480. else
  481. {
  482. NRF_LOG_ERROR("Unable to allocate buffer for incoming packet.");
  483. return;
  484. }
  485. }
  486. NRF_LOG_DEBUG("Resetting rx pointer.");
  487. m_rx.offset = 0;
  488. }
  489. static void handle_rx_transfer_complete(bool success)
  490. {
  491. if (success)
  492. {
  493. uint32_t err_code = handle_request();
  494. if (err_code != NRF_SUCCESS)
  495. {
  496. NRF_LOG_ERROR("Error %d handling request.", err_code);
  497. }
  498. }
  499. release_rx_buff();
  500. }
  501. static void handle_rx_transfer_data(uint8_t * data, size_t len)
  502. {
  503. if (m_rx.buff == NULL)
  504. {
  505. NRF_LOG_DEBUG("Ignoring transfer data.");
  506. return;
  507. }
  508. if (m_rx.offset + len > NRF_DFU_ANT_MTU)
  509. {
  510. NRF_LOG_ERROR("Received packet overflows MTU.");
  511. handle_rx_transfer_complete(false);
  512. return;
  513. }
  514. memcpy(&m_rx.buff[m_rx.offset], data, len);
  515. m_rx.offset += len;
  516. }
  517. static void handle_data_mesg(ANT_MESSAGE * p_msg)
  518. {
  519. bool is_first = false;
  520. bool is_last = false;
  521. uint8_t len = ANT_STANDARD_DATA_PAYLOAD_SIZE;
  522. switch(p_msg->ANT_MESSAGE_ucMesgID)
  523. {
  524. case MESG_BROADCAST_DATA_ID:
  525. {
  526. // Broadcast data is ignored.
  527. len = 0;
  528. } break;
  529. case MESG_ACKNOWLEDGED_DATA_ID:
  530. {
  531. is_first = true;
  532. is_last = true;
  533. } break;
  534. case MESG_ADV_BURST_DATA_ID:
  535. {
  536. len = p_msg->ANT_MESSAGE_ucSize - MESG_CHANNEL_NUM_SIZE;
  537. } // FALL-THROUGH : both burst types act the same other than len.
  538. case MESG_BURST_DATA_ID:
  539. {
  540. uint8_t seq = p_msg->ANT_MESSAGE_ucChannel & SEQUENCE_NUMBER_MASK;
  541. is_first = seq == SEQUENCE_FIRST_MESSAGE;
  542. is_last = !!(seq & SEQUENCE_LAST_MESSAGE);
  543. } break;
  544. }
  545. if (len != 0)
  546. {
  547. if (is_first)
  548. {
  549. handle_rx_transfer_start();
  550. }
  551. handle_rx_transfer_data(p_msg->ANT_MESSAGE_aucPayload, len);
  552. if (is_last)
  553. {
  554. handle_rx_transfer_complete(true);
  555. }
  556. }
  557. }
  558. static void ant_dfu_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  559. {
  560. // Ignore messages meant for other channels.
  561. if (p_ant_evt->channel != m_channel_config.channel_number)
  562. {
  563. return;
  564. }
  565. switch(p_ant_evt->event)
  566. {
  567. case EVENT_TX:
  568. {
  569. if (!m_started)
  570. {
  571. m_started = true;
  572. m_observer(NRF_DFU_EVT_TRANSPORT_ACTIVATED);
  573. }
  574. } break;
  575. case EVENT_TRANSFER_TX_COMPLETED:
  576. handle_tx_transfer_complete(true);
  577. break;
  578. case EVENT_TRANSFER_TX_FAILED:
  579. handle_tx_transfer_complete(false);
  580. break;
  581. case EVENT_RX:
  582. handle_data_mesg(&p_ant_evt->message);
  583. break;
  584. case EVENT_TRANSFER_RX_FAILED:
  585. handle_rx_transfer_complete(false);
  586. break;
  587. }
  588. }
  589. static uint32_t ant_dfu_init(nrf_dfu_observer_t observer)
  590. {
  591. uint32_t err_code = NRF_SUCCESS;
  592. if (m_initialized)
  593. {
  594. return err_code;
  595. }
  596. NRF_SDH_ANT_OBSERVER(ant_dfu_observer, NRF_DFU_ANT_EVT_HANDLER_PRIO,
  597. ant_dfu_evt_handler, NULL);
  598. m_observer = observer;
  599. m_tx.seq = m_rx.seq = 0;
  600. m_active = false;
  601. m_started = false;
  602. NRF_LOG_DEBUG("Initializing ANT DFU transport");
  603. err_code = nrf_balloc_init(&m_buffer_pool);
  604. VERIFY_SUCCESS(err_code);
  605. err_code = nrf_dfu_mbr_init_sd();
  606. VERIFY_SUCCESS(err_code);
  607. NRF_LOG_DEBUG("Setting up vector table: 0x%08x", BOOTLOADER_START_ADDR);
  608. err_code = sd_softdevice_vector_table_base_set(BOOTLOADER_START_ADDR);
  609. VERIFY_SUCCESS(err_code);
  610. NRF_LOG_DEBUG("Enabling softdevice");
  611. err_code = nrf_sdh_enable_request();
  612. VERIFY_SUCCESS(err_code);
  613. err_code = nrf_sdh_ant_enable();
  614. VERIFY_SUCCESS(err_code);
  615. static uint8_t adv_burst_conf[] = {
  616. ADV_BURST_MODE_ENABLE,
  617. ADV_BURST_MODES_SIZE_24_BYTES,
  618. 0, // No required modes.
  619. 0, 0, // Reserved
  620. ADV_BURST_MODES_FREQ_HOP, // Optional Modes
  621. 0, 0, // Reserved
  622. // No optional configs.
  623. };
  624. err_code = sd_ant_adv_burst_config_set(adv_burst_conf, sizeof(adv_burst_conf));
  625. VERIFY_SUCCESS(err_code);
  626. m_channel_config.device_number = NRF_FICR->DEVICEID[0];
  627. m_channel_config.transmission_type |= (NRF_FICR->DEVICEID[1] & 0xF) << 4;
  628. err_code = ant_channel_init(&m_channel_config);
  629. VERIFY_SUCCESS(err_code);
  630. update_bcast_data();
  631. err_code = sd_ant_channel_open(m_channel_config.channel_number);
  632. VERIFY_SUCCESS(err_code);
  633. NRF_LOG_DEBUG("ANT transport intialized");
  634. m_initialized = true;
  635. return err_code;
  636. }
  637. static uint32_t ant_dfu_close(nrf_dfu_transport_t const * p_exception)
  638. {
  639. uint32_t err_code = NRF_SUCCESS;
  640. if (p_exception != &ant_dfu_transport && m_initialized)
  641. {
  642. NRF_LOG_DEBUG("Shutting down ANT DFU transport");
  643. m_initialized = false;
  644. err_code = sd_ant_channel_close(m_channel_config.channel_number);
  645. VERIFY_SUCCESS(err_code);
  646. uint8_t status;
  647. do
  648. {
  649. // The initial wait is safe because the close command above would
  650. // have generated at least 1 app event.
  651. err_code = sd_app_evt_wait();
  652. VERIFY_SUCCESS(err_code);
  653. err_code = sd_ant_channel_status_get(
  654. m_channel_config.channel_number, &status);
  655. VERIFY_SUCCESS(err_code);
  656. } while ((status & STATUS_CHANNEL_STATE_MASK) != STATUS_ASSIGNED_CHANNEL);
  657. err_code = nrf_sdh_disable_request();
  658. VERIFY_SUCCESS(err_code);
  659. NRF_LOG_DEBUG("ANT transport disabled.");
  660. }
  661. return err_code;
  662. }