From 310cdab3d30b21dd9d29f372f3a411ef1003059f Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Tue, 26 Mar 2019 09:50:08 +0100 Subject: [PATCH] ble/nimble: add addr helper module --- pkg/nimble/Makefile | 3 ++ pkg/nimble/Makefile.dep | 4 ++ pkg/nimble/Makefile.include | 5 +++ pkg/nimble/addr/Makefile | 3 ++ pkg/nimble/addr/include/nimble_addr.h | 58 +++++++++++++++++++++++++++ pkg/nimble/addr/nimble_addr.c | 47 ++++++++++++++++++++++ 6 files changed, 120 insertions(+) create mode 100644 pkg/nimble/addr/Makefile create mode 100644 pkg/nimble/addr/include/nimble_addr.h create mode 100644 pkg/nimble/addr/nimble_addr.c diff --git a/pkg/nimble/Makefile b/pkg/nimble/Makefile index 8431345abc44..412cea1a9cd5 100644 --- a/pkg/nimble/Makefile +++ b/pkg/nimble/Makefile @@ -64,4 +64,7 @@ nimble_controller: nimble_drivers_nrf52: "$(MAKE)" -C $(PDIR)/nimble/drivers/nrf52/src/ -f $(TDIR)/drivers.nrf52.mk +# additional, RIOT specific nimble modules +nimble_addr: + "$(MAKE)" -C $(TDIR)/addr/ include $(RIOTBASE)/pkg/pkg.mk diff --git a/pkg/nimble/Makefile.dep b/pkg/nimble/Makefile.dep index bad9ccdef33f..da0edf3996cb 100644 --- a/pkg/nimble/Makefile.dep +++ b/pkg/nimble/Makefile.dep @@ -30,3 +30,7 @@ ifneq (,$(filter nimble_controller,$(USEMODULE))) USEMODULE += nimble_drivers_nrf52 endif endif + +ifneq (,$(filter nimble_addr,$(USEMODULE))) + USEMODULE += bluetil_addr +endif diff --git a/pkg/nimble/Makefile.include b/pkg/nimble/Makefile.include index f85f61586339..6c7c67c681a7 100644 --- a/pkg/nimble/Makefile.include +++ b/pkg/nimble/Makefile.include @@ -68,3 +68,8 @@ endif ifneq (,$(filter nimble_svc_tps,$(USEMODULE))) INCLUDES += $(NIMIBASE)/nimble/host/services/tps/include endif + +# include additional headers for RIOT specific NimBLE submodules +ifneq (,$(filter nimble_addr,$(USEMODULE))) + INCLUDES += -I$(RIOTPKG)/nimble/addr/include +endif diff --git a/pkg/nimble/addr/Makefile b/pkg/nimble/addr/Makefile new file mode 100644 index 000000000000..a0c23cd58b03 --- /dev/null +++ b/pkg/nimble/addr/Makefile @@ -0,0 +1,3 @@ +MODULE = nimble_addr + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/nimble/addr/include/nimble_addr.h b/pkg/nimble/addr/include/nimble_addr.h new file mode 100644 index 000000000000..063a77d31c03 --- /dev/null +++ b/pkg/nimble/addr/include/nimble_addr.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2019 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup ble_nimble_addr Address helper for NimBLE + * @ingroup ble_nimble + * @brief NimBLE specific helper functions for handling addresses + * @{ + * + * @file + * @brief Interface for NimBLE specific address helper functions + * + * @author Hauke Petersen + */ + +#ifndef NIMBLE_ADDR_H +#define NIMBLE_ADDR_H + +#include "nimble/ble.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Length of an address string in byte, including `\0` termination + */ +#define NIMBLE_ADDR_STRLEN (28U) + +/** + * @brief Print the given address on STDIO + * + * @param[in] addr address to print + */ +void nimble_addr_print(const ble_addr_t *addr); + +/** + * @brief Write the string representation of the given address into a buffer + * + * The resulting string written to @p buf is `\0` terminated and is always + * 28 bytes (NIMBLE_ADDR_STRLEN) long. + * + * @param[out] buf buffer + * @param[in] addr addre to convert + */ +void nimble_addr_sprint(char *buf, const ble_addr_t *addr); + +#ifdef __cplusplus +} +#endif + +#endif /* NIMBLE_ADDR_H */ +/** @} */ diff --git a/pkg/nimble/addr/nimble_addr.c b/pkg/nimble/addr/nimble_addr.c new file mode 100644 index 000000000000..a698fcd3eb2c --- /dev/null +++ b/pkg/nimble/addr/nimble_addr.c @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2019 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup ble_nimble_addr + * @{ + * + * @file + * @brief NimBLE specific BLE address helper functions + * + * @author Hauke Petersen + * + * @} + */ + +#include +#include + +#include "nimble_addr.h" +#include "net/bluetil/addr.h" + +void nimble_addr_print(const ble_addr_t *addr) +{ + char tmp[NIMBLE_ADDR_STRLEN]; + nimble_addr_sprint(tmp, addr); + printf("%s", tmp); +} + +void nimble_addr_sprint(char *buf, const ble_addr_t *addr) +{ + bluetil_addr_sprint(buf, addr->val); + const char *type; + switch (addr->type) { + case BLE_ADDR_PUBLIC: type = " (public) "; break; + case BLE_ADDR_RANDOM: type = " (random) "; break; + case BLE_ADDR_PUBLIC_ID: type = " (id_pub) "; break; + case BLE_ADDR_RANDOM_ID: type = " (id_rand)"; break; + default: type = " (unknown)"; break; + } + memcpy(&buf[BLUETIL_ADDR_STRLEN - 1], type, 10); + buf[BLUETIL_ADDR_STRLEN - 1] = '\0'; +}