1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 #include <stubs.h>
9 
10 LOG_MODULE_DECLARE(lwm2m_engine_test);
11 
12 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_rd_client_pause);
13 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_rd_client_resume);
14 DEFINE_FAKE_VALUE_FUNC(struct lwm2m_message *, find_msg, struct coap_pending *,
15 		       struct coap_reply *);
16 DEFINE_FAKE_VOID_FUNC(coap_pending_clear, struct coap_pending *);
17 DEFINE_FAKE_VOID_FUNC(lwm2m_reset_message, struct lwm2m_message *, bool);
18 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_send_message_async, struct lwm2m_message *);
19 DEFINE_FAKE_VOID_FUNC(lwm2m_registry_lock);
20 DEFINE_FAKE_VOID_FUNC(lwm2m_registry_unlock);
21 DEFINE_FAKE_VALUE_FUNC(bool, coap_pending_cycle, struct coap_pending *);
22 DEFINE_FAKE_VALUE_FUNC(int, generate_notify_message, struct lwm2m_ctx *, struct observe_node *,
23 		       void *);
24 DEFINE_FAKE_VALUE_FUNC(int64_t, engine_observe_shedule_next_event, struct observe_node *, uint16_t,
25 		       const int64_t);
26 DEFINE_FAKE_VALUE_FUNC(int, handle_request, struct coap_packet *, struct lwm2m_message *);
27 DEFINE_FAKE_VOID_FUNC(lwm2m_udp_receive, struct lwm2m_ctx *, uint8_t *, uint16_t, struct sockaddr *,
28 		      udp_request_handler_cb_t);
29 DEFINE_FAKE_VALUE_FUNC(bool, lwm2m_rd_client_is_registred, struct lwm2m_ctx *);
30 DEFINE_FAKE_VOID_FUNC(lwm2m_engine_context_close, struct lwm2m_ctx *);
31 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_get_res_buf, const struct lwm2m_obj_path *, void **, uint16_t *,
32 		       uint16_t *, uint8_t *);
33 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_parse_peerinfo, char *, struct lwm2m_ctx *, bool);
34 DEFINE_FAKE_VALUE_FUNC(int, tls_credential_add, sec_tag_t, enum tls_credential_type, const void *,
35 		       size_t);
36 DEFINE_FAKE_VALUE_FUNC(int, tls_credential_delete, sec_tag_t, enum tls_credential_type);
37 DEFINE_FAKE_VALUE_FUNC(struct lwm2m_engine_obj_field *, lwm2m_get_engine_obj_field,
38 		       struct lwm2m_engine_obj *, int);
39 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_get_bool, const struct lwm2m_obj_path *, bool *);
40 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_delete_obj_inst, uint16_t, uint16_t);
41 DEFINE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts);
42 
43 static sys_slist_t obs_obj_path_list = SYS_SLIST_STATIC_INIT(&obs_obj_path_list);
lwm2m_obs_obj_path_list(void)44 sys_slist_t *lwm2m_obs_obj_path_list(void)
45 {
46 	return &obs_obj_path_list;
47 }
48 
49 static sys_slist_t engine_obj_inst_list = SYS_SLIST_STATIC_INIT(&engine_obj_inst_list);
lwm2m_engine_obj_inst_list(void)50 sys_slist_t *lwm2m_engine_obj_inst_list(void) { return &engine_obj_inst_list; }
51 
52 struct zsock_pollfd {
53 	int fd;
54 	short events;
55 	short revents;
56 };
57 
58 static short my_events;
59 
set_socket_events(short events)60 void set_socket_events(short events)
61 {
62 	my_events |= events;
63 }
64 
clear_socket_events(void)65 void clear_socket_events(void)
66 {
67 	my_events = 0;
68 }
69 
z_impl_zsock_socket(int family,int type,int proto)70 int z_impl_zsock_socket(int family, int type, int proto)
71 {
72 	return 0;
73 }
74 
z_impl_zsock_close(int sock)75 int z_impl_zsock_close(int sock)
76 {
77 	return 0;
78 }
79 
80 DEFINE_FAKE_VALUE_FUNC(int, z_impl_zsock_connect, int, const struct sockaddr *, socklen_t);
81 
z_impl_zsock_sendto(int sock,const void * buf,size_t len,int flags,const struct sockaddr * dest_addr,socklen_t addrlen)82 ssize_t z_impl_zsock_sendto(int sock, const void *buf, size_t len, int flags,
83 			    const struct sockaddr *dest_addr, socklen_t addrlen)
84 {
85 	k_sleep(K_MSEC(1));
86 	if (my_events & ZSOCK_POLLOUT) {
87 		my_events = 0;
88 	}
89 	return 1;
90 }
91 
z_impl_zsock_recvfrom(int sock,void * buf,size_t max_len,int flags,struct sockaddr * src_addr,socklen_t * addrlen)92 ssize_t z_impl_zsock_recvfrom(int sock, void *buf, size_t max_len, int flags,
93 			      struct sockaddr *src_addr, socklen_t *addrlen)
94 {
95 	k_sleep(K_MSEC(1));
96 	if (my_events & ZSOCK_POLLIN) {
97 		my_events = 0;
98 		return 1;
99 	}
100 	errno = EWOULDBLOCK;
101 	return -1;
102 }
103 
z_impl_zsock_poll(struct zsock_pollfd * fds,int nfds,int poll_timeout)104 int z_impl_zsock_poll(struct zsock_pollfd *fds, int nfds, int poll_timeout)
105 {
106 	k_sleep(K_MSEC(poll_timeout));
107 	fds->revents = my_events;
108 	return 0;
109 }
110 
z_impl_zsock_setsockopt(int sock,int level,int optname,const void * optval,socklen_t optlen)111 int z_impl_zsock_setsockopt(int sock, int level, int optname, const void *optval, socklen_t optlen)
112 {
113 	return 0;
114 }
115 
z_impl_zsock_fcntl(int sock,int cmd,int flags)116 int z_impl_zsock_fcntl(int sock, int cmd, int flags)
117 {
118 	return 0;
119 }
120