apps/wireless/bluetooth/btsak: Add beginning of a a Bluetooth tool.

This commit is contained in:
Gregory Nutt 2018-04-02 10:47:17 -06:00
parent 467a7980f0
commit 9f1a10996b
12 changed files with 1233 additions and 6 deletions

View File

@ -0,0 +1,10 @@
#
# For a description of the syntax of this configuration file,
# see misc/tools/kconfig-language.txt.
#
menu "Bluetooth applications"
source "$APPSDIR/wireless/bluetooth/btsak/Kconfig"
endmenu # Bluetooth applications

View File

@ -0,0 +1,37 @@
############################################################################
# apps/wireless/bluetooth/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2018 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 $(wildcard wireless/bluetooth/*/Make.defs)

View File

@ -0,0 +1,71 @@
############################################################################
# apps/wireless/bluetooth/Makefile
#
# Copyright (C) 2018 Gregory Nutt. All rights reserved.
# Author: Sebastien Lorquet <sebastien@lorquet.fr>
#
# 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 = $(dir $(wildcard */Makefile))
all: nothing
.PHONY: nothing context depend clean distclean preconfig
.PRECIOUS: ../../libapps$(LIBEXT)
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)
preconfig:
-include Make.dep

11
wireless/bluetooth/btsak/.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/Make.dep
/.depend
/.built
/*.asm
/*.obj
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src

View File

@ -0,0 +1,42 @@
#
# For a description of the syntax of this configuration file,
# see misc/tools/kconfig-language.txt.
#
config BTSAK
bool "Bluetooth Swiss Army Knife"
default n
---help---
Enable the Bluetooth Swiss Army Knife
if BTSAK
config BTSAK_PROGNAME
string "Program name"
default "bt"
depends on BUILD_KERNEL
---help---
This is the name of the program that will be use when the NSH ELF
program is installed.
config BTSAK_PRIORITY
int "btsak task priority"
default 100
config BTSAK_STACKSIZE
int "btsak stack size"
default 2048
if NET_6LOWPAN && !NET_BLUETOOTH
config BTSAK_DEFAULT_PORT
int "Default Port"
default 61616
---help---
The default port to be used for commands when using btsak through a
Pv6 network interface (ex. wpan0). Port is 61616 because 6LoWPAN has
special compression for port 61616-61631
endif
endif

View File

@ -0,0 +1,39 @@
############################################################################
# apps/wireless/bluetooth/btsak/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2018 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_BTSAK),y)
CONFIGURED_APPS += wireless/bluetooth/btsak
endif

View File

@ -0,0 +1,138 @@
############################################################################
# apps/wireless/bluetooth/btsak/Makefile
#
# Copyright (C) 2018 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
# Bluetooth SAK (swiss army knife )
APPNAME = i8
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 4096
# Bluetooth SAK (Swiss Army Knife)
ASRCS =
CSRCS =
MAINSRC = btsak_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_BTSAK_PROGNAME ?= bt$(EXEEXT)
PROGNAME = $(CONFIG_BTSAK_PROGNAME)
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
.PRECIOUS: ../../../libapps$(LIBEXT)
$(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

View File

@ -0,0 +1,246 @@
/****************************************************************************
* apps/wireless/bluetooth/btsak/btsak.h
* Bluetooth Swiss Army Knife
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Based loosely on the i8sak IEEE 802.15.4 program by Anthony Merlino and
* Sebastien Lorquet. Commands inspired for btshell example in the
* Intel/Zephyr Arduino 101 package (BSD license).
*
* 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_EXAMPLES_WIRELESS_BLUETOOTH_BTSAK_BTSAK_H
#define __APPS_EXAMPLES_WIRELESS_BLUETOOTH_BTSAK_BTSAK_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netpacket/bluetooth.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BTSAK_MAX_IFNAME 12
#define BTSAK_DAEMONNAME_FMT "btsak_%s"
#define BTSAK_DAEMONNAME_PREFIX_LEN 6
#define BTSAK_MAX_DAEMONNAME BTSAK_DAEMONNAME_PREFIX_LEN + BTSAK_MAX_IFNAME
#define BTSAK_DEFAULT_EPADDR {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
#define PRINTF_FORMAT_ADDR(addr) \
"%02x:%02x:%02x:%02x:%02x:%02x\n", \
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
/****************************************************************************
* Public Types
****************************************************************************/
struct btsak_s
{
FAR char *progname; /* Program name */
FAR char *ifname; /* Interface name */
int sockfd; /* Socket descriptor */
bt_addr_t ep_btaddr; /* Blue tooth address */
#if defined(CONFIG_NET_BLUETOOTH)
struct sockaddr_bt_s ep_sockaddr; /* AF_BLUETOOTH endpoint address */
#elif defined(CONFIG_NET_6LOWPAN)
struct sockaddr_in6 ep_sockaddr; /* IPv6 endpoint address */
#endif
};
/****************************************************************************
* Inline Functions
****************************************************************************/
#ifdef CONFIG_NET_6LOWPAN
static inline void btsak_update_ipv6addr(FAR struct btsak_s *btsak)
{ dev->d_ipv6addr[0] = HTONS(0xfe80);
dev->d_ipv6addr[1] = 0;
dev->d_ipv6addr[2] = 0;
dev->d_ipv6addr[3] = 0;
dev->d_ipv6addr[4] = HTONS(0x0200);
dev->d_ipv6addr[5] = (uint16_t)addr[0] << 8 | (uint16_t)addr[1];
dev->d_ipv6addr[6] = (uint16_t)addr[2] << 8 | (uint16_t)addr[3];
dev->d_ipv6addr[7] = (uint16_t)addr[4] << 8 | (uint16_t)addr[5];
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[0] = HTONS(0xfe80);
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[1] = 0;
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[2] = 0;
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[3] = 0;
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[4] = HTONS(0x0200);
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[5] =
((uint16_t)btsak->ep_btaddr.val[0] << 8 | (uint16_t)btsak->ep_btaddr.val[1]);
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[6] =
((uint16_t)btsak->ep_btaddr.val[2] << 8 | (uint16_t)btsak->ep_btaddr.val[3]);
btsak->ep_in6addr.sin6_addr.in6_u.u6_addr16[7] =
((uint16_t)btsak->ep_btaddr.val[4] << 8 | (uint16_t)btsak->ep_btaddr.val[5]);
}
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: btsak_char2nibble
*
* Description:
* Convert an hexadecimal character to a 4-bit nibble.
*
****************************************************************************/
uint8_t btsak_char2nibble(char ch);
/****************************************************************************
* Name: btsak_str2long
*
* Description:
* Convert a hex string to an integer value
*
****************************************************************************/
long btsak_str2long(FAR const char *str);
/****************************************************************************
* Name: btsak_str2luint8
*
* Description:
* Convert a string to an integer value
*
****************************************************************************/
uint8_t btsak_str2luint8(FAR const char *str);
/****************************************************************************
* Name: btsak_str2luint16
*
* Description:
* Convert a string to an integer value
*
****************************************************************************/
uint16_t btsak_str2luint16(FAR const char *str);
/****************************************************************************
* Name: btsak_str2bool
*
* Description:
* Convert a boolean name to a boolean value.
*
****************************************************************************/
bool btsak_str2bool(FAR const char *str);
/****************************************************************************
* Name : btsak_str2payload
*
* Description :
* Parse string to get buffer of data. Buf is expected to be of size
* BLUETOOTH_SMP_MTU or larger.
*
* Returns:
* Positive length value of frame payload
****************************************************************************/
int btsak_str2payload(FAR const char *str, FAR uint8_t *buf);
/****************************************************************************
* Name: btsak_str2addr
*
* Description:
* Convert a string 8-byte EADDR array.
*
****************************************************************************/
void btsak_str2addr(FAR const char *str, FAR uint8_t *addr);
/****************************************************************************
* Name: btsak_socket
*
* Description:
* Create a socket on the selected device.
*
****************************************************************************/
int btsak_socket(FAR struct btsak_s *btsak);
/****************************************************************************
* Name: btsak_showusage
*
* Description:
* Show program usage.
*
****************************************************************************/
void btsak_showusage(FAR const char *progname, int exitcode);
/****************************************************************************
* Name: btsak_gatt_showusage
*
* Description:
* Show gatt command usage.
*
****************************************************************************/
void btsak_gatt_showusage(FAR const char *progname, FAR const char *cmd,
int exitcode);
/****************************************************************************
* Command handlers
****************************************************************************/
void btsak_cmd_scan(FAR struct btsak_s *btsak, int argc, FAR char *argv[]);
void btsak_cmd_advertise(FAR struct btsak_s *btsak, int argc,
FAR char *argv[]);
void btsak_cmd_security(FAR struct btsak_s *btsak, int argc,
FAR char *argv[]);
void btsak_cmd_gatt_exchange_mtu(FAR struct btsak_s *btsak, int argc,
FAR char *argv[]);
void btsak_cmd_discover(FAR struct btsak_s *btsak, int argc,
FAR char *argv[]);
void btsak_cmd_gatt_discover_characteristc(FAR struct btsak_s *btsak,
int argc, FAR char *argv[]);
void btsak_cmd_gat_discover_descriptor(FAR struct btsak_s *btsak,
int argc, FAR char *argv[]);
void btsak_cmd_gatt_read(FAR struct btsak_s *btsak, int argc,
FAR char *argv[]);
void btsak_cmd_gatt_read_multiple(FAR struct btsak_s *btsak, int argc,
FAR char *argv[]);
void btsak_cmd_gatt_write(FAR struct btsak_s *btsak, int argc,
FAR char *argv[]);
#endif /* __APPS_EXAMPLES_WIRELESS_BLUETOOTH_BTSAK_BTSAK_H */

