Wirepas SDK
node_configuration.h
Go to the documentation of this file.
1 /* Copyright 2017 Wirepas Ltd. All Rights Reserved.
2  *
3  * See file LICENSE.txt for full license details.
4  *
5  */
6 
7 #ifndef NODECONFIGURATION_H_
8 #define NODECONFIGURATION_H_
9 
10 #include "api.h"
11 #include "uniqueId.h"
12 
64  app_addr_t my_addr,
65  app_lib_settings_net_addr_t my_network_addr,
66  app_lib_settings_net_channel_t my_network_ch,
67  const uint8_t * my_authentication_key_p,
68  const uint8_t * my_encryption_key_p)
69 {
70  // Check node address
71  app_addr_t node_addr;
72  app_res_e res;
73 
74  if (lib_settings->getNodeAddress(&node_addr) != APP_RES_OK)
75  {
76  // Not set
77  res = lib_settings->setNodeAddress(my_addr);
78  if (res != APP_RES_OK)
79  {
80  return res;
81  }
82 
83  // Node address was not set => first time we call this function
84  if (my_authentication_key_p != NULL
85  && lib_settings->getAuthenticationKey(NULL) != APP_RES_OK)
86  {
87  // Not set
88  res = lib_settings->setAuthenticationKey(my_authentication_key_p);
89  if (res != APP_RES_OK)
90  {
91  return res;
92  }
93  }
94 
95  // Node address was not set so first time we call this function
96  if (my_encryption_key_p != NULL
97  && lib_settings->getEncryptionKey(NULL) != APP_RES_OK)
98  {
99  // Not set
100  res = lib_settings->setEncryptionKey(my_encryption_key_p);
101  if (res != APP_RES_OK)
102  {
103  return res;
104  }
105  }
106  }
107 
108  // Check network address
109  app_lib_settings_net_addr_t network_addr;
110  if (lib_settings->getNetworkAddress(&network_addr) != APP_RES_OK)
111  {
112  // Not set
113  res = lib_settings->setNetworkAddress(my_network_addr);
114  if (res != APP_RES_OK)
115  {
116  return res;
117  }
118  }
119 
120  // Check network channel
122  if (lib_settings->getNetworkChannel(&network_ch) != APP_RES_OK)
123  {
124  // Not set
125  res = lib_settings->setNetworkChannel(my_network_ch);
126  if (res != APP_RES_OK)
127  {
128  return res;
129  }
130  }
131 
132  return APP_RES_OK;
133 }
134 
140 {
141  app_addr_t address = getUniqueId();
142 
143  // Check the address is in correct range
144  if (address == 0x0)
145  {
146  // Zero is not a valid address
147  address = 1;
148  }
149  else if ((address & 0xFF000000) == 0x80000000)
150  {
151  // Address would be in the multicast space
152  address &= 0x7ffffffd;
153  }
154 
155  // Workaround:
156  // there is a bug in Wirepas reference gateway fixed in version 1.3.
157  // It doesn't forward messages from nodes with an address with the
158  // MSB set to one.
159  // In order to avoid this situation, explicitly reset the highest bit when
160  // generating a unique address.
161  // If the target network only has gateways with version >= 1.3, or with
162  // different gateway implementation it is safe to remove this line
163  address &= 0x7fffffff;
164 
165  return address;
166 }
167 
183  app_lib_settings_role_t new_role,
185  new_network_addr,
187  new_network_ch)
188 {
189  app_res_e res;
190 
191  res = lib_settings->setNodeAddress(new_addr);
192  if (res != APP_RES_OK)
193  {
194  return res;
195  }
196  res = lib_settings->setNodeRole(new_role);
197  if (res != APP_RES_OK)
198  {
199  return res;
200  }
201  res = lib_settings->setNetworkAddress(new_network_addr);
202  if (res != APP_RES_OK)
203  {
204  return res;
205  }
206  res = lib_settings->setNetworkChannel(new_network_ch);
207  if (res != APP_RES_OK)
208  {
209  return res;
210  }
211 
212  return APP_RES_OK;
213 }
214 
215 /*
216  * Network keys define in mcu/common/start.c and
217  * used only if default_network_cipher_key and default_network_authen_key
218  * are defined in one of the config.mk (set to NULL otherwise)
219  */
220 extern const uint8_t * authen_key_p;
221 extern const uint8_t * cipher_key_p;
222 
234 {
235 #if defined(NETWORK_ADDRESS) & defined(NETWORK_CHANNEL)
237  NETWORK_ADDRESS,
238  NETWORK_CHANNEL,
239  authen_key_p,
240  cipher_key_p);
241 #else
243 #endif
244 }
245 
246 #endif /* NODECONFIGURATION_H_ */
app_lib_settings_net_channel_t
uint8_t app_lib_settings_net_channel_t
Network channel type definition.
Definition: wms_settings.h:60
configureNode
__STATIC_INLINE app_res_e configureNode(app_addr_t my_addr, app_lib_settings_net_addr_t my_network_addr, app_lib_settings_net_channel_t my_network_ch, const uint8_t *my_authentication_key_p, const uint8_t *my_encryption_key_p)
Helper function to initially setup a node if not already configured. This configuration can be modifi...
Definition: node_configuration.h:63
app_res_e
app_res_e
Definition: wms_app.h:201
APP_RES_NOT_IMPLEMENTED
@ APP_RES_NOT_IMPLEMENTED
Definition: wms_app.h:208
app_lib_settings_net_addr_t
uint32_t app_lib_settings_net_addr_t
Network address type definition.
Definition: wms_settings.h:53
__STATIC_INLINE
#define __STATIC_INLINE
Definition: wms_app.h:20
APP_RES_OK
@ APP_RES_OK
Definition: wms_app.h:204
configureNodeFromBuildParameters
__STATIC_INLINE app_res_e configureNodeFromBuildParameters()
Wrapper on top of configureNode to get parameters from build system and hardcoded values from chip (f...
Definition: node_configuration.h:233
OverrideNodeConfig
__STATIC_INLINE app_res_e OverrideNodeConfig(app_addr_t new_addr, app_lib_settings_role_t new_role, app_lib_settings_net_addr_t new_network_addr, app_lib_settings_net_channel_t new_network_ch)
Helper function to apply new configuration on a node. Will override old configuration.
Definition: node_configuration.h:182
getUniqueAddress
__STATIC_INLINE app_addr_t getUniqueAddress()
Helper function to generate a unique unicast address.
Definition: node_configuration.h:139
app_addr_t
uint32_t app_addr_t
Definition: wms_app.h:228
authen_key_p
const uint8_t * authen_key_p
app_lib_settings_role_t
uint8_t app_lib_settings_role_t
Node role type.
Definition: wms_settings.h:88
cipher_key_p
const uint8_t * cipher_key_p
api.h