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-2019 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 1.9.4
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
104  * handle periodic 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  * @return Error code
173  **/
174 
176  const NetBuffer *buffer, size_t offset)
177 {
178  error_t error;
179  size_t length;
180 
181  //Initialize status code
182  error = NO_ERROR;
183 
184  //Retrieve the length of the packet
185  length = netBufferGetLength(buffer) - offset;
186 
187  //Valid packet length?
188  if(length <= ETH_MTU)
189  {
190  //Check whether the queue is full
191  if(queueLength < LOOPBACK_DRIVER_QUEUE_SIZE)
192  {
193  //Retrieve the length of the packet
194  queue[queueTxIndex].length = netBufferGetLength(buffer) - offset;
195 
196  //Copy data to the queue
197  netBufferRead(queue[queueTxIndex].data, buffer,
198  offset, queue[queueTxIndex].length);
199 
200  //Increment index and wrap around if necessary
201  if(++queueTxIndex >= LOOPBACK_DRIVER_QUEUE_SIZE)
202  queueTxIndex = 0;
203 
204  //Update the length of the queue
205  queueLength++;
206 
207  //Set event flag
208  interface->nicEvent = TRUE;
209  //Notify the TCP/IP stack of the event
211  }
212  }
213  else
214  {
215  //Report an error
216  error = ERROR_INVALID_LENGTH;
217  }
218 
219  //The transmitter can accept another packet
220  osSetEvent(&interface->nicTxEvent);
221 
222  //Return status code
223  return error;
224 }
225 
226 
227 /**
228  * @brief Receive a packet
229  * @param[in] interface Underlying network interface
230  * @return Error code
231  **/
232 
234 {
235  error_t error;
236 
237  //Check whether a packet is pending in the queue
238  if(queueLength > 0)
239  {
240  //Pass the packet to the upper layer
241  nicProcessPacket(interface, queue[queueRxIndex].data,
242  queue[queueRxIndex].length);
243 
244  //Increment index and wrap around if necessary
245  if(++queueRxIndex >= LOOPBACK_DRIVER_QUEUE_SIZE)
246  queueRxIndex = 0;
247 
248  //Update the length of the queue
249  queueLength--;
250 
251  //Packet successfully received
252  error = NO_ERROR;
253  }
254  else
255  {
256  //No more packet in the queue
257  error = ERROR_BUFFER_EMPTY;
258  }
259 
260  //Return status code
261  return error;
262 }
263 
264 
265 /**
266  * @brief Configure MAC address filtering
267  * @param[in] interface Underlying network interface
268  * @return Error code
269  **/
270 
272 {
273  //Not implemented
274  return NO_ERROR;
275 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:536
size_t length
void loopbackDriverTick(NetInterface *interface)
Loopback interface timer handler.
TCP/IP stack core.
Debugging facilities.
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
Loopback interface queue entry.
#define TRUE
Definition: os_port.h:50
Loopback interface driver.
error_t loopbackDriverSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet.
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:672
NIC driver.
Definition: nic.h:179
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
#define TRACE_INFO(...)
Definition: debug.h:94
#define LOOPBACK_DRIVER_QUEUE_SIZE
#define ETH_MTU
Definition: ethernet.h:91
Success.
Definition: error.h:44
OsEvent netEvent
Definition: net.c:74
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t
Error codes.
Definition: error.h:42
Loopback interface.
Definition: nic.h:82
unsigned int uint_t
Definition: compiler_port.h:45
error_t loopbackDriverUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
uint8_t data[]
Definition: dtls_misc.h:169
#define NetInterface
Definition: net.h:36
error_t loopbackDriverInit(NetInterface *interface)
Loopback interface initialization.
const NicDriver loopbackDriver
Loopback interface driver.
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length)
Handle a packet received by the network controller.
Definition: nic.c:395
void loopbackDriverEnableIrq(NetInterface *interface)
Enable interrupts.
void loopbackDriverDisableIrq(NetInterface *interface)
Disable interrupts.
void loopbackDriverEventHandler(NetInterface *interface)
Loopback interface event handler.
error_t loopbackDriverReceivePacket(NetInterface *interface)
Receive a packet.
uint8_t length
Definition: dtls_misc.h:142
#define FALSE
Definition: os_port.h:46