net: ieee802154: Add grounds for passive scan

Currently, as only CC2520 device is supported: it will loop over the 16
channels present in 2.4Ghz spectrum. However, this will need refinement
as soon as other spectrums will be in use: L2 layer could request
capabilites from the device in order to know which range to use.

Scan result is done one at a time for now. It's unlikely going to fit
all production use case, so it's going to change in the future.

Change-Id: I2fafec49eb5cbfca1a4f04107ba3b6511d7d956d
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
This commit is contained in:
Tomasz Bursztyka 2016-10-07 10:50:21 +02:00
commit 88d4f1f543
3 changed files with 232 additions and 0 deletions

View file

@ -24,12 +24,18 @@
#include <net/net_mgmt.h>
#define IEEE802154_MAX_ADDR_LENGTH 8
/* This not meant to be used by any code but 802.15.4 L2 stack */
struct ieee802154_context {
uint16_t pan_id;
uint16_t channel;
uint8_t sequence;
struct k_sem ack_lock;
#ifdef CONFIG_NET_L2_IEEE802154_MGMT
struct ieee802154_req_params *scan_ctx;
struct k_sem res_lock;
#endif
uint8_t ack_received : 1;
uint8_t ack_requested : 1;
uint8_t _unused : 6;
@ -43,10 +49,13 @@ struct ieee802154_context {
#define _NET_IEEE802154_BASE (NET_MGMT_IFACE_BIT | \
NET_MGMT_LAYER(_NET_IEEE802154_LAYER) |\
NET_MGMT_LAYER_CODE(_NET_IEEE802154_CODE))
#define _NET_IEEE802154_EVENT (_NET_IEEE802154_BASE | NET_MGMT_EVENT_BIT)
enum net_request_ieee802154_cmd {
NET_REQUEST_IEEE802154_CMD_SET_ACK = 1,
NET_REQUEST_IEEE802154_CMD_UNSET_ACK,
NET_REQUEST_IEEE802154_CMD_PASSIVE_SCAN,
NET_REQUEST_IEEE802154_CMD_CANCEL_SCAN,
};
@ -60,4 +69,62 @@ NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_IEEE802154_SET_ACK);
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_IEEE802154_UNSET_ACK);
#define NET_REQUEST_IEEE802154_PASSIVE_SCAN \
(_NET_IEEE802154_BASE | NET_REQUEST_IEEE802154_CMD_PASSIVE_SCAN)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_IEEE802154_PASSIVE_SCAN);
#define NET_REQUEST_IEEE802154_CANCEL_SCAN \
(_NET_IEEE802154_BASE | NET_REQUEST_IEEE802154_CMD_CANCEL_SCAN)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_IEEE802154_CANCEL_SCAN);
enum net_event_ieee802154_cmd {
NET_EVENT_IEEE802154_CMD_SCAN_RESULT = 1,
};
#define NET_EVENT_IEEE802154_SCAN_RESULT \
(_NET_IEEE802154_EVENT | NET_EVENT_IEEE802154_CMD_SCAN_RESULT)
#define IEEE802154_IS_CHAN_SCANNED(_channel_set, _chan) \
(_channel_set & BIT(_chan - 1))
#define IEEE802154_IS_CHAN_UNSCANNED(_channel_set, _chan) \
(!IEEE802154_IS_CHAN_SCANNED(_channel_set, _chan))
/* Useful define to request all channels to be scanned,
* from 11 to 26 included.
*/
#define IEEE802154_ALL_CHANNELS (0x03FFFC00)
/**
* @brief Scanning parameters
*
* Used to request a scan and get results as well
*/
struct ieee802154_req_params {
/** The set of channels to scan, use above macros to manage it */
uint32_t channel_set;
/** Duration of scan, per-channel, in milliseconds */
uint32_t duration;
/** Current channel in use as a result */
uint16_t channel;
/** Current pan_id in use as a result */
uint16_t pan_id;
/** Result address */
union {
uint8_t addr[IEEE802154_MAX_ADDR_LENGTH];
uint16_t short_addr;
};
/** length of address */
uint8_t len;
/** Link quality information, between 0 and 255 */
uint8_t lqi;
} __packed;
#endif /* __IEEE802154_H__ */

View file

