2017-06-14 08:49:27 +02:00
|
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
|
* wireless/ieee802154/mac802154_scan.c
|
2017-06-14 08:49:27 +02:00
|
|
|
|
*
|
2021-09-06 13:39:36 +02:00
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
|
* ASF licenses this file to you 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
|
2017-06-14 08:49:27 +02:00
|
|
|
|
*
|
2021-09-06 13:39:36 +02:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-06-14 08:49:27 +02:00
|
|
|
|
*
|
2021-09-06 13:39:36 +02:00
|
|
|
|
* 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.
|
2017-06-14 08:49:27 +02:00
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "mac802154.h"
|
2017-06-26 00:07:50 +02:00
|
|
|
|
#include "mac802154_internal.h"
|
|
|
|
|
#include "mac802154_scan.h"
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
|
|
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
|
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Function Prototypes
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-07-10 05:55:48 +02:00
|
|
|
|
static void mac802154_scantimeout(FAR void *arg);
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2017-06-14 08:49:27 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public MAC Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: mac802154_req_scan
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2020-04-23 11:51:21 +02:00
|
|
|
|
* The MLME-SCAN.request primitive is used to initiate a channel scan over
|
|
|
|
|
* a given list of channels. A device can use a channel scan to measure the
|
|
|
|
|
* energy on the channel, search for the coordinator with which it
|
|
|
|
|
* associated, or search for all coordinators transmitting beacon frames
|
|
|
|
|
* within the POS of the scanning device. Scan results are returned
|
2017-06-14 08:49:27 +02:00
|
|
|
|
* via MULTIPLE calls to the struct mac802154_maccb_s->conf_scan callback.
|
2020-04-23 11:51:21 +02:00
|
|
|
|
* This is a difference with the official 802.15.4 specification,
|
|
|
|
|
* implemented here to save memory.
|
2017-06-14 08:49:27 +02:00
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req)
|
|
|
|
|
{
|
|
|
|
|
FAR struct ieee802154_privmac_s *priv =
|
|
|
|
|
(FAR struct ieee802154_privmac_s *)mac;
|
2017-06-22 06:45:34 +02:00
|
|
|
|
int ret;
|
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
if (req->duration > 15 || req->numchan < 0 || req->numchan > 15)
|
2017-06-22 06:45:34 +02:00
|
|
|
|
{
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
goto errout;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 07:31:01 +02:00
|
|
|
|
wlinfo("MLME: SCAN.request received\n");
|
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
|
/* Need to get access to the ops semaphore since operations are serial.
|
|
|
|
|
* This must be done before locking the MAC so that we don't hold the MAC
|
2017-06-22 06:45:34 +02:00
|
|
|
|
*/
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxsem_wait_uninterruptible(&priv->opsem);
|
2017-06-22 06:45:34 +02:00
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
goto errout;
|
|
|
|
|
}
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
|
|
|
|
priv->curr_op = MAC802154_OP_SCAN;
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/* Get exclusive access to the MAC */
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2017-06-22 06:45:34 +02:00
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxsem_post(&priv->opsem);
|
2017-06-22 06:45:34 +02:00
|
|
|
|
goto errout;
|
|
|
|
|
}
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/* Copy the request so we have a reference */
|
2017-06-22 06:45:34 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
memcpy(&priv->currscan, req, sizeof(struct ieee802154_scan_req_s));
|
|
|
|
|
priv->scanindex = 0;
|
|
|
|
|
priv->npandesc = 0;
|
2017-06-22 06:45:34 +02:00
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
|
priv->scansymdur = IEEE802154_BASE_SUPERFRAME_DURATION *
|
|
|
|
|
((1 << req->duration) + 1);
|
2019-08-10 19:03:24 +02:00
|
|
|
|
|
2017-06-22 06:45:34 +02:00
|
|
|
|
switch (req->type)
|
|
|
|
|
{
|
|
|
|
|
case IEEE802154_SCANTYPE_PASSIVE:
|
|
|
|
|
{
|
2019-08-10 19:03:24 +02:00
|
|
|
|
wlinfo("MLME: Starting Passive Scan\n");
|
2017-06-26 07:31:01 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/* Set the channel to the first channel in the list */
|
|
|
|
|
|
|
|
|
|
mac802154_setchannel(priv, req->channels[priv->scanindex]);
|
|
|
|
|
mac802154_setchpage(priv, req->chpage);
|
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
|
/* Before commencing an active or passive scan, the MAC sublayer
|
|
|
|
|
* shall store the value of macPANId and then set it to 0xffff for
|
|
|
|
|
* the duration of the scan. This enables the receive filter to
|
|
|
|
|
* accept all beacons rather than just the beacons from its current
|
|
|
|
|
* PAN, as described in 5.1.6.2. On completion of the scan, the MAC
|
|
|
|
|
* sublayer shall restore the value of macPANId to the value stored
|
|
|
|
|
* before the scan began. [1] pg. 24
|
2017-06-26 00:07:50 +02:00
|
|
|
|
*/
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
IEEE802154_PANIDCOPY(priv->panidbeforescan, priv->addr.panid);
|
2020-04-23 11:51:21 +02:00
|
|
|
|
mac802154_setpanid(priv,
|
|
|
|
|
(const uint8_t *) & IEEE802154_PANID_UNSPEC);
|
2017-06-22 06:45:34 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/* ...after switching to the channel for a passive scan, the device
|
|
|
|
|
* shall enable its receiver for at most
|
|
|
|
|
* [aBaseSuperframeDuration × (2 * n + 1)],
|
|
|
|
|
* where n is the value of the ScanDuration parameter. [1] pg. 25
|
|
|
|
|
*/
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
mac802154_rxenable(priv);
|
2020-04-23 11:51:21 +02:00
|
|
|
|
mac802154_timerstart(priv,
|
|
|
|
|
priv->scansymdur,
|
|
|
|
|
mac802154_scantimeout);
|
2017-06-22 06:45:34 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case IEEE802154_SCANTYPE_ACTIVE:
|
|
|
|
|
{
|
|
|
|
|
ret = -ENOTTY;
|
2022-09-06 08:18:45 +02:00
|
|
|
|
goto errout_with_lock;
|
2017-06-22 06:45:34 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case IEEE802154_SCANTYPE_ED:
|
|
|
|
|
{
|
2019-08-10 19:03:24 +02:00
|
|
|
|
wlinfo("MLME: Starting Energy Scan\n");
|
|
|
|
|
|
|
|
|
|
/* Set the channel to the first channel in the list, and trigger an
|
|
|
|
|
* energy detect operation with the radio layer.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
mac802154_setchpage(priv, req->chpage);
|
|
|
|
|
mac802154_setchannel(priv, req->channels[priv->scanindex]);
|
|
|
|
|
priv->radio->energydetect(priv->radio, priv->scansymdur);
|
2017-06-22 06:45:34 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case IEEE802154_SCANTYPE_ORPHAN:
|
|
|
|
|
{
|
|
|
|
|
ret = -ENOTTY;
|
2022-09-06 08:18:45 +02:00
|
|
|
|
goto errout_with_lock;
|
2017-06-22 06:45:34 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
ret = -EINVAL;
|
2022-09-06 08:18:45 +02:00
|
|
|
|
goto errout_with_lock;
|
2017-06-22 06:45:34 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&priv->lock);
|
2020-04-23 11:51:21 +02:00
|
|
|
|
return OK;
|
2017-06-22 06:45:34 +02:00
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
errout_with_lock:
|
|
|
|
|
nxmutex_unlock(&priv->lock);
|
|
|
|
|
nxsem_post(&priv->opsem);
|
2017-06-22 06:45:34 +02:00
|
|
|
|
errout:
|
|
|
|
|
return ret;
|
2017-06-14 08:49:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Internal MAC Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void mac802154_scanfinish(FAR struct ieee802154_privmac_s *priv,
|
|
|
|
|
enum ieee802154_status_e status)
|
|
|
|
|
{
|
2017-11-01 21:15:21 +01:00
|
|
|
|
FAR struct ieee802154_primitive_s * primitive;
|
|
|
|
|
FAR struct ieee802154_scan_conf_s *scanconf;
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2017-11-01 21:15:21 +01:00
|
|
|
|
primitive = ieee802154_primitive_allocate();
|
|
|
|
|
scanconf = &primitive->u.scanconf;
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2017-11-01 21:15:21 +01:00
|
|
|
|
primitive->type = IEEE802154_PRIMITIVE_CONF_SCAN;
|
|
|
|
|
scanconf->type = priv->currscan.type;
|
|
|
|
|
scanconf->chpage = priv->currscan.chpage;
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2019-08-10 19:03:24 +02:00
|
|
|
|
if (priv->currscan.type == IEEE802154_SCANTYPE_ED)
|
2017-06-26 00:07:50 +02:00
|
|
|
|
{
|
2020-04-23 11:51:21 +02:00
|
|
|
|
/* "The list of energy measurements, one for each channel searched
|
|
|
|
|
* during an ED scan. This parameter is null for active, passive,
|
|
|
|
|
* and orphan scans." [1]
|
2019-08-10 19:03:24 +02:00
|
|
|
|
*/
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
|
memcpy(scanconf->edlist,
|
|
|
|
|
priv->edlist,
|
|
|
|
|
sizeof(scanconf->edlist));
|
|
|
|
|
memcpy(scanconf->chlist,
|
|
|
|
|
priv->currscan.channels,
|
|
|
|
|
sizeof(scanconf->chlist));
|
2019-08-10 19:03:24 +02:00
|
|
|
|
scanconf->numresults = priv->currscan.numchan;
|
|
|
|
|
}
|
2017-11-01 21:15:21 +01:00
|
|
|
|
|
2019-08-10 19:03:24 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-23 11:51:21 +02:00
|
|
|
|
/* "A list of the channels given in the request which were not
|
|
|
|
|
* scanned. This parameter is not valid for ED scans." [1]
|
|
|
|
|
*/
|
2017-11-01 21:15:21 +01:00
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
|
scanconf->numunscanned = priv->currscan.numchan - priv->scanindex;
|
|
|
|
|
if (scanconf->numunscanned)
|
2019-08-10 19:03:24 +02:00
|
|
|
|
{
|
|
|
|
|
memcpy(scanconf->chlist, &priv->currscan.channels[priv->scanindex],
|
|
|
|
|
scanconf->numunscanned);
|
|
|
|
|
}
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
|
/* "The list of PAN descriptors, one for each beacon found during an
|
|
|
|
|
* active or passive scan if macAutoRequest is set to TRUE. This
|
|
|
|
|
* parameter is null for ED and orphan scans or when macAutoRequest
|
|
|
|
|
* is set to FALSE during an active or passive scan." [1]
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (priv->currscan.type != IEEE802154_SCANTYPE_ORPHAN && priv->autoreq)
|
|
|
|
|
{
|
|
|
|
|
memcpy(scanconf->pandescs, priv->pandescs,
|
|
|
|
|
sizeof(struct ieee802154_pandesc_s) * priv->npandesc);
|
|
|
|
|
scanconf->numresults = priv->npandesc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (priv->currscan.type == IEEE802154_SCANTYPE_PASSIVE)
|
|
|
|
|
{
|
|
|
|
|
/* Reset the PAN ID to the setting before the scan started */
|
|
|
|
|
|
|
|
|
|
mac802154_setpanid(priv, priv->panidbeforescan);
|
|
|
|
|
}
|
2019-08-10 19:03:24 +02:00
|
|
|
|
}
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2019-08-10 19:03:24 +02:00
|
|
|
|
scanconf->status = status;
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2017-11-01 21:15:21 +01:00
|
|
|
|
priv->curr_op = MAC802154_OP_NONE;
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxsem_post(&priv->opsem);
|
2017-06-26 00:07:50 +02:00
|
|
|
|
|
2017-11-01 21:15:21 +01:00
|
|
|
|
mac802154_notify(priv, primitive);
|
2017-06-26 00:07:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 19:03:24 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: mac802154_edscan_onresult
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2020-04-23 11:51:21 +02:00
|
|
|
|
* Function indirectly called from the radio layer via the radiocb
|
|
|
|
|
* edresult() call.
|
2019-08-10 19:03:24 +02:00
|
|
|
|
*
|
|
|
|
|
* Assumptions:
|
|
|
|
|
* Called with the priv mac struct locked
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
|
void mac802154_edscan_onresult(FAR struct ieee802154_privmac_s *priv,
|
|
|
|
|
uint8_t edval)
|
2019-08-10 19:03:24 +02:00
|
|
|
|
{
|
|
|
|
|
DEBUGASSERT(priv->curr_op == MAC802154_OP_SCAN &&
|
|
|
|
|
priv->currscan.type == IEEE802154_SCANTYPE_ED);
|
|
|
|
|
|
|
|
|
|
/* Copy the energy value into our local list */
|
|
|
|
|
|
|
|
|
|
priv->edlist[priv->scanindex] = edval;
|
|
|
|
|
|
|
|
|
|
/* If we got here it means we are done scanning that channel */
|
|
|
|
|
|
|
|
|
|
priv->scanindex++;
|
|
|
|
|
|
|
|
|
|
/* Check to see if this was the last channel to scan */
|
|
|
|
|
|
|
|
|
|
if (priv->scanindex == priv->currscan.numchan)
|
|
|
|
|
{
|
|
|
|
|
mac802154_scanfinish(priv, IEEE802154_STATUS_SUCCESS);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Continue on with the next channel in the list */
|
|
|
|
|
|
|
|
|
|
mac802154_setchannel(priv, priv->currscan.channels[priv->scanindex]);
|
|
|
|
|
|
|
|
|
|
/* ...after switching to the channel for a passive scan, the device
|
|
|
|
|
* shall enable its receiver for at most
|
|
|
|
|
* [aBaseSuperframeDuration × (2 * n + 1)],
|
|
|
|
|
* where n is the value of the ScanDuration parameter. [1] pg. 25
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
priv->radio->energydetect(priv->radio, priv->scansymdur);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: mac802154_scantimeout
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
2020-04-23 11:51:21 +02:00
|
|
|
|
* Function registered with MAC timer that gets called via the work queue
|
|
|
|
|
* to handle a timeout for performing a scan operation.
|
2017-06-26 00:07:50 +02:00
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-07-10 05:55:48 +02:00
|
|
|
|
static void mac802154_scantimeout(FAR void *arg)
|
2017-06-26 00:07:50 +02:00
|
|
|
|
{
|
2020-04-23 11:51:21 +02:00
|
|
|
|
FAR struct ieee802154_privmac_s *priv =
|
|
|
|
|
(FAR struct ieee802154_privmac_s *)arg;
|
2017-06-26 00:07:50 +02:00
|
|
|
|
DEBUGASSERT(priv->curr_op == MAC802154_OP_SCAN);
|
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_lock(&priv->lock);
|
2017-11-01 21:15:21 +01:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
/* If we got here it means we are done scanning that channel */
|
|
|
|
|
|
|
|
|
|
mac802154_rxdisable(priv);
|
|
|
|
|
priv->scanindex++;
|
|
|
|
|
|
|
|
|
|
/* Check to see if this was the last channel to scan */
|
|
|
|
|
|
|
|
|
|
if (priv->scanindex == priv->currscan.numchan)
|
|
|
|
|
{
|
|
|
|
|
if (priv->npandesc > 0)
|
|
|
|
|
{
|
|
|
|
|
mac802154_scanfinish(priv, IEEE802154_STATUS_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mac802154_scanfinish(priv, IEEE802154_STATUS_NO_BEACON);
|
|
|
|
|
}
|
2020-04-23 11:51:21 +02:00
|
|
|
|
|
2022-11-03 11:26:00 +01:00
|
|
|
|
nxmutex_unlock(&priv->lock);
|
2017-06-26 00:07:50 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
mac802154_setchannel(priv, priv->currscan.channels[priv->scanindex]);
|
|
|
|
|
|
|
|
|
|
/* ...after switching to the channel for a passive scan, the device
|
|
|
|
|
* shall enable its receiver for at most
|
|
|
|
|
* [aBaseSuperframeDuration × (2 * n + 1)],
|
|
|
|
|
* where n is the value of the ScanDuration parameter. [1] pg. 25
|
|
|
|
|
*/
|
2017-06-26 19:03:44 +02:00
|
|
|
|
|
2017-06-26 00:07:50 +02:00
|
|
|
|
mac802154_rxenable(priv);
|
|
|
|
|
mac802154_timerstart(priv, priv->scansymdur, mac802154_scantimeout);
|
2022-09-06 08:18:45 +02:00
|
|
|
|
nxmutex_unlock(&priv->lock);
|
2017-06-26 00:07:50 +02:00
|
|
|
|
}
|