commissioning.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup commissioning_module Commissioning Module
  43. * @{
  44. * @ingroup iot_sdk_common
  45. * @brief Commissioning module.
  46. *
  47. * @details Enables commissioning of the node by managing transitions between the Config, Joining, and
  48. * Identity modes. In Config mode the node can be configured with the settings required to
  49. * join the network in Joining mode. The Identity mode can be requested to make the node
  50. * easily recognizable for the user.
  51. * The settings managed by the module are stored in persistent storage.
  52. */
  53. #ifndef COMMISSIONING_H__
  54. #define COMMISSIONING_H__
  55. #include <stdint.h>
  56. #include <stdbool.h>
  57. #include "ble_ncfgs.h"
  58. #include "iot_timer.h"
  59. #include "ipv6_medium_ble.h"
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. #define COMMISSIONING_TICK_INTERVAL_SEC 1 /**< Interval between periodic callbacks to the Commissioning module. */
  64. #define COMMISSIONING_EVT_CONFIG_MODE_ENTER 0x01 /**< Indicates that the medium entered mode for commissioning configuration. */
  65. #define COMMISSIONING_EVT_JOINING_MODE_ENTER 0x02 /**< Indicates that the medium exited mode for commissioning configuration. */
  66. #define COMMISSIONING_EVT_IDENTITY_MODE_ENTER 0x03 /**< Indicates that identity mode was requested. */
  67. #define COMMISSIONING_EVT_IDENTITY_MODE_EXIT 0x04 /**< Indicates that the node should stop using any features associated with the Identity mode. */
  68. #define NODE_MODE_NONE 0x00 /**< Node mode: before initialization. */
  69. #define NODE_MODE_JOINING 0x01 /**< Node mode: joining the network. */
  70. #define NODE_MODE_CONFIG 0x02 /**< Node mode: configuration. */
  71. #define NODE_MODE_IDENTITY 0x03 /**< Node mode: conspicuous for the user. */
  72. /**@brief Joining mode timer control commands. */
  73. typedef enum
  74. {
  75. JOINING_MODE_TIMER_START = 0x01,
  76. JOINING_MODE_TIMER_STOP_RESET = 0x02
  77. } joining_mode_timer_ctrl_cmd_t;
  78. /**@brief Structure for storing all settings necessary for commissioning. */
  79. typedef struct __attribute__ ((__packed__)) __attribute__((aligned))
  80. {
  81. uint8_t poweron_mode; // Checked at startup to enter correct mode.
  82. state_on_failure_t joining_mode_failure; // Mode to enter if Joining mode fails.
  83. uint32_t joining_mode_to; // General timeout in Joining mode.
  84. state_on_failure_t config_mode_failure; // Mode to enter if Config mode fails.
  85. uint32_t config_mode_to; // General timeout in Config mode.
  86. uint32_t id_mode_to; // Duration of Identity Mode.
  87. ssid_store_t ssid_store; // SSID received from the router.
  88. keys_store_t keys_store; // Keys received from the router.
  89. id_data_store_t id_data_store; // Custom node identifier data.
  90. } commissioning_settings_t;
  91. /**@brief Commissioning module event handler type. */
  92. typedef void (*commissioning_timeout_handler_t)(void);
  93. /**@brief Structure for creating timers in the Commissioning module. */
  94. typedef struct
  95. {
  96. bool is_timer_running;
  97. uint32_t current_value_sec;
  98. commissioning_timeout_handler_t timeout_handler;
  99. } commissioning_timer_t;
  100. /**@brief Structure of events passed by the Commissioning module to the parent layer. */
  101. typedef struct
  102. {
  103. uint8_t commissioning_evt_id;
  104. bool power_off_enable_requested;
  105. commissioning_settings_t * p_commissioning_settings;
  106. } commissioning_evt_t;
  107. /**@brief Function for handling BLE events.
  108. *
  109. * @details This function must be called from the BLE stack event dispatcher
  110. * to handle BLE events that are relevant for the Commissioning module.
  111. *
  112. * @param[in] p_ble_evt BLE stack event.
  113. */
  114. void commissioning_ble_evt_handler(const ble_evt_t * p_ble_evt);
  115. /**@brief Commissioning module event handler type. */
  116. typedef void (*commissioning_evt_handler_t)(commissioning_evt_t * p_commissioning_evt);
  117. /**@brief Structure for initialization parameters of the Commissioning module. */
  118. typedef struct
  119. {
  120. commissioning_evt_handler_t commissioning_evt_handler;
  121. } commissioning_init_params_t;
  122. /**@brief Function for initializing the Commissioning module.
  123. *
  124. * @details Initializes the Node Configuration Service module to create the GATT database.
  125. * Loads previously stored node settings from the persistent storage and if
  126. * the settings are valid, sets up the node to start in the right mode.
  127. *
  128. * @param[in] p_init_param Pointer to the initialization parameters.
  129. * @param[out] p_poweron_state Pointer to the value of the mode that should be started.
  130. *
  131. * @retval NRF_SUCCESS If initialization was successful. Otherwise, a propagated
  132. * error code is returned.
  133. *
  134. */
  135. uint32_t commissioning_init(commissioning_init_params_t * p_init_param,
  136. uint8_t * p_poweron_state);
  137. /**@brief Function for advancing the node to a new mode.
  138. *
  139. * @details Stops and starts app timers appropriate for the mode requested.
  140. * Propagates the mode change event to the parent layer.
  141. *
  142. * @param[in] new_mode New mode to start.
  143. *
  144. */
  145. void commissioning_node_mode_change(uint8_t new_mode);
  146. /**@brief Function for getting the address of GAP parameters for the active mode.
  147. *
  148. * @param[out] pp_node_gap_params Address of GAP parameters for the active mode.
  149. *
  150. */
  151. void commissioning_gap_params_get(ipv6_medium_ble_gap_params_t ** pp_node_gap_params);
  152. /**@brief Function for getting the address of advertising parameters for the active mode.
  153. *
  154. * @param[out] pp_node_adv_params Address of advertising parameters for the active mode.
  155. *
  156. */
  157. void commissioning_adv_params_get(ipv6_medium_ble_adv_params_t ** pp_node_adv_params);
  158. /**@brief Function for clearing all node settings from the persistent storage.
  159. *
  160. * @details Calls the appropriate persistent storage interface function to clear
  161. * all commissioning-related settings from the persistent storage.
  162. *
  163. */
  164. void commissioning_settings_clear(void);
  165. /**@brief Function for controlling the joining mode timer from the parent layer(s).
  166. *
  167. * @details If the Joining mode timer reaches zero, the node must enter the
  168. * state-on-failure, as set by the user. This function allows the
  169. * application designer to control the Joining mode timer from the
  170. * application layer.
  171. */
  172. void commissioning_joining_mode_timer_ctrl(
  173. joining_mode_timer_ctrl_cmd_t joining_mode_timer_ctrl_cmd);
  174. /**@brief Commissioning time tick used for measuring delays and time between events.
  175. *
  176. * @param[in] wall_clock_value Wall clock value from the IoT Timer module.
  177. */
  178. void commissioning_time_tick(iot_timer_time_in_ms_t wall_clock_value);
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. #endif // COMMISSIONING_H__
  183. /** @} */