Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers net.h Source File

net.h

Go to the documentation of this file.
00001 /**
00002  * @file net.h
00003  * @brief TCP/IP stack core
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _NET_H
00030 #define _NET_H
00031 
00032 //Forward declaration of NetInterface structure
00033 struct _NetInterface;
00034 #define NetInterface struct _NetInterface
00035 
00036 //Dependencies
00037 #include "os_port.h"
00038 #include "net_config.h"
00039 #include "core/net_legacy.h"
00040 #include "core/net_mem.h"
00041 #include "core/nic.h"
00042 #include "core/ethernet.h"
00043 #include "ipv4/ipv4.h"
00044 #include "ipv4/ipv4_frag.h"
00045 #include "ipv4/auto_ip.h"
00046 #include "ipv6/ipv6.h"
00047 #include "ipv4/arp.h"
00048 #include "ipv6/ndp.h"
00049 #include "ipv6/ndp_router_adv.h"
00050 #include "ipv6/slaac.h"
00051 #include "ppp/ppp.h"
00052 #include "dhcp/dhcp_client.h"
00053 #include "dhcp/dhcp_server.h"
00054 #include "dhcpv6/dhcpv6_client.h"
00055 #include "dns/dns_client.h"
00056 #include "mdns/mdns_responder.h"
00057 #include "mdns/mdns_common.h"
00058 #include "dns_sd/dns_sd.h"
00059 #include "mibs/mib2_module.h"
00060 #include "cpu_endian.h"
00061 #include "error.h"
00062 
00063 //Version string
00064 #define NET_VERSION_STRING "1.7.6"
00065 //Major version
00066 #define NET_MAJOR_VERSION 1
00067 //Minor version
00068 #define NET_MINOR_VERSION 7
00069 //Revision number
00070 #define NET_REV_NUMBER 6
00071 
00072 //RTOS support
00073 #ifndef NET_RTOS_SUPPORT
00074    #define NET_RTOS_SUPPORT ENABLED
00075 #elif (NET_RTOS_SUPPORT != ENABLED && NET_RTOS_SUPPORT != DISABLED)
00076    #error NET_RTOS_SUPPORT parameter is not valid
00077 #endif
00078 
00079 //Number of network adapters
00080 #ifndef NET_INTERFACE_COUNT
00081    #define NET_INTERFACE_COUNT 1
00082 #elif (NET_INTERFACE_COUNT < 1)
00083    #error NET_INTERFACE_COUNT parameter is not valid
00084 #endif
00085 
00086 //Maximum number of callback functions that can be registered
00087 //to monitor link changes
00088 #ifndef NET_CALLBACK_TABLE_SIZE
00089    #define NET_CALLBACK_TABLE_SIZE 6
00090 #elif (NET_CALLBACK_TABLE_SIZE < 1)
00091    #error NET_CALLBACK_TABLE_SIZE parameter is not valid
00092 #endif
00093 
00094 //Maximum length of interface name
00095 #ifndef NET_MAX_IF_NAME_LEN
00096    #define NET_MAX_IF_NAME_LEN 8
00097 #elif (NET_MAX_IF_NAME_LEN < 1)
00098    #error NET_MAX_IF_NAME_LEN parameter is not valid
00099 #endif
00100 
00101 //Maximum length of host name
00102 #ifndef NET_MAX_HOSTNAME_LEN
00103    #define NET_MAX_HOSTNAME_LEN 16
00104 #elif (NET_MAX_HOSTNAME_LEN < 1)
00105    #error NET_MAX_HOSTNAME_LEN parameter is not valid
00106 #endif
00107 
00108 //Maximum length of proxy server name
00109 #ifndef NET_MAX_PROXY_NAME_LEN
00110    #define NET_MAX_PROXY_NAME_LEN 16
00111 #elif (NET_MAX_PROXY_NAME_LEN < 1)
00112    #error NET_MAX_PROXY_NAME_LEN parameter is not valid
00113 #endif
00114 
00115 //OS resources are statically allocated at compile time
00116 #ifndef NET_STATIC_OS_RESOURCES
00117    #define NET_STATIC_OS_RESOURCES DISABLED
00118 #elif (NET_STATIC_OS_RESOURCES != ENABLED && NET_STATIC_OS_RESOURCES != DISABLED)
00119    #error NET_STATIC_OS_RESOURCES parameter is not valid
00120 #endif
00121 
00122 //Stack size required to run the TCP/IP task
00123 #ifndef NET_TASK_STACK_SIZE
00124    #define NET_TASK_STACK_SIZE 650
00125 #elif (NET_TASK_STACK_SIZE < 1)
00126    #error NET_TASK_STACK_SIZE parameter is not valid
00127 #endif
00128 
00129 //Priority at which the TCP/IP task should run
00130 #ifndef NET_TASK_PRIORITY
00131    #define NET_TASK_PRIORITY OS_TASK_PRIORITY_HIGH
00132 #endif
00133 
00134 //TCP/IP stack tick interval
00135 #ifndef NET_TICK_INTERVAL
00136    #define NET_TICK_INTERVAL 100
00137 #elif (NET_TICK_INTERVAL < 10)
00138    #error NET_TICK_INTERVAL parameter is not valid
00139 #endif
00140 
00141 
00142 /**
00143  * @brief Structure describing a network interface
00144  **/
00145 
00146 struct _NetInterface
00147 {
00148    uint32_t id;                                   ///<A unique number identifying the interface
00149    Eui64 eui64;                                   ///<EUI-64 interface identifier
00150    char_t name[NET_MAX_IF_NAME_LEN + 1];          ///<A unique name identifying the interface
00151    char_t hostname[NET_MAX_HOSTNAME_LEN + 1];     ///<Host name
00152    char_t proxyName[NET_MAX_PROXY_NAME_LEN + 1];  ///<Proxy server name
00153    uint16_t proxyPort;                            ///<Proxy server port
00154    const NicDriver *nicDriver;                    ///<NIC driver
00155    const PhyDriver *phyDriver;                    ///<PHY driver
00156    uint8_t phyAddr;                               ///<PHY address
00157    const SpiDriver *spiDriver;                    ///<Underlying SPI driver
00158    const UartDriver *uartDriver;                  ///<Underlying UART driver
00159    const ExtIntDriver *extIntDriver;              ///<External interrupt line driver
00160    uint8_t nicContext[NIC_CONTEXT_SIZE];          ///<Driver specific context
00161    OsEvent nicTxEvent;                            ///<Network controller TX event
00162    bool_t nicEvent;                               ///<A NIC event is pending
00163    bool_t phyEvent;                               ///<A PHY event is pending
00164    bool_t linkState;                              ///<Link state
00165    uint32_t linkSpeed;                            ///<Link speed
00166    NicDuplexMode duplexMode;                      ///<Duplex mode
00167    bool_t configured;                             ///<Configuration done
00168 
00169 #if (ETH_SUPPORT == ENABLED)
00170    MacAddr macAddr;                               ///<Link-layer address
00171    MacFilterEntry macMulticastFilter[MAC_MULTICAST_FILTER_SIZE]; ///<Multicast MAC filter
00172 #endif
00173 
00174 #if (IPV4_SUPPORT == ENABLED)
00175    Ipv4Context ipv4Context;                       ///<IPv4 context
00176    ArpCacheEntry arpCache[ARP_CACHE_SIZE];        ///<ARP cache
00177 #if (IGMP_SUPPORT == ENABLED)
00178    systime_t igmpv1RouterPresentTimer;            ///<IGMPv1 router present timer
00179    bool_t igmpv1RouterPresent;                    ///<An IGMPv1 query has been recently heard
00180 #endif
00181 #if (AUTO_IP_SUPPORT == ENABLED)
00182    AutoIpContext *autoIpContext;                  ///<Auto-IP context
00183 #endif
00184 #if (DHCP_CLIENT_SUPPORT == ENABLED)
00185    DhcpClientContext *dhcpClientContext;          ///<DHCP client context
00186 #endif
00187 #if (DHCP_SERVER_SUPPORT == ENABLED)
00188    DhcpServerContext *dhcpServerContext;          ///<DHCP server context
00189 #endif
00190 #endif
00191 
00192 #if (IPV6_SUPPORT == ENABLED)
00193    Ipv6Context ipv6Context;                       ///<IPv6 context
00194 #if (NDP_SUPPORT == ENABLED)
00195    NdpContext ndpContext;                         ///<NDP context
00196 #endif
00197 #if (NDP_ROUTER_ADV_SUPPORT == ENABLED)
00198    NdpRouterAdvContext *ndpRouterAdvContext;      ///<RA service context
00199 #endif
00200 #if (SLAAC_SUPPORT == ENABLED)
00201    SlaacContext *slaacContext;                    ///<SLAAC context
00202 #endif
00203 #if (DHCPV6_CLIENT_SUPPORT == ENABLED)
00204    Dhcpv6ClientContext *dhcpv6ClientContext;      ///<DHCPv6 client context
00205 #endif
00206 #endif
00207 
00208 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
00209    MdnsResponderContext *mdnsResponderContext;    ///<mDNS responder context
00210 #endif
00211 
00212 #if (DNS_SD_SUPPORT == ENABLED)
00213    DnsSdContext *dnsSdContext;                    ///DNS-SD context
00214 #endif
00215 
00216 #if (PPP_SUPPORT == ENABLED)
00217    PppContext *pppContext;                        ///<PPP context
00218 #endif
00219 
00220 #if (MIB2_SUPPORT == ENABLED)
00221    Mib2IfEntry *mibIfEntry;
00222 #endif
00223 };
00224 
00225 
00226 /**
00227  * @brief Link change callback
00228  **/
00229 
00230 typedef void (*LinkChangeCallback)(NetInterface *interface, bool_t linkState, void *params);
00231 
00232 
00233 /**
00234  * @brief Entry describing a user callback
00235  **/
00236 
00237 typedef struct
00238 {
00239    NetInterface *interface;
00240    LinkChangeCallback callback;
00241    void *params;
00242 } LinkChangeCallbackDesc;
00243 
00244 
00245 //Global variables
00246 extern OsTask *netTaskHandle;
00247 extern OsMutex netMutex;
00248 extern OsEvent netEvent;
00249 extern NetInterface netInterface[NET_INTERFACE_COUNT];
00250 
00251 //TCP/IP stack related functions
00252 error_t netInit(void);
00253 
00254 error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr);
00255 error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr);
00256 
00257 error_t netSetEui64(NetInterface *interface, const Eui64 *eui64);
00258 error_t netGetEui64(NetInterface *interface, Eui64 *eui64);
00259 
00260 error_t netSetInterfaceId(NetInterface *interface, uint32_t id);
00261 error_t netSetInterfaceName(NetInterface *interface, const char_t *name);
00262 error_t netSetHostname(NetInterface *interface, const char_t *name);
00263 error_t netSetProxy(NetInterface *interface, const char_t *name, uint16_t port);
00264 
00265 error_t netSetDriver(NetInterface *interface, const NicDriver *driver);
00266 
00267 error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver);
00268 error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr);
00269 
00270 error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver);
00271 error_t netSetUartDriver(NetInterface *interface, const UartDriver *driver);
00272 error_t netSetExtIntDriver(NetInterface *interface, const ExtIntDriver *driver);
00273 
00274 error_t netSetLinkState(NetInterface *interface, NicLinkState linkState);
00275 bool_t netGetLinkState(NetInterface *interface);
00276 
00277 error_t netConfigInterface(NetInterface *interface);
00278 
00279 void netTask(void);
00280 void netTick(void);
00281 
00282 NetInterface *netGetDefaultInterface(void);
00283 
00284 error_t netInitRand(uint32_t seed);
00285 uint32_t netGetRand(void);
00286 int32_t netGetRandRange(int32_t min, int32_t max);
00287 
00288 error_t netAttachLinkChangeCallback(NetInterface *interface,
00289    LinkChangeCallback callback, void *params, uint_t *cookie);
00290 
00291 error_t netDetachLinkChangeCallback(uint_t cookie);
00292 
00293 void netInvokeLinkChangeCallback(NetInterface *interface, bool_t linkState);
00294 
00295 #endif
00296