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(size_t, coap_pendings_count, struct coap_pending *, size_t);
23 DEFINE_FAKE_VALUE_FUNC(int, generate_notify_message, struct lwm2m_ctx *, struct observe_node *,
24 void *);
25 DEFINE_FAKE_VALUE_FUNC(int64_t, engine_observe_shedule_next_event, struct observe_node *, uint16_t,
26 const int64_t);
27 DEFINE_FAKE_VALUE_FUNC(int, handle_request, struct coap_packet *, struct lwm2m_message *);
28 DEFINE_FAKE_VOID_FUNC(lwm2m_udp_receive, struct lwm2m_ctx *, uint8_t *, uint16_t,
29 struct sockaddr *);
30 DEFINE_FAKE_VALUE_FUNC(bool, lwm2m_rd_client_is_registred, struct lwm2m_ctx *);
31 DEFINE_FAKE_VOID_FUNC(lwm2m_engine_context_close, struct lwm2m_ctx *);
32 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_get_res_buf, const struct lwm2m_obj_path *, void **, uint16_t *,
33 uint16_t *, uint8_t *);
34 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_parse_peerinfo, char *, struct lwm2m_ctx *, bool);
35 DEFINE_FAKE_VALUE_FUNC(int, tls_credential_add, sec_tag_t, enum tls_credential_type, const void *,
36 size_t);
37 DEFINE_FAKE_VALUE_FUNC(int, tls_credential_delete, sec_tag_t, enum tls_credential_type);
38 DEFINE_FAKE_VALUE_FUNC(struct lwm2m_engine_obj_field *, lwm2m_get_engine_obj_field,
39 struct lwm2m_engine_obj *, int);
40 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_get_bool, const struct lwm2m_obj_path *, bool *);
41 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_delete_obj_inst, uint16_t, uint16_t);
42 DEFINE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts);
43 DEFINE_FAKE_VALUE_FUNC(int, lwm2m_security_mode, struct lwm2m_ctx *);
44 DEFINE_FAKE_VALUE_FUNC(int, z_impl_zsock_setsockopt, int, int, int, const void *, socklen_t);
45 DEFINE_FAKE_VOID_FUNC(engine_update_tx_time);
46 DEFINE_FAKE_VALUE_FUNC(bool, coap_block_has_more, struct coap_packet *);
47
48 static sys_slist_t obs_obj_path_list = SYS_SLIST_STATIC_INIT(&obs_obj_path_list);
lwm2m_obs_obj_path_list(void)49 sys_slist_t *lwm2m_obs_obj_path_list(void)
50 {
51 return &obs_obj_path_list;
52 }
53
54 static sys_slist_t engine_obj_inst_list = SYS_SLIST_STATIC_INIT(&engine_obj_inst_list);
lwm2m_engine_obj_inst_list(void)55 sys_slist_t *lwm2m_engine_obj_inst_list(void) { return &engine_obj_inst_list; }
56
57 struct zvfs_pollfd {
58 int fd;
59 short events;
60 short revents;
61 };
62
63 static short my_events;
64
set_socket_events(short events)65 void set_socket_events(short events)
66 {
67 my_events |= events;
68 }
69
clear_socket_events(void)70 void clear_socket_events(void)
71 {
72 my_events = 0;
73 }
74
z_impl_zsock_socket(int family,int type,int proto)75 int z_impl_zsock_socket(int family, int type, int proto)
76 {
77 return 0;
78 }
79
z_impl_zsock_close(int sock)80 int z_impl_zsock_close(int sock)
81 {
82 return 0;
83 }
84
85 #define PAIR_IN 10
86 #define PAIR_OUT 11
87
z_impl_zsock_socketpair(int family,int type,int proto,int * sv)88 int z_impl_zsock_socketpair(int family, int type, int proto, int *sv)
89 {
90 sv[0] = PAIR_IN;
91 sv[1] = PAIR_OUT;
92 return 0;
93 }
94
95 DEFINE_FAKE_VALUE_FUNC(int, z_impl_zsock_connect, int, const struct sockaddr *, socklen_t);
96
z_impl_zsock_sendto(int sock,const void * buf,size_t len,int flags,const struct sockaddr * dest_addr,socklen_t addrlen)97 ssize_t z_impl_zsock_sendto(int sock, const void *buf, size_t len, int flags,
98 const struct sockaddr *dest_addr, socklen_t addrlen)
99 {
100 if (sock == PAIR_OUT) {
101 return 1;
102 }
103 k_sleep(K_MSEC(1));
104 if (my_events & ZSOCK_POLLOUT) {
105 my_events = 0;
106 }
107 return 1;
108 }
109
z_impl_zsock_recvfrom(int sock,void * buf,size_t max_len,int flags,struct sockaddr * src_addr,socklen_t * addrlen)110 ssize_t z_impl_zsock_recvfrom(int sock, void *buf, size_t max_len, int flags,
111 struct sockaddr *src_addr, socklen_t *addrlen)
112 {
113 if (sock == PAIR_IN) {
114 return 1;
115 }
116
117 k_sleep(K_MSEC(1));
118 if (my_events & ZSOCK_POLLIN) {
119 my_events = 0;
120 return 1;
121 }
122 errno = EWOULDBLOCK;
123 return -1;
124 }
125
z_impl_zvfs_poll(struct zvfs_pollfd * fds,int nfds,int poll_timeout)126 int z_impl_zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout)
127 {
128 k_sleep(K_MSEC(1));
129 fds->revents = my_events;
130 return 0;
131 }
132
z_impl_zsock_fcntl_impl(int sock,int cmd,int flags)133 int z_impl_zsock_fcntl_impl(int sock, int cmd, int flags)
134 {
135 return 0;
136 }
137