1 /*
2  * Copyright (c) 2022 grandcentrix GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef STUBS_H
8 #define STUBS_H
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 #include <zephyr/fff.h>
14 #include <zephyr/net/lwm2m.h>
15 #include <zephyr/ztest.h>
16 
17 #include <lwm2m_engine.h>
18 
19 /* Number of iterations the state machine within the RDClient service
20  * is triggered
21  */
22 static const uint16_t RD_CLIENT_MAX_SERVICE_ITERATIONS = UINT16_MAX;
23 
24 /* zephyr/net/coap.h */
25 DECLARE_FAKE_VALUE_FUNC(uint8_t, coap_header_get_code, const struct coap_packet *);
26 uint8_t coap_header_get_code_fake_created(const struct coap_packet *cpkt);
27 uint8_t coap_header_get_code_fake_deleted(const struct coap_packet *cpkt);
28 uint8_t coap_header_get_code_fake_changed(const struct coap_packet *cpkt);
29 uint8_t coap_header_get_code_fake_bad_request(const struct coap_packet *cpkt);
30 DECLARE_FAKE_VALUE_FUNC(int, coap_append_option_int, struct coap_packet *, uint16_t, unsigned int);
31 DECLARE_FAKE_VALUE_FUNC(int, coap_packet_append_option, struct coap_packet *, uint16_t,
32 			const uint8_t *, uint16_t);
33 int coap_packet_append_option_fake_err(struct coap_packet *cpkt, uint16_t code,
34 				       const uint8_t *value, uint16_t len);
35 DECLARE_FAKE_VALUE_FUNC(int, coap_packet_append_payload_marker, struct coap_packet *);
36 DECLARE_FAKE_VALUE_FUNC(int, coap_find_options, const struct coap_packet *, uint16_t,
37 			struct coap_option *, uint16_t);
38 int coap_find_options_do_registration_reply_cb_ok(const struct coap_packet *cpkt, uint16_t code,
39 						  struct coap_option *options, uint16_t veclen);
40 DECLARE_FAKE_VALUE_FUNC(uint16_t, coap_next_id);
41 
42 /* zephyr/net/lwm2m.h */
43 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_engine_start, struct lwm2m_ctx *);
44 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_engine_stop, struct lwm2m_ctx *);
45 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_open_socket, struct lwm2m_ctx *);
46 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_get_u32, const struct lwm2m_obj_path *, uint32_t *);
47 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_get_u16, const struct lwm2m_obj_path *, uint16_t *);
48 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_get_bool, const struct lwm2m_obj_path *, bool *);
49 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_set_u32, const struct lwm2m_obj_path *, uint32_t);
50 int lwm2m_get_bool_fake_default(const struct lwm2m_obj_path *path, bool *value);
51 int lwm2m_get_bool_fake_true(const struct lwm2m_obj_path *path, bool *value);
52 extern uint32_t get_u32_val;
53 int lwm2m_get_u32_val(const struct lwm2m_obj_path *path, uint32_t *val);
54 
55 /* subsys/net/lib/lwm2m/lwm2m_engine.h */
56 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_socket_start, struct lwm2m_ctx *);
57 int lwm2m_socket_start_fake_fail(struct lwm2m_ctx *client_ctx);
58 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_socket_close, struct lwm2m_ctx *);
59 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_close_socket, struct lwm2m_ctx *);
60 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_socket_suspend, struct lwm2m_ctx *);
61 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_security_inst_id_to_index, uint16_t);
62 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_security_short_id_to_inst, uint16_t);
63 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_security_index_to_inst_id, int);
64 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_engine_connection_resume, struct lwm2m_ctx *);
65 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_push_queued_buffers, struct lwm2m_ctx *);
66 DECLARE_FAKE_VOID_FUNC(lwm2m_engine_context_init, struct lwm2m_ctx *);
67 void lwm2m_engine_context_init_fake1(struct lwm2m_ctx *client_ctx);
68 void test_throw_network_error_from_engine(int err);
69 DECLARE_FAKE_VOID_FUNC(lwm2m_engine_context_close, struct lwm2m_ctx *);
70 DECLARE_FAKE_VALUE_FUNC(char *, lwm2m_sprint_ip_addr, const struct sockaddr *);
71 char *lwm2m_sprint_ip_addr_fake_default(const struct sockaddr *addr);
72 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_server_short_id_to_inst, uint16_t);
73 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_server_disable, uint16_t, k_timeout_t);
74 DECLARE_FAKE_VALUE_FUNC(uint8_t, lwm2m_server_get_prio, uint16_t);
75 DECLARE_FAKE_VOID_FUNC(lwm2m_server_reset_timestamps);
76 
77 
78 void wait_for_service(uint16_t cycles);
79 void test_lwm2m_engine_start_service(void);
80 void test_lwm2m_engine_stop_service(void);
81 void stub_lwm2m_server_disable(bool disable);
82 
83 /* subsys/net/lib/lwm2m/lwm2m_message_handling.h  */
84 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_init_message, struct lwm2m_message *);
85 DECLARE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts);
86 int lwm2m_init_message_fake_default(struct lwm2m_message *msg);
87 void test_prepare_pending_message_cb(void *cb);
88 
89 DECLARE_FAKE_VOID_FUNC(lwm2m_reset_message, struct lwm2m_message *, bool);
90 DECLARE_FAKE_VALUE_FUNC(int, lwm2m_send_message_async, struct lwm2m_message *);
91 
92 /* subsys/net/lib/lwm2m/lwm2m_registry.h */
93 DECLARE_FAKE_VOID_FUNC(lwm2m_engine_get_binding, char *);
94 DECLARE_FAKE_VOID_FUNC(lwm2m_engine_get_queue_mode, char *);
95 
96 /* subsys/net/lib/lwm2m/lwm2m_rw_link_format.h */
97 DECLARE_FAKE_VALUE_FUNC(int, do_register_op_link_format, struct lwm2m_message *);
98 
99 #define DO_FOREACH_FAKE(FUNC)                                                                      \
100 	do {                                                                                       \
101 		FUNC(coap_header_get_code)                                                         \
102 		FUNC(coap_append_option_int)                                                       \
103 		FUNC(coap_packet_append_option)                                                    \
104 		FUNC(coap_packet_append_payload_marker)                                            \
105 		FUNC(coap_find_options)                                                            \
106 		FUNC(coap_next_id)                                                                 \
107 		FUNC(lwm2m_engine_start)                                                           \
108 		FUNC(lwm2m_engine_stop)                                                            \
109 		FUNC(lwm2m_get_u32)                                                                \
110 		FUNC(lwm2m_get_u16)                                                                \
111 		FUNC(lwm2m_get_bool)                                                               \
112 		FUNC(lwm2m_socket_start)                                                           \
113 		FUNC(lwm2m_socket_close)                                                           \
114 		FUNC(lwm2m_close_socket)                                                           \
115 		FUNC(lwm2m_security_inst_id_to_index)                                              \
116 		FUNC(lwm2m_engine_connection_resume)                                               \
117 		FUNC(lwm2m_push_queued_buffers)                                                    \
118 		FUNC(lwm2m_engine_context_init)                                                    \
119 		FUNC(lwm2m_engine_context_close)                                                   \
120 		FUNC(lwm2m_sprint_ip_addr)                                                         \
121 		FUNC(lwm2m_server_short_id_to_inst)                                                \
122 		FUNC(lwm2m_security_index_to_inst_id)                                              \
123 		FUNC(lwm2m_init_message)                                                           \
124 		FUNC(lwm2m_reset_message)                                                          \
125 		FUNC(lwm2m_send_message_async)                                                     \
126 		FUNC(lwm2m_engine_get_binding)                                                     \
127 		FUNC(lwm2m_engine_get_queue_mode)                                                  \
128 		FUNC(do_register_op_link_format)                                                   \
129 		FUNC(lwm2m_clear_block_contexts)                                                   \
130 	} while (0)
131 
132 #endif /* STUBS_H */
133