162306a36Sopenharmony_ci===============================
262306a36Sopenharmony_ciIEEE 802.15.4 Developer's Guide
362306a36Sopenharmony_ci===============================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciIntroduction
662306a36Sopenharmony_ci============
762306a36Sopenharmony_ciThe IEEE 802.15.4 working group focuses on standardization of the bottom
862306a36Sopenharmony_citwo layers: Medium Access Control (MAC) and Physical access (PHY). And there
962306a36Sopenharmony_ciare mainly two options available for upper layers:
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci- ZigBee - proprietary protocol from the ZigBee Alliance
1262306a36Sopenharmony_ci- 6LoWPAN - IPv6 networking over low rate personal area networks
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ciThe goal of the Linux-wpan is to provide a complete implementation
1562306a36Sopenharmony_ciof the IEEE 802.15.4 and 6LoWPAN protocols. IEEE 802.15.4 is a stack
1662306a36Sopenharmony_ciof protocols for organizing Low-Rate Wireless Personal Area Networks.
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciThe stack is composed of three main parts:
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci- IEEE 802.15.4 layer;  We have chosen to use plain Berkeley socket API,
2162306a36Sopenharmony_ci  the generic Linux networking stack to transfer IEEE 802.15.4 data
2262306a36Sopenharmony_ci  messages and a special protocol over netlink for configuration/management
2362306a36Sopenharmony_ci- MAC - provides access to shared channel and reliable data delivery
2462306a36Sopenharmony_ci- PHY - represents device drivers
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciSocket API
2762306a36Sopenharmony_ci==========
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci::
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci    int sd = socket(PF_IEEE802154, SOCK_DGRAM, 0);
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciThe address family, socket addresses etc. are defined in the
3462306a36Sopenharmony_ciinclude/net/af_ieee802154.h header or in the special header
3562306a36Sopenharmony_ciin the userspace package (see either https://linux-wpan.org/wpan-tools.html
3662306a36Sopenharmony_cior the git tree at https://github.com/linux-wpan/wpan-tools).
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci6LoWPAN Linux implementation
3962306a36Sopenharmony_ci============================
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciThe IEEE 802.15.4 standard specifies an MTU of 127 bytes, yielding about 80
4262306a36Sopenharmony_cioctets of actual MAC payload once security is turned on, on a wireless link
4362306a36Sopenharmony_ciwith a link throughput of 250 kbps or less.  The 6LoWPAN adaptation format
4462306a36Sopenharmony_ci[RFC4944] was specified to carry IPv6 datagrams over such constrained links,
4562306a36Sopenharmony_citaking into account limited bandwidth, memory, or energy resources that are
4662306a36Sopenharmony_ciexpected in applications such as wireless Sensor Networks.  [RFC4944] defines
4762306a36Sopenharmony_cia Mesh Addressing header to support sub-IP forwarding, a Fragmentation header
4862306a36Sopenharmony_cito support the IPv6 minimum MTU requirement [RFC2460], and stateless header
4962306a36Sopenharmony_cicompression for IPv6 datagrams (LOWPAN_HC1 and LOWPAN_HC2) to reduce the
5062306a36Sopenharmony_cirelatively large IPv6 and UDP headers down to (in the best case) several bytes.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciIn September 2011 the standard update was published - [RFC6282].
5362306a36Sopenharmony_ciIt deprecates HC1 and HC2 compression and defines IPHC encoding format which is
5462306a36Sopenharmony_ciused in this Linux implementation.
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ciAll the code related to 6lowpan you may find in files: net/6lowpan/*
5762306a36Sopenharmony_ciand net/ieee802154/6lowpan/*
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciTo setup a 6LoWPAN interface you need:
6062306a36Sopenharmony_ci1. Add IEEE802.15.4 interface and set channel and PAN ID;
6162306a36Sopenharmony_ci2. Add 6lowpan interface by command like:
6262306a36Sopenharmony_ci# ip link add link wpan0 name lowpan0 type lowpan
6362306a36Sopenharmony_ci3. Bring up 'lowpan0' interface
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciDrivers
6662306a36Sopenharmony_ci=======
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ciLike with WiFi, there are several types of devices implementing IEEE 802.15.4.
6962306a36Sopenharmony_ci1) 'HardMAC'. The MAC layer is implemented in the device itself, the device
7062306a36Sopenharmony_ciexports a management (e.g. MLME) and data API.
7162306a36Sopenharmony_ci2) 'SoftMAC' or just radio. These types of devices are just radio transceivers
7262306a36Sopenharmony_cipossibly with some kinds of acceleration like automatic CRC computation and
7362306a36Sopenharmony_cicomparison, automagic ACK handling, address matching, etc.
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciThose types of devices require different approach to be hooked into Linux kernel.
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciHardMAC
7862306a36Sopenharmony_ci-------
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ciSee the header include/net/ieee802154_netdev.h. You have to implement Linux
8162306a36Sopenharmony_cinet_device, with .type = ARPHRD_IEEE802154. Data is exchanged with socket family
8262306a36Sopenharmony_cicode via plain sk_buffs. On skb reception skb->cb must contain additional
8362306a36Sopenharmony_ciinfo as described in the struct ieee802154_mac_cb. During packet transmission
8462306a36Sopenharmony_cithe skb->cb is used to provide additional data to device's header_ops->create
8562306a36Sopenharmony_cifunction. Be aware that this data can be overridden later (when socket code
8662306a36Sopenharmony_cisubmits skb to qdisc), so if you need something from that cb later, you should
8762306a36Sopenharmony_cistore info in the skb->data on your own.
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ciTo hook the MLME interface you have to populate the ml_priv field of your
9062306a36Sopenharmony_cinet_device with a pointer to struct ieee802154_mlme_ops instance. The fields
9162306a36Sopenharmony_ciassoc_req, assoc_resp, disassoc_req, start_req, and scan_req are optional.
9262306a36Sopenharmony_ciAll other fields are required.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciSoftMAC
9562306a36Sopenharmony_ci-------
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ciThe MAC is the middle layer in the IEEE 802.15.4 Linux stack. This moment it
9862306a36Sopenharmony_ciprovides interface for drivers registration and management of slave interfaces.
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ciNOTE: Currently the only monitor device type is supported - it's IEEE 802.15.4
10162306a36Sopenharmony_cistack interface for network sniffers (e.g. WireShark).
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciThis layer is going to be extended soon.
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciSee header include/net/mac802154.h and several drivers in
10662306a36Sopenharmony_cidrivers/net/ieee802154/.
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciFake drivers
10962306a36Sopenharmony_ci------------
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciIn addition there is a driver available which simulates a real device with
11262306a36Sopenharmony_ciSoftMAC (fakelb - IEEE 802.15.4 loopback driver) interface. This option
11362306a36Sopenharmony_ciprovides a possibility to test and debug the stack without usage of real hardware.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ciDevice drivers API
11662306a36Sopenharmony_ci==================
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciThe include/net/mac802154.h defines following functions:
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci.. c:function:: struct ieee802154_dev *ieee802154_alloc_device (size_t priv_size, struct ieee802154_ops *ops)
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ciAllocation of IEEE 802.15.4 compatible device.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci.. c:function:: void ieee802154_free_device(struct ieee802154_dev *dev)
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ciFreeing allocated device.
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci.. c:function:: int ieee802154_register_device(struct ieee802154_dev *dev)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciRegister PHY in the system.
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci.. c:function:: void ieee802154_unregister_device(struct ieee802154_dev *dev)
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciFreeing registered PHY.
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci.. c:function:: void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciTelling 802.15.4 module there is a new received frame in the skb with
13962306a36Sopenharmony_cithe RF Link Quality Indicator (LQI) from the hardware device.
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci.. c:function:: void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb, bool ifs_handling)
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciTelling 802.15.4 module the frame in the skb is or going to be
14462306a36Sopenharmony_citransmitted through the hardware device
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ciThe device driver must implement the following callbacks in the IEEE 802.15.4
14762306a36Sopenharmony_cioperations structure at least::
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci   struct ieee802154_ops {
15062306a36Sopenharmony_ci        ...
15162306a36Sopenharmony_ci        int     (*start)(struct ieee802154_hw *hw);
15262306a36Sopenharmony_ci        void    (*stop)(struct ieee802154_hw *hw);
15362306a36Sopenharmony_ci        ...
15462306a36Sopenharmony_ci        int     (*xmit_async)(struct ieee802154_hw *hw, struct sk_buff *skb);
15562306a36Sopenharmony_ci        int     (*ed)(struct ieee802154_hw *hw, u8 *level);
15662306a36Sopenharmony_ci        int     (*set_channel)(struct ieee802154_hw *hw, u8 page, u8 channel);
15762306a36Sopenharmony_ci        ...
15862306a36Sopenharmony_ci   };
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci.. c:function:: int start(struct ieee802154_hw *hw)
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciHandler that 802.15.4 module calls for the hardware device initialization.
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci.. c:function:: void stop(struct ieee802154_hw *hw)
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ciHandler that 802.15.4 module calls for the hardware device cleanup.
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci.. c:function:: int xmit_async(struct ieee802154_hw *hw, struct sk_buff *skb)
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ciHandler that 802.15.4 module calls for each frame in the skb going to be
17162306a36Sopenharmony_citransmitted through the hardware device.
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci.. c:function:: int ed(struct ieee802154_hw *hw, u8 *level)
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ciHandler that 802.15.4 module calls for Energy Detection from the hardware
17662306a36Sopenharmony_cidevice.
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci.. c:function:: int set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciSet radio for listening on specific channel of the hardware device.
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciMoreover IEEE 802.15.4 device operations structure should be filled.
183