2017-06-14 08:34:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/wireless/ieee802154/i8sak/i8sak_assoc.c
|
|
|
|
*
|
2022-03-07 15:05:57 +01: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:34:36 +02:00
|
|
|
*
|
2022-03-07 15:05:57 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-06-14 08:34:36 +02:00
|
|
|
*
|
2022-03-07 15:05:57 +01: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:34:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2017-06-29 21:30:07 +02:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2017-06-14 08:34:36 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-06-29 21:30:07 +02:00
|
|
|
#include <unistd.h>
|
2017-06-14 08:34:36 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
|
2017-09-15 16:16:47 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
#include "wireless/ieee802154.h"
|
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
#include "i8sak.h"
|
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-19 05:43:29 +02:00
|
|
|
static void assoc_eventcb(FAR struct ieee802154_primitive_s *primitive,
|
|
|
|
FAR void *arg);
|
2017-06-14 08:34:36 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name : i8sak_assoc
|
|
|
|
*
|
|
|
|
* Description :
|
|
|
|
* Request association with the Coordinator
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void i8sak_assoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
struct ieee802154_assoc_req_s assocreq;
|
2017-09-15 16:16:47 +02:00
|
|
|
struct i8sak_eventfilter_s filter;
|
2017-06-26 02:01:16 +02:00
|
|
|
FAR struct ieee802154_pandesc_s *pandesc;
|
2017-07-01 23:14:17 +02:00
|
|
|
struct ieee802154_set_req_s setreq;
|
2017-06-29 21:30:07 +02:00
|
|
|
bool retry = false;
|
|
|
|
int maxretries = 0;
|
|
|
|
int retrycnt;
|
2017-09-15 16:16:47 +02:00
|
|
|
int fd = 0;
|
2017-06-15 19:10:25 +02:00
|
|
|
int option;
|
|
|
|
int optcnt;
|
|
|
|
int ret;
|
2017-06-26 02:01:16 +02:00
|
|
|
uint8_t resindex;
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-07-01 23:14:17 +02:00
|
|
|
setreq.attr = IEEE802154_ATTR_MAC_RESPONSE_WAIT_TIME;
|
|
|
|
setreq.attrval.mac.resp_waittime = 32;
|
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
optcnt = 0;
|
2017-09-15 16:16:47 +02:00
|
|
|
while ((option = getopt(argc, argv, ":hp:e:s:w:r:t:")) != ERROR)
|
2017-06-14 08:34:36 +02:00
|
|
|
{
|
|
|
|
optcnt++;
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
fprintf(stderr, "Requests association with endpoint\n"
|
2017-06-29 21:30:07 +02:00
|
|
|
"Usage: %s [-h] [-w <count>\n"
|
2017-06-14 08:34:36 +02:00
|
|
|
" -h = this help menu\n"
|
2017-09-15 16:16:47 +02:00
|
|
|
" -p = coordinator PAN ID\n"
|
|
|
|
" -e = coordinator ext address\n"
|
|
|
|
" -s = coordinator short address\n"
|
2017-06-29 21:30:07 +02:00
|
|
|
" -w = wait and retry on failure\n"
|
2017-06-26 02:01:16 +02:00
|
|
|
" -r = use scan result index\n"
|
2017-09-15 16:16:47 +02:00
|
|
|
" -t = response wait time\n"
|
2017-06-14 08:34:36 +02:00
|
|
|
, argv[0]);
|
2017-06-15 19:10:25 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
/* Must manually reset optind if we are going to exit early */
|
|
|
|
|
|
|
|
optind = -1;
|
|
|
|
return;
|
2017-06-29 21:30:07 +02:00
|
|
|
|
2017-06-26 02:01:16 +02:00
|
|
|
case 'r':
|
|
|
|
resindex = i8sak_str2luint8(optarg);
|
|
|
|
|
|
|
|
if (resindex >= i8sak->npandesc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: missing argument\n");
|
2017-06-29 21:30:07 +02:00
|
|
|
|
2017-06-26 02:01:16 +02:00
|
|
|
/* Must manually reset optind if we are going to exit early */
|
|
|
|
|
|
|
|
optind = -1;
|
|
|
|
i8sak_cmd_error(i8sak); /* This exits for us */
|
|
|
|
}
|
|
|
|
|
|
|
|
pandesc = &i8sak->pandescs[resindex];
|
|
|
|
|
|
|
|
i8sak->chan = pandesc->chan;
|
|
|
|
i8sak->chpage = pandesc->chpage;
|
2017-09-15 16:16:47 +02:00
|
|
|
memcpy(&i8sak->ep_addr, &pandesc->coordaddr,
|
|
|
|
sizeof(struct ieee802154_addr_s));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
2020-10-19 05:43:29 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
/* Parse short address and put it into the i8sak instance */
|
|
|
|
|
|
|
|
i8sak_str2panid(optarg, i8sak->ep_addr.panid);
|
2017-06-26 02:01:16 +02:00
|
|
|
break;
|
2017-06-15 19:10:25 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
case 's':
|
2020-10-19 05:43:29 +02:00
|
|
|
|
2017-07-01 23:14:17 +02:00
|
|
|
/* Parse short address and put it into the i8sak instance */
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
i8sak_str2saddr(optarg, i8sak->ep_addr.saddr);
|
2020-10-19 05:43:29 +02:00
|
|
|
i8sak->ep_addr.mode = IEEE802154_ADDRMODE_SHORT;
|
2017-06-14 08:34:36 +02:00
|
|
|
break;
|
2017-06-15 19:10:25 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
case 'e':
|
2020-10-19 05:43:29 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
/* Parse extended address and put it into the i8sak instance */
|
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
i8sak_str2eaddr(optarg, i8sak->ep_addr.eaddr);
|
|
|
|
i8sak->ep_addr.mode = IEEE802154_ADDRMODE_EXTENDED;
|
2017-06-14 08:34:36 +02:00
|
|
|
break;
|
2017-09-15 16:16:47 +02:00
|
|
|
|
2017-07-01 23:14:17 +02:00
|
|
|
case 't':
|
2020-10-19 05:43:29 +02:00
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Parse wait time and set the parameter in the request */
|
2017-07-01 23:14:17 +02:00
|
|
|
|
|
|
|
setreq.attrval.mac.resp_waittime = i8sak_str2luint8(optarg);
|
|
|
|
break;
|
2017-06-15 19:10:25 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
case 'w':
|
2020-10-19 05:43:29 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
/* Wait and retry if we fail to associate */
|
|
|
|
|
|
|
|
retry = true;
|
|
|
|
maxretries = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "ERROR: missing argument\n");
|
2017-06-29 21:30:07 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
/* Must manually reset optind if we are going to exit early */
|
|
|
|
|
|
|
|
optind = -1;
|
|
|
|
i8sak_cmd_error(i8sak); /* This exits for us */
|
2017-06-15 19:10:25 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "ERROR: unknown argument\n");
|
2017-06-29 21:30:07 +02:00
|
|
|
|
2017-06-14 08:34:36 +02:00
|
|
|
/* Must manually reset optind if we are going to exit early */
|
|
|
|
|
|
|
|
optind = -1;
|
|
|
|
i8sak_cmd_error(i8sak); /* This exits for us */
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 18:51:25 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
if (i8sak->mode == I8SAK_MODE_CHAR)
|
2017-06-14 08:34:36 +02:00
|
|
|
{
|
2017-09-15 16:16:47 +02:00
|
|
|
fd = open(i8sak->ifname, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2020-10-19 05:43:29 +02:00
|
|
|
fprintf(stderr, "ERROR: cannot open %s, errno=%d\n",
|
|
|
|
i8sak->ifname, errno);
|
2017-09-15 16:16:47 +02:00
|
|
|
i8sak_cmd_error(i8sak);
|
|
|
|
}
|
2017-06-15 18:51:25 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
ieee802154_set_req(fd, &setreq);
|
2017-06-14 08:34:36 +02:00
|
|
|
}
|
2017-09-15 16:16:47 +02:00
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
else if (i8sak->mode == I8SAK_MODE_NETIF)
|
|
|
|
{
|
|
|
|
fd = socket(PF_INET6, SOCK_DGRAM, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: failed to open socket, errno=%d\n", errno);
|
|
|
|
i8sak_cmd_error(i8sak);
|
|
|
|
}
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
sixlowpan_set_req(fd, i8sak->ifname, &setreq);
|
|
|
|
}
|
|
|
|
#endif
|
2017-07-01 23:14:17 +02:00
|
|
|
|
2018-08-19 20:47:40 +02:00
|
|
|
i8sak_requestdaemon(i8sak);
|
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
/* Register new callback for receiving the association notifications. */
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
memset(&filter, 0, sizeof(struct i8sak_eventfilter_s));
|
2017-06-14 08:34:36 +02:00
|
|
|
filter.confevents.assoc = true;
|
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
i8sak_eventlistener_addreceiver(i8sak, assoc_eventcb, &filter, false);
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
/* Loop for the specified retry count if the association fails.
|
|
|
|
*/
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
retrycnt = 0;
|
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
printf("i8sak: issuing ASSOC. request %d\n", retrycnt + 1);
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
/* Issue association request */
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
assocreq.chan = i8sak->chan;
|
|
|
|
assocreq.chpage = i8sak->chpage;
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
memcpy(&assocreq.coordaddr, &i8sak->ep_addr,
|
2017-06-29 21:30:07 +02:00
|
|
|
sizeof(struct ieee802154_addr_s));
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
assocreq.capabilities.devtype = 0;
|
|
|
|
assocreq.capabilities.powersource = 1;
|
|
|
|
assocreq.capabilities.rxonidle = 1;
|
|
|
|
assocreq.capabilities.security = 0;
|
|
|
|
assocreq.capabilities.allocaddr = 1;
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-09-15 16:16:47 +02:00
|
|
|
if (i8sak->mode == I8SAK_MODE_CHAR)
|
|
|
|
{
|
|
|
|
ieee802154_assoc_req(fd, &assocreq);
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
else if (i8sak->mode == I8SAK_MODE_NETIF)
|
|
|
|
{
|
|
|
|
sixlowpan_assoc_req(fd, i8sak->ifname, &assocreq);
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
/* Wait for the assocconf event */
|
2017-06-14 08:34:36 +02:00
|
|
|
|
2017-06-29 21:30:07 +02:00
|
|
|
i8sak->assoc = true;
|
|
|
|
i8sak->result = -EBUSY;
|
|
|
|
|
|
|
|
ret = sem_wait(&i8sak->sigsem);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
i8sak->assoc = false;
|
|
|
|
i8sak_cmd_error(i8sak);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the association was successful */
|
|
|
|
|
|
|
|
if (i8sak->result == OK)
|
|
|
|
{
|
|
|
|
/* Break out and return if the association was successful. */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not successful .. were we asked to retry in the event of an
|
|
|
|
* association failure?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!retry)
|
|
|
|
{
|
|
|
|
/* No retries... break out now with the failed association */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A retry count of zero means to retry forever. Otherwise,
|
|
|
|
* abort as soon as the maximum number of retries have been
|
|
|
|
* performed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (maxretries > 0 && retrycnt >= maxretries)
|
|
|
|
{
|
|
|
|
/* Max retry count exceeded -- break out now with a failed
|
|
|
|
* association.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, wait a bit and try again */
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
retrycnt++;
|
2017-06-14 08:34:36 +02:00
|
|
|
}
|
2017-06-29 21:30:07 +02:00
|
|
|
|
|
|
|
/* Clean up and return */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
i8sak_eventlistener_removereceiver(i8sak, assoc_eventcb);
|
2018-08-19 20:47:40 +02:00
|
|
|
i8sak_releasedaemon(i8sak);
|
2017-06-29 21:30:07 +02:00
|
|
|
close(fd);
|
2017-06-14 08:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-19 05:43:29 +02:00
|
|
|
static void assoc_eventcb(FAR struct ieee802154_primitive_s *primitive,
|
|
|
|
FAR void *arg)
|
2017-06-14 08:34:36 +02:00
|
|
|
{
|
|
|
|
FAR struct i8sak_s *i8sak = (FAR struct i8sak_s *)arg;
|
|
|
|
|
2017-11-01 21:32:29 +01:00
|
|
|
if (primitive->u.assocconf.status == IEEE802154_STATUS_SUCCESS)
|
2017-06-14 08:34:36 +02:00
|
|
|
{
|
|
|
|
printf("i8sak: ASSOC.request succeeded\n");
|
2017-06-29 21:30:07 +02:00
|
|
|
i8sak->result = OK;
|
2017-06-14 08:34:36 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-15 18:51:25 +02:00
|
|
|
printf("i8sak: ASSOC.request failed: %s\n",
|
2017-11-01 21:32:29 +01:00
|
|
|
IEEE802154_STATUS_STRING[primitive->u.assocconf.status]);
|
2017-06-29 21:30:07 +02:00
|
|
|
i8sak->result = -EAGAIN;
|
2017-06-14 08:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i8sak->assoc)
|
|
|
|
{
|
|
|
|
i8sak->assoc = false;
|
2017-06-29 21:30:07 +02:00
|
|
|
sem_post(&i8sak->sigsem);
|
2017-06-14 08:34:36 +02:00
|
|
|
}
|
|
|
|
}
|