arduino-audio-tools
Loading...
Searching...
No Matches
EthernetZephyr.h
Go to the documentation of this file.
1#pragma once
2
3#include <string.h>
4#include <zephyr/kernel.h>
5#include <zephyr/net/dhcpv4.h>
6#include <zephyr/net/net_event.h>
7#include <zephyr/net/net_if.h>
8#include <zephyr/net/net_ip.h>
9#include <zephyr/version.h>
10
11#include "AudioLogger.h"
12
13namespace audio_tools {
14
16 public:
17 // ------------------------------------------------------------
18 // Arduino-style API (SUPPORTED MODES ONLY)
19 // ------------------------------------------------------------
20 bool begin() {
23 uint8_t* mac = addr->addr;
24 // uint8_t len = addr->len;
25 return begin(mac);
26 }
27
28 bool begin(uint8_t* mac) {
29 struct in_addr ip = make_ip(0);
30 return begin(mac, ip, false);
31 }
32
33 bool begin(uint8_t* mac, struct in_addr ip) {
34 bool static_ip = (ip.s_addr != 0);
35 return begin(mac, ip, static_ip);
36 }
37
38 // ------------------------------------------------------------
39 // Status API
40 // ------------------------------------------------------------
41
42 bool connected() const { return _ip_ready; }
43
44 bool linkUp() const { return _link_up; }
45
46 const struct in_addr& localIP() const { return _ip; }
47
48 const char* localIPString() {
49 static char buf[NET_IPV4_ADDR_LEN];
50 net_addr_ntop(AF_INET, &_ip, buf, sizeof(buf));
51 return buf;
52 }
53
54 void end() {
55 if (_iface) {
56 net_if_down(_iface);
57 }
58 _ip_ready = false;
59 _link_up = false;
60 }
61
62 // ------------------------------------------------------------
63 // Internal begin
64 // ------------------------------------------------------------
65
66 private:
67 bool begin(uint8_t* mac, struct in_addr ip, bool static_ip) {
68 TRACEI();
69
70 _iface = net_if_get_default();
71 if (!_iface) {
72 LOGE("No network interface");
73 return false;
74 }
75
76 // MAC setup
78
79 // Bring interface up
80 net_if_up(_iface);
81
82 _instance = this;
83
84 // Register IP event
85 net_mgmt_init_event_callback(&_ip_cb, ip_event_handler,
87
89
90 if (static_ip) {
91 LOGI("Ethernet: static IP");
92
94
95 _ip = ip;
96 _ip_ready = true;
97 return true;
98 }
99
100 LOGI("Ethernet: DHCP");
101
102 net_dhcpv4_start(_iface);
103
104 return true;
105 }
106
107 // ------------------------------------------------------------
108 // Event handler
109 // ------------------------------------------------------------
110
111 static void ip_event_handler(struct net_mgmt_event_callback* cb,
112 uint64_t event, struct net_if* iface) {
113 if (!_instance) return;
114
115 if (event != NET_EVENT_IPV4_ADDR_ADD) return;
116
117 struct net_if_ipv4* ipv4 = iface->config.ip.ipv4;
118 if (!ipv4) return;
119
120 for (int i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
121#if KERNEL_VERSION_NUMBER >= ZEPHYR_VERSION(3, 4, 0)
122 struct net_if_addr_ipv4* entry = &ipv4->unicast[i];
123 struct net_if_addr* addr = &entry->ipv4;
124#else
125 struct net_if_addr* addr = &ipv4->unicast[i];
126#endif
127
128 if (addr->is_used && addr->addr_type == NET_ADDR_DHCP &&
129 addr->address.family == AF_INET) {
130 _instance->_ip = addr->address.in_addr;
131 _instance->_ip_ready = true;
132
134 net_addr_ntop(AF_INET, &_instance->_ip, buf, sizeof(buf));
135
136 LOGI("Ethernet IP: %s", buf);
137 break;
138 }
139 }
140 }
141
142 private:
143 // ------------------------------------------------------------
144 // helpers
145 // ------------------------------------------------------------
146
147 static inline struct in_addr make_ip(uint32_t v = 0) {
148 struct in_addr a;
149 a.s_addr = v;
150 return a;
151 }
152
153 private:
154 struct net_if* _iface = nullptr;
155
156 struct in_addr _ip = {};
157 volatile bool _ip_ready = false;
158 volatile bool _link_up = false;
159
160 struct net_mgmt_event_callback _ip_cb;
161
162 static EthernetZephyr* _instance;
163};
164
166
167// ------------------------------------------------------------
168// static instance
169// ------------------------------------------------------------
170
171inline EthernetZephyr* EthernetZephyr::_instance = nullptr;
172
174
175} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition EthernetZephyr.h:15
const char * localIPString()
Definition EthernetZephyr.h:48
const struct in_addr & localIP() const
Definition EthernetZephyr.h:46
bool begin()
Definition EthernetZephyr.h:20
bool linkUp() const
Definition EthernetZephyr.h:44
bool begin(uint8_t *mac, struct in_addr ip)
Definition EthernetZephyr.h:33
bool connected() const
Definition EthernetZephyr.h:42
void end()
Definition EthernetZephyr.h:54
bool begin(uint8_t *mac)
Definition EthernetZephyr.h:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
static EthernetZephyr Ethernet
Definition EthernetZephyr.h:173
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508