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(coap_client_test, LOG_LEVEL_DBG);
11
12 DEFINE_FAKE_VALUE_FUNC(uint32_t, z_impl_sys_rand32_get);
13 DEFINE_FAKE_VALUE_FUNC(ssize_t, z_impl_zsock_recvfrom, int, void *, size_t, int, struct sockaddr *,
14 socklen_t *);
15 DEFINE_FAKE_VALUE_FUNC(ssize_t, z_impl_zsock_sendto, int, void *, size_t, int,
16 const struct sockaddr *, socklen_t);
17
18 struct zvfs_pollfd {
19 int fd;
20 short events;
21 short revents;
22 };
23
24 static short my_events[NUM_FD];
25
set_socket_events(int fd,short events)26 void set_socket_events(int fd, short events)
27 {
28 __ASSERT_NO_MSG(fd < NUM_FD);
29 my_events[fd] |= events;
30 }
31
clear_socket_events(int fd,short events)32 void clear_socket_events(int fd, short events)
33 {
34 my_events[fd] &= ~events;
35 }
36
z_impl_zvfs_poll(struct zvfs_pollfd * fds,int nfds,int poll_timeout)37 int z_impl_zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout)
38 {
39 int events = 0;
40 k_sleep(K_MSEC(1));
41 for (int i = 0; i < nfds; i++) {
42 fds[i].revents =
43 my_events[fds[i].fd] & (fds[i].events | ZSOCK_POLLERR | ZSOCK_POLLHUP);
44 if (fds[i].revents) {
45 events++;
46 }
47 }
48 if (events == 0) {
49 k_sleep(K_MSEC(poll_timeout));
50 }
51
52 return events;
53 }
54