Rewrite from scratch a TCP/IP stack for mbed. So far the following parts are usable: Drivers: - EMAC driver (from CMSIS 2.0) Protocols: - Ethernet protocol - ARP over ethernet for IPv4 - IPv4 over Ethernet - ICMPv4 over IPv4 - UDPv4 over IPv4 APIs: - Sockets for UDPv4 The structure of this stack is designed to be very modular. Each protocol can register one or more protocol to handle its payload, and in each protocol, an API can be hooked (like Sockets for example). This is an early release.
Revision 5:3cd83fcb1467, committed 2011-06-13
- Comitter:
- Benoit
- Date:
- Mon Jun 13 13:13:59 2011 +0000
- Parent:
- 4:cb3dc3361be5
- Child:
- 6:7f7f29fde21c
- Commit message:
- Renamed some types to avoid conflicts with other objects
Changed in this revision
--- a/ARP.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/ARP.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -3,566 +3,578 @@ * $Author: benoit $ * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $ * $Rev: 29 $ - * - * - * - * - * + * + * + * + * + * */ - + #include "ARP.h" #include "Ethernet.h" #include "Debug.h" #include "IPv4.h" #include <string.h> - -#define DEBUG_CURRENT_MODULE_NAME "ARP" -#define DEBUG_CURRENT_MODULE_ID DEBUG_MODULE_ARP - +#define DEBUG_CURRENT_MODULE_NAME "ARP" +#define DEBUG_CURRENT_MODULE_ID DEBUG_MODULE_ARP enum ARP_EntryStatus { - ARP_Entry_Free = 0, - ARP_Entry_PendingReply, - ARP_Entry_Dynamic, - ARP_Entry_Expired, - ARP_Entry_Static, - ARP_Entry_Count, + ARP_Entry_Free = 0, + ARP_Entry_PendingReply, + ARP_Entry_Dynamic, + ARP_Entry_Expired, + ARP_Entry_Static, + ARP_Entry_Count, }; typedef enum ARP_EntryStatus ARP_EntryStatus_t; - -const char *arpEntryStatusText[ARP_Entry_Count] = +const char *arpEntryStatusText[ARP_Entry_Count] = { - "free", - "pending", - "dynamic", - "expired", - "static", + "free", + "pending", + "dynamic", + "expired", + "static", }; struct ARP_CacheEntry { - IPv4_Addr_t ipv4Addr; - Ethernet_Addr_t ethernetAddr; - NetIF_t *netIF; - uint16_t age; - ARP_EntryStatus_t status; - RTOS_Mutex_t mutex; + IPv4_Addr_t ipv4Addr; + Ethernet_Addr_t ethernetAddr; + NetIF_t *netIF; + uint16_t age; + ARP_EntryStatus_t status; + RTOS_Mutex_t mutex; }; typedef struct ARP_CacheEntry ARP_CacheEntry_t; - #pragma push #pragma pack(1) -static struct +static struct { - Ethernet_Header_t ethernetHeader; - ARP_Header_t arpHeader; - ARP_IPv4Data_t ipv4ARPData; + Ethernet_Header_t ethernetHeader; + ARP_Header_t arpHeader; + ARP_IPv4Data_t ipv4ARPData; } arp_FullIPv4Packet; #pragma pop - static void Init(void); -static void Handler(NetIF_t *netIF, Packet_t *packet); +static void Handler(NetIF_t *netIF, NetPacket_t *packet); static void PeriodicFunction(void); static ARP_CacheEntry_t *GetReusableEntry(void); static ARP_CacheEntry_t *GetEntryByIPv4Address(IPv4_Addr_t address); - static ARP_CacheEntry_t arp_CacheTable[ARP_CACHE_MAX_ENTRIES]; - -Protocol_Handler_t arp = +Protocol_Handler_t arp = { - PROTOCOL_INDEX_NOT_INITIALIZED, /* Always PROTOCOL_INDEX_NOT_INITIALIZED at initialization */ - Protocol_ID_ARP, /* Protocol ID */ - htons(ETHERNET_PROTO_ARP), /* Protocol number */ - Init, /* Protocol initialisation function */ - Handler, /* Protocol handler */ - NULL, /* Protocol registration function */ - NULL, /* API registration function */ + PROTOCOL_INDEX_NOT_INITIALIZED, /* Always PROTOCOL_INDEX_NOT_INITIALIZED at initialization */ + Protocol_ID_ARP, /* Protocol ID */ + htons(ETHERNET_PROTO_ARP), /* Protocol number */ + Init, /* Protocol initialisation function */ + Handler, /* Protocol handler */ + NULL, /* Protocol registration function */ + NULL, /* API registration function */ }; - static void Init(void) { - int32_t index; - - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Initializing ARP layer")); - memset(arp_CacheTable, 0, sizeof(arp_CacheTable)); - for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) - { - arp_CacheTable[index].mutex = RTOS_MUTEX_CREATE(); - } - NetIF_RegisterPeriodicFunction("ARP cache", PeriodicFunction, ARP_FUNCTION_PERIOD); + int32_t index; + + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Initializing ARP layer")); + memset(arp_CacheTable, 0, sizeof(arp_CacheTable)); + for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) + { + arp_CacheTable[index].mutex = RTOS_MUTEX_CREATE(); + } + NetIF_RegisterPeriodicFunction("ARP cache", PeriodicFunction, ARP_FUNCTION_PERIOD); } -static void Handler(NetIF_t *netIF, Packet_t *packet) + + +static void Handler(NetIF_t *netIF, NetPacket_t *packet) { - static ARP_Type_t type; - static ARP_Protocol_t protocol; - static ARP_Operation_t operation; - static ARP_IPv4Data_t *ipv4ARP; - static ARP_Header_t *arpHeader; - static Ethernet_Addr_t *ourHWAddress; - static Ethernet_Header_t *ethernetHeader; - static ARP_CacheEntry_t *entry; + static ARP_Type_t type; + static ARP_Protocol_t protocol; + static ARP_Operation_t operation; + static ARP_IPv4Data_t *ipv4ARP; + static ARP_Header_t *arpHeader; + static Ethernet_Addr_t *ourHWAddress; + static Ethernet_Header_t *ethernetHeader; + static ARP_CacheEntry_t *entry; arpHeader = (ARP_Header_t *)packet->data; - type = ntohs(arpHeader->type); - protocol = ntohs(arpHeader->protocol); - operation = ntohs(arpHeader->operation); + type = ntohs(arpHeader->type); + protocol = ntohs(arpHeader->protocol); + operation = ntohs(arpHeader->operation); + + if (type != ARP_HW_TYPE_ENET) goto Exit; /* not an ethernet ARP, ignore */ +/* Not an IPv4 ARP, ignore */ + if (protocol != ETHERNET_PROTO_IPV4) goto Exit; + + ipv4ARP = (ARP_IPv4Data_t *)(arpHeader + 1); - if (type != ARP_HW_TYPE_ENET) goto Exit; /* not an ethernet ARP, ignore */ - if (protocol != ETHERNET_PROTO_IPV4) goto Exit; /* Not an IPv4 ARP, ignore */ - - ipv4ARP = (ARP_IPv4Data_t *)(arpHeader + 1); - - switch(operation) - { - case ARP_OPERATION_REQUEST: - if (ipv4ARP->ipDest.addr == netIF->ipv4Address.addr) /* Does it match our hw address? */ - { - DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) - { - ARP_DumpHeader("Got ", arpHeader); - } - - ourHWAddress = (Ethernet_Addr_t *)netIF->driverParameter; - - arpHeader->operation = htons(ARP_OPERATION_REPLY); - - ipv4ARP->hwDest = ipv4ARP->hwSource; - ipv4ARP->ipDest.addr = ipv4ARP->ipSource.addr; - ipv4ARP->hwSource = *ourHWAddress; - ipv4ARP->ipSource = netIF->ipv4Address; - - NetIF_ProtoPop(packet); + switch(operation) + { + case ARP_OPERATION_REQUEST: +/* Does it match our hw address? */ + if (ipv4ARP->ipDest.addr == netIF->ipv4Address.addr) + { + DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) + { + ARP_DumpHeader("Got ", arpHeader); + } + + ourHWAddress = (Ethernet_Addr_t *)netIF->driverParameter; + + arpHeader->operation = htons(ARP_OPERATION_REPLY); + + ipv4ARP->hwDest = ipv4ARP->hwSource; + ipv4ARP->ipDest.addr = ipv4ARP->ipSource.addr; + ipv4ARP->hwSource = *ourHWAddress; + ipv4ARP->ipSource = netIF->ipv4Address; + + NetIF_ProtoPop(packet); + + ethernetHeader = (Ethernet_Header_t *)packet->data; + ethernetHeader->destination = ethernetHeader->source; + ethernetHeader->source = *ourHWAddress; + + DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) + { + ARP_DumpHeader("Replying ", arpHeader); + } - ethernetHeader = (Ethernet_Header_t *)packet->data; - ethernetHeader->destination = ethernetHeader->source; - ethernetHeader->source = *ourHWAddress; - - DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) - { - ARP_DumpHeader("Replying ", arpHeader); - } - - netIF->driver->Write(packet->data, packet->length); - } - - break; - - case ARP_OPERATION_REPLY: - /* Check if it matches an entry we requested */ - DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) - { - ARP_DumpHeader("Got ", arpHeader); - } - - entry = GetEntryByIPv4Address(ipv4ARP->ipSource); - if (entry == NULL) break; /* fake arp request */ - - entry->status = ARP_Entry_Dynamic; - entry->ethernetAddr = ipv4ARP->hwSource; - entry->netIF = netIF; - entry->age = 0; - - RTOS_MUTEX_UNLOCK(entry->mutex); - - DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) - { - DEBUG_RAW(("Adding entry %d.%d.%d.%d at %02x:%02x:%02x:%02x:%02x:%02x", - ipv4ARP->ipSource.IP0, - ipv4ARP->ipSource.IP1, - ipv4ARP->ipSource.IP2, - ipv4ARP->ipSource.IP3, - ipv4ARP->hwSource.MA0, - ipv4ARP->hwSource.MA1, - ipv4ARP->hwSource.MA2, - ipv4ARP->hwSource.MA3, - ipv4ARP->hwSource.MA4, - ipv4ARP->hwSource.MA5 - )); - } - break; - } - -Exit: - return; + netIF->driver->Write(packet->data, packet->length); + } + + break; + + case ARP_OPERATION_REPLY: +/* Check if it matches an entry we requested */ + DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) + { + ARP_DumpHeader("Got ", arpHeader); + } + + entry = GetEntryByIPv4Address(ipv4ARP->ipSource); + if (entry == NULL) break; /* fake arp request */ + + entry->status = ARP_Entry_Dynamic; + entry->ethernetAddr = ipv4ARP->hwSource; + entry->netIF = netIF; + entry->age = 0; + + RTOS_MUTEX_UNLOCK(entry->mutex); + + DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) + { + DEBUG_RAW(("Adding entry %d.%d.%d.%d at %02x:%02x:%02x:%02x:%02x:%02x", + ipv4ARP->ipSource.IP0, + ipv4ARP->ipSource.IP1, + ipv4ARP->ipSource.IP2, + ipv4ARP->ipSource.IP3, + ipv4ARP->hwSource.MA0, + ipv4ARP->hwSource.MA1, + ipv4ARP->hwSource.MA2, + ipv4ARP->hwSource.MA3, + ipv4ARP->hwSource.MA4, + ipv4ARP->hwSource.MA5 + )); + } + break; + } + + Exit: + return; } + + static void PeriodicFunction(void) { - int32_t index; - ARP_CacheEntry_t *entry; - - for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) - { - entry = arp_CacheTable + index; - RTOS_MUTEX_LOCK(entry->mutex); - switch(entry->status) - { - case ARP_Entry_Dynamic: - entry->age += ARP_FUNCTION_PERIOD; - if (entry->age > ARP_MAX_ENTRY_AGE) - { - entry->status = ARP_Entry_Expired; - entry->age = 0; - } - break; - - case ARP_Entry_PendingReply: - entry->age += ARP_FUNCTION_PERIOD; - if (entry->age > ARP_MAX_ENTRY_AGE) - { - entry->status = ARP_Entry_Free; - entry->age = 0; - } - break; - } - RTOS_MUTEX_UNLOCK(entry->mutex); - } + int32_t index; + ARP_CacheEntry_t *entry; + + for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) + { + entry = arp_CacheTable + index; + RTOS_MUTEX_LOCK(entry->mutex); + switch(entry->status) + { + case ARP_Entry_Dynamic: + entry->age += ARP_FUNCTION_PERIOD; + if (entry->age > ARP_MAX_ENTRY_AGE) + { + entry->status = ARP_Entry_Expired; + entry->age = 0; + } + break; + + case ARP_Entry_PendingReply: + entry->age += ARP_FUNCTION_PERIOD; + if (entry->age > ARP_MAX_ENTRY_AGE) + { + entry->status = ARP_Entry_Free; + entry->age = 0; + } + break; + } + RTOS_MUTEX_UNLOCK(entry->mutex); + } } + + static ARP_CacheEntry_t *GetReusableEntry(void) { - int32_t index, - oldestEntryIndex, oldestEntryAge; - ARP_CacheEntry_t *entry; - - /* First look for a free entry */ - for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) - { - entry = arp_CacheTable + index; - RTOS_MUTEX_LOCK(entry->mutex); - - if (entry->status == ARP_Entry_Free) - { - break; - } + int32_t index, + oldestEntryIndex, oldestEntryAge; + ARP_CacheEntry_t *entry; + +/* First look for a free entry */ + for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) + { + entry = arp_CacheTable + index; + RTOS_MUTEX_LOCK(entry->mutex); + + if (entry->status == ARP_Entry_Free) + { + break; + } + + RTOS_MUTEX_UNLOCK(entry->mutex); + entry = NULL; + } + + if (entry != NULL) goto Exit; /* A free entry was found, return it */ - RTOS_MUTEX_UNLOCK(entry->mutex); - entry = NULL; - } - - if (entry != NULL) goto Exit; /* A free entry was found, return it */ - - /* Now look for an expired entry */ - oldestEntryIndex = -1; - oldestEntryAge = -1; - for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) - { - entry = arp_CacheTable + index; - RTOS_MUTEX_LOCK(entry->mutex); - - if (entry->age > oldestEntryAge) - { - oldestEntryIndex = index; - oldestEntryAge = entry->age; - } - - if (entry->status == ARP_Entry_Expired) - { - break; - } +/* Now look for an expired entry */ + oldestEntryIndex = -1; + oldestEntryAge = -1; + for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) + { + entry = arp_CacheTable + index; + RTOS_MUTEX_LOCK(entry->mutex); + + if (entry->age > oldestEntryAge) + { + oldestEntryIndex = index; + oldestEntryAge = entry->age; + } - RTOS_MUTEX_UNLOCK(entry->mutex); - entry = NULL; - } + if (entry->status == ARP_Entry_Expired) + { + break; + } - if (entry != NULL) goto Exit; /* An expired entry was found, return it */ + RTOS_MUTEX_UNLOCK(entry->mutex); + entry = NULL; + } - /* Last possibility, return the oldest non static entry */ - entry = arp_CacheTable + oldestEntryIndex; - RTOS_MUTEX_LOCK(entry->mutex); + if (entry != NULL) goto Exit; /* An expired entry was found, return it */ + +/* Last possibility, return the oldest non static entry */ + entry = arp_CacheTable + oldestEntryIndex; + RTOS_MUTEX_LOCK(entry->mutex); -Exit: - return entry; + Exit: + return entry; } + + static ARP_CacheEntry_t *GetEntryByIPv4Address(IPv4_Addr_t address) { - int32_t index; - ARP_CacheEntry_t *entry = NULL; - - for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) - { - entry = arp_CacheTable + index; - RTOS_MUTEX_LOCK(entry->mutex); - - if (entry->ipv4Addr.addr == address.addr) - { - break; - } - RTOS_MUTEX_UNLOCK(entry->mutex); - entry = NULL; - } - - return entry; + int32_t index; + ARP_CacheEntry_t *entry = NULL; + + for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) + { + entry = arp_CacheTable + index; + RTOS_MUTEX_LOCK(entry->mutex); + + if (entry->ipv4Addr.addr == address.addr) + { + break; + } + RTOS_MUTEX_UNLOCK(entry->mutex); + entry = NULL; + } + + return entry; } + + int32_t ARP_ResolveIPv4Address(NetIF_t *netIF, IPv4_Addr_t address, Ethernet_Addr_t *ethernetAddr) { - int32_t result = -1; - Ethernet_Addr_t *hwAddress; - ARP_CacheEntry_t *entry; - - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Resolving %d.%d.%d.%d", - address.IP0, - address.IP1, - address.IP2, - address.IP3 - )); - - /* Look if entry is already available in table */ - entry = GetEntryByIPv4Address(address); - if (entry != NULL) /* Found entry, look its status */ - { - switch(entry->status) - { - case ARP_Entry_Static: - DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found static entry")); - if (ethernetAddr != NULL) *ethernetAddr = entry->ethernetAddr; - RTOS_MUTEX_UNLOCK(entry->mutex); - result = 0; - break; - - case ARP_Entry_Dynamic: - DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found dynamic entry")); - if (ethernetAddr != NULL) *ethernetAddr = entry->ethernetAddr; - entry->age = 0; - RTOS_MUTEX_UNLOCK(entry->mutex); - result = 0; - break; - - case ARP_Entry_Expired: - DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found expired entry, reactivating it")); - if (ethernetAddr != NULL) *ethernetAddr = entry->ethernetAddr; - entry->status = ARP_Entry_Dynamic; - entry->age = 0; - RTOS_MUTEX_UNLOCK(entry->mutex); - result = 0; - break; - - case ARP_Entry_PendingReply: - DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found pending entry")); - entry->age = 0; - RTOS_MUTEX_UNLOCK(entry->mutex); - break; - - default: - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Default?!")); - break; - } - } - - if (result == 0) goto Exit; /* Resolution was successfull, exit */ - - - /* Entry not found, send a request */ - result = -1; - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Sending ARP resolution request for %d.%d.%d.%d", - address.IP0, - address.IP1, - address.IP2, - address.IP3 - )); - - /* Update entry, setting its status to Pending reply */ - entry = GetReusableEntry(); - if (entry != NULL) - { - entry->status = ARP_Entry_PendingReply; - entry->ipv4Addr.addr = address.addr; - entry->netIF = netIF; - entry->age = 0; - RTOS_MUTEX_UNLOCK(entry->mutex); - } - /* Send ARP who-has */ - hwAddress = (Ethernet_Addr_t *)netIF->driverParameter; + int32_t result = -1; + Ethernet_Addr_t *hwAddress; + ARP_CacheEntry_t *entry; + + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Resolving %d.%d.%d.%d", + address.IP0, + address.IP1, + address.IP2, + address.IP3 + )); + +/* Look if entry is already available in table */ + entry = GetEntryByIPv4Address(address); + if (entry != NULL) /* Found entry, look its status */ + { + switch(entry->status) + { + case ARP_Entry_Static: + DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found static entry")); + if (ethernetAddr != NULL) *ethernetAddr = entry->ethernetAddr; + RTOS_MUTEX_UNLOCK(entry->mutex); + result = 0; + break; + + case ARP_Entry_Dynamic: + DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found dynamic entry")); + if (ethernetAddr != NULL) *ethernetAddr = entry->ethernetAddr; + entry->age = 0; + RTOS_MUTEX_UNLOCK(entry->mutex); + result = 0; + break; + + case ARP_Entry_Expired: + DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found expired entry, reactivating it")); + if (ethernetAddr != NULL) *ethernetAddr = entry->ethernetAddr; + entry->status = ARP_Entry_Dynamic; + entry->age = 0; + RTOS_MUTEX_UNLOCK(entry->mutex); + result = 0; + break; + + case ARP_Entry_PendingReply: + DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Found pending entry")); + entry->age = 0; + RTOS_MUTEX_UNLOCK(entry->mutex); + break; - arp_FullIPv4Packet.ethernetHeader.destination = ethernet_Addr_Broadcast; - arp_FullIPv4Packet.ethernetHeader.source = *hwAddress; - arp_FullIPv4Packet.ethernetHeader.protocol = htons(ETHERNET_PROTO_ARP); - - arp_FullIPv4Packet.arpHeader.type = htons(ARP_HW_TYPE_ENET); - arp_FullIPv4Packet.arpHeader.protocol = htons(ETHERNET_PROTO_IPV4); - arp_FullIPv4Packet.arpHeader.operation = htons(ARP_OPERATION_REQUEST); - arp_FullIPv4Packet.arpHeader.hardAddrLen = 6; - arp_FullIPv4Packet.arpHeader.protoAddrLen = 4; - - arp_FullIPv4Packet.ipv4ARPData.hwSource = *hwAddress; - arp_FullIPv4Packet.ipv4ARPData.ipSource = netIF->ipv4Address; - arp_FullIPv4Packet.ipv4ARPData.hwDest = ethernet_Addr_Null; - arp_FullIPv4Packet.ipv4ARPData.ipDest = address; - - DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) - { - ARP_DumpHeader("Sending ", &arp_FullIPv4Packet.arpHeader); - } - - netIF->driver->Write((uint8_t *)&arp_FullIPv4Packet, sizeof(arp_FullIPv4Packet)); - -Exit: - return result; + default: + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Default?!")); + break; + } + } + + if (result == 0) goto Exit; /* Resolution was successfull, exit */ + +/* Entry not found, send a request */ + result = -1; + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Sending ARP resolution request for %d.%d.%d.%d", + address.IP0, + address.IP1, + address.IP2, + address.IP3 + )); + +/* Update entry, setting its status to Pending reply */ + entry = GetReusableEntry(); + if (entry != NULL) + { + entry->status = ARP_Entry_PendingReply; + entry->ipv4Addr.addr = address.addr; + entry->netIF = netIF; + entry->age = 0; + RTOS_MUTEX_UNLOCK(entry->mutex); + } +/* Send ARP who-has */ + hwAddress = (Ethernet_Addr_t *)netIF->driverParameter; + + arp_FullIPv4Packet.ethernetHeader.destination = ethernet_Addr_Broadcast; + arp_FullIPv4Packet.ethernetHeader.source = *hwAddress; + arp_FullIPv4Packet.ethernetHeader.protocol = htons(ETHERNET_PROTO_ARP); + + arp_FullIPv4Packet.arpHeader.type = htons(ARP_HW_TYPE_ENET); + arp_FullIPv4Packet.arpHeader.protocol = htons(ETHERNET_PROTO_IPV4); + arp_FullIPv4Packet.arpHeader.operation = htons(ARP_OPERATION_REQUEST); + arp_FullIPv4Packet.arpHeader.hardAddrLen = 6; + arp_FullIPv4Packet.arpHeader.protoAddrLen = 4; + + arp_FullIPv4Packet.ipv4ARPData.hwSource = *hwAddress; + arp_FullIPv4Packet.ipv4ARPData.ipSource = netIF->ipv4Address; + arp_FullIPv4Packet.ipv4ARPData.hwDest = ethernet_Addr_Null; + arp_FullIPv4Packet.ipv4ARPData.ipDest = address; + + DEBUG_BLOCK(DEBUG_LEVEL_VERBOSE0) + { + ARP_DumpHeader("Sending ", &arp_FullIPv4Packet.arpHeader); + } + + netIF->driver->Write((uint8_t *)&arp_FullIPv4Packet, sizeof(arp_FullIPv4Packet)); + + Exit: + return result; } + + int32_t ARP_AddStaticEntry(NetIF_t *netIF, IPv4_Addr_t address, const Ethernet_Addr_t *ethernetAddr) { - int32_t result = 0; - ARP_CacheEntry_t *entry; - - entry = GetReusableEntry(); - if (entry == NULL) - { - result = -1; - goto Exit; - } - entry->netIF = netIF; - entry->status = ARP_Entry_Static; - entry->ipv4Addr.addr = address.addr; - entry->ethernetAddr = *ethernetAddr; - entry->age = 0; - RTOS_MUTEX_UNLOCK(entry->mutex); + int32_t result = 0; + ARP_CacheEntry_t *entry; -Exit: - return result; + entry = GetReusableEntry(); + if (entry == NULL) + { + result = -1; + goto Exit; + } + entry->netIF = netIF; + entry->status = ARP_Entry_Static; + entry->ipv4Addr.addr = address.addr; + entry->ethernetAddr = *ethernetAddr; + entry->age = 0; + RTOS_MUTEX_UNLOCK(entry->mutex); + + Exit: + return result; } + + int32_t ARP_RemoveEntry(const NetIF_t *netIF, IPv4_Addr_t address) { - int32_t result = 0; - - - - return result; + int32_t result = 0; + + return result; } + + void ARP_FlushCache(Bool_t flushStaticEntries) { - int32_t index; - ARP_CacheEntry_t *entry; - - for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) - { - entry = arp_CacheTable + index; - RTOS_MUTEX_LOCK(entry->mutex); - if ((entry->status == ARP_Entry_Static) && (!flushStaticEntries)) - { - RTOS_MUTEX_UNLOCK(entry->mutex); - continue; - } - entry->status = ARP_Entry_Free; + int32_t index; + ARP_CacheEntry_t *entry; + + for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) + { + entry = arp_CacheTable + index; + RTOS_MUTEX_LOCK(entry->mutex); + if ((entry->status == ARP_Entry_Static) && (!flushStaticEntries)) + { + RTOS_MUTEX_UNLOCK(entry->mutex); + continue; + } + entry->status = ARP_Entry_Free; entry->ipv4Addr = ipv4_Addr_Any; entry->ethernetAddr = ethernet_Addr_Null; entry->age = 0; - RTOS_MUTEX_UNLOCK(entry->mutex); - } + RTOS_MUTEX_UNLOCK(entry->mutex); + } } + + void ARP_DisplayCache(void) { - int32_t index; - ARP_CacheEntry_t *entry = NULL; - - DEBUG_RAW(("ARP cache")); - DEBUG_RAW(("index dev MAC address type age IPv4 address")); - DEBUG_RAW(("----------------------------------------------------------")); - /* en0 00:11:22:33:44:55 dyn 10 163.157.128.131 */ - for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) - { - entry = arp_CacheTable + index; - - DEBUG_RAW(("%2d %s%c %02x:%02x:%02x:%02x:%02x:%02x %8s %4d %d.%d.%d.%d", - index, - entry->status != ARP_Entry_Free ? entry->netIF->name : "--", - entry->status != ARP_Entry_Free ? entry->netIF->index + '0' : '-', - entry->ethernetAddr.MA0, - entry->ethernetAddr.MA1, - entry->ethernetAddr.MA2, - entry->ethernetAddr.MA3, - entry->ethernetAddr.MA4, - entry->ethernetAddr.MA5, - - arpEntryStatusText[entry->status], - entry->age, - - entry->ipv4Addr.IP0, - entry->ipv4Addr.IP1, - entry->ipv4Addr.IP2, - entry->ipv4Addr.IP3 - )); - } + int32_t index; + ARP_CacheEntry_t *entry = NULL; + + DEBUG_RAW(("ARP cache")); + DEBUG_RAW(("index dev MAC address type age IPv4 address")); + DEBUG_RAW(("----------------------------------------------------------")); +/* en0 00:11:22:33:44:55 dyn 10 163.157.128.131 */ + for (index = 0; index < ARP_CACHE_MAX_ENTRIES; index++) + { + entry = arp_CacheTable + index; + + DEBUG_RAW(("%2d %s%c %02x:%02x:%02x:%02x:%02x:%02x %8s %4d %d.%d.%d.%d", + index, + entry->status != ARP_Entry_Free ? entry->netIF->name : "--", + entry->status != ARP_Entry_Free ? entry->netIF->index + '0' : '-', + entry->ethernetAddr.MA0, + entry->ethernetAddr.MA1, + entry->ethernetAddr.MA2, + entry->ethernetAddr.MA3, + entry->ethernetAddr.MA4, + entry->ethernetAddr.MA5, + + arpEntryStatusText[entry->status], + entry->age, + + entry->ipv4Addr.IP0, + entry->ipv4Addr.IP1, + entry->ipv4Addr.IP2, + entry->ipv4Addr.IP3 + )); + } } + + + void ARP_DumpHeader(const char *prefix, ARP_Header_t *arpHeader) { - ARP_IPv4Data_t *ipv4ARP = NULL; - - if (arpHeader->protocol == htons(ETHERNET_PROTO_IPV4)) - { - ipv4ARP = (ARP_IPv4Data_t *)(arpHeader + 1); - - switch(ntohs(arpHeader->operation)) - { - case ARP_OPERATION_REQUEST: - DEBUG_RAW(("%sARP who-has %d.%d.%d.%d tell %02x:%02x:%02x:%02x:%02x:%02x", - prefix != NULL ? prefix : "", - - ipv4ARP->ipDest.IP0, - ipv4ARP->ipDest.IP1, - ipv4ARP->ipDest.IP2, - ipv4ARP->ipDest.IP3, - - ipv4ARP->hwSource.MA0, - ipv4ARP->hwSource.MA1, - ipv4ARP->hwSource.MA2, - ipv4ARP->hwSource.MA3, - ipv4ARP->hwSource.MA4, - ipv4ARP->hwSource.MA5 - )); - break; - - case ARP_OPERATION_REPLY: - DEBUG_RAW(("%sARP %d.%d.%d.%d is-at %02x:%02x:%02x:%02x:%02x:%02x", - prefix != NULL ? prefix : "", - - ipv4ARP->ipSource.IP0, - ipv4ARP->ipSource.IP1, - ipv4ARP->ipSource.IP2, - ipv4ARP->ipSource.IP3, - - ipv4ARP->hwSource.MA0, - ipv4ARP->hwSource.MA1, - ipv4ARP->hwSource.MA2, - ipv4ARP->hwSource.MA3, - ipv4ARP->hwSource.MA4, - ipv4ARP->hwSource.MA5 - )); - break; - - default: - break; - } - } - else - { - DEBUG_RAW(("%sARP: unsupported protocol %d", - prefix != NULL ? prefix : "", - arpHeader->protocol - )); - } + ARP_IPv4Data_t *ipv4ARP = NULL; + + if (arpHeader->protocol == htons(ETHERNET_PROTO_IPV4)) + { + ipv4ARP = (ARP_IPv4Data_t *)(arpHeader + 1); + + switch(ntohs(arpHeader->operation)) + { + case ARP_OPERATION_REQUEST: + DEBUG_RAW(("%sARP who-has %d.%d.%d.%d tell %02x:%02x:%02x:%02x:%02x:%02x", + prefix != NULL ? prefix : "", + + ipv4ARP->ipDest.IP0, + ipv4ARP->ipDest.IP1, + ipv4ARP->ipDest.IP2, + ipv4ARP->ipDest.IP3, + + ipv4ARP->hwSource.MA0, + ipv4ARP->hwSource.MA1, + ipv4ARP->hwSource.MA2, + ipv4ARP->hwSource.MA3, + ipv4ARP->hwSource.MA4, + ipv4ARP->hwSource.MA5 + )); + break; + + case ARP_OPERATION_REPLY: + DEBUG_RAW(("%sARP %d.%d.%d.%d is-at %02x:%02x:%02x:%02x:%02x:%02x", + prefix != NULL ? prefix : "", + + ipv4ARP->ipSource.IP0, + ipv4ARP->ipSource.IP1, + ipv4ARP->ipSource.IP2, + ipv4ARP->ipSource.IP3, + + ipv4ARP->hwSource.MA0, + ipv4ARP->hwSource.MA1, + ipv4ARP->hwSource.MA2, + ipv4ARP->hwSource.MA3, + ipv4ARP->hwSource.MA4, + ipv4ARP->hwSource.MA5 + )); + break; + + default: + break; + } + } + else + { + DEBUG_RAW(("%sARP: unsupported protocol %d", + prefix != NULL ? prefix : "", + arpHeader->protocol + )); + } }
--- a/ARP.h Sun Jun 12 20:24:23 2011 +0000 +++ b/ARP.h Mon Jun 13 13:13:59 2011 +0000 @@ -1,70 +1,54 @@ -/* - * $Id: ARP.h 29 2011-06-11 14:53:08Z benoit $ - * $Author: benoit $ - * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $ - * $Rev: 29 $ - * - * - * - * - * +/*$Id: + * ARP.h 29 2011-06-11 14:53:08Z benoit $ $AUTHOR: BENOIT $ $Date: 2011-06-11 + * 16:53:08 +0200 (sam., 11 juin 2011) $ $Rev: 29 $ */ - #ifndef __ARP_H__ -#define __ARP_H__ - +#define __ARP_H__ #include "IPv4.h" #include "Ethernet.h" - -#define ARP_HW_TYPE_ENET 0x0001 - -#define ARP_OPERATION_REQUEST 0x0001 -#define ARP_OPERATION_REPLY 0x0002 +#define ARP_HW_TYPE_ENET 0x0001 +#define ARP_OPERATION_REQUEST 0x0001 +#define ARP_OPERATION_REPLY 0x0002 -typedef uint16_t ARP_Type_t; -typedef uint16_t ARP_Protocol_t; -typedef uint16_t ARP_Operation_t; - +typedef uint16_t ARP_Type_t; +typedef uint16_t ARP_Protocol_t; +typedef uint16_t ARP_Operation_t; #pragma push #pragma pack(1) struct ARP_Header { - ARP_Type_t type; - ARP_Protocol_t protocol; - uint8_t hardAddrLen; - uint8_t protoAddrLen; - ARP_Operation_t operation; + ARP_Type_t type; + ARP_Protocol_t protocol; + uint8_t hardAddrLen; + uint8_t protoAddrLen; + ARP_Operation_t operation; }; #pragma pop -typedef struct ARP_Header ARP_Header_t; - +typedef struct ARP_Header ARP_Header_t; #pragma push #pragma pack(1) struct ARP_IPv4Data { - Ethernet_Addr_t hwSource; - IPv4_Addr_t ipSource; - Ethernet_Addr_t hwDest; - IPv4_Addr_t ipDest; + Ethernet_Addr_t hwSource; + IPv4_Addr_t ipSource; + Ethernet_Addr_t hwDest; + IPv4_Addr_t ipDest; }; #pragma pop typedef struct ARP_IPv4Data ARP_IPv4Data_t; - -extern Protocol_Handler_t arp; - +extern Protocol_Handler_t arp; -int32_t ARP_ResolveIPv4Address(NetIF_t *netIF, IPv4_Addr_t address, Ethernet_Addr_t *ethernetAddr); -int32_t ARP_AddStaticEntry(NetIF_t *netIF, IPv4_Addr_t address, const Ethernet_Addr_t *ethernetAddr); -int32_t ARP_RemoveEntry(const NetIF_t *netIF, IPv4_Addr_t address); -void ARP_FlushCache(Bool_t flushStaticEntries); -void ARP_Timer(void); -void ARP_DisplayCache(void); -void ARP_DumpHeader(const char *prefix, ARP_Header_t *arpHeader); - +int32_t ARP_ResolveIPv4Address(NetIF_t *netIF, IPv4_Addr_t address, Ethernet_Addr_t *ethernetAddr); +int32_t ARP_AddStaticEntry(NetIF_t *netIF, IPv4_Addr_t address, const Ethernet_Addr_t *ethernetAddr); +int32_t ARP_RemoveEntry(const NetIF_t *netIF, IPv4_Addr_t address); +void ARP_FlushCache(Bool_t flushStaticEntries); +void ARP_Timer(void); +void ARP_DisplayCache(void); +void ARP_DumpHeader(const char *prefix, ARP_Header_t *arpHeader); #endif /* __ARP_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CQueue.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -0,0 +1,142 @@ +/* + * $Id: Queue.c 29 2011-06-11 14:53:08Z benoit $ + * $Author: benoit $ + * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $ + * $Rev: 29 $ + * + * + * + * + * + */ + +#include "CQueue.h" +#include "Debug.h" +#include <stdlib.h> +#include <string.h> + + +#define DEBUG_CURRENT_MODULE_NAME "Queue" +#define DEBUG_CURRENT_MODULE_ID DEBUG_MODULE_QUEUE + + +CQueue_t *CQueue_Alloc(int16_t size) +{ + CQueue_t *queue; + + queue = (CQueue_t *)malloc(sizeof(CQueue_t)); + if (queue == NULL) + { + DEBUG_MODULE(DEBUG_LEVEL_WARNING, ("Not enough memory")); + mbedNet_LastError = mbedNetResult_NotEnoughMemory; + goto Exit; + } + memset(queue, 0, sizeof(CQueue_t)); + + queue->size = size + 1; + + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Allocated queue of %d items", queue->popIndex, queue->pushIndex, size)); + +Exit: + return queue; +} + + +int32_t CQueue_Push(CQueue_t *queue, void *entry) +{ + int32_t result = 0; + int16_t index; + + if (queue->popIndex == ((queue->pushIndex + 1) % queue->size)) + { + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Queue is full", queue->popIndex, queue->pushIndex)); + result = -1; + mbedNet_LastError = mbedNetResult_QueueFull; + goto Exit; + } + + index = queue->pushIndex; + queue->entries[index] = entry; + index++; + if (index == queue->size) index = 0; + queue->pushIndex = index; + + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Pushing one entry from queue", queue->popIndex, queue->pushIndex)); + +Exit: + return result; +} + + +int32_t CQueue_Pop(CQueue_t *queue, void **entry) +{ + int32_t result = 0; + int16_t index; + + if (queue->popIndex == queue->pushIndex) + { + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Queue is empty", queue->popIndex, queue->pushIndex)); + result = -1; + mbedNet_LastError = mbedNetResult_QueueEmpty; + goto Exit; + } + + + index = queue->popIndex; + *entry = queue->entries[index]; + index++; + if (index == queue->size) index = 0; + queue->popIndex = index; + + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Popping one entry from queue", queue->popIndex, queue->pushIndex)); + +Exit: + return result; +} + + +int32_t CQueue_Peek(CQueue_t *queue, void **entry) +{ + int32_t result = 0; + int16_t index; + + if (queue->popIndex == queue->pushIndex) + { + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Queue is empty", queue->popIndex, queue->pushIndex)); + result = -1; + mbedNet_LastError = mbedNetResult_QueueEmpty; + goto Exit; + } + + index = queue->popIndex; + *entry = queue->entries[index]; + + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Peeking first entry from queue", queue->popIndex, queue->pushIndex)); + +Exit: + return result; +} + + +int32_t CQueue_Free(CQueue_t *queue) +{ + int32_t result = 0; + + DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Free queue", queue->popIndex, queue->pushIndex)); + free(queue); + + return result; +} + + +Bool_t CQueue_IsEmpty(const CQueue_t *queue) +{ + return (queue->popIndex == queue->pushIndex) ? True : False; +} + + +Bool_t CQueue_IsFull(const CQueue_t *queue) +{ + return (queue->popIndex == ((queue->pushIndex + 1) % queue->size)) ? True : False; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CQueue.h Mon Jun 13 13:13:59 2011 +0000 @@ -0,0 +1,40 @@ +/* + * $Id: Queue.h 26 2011-06-09 10:24:02Z benoit $ + * $Author: benoit $ + * $Date: 2011-06-09 12:24:02 +0200 (jeu., 09 juin 2011) $ + * $Rev: 26 $ + * + * + * + * + * + */ + +#ifndef __CQUEUE_H__ +#define __CQUEUE_H__ + + +#include <stdint.h> +#include "mbedNet.h" + +struct CQueue +{ + uint8_t pushIndex, + popIndex; + void *entries[QUEUE_MAX_ENTRY_COUNT + 1]; + int16_t size; +}; +typedef struct CQueue CQueue_t; + + +CQueue_t *CQueue_Alloc(int16_t size); +int32_t CQueue_Push(CQueue_t *queue, void *entry); +int32_t CQueue_Pop(CQueue_t *queue, void **entry); +int32_t CQueue_Peek(CQueue_t *queue, void **entry); +int32_t CQueue_Free(CQueue_t *queue); +Bool_t CQueue_IsEmpty(const CQueue_t *queue); +Bool_t CQueue_IsFull(const CQueue_t *queue); + + +#endif /* __QUEUE_H__ */ +
--- a/Ethernet.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/Ethernet.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -22,7 +22,7 @@ static void Init(void); static int32_t RegisterProtocol(Protocol_Handler_t *protocolHandler); static int32_t RegisterAPI(Net_API_t *api); -static void Handler(NetIF_t *netIF, Packet_t *packet); +static void Handler(NetIF_t *netIF, NetPacket_t *packet); static Protocol_Handler_t *protocolHandlerTable[ETHERNET_PROTOCOL_MAX_COUNT]; @@ -96,7 +96,7 @@ } -void Handler(NetIF_t *netIF, Packet_t *packet) +void Handler(NetIF_t *netIF, NetPacket_t *packet) { int32_t protocolIndex, index; Ethernet_Proto_t protocolNumber;
--- a/ICMPv4.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/ICMPv4.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -20,7 +20,7 @@ static void Init(void); -static void Handler(NetIF_t *netIF, Packet_t *packet); +static void Handler(NetIF_t *netIF, NetPacket_t *packet); Protocol_Handler_t icmpv4 = @@ -41,7 +41,7 @@ } -static void Handler(NetIF_t *netIF, Packet_t *packet) +static void Handler(NetIF_t *netIF, NetPacket_t *packet) { ICMPv4_Header_t *icmpv4Packet; ICMPv4_Type_t type;
--- a/IPv4.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/IPv4.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -22,7 +22,7 @@ static void Init(void); static int32_t RegisterProtocol(Protocol_Handler_t *protocolHandler); -static void Handler(NetIF_t *netIF, Packet_t *packet); +static void Handler(NetIF_t *netIF, NetPacket_t *packet); static Protocol_Handler_t *protocolHandlerTable[IPV4_PROTOCOL_MAX_COUNT]; @@ -81,7 +81,7 @@ } -static void Handler(NetIF_t *netIF, Packet_t *packet) +static void Handler(NetIF_t *netIF, NetPacket_t *packet) { int32_t protocolIndex, payloadOffset;
--- a/NetIF.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/NetIF.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -40,7 +40,7 @@ static RTOS_Mutex_t netIF_PeriodicFunctionTableMutex; static Bool_t netIFLayerInitialized = False; -static Packet_t rxPacket, +static NetPacket_t rxPacket, txPacket; static int32_t gatewayNetIFIndex = -1; @@ -412,7 +412,7 @@ } -void NetIF_ProtoPop(Packet_t *packet) +void NetIF_ProtoPop(NetPacket_t *packet) { static int32_t depth, headerSize, index; @@ -440,7 +440,7 @@ } -void NetIF_ProtoPush(Packet_t *packet, int32_t headerSize, Protocol_ID_t protocol) +void NetIF_ProtoPush(NetPacket_t *packet, int32_t headerSize, Protocol_ID_t protocol) { static int32_t depth, index;
--- a/NetIF.h Sun Jun 12 20:24:23 2011 +0000 +++ b/NetIF.h Mon Jun 13 13:13:59 2011 +0000 @@ -10,8 +10,8 @@ * */ -#ifndef __NETIF_H__ -#define __NETIF_H__ +#ifndef __NETIF_H__ +#define __NETIF_H__ #include "mbedNet.h" @@ -30,7 +30,6 @@ typedef uint8_t *(*enet_drv_get_tx_buffer_t)(void); - enum Protocol_ID { Protocol_ID_Ethernet = 0, @@ -44,7 +43,7 @@ typedef enum Protocol_ID Protocol_ID_t; -struct Packet +struct NetPacket { int8_t depth; uint8_t *headerPtrTable[NET_ENCAPSULATION_MAX_DEPTH]; @@ -53,7 +52,7 @@ uint8_t *data; int32_t length; }; -typedef struct Packet Packet_t; +typedef struct NetPacket NetPacket_t; enum API_ID @@ -68,13 +67,13 @@ { API_ID_t apiID; void (*InitAPI)(void); - int32_t (*Hook)(NetIF_t *netIF, Protocol_ID_t protocolID, Packet_t *packet); + int32_t (*Hook)(NetIF_t *netIF, Protocol_ID_t protocolID, NetPacket_t *packet); }; typedef struct Net_API Net_API_t; typedef int32_t (*Protocol_RegisterFunction_t)(Protocol_Handler_t *); -typedef void (*Protocol_HandlerFunction_t)(NetIF_t *, Packet_t *); +typedef void (*Protocol_HandlerFunction_t)(NetIF_t *, NetPacket_t *); typedef void (*Protocol_InitFunction_t)(void); typedef int32_t (*API_RegisterFunction_t)(Net_API_t *); typedef void (*PeriodicFunction_t)(void); @@ -141,7 +140,7 @@ int32_t NetIF_SendIPv4Packet(IPv4_Header_t *ipv4Header); int32_t NetIF_Up(NetIF_t *netIF); int32_t NetIF_Down(NetIF_t *netIF); -void NetIF_ProtoPop(Packet_t *packet); -void NetIF_ProtoPush(Packet_t *packet, int32_t headerSize, Protocol_ID_t protocol); +void NetIF_ProtoPop(NetPacket_t *packet); +void NetIF_ProtoPush(NetPacket_t *packet, int32_t headerSize, Protocol_ID_t protocol); #endif /* __NETIF_H__ */
--- a/Queue.cpp Sun Jun 12 20:24:23 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,142 +0,0 @@ -/* - * $Id: Queue.c 29 2011-06-11 14:53:08Z benoit $ - * $Author: benoit $ - * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $ - * $Rev: 29 $ - * - * - * - * - * - */ - -#include "Queue.h" -#include "Debug.h" -#include <stdlib.h> -#include <string.h> - - -#define DEBUG_CURRENT_MODULE_NAME "Queue" -#define DEBUG_CURRENT_MODULE_ID DEBUG_MODULE_QUEUE - - -Queue_t *Queue_Alloc(int16_t size) -{ - Queue_t *queue; - - queue = (Queue_t *)malloc(sizeof(Queue_t)); - if (queue == NULL) - { - DEBUG_MODULE(DEBUG_LEVEL_WARNING, ("Not enough memory")); - mbedNet_LastError = mbedNetResult_NotEnoughMemory; - goto Exit; - } - memset(queue, 0, sizeof(Queue_t)); - - queue->size = size + 1; - - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Allocated queue of %d items", queue->popIndex, queue->pushIndex, size)); - -Exit: - return queue; -} - - -int32_t Queue_Push(Queue_t *queue, void *entry) -{ - int32_t result = 0; - int16_t index; - - if (queue->popIndex == ((queue->pushIndex + 1) % queue->size)) - { - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Queue is full", queue->popIndex, queue->pushIndex)); - result = -1; - mbedNet_LastError = mbedNetResult_QueueFull; - goto Exit; - } - - index = queue->pushIndex; - queue->entries[index] = entry; - index++; - if (index == queue->size) index = 0; - queue->pushIndex = index; - - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Pushing one entry from queue", queue->popIndex, queue->pushIndex)); - -Exit: - return result; -} - - -int32_t Queue_Pop(Queue_t *queue, void **entry) -{ - int32_t result = 0; - int16_t index; - - if (queue->popIndex == queue->pushIndex) - { - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Queue is empty", queue->popIndex, queue->pushIndex)); - result = -1; - mbedNet_LastError = mbedNetResult_QueueEmpty; - goto Exit; - } - - - index = queue->popIndex; - *entry = queue->entries[index]; - index++; - if (index == queue->size) index = 0; - queue->popIndex = index; - - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Popping one entry from queue", queue->popIndex, queue->pushIndex)); - -Exit: - return result; -} - - -int32_t Queue_Peek(Queue_t *queue, void **entry) -{ - int32_t result = 0; - int16_t index; - - if (queue->popIndex == queue->pushIndex) - { - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Queue is empty", queue->popIndex, queue->pushIndex)); - result = -1; - mbedNet_LastError = mbedNetResult_QueueEmpty; - goto Exit; - } - - index = queue->popIndex; - *entry = queue->entries[index]; - - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Peeking first entry from queue", queue->popIndex, queue->pushIndex)); - -Exit: - return result; -} - - -int32_t Queue_Free(Queue_t *queue) -{ - int32_t result = 0; - - DEBUG_MODULE(DEBUG_LEVEL_INFO, ("%d-%d Free queue", queue->popIndex, queue->pushIndex)); - free(queue); - - return result; -} - - -Bool_t Queue_IsEmpty(const Queue_t *queue) -{ - return (queue->popIndex == queue->pushIndex) ? True : False; -} - - -Bool_t Queue_IsFull(const Queue_t *queue) -{ - return (queue->popIndex == ((queue->pushIndex + 1) % queue->size)) ? True : False; -} -
--- a/Queue.h Sun Jun 12 20:24:23 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -/* - * $Id: Queue.h 26 2011-06-09 10:24:02Z benoit $ - * $Author: benoit $ - * $Date: 2011-06-09 12:24:02 +0200 (jeu., 09 juin 2011) $ - * $Rev: 26 $ - * - * - * - * - * - */ - -#ifndef __QUEUE_H__ -#define __QUEUE_H__ - - -#include <stdint.h> -#include "mbedNet.h" - -struct Queue -{ - uint8_t pushIndex, - popIndex; - void *entries[QUEUE_MAX_ENTRY_COUNT + 1]; - int16_t size; -}; -typedef struct Queue Queue_t; - - -Queue_t *Queue_Alloc(int16_t size); -int32_t Queue_Push(Queue_t *queue, void *entry); -int32_t Queue_Pop(Queue_t *queue, void **entry); -int32_t Queue_Peek(Queue_t *queue, void **entry); -int32_t Queue_Free(Queue_t *queue); -Bool_t Queue_IsEmpty(const Queue_t *queue); -Bool_t Queue_IsFull(const Queue_t *queue); - - -#endif /* __QUEUE_H__ */ -
--- a/Sockets.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/Sockets.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -15,7 +15,7 @@ #include "IPv4.h" #include "UDPv4.h" #include "Debug.h" -#include "Queue.h" +#include "CQueue.h" #include <string.h> #include <stdlib.h> @@ -55,7 +55,7 @@ State_t state; Socket_Addr_t *localAddr, *remoteAddr; - Queue_t *dataQueue; + CQueue_t *dataQueue; int32_t index; }; typedef struct Socket_Entry Socket_Entry_t; @@ -66,8 +66,8 @@ static void Init(void); -static int32_t Hook(NetIF_t *netIF, Protocol_ID_t protocolID, Packet_t *packet); -static void Hook_UDPv4(NetIF_t *netIF, Packet_t *packet, Socket_Entry_t *entry); +static int32_t Hook(NetIF_t *netIF, Protocol_ID_t protocolID, NetPacket_t *packet); +static void Hook_UDPv4(NetIF_t *netIF, NetPacket_t *packet, Socket_Entry_t *entry); static Socket_Entry_t *GetSocketEntry(Socket_t socket); static int32_t BindUDPv4(Socket_Entry_t *entry, Socket_AddrIn_t *addrIn); static int32_t Recv_Data(Socket_Entry_t *entry, uint8_t *data, int32_t length); @@ -95,7 +95,7 @@ } -static int32_t Hook(NetIF_t *netIF, Protocol_ID_t protocolID, Packet_t *packet) +static int32_t Hook(NetIF_t *netIF, Protocol_ID_t protocolID, NetPacket_t *packet) { int32_t index = 0; Socket_Entry_t *entry = NULL; @@ -125,7 +125,7 @@ return 0; } -static void Hook_UDPv4(NetIF_t *netIF, Packet_t *packet, Socket_Entry_t *entry) +static void Hook_UDPv4(NetIF_t *netIF, NetPacket_t *packet, Socket_Entry_t *entry) { IPv4_Header_t *ipv4Header; UDPv4_Header_t *udpv4Header; @@ -154,7 +154,7 @@ )); if ((localAddrIn->port == udpv4Header->destPort) && ( (localAddrIn->address.addr == IPADDR_ANY) || (ipv4Header->dest.addr == localAddrIn->address.addr) ) ) { - if (!Queue_IsFull(entry->dataQueue)) + if (!CQueue_IsFull(entry->dataQueue)) { remoteAddrIn->address = ipv4Header->source; remoteAddrIn->port = udpv4Header->sourcePort; @@ -175,7 +175,7 @@ } dataBlock->readPtr = dataBlock->dataPtr; memcpy(dataBlock->dataPtr, packet->data + sizeof(UDPv4_Header_t), dataBlock->totalSize); - Queue_Push(entry->dataQueue, (void *)dataBlock); + CQueue_Push(entry->dataQueue, (void *)dataBlock); DEBUG_MODULE(DEBUG_LEVEL_VERBOSE0, ("Added block of %d bytes to socket %d", dataBlock->totalSize, entry->index)); } } @@ -230,11 +230,11 @@ int32_t count = 0; DataBlock_t *dataBlock = NULL; - Queue_Peek(entry->dataQueue, (void **)&dataBlock); + CQueue_Peek(entry->dataQueue, (void **)&dataBlock); if (dataBlock->remainingSize <= length) { count = dataBlock->remainingSize; - Queue_Pop(entry->dataQueue, (void **)&dataBlock); + CQueue_Pop(entry->dataQueue, (void **)&dataBlock); memcpy(data, dataBlock->readPtr, count); free(dataBlock->dataPtr); free(dataBlock); @@ -414,7 +414,7 @@ goto Exit; } - entry->dataQueue = Queue_Alloc(SOCKET_DATAQUEUE_ENTRY_COUNT); + entry->dataQueue = CQueue_Alloc(SOCKET_DATAQUEUE_ENTRY_COUNT); if (entry == NULL) { @@ -510,7 +510,7 @@ entry = GetSocketEntry(socket); if (entry == NULL) goto Exit; - if (Queue_IsEmpty(entry->dataQueue)) + if (CQueue_IsEmpty(entry->dataQueue)) { mbedNet_LastError = mbedNetResult_WouldBlock; goto Exit; @@ -531,7 +531,7 @@ entry = GetSocketEntry(socket); if (entry == NULL) goto Exit; - if (Queue_IsEmpty(entry->dataQueue)) + if (CQueue_IsEmpty(entry->dataQueue)) { mbedNet_LastError = mbedNetResult_WouldBlock; goto Exit; @@ -568,11 +568,11 @@ free(entry->remoteAddr); entry->remoteAddr = NULL; /* Free pending data blocks */ - while(Queue_Peek(entry->dataQueue, &ptr) != -1) + while(CQueue_Peek(entry->dataQueue, &ptr) != -1) { free(ptr); } - Queue_Free(entry->dataQueue); + CQueue_Free(entry->dataQueue); entry->dataQueue = NULL; result = 0;
--- a/TCPv4.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/TCPv4.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -19,7 +19,7 @@ static void Init(void); -static void Handler(NetIF_t *netIF, Packet_t *packet); +static void Handler(NetIF_t *netIF, NetPacket_t *packet); static int32_t RegisterAPI(Net_API_t *api); @@ -41,7 +41,7 @@ } -static void Handler(NetIF_t *netIF, Packet_t *packet) +static void Handler(NetIF_t *netIF, NetPacket_t *packet) { //TCPv4_Header_t *tcpv4Header; IPv4_Header_t *ipv4Header;
--- a/UDPv4.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/UDPv4.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -20,7 +20,7 @@ static void Init(void); -static void Handler(NetIF_t *netIF, Packet_t *packet); +static void Handler(NetIF_t *netIF, NetPacket_t *packet); static int32_t RegisterAPI(Net_API_t *api); @@ -48,7 +48,7 @@ } -static void Handler(NetIF_t *netIF, Packet_t *packet) +static void Handler(NetIF_t *netIF, NetPacket_t *packet) { IPv4_Header_t *ipv4Header; int32_t depth, index;
--- a/lpc17xx.h Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc17xx.h Mon Jun 13 13:13:59 2011 +0000 @@ -1078,4 +1078,4 @@ */ #endif // __LPC17xx_H__ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/lpc17xx_clkpwr.h Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc17xx_clkpwr.h Mon Jun 13 13:13:59 2011 +0000 @@ -393,4 +393,4 @@ */ /* --------------------------------- End Of File ------------------------------ */ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/lpc17xx_emac.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc17xx_emac.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -949,4 +949,4 @@ */ /* --------------------------------- End Of File ------------------------------ */ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/lpc17xx_emac.h Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc17xx_emac.h Mon Jun 13 13:13:59 2011 +0000 @@ -698,4 +698,4 @@ */ /* --------------------------------- End Of File ------------------------------ */ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/lpc17xx_libcfg_default.h Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc17xx_libcfg_default.h Mon Jun 13 13:13:59 2011 +0000 @@ -40,7 +40,7 @@ /* Un-comment the line below to compile the library in DEBUG mode, this will expanse the "CHECK_PARAM" macro in the FW library code */ -#define DEBUG +//#define DEBUG /******************* PERIPHERAL FW LIBRARY CONFIGURATION DEFINITIONS ***********************/ @@ -169,4 +169,4 @@ */ /* --------------------------------- End Of File ------------------------------ */ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/lpc17xx_pinsel.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc17xx_pinsel.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -305,4 +305,4 @@ */ /* --------------------------------- End Of File ------------------------------ */ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/lpc17xx_pinsel.h Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc17xx_pinsel.h Mon Jun 13 13:13:59 2011 +0000 @@ -190,4 +190,4 @@ /* --------------------------------- End Of File ------------------------------ */ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/lpc_types.h Sun Jun 12 20:24:23 2011 +0000 +++ b/lpc_types.h Mon Jun 13 13:13:59 2011 +0000 @@ -198,4 +198,4 @@ */ /* --------------------------------- End Of File ------------------------------ */ -/* @endcond */ \ No newline at end of file +/* @endcond */
--- a/mbedNet.h Sun Jun 12 20:24:23 2011 +0000 +++ b/mbedNet.h Mon Jun 13 13:13:59 2011 +0000 @@ -3,13 +3,13 @@ * $Author: benoit $ * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $ * $Rev: 29 $ - * - * - * - * - * + * + * + * + * + * */ - + #ifndef __MBEDNET_H__ #define __MBEDNET_H__ @@ -32,7 +32,7 @@ #define NET_PERIODIC_FUNCTION_MAX_COUNT 5 /* Maximum number of timers */ #define NETIF_MAX_COUNT 2 /* Maximum number of registrable interfaces */ #define ETHERNET_PROTOCOL_MAX_COUNT 3 /* Maximum number of registrable protocols over ethernet */ -#define IPV4_PROTOCOL_MAX_COUNT 4 /* Maximum number of registrable protocols over IPv4 */ +#define IPV4_PROTOCOL_MAX_COUNT 4 /* Maximum number of registrable protocols over IPv4 */ #define ARP_CACHE_MAX_ENTRIES 4 /* Maximum number of entries in the ARP cache */ #define ARP_FUNCTION_PERIOD 10 /* Period in seconds of the ARP periodic function */ #define ARP_MAX_ENTRY_AGE 60 /* Max age of a dynamic entry in seconds */
--- a/mbedNetIF.cpp Sun Jun 12 20:24:23 2011 +0000 +++ b/mbedNetIF.cpp Mon Jun 13 13:13:59 2011 +0000 @@ -225,7 +225,7 @@ extern "C" void ENET_IRQHandler(void) { uint32_t status; - Packet_t rxP; + NetPacket_t rxP; status = LPC_EMAC->IntStatus; LPC_EMAC->IntClear = status;