From 9ae283031aad7c1869e9d9b695795b638a6a62e3 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Sat, 26 May 2018 05:03:08 -0700 Subject: [PATCH] Added deployment module --- os/services/deployment/deployment.c | 141 +++++++++++++++++++++++++ os/services/deployment/deployment.h | 126 ++++++++++++++++++++++ os/services/deployment/module-macros.h | 1 + os/sys/node-id.c | 5 + 4 files changed, 273 insertions(+) create mode 100644 os/services/deployment/deployment.c create mode 100644 os/services/deployment/deployment.h create mode 100644 os/services/deployment/module-macros.h diff --git a/os/services/deployment/deployment.c b/os/services/deployment/deployment.c new file mode 100644 index 000000000..c48a0cbae --- /dev/null +++ b/os/services/deployment/deployment.c @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2018, Swedish Institute of Computer Science. + * All rights reserved. + * + * 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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. + * + */ + +/** +* \addtogroup deployment +* @{ +*/ + +/** + * \file + * Code managing id<->mac address<->IPv6 address mapping, and doing this + * for different deployment scenarios: Cooja, Nodes, Indriya or Twist testbeds + * + * \author Simon Duquennoy + */ + +#include "contiki.h" +#include "contiki-net.h" +#include "deployment.h" +#include "sys/node-id.h" +#include +#include + +/** + * \brief List of ID<->MAC mapping used for different deployments + */ +extern const struct id_mac DEPLOYMENT_MAPPING[]; +/** + * \brief The number of nodes in the deployment + */ +static int node_count = 0; + +/*---------------------------------------------------------------------------*/ +void +deployment_init(void) +{ + const struct id_mac *curr = DEPLOYMENT_MAPPING; + /* Initialize node_id */ + node_id = deployment_id_from_lladdr((const linkaddr_t *)&linkaddr_node_addr); + /* Count nodes */ + node_count = 0; + while(curr->id != 0) { + node_count++; + curr++; + } +} +/*---------------------------------------------------------------------------*/ +int +deployment_node_count(void) +{ + return node_count; +} +/*---------------------------------------------------------------------------*/ +uint16_t +deployment_id_from_lladdr(const linkaddr_t *lladdr) +{ + const struct id_mac *curr = DEPLOYMENT_MAPPING; + if(lladdr == NULL) { + return 0; + } + while(curr->id != 0) { + /* Assume network-wide unique 16-bit MAC addresses */ + if(linkaddr_cmp(lladdr, &curr->mac)) { + return curr->id; + } + curr++; + } + return 0; +} +/*---------------------------------------------------------------------------*/ +void +deployment_lladdr_from_id(linkaddr_t *lladdr, uint16_t id) +{ + const struct id_mac *curr = DEPLOYMENT_MAPPING; + if(id == 0 || lladdr == NULL) { + return; + } + while(curr->id != 0) { + if(curr->id == id) { + linkaddr_copy(lladdr, &curr->mac); + return; + } + curr++; + } +} +/*---------------------------------------------------------------------------*/ +uint16_t +deployment_id_from_iid(const uip_ipaddr_t *ipaddr) +{ + const linkaddr_t lladdr; + uip_ds6_set_lladdr_from_iid((uip_lladdr_t *)&lladdr, ipaddr); + return deployment_id_from_lladdr(&lladdr); +} +/*---------------------------------------------------------------------------*/ +void +deployment_iid_from_id(uip_ipaddr_t *ipaddr, uint16_t id) +{ + linkaddr_t lladdr; + deployment_lladdr_from_id(&lladdr, id); + uip_ds6_set_addr_iid(ipaddr, (uip_lladdr_t *)&lladdr); +} +/*---------------------------------------------------------------------------*/ +uint16_t +deployment_id_from_index(uint16_t index) +{ + if(index < deployment_node_count()) { + return DEPLOYMENT_MAPPING[index].id; + } else { + return 0; + } +} +/*---------------------------------------------------------------------------*/ + +/** @} */ diff --git a/os/services/deployment/deployment.h b/os/services/deployment/deployment.h new file mode 100644 index 000000000..b2e3c1928 --- /dev/null +++ b/os/services/deployment/deployment.h @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2018, Swedish Institute of Computer Science. + * All rights reserved. + * + * 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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. + * + */ + +/** +* \addtogroup deployment +* @{ +* +* \file + Per-deployment MAC <-> nodeid mapping +* \author Simon Duquennoy +* +*/ + +#ifndef DEPLOYMENT_H_ +#define DEPLOYMENT_H_ + +#include "contiki-conf.h" +#include "sys/node-id.h" +#include "net/ipv6/uip.h" +#include "net/linkaddr.h" + +/** + * \brief ID<->MAC address mapping structure + */ +struct id_mac { + uint16_t id; + linkaddr_t mac; +}; + +/** + * DEPLOYMENT_MAPPING: + * A table of struct id_mac that provides ID-MAC mapping for a deployment. + * Example with four nodes: + * In configuration file: + * \#define DEPLOYMENT_MAPPING custom_array + * In a .c file: + * const struct id_mac custom_array[] = { + { 1, {{0x00,0x12,0x4b,0x00,0x06,0x0d,0xb6,0x14}}}, + { 2, {{0x00,0x12,0x4b,0x00,0x06,0x0d,0xb1,0xe7}}}, + { 3, {{0x00,0x12,0x4b,0x00,0x06,0x0d,0xb4,0x35}}}, + { 4, {{0x00,0x12,0x4b,0x00,0x06,0x0d,0xb1,0xcf}}}, + { 0, {{0}}} + }; + */ + +/** + * Initialize the deployment module + */ +void deployment_init(void); + +/** + * Get the number of nodes for the deployment (length of mapping table) + * + * \return The number of nodes in the deployment + */ +int deployment_node_count(void); + +/** + * Get node ID from a link-layer address, from the deployment mapping table + * + * \param lladdr The link-layer address to look up for + * \return Node ID from a corresponding link-layer address + */ +uint16_t deployment_id_from_lladdr(const linkaddr_t *lladdr); + +/** + * Get node link-layer address from a node ID, from the deployment mapping table + * + * \param lladdr A pointer where to write the link-layer address + * \param id The node ID to look up for + */ +void deployment_lladdr_from_id(linkaddr_t *lladdr, uint16_t id); + +/** + * Get node ID from the IID of an IPv6 address + * + * \param ipaddr The IPv6 (global or link-local) address that contains the IID + * \return Node ID from a corresponding IID + */ +uint16_t deployment_id_from_iid(const uip_ipaddr_t *ipaddr); + +/** + * Get IPv6 IID from node IDs + * + * \param ipaddr The IPv6 where to write the IID + * \param id The node ID + */ +void deployment_iid_from_id(uip_ipaddr_t *ipaddr, uint16_t id); + +/** + * Get node ID from index in mapping table + * + * \param index The index in the deployment mapping table + * \return Node ID at the corresponding index + */ +uint16_t deployment_id_from_index(uint16_t index); + +#endif /* DEPLOYMENT_H_ */ +/** @} */ diff --git a/os/services/deployment/module-macros.h b/os/services/deployment/module-macros.h new file mode 100644 index 000000000..ed2d639ef --- /dev/null +++ b/os/services/deployment/module-macros.h @@ -0,0 +1 @@ +#define BUILD_WITH_DEPLOYMENT 1 diff --git a/os/sys/node-id.c b/os/sys/node-id.c index 7914256e9..2fd52f8dc 100644 --- a/os/sys/node-id.c +++ b/os/sys/node-id.c @@ -40,12 +40,17 @@ #include "contiki.h" #include "sys/node-id.h" #include "net/linkaddr.h" +#include "services/deployment/deployment.h" uint16_t node_id = 0; void node_id_init(void) { +#if BUILD_WITH_DEPLOYMENT + deployment_init(); +#else /* BUILD_WITH_DEPLOYMENT */ /* Initialize with a default value derived from linkaddr */ node_id = linkaddr_node_addr.u8[LINKADDR_SIZE - 1] + (linkaddr_node_addr.u8[LINKADDR_SIZE - 2] << 8); +#endif /* BUILD_WITH_DEPLOYMENT */ }