From 5ca0bc0a5c641bb91eb9e2f3ddc6b055d2852f52 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Tue, 4 Aug 2015 01:31:07 +0200 Subject: [PATCH 01/27] add ieee apps --- .gitignore | 2 + Kconfig | 4 + Makefile | 2 + ieee802154/Kconfig | 7 + ieee802154/Make.defs | 37 +++ ieee802154/Makefile | 78 ++++++ ieee802154/coord/Kconfig | 22 ++ ieee802154/coord/Make.defs | 39 +++ ieee802154/coord/Makefile | 138 ++++++++++ ieee802154/coord/coord_main.c | 162 ++++++++++++ ieee802154/i8sak/Kconfig | 23 ++ ieee802154/i8sak/Make.defs | 39 +++ ieee802154/i8sak/Makefile | 138 ++++++++++ ieee802154/i8sak/i8sak_main.c | 483 ++++++++++++++++++++++++++++++++++ 14 files changed, 1174 insertions(+) create mode 100644 ieee802154/Kconfig create mode 100644 ieee802154/Make.defs create mode 100644 ieee802154/Makefile create mode 100644 ieee802154/coord/Kconfig create mode 100644 ieee802154/coord/Make.defs create mode 100644 ieee802154/coord/Makefile create mode 100644 ieee802154/coord/coord_main.c create mode 100644 ieee802154/i8sak/Kconfig create mode 100644 ieee802154/i8sak/Make.defs create mode 100644 ieee802154/i8sak/Makefile create mode 100644 ieee802154/i8sak/i8sak_main.c diff --git a/.gitignore b/.gitignore index 7a658a38b..f86f1545e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ cscope.out /*.lib /romfs.img /boot_romfsimg.h +/*/*/.built + diff --git a/Kconfig b/Kconfig index 7b21c6387..cc806a858 100644 --- a/Kconfig +++ b/Kconfig @@ -15,6 +15,10 @@ menu "Graphics Support" source "$APPSDIR/graphics/Kconfig" endmenu +menu "IEEE 802.15.4" +source "$APPSDIR/ieee802154/Kconfig" +endmenu + menu "Interpreters" source "$APPSDIR/interpreters/Kconfig" endmenu diff --git a/Makefile b/Makefile index d580d2f10..8c9a8e182 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ TOPDIR ?= $(APPDIR)/import CONFIGURED_APPS = SUBDIRS = examples graphics interpreters modbus builtin import nshlib SUBDIRS += netutils platform system +SUBDIRS += ieee802154 # The list of configured directories is derived from NuttX configuration # file: The selected applications are enabled settings in the configuration @@ -72,6 +73,7 @@ include netutils/Make.defs include nshlib/Make.defs include platform/Make.defs include system/Make.defs +include ieee802154/Make.defs -include external/Make.defs # INSTALLED_APPS is the list of currently available application directories. It diff --git a/ieee802154/Kconfig b/ieee802154/Kconfig new file mode 100644 index 000000000..a491e6289 --- /dev/null +++ b/ieee802154/Kconfig @@ -0,0 +1,7 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +source "$APPSDIR/ieee802154/coord/Kconfig" +source "$APPSDIR/ieee802154/i8sak/Kconfig" diff --git a/ieee802154/Make.defs b/ieee802154/Make.defs new file mode 100644 index 000000000..30e651189 --- /dev/null +++ b/ieee802154/Make.defs @@ -0,0 +1,37 @@ +############################################################################ +# apps/ieee802154/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2012, 2015 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. +# +############################################################################ + +include $(wildcard ieee802154/*/Make.defs) diff --git a/ieee802154/Makefile b/ieee802154/Makefile new file mode 100644 index 000000000..dd4067914 --- /dev/null +++ b/ieee802154/Makefile @@ -0,0 +1,78 @@ +############################################################################ +# apps/ieee802154/Makefile +# +# Copyright (C) 2014 Gregory Nutt. All rights reserved. +# Author: Sebastien Lorquet +# +# 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. +# +############################################################################ + +-include $(TOPDIR)/.config # Current configuration + +# Sub-directories + +SUBDIRS = coord i8sak + +# Sub-directories that might need context setup. Directories may need +# context setup for a variety of reasons, but the most common is because +# the example may be built as an NSH built-in function. + +CNTXTDIRS = + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +CNTXTDIRS += coord i8sak +endif + +all: nothing + +.PHONY: nothing context depend clean distclean + +define SDIR_template +$(1)_$(2): + $(Q) $(MAKE) -C $(1) $(2) TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" +endef + +$(foreach SDIR, $(CNTXTDIRS), $(eval $(call SDIR_template,$(SDIR),context))) +$(foreach SDIR, $(SUBDIRS), $(eval $(call SDIR_template,$(SDIR),depend))) +$(foreach SDIR, $(SUBDIRS), $(eval $(call SDIR_template,$(SDIR),clean))) +$(foreach SDIR, $(SUBDIRS), $(eval $(call SDIR_template,$(SDIR),distclean))) + +nothing: + +install: + +context: $(foreach SDIR, $(CNTXTDIRS), $(SDIR)_context) + +depend: $(foreach SDIR, $(SUBDIRS), $(SDIR)_depend) + +clean: $(foreach SDIR, $(SUBDIRS), $(SDIR)_clean) + +distclean: $(foreach SDIR, $(SUBDIRS), $(SDIR)_distclean) + +-include Make.dep diff --git a/ieee802154/coord/Kconfig b/ieee802154/coord/Kconfig new file mode 100644 index 000000000..ef7c5148b --- /dev/null +++ b/ieee802154/coord/Kconfig @@ -0,0 +1,22 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config IEEE802154_COORD + bool "Generic IEEE 802.15.4 coordinator" + default n + ---help--- + Enable the IEEE 802.15.4 generic coordinator + +if IEEE802154_COORD + +config IEEE802154_COORD_PROGNAME + string "Program name" + default "coord" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +endif diff --git a/ieee802154/coord/Make.defs b/ieee802154/coord/Make.defs new file mode 100644 index 000000000..6df3e7b4a --- /dev/null +++ b/ieee802154/coord/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/ieee802154/coord/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2015 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. +# +############################################################################ + +ifeq ($(CONFIG_IEEE802154_COORD),y) +CONFIGURED_APPS += ieee802154/coord +endif diff --git a/ieee802154/coord/Makefile b/ieee802154/coord/Makefile new file mode 100644 index 000000000..5dc604f78 --- /dev/null +++ b/ieee802154/coord/Makefile @@ -0,0 +1,138 @@ +############################################################################ +# apps/ieee802154/coord/Makefile +# +# Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. +# Copyright (C) 2014-2015 Sebastien Lorquet. All rights reserved. +# Author: Sebastien Lorquet +# +# 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. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# Built-in application info + +APPNAME = coord +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 2048 + +# Generic Coordinator + +ASRCS = +CSRCS = +MAINSRC = coord_main.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) +MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) $(MAINSRC) +OBJS = $(AOBJS) $(COBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) + OBJS += $(MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ifeq ($(WINTOOL),y) + INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}" +else + INSTALL_DIR = $(BIN_DIR) +endif + +CONFIG_IEEE802154_COORD_PROGNAME ?= coord$(EXEEXT) +PROGNAME = $(CONFIG_IEEE802154_COORD_PROGNAME) + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: clean depend distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + @touch .built + +ifeq ($(CONFIG_BUILD_KERNEL),y) +$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ) + @echo "LD: $(PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) + +install: $(BIN_DIR)$(DELIM)$(PROGNAME) + +else +install: + +endif + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat +else +context: +endif + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c new file mode 100644 index 000000000..a4bc706f0 --- /dev/null +++ b/ieee802154/coord/coord_main.c @@ -0,0 +1,162 @@ +/**************************************************************************** + * apps/ieee802154/coord/coord_main.c + * + * Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2014-2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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. + * + ****************************************************************************/ + +/* Application description + * + * The coordinator is a central node in any IEEE 802.15.4 wireless network. + * It listens for clients and dispatches short addresses. It stores data from + * source clients and makes it available to destination clients. + * Also, in beacon enabled networks, it broadcasts beacons frames and + * manages guaranteed time slots. + * On non-beacon enabled networks, it sends a beacon only when a beacon + * request is received. + * + * This coordinator is generic. It does not interpret the contents of data + * frames. It only interprets command frames to manage client associations + * and data dispatch. + * + * There is no support for mesh networking (coord/coord traffic and packet + * forwarding). + * + * There is no support either for data security (yet). + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ +#ifndef CONFIG_IEEE802154_COORD_MAXCLIENTS +#define CONFIG_IEEE802154_COORD_MAXCLIENTS 8 +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ieee_client_s +{ + uint8_t eaddr[8]; /* client extended address */ + uint8_t saddr[2]; /* client short address */ + struct ieee802154_packet_s pending; /* pending packet for device */ +}; + +struct ieee_coord_s +{ + int fd; /* Device handle */ + uint8_t chan; /* PAN channel */ + uint8_t panid[2]; /* PAN ID */ + uint8_t nclients; /* Number of coordinated clients */ + struct ieee802154_packet_s rxbuf; /* General rx buffer */ + struct ieee_client_s clients[CONFIG_IEEE802154_COORD_MAXCLIENTS]; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ +static struct ieee_coord_s g_coord; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void coord_initialize(struct ieee_coord_s *coord) +{ + int i; + coord->nclients = 0; + for (i = 0; i < CONFIG_IEEE802154_COORD_MAXCLIENTS; i++) + { + coord->clients[i].pending.len = 0; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +static int coord_loop(FAR struct ieee_coord_s *coord, FAR char *dev, int chan, int panid) +{ + uint8_t pan[2]; + int ret = OK; + + pan[0] = panid >> 8; + pan[1] = panid & 0xFF; + + printf("starting %s on chan %d, panid %02X%02X", dev, chan, pan[0], pan[1]); + + while(ret == OK) + { + ret = read(coord->fd, &coord->rxbuf, sizeof(struct ieee802154_packet_s)); + + } + return OK; +} + +/**************************************************************************** + * coord_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int coord_main(int argc, FAR char *argv[]) +#endif +{ + int chan; + int panid; + + coord_initialize(&g_coord); + printf("IEEE 802.15.4 Coordinator start\n"); + + if (argc<4) + { + printf("coord "); + return ERROR; + } + + chan = strtol(argv[2], NULL, 0); + panid = strtol(argv[3], NULL, 0); + + return coord_loop(&g_coord, argv[1], chan, panid); +} diff --git a/ieee802154/i8sak/Kconfig b/ieee802154/i8sak/Kconfig new file mode 100644 index 000000000..95bae3d3b --- /dev/null +++ b/ieee802154/i8sak/Kconfig @@ -0,0 +1,23 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config IEEE802154_I8SAK + bool "IEEE 802.15.4 Swiss Army Knife" + default n + ---help--- + Enable the IEEE 802.15.4 Swiss Army Knife + +if IEEE802154_I8SAK + +config IEEE802154_I8SAK_PROGNAME + string "Program name" + default "i8" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +endif + diff --git a/ieee802154/i8sak/Make.defs b/ieee802154/i8sak/Make.defs new file mode 100644 index 000000000..28f871e65 --- /dev/null +++ b/ieee802154/i8sak/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/ieee802154/i8sak/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2015 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. +# +############################################################################ + +ifeq ($(CONFIG_IEEE802154_I8SAK),y) +CONFIGURED_APPS += ieee802154/i8sak +endif diff --git a/ieee802154/i8sak/Makefile b/ieee802154/i8sak/Makefile new file mode 100644 index 000000000..7dc7d266d --- /dev/null +++ b/ieee802154/i8sak/Makefile @@ -0,0 +1,138 @@ +############################################################################ +# apps/ieee802154/i8sak/Makefile +# +# Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. +# Copyright (C) 2014-2015 Sebastien Lorquet. All rights reserved. +# Author: Sebastien Lorquet +# +# 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. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# IEEE 802.15.4 SAK (swiss army knife ) + +APPNAME = i8 +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 2048 + +# IEEE 802.15.4 SAK (Swiss Army Knife) + +ASRCS = +CSRCS = +MAINSRC = i8sak_main.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) +MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) $(MAINSRC) +OBJS = $(AOBJS) $(COBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) + OBJS += $(MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ifeq ($(WINTOOL),y) + INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}" +else + INSTALL_DIR = $(BIN_DIR) +endif + +CONFIG_EXAMPLES_IEEEDUMP_PROGNAME ?= ieeedump$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_IEEEDUMP_PROGNAME) + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: clean depend distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + @touch .built + +ifeq ($(CONFIG_BUILD_KERNEL),y) +$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ) + @echo "LD: $(PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) + +install: $(BIN_DIR)$(DELIM)$(PROGNAME) + +else +install: + +endif + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat +else +context: +endif + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c new file mode 100644 index 000000000..3828f7529 --- /dev/null +++ b/ieee802154/i8sak/i8sak_main.c @@ -0,0 +1,483 @@ +/**************************************************************************** + * ieee802154/i8sak/i8sak_main.c + * IEEE 802.15.4 Swiss Army Knife + * + * Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. + * Copyright (C) 2014-2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * This app can be used to control an IEEE 802.15.4 device from command line. + * Example valid ieee802154 packets: + * + * Beacon request, seq number 01: + * xx xx 01 + * + * Simple data packet + * from long address xx to long address yy, pan id pp for src and dest + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct ieee802154_packet_s rxpacket; +static struct ieee802154_packet_s txpacket; + +int setchan(int fd, int chan) +{ + int ret = ioctl(fd, MAC854IOCSCHAN, (unsigned long)chan ); + if (ret<0) + { + printf("MAC854IOCSCHAN failed\n"); + } + return ret; +} + +int setcca(int fd, FAR struct ieee802154_cca_s *cca) +{ + int ret = ioctl(fd, MAC854IOCSCCA, (unsigned long)cca ); + if (ret<0) + { + printf("MAC854IOCSCCA failed\n"); + } + return ret; +} + +int getcca(int fd, FAR struct ieee802154_cca_s *cca) +{ + int ret = ioctl(fd, MAC854IOCGCCA, (unsigned long)cca ); + if (ret<0) + { + printf("MAC854IOCGCCA failed\n"); + } + return ret; +} + +int scan(int fd) +{ + int chan,scan,oldchan; + int ret = OK; + uint8_t energy; + uint8_t levels[16]; + + /* save original channel */ + + ret = ioctl(fd, MAC854IOCGCHAN, (unsigned long)&oldchan); + if (ret) + { + printf("MAC854IOCGCHAN failed\n"); + return ret; + } + + /* do scan */ + + printf("Scanning channels "); + memset(levels, 0, 16); + for(scan=0;scan<256;scan++) + { + printf("."); fflush(stdout); + for (chan=0; chan<16; chan++) + { + ret = setchan(fd, chan+11); + if (ret<0) + { + printf("Device is not an IEEE 802.15.4 interface!\n"); + return ret; + } + + ret = ioctl(fd, MAC854IOCGED, (unsigned long)&energy); + if (ret<0) + { + printf("Device is not an IEEE 802.15.4 interface!\n"); + return ret; + } + if(energy > levels[chan]) + { + levels[chan] = energy; + } + } + } + printf("\n"); + for (chan=0;chan < 16; chan++) + { + printf("%2d : [%3d] ",chan+11, levels[chan]); + energy = levels[chan] >> 3; + while(energy-- > 0) printf("#"); + printf("\n"); + } + + /* restore original channel */ + + ret = setchan(fd, oldchan); + + return ret; +} + +/**************************************************************************** + * Name : status + * + * Description : + * Show device status + ****************************************************************************/ + +static int status(int fd) +{ + int ret,i; + uint8_t panid[2], saddr[2], eaddr[8]; + int promisc, chan; + struct ieee802154_cca_s cca; + + /* Get information */ + + ret = ioctl(fd, MAC854IOCGPANID, (unsigned long)&panid); + if (ret) + { + printf("MAC854IOCGPANID failed\n"); + return ret; + } + ret = ioctl(fd, MAC854IOCGCHAN, (unsigned long)&chan); + if (ret) + { + printf("MAC854IOCGCHAN failed\n"); + return ret; + } + + ret = ioctl(fd, MAC854IOCGSADDR, (unsigned long)&saddr); + if (ret) + { + printf("MAC854IOCGSADDR failed\n"); + return ret; + } + ret = ioctl(fd, MAC854IOCGEADDR, (unsigned long)&eaddr); + if (ret) + { + printf("MAC854IOCGEADR failed\n"); + return ret; + } + ret = ioctl(fd, MAC854IOCGPROMISC, (unsigned long)&promisc); + if (ret) + { + printf("MAC854IOCGPROMISC failed\n"); + return ret; + } + ret = ioctl(fd, MAC854IOCGCCA, (unsigned long)&cca); + if (ret) + { + printf("MAC854IOCGCCA failed\n"); + return ret; + } +#if 0 + ret = ioctl(fd, MAC854IOCGORDER, (unsigned long)&order); + if (ret) + { + printf("MAC854IOCGORDER failed\n"); + return ret; + } +#endif + /* Display */ + + printf("PANID %02X%02X CHAN %2d (%d MHz)\nSADDR %02X%02X EADDR ", + panid[0], panid[1], chan, 2350+(5*chan), saddr[0], saddr[1]); + for (i=0; i<8; i++) + { + printf("%02X", eaddr[i]); + } + printf("\nCCA: "); + if (cca.use_ed) + { + printf("ED (%d) ", cca.edth); + } + if (cca.use_cs) + { + printf("CS (%d)", cca.csth); + } + printf("\nPromisc:%s\n", promisc?"Yes":"No"); + return 0; +} + +/**************************************************************************** + * Name : display + * + * Description : + * Display a single packet + ****************************************************************************/ + +static int display(FAR struct ieee802154_packet_s *pack) +{ + printf("Packet len=%d\n", pack->len); + return 0; +} + +/**************************************************************************** + * Name : sniff + * + * Description : + * Listen for all packets with a valid CRC on a given channel. + ****************************************************************************/ + +static int sniff(int fd, int chan) +{ + int ret; + ret = setchan(fd, chan); + if (ret<0) + { + return ret; + } +#if 0 + ret = ioctl(fd, MAC854IOCSPROMISC, TRUE); + if (ret<0) + { + printf("Device is not an IEEE 802.15.4 interface!\n"); + return ret; + } +#endif + printf("Listening on channel %d\n",chan); + while (1) + { + ret = read(fd, &rxpacket, sizeof(struct ieee802154_packet_s)); + if(ret < 0) + { + if (errno == EAGAIN) + { + usleep(1000); + continue; + } + else + { + printf("read: errno=%d\n",errno); + break; + } + } + + /* Display packet */ + display(&rxpacket); + } + + return ret; +} + +static int tx(int fd, FAR struct ieee802154_packet_s *packet) +{ + int i; + for (i = 0; i < packet->len; i++) + { + printf("%02X", packet->data[i]); + } + printf("\n"); + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * usage + ****************************************************************************/ + +int usage(void) +{ + printf("i8 \n" + " scan\n" + " dump\n" + " stat\n" + " chan \n" + " edth \n" + " csth \n" + " tx \n" + ); + return ERROR; +} + +/**************************************************************************** + * i8_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int i8_main(int argc, char *argv[]) +#endif +{ + int fd; + int ret = OK; + unsigned long arg=0; + struct ieee802154_cca_s cca; + + printf("IEEE packet sniffer/dumper\n"); + if (argc<3) + { + 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; + } + + /* get mode */ + if (!strcmp(argv[2], "scan")) + { + ret = scan(fd); + } + else if (!strcmp(argv[2], "dump")) + { + ret = ioctl(fd, 1000, 0); + } + else if (!strcmp(argv[2], "stat")) + { + ret = status(fd); + } + else if (!strcmp(argv[2], "chan")) + { + if(argc != 4) + { + ret = usage(); + } + ret = setchan(fd, arg); + } + else if (!strcmp(argv[2], "edth")) + { + if(argc != 4) + { + goto usage; + } + ret = getcca(fd, &cca); + if(!strcmp("off",argv[3])) + { + cca.use_ed = 0; + } + else + { + cca.use_ed = 1; + cca.edth = arg; + } + ret = setcca(fd, &cca); + } + else if (!strcmp(argv[2], "csth")) + { + if(argc != 4) + { + goto usage; + } + ret = getcca(fd, &cca); + if(!strcmp("off",argv[3])) + { + cca.use_cs = 0; + } + else + { + cca.use_cs = 1; + cca.csth = arg; + } + ret = setcca(fd, &cca); + } + else if (!strcmp(argv[2], "snif")) + { + ret = sniff(fd,arg); + } + else if (!strcmp(argv[2], "tx")) + { + int id=0; + int len = strlen(argv[3]); + FAR char *ptr = argv[3]; + + if (len & 1) + { + goto data_error; + } + + /* decode hex packet */ + + while (id<125 && len>0) + { + int dat; + if (sscanf(ptr, "%2x", &dat)==1) + { + txpacket.data[id++] = dat; + ptr += 2; + len -= 2; + } + else + { +data_error: + printf("data error\n"); + ret = ERROR; + goto error; + } + } + txpacket.len = id; + + ret = tx(fd,&txpacket); + } + else + { +usage: + ret = usage(); + } + +error: + close(fd); +exit: + return ret; +} + From 45864732c58a2f8831e5b22b6d25fb2e36760297 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Tue, 4 Aug 2015 16:40:56 +0200 Subject: [PATCH 02/27] update i8 sak to reflect new driver structure --- ieee802154/i8sak/i8sak_main.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index 3828f7529..044b7bd31 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -73,7 +73,7 @@ static struct ieee802154_packet_s rxpacket; static struct ieee802154_packet_s txpacket; -int setchan(int fd, int chan) +int setchan(int fd, uint8_t chan) { int ret = ioctl(fd, MAC854IOCSCHAN, (unsigned long)chan ); if (ret<0) @@ -105,9 +105,9 @@ int getcca(int fd, FAR struct ieee802154_cca_s *cca) int scan(int fd) { - int chan,scan,oldchan; + int scan; int ret = OK; - uint8_t energy; + uint8_t oldchan, chan, energy; uint8_t levels[16]; /* save original channel */ @@ -141,6 +141,7 @@ int scan(int fd) printf("Device is not an IEEE 802.15.4 interface!\n"); return ret; } + if(energy > levels[chan]) { levels[chan] = energy; @@ -173,8 +174,9 @@ int scan(int fd) static int status(int fd) { int ret,i; - uint8_t panid[2], saddr[2], eaddr[8]; - int promisc, chan; + uint8_t chan, eaddr[8]; + uint16_t panid, saddr; + bool promisc; struct ieee802154_cca_s cca; /* Get information */ @@ -198,7 +200,7 @@ static int status(int fd) printf("MAC854IOCGSADDR failed\n"); return ret; } - ret = ioctl(fd, MAC854IOCGEADDR, (unsigned long)&eaddr); + ret = ioctl(fd, MAC854IOCGEADDR, (unsigned long)&eaddr[0]); if (ret) { printf("MAC854IOCGEADR failed\n"); @@ -227,7 +229,7 @@ static int status(int fd) /* Display */ printf("PANID %02X%02X CHAN %2d (%d MHz)\nSADDR %02X%02X EADDR ", - panid[0], panid[1], chan, 2350+(5*chan), saddr[0], saddr[1]); + panid>>8, panid&0xFF, chan, 2350+(5*chan), saddr>>8, saddr&0xFF); for (i=0; i<8; i++) { printf("%02X", eaddr[i]); @@ -241,7 +243,7 @@ static int status(int fd) { printf("CS (%d)", cca.csth); } - printf("\nPromisc:%s\n", promisc?"Yes":"No"); + printf("\nPromisc: %s\n", promisc?"Yes":"No"); return 0; } @@ -330,6 +332,7 @@ int usage(void) printf("i8 \n" " scan\n" " dump\n" + " snif\n" " stat\n" " chan \n" " edth \n" From 0a506f86015e10e386a765e9104d45f678e39e8a Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Tue, 4 Aug 2015 18:08:58 +0200 Subject: [PATCH 03/27] energy detection scan now works in real time. --- ieee802154/i8sak/i8sak_main.c | 61 ++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index 044b7bd31..ba4d141bb 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -103,29 +103,21 @@ int getcca(int fd, FAR struct ieee802154_cca_s *cca) return ret; } +uint8_t levels[16]; +uint8_t disp[16]; + int scan(int fd) { - int scan; int ret = OK; - uint8_t oldchan, chan, energy; - uint8_t levels[16]; + uint8_t chan, energy; - /* save original channel */ - - ret = ioctl(fd, MAC854IOCGCHAN, (unsigned long)&oldchan); - if (ret) - { - printf("MAC854IOCGCHAN failed\n"); - return ret; - } + printf("\n"); /* do scan */ - - printf("Scanning channels "); - memset(levels, 0, 16); - for(scan=0;scan<256;scan++) + memset(disp,0,16); + + while(1) { - printf("."); fflush(stdout); for (chan=0; chan<16; chan++) { ret = setchan(fd, chan+11); @@ -134,7 +126,6 @@ int scan(int fd) printf("Device is not an IEEE 802.15.4 interface!\n"); return ret; } - ret = ioctl(fd, MAC854IOCGED, (unsigned long)&energy); if (ret<0) { @@ -142,24 +133,34 @@ int scan(int fd) return ret; } - if(energy > levels[chan]) + levels[chan] = energy; + } + + /* compute max with decay */ + + for (chan=0; chan<16; chan++) + { + if(levels[chan] > disp[chan]) { - levels[chan] = energy; + disp[chan] = levels[chan]; + } + else + { + if(disp[chan] > 0) disp[chan] -= 1; } } - } - printf("\n"); - for (chan=0;chan < 16; chan++) - { - printf("%2d : [%3d] ",chan+11, levels[chan]); - energy = levels[chan] >> 3; - while(energy-- > 0) printf("#"); - printf("\n"); - } - /* restore original channel */ + for (chan=0;chan < 16; chan++) + { + printf("%2d : [%3d] ",chan+11, disp[chan]); + energy = disp[chan] >> 3; + while(energy-- > 0) printf("#"); + printf(" \n"); + } - ret = setchan(fd, oldchan); + /* move cursor 17 lines up : http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html */ + printf("\x1B[16A"); + } return ret; } From 853e9892b8f6b8b4354b95315bc0f0459f6f6b2b Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Fri, 7 Aug 2015 01:41:22 +0200 Subject: [PATCH 04/27] fix app for latest driver. --- ieee802154/i8sak/i8sak_main.c | 68 +++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index ba4d141bb..fc80b16ee 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -70,8 +70,8 @@ * Private Data ****************************************************************************/ -static struct ieee802154_packet_s rxpacket; -static struct ieee802154_packet_s txpacket; +static struct ieee802154_packet_s gRxPacket; +static struct ieee802154_packet_s gTxPacket; int setchan(int fd, uint8_t chan) { @@ -257,7 +257,13 @@ static int status(int fd) static int display(FAR struct ieee802154_packet_s *pack) { - printf("Packet len=%d\n", pack->len); + int i; + printf("Packet len=%u rssi=%u lqi=%u [\n", pack->len, pack->rssi, pack->lqi); + for (i = 0; i < pack->len; i++) + { + printf("%02X", pack->data[i]); + } + printf("]\n"); return 0; } @@ -276,23 +282,22 @@ static int sniff(int fd, int chan) { return ret; } -#if 0 + ret = ioctl(fd, MAC854IOCSPROMISC, TRUE); if (ret<0) { printf("Device is not an IEEE 802.15.4 interface!\n"); return ret; } -#endif - printf("Listening on channel %d\n",chan); + + printf("Listening on channel %d in promisc mode.\n",chan); while (1) { - ret = read(fd, &rxpacket, sizeof(struct ieee802154_packet_s)); + ret = read(fd, &gRxPacket, sizeof(struct ieee802154_packet_s)); if(ret < 0) { if (errno == EAGAIN) { - usleep(1000); continue; } else @@ -303,20 +308,36 @@ static int sniff(int fd, int chan) } /* Display packet */ - display(&rxpacket); + display(&gRxPacket); } return ret; } -static int tx(int fd, FAR struct ieee802154_packet_s *packet) +static int tx(int fd, int chan, FAR struct ieee802154_packet_s *pack) { - int i; - for (i = 0; i < packet->len; i++) + int i,ret; + + ret = setchan(fd, chan); + if (ret<0) { - printf("%02X", packet->data[i]); + return ret; + } + + for (i = 0; i < pack->len; i++) + { + printf("%02X", pack->data[i]); + } + fflush(stdout); + ret = write(fd, pack, sizeof(struct ieee802154_packet_s)); + if(ret==OK) + { + printf(" OK\n"); + } + else + { + printf(" write: errno=%d\n",errno); } - printf("\n"); return OK; } @@ -333,12 +354,12 @@ int usage(void) printf("i8 \n" " scan\n" " dump\n" - " snif\n" + " snif \n" " stat\n" " chan \n" " edth \n" " csth \n" - " tx \n" + " tx \n" ); return ERROR; } @@ -358,13 +379,13 @@ int i8_main(int argc, char *argv[]) unsigned long arg=0; struct ieee802154_cca_s cca; - printf("IEEE packet sniffer/dumper\n"); + printf("IEEE packet sniffer/dumper argc=%d\n",argc); if (argc<3) { return usage(); } - if (argc==4) + if (argc>=4) { arg = atol(argv[3]); } @@ -442,8 +463,9 @@ int i8_main(int argc, char *argv[]) else if (!strcmp(argv[2], "tx")) { int id=0; - int len = strlen(argv[3]); - FAR char *ptr = argv[3]; + unsigned long ch = arg; + int len = strlen(argv[4]); + FAR char *ptr = argv[4]; if (len & 1) { @@ -457,7 +479,7 @@ int i8_main(int argc, char *argv[]) int dat; if (sscanf(ptr, "%2x", &dat)==1) { - txpacket.data[id++] = dat; + gTxPacket.data[id++] = dat; ptr += 2; len -= 2; } @@ -469,9 +491,9 @@ data_error: goto error; } } - txpacket.len = id; + gTxPacket.len = id; - ret = tx(fd,&txpacket); + ret = tx(fd, ch, &gTxPacket); } else { From b9ae8069b7906ae2e0184d1339cf93a5d573d0e3 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Fri, 7 Aug 2015 12:26:06 +0200 Subject: [PATCH 05/27] improve packet sniffer display --- ieee802154/i8sak/i8sak_main.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index fc80b16ee..08b27c352 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -258,7 +258,7 @@ static int status(int fd) static int display(FAR struct ieee802154_packet_s *pack) { int i; - printf("Packet len=%u rssi=%u lqi=%u [\n", pack->len, pack->rssi, pack->lqi); + printf("len=%3u rssi=%3u lqi=%3u [", pack->len, pack->rssi, pack->lqi); for (i = 0; i < pack->len; i++) { printf("%02X", pack->data[i]); @@ -314,6 +314,13 @@ static int sniff(int fd, int chan) return ret; } +/**************************************************************************** + * Name : tx + * + * Description : + * Transmit a frame. + ****************************************************************************/ + static int tx(int fd, int chan, FAR struct ieee802154_packet_s *pack) { int i,ret; From db1e12512200bd571d92b6c422b40a780d490c5e Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Fri, 7 Aug 2015 16:58:45 +0200 Subject: [PATCH 06/27] add common ieee library for all apps. working on coordinator. --- ieee802154/Kconfig | 1 + ieee802154/Makefile | 12 +- ieee802154/common/Kconfig | 11 ++ ieee802154/common/Make.defs | 40 ++++ ieee802154/common/Makefile | 102 ++++++++++ ieee802154/common/ieee802154_setchan.c | 55 ++++++ ieee802154/common/ieee802154_seteaddr.c | 55 ++++++ ieee802154/common/ieee802154_setpanid.c | 55 ++++++ ieee802154/common/ieee802154_setpromisc.c | 56 ++++++ ieee802154/common/ieee802154_setsaddr.c | 55 ++++++ ieee802154/coord/Kconfig | 1 + ieee802154/coord/coord_main.c | 217 +++++++++++++++++++--- ieee802154/i8sak/Kconfig | 1 + include/ieee802154/ieee802154.h | 48 +++++ 14 files changed, 673 insertions(+), 36 deletions(-) create mode 100644 ieee802154/common/Kconfig create mode 100644 ieee802154/common/Make.defs create mode 100644 ieee802154/common/Makefile create mode 100644 ieee802154/common/ieee802154_setchan.c create mode 100644 ieee802154/common/ieee802154_seteaddr.c create mode 100644 ieee802154/common/ieee802154_setpanid.c create mode 100644 ieee802154/common/ieee802154_setpromisc.c create mode 100644 ieee802154/common/ieee802154_setsaddr.c create mode 100644 include/ieee802154/ieee802154.h diff --git a/ieee802154/Kconfig b/ieee802154/Kconfig index a491e6289..213550783 100644 --- a/ieee802154/Kconfig +++ b/ieee802154/Kconfig @@ -3,5 +3,6 @@ # see misc/tools/kconfig-language.txt. # +source "$APPSDIR/ieee802154/common/Kconfig" source "$APPSDIR/ieee802154/coord/Kconfig" source "$APPSDIR/ieee802154/i8sak/Kconfig" diff --git a/ieee802154/Makefile b/ieee802154/Makefile index dd4067914..8c340caf9 100644 --- a/ieee802154/Makefile +++ b/ieee802154/Makefile @@ -37,17 +37,7 @@ # Sub-directories -SUBDIRS = coord i8sak - -# Sub-directories that might need context setup. Directories may need -# context setup for a variety of reasons, but the most common is because -# the example may be built as an NSH built-in function. - -CNTXTDIRS = - -ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) -CNTXTDIRS += coord i8sak -endif +SUBDIRS = $(dir $(wildcard */Makefile)) all: nothing diff --git a/ieee802154/common/Kconfig b/ieee802154/common/Kconfig new file mode 100644 index 000000000..c214543ed --- /dev/null +++ b/ieee802154/common/Kconfig @@ -0,0 +1,11 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config IEEE802154_COMMON + bool "Common IEEE 802.15.4 library routines" + default n + ---help--- + Some common library routines for ieee 802.15.4 apps + diff --git a/ieee802154/common/Make.defs b/ieee802154/common/Make.defs new file mode 100644 index 000000000..421300973 --- /dev/null +++ b/ieee802154/common/Make.defs @@ -0,0 +1,40 @@ +############################################################################ +# apps/nshlib/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2012 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. +# +############################################################################ + +ifeq ($(CONFIG_IEEE802154_COMMON),y) +CONFIGURED_APPS += ieee802154/common +endif + diff --git a/ieee802154/common/Makefile b/ieee802154/common/Makefile new file mode 100644 index 000000000..045d2b1f5 --- /dev/null +++ b/ieee802154/common/Makefile @@ -0,0 +1,102 @@ +############################################################################ +# apps/nshlib/Makefile +# +# Copyright (C) 2011-2013 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. +# +############################################################################ + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +# NSH Library + +ASRCS = +CSRCS = ieee802154_setchan.c ieee802154_setpanid.c +CSRCS += ieee802154_seteaddr.c ieee802154_setsaddr.c +CSRCS += ieee802154_setpromisc.c + +# + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) +OBJS = $(AOBJS) $(COBJS) + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ROOTDEPPATH = --dep-path . +VPATH = + +# Build targets + +all: .built +.PHONY: context .depend depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + @touch .built + +install: + +context: + +.depend: Makefile $(SRCS) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep + diff --git a/ieee802154/common/ieee802154_setchan.c b/ieee802154/common/ieee802154_setchan.c new file mode 100644 index 000000000..b1f1d3d50 --- /dev/null +++ b/ieee802154/common/ieee802154_setchan.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_setchan.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_setchan(int fd, uint8_t chan) +{ + int ret = ioctl(fd, MAC854IOCSCHAN, (unsigned long)chan ); + if (ret<0) + { + printf("MAC854IOCSCHAN failed\n"); + } + return ret; +} diff --git a/ieee802154/common/ieee802154_seteaddr.c b/ieee802154/common/ieee802154_seteaddr.c new file mode 100644 index 000000000..251f4234a --- /dev/null +++ b/ieee802154/common/ieee802154_seteaddr.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_eaddr.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_seteaddr(int fd, uint8_t *eaddr) +{ + int ret = ioctl(fd, MAC854IOCSEADDR, (unsigned long)eaddr ); + if (ret<0) + { + printf("MAC854IOCSEADDR failed\n"); + } + return ret; +} diff --git a/ieee802154/common/ieee802154_setpanid.c b/ieee802154/common/ieee802154_setpanid.c new file mode 100644 index 000000000..4fd2a862c --- /dev/null +++ b/ieee802154/common/ieee802154_setpanid.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_setpanid.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_setpanid(int fd, uint16_t panid) +{ + int ret = ioctl(fd, MAC854IOCSPANID, (unsigned long)panid ); + if (ret<0) + { + printf("MAC854IOCSPANID failed\n"); + } + return ret; +} diff --git a/ieee802154/common/ieee802154_setpromisc.c b/ieee802154/common/ieee802154_setpromisc.c new file mode 100644 index 000000000..1ba2cc9c1 --- /dev/null +++ b/ieee802154/common/ieee802154_setpromisc.c @@ -0,0 +1,56 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_setpromisc.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_setpromisc(int fd, bool promisc) +{ + int ret = ioctl(fd, MAC854IOCSPROMISC, (unsigned long)promisc ); + if (ret<0) + { + printf("MAC854IOCSPROMISC failed\n"); + } + return ret; +} diff --git a/ieee802154/common/ieee802154_setsaddr.c b/ieee802154/common/ieee802154_setsaddr.c new file mode 100644 index 000000000..7ca225897 --- /dev/null +++ b/ieee802154/common/ieee802154_setsaddr.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_setsaddr.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_setsaddr(int fd, uint16_t saddr) +{ + int ret = ioctl(fd, MAC854IOCSCHAN, (unsigned long)saddr ); + if (ret<0) + { + printf("MAC854IOCSSADDR failed\n"); + } + return ret; +} diff --git a/ieee802154/coord/Kconfig b/ieee802154/coord/Kconfig index ef7c5148b..61b84979b 100644 --- a/ieee802154/coord/Kconfig +++ b/ieee802154/coord/Kconfig @@ -6,6 +6,7 @@ config IEEE802154_COORD bool "Generic IEEE 802.15.4 coordinator" default n + select IEEE802154_COMMON ---help--- Enable the IEEE 802.15.4 generic coordinator diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c index a4bc706f0..48f9ad5eb 100644 --- a/ieee802154/coord/coord_main.c +++ b/ieee802154/coord/coord_main.c @@ -61,9 +61,13 @@ #include #include #include +#include +#include #include +#include + /**************************************************************************** * Definitions ****************************************************************************/ @@ -82,14 +86,29 @@ struct ieee_client_s struct ieee802154_packet_s pending; /* pending packet for device */ }; +struct ieee_frame_s +{ + struct ieee802154_packet_s packet; + uint8_t fc1; + uint8_t fc2; + uint8_t seq; + uint8_t *dpanid; + uint8_t *daddr; + uint8_t daddrlen; + uint8_t *spanid; + uint8_t *saddr; + uint8_t saddrlen; + uint8_t *payload; +}; + struct ieee_coord_s { - int fd; /* Device handle */ - uint8_t chan; /* PAN channel */ - uint8_t panid[2]; /* PAN ID */ - uint8_t nclients; /* Number of coordinated clients */ - struct ieee802154_packet_s rxbuf; /* General rx buffer */ - struct ieee_client_s clients[CONFIG_IEEE802154_COORD_MAXCLIENTS]; + int fd; /* Device handle */ + uint8_t chan; /* PAN channel */ + uint16_t panid; /* PAN ID */ + uint8_t nclients; /* Number of coordinated clients */ + struct ieee_frame_s rxbuf; /* General rx buffer */ + struct ieee_client_s clients[CONFIG_IEEE802154_COORD_MAXCLIENTS]; }; /**************************************************************************** @@ -98,10 +117,10 @@ struct ieee_coord_s static struct ieee_coord_s g_coord; /**************************************************************************** - * Private Functions + * coord_initialize ****************************************************************************/ -static void coord_initialize(struct ieee_coord_s *coord) +static void coord_initialize(FAR struct ieee_coord_s *coord, FAR char *dev, FAR char *chan, FAR char *panid) { int i; coord->nclients = 0; @@ -109,26 +128,173 @@ static void coord_initialize(struct ieee_coord_s *coord) { coord->clients[i].pending.len = 0; } + coord->chan = strtol(chan , NULL, 0); + coord->panid = strtol(panid, NULL, 0); + + coord->fd = open(dev, O_RDWR); + } /**************************************************************************** - * Public Functions + * coord_main ****************************************************************************/ -static int coord_loop(FAR struct ieee_coord_s *coord, FAR char *dev, int chan, int panid) +static int coord_beacon(FAR struct ieee_coord_s *coord) { - uint8_t pan[2]; - int ret = OK; + printf("Beacon!\n"); + return 0; +} - pan[0] = panid >> 8; - pan[1] = panid & 0xFF; +/**************************************************************************** + * coord_main + ****************************************************************************/ - printf("starting %s on chan %d, panid %02X%02X", dev, chan, pan[0], pan[1]); +static int coord_data(FAR struct ieee_coord_s *coord) +{ + printf("Data!\n"); + return 0; +} - while(ret == OK) +/**************************************************************************** + * coord_main + ****************************************************************************/ + +static int coord_ack(FAR struct ieee_coord_s *coord) +{ + printf("Ack!\n"); + return 0; +} + +/**************************************************************************** + * coord_main + ****************************************************************************/ + +static int coord_command(FAR struct ieee_coord_s *coord) +{ + printf("Command!\n"); + return 0; +} + +/**************************************************************************** + * coord_main + ****************************************************************************/ + +static int coord_manage(FAR struct ieee_coord_s *coord) +{ + /* Decode frame type */ + uint8_t ftype, saddr, daddr; + int index; + + FAR struct ieee_frame_s *rx = &coord->rxbuf; + int i; + + for(i=0; ipacket.len; i++) printf("%02X", rx->packet.data[i]); + printf("\n"); + + rx->fc1 = rx->packet.data[0]; + rx->fc2 = rx->packet.data[1]; + rx->seq = rx->packet.data[2]; + + ftype = rx->fc1 & IEEE802154_FC1_FTYPE; + daddr = rx->fc2 & IEEE802154_FC2_DADDR; + saddr = rx->fc2 & IEEE802154_FC2_SADDR; + + index = 3; + + if(daddr == IEEE802154_DADDR_SHORT) { - ret = read(coord->fd, &coord->rxbuf, sizeof(struct ieee802154_packet_s)); + rx->dpanid = rx->packet.data+index; + index += 2; /* skip dest pan id */ + rx->daddr = rx->packet.data+index; + index += 2; /* skip dest addr */ + rx->daddrlen = 2; + } + else if(daddr == IEEE802154_DADDR_EXT) + { + rx->dpanid = rx->packet.data+index; + index += 2; /* skip dest pan id */ + rx->daddr = rx->packet.data+index; + index += 8; /* skip dest addr */ + rx->daddrlen = 8; + } + else + { + rx->dpanid = NULL; + rx->daddr = NULL; + rx->daddrlen = 0; + } + if(saddr == IEEE802154_SADDR_SHORT) + { + if(rx->fc1 & IEEE802154_FC1_INTRA) + { + rx->spanid = rx->dpanid; + } + else + { + rx->spanid = rx->packet.data+index; + index += 2; /*skip dest pan id*/ + } + rx->saddr = rx->packet.data+index; + index += 2; /* skip dest addr */ + rx->saddrlen = 2; + } + else if(saddr == IEEE802154_SADDR_EXT) + { + if(rx->fc1 & IEEE802154_FC1_INTRA) + { + rx->spanid = rx->dpanid; + } + else + { + rx->spanid = rx->packet.data+index; + index += 2; /*skip dest pan id*/ + } + rx->saddr = rx->packet.data+index; + index += 8; /* skip dest addr */ + rx->saddrlen = 8; + } + else + { + rx->spanid = NULL; + rx->saddr = NULL; + rx->saddrlen = 0; + } + + printf("SADDR len %d DADDR len %d\n", rx->saddrlen, rx->daddrlen); + switch(ftype) + { + case IEEE802154_FRAME_BEACON : coord_beacon (coord); break; + case IEEE802154_FRAME_DATA : coord_data (coord); break; + case IEEE802154_FRAME_ACK : coord_ack (coord); break; + case IEEE802154_FRAME_COMMAND: coord_command(coord); break; + default : fprintf(stderr, "unknown frame type!"); + } + return 0; +} + +/**************************************************************************** + * coord_loop + ****************************************************************************/ + +static int coord_loop(FAR struct ieee_coord_s *coord) +{ + int ret = 1; + FAR struct ieee_frame_s *rx = &coord->rxbuf; + + printf("starting on chan %d, panid %04X\n", coord->chan, coord->panid); + + ieee802154_setchan (coord->fd, coord->chan ); + ieee802154_setpanid(coord->fd, coord->panid); + //ieee802154_setpromisc(coord->fd, TRUE); + + while(ret > 0) + { + ret = read(coord->fd, &rx->packet, sizeof(struct ieee802154_packet_s)); + if(ret > 0) + { + coord_manage(coord); + } } return OK; } @@ -143,20 +309,21 @@ int main(int argc, FAR char *argv[]) int coord_main(int argc, FAR char *argv[]) #endif { - int chan; - int panid; - - coord_initialize(&g_coord); printf("IEEE 802.15.4 Coordinator start\n"); if (argc<4) { - printf("coord "); + printf("coord [0x]\n"); return ERROR; } - chan = strtol(argv[2], NULL, 0); - panid = strtol(argv[3], NULL, 0); - return coord_loop(&g_coord, argv[1], chan, panid); + coord_initialize(&g_coord, argv[1], argv[2], argv[3]); + if(g_coord.fd<0) + { + fprintf(stderr, "cannot open %s, errno=%d\n", argv[1], errno); + exit(ERROR); + } + + return coord_loop(&g_coord); } diff --git a/ieee802154/i8sak/Kconfig b/ieee802154/i8sak/Kconfig index 95bae3d3b..fb11839ba 100644 --- a/ieee802154/i8sak/Kconfig +++ b/ieee802154/i8sak/Kconfig @@ -6,6 +6,7 @@ config IEEE802154_I8SAK bool "IEEE 802.15.4 Swiss Army Knife" default n + select IEEE802154_COMMON ---help--- Enable the IEEE 802.15.4 Swiss Army Knife diff --git a/include/ieee802154/ieee802154.h b/include/ieee802154/ieee802154.h new file mode 100644 index 000000000..6d4602db6 --- /dev/null +++ b/include/ieee802154/ieee802154.h @@ -0,0 +1,48 @@ +/**************************************************************************** + * apps/include/ieee802154/ieee802154.h + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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. + * + ****************************************************************************/ + +#ifndef __APPS_INCLUDE_IEEE802154_IEEE802154_H +#define __APPS_INCLUDE_IEEE802154_IEEE802154_H + +#include +#include + +int ieee802154_setchan (int fd, uint8_t chan); +int ieee802154_setpanid (int fd, uint16_t panid); +int ieee802154_setsaddr (int fd, uint16_t saddr); +int ieee802154_seteaddr (int fd, uint8_t *chan); +int ieee802154_setpromisc(int fd, bool promisc); + +#endif /*__APPS_INCLUDE_IEEE802154_IEEE802154_H */ From 49913fa0be97ba4e25af628a7ae5be04735cac9c Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Fri, 7 Aug 2015 17:40:04 +0200 Subject: [PATCH 07/27] demux mac commands --- ieee802154/coord/coord_main.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c index 48f9ad5eb..635c3ba7e 100644 --- a/ieee802154/coord/coord_main.c +++ b/ieee802154/coord/coord_main.c @@ -171,7 +171,23 @@ static int coord_ack(FAR struct ieee_coord_s *coord) static int coord_command(FAR struct ieee_coord_s *coord) { - printf("Command!\n"); + FAR struct ieee_frame_s *rx = &coord->rxbuf; + uint8_t cmd = rx->payload[0]; + + printf("Command %02X!\n",cmd); + + switch(cmd) + { + case IEEE802154_CMD_ASSOC_REQ : break; + case IEEE802154_CMD_ASSOC_RSP : break; + case IEEE802154_CMD_DIS_NOT : break; + case IEEE802154_CMD_DATA_REQ : break; + case IEEE802154_CMD_PANID_CONF_NOT : break; + case IEEE802154_CMD_ORPHAN_NOT : break; + case IEEE802154_CMD_BEACON_REQ : break; + case IEEE802154_CMD_COORD_REALIGN : break; + case IEEE802154_CMD_GTS_REQ : break; + } return 0; } @@ -261,6 +277,8 @@ static int coord_manage(FAR struct ieee_coord_s *coord) rx->saddrlen = 0; } + rx->payload = rx->packet.data + index; + printf("SADDR len %d DADDR len %d\n", rx->saddrlen, rx->daddrlen); switch(ftype) { From d15e000b36e5effa9244705e27b2a858d383bebf Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Sun, 9 Aug 2015 23:43:05 +0200 Subject: [PATCH 08/27] install basic coordinator daemon task --- ieee802154/coord/coord_main.c | 271 ++++++++++++++++++++++++---------- 1 file changed, 193 insertions(+), 78 deletions(-) diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c index 635c3ba7e..0ac396555 100644 --- a/ieee802154/coord/coord_main.c +++ b/ieee802154/coord/coord_main.c @@ -59,10 +59,14 @@ ****************************************************************************/ #include +#include #include #include +#include +#include #include #include +#include #include @@ -86,18 +90,25 @@ struct ieee_client_s struct ieee802154_packet_s pending; /* pending packet for device */ }; +struct ieee_addr +{ + uint8_t len; + uint16_t panid; + union + { + uint16_t saddr; + uint8_t eaddr[8]; + } addr; +}; + struct ieee_frame_s { struct ieee802154_packet_s packet; uint8_t fc1; uint8_t fc2; uint8_t seq; - uint8_t *dpanid; - uint8_t *daddr; - uint8_t daddrlen; - uint8_t *spanid; - uint8_t *saddr; - uint8_t saddrlen; + struct ieee_addr daddr; + struct ieee_addr saddr; uint8_t *payload; }; @@ -117,26 +128,7 @@ struct ieee_coord_s static struct ieee_coord_s g_coord; /**************************************************************************** - * coord_initialize - ****************************************************************************/ - -static void coord_initialize(FAR struct ieee_coord_s *coord, FAR char *dev, FAR char *chan, FAR char *panid) -{ - int i; - coord->nclients = 0; - for (i = 0; i < CONFIG_IEEE802154_COORD_MAXCLIENTS; i++) - { - coord->clients[i].pending.len = 0; - } - coord->chan = strtol(chan , NULL, 0); - coord->panid = strtol(panid, NULL, 0); - - coord->fd = open(dev, O_RDWR); - -} - -/**************************************************************************** - * coord_main + * coord_beacon ****************************************************************************/ static int coord_beacon(FAR struct ieee_coord_s *coord) @@ -146,7 +138,7 @@ static int coord_beacon(FAR struct ieee_coord_s *coord) } /**************************************************************************** - * coord_main + * coord_data ****************************************************************************/ static int coord_data(FAR struct ieee_coord_s *coord) @@ -156,7 +148,7 @@ static int coord_data(FAR struct ieee_coord_s *coord) } /**************************************************************************** - * coord_main + * coord_ack ****************************************************************************/ static int coord_ack(FAR struct ieee_coord_s *coord) @@ -166,7 +158,24 @@ static int coord_ack(FAR struct ieee_coord_s *coord) } /**************************************************************************** - * coord_main + * coord_command_beacon_req + ****************************************************************************/ + +static int coord_command_beacon_req(FAR struct ieee_coord_s *coord) +{ + FAR struct ieee_frame_s *rx = &coord->rxbuf; + + //check the request looks good + + printf("Beacon request\n"); + + //compose a basic response + + return 0; +} + +/**************************************************************************** + * coord_command ****************************************************************************/ static int coord_command(FAR struct ieee_coord_s *coord) @@ -184,7 +193,7 @@ static int coord_command(FAR struct ieee_coord_s *coord) case IEEE802154_CMD_DATA_REQ : break; case IEEE802154_CMD_PANID_CONF_NOT : break; case IEEE802154_CMD_ORPHAN_NOT : break; - case IEEE802154_CMD_BEACON_REQ : break; + case IEEE802154_CMD_BEACON_REQ : return coord_command_beacon_req(coord); break; case IEEE802154_CMD_COORD_REALIGN : break; case IEEE802154_CMD_GTS_REQ : break; } @@ -192,7 +201,7 @@ static int coord_command(FAR struct ieee_coord_s *coord) } /**************************************************************************** - * coord_main + * coord_manage ****************************************************************************/ static int coord_manage(FAR struct ieee_coord_s *coord) @@ -219,67 +228,63 @@ static int coord_manage(FAR struct ieee_coord_s *coord) if(daddr == IEEE802154_DADDR_SHORT) { - rx->dpanid = rx->packet.data+index; + memcpy(&rx->daddr.panid, rx->packet.data+index, 2); index += 2; /* skip dest pan id */ - rx->daddr = rx->packet.data+index; + memcpy(&rx->daddr.addr.saddr, rx->packet.data+index, 2); index += 2; /* skip dest addr */ - rx->daddrlen = 2; + rx->daddr.len = 2; } else if(daddr == IEEE802154_DADDR_EXT) { - rx->dpanid = rx->packet.data+index; + memcpy(&rx->daddr.panid, rx->packet.data+index, 2); index += 2; /* skip dest pan id */ - rx->daddr = rx->packet.data+index; + memcpy(&rx->daddr.addr.eaddr, rx->packet.data+index, 8); index += 8; /* skip dest addr */ - rx->daddrlen = 8; + rx->daddr.len = 8; } else { - rx->dpanid = NULL; - rx->daddr = NULL; - rx->daddrlen = 0; + rx->daddr.len = 0; } if(saddr == IEEE802154_SADDR_SHORT) { if(rx->fc1 & IEEE802154_FC1_INTRA) { - rx->spanid = rx->dpanid; + rx->saddr.panid = rx->daddr.panid; } else { - rx->spanid = rx->packet.data+index; + memcpy(&rx->saddr.panid, rx->packet.data+index, 2); index += 2; /*skip dest pan id*/ } - rx->saddr = rx->packet.data+index; + memcpy(&rx->saddr.addr.saddr, rx->packet.data+index, 2); index += 2; /* skip dest addr */ - rx->saddrlen = 2; + rx->saddr.len = 2; } else if(saddr == IEEE802154_SADDR_EXT) { if(rx->fc1 & IEEE802154_FC1_INTRA) { - rx->spanid = rx->dpanid; + rx->saddr.panid = rx->daddr.panid; } else { - rx->spanid = rx->packet.data+index; + memcpy(&rx->saddr.panid, rx->packet.data+index, 2); index += 2; /*skip dest pan id*/ } - rx->saddr = rx->packet.data+index; + memcpy(&rx->saddr.addr.eaddr, rx->packet.data+index, 8); index += 8; /* skip dest addr */ - rx->saddrlen = 8; + rx->saddr.len = 8; } else { - rx->spanid = NULL; - rx->saddr = NULL; - rx->saddrlen = 0; + rx->saddr.len = 0; } rx->payload = rx->packet.data + index; - printf("SADDR len %d DADDR len %d\n", rx->saddrlen, rx->daddrlen); + printf("SADDR len %d DADDR len %d\n", rx->saddr.len, rx->daddr.len); switch(ftype) { case IEEE802154_FRAME_BEACON : coord_beacon (coord); break; @@ -295,31 +300,102 @@ static int coord_manage(FAR struct ieee_coord_s *coord) * coord_loop ****************************************************************************/ -static int coord_loop(FAR struct ieee_coord_s *coord) +static volatile int gRun; +static pthread_t gDaemonPid; + +void task_signal(int sig) { - int ret = 1; - FAR struct ieee_frame_s *rx = &coord->rxbuf; + gRun = 0; +} - printf("starting on chan %d, panid %04X\n", coord->chan, coord->panid); +/**************************************************************************** + * coord_initialize + ****************************************************************************/ - ieee802154_setchan (coord->fd, coord->chan ); - ieee802154_setpanid(coord->fd, coord->panid); - //ieee802154_setpromisc(coord->fd, TRUE); - - while(ret > 0) +static void coord_initialize(FAR struct ieee_coord_s *coord, FAR char *dev, FAR char *chan, FAR char *panid) +{ + int i; + coord->nclients = 0; + for (i = 0; i < CONFIG_IEEE802154_COORD_MAXCLIENTS; i++) { - ret = read(coord->fd, &rx->packet, sizeof(struct ieee802154_packet_s)); + coord->clients[i].pending.len = 0; + } + coord->chan = strtol(chan , NULL, 0); + coord->panid = strtol(panid, NULL, 0); + + coord->fd = open(dev, O_RDWR); + +} + +#define ACTION_STOP 1 +#define ACTION_PANID 2 + +struct message +{ + int action; + unsigned long param; +}; + +struct message g_message; + +/**************************************************************************** + * coord_task + ****************************************************************************/ +int coord_task(int s_argc, char **s_argv) +{ + FAR struct ieee_frame_s *rx = &g_coord.rxbuf; + int ret; + + coord_initialize(&g_coord, s_argv[3], s_argv[4], s_argv[5]); + + printf("IEEE 802.15.4 Coordinator started, chan %d, panid %04X, argc %d\n", g_coord.chan, g_coord.panid, s_argc); + + ieee802154_setchan (g_coord.fd, g_coord.chan ); + ieee802154_setpanid(g_coord.fd, g_coord.panid); + + if(g_coord.fd<0) + { + fprintf(stderr, "cannot open %s, errno=%d\n", s_argv[3], errno); + exit(ERROR); + } + gRun = 1; + + while(gRun) + { + ret = read(g_coord.fd, &rx->packet, sizeof(struct ieee802154_packet_s)); if(ret > 0) { - coord_manage(coord); + coord_manage(&g_coord); + } + if(ret < 0) + { + if(errno==4) //EINTR, signal received + { + switch(g_message.action) + { + case ACTION_STOP: + gRun = 0; + break; + + case ACTION_PANID: + g_coord.panid = (uint16_t)g_message.param; + ieee802154_setpanid(g_coord.fd, g_coord.panid); + break; + + default: + printf("received unknown message\n"); + } + } } } - return OK; + + printf("IEEE 802.15.4 Coordinator stopped\n"); + return 0; } /**************************************************************************** * coord_main - ****************************************************************************/ +11 ****************************************************************************/ #ifdef CONFIG_BUILD_KERNEL int main(int argc, FAR char *argv[]) @@ -327,21 +403,60 @@ int main(int argc, FAR char *argv[]) int coord_main(int argc, FAR char *argv[]) #endif { - printf("IEEE 802.15.4 Coordinator start\n"); - - if (argc<4) + + if(argc < 2) { - printf("coord [0x]\n"); - return ERROR; + printf("coord start | stop | panid\n"); + exit(1); } - - - coord_initialize(&g_coord, argv[1], argv[2], argv[3]); - if(g_coord.fd<0) + + if(!strcmp(argv[1], "start")) { - fprintf(stderr, "cannot open %s, errno=%d\n", argv[1], errno); - exit(ERROR); + if(argc != 5) + { + printf("coord start \n"); + exit(EXIT_FAILURE); + } + if(gRun) + { + printf("Already started.\n"); + exit(EXIT_FAILURE); + } + printf("IEEE 802.15.4 Coordinator starting...\n"); + gDaemonPid = task_create("coord", SCHED_PRIORITY_DEFAULT, + 1024, + coord_task, argv); } - - return coord_loop(&g_coord); + else if(!strcmp(argv[1], "stop")) + { + if(!gRun) + { +norun: + printf("Not started.\n"); + exit(EXIT_FAILURE); + } + printf("IEEE 802.15.4 Coordinator stopping...\n"); + g_message.action = ACTION_STOP; + kill(gDaemonPid, SIGUSR1); + } + else if(!strcmp(argv[1], "status")) + { + printf("IEEE 802.15.4 Coordinator %s.\n", gRun?"started":"stopped"); + } + else if(!strcmp(argv[1], "panid")) + { + if(argc != 3) + { + printf("coord panid \n"); + exit(EXIT_FAILURE); + } + if(!gRun) + { + goto norun; + } + g_message.action = ACTION_PANID; + g_message.param = strtol(argv[2], NULL, 0); + kill(gDaemonPid, SIGUSR1); + } + return EXIT_SUCCESS; } From 6fa4bd6a951993eb6dc1d469a93d0e1be6c3a87e Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 11:05:23 +0200 Subject: [PATCH 09/27] avoid useless chan changes --- ieee802154/i8sak/i8sak_main.c | 92 ++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index 08b27c352..55b26b23e 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -273,24 +273,25 @@ static int display(FAR struct ieee802154_packet_s *pack) * Description : * Listen for all packets with a valid CRC on a given channel. ****************************************************************************/ +struct sniffargs +{ + int fd; +}; -static int sniff(int fd, int chan) +static void* sniff(void *arg) { int ret; - ret = setchan(fd, chan); - if (ret<0) - { - return ret; - } + struct sniffargs *sa = (struct sniffargs*)arg; + int fd = sa->fd; ret = ioctl(fd, MAC854IOCSPROMISC, TRUE); if (ret<0) { printf("Device is not an IEEE 802.15.4 interface!\n"); - return ret; + return (void*)ret; } - printf("Listening on channel %d in promisc mode.\n",chan); + printf("Listening...\n"); while (1) { ret = read(fd, &gRxPacket, sizeof(struct ieee802154_packet_s)); @@ -300,6 +301,11 @@ static int sniff(int fd, int chan) { continue; } + if (errno == EINTR) + { + printf("read: interrupted\n"); + break; + } else { printf("read: errno=%d\n",errno); @@ -311,7 +317,14 @@ static int sniff(int fd, int chan) display(&gRxPacket); } - return ret; + ret = ioctl(fd, MAC854IOCSPROMISC, FALSE); + if (ret<0) + { + printf("Device is not an IEEE 802.15.4 interface!\n"); + return (void*)ret; + } + + return (void*)ret; } /**************************************************************************** @@ -321,16 +334,10 @@ static int sniff(int fd, int chan) * Transmit a frame. ****************************************************************************/ -static int tx(int fd, int chan, FAR struct ieee802154_packet_s *pack) +static int tx(int fd, FAR struct ieee802154_packet_s *pack) { int i,ret; - ret = setchan(fd, chan); - if (ret<0) - { - return ret; - } - for (i = 0; i < pack->len; i++) { printf("%02X", pack->data[i]); @@ -361,12 +368,12 @@ int usage(void) printf("i8 \n" " scan\n" " dump\n" - " snif \n" - " stat\n" " chan \n" + " snif\n" + " stat\n" " edth \n" " csth \n" - " tx \n" + " tx \n" ); return ERROR; } @@ -465,14 +472,15 @@ int i8_main(int argc, char *argv[]) } else if (!strcmp(argv[2], "snif")) { - ret = sniff(fd,arg); + struct sniffargs args; + args.fd = fd; + ret = (int)sniff(&args); } else if (!strcmp(argv[2], "tx")) { int id=0; - unsigned long ch = arg; - int len = strlen(argv[4]); - FAR char *ptr = argv[4]; + int len = strlen(argv[3]); + FAR char *ptr = argv[3]; if (len & 1) { @@ -500,7 +508,43 @@ data_error: } gTxPacket.len = id; - ret = tx(fd, ch, &gTxPacket); + ret = tx(fd, &gTxPacket); + } + else if (!strcmp(argv[2], "beacons")) + { + struct sniffargs args; + pthread_t pth; + int i; + + args.fd = fd; + + pthread_create(&pth, NULL, sniff, &args); + + //beacon request + gTxPacket.len = 0; + gTxPacket.data[gTxPacket.len++] = 0x03; //mac command, no ack, no panid compression + gTxPacket.data[gTxPacket.len++] = 0x08; //short destination address, no source address + gTxPacket.data[gTxPacket.len++] = 0; //seq + gTxPacket.data[gTxPacket.len++] = 0xFF; //panid + gTxPacket.data[gTxPacket.len++] = 0xFF; + gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr + gTxPacket.data[gTxPacket.len++] = 0xFF; +// for(i=1;i<3;i++) + while(1) + { + int ch; + for(ch=11; ch<27; ch++) + { + setchan(fd, ch); + printf("txbeacon on chan %d\n", ch); + gTxPacket.data[2] = i; //seq + tx(fd, &gTxPacket); + sleep(1); + } + i++; if(i==256) i=0; + } + pthread_kill(pth, SIGUSR1); + } else { From 51c4ec5d22f98f2abec44b88265f268753b31e45 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 11:28:12 +0200 Subject: [PATCH 10/27] move address parsing to a lib routine. --- ieee802154/common/Makefile | 1 + ieee802154/common/ieee802154_addrparse.c | 126 +++++++++++++++++++++++ ieee802154/common/ieee802154_addrtostr.c | 83 +++++++++++++++ ieee802154/coord/coord_main.c | 103 ++++-------------- ieee802154/i8sak/i8sak_main.c | 1 + include/ieee802154/ieee802154.h | 22 +++- 6 files changed, 252 insertions(+), 84 deletions(-) create mode 100644 ieee802154/common/ieee802154_addrparse.c create mode 100644 ieee802154/common/ieee802154_addrtostr.c diff --git a/ieee802154/common/Makefile b/ieee802154/common/Makefile index 045d2b1f5..22fdbdad8 100644 --- a/ieee802154/common/Makefile +++ b/ieee802154/common/Makefile @@ -43,6 +43,7 @@ ASRCS = CSRCS = ieee802154_setchan.c ieee802154_setpanid.c CSRCS += ieee802154_seteaddr.c ieee802154_setsaddr.c CSRCS += ieee802154_setpromisc.c +CSRCS += ieee802154_addrparse.c ieee802154_addrtostr.c # diff --git a/ieee802154/common/ieee802154_addrparse.c b/ieee802154/common/ieee802154_addrparse.c new file mode 100644 index 000000000..02b15c16a --- /dev/null +++ b/ieee802154/common/ieee802154_addrparse.c @@ -0,0 +1,126 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_addrparse.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_addrparse(FAR struct ieee802154_packet_s *packet, + FAR struct ieee802154_addr_s *dest, + FAR struct ieee802154_addr_s *src) +{ + uint8_t fc1, fc2, daddr, saddr; + int index=3; + + /* read fc */ + fc1 = packet->data[0]; + fc2 = packet->data[1]; + daddr = fc2 & IEEE802154_FC2_DADDR; + saddr = fc2 & IEEE802154_FC2_SADDR; + + /* decode dest addr */ + + if(daddr == IEEE802154_DADDR_SHORT) + { + memcpy(&dest->ia_panid, packet->data+index, 2); + index += 2; /* skip dest pan id */ + memcpy(&dest->ia_saddr, packet->data+index, 2); + index += 2; /* skip dest addr */ + dest->ia_len = 2; + } + else if(daddr == IEEE802154_DADDR_EXT) + { + memcpy(&dest->ia_panid, packet->data+index, 2); + index += 2; /* skip dest pan id */ + memcpy(dest->ia_eaddr, packet->data+index, 8); + index += 8; /* skip dest addr */ + dest->ia_len = 8; + } + else if(daddr == IEEE802154_DADDR_NONE) + { + dest->ia_len = 0; + } + else + { + return -EINVAL; + } + + /* decode source pan id according to compression */ + if( (saddr == IEEE802154_SADDR_SHORT) || (saddr == IEEE802154_SADDR_EXT) ) + { + if(fc1 & IEEE802154_FC1_INTRA) + { + src->ia_panid = dest->ia_panid; + } + else + { + memcpy(&src->ia_panid, packet->data+index, 2); + index += 2; /*skip dest pan id*/ + } + } + + /* decode source addr */ + + if(saddr == IEEE802154_SADDR_SHORT) + { + memcpy(&src->ia_saddr, packet->data+index, 2); + index += 2; /* skip dest addr */ + src->ia_len = 2; + } + if(saddr == IEEE802154_SADDR_EXT) + { + memcpy(src->ia_eaddr, packet->data+index, 8); + index += 8; /* skip dest addr */ + src->ia_len = 8; + } + else if(saddr == IEEE802154_SADDR_NONE) + { + src->ia_len = 0; + } + else + { + return -EINVAL; + } + + return index; +} + diff --git a/ieee802154/common/ieee802154_addrtostr.c b/ieee802154/common/ieee802154_addrtostr.c new file mode 100644 index 000000000..743b2fd55 --- /dev/null +++ b/ieee802154/common/ieee802154_addrtostr.c @@ -0,0 +1,83 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_addrtostr.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_addrtostr(FAR char *buf, int len, FAR struct ieee802154_addr_s *addr) +{ +#ifndef CONFIG_BIG_ENDIAN + uint16_t panid = ((addr->ia_panid & 0xff)<<8) | ((addr->ia_panid>>8) & 0xff); +#else + uint16_t panid = addr->ia_panid; +#endif + + if(addr->ia_len == 0) + { + return snprintf(buf, len, "none"); + } + else if(addr->ia_len == 2) + { +#ifndef CONFIG_BIG_ENDIAN + uint16_t saddr = ((addr->ia_saddr & 0xff)<<8) | ((addr->ia_saddr>>8) & 0xff); +#else + uint16_t saddr = addr->ia_saddr; +#endif + return snprintf(buf, len, "%04X/%04X", panid, saddr); + } + else if(addr->ia_len == 8) + { + int i; + int off = snprintf(buf, len, "%04X/", panid); + for(i=0; i<8; i++) + { + off += snprintf(buf+off, len-off, "%02X", addr->ia_eaddr[i]); + } + return off; + } + else + { + return snprintf(buf,len,""); + } + return -1; +} + diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c index 0ac396555..89e582f11 100644 --- a/ieee802154/coord/coord_main.c +++ b/ieee802154/coord/coord_main.c @@ -90,25 +90,13 @@ struct ieee_client_s struct ieee802154_packet_s pending; /* pending packet for device */ }; -struct ieee_addr -{ - uint8_t len; - uint16_t panid; - union - { - uint16_t saddr; - uint8_t eaddr[8]; - } addr; -}; - struct ieee_frame_s { struct ieee802154_packet_s packet; - uint8_t fc1; - uint8_t fc2; + struct ieee802154_addr_s dest; + struct ieee802154_addr_s src; uint8_t seq; - struct ieee_addr daddr; - struct ieee_addr saddr; + uint8_t plen; uint8_t *payload; }; @@ -207,84 +195,37 @@ static int coord_command(FAR struct ieee_coord_s *coord) static int coord_manage(FAR struct ieee_coord_s *coord) { /* Decode frame type */ - uint8_t ftype, saddr, daddr; - int index; + uint8_t fc1, ftype; + int hlen; + char buf[IEEE802154_ADDRSTRLEN+1]; FAR struct ieee_frame_s *rx = &coord->rxbuf; int i; - for(i=0; ipacket.len; i++) printf("%02X", rx->packet.data[i]); - printf("\n"); - rx->fc1 = rx->packet.data[0]; - rx->fc2 = rx->packet.data[1]; + fc1 = rx->packet.data[0]; rx->seq = rx->packet.data[2]; - ftype = rx->fc1 & IEEE802154_FC1_FTYPE; - daddr = rx->fc2 & IEEE802154_FC2_DADDR; - saddr = rx->fc2 & IEEE802154_FC2_SADDR; - - index = 3; - - if(daddr == IEEE802154_DADDR_SHORT) + hlen = ieee802154_addrparse(&rx->packet, &rx->dest, &rx->src); + if(hlen<0) { - memcpy(&rx->daddr.panid, rx->packet.data+index, 2); - index += 2; /* skip dest pan id */ - memcpy(&rx->daddr.addr.saddr, rx->packet.data+index, 2); - index += 2; /* skip dest addr */ - rx->daddr.len = 2; - } - else if(daddr == IEEE802154_DADDR_EXT) - { - memcpy(&rx->daddr.panid, rx->packet.data+index, 2); - index += 2; /* skip dest pan id */ - memcpy(&rx->daddr.addr.eaddr, rx->packet.data+index, 8); - index += 8; /* skip dest addr */ - rx->daddr.len = 8; - } - else - { - rx->daddr.len = 0; + printf("invalid packet\n"); + return 1; } - if(saddr == IEEE802154_SADDR_SHORT) - { - if(rx->fc1 & IEEE802154_FC1_INTRA) - { - rx->saddr.panid = rx->daddr.panid; - } - else - { - memcpy(&rx->saddr.panid, rx->packet.data+index, 2); - index += 2; /*skip dest pan id*/ - } - memcpy(&rx->saddr.addr.saddr, rx->packet.data+index, 2); - index += 2; /* skip dest addr */ - rx->saddr.len = 2; - } - else if(saddr == IEEE802154_SADDR_EXT) - { - if(rx->fc1 & IEEE802154_FC1_INTRA) - { - rx->saddr.panid = rx->daddr.panid; - } - else - { - memcpy(&rx->saddr.panid, rx->packet.data+index, 2); - index += 2; /*skip dest pan id*/ - } - memcpy(&rx->saddr.addr.eaddr, rx->packet.data+index, 8); - index += 8; /* skip dest addr */ - rx->saddr.len = 8; - } - else - { - rx->saddr.len = 0; - } + rx->payload = rx->packet.data + hlen; + rx->plen = rx->packet.len - hlen; - rx->payload = rx->packet.data + index; + ftype = fc1 & IEEE802154_FC1_FTYPE; + + ieee802154_addrtostr(buf,sizeof(buf),&rx->src); + printf("[%s -> ", buf); + ieee802154_addrtostr(buf,sizeof(buf),&rx->dest); + printf("%s] ", buf); + + for(i=0; iplen; i++) printf("%02X", rx->payload[i]); + printf("\n"); - printf("SADDR len %d DADDR len %d\n", rx->saddr.len, rx->daddr.len); switch(ftype) { case IEEE802154_FRAME_BEACON : coord_beacon (coord); break; diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index 55b26b23e..42fc6501d 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -529,6 +529,7 @@ data_error: gTxPacket.data[gTxPacket.len++] = 0xFF; gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr gTxPacket.data[gTxPacket.len++] = 0xFF; + gTxPacket.data[gTxPacket.len++] = 0x07; //BEACON_REQ // for(i=1;i<3;i++) while(1) { diff --git a/include/ieee802154/ieee802154.h b/include/ieee802154/ieee802154.h index 6d4602db6..60ae08677 100644 --- a/include/ieee802154/ieee802154.h +++ b/include/ieee802154/ieee802154.h @@ -39,10 +39,26 @@ #include #include -int ieee802154_setchan (int fd, uint8_t chan); +struct ieee802154_addr_s +{ + uint8_t ia_len; /* structure length NOT including panid, so 2/8*/ + uint16_t ia_panid; + union { + uint16_t _ia_saddr; + uint8_t _ia_eaddr[8]; + } ia_addr; +#define ia_saddr ia_addr._ia_saddr +#define ia_eaddr ia_addr._ia_eaddr +}; +#define IEEE802154_ADDRSTRLEN 22 /* (4+1+8*2) */ + +int ieee802154_setchan (int fd, uint8_t chan); int ieee802154_setpanid (int fd, uint16_t panid); int ieee802154_setsaddr (int fd, uint16_t saddr); -int ieee802154_seteaddr (int fd, uint8_t *chan); -int ieee802154_setpromisc(int fd, bool promisc); +int ieee802154_seteaddr (int fd, FAR uint8_t *chan); +int ieee802154_setpromisc(int fd, bool promisc); + +int ieee802154_addrparse(FAR struct ieee802154_packet_s *inPacket, FAR struct ieee802154_addr_s *dest, FAR struct ieee802154_addr_s *src); +int ieee802154_addrtostr(FAR char *buf, int len, FAR struct ieee802154_addr_s *addr); #endif /*__APPS_INCLUDE_IEEE802154_IEEE802154_H */ From d80fd440b6e343070fb235e3f1898d4d9dc869ce Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 14:34:35 +0200 Subject: [PATCH 11/27] add support for dev mode change and address encoding --- ieee802154/common/ieee802154_addrstore.c | 118 ++++++++++++++++++++++ ieee802154/common/ieee802154_setdevmode.c | 55 ++++++++++ include/ieee802154/ieee802154.h | 2 + 3 files changed, 175 insertions(+) create mode 100644 ieee802154/common/ieee802154_addrstore.c create mode 100644 ieee802154/common/ieee802154_setdevmode.c diff --git a/ieee802154/common/ieee802154_addrstore.c b/ieee802154/common/ieee802154_addrstore.c new file mode 100644 index 000000000..121a597cf --- /dev/null +++ b/ieee802154/common/ieee802154_addrstore.c @@ -0,0 +1,118 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_addrparse.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_addrstore(FAR struct ieee802154_packet_s *packet, + FAR struct ieee802154_addr_s *dest, + FAR struct ieee802154_addr_s *src) +{ + int index=3; + + /* encode dest addr */ + + if(dest->ia_len == 2) + { + memcpy(packet->data+index, &dest->ia_panid, 2); + index += 2; /* skip dest pan id */ + memcpy(packet->data+index, &dest->ia_saddr, 2); + index += 2; /* skip dest addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_SHORT; + } + else if(dest->ia_len == 8) + { + memcpy(packet->data+index, &dest->ia_panid, 2); + index += 2; /* skip dest pan id */ + memcpy(packet->data+index, dest->ia_eaddr, 8); + index += 8; /* skip dest addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_EXT; + } + else if(dest->ia_len == 0) + { + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_NONE; + } + else + { + return -EINVAL; + } + + /* encode source pan id according to compression */ + + if( (dest->ia_len != 0) && (src->ia_len != 0) && (dest->ia_panid == src->ia_panid) ) + { + packet->data[0] |= IEEE802154_FC1_INTRA; + } + else + { + memcpy(packet->data+index, &src->ia_panid, 2); + index += 2; /*skip dest pan id*/ + packet->data[0] &= ~IEEE802154_FC1_INTRA; + } + + /* encode source addr */ + + if(src->ia_len == 2) + { + memcpy(packet->data+index, &src->ia_saddr, 2); + index += 2; /* skip src addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_SHORT; + } + else if(src->ia_len == 8) + { + memcpy(packet->data+index, src->ia_eaddr, 8); + index += 8; /* skip src addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_EXT; + } + else if(dest->ia_len == 0) + { + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_NONE; + } + else + { + return -EINVAL; + } + + return index; +} + diff --git a/ieee802154/common/ieee802154_setdevmode.c b/ieee802154/common/ieee802154_setdevmode.c new file mode 100644 index 000000000..6eae328b4 --- /dev/null +++ b/ieee802154/common/ieee802154_setdevmode.c @@ -0,0 +1,55 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_setdevmode.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_setdevmode(int fd, uint8_t devmode) +{ + int ret = ioctl(fd, MAC854IOCSDEVMODE, (unsigned long)devmode ); + if (ret<0) + { + printf("MAC854IOCSDEVMODE failed\n"); + } + return ret; +} diff --git a/include/ieee802154/ieee802154.h b/include/ieee802154/ieee802154.h index 60ae08677..9ba8590dd 100644 --- a/include/ieee802154/ieee802154.h +++ b/include/ieee802154/ieee802154.h @@ -57,8 +57,10 @@ int ieee802154_setpanid (int fd, uint16_t panid); int ieee802154_setsaddr (int fd, uint16_t saddr); int ieee802154_seteaddr (int fd, FAR uint8_t *chan); int ieee802154_setpromisc(int fd, bool promisc); +int ieee802154_setdevmode(int fd, uint8_t devmode); int ieee802154_addrparse(FAR struct ieee802154_packet_s *inPacket, FAR struct ieee802154_addr_s *dest, FAR struct ieee802154_addr_s *src); +int ieee802154_addrstore(FAR struct ieee802154_packet_s *inPacket, FAR struct ieee802154_addr_s *dest, FAR struct ieee802154_addr_s *src); int ieee802154_addrtostr(FAR char *buf, int len, FAR struct ieee802154_addr_s *addr); #endif /*__APPS_INCLUDE_IEEE802154_IEEE802154_H */ From b533d4b12d3fccfd7d239f7303dd8e0e4e016add Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 14:35:13 +0200 Subject: [PATCH 12/27] fix setsaddr --- ieee802154/common/ieee802154_setsaddr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ieee802154/common/ieee802154_setsaddr.c b/ieee802154/common/ieee802154_setsaddr.c index 7ca225897..a820c77fe 100644 --- a/ieee802154/common/ieee802154_setsaddr.c +++ b/ieee802154/common/ieee802154_setsaddr.c @@ -46,7 +46,7 @@ int ieee802154_setsaddr(int fd, uint16_t saddr) { - int ret = ioctl(fd, MAC854IOCSCHAN, (unsigned long)saddr ); + int ret = ioctl(fd, MAC854IOCSSADDR, (unsigned long)saddr ); if (ret<0) { printf("MAC854IOCSSADDR failed\n"); From a867dfb3bb0ad9a8e9e7027d1040b042514fbb35 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 14:35:38 +0200 Subject: [PATCH 13/27] add forgotten makefile in previous commit --- ieee802154/common/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ieee802154/common/Makefile b/ieee802154/common/Makefile index 22fdbdad8..a713436b8 100644 --- a/ieee802154/common/Makefile +++ b/ieee802154/common/Makefile @@ -42,8 +42,8 @@ include $(APPDIR)/Make.defs ASRCS = CSRCS = ieee802154_setchan.c ieee802154_setpanid.c CSRCS += ieee802154_seteaddr.c ieee802154_setsaddr.c -CSRCS += ieee802154_setpromisc.c -CSRCS += ieee802154_addrparse.c ieee802154_addrtostr.c +CSRCS += ieee802154_setpromisc.c ieee802154_setdevmode.c +CSRCS += ieee802154_addrparse.c ieee802154_addrstore.c ieee802154_addrtostr.c # From 2b4fa91f373daad75978c1c937c8d5fcceb537b4 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 14:37:01 +0200 Subject: [PATCH 14/27] only sniff uses promisc mode, beacon scan does not. --- ieee802154/i8sak/i8sak_main.c | 65 ++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index 42fc6501d..dd3ee663d 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -61,6 +61,7 @@ #include #include #include +#include /**************************************************************************** * Definitions @@ -258,12 +259,33 @@ static int status(int fd) static int display(FAR struct ieee802154_packet_s *pack) { int i; - printf("len=%3u rssi=%3u lqi=%3u [", pack->len, pack->rssi, pack->lqi); - for (i = 0; i < pack->len; i++) + int hlen=0; + char buf[IEEE802154_ADDRSTRLEN+1]; + struct ieee802154_addr_s dest,src; + + printf("rssi=%3u lqi=%3u ", pack->rssi, pack->lqi); + + hlen = ieee802154_addrparse(pack, &dest, &src); + if(hlen<0) { - printf("%02X", pack->data[i]); + printf("invalid header \n"); + hlen = 0; } - printf("]\n"); + else + { + ieee802154_addrtostr(buf,sizeof(buf),&src); + printf("[%s -> ", buf); + ieee802154_addrtostr(buf,sizeof(buf),&dest); + printf("%s] ", buf); + } + + printf("len=%d ", pack->len - hlen); + + for (i = 0; i < pack->len - hlen; i++) + { + printf("%02X", pack->data[i+hlen]); + } + printf("\n"); return 0; } @@ -284,13 +306,6 @@ static void* sniff(void *arg) struct sniffargs *sa = (struct sniffargs*)arg; int fd = sa->fd; - ret = ioctl(fd, MAC854IOCSPROMISC, TRUE); - if (ret<0) - { - printf("Device is not an IEEE 802.15.4 interface!\n"); - return (void*)ret; - } - printf("Listening...\n"); while (1) { @@ -317,12 +332,6 @@ static void* sniff(void *arg) display(&gRxPacket); } - ret = ioctl(fd, MAC854IOCSPROMISC, FALSE); - if (ret<0) - { - printf("Device is not an IEEE 802.15.4 interface!\n"); - return (void*)ret; - } return (void*)ret; } @@ -472,9 +481,25 @@ int i8_main(int argc, char *argv[]) } else if (!strcmp(argv[2], "snif")) { - struct sniffargs args; - args.fd = fd; - ret = (int)sniff(&args); + struct sniffargs args; + + ret = ioctl(fd, MAC854IOCSPROMISC, TRUE); + if (ret<0) + { + printf("Device is not an IEEE 802.15.4 interface!\n"); + return (void*)ret; + } + + args.fd = fd; + ret = (int)sniff(&args); + + ret = ioctl(fd, MAC854IOCSPROMISC, FALSE); + if (ret<0) + { + printf("Device is not an IEEE 802.15.4 interface!\n"); + return (void*)ret; + } + } else if (!strcmp(argv[2], "tx")) { From dee320949dcafeb635e185df858f62c3cbe00083 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 14:37:30 +0200 Subject: [PATCH 15/27] coord is now sending replies to beacon requests --- ieee802154/coord/coord_main.c | 75 +++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c index 89e582f11..234d67527 100644 --- a/ieee802154/coord/coord_main.c +++ b/ieee802154/coord/coord_main.c @@ -102,12 +102,14 @@ struct ieee_frame_s struct ieee_coord_s { - int fd; /* Device handle */ - uint8_t chan; /* PAN channel */ - uint16_t panid; /* PAN ID */ - uint8_t nclients; /* Number of coordinated clients */ - struct ieee_frame_s rxbuf; /* General rx buffer */ - struct ieee_client_s clients[CONFIG_IEEE802154_COORD_MAXCLIENTS]; + int fd; /* Device handle */ + uint8_t chan; /* PAN channel */ + struct ieee802154_addr_s addr; /* coord own address */ + uint8_t nclients; /* Number of coordinated clients */ + struct ieee_frame_s rxbuf; /* General rx buffer */ + struct ieee_frame_s txbuf; /* General tx buffer */ + struct ieee_client_s clients[CONFIG_IEEE802154_COORD_MAXCLIENTS]; + uint8_t macBSN; }; /**************************************************************************** @@ -121,6 +123,7 @@ static struct ieee_coord_s g_coord; static int coord_beacon(FAR struct ieee_coord_s *coord) { + FAR struct ieee_frame_s *rx = &coord->rxbuf; printf("Beacon!\n"); return 0; } @@ -151,15 +154,49 @@ static int coord_ack(FAR struct ieee_coord_s *coord) static int coord_command_beacon_req(FAR struct ieee_coord_s *coord) { - FAR struct ieee_frame_s *rx = &coord->rxbuf; - - //check the request looks good - + FAR struct ieee_frame_s *rx = &coord->rxbuf; + FAR struct ieee802154_packet_s *tx = &coord->txbuf.packet; + FAR struct ieee802154_addr_s dest; + printf("Beacon request\n"); - //compose a basic response + /* check command */ + + /* build response */ + tx->len = 0; - return 0; + /* frame control */ + tx->data[tx->len++] = 0x00; /* beacon, no ack */ + tx->data[tx->len++] = 0x00; /* updated later */ + + /* seq */ + tx->data[tx->len++] = coord->macBSN; + coord->macBSN++; + + /* adressing */ + dest.ia_len = 0; /*no dest address*/ + tx->len = ieee802154_addrstore(tx, &dest, &coord->addr); + + /* superframe spec */ + tx->data[tx->len++] = 0xff; + tx->data[tx->len++] = 0xcf; + + /* GTS fields */ + tx->data[tx->len++] = 0x00; + + /* pending addresses */ + tx->data[tx->len++] = 0x00; + + /* payload */ + tx->data[tx->len++] = 'F'; + tx->data[tx->len++] = '4'; + tx->data[tx->len++] = 'G'; + tx->data[tx->len++] = 'R'; + tx->data[tx->len++] = 'X'; + tx->data[tx->len++] = '/'; + tx->data[tx->len++] = '0'; + + return write(coord->fd, tx, sizeof(struct ieee802154_packet_s)); } /**************************************************************************** @@ -262,7 +299,10 @@ static void coord_initialize(FAR struct ieee_coord_s *coord, FAR char *dev, FAR coord->clients[i].pending.len = 0; } coord->chan = strtol(chan , NULL, 0); - coord->panid = strtol(panid, NULL, 0); + + coord->addr.ia_len = 2; + coord->addr.ia_panid = strtol(panid, NULL, 0); + coord->addr.ia_saddr = 0x0001; coord->fd = open(dev, O_RDWR); @@ -289,10 +329,11 @@ int coord_task(int s_argc, char **s_argv) coord_initialize(&g_coord, s_argv[3], s_argv[4], s_argv[5]); - printf("IEEE 802.15.4 Coordinator started, chan %d, panid %04X, argc %d\n", g_coord.chan, g_coord.panid, s_argc); + printf("IEEE 802.15.4 Coordinator started, chan %d, panid %04X, argc %d\n", g_coord.chan, g_coord.addr.ia_panid, s_argc); ieee802154_setchan (g_coord.fd, g_coord.chan ); - ieee802154_setpanid(g_coord.fd, g_coord.panid); + ieee802154_setsaddr(g_coord.fd, g_coord.addr.ia_saddr); + ieee802154_setpanid(g_coord.fd, g_coord.addr.ia_panid); if(g_coord.fd<0) { @@ -319,8 +360,8 @@ int coord_task(int s_argc, char **s_argv) break; case ACTION_PANID: - g_coord.panid = (uint16_t)g_message.param; - ieee802154_setpanid(g_coord.fd, g_coord.panid); + g_coord.addr.ia_panid = (uint16_t)g_message.param; + ieee802154_setpanid(g_coord.fd, g_coord.addr.ia_panid); break; default: From c57fd24078279ec81d936223d7d84d38e6f0dd16 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 17:21:20 +0200 Subject: [PATCH 16/27] add cca support in common lib --- ieee802154/common/ieee802154_getcca.c | 57 +++++++++++++++++++++++++++ ieee802154/common/ieee802154_setcca.c | 56 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 ieee802154/common/ieee802154_getcca.c create mode 100644 ieee802154/common/ieee802154_setcca.c diff --git a/ieee802154/common/ieee802154_getcca.c b/ieee802154/common/ieee802154_getcca.c new file mode 100644 index 000000000..5cdcb6ba4 --- /dev/null +++ b/ieee802154/common/ieee802154_getcca.c @@ -0,0 +1,57 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_setchan.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_getcca(int fd, FAR struct ieee802154_cca_s *cca) +{ + int ret = ioctl(fd, MAC854IOCGCCA, (unsigned long)cca ); + if (ret<0) + { + printf("MAC854IOCGCCA failed\n"); + } + return ret; +} + diff --git a/ieee802154/common/ieee802154_setcca.c b/ieee802154/common/ieee802154_setcca.c new file mode 100644 index 000000000..32515b879 --- /dev/null +++ b/ieee802154/common/ieee802154_setcca.c @@ -0,0 +1,56 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_setchan.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_setcca(int fd, FAR struct ieee802154_cca_s *cca) +{ + int ret = ioctl(fd, MAC854IOCSCCA, (unsigned long)cca ); + if (ret<0) + { + printf("MAC854IOCSCCA failed\n"); + } + return ret; +} From 90d31d266665508940215eba5c6000aa0d2ae6bc Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 17:21:47 +0200 Subject: [PATCH 17/27] fix bug that prevented correct address parsing --- ieee802154/common/ieee802154_addrparse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ieee802154/common/ieee802154_addrparse.c b/ieee802154/common/ieee802154_addrparse.c index 02b15c16a..91ee37d12 100644 --- a/ieee802154/common/ieee802154_addrparse.c +++ b/ieee802154/common/ieee802154_addrparse.c @@ -106,7 +106,7 @@ int ieee802154_addrparse(FAR struct ieee802154_packet_s *packet, index += 2; /* skip dest addr */ src->ia_len = 2; } - if(saddr == IEEE802154_SADDR_EXT) + else if(saddr == IEEE802154_SADDR_EXT) { memcpy(src->ia_eaddr, packet->data+index, 8); index += 8; /* skip dest addr */ From 0ed2a4491fdac69187bbf1eaef21edbff42ac918 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 17:22:19 +0200 Subject: [PATCH 18/27] forgot makefile in previous commit --- ieee802154/common/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/ieee802154/common/Makefile b/ieee802154/common/Makefile index a713436b8..52079408f 100644 --- a/ieee802154/common/Makefile +++ b/ieee802154/common/Makefile @@ -43,6 +43,7 @@ ASRCS = CSRCS = ieee802154_setchan.c ieee802154_setpanid.c CSRCS += ieee802154_seteaddr.c ieee802154_setsaddr.c CSRCS += ieee802154_setpromisc.c ieee802154_setdevmode.c +CSRCS += ieee802154_setcca.c ieee802154_getcca.c CSRCS += ieee802154_addrparse.c ieee802154_addrstore.c ieee802154_addrtostr.c # From 329c680d609244c83e2cdce42818065b4b92fdd3 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 17:24:22 +0200 Subject: [PATCH 19/27] debug response beacon set mrf mode to pan coordinator --- ieee802154/coord/coord_main.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c index 234d67527..9d740e1e5 100644 --- a/ieee802154/coord/coord_main.c +++ b/ieee802154/coord/coord_main.c @@ -157,6 +157,7 @@ static int coord_command_beacon_req(FAR struct ieee_coord_s *coord) FAR struct ieee_frame_s *rx = &coord->rxbuf; FAR struct ieee802154_packet_s *tx = &coord->txbuf.packet; FAR struct ieee802154_addr_s dest; + int i; printf("Beacon request\n"); @@ -196,6 +197,9 @@ static int coord_command_beacon_req(FAR struct ieee_coord_s *coord) tx->data[tx->len++] = '/'; tx->data[tx->len++] = '0'; + printf("Beacon: "); + for(i=0;ilen;i++) printf("%02X", tx->data[i]); + printf("\n"); return write(coord->fd, tx, sizeof(struct ieee802154_packet_s)); } @@ -331,9 +335,10 @@ int coord_task(int s_argc, char **s_argv) printf("IEEE 802.15.4 Coordinator started, chan %d, panid %04X, argc %d\n", g_coord.chan, g_coord.addr.ia_panid, s_argc); - ieee802154_setchan (g_coord.fd, g_coord.chan ); - ieee802154_setsaddr(g_coord.fd, g_coord.addr.ia_saddr); - ieee802154_setpanid(g_coord.fd, g_coord.addr.ia_panid); + ieee802154_setchan (g_coord.fd , g_coord.chan ); + ieee802154_setsaddr(g_coord.fd , g_coord.addr.ia_saddr); + ieee802154_setpanid(g_coord.fd , g_coord.addr.ia_panid); + ieee802154_setdevmode(g_coord.fd, IEEE802154_MODE_PANCOORD); if(g_coord.fd<0) { From 1653cb628526464196e41019ddd3475049c97de2 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 17:25:08 +0200 Subject: [PATCH 20/27] add cca declares to common header --- include/ieee802154/ieee802154.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/ieee802154/ieee802154.h b/include/ieee802154/ieee802154.h index 9ba8590dd..c7b41db57 100644 --- a/include/ieee802154/ieee802154.h +++ b/include/ieee802154/ieee802154.h @@ -59,6 +59,9 @@ int ieee802154_seteaddr (int fd, FAR uint8_t *chan); int ieee802154_setpromisc(int fd, bool promisc); int ieee802154_setdevmode(int fd, uint8_t devmode); +int ieee802154_setcca(int fd, FAR struct ieee802154_cca_s *cca); +int ieee802154_getcca(int fd, FAR struct ieee802154_cca_s *cca); + int ieee802154_addrparse(FAR struct ieee802154_packet_s *inPacket, FAR struct ieee802154_addr_s *dest, FAR struct ieee802154_addr_s *src); int ieee802154_addrstore(FAR struct ieee802154_packet_s *inPacket, FAR struct ieee802154_addr_s *dest, FAR struct ieee802154_addr_s *src); int ieee802154_addrtostr(FAR char *buf, int len, FAR struct ieee802154_addr_s *addr); From 7a769509f06bcf359cfadd1de4a11c9f61e6bf4d Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 10 Aug 2015 17:25:31 +0200 Subject: [PATCH 21/27] move cca routines to common lib improve beacons scan display --- ieee802154/i8sak/i8sak_main.c | 95 +++++++++++++---------------------- 1 file changed, 36 insertions(+), 59 deletions(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index dd3ee663d..eec521fa7 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -73,36 +73,7 @@ static struct ieee802154_packet_s gRxPacket; static struct ieee802154_packet_s gTxPacket; - -int setchan(int fd, uint8_t chan) -{ - int ret = ioctl(fd, MAC854IOCSCHAN, (unsigned long)chan ); - if (ret<0) - { - printf("MAC854IOCSCHAN failed\n"); - } - return ret; -} - -int setcca(int fd, FAR struct ieee802154_cca_s *cca) -{ - int ret = ioctl(fd, MAC854IOCSCCA, (unsigned long)cca ); - if (ret<0) - { - printf("MAC854IOCSCCA failed\n"); - } - return ret; -} - -int getcca(int fd, FAR struct ieee802154_cca_s *cca) -{ - int ret = ioctl(fd, MAC854IOCGCCA, (unsigned long)cca ); - if (ret<0) - { - printf("MAC854IOCGCCA failed\n"); - } - return ret; -} +static uint8_t gChan; uint8_t levels[16]; uint8_t disp[16]; @@ -121,7 +92,7 @@ int scan(int fd) { for (chan=0; chan<16; chan++) { - ret = setchan(fd, chan+11); + ret = ieee802154_setchan(fd, chan+11); if (ret<0) { printf("Device is not an IEEE 802.15.4 interface!\n"); @@ -214,10 +185,9 @@ static int status(int fd) printf("MAC854IOCGPROMISC failed\n"); return ret; } - ret = ioctl(fd, MAC854IOCGCCA, (unsigned long)&cca); + ret = ieee802154_getcca(fd, &cca); if (ret) { - printf("MAC854IOCGCCA failed\n"); return ret; } #if 0 @@ -259,17 +229,21 @@ static int status(int fd) static int display(FAR struct ieee802154_packet_s *pack) { int i; - int hlen=0; + int hlen=0, dhlen=0; char buf[IEEE802154_ADDRSTRLEN+1]; struct ieee802154_addr_s dest,src; - printf("rssi=%3u lqi=%3u ", pack->rssi, pack->lqi); - hlen = ieee802154_addrparse(pack, &dest, &src); + + dhlen = hlen; + if(hlen>pack->len) dhlen = 0; + + printf("chan=%2d rssi=%3u lqi=%3u len=%d ", gChan, pack->rssi, pack->lqi, pack->len - dhlen); + if(hlen<0) { - printf("invalid header \n"); - hlen = 0; + printf("[invalid header] "); + dhlen = 0; } else { @@ -279,11 +253,9 @@ static int display(FAR struct ieee802154_packet_s *pack) printf("%s] ", buf); } - printf("len=%d ", pack->len - hlen); - - for (i = 0; i < pack->len - hlen; i++) + for (i = 0; i < pack->len - dhlen; i++) { - printf("%02X", pack->data[i+hlen]); + printf("%02X", pack->data[i+dhlen]); } printf("\n"); return 0; @@ -343,19 +315,22 @@ static void* sniff(void *arg) * Transmit a frame. ****************************************************************************/ -static int tx(int fd, FAR struct ieee802154_packet_s *pack) +static int tx(int fd, FAR struct ieee802154_packet_s *pack, int verbose) { int i,ret; - for (i = 0; i < pack->len; i++) + if(verbose) { - printf("%02X", pack->data[i]); + for (i = 0; i < pack->len; i++) + { + printf("%02X", pack->data[i]); + } + fflush(stdout); } - fflush(stdout); ret = write(fd, pack, sizeof(struct ieee802154_packet_s)); if(ret==OK) { - printf(" OK\n"); + if(verbose) printf(" OK\n"); } else { @@ -441,7 +416,8 @@ int i8_main(int argc, char *argv[]) { ret = usage(); } - ret = setchan(fd, arg); + ret = ieee802154_setchan(fd, arg); + gChan = arg; } else if (!strcmp(argv[2], "edth")) { @@ -449,7 +425,7 @@ int i8_main(int argc, char *argv[]) { goto usage; } - ret = getcca(fd, &cca); + ret = ieee802154_getcca(fd, &cca); if(!strcmp("off",argv[3])) { cca.use_ed = 0; @@ -459,7 +435,7 @@ int i8_main(int argc, char *argv[]) cca.use_ed = 1; cca.edth = arg; } - ret = setcca(fd, &cca); + ret = ieee802154_setcca(fd, &cca); } else if (!strcmp(argv[2], "csth")) { @@ -467,7 +443,7 @@ int i8_main(int argc, char *argv[]) { goto usage; } - ret = getcca(fd, &cca); + ret = ieee802154_getcca(fd, &cca); if(!strcmp("off",argv[3])) { cca.use_cs = 0; @@ -477,7 +453,7 @@ int i8_main(int argc, char *argv[]) cca.use_cs = 1; cca.csth = arg; } - ret = setcca(fd, &cca); + ret = ieee802154_setcca(fd, &cca); } else if (!strcmp(argv[2], "snif")) { @@ -487,7 +463,7 @@ int i8_main(int argc, char *argv[]) if (ret<0) { printf("Device is not an IEEE 802.15.4 interface!\n"); - return (void*)ret; + return ret; } args.fd = fd; @@ -497,7 +473,7 @@ int i8_main(int argc, char *argv[]) if (ret<0) { printf("Device is not an IEEE 802.15.4 interface!\n"); - return (void*)ret; + return ret; } } @@ -533,7 +509,7 @@ data_error: } gTxPacket.len = id; - ret = tx(fd, &gTxPacket); + ret = tx(fd, &gTxPacket, TRUE); } else if (!strcmp(argv[2], "beacons")) { @@ -554,17 +530,18 @@ data_error: gTxPacket.data[gTxPacket.len++] = 0xFF; gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr gTxPacket.data[gTxPacket.len++] = 0xFF; - gTxPacket.data[gTxPacket.len++] = 0x07; //BEACON_REQ + gTxPacket.data[gTxPacket.len++] = IEEE802154_CMD_BEACON_REQ; // for(i=1;i<3;i++) while(1) { int ch; for(ch=11; ch<27; ch++) { - setchan(fd, ch); - printf("txbeacon on chan %d\n", ch); + printf("chan=%2d...\r", ch); fflush(stdout); + ieee802154_setchan(fd, ch); + gChan = ch; gTxPacket.data[2] = i; //seq - tx(fd, &gTxPacket); + tx(fd, &gTxPacket, FALSE); sleep(1); } i++; if(i==256) i=0; From 0d00a82c8811aef1cf7a6b9c14edcd7288fb9b28 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Wed, 12 Aug 2015 22:26:57 +0200 Subject: [PATCH 22/27] support the use of null to signal absence of address --- ieee802154/common/ieee802154_addrstore.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ieee802154/common/ieee802154_addrstore.c b/ieee802154/common/ieee802154_addrstore.c index 121a597cf..5816271b4 100644 --- a/ieee802154/common/ieee802154_addrstore.c +++ b/ieee802154/common/ieee802154_addrstore.c @@ -52,6 +52,10 @@ int ieee802154_addrstore(FAR struct ieee802154_packet_s *packet, /* encode dest addr */ + if(dest == NULL) + { + goto nodest; + } if(dest->ia_len == 2) { memcpy(packet->data+index, &dest->ia_panid, 2); @@ -70,6 +74,7 @@ int ieee802154_addrstore(FAR struct ieee802154_packet_s *packet, } else if(dest->ia_len == 0) { +nodest: packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_NONE; } else @@ -79,7 +84,7 @@ int ieee802154_addrstore(FAR struct ieee802154_packet_s *packet, /* encode source pan id according to compression */ - if( (dest->ia_len != 0) && (src->ia_len != 0) && (dest->ia_panid == src->ia_panid) ) + if( (dest != NULL && dest->ia_len != 0) && (src != NULL && src->ia_len != 0) && (dest->ia_panid == src->ia_panid) ) { packet->data[0] |= IEEE802154_FC1_INTRA; } @@ -92,6 +97,10 @@ int ieee802154_addrstore(FAR struct ieee802154_packet_s *packet, /* encode source addr */ + if(src == NULL) + { + goto nosrc; + } if(src->ia_len == 2) { memcpy(packet->data+index, &src->ia_saddr, 2); @@ -106,6 +115,7 @@ int ieee802154_addrstore(FAR struct ieee802154_packet_s *packet, } else if(dest->ia_len == 0) { +nosrc: packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_NONE; } else From cf097c84b82ee642989a605f70d4c841f9f496b2 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Wed, 12 Aug 2015 22:27:38 +0200 Subject: [PATCH 23/27] minor sniff update send beacons using the addrstore routine. --- ieee802154/i8sak/i8sak_main.c | 89 +++++++++++++++++------------------ 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index eec521fa7..ea4178839 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -226,7 +226,7 @@ static int status(int fd) * Display a single packet ****************************************************************************/ -static int display(FAR struct ieee802154_packet_s *pack) +static int display(FAR struct ieee802154_packet_s *pack, bool verbose) { int i; int hlen=0, dhlen=0; @@ -238,7 +238,7 @@ static int display(FAR struct ieee802154_packet_s *pack) dhlen = hlen; if(hlen>pack->len) dhlen = 0; - printf("chan=%2d rssi=%3u lqi=%3u len=%d ", gChan, pack->rssi, pack->lqi, pack->len - dhlen); + printf("chan=%2d rssi=%3u lqi=%3u len=%3u ", gChan, pack->rssi, pack->lqi, pack->len - dhlen); if(hlen<0) { @@ -270,6 +270,7 @@ static int display(FAR struct ieee802154_packet_s *pack) struct sniffargs { int fd; + int verbose; }; static void* sniff(void *arg) @@ -301,7 +302,7 @@ static void* sniff(void *arg) } /* Display packet */ - display(&gRxPacket); + display(&gRxPacket, sa->verbose); } @@ -459,22 +460,13 @@ int i8_main(int argc, char *argv[]) { struct sniffargs args; - ret = ioctl(fd, MAC854IOCSPROMISC, TRUE); - if (ret<0) - { - printf("Device is not an IEEE 802.15.4 interface!\n"); - return ret; - } + ret = ieee802154_setpromisc(fd, TRUE); args.fd = fd; + args.verbose = FALSE; ret = (int)sniff(&args); - ret = ioctl(fd, MAC854IOCSPROMISC, FALSE); - if (ret<0) - { - printf("Device is not an IEEE 802.15.4 interface!\n"); - return ret; - } + ret = ieee802154_setpromisc(fd, FALSE); } else if (!strcmp(argv[2], "tx")) @@ -513,41 +505,46 @@ data_error: } else if (!strcmp(argv[2], "beacons")) { - struct sniffargs args; - pthread_t pth; - int i; + struct sniffargs args; + struct ieee802154_addr_s dest; + pthread_t pth; + int i; - args.fd = fd; + args.fd = fd; + args.verbose = FALSE; - pthread_create(&pth, NULL, sniff, &args); + pthread_create(&pth, NULL, sniff, &args); - //beacon request - gTxPacket.len = 0; - gTxPacket.data[gTxPacket.len++] = 0x03; //mac command, no ack, no panid compression - gTxPacket.data[gTxPacket.len++] = 0x08; //short destination address, no source address - gTxPacket.data[gTxPacket.len++] = 0; //seq - gTxPacket.data[gTxPacket.len++] = 0xFF; //panid - gTxPacket.data[gTxPacket.len++] = 0xFF; - gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr - gTxPacket.data[gTxPacket.len++] = 0xFF; - gTxPacket.data[gTxPacket.len++] = IEEE802154_CMD_BEACON_REQ; + //beacon request + gTxPacket.len = 0; + gTxPacket.data[gTxPacket.len++] = 0x03; //mac command, no ack, no panid compression + gTxPacket.data[gTxPacket.len++] = 0x00; //short destination address, no source address + gTxPacket.data[gTxPacket.len++] = 0; //seq + dest.ia_len = 2; + dest.ia_panid = 0xFFFF; + dest.ia_saddr = 0xFFFF; + ieee802154_addrstore(&gTxPacket, &dest, NULL); + gTxPacket.data[gTxPacket.len++] = 0xFF; //panid + gTxPacket.data[gTxPacket.len++] = 0xFF; + gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr + gTxPacket.data[gTxPacket.len++] = 0xFF; + gTxPacket.data[gTxPacket.len++] = IEEE802154_CMD_BEACON_REQ; // for(i=1;i<3;i++) - while(1) - { - int ch; - for(ch=11; ch<27; ch++) - { - printf("chan=%2d...\r", ch); fflush(stdout); - ieee802154_setchan(fd, ch); - gChan = ch; - gTxPacket.data[2] = i; //seq - tx(fd, &gTxPacket, FALSE); - sleep(1); - } - i++; if(i==256) i=0; - } - pthread_kill(pth, SIGUSR1); - + while(1) + { + int ch; + for(ch=11; ch<27; ch++) + { + printf("chan=%2d...\r", ch); fflush(stdout); + ieee802154_setchan(fd, ch); + gChan = ch; + gTxPacket.data[2] = i; //seq + tx(fd, &gTxPacket, FALSE); + sleep(1); + } + i++; if(i==256) i=0; + } + pthread_kill(pth, SIGUSR1); } else { From 494046c61a1932e25d5e7053e52c1988dc19aef2 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Sun, 30 Aug 2015 23:37:29 +0200 Subject: [PATCH 24/27] Add a submenu for 802.15.4 apps --- ieee802154/Kconfig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ieee802154/Kconfig b/ieee802154/Kconfig index 213550783..0121396b5 100644 --- a/ieee802154/Kconfig +++ b/ieee802154/Kconfig @@ -3,6 +3,10 @@ # see misc/tools/kconfig-language.txt. # +menu "IEEE 802.15.4 applications" + source "$APPSDIR/ieee802154/common/Kconfig" source "$APPSDIR/ieee802154/coord/Kconfig" source "$APPSDIR/ieee802154/i8sak/Kconfig" + +endmenu #ieee 802.15.4 apps From 1d70e552964d402449751e2ca8f9c4ab0540abf6 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Mon, 31 Aug 2015 18:13:51 +0200 Subject: [PATCH 25/27] pa can be enabled via ioctl --- ieee802154/i8sak/i8sak_main.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index ea4178839..387d43916 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -356,6 +356,7 @@ int usage(void) " chan \n" " snif\n" " stat\n" + " pa \n" " edth \n" " csth \n" " tx \n" @@ -407,6 +408,25 @@ int i8_main(int argc, char *argv[]) { ret = ioctl(fd, 1000, 0); } + else if (!strcmp(argv[2], "pa")) + { + if(argc != 4) + { + goto usage; + } + if(!strcmp(argv[3],"off")) + { + ret = ioctl(fd, 1001, 0); + } + else if(!strcmp(argv[3],"on")) + { + ret = ioctl(fd, 1001, 1); + } + else + { + goto usage; + } + } else if (!strcmp(argv[2], "stat")) { ret = status(fd); @@ -415,7 +435,7 @@ int i8_main(int argc, char *argv[]) { if(argc != 4) { - ret = usage(); + goto usage; } ret = ieee802154_setchan(fd, arg); gChan = arg; From 331571f69c3d94d69a6d20d68991ee827fe7361a Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Tue, 1 Sep 2015 18:21:48 +0200 Subject: [PATCH 26/27] add getchan routine support beacon scan on single channel --- ieee802154/common/Makefile | 3 +- ieee802154/common/ieee802154_getcca.c | 2 +- ieee802154/common/ieee802154_getchan.c | 57 ++++++++++++++++ ieee802154/i8sak/Makefile | 2 +- ieee802154/i8sak/i8sak_main.c | 92 ++++++++++++++++---------- include/ieee802154/ieee802154.h | 2 + 6 files changed, 121 insertions(+), 37 deletions(-) create mode 100644 ieee802154/common/ieee802154_getchan.c diff --git a/ieee802154/common/Makefile b/ieee802154/common/Makefile index 52079408f..e2f441c2c 100644 --- a/ieee802154/common/Makefile +++ b/ieee802154/common/Makefile @@ -40,7 +40,8 @@ include $(APPDIR)/Make.defs # NSH Library ASRCS = -CSRCS = ieee802154_setchan.c ieee802154_setpanid.c +CSRCS = ieee802154_setchan.c ieee802154_getchan.c +CSRCS += ieee802154_setpanid.c CSRCS += ieee802154_seteaddr.c ieee802154_setsaddr.c CSRCS += ieee802154_setpromisc.c ieee802154_setdevmode.c CSRCS += ieee802154_setcca.c ieee802154_getcca.c diff --git a/ieee802154/common/ieee802154_getcca.c b/ieee802154/common/ieee802154_getcca.c index 5cdcb6ba4..5df88e617 100644 --- a/ieee802154/common/ieee802154_getcca.c +++ b/ieee802154/common/ieee802154_getcca.c @@ -1,5 +1,5 @@ /**************************************************************************** - * ieee802154/common/ieee802154_setchan.c + * ieee802154/common/ieee802154_getcca.c * * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. * Author: Sebastien Lorquet diff --git a/ieee802154/common/ieee802154_getchan.c b/ieee802154/common/ieee802154_getchan.c new file mode 100644 index 000000000..93bede8a2 --- /dev/null +++ b/ieee802154/common/ieee802154_getchan.c @@ -0,0 +1,57 @@ +/**************************************************************************** + * ieee802154/common/ieee802154_getchan.c + * + * Copyright (C) 2015 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 + +int ieee802154_getchan(int fd, FAR uint8_t *chan) +{ + int ret = ioctl(fd, MAC854IOCGCHAN, (unsigned long)chan ); + if (ret<0) + { + printf("MAC854IOCGCHAN failed\n"); + } + return ret; +} + diff --git a/ieee802154/i8sak/Makefile b/ieee802154/i8sak/Makefile index 7dc7d266d..37fcd8dd0 100644 --- a/ieee802154/i8sak/Makefile +++ b/ieee802154/i8sak/Makefile @@ -42,7 +42,7 @@ include $(APPDIR)/Make.defs APPNAME = i8 PRIORITY = SCHED_PRIORITY_DEFAULT -STACKSIZE = 2048 +STACKSIZE = 4096 # IEEE 802.15.4 SAK (Swiss Army Knife) diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index 387d43916..e56103bb0 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -73,12 +73,11 @@ static struct ieee802154_packet_s gRxPacket; static struct ieee802154_packet_s gTxPacket; -static uint8_t gChan; uint8_t levels[16]; uint8_t disp[16]; -int scan(int fd) +int energy_scan(int fd) { int ret = OK; uint8_t chan, energy; @@ -160,10 +159,10 @@ static int status(int fd) printf("MAC854IOCGPANID failed\n"); return ret; } - ret = ioctl(fd, MAC854IOCGCHAN, (unsigned long)&chan); + + ret = ieee802154_getchan(fd, &chan); if (ret) { - printf("MAC854IOCGCHAN failed\n"); return ret; } @@ -201,7 +200,7 @@ static int status(int fd) /* Display */ printf("PANID %02X%02X CHAN %2d (%d MHz)\nSADDR %02X%02X EADDR ", - panid>>8, panid&0xFF, chan, 2350+(5*chan), saddr>>8, saddr&0xFF); + panid>>8, panid&0xFF, (int)chan, 2350+(5*chan), saddr>>8, saddr&0xFF); for (i=0; i<8; i++) { printf("%02X", eaddr[i]); @@ -226,7 +225,7 @@ static int status(int fd) * Display a single packet ****************************************************************************/ -static int display(FAR struct ieee802154_packet_s *pack, bool verbose) +static int display(uint8_t chan, FAR struct ieee802154_packet_s *pack, bool verbose) { int i; int hlen=0, dhlen=0; @@ -238,7 +237,7 @@ static int display(FAR struct ieee802154_packet_s *pack, bool verbose) dhlen = hlen; if(hlen>pack->len) dhlen = 0; - printf("chan=%2d rssi=%3u lqi=%3u len=%3u ", gChan, pack->rssi, pack->lqi, pack->len - dhlen); + printf("chan=%2d rssi=%3u lqi=%3u len=%3u ", chan, pack->rssi, pack->lqi, pack->len - dhlen); if(hlen<0) { @@ -278,6 +277,7 @@ static void* sniff(void *arg) int ret; struct sniffargs *sa = (struct sniffargs*)arg; int fd = sa->fd; + uint8_t chan; printf("Listening...\n"); while (1) @@ -287,12 +287,12 @@ static void* sniff(void *arg) { if (errno == EAGAIN) { - continue; + continue; } if (errno == EINTR) { - printf("read: interrupted\n"); - break; + printf("read: interrupted\n"); + break; } else { @@ -302,7 +302,9 @@ static void* sniff(void *arg) } /* Display packet */ - display(&gRxPacket, sa->verbose); + chan=0; + ieee802154_getchan(fd, &chan); + display(chan, &gRxPacket, sa->verbose); } @@ -350,16 +352,17 @@ static int tx(int fd, FAR struct ieee802154_packet_s *pack, int verbose) int usage(void) { - printf("i8 \n" - " scan\n" - " dump\n" - " chan \n" - " snif\n" - " stat\n" - " pa \n" - " edth \n" - " csth \n" - " tx \n" + printf("i8 []\n" + " scan Energy scan\n" + " beacons [single] Aactive scan [on cur chan]\n" + " dump Show device registers\n" + " chan Select current channel\n" + " snif Listen for packets\n" + " stat Device status\n" + " pa Enable/Disable external RF amplifiers\n" + " edth Select Energy detection rx threshold\n" + " csth Select preamble correlation rx threshold\n" + " tx Transmit a raw packet\n" ); return ERROR; } @@ -402,7 +405,7 @@ int i8_main(int argc, char *argv[]) /* get mode */ if (!strcmp(argv[2], "scan")) { - ret = scan(fd); + ret = energy_scan(fd); } else if (!strcmp(argv[2], "dump")) { @@ -438,7 +441,6 @@ int i8_main(int argc, char *argv[]) goto usage; } ret = ieee802154_setchan(fd, arg); - gChan = arg; } else if (!strcmp(argv[2], "edth")) { @@ -528,8 +530,14 @@ data_error: struct sniffargs args; struct ieee802154_addr_s dest; pthread_t pth; - int i; - + int i,single = FALSE; + uint8_t ch; + + if(argc==4 && !strcmp(argv[3], "single") ) + { + single = TRUE; + } + args.fd = fd; args.verbose = FALSE; @@ -543,26 +551,42 @@ data_error: dest.ia_len = 2; dest.ia_panid = 0xFFFF; dest.ia_saddr = 0xFFFF; + + gTxPacket.len = ieee802154_addrstore(&gTxPacket, &dest, NULL); + ieee802154_addrstore(&gTxPacket, &dest, NULL); gTxPacket.data[gTxPacket.len++] = 0xFF; //panid gTxPacket.data[gTxPacket.len++] = 0xFF; gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr gTxPacket.data[gTxPacket.len++] = 0xFF; + gTxPacket.data[gTxPacket.len++] = IEEE802154_CMD_BEACON_REQ; -// for(i=1;i<3;i++) + if(!single) + { + ch = 11; + } + i=0; while(1) { - int ch; - for(ch=11; ch<27; ch++) + if(!single) { - printf("chan=%2d...\r", ch); fflush(stdout); ieee802154_setchan(fd, ch); - gChan = ch; - gTxPacket.data[2] = i; //seq - tx(fd, &gTxPacket, FALSE); - sleep(1); } - i++; if(i==256) i=0; + ieee802154_getchan(fd, &ch); + printf("chan=%2d seq=%2X ...\r", (int)ch, i); fflush(stdout); + gTxPacket.data[2] = i; //seq + tx(fd, &gTxPacket, FALSE); + sleep(1); + i++; + if(i==256) + { + i=0; + } + if(!single) + { + ch++; + if(ch==27) ch=11; + } } pthread_kill(pth, SIGUSR1); } diff --git a/include/ieee802154/ieee802154.h b/include/ieee802154/ieee802154.h index c7b41db57..d506ded7a 100644 --- a/include/ieee802154/ieee802154.h +++ b/include/ieee802154/ieee802154.h @@ -53,6 +53,8 @@ struct ieee802154_addr_s #define IEEE802154_ADDRSTRLEN 22 /* (4+1+8*2) */ int ieee802154_setchan (int fd, uint8_t chan); +int ieee802154_getchan (int fd, FAR uint8_t *chan); + int ieee802154_setpanid (int fd, uint16_t panid); int ieee802154_setsaddr (int fd, uint16_t saddr); int ieee802154_seteaddr (int fd, FAR uint8_t *chan); From ae88ed1d67bdea7b5bc82fd6314ec17f619c7b02 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Wed, 2 Sep 2015 15:42:29 +0200 Subject: [PATCH 27/27] refactor address encoding, pan id compression was wrong. --- ieee802154/common/ieee802154_addrstore.c | 110 +++++++++++------------ ieee802154/coord/coord_main.c | 4 +- ieee802154/i8sak/i8sak_main.c | 6 -- 3 files changed, 56 insertions(+), 64 deletions(-) diff --git a/ieee802154/common/ieee802154_addrstore.c b/ieee802154/common/ieee802154_addrstore.c index 5816271b4..6b3aef2df 100644 --- a/ieee802154/common/ieee802154_addrstore.c +++ b/ieee802154/common/ieee802154_addrstore.c @@ -48,79 +48,79 @@ int ieee802154_addrstore(FAR struct ieee802154_packet_s *packet, FAR struct ieee802154_addr_s *dest, FAR struct ieee802154_addr_s *src) { - int index=3; + int index=3; //skip fc and seq /* encode dest addr */ - if(dest == NULL) + if(dest == NULL || dest->ia_len == 0) { - goto nodest; - } - if(dest->ia_len == 2) - { - memcpy(packet->data+index, &dest->ia_panid, 2); - index += 2; /* skip dest pan id */ - memcpy(packet->data+index, &dest->ia_saddr, 2); - index += 2; /* skip dest addr */ - packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_SHORT; - } - else if(dest->ia_len == 8) - { - memcpy(packet->data+index, &dest->ia_panid, 2); - index += 2; /* skip dest pan id */ - memcpy(packet->data+index, dest->ia_eaddr, 8); - index += 8; /* skip dest addr */ - packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_EXT; - } - else if(dest->ia_len == 0) - { -nodest: packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_NONE; } else { - return -EINVAL; + memcpy(packet->data+index, &dest->ia_panid, 2); + index += 2; /* skip dest pan id */ + if(dest->ia_len == 2) + { + memcpy(packet->data+index, &dest->ia_saddr, 2); + index += 2; /* skip dest addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_SHORT; + } + else if(dest->ia_len == 8) + { + memcpy(packet->data+index, &dest->ia_panid, 2); + index += 2; /* skip dest pan id */ + memcpy(packet->data+index, dest->ia_eaddr, 8); + index += 8; /* skip dest addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_DADDR) | IEEE802154_DADDR_EXT; + } + else + { + return -EINVAL; + } } - /* encode source pan id according to compression */ + packet->data[0] &= ~IEEE802154_FC1_INTRA; + if( (dest != NULL && dest->ia_len != 0) && (src != NULL && src->ia_len != 0) ) + { + /* we have both adresses, encode source pan id according to compression */ + if( dest->ia_panid == src->ia_panid) + { + packet->data[0] |= IEEE802154_FC1_INTRA; + } + } - if( (dest != NULL && dest->ia_len != 0) && (src != NULL && src->ia_len != 0) && (dest->ia_panid == src->ia_panid) ) - { - packet->data[0] |= IEEE802154_FC1_INTRA; - } - else - { - memcpy(packet->data+index, &src->ia_panid, 2); - index += 2; /*skip dest pan id*/ - packet->data[0] &= ~IEEE802154_FC1_INTRA; - } /* encode source addr */ - if(src == NULL) + if(src == NULL || src->ia_len==0) { - goto nosrc; - } - if(src->ia_len == 2) - { - memcpy(packet->data+index, &src->ia_saddr, 2); - index += 2; /* skip src addr */ - packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_SHORT; - } - else if(src->ia_len == 8) - { - memcpy(packet->data+index, src->ia_eaddr, 8); - index += 8; /* skip src addr */ - packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_EXT; - } - else if(dest->ia_len == 0) - { -nosrc: - packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_NONE; + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_NONE; } else { - return -EINVAL; + /*add src pan id if it was not compressed before*/ + if(!(packet->data[0] & IEEE802154_FC1_INTRA)) + { + memcpy(packet->data+index, &src->ia_panid, 2); + index += 2; /*skip src pan id*/ + } + if(src->ia_len == 2) + { + memcpy(packet->data+index, &src->ia_saddr, 2); + index += 2; /* skip src addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_SHORT; + } + else if(src->ia_len == 8) + { + memcpy(packet->data+index, src->ia_eaddr, 8); + index += 8; /* skip src addr */ + packet->data[1] = (packet->data[1] & ~IEEE802154_FC2_SADDR) | IEEE802154_SADDR_EXT; + } + else + { + return -EINVAL; + } } return index; diff --git a/ieee802154/coord/coord_main.c b/ieee802154/coord/coord_main.c index 9d740e1e5..1ef7d174a 100644 --- a/ieee802154/coord/coord_main.c +++ b/ieee802154/coord/coord_main.c @@ -156,7 +156,6 @@ static int coord_command_beacon_req(FAR struct ieee_coord_s *coord) { FAR struct ieee_frame_s *rx = &coord->rxbuf; FAR struct ieee802154_packet_s *tx = &coord->txbuf.packet; - FAR struct ieee802154_addr_s dest; int i; printf("Beacon request\n"); @@ -175,8 +174,7 @@ static int coord_command_beacon_req(FAR struct ieee_coord_s *coord) coord->macBSN++; /* adressing */ - dest.ia_len = 0; /*no dest address*/ - tx->len = ieee802154_addrstore(tx, &dest, &coord->addr); + tx->len = ieee802154_addrstore(tx, NULL, &coord->addr); /* superframe spec */ tx->data[tx->len++] = 0xff; diff --git a/ieee802154/i8sak/i8sak_main.c b/ieee802154/i8sak/i8sak_main.c index e56103bb0..b4dbc025c 100644 --- a/ieee802154/i8sak/i8sak_main.c +++ b/ieee802154/i8sak/i8sak_main.c @@ -554,12 +554,6 @@ data_error: gTxPacket.len = ieee802154_addrstore(&gTxPacket, &dest, NULL); - ieee802154_addrstore(&gTxPacket, &dest, NULL); - gTxPacket.data[gTxPacket.len++] = 0xFF; //panid - gTxPacket.data[gTxPacket.len++] = 0xFF; - gTxPacket.data[gTxPacket.len++] = 0xFF; //saddr - gTxPacket.data[gTxPacket.len++] = 0xFF; - gTxPacket.data[gTxPacket.len++] = IEEE802154_CMD_BEACON_REQ; if(!single) {