Contiki-NG
deployment.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, RISE SICS.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /**
32 * \addtogroup deployment
33 * @{
34 */
35 
36 /**
37  * \file
38  * Code managing id<->mac address<->IPv6 address mapping, and doing this
39  * for different deployment scenarios: Cooja, Nodes, Indriya or Twist testbeds
40  *
41  * \author Simon Duquennoy <simonduq@sics.se>
42  */
43 
44 #include "contiki.h"
45 #include "contiki-net.h"
46 #include "deployment.h"
47 #include "sys/node-id.h"
48 #include <string.h>
49 #include <stdio.h>
50 
51 /**
52  * \brief List of ID<->MAC mapping used for different deployments
53  */
54 extern const struct id_mac DEPLOYMENT_MAPPING[];
55 /**
56  * \brief The number of nodes in the deployment
57  */
58 static int node_count = 0;
59 
60 /*---------------------------------------------------------------------------*/
61 void
63 {
64  const struct id_mac *curr = DEPLOYMENT_MAPPING;
65  /* Initialize node_id */
66  node_id = deployment_id_from_lladdr((const linkaddr_t *)&linkaddr_node_addr);
67  /* Count nodes */
68  node_count = 0;
69  while(curr->id != 0) {
70  node_count++;
71  curr++;
72  }
73 }
74 /*---------------------------------------------------------------------------*/
75 int
77 {
78  return node_count;
79 }
80 /*---------------------------------------------------------------------------*/
81 uint16_t
82 deployment_id_from_lladdr(const linkaddr_t *lladdr)
83 {
84  const struct id_mac *curr = DEPLOYMENT_MAPPING;
85  if(lladdr == NULL) {
86  return 0;
87  }
88  while(curr->id != 0) {
89  /* Assume network-wide unique 16-bit MAC addresses */
90  if(linkaddr_cmp(lladdr, &curr->mac)) {
91  return curr->id;
92  }
93  curr++;
94  }
95  return 0;
96 }
97 /*---------------------------------------------------------------------------*/
98 void
99 deployment_lladdr_from_id(linkaddr_t *lladdr, uint16_t id)
100 {
101  const struct id_mac *curr = DEPLOYMENT_MAPPING;
102  if(id == 0 || lladdr == NULL) {
103  return;
104  }
105  while(curr->id != 0) {
106  if(curr->id == id) {
107  linkaddr_copy(lladdr, &curr->mac);
108  return;
109  }
110  curr++;
111  }
112 }
113 /*---------------------------------------------------------------------------*/
114 uint16_t
115 deployment_id_from_iid(const uip_ipaddr_t *ipaddr)
116 {
117  const linkaddr_t lladdr;
118  uip_ds6_set_lladdr_from_iid((uip_lladdr_t *)&lladdr, ipaddr);
119  return deployment_id_from_lladdr(&lladdr);
120 }
121 /*---------------------------------------------------------------------------*/
122 void
123 deployment_iid_from_id(uip_ipaddr_t *ipaddr, uint16_t id)
124 {
125  linkaddr_t lladdr;
126  deployment_lladdr_from_id(&lladdr, id);
127  uip_ds6_set_addr_iid(ipaddr, (uip_lladdr_t *)&lladdr);
128 }
129 /*---------------------------------------------------------------------------*/
130 uint16_t
132 {
133  if(index < deployment_node_count()) {
134  return DEPLOYMENT_MAPPING[index].id;
135  } else {
136  return 0;
137  }
138 }
139 /*---------------------------------------------------------------------------*/
140 
141 /** @} */
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:116
void uip_ds6_set_lladdr_from_iid(uip_lladdr_t *lladdr, const uip_ipaddr_t *ipaddr)
Build a link-layer address from an IPv6 address based on its UUID64.
Definition: uip-ds6.c:597
Node-id (simple 16-bit identifiers) handling.
void deployment_iid_from_id(uip_ipaddr_t *ipaddr, uint16_t id)
Get IPv6 IID from node IDs.
Definition: deployment.c:123
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition: uip-ds6.c:576
ID<->MAC address mapping structure.
Definition: deployment.h:54
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
void deployment_init(void)
DEPLOYMENT_MAPPING: A table of struct id_mac that provides ID-MAC mapping for a deployment.
Definition: deployment.c:62
int deployment_node_count(void)
Get the number of nodes for the deployment (length of mapping table)
Definition: deployment.c:76
const struct id_mac DEPLOYMENT_MAPPING[]
List of ID<->MAC mapping used for different deployments.
Per-deployment MAC <-> nodeid mapping.
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a link-layer address.
Definition: linkaddr.c:63
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
uint16_t deployment_id_from_lladdr(const linkaddr_t *lladdr)
Get node ID from a link-layer address, from the deployment mapping table.
Definition: deployment.c:82
void deployment_lladdr_from_id(linkaddr_t *lladdr, uint16_t id)
Get node link-layer address from a node ID, from the deployment mapping table.
Definition: deployment.c:99
static int node_count
The number of nodes in the deployment.
Definition: deployment.c:58
uint16_t deployment_id_from_index(uint16_t index)
Get node ID from index in mapping table.
Definition: deployment.c:131
uint16_t deployment_id_from_iid(const uip_ipaddr_t *ipaddr)
Get node ID from the IID of an IPv6 address.
Definition: deployment.c:115