diff --git a/include/wireless/ieee802154.h b/include/wireless/ieee802154.h index daf66f3f1..028db3166 100644 --- a/include/wireless/ieee802154.h +++ b/include/wireless/ieee802154.h @@ -100,6 +100,9 @@ int ieee802154_geteaddr(int fd, FAR uint8_t *eaddr); int ieee802154_setpromisc(int fd, bool promisc); int ieee802154_getpromisc(int fd, FAR bool *promisc); +int ieee802154_setrxonidle(int fd, bool rxonidle); +int ieee802154_getrxonidle(int fd, FAR bool *rxonidle); + int ieee802154_settxpwr(int fd, int32_t txpwr); int ieee802154_gettxpwr(int fd, FAR int32_t *txpwr); @@ -168,22 +171,14 @@ int sixlowpan_setpromisc(int sock, FAR const char *ifname, bool promisc); int sixlowpan_getpromisc(int sock, FAR const char *ifname, FAR bool *promisc); -int sixlowpan_setdevmode(int sock, FAR const char *ifname, uint8_t devmode); -int sixlowpan_getdevmode(int sock, FAR const char *ifname, - FAR uint8_t *devmode); +int sixlowpan_setrxonidle(int sock, FAR const char *ifname, bool rxonidle); +int sixlowpan_getrxonidle(int sock, FAR const char *ifname, + FAR bool *rxonidle); int sixlowpan_settxpwr(int sock, FAR const char *ifname, int32_t txpwr); int sixlowpan_gettxpwr(int sock, FAR const char *ifname, FAR int32_t *txpwr); -int sixlowpan_setcca(int sock, FAR const char *ifname, - FAR struct ieee802154_cca_s *cca); -int sixlowpan_getcca(int sock, FAR const char *ifname, - FAR struct ieee802154_cca_s *cca); - -int sixlowpan_energydetect(int sock, FAR const char *ifname, - FAR bool *energy); - #endif /* CONFIG_NET_6LOWPAN*/ /* libutils *****************************************************************/ diff --git a/wireless/ieee802154/i8sak/Kconfig b/wireless/ieee802154/i8sak/Kconfig index 4cab0b542..a16865092 100644 --- a/wireless/ieee802154/i8sak/Kconfig +++ b/wireless/ieee802154/i8sak/Kconfig @@ -20,5 +20,13 @@ config IEEE802154_I8SAK_PROGNAME This is the name of the program that will be use when the NSH ELF program is installed. +config IEEE802154_I8SAK_PRIORITY + int "i8sak task priority" + default 100 + +config IEEE802154_I8SAK_STACKSIZE + int "i8sak stack size" + default 2048 + endif diff --git a/wireless/ieee802154/i8sak/i8sak_main.c b/wireless/ieee802154/i8sak/i8sak_main.c index c1cfe05b3..d6931abf0 100644 --- a/wireless/ieee802154/i8sak/i8sak_main.c +++ b/wireless/ieee802154/i8sak/i8sak_main.c @@ -73,7 +73,8 @@ * Private Function Prototypes ****************************************************************************/ -static int tx(int fd, FAR const char *str, int verbose); +static int tx(FAR const char *devname, FAR const char *str, int verbose); +static int start_sniffer_daemon(FAR const char *devname); /**************************************************************************** * Private Data @@ -81,6 +82,9 @@ static int tx(int fd, FAR const char *str, int verbose); uint8_t g_handle = 0; uint8_t g_txframe[IEEE802154_MAX_MAC_PAYLOAD_SIZE]; +static int sniffer_daemon(int argc, FAR char *argv[]); + +bool g_sniffer_daemon_started = false; /**************************************************************************** * Public Data @@ -90,6 +94,112 @@ uint8_t g_txframe[IEEE802154_MAX_MAC_PAYLOAD_SIZE]; * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name : start_sniff + * + * Description : + * Starts a thread to run the sniffer in the background + ****************************************************************************/ + +static int start_sniffer_daemon(FAR const char *devname) +{ + int ret; + FAR const char *sniffer_argv[2]; + + printf("i8sak: Starting sniffer_daemon\n"); + + if (g_sniffer_daemon_started) + { + printf("i8sak: sniffer_daemon already running\n"); + return EXIT_SUCCESS; + } + + sniffer_argv[0] = devname; + sniffer_argv[1] = NULL; + + ret = task_create("sniffer_daemon", CONFIG_IEEE802154_I8SAK_PRIORITY, + CONFIG_IEEE802154_I8SAK_STACKSIZE, sniffer_daemon, + (FAR char * const *)sniffer_argv); + if (ret < 0) + { + int errcode = errno; + printf("i8sak: ERROR: Failed to start sniffer_daemon: %d\n", + errcode); + return EXIT_FAILURE; + } + + g_sniffer_daemon_started = true; + printf("i8sak: sniffer_daemon started\n"); + + return OK; +} + +/**************************************************************************** + * Name : sniffer_daemon + * + * Description : + * Sniff for frames (Promiscuous mode) + ****************************************************************************/ + +static int sniffer_daemon(int argc, FAR char *argv[]) +{ + int ret, fd, i; + struct mac802154dev_rxframe_s rx; + + fd = open(argv[1], O_RDWR); + if (fd < 0) + { + printf("cannot open %s, errno=%d\n", argv[1], errno); + ret = errno; + return ret; + } + + printf("Listening...\n"); + + /* Enable promiscuous mode */ + + ret = ieee802154_setpromisc(fd, true); + + /* Make sure receiver is always on while idle */ + + ret = ieee802154_setrxonidle(fd, true); + + while(1) + { + ret = read(fd, &rx, sizeof(struct mac802154dev_rxframe_s)); + if (ret < 0) + { + printf("sniffer_daemon: read failed: %d\n", errno); + goto errout; + } + + printf("Frame Received:\n"); + + for (i = 0; i < rx.length; i++) + { + printf("%02X", rx.payload[i]); + } + + printf("\n"); + + fflush(stdout); + } + +errout: + /* Turn receiver off when idle */ + + ret = ieee802154_setrxonidle(fd, false); + + /* Disable promiscuous mode */ + + ret = ieee802154_setpromisc(fd, false); + + printf("sniffer_daemon: closing"); + close(fd); + g_sniffer_daemon_started = false; + return OK; +} + /**************************************************************************** * Name : tx * @@ -97,12 +207,22 @@ uint8_t g_txframe[IEEE802154_MAX_MAC_PAYLOAD_SIZE]; * Transmit a data frame. ****************************************************************************/ -static int tx(int fd, FAR const char *str, int verbose) +static int tx(FAR const char *devname, FAR const char *str, int verbose) { struct mac802154dev_txframe_s tx; - int ret, str_len; + int ret, str_len, fd; int i = 0; + /* Open device */ + + fd = open(devname, O_RDWR); + if (fd < 0) + { + printf("cannot open %s, errno=%d\n", devname, errno); + ret = errno; + return ret; + } + /* Set an application defined handle */ tx.meta.msdu_handle = g_handle++; @@ -116,6 +236,8 @@ static int tx(int fd, FAR const char *str, int verbose) tx.meta.ranging = IEEE802154_NON_RANGING; tx.meta.src_addr_mode = IEEE802154_ADDRMODE_EXTENDED; + tx.meta.dest_addr.mode = IEEE802154_ADDRMODE_SHORT; + tx.meta.dest_addr.saddr = 0xFADE; str_len = strlen(str); @@ -128,7 +250,7 @@ static int tx(int fd, FAR const char *str, int verbose) if ((str_len & 1) || (tx.length > IEEE802154_MAX_MAC_PAYLOAD_SIZE)) { - goto data_error; + goto error; } /* Decode hex packet */ @@ -144,7 +266,7 @@ static int tx(int fd, FAR const char *str, int verbose) } else { - goto data_error; + goto error; } } @@ -173,11 +295,13 @@ static int tx(int fd, FAR const char *str, int verbose) printf(" write: errno=%d\n",errno); } -return ret; + close(fd); + return ret; -data_error: - printf("data error\n"); - return ERROR;; +error: + printf("data error\n"); + close(fd); + return ERROR; } /**************************************************************************** @@ -190,7 +314,8 @@ data_error: int usage(void) { - printf("i8 []\n" + printf("i8 []\n" + " sniffer Listen for packets\n" " tx Transmit a data frame\n" ); @@ -207,48 +332,33 @@ int main(int argc, FAR char *argv[]) int i8_main(int argc, char *argv[]) #endif { - unsigned long arg = 0; - int fd; + FAR const char *devname; + FAR const char *cmdname; int ret = OK; if (argc < 3) { + printf("ERROR: Missing argument\n"); return usage(); } - if (argc >= 4) - { - arg = atol(argv[3]); - } - - /* Open device */ - - fd = open(argv[1], O_RDWR); - if (fd < 0) - { - printf("cannot open %s, errno=%d\n", argv[1], errno); - ret = errno; - goto exit; - } + devname = argv[1]; + cmdname = argv[2]; /* Get mode */ - if (!strcmp(argv[2], "tx")) + if (!strcmp(cmdname, "tx")) { - ret = tx(fd, argv[3], TRUE); - if (ret < 0) - { - goto error; - } + ret = tx(devname, argv[3], TRUE); + } + else if(!strcmp(argv[2], "sniffer")) + { + ret = start_sniffer_daemon(devname); } else { -usage: ret = usage(); } -error: - close(fd); -exit: return ret; } diff --git a/wireless/ieee802154/libmac/Makefile b/wireless/ieee802154/libmac/Makefile index bc8fc6b54..3f449716c 100644 --- a/wireless/ieee802154/libmac/Makefile +++ b/wireless/ieee802154/libmac/Makefile @@ -47,11 +47,12 @@ CSRCS += ieee802154_resetreq.c ieee802154_rxenabreq.c ieee802154_scanreq.c CSRCS += ieee802154_setreq.c ieee802154_startreq.c ieee802154_syncreq.c CSRCS += ieee802154_pollreq.c # Add Get/Set Attribute helpers -CSRCS = ieee802154_setchan.c ieee802154_getchan.c +CSRCS += ieee802154_setchan.c ieee802154_getchan.c CSRCS += ieee802154_setpanid.c ieee802154_getpanid.c CSRCS += ieee802154_setsaddr.c ieee802154_getsaddr.c 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 ifeq ($(CONFIG_NET_6LOWPAN),y) @@ -67,6 +68,7 @@ CSRCS += sixlowpan_setpanid.c sixlowpan_getpanid.c CSRCS += sixlowpan_setsaddr.c sixlowpan_getsaddr.c CSRCS += sixlowpan_seteaddr.c sixlowpan_geteaddr.c CSRCS += sixlowpan_setpromisc.c sixlowpan_getpromisc.c +CSRCS += sixlowpan_setrxonidle.c sixlowpan_getrxonidle.c CSRCS += sixlowpan_settxpwr.c sixlowpan_gettxpwr.c endif diff --git a/wireless/ieee802154/libmac/ieee802154_getpromisc.c b/wireless/ieee802154/libmac/ieee802154_getpromisc.c index bf97d6ea2..3e144fa63 100644 --- a/wireless/ieee802154/libmac/ieee802154_getpromisc.c +++ b/wireless/ieee802154/libmac/ieee802154_getpromisc.c @@ -63,5 +63,4 @@ int ieee802154_getpromisc(int fd, FAR bool *promisc) *promisc = req.attr_value.mac.promisc_mode; return ret; - } diff --git a/wireless/ieee802154/libmac/ieee802154_getrxonidle.c b/wireless/ieee802154/libmac/ieee802154_getrxonidle.c new file mode 100644 index 000000000..f80c1f460 --- /dev/null +++ b/wireless/ieee802154/libmac/ieee802154_getrxonidle.c @@ -0,0 +1,67 @@ +/**************************************************************************** + * apps/wireless/ieee802154/libmac/ieee802154_getrxonidle.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Author: Anthony Merlino + * + * 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 + +#include +#include +#include +#include + +#include + +#include "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int ieee802154_getrxonidle(int fd, FAR bool *rxonidle) +{ + struct ieee802154_get_req_s req; + int ret; + + req.pib_attr = IEEE802154_PIB_MAC_RX_ON_WHEN_IDLE; + ret = ieee802154_get_req(fd, &req); + + *rxonidle = req.attr_value.mac.rxonidle; + + return ret; +} diff --git a/wireless/ieee802154/libmac/ieee802154_setrxonidle.c b/wireless/ieee802154/libmac/ieee802154_setrxonidle.c new file mode 100644 index 000000000..de4d32d21 --- /dev/null +++ b/wireless/ieee802154/libmac/ieee802154_setrxonidle.c @@ -0,0 +1,65 @@ +/**************************************************************************** + * apps/wireless/ieee802154/libmac/ieee802154_setrxonidle.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Author: Anthony Merlino + * + * 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 + +#include +#include +#include +#include +#include + +#include + +#include "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int ieee802154_setrxonidle(int fd, bool rxonidle) +{ + struct ieee802154_set_req_s req; + + req.pib_attr = IEEE802154_PIB_MAC_RX_ON_WHEN_IDLE; + req.attr_value.mac.rxonidle = rxonidle; + + return ieee802154_set_req(fd, &req); +} diff --git a/wireless/ieee802154/libmac/sixlowpan_getrxonidle.c b/wireless/ieee802154/libmac/sixlowpan_getrxonidle.c new file mode 100644 index 000000000..0c5a5ec21 --- /dev/null +++ b/wireless/ieee802154/libmac/sixlowpan_getrxonidle.c @@ -0,0 +1,67 @@ +/**************************************************************************** + * apps/wireless/ieee802154/libmac/sixlowpan_getrxonidle.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include + +#include + +#include "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int sixlowpan_getrxonidle(int sock, FAR const char *ifname, FAR bool *rxonidle) +{ + struct ieee802154_get_req_s req; + int ret; + + req.pib_attr = IEEE802154_PIB_MAC_RX_ON_WHEN_IDLE; + ret = sixlowpan_get_req(sock, ifname, &req); + + *rxonidle = req.attr_value.mac.rxonidle; + + return ret; +} diff --git a/wireless/ieee802154/libmac/sixlowpan_setrxonidle.c b/wireless/ieee802154/libmac/sixlowpan_setrxonidle.c new file mode 100644 index 000000000..cc0c0d889 --- /dev/null +++ b/wireless/ieee802154/libmac/sixlowpan_setrxonidle.c @@ -0,0 +1,65 @@ +/**************************************************************************** + * apps/wireless/ieee802154/libmac/sixlowpan_setrxonidle.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include +#include + +#include + +#include "wireless/ieee802154.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int sixlowpan_setrxonidle(int sock, FAR const char *ifname, bool rxonidle) +{ + struct ieee802154_set_req_s req; + + req.pib_attr = IEEE802154_PIB_MAC_RX_ON_WHEN_IDLE; + req.attr_value.mac.rxonidle = rxonidle; + + return sixlowpan_set_req(sock, ifname, &req); +}