Squashed commit of the following:
commit 6fbbca4ce308f5d8130d795342c7639555a7e5bf Author: Gregory Nutt <gnutt@nuttx.org> Date: Sat Aug 19 08:46:27 2017 -0600 apps/examples/pf_ieee802154: Add PANID to command line options; Cannot bind to address zero... There is no counterpart to INADDR_ANY for these radios (not now at least). commit 354111108a88f549248537e79dbf79998f458ded Author: Gregory Nutt <gnutt@nuttx.org> Date: Fri Aug 18 17:47:10 2017 -0600 PF_IEEE802154: A few bugfixes commit f1bb947609ed5e9e8816234653d6c8ee24585079 Author: Gregory Nutt <gnutt@nuttx.org> Date: Fri Aug 18 15:17:32 2017 -0600 Add file missed in last commit commit 9a73930e864db83609b5cc0d7c940d8efeaade66 Merge: 315b888f c433e07a Author: Gregory Nutt <gnutt@nuttx.org> Date: Fri Aug 18 12:08:03 2017 -0600 Merge remote-tracking branch 'origin/master' into pf_ieee802154 commit 315b888f4e72523d90b526edc5a82fb97678ff7e Author: Gregory Nutt <gnutt@nuttx.org> Date: Fri Aug 18 12:06:51 2017 -0600 examples/pf_ieee802154: Add for testing PF_IEEE802154 sockets.
This commit is contained in:
parent
c433e07a27
commit
6cbf2b9130
16
examples/pf_ieee802154/.gitignore
vendored
Normal file
16
examples/pf_ieee802154/.gitignore
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
/Make.dep
|
||||
/.depend
|
||||
/.built
|
||||
/host
|
||||
/config.h
|
||||
/*.asm
|
||||
/*.obj
|
||||
/*.rel
|
||||
/*.lst
|
||||
/*.sym
|
||||
/*.adb
|
||||
/*.lib
|
||||
/*.src
|
||||
/*.exe
|
||||
/*.dSYM
|
||||
/*.hobj
|
47
examples/pf_ieee802154/Kconfig
Normal file
47
examples/pf_ieee802154/Kconfig
Normal file
@ -0,0 +1,47 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see the file kconfig-language.txt in the NuttX tools repository.
|
||||
#
|
||||
|
||||
config EXAMPLES_PFIEEE802154
|
||||
bool "PF_IEEE802154 example"
|
||||
default n
|
||||
depends on NET_IEEE802154
|
||||
---help---
|
||||
Enable the PF_IEEE802154 socket example
|
||||
|
||||
if EXAMPLES_PFIEEE802154
|
||||
|
||||
config EXAMPLES_PFIEEE802154_PROGNAME1
|
||||
string "Target1 program name"
|
||||
default "pfserver"
|
||||
depends on BUILD_KERNEL
|
||||
---help---
|
||||
This is the name of the Target1 program that will be use when the
|
||||
NSH ELF program is installed.
|
||||
|
||||
config EXAMPLES_PFIEEE802154_PRIORITY1
|
||||
int "Target1 task priority"
|
||||
default 100
|
||||
|
||||
config EXAMPLES_PFIEEE802154_STACKSIZE1
|
||||
int "Target1 stack size"
|
||||
default 2048
|
||||
|
||||
config EXAMPLES_PFIEEE802154_PROGNAME2
|
||||
string "Target2 program name"
|
||||
default "pfclient"
|
||||
depends on BUILD_KERNEL
|
||||
---help---
|
||||
This is the name of the Target2 program that will be use when the
|
||||
NSH ELF program is installed.
|
||||
|
||||
config EXAMPLES_PFIEEE802154_PRIORITY2
|
||||
int "Target2 task priority"
|
||||
default 100
|
||||
|
||||
config EXAMPLES_PFIEEE802154_STACKSIZE2
|
||||
int "Target2 stack size"
|
||||
default 2048
|
||||
|
||||
endif # EXAMPLES_PFIEEE802154
|
39
examples/pf_ieee802154/Make.defs
Normal file
39
examples/pf_ieee802154/Make.defs
Normal file
@ -0,0 +1,39 @@
|
||||
############################################################################
|
||||
# apps/examples/pf_ieee802154/Make.defs
|
||||
# Adds selected applications to apps/ build
|
||||
#
|
||||
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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_EXAMPLES_PFIEEE802154),y)
|
||||
CONFIGURED_APPS += examples/pf_ieee802154
|
||||
endif
|
172
examples/pf_ieee802154/Makefile
Normal file
172
examples/pf_ieee802154/Makefile
Normal file
@ -0,0 +1,172 @@
|
||||
############################################################################
|
||||
# apps/examples/pf_ieee802154/Makefile
|
||||
#
|
||||
# Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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
|
||||
|
||||
# PF_IEEE802154 Socket Test
|
||||
|
||||
TARGCMN_CSRCS = pf_cmdline.c
|
||||
TARGCMN_COBJS = $(TARGCMN_CSRCS:.c=$(OBJEXT))
|
||||
|
||||
# Target 1
|
||||
|
||||
TARG1_CRCS =
|
||||
TARG1_MAINSRC = pf_server.c
|
||||
|
||||
TARG1_COBJS = $(TARG1_CRCS:.c=$(OBJEXT))
|
||||
TARG1_MAINOBJ = $(TARG1_MAINSRC:.c=$(OBJEXT))
|
||||
|
||||
CONFIG_EXAMPLES_PFIEEE802154_PROGNAME1 ?= pfserver
|
||||
APPNAME1 = pfserver
|
||||
|
||||
CONFIG_EXAMPLES_PFIEEE802154_PRIORITY1 ?= 100
|
||||
CONFIG_EXAMPLES_PFIEEE802154_STACKSIZE1 ?= 2048
|
||||
|
||||
PROGNAME1 = $(CONFIG_EXAMPLES_PFIEEE802154_PROGNAME1)
|
||||
PRIORITY1 = $(CONFIG_EXAMPLES_PFIEEE802154_PRIORITY1)
|
||||
STACKSIZE1 = $(CONFIG_EXAMPLES_PFIEEE802154_STACKSIZE1)
|
||||
|
||||
# Target 2
|
||||
|
||||
TARG2_CSRCS =
|
||||
TARG2_MAINSRC = pf_client.c
|
||||
|
||||
TARG2_COBJS = $(TARG2_CSRCS:.c=$(OBJEXT))
|
||||
TARG2_MAINOBJ = $(TARG2_MAINSRC:.c=$(OBJEXT))
|
||||
|
||||
CONFIG_EXAMPLES_PFIEEE802154_PROGNAME2 ?= pfclient
|
||||
APPNAME2 = pfclient
|
||||
|
||||
CONFIG_EXAMPLES_PFIEEE802154_PRIORITY2 ?= 100
|
||||
CONFIG_EXAMPLES_PFIEEE802154_STACKSIZE2 ?= 2048
|
||||
|
||||
PROGNAME2 = $(CONFIG_EXAMPLES_PFIEEE802154_PROGNAME2)
|
||||
PRIORITY2 = $(CONFIG_EXAMPLES_PFIEEE802154_PRIORITY2)
|
||||
STACKSIZE2 = $(CONFIG_EXAMPLES_PFIEEE802154_STACKSIZE2)
|
||||
|
||||
SRCS = $(TARG1_CRCS) $(TARG1_MAINSRC) $(TARG2_CSRCS) $(TARG2_MAINSRC) $(TARGCMN_CSRCS)
|
||||
OBJS = $(TARG1_COBJS) $(TARG2_COBJS) $(TARGCMN_COBJS)
|
||||
MAINOBJS = $(TARG1_MAINOBJ) $(TARG2_MAINOBJ)
|
||||
|
||||
MAINNAME1 = pfserver_main
|
||||
MAINNAME2 = pfclient_main
|
||||
|
||||
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
|
||||
|
||||
ROOTDEPPATH = --dep-path .
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH =
|
||||
|
||||
all: .built
|
||||
.PHONY: clean depend distclean preconfig
|
||||
.PRECIOUS: ../../libapps$(LIBEXT)
|
||||
|
||||
$(OBJS) $(MAINOBJS): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
$(BIN): $(OBJS) $(MAINOBJS)
|
||||
$(call ARCHIVE, $@, $(OBJS) $(MAINOBJS))
|
||||
|
||||
.built: $(BIN)
|
||||
$(Q) touch .built
|
||||
|
||||
ifeq ($(CONFIG_BUILD_KERNEL),y)
|
||||
$(BIN_DIR)$(DELIM)$(PROGNAME1): $(OBJS) $(TARG1_MAINOBJ)
|
||||
@echo "LD: $(PROGNAME1)"
|
||||
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME1) $(ARCHCRT0OBJ) $(TARG1_MAINOBJ) $(LDLIBS)
|
||||
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME1)
|
||||
|
||||
$(BIN_DIR)$(DELIM)$(PROGNAME2): $(OBJS) $(TARG2_MAINOBJ)
|
||||
@echo "LD: $(PROGNAME2)"
|
||||
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME2) $(ARCHCRT0OBJ) $(TARG2_MAINOBJ) $(LDLIBS)
|
||||
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME2)
|
||||
|
||||
install: $(BIN_DIR)$(DELIM)$(PROGNAME1) $(BIN_DIR)$(DELIM)$(PROGNAME2)
|
||||
|
||||
else
|
||||
install:
|
||||
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
||||
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat: $(DEPCONFIG) Makefile
|
||||
$(call REGISTER,$(APPNAME1),$(PRIORITY1),$(STACKSIZE1),$(MAINNAME1))
|
||||
|
||||
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME2)_main.bdat: $(DEPCONFIG) Makefile
|
||||
$(call REGISTER,$(APPNAME2),$(PRIORITY2),$(STACKSIZE2),$(MAINNAME2))
|
||||
|
||||
context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME1)_main.bdat \
|
||||
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME2)_main.bdat
|
||||
else
|
||||
context:
|
||||
endif
|
||||
|
||||
.depend: Makefile config.h $(SRCS)
|
||||
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||
@touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
$(call DELFILE, .built)
|
||||
$(call DELFILE, *.dSYM)
|
||||
$(call DELFILE, config.h)
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
$(call DELFILE, Make.dep)
|
||||
$(call DELFILE, .depend)
|
||||
|
||||
preconfig:
|
||||
|
||||
-include Make.dep
|
181
examples/pf_ieee802154/pf_client.c
Normal file
181
examples/pf_ieee802154/pf_client.c
Normal file
@ -0,0 +1,181 @@
|
||||
/****************************************************************************
|
||||
* examples/pf_ieee802154/pf_client.c
|
||||
*
|
||||
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 Gregory Nutt nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "pfieee802154.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static int create_socket(void)
|
||||
{
|
||||
struct sockaddr_ieee802154_s addr;
|
||||
socklen_t addrlen;
|
||||
int sockfd;
|
||||
|
||||
/* Create a new DGRAM socket */
|
||||
|
||||
sockfd = socket(AF_IEEE802154, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
printf("client ERROR: client socket failure %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Bind the DGRAM socket to the radio address */
|
||||
|
||||
addr.sa_family = AF_IEEE802154;
|
||||
memcpy(&addr.sa_addr, &g_server_addr, sizeof(struct ieee802154_saddr_s));
|
||||
addrlen = sizeof(struct sockaddr_ieee802154_s);
|
||||
|
||||
if (bind(sockfd, (FAR struct sockaddr *)&addr, addrlen) < 0)
|
||||
{
|
||||
printf("client ERROR: Bind failure: %d\n", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
static inline void fill_buffer(unsigned char *buf, int offset)
|
||||
{
|
||||
int ch;
|
||||
int j;
|
||||
|
||||
buf[0] = offset;
|
||||
for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
|
||||
{
|
||||
if (j >= SENDSIZE)
|
||||
{
|
||||
j = 1;
|
||||
}
|
||||
|
||||
buf[j] = ch;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_BUILD_KERNEL
|
||||
int main(int argc, FAR char *argv[])
|
||||
#else
|
||||
int pfclient_main(int argc, char *argv[])
|
||||
#endif
|
||||
{
|
||||
struct sockaddr_ieee802154_s server;
|
||||
unsigned char outbuf[SENDSIZE];
|
||||
socklen_t addrlen;
|
||||
int sockfd;
|
||||
int nbytes;
|
||||
int offset;
|
||||
|
||||
/* Parse any command line options */
|
||||
|
||||
pf_cmdline(argc, argv);
|
||||
|
||||
/* Create a new DGRAM socket */
|
||||
|
||||
sockfd = create_socket();
|
||||
if (sockfd < 0)
|
||||
{
|
||||
printf("client ERROR: create_socket failed %d\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Then send and receive 256 messages */
|
||||
|
||||
for (offset = 0; offset < 256; offset++)
|
||||
{
|
||||
/* Set up the output buffer */
|
||||
|
||||
fill_buffer(outbuf, offset);
|
||||
|
||||
/* Set up the server address */
|
||||
|
||||
server.sa_family = AF_IEEE802154;
|
||||
memcpy(&server.sa_addr, &g_server_addr, sizeof(struct ieee802154_saddr_s));
|
||||
addrlen = sizeof(struct sockaddr_ieee802154_s);
|
||||
|
||||
/* Send the message */
|
||||
|
||||
printf("client: %d. Sending %d bytes\n", offset, SENDSIZE);
|
||||
nbytes = sendto(sockfd, outbuf, SENDSIZE, 0,
|
||||
(struct sockaddr*)&server, addrlen);
|
||||
printf("client: %d. Sent %d bytes\n", offset, nbytes);
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
printf("client: %d. sendto failed: %d\n", offset, errno);
|
||||
close(sockfd);
|
||||
exit(1);
|
||||
}
|
||||
else if (nbytes != SENDSIZE)
|
||||
{
|
||||
printf("client: %d. Bad send length: %d Expected: %d\n",
|
||||
offset, nbytes, SENDSIZE);
|
||||
close(sockfd);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Now, sleep a bit. No packets should be dropped due to overrunning
|
||||
* the server.
|
||||
*/
|
||||
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
147
examples/pf_ieee802154/pf_cmdline.c
Normal file
147
examples/pf_ieee802154/pf_cmdline.c
Normal file
@ -0,0 +1,147 @@
|
||||
/****************************************************************************
|
||||
* examples/pf_ieee802154/pf_cmdline.c
|
||||
*
|
||||
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <netpacket/ieee802154.h>
|
||||
|
||||
#include "netutils/netlib.h"
|
||||
#include "pfieee802154.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
struct ieee802154_saddr_s g_server_addr;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* show_usage
|
||||
****************************************************************************/
|
||||
|
||||
static void show_usage(FAR const char *progname)
|
||||
{
|
||||
fprintf(stderr, "USAGE:\n");
|
||||
fprintf(stderr, "\t%s [-p <panid>] <short-addr>|<extended-addr>\n",
|
||||
progname);
|
||||
fprintf(stderr, "Where:\n");
|
||||
fprintf(stderr, "\t<panid> is the PAN ID of the form: xx:xx\n");
|
||||
fprintf(stderr, "\t<short-addr> is a short address of the form xx:xx\n");
|
||||
fprintf(stderr, "\t<extended-addr>is an extended address of the form\n");
|
||||
fprintf(stderr, "\t\txx:xx:xx:xx:xx:xx:xx:xx\n\n");
|
||||
fprintf(stderr, "NOTES:\n");
|
||||
fprintf(stderr, "\txx is any 2-character hexadecimal string.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* pf_cmdline
|
||||
****************************************************************************/
|
||||
|
||||
void pf_cmdline(int argc, char **argv)
|
||||
{
|
||||
uint16_t panid = IEEE802154_PANID_BROADCAST;
|
||||
int ndx = 1;
|
||||
|
||||
/* Forms:
|
||||
*
|
||||
* argc=1: progname
|
||||
* argc=2: progname <address>
|
||||
* argc=4: progname -p <panid> <address>
|
||||
*/
|
||||
|
||||
if (argc == 4)
|
||||
{
|
||||
if (strcmp(argv[ndx], "-p") == 0)
|
||||
{
|
||||
ndx++;
|
||||
if (!netlib_saddrconv(argv[ndx], g_server_addr.s_panid))
|
||||
{
|
||||
fprintf(stderr, "ERROR: PAN ID is invalid\n");
|
||||
show_usage(argv[0]);
|
||||
}
|
||||
|
||||
ndx++;
|
||||
argc = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "ERROR: Unrecognized options: %s\n", argv[ndx]);
|
||||
show_usage(argv[0]);
|
||||
}
|
||||
}
|
||||
|
||||
if (argc == 2)
|
||||
{
|
||||
if (netlib_saddrconv(argv[ndx], g_server_addr.s_saddr))
|
||||
{
|
||||
g_server_addr.s_mode = IEEE802154_ADDRMODE_SHORT;
|
||||
}
|
||||
else if (netlib_eaddrconv(argv[ndx], g_server_addr.s_eaddr))
|
||||
{
|
||||
g_server_addr.s_mode = IEEE802154_ADDRMODE_EXTENDED;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "ERROR: Server address is invalid\n");
|
||||
show_usage(argv[0]);
|
||||
}
|
||||
}
|
||||
else if (argc != 1)
|
||||
{
|
||||
fprintf(stderr, "ERROR: Unexpected number of arguments\n");
|
||||
show_usage(argv[0]);
|
||||
}
|
||||
|
||||
if (g_server_addr.s_mode == IEEE802154_ADDRMODE_NONE)
|
||||
{
|
||||
fprintf(stderr, "ERROR: No server address\n");
|
||||
show_usage(argv[0]);
|
||||
}
|
||||
}
|
204
examples/pf_ieee802154/pf_server.c
Normal file
204
examples/pf_ieee802154/pf_server.c
Normal file
@ -0,0 +1,204 @@
|
||||
/****************************************************************************
|
||||
* examples/pf_ieee802154/pf_server.c
|
||||
*
|
||||
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 Gregory Nutt nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "pfieee802154.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static inline int check_buffer(unsigned char *buf)
|
||||
{
|
||||
int ret = 1;
|
||||
int offset;
|
||||
int ch;
|
||||
int j;
|
||||
|
||||
offset = buf[0];
|
||||
for (ch = 0x20, j = offset + 1; ch < 0x7f; ch++, j++)
|
||||
{
|
||||
if (j >= SENDSIZE)
|
||||
{
|
||||
j = 1;
|
||||
}
|
||||
if (buf[j] != ch)
|
||||
{
|
||||
printf("server: Buffer content error for offset=%d, index=%d\n", offset, j);
|
||||
ret = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_BUILD_KERNEL
|
||||
int main(int argc, FAR char *argv[])
|
||||
#else
|
||||
int pfserver_main(int argc, char *argv[])
|
||||
#endif
|
||||
{
|
||||
struct sockaddr_ieee802154_s server;
|
||||
struct sockaddr_ieee802154_s client;
|
||||
unsigned char inbuf[1024];
|
||||
socklen_t addrlen;
|
||||
socklen_t recvlen;
|
||||
int sockfd;
|
||||
int nbytes;
|
||||
int optval;
|
||||
int offset;
|
||||
|
||||
/* Parse any command line options */
|
||||
|
||||
pf_cmdline(argc, argv);
|
||||
|
||||
/* Create a new DGRAM socket */
|
||||
|
||||
sockfd = socket(PF_IEEE802154, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
printf("server: socket failure: %d\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Set socket to reuse address */
|
||||
|
||||
optval = 1;
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||
{
|
||||
printf("server: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Bind the socket to a local address */
|
||||
|
||||
server.sa_family = AF_IEEE802154;
|
||||
memcpy(&server.sa_addr, &g_server_addr, sizeof(struct ieee802154_saddr_s));
|
||||
addrlen = sizeof(struct sockaddr_ieee802154_s);
|
||||
|
||||
if (bind(sockfd, (struct sockaddr*)&server, addrlen) < 0)
|
||||
{
|
||||
printf("server: bind failure: %d\n", errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Then receive up to 256 packets of data */
|
||||
|
||||
for (offset = 0; offset < 256; offset++)
|
||||
{
|
||||
printf("server: %d. Receiving up 1024 bytes\n", offset);
|
||||
recvlen = addrlen;
|
||||
nbytes = recvfrom(sockfd, inbuf, 1024, 0,
|
||||
(struct sockaddr *)&client, &recvlen);
|
||||
|
||||
if (client.sa_addr.s_mode == IEEE802154_ADDRMODE_SHORT)
|
||||
{
|
||||
printf("server: %d. Received %d bytes from %02x:02x\n",
|
||||
offset, nbytes,
|
||||
client.sa_addr.s_saddr[0], client.sa_addr.s_saddr[1]);
|
||||
}
|
||||
else if (client.sa_addr.s_mode == IEEE802154_ADDRMODE_SHORT)
|
||||
{
|
||||
printf("server: %d. Received %d bytes from "
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x:%02x: %02x\n",
|
||||
offset, nbytes,
|
||||
client.sa_addr.s_saddr[0], client.sa_addr.s_saddr[1],
|
||||
client.sa_addr.s_saddr[2], client.sa_addr.s_saddr[3],
|
||||
client.sa_addr.s_saddr[4], client.sa_addr.s_saddr[5],
|
||||
client.sa_addr.s_saddr[6], client.sa_addr.s_saddr[7]);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("server: %d. Received %d bytes (no address)\n",
|
||||
offset, nbytes);
|
||||
}
|
||||
|
||||
if (nbytes < 0)
|
||||
{
|
||||
printf("server: %d. recv failed: %d\n", offset, errno);
|
||||
close(sockfd);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (nbytes != SENDSIZE)
|
||||
{
|
||||
printf("server: %d. recv size incorrect: %d vs %d\n", offset, nbytes, SENDSIZE);
|
||||
close(sockfd);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (offset < inbuf[0])
|
||||
{
|
||||
printf("server: %d. %d packets lost, resetting offset\n", offset, inbuf[0] - offset);
|
||||
offset = inbuf[0];
|
||||
}
|
||||
else if (offset > inbuf[0])
|
||||
{
|
||||
printf("server: %d. Bad offset in buffer: %d\n", offset, inbuf[0]);
|
||||
close(sockfd);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!check_buffer(inbuf))
|
||||
{
|
||||
printf("server: %d. Bad buffer contents\n", offset);
|
||||
close(sockfd);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
return 0;
|
||||
}
|
67
examples/pf_ieee802154/pfieee802154.h
Normal file
67
examples/pf_ieee802154/pfieee802154.h
Normal file
@ -0,0 +1,67 @@
|
||||
/****************************************************************************
|
||||
* examples/pf_ieee802154/pfieee802154.h
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2015, 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 __EXAMPLES_PFIEEE802154_PFIEEE802154_H
|
||||
#define __EXAMPLES_PFIEEE802154_PFIEEE802154_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <debug.h>
|
||||
#include <netpacket/ieee802154.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define ASCIISIZE (0x7f - 0x20)
|
||||
#define SENDSIZE (ASCIISIZE+1)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
extern struct ieee802154_saddr_s g_server_addr;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
void pf_cmdline(int argc, char **argv);
|
||||
void pf_client(void);
|
||||
void pf_server(void);
|
||||
|
||||
#endif /* __EXAMPLES_PFIEEE802154_IEEE802154_H */
|
109
netutils/netlib/netlib_saddrconv.c
Normal file
109
netutils/netlib/netlib_saddrconv.c
Normal file
@ -0,0 +1,109 @@
|
||||
/****************************************************************************
|
||||
* netutils/netlib/netlib_saddrconv.c
|
||||
*
|
||||
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <nuttx/net/sixlowpan.h>
|
||||
|
||||
#include "netutils/netlib.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: netlib_saddrconv
|
||||
****************************************************************************/
|
||||
|
||||
bool netlib_saddrconv(FAR const char *hwstr, FAR uint8_t *hw)
|
||||
{
|
||||
unsigned char tmp;
|
||||
unsigned char i;
|
||||
unsigned char j;
|
||||
char ch;
|
||||
|
||||
/* Extended Address Form: xx:xx */
|
||||
|
||||
if (strlen(hwstr) != 5)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
tmp = 0;
|
||||
|
||||
for (i = 0; i < 2; ++i)
|
||||
{
|
||||
j = 0;
|
||||
do
|
||||
{
|
||||
ch = *hwstr++;
|
||||
if (++j > 3)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ch == ':' || ch == '\0')
|
||||
{
|
||||
*hw++ = tmp;
|
||||
tmp = 0;
|
||||
}
|
||||
else if (ch >= '0' && ch <= '9')
|
||||
{
|
||||
tmp = (tmp << 4) + (ch - '0');
|
||||
}
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
{
|
||||
tmp = (tmp << 4) + (ch - 'a' + 10);
|
||||
}
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
{
|
||||
tmp = (tmp << 4) + (ch - 'A' + 10);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while (ch != ':' && ch != 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user