Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file net.c
Sergunb 0:8918a71cdbe9 3 * @brief TCP/IP stack core
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This file is part of CycloneTCP Open.
Sergunb 0:8918a71cdbe9 10 *
Sergunb 0:8918a71cdbe9 11 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 12 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 13 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 14 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 15 *
Sergunb 0:8918a71cdbe9 16 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 19 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 20 *
Sergunb 0:8918a71cdbe9 21 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 22 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 24 *
Sergunb 0:8918a71cdbe9 25 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 26 * @version 1.7.6
Sergunb 0:8918a71cdbe9 27 **/
Sergunb 0:8918a71cdbe9 28
Sergunb 0:8918a71cdbe9 29 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 30 #define TRACE_LEVEL ETH_TRACE_LEVEL
Sergunb 0:8918a71cdbe9 31
Sergunb 0:8918a71cdbe9 32 //Dependencies
Sergunb 0:8918a71cdbe9 33 #include <stdlib.h>
Sergunb 0:8918a71cdbe9 34 #include "core/net.h"
Sergunb 0:8918a71cdbe9 35 #include "core/socket.h"
Sergunb 0:8918a71cdbe9 36 #include "core/tcp_timer.h"
Sergunb 0:8918a71cdbe9 37 #include "core/ethernet.h"
Sergunb 0:8918a71cdbe9 38 #include "ipv4/arp.h"
Sergunb 0:8918a71cdbe9 39 #include "ipv4/ipv4.h"
Sergunb 0:8918a71cdbe9 40 #include "ipv4/ipv4_routing.h"
Sergunb 0:8918a71cdbe9 41 #include "ipv4/igmp.h"
Sergunb 0:8918a71cdbe9 42 #include "ipv6/ipv6.h"
Sergunb 0:8918a71cdbe9 43 #include "ipv6/ipv6_routing.h"
Sergunb 0:8918a71cdbe9 44 #include "ipv6/mld.h"
Sergunb 0:8918a71cdbe9 45 #include "ipv6/ndp.h"
Sergunb 0:8918a71cdbe9 46 #include "ipv6/ndp_router_adv.h"
Sergunb 0:8918a71cdbe9 47 #include "dhcp/dhcp_client.h"
Sergunb 0:8918a71cdbe9 48 #include "dhcp/dhcp_server.h"
Sergunb 0:8918a71cdbe9 49 #include "dns/dns_cache.h"
Sergunb 0:8918a71cdbe9 50 #include "dns/dns_client.h"
Sergunb 0:8918a71cdbe9 51 #include "mdns/mdns_client.h"
Sergunb 0:8918a71cdbe9 52 #include "mdns/mdns_responder.h"
Sergunb 0:8918a71cdbe9 53 #include "mdns/mdns_common.h"
Sergunb 0:8918a71cdbe9 54 #include "dns_sd/dns_sd.h"
Sergunb 0:8918a71cdbe9 55 #include "netbios/nbns_client.h"
Sergunb 0:8918a71cdbe9 56 #include "netbios/nbns_responder.h"
Sergunb 0:8918a71cdbe9 57 #include "netbios/nbns_common.h"
Sergunb 0:8918a71cdbe9 58 #include "dns_sd/dns_sd.h"
Sergunb 0:8918a71cdbe9 59 #include "mibs/mib2_module.h"
Sergunb 0:8918a71cdbe9 60 #include "str.h"
Sergunb 0:8918a71cdbe9 61 #include "debug.h"
Sergunb 0:8918a71cdbe9 62
Sergunb 0:8918a71cdbe9 63 #if (WEB_SOCKET_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 64 #include "web_socket/web_socket.h"
Sergunb 0:8918a71cdbe9 65 #endif
Sergunb 0:8918a71cdbe9 66
Sergunb 0:8918a71cdbe9 67 //TCP/IP stack handle
Sergunb 0:8918a71cdbe9 68 OsTask *netTaskHandle;
Sergunb 0:8918a71cdbe9 69 //Mutex preventing simultaneous access to the TCP/IP stack
Sergunb 0:8918a71cdbe9 70 OsMutex netMutex;
Sergunb 0:8918a71cdbe9 71 //Event object to receive notifications from device drivers
Sergunb 0:8918a71cdbe9 72 OsEvent netEvent;
Sergunb 0:8918a71cdbe9 73 //Network interfaces
Sergunb 0:8918a71cdbe9 74 NetInterface netInterface[NET_INTERFACE_COUNT];
Sergunb 0:8918a71cdbe9 75
Sergunb 0:8918a71cdbe9 76 //TCP/IP process state
Sergunb 0:8918a71cdbe9 77 static bool_t netTaskRunning;
Sergunb 0:8918a71cdbe9 78 //Timestamp
Sergunb 0:8918a71cdbe9 79 static systime_t netTimestamp;
Sergunb 0:8918a71cdbe9 80 //Pseudo-random number generator state
Sergunb 0:8918a71cdbe9 81 static uint32_t prngState = 0;
Sergunb 0:8918a71cdbe9 82
Sergunb 0:8918a71cdbe9 83 //Mutex to prevent simultaneous access to the callback table
Sergunb 0:8918a71cdbe9 84 static OsMutex callbackTableMutex;
Sergunb 0:8918a71cdbe9 85 //Table that holds the registered user callbacks
Sergunb 0:8918a71cdbe9 86 static LinkChangeCallbackDesc callbackTable[NET_CALLBACK_TABLE_SIZE];
Sergunb 0:8918a71cdbe9 87
Sergunb 0:8918a71cdbe9 88 //Check TCP/IP stack configuration
Sergunb 0:8918a71cdbe9 89 #if (NET_STATIC_OS_RESOURCES == ENABLED)
Sergunb 0:8918a71cdbe9 90
Sergunb 0:8918a71cdbe9 91 //Task responsible for handling TCP/IP events
Sergunb 0:8918a71cdbe9 92 static OsTask netTaskInstance;
Sergunb 0:8918a71cdbe9 93 static uint_t netTaskStack[NET_TASK_STACK_SIZE];
Sergunb 0:8918a71cdbe9 94
Sergunb 0:8918a71cdbe9 95 #endif
Sergunb 0:8918a71cdbe9 96
Sergunb 0:8918a71cdbe9 97
Sergunb 0:8918a71cdbe9 98 /**
Sergunb 0:8918a71cdbe9 99 * @brief TCP/IP stack initialization
Sergunb 0:8918a71cdbe9 100 * @return Error code
Sergunb 0:8918a71cdbe9 101 **/
Sergunb 0:8918a71cdbe9 102
Sergunb 0:8918a71cdbe9 103 error_t netInit(void)
Sergunb 0:8918a71cdbe9 104 {
Sergunb 0:8918a71cdbe9 105 error_t error;
Sergunb 0:8918a71cdbe9 106 uint_t i;
Sergunb 0:8918a71cdbe9 107 NetInterface *interface;
Sergunb 0:8918a71cdbe9 108
Sergunb 0:8918a71cdbe9 109 //The TCP/IP process is currently suspended
Sergunb 0:8918a71cdbe9 110 netTaskRunning = FALSE;
Sergunb 0:8918a71cdbe9 111 //Get current time
Sergunb 0:8918a71cdbe9 112 netTimestamp = osGetSystemTime();
Sergunb 0:8918a71cdbe9 113
Sergunb 0:8918a71cdbe9 114 //Create a mutex to prevent simultaneous access to the TCP/IP stack
Sergunb 0:8918a71cdbe9 115 if(!osCreateMutex(&netMutex))
Sergunb 0:8918a71cdbe9 116 {
Sergunb 0:8918a71cdbe9 117 //Failed to create mutex
Sergunb 0:8918a71cdbe9 118 return ERROR_OUT_OF_RESOURCES;
Sergunb 0:8918a71cdbe9 119 }
Sergunb 0:8918a71cdbe9 120
Sergunb 0:8918a71cdbe9 121 //Create a event object to receive notifications from device drivers
Sergunb 0:8918a71cdbe9 122 if(!osCreateEvent(&netEvent))
Sergunb 0:8918a71cdbe9 123 {
Sergunb 0:8918a71cdbe9 124 //Failed to create mutex
Sergunb 0:8918a71cdbe9 125 return ERROR_OUT_OF_RESOURCES;
Sergunb 0:8918a71cdbe9 126 }
Sergunb 0:8918a71cdbe9 127
Sergunb 0:8918a71cdbe9 128 //Memory pool initialization
Sergunb 0:8918a71cdbe9 129 error = memPoolInit();
Sergunb 0:8918a71cdbe9 130 //Any error to report?
Sergunb 0:8918a71cdbe9 131 if(error)
Sergunb 0:8918a71cdbe9 132 return error;
Sergunb 0:8918a71cdbe9 133
Sergunb 0:8918a71cdbe9 134 //Clear configuration data for each interface
Sergunb 0:8918a71cdbe9 135 memset(netInterface, 0, sizeof(netInterface));
Sergunb 0:8918a71cdbe9 136
Sergunb 0:8918a71cdbe9 137 //Save the number of interfaces
Sergunb 0:8918a71cdbe9 138 MIB2_SET_INTEGER(mib2Base.ifGroup.ifNumber, NET_INTERFACE_COUNT);
Sergunb 0:8918a71cdbe9 139
Sergunb 0:8918a71cdbe9 140 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 141 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 142 {
Sergunb 0:8918a71cdbe9 143 //Point to the current interface
Sergunb 0:8918a71cdbe9 144 interface = &netInterface[i];
Sergunb 0:8918a71cdbe9 145
Sergunb 0:8918a71cdbe9 146 //Default interface name
Sergunb 0:8918a71cdbe9 147 sprintf(interface->name, "eth%u", i);
Sergunb 0:8918a71cdbe9 148 //Default interface identifier
Sergunb 0:8918a71cdbe9 149 interface->id = i;
Sergunb 0:8918a71cdbe9 150 //Default PHY address
Sergunb 0:8918a71cdbe9 151 interface->phyAddr = UINT8_MAX;
Sergunb 0:8918a71cdbe9 152
Sergunb 0:8918a71cdbe9 153 #if (MIB2_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 154 //MIB ifEntry object
Sergunb 0:8918a71cdbe9 155 interface->mibIfEntry = &mib2Base.ifGroup.ifTable[i];
Sergunb 0:8918a71cdbe9 156 #endif
Sergunb 0:8918a71cdbe9 157
Sergunb 0:8918a71cdbe9 158 //Interface identifier
Sergunb 0:8918a71cdbe9 159 MIB2_SET_INTEGER(interface->mibIfEntry->ifIndex, i + 1);
Sergunb 0:8918a71cdbe9 160 //Interface name
Sergunb 0:8918a71cdbe9 161 MIB2_SET_OCTET_STRING(interface->mibIfEntry->ifDescr, interface->name, strlen(interface->name));
Sergunb 0:8918a71cdbe9 162 MIB2_SET_OCTET_STRING_LEN(interface->mibIfEntry->ifDescrLen, strlen(interface->name));
Sergunb 0:8918a71cdbe9 163 //Default interface type
Sergunb 0:8918a71cdbe9 164 MIB2_SET_INTEGER(interface->mibIfEntry->ifType, MIB2_IF_TYPE_OTHER);
Sergunb 0:8918a71cdbe9 165 //Default interface state
Sergunb 0:8918a71cdbe9 166 MIB2_SET_INTEGER(interface->mibIfEntry->ifAdminStatus, MIB2_IF_ADMIN_STATUS_DOWN);
Sergunb 0:8918a71cdbe9 167 MIB2_SET_INTEGER(interface->mibIfEntry->ifOperStatus, MIB2_IF_OPER_STATUS_DOWN);
Sergunb 0:8918a71cdbe9 168 }
Sergunb 0:8918a71cdbe9 169
Sergunb 0:8918a71cdbe9 170 //Create a mutex to prevent simultaneous access to the callback table
Sergunb 0:8918a71cdbe9 171 if(!osCreateMutex(&callbackTableMutex))
Sergunb 0:8918a71cdbe9 172 {
Sergunb 0:8918a71cdbe9 173 //Failed to create mutex
Sergunb 0:8918a71cdbe9 174 return ERROR_OUT_OF_RESOURCES;
Sergunb 0:8918a71cdbe9 175 }
Sergunb 0:8918a71cdbe9 176
Sergunb 0:8918a71cdbe9 177 //Initialize callback table
Sergunb 0:8918a71cdbe9 178 memset(callbackTable, 0, sizeof(callbackTable));
Sergunb 0:8918a71cdbe9 179
Sergunb 0:8918a71cdbe9 180 //Socket related initialization
Sergunb 0:8918a71cdbe9 181 error = socketInit();
Sergunb 0:8918a71cdbe9 182 //Any error to report?
Sergunb 0:8918a71cdbe9 183 if(error)
Sergunb 0:8918a71cdbe9 184 return error;
Sergunb 0:8918a71cdbe9 185
Sergunb 0:8918a71cdbe9 186 #if (WEB_SOCKET_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 187 //WebSocket related initialization
Sergunb 0:8918a71cdbe9 188 webSocketInit();
Sergunb 0:8918a71cdbe9 189 #endif
Sergunb 0:8918a71cdbe9 190
Sergunb 0:8918a71cdbe9 191 #if (IPV4_SUPPORT == ENABLED && IPV4_ROUTING_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 192 //Initialize IPv4 routing table
Sergunb 0:8918a71cdbe9 193 error = ipv4InitRouting();
Sergunb 0:8918a71cdbe9 194 //Any error to report?
Sergunb 0:8918a71cdbe9 195 if(error)
Sergunb 0:8918a71cdbe9 196 return error;
Sergunb 0:8918a71cdbe9 197 #endif
Sergunb 0:8918a71cdbe9 198
Sergunb 0:8918a71cdbe9 199 #if (IPV6_SUPPORT == ENABLED && IPV6_ROUTING_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 200 //Initialize IPv6 routing table
Sergunb 0:8918a71cdbe9 201 error = ipv6InitRouting();
Sergunb 0:8918a71cdbe9 202 //Any error to report?
Sergunb 0:8918a71cdbe9 203 if(error)
Sergunb 0:8918a71cdbe9 204 return error;
Sergunb 0:8918a71cdbe9 205 #endif
Sergunb 0:8918a71cdbe9 206
Sergunb 0:8918a71cdbe9 207 #if (UDP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 208 //UDP related initialization
Sergunb 0:8918a71cdbe9 209 error = udpInit();
Sergunb 0:8918a71cdbe9 210 //Any error to report?
Sergunb 0:8918a71cdbe9 211 if(error)
Sergunb 0:8918a71cdbe9 212 return error;
Sergunb 0:8918a71cdbe9 213 #endif
Sergunb 0:8918a71cdbe9 214
Sergunb 0:8918a71cdbe9 215 #if (TCP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 216 //TCP related initialization
Sergunb 0:8918a71cdbe9 217 error = tcpInit();
Sergunb 0:8918a71cdbe9 218 //Any error to report?
Sergunb 0:8918a71cdbe9 219 if(error)
Sergunb 0:8918a71cdbe9 220 return error;
Sergunb 0:8918a71cdbe9 221 #endif
Sergunb 0:8918a71cdbe9 222
Sergunb 0:8918a71cdbe9 223 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
Sergunb 0:8918a71cdbe9 224 NBNS_CLIENT_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 225 //DNS cache initialization
Sergunb 0:8918a71cdbe9 226 error = dnsInit();
Sergunb 0:8918a71cdbe9 227 //Any error to report?
Sergunb 0:8918a71cdbe9 228 if(error)
Sergunb 0:8918a71cdbe9 229 return error;
Sergunb 0:8918a71cdbe9 230 #endif
Sergunb 0:8918a71cdbe9 231
Sergunb 0:8918a71cdbe9 232 //Initialize tick counters
Sergunb 0:8918a71cdbe9 233 nicTickCounter = 0;
Sergunb 0:8918a71cdbe9 234
Sergunb 0:8918a71cdbe9 235 #if (PPP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 236 pppTickCounter = 0;
Sergunb 0:8918a71cdbe9 237 #endif
Sergunb 0:8918a71cdbe9 238 #if (IPV4_SUPPORT == ENABLED && ETH_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 239 arpTickCounter = 0;
Sergunb 0:8918a71cdbe9 240 #endif
Sergunb 0:8918a71cdbe9 241 #if (IPV4_SUPPORT == ENABLED && IPV4_FRAG_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 242 ipv4FragTickCounter = 0;
Sergunb 0:8918a71cdbe9 243 #endif
Sergunb 0:8918a71cdbe9 244 #if (IPV4_SUPPORT == ENABLED && IGMP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 245 igmpTickCounter = 0;
Sergunb 0:8918a71cdbe9 246 #endif
Sergunb 0:8918a71cdbe9 247 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 248 autoIpTickCounter = 0;
Sergunb 0:8918a71cdbe9 249 #endif
Sergunb 0:8918a71cdbe9 250 #if (IPV4_SUPPORT == ENABLED && DHCP_CLIENT_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 251 dhcpClientTickCounter = 0;
Sergunb 0:8918a71cdbe9 252 #endif
Sergunb 0:8918a71cdbe9 253 #if (IPV4_SUPPORT == ENABLED && DHCP_SERVER_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 254 dhcpServerTickCounter = 0;
Sergunb 0:8918a71cdbe9 255 #endif
Sergunb 0:8918a71cdbe9 256 #if (IPV6_SUPPORT == ENABLED && IPV6_FRAG_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 257 ipv6FragTickCounter = 0;
Sergunb 0:8918a71cdbe9 258 #endif
Sergunb 0:8918a71cdbe9 259 #if (IPV6_SUPPORT == ENABLED && MLD_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 260 mldTickCounter = 0;
Sergunb 0:8918a71cdbe9 261 #endif
Sergunb 0:8918a71cdbe9 262 #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 263 ndpTickCounter = 0;
Sergunb 0:8918a71cdbe9 264 #endif
Sergunb 0:8918a71cdbe9 265 #if (IPV6_SUPPORT == ENABLED && NDP_ROUTER_ADV_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 266 ndpRouterAdvTickCounter = 0;
Sergunb 0:8918a71cdbe9 267 #endif
Sergunb 0:8918a71cdbe9 268 #if (IPV6_SUPPORT == ENABLED && DHCPV6_CLIENT_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 269 dhcpv6ClientTickCounter = 0;
Sergunb 0:8918a71cdbe9 270 #endif
Sergunb 0:8918a71cdbe9 271 #if (TCP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 272 tcpTickCounter = 0;
Sergunb 0:8918a71cdbe9 273 #endif
Sergunb 0:8918a71cdbe9 274 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
Sergunb 0:8918a71cdbe9 275 NBNS_CLIENT_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 276 dnsTickCounter = 0;
Sergunb 0:8918a71cdbe9 277 #endif
Sergunb 0:8918a71cdbe9 278 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 279 mdnsResponderTickCounter = 0;
Sergunb 0:8918a71cdbe9 280 #endif
Sergunb 0:8918a71cdbe9 281 #if (DNS_SD_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 282 dnsSdTickCounter = 0;
Sergunb 0:8918a71cdbe9 283 #endif
Sergunb 0:8918a71cdbe9 284
Sergunb 0:8918a71cdbe9 285 #if (NET_STATIC_OS_RESOURCES == ENABLED)
Sergunb 0:8918a71cdbe9 286 //Create a task to handle TCP/IP events
Sergunb 0:8918a71cdbe9 287 osCreateStaticTask(&netTaskInstance, "TCP/IP Stack", (OsTaskCode) netTask,
Sergunb 0:8918a71cdbe9 288 NULL, netTaskStack, NET_TASK_STACK_SIZE, NET_TASK_PRIORITY);
Sergunb 0:8918a71cdbe9 289 #else
Sergunb 0:8918a71cdbe9 290 //Create a task to handle TCP/IP events
Sergunb 0:8918a71cdbe9 291 netTaskHandle = osCreateTask("TCP/IP Stack", (OsTaskCode) netTask,
Sergunb 0:8918a71cdbe9 292 NULL, NET_TASK_STACK_SIZE, NET_TASK_PRIORITY);
Sergunb 0:8918a71cdbe9 293
Sergunb 0:8918a71cdbe9 294 //Unable to create the task?
Sergunb 0:8918a71cdbe9 295 if(netTaskHandle == OS_INVALID_HANDLE)
Sergunb 0:8918a71cdbe9 296 return ERROR_OUT_OF_RESOURCES;
Sergunb 0:8918a71cdbe9 297 #endif
Sergunb 0:8918a71cdbe9 298
Sergunb 0:8918a71cdbe9 299 #if (NET_RTOS_SUPPORT == DISABLED)
Sergunb 0:8918a71cdbe9 300 //The TCP/IP process is now running
Sergunb 0:8918a71cdbe9 301 netTaskRunning = TRUE;
Sergunb 0:8918a71cdbe9 302 #endif
Sergunb 0:8918a71cdbe9 303
Sergunb 0:8918a71cdbe9 304 //Successful initialization
Sergunb 0:8918a71cdbe9 305 return NO_ERROR;
Sergunb 0:8918a71cdbe9 306 }
Sergunb 0:8918a71cdbe9 307
Sergunb 0:8918a71cdbe9 308
Sergunb 0:8918a71cdbe9 309 /**
Sergunb 0:8918a71cdbe9 310 * @brief Set MAC address
Sergunb 0:8918a71cdbe9 311 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 312 * @param[in] macAddr MAC address
Sergunb 0:8918a71cdbe9 313 * @return Error code
Sergunb 0:8918a71cdbe9 314 **/
Sergunb 0:8918a71cdbe9 315
Sergunb 0:8918a71cdbe9 316 error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
Sergunb 0:8918a71cdbe9 317 {
Sergunb 0:8918a71cdbe9 318 //Check parameters
Sergunb 0:8918a71cdbe9 319 if(interface == NULL || macAddr == NULL)
Sergunb 0:8918a71cdbe9 320 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 321
Sergunb 0:8918a71cdbe9 322 #if (ETH_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 323 //Get exclusive access
Sergunb 0:8918a71cdbe9 324 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 325
Sergunb 0:8918a71cdbe9 326 //Set MAC address
Sergunb 0:8918a71cdbe9 327 interface->macAddr = *macAddr;
Sergunb 0:8918a71cdbe9 328
Sergunb 0:8918a71cdbe9 329 //Generate the 64-bit interface identifier
Sergunb 0:8918a71cdbe9 330 macAddrToEui64(macAddr, &interface->eui64);
Sergunb 0:8918a71cdbe9 331
Sergunb 0:8918a71cdbe9 332 //Interface's physical address
Sergunb 0:8918a71cdbe9 333 MIB2_SET_OCTET_STRING(interface->mibIfEntry->ifPhysAddress, &interface->macAddr, 6);
Sergunb 0:8918a71cdbe9 334 MIB2_SET_OCTET_STRING_LEN(interface->mibIfEntry->ifPhysAddressLen, 6);
Sergunb 0:8918a71cdbe9 335
Sergunb 0:8918a71cdbe9 336 //Release exclusive access
Sergunb 0:8918a71cdbe9 337 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 338 #endif
Sergunb 0:8918a71cdbe9 339
Sergunb 0:8918a71cdbe9 340 //Successful processing
Sergunb 0:8918a71cdbe9 341 return NO_ERROR;
Sergunb 0:8918a71cdbe9 342 }
Sergunb 0:8918a71cdbe9 343
Sergunb 0:8918a71cdbe9 344
Sergunb 0:8918a71cdbe9 345 /**
Sergunb 0:8918a71cdbe9 346 * @brief Retrieve MAC address
Sergunb 0:8918a71cdbe9 347 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 348 * @param[out] macAddr MAC address
Sergunb 0:8918a71cdbe9 349 * @return Error code
Sergunb 0:8918a71cdbe9 350 **/
Sergunb 0:8918a71cdbe9 351
Sergunb 0:8918a71cdbe9 352 error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Sergunb 0:8918a71cdbe9 353 {
Sergunb 0:8918a71cdbe9 354 //Check parameters
Sergunb 0:8918a71cdbe9 355 if(interface == NULL || macAddr == NULL)
Sergunb 0:8918a71cdbe9 356 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 357
Sergunb 0:8918a71cdbe9 358 #if (ETH_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 359 //Get exclusive access
Sergunb 0:8918a71cdbe9 360 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 361 //Get MAC address
Sergunb 0:8918a71cdbe9 362 *macAddr = interface->macAddr;
Sergunb 0:8918a71cdbe9 363 //Release exclusive access
Sergunb 0:8918a71cdbe9 364 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 365 #endif
Sergunb 0:8918a71cdbe9 366
Sergunb 0:8918a71cdbe9 367 //Successful processing
Sergunb 0:8918a71cdbe9 368 return NO_ERROR;
Sergunb 0:8918a71cdbe9 369 }
Sergunb 0:8918a71cdbe9 370
Sergunb 0:8918a71cdbe9 371
Sergunb 0:8918a71cdbe9 372 /**
Sergunb 0:8918a71cdbe9 373 * @brief Set EUI-64 interface identifier
Sergunb 0:8918a71cdbe9 374 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 375 * @param[in] eui64 Interface identifier
Sergunb 0:8918a71cdbe9 376 * @return Error code
Sergunb 0:8918a71cdbe9 377 **/
Sergunb 0:8918a71cdbe9 378
Sergunb 0:8918a71cdbe9 379 error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
Sergunb 0:8918a71cdbe9 380 {
Sergunb 0:8918a71cdbe9 381 //Check parameters
Sergunb 0:8918a71cdbe9 382 if(interface == NULL || eui64 == NULL)
Sergunb 0:8918a71cdbe9 383 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 384
Sergunb 0:8918a71cdbe9 385 //Get exclusive access
Sergunb 0:8918a71cdbe9 386 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 387 //Set interface identifier
Sergunb 0:8918a71cdbe9 388 interface->eui64 = *eui64;
Sergunb 0:8918a71cdbe9 389 //Release exclusive access
Sergunb 0:8918a71cdbe9 390 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 391
Sergunb 0:8918a71cdbe9 392 //Successful processing
Sergunb 0:8918a71cdbe9 393 return NO_ERROR;
Sergunb 0:8918a71cdbe9 394 }
Sergunb 0:8918a71cdbe9 395
Sergunb 0:8918a71cdbe9 396
Sergunb 0:8918a71cdbe9 397 /**
Sergunb 0:8918a71cdbe9 398 * @brief Retrieve EUI-64 interface identifier
Sergunb 0:8918a71cdbe9 399 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 400 * @param[out] eui64 Interface identifier
Sergunb 0:8918a71cdbe9 401 * @return Error code
Sergunb 0:8918a71cdbe9 402 **/
Sergunb 0:8918a71cdbe9 403
Sergunb 0:8918a71cdbe9 404 error_t netGetEui64(NetInterface *interface, Eui64 *eui64)
Sergunb 0:8918a71cdbe9 405 {
Sergunb 0:8918a71cdbe9 406 //Check parameters
Sergunb 0:8918a71cdbe9 407 if(interface == NULL || eui64 == NULL)
Sergunb 0:8918a71cdbe9 408 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 409
Sergunb 0:8918a71cdbe9 410 //Get exclusive access
Sergunb 0:8918a71cdbe9 411 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 412 //Get interface identifier
Sergunb 0:8918a71cdbe9 413 *eui64 = interface->eui64;
Sergunb 0:8918a71cdbe9 414 //Release exclusive access
Sergunb 0:8918a71cdbe9 415 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 416
Sergunb 0:8918a71cdbe9 417 //Successful processing
Sergunb 0:8918a71cdbe9 418 return NO_ERROR;
Sergunb 0:8918a71cdbe9 419 }
Sergunb 0:8918a71cdbe9 420
Sergunb 0:8918a71cdbe9 421
Sergunb 0:8918a71cdbe9 422 /**
Sergunb 0:8918a71cdbe9 423 * @brief Set interface identifier
Sergunb 0:8918a71cdbe9 424 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 425 * @param[in] id Unique number identifying the interface
Sergunb 0:8918a71cdbe9 426 * @return Error code
Sergunb 0:8918a71cdbe9 427 **/
Sergunb 0:8918a71cdbe9 428
Sergunb 0:8918a71cdbe9 429 error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
Sergunb 0:8918a71cdbe9 430 {
Sergunb 0:8918a71cdbe9 431 //Check parameters
Sergunb 0:8918a71cdbe9 432 if(interface == NULL)
Sergunb 0:8918a71cdbe9 433 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 434
Sergunb 0:8918a71cdbe9 435 //Get exclusive access
Sergunb 0:8918a71cdbe9 436 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 437 //Set interface identifier
Sergunb 0:8918a71cdbe9 438 interface->id = id;
Sergunb 0:8918a71cdbe9 439 //Release exclusive access
Sergunb 0:8918a71cdbe9 440 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 441
Sergunb 0:8918a71cdbe9 442 //Successful processing
Sergunb 0:8918a71cdbe9 443 return NO_ERROR;
Sergunb 0:8918a71cdbe9 444 }
Sergunb 0:8918a71cdbe9 445
Sergunb 0:8918a71cdbe9 446
Sergunb 0:8918a71cdbe9 447 /**
Sergunb 0:8918a71cdbe9 448 * @brief Set interface name
Sergunb 0:8918a71cdbe9 449 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 450 * @param[in] name NULL-terminated string that contains the interface name
Sergunb 0:8918a71cdbe9 451 * @return Error code
Sergunb 0:8918a71cdbe9 452 **/
Sergunb 0:8918a71cdbe9 453
Sergunb 0:8918a71cdbe9 454 error_t netSetInterfaceName(NetInterface *interface, const char_t *name)
Sergunb 0:8918a71cdbe9 455 {
Sergunb 0:8918a71cdbe9 456 #if (MIB2_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 457 size_t n;
Sergunb 0:8918a71cdbe9 458 #endif
Sergunb 0:8918a71cdbe9 459
Sergunb 0:8918a71cdbe9 460 //Check parameters
Sergunb 0:8918a71cdbe9 461 if(interface == NULL || name == NULL)
Sergunb 0:8918a71cdbe9 462 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 463
Sergunb 0:8918a71cdbe9 464 //Get exclusive access
Sergunb 0:8918a71cdbe9 465 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 466
Sergunb 0:8918a71cdbe9 467 //Set interface name
Sergunb 0:8918a71cdbe9 468 strSafeCopy(interface->name, name, NET_MAX_IF_NAME_LEN);
Sergunb 0:8918a71cdbe9 469
Sergunb 0:8918a71cdbe9 470 #if (MIB2_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 471 //Get the length of the string
Sergunb 0:8918a71cdbe9 472 n = strlen(interface->name);
Sergunb 0:8918a71cdbe9 473
Sergunb 0:8918a71cdbe9 474 //Text string containing information about the interface
Sergunb 0:8918a71cdbe9 475 MIB2_SET_OCTET_STRING(interface->mibIfEntry->ifDescr, interface->name, n);
Sergunb 0:8918a71cdbe9 476 MIB2_SET_OCTET_STRING_LEN(interface->mibIfEntry->ifDescrLen, n);
Sergunb 0:8918a71cdbe9 477 #endif
Sergunb 0:8918a71cdbe9 478
Sergunb 0:8918a71cdbe9 479 //Release exclusive access
Sergunb 0:8918a71cdbe9 480 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 481
Sergunb 0:8918a71cdbe9 482 //Successful processing
Sergunb 0:8918a71cdbe9 483 return NO_ERROR;
Sergunb 0:8918a71cdbe9 484 }
Sergunb 0:8918a71cdbe9 485
Sergunb 0:8918a71cdbe9 486
Sergunb 0:8918a71cdbe9 487 /**
Sergunb 0:8918a71cdbe9 488 * @brief Set host name
Sergunb 0:8918a71cdbe9 489 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 490 * @param[in] name NULL-terminated string that contains the host name
Sergunb 0:8918a71cdbe9 491 * @return Error code
Sergunb 0:8918a71cdbe9 492 **/
Sergunb 0:8918a71cdbe9 493
Sergunb 0:8918a71cdbe9 494 error_t netSetHostname(NetInterface *interface, const char_t *name)
Sergunb 0:8918a71cdbe9 495 {
Sergunb 0:8918a71cdbe9 496 //Check parameters
Sergunb 0:8918a71cdbe9 497 if(interface == NULL || name == NULL)
Sergunb 0:8918a71cdbe9 498 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 499
Sergunb 0:8918a71cdbe9 500 //Get exclusive access
Sergunb 0:8918a71cdbe9 501 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 502
Sergunb 0:8918a71cdbe9 503 //Set host name
Sergunb 0:8918a71cdbe9 504 strSafeCopy(interface->hostname, name, NET_MAX_HOSTNAME_LEN);
Sergunb 0:8918a71cdbe9 505
Sergunb 0:8918a71cdbe9 506 //Release exclusive access
Sergunb 0:8918a71cdbe9 507 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 508
Sergunb 0:8918a71cdbe9 509 //Successful processing
Sergunb 0:8918a71cdbe9 510 return NO_ERROR;
Sergunb 0:8918a71cdbe9 511 }
Sergunb 0:8918a71cdbe9 512
Sergunb 0:8918a71cdbe9 513
Sergunb 0:8918a71cdbe9 514 /**
Sergunb 0:8918a71cdbe9 515 * @brief Set proxy server
Sergunb 0:8918a71cdbe9 516 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 517 * @param[in] name Proxy server name
Sergunb 0:8918a71cdbe9 518 * @param[in] port Proxy server port
Sergunb 0:8918a71cdbe9 519 * @return Error code
Sergunb 0:8918a71cdbe9 520 **/
Sergunb 0:8918a71cdbe9 521
Sergunb 0:8918a71cdbe9 522 error_t netSetProxy(NetInterface *interface, const char_t *name, uint16_t port)
Sergunb 0:8918a71cdbe9 523 {
Sergunb 0:8918a71cdbe9 524 //Check parameters
Sergunb 0:8918a71cdbe9 525 if(interface == NULL || name == NULL)
Sergunb 0:8918a71cdbe9 526 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 527
Sergunb 0:8918a71cdbe9 528 //Get exclusive access
Sergunb 0:8918a71cdbe9 529 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 530
Sergunb 0:8918a71cdbe9 531 //Set proxy server name
Sergunb 0:8918a71cdbe9 532 strSafeCopy(interface->proxyName, name, NET_MAX_PROXY_NAME_LEN);
Sergunb 0:8918a71cdbe9 533 //Set proxy server port
Sergunb 0:8918a71cdbe9 534 interface->proxyPort = port;
Sergunb 0:8918a71cdbe9 535
Sergunb 0:8918a71cdbe9 536 //Release exclusive access
Sergunb 0:8918a71cdbe9 537 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 538
Sergunb 0:8918a71cdbe9 539 //Successful processing
Sergunb 0:8918a71cdbe9 540 return NO_ERROR;
Sergunb 0:8918a71cdbe9 541 }
Sergunb 0:8918a71cdbe9 542
Sergunb 0:8918a71cdbe9 543
Sergunb 0:8918a71cdbe9 544 /**
Sergunb 0:8918a71cdbe9 545 * @brief Set Ethernet MAC driver
Sergunb 0:8918a71cdbe9 546 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 547 * @param[in] driver Ethernet MAC driver
Sergunb 0:8918a71cdbe9 548 * @return Error code
Sergunb 0:8918a71cdbe9 549 **/
Sergunb 0:8918a71cdbe9 550
Sergunb 0:8918a71cdbe9 551 error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
Sergunb 0:8918a71cdbe9 552 {
Sergunb 0:8918a71cdbe9 553 //Check parameters
Sergunb 0:8918a71cdbe9 554 if(interface == NULL || driver == NULL)
Sergunb 0:8918a71cdbe9 555 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 556
Sergunb 0:8918a71cdbe9 557 //Get exclusive access
Sergunb 0:8918a71cdbe9 558 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 559
Sergunb 0:8918a71cdbe9 560 //Set Ethernet MAC driver
Sergunb 0:8918a71cdbe9 561 interface->nicDriver = driver;
Sergunb 0:8918a71cdbe9 562
Sergunb 0:8918a71cdbe9 563 //Set interface type
Sergunb 0:8918a71cdbe9 564 if(driver->type == NIC_TYPE_ETHERNET)
Sergunb 0:8918a71cdbe9 565 {
Sergunb 0:8918a71cdbe9 566 //Ethernet interface
Sergunb 0:8918a71cdbe9 567 MIB2_SET_INTEGER(interface->mibIfEntry->ifType, MIB2_IF_TYPE_ETHERNET_CSMACD);
Sergunb 0:8918a71cdbe9 568 }
Sergunb 0:8918a71cdbe9 569 else if(driver->type == NIC_TYPE_PPP)
Sergunb 0:8918a71cdbe9 570 {
Sergunb 0:8918a71cdbe9 571 //PPP interface
Sergunb 0:8918a71cdbe9 572 MIB2_SET_INTEGER(interface->mibIfEntry->ifType, MIB2_IF_TYPE_PPP);
Sergunb 0:8918a71cdbe9 573 }
Sergunb 0:8918a71cdbe9 574 else if(driver->type == NIC_TYPE_6LOWPAN)
Sergunb 0:8918a71cdbe9 575 {
Sergunb 0:8918a71cdbe9 576 //IEEE 802.15.4 WPAN interface
Sergunb 0:8918a71cdbe9 577 MIB2_SET_INTEGER(interface->mibIfEntry->ifType, MIB2_IF_TYPE_IEEE_802_15_4);
Sergunb 0:8918a71cdbe9 578 }
Sergunb 0:8918a71cdbe9 579 else
Sergunb 0:8918a71cdbe9 580 {
Sergunb 0:8918a71cdbe9 581 //Unknown interface type
Sergunb 0:8918a71cdbe9 582 MIB2_SET_INTEGER(interface->mibIfEntry->ifType, MIB2_IF_TYPE_OTHER);
Sergunb 0:8918a71cdbe9 583 }
Sergunb 0:8918a71cdbe9 584
Sergunb 0:8918a71cdbe9 585 //Set interface MTU
Sergunb 0:8918a71cdbe9 586 MIB2_SET_INTEGER(interface->mibIfEntry->ifMtu, driver->mtu);
Sergunb 0:8918a71cdbe9 587 //Update interface state
Sergunb 0:8918a71cdbe9 588 MIB2_SET_INTEGER(interface->mibIfEntry->ifAdminStatus, MIB2_IF_ADMIN_STATUS_UP);
Sergunb 0:8918a71cdbe9 589
Sergunb 0:8918a71cdbe9 590 //Release exclusive access
Sergunb 0:8918a71cdbe9 591 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 592
Sergunb 0:8918a71cdbe9 593 //Successful processing
Sergunb 0:8918a71cdbe9 594 return NO_ERROR;
Sergunb 0:8918a71cdbe9 595 }
Sergunb 0:8918a71cdbe9 596
Sergunb 0:8918a71cdbe9 597
Sergunb 0:8918a71cdbe9 598 /**
Sergunb 0:8918a71cdbe9 599 * @brief Set Ethernet PHY driver
Sergunb 0:8918a71cdbe9 600 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 601 * @param[in] driver Ethernet PHY driver (can be NULL for MAC + PHY controller)
Sergunb 0:8918a71cdbe9 602 * @return Error code
Sergunb 0:8918a71cdbe9 603 **/
Sergunb 0:8918a71cdbe9 604
Sergunb 0:8918a71cdbe9 605 error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
Sergunb 0:8918a71cdbe9 606 {
Sergunb 0:8918a71cdbe9 607 //Check parameters
Sergunb 0:8918a71cdbe9 608 if(interface == NULL || driver == NULL)
Sergunb 0:8918a71cdbe9 609 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 610
Sergunb 0:8918a71cdbe9 611 //Get exclusive access
Sergunb 0:8918a71cdbe9 612 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 613 //Set Ethernet PHY driver
Sergunb 0:8918a71cdbe9 614 interface->phyDriver = driver;
Sergunb 0:8918a71cdbe9 615 //Release exclusive access
Sergunb 0:8918a71cdbe9 616 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 617
Sergunb 0:8918a71cdbe9 618 //Successful processing
Sergunb 0:8918a71cdbe9 619 return NO_ERROR;
Sergunb 0:8918a71cdbe9 620 }
Sergunb 0:8918a71cdbe9 621
Sergunb 0:8918a71cdbe9 622
Sergunb 0:8918a71cdbe9 623 /**
Sergunb 0:8918a71cdbe9 624 * @brief Set Ethernet PHY address
Sergunb 0:8918a71cdbe9 625 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 626 * @param[in] phyAddr PHY address
Sergunb 0:8918a71cdbe9 627 * @return Error code
Sergunb 0:8918a71cdbe9 628 **/
Sergunb 0:8918a71cdbe9 629
Sergunb 0:8918a71cdbe9 630 error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
Sergunb 0:8918a71cdbe9 631 {
Sergunb 0:8918a71cdbe9 632 //Make sure the network interface is valid
Sergunb 0:8918a71cdbe9 633 if(interface == NULL)
Sergunb 0:8918a71cdbe9 634 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 635
Sergunb 0:8918a71cdbe9 636 //Make sure the PHY address is valid
Sergunb 0:8918a71cdbe9 637 if(phyAddr >= 32)
Sergunb 0:8918a71cdbe9 638 return ERROR_OUT_OF_RANGE;
Sergunb 0:8918a71cdbe9 639
Sergunb 0:8918a71cdbe9 640 //Get exclusive access
Sergunb 0:8918a71cdbe9 641 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 642 //Set PHY address
Sergunb 0:8918a71cdbe9 643 interface->phyAddr = phyAddr;
Sergunb 0:8918a71cdbe9 644 //Release exclusive access
Sergunb 0:8918a71cdbe9 645 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 646
Sergunb 0:8918a71cdbe9 647 //Successful processing
Sergunb 0:8918a71cdbe9 648 return NO_ERROR;
Sergunb 0:8918a71cdbe9 649 }
Sergunb 0:8918a71cdbe9 650
Sergunb 0:8918a71cdbe9 651
Sergunb 0:8918a71cdbe9 652 /**
Sergunb 0:8918a71cdbe9 653 * @brief Set SPI driver
Sergunb 0:8918a71cdbe9 654 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 655 * @param[in] driver Underlying SPI driver
Sergunb 0:8918a71cdbe9 656 * @return Error code
Sergunb 0:8918a71cdbe9 657 **/
Sergunb 0:8918a71cdbe9 658
Sergunb 0:8918a71cdbe9 659 error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
Sergunb 0:8918a71cdbe9 660 {
Sergunb 0:8918a71cdbe9 661 //Check parameters
Sergunb 0:8918a71cdbe9 662 if(interface == NULL || driver == NULL)
Sergunb 0:8918a71cdbe9 663 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 664
Sergunb 0:8918a71cdbe9 665 //Get exclusive access
Sergunb 0:8918a71cdbe9 666 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 667 //Set SPI driver
Sergunb 0:8918a71cdbe9 668 interface->spiDriver = driver;
Sergunb 0:8918a71cdbe9 669 //Release exclusive access
Sergunb 0:8918a71cdbe9 670 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 671
Sergunb 0:8918a71cdbe9 672 //Successful processing
Sergunb 0:8918a71cdbe9 673 return NO_ERROR;
Sergunb 0:8918a71cdbe9 674 }
Sergunb 0:8918a71cdbe9 675
Sergunb 0:8918a71cdbe9 676
Sergunb 0:8918a71cdbe9 677 /**
Sergunb 0:8918a71cdbe9 678 * @brief Set UART driver
Sergunb 0:8918a71cdbe9 679 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 680 * @param[in] driver Underlying UART driver
Sergunb 0:8918a71cdbe9 681 * @return Error code
Sergunb 0:8918a71cdbe9 682 **/
Sergunb 0:8918a71cdbe9 683
Sergunb 0:8918a71cdbe9 684 error_t netSetUartDriver(NetInterface *interface, const UartDriver *driver)
Sergunb 0:8918a71cdbe9 685 {
Sergunb 0:8918a71cdbe9 686 //Check parameters
Sergunb 0:8918a71cdbe9 687 if(interface == NULL || driver == NULL)
Sergunb 0:8918a71cdbe9 688 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 689
Sergunb 0:8918a71cdbe9 690 //Get exclusive access
Sergunb 0:8918a71cdbe9 691 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 692 //Set UART driver
Sergunb 0:8918a71cdbe9 693 interface->uartDriver = driver;
Sergunb 0:8918a71cdbe9 694 //Release exclusive access
Sergunb 0:8918a71cdbe9 695 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 696
Sergunb 0:8918a71cdbe9 697 //Successful processing
Sergunb 0:8918a71cdbe9 698 return NO_ERROR;
Sergunb 0:8918a71cdbe9 699 }
Sergunb 0:8918a71cdbe9 700
Sergunb 0:8918a71cdbe9 701
Sergunb 0:8918a71cdbe9 702 /**
Sergunb 0:8918a71cdbe9 703 * @brief Set external interrupt line driver
Sergunb 0:8918a71cdbe9 704 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 705 * @param[in] driver Underlying SPI driver
Sergunb 0:8918a71cdbe9 706 * @return Error code
Sergunb 0:8918a71cdbe9 707 **/
Sergunb 0:8918a71cdbe9 708
Sergunb 0:8918a71cdbe9 709 error_t netSetExtIntDriver(NetInterface *interface, const ExtIntDriver *driver)
Sergunb 0:8918a71cdbe9 710 {
Sergunb 0:8918a71cdbe9 711 //Check parameters
Sergunb 0:8918a71cdbe9 712 if(interface == NULL || driver == NULL)
Sergunb 0:8918a71cdbe9 713 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 714
Sergunb 0:8918a71cdbe9 715 //Get exclusive access
Sergunb 0:8918a71cdbe9 716 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 717 //Set external interrupt line driver
Sergunb 0:8918a71cdbe9 718 interface->extIntDriver = driver;
Sergunb 0:8918a71cdbe9 719 //Release exclusive access
Sergunb 0:8918a71cdbe9 720 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 721
Sergunb 0:8918a71cdbe9 722 //Successful processing
Sergunb 0:8918a71cdbe9 723 return NO_ERROR;
Sergunb 0:8918a71cdbe9 724 }
Sergunb 0:8918a71cdbe9 725
Sergunb 0:8918a71cdbe9 726
Sergunb 0:8918a71cdbe9 727 /**
Sergunb 0:8918a71cdbe9 728 * @brief Set link state (for virtual drivers only)
Sergunb 0:8918a71cdbe9 729 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 730 * @param[in] linkState Link state
Sergunb 0:8918a71cdbe9 731 * @return Error code
Sergunb 0:8918a71cdbe9 732 **/
Sergunb 0:8918a71cdbe9 733
Sergunb 0:8918a71cdbe9 734 error_t netSetLinkState(NetInterface *interface, NicLinkState linkState)
Sergunb 0:8918a71cdbe9 735 {
Sergunb 0:8918a71cdbe9 736 //Make sure the network interface is valid
Sergunb 0:8918a71cdbe9 737 if(interface == NULL)
Sergunb 0:8918a71cdbe9 738 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 739
Sergunb 0:8918a71cdbe9 740 //Get exclusive access
Sergunb 0:8918a71cdbe9 741 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 742
Sergunb 0:8918a71cdbe9 743 //Link state changed?
Sergunb 0:8918a71cdbe9 744 if(linkState != interface->linkState)
Sergunb 0:8918a71cdbe9 745 {
Sergunb 0:8918a71cdbe9 746 //Update link state
Sergunb 0:8918a71cdbe9 747 interface->linkState = linkState;
Sergunb 0:8918a71cdbe9 748 //Process link state change event
Sergunb 0:8918a71cdbe9 749 nicNotifyLinkChange(interface);
Sergunb 0:8918a71cdbe9 750 }
Sergunb 0:8918a71cdbe9 751
Sergunb 0:8918a71cdbe9 752 //Release exclusive access
Sergunb 0:8918a71cdbe9 753 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 754
Sergunb 0:8918a71cdbe9 755 //Successful processing
Sergunb 0:8918a71cdbe9 756 return NO_ERROR;
Sergunb 0:8918a71cdbe9 757 }
Sergunb 0:8918a71cdbe9 758
Sergunb 0:8918a71cdbe9 759
Sergunb 0:8918a71cdbe9 760 /**
Sergunb 0:8918a71cdbe9 761 * @brief Get link state
Sergunb 0:8918a71cdbe9 762 * @param[in] interface Pointer to the desired network interface
Sergunb 0:8918a71cdbe9 763 * @return Link state
Sergunb 0:8918a71cdbe9 764 **/
Sergunb 0:8918a71cdbe9 765
Sergunb 0:8918a71cdbe9 766 bool_t netGetLinkState(NetInterface *interface)
Sergunb 0:8918a71cdbe9 767 {
Sergunb 0:8918a71cdbe9 768 bool_t linkState;
Sergunb 0:8918a71cdbe9 769
Sergunb 0:8918a71cdbe9 770 //Make sure the network interface is valid
Sergunb 0:8918a71cdbe9 771 if(interface == NULL)
Sergunb 0:8918a71cdbe9 772 return FALSE;
Sergunb 0:8918a71cdbe9 773
Sergunb 0:8918a71cdbe9 774 //Get exclusive access
Sergunb 0:8918a71cdbe9 775 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 776 //Retrieve link state
Sergunb 0:8918a71cdbe9 777 linkState = interface->linkState;
Sergunb 0:8918a71cdbe9 778 //Release exclusive access
Sergunb 0:8918a71cdbe9 779 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 780
Sergunb 0:8918a71cdbe9 781 //Return link state
Sergunb 0:8918a71cdbe9 782 return linkState;
Sergunb 0:8918a71cdbe9 783 }
Sergunb 0:8918a71cdbe9 784
Sergunb 0:8918a71cdbe9 785
Sergunb 0:8918a71cdbe9 786 /**
Sergunb 0:8918a71cdbe9 787 * @brief Configure network interface
Sergunb 0:8918a71cdbe9 788 * @param[in] interface Network interface to configure
Sergunb 0:8918a71cdbe9 789 * @return Error code
Sergunb 0:8918a71cdbe9 790 **/
Sergunb 0:8918a71cdbe9 791
Sergunb 0:8918a71cdbe9 792 error_t netConfigInterface(NetInterface *interface)
Sergunb 0:8918a71cdbe9 793 {
Sergunb 0:8918a71cdbe9 794 error_t error;
Sergunb 0:8918a71cdbe9 795
Sergunb 0:8918a71cdbe9 796 //Make sure the network interface is valid
Sergunb 0:8918a71cdbe9 797 if(interface == NULL)
Sergunb 0:8918a71cdbe9 798 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 799
Sergunb 0:8918a71cdbe9 800 //Get exclusive access
Sergunb 0:8918a71cdbe9 801 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 802
Sergunb 0:8918a71cdbe9 803 //Disable hardware interrupts
Sergunb 0:8918a71cdbe9 804 interface->nicDriver->disableIrq(interface);
Sergunb 0:8918a71cdbe9 805
Sergunb 0:8918a71cdbe9 806 //Start of exception handling block
Sergunb 0:8918a71cdbe9 807 do
Sergunb 0:8918a71cdbe9 808 {
Sergunb 0:8918a71cdbe9 809 //Receive notifications when the transmitter is ready to send
Sergunb 0:8918a71cdbe9 810 if(!osCreateEvent(&interface->nicTxEvent))
Sergunb 0:8918a71cdbe9 811 {
Sergunb 0:8918a71cdbe9 812 //Failed to create event object
Sergunb 0:8918a71cdbe9 813 error = ERROR_OUT_OF_RESOURCES;
Sergunb 0:8918a71cdbe9 814 //Stop immediately
Sergunb 0:8918a71cdbe9 815 break;
Sergunb 0:8918a71cdbe9 816 }
Sergunb 0:8918a71cdbe9 817
Sergunb 0:8918a71cdbe9 818 //Network controller initialization
Sergunb 0:8918a71cdbe9 819 error = interface->nicDriver->init(interface);
Sergunb 0:8918a71cdbe9 820 //Any error to report?
Sergunb 0:8918a71cdbe9 821 if(error)
Sergunb 0:8918a71cdbe9 822 break;
Sergunb 0:8918a71cdbe9 823
Sergunb 0:8918a71cdbe9 824 #if (ETH_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 825 //Ethernet related initialization
Sergunb 0:8918a71cdbe9 826 error = ethInit(interface);
Sergunb 0:8918a71cdbe9 827 //Any error to report?
Sergunb 0:8918a71cdbe9 828 if(error)
Sergunb 0:8918a71cdbe9 829 break;
Sergunb 0:8918a71cdbe9 830
Sergunb 0:8918a71cdbe9 831 //Interface's physical address
Sergunb 0:8918a71cdbe9 832 MIB2_SET_OCTET_STRING(interface->mibIfEntry->ifPhysAddress, &interface->macAddr, 6);
Sergunb 0:8918a71cdbe9 833 MIB2_SET_OCTET_STRING_LEN(interface->mibIfEntry->ifPhysAddressLen, 6);
Sergunb 0:8918a71cdbe9 834 #endif
Sergunb 0:8918a71cdbe9 835
Sergunb 0:8918a71cdbe9 836 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 837 //IPv4 initialization
Sergunb 0:8918a71cdbe9 838 error = ipv4Init(interface);
Sergunb 0:8918a71cdbe9 839 //Any error to report?
Sergunb 0:8918a71cdbe9 840 if(error)
Sergunb 0:8918a71cdbe9 841 break;
Sergunb 0:8918a71cdbe9 842
Sergunb 0:8918a71cdbe9 843 #if (ETH_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 844 //ARP cache initialization
Sergunb 0:8918a71cdbe9 845 error = arpInit(interface);
Sergunb 0:8918a71cdbe9 846 //Any error to report?
Sergunb 0:8918a71cdbe9 847 if(error)
Sergunb 0:8918a71cdbe9 848 break;
Sergunb 0:8918a71cdbe9 849 #endif
Sergunb 0:8918a71cdbe9 850
Sergunb 0:8918a71cdbe9 851 #if (IGMP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 852 //IGMP related initialization
Sergunb 0:8918a71cdbe9 853 error = igmpInit(interface);
Sergunb 0:8918a71cdbe9 854 //Any error to report?
Sergunb 0:8918a71cdbe9 855 if(error)
Sergunb 0:8918a71cdbe9 856 break;
Sergunb 0:8918a71cdbe9 857
Sergunb 0:8918a71cdbe9 858 //Join the all-systems group
Sergunb 0:8918a71cdbe9 859 error = ipv4JoinMulticastGroup(interface, IGMP_ALL_SYSTEMS_ADDR);
Sergunb 0:8918a71cdbe9 860 //Any error to report?
Sergunb 0:8918a71cdbe9 861 if(error)
Sergunb 0:8918a71cdbe9 862 break;
Sergunb 0:8918a71cdbe9 863 #endif
Sergunb 0:8918a71cdbe9 864
Sergunb 0:8918a71cdbe9 865 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 866 //NetBIOS Name Service related initialization
Sergunb 0:8918a71cdbe9 867 error = nbnsInit(interface);
Sergunb 0:8918a71cdbe9 868 //Any error to report?
Sergunb 0:8918a71cdbe9 869 if(error)
Sergunb 0:8918a71cdbe9 870 break;
Sergunb 0:8918a71cdbe9 871 #endif
Sergunb 0:8918a71cdbe9 872 #endif
Sergunb 0:8918a71cdbe9 873
Sergunb 0:8918a71cdbe9 874 #if (IPV6_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 875 //IPv6 initialization
Sergunb 0:8918a71cdbe9 876 error = ipv6Init(interface);
Sergunb 0:8918a71cdbe9 877 //Any error to report?
Sergunb 0:8918a71cdbe9 878 if(error)
Sergunb 0:8918a71cdbe9 879 break;
Sergunb 0:8918a71cdbe9 880
Sergunb 0:8918a71cdbe9 881 #if (NDP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 882 //NDP related initialization
Sergunb 0:8918a71cdbe9 883 error = ndpInit(interface);
Sergunb 0:8918a71cdbe9 884 //Any error to report?
Sergunb 0:8918a71cdbe9 885 if(error)
Sergunb 0:8918a71cdbe9 886 break;
Sergunb 0:8918a71cdbe9 887 #endif
Sergunb 0:8918a71cdbe9 888
Sergunb 0:8918a71cdbe9 889 #if (MLD_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 890 //MLD related initialization
Sergunb 0:8918a71cdbe9 891 error = mldInit(interface);
Sergunb 0:8918a71cdbe9 892 //Any error to report?
Sergunb 0:8918a71cdbe9 893 if(error)
Sergunb 0:8918a71cdbe9 894 break;
Sergunb 0:8918a71cdbe9 895 #endif
Sergunb 0:8918a71cdbe9 896
Sergunb 0:8918a71cdbe9 897 //Join the All-Nodes multicast address
Sergunb 0:8918a71cdbe9 898 error = ipv6JoinMulticastGroup(interface, &IPV6_LINK_LOCAL_ALL_NODES_ADDR);
Sergunb 0:8918a71cdbe9 899 //Any error to report?
Sergunb 0:8918a71cdbe9 900 if(error)
Sergunb 0:8918a71cdbe9 901 break;
Sergunb 0:8918a71cdbe9 902 #endif
Sergunb 0:8918a71cdbe9 903
Sergunb 0:8918a71cdbe9 904 #if (MDNS_CLIENT_SUPPORT == ENABLED || MDNS_RESPONDER_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 905 //mDNS related initialization
Sergunb 0:8918a71cdbe9 906 error = mdnsInit(interface);
Sergunb 0:8918a71cdbe9 907 //Any error to report?
Sergunb 0:8918a71cdbe9 908 if(error)
Sergunb 0:8918a71cdbe9 909 break;
Sergunb 0:8918a71cdbe9 910 #endif
Sergunb 0:8918a71cdbe9 911
Sergunb 0:8918a71cdbe9 912 //End of exception handling block
Sergunb 0:8918a71cdbe9 913 } while(0);
Sergunb 0:8918a71cdbe9 914
Sergunb 0:8918a71cdbe9 915 //Check status code
Sergunb 0:8918a71cdbe9 916 if(!error)
Sergunb 0:8918a71cdbe9 917 {
Sergunb 0:8918a71cdbe9 918 //The network interface is now fully configured
Sergunb 0:8918a71cdbe9 919 interface->configured = TRUE;
Sergunb 0:8918a71cdbe9 920
Sergunb 0:8918a71cdbe9 921 //Check whether the TCP/IP process is running
Sergunb 0:8918a71cdbe9 922 if(netTaskRunning)
Sergunb 0:8918a71cdbe9 923 {
Sergunb 0:8918a71cdbe9 924 //Interrupts can be safely enabled
Sergunb 0:8918a71cdbe9 925 interface->nicDriver->enableIrq(interface);
Sergunb 0:8918a71cdbe9 926 }
Sergunb 0:8918a71cdbe9 927 }
Sergunb 0:8918a71cdbe9 928 else
Sergunb 0:8918a71cdbe9 929 {
Sergunb 0:8918a71cdbe9 930 //Clean up side effects before returning
Sergunb 0:8918a71cdbe9 931 osDeleteEvent(&interface->nicTxEvent);
Sergunb 0:8918a71cdbe9 932 }
Sergunb 0:8918a71cdbe9 933
Sergunb 0:8918a71cdbe9 934 //Release exclusive access
Sergunb 0:8918a71cdbe9 935 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 936
Sergunb 0:8918a71cdbe9 937 //Return status code
Sergunb 0:8918a71cdbe9 938 return error;
Sergunb 0:8918a71cdbe9 939 }
Sergunb 0:8918a71cdbe9 940
Sergunb 0:8918a71cdbe9 941
Sergunb 0:8918a71cdbe9 942 /**
Sergunb 0:8918a71cdbe9 943 * @brief TCP/IP events handling
Sergunb 0:8918a71cdbe9 944 **/
Sergunb 0:8918a71cdbe9 945
Sergunb 0:8918a71cdbe9 946 void netTask(void)
Sergunb 0:8918a71cdbe9 947 {
Sergunb 0:8918a71cdbe9 948 uint_t i;
Sergunb 0:8918a71cdbe9 949 bool_t status;
Sergunb 0:8918a71cdbe9 950 systime_t time;
Sergunb 0:8918a71cdbe9 951 systime_t timeout;
Sergunb 0:8918a71cdbe9 952 NetInterface *interface;
Sergunb 0:8918a71cdbe9 953
Sergunb 0:8918a71cdbe9 954 #if (NET_RTOS_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 955 //Get exclusive access
Sergunb 0:8918a71cdbe9 956 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 957
Sergunb 0:8918a71cdbe9 958 //The TCP/IP process is now running
Sergunb 0:8918a71cdbe9 959 netTaskRunning = TRUE;
Sergunb 0:8918a71cdbe9 960
Sergunb 0:8918a71cdbe9 961 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 962 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 963 {
Sergunb 0:8918a71cdbe9 964 //Point to the current network interface
Sergunb 0:8918a71cdbe9 965 interface = &netInterface[i];
Sergunb 0:8918a71cdbe9 966
Sergunb 0:8918a71cdbe9 967 //Check whether the interface is fully configured
Sergunb 0:8918a71cdbe9 968 if(interface->configured)
Sergunb 0:8918a71cdbe9 969 {
Sergunb 0:8918a71cdbe9 970 //Interrupts can be safely enabled
Sergunb 0:8918a71cdbe9 971 interface->nicDriver->enableIrq(interface);
Sergunb 0:8918a71cdbe9 972 }
Sergunb 0:8918a71cdbe9 973 }
Sergunb 0:8918a71cdbe9 974
Sergunb 0:8918a71cdbe9 975 //Release exclusive access
Sergunb 0:8918a71cdbe9 976 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 977
Sergunb 0:8918a71cdbe9 978 //Main loop
Sergunb 0:8918a71cdbe9 979 while(1)
Sergunb 0:8918a71cdbe9 980 {
Sergunb 0:8918a71cdbe9 981 #endif
Sergunb 0:8918a71cdbe9 982 //Get current time
Sergunb 0:8918a71cdbe9 983 time = osGetSystemTime();
Sergunb 0:8918a71cdbe9 984
Sergunb 0:8918a71cdbe9 985 //Compute the maximum blocking time when waiting for an event
Sergunb 0:8918a71cdbe9 986 if(timeCompare(time, netTimestamp) < 0)
Sergunb 0:8918a71cdbe9 987 timeout = netTimestamp - time;
Sergunb 0:8918a71cdbe9 988 else
Sergunb 0:8918a71cdbe9 989 timeout = 0;
Sergunb 0:8918a71cdbe9 990
Sergunb 0:8918a71cdbe9 991 //Receive notifications when a frame has been received, or the
Sergunb 0:8918a71cdbe9 992 //link state of any network interfaces has changed
Sergunb 0:8918a71cdbe9 993 status = osWaitForEvent(&netEvent, timeout);
Sergunb 0:8918a71cdbe9 994
Sergunb 0:8918a71cdbe9 995 //Check whether the specified event is in signaled state
Sergunb 0:8918a71cdbe9 996 if(status)
Sergunb 0:8918a71cdbe9 997 {
Sergunb 0:8918a71cdbe9 998 //Get exclusive access
Sergunb 0:8918a71cdbe9 999 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 1000
Sergunb 0:8918a71cdbe9 1001 //Process events
Sergunb 0:8918a71cdbe9 1002 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1003 {
Sergunb 0:8918a71cdbe9 1004 //Point to the current network interface
Sergunb 0:8918a71cdbe9 1005 interface = &netInterface[i];
Sergunb 0:8918a71cdbe9 1006
Sergunb 0:8918a71cdbe9 1007 //Check whether a NIC event is pending
Sergunb 0:8918a71cdbe9 1008 if(interface->nicEvent)
Sergunb 0:8918a71cdbe9 1009 {
Sergunb 0:8918a71cdbe9 1010 //Acknowledge the event by clearing the flag
Sergunb 0:8918a71cdbe9 1011 interface->nicEvent = FALSE;
Sergunb 0:8918a71cdbe9 1012
Sergunb 0:8918a71cdbe9 1013 //Disable hardware interrupts
Sergunb 0:8918a71cdbe9 1014 interface->nicDriver->disableIrq(interface);
Sergunb 0:8918a71cdbe9 1015 //Handle NIC events
Sergunb 0:8918a71cdbe9 1016 interface->nicDriver->eventHandler(interface);
Sergunb 0:8918a71cdbe9 1017 //Re-enable hardware interrupts
Sergunb 0:8918a71cdbe9 1018 interface->nicDriver->enableIrq(interface);
Sergunb 0:8918a71cdbe9 1019 }
Sergunb 0:8918a71cdbe9 1020
Sergunb 0:8918a71cdbe9 1021 //Check whether a PHY event is pending
Sergunb 0:8918a71cdbe9 1022 if(interface->phyEvent)
Sergunb 0:8918a71cdbe9 1023 {
Sergunb 0:8918a71cdbe9 1024 //Acknowledge the event by clearing the flag
Sergunb 0:8918a71cdbe9 1025 interface->phyEvent = FALSE;
Sergunb 0:8918a71cdbe9 1026
Sergunb 0:8918a71cdbe9 1027 //Disable hardware interrupts
Sergunb 0:8918a71cdbe9 1028 interface->nicDriver->disableIrq(interface);
Sergunb 0:8918a71cdbe9 1029 //Handle PHY events
Sergunb 0:8918a71cdbe9 1030 interface->phyDriver->eventHandler(interface);
Sergunb 0:8918a71cdbe9 1031 //Re-enable hardware interrupts
Sergunb 0:8918a71cdbe9 1032 interface->nicDriver->enableIrq(interface);
Sergunb 0:8918a71cdbe9 1033 }
Sergunb 0:8918a71cdbe9 1034 }
Sergunb 0:8918a71cdbe9 1035
Sergunb 0:8918a71cdbe9 1036 //Release exclusive access
Sergunb 0:8918a71cdbe9 1037 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 1038 }
Sergunb 0:8918a71cdbe9 1039
Sergunb 0:8918a71cdbe9 1040 //Check current time
Sergunb 0:8918a71cdbe9 1041 if(timeCompare(time, netTimestamp) > 0)
Sergunb 0:8918a71cdbe9 1042 {
Sergunb 0:8918a71cdbe9 1043 //Get exclusive access
Sergunb 0:8918a71cdbe9 1044 osAcquireMutex(&netMutex);
Sergunb 0:8918a71cdbe9 1045 //Handle periodic operations
Sergunb 0:8918a71cdbe9 1046 netTick();
Sergunb 0:8918a71cdbe9 1047 //Release exclusive access
Sergunb 0:8918a71cdbe9 1048 osReleaseMutex(&netMutex);
Sergunb 0:8918a71cdbe9 1049
Sergunb 0:8918a71cdbe9 1050 //Next event
Sergunb 0:8918a71cdbe9 1051 netTimestamp = time + NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1052 }
Sergunb 0:8918a71cdbe9 1053 #if (NET_RTOS_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1054 }
Sergunb 0:8918a71cdbe9 1055 #endif
Sergunb 0:8918a71cdbe9 1056 }
Sergunb 0:8918a71cdbe9 1057
Sergunb 0:8918a71cdbe9 1058
Sergunb 0:8918a71cdbe9 1059 /**
Sergunb 0:8918a71cdbe9 1060 * @brief Manage TCP/IP timers
Sergunb 0:8918a71cdbe9 1061 **/
Sergunb 0:8918a71cdbe9 1062
Sergunb 0:8918a71cdbe9 1063 void netTick(void)
Sergunb 0:8918a71cdbe9 1064 {
Sergunb 0:8918a71cdbe9 1065 uint_t i;
Sergunb 0:8918a71cdbe9 1066
Sergunb 0:8918a71cdbe9 1067 //Increment tick counter
Sergunb 0:8918a71cdbe9 1068 nicTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1069
Sergunb 0:8918a71cdbe9 1070 //Handle periodic operations such as polling the link state
Sergunb 0:8918a71cdbe9 1071 if(nicTickCounter >= NIC_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1072 {
Sergunb 0:8918a71cdbe9 1073 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1074 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1075 {
Sergunb 0:8918a71cdbe9 1076 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1077 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1078 nicTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1079 }
Sergunb 0:8918a71cdbe9 1080
Sergunb 0:8918a71cdbe9 1081 //Reset tick counter
Sergunb 0:8918a71cdbe9 1082 nicTickCounter = 0;
Sergunb 0:8918a71cdbe9 1083 }
Sergunb 0:8918a71cdbe9 1084
Sergunb 0:8918a71cdbe9 1085 #if (PPP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1086 //Increment tick counter
Sergunb 0:8918a71cdbe9 1087 pppTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1088
Sergunb 0:8918a71cdbe9 1089 //Manage PPP related timers
Sergunb 0:8918a71cdbe9 1090 if(pppTickCounter >= PPP_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1091 {
Sergunb 0:8918a71cdbe9 1092 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1093 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1094 {
Sergunb 0:8918a71cdbe9 1095 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1096 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1097 pppTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1098 }
Sergunb 0:8918a71cdbe9 1099
Sergunb 0:8918a71cdbe9 1100 //Reset tick counter
Sergunb 0:8918a71cdbe9 1101 pppTickCounter = 0;
Sergunb 0:8918a71cdbe9 1102 }
Sergunb 0:8918a71cdbe9 1103 #endif
Sergunb 0:8918a71cdbe9 1104
Sergunb 0:8918a71cdbe9 1105 #if (IPV4_SUPPORT == ENABLED && ETH_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1106 //Increment tick counter
Sergunb 0:8918a71cdbe9 1107 arpTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1108
Sergunb 0:8918a71cdbe9 1109 //Manage ARP cache
Sergunb 0:8918a71cdbe9 1110 if(arpTickCounter >= ARP_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1111 {
Sergunb 0:8918a71cdbe9 1112 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1113 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1114 {
Sergunb 0:8918a71cdbe9 1115 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1116 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1117 arpTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1118 }
Sergunb 0:8918a71cdbe9 1119
Sergunb 0:8918a71cdbe9 1120 //Reset tick counter
Sergunb 0:8918a71cdbe9 1121 arpTickCounter = 0;
Sergunb 0:8918a71cdbe9 1122 }
Sergunb 0:8918a71cdbe9 1123 #endif
Sergunb 0:8918a71cdbe9 1124
Sergunb 0:8918a71cdbe9 1125 #if (IPV4_SUPPORT == ENABLED && IPV4_FRAG_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1126 //Increment tick counter
Sergunb 0:8918a71cdbe9 1127 ipv4FragTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1128
Sergunb 0:8918a71cdbe9 1129 //Handle IPv4 fragment reassembly timeout
Sergunb 0:8918a71cdbe9 1130 if(ipv4FragTickCounter >= IPV4_FRAG_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1131 {
Sergunb 0:8918a71cdbe9 1132 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1133 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1134 {
Sergunb 0:8918a71cdbe9 1135 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1136 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1137 ipv4FragTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1138 }
Sergunb 0:8918a71cdbe9 1139
Sergunb 0:8918a71cdbe9 1140 //Reset tick counter
Sergunb 0:8918a71cdbe9 1141 ipv4FragTickCounter = 0;
Sergunb 0:8918a71cdbe9 1142 }
Sergunb 0:8918a71cdbe9 1143 #endif
Sergunb 0:8918a71cdbe9 1144
Sergunb 0:8918a71cdbe9 1145 #if (IPV4_SUPPORT == ENABLED && IGMP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1146 //Increment tick counter
Sergunb 0:8918a71cdbe9 1147 igmpTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1148
Sergunb 0:8918a71cdbe9 1149 //Handle IGMP related timers
Sergunb 0:8918a71cdbe9 1150 if(igmpTickCounter >= IGMP_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1151 {
Sergunb 0:8918a71cdbe9 1152 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1153 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1154 {
Sergunb 0:8918a71cdbe9 1155 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1156 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1157 igmpTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1158 }
Sergunb 0:8918a71cdbe9 1159
Sergunb 0:8918a71cdbe9 1160 //Reset tick counter
Sergunb 0:8918a71cdbe9 1161 igmpTickCounter = 0;
Sergunb 0:8918a71cdbe9 1162 }
Sergunb 0:8918a71cdbe9 1163 #endif
Sergunb 0:8918a71cdbe9 1164
Sergunb 0:8918a71cdbe9 1165 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1166 //Increment tick counter
Sergunb 0:8918a71cdbe9 1167 autoIpTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1168
Sergunb 0:8918a71cdbe9 1169 //Handle Auto-IP related timers
Sergunb 0:8918a71cdbe9 1170 if(autoIpTickCounter >= AUTO_IP_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1171 {
Sergunb 0:8918a71cdbe9 1172 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1173 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1174 autoIpTick(netInterface[i].autoIpContext);
Sergunb 0:8918a71cdbe9 1175
Sergunb 0:8918a71cdbe9 1176 //Reset tick counter
Sergunb 0:8918a71cdbe9 1177 autoIpTickCounter = 0;
Sergunb 0:8918a71cdbe9 1178 }
Sergunb 0:8918a71cdbe9 1179 #endif
Sergunb 0:8918a71cdbe9 1180
Sergunb 0:8918a71cdbe9 1181 #if (IPV4_SUPPORT == ENABLED && DHCP_CLIENT_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1182 //Increment tick counter
Sergunb 0:8918a71cdbe9 1183 dhcpClientTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1184
Sergunb 0:8918a71cdbe9 1185 //Handle DHCP client related timers
Sergunb 0:8918a71cdbe9 1186 if(dhcpClientTickCounter >= DHCP_CLIENT_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1187 {
Sergunb 0:8918a71cdbe9 1188 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1189 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1190 dhcpClientTick(netInterface[i].dhcpClientContext);
Sergunb 0:8918a71cdbe9 1191
Sergunb 0:8918a71cdbe9 1192 //Reset tick counter
Sergunb 0:8918a71cdbe9 1193 dhcpClientTickCounter = 0;
Sergunb 0:8918a71cdbe9 1194 }
Sergunb 0:8918a71cdbe9 1195 #endif
Sergunb 0:8918a71cdbe9 1196
Sergunb 0:8918a71cdbe9 1197 #if (IPV4_SUPPORT == ENABLED && DHCP_SERVER_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1198 //Increment tick counter
Sergunb 0:8918a71cdbe9 1199 dhcpServerTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1200
Sergunb 0:8918a71cdbe9 1201 //Handle DHCP server related timers
Sergunb 0:8918a71cdbe9 1202 if(dhcpServerTickCounter >= DHCP_SERVER_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1203 {
Sergunb 0:8918a71cdbe9 1204 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1205 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1206 dhcpServerTick(netInterface[i].dhcpServerContext);
Sergunb 0:8918a71cdbe9 1207
Sergunb 0:8918a71cdbe9 1208 //Reset tick counter
Sergunb 0:8918a71cdbe9 1209 dhcpServerTickCounter = 0;
Sergunb 0:8918a71cdbe9 1210 }
Sergunb 0:8918a71cdbe9 1211 #endif
Sergunb 0:8918a71cdbe9 1212
Sergunb 0:8918a71cdbe9 1213 #if (IPV6_SUPPORT == ENABLED && IPV6_FRAG_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1214 //Increment tick counter
Sergunb 0:8918a71cdbe9 1215 ipv6FragTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1216
Sergunb 0:8918a71cdbe9 1217 //Handle IPv6 fragment reassembly timeout
Sergunb 0:8918a71cdbe9 1218 if(ipv6FragTickCounter >= IPV6_FRAG_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1219 {
Sergunb 0:8918a71cdbe9 1220 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1221 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1222 {
Sergunb 0:8918a71cdbe9 1223 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1224 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1225 ipv6FragTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1226 }
Sergunb 0:8918a71cdbe9 1227
Sergunb 0:8918a71cdbe9 1228 //Reset tick counter
Sergunb 0:8918a71cdbe9 1229 ipv6FragTickCounter = 0;
Sergunb 0:8918a71cdbe9 1230 }
Sergunb 0:8918a71cdbe9 1231 #endif
Sergunb 0:8918a71cdbe9 1232
Sergunb 0:8918a71cdbe9 1233 #if (IPV6_SUPPORT == ENABLED && MLD_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1234 //Increment tick counter
Sergunb 0:8918a71cdbe9 1235 mldTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1236
Sergunb 0:8918a71cdbe9 1237 //Handle MLD related timers
Sergunb 0:8918a71cdbe9 1238 if(mldTickCounter >= MLD_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1239 {
Sergunb 0:8918a71cdbe9 1240 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1241 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1242 {
Sergunb 0:8918a71cdbe9 1243 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1244 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1245 mldTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1246 }
Sergunb 0:8918a71cdbe9 1247
Sergunb 0:8918a71cdbe9 1248 //Reset tick counter
Sergunb 0:8918a71cdbe9 1249 mldTickCounter = 0;
Sergunb 0:8918a71cdbe9 1250 }
Sergunb 0:8918a71cdbe9 1251 #endif
Sergunb 0:8918a71cdbe9 1252
Sergunb 0:8918a71cdbe9 1253 #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1254 //Increment tick counter
Sergunb 0:8918a71cdbe9 1255 ndpTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1256
Sergunb 0:8918a71cdbe9 1257 //Handle NDP related timers
Sergunb 0:8918a71cdbe9 1258 if(ndpTickCounter >= NDP_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1259 {
Sergunb 0:8918a71cdbe9 1260 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1261 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1262 {
Sergunb 0:8918a71cdbe9 1263 //Make sure the interface has been properly configured
Sergunb 0:8918a71cdbe9 1264 if(netInterface[i].configured)
Sergunb 0:8918a71cdbe9 1265 ndpTick(&netInterface[i]);
Sergunb 0:8918a71cdbe9 1266 }
Sergunb 0:8918a71cdbe9 1267
Sergunb 0:8918a71cdbe9 1268 //Reset tick counter
Sergunb 0:8918a71cdbe9 1269 ndpTickCounter = 0;
Sergunb 0:8918a71cdbe9 1270 }
Sergunb 0:8918a71cdbe9 1271 #endif
Sergunb 0:8918a71cdbe9 1272
Sergunb 0:8918a71cdbe9 1273 #if (IPV6_SUPPORT == ENABLED && NDP_ROUTER_ADV_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1274 //Increment tick counter
Sergunb 0:8918a71cdbe9 1275 ndpRouterAdvTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1276
Sergunb 0:8918a71cdbe9 1277 //Handle RA service related timers
Sergunb 0:8918a71cdbe9 1278 if(ndpRouterAdvTickCounter >= NDP_ROUTER_ADV_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1279 {
Sergunb 0:8918a71cdbe9 1280 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1281 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1282 ndpRouterAdvTick(netInterface[i].ndpRouterAdvContext);
Sergunb 0:8918a71cdbe9 1283
Sergunb 0:8918a71cdbe9 1284 //Reset tick counter
Sergunb 0:8918a71cdbe9 1285 ndpRouterAdvTickCounter = 0;
Sergunb 0:8918a71cdbe9 1286 }
Sergunb 0:8918a71cdbe9 1287 #endif
Sergunb 0:8918a71cdbe9 1288
Sergunb 0:8918a71cdbe9 1289 #if (IPV6_SUPPORT == ENABLED && DHCPV6_CLIENT_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1290 //Increment tick counter
Sergunb 0:8918a71cdbe9 1291 dhcpv6ClientTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1292
Sergunb 0:8918a71cdbe9 1293 //Handle DHCPv6 client related timers
Sergunb 0:8918a71cdbe9 1294 if(dhcpv6ClientTickCounter >= DHCPV6_CLIENT_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1295 {
Sergunb 0:8918a71cdbe9 1296 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1297 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1298 dhcpv6ClientTick(netInterface[i].dhcpv6ClientContext);
Sergunb 0:8918a71cdbe9 1299
Sergunb 0:8918a71cdbe9 1300 //Reset tick counter
Sergunb 0:8918a71cdbe9 1301 dhcpv6ClientTickCounter = 0;
Sergunb 0:8918a71cdbe9 1302 }
Sergunb 0:8918a71cdbe9 1303 #endif
Sergunb 0:8918a71cdbe9 1304
Sergunb 0:8918a71cdbe9 1305 #if (TCP_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1306 //Increment tick counter
Sergunb 0:8918a71cdbe9 1307 tcpTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1308
Sergunb 0:8918a71cdbe9 1309 //Manage TCP related timers
Sergunb 0:8918a71cdbe9 1310 if(tcpTickCounter >= TCP_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1311 {
Sergunb 0:8918a71cdbe9 1312 //TCP timer handler
Sergunb 0:8918a71cdbe9 1313 tcpTick();
Sergunb 0:8918a71cdbe9 1314 //Reset tick counter
Sergunb 0:8918a71cdbe9 1315 tcpTickCounter = 0;
Sergunb 0:8918a71cdbe9 1316 }
Sergunb 0:8918a71cdbe9 1317 #endif
Sergunb 0:8918a71cdbe9 1318
Sergunb 0:8918a71cdbe9 1319 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
Sergunb 0:8918a71cdbe9 1320 NBNS_CLIENT_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1321 //Increment tick counter
Sergunb 0:8918a71cdbe9 1322 dnsTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1323
Sergunb 0:8918a71cdbe9 1324 //Manage DNS cache
Sergunb 0:8918a71cdbe9 1325 if(dnsTickCounter >= DNS_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1326 {
Sergunb 0:8918a71cdbe9 1327 //DNS timer handler
Sergunb 0:8918a71cdbe9 1328 dnsTick();
Sergunb 0:8918a71cdbe9 1329 //Reset tick counter
Sergunb 0:8918a71cdbe9 1330 dnsTickCounter = 0;
Sergunb 0:8918a71cdbe9 1331 }
Sergunb 0:8918a71cdbe9 1332 #endif
Sergunb 0:8918a71cdbe9 1333
Sergunb 0:8918a71cdbe9 1334 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1335 //Increment tick counter
Sergunb 0:8918a71cdbe9 1336 mdnsResponderTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1337
Sergunb 0:8918a71cdbe9 1338 //Manage mDNS probing and announcing
Sergunb 0:8918a71cdbe9 1339 if(mdnsResponderTickCounter >= MDNS_RESPONDER_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1340 {
Sergunb 0:8918a71cdbe9 1341 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1342 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1343 mdnsResponderTick(netInterface[i].mdnsResponderContext);
Sergunb 0:8918a71cdbe9 1344
Sergunb 0:8918a71cdbe9 1345 //Reset tick counter
Sergunb 0:8918a71cdbe9 1346 mdnsResponderTickCounter = 0;
Sergunb 0:8918a71cdbe9 1347 }
Sergunb 0:8918a71cdbe9 1348 #endif
Sergunb 0:8918a71cdbe9 1349
Sergunb 0:8918a71cdbe9 1350 #if (DNS_SD_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 1351 //Increment tick counter
Sergunb 0:8918a71cdbe9 1352 dnsSdTickCounter += NET_TICK_INTERVAL;
Sergunb 0:8918a71cdbe9 1353
Sergunb 0:8918a71cdbe9 1354 //Manage DNS-SD probing and announcing
Sergunb 0:8918a71cdbe9 1355 if(dnsSdTickCounter >= DNS_SD_TICK_INTERVAL)
Sergunb 0:8918a71cdbe9 1356 {
Sergunb 0:8918a71cdbe9 1357 //Loop through network interfaces
Sergunb 0:8918a71cdbe9 1358 for(i = 0; i < NET_INTERFACE_COUNT; i++)
Sergunb 0:8918a71cdbe9 1359 dnsSdTick(netInterface[i].dnsSdContext);
Sergunb 0:8918a71cdbe9 1360
Sergunb 0:8918a71cdbe9 1361 //Reset tick counter
Sergunb 0:8918a71cdbe9 1362 dnsSdTickCounter = 0;
Sergunb 0:8918a71cdbe9 1363 }
Sergunb 0:8918a71cdbe9 1364 #endif
Sergunb 0:8918a71cdbe9 1365 }
Sergunb 0:8918a71cdbe9 1366
Sergunb 0:8918a71cdbe9 1367
Sergunb 0:8918a71cdbe9 1368 /**
Sergunb 0:8918a71cdbe9 1369 * @brief Get default network interface
Sergunb 0:8918a71cdbe9 1370 * @return Pointer to the default network interface to be used
Sergunb 0:8918a71cdbe9 1371 **/
Sergunb 0:8918a71cdbe9 1372
Sergunb 0:8918a71cdbe9 1373 NetInterface *netGetDefaultInterface(void)
Sergunb 0:8918a71cdbe9 1374 {
Sergunb 0:8918a71cdbe9 1375 //Default network interface
Sergunb 0:8918a71cdbe9 1376 return &netInterface[0];
Sergunb 0:8918a71cdbe9 1377 }
Sergunb 0:8918a71cdbe9 1378
Sergunb 0:8918a71cdbe9 1379
Sergunb 0:8918a71cdbe9 1380 /**
Sergunb 0:8918a71cdbe9 1381 * @brief Seed pseudo-random number generator
Sergunb 0:8918a71cdbe9 1382 * @param[in] seed An integer value to be used as seed by the pseudo-random number generator
Sergunb 0:8918a71cdbe9 1383 * @return Error code
Sergunb 0:8918a71cdbe9 1384 **/
Sergunb 0:8918a71cdbe9 1385
Sergunb 0:8918a71cdbe9 1386 error_t netInitRand(uint32_t seed)
Sergunb 0:8918a71cdbe9 1387 {
Sergunb 0:8918a71cdbe9 1388 //Seed the pseudo-random number generator
Sergunb 0:8918a71cdbe9 1389 prngState += seed;
Sergunb 0:8918a71cdbe9 1390
Sergunb 0:8918a71cdbe9 1391 //Successful processing
Sergunb 0:8918a71cdbe9 1392 return NO_ERROR;
Sergunb 0:8918a71cdbe9 1393 }
Sergunb 0:8918a71cdbe9 1394
Sergunb 0:8918a71cdbe9 1395
Sergunb 0:8918a71cdbe9 1396 /**
Sergunb 0:8918a71cdbe9 1397 * @brief Get a random value
Sergunb 0:8918a71cdbe9 1398 * @return Error code
Sergunb 0:8918a71cdbe9 1399 **/
Sergunb 0:8918a71cdbe9 1400
Sergunb 0:8918a71cdbe9 1401 uint32_t netGetRand(void)
Sergunb 0:8918a71cdbe9 1402 {
Sergunb 0:8918a71cdbe9 1403 uint32_t result;
Sergunb 0:8918a71cdbe9 1404
Sergunb 0:8918a71cdbe9 1405 //Use a linear congruential generator (LCG) to update the state of the PRNG
Sergunb 0:8918a71cdbe9 1406 prngState *= 1103515245;
Sergunb 0:8918a71cdbe9 1407 prngState += 12345;
Sergunb 0:8918a71cdbe9 1408 result = (prngState >> 16) & 0x07FF;
Sergunb 0:8918a71cdbe9 1409
Sergunb 0:8918a71cdbe9 1410 prngState *= 1103515245;
Sergunb 0:8918a71cdbe9 1411 prngState += 12345;
Sergunb 0:8918a71cdbe9 1412 result <<= 10;
Sergunb 0:8918a71cdbe9 1413 result |= (prngState >> 16) & 0x03FF;
Sergunb 0:8918a71cdbe9 1414
Sergunb 0:8918a71cdbe9 1415 prngState *= 1103515245;
Sergunb 0:8918a71cdbe9 1416 prngState += 12345;
Sergunb 0:8918a71cdbe9 1417 result <<= 10;
Sergunb 0:8918a71cdbe9 1418 result |= (prngState >> 16) & 0x03FF;
Sergunb 0:8918a71cdbe9 1419
Sergunb 0:8918a71cdbe9 1420 //Return the resulting value
Sergunb 0:8918a71cdbe9 1421 return result;
Sergunb 0:8918a71cdbe9 1422 }
Sergunb 0:8918a71cdbe9 1423
Sergunb 0:8918a71cdbe9 1424
Sergunb 0:8918a71cdbe9 1425 /**
Sergunb 0:8918a71cdbe9 1426 * @brief Get a random value in the specified range
Sergunb 0:8918a71cdbe9 1427 * @param[in] min Lower bound
Sergunb 0:8918a71cdbe9 1428 * @param[in] max Upper bound
Sergunb 0:8918a71cdbe9 1429 * @return Random value in the specified range
Sergunb 0:8918a71cdbe9 1430 **/
Sergunb 0:8918a71cdbe9 1431
Sergunb 0:8918a71cdbe9 1432 int32_t netGetRandRange(int32_t min, int32_t max)
Sergunb 0:8918a71cdbe9 1433 {
Sergunb 0:8918a71cdbe9 1434 //Return a random value in the given range
Sergunb 0:8918a71cdbe9 1435 return min + netGetRand() % (max - min + 1);
Sergunb 0:8918a71cdbe9 1436 }
Sergunb 0:8918a71cdbe9 1437
Sergunb 0:8918a71cdbe9 1438
Sergunb 0:8918a71cdbe9 1439 /**
Sergunb 0:8918a71cdbe9 1440 * @brief Register link change callback
Sergunb 0:8918a71cdbe9 1441 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 1442 * @param[in] callback Callback function to be called when the link state changed
Sergunb 0:8918a71cdbe9 1443 * @param[in] params Callback function parameter (optional)
Sergunb 0:8918a71cdbe9 1444 * @param[out] cookie Identifier that can be used to unregister the callback function
Sergunb 0:8918a71cdbe9 1445 * @return Error code
Sergunb 0:8918a71cdbe9 1446 **/
Sergunb 0:8918a71cdbe9 1447
Sergunb 0:8918a71cdbe9 1448 error_t netAttachLinkChangeCallback(NetInterface *interface,
Sergunb 0:8918a71cdbe9 1449 LinkChangeCallback callback, void *params, uint_t *cookie)
Sergunb 0:8918a71cdbe9 1450 {
Sergunb 0:8918a71cdbe9 1451 uint_t i;
Sergunb 0:8918a71cdbe9 1452 LinkChangeCallbackDesc *entry;
Sergunb 0:8918a71cdbe9 1453
Sergunb 0:8918a71cdbe9 1454 //Acquire exclusive access to the callback table
Sergunb 0:8918a71cdbe9 1455 osAcquireMutex(&callbackTableMutex);
Sergunb 0:8918a71cdbe9 1456
Sergunb 0:8918a71cdbe9 1457 //Loop through the table
Sergunb 0:8918a71cdbe9 1458 for(i = 0; i < NET_CALLBACK_TABLE_SIZE; i++)
Sergunb 0:8918a71cdbe9 1459 {
Sergunb 0:8918a71cdbe9 1460 //Point to the current entry
Sergunb 0:8918a71cdbe9 1461 entry = &callbackTable[i];
Sergunb 0:8918a71cdbe9 1462
Sergunb 0:8918a71cdbe9 1463 //Check whether the entry is currently in used
Sergunb 0:8918a71cdbe9 1464 if(entry->callback == NULL)
Sergunb 0:8918a71cdbe9 1465 {
Sergunb 0:8918a71cdbe9 1466 //Create a new entry
Sergunb 0:8918a71cdbe9 1467 entry->interface = interface;
Sergunb 0:8918a71cdbe9 1468 entry->callback = callback;
Sergunb 0:8918a71cdbe9 1469 entry->params = params;
Sergunb 0:8918a71cdbe9 1470 //We are done
Sergunb 0:8918a71cdbe9 1471 break;
Sergunb 0:8918a71cdbe9 1472 }
Sergunb 0:8918a71cdbe9 1473 }
Sergunb 0:8918a71cdbe9 1474
Sergunb 0:8918a71cdbe9 1475 //Release exclusive access to the callback table
Sergunb 0:8918a71cdbe9 1476 osReleaseMutex(&callbackTableMutex);
Sergunb 0:8918a71cdbe9 1477
Sergunb 0:8918a71cdbe9 1478 //Failed to attach the specified user callback?
Sergunb 0:8918a71cdbe9 1479 if(i >= NET_CALLBACK_TABLE_SIZE)
Sergunb 0:8918a71cdbe9 1480 return ERROR_OUT_OF_RESOURCES;
Sergunb 0:8918a71cdbe9 1481
Sergunb 0:8918a71cdbe9 1482 //Return a cookie that can be used later to unregister the callback
Sergunb 0:8918a71cdbe9 1483 if(cookie != NULL)
Sergunb 0:8918a71cdbe9 1484 *cookie = i;
Sergunb 0:8918a71cdbe9 1485
Sergunb 0:8918a71cdbe9 1486 //Successful processing
Sergunb 0:8918a71cdbe9 1487 return NO_ERROR;
Sergunb 0:8918a71cdbe9 1488 }
Sergunb 0:8918a71cdbe9 1489
Sergunb 0:8918a71cdbe9 1490
Sergunb 0:8918a71cdbe9 1491 /**
Sergunb 0:8918a71cdbe9 1492 * @brief Unregister link change callback
Sergunb 0:8918a71cdbe9 1493 * @param[in] cookie Identifier specifying the callback to be unregistered
Sergunb 0:8918a71cdbe9 1494 * @return Error code
Sergunb 0:8918a71cdbe9 1495 **/
Sergunb 0:8918a71cdbe9 1496
Sergunb 0:8918a71cdbe9 1497 error_t netDetachLinkChangeCallback(uint_t cookie)
Sergunb 0:8918a71cdbe9 1498 {
Sergunb 0:8918a71cdbe9 1499 //Make sure the cookie is valid
Sergunb 0:8918a71cdbe9 1500 if(cookie >= NET_CALLBACK_TABLE_SIZE)
Sergunb 0:8918a71cdbe9 1501 return ERROR_INVALID_PARAMETER;
Sergunb 0:8918a71cdbe9 1502
Sergunb 0:8918a71cdbe9 1503 //Acquire exclusive access to the callback table
Sergunb 0:8918a71cdbe9 1504 osAcquireMutex(&callbackTableMutex);
Sergunb 0:8918a71cdbe9 1505 //Unregister user callback
Sergunb 0:8918a71cdbe9 1506 callbackTable[cookie].callback = NULL;
Sergunb 0:8918a71cdbe9 1507 //Release exclusive access to the callback table
Sergunb 0:8918a71cdbe9 1508 osReleaseMutex(&callbackTableMutex);
Sergunb 0:8918a71cdbe9 1509
Sergunb 0:8918a71cdbe9 1510 //Successful processing
Sergunb 0:8918a71cdbe9 1511 return NO_ERROR;
Sergunb 0:8918a71cdbe9 1512 }
Sergunb 0:8918a71cdbe9 1513
Sergunb 0:8918a71cdbe9 1514
Sergunb 0:8918a71cdbe9 1515 /**
Sergunb 0:8918a71cdbe9 1516 * @brief Invoke link change callback
Sergunb 0:8918a71cdbe9 1517 * @param[in] interface Underlying network interface
Sergunb 0:8918a71cdbe9 1518 * @param[in] linkState Link state
Sergunb 0:8918a71cdbe9 1519 **/
Sergunb 0:8918a71cdbe9 1520
Sergunb 0:8918a71cdbe9 1521 void netInvokeLinkChangeCallback(NetInterface *interface, bool_t linkState)
Sergunb 0:8918a71cdbe9 1522 {
Sergunb 0:8918a71cdbe9 1523 uint_t i;
Sergunb 0:8918a71cdbe9 1524 LinkChangeCallbackDesc *entry;
Sergunb 0:8918a71cdbe9 1525
Sergunb 0:8918a71cdbe9 1526 //Acquire exclusive access to the callback table
Sergunb 0:8918a71cdbe9 1527 osAcquireMutex(&callbackTableMutex);
Sergunb 0:8918a71cdbe9 1528
Sergunb 0:8918a71cdbe9 1529 //Loop through the table
Sergunb 0:8918a71cdbe9 1530 for(i = 0; i < NET_CALLBACK_TABLE_SIZE; i++)
Sergunb 0:8918a71cdbe9 1531 {
Sergunb 0:8918a71cdbe9 1532 //Point to the current entry
Sergunb 0:8918a71cdbe9 1533 entry = &callbackTable[i];
Sergunb 0:8918a71cdbe9 1534
Sergunb 0:8918a71cdbe9 1535 //Any registered callback?
Sergunb 0:8918a71cdbe9 1536 if(entry->callback != NULL)
Sergunb 0:8918a71cdbe9 1537 {
Sergunb 0:8918a71cdbe9 1538 //Check whether the network interface matches the current entry
Sergunb 0:8918a71cdbe9 1539 if(entry->interface == NULL || entry->interface == interface)
Sergunb 0:8918a71cdbe9 1540 {
Sergunb 0:8918a71cdbe9 1541 //Invoke user callback function
Sergunb 0:8918a71cdbe9 1542 entry->callback(interface, linkState, entry->params);
Sergunb 0:8918a71cdbe9 1543 }
Sergunb 0:8918a71cdbe9 1544 }
Sergunb 0:8918a71cdbe9 1545 }
Sergunb 0:8918a71cdbe9 1546
Sergunb 0:8918a71cdbe9 1547 //Release exclusive access to the callback table
Sergunb 0:8918a71cdbe9 1548 osReleaseMutex(&callbackTableMutex);
Sergunb 0:8918a71cdbe9 1549 }
Sergunb 0:8918a71cdbe9 1550