1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2018-2019 Foundries.io
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef LWM2M_ENGINE_H
9 #define LWM2M_ENGINE_H
10 
11 #include "lwm2m_object.h"
12 
13 #define LWM2M_PROTOCOL_VERSION_MAJOR 1
14 #define LWM2M_PROTOCOL_VERSION_MINOR 0
15 
16 #define LWM2M_PROTOCOL_VERSION_STRING STRINGIFY(LWM2M_PROTOCOL_VERSION_MAJOR) \
17 				      "." \
18 				      STRINGIFY(LWM2M_PROTOCOL_VERSION_MINOR)
19 
20 /* LWM2M / CoAP Content-Formats */
21 #define LWM2M_FORMAT_PLAIN_TEXT		0
22 #define LWM2M_FORMAT_APP_LINK_FORMAT	40
23 #define LWM2M_FORMAT_APP_OCTET_STREAM	42
24 #define LWM2M_FORMAT_APP_EXI		47
25 #define LWM2M_FORMAT_APP_JSON		50
26 #define LWM2M_FORMAT_OMA_PLAIN_TEXT	1541
27 #define LWM2M_FORMAT_OMA_OLD_TLV	1542
28 #define LWM2M_FORMAT_OMA_OLD_JSON	1543
29 #define LWM2M_FORMAT_OMA_OLD_OPAQUE	1544
30 #define LWM2M_FORMAT_OMA_TLV		11542
31 #define LWM2M_FORMAT_OMA_JSON		11543
32 /* 65000 ~ 65535 inclusive are reserved for experiments */
33 #define LWM2M_FORMAT_NONE		65535
34 
35 
36 #define COAP_RESPONSE_CODE_CLASS(x)	(x >> 5)
37 #define COAP_RESPONSE_CODE_DETAIL(x)	(x & 0x1F)
38 
39 /* TODO: */
40 #define NOTIFY_OBSERVER(o, i, r)	lwm2m_notify_observer(o, i, r)
41 #define NOTIFY_OBSERVER_PATH(path)	lwm2m_notify_observer_path(path)
42 
43 /* Use this value to generate new token */
44 #define LWM2M_MSG_TOKEN_GENERATE_NEW 0xFFU
45 
46 /* length of time in milliseconds to wait for buffer allocations */
47 #define BUF_ALLOC_TIMEOUT K_SECONDS(1)
48 
49 /* coap reply status */
50 #define COAP_REPLY_STATUS_NONE		0
51 #define COAP_REPLY_STATUS_ERROR		1
52 
53 /* Establish a request handler callback type */
54 typedef int (*udp_request_handler_cb_t)(struct coap_packet *request,
55 					struct lwm2m_message *msg);
56 
57 char *lwm2m_sprint_ip_addr(const struct sockaddr *addr);
58 
59 int lwm2m_notify_observer(uint16_t obj_id, uint16_t obj_inst_id, uint16_t res_id);
60 int lwm2m_notify_observer_path(struct lwm2m_obj_path *path);
61 
62 void lwm2m_register_obj(struct lwm2m_engine_obj *obj);
63 void lwm2m_unregister_obj(struct lwm2m_engine_obj *obj);
64 struct lwm2m_engine_obj_field *
65 lwm2m_get_engine_obj_field(struct lwm2m_engine_obj *obj, int res_id);
66 int  lwm2m_create_obj_inst(uint16_t obj_id, uint16_t obj_inst_id,
67 			   struct lwm2m_engine_obj_inst **obj_inst);
68 int  lwm2m_delete_obj_inst(uint16_t obj_id, uint16_t obj_inst_id);
69 int  lwm2m_get_or_create_engine_obj(struct lwm2m_message *msg,
70 				    struct lwm2m_engine_obj_inst **obj_inst,
71 				    uint8_t *created);
72 
73 struct lwm2m_engine_obj *lwm2m_engine_get_obj(
74 					const struct lwm2m_obj_path *path);
75 struct lwm2m_engine_obj_inst *lwm2m_engine_get_obj_inst(
76 					const struct lwm2m_obj_path *path);
77 struct lwm2m_engine_res *lwm2m_engine_get_res(
78 					const struct lwm2m_obj_path *path);
79 
80 bool lwm2m_engine_shall_report_obj_version(const struct lwm2m_engine_obj *obj);
81 
82 /* LwM2M context functions */
83 int lwm2m_engine_context_close(struct lwm2m_ctx *client_ctx);
84 void lwm2m_engine_context_init(struct lwm2m_ctx *client_ctx);
85 
86 /* Message buffer functions */
87 uint8_t *lwm2m_get_message_buf(void);
88 int lwm2m_put_message_buf(uint8_t *buf);
89 
90 /* LwM2M message functions */
91 struct lwm2m_message *lwm2m_get_message(struct lwm2m_ctx *client_ctx);
92 void lwm2m_reset_message(struct lwm2m_message *msg, bool release);
93 int lwm2m_init_message(struct lwm2m_message *msg);
94 int lwm2m_send_message_async(struct lwm2m_message *msg);
95 int lwm2m_send_empty_ack(struct lwm2m_ctx *client_ctx, uint16_t mid);
96 
97 int lwm2m_register_payload_handler(struct lwm2m_message *msg);
98 
99 int lwm2m_perform_read_op(struct lwm2m_message *msg, uint16_t content_format);
100 
101 int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
102 			struct lwm2m_engine_res *res,
103 			struct lwm2m_engine_res_inst *res_inst,
104 			struct lwm2m_engine_obj_field *obj_field,
105 			struct lwm2m_message *msg);
106 
107 int lwm2m_discover_handler(struct lwm2m_message *msg, bool is_bootstrap);
108 
109 enum coap_block_size lwm2m_default_block_size(void);
110 
111 int lwm2m_engine_add_service(k_work_handler_t service, uint32_t period_ms);
112 
113 int lwm2m_engine_get_resource(char *pathstr,
114 			      struct lwm2m_engine_res **res);
115 
116 void lwm2m_engine_get_binding(char *binding);
117 
118 size_t lwm2m_engine_get_opaque_more(struct lwm2m_input_context *in,
119 				    uint8_t *buf, size_t buflen,
120 				    struct lwm2m_opaque_context *opaque,
121 				    bool *last_block);
122 
123 int lwm2m_security_inst_id_to_index(uint16_t obj_inst_id);
124 int lwm2m_security_index_to_inst_id(int index);
125 
126 int32_t lwm2m_server_get_pmin(uint16_t obj_inst_id);
127 int32_t lwm2m_server_get_pmax(uint16_t obj_inst_id);
128 int lwm2m_server_short_id_to_inst(uint16_t short_id);
129 
130 #if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT)
131 uint8_t lwm2m_firmware_get_update_state(void);
132 void lwm2m_firmware_set_update_state(uint8_t state);
133 void lwm2m_firmware_set_update_result(uint8_t result);
134 uint8_t lwm2m_firmware_get_update_result(void);
135 #endif
136 
137 /* Attribute handling. */
138 
139 struct lwm2m_attr *lwm2m_engine_get_next_attr(const void *ref,
140 					      struct lwm2m_attr *prev);
141 const char *lwm2m_engine_get_attr_name(const struct lwm2m_attr *attr);
142 
143 /* Network Layer */
144 int  lwm2m_socket_add(struct lwm2m_ctx *ctx);
145 void lwm2m_socket_del(struct lwm2m_ctx *ctx);
146 int  lwm2m_socket_start(struct lwm2m_ctx *client_ctx);
147 int  lwm2m_parse_peerinfo(char *url, struct sockaddr *addr, bool *use_dtls);
148 
149 #endif /* LWM2M_ENGINE_H */
150