1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef LWM2M_OBJ_SERVER_H_
8 #define LWM2M_OBJ_SERVER_H_
9 
10 #include <stdint.h>
11 #include <stdbool.h>
12 #include <zephyr/sys_clock.h>
13 
14 /* Server resource IDs */
15 #define SERVER_SHORT_SERVER_ID				0
16 #define SERVER_LIFETIME_ID				1
17 #define SERVER_DEFAULT_MIN_PERIOD_ID			2
18 #define SERVER_DEFAULT_MAX_PERIOD_ID			3
19 #define SERVER_DISABLE_ID				4
20 #define SERVER_DISABLE_TIMEOUT_ID			5
21 #define SERVER_STORE_NOTIFY_ID				6
22 #define SERVER_TRANSPORT_BINDING_ID			7
23 #define SERVER_REG_UPDATE_TRIGGER_ID			8
24 /* Server object version 1.1 resource IDs */
25 #define SERVER_BOOTSTRAP_UPDATE_TRIGGER_ID		9
26 #define SERVER_APN_LINK_ID				10
27 #define SERVER_TLS_DTLS_ALERT_CODE_ID			11
28 #define SERVER_LAST_BOOTSTRAPPED_ID			12
29 #define SERVER_REGISTRATION_PRIORITY_ORDER_ID		13
30 #define SERVER_INITIAL_REGISTRATION_DELAY_TIMER_ID	14
31 #define SERVER_REGISTRATION_FAILURE_BLOCK_ID		15
32 #define SERVER_BOOTSTRAP_ON_REGISTRATION_FAILURE_ID	16
33 #define SERVER_COMMUNICATION_RETRY_COUNT_ID		17
34 #define SERVER_COMMUNICATION_RETRY_TIMER_ID		18
35 #define SERVER_COMMUNICATION_SEQUENCE_DELAY_TIMER_ID	19
36 #define SERVER_COMMUNICATION_SEQUENCE_RETRY_TIMER_ID	20
37 #define SERVER_SMS_TRIGGER_ID				21
38 #define SERVER_PREFERRED_TRANSPORT_ID			22
39 #define SERVER_MUTE_SEND_ID				23
40 
41 /**
42  * @brief Returns the default minimum period for an observation set for the server
43  * with object instance id given by @p obj_inst_id.
44  *
45  * @param[in] obj_inst_id Object instance id of the server object instance
46  * @return int32_t pmin value
47  */
48 int32_t lwm2m_server_get_pmin(uint16_t obj_inst_id);
49 
50 /**
51  * @brief Returns the default maximum period for an observation set for the server
52  * with object instance id given by @p obj_inst_id.
53  *
54  * @param[in] obj_inst_id Object instance id of the server object instance
55  * @return int32_t pmax value
56  */
57 int32_t lwm2m_server_get_pmax(uint16_t obj_inst_id);
58 
59 /**
60  * @brief Returns the Short Server ID of the server object instance with
61  * object instance id given by @p obj_inst_id.
62  *
63  * @param[in] obj_inst_id Object instance id of the server object
64  * @return SSID or negative in case not found
65  */
66 int lwm2m_server_get_ssid(uint16_t obj_inst_id);
67 
68 /**
69  * @brief Returns the object instance id of the server having ssid given by @p short_id.
70  *
71  * @param[in] short_id ssid of the server object
72  * @return Object instance id or negative in case not found
73  */
74 int lwm2m_server_short_id_to_inst(uint16_t short_id);
75 
76 /**
77  * @brief Check if given server instance is not disabled
78  *
79  * @param[in] obj_inst_id server instance
80  * @return true if not disabled, false otherwise.
81  */
82 bool lwm2m_server_is_enabled(uint16_t obj_inst_id);
83 
84 /**
85  * @brief Select server instance.
86  *
87  * Find possible server instance considering values on server data.
88  * Server candidates cannot be in disabled state and if priority values are set,
89  * those are compared and lowest values are considered first.
90  *
91  * If @ref obj_inst_id is NULL, this can be used to check if there are any server available.
92  *
93  * @param[out] obj_inst_id where selected server instance ID is written. Can be NULL.
94  * @return true if server instance was found, false otherwise.
95  */
96 bool lwm2m_server_select(uint16_t *obj_inst_id);
97 
98 /**
99  * @brief Disable server instance for a period of time.
100  *
101  * Timeout values can be calculated using kernel macros like K_SECONDS().
102  * Values like K_FOREVER or K_NO_WAIT are also accepted.
103  *
104  * @param timeout Timeout value.
105  * @return zero on success, negative error code otherwise.
106  */
107 int lwm2m_server_disable(uint16_t obj_inst_id, k_timeout_t timeout);
108 
109 /**
110  * @brief Get timepoint how long server instance is disabled.
111  *
112  * If server instance is not disabled, this still returns a valid timepoint
113  * that have already expired.
114  * If the instance id is not valid, the timepoint is set to K_FOREVER.
115  *
116  * @param obj_inst_id Server instance ID.
117  * @return timepoint
118  */
119 k_timepoint_t lwm2m_server_get_disabled_time(uint16_t obj_inst_id);
120 
121 /**
122  * @brief Get priority of given server instance.
123  *
124  * Lower values mean higher priority.
125  * If LwM2M server object version 1.1 is not enabled,
126  * this returns obj_inst_id as priority.
127  *
128  * @param obj_inst_id instance ID
129  * @return priority or UINT8_MAX if instance not found
130  */
131 uint8_t lwm2m_server_get_prio(uint16_t obj_inst_id);
132 
133 /**
134  * @brief Reset all disable-timers for all server instances.
135  *
136  */
137 void lwm2m_server_reset_timestamps(void);
138 
139 #if defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)
140 bool lwm2m_server_get_mute_send(uint16_t obj_inst_id);
141 #endif
142 
143 
144 #endif /* LWM2M_OBJ_SERVER_H_ */
145