loopback_driver.c
Go to the documentation of this file.
1 /**
2  * @file loopback_driver.c
3  * @brief Loopback interface driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "loopback_driver.h"
37 #include "debug.h"
38 
39 //Loopback interface queue
41 
42 static uint_t queueLength;
43 static uint_t queueTxIndex;
44 static uint_t queueRxIndex;
45 
46 
47 /**
48  * @brief Loopback interface driver
49  **/
50 
52 {
54  ETH_MTU,
62  NULL,
63  NULL,
64  NULL,
65  FALSE,
66  FALSE,
67  FALSE,
68  FALSE
69 };
70 
71 
72 /**
73  * @brief Loopback interface initialization
74  * @param[in] interface Underlying network interface
75  * @return Error code
76  **/
77 
79 {
80  //Debug message
81  TRACE_INFO("Initializing loopback interface...\r\n");
82 
83  //Initialize variables
84  queueLength = 0;
85  queueTxIndex = 0;
86  queueRxIndex = 0;
87 
88  //Force the TCP/IP stack to poll the link state at startup
89  interface->nicEvent = TRUE;
91 
92  //The loopback interface is now ready to send
93  osSetEvent(&interface->nicTxEvent);
94 
95  //Successful initialization
96  return NO_ERROR;
97 }
98 
99 
100 /**
101  * @brief Loopback interface timer handler
102  *
103  * This routine is periodically called by the TCP/IP stack to handle periodic
104  * operations such as polling the link state
105  *
106  * @param[in] interface Underlying network interface
107  **/
108 
110 {
111  //Not implemented
112 }
113 
114 
115 /**
116  * @brief Enable interrupts
117  * @param[in] interface Underlying network interface
118  **/
119 
121 {
122  //Not implemented
123 }
124 
125 
126 /**
127  * @brief Disable interrupts
128  * @param[in] interface Underlying network interface
129  **/
130 
132 {
133  //Not implemented
134 }
135 
136 
137 /**
138  * @brief Loopback interface event handler
139  * @param[in] interface Underlying network interface
140  **/
141 
143 {
144  //Link up event is pending?
145  if(!interface->linkState)
146  {
147  //Link is up
148  interface->linkState = TRUE;
149  //Process link state change event
150  nicNotifyLinkChange(interface);
151  }
152 
153  //Read incoming packet
154  loopbackDriverReceivePacket(interface);
155 
156  //Check whether another packet is pending in the queue
157  if(queueLength > 0)
158  {
159  //Set event flag
160  interface->nicEvent = TRUE;
161  //Notify the TCP/IP stack of the event
163  }
164 }
165 
166 
167 /**
168  * @brief Send a packet
169  * @param[in] interface Underlying network interface
170  * @param[in] buffer Multi-part buffer containing the data to send
171  * @param[in] offset Offset to the first data byte
172  * @param[in] ancillary Additional options passed to the stack along with
173  * the packet
174  * @return Error code
175  **/
176 
178  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
179 {
180  error_t error;
181  size_t length;
182 
183  //Initialize status code
184  error = NO_ERROR;
185 
186  //Retrieve the length of the packet
187  length = netBufferGetLength(buffer) - offset;
188 
189  //Valid packet length?
190  if(length <= ETH_MTU)
191  {
192  //Check whether the queue is full
193  if(queueLength < LOOPBACK_DRIVER_QUEUE_SIZE)
194  {
195  //Retrieve the length of the packet
196  queue[queueTxIndex].length = netBufferGetLength(buffer) - offset;
197 
198  //Copy data to the queue
199  netBufferRead(queue[queueTxIndex].data, buffer,
200  offset, queue[queueTxIndex].length);
201 
202  //Increment index and wrap around if necessary
203  if(++queueTxIndex >= LOOPBACK_DRIVER_QUEUE_SIZE)
204  {
205  queueTxIndex = 0;
206  }
207 
208  //Update the length of the queue
209  queueLength++;
210 
211  //Set event flag
212  interface->nicEvent = TRUE;
213  //Notify the TCP/IP stack of the event
215  }
216  }
217  else
218  {
219  //Report an error
220  error = ERROR_INVALID_LENGTH;
221  }
222 
223  //The transmitter can accept another packet
224  osSetEvent(&interface->nicTxEvent);
225 
226  //Return status code
227  return error;
228 }
229 
230 
231 /**
232  * @brief Receive a packet
233  * @param[in] interface Underlying network interface
234  * @return Error code
235  **/
236 
238 {
239  error_t error;
240  NetRxAncillary ancillary;
241 
242  //Check whether a packet is pending in the queue
243  if(queueLength > 0)
244  {
245  //Additional options can be passed to the stack along with the packet
246  ancillary = NET_DEFAULT_RX_ANCILLARY;
247 
248  //Pass the packet to the upper layer
249  nicProcessPacket(interface, queue[queueRxIndex].data,
250  queue[queueRxIndex].length, &ancillary);
251 
252  //Increment index and wrap around if necessary
253  if(++queueRxIndex >= LOOPBACK_DRIVER_QUEUE_SIZE)
254  {
255  queueRxIndex = 0;
256  }
257 
258  //Update the length of the queue
259  queueLength--;
260 
261  //Packet successfully received
262  error = NO_ERROR;
263  }
264  else
265  {
266  //No more packet in the queue
267  error = ERROR_BUFFER_EMPTY;
268  }
269 
270  //Return status code
271  return error;
272 }
273 
274 
275 /**
276  * @brief Configure MAC address filtering
277  * @param[in] interface Underlying network interface
278  * @return Error code
279  **/
280 
282 {
283  //Not implemented
284  return NO_ERROR;
285 }
unsigned int uint_t
Definition: compiler_port.h:50
Debugging facilities.
#define TRACE_INFO(...)
Definition: debug.h:95
error_t
Error codes.
Definition: error.h:43
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
#define ETH_MTU
Definition: ethernet.h:116
uint8_t data[]
Definition: ethernet.h:222
void loopbackDriverEnableIrq(NetInterface *interface)
Enable interrupts.
error_t loopbackDriverInit(NetInterface *interface)
Loopback interface initialization.
const NicDriver loopbackDriver
Loopback interface driver.
error_t loopbackDriverSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
error_t loopbackDriverReceivePacket(NetInterface *interface)
Receive a packet.
void loopbackDriverDisableIrq(NetInterface *interface)
Disable interrupts.
void loopbackDriverEventHandler(NetInterface *interface)
Loopback interface event handler.
void loopbackDriverTick(NetInterface *interface)
Loopback interface timer handler.
error_t loopbackDriverUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Loopback interface driver.
#define LOOPBACK_DRIVER_QUEUE_SIZE
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:674
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:101
#define NetRxAncillary
Definition: net_misc.h:40
#define NetTxAncillary
Definition: net_misc.h:36
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:391
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:548
@ NIC_TYPE_LOOPBACK
Loopback interface.
Definition: nic.h:88
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Loopback interface queue entry.
size_t length
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
NIC driver.
Definition: nic.h:283
uint8_t length
Definition: tcp.h:368