ieee802154.rst 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ===============================
  2. IEEE 802.15.4 Developer's Guide
  3. ===============================
  4. Introduction
  5. ============
  6. The IEEE 802.15.4 working group focuses on standardization of the bottom
  7. two layers: Medium Access Control (MAC) and Physical access (PHY). And there
  8. are mainly two options available for upper layers:
  9. - ZigBee - proprietary protocol from the ZigBee Alliance
  10. - 6LoWPAN - IPv6 networking over low rate personal area networks
  11. The goal of the Linux-wpan is to provide a complete implementation
  12. of the IEEE 802.15.4 and 6LoWPAN protocols. IEEE 802.15.4 is a stack
  13. of protocols for organizing Low-Rate Wireless Personal Area Networks.
  14. The stack is composed of three main parts:
  15. - IEEE 802.15.4 layer; We have chosen to use plain Berkeley socket API,
  16. the generic Linux networking stack to transfer IEEE 802.15.4 data
  17. messages and a special protocol over netlink for configuration/management
  18. - MAC - provides access to shared channel and reliable data delivery
  19. - PHY - represents device drivers
  20. Socket API
  21. ==========
  22. ::
  23. int sd = socket(PF_IEEE802154, SOCK_DGRAM, 0);
  24. The address family, socket addresses etc. are defined in the
  25. include/net/af_ieee802154.h header or in the special header
  26. in the userspace package (see either https://linux-wpan.org/wpan-tools.html
  27. or the git tree at https://github.com/linux-wpan/wpan-tools).
  28. 6LoWPAN Linux implementation
  29. ============================
  30. The IEEE 802.15.4 standard specifies an MTU of 127 bytes, yielding about 80
  31. octets of actual MAC payload once security is turned on, on a wireless link
  32. with a link throughput of 250 kbps or less. The 6LoWPAN adaptation format
  33. [RFC4944] was specified to carry IPv6 datagrams over such constrained links,
  34. taking into account limited bandwidth, memory, or energy resources that are
  35. expected in applications such as wireless Sensor Networks. [RFC4944] defines
  36. a Mesh Addressing header to support sub-IP forwarding, a Fragmentation header
  37. to support the IPv6 minimum MTU requirement [RFC2460], and stateless header
  38. compression for IPv6 datagrams (LOWPAN_HC1 and LOWPAN_HC2) to reduce the
  39. relatively large IPv6 and UDP headers down to (in the best case) several bytes.
  40. In September 2011 the standard update was published - [RFC6282].
  41. It deprecates HC1 and HC2 compression and defines IPHC encoding format which is
  42. used in this Linux implementation.
  43. All the code related to 6lowpan you may find in files: net/6lowpan/*
  44. and net/ieee802154/6lowpan/*
  45. To setup a 6LoWPAN interface you need:
  46. 1. Add IEEE802.15.4 interface and set channel and PAN ID;
  47. 2. Add 6lowpan interface by command like:
  48. # ip link add link wpan0 name lowpan0 type lowpan
  49. 3. Bring up 'lowpan0' interface
  50. Drivers
  51. =======
  52. Like with WiFi, there are several types of devices implementing IEEE 802.15.4.
  53. 1) 'HardMAC'. The MAC layer is implemented in the device itself, the device
  54. exports a management (e.g. MLME) and data API.
  55. 2) 'SoftMAC' or just radio. These types of devices are just radio transceivers
  56. possibly with some kinds of acceleration like automatic CRC computation and
  57. comparation, automagic ACK handling, address matching, etc.
  58. Those types of devices require different approach to be hooked into Linux kernel.
  59. HardMAC
  60. -------
  61. See the header include/net/ieee802154_netdev.h. You have to implement Linux
  62. net_device, with .type = ARPHRD_IEEE802154. Data is exchanged with socket family
  63. code via plain sk_buffs. On skb reception skb->cb must contain additional
  64. info as described in the struct ieee802154_mac_cb. During packet transmission
  65. the skb->cb is used to provide additional data to device's header_ops->create
  66. function. Be aware that this data can be overridden later (when socket code
  67. submits skb to qdisc), so if you need something from that cb later, you should
  68. store info in the skb->data on your own.
  69. To hook the MLME interface you have to populate the ml_priv field of your
  70. net_device with a pointer to struct ieee802154_mlme_ops instance. The fields
  71. assoc_req, assoc_resp, disassoc_req, start_req, and scan_req are optional.
  72. All other fields are required.
  73. SoftMAC
  74. -------
  75. The MAC is the middle layer in the IEEE 802.15.4 Linux stack. This moment it
  76. provides interface for drivers registration and management of slave interfaces.
  77. NOTE: Currently the only monitor device type is supported - it's IEEE 802.15.4
  78. stack interface for network sniffers (e.g. WireShark).
  79. This layer is going to be extended soon.
  80. See header include/net/mac802154.h and several drivers in
  81. drivers/net/ieee802154/.
  82. Fake drivers
  83. ------------
  84. In addition there is a driver available which simulates a real device with
  85. SoftMAC (fakelb - IEEE 802.15.4 loopback driver) interface. This option
  86. provides a possibility to test and debug the stack without usage of real hardware.
  87. Device drivers API
  88. ==================
  89. The include/net/mac802154.h defines following functions:
  90. .. c:function:: struct ieee802154_dev *ieee802154_alloc_device (size_t priv_size, struct ieee802154_ops *ops)
  91. Allocation of IEEE 802.15.4 compatible device.
  92. .. c:function:: void ieee802154_free_device(struct ieee802154_dev *dev)
  93. Freeing allocated device.
  94. .. c:function:: int ieee802154_register_device(struct ieee802154_dev *dev)
  95. Register PHY in the system.
  96. .. c:function:: void ieee802154_unregister_device(struct ieee802154_dev *dev)
  97. Freeing registered PHY.
  98. .. c:function:: void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
  99. Telling 802.15.4 module there is a new received frame in the skb with
  100. the RF Link Quality Indicator (LQI) from the hardware device.
  101. .. c:function:: void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb, bool ifs_handling)
  102. Telling 802.15.4 module the frame in the skb is or going to be
  103. transmitted through the hardware device
  104. The device driver must implement the following callbacks in the IEEE 802.15.4
  105. operations structure at least::
  106. struct ieee802154_ops {
  107. ...
  108. int (*start)(struct ieee802154_hw *hw);
  109. void (*stop)(struct ieee802154_hw *hw);
  110. ...
  111. int (*xmit_async)(struct ieee802154_hw *hw, struct sk_buff *skb);
  112. int (*ed)(struct ieee802154_hw *hw, u8 *level);
  113. int (*set_channel)(struct ieee802154_hw *hw, u8 page, u8 channel);
  114. ...
  115. };
  116. .. c:function:: int start(struct ieee802154_hw *hw)
  117. Handler that 802.15.4 module calls for the hardware device initialization.
  118. .. c:function:: void stop(struct ieee802154_hw *hw)
  119. Handler that 802.15.4 module calls for the hardware device cleanup.
  120. .. c:function:: int xmit_async(struct ieee802154_hw *hw, struct sk_buff *skb)
  121. Handler that 802.15.4 module calls for each frame in the skb going to be
  122. transmitted through the hardware device.
  123. .. c:function:: int ed(struct ieee802154_hw *hw, u8 *level)
  124. Handler that 802.15.4 module calls for Energy Detection from the hardware
  125. device.
  126. .. c:function:: int set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  127. Set radio for listening on specific channel of the hardware device.
  128. Moreover IEEE 802.15.4 device operations structure should be filled.