net.c
Go to the documentation of this file.
1 /**
2  * @file net.c
3  * @brief TCP/IP stack core
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 <stdlib.h>
36 #include "core/net.h"
37 #include "core/socket.h"
38 #include "core/raw_socket.h"
39 #include "core/tcp_timer.h"
40 #include "core/tcp_misc.h"
41 #include "core/ethernet.h"
42 #include "ipv4/arp.h"
43 #include "ipv4/ipv4.h"
44 #include "ipv4/ipv4_routing.h"
45 #include "ipv4/auto_ip_misc.h"
46 #include "igmp/igmp_host.h"
47 #include "igmp/igmp_router.h"
48 #include "igmp/igmp_snooping.h"
49 #include "ipv6/ipv6.h"
50 #include "ipv6/ipv6_routing.h"
51 #include "ipv6/mld.h"
52 #include "ipv6/ndp.h"
53 #include "ipv6/ndp_router_adv.h"
54 #include "dhcp/dhcp_client_misc.h"
55 #include "dhcp/dhcp_server_misc.h"
57 #include "dns/dns_cache.h"
58 #include "dns/dns_client.h"
59 #include "mdns/mdns_client.h"
60 #include "mdns/mdns_responder.h"
61 #include "mdns/mdns_common.h"
62 #include "dns_sd/dns_sd.h"
63 #include "netbios/nbns_client.h"
64 #include "netbios/nbns_responder.h"
65 #include "netbios/nbns_common.h"
66 #include "llmnr/llmnr_responder.h"
67 #include "str.h"
68 #include "debug.h"
69 
70 #if (defined(WEB_SOCKET_SUPPORT) && WEB_SOCKET_SUPPORT == ENABLED)
71  #include "web_socket/web_socket.h"
72 #endif
73 
74 //TCP/IP stack context
76 
77 
78 /**
79  * @brief Initialize settings with default values
80  * @param[out] settings Structure that contains TCP/IP stack settings
81  **/
82 
84 {
85  //Default task parameters
86  settings->task = OS_TASK_DEFAULT_PARAMS;
88  settings->task.priority = NET_TASK_PRIORITY;
89 }
90 
91 
92 /**
93  * @brief Initialize TCP/IP stack (deprecated)
94  * @param[in] context Pointer to the TCP/IP stack context
95  * @param[in] settings TCP/IP stack specific settings
96  * @return Error code
97  **/
98 
100 {
101  error_t error;
102  NetSettings netSettings;
103 
104  //Get default settings
105  netGetDefaultSettings(&netSettings);
106 
107  //Initialize TCP/IP stack
108  error = netInitEx(&netContext, &netSettings);
109 
110  //Check status code
111  if(!error)
112  {
113  //Start TCP/IP stack
114  error = netStart(&netContext);
115  }
116 
117  //Return status code
118  return error;
119 }
120 
121 
122 /**
123  * @brief Initialize TCP/IP stack
124  * @param[in] context Pointer to the TCP/IP stack context
125  * @param[in] settings TCP/IP stack specific settings
126  * @return Error code
127  **/
128 
129 error_t netInitEx(NetContext *context, const NetSettings *settings)
130 {
131  error_t error;
132  uint_t i;
133  NetInterface *interface;
134 
135  //Clear TCP/IP stack context
136  osMemset(context, 0, sizeof(NetContext));
137 
138  //Initialize task parameters
139  context->taskParams = settings->task;
140  context->taskId = OS_INVALID_TASK_ID;
141 
142  //The TCP/IP process is currently suspended
144  //Get current time
146 
147  //Create a mutex to prevent simultaneous access to the TCP/IP stack
148  if(!osCreateMutex(&netMutex))
149  {
150  //Failed to create mutex
151  return ERROR_OUT_OF_RESOURCES;
152  }
153 
154  //Create a event object to receive notifications from device drivers
155  if(!osCreateEvent(&netEvent))
156  {
157  //Failed to create mutex
158  return ERROR_OUT_OF_RESOURCES;
159  }
160 
161  //Memory pool initialization
162  error = memPoolInit();
163  //Any error to report?
164  if(error)
165  return error;
166 
167  //Clear configuration data for each interface
168  osMemset(netInterface, 0, sizeof(netInterface));
169 
170  //Loop through network interfaces
171  for(i = 0; i < NET_INTERFACE_COUNT; i++)
172  {
173  //Point to the current interface
174  interface = &netInterface[i];
175 
176  //Default interface name
177  osSprintf(interface->name, "eth%u", i);
178 
179  //Zero-based index
180  interface->index = i;
181  //Unique number identifying the interface
182  interface->id = i;
183 
184 #if (ETH_SUPPORT == ENABLED)
185  //Default PHY address
186  interface->phyAddr = UINT8_MAX;
187 #endif
188 #if (TCP_SUPPORT == ENABLED)
189  //Default TCP initial retransmission timeout
190  interface->initialRto = TCP_INITIAL_RTO;
191 #endif
192  }
193 
194  //Socket related initialization
195  error = socketInit();
196  //Any error to report?
197  if(error)
198  return error;
199 
200 #if (defined(WEB_SOCKET_SUPPORT) && WEB_SOCKET_SUPPORT == ENABLED)
201  //WebSocket related initialization
202  webSocketInit();
203 #endif
204 
205 #if (IPV4_SUPPORT == ENABLED && IPV4_ROUTING_SUPPORT == ENABLED)
206  //Initialize IPv4 routing table
207  error = ipv4InitRouting();
208  //Any error to report?
209  if(error)
210  return error;
211 #endif
212 
213 #if (IPV6_SUPPORT == ENABLED && IPV6_ROUTING_SUPPORT == ENABLED)
214  //Initialize IPv6 routing table
215  error = ipv6InitRouting();
216  //Any error to report?
217  if(error)
218  return error;
219 #endif
220 
221 #if (UDP_SUPPORT == ENABLED)
222  //UDP related initialization
223  error = udpInit();
224  //Any error to report?
225  if(error)
226  return error;
227 #endif
228 
229 #if (TCP_SUPPORT == ENABLED)
230  //TCP related initialization
231  error = tcpInit();
232  //Any error to report?
233  if(error)
234  return error;
235 #endif
236 
237 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
238  NBNS_CLIENT_SUPPORT == ENABLED)
239  //DNS cache initialization
240  error = dnsInit();
241  //Any error to report?
242  if(error)
243  return error;
244 #endif
245 
246  //Initialize tick counters
247  nicTickCounter = 0;
248 
249 #if (PPP_SUPPORT == ENABLED)
250  pppTickCounter = 0;
251 #endif
252 #if (IPV4_SUPPORT == ENABLED && ETH_SUPPORT == ENABLED)
253  arpTickCounter = 0;
254 #endif
255 #if (IPV4_SUPPORT == ENABLED && IPV4_FRAG_SUPPORT == ENABLED)
257 #endif
258 #if (IPV4_SUPPORT == ENABLED && (IGMP_HOST_SUPPORT == ENABLED || \
259  IGMP_ROUTER_SUPPORT == ENABLED || IGMP_SNOOPING_SUPPORT == ENABLED))
260  igmpTickCounter = 0;
261 #endif
262 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
263  autoIpTickCounter = 0;
264 #endif
265 #if (IPV4_SUPPORT == ENABLED && DHCP_CLIENT_SUPPORT == ENABLED)
267 #endif
268 #if (IPV4_SUPPORT == ENABLED && DHCP_SERVER_SUPPORT == ENABLED)
270 #endif
271 #if (IPV6_SUPPORT == ENABLED && IPV6_FRAG_SUPPORT == ENABLED)
273 #endif
274 #if (IPV6_SUPPORT == ENABLED && MLD_SUPPORT == ENABLED)
275  mldTickCounter = 0;
276 #endif
277 #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED)
278  ndpTickCounter = 0;
279 #endif
280 #if (IPV6_SUPPORT == ENABLED && NDP_ROUTER_ADV_SUPPORT == ENABLED)
282 #endif
283 #if (IPV6_SUPPORT == ENABLED && DHCPV6_CLIENT_SUPPORT == ENABLED)
285 #endif
286 #if (TCP_SUPPORT == ENABLED)
287  tcpTickCounter = 0;
288 #endif
289 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
290  NBNS_CLIENT_SUPPORT == ENABLED)
291  dnsTickCounter = 0;
292 #endif
293 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
295 #endif
296 #if (DNS_SD_SUPPORT == ENABLED)
297  dnsSdTickCounter = 0;
298 #endif
299 
300  //Successful initialization
301  return NO_ERROR;
302 }
303 
304 
305 /**
306  * @brief Start TCP/IP stack
307  * @param[in] context Pointer to the TCP/IP stack context
308  * @return Error code
309  **/
310 
312 {
313  //Create a task
314  context->taskId = osCreateTask("TCP/IP", (OsTaskCode) netTaskEx, context,
315  &context->taskParams);
316 
317  //Unable to create the task?
318  if(context->taskId == OS_INVALID_TASK_ID)
319  return ERROR_OUT_OF_RESOURCES;
320 
321 #if (NET_RTOS_SUPPORT == DISABLED)
322  //The TCP/IP process is now running
324 #endif
325 
326  //Successful processing
327  return NO_ERROR;
328 }
329 
330 
331 /**
332  * @brief Seed the pseudo-random number generator
333  * @param[in] seed Pointer to the random seed
334  * @param[in] length Length of the random seed, in bytes
335  * @return Error code
336  **/
337 
338 error_t netSeedRand(const uint8_t *seed, size_t length)
339 {
340  size_t i;
341  size_t j;
342 
343  //Check parameters
344  if(seed == NULL || length == 0)
346 
347  //Get exclusive access
348  if(netTaskRunning)
349  {
351  }
352 
353  //Save random seed
354  for(i = 0, j = 0; i < NET_RAND_SEED_SIZE; i++)
355  {
356  //Copy current byte
357  netContext.randSeed[i] = seed[j];
358 
359  //Increment index and wrap around if necessary
360  if(++j >= length)
361  {
362  j = 0;
363  }
364  }
365 
366  //Initialize pseudo-random generator
367  netInitRand();
368 
369  //Release exclusive access
370  if(netTaskRunning)
371  {
373  }
374 
375  //Successful processing
376  return NO_ERROR;
377 }
378 
379 
380 /**
381  * @brief Generate a random 32-bit value
382  * @return Random value
383  **/
384 
385 uint32_t netGetRand(void)
386 {
387  uint32_t value;
388 
389  //Get exclusive access
390  if(netTaskRunning)
391  {
393  }
394 
395  //Generate a random 32-bit value
397 
398  //Release exclusive access
399  if(netTaskRunning)
400  {
402  }
403 
404  //Return the random value
405  return value;
406 }
407 
408 
409 /**
410  * @brief Generate a random value in the specified range
411  * @param[in] min Lower bound
412  * @param[in] max Upper bound
413  * @return Random value in the specified range
414  **/
415 
416 uint32_t netGetRandRange(uint32_t min, uint32_t max)
417 {
418  uint32_t value;
419 
420  //Get exclusive access
421  if(netTaskRunning)
422  {
424  }
425 
426  //Generate a random value in the specified range
427  value = netGenerateRandRange(min, max);
428 
429  //Release exclusive access
430  if(netTaskRunning)
431  {
433  }
434 
435  //Return the random value
436  return value;
437 }
438 
439 
440 /**
441  * @brief Get a string of random data
442  * @param[out] data Buffer where to store random data
443  * @param[in] length Number of random bytes to generate
444  **/
445 
446 void netGetRandData(uint8_t *data, size_t length)
447 {
448  //Get exclusive access
449  if(netTaskRunning)
450  {
452  }
453 
454  //Generate a random value in the specified range
456 
457  //Release exclusive access
458  if(netTaskRunning)
459  {
461  }
462 }
463 
464 
465 /**
466  * @brief Get default network interface
467  * @return Pointer to the default network interface to be used
468  **/
469 
471 {
472  //Default network interface
473  return &netInterface[0];
474 }
475 
476 
477 /**
478  * @brief Set MAC address
479  * @param[in] interface Pointer to the desired network interface
480  * @param[in] macAddr MAC address
481  * @return Error code
482  **/
483 
484 error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
485 {
486 #if (ETH_SUPPORT == ENABLED)
487  //Check parameters
488  if(interface == NULL || macAddr == NULL)
490 
491  //Get exclusive access
493 
494  //Set MAC address
495  interface->macAddr = *macAddr;
496 
497  //Generate the 64-bit interface identifier
498  macAddrToEui64(macAddr, &interface->eui64);
499 
500  //Release exclusive access
502 
503  //Successful processing
504  return NO_ERROR;
505 #else
506  //Not implemented
507  return ERROR_NOT_IMPLEMENTED;
508 #endif
509 }
510 
511 
512 /**
513  * @brief Retrieve MAC address
514  * @param[in] interface Pointer to the desired network interface
515  * @param[out] macAddr MAC address
516  * @return Error code
517  **/
518 
520 {
521 #if (ETH_SUPPORT == ENABLED)
522  NetInterface *logicalInterface;
523 
524  //Check parameters
525  if(interface == NULL || macAddr == NULL)
527 
528  //Get exclusive access
530 
531  //Point to the logical interface
532  logicalInterface = nicGetLogicalInterface(interface);
533 
534  //Get MAC address
535  *macAddr = logicalInterface->macAddr;
536 
537  //Release exclusive access
539 
540  //Successful processing
541  return NO_ERROR;
542 #else
543  //Not implemented
544  return ERROR_NOT_IMPLEMENTED;
545 #endif
546 }
547 
548 
549 /**
550  * @brief Set EUI-64 interface identifier
551  * @param[in] interface Pointer to the desired network interface
552  * @param[in] eui64 Interface identifier
553  * @return Error code
554  **/
555 
556 error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
557 {
558  //Check parameters
559  if(interface == NULL || eui64 == NULL)
561 
562  //Get exclusive access
564  //Set interface identifier
565  interface->eui64 = *eui64;
566  //Release exclusive access
568 
569  //Successful processing
570  return NO_ERROR;
571 }
572 
573 
574 /**
575  * @brief Retrieve EUI-64 interface identifier
576  * @param[in] interface Pointer to the desired network interface
577  * @param[out] eui64 Interface identifier
578  * @return Error code
579  **/
580 
582 {
583  NetInterface *logicalInterface;
584 
585  //Check parameters
586  if(interface == NULL || eui64 == NULL)
588 
589  //Get exclusive access
591 
592  //Point to the logical interface
593  logicalInterface = nicGetLogicalInterface(interface);
594 
595  //Get interface identifier
596  *eui64 = logicalInterface->eui64;
597 
598  //Release exclusive access
600 
601  //Successful processing
602  return NO_ERROR;
603 }
604 
605 
606 /**
607  * @brief Set interface identifier
608  * @param[in] interface Pointer to the desired network interface
609  * @param[in] id Unique number identifying the interface
610  * @return Error code
611  **/
612 
613 error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
614 {
615  //Check parameters
616  if(interface == NULL)
618 
619  //Get exclusive access
621  //Set interface identifier
622  interface->id = id;
623  //Release exclusive access
625 
626  //Successful processing
627  return NO_ERROR;
628 }
629 
630 
631 /**
632  * @brief Set interface name
633  * @param[in] interface Pointer to the desired network interface
634  * @param[in] name NULL-terminated string that contains the interface name
635  * @return Error code
636  **/
637 
639 {
640  //Check parameters
641  if(interface == NULL || name == NULL)
643 
644  //Make sure the length of the interface name is acceptable
646  return ERROR_INVALID_LENGTH;
647 
648  //Get exclusive access
650  //Set interface name
651  osStrcpy(interface->name, name);
652  //Release exclusive access
654 
655  //Successful processing
656  return NO_ERROR;
657 }
658 
659 
660 /**
661  * @brief Set host name
662  * @param[in] interface Pointer to the desired network interface
663  * @param[in] name NULL-terminated string that contains the host name
664  * @return Error code
665  **/
666 
668 {
669  //Check parameters
670  if(interface == NULL || name == NULL)
672 
673  //Make sure the length of the host name is acceptable
675  return ERROR_INVALID_LENGTH;
676 
677  //Get exclusive access
679  //Set host name
680  osStrcpy(interface->hostname, name);
681  //Release exclusive access
683 
684  //Successful processing
685  return NO_ERROR;
686 }
687 
688 
689 /**
690  * @brief Specify VLAN identifier (802.1Q)
691  * @param[in] interface Pointer to the desired network interface
692  * @param[in] vlanId VLAN identifier
693  * @return Error code
694  **/
695 
696 error_t netSetVlanId(NetInterface *interface, uint16_t vlanId)
697 {
698 #if (ETH_VLAN_SUPPORT == ENABLED)
699  //Make sure the network interface is valid
700  if(interface == NULL)
702 
703  //The VID value FFF is reserved
704  if((vlanId & VLAN_VID_MASK) == VLAN_VID_MASK)
706 
707  //Get exclusive access
709  //Set VLAN identifier
710  interface->vlanId = vlanId;
711  //Release exclusive access
713 
714  //Successful processing
715  return NO_ERROR;
716 #else
717  //Not implemented
718  return ERROR_NOT_IMPLEMENTED;
719 #endif
720 }
721 
722 
723 /**
724  * @brief Specify VMAN identifier (802.1ad)
725  * @param[in] interface Pointer to the desired network interface
726  * @param[in] vmanId VMAN identifier
727  * @return Error code
728  **/
729 
730 error_t netSetVmanId(NetInterface *interface, uint16_t vmanId)
731 {
732 #if (ETH_VMAN_SUPPORT == ENABLED)
733  //Make sure the network interface is valid
734  if(interface == NULL)
736 
737  //The VID value FFF is reserved
738  if((vmanId & VLAN_VID_MASK) == VLAN_VID_MASK)
740 
741  //Get exclusive access
743  //Set VMAN identifier
744  interface->vmanId = vmanId;
745  //Release exclusive access
747 
748  //Successful processing
749  return NO_ERROR;
750 #else
751  //Not implemented
752  return ERROR_NOT_IMPLEMENTED;
753 #endif
754 }
755 
756 
757 /**
758  * @brief Attach a virtual interface to a given physical interface
759  * @param[in] interface Pointer to the virtual interface
760  * @param[in] physicalInterface physical interface on top of which the virtual
761  * interface will run
762  * @return Error code
763  **/
764 
766  NetInterface *physicalInterface)
767 {
768 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_VLAN_SUPPORT == ENABLED || \
769  ETH_PORT_TAGGING_SUPPORT == ENABLED)
770  //Make sure the network interface is valid
771  if(interface == NULL)
773 
774  //Get exclusive access
776  //Bind the virtual interface to the physical interface
777  interface->parent = physicalInterface;
778  //Release exclusive access
780 
781  //Successful processing
782  return NO_ERROR;
783 #else
784  //Not implemented
785  return ERROR_NOT_IMPLEMENTED;
786 #endif
787 }
788 
789 
790 /**
791  * @brief Set Ethernet MAC driver
792  * @param[in] interface Pointer to the desired network interface
793  * @param[in] driver Ethernet MAC driver
794  * @return Error code
795  **/
796 
797 error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
798 {
799  //Check parameters
800  if(interface == NULL || driver == NULL)
802 
803  //Get exclusive access
805  //Set Ethernet MAC driver
806  interface->nicDriver = driver;
807  //Release exclusive access
809 
810  //Successful processing
811  return NO_ERROR;
812 }
813 
814 
815 /**
816  * @brief Set Ethernet PHY driver
817  * @param[in] interface Pointer to the desired network interface
818  * @param[in] driver Ethernet PHY driver (can be NULL for MAC + PHY controller)
819  * @return Error code
820  **/
821 
822 error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
823 {
824 #if (ETH_SUPPORT == ENABLED)
825  //Check parameters
826  if(interface == NULL || driver == NULL)
828 
829  //Get exclusive access
831  //Set Ethernet PHY driver
832  interface->phyDriver = driver;
833  //Release exclusive access
835 
836  //Successful processing
837  return NO_ERROR;
838 #else
839  //Not implemented
840  return ERROR_NOT_IMPLEMENTED;
841 #endif
842 }
843 
844 
845 /**
846  * @brief Specify Ethernet PHY address
847  * @param[in] interface Pointer to the desired network interface
848  * @param[in] phyAddr PHY address
849  * @return Error code
850  **/
851 
852 error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
853 {
854 #if (ETH_SUPPORT == ENABLED)
855  //Make sure the network interface is valid
856  if(interface == NULL)
858 
859  //Make sure the PHY address is valid
860  if(phyAddr >= 32)
861  return ERROR_OUT_OF_RANGE;
862 
863  //Get exclusive access
865  //Set PHY address
866  interface->phyAddr = phyAddr;
867  //Release exclusive access
869 
870  //Successful processing
871  return NO_ERROR;
872 #else
873  //Not implemented
874  return ERROR_NOT_IMPLEMENTED;
875 #endif
876 }
877 
878 
879 /**
880  * @brief Set Ethernet switch driver
881  * @param[in] interface Pointer to the desired network interface
882  * @param[in] driver Ethernet switch driver
883  * @return Error code
884  **/
885 
887 {
888 #if (ETH_SUPPORT == ENABLED)
889  //Check parameters
890  if(interface == NULL || driver == NULL)
892 
893  //Get exclusive access
895  //Set Ethernet switch driver
896  interface->switchDriver = driver;
897  //Release exclusive access
899 
900  //Successful processing
901  return NO_ERROR;
902 #else
903  //Not implemented
904  return ERROR_NOT_IMPLEMENTED;
905 #endif
906 }
907 
908 
909 /**
910  * @brief Specify switch port
911  * @param[in] interface Pointer to the desired network interface
912  * @param[in] port Switch port identifier
913  * @return Error code
914  **/
915 
917 {
918 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
919  //Make sure the network interface is valid
920  if(interface == NULL)
922 
923  //Get exclusive access
925  //Set switch port identifier
926  interface->port = port;
927  //Release exclusive access
929 
930  //Successful processing
931  return NO_ERROR;
932 #else
933  //Not implemented
934  return ERROR_NOT_IMPLEMENTED;
935 #endif
936 }
937 
938 
939 /**
940  * @brief Set SMI driver
941  * @param[in] interface Pointer to the desired network interface
942  * @param[in] driver Underlying SMI driver
943  * @return Error code
944  **/
945 
946 error_t netSetSmiDriver(NetInterface *interface, const SmiDriver *driver)
947 {
948 #if (ETH_SUPPORT == ENABLED)
949  //Check parameters
950  if(interface == NULL || driver == NULL)
952 
953  //Get exclusive access
955  //Set SMI driver
956  interface->smiDriver = driver;
957  //Release exclusive access
959 
960  //Successful processing
961  return NO_ERROR;
962 #else
963  //Not implemented
964  return ERROR_NOT_IMPLEMENTED;
965 #endif
966 }
967 
968 
969 /**
970  * @brief Set SPI driver
971  * @param[in] interface Pointer to the desired network interface
972  * @param[in] driver Underlying SPI driver
973  * @return Error code
974  **/
975 
976 error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
977 {
978  //Check parameters
979  if(interface == NULL || driver == NULL)
981 
982  //Get exclusive access
984  //Set SPI driver
985  interface->spiDriver = driver;
986  //Release exclusive access
988 
989  //Successful processing
990  return NO_ERROR;
991 }
992 
993 
994 /**
995  * @brief Set UART driver
996  * @param[in] interface Pointer to the desired network interface
997  * @param[in] driver Underlying UART driver
998  * @return Error code
999  **/
1000 
1002 {
1003  //Check parameters
1004  if(interface == NULL || driver == NULL)
1005  return ERROR_INVALID_PARAMETER;
1006 
1007  //Get exclusive access
1009  //Set UART driver
1010  interface->uartDriver = driver;
1011  //Release exclusive access
1013 
1014  //Successful processing
1015  return NO_ERROR;
1016 }
1017 
1018 
1019 /**
1020  * @brief Set external interrupt line driver
1021  * @param[in] interface Pointer to the desired network interface
1022  * @param[in] driver Underlying SPI driver
1023  * @return Error code
1024  **/
1025 
1027 {
1028  //Check parameters
1029  if(interface == NULL || driver == NULL)
1030  return ERROR_INVALID_PARAMETER;
1031 
1032  //Get exclusive access
1034  //Set external interrupt line driver
1035  interface->extIntDriver = driver;
1036  //Release exclusive access
1038 
1039  //Successful processing
1040  return NO_ERROR;
1041 }
1042 
1043 
1044 /**
1045  * @brief Set administrative link state
1046  * @param[in] interface Pointer to the desired network interface
1047  * @param[in] linkState Administrative link state (up, down or auto)
1048  * @return Error code
1049  **/
1050 
1052 {
1053  //Make sure the network interface is valid
1054  if(interface == NULL)
1055  return ERROR_INVALID_PARAMETER;
1056 
1057  //Get exclusive access
1059 
1060  //Any change detected?
1061  if(linkState != interface->linkState)
1062  {
1063  //Update link state
1064  interface->linkState = linkState;
1065  //Process link state change event
1066  netProcessLinkChange(interface);
1067  }
1068 
1069  //Release exclusive access
1071 
1072  //Successful processing
1073  return NO_ERROR;
1074 }
1075 
1076 
1077 /**
1078  * @brief Get link state
1079  * @param[in] interface Pointer to the desired network interface
1080  * @return Link state
1081  **/
1082 
1084 {
1085  bool_t linkState;
1086 
1087  //Make sure the network interface is valid
1088  if(interface != NULL)
1089  {
1090  //Get exclusive access
1092  //Retrieve link state
1093  linkState = interface->linkState;
1094  //Release exclusive access
1096  }
1097  else
1098  {
1099  //Unknown link state
1100  linkState = FALSE;
1101  }
1102 
1103  //Return link state
1104  return linkState;
1105 }
1106 
1107 
1108 /**
1109  * @brief Get link speed
1110  * @param[in] interface Pointer to the desired network interface
1111  * @return Link speed
1112  **/
1113 
1115 {
1116  uint_t linkSpeed;
1117 
1118  //Make sure the network interface is valid
1119  if(interface != NULL)
1120  {
1121  //Get exclusive access
1123  //Retrieve link speed
1124  linkSpeed = interface->linkSpeed;
1125  //Release exclusive access
1127  }
1128  else
1129  {
1130  //Unknown link speed
1131  linkSpeed = NIC_LINK_SPEED_UNKNOWN;
1132  }
1133 
1134  //Return link speed
1135  return linkSpeed;
1136 }
1137 
1138 
1139 /**
1140  * @brief Get duplex mode
1141  * @param[in] interface Pointer to the desired network interface
1142  * @return Duplex mode
1143  **/
1144 
1146 {
1147  NicDuplexMode duplexMode;
1148 
1149  //Make sure the network interface is valid
1150  if(interface != NULL)
1151  {
1152  //Get exclusive access
1154  //Retrieve duplex mode
1155  duplexMode = interface->duplexMode;
1156  //Release exclusive access
1158  }
1159  else
1160  {
1161  //Unknown duplex mode
1162  duplexMode = NIC_UNKNOWN_DUPLEX_MODE;
1163  }
1164 
1165  //Return duplex mode
1166  return duplexMode;
1167 }
1168 
1169 
1170 /**
1171  * @brief Enable promiscuous mode
1172  * @param[in] interface Pointer to the desired network interface
1173  * @param[in] enable Enable or disable promiscuous mode
1174  * @return Error code
1175  **/
1176 
1178 {
1179  //Make sure the network interface is valid
1180  if(interface == NULL)
1181  return ERROR_INVALID_PARAMETER;
1182 
1183 #if (ETH_SUPPORT == ENABLED)
1184  //Get exclusive access
1186  //Enable or disable promiscuous mode
1187  interface->promiscuous = enable;
1188  //Release exclusive access
1190 #endif
1191 
1192  //Successful processing
1193  return NO_ERROR;
1194 }
1195 
1196 
1197 /**
1198  * @brief Configure network interface
1199  * @param[in] interface Network interface to configure
1200  * @return Error code
1201  **/
1202 
1204 {
1205  error_t error;
1206 
1207  //Make sure the network interface is valid
1208  if(interface == NULL)
1209  return ERROR_INVALID_PARAMETER;
1210 
1211  //Get exclusive access
1213 
1214  //Disable hardware interrupts
1215  if(interface->nicDriver != NULL)
1216  interface->nicDriver->disableIrq(interface);
1217 
1218  //Start of exception handling block
1219  do
1220  {
1221  //Receive notifications when the transmitter is ready to send
1222  if(!osCreateEvent(&interface->nicTxEvent))
1223  {
1224  //Failed to create event object
1225  error = ERROR_OUT_OF_RESOURCES;
1226  //Stop immediately
1227  break;
1228  }
1229 
1230  //Valid NIC driver?
1231  if(interface->nicDriver != NULL)
1232  {
1233  //Network controller initialization
1234  error = interface->nicDriver->init(interface);
1235  //Any error to report?
1236  if(error)
1237  break;
1238  }
1239  else
1240  {
1241 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_PORT_TAGGING_SUPPORT == ENABLED)
1242  NetInterface *physicalInterface;
1243 
1244  //Point to the physical interface
1245  physicalInterface = nicGetPhysicalInterface(interface);
1246 
1247  //Check whether the network interface is a virtual interface
1248  if(physicalInterface != interface)
1249  {
1250  //Valid MAC address assigned to the virtual interface?
1251  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1252  {
1253  //Configure the physical interface to accept the MAC address of
1254  //the virtual interface
1255  error = ethAcceptMacAddr(physicalInterface, &interface->macAddr);
1256  //Any error to report?
1257  if(error)
1258  break;
1259  }
1260  }
1261 #endif
1262  }
1263 
1264 #if (ETH_SUPPORT == ENABLED)
1265  //Ethernet related initialization
1266  error = ethInit(interface);
1267  //Any error to report?
1268  if(error)
1269  break;
1270 #endif
1271 
1272 #if (IPV4_SUPPORT == ENABLED)
1273  //IPv4 initialization
1274  error = ipv4Init(interface);
1275  //Any error to report?
1276  if(error)
1277  break;
1278 
1279 #if (ETH_SUPPORT == ENABLED)
1280  //ARP cache initialization
1281  error = arpInit(interface);
1282  //Any error to report?
1283  if(error)
1284  break;
1285 #endif
1286 
1287 #if (IPV4_SUPPORT == ENABLED && (IGMP_HOST_SUPPORT == ENABLED || \
1288  IGMP_ROUTER_SUPPORT == ENABLED || IGMP_SNOOPING_SUPPORT == ENABLED))
1289  //IGMP related initialization
1290  error = igmpInit(interface);
1291  //Any error to report?
1292  if(error)
1293  break;
1294 #endif
1295 
1296 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
1297  //NetBIOS Name Service related initialization
1298  error = nbnsInit(interface);
1299  //Any error to report?
1300  if(error)
1301  break;
1302 #endif
1303 #endif
1304 
1305 #if (IPV6_SUPPORT == ENABLED)
1306  //IPv6 initialization
1307  error = ipv6Init(interface);
1308  //Any error to report?
1309  if(error)
1310  break;
1311 
1312 #if (NDP_SUPPORT == ENABLED)
1313  //NDP related initialization
1314  error = ndpInit(interface);
1315  //Any error to report?
1316  if(error)
1317  break;
1318 #endif
1319 
1320 #if (MLD_SUPPORT == ENABLED)
1321  //MLD related initialization
1322  error = mldInit(interface);
1323  //Any error to report?
1324  if(error)
1325  break;
1326 #endif
1327 
1328  //Join the All-Nodes multicast address
1330  //Any error to report?
1331  if(error)
1332  break;
1333 #endif
1334 
1335 #if (MDNS_CLIENT_SUPPORT == ENABLED || MDNS_RESPONDER_SUPPORT == ENABLED)
1336  //mDNS related initialization
1337  error = mdnsInit(interface);
1338  //Any error to report?
1339  if(error)
1340  break;
1341 #endif
1342 
1343 #if (LLMNR_RESPONDER_SUPPORT == ENABLED)
1344  //LLMNR responder initialization
1345  error = llmnrResponderInit(interface);
1346  //Any error to report?
1347  if(error)
1348  break;
1349 #endif
1350 
1351  //End of exception handling block
1352  } while(0);
1353 
1354  //Check status code
1355  if(!error)
1356  {
1357  //Initialize pseudo-random generator
1358  netInitRand();
1359 
1360  //The network interface is now fully configured
1361  interface->configured = TRUE;
1362 
1363  //Check whether the TCP/IP process is running
1364  if(netTaskRunning)
1365  {
1366  //Interrupts can be safely enabled
1367  if(interface->nicDriver != NULL)
1368  interface->nicDriver->enableIrq(interface);
1369  }
1370  }
1371  else
1372  {
1373  //Clean up side effects before returning
1374  osDeleteEvent(&interface->nicTxEvent);
1375  }
1376 
1377  //Release exclusive access
1379 
1380  //Return status code
1381  return error;
1382 }
1383 
1384 
1385 /**
1386  * @brief Start network interface
1387  * @param[in] interface Network interface to start
1388  * @return Error code
1389  **/
1390 
1392 {
1393  error_t error;
1394 
1395  //Make sure the network interface is valid
1396  if(interface == NULL)
1397  return ERROR_INVALID_PARAMETER;
1398 
1399  //Initialize status code
1400  error = NO_ERROR;
1401 
1402  //Get exclusive access
1404 
1405 #if (ETH_SUPPORT == ENABLED)
1406  //Check whether the interface is enabled for operation
1407  if(!interface->configured)
1408  {
1409  NetInterface *physicalInterface;
1410 
1411  //Point to the physical interface
1412  physicalInterface = nicGetPhysicalInterface(interface);
1413 
1414  //Virtual interface?
1415  if(interface != physicalInterface)
1416  {
1417  //Valid MAC address assigned to the virtual interface?
1418  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1419  {
1420  //Configure the physical interface to accept the MAC address of
1421  //the virtual interface
1422  error = ethAcceptMacAddr(physicalInterface, &interface->macAddr);
1423  }
1424  }
1425  else
1426  {
1427 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
1428  //Valid switch driver?
1429  if(interface->switchDriver != NULL &&
1430  interface->switchDriver->init != NULL)
1431  {
1432  //Reconfigure switch operation
1433  error = interface->switchDriver->init(interface);
1434  }
1435 #endif
1436  //Check status code
1437  if(!error)
1438  {
1439  //Update the MAC filter
1440  error = nicUpdateMacAddrFilter(interface);
1441  }
1442  }
1443  }
1444 #endif
1445 
1446  //Enable network interface
1447  interface->configured = TRUE;
1448 
1449  //Check whether the TCP/IP process is running
1450  if(netTaskRunning)
1451  {
1452  //Interrupts can be safely enabled
1453  if(interface->nicDriver != NULL)
1454  interface->nicDriver->enableIrq(interface);
1455  }
1456 
1457  //Release exclusive access
1459 
1460  //Return status code
1461  return error;
1462 }
1463 
1464 
1465 /**
1466  * @brief Stop network interface
1467  * @param[in] interface Network interface to stop
1468  * @return Error code
1469  **/
1470 
1472 {
1473  NetInterface *physicalInterface;
1474 
1475  //Make sure the network interface is valid
1476  if(interface == NULL)
1477  return ERROR_INVALID_PARAMETER;
1478 
1479  //Get exclusive access
1481 
1482  //Point to the physical interface
1483  physicalInterface = nicGetPhysicalInterface(interface);
1484 
1485  //Check whether the interface is enabled for operation
1486  if(interface->configured)
1487  {
1488  //Update link state
1489  interface->linkState = FALSE;
1490  //Process link state change event
1491  netProcessLinkChange(interface);
1492 
1493  //Disable hardware interrupts
1494  if(interface->nicDriver != NULL)
1495  interface->nicDriver->disableIrq(interface);
1496 
1497  //Disable network interface
1498  interface->configured = FALSE;
1499 
1500  //Virtual interface?
1501  if(interface != physicalInterface)
1502  {
1503 #if (ETH_SUPPORT == ENABLED)
1504  //Valid MAC address assigned to the virtual interface?
1505  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1506  {
1507  //Drop the corresponding address from the MAC filter table of
1508  //the physical interface
1509  ethDropMacAddr(physicalInterface, &interface->macAddr);
1510  }
1511 #endif
1512  }
1513  }
1514 
1515  //Release exclusive access
1517 
1518  //Successful operation
1519  return NO_ERROR;
1520 }
1521 
1522 
1523 /**
1524  * @brief TCP/IP events handling (deprecated)
1525  **/
1526 
1527 void netTask(void)
1528 {
1530 }
1531 
1532 
1533 /**
1534  * @brief TCP/IP events handling
1535  * @param[in] context Pointer to the TCP/IP stack context
1536  **/
1537 
1538 void netTaskEx(NetContext *context)
1539 {
1540  uint_t i;
1541  bool_t status;
1542  systime_t time;
1543  systime_t timeout;
1544  NetInterface *interface;
1545 
1546 #if (NET_RTOS_SUPPORT == ENABLED)
1547  //Task prologue
1548  osEnterTask();
1549 
1550  //Get exclusive access
1552 
1553  //The TCP/IP process is now running
1554  netTaskRunning = TRUE;
1555 
1556  //Loop through network interfaces
1557  for(i = 0; i < NET_INTERFACE_COUNT; i++)
1558  {
1559  //Point to the current network interface
1560  interface = &netInterface[i];
1561 
1562  //Check whether the interface is fully configured
1563  if(interface->configured)
1564  {
1565  //Interrupts can be safely enabled
1566  if(interface->nicDriver != NULL)
1567  {
1568  interface->nicDriver->enableIrq(interface);
1569  }
1570  }
1571  }
1572 
1573  //Release exclusive access
1575 
1576  //Main loop
1577  while(1)
1578  {
1579 #endif
1580  //Get current time
1581  time = osGetSystemTime();
1582 
1583  //Compute the maximum blocking time when waiting for an event
1584  if(timeCompare(time, netTimestamp) < 0)
1585  {
1586  timeout = netTimestamp - time;
1587  }
1588  else
1589  {
1590  timeout = 0;
1591  }
1592 
1593  //Receive notifications when a frame has been received, or the
1594  //link state of any network interfaces has changed
1595  status = osWaitForEvent(&netEvent, timeout);
1596 
1597  //Check whether the specified event is in signaled state
1598  if(status)
1599  {
1600  //Get exclusive access
1602 
1603  //Process events
1604  for(i = 0; i < NET_INTERFACE_COUNT; i++)
1605  {
1606  //Point to the current network interface
1607  interface = &netInterface[i];
1608 
1609  //Check whether a NIC event is pending
1610  if(interface->nicEvent)
1611  {
1612  //Acknowledge the event by clearing the flag
1613  interface->nicEvent = FALSE;
1614 
1615  //Valid NIC driver?
1616  if(interface->nicDriver != NULL)
1617  {
1618  //Disable hardware interrupts
1619  interface->nicDriver->disableIrq(interface);
1620  //Handle NIC events
1621  interface->nicDriver->eventHandler(interface);
1622  //Re-enable hardware interrupts
1623  interface->nicDriver->enableIrq(interface);
1624  }
1625  }
1626 
1627 #if (ETH_SUPPORT == ENABLED)
1628  //Check whether a PHY event is pending
1629  if(interface->phyEvent)
1630  {
1631  //Acknowledge the event by clearing the flag
1632  interface->phyEvent = FALSE;
1633 
1634  //Valid NIC driver?
1635  if(interface->nicDriver != NULL)
1636  {
1637  //Disable hardware interrupts
1638  interface->nicDriver->disableIrq(interface);
1639 
1640  //Valid Ethernet PHY or switch driver?
1641  if(interface->phyDriver != NULL)
1642  {
1643  //Handle events
1644  interface->phyDriver->eventHandler(interface);
1645  }
1646  else if(interface->switchDriver != NULL)
1647  {
1648  //Handle events
1649  interface->switchDriver->eventHandler(interface);
1650  }
1651  else
1652  {
1653  //The interface is not properly configured
1654  }
1655 
1656  //Re-enable hardware interrupts
1657  interface->nicDriver->enableIrq(interface);
1658  }
1659  }
1660 #endif
1661  }
1662 
1663  //Release exclusive access
1665  }
1666 
1667  //Get current time
1668  time = osGetSystemTime();
1669 
1670  //Check current time
1671  if(timeCompare(time, netTimestamp) >= 0)
1672  {
1673  //Get exclusive access
1675  //Handle periodic operations
1676  netTick();
1677  //Release exclusive access
1679 
1680  //Next event
1682  }
1683 #if (NET_RTOS_SUPPORT == ENABLED)
1684  }
1685 #endif
1686 }
systime_t arpTickCounter
Definition: arp.c:51
error_t arpInit(NetInterface *interface)
ARP cache initialization.
Definition: arp.c:60
ARP (Address Resolution Protocol)
systime_t autoIpTickCounter
Definition: auto_ip_misc.c:47
Helper functions for Auto-IP.
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
systime_t dhcpClientTickCounter
Helper functions for DHCP client.
systime_t dhcpServerTickCounter
Helper functions for DHCP server.
systime_t dhcpv6ClientTickCounter
Helper functions for DHCPv6 client.
uint32_t time
error_t dnsInit(void)
DNS cache initialization.
Definition: dns_cache.c:60
systime_t dnsTickCounter
Definition: dns_cache.c:50
DNS cache management.
DNS client (Domain Name System)
uint16_t port
Definition: dns_common.h:267
systime_t dnsSdTickCounter
Definition: dns_sd.c:54
DNS-SD (DNS-Based Service Discovery)
error_t
Error codes.
Definition: error.h:43
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
@ ERROR_OUT_OF_RANGE
Definition: error.h:137
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:664
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
error_t ethInit(NetInterface *interface)
Ethernet related initialization.
Definition: ethernet.c:64
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:944
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:594
Ethernet.
Eui64
Definition: ethernet.h:210
uint8_t data[]
Definition: ethernet.h:222
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
MacAddr
Definition: ethernet.h:195
#define VLAN_VID_MASK
Definition: ethernet.h:124
error_t igmpInit(NetInterface *interface)
IGMP initialization.
Definition: igmp_common.c:59
systime_t igmpTickCounter
Definition: igmp_common.c:50
IGMP host.
IGMP router.
IGMP snooping switch.
error_t ipv4Init(NetInterface *interface)
IPv4 related initialization.
Definition: ipv4.c:79
IPv4 (Internet Protocol Version 4)
systime_t ipv4FragTickCounter
Definition: ipv4_frag.c:57
IPv4 routing.
error_t ipv4InitRouting(void)
error_t ipv6Init(NetInterface *interface)
IPv6 related initialization.
Definition: ipv6.c:95
const Ipv6Addr IPV6_LINK_LOCAL_ALL_NODES_ADDR
Definition: ipv6.c:73
error_t ipv6JoinMulticastGroup(NetInterface *interface, const Ipv6Addr *groupAddr)
Join an IPv6 multicast group.
Definition: ipv6.c:2035
IPv6 (Internet Protocol Version 6)
systime_t ipv6FragTickCounter
Definition: ipv6_frag.c:47
error_t ipv6InitRouting(void)
Initialize IPv6 routing table.
Definition: ipv6_routing.c:57
IPv6 routing.
error_t llmnrResponderInit(NetInterface *interface)
LLMNR responder initialization.
LLMNR responder (Link-Local Multicast Name Resolution)
mDNS client (Multicast DNS)
error_t mdnsInit(NetInterface *interface)
mDNS related initialization
Definition: mdns_common.c:67
Definitions common to mDNS client and mDNS responder.
systime_t mdnsResponderTickCounter
mDNS responder (Multicast DNS)
error_t mldInit(NetInterface *interface)
MLD initialization.
Definition: mld.c:65
systime_t mldTickCounter
Definition: mld.c:56
MLD (Multicast Listener Discovery for IPv6)
NBNS client (NetBIOS Name Service)
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:53
Definitions common to NBNS client and NBNS responder.
NBNS responder (NetBIOS Name Service)
error_t ndpInit(NetInterface *interface)
Neighbor cache initialization.
Definition: ndp.c:68
systime_t ndpTickCounter
Definition: ndp.c:59
NDP (Neighbor Discovery Protocol)
Router advertisement service.
systime_t ndpRouterAdvTickCounter
bool_t netGetLinkState(NetInterface *interface)
Get link state.
Definition: net.c:1083
void netGetDefaultSettings(NetSettings *settings)
Initialize settings with default values.
Definition: net.c:83
void netTaskEx(NetContext *context)
TCP/IP events handling.
Definition: net.c:1538
error_t netSetParentInterface(NetInterface *interface, NetInterface *physicalInterface)
Attach a virtual interface to a given physical interface.
Definition: net.c:765
NetContext netContext
Definition: net.c:75
error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
Set Ethernet MAC driver.
Definition: net.c:797
error_t netSetSmiDriver(NetInterface *interface, const SmiDriver *driver)
Set SMI driver.
Definition: net.c:946
error_t netSeedRand(const uint8_t *seed, size_t length)
Seed the pseudo-random number generator.
Definition: net.c:338
error_t netInitEx(NetContext *context, const NetSettings *settings)
Initialize TCP/IP stack.
Definition: net.c:129
error_t netEnablePromiscuousMode(NetInterface *interface, bool_t enable)
Enable promiscuous mode.
Definition: net.c:1177
void netGetRandData(uint8_t *data, size_t length)
Get a string of random data.
Definition: net.c:446
error_t netSetInterfaceName(NetInterface *interface, const char_t *name)
Set interface name.
Definition: net.c:638
error_t netSetExtIntDriver(NetInterface *interface, const ExtIntDriver *driver)
Set external interrupt line driver.
Definition: net.c:1026
error_t netSetVlanId(NetInterface *interface, uint16_t vlanId)
Specify VLAN identifier (802.1Q)
Definition: net.c:696
error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
Set SPI driver.
Definition: net.c:976
NicDuplexMode netGetDuplexMode(NetInterface *interface)
Get duplex mode.
Definition: net.c:1145
error_t netGetEui64(NetInterface *interface, Eui64 *eui64)
Retrieve EUI-64 interface identifier.
Definition: net.c:581
error_t netSetSwitchDriver(NetInterface *interface, const SwitchDriver *driver)
Set Ethernet switch driver.
Definition: net.c:886
error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
Set Ethernet PHY driver.
Definition: net.c:822
error_t netConfigInterface(NetInterface *interface)
Configure network interface.
Definition: net.c:1203
error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
Set MAC address.
Definition: net.c:484
error_t netSetHostname(NetInterface *interface, const char_t *name)
Set host name.
Definition: net.c:667
error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
Specify Ethernet PHY address.
Definition: net.c:852
uint32_t netGetRand(void)
Generate a random 32-bit value.
Definition: net.c:385
error_t netSetUartDriver(NetInterface *interface, const UartDriver *driver)
Set UART driver.
Definition: net.c:1001
error_t netSetSwitchPort(NetInterface *interface, uint8_t port)
Specify switch port.
Definition: net.c:916
uint_t netGetLinkSpeed(NetInterface *interface)
Get link speed.
Definition: net.c:1114
void netTask(void)
TCP/IP events handling (deprecated)
Definition: net.c:1527
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:470
error_t netSetLinkState(NetInterface *interface, NicLinkState linkState)
Set administrative link state.
Definition: net.c:1051
error_t netStopInterface(NetInterface *interface)
Stop network interface.
Definition: net.c:1471
error_t netInit(void)
Initialize TCP/IP stack (deprecated)
Definition: net.c:99
error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
Set EUI-64 interface identifier.
Definition: net.c:556
error_t netSetVmanId(NetInterface *interface, uint16_t vmanId)
Specify VMAN identifier (802.1ad)
Definition: net.c:730
error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Retrieve MAC address.
Definition: net.c:519
error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
Set interface identifier.
Definition: net.c:613
uint32_t netGetRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net.c:416
error_t netStartInterface(NetInterface *interface)
Start network interface.
Definition: net.c:1391
error_t netStart(NetContext *context)
Start TCP/IP stack.
Definition: net.c:311
TCP/IP stack core.
#define NET_TASK_PRIORITY
Definition: net.h:169
#define NET_RAND_SEED_SIZE
Definition: net.h:155
#define NET_INTERFACE_COUNT
Definition: net.h:113
#define NET_MAX_IF_NAME_LEN
Definition: net.h:141
#define NetInterface
Definition: net.h:36
#define NET_MAX_HOSTNAME_LEN
Definition: net.h:148
#define NET_TICK_INTERVAL
Definition: net.h:174
#define NET_TASK_STACK_SIZE
Definition: net.h:162
#define netMutex
Definition: net_legacy.h:195
#define netTimestamp
Definition: net_legacy.h:198
#define netTaskRunning
Definition: net_legacy.h:197
#define netInterface
Definition: net_legacy.h:199
#define netEvent
Definition: net_legacy.h:196
error_t memPoolInit(void)
Memory pool initialization.
Definition: net_mem.c:70
void netInitRand(void)
Initialize random number generator.
Definition: net_misc.c:832
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net_misc.c:198
void netGenerateRandData(uint8_t *data, size_t length)
Get a string of random data.
Definition: net_misc.c:941
uint32_t netGenerateRand(void)
Generate a random 32-bit value.
Definition: net_misc.c:888
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:914
void netTick(void)
Manage TCP/IP timers.
Definition: net_misc.c:413
systime_t nicTickCounter
Definition: nic.c:43
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:52
error_t nicUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Definition: nic.c:352
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:84
NicLinkState
Link state.
Definition: nic.h:97
NicDuplexMode
Duplex mode.
Definition: nic.h:122
@ NIC_UNKNOWN_DUPLEX_MODE
Definition: nic.h:123
@ NIC_LINK_SPEED_UNKNOWN
Definition: nic.h:110
#define osMemset(p, value, length)
Definition: os_port.h:135
#define timeCompare(t1, t2)
Definition: os_port.h:40
#define osStrlen(s)
Definition: os_port.h:165
#define osSprintf(dest,...)
Definition: os_port.h:231
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
#define osStrcpy(s1, s2)
Definition: os_port.h:207
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void osDeleteEvent(OsEvent *event)
Delete an event object.
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
systime_t osGetSystemTime(void)
Retrieve system time.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void(* OsTaskCode)(void *arg)
Task routine.
#define osEnterTask()
#define OS_INVALID_TASK_ID
uint32_t systime_t
System time.
systime_t pppTickCounter
Definition: ppp.c:53
TCP/IP raw sockets.
char_t name[]
error_t socketInit(void)
Socket related initialization.
Definition: socket.c:85
Socket API.
String manipulation helper functions.
External interrupt line driver.
Definition: nic.h:394
TCP/IP stack context.
Definition: net.h:315
uint8_t randSeed[NET_RAND_SEED_SIZE]
Random seed.
Definition: net.h:323
OsTaskId taskId
Task identifier.
Definition: net.h:320
OsTaskParameters taskParams
Task parameters.
Definition: net.h:319
TCP/IP stack settings.
Definition: net.h:305
OsTaskParameters task
Task parameters.
Definition: net.h:306
NIC driver.
Definition: nic.h:283
Ethernet PHY driver.
Definition: nic.h:308
SMI driver.
Definition: nic.h:354
SPI driver.
Definition: nic.h:366
Ethernet switch driver.
Definition: nic.h:322
UART driver.
Definition: nic.h:381
systime_t tcpTickCounter
Definition: tcp.c:49
error_t tcpInit(void)
TCP related initialization.
Definition: tcp.c:60
uint8_t length
Definition: tcp.h:368
uint8_t value[]
Definition: tcp.h:369
#define TCP_INITIAL_RTO
Definition: tcp.h:117
Helper functions for TCP.
TCP timer management.
error_t udpInit(void)
UDP related initialization.
Definition: udp.c:62
error_t webSocketInit(void)
WebSocket related initialization.
Definition: web_socket.c:60
WebSocket API (client and server)