ble_ncfgs.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 ble_ncfgs Node Configuration Service
  43. * @{
  44. * @ingroup iot_sdk_common
  45. * @brief Node Configuration Service module.
  46. *
  47. * @details The Node Configuration Service allows configuration of the node during commissioning.
  48. * During initialization it adds the Node Configuration Service and the corresponding
  49. * characteristics to the BLE GATT database. It decodes and checks the received values
  50. * and then passes them to the parent module.
  51. */
  52. #ifndef BLE_NODE_CFG_H__
  53. #define BLE_NODE_CFG_H__
  54. #include <stdint.h>
  55. #include "ble.h"
  56. #include "app_util.h"
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. #define BLE_UUID_NODE_CFG_SERVICE 0x7799
  61. #define BLE_UUID_NCFGS_SSID_CHAR 0x77A9
  62. #define BLE_UUID_NCFGS_KEYS_STORE_CHAR 0x77B9
  63. #define BLE_UUID_NCFGS_CTRLPT_CHAR 0x77C9
  64. #define APP_GATTERR_NOT_CONFIGURED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 1 /**< ATT Error: Node configuration incomplete. */
  65. #define APP_GATTERR_UNKNOWN_OPCODE BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2 /**< ATT Error: Unknown opcode. */
  66. #define APP_GATTERR_INVALID_ATTR_VALUE BLE_GATT_STATUS_ATTERR_APP_BEGIN + 3 /**< ATT Error: Invalid attribute value. */
  67. #define NCFGS_SSID_MIN_LEN 6 /**< SSID minimum length. */
  68. #define NCFGS_SSID_MAX_LEN 16 /**< SSID maximum length. */
  69. #define NCFGS_KEYS_MAX_LEN 16 /**< Keys maximum length. */
  70. #define NCFGS_IDENTITY_DATA_MAX_LEN 8 /**< Identity data maximum length. */
  71. #define NCFGS_CTRLP_OPCODE_LEN 1 /**< Ctrlp: Opcode value length. */
  72. #define NCFGS_CTRLP_DELAY_LEN 4 /**< Ctrlp: Length of action delay value. */
  73. #define NCFGS_CTRLP_DURATION_LEN 4 /**< Ctrlp: Length of next mode duration value. */
  74. #define NCFGS_CTRLP_STATE_ON_FAILURE_LEN 1 /**< Ctrlp: Length of state-on-failure value. */
  75. #define NCFGS_CTRLP_ALL_BUT_ID_DATA_LEN (NCFGS_CTRLP_OPCODE_LEN + \
  76. NCFGS_CTRLP_DELAY_LEN + \
  77. NCFGS_CTRLP_DURATION_LEN + \
  78. NCFGS_CTRLP_STATE_ON_FAILURE_LEN) /**< Ctrlp: Total length of all values except identity data. */
  79. #define NCFGS_CTRLP_VALUE_LEN (NCFGS_CTRLP_OPCODE_LEN + \
  80. NCFGS_CTRLP_DELAY_LEN + \
  81. NCFGS_CTRLP_DURATION_LEN + \
  82. NCFGS_CTRLP_STATE_ON_FAILURE_LEN + \
  83. NCFGS_IDENTITY_DATA_MAX_LEN) /**< Ctrlp: Total length of all values. */
  84. #define HTONL(val) ((((uint32_t) (val) & 0xff000000) >> 24) | \
  85. (((uint32_t) (val) & 0x00ff0000) >> 8) | \
  86. (((uint32_t) (val) & 0x0000ff00) << 8) | \
  87. (((uint32_t) (val) & 0x000000ff) << 24))
  88. /**@brief Node Configuration Service control point opcode values. */
  89. typedef enum
  90. {
  91. NCFGS_OPCODE_GOTO_JOINING_MODE = 0x01,
  92. NCFGS_OPCODE_GOTO_CONFIG_MODE = 0x02,
  93. NCFGS_OPCODE_GOTO_IDENTITY_MODE = 0x03
  94. } ble_ncfgs_opcode_t;
  95. /**@brief Node Configuration Service configuration states. */
  96. typedef enum
  97. {
  98. NCFGS_STATE_IDLE = 0x00,
  99. NCFGS_STATE_SSID_WRITTEN = 0x01,
  100. NCFGS_STATE_KEYS_STORE_WRITTEN = 0x02
  101. } ble_ncfgs_state_t;
  102. /**@brief Node Configuration Service state-on-failure values. */
  103. typedef enum
  104. {
  105. NCFGS_SOF_NO_CHANGE = 0x00,
  106. NCFGS_SOF_PWR_OFF = 0x01,
  107. NCFGS_SOF_CONFIG_MODE = 0x02
  108. } state_on_failure_t;
  109. /**@brief Structure for storing keys received from the peer. */
  110. typedef struct __attribute__ ((__packed__))
  111. {
  112. uint8_t keys_len;
  113. uint8_t keys[NCFGS_KEYS_MAX_LEN]; // Keys received from the router.
  114. } keys_store_t;
  115. /**@brief Structure for storing the SSID received from the peer. */
  116. typedef struct __attribute__ ((__packed__))
  117. {
  118. uint8_t ssid_len;
  119. uint8_t ssid[NCFGS_SSID_MAX_LEN]; // SSID received from the router.
  120. } ssid_store_t;
  121. /**@brief Structure for storing the identity data from the peer. */
  122. typedef struct __attribute__ ((__packed__))
  123. {
  124. uint8_t identity_data_len;
  125. uint8_t identity_data[NCFGS_IDENTITY_DATA_MAX_LEN]; // Custom node identifier data.
  126. } id_data_store_t;
  127. /**@brief Structure for control point value. */
  128. typedef struct __attribute__ ((__packed__))
  129. {
  130. ble_ncfgs_opcode_t opcode; // Mode to start.
  131. uint32_t delay_sec; // Delay before entering >Opcode< mode.
  132. uint32_t duration_sec; // General timeout for >Opcode< mode.
  133. state_on_failure_t state_on_failure; // Mode to enter if >Opcode< mode fails (times out).
  134. } ble_ncfgs_ctrlp_value_t;
  135. /**@brief Structure for storing Node Configuration Service characteristic values. */
  136. typedef struct __attribute__ ((__packed__))
  137. {
  138. ble_ncfgs_ctrlp_value_t ctrlp_value;
  139. ssid_store_t ssid_from_router; // SSID received from the peer.
  140. keys_store_t keys_from_router; // Keys received from the peer.
  141. id_data_store_t id_data; // Identity data received from the peer.
  142. } ble_ncfgs_data_t;
  143. /**@brief Function for handling BLE events.
  144. *
  145. * @details This function must be called from the BLE stack event dispatcher
  146. * to handle BLE events that are relevant for the Node Configuration Service module.
  147. * It propagates an event to the parent layer if the Control Point characteristic
  148. * was successfully written.
  149. *
  150. * @param[in] p_ble_evt BLE stack event.
  151. */
  152. void ble_ncfgs_ble_evt_handler(const ble_evt_t * p_ble_evt);
  153. /**@brief Node Configuration Service event handler type. */
  154. typedef void (*ble_ncfgs_evt_handler_t) (ble_ncfgs_data_t * ncfgs_data);
  155. /**@brief Function for initializing the Node Configuration Service module.
  156. *
  157. * @details Interface for the Commissioning module to create a GATT database to
  158. * allow for node configuration.
  159. *
  160. * @param[in] ble_ncfgs_cb Function to be called in case of an error.
  161. *
  162. * @retval NRF_SUCCESS If initialization was successful. Otherwise, a propagated
  163. * error code is returned.
  164. *
  165. */
  166. uint32_t ble_ncfgs_init(ble_ncfgs_evt_handler_t ble_ncfgs_cb);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif // BLE_NODE_CFG_H__
  171. /** @} */