nRF5 SDK for Mesh v1.0.1
main.c
1 /* Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form, except as embedded into a Nordic
11  * Semiconductor ASA integrated circuit in a product or a software update for
12  * such product, must reproduce the above copyright notice, this list of
13  * conditions and the following disclaimer in the documentation and/or other
14  * materials provided with the distribution.
15  *
16  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * 4. This software, with or without modification, must only be used with a
21  * Nordic Semiconductor ASA integrated circuit.
22  *
23  * 5. Any software provided in binary form under this license must not be reverse
24  * engineered, decompiled, modified and/or disassembled.
25  *
26  * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
27  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <stdint.h>
39 #include <string.h>
40 
41 /* HAL */
42 #include "nrf.h"
43 #include "boards.h"
44 #include "nrf_mesh_sdk.h"
45 #include "nrf_delay.h"
46 #include "simple_hal.h"
47 
48 /* Core */
49 #include "nrf_mesh.h"
50 #include "nrf_mesh_events.h"
51 #include "log.h"
52 
53 #include "access.h"
54 #include "access_config.h"
55 #include "device_state_manager.h"
56 #include "nrf_mesh_node_config.h"
57 
58 #include "simple_on_off_server.h"
59 
60 #include "light_switch_example_common.h"
61 
62 /*****************************************************************************
63  * Definitions
64  *****************************************************************************/
65 
66 #define LED_PIN_NUMBER (BSP_LED_0)
67 #define LED_PIN_MASK (1u << LED_PIN_NUMBER)
68 
69 /*****************************************************************************
70  * Static data
71  *****************************************************************************/
72 
73 static simple_on_off_server_t m_server;
74 
75 /* Forward declaration */
76 static bool get_cb(const simple_on_off_server_t * p_server);
77 static bool set_cb(const simple_on_off_server_t * p_server, bool value);
78 
79 /*****************************************************************************
80  * Static utility functions
81  *****************************************************************************/
82 
83 static void configuration_setup(void * p_unused)
84 {
85  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Initializing and adding models\n");
86  m_server.get_cb = get_cb;
87  m_server.set_cb = set_cb;
88  ERROR_CHECK(simple_on_off_server_init(&m_server, 0));
89  ERROR_CHECK(access_model_subscription_list_alloc(m_server.model_handle));
90  hal_led_mask_set(LEDS_MASK, true);
91 }
92 
93 static void provisioning_complete(void * p_unused)
94 {
95  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Successfully provisioned\n");
96  hal_led_mask_set(LEDS_MASK, false);
97  hal_led_blink_ms(LED_PIN_MASK, 200, 4);
98 }
99 
100 /*****************************************************************************
101  * Simple OnOff Callbacks
102  *****************************************************************************/
103 
104 static bool get_cb(const simple_on_off_server_t * p_server)
105 {
106  return hal_led_pin_get(LED_PIN_NUMBER);
107 }
108 
109 static bool set_cb(const simple_on_off_server_t * p_server, bool value)
110 {
111  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Got SET command to %u\n", value);
112  hal_led_pin_set(LED_PIN_NUMBER, value);
113  return value;
114 }
115 
116 int main(void)
117 {
118  __LOG_INIT(LOG_SRC_APP | LOG_SRC_ACCESS, LOG_LEVEL_INFO, LOG_CALLBACK_DEFAULT);
119  __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "----- BLE Mesh Light Switch Server Demo -----\n");
120 
121  hal_leds_init();
122 
123  static const uint8_t static_auth_data[NRF_MESH_KEY_SIZE] = STATIC_AUTH_DATA;
124  static nrf_mesh_node_config_params_t config_params =
126  config_params.p_static_data = static_auth_data;
127  config_params.complete_callback = provisioning_complete;
128  config_params.setup_callback = configuration_setup;
130 
131 #if defined(S110)
132  config_params.lf_clk_cfg = NRF_CLOCK_LFCLKSRC_XTAL_20_PPM;
133 #elif SD_BLE_API_VERSION >= 5
134  config_params.lf_clk_cfg.source = NRF_CLOCK_LF_SRC_XTAL;
135  config_params.lf_clk_cfg.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM;
136 #else
137  config_params.lf_clk_cfg.source = NRF_CLOCK_LF_SRC_XTAL;
138  config_params.lf_clk_cfg.xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM;
139 #endif
140 
141  ERROR_CHECK(nrf_mesh_node_config(&config_params));
142 
143  while (true)
144  {
145  (void)sd_app_evt_wait();
146  }
147 }
nrf_mesh_prov_oob_caps_t prov_caps
Device capabilities for OOB authentication.
uint32_t access_model_subscription_list_alloc(access_model_handle_t handle)
Allocates a subscription list for a model.
#define NRF_MESH_KEY_SIZE
Size (in octets) of an encryption key.
Node configuration parameters.
#define NRF_MESH_PROV_OOB_CAPS_DEFAULT(NUM_ELEMENTS)
Sets the default authentication capabilities.
Definition: nrf_mesh_prov.h:66
nrf_clock_lf_cfg_t lf_clk_cfg
Low frequency clock configuration.
uint8_t irq_priority
Application IRQ priority.
nrf_mesh_node_config_complete_cb_t complete_callback
Pointer to a function used to signal the completion of the node configuration procedure.
#define NRF_MESH_IRQ_PRIORITY_LOWEST
Lowest available IRQ priority on current architecture.
#define LOG_CALLBACK_DEFAULT
The default callback function to use.
uint32_t simple_on_off_server_init(simple_on_off_server_t *p_server, uint16_t element_index)
Initializes the Simple OnOff server.
nrf_mesh_node_config_setup_cb_t setup_callback
Pointer to a function used to allow initialization of application-specific models.
const uint8_t * p_static_data
Data used for OOB authentication when the static authentication method is used.
uint32_t nrf_mesh_node_config(const nrf_mesh_node_config_params_t *p_params)
Configures the mesh node.
#define ACCESS_ELEMENT_COUNT
The number of elements in the application.

Documentation feedback | Developer Zone | Subscribe | Updated