nRF5 SDK for Mesh v4.2.0
model_common.h
1 /* Copyright (c) 2010 - 2020, 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 #ifndef MODEL_COMMON_H__
39 #define MODEL_COMMON_H__
40 
41 #include <stdint.h>
42 
43 #include "nrf_mesh_config_core.h"
44 #include "utils.h"
45 #include "app_timer.h"
46 #include "timer_scheduler.h"
47 #include "access.h"
48 
59 #ifndef LIGHT_LIGHTNESS_SETUP_SERVER_INSTANCES_MAX
60 #define LIGHT_LIGHTNESS_SETUP_SERVER_INSTANCES_MAX (0)
61 #endif
62 
66 #ifndef LIGHT_LC_SETUP_SERVER_INSTANCES_MAX
67 #define LIGHT_LC_SETUP_SERVER_INSTANCES_MAX (0)
68 #endif
69 
73 #ifndef LIGHT_CTL_SETUP_SERVER_INSTANCES_MAX
74 #define LIGHT_CTL_SETUP_SERVER_INSTANCES_MAX (0)
75 #endif
76 
80 #ifndef SCENE_SETUP_SERVER_INSTANCES_MAX
81 #define SCENE_SETUP_SERVER_INSTANCES_MAX (0)
82 #endif
83 
85 #define MESH_APP_MODEL_COMMON_ID (0x0001)
86 
87 #define MESH_APP_MODEL_LIGHT_LIGHTNESS_ID_START (0x1000)
88 
89 #define MESH_APP_MODEL_LIGHT_LIGHTNESS_ID_END (0x10FF)
90 
91 #define MESH_APP_MODEL_LIGHT_LC_SERVER_ID_START (0x1100)
92 
93 #define MESH_APP_MODEL_LIGHT_LC_SERVER_ID_END (0x12FF)
94 
95 #define MESH_APP_MODEL_LIGHT_CTL_SERVER_ID_START (0x1300)
96 
97 #define MESH_APP_MODEL_LIGHT_CTL_SERVER_ID_END (0x13FF)
98 
99 #define MESH_APP_MODEL_SCENE_SERVER_ID_START (0x1400)
100 
101 #define MESH_APP_MODEL_SCENE_SERVER_ID_END (0x14FF)
102 
104 #define MODEL_TRANSITION_TIME_UNKNOWN (UINT32_MAX)
105 
106 
108 #ifndef MODEL_ACKNOWLEDGED_TRANSACTION_TIMEOUT
109 #define MODEL_ACKNOWLEDGED_TRANSACTION_TIMEOUT (SEC_TO_US(30))
110 #endif
111 
113 #ifndef TRANSITION_STEP_MIN_MS
114 #define TRANSITION_STEP_MIN_MS (45)
115 #endif
116 
117 #define TRANSITION_TIME_STEP_100MS_MAX (6200ul)
118 
119 #define TRANSITION_TIME_STEP_1S_MAX (SEC_TO_MS(62ul))
120 
121 #define TRANSITION_TIME_STEP_10S_MAX (SEC_TO_MS(620ul))
122 
123 #define TRANSITION_TIME_STEP_10M_MAX (MIN_TO_MS(620ul))
124 
125 #define TRANSITION_TIME_MAX (0x3E)
126 
127 #define TRANSITION_TIME_UNKNOWN (0x3F)
128 
129 #define TRANSITION_TIME_MAX_MS (TRANSITION_TIME_STEP_10M_MAX)
130 
132 #define DELAY_TIME_STEP_FACTOR_MS (5)
133 
134 #define DELAY_TIME_STEP_MAX (0xFF)
135 
136 #define DELAY_TIME_MAX_MS (DELAY_TIME_STEP_MAX * DELAY_TIME_STEP_FACTOR_MS)
137 
139 #define MODEL_TIMER_TIMEOUT_MIN_TICKS (APP_TIMER_TICKS(1))
140 
141 #define MODEL_TIMER_TIMEOUT_MIN_US (MODEL_TIMER_PERIOD_US_GET(MODEL_TIMER_TIMEOUT_MIN_TICKS))
142 
143 #define MODEL_TIMER_MAX_TIMEOUT_TICKS (APP_TIMER_MAX_CNT_VAL)
144 
145 #define MODEL_TIMER_PERIOD_MS_GET(TICKS) ((1000ul * (TICKS)) / APP_TIMER_CLOCK_FREQ)
146 
147 #define MODEL_TIMER_PERIOD_US_GET(TICKS) ((1000ul * 1000ul * (TICKS)) / APP_TIMER_CLOCK_FREQ)
148 
149 #define MODEL_TIMER_TICKS_GET_MS(MS_TIME) \
150  ((uint64_t)ROUNDED_DIV((MS_TIME) * (uint64_t)APP_TIMER_CLOCK_FREQ, \
151  1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
152 
153 #define MODEL_TIMER_TICKS_GET_US(US_TIME) \
154  ((uint64_t)ROUNDED_DIV( \
155  (US_TIME) * (uint64_t)APP_TIMER_CLOCK_FREQ, \
156  1000 * 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
157 
159 typedef struct
160 {
162  uint32_t delay_ms;
164 
166 typedef struct
167 {
169  uint16_t src;
171  uint16_t dst;
173  uint32_t message_id;
175  uint8_t old_tid;
179  timer_event_t tid_expiry_timer;
180 } tid_tracker_t;
181 
183 typedef enum
184 {
190 
191 typedef void(*model_timer_cb_t)(void * p_context);
192 
194 typedef struct
195 {
201  void * p_context;
203  model_timer_cb_t cb;
205  uint64_t total_rtc_ticks;
206 
208  app_timer_id_t const * p_timer_id;
209 
211  uint64_t remaining_ticks;
213  uint32_t last_rtc_stamp;
215  bool cb_active;
216 } model_timer_t;
217 
218 
227 uint32_t model_transition_time_decode(uint8_t enc_transition_time);
228 
237 uint8_t model_transition_time_encode(uint32_t transition_time);
238 
248 bool model_transition_time_is_valid(uint8_t enc_transition_time);
249 
257 uint32_t model_delay_decode(uint8_t enc_delay);
258 
267 uint8_t model_delay_encode(uint32_t delay);
268 
287 bool model_tid_validate(tid_tracker_t * p_tid_tracker, const access_message_rx_meta_t * p_meta,
288  uint32_t message_id, uint8_t tid);
289 
304 bool model_transaction_is_new(tid_tracker_t * p_tid_tracker);
305 
319 uint32_t model_timer_schedule(model_timer_t * p_timer);
320 
326 void model_timer_abort(model_timer_t * p_timer);
327 
336 uint64_t model_timer_elapsed_ticks_get(model_timer_t * p_timer);
337 
352 uint32_t model_timer_create(model_timer_t * p_timer);
353 
360 void model_common_init(void);
361 
373 uint32_t model_common_config_apply(void);
374 
377 #endif /* MODEL_COMMON_H__ */
378 
uint32_t transition_time_ms
Transition time value in milliseconds.
Definition: model_common.h:161
uint32_t model_transition_time_decode(uint8_t enc_transition_time)
Gets the decoded value of the transition time in milliseconds.
Structure for tracking TID expiry for the models.
Definition: model_common.h:166
uint32_t delay_ms
Message execution delay in milliseconds.
Definition: model_common.h:162
uint8_t model_delay_encode(uint32_t delay)
Gets the encoded value of the delay time as specified in the Mesh Model Specification (MshMDLv1...
bool model_transaction_is_new(tid_tracker_t *p_tid_tracker)
Checks if given TID tracker instance has recorded a new transaction.
The timer will restart each time it expires.
Definition: model_common.h:188
Structure for model timers.
Definition: model_common.h:194
void model_common_init(void)
Initialize persistent memory of all models used.
The timer will expire only once.
Definition: model_common.h:186
bool cb_active
Internal variable.
Definition: model_common.h:215
uint32_t message_id
Previously received Opcode.
Definition: model_common.h:173
uint8_t model_transition_time_encode(uint32_t transition_time)
Gets the encoded value of the transition time as specified in the Mesh Model Specification (MshMDLv1...
uint32_t model_delay_decode(uint8_t enc_delay)
Gets the decoded value of the delay time in milliseconds.
uint8_t old_tid
Previously received TID.
Definition: model_common.h:175
timer_event_t tid_expiry_timer
Expiration timer instance.
Definition: model_common.h:179
uint32_t model_common_config_apply(void)
Apply data loaded from the mesh configuration system into persistent memory structures.
app_timer_id_t const * p_timer_id
APP timer instance pointer.
Definition: model_common.h:208
uint16_t src
Source address.
Definition: model_common.h:169
void * p_context
context pointer for the timer callback
Definition: model_common.h:201
uint32_t model_timer_create(model_timer_t *p_timer)
Creates a model timer.
Metadata for received messages.
Definition: access.h:196
bool new_transaction
New transaction indicator flag.
Definition: model_common.h:177
uint64_t model_timer_elapsed_ticks_get(model_timer_t *p_timer)
Returns the total elapsed rtc ticks since the last call to model_timer_schedule() API...
uint32_t last_rtc_stamp
Internal variable.
Definition: model_common.h:213
bool model_tid_validate(tid_tracker_t *p_tid_tracker, const access_message_rx_meta_t *p_meta, uint32_t message_id, uint8_t tid)
Checks if the given message parameters represents a new transaction.
model_timer_mode_t mode
Timer mode : Single shot, repeated.
Definition: model_common.h:197
bool model_transition_time_is_valid(uint8_t enc_transition_time)
Validates the given transition time value.
uint64_t timeout_rtc_ticks
Timeout in number of RTC ticks.
Definition: model_common.h:199
uint64_t total_rtc_ticks
Total rtc ticks since beginning of the timer.
Definition: model_common.h:205
Generic Transition parameters for the model messages.
Definition: model_common.h:159
uint32_t model_timer_schedule(model_timer_t *p_timer)
Schedules a model timer for a given interval.
uint64_t remaining_ticks
Internal variable.
Definition: model_common.h:211
model_timer_mode_t
Timer modes.
Definition: model_common.h:183
void model_timer_abort(model_timer_t *p_timer)
Aborts the currently scheduled timer.
uint16_t dst
Destination address.
Definition: model_common.h:171

Documentation feedback | Developer Zone | Subscribe | Updated