45 #include "nrf_mesh_sdk.h" 46 #include "nrf_delay.h" 50 #include "nrf_mesh_events.h" 51 #include "nrf_mesh_prov.h" 52 #include "nrf_mesh_assert.h" 56 #include "access_config.h" 57 #include "device_state_manager.h" 59 #include "config_client.h" 60 #include "health_client.h" 61 #include "simple_on_off_client.h" 63 #include "simple_hal.h" 64 #include "provisioner.h" 66 #include "light_switch_example_common.h" 67 #include "rtt_input.h" 73 #define CLIENT_COUNT (SERVER_COUNT + 1) 74 #define GROUP_CLIENT_INDEX (SERVER_COUNT) 75 #define BUTTON_NUMBER_GROUP (3) 76 #define RTT_INPUT_POLL_PERIOD_MS (100) 91 static simple_on_off_client_t m_clients[CLIENT_COUNT];
92 static health_client_t m_health_client;
94 static uint16_t m_provisioned_devices;
95 static uint16_t m_configured_devices;
98 static void client_status_cb(
const simple_on_off_client_t * p_self,
simple_on_off_status_t status, uint16_t src);
99 static void health_event_cb(
const health_client_t * p_client,
const health_client_evt_t * p_event);
112 static uint16_t provisioned_device_handles_load(
void)
114 uint16_t provisioned_devices = 0;
127 for (uint32_t i = 0; i < count; ++i)
141 provisioned_devices++;
149 return provisioned_devices;
159 static uint16_t configured_devices_count_get(
void)
161 uint16_t configured_devices = 0;
162 for (uint32_t i = 0; i < SERVER_COUNT; ++i)
166 &address_handle) == NRF_SUCCESS)
169 configured_devices++;
178 return configured_devices;
181 static void access_setup(
void)
183 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"Setting up access layer and models\n");
190 for (uint32_t i = 0; i < SERVER_COUNT; ++i)
201 for (uint32_t i = 0; i < CLIENT_COUNT; ++i)
203 m_clients[i].status_cb = client_status_cb;
209 m_provisioned_devices = provisioned_device_handles_load();
218 ERROR_CHECK(
dsm_appkey_add(0, m_netkey_handle, m_appkey, &m_appkey_handle));
223 m_configured_devices = configured_devices_count_get();
232 for (uint32_t i = 0; i < SERVER_COUNT; ++i)
245 if (m_configured_devices < m_provisioned_devices)
247 provisioner_configure(UNPROV_START_ADDRESS + m_configured_devices);
249 else if (m_provisioned_devices < SERVER_COUNT)
251 provisioner_wait_for_unprov(UNPROV_START_ADDRESS + m_provisioned_devices);
255 static uint32_t server_index_get(
const simple_on_off_client_t * p_client)
257 uint32_t index = (((uint32_t) p_client - ((uint32_t) &m_clients[0]))) /
sizeof(m_clients[0]);
262 static void client_status_cb(
const simple_on_off_client_t * p_self,
simple_on_off_status_t status, uint16_t src)
264 uint32_t server_index = server_index_get(p_self);
268 hal_led_pin_set(BSP_LED_0 + server_index,
true);
272 hal_led_pin_set(BSP_LED_0 + server_index,
false);
276 hal_led_blink_ms(LEDS_MASK, 100, 6);
285 bool all_servers_on =
true;
286 for (uint32_t i = BSP_LED_0; i < BSP_LED_0 + m_configured_devices; ++i)
288 if (!hal_led_pin_get(i))
290 all_servers_on =
false;
295 hal_led_pin_set(BSP_LED_3, all_servers_on);
298 static void health_event_cb(
const health_client_t * p_client,
const health_client_evt_t * p_event)
300 switch (p_event->
type)
303 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"Node 0x%04x alive with %u active fault(s), RSSI: %d\n",
312 static void button_event_handler(uint32_t button_number)
314 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"Button %u pressed\n", button_number);
315 if (m_configured_devices == 0)
317 __LOG(LOG_SRC_APP, LOG_LEVEL_WARN,
"No devices provisioned\n");
320 else if (m_configured_devices <= button_number && button_number != BUTTON_NUMBER_GROUP)
322 __LOG(LOG_SRC_APP, LOG_LEVEL_WARN,
"Device %u not provisioned yet.\n", button_number);
326 uint32_t status = NRF_SUCCESS;
327 switch (button_number)
334 !hal_led_pin_get(BSP_LED_0 + button_number));
339 !hal_led_pin_get(BSP_LED_0 + button_number), 3);
346 if (status == NRF_ERROR_INVALID_STATE ||
347 status == NRF_ERROR_NO_MEM ||
348 status == NRF_ERROR_BUSY)
350 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"Cannot send. Device is busy.\n");
351 hal_led_blink_ms(LEDS_MASK, 50, 4);
363 void provisioner_config_successful_cb(
void)
365 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"Configuration of device %u successful\n", m_configured_devices);
369 m_server_handles[m_configured_devices]));
372 hal_led_pin_set(BSP_LED_0 + m_configured_devices,
false);
373 m_configured_devices++;
375 if (m_configured_devices < SERVER_COUNT)
377 provisioner_wait_for_unprov(UNPROV_START_ADDRESS + m_provisioned_devices);
378 hal_led_pin_set(BSP_LED_0 + m_configured_devices,
true);
382 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"All servers provisioned\n");
383 hal_led_blink_ms(LEDS_MASK, 100, 4);
387 void provisioner_config_failed_cb(
void)
389 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"Configuration of device %u failed\n", m_configured_devices);
394 provisioner_wait_for_unprov(UNPROV_START_ADDRESS + m_provisioned_devices);
402 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"Provisioning complete. Adding address 0x%04x.\n", p_prov_data->
address);
411 m_server_handles[m_provisioned_devices]));
413 m_provisioned_devices++;
416 provisioner_configure(UNPROV_START_ADDRESS + m_configured_devices);
419 static void rtt_input_handler(
int key)
421 if (key >=
'0' && key <=
'3')
423 uint32_t button_number = key -
'0';
424 button_event_handler(button_number);
431 __LOG(LOG_SRC_APP, LOG_LEVEL_INFO,
"----- BLE Mesh Light Switch Client Demo -----\n");
434 ERROR_CHECK(hal_buttons_init(button_event_handler));
437 hal_led_pin_set(BSP_LED_0,
true);
440 rtt_input_enable(rtt_input_handler, RTT_INPUT_POLL_PERIOD_MS);
444 (void)sd_app_evt_wait();
uint32_t access_model_publish_address_get(access_model_handle_t handle, dsm_handle_t *p_address_handle)
Gets the current publish address for the given model.
uint32_t access_model_publish_application_set(access_model_handle_t handle, dsm_handle_t appkey_handle)
Sets the application key to be used when publishing for the given model.
A Health Current Status message was received.
#define NRF_MESH_KEY_SIZE
Size (in octets) of an encryption key.
uint32_t dsm_devkey_handle_get(uint16_t unicast_address, dsm_handle_t *p_devkey_handle)
Obtains the handle for a device key.
uint32_t simple_on_off_client_set_unreliable(simple_on_off_client_t *p_client, bool on_off, uint8_t repeats)
Sets the state of the Simple OnOff Server unreliably (without acknowledgment).
uint16_t value
Address value.
uint32_t dsm_address_publish_add(uint16_t raw_address, dsm_handle_t *p_address_handle)
Adds an address to the DSM to be used as a publish address.
#define NRF_MESH_ASSERT(cond)
Run-time assertion.
uint32_t config_client_init(config_client_event_cb_t event_cb)
Initializes the configuration client.
Provisioning complete event.
The server did not reply to a Simple OnOff Set/Get.
uint32_t simple_on_off_client_init(simple_on_off_client_t *p_client, uint16_t element_index)
Initializes the Simple OnOff client.
uint32_t dsm_address_get(dsm_handle_t address_handle, nrf_mesh_address_t *p_address)
Retrieves the address for a given address handle and fills out the given nrf_mesh_address_t structure...
#define DSM_NONVIRTUAL_ADDR_MAX
Maximum number of non-virtual addresses.
uint32_t dsm_address_publish_remove(dsm_handle_t address_handle)
Removes an address that has been used as a publish address.
uint32_t config_client_server_set(dsm_handle_t server_devkey_handle, dsm_handle_t server_address_handle)
Sets the configuration server to configure.
void access_init(void)
Initializes the access layer.
uint8_t fault_array_length
Length of the fault array.
Structure representing the unicast addresses assigned to this device.
bool access_flash_config_load(void)
Recover access layer configuration from flash.
uint32_t dsm_address_handle_get(const nrf_mesh_address_t *p_address, dsm_handle_t *p_address_handle)
Retrieves the address handle for a given nrf_mesh_address_t structure.
#define DSM_ADDR_MAX
Maximum number of addresses in total.
uint16_t dsm_handle_t
DSM handle type, used for the handles returned for the each set of data added.
void access_flash_config_store(void)
Store the current state of access layer - information related to element and model configuration - in...
uint32_t health_client_init(health_client_t *p_client, uint16_t element_index, health_client_evt_cb_t evt_handler)
Initializes a health client instance.
uint32_t dsm_address_get_all(dsm_handle_t *p_address_handle_list, uint32_t *p_count)
Get a list of all address handles in the address pool.
uint32_t access_model_application_bind(access_model_handle_t handle, dsm_handle_t appkey_handle)
Binds an application key to a model.
uint32_t dsm_appkey_get_all(dsm_handle_t subnet_handle, mesh_key_index_t *p_key_list, uint32_t *p_count)
Retrieves all the application key indices of the stored application keys of a specific subnetwork...
health_client_evt_fault_status_t fault_status
Fault status data for the Current Status and Fault Status messages.
uint16_t address
Unicast address for the device.
uint32_t access_model_publish_address_set(access_model_handle_t handle, dsm_handle_t address_handle)
Changes the publish address for the given model.
uint32_t dsm_devkey_delete(dsm_handle_t dev_handle)
Removes an existing device key from the device state storage.
uint32_t dsm_local_unicast_addresses_set(const dsm_local_unicast_address_t *p_address)
Set the unicast addresses of the device.
Received status ON from the server.
health_client_evt_type_t type
Type of the event.
nrf_mesh_address_type_t type
Address type.
#define LOG_CALLBACK_DEFAULT
The default callback function to use.
const uint8_t * p_devkey
Device key of the provisioned device.
uint32_t config_client_server_bind(dsm_handle_t server_devkey_handle)
Binds the configuration client to a server.
const access_message_rx_meta_t * p_meta_data
Meta data for the received message.
uint32_t dsm_appkey_add(mesh_key_index_t app_key_id, dsm_handle_t subnet_handle, const uint8_t *p_key, dsm_handle_t *p_app_handle)
Adds an application key and its associated application key index to the device state storage...
#define DSM_HANDLE_INVALID
Invalid handle index.
uint32_t dsm_subnet_add(mesh_key_index_t net_key_id, const uint8_t *p_key, dsm_handle_t *p_subnet_handle)
Adds a subnetwork and its associated network key index to the device state storage.
bool dsm_flash_config_load(void)
Load DSM state from flash, to recover state.
Received status OFF from the server.
uint32_t simple_on_off_client_set(simple_on_off_client_t *p_client, bool on_off)
Sets the state of the Simple OnOff server.
uint32_t dsm_devkey_add(uint16_t raw_unicast_addr, dsm_handle_t subnet_handle, const uint8_t *p_key, dsm_handle_t *p_devkey_handle)
Adds a device key.
simple_on_off_status_t
Simple OnOff status codes.
uint32_t dsm_subnet_get_all(mesh_key_index_t *p_key_list, uint32_t *p_count)
Retrieves all the network key indices of the stored subnetworks.
#define ACCESS_ELEMENT_COUNT
The number of elements in the application.
void dsm_init(void)
Initialize the device state manager.