View File

@ -0,0 +1,633 @@
/****************************************************************************
* apps/wireless/bluetooth/btsak/btsak_main.c
* Bluetooth Swiss Army Knife
*
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Based loosely on the i8sak IEEE 802.15.4 program by Anthony Merlino and
* Sebastien Lorquet. Commands inspired for btshell example in the
* Intel/Zephyr Arduino 101 package (BSD license).
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <fcntl.h>
#include <queue.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/wireless/bt_ioctl.h>
#include <nuttx/net/bluetooth.h>
#include "btsak.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#if !defined(CONFIG_BTSAK_NINSTANCES) || CONFIG_BTSAK_NINSTANCES <= 0
# undef CONFIG_BTSAK_NINSTANCES
# define CONFIG_BTSAK_NINSTANCES 3
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/* Describes one command */
struct btsak_command_s
{
FAR const char *name;
CODE void (*handler)(FAR struct btsak_s *btsak, int argc, FAR char *argv[]);
FAR const char *help;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
void btsak_cmd_help(FAR struct btsak_s *btsak, int argc, FAR char *argv[]);
void btsak_cmd_gatt(FAR struct btsak_s *btsak, int argc, FAR char *argv[]);
/****************************************************************************
* Private Data
****************************************************************************/
/* Alphabetical, except for help */
static const struct btsak_command_s g_btsak_commands[] =
{
{
"help",
(CODE void *)btsak_cmd_help,
NULL
},
{"scan",
(CODE void *)btsak_cmd_scan,
"[-h] <on|off>"
},
{
"advertise",
(CODE void *)btsak_cmd_advertise,
"[-h] <on|off>"
},
{
"security",
(CODE void *)btsak_cmd_security,
"[-h] <addr> <addr-type> <level>"
},
{
"gatt",
(CODE void *)btsak_cmd_gatt,
"[-h] <cmd> [option [option [option...]]]"
}
};
#define NCOMMANDS (sizeof(g_btsak_commands) / sizeof(struct btsak_command_s))
static const struct btsak_command_s g_btsak_gatt_commands[] =
{
{"exchange-mtu",
(CODE void *)btsak_cmd_gatt_exchange_mtu,
"[-h] <addr> <addr-type>"
},
{
"discover",
(CODE void *)btsak_cmd_discover,
"[-h] <addr> <addr-type> <uuid-type>"
},
{
"characteristic",
(CODE void *)btsak_cmd_gatt_discover_characteristc,
"[-h] <addr> <addr-type>"
},
{
"descriptor",
(CODE void *)btsak_cmd_gat_discover_descriptor,
"[-h] <addr> <addr-type>"
},
{
"read",
(CODE void *)btsak_cmd_gatt_read,
"[-h] <addr> <addr-type> <handle> [<offset>]"
},
{
"read-multiple",
(CODE void *)btsak_cmd_gatt_read_multiple,
"[-h] <addr> <addr-type> <handle> <nitems>"
},
{
"write",
(CODE void *)btsak_cmd_gatt_write,
"[-h] <addr> <addr-type> <handle> <datum>"
}
};
#define GATT_NCOMMANDS (sizeof(g_btsak_gatt_commands) / sizeof(struct btsak_command_s))
static const bt_addr_t g_default_epaddr =
{
BTSAK_DEFAULT_EPADDR
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: btsak_cmd_help
*
* Description:
* Handle help command.
*
****************************************************************************/
void btsak_cmd_help(FAR struct btsak_s *btsak, int argc, FAR char *argv[])
{
btsak_showusage(btsak->progname, EXIT_SUCCESS);
}
/****************************************************************************
* Name: btsak_cmd_gatt
*
* Description:
* Handle gatt command.
*
****************************************************************************/
void btsak_cmd_gatt(FAR struct btsak_s *btsak, int argc, FAR char *argv[])
{
FAR const struct btsak_command_s *cmd;
int argind;
int i;
/* Verify that a command was provided */
argind = 1;
if (argc < 2)
{
fprintf(stderr, "ERROR: Missing gatt command\n");
btsak_showusage(btsak->progname, EXIT_FAILURE);
}
/* Check for help */
if (strcmp(argv[argind], "-h") == 0)
{
btsak_gatt_showusage(btsak->progname, argv[0], EXIT_SUCCESS);
}
/* Find the command in the g_btsak_gatt_commands[] list */
cmd = NULL;
for (i = 0; i < GATT_NCOMMANDS; i++)
{
FAR const struct btsak_command_s *gattcmd = &g_btsak_gatt_commands[i];
if (strcmp(argv[argind], gattcmd->name) == 0)
{
cmd = gattcmd;
break;
}
}
if (cmd == NULL)
{
fprintf(stderr, "ERROR: Unrecognized gatt command: %s\n", argv[argind]);
btsak_gatt_showusage(btsak->progname, argv[0], EXIT_SUCCESS);
}
cmd->handler(btsak, argc - argind, &argv[argind]);
}
/****************************************************************************
* bt_main
****************************************************************************/
#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int bt_main(int argc, char *argv[])
#endif
{
FAR const struct btsak_command_s *cmd;
struct btsak_s btsak;
int argind;
int i;
/* Initialize the default remote endpoint address */
memset(&btsak, 0, sizeof(struct btsak_s));
BLUETOOTH_ADDRCOPY(btsak.ep_btaddr.val, g_default_epaddr.val);
btsak.progname = argv[0];
/* Check if ifname was included */
argind = 1;
if (argc < 2)
{
fprintf(stderr, "ERROR: Missing interface name\n");
btsak_showusage(btsak.progname, EXIT_FAILURE);
}
btsak.ifname = argv[argind];
argind++;
if (argc < 3)
{
fprintf(stderr, "ERROR: Missing command\n");
btsak_showusage(btsak.progname, EXIT_FAILURE);
}
/* Find the command in the g_btsak_command[] list */
cmd = NULL;
for (i = 0; i < NCOMMANDS; i++)
{
FAR const struct btsak_command_s *btcmd = &g_btsak_commands[i];
if (strcmp(argv[argind], btcmd->name) == 0)
{
cmd = btcmd;
break;
}
}
if (cmd == NULL)
{
fprintf(stderr, "ERROR: Unrecognized command: %s\n", argv[argind]);
btsak_showusage(btsak.progname, EXIT_FAILURE);
}
cmd->handler(&btsak, argc - argind, &argv[argind]);
return EXIT_SUCCESS;
}
/****************************************************************************
* Name: btsak_char2nibble
*
* Description:
* Convert an hexadecimal character to a 4-bit nibble.
*
****************************************************************************/
uint8_t btsak_char2nibble(char ch)
{
if (ch >= '0' && ch <= '9')
{
return ch - '0';
}
else if (ch >= 'a' && ch <= 'f')
{
return ch - 'a' + 10;
}
else if (ch >= 'A' && ch <= 'F')
{
return ch - 'A' + 10;
}
else if (ch == '\0')
{
fprintf(stderr, "ERROR: Unexpected end hex\n");
exit(EXIT_FAILURE);
}
else
{
fprintf(stderr, "ERROR: Unexpected character in hex value: %02x\n", ch);
exit(EXIT_FAILURE);
}
}
/****************************************************************************
* Name: btsak_str2long
*
* Description:
* Convert a hex string to an integer value
*
****************************************************************************/
long btsak_str2long(FAR const char *str)
{
FAR char *endptr;
long value;
value = strtol(str, &endptr, 0);
if (*endptr != '\0')
{
fprintf(stderr, "ERROR: Garbage after numeric argument\n");
exit(EXIT_FAILURE);
}
if (value > INT_MAX || value < INT_MIN)
{
fprintf(stderr, "ERROR: Integer value out of range\n");
return LONG_MAX;
exit(EXIT_FAILURE);
}
return value;
}
/****************************************************************************
* Name: btsak_str2luint8
*
* Description:
* Convert a string to an integer value
*
****************************************************************************/
uint8_t btsak_str2luint8(FAR const char *str)
{
long value = btsak_str2long(str);
if (value < 0 || value > UINT8_MAX)
{
fprintf(stderr, "ERROR: 8-bit value out of range\n");
exit(EXIT_FAILURE);
}
return (uint8_t)value;
}
/****************************************************************************
* Name: btsak_str2luint16
*
* Description:
* Convert a string to an integer value
*
****************************************************************************/
uint16_t btsak_str2luint16(FAR const char *str)
{
long value = btsak_str2long(str);
if (value < 0 || value > UINT16_MAX)
{
fprintf(stderr, "ERROR: 16-bit value out of range\n");
exit(EXIT_FAILURE);
}
return (uint16_t)value;
}
/****************************************************************************
* Name: btsak_str2bool
*
* Description:
* Convert a boolean name to a boolean value.
*
****************************************************************************/
bool btsak_str2bool(FAR const char *str)
{
if (strcasecmp(str, "true") == 0)
{
return true;
}
else if (strcasecmp(str, "false") == 0)
{
return false;
}
else
{
fprintf(stderr, "ERROR: Invalid boolean name: %s\n", str);
fprintf(stderr, " Expected true or false\n");
exit(EXIT_FAILURE);
}
}
/****************************************************************************
* Name : btsak_str2payload
*
* Description :
* Parse string to get buffer of data. Buf is expected to be of size
* BLUETOOTH_SMP_MTU or larger.
*
* Returns:
* Positive length value of frame payload
****************************************************************************/
int btsak_str2payload(FAR const char *str, FAR uint8_t *buf)
{
int str_len;
int ret;
int i = 0;
str_len = strlen(str);
/* Each byte is represented by 2 chars */
ret = str_len >> 1;
/* Check if the number of chars is a multiple of 2 and that the number of
* bytes does not exceed the max MAC frame payload supported.
*/
if ((str_len & 1) || (ret > BLUETOOTH_SMP_MTU))
{
fprintf(stderr, "ERROR: Invalid payload\n");
exit(EXIT_FAILURE);
}
/* Decode hex packet */
while (str_len > 0)
{
int dat;
if (sscanf(str, "%2x", &dat) == 1)
{
buf[i++] = dat;
str += 2;
str_len -= 2;
}
else
{
fprintf(stderr, "ERROR: Invalid payload\n");
exit(EXIT_FAILURE);
}
}
return ret;
}
/****************************************************************************
* Name: btsak_str2addr
*
* Description:
* Convert a string 8-byte EADDR array.
*
****************************************************************************/
void btsak_str2addr(FAR const char *str, FAR uint8_t *addr)
{
FAR const char *src = str;
uint8_t bvalue;
char ch;
int i;
for (i = 0; i < 8; i++)
{
ch = (char)*src++;
bvalue = btsak_char2nibble(ch) << 4;
ch = (char)*src++;
bvalue |= btsak_char2nibble(ch);
*addr++ = bvalue;
if (i < 7)
{
ch = (char)*src++;
if (ch != ':')
{
fprintf(stderr, "ERROR: Missing colon separator: %s\n", str);
fprintf(stderr, " Expected xx:xx:xx:xx:xx:xx:xx:xx\n");
exit(EXIT_FAILURE);
}
}
}
}
/****************************************************************************
* Name: btsak_socket
*
* Description:
* Create a socket on the selected device.
*
****************************************************************************/
int btsak_socket(FAR struct btsak_s *btsak)
{
int sockfd = -1;
/* Create the socket with the correct addressing */
BLUETOOTH_ADDRCOPY(btsak->ep_btaddr.val, g_default_epaddr.val);
#if defined(CONFIG_NET_BLUETOOTH)
btsak->ep_sockaddr.bt_family = AF_BLUETOOTH;
btsak->ep_sockaddr.bt_channel = 0; /* REVISIT */
BLUETOOTH_ADDRCOPY(btsak->ep_sockaddr.bt_bdaddr.val, btsak->ep_btaddr.val);
sockfd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
#elif defined(CONFIG_NET_6LOWPAN)
btsak->ep_sockaddr.sin6_family = AF_INET6;
btsak->ep_sockaddr.sin6_port = HTONS(CONFIG_BTSAK_DEFAULT_PORT);
btsak_update_ipv6addr(btsak);
sockfd = socket(PF_INET6, SOCK_DGRAM, 0);
#endif
if (sockfd < 0)
{
fprintf(stderr, "ERROR: failed to create socket, errno=%d\n", errno);
}
return sockfd;
}
/****************************************************************************
* Name: btsak_showusage
*
* Description:
* Show program usage.
*
****************************************************************************/
void btsak_showusage(FAR const char *progname, int exitcode)
{
int i;
fprintf(stderr, "\nUsage:\n\n");
fprintf(stderr, "\t%s progname <ifname> <cmd> [option [option [option...]]]\n",
progname);
fprintf(stderr, "\nWhere <cmd> [option [option [option...]]] is one of:\n\n");
for (i = 0; i < NCOMMANDS; i++)
{
FAR const struct btsak_command_s *cmd = &g_btsak_commands[i];
if (cmd->help != NULL)
{
fprintf(stderr, "\t%s\t%s\n", cmd->name, cmd->help);
}
else
{
fprintf(stderr, "\t%s\n", cmd->name);
}
}
fprintf(stderr, "\nUse the -h option on any command to get more info.\n");
exit(exitcode);
}
/****************************************************************************
* Name: btsak_gatt_showusage
*
* Description:
* Show gatt command usage.
*
****************************************************************************/
void btsak_gatt_showusage(FAR const char *progname, FAR const char *cmd,
int exitcode)
{
int i;
fprintf(stderr, "%s: Generic Attribute (GATT) commands:\n", cmd);
fprintf(stderr, "Usage:\n\n");
fprintf(stderr, " %s <ifname> %s [-h] <cmd> [option [option [option...]]]\n",
progname, cmd);
fprintf(stderr, "\nWhere <cmd> [option [option [option...]]] is one of:\n\n");
for (i = 0; i < GATT_NCOMMANDS; i++)
{
FAR const struct btsak_command_s *gattcmd = &g_btsak_gatt_commands[i];
if (gattcmd->help != NULL)
{
fprintf(stderr, "\t%s\t%s\n", gattcmd->name, gattcmd->help);
}
else
{
fprintf(stderr, "\t%s\n", gattcmd->name);
}
}
fprintf(stderr, "\nUse the -h option on any command to get more info.\n");
exit(exitcode);
}

View File

@ -79,8 +79,8 @@ else
INSTALL_DIR = $(BIN_DIR)
endif
CONFIG_EXAMPLES_IEEEDUMP_PROGNAME ?= ieeedump$(EXEEXT)
PROGNAME = $(CONFIG_EXAMPLES_IEEEDUMP_PROGNAME)
CONFIG_IEEE802154_I8SAK_PROGNAME ?= i8$(EXEEXT)
PROGNAME = $(CONFIG_IEEE802154_I8SAK_PROGNAME)
ROOTDEPPATH = --dep-path .

View File

@ -39,8 +39,8 @@
*
****************************************************************************/
#ifndef __APPS_EXAMPLES_WIRELESS_IEEE802154_I8SAK_H
#define __APPS_EXAMPLES_WIRELESS_IEEE802154_I8SAK_H
#ifndef __APPS_EXAMPLES_WIRELESS_IEEE802154_I8SAK_I8SAK_H
#define __APPS_EXAMPLES_WIRELESS_IEEE802154_I8SAK_I8SAK_H
/****************************************************************************
* Included Files
@ -291,4 +291,4 @@ static inline void i8sak_update_ep_ip(FAR struct i8sak_s *i8sak)
}
#endif
#endif /* __APPS_EXAMPLES_WIRELESS_IEEE802154_I8SAK_H */
#endif /* __APPS_EXAMPLES_WIRELESS_IEEE802154_I8SAK_I8SAK_H */

View File

@ -8,7 +8,7 @@
*
* Author: Sebastien Lorquet <sebastien@lorquet.fr>
* Author: Anthony Merlino <anthony@vergeaero.com>
* Author: Gregory Nuttx <gnutt@nuttx.org>
* 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