1 #include "no_warn_host.h"
2 #include "lwip/opt.h"
3 #include "lwip/def.h"
4 #include "lwip/pbuf.h"
5 #include "lwip/udp.h"
6 #include "tcpip_adapter.h"
7 #include <string.h>
8 #include <stdio.h>
9 
10 const ip_addr_t ip_addr_any;
11 const ip_addr_t ip_addr_broadcast;
12 const ip_addr_t ip_addr_any_type;
13 struct ip_globals ip_data;
14 struct netif *netif_list;
15 struct udp_pcb mock_pcb;
16 uint32_t g_random_numbers[8] = {0};
17 uint32_t g_random_numbers_cnt = 0;
18 
19 
pbuf_skip(struct pbuf * in,u16_t in_offset,u16_t * out_offset)20 struct pbuf* pbuf_skip(struct pbuf* in, u16_t in_offset, u16_t* out_offset)
21 {
22     u16_t offset_left = in_offset;
23     struct pbuf* q = in;
24 
25     /* get the correct pbuf */
26     while ((q != NULL) && (q->len <= offset_left)) {
27         offset_left -= q->len;
28         q = q->next;
29     }
30     if (out_offset != NULL) {
31         *out_offset = offset_left;
32     }
33     return q;
34 }
35 
pbuf_try_get_at(const struct pbuf * p,u16_t offset)36 int pbuf_try_get_at(const struct pbuf* p, u16_t offset)
37 {
38     u16_t q_idx;
39     struct pbuf* q = pbuf_skip(p, offset, &q_idx);
40 
41     /* return requested data if pbuf is OK */
42     if ((q != NULL) && (q->len > q_idx)) {
43         return ((u8_t*)q->payload)[q_idx];
44     }
45     return -1;
46 }
47 
pbuf_put_at(struct pbuf * p,u16_t offset,u8_t data)48 void pbuf_put_at(struct pbuf* p, u16_t offset, u8_t data)
49 {
50     u16_t q_idx;
51     struct pbuf* q = pbuf_skip(p, offset, &q_idx);
52 
53     /* write requested data if pbuf is OK */
54     if ((q != NULL) && (q->len > q_idx)) {
55         ((u8_t*)q->payload)[q_idx] = data;
56     }
57 }
58 
pbuf_get_at(const struct pbuf * p,u16_t offset)59 u8_t pbuf_get_at(const struct pbuf* p, u16_t offset)
60 {
61     u16_t q_idx;
62     struct pbuf* q = pbuf_skip(p, offset, &q_idx);
63 
64     /* return requested data if pbuf is OK */
65     if ((q != NULL) && (q->len > q_idx)) {
66         return ((u8_t*)q->payload)[q_idx];
67     }
68     return 0;
69 }
70 
pbuf_take(struct pbuf * buf,const void * dataptr,u16_t len)71 err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
72 {
73     return ERR_OK;
74 }
75 
pbuf_take_at(struct pbuf * buf,const void * dataptr,u16_t len,u16_t offset)76 err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
77 {
78     return ERR_OK;
79 }
80 
udp_new_ip_type(u8_t type)81 struct udp_pcb * udp_new_ip_type(u8_t type)
82 {
83     return &mock_pcb;
84 }
85 
tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if,tcpip_adapter_ip_info_t * ip_info)86 esp_err_t tcpip_adapter_get_ip_info(tcpip_adapter_if_t tcpip_if, tcpip_adapter_ip_info_t *ip_info)
87 {
88     return ESP_OK;
89 }
90 
pbuf_alloc(pbuf_layer layer,u16_t length,pbuf_type type)91 struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
92 {
93     struct pbuf * p;
94     p = (struct pbuf *)malloc(MEMP_PBUF_POOL);
95     p->tot_len = length;
96     p->next = NULL;
97     p->type_internal = PBUF_POOL;
98     p->len = length;
99     p->payload = malloc(length);
100     return p;
101 }
102 
pbuf_free(struct pbuf * p)103 u8_t pbuf_free(struct pbuf *p)
104 {
105     if (p) {
106         if (p->payload) {
107             free(p->payload);
108             p->payload = NULL;
109         }
110         free (p);
111         p = NULL;
112     }
113     return 1;
114 }
115 
udp_sendto(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port)116 err_t udp_sendto(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port)
117 {
118     return ESP_OK;
119 }
120 
udp_remove(struct udp_pcb * pcb)121 void udp_remove(struct udp_pcb *pcb)
122 {
123     if (pcb == NULL)
124     {
125        free(pcb);
126     }
127 }
128 
udp_new(void)129 struct udp_pcb *udp_new(void)
130 {
131     return malloc(sizeof(struct udp_pcb));
132 }
133 
udp_bind(struct udp_pcb * pcb,const ip_addr_t * ipaddr,u16_t port)134 err_t udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
135 {
136     return ESP_OK;
137 }
138 
udp_recv(struct udp_pcb * pcb,udp_recv_fn recv,void * recv_arg)139 void udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
140 {
141 }
142 
udp_disconnect(struct udp_pcb * pcb)143 void udp_disconnect(struct udp_pcb *pcb)
144 {
145 }
146 
147 #ifndef NOT_MOCK_DNS
dns_setserver(u8_t numdns,const ip_addr_t * dnsserver)148 void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
149 {
150 }
151 #endif
152 
esp_random(void)153 uint32_t esp_random(void)
154 {
155     // Preparation for injecting favorable random numbers
156     return g_random_numbers[g_random_numbers_cnt++ % 8];
157 }
158 
etharp_query(struct netif * netif,const ip4_addr_t * ipaddr,struct pbuf * q)159 err_t etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
160 {
161     return ESP_OK;
162 }
163 
netif_set_addr(struct netif * netif,const ip4_addr_t * ipaddr,const ip4_addr_t * netmask,const ip4_addr_t * gw)164 void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
165                     const ip4_addr_t *gw)
166 {
167 }
168 
pbuf_realloc(struct pbuf * p,u16_t size)169 void pbuf_realloc(struct pbuf *p, u16_t size)
170 {
171     if (p != NULL)
172     {
173         uint8_t *buf = malloc(size);
174         free(p->payload);
175         p->payload = buf;
176         p->len = size;
177         p->tot_len = size;
178     }
179 }
180 
pbuf_copy_partial(const struct pbuf * buf,void * dataptr,u16_t len,u16_t offset)181 u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
182 {
183     struct pbuf *p;
184     u16_t left;
185     u16_t buf_copy_len;
186     u16_t copied_total = 0;
187 
188     LWIP_ERROR("pbuf_copy_partial: invalid buf", (buf != NULL), return 0;);
189     LWIP_ERROR("pbuf_copy_partial: invalid dataptr", (dataptr != NULL), return 0;);
190 
191     left = 0;
192 
193     if ((buf == NULL) || (dataptr == NULL)) {
194         return 0;
195     }
196 
197     /* Note some systems use byte copy if dataptr or one of the pbuf payload pointers are unaligned. */
198     for (p = buf; len != 0 && p != NULL; p = p->next) {
199         if ((offset != 0) && (offset >= p->len)) {
200         /* don't copy from this buffer -> on to the next */
201         offset -= p->len;
202         } else {
203         /* copy from this buffer. maybe only partially. */
204         buf_copy_len = p->len - offset;
205         if (buf_copy_len > len)
206             buf_copy_len = len;
207         /* copy the necessary parts of the buffer */
208         MEMCPY(&((char*)dataptr)[left], &((char*)p->payload)[offset], buf_copy_len);
209         copied_total += buf_copy_len;
210         left += buf_copy_len;
211         len -= buf_copy_len;
212         offset = 0;
213         }
214     }
215     return copied_total;
216 }
217 
udp_connect(struct udp_pcb * pcb,const ip_addr_t * ipaddr,u16_t port)218 err_t udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
219 {
220     return ESP_OK;
221 }
222 
udp_sendto_if(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port,struct netif * netif)223 err_t udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
224 {
225     return ESP_OK;
226 }
227 
udp_sendto_if_src(struct udp_pcb * pcb,struct pbuf * p,const ip_addr_t * dst_ip,u16_t dst_port,struct netif * netif,const ip_addr_t * src_ip)228 err_t udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
229 {
230     return ESP_OK;
231 }
232 
mem_malloc(mem_size_t size)233 void * mem_malloc(mem_size_t size)
234 {
235     return malloc(size);
236 }
237 
mem_free(void * rmem)238 void mem_free(void *rmem)
239 {
240     free(rmem);
241 }
242