@ -17,8 +17,121 @@
#include <errno.h>
#include <net/net_if.h>
#include <net/ieee802154_radio.h>
#include <net/ieee802154.h>
#include "ieee802154_frame.h"
#include "ieee802154_mgmt.h"
enum net_verdict ieee802154_handle_beacon(struct net_if *iface,
struct ieee802154_mpdu *mpdu)
{
struct ieee802154_context *ctx = net_if_l2_data(iface);
struct ieee802154_radio_api *radio =
(struct ieee802154_radio_api *)iface->dev->driver_api;
if (!ctx->scan_ctx) {
return NET_DROP;
}
if (!mpdu->beacon->sf.association) {
return NET_DROP;
}
k_sem_take(&ctx->res_lock, K_FOREVER);
ctx->scan_ctx->pan_id = mpdu->mhr.src_addr->plain.pan_id;
ctx->scan_ctx->lqi = radio->get_lqi(iface->dev);
if (mpdu->mhr.fs->fc.src_addr_mode == IEEE802154_ADDR_MODE_SHORT) {
ctx->scan_ctx->len = IEEE802154_SHORT_ADDR_LENGTH;
ctx->scan_ctx->short_addr =
mpdu->mhr.src_addr->plain.addr.short_addr;
} else {
ctx->scan_ctx->len = IEEE802154_EXT_ADDR_LENGTH;
sys_memcpy_swap(ctx->scan_ctx->addr,
mpdu->mhr.src_addr->plain.addr.ext_addr,
IEEE802154_EXT_ADDR_LENGTH);
}
net_mgmt_event_notify(NET_EVENT_IEEE802154_SCAN_RESULT, iface);
k_sem_give(&ctx->res_lock);
return NET_OK;
}
static int ieee802154_cancel_scan(uint32_t mgmt_request, struct net_if *iface,
void *data, size_t len)
{
struct ieee802154_context *ctx = net_if_l2_data(iface);
ARG_UNUSED(data);
ARG_UNUSED(len);
ctx->scan_ctx = NULL;
return 0;
}
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_IEEE802154_CANCEL_SCAN,
ieee802154_cancel_scan);
static int ieee802154_scan(uint32_t mgmt_request, struct net_if *iface,
void *data, size_t len)
{
struct ieee802154_radio_api *radio =
(struct ieee802154_radio_api *)iface->dev->driver_api;
struct ieee802154_context *ctx = net_if_l2_data(iface);
struct ieee802154_req_params *scan =
(struct ieee802154_req_params *)data;
uint8_t channel;
int ret;
if (ctx->scan_ctx) {
return -EALREADY;
}
if (radio->start(iface->dev)) {
return -EIO;
}
ctx->scan_ctx = scan;
ret = 0;
radio->set_pan_id(iface->dev, IEEE802154_BROADCAST_PAN_ID);
/* ToDo: For now, we assume we are on 2.4Ghz
* (device will have to export capabilities) */
for (channel = 11; channel <= 26; channel++) {
if (IEEE802154_IS_CHAN_UNSCANNED(scan->channel_set, channel)) {
continue;
}
scan->channel = channel;
radio->set_channel(iface->dev, channel);
/* Context aware sleep */
k_sleep(scan->duration);
if (!ctx->scan_ctx) {
ret = -ECANCELED;
break;
}
}
/* Let's come back to context's settings */
radio->set_pan_id(iface->dev, ctx->pan_id);
radio->set_channel(iface->dev, ctx->channel);
ctx->scan_ctx = NULL;
return ret;
}
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_IEEE802154_PASSIVE_SCAN,
ieee802154_scan);
static int ieee802154_set_ack(uint32_t mgmt_request, struct net_if *iface,
void *data, size_t len)
{

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2016 Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* @brief IEEE 802.15.4 Management
*/
#ifndef __IEEE802154_MGMT_H__
#define __IEEE802154_MGMT_H__
#ifdef CONFIG_NET_MGMT
static inline bool ieee802154_is_scanning(struct net_if *iface)
{
struct ieee802154_context *ctx = net_if_l2_data(iface);
return !(!ctx->scan_ctx);
}
static inline void ieee802154_mgmt_init(struct net_if *iface)
{
struct ieee802154_context *ctx = net_if_l2_data(iface);
k_sem_init(&ctx->res_lock, 1, 1);
}
enum net_verdict ieee802154_handle_beacon(struct net_if *iface,
struct ieee802154_mpdu *mpdu);
#else /* CONFIG_NET_MGMT */
#define ieee802154_is_scanning(...) false
#define ieee802154_mgmt_init(...)
#define ieee802154_handle_beacon(...) NET_DROP
#endif /* CONFIG_NET_MGMT */
#endif /* __IEEE802154_MGMT_H__ */