1
+ #pragma once
2
+ #include < Arduino.h>
3
+ #include " lwip/include/lwip/ip_addr.h"
4
+
5
+ inline ip_addr_t fromArduinoIP (const IPAddress& ip) {
6
+ #if LWIP_IPV4
7
+ ip_addr_t res;
8
+ if (ip.type () == arduino::IPv4) {
9
+ if (ip == INADDR_NONE) {
10
+ ip_addr_copy (res, *IP4_ADDR_ANY);
11
+ } else {
12
+ IP_ADDR4 (&res, ip[0 ], ip[1 ], ip[2 ], ip[3 ]);
13
+ }
14
+ }
15
+ #endif // LWIP_IPV4
16
+ #if LWIP_IPV4 && LWIP_IPV6
17
+ else
18
+ #endif // LWIP_IPV4 && LWIP_IPV6
19
+ #if LWIP_IPV6 // TODO change the setting and try ipv6: This is currently set to 0
20
+ if (ip.type () == arduino::IPv6) {
21
+ if (ip == INADDR_NONE) {
22
+ // ip_addr_copy(res, *IP6_ADDR_ANY);
23
+ // FIXME implement this
24
+ } else {
25
+ // FIXME implement this, it could be useful to have a function in the IPAddress class to help this out
26
+ }
27
+ }
28
+ #endif // LWIP_IPV6
29
+ return res;
30
+ }
31
+
32
+ inline IPAddress toArduinoIP (const ip_addr_t * ip) {
33
+ if (ip == nullptr ) {
34
+ return INADDR_NONE;
35
+ }
36
+
37
+ #if LWIP_IPV4
38
+ if (IP_IS_V4 (ip)) {
39
+ if (ip_addr_isany_val (*ip)) {
40
+ return INADDR_NONE;
41
+ } else {
42
+ return IPAddress (arduino::IPv4, (uint8_t *)&ip_2_ip4 (ip)->addr );
43
+ }
44
+ }
45
+ #endif // LWIP_IPV4
46
+
47
+ #if LWIP_IPV6 // TODO change the setting and try ipv6: This is currently set to 0
48
+ if (IP_IS_V6 (ip)) {
49
+ if (ip_addr_isany_val (*ip)) {
50
+ return IN6ADDR_ANY;
51
+ } else {
52
+ return IPAddress (arduino::IPv6, (uint8_t *)ip_2_ip6 (ip)->addr );
53
+ }
54
+ }
55
+ #endif
56
+
57
+ #if LWIP_IPV4 && LWIP_IPV6
58
+ if (IP_IS_ANY_TYPE_VAL (ip)) {
59
+ // FIXME understand what this means
60
+ }
61
+ #endif
62
+
63
+ return INADDR_NONE;
64
+ }
65
+
66
+
67
+ namespace arduino {
68
+ // TODO leverage on RAII
69
+ inline volatile uint32_t lock_counter;
70
+ inline void lock () {
71
+ __disable_irq ();
72
+ lock_counter++; // This action breaks everything
73
+ }
74
+
75
+ inline void unlock () {
76
+ if (lock_counter > 0 ) {
77
+ lock_counter--;
78
+ }
79
+
80
+ if (lock_counter == 0 ) {
81
+ __enable_irq (); // this could be called multiple times if the calls are not setup properly
82
+ }
83
+ }
84
+ }
0 commit comments