mac802154_data.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /****************************************************************************
  2. * wireless/ieee802154/mac802154_data.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Copyright (C) 2017 Verge Inc. All rights reserved.
  6. *
  7. * Author: Gregory Nutt <gnutt@nuttx.org>
  8. * Author: Anthony Merlino <anthony@vergeaero.com>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * 3. Neither the name NuttX nor the names of its contributors may be
  21. * used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  32. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ****************************************************************************/
  38. /****************************************************************************
  39. * Included Files
  40. ****************************************************************************/
  41. #include <nuttx/config.h>
  42. #include <stdlib.h>
  43. #include <assert.h>
  44. #include <errno.h>
  45. #include <debug.h>
  46. #include <string.h>
  47. #include <nuttx/mm/iob.h>
  48. #include "mac802154.h"
  49. #include "mac802154_internal.h"
  50. #include "mac802154_data.h"
  51. #include <nuttx/wireless/ieee802154/ieee802154_mac.h>
  52. /****************************************************************************
  53. * Public MAC Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: mac802154_req_data
  57. *
  58. * Description:
  59. * The MCPS-DATA.request primitive requests the transfer of a data SPDU
  60. * (i.e., MSDU) from a local SSCS entity to a single peer SSCS entity.
  61. * Confirmation is returned via the
  62. * struct mac802154_maccb_s->conf_data callback.
  63. *
  64. ****************************************************************************/
  65. int mac802154_req_data(MACHANDLE mac,
  66. FAR const struct ieee802154_frame_meta_s *meta,
  67. FAR struct iob_s *frame)
  68. {
  69. FAR struct ieee802154_privmac_s *priv =
  70. (FAR struct ieee802154_privmac_s *)mac;
  71. FAR struct ieee802154_txdesc_s *txdesc;
  72. uint16_t *frame_ctrl;
  73. uint8_t mhr_len = 3;
  74. int ret;
  75. wlinfo("Accepting outbound frame io_len=%u io_offset=%u\n",
  76. frame->io_len, frame->io_offset);
  77. /* Check the required frame size */
  78. if (frame->io_len > IEEE802154_MAX_PHY_PACKET_SIZE)
  79. {
  80. return -E2BIG;
  81. }
  82. /* Cast the first two bytes of the IOB to a uint16_t frame control field */
  83. frame_ctrl = (FAR uint16_t *)&frame->io_data[0];
  84. /* Ensure we start with a clear frame control field */
  85. *frame_ctrl = 0;
  86. /* Set the frame type to Data */
  87. *frame_ctrl |= IEEE802154_FRAME_DATA << IEEE802154_FRAMECTRL_SHIFT_FTYPE;
  88. /* If the msduLength is greater than aMaxMACSafePayloadSize, the MAC
  89. * sublayer will set the Frame Version to one. [1] pg. 118.
  90. */
  91. if ((frame->io_len - frame->io_offset) > IEEE802154_MAX_SAFE_MAC_PAYLOAD_SIZE)
  92. {
  93. *frame_ctrl |= (1 << IEEE802154_FRAMECTRL_SHIFT_VERSION);
  94. }
  95. /* If the TXOptions parameter specifies that an acknowledged transmission
  96. * is required, the AR field will be set appropriately, as described in
  97. * 5.1.6.4 [1] pg. 118.
  98. */
  99. *frame_ctrl |= (meta->flags.ackreq << IEEE802154_FRAMECTRL_SHIFT_ACKREQ);
  100. /* If the destination address is present, copy the PAN ID and one of the
  101. * addresses, depending on mode, into the MHR.
  102. */
  103. if (meta->destaddr.mode != IEEE802154_ADDRMODE_NONE)
  104. {
  105. IEEE802154_PANIDCOPY(&frame->io_data[mhr_len], meta->destaddr.panid);
  106. mhr_len += IEEE802154_PANIDSIZE;
  107. if (meta->destaddr.mode == IEEE802154_ADDRMODE_SHORT)
  108. {
  109. IEEE802154_SADDRCOPY(&frame->io_data[mhr_len], meta->destaddr.saddr);
  110. mhr_len += IEEE802154_SADDRSIZE;
  111. }
  112. else if (meta->destaddr.mode == IEEE802154_ADDRMODE_EXTENDED)
  113. {
  114. int index;
  115. /* The IEEE 802.15.4 Standard is confusing with regards to byte-order
  116. * for * extended address. More research discovers that the extended
  117. * address should be sent in reverse-canonical form.
  118. */
  119. for (index = IEEE802154_EADDRSIZE - 1; index >= 0; index--)
  120. {
  121. frame->io_data[mhr_len++] = meta->destaddr.eaddr[index];
  122. }
  123. }
  124. }
  125. /* Set the destination addr mode inside the frame control field */
  126. *frame_ctrl |= (meta->destaddr.mode << IEEE802154_FRAMECTRL_SHIFT_DADDR);
  127. /* From this point on, we need exclusive access to the privmac struct */
  128. ret = mac802154_lock(priv, true);
  129. if (ret < 0)
  130. {
  131. /* Should only fail if interrupted by a signal */
  132. wlwarn("WARNING: mac802154_takesem failed: %d\n", ret);
  133. return ret;
  134. }
  135. /* If both destination and source addressing information is present, the MAC
  136. * sublayer shall compare the destination and source PAN identifiers.
  137. * [1] pg. 41.
  138. */
  139. if (meta->srcmode != IEEE802154_ADDRMODE_NONE &&
  140. meta->destaddr.mode != IEEE802154_ADDRMODE_NONE)
  141. {
  142. /* If the PAN identifiers are identical, the PAN ID Compression field
  143. * shall be set to one, and the source PAN identifier shall be omitted
  144. * from the transmitted frame. [1] pg. 41.
  145. */
  146. if (IEEE802154_PANIDCMP(meta->destaddr.panid, priv->addr.panid))
  147. {
  148. *frame_ctrl |= IEEE802154_FRAMECTRL_PANIDCOMP;
  149. }
  150. }
  151. if (meta->srcmode != IEEE802154_ADDRMODE_NONE)
  152. {
  153. /* If the destination address is not included, or if PAN ID Compression
  154. * is off, we need to include the Source PAN ID.
  155. */
  156. if ((meta->destaddr.mode == IEEE802154_ADDRMODE_NONE) ||
  157. (!(*frame_ctrl & IEEE802154_FRAMECTRL_PANIDCOMP)))
  158. {
  159. IEEE802154_PANIDCOPY(&frame->io_data[mhr_len], priv->addr.panid);
  160. mhr_len += IEEE802154_PANIDSIZE;
  161. }
  162. if (meta->srcmode == IEEE802154_ADDRMODE_SHORT)
  163. {
  164. IEEE802154_SADDRCOPY(&frame->io_data[mhr_len], priv->addr.saddr);
  165. mhr_len += IEEE802154_SADDRSIZE;
  166. }
  167. else if (meta->srcmode == IEEE802154_ADDRMODE_EXTENDED)
  168. {
  169. int index;
  170. /* The IEEE 802.15.4 Standard is confusing with regards to byte-order
  171. * for * extended address. More research discovers that the extended
  172. * address should be sent in reverse-canonical form.
  173. */
  174. for (index = IEEE802154_EADDRSIZE - 1; index >= 0; index--)
  175. {
  176. frame->io_data[mhr_len++] = priv->addr.eaddr[index];
  177. }
  178. }
  179. }
  180. else
  181. {
  182. /* If this device is not the PAN coordinator, it shouldn't be sending
  183. * frames with a source address mode of NONE
  184. */
  185. if (priv->devmode != IEEE802154_DEVMODE_PANCOORD)
  186. {
  187. ret = -EINVAL;
  188. goto errout_with_sem;
  189. }
  190. }
  191. /* Set the source addr mode inside the frame control field */
  192. *frame_ctrl |= (meta->srcmode << IEEE802154_FRAMECTRL_SHIFT_SADDR);
  193. /* Each time a data or a MAC command frame is generated, the MAC sublayer
  194. * shall copy the value of macDSN into the Sequence Number field of the MHR
  195. * of the outgoing frame and then increment it by one. [1] pg. 40.
  196. */
  197. frame->io_data[2] = priv->dsn++;
  198. /* The MAC header we just created must never have exceeded where the app
  199. * data starts. This should never happen since the offset should have
  200. * been set via the same logic to calculate the header length as the logic
  201. * here that created the header
  202. */
  203. wlinfo("mhr_len=%u\n", mhr_len);
  204. DEBUGASSERT(mhr_len == frame->io_offset);
  205. /* Allocate the txdesc, waiting if necessary, allow interruptions */
  206. ret = mac802154_txdesc_alloc(priv, &txdesc, true);
  207. if (ret < 0)
  208. {
  209. /* Should only fail if interrupted by a signal while re-acquiring
  210. * exclsem. So the lock is not held if a failure is returned.
  211. */
  212. wlwarn("WARNING: mac802154_txdesc_alloc failed: %d\n", ret);
  213. return ret;
  214. }
  215. /* Set the offset to 0 to include the header ( we do not want to
  216. * modify the frame until AFTER the last place that -EINTR could
  217. * be returned and could generate a retry. Subsequent error returns
  218. * are fatal and no retry should occur.
  219. */
  220. frame->io_offset = 0;
  221. /* Then initialize the TX descriptor */
  222. txdesc->conf->handle = meta->handle;
  223. txdesc->frame = frame;
  224. txdesc->frametype = IEEE802154_FRAME_DATA;
  225. txdesc->ackreq = meta->flags.ackreq;
  226. /* If the TxOptions parameter specifies that a GTS transmission is required,
  227. * the MAC sublayer will determine whether it has a valid GTS as described
  228. * 5.1.7.3. If a valid GTS could not be found, the MAC sublayer will discard
  229. * the MSDU. If a valid GTS was found, the MAC sublayer will defer, if
  230. * necessary, until the GTS. If the TxOptions parameter specifies that a GTS
  231. * transmission is not required, the MAC sublayer will transmit the MSDU using
  232. * either slotted CSMA-CA in the CAP for a beacon-enabled PAN or unslotted
  233. * CSMA-CA for a nonbeacon-enabled PAN. Specifying a GTS transmission in the
  234. * TxOptions parameter overrides an indirect transmission request.
  235. * [1] pg. 118.
  236. */
  237. if (meta->flags.usegts)
  238. {
  239. /* TODO: Support GTS transmission. This should just change where we link
  240. * the transaction. Instead of going in the CSMA transaction list, it
  241. * should be linked to the GTS' transaction list. We'll need to check if
  242. * the GTS is valid, and then find the GTS, before linking. Note, we also
  243. * don't have to try and kick-off any transmission here.
  244. */
  245. ret = -ENOTSUP;
  246. goto errout_with_txdesc;
  247. }
  248. else
  249. {
  250. /* If the TxOptions parameter specifies that an indirect transmission is
  251. * required and this primitive is received by the MAC sublayer of a
  252. * coordinator, the data frame is sent using indirect transmission, as
  253. * described in 5.1.5 and 5.1.6.3. [1]
  254. */
  255. if (meta->flags.indirect)
  256. {
  257. /* If the TxOptions parameter specifies that an indirect transmission
  258. * is required and if the device receiving this primitive is not a
  259. * coordinator, the destination address is not present, or the
  260. * TxOptions parameter also specifies a GTS transmission, the indirect
  261. * transmission option will be ignored. [1]
  262. *
  263. * NOTE: We don't just ignore the parameter. Instead, we throw an
  264. * error, since this really shouldn't be happening.
  265. */
  266. if (priv->devmode >= IEEE802154_DEVMODE_COORD &&
  267. meta->destaddr.mode != IEEE802154_ADDRMODE_NONE)
  268. {
  269. /* Copy in a reference to the destination address to assist in
  270. * searching when data is requested.
  271. */
  272. memcpy(&txdesc->destaddr, &meta->destaddr,
  273. sizeof(struct ieee802154_addr_s));
  274. mac802154_setupindirect(priv, txdesc);
  275. mac802154_unlock(priv)
  276. }
  277. else
  278. {
  279. ret = -EINVAL;
  280. goto errout_with_txdesc;
  281. }
  282. }
  283. else
  284. {
  285. /* Link the transaction into the CSMA transaction list */
  286. sq_addlast((FAR sq_entry_t *)txdesc, &priv->csma_queue);
  287. /* We no longer need to have the MAC layer locked. */
  288. mac802154_unlock(priv)
  289. /* Notify the radio driver that there is data available */
  290. priv->radio->txnotify(priv->radio, false);
  291. }
  292. }
  293. return OK;
  294. errout_with_txdesc:
  295. /* Free TX the descriptor, but preserve the IOB. */
  296. txdesc->frame = NULL;
  297. mac802154_txdesc_free(priv, txdesc);
  298. errout_with_sem:
  299. mac802154_unlock(priv)
  300. return ret;
  301. }
  302. /****************************************************************************
  303. * Internal MAC Functions
  304. ****************************************************************************/