From 88e0312897115d41409521a50f4fd3a2b4dc121f Mon Sep 17 00:00:00 2001
From: Anthony Merlino <anthony@vergeaero.com>
Date: Wed, 21 Jun 2017 15:01:39 -0400
Subject: [PATCH] ieee802154: Changes to support beacon-enabled networks

---
 include/wireless/ieee802154.h                 |  2 +
 wireless/ieee802154/i8sak/i8sak_acceptassoc.c | 10 +++
 wireless/ieee802154/i8sak/i8sak_startpan.c    | 24 +++++--
 wireless/ieee802154/libmac/Makefile           |  2 +-
 .../libmac/ieee802154_setassocpermit.c        | 64 +++++++++++++++++++
 5 files changed, 97 insertions(+), 5 deletions(-)
 create mode 100644 wireless/ieee802154/libmac/ieee802154_setassocpermit.c

diff --git a/include/wireless/ieee802154.h b/include/wireless/ieee802154.h
index 7822a4e5c..276c90ad6 100644
--- a/include/wireless/ieee802154.h
+++ b/include/wireless/ieee802154.h
@@ -112,6 +112,8 @@ int ieee802154_getcca(int fd, FAR struct ieee802154_cca_s *cca);
 
 int ieee802154_getdevmode(int fd, FAR enum ieee802154_devmode_e *devmode);
 
+int ieee802154_setassocpermit(int fd, bool assocpermit);
+
 #ifdef CONFIG_NET_6LOWPAN
 /* Netork driver IOCTL helpers */
 
diff --git a/wireless/ieee802154/i8sak/i8sak_acceptassoc.c b/wireless/ieee802154/i8sak/i8sak_acceptassoc.c
index c68caa466..6a896a76b 100644
--- a/wireless/ieee802154/i8sak/i8sak_acceptassoc.c
+++ b/wireless/ieee802154/i8sak/i8sak_acceptassoc.c
@@ -76,6 +76,7 @@ void i8sak_acceptassoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]
   bool acceptall = true; /* start off assuming we are going to allow all connections */
   int option;
   int optcnt;
+  int fd;
 
   optcnt = 0;
   while ((option = getopt(argc, argv, ":he:")) != ERROR)
@@ -115,6 +116,15 @@ void i8sak_acceptassoc_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[]
         }
     }
 
+  fd = open(i8sak->devname, O_RDWR);
+  if (fd < 0)
+    {
+      printf("cannot open %s, errno=%d\n", i8sak->devname, errno);
+      i8sak_cmd_error(i8sak);
+    }
+  
+  ieee802154_setassocpermit(fd, true);
+
   if (!optcnt)
     {
       i8sak->acceptall = acceptall;
diff --git a/wireless/ieee802154/i8sak/i8sak_startpan.c b/wireless/ieee802154/i8sak/i8sak_startpan.c
index 409e83ea2..5a09a58d7 100644
--- a/wireless/ieee802154/i8sak/i8sak_startpan.c
+++ b/wireless/ieee802154/i8sak/i8sak_startpan.c
@@ -73,11 +73,12 @@ void i8sak_startpan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[])
 {
   struct ieee802154_reset_req_s resetreq;
   struct ieee802154_start_req_s startreq;
+  bool beaconenabled = false;
   int option;
   int fd;
   int i;
 
-  while ((option = getopt(argc, argv, ":h")) != ERROR)
+  while ((option = getopt(argc, argv, ":hb")) != ERROR)
     {
       switch (option)
         {
@@ -85,11 +86,15 @@ void i8sak_startpan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[])
             fprintf(stderr, "Starts PAN as PAN Coordinator\n"
                     "Usage: %s [-h]\n"
                     "    -h = this help menu\n"
+                    "    -b = start beacon-enabled PAN\n"
                     , argv[0]);
             /* Must manually reset optind if we are going to exit early */
 
             optind = -1;
             return;
+          case 'b':
+            beaconenabled = true;
+            break;
           case ':':
             fprintf(stderr, "ERROR: missing argument\n");
             /* Must manually reset optind if we are going to exit early */
@@ -184,11 +189,22 @@ void i8sak_startpan_cmd(FAR struct i8sak_s *i8sak, int argc, FAR char *argv[])
   printf("i8sak: starting PAN\n");
 
   IEEE802154_PANIDCOPY(startreq.panid, i8sak->addr.panid);
-  startreq.chnum = i8sak->chnum;
+  startreq.chnum  = i8sak->chnum;
   startreq.chpage = i8sak->chpage;
-  startreq.beaconorder = 15;
-  startreq.pancoord = true;
+
+  if (beaconenabled)
+    {
+      startreq.beaconorder = 6;
+      startreq.superframeorder = 5;
+    }
+  else
+    {
+      startreq.beaconorder = 15;
+    }
+
+  startreq.pancoord     = true;
   startreq.coordrealign = false;
+  startreq.battlifeext  = false;
 
   ieee802154_start_req(fd, &startreq);
 
diff --git a/wireless/ieee802154/libmac/Makefile b/wireless/ieee802154/libmac/Makefile
index fe4022108..6dbf07cf4 100644
--- a/wireless/ieee802154/libmac/Makefile
+++ b/wireless/ieee802154/libmac/Makefile
@@ -54,7 +54,7 @@ CSRCS += ieee802154_seteaddr.c ieee802154_geteaddr.c
 CSRCS += ieee802154_setpromisc.c ieee802154_getpromisc.c 
 CSRCS += ieee802154_setrxonidle.c ieee802154_getrxonidle.c 
 CSRCS += ieee802154_settxpwr.c ieee802154_gettxpwr.c 
-CSRCS += ieee802154_getdevmode.c 
+CSRCS += ieee802154_getdevmode.c ieee802154_setassocpermit.c
 
 ifeq ($(CONFIG_NET_6LOWPAN),y) 
 # Add Get/Set Attribute helpers
diff --git a/wireless/ieee802154/libmac/ieee802154_setassocpermit.c b/wireless/ieee802154/libmac/ieee802154_setassocpermit.c
new file mode 100644
index 000000000..9336e4699
--- /dev/null
+++ b/wireless/ieee802154/libmac/ieee802154_setassocpermit.c
@@ -0,0 +1,64 @@
+/****************************************************************************
+ * apps/wireless/ieee802154/libmac/ieee802154_assocpermit.c
+ *
+ *   Copyright (C) 2017 Verge Inc. All rights reserved.
+ *   Author: Anthony Merlino <anthony@vergeaero.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ *    used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/ioctl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <errno.h>
+
+#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
+
+#include "wireless/ieee802154.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int ieee802154_setassocpermit(int fd, bool assocpermit)
+{
+  struct ieee802154_set_req_s req;
+
+  req.attr = IEEE802154_ATTR_MAC_ASSOCIATION_PERMIT;
+  req.attrval.mac.assocpermit = assocpermit;
+
+  return ieee802154_set_req(fd, &req);
+}