Mbed OS Reference
Loading...
Searching...
No Matches
EMAC.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2016 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef EMAC_H
19#define EMAC_H
20
21#include <stdbool.h>
22#include "Callback.h"
23#include "EMACMemoryManager.h"
24
25// Nuvoton platform headers define EMAC - avoid the collision
26#undef EMAC
27
28/**
29 * This interface should be used to abstract low level access to networking hardware
30 * All operations receive a `void *` hardware pointer which an EMAC device provides when
31 * it is registered with a stack.
32 */
33class EMAC {
34public:
35
36 /** Return the default on-board EMAC
37 *
38 * Returns the default on-board EMAC - this will be target-specific, and
39 * may not be available on all targets.
40 */
42
43 /**
44 * Callback to be register with EMAC interface and to be called for received packets
45 *
46 * <br>
47 * \c buf : Received data
48 */
49 typedef mbed::Callback<void (emac_mem_buf_t *buf)> emac_link_input_cb_t;
50
51 /**
52 * Callback to be registered with EMAC interface and to be called for link status changes.
53 *
54 * Note that duplicate callbacks (i.e. this being called twice when the network comes up)
55 * are possible in some cases.
56 *
57 * <br>
58 * \c up : Link status
59 */
61
62 /**
63 * Return maximum transmission unit
64 *
65 * @return MTU in bytes
66 */
67 virtual uint32_t get_mtu_size() const = 0;
68
69 /**
70 * Gets memory buffer alignment preference
71 *
72 * Gets preferred memory buffer alignment of the EMAC device. IP stack may or may not
73 * align link out memory buffer chains using the alignment.
74 *
75 * @return Memory alignment requirement in bytes
76 */
77 virtual uint32_t get_align_preference() const = 0;
78
79 /**
80 * Return interface name
81 *
82 * @param name Pointer to where the name should be written
83 * @param size Maximum number of character to copy
84 */
85 virtual void get_ifname(char *name, uint8_t size) const = 0;
86
87 /**
88 * Returns size of the underlying interface HW address size.
89 *
90 * @return HW address size in bytes
91 */
92 virtual uint8_t get_hwaddr_size() const = 0;
93
94 /**
95 * Return interface-supplied HW address
96 *
97 * Copies HW address to provided memory, \c addr has to be of correct size see #get_hwaddr_size
98 *
99 * HW address need not be provided if this interface does not have its own HW
100 * address configuration; stack will choose address from central system
101 * configuration if the function returns false and does not write to addr.
102 *
103 * @param addr HW address for underlying interface
104 * @return true if HW address is available
105 */
106 virtual bool get_hwaddr(uint8_t *addr) const = 0;
107
108 /**
109 * Set HW address for interface
110 *
111 * Provided address has to be of correct size, see @a get_hwaddr_size
112 *
113 * Called to set the MAC address to actually use - if @a get_hwaddr is provided
114 * the stack would normally use that, but it could be overridden, for example for test
115 * purposes.
116 *
117 * @param addr Address to be set
118 */
119 virtual void set_hwaddr(const uint8_t *addr) = 0;
120
121 /**
122 * Sends the packet over the link
123 *
124 * Will not be called from an interrupt context.
125 *
126 * @param buf Packet to be sent. This packet is now owned by the MAC and must be freed in all cases.
127 * @return True if the packet was sent successfully, False otherwise
128 */
129 virtual bool link_out(emac_mem_buf_t *buf) = 0;
130
131 /**
132 * Initializes the HW
133 *
134 * @return True on success, False in case of an error.
135 */
136 virtual bool power_up() = 0;
137
138 /**
139 * Deinitializes the HW
140 *
141 */
142 virtual void power_down() = 0;
143
144 /**
145 * Sets a callback that needs to be called for packets received for this interface.
146 *
147 * Note that the callback function will contain appropriate locking such that it may be called from any OS thread.
148 * However, it shall not be called from an interrupt.
149 *
150 * Also note that the callback will take ownership of the passed packet and must free it in all cases.
151 *
152 * @param input_cb Function to be registered as a callback
153 */
154 virtual void set_link_input_cb(emac_link_input_cb_t input_cb) = 0;
155
156 /**
157 * Sets a callback that needs to be called on link status changes for given interface
158 *
159 * Note that the callback function will contain appropriate locking such that it may be called from any OS thread.
160 * However, it shall not be called from an interrupt.
161 *
162 * @param state_cb Function to be registered as a callback
163 */
165
166 /** Add device to a multicast group
167 *
168 * @param address A multicast group hardware address
169 *
170 * @note The network stack will ensure that no two threads can call this function at
171 * the same time. It doesn't need locking at the MAC level.
172 */
173 virtual void add_multicast_group(const uint8_t *address) = 0;
174
175 /** Remove device from a multicast group
176 *
177 * @param address A multicast group hardware address
178 *
179 * @note The network stack will ensure that no two threads can call this function at
180 * the same time. It doesn't need locking at the MAC level.
181 */
182 virtual void remove_multicast_group(const uint8_t *address) = 0;
183
184 /** Request reception of all multicast packets
185 *
186 * @param all True to receive all multicasts
187 * False to receive only multicasts addressed to specified groups
188 */
189 virtual void set_all_multicast(bool all) = 0;
190
191 /** Sets memory manager that is used to handle memory buffers
192 *
193 * @param mem_mngr Pointer to memory manager
194 */
195 virtual void set_memory_manager(EMACMemoryManager &mem_mngr) = 0;
196};
197
198#endif /* EMAC_H */
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition EMAC.h:33
virtual void set_all_multicast(bool all)=0
Request reception of all multicast packets.
virtual uint8_t get_hwaddr_size() const =0
Returns size of the underlying interface HW address size.
mbed::Callback< void(bool up)> emac_link_state_change_cb_t
Callback to be registered with EMAC interface and to be called for link status changes.
Definition EMAC.h:60
mbed::Callback< void(emac_mem_buf_t *buf)> emac_link_input_cb_t
Callback to be register with EMAC interface and to be called for received packets.
Definition EMAC.h:49
virtual void set_hwaddr(const uint8_t *addr)=0
Set HW address for interface.
virtual bool power_up()=0
Initializes the HW.
static EMAC & get_default_instance()
Return the default on-board EMAC.
virtual void set_memory_manager(EMACMemoryManager &mem_mngr)=0
Sets memory manager that is used to handle memory buffers.
virtual bool get_hwaddr(uint8_t *addr) const =0
Return interface-supplied HW address.
virtual void get_ifname(char *name, uint8_t size) const =0
Return interface name.
virtual void set_link_state_cb(emac_link_state_change_cb_t state_cb)=0
Sets a callback that needs to be called on link status changes for given interface.
virtual uint32_t get_align_preference() const =0
Gets memory buffer alignment preference.
virtual bool link_out(emac_mem_buf_t *buf)=0
Sends the packet over the link.
virtual uint32_t get_mtu_size() const =0
Return maximum transmission unit.
virtual void remove_multicast_group(const uint8_t *address)=0
Remove device from a multicast group.
virtual void set_link_input_cb(emac_link_input_cb_t input_cb)=0
Sets a callback that needs to be called for packets received for this interface.
virtual void power_down()=0
Deinitializes the HW.
virtual void add_multicast_group(const uint8_t *address)=0
Add device to a multicast group.
Callback class based on template specialization.
Definition Callback.h:53