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.

Committer:
Benoit
Date:
Sun Jun 26 09:56:31 2011 +0000
Revision:
7:8e12f7357b9f
Parent:
5:3cd83fcb1467
Added IPv4 global broadcast address to processed frames inside IPv4 layer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Benoit 1:f4040665bc61 1 /*
Benoit 1:f4040665bc61 2 * $Id: Ethernet.c 29 2011-06-11 14:53:08Z benoit $
Benoit 1:f4040665bc61 3 * $Author: benoit $
Benoit 1:f4040665bc61 4 * $Date: 2011-06-11 16:53:08 +0200 (sam., 11 juin 2011) $
Benoit 1:f4040665bc61 5 * $Rev: 29 $
Benoit 1:f4040665bc61 6 *
Benoit 1:f4040665bc61 7 *
Benoit 1:f4040665bc61 8 *
Benoit 1:f4040665bc61 9 *
Benoit 1:f4040665bc61 10 *
Benoit 1:f4040665bc61 11 */
Benoit 1:f4040665bc61 12
Benoit 1:f4040665bc61 13 #include "Ethernet.h"
Benoit 1:f4040665bc61 14 #include "Debug.h"
Benoit 1:f4040665bc61 15 #include <string.h>
Benoit 1:f4040665bc61 16
Benoit 1:f4040665bc61 17
Benoit 1:f4040665bc61 18 #define DEBUG_CURRENT_MODULE_NAME "Ethernet"
Benoit 1:f4040665bc61 19 #define DEBUG_CURRENT_MODULE_ID DEBUG_MODULE_ETHERNET
Benoit 1:f4040665bc61 20
Benoit 1:f4040665bc61 21
Benoit 1:f4040665bc61 22 static void Init(void);
Benoit 1:f4040665bc61 23 static int32_t RegisterProtocol(Protocol_Handler_t *protocolHandler);
Benoit 1:f4040665bc61 24 static int32_t RegisterAPI(Net_API_t *api);
Benoit 5:3cd83fcb1467 25 static void Handler(NetIF_t *netIF, NetPacket_t *packet);
Benoit 1:f4040665bc61 26
Benoit 1:f4040665bc61 27
Benoit 1:f4040665bc61 28 static Protocol_Handler_t *protocolHandlerTable[ETHERNET_PROTOCOL_MAX_COUNT];
Benoit 1:f4040665bc61 29 static int32_t protocolHandlerCount = 0;
Benoit 1:f4040665bc61 30
Benoit 1:f4040665bc61 31 const Ethernet_Addr_t ethernet_Addr_Broadcast = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Benoit 1:f4040665bc61 32 const Ethernet_Addr_t ethernet_Addr_Null = {0, 0, 0, 0, 0, 0};
Benoit 1:f4040665bc61 33
Benoit 1:f4040665bc61 34
Benoit 1:f4040665bc61 35 Protocol_Handler_t ethernet =
Benoit 1:f4040665bc61 36 {
Benoit 1:f4040665bc61 37 PROTOCOL_INDEX_NOT_INITIALIZED, /* Always PROTOCOL_INDEX_NOT_INITIALIZED at initialization */
Benoit 1:f4040665bc61 38 Protocol_ID_Ethernet, /* Protocol ID */
Benoit 1:f4040665bc61 39 PROTOCOL_NUMBER_NONE, /* Protocol number */
Benoit 1:f4040665bc61 40 Init, /* Protocol initialisation function */
Benoit 1:f4040665bc61 41 Handler, /* Protocol handler */
Benoit 1:f4040665bc61 42 RegisterProtocol, /* Protocol registration function */
Benoit 1:f4040665bc61 43 RegisterAPI, /* API registration function */
Benoit 1:f4040665bc61 44 };
Benoit 1:f4040665bc61 45
Benoit 1:f4040665bc61 46
Benoit 1:f4040665bc61 47 static Net_API_t *netAPITable[NET_API_PER_PROTOCOL_MAX_COUNT];
Benoit 1:f4040665bc61 48 static int32_t netAPICount = 0;
Benoit 1:f4040665bc61 49
Benoit 1:f4040665bc61 50
Benoit 1:f4040665bc61 51 static void Init(void)
Benoit 1:f4040665bc61 52 {
Benoit 1:f4040665bc61 53 DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Initializing ethernet layer"));
Benoit 1:f4040665bc61 54 memset(protocolHandlerTable, 0, sizeof(protocolHandlerTable));
Benoit 1:f4040665bc61 55 protocolHandlerCount = 0;
Benoit 1:f4040665bc61 56 memset(netAPITable, 0, sizeof(netAPITable));
Benoit 1:f4040665bc61 57 netAPICount = 0;
Benoit 1:f4040665bc61 58 }
Benoit 1:f4040665bc61 59
Benoit 1:f4040665bc61 60
Benoit 1:f4040665bc61 61 static int32_t RegisterProtocol(Protocol_Handler_t *protocolHandler)
Benoit 1:f4040665bc61 62 {
Benoit 1:f4040665bc61 63 int32_t result = 0;
Benoit 1:f4040665bc61 64
Benoit 1:f4040665bc61 65 if (protocolHandlerCount >= ETHERNET_PROTOCOL_MAX_COUNT)
Benoit 1:f4040665bc61 66 {
Benoit 1:f4040665bc61 67 DEBUG_SOURCE(DEBUG_LEVEL_ERROR, ("Too many protocols"));
Benoit 1:f4040665bc61 68 result = -1;
Benoit 1:f4040665bc61 69 mbedNet_LastError = mbedNetResult_TooManyRegisteredProtocols;
Benoit 1:f4040665bc61 70 goto Exit;
Benoit 1:f4040665bc61 71 }
Benoit 1:f4040665bc61 72
Benoit 1:f4040665bc61 73 protocolHandlerTable[protocolHandlerCount] = protocolHandler;
Benoit 1:f4040665bc61 74 protocolHandler->index = protocolHandlerCount;
Benoit 1:f4040665bc61 75 protocolHandler->Init();
Benoit 1:f4040665bc61 76
Benoit 1:f4040665bc61 77 DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Registered protocol %04X ethernet/%s",
Benoit 1:f4040665bc61 78 ntohs(protocolHandler->protocolNumber),
Benoit 1:f4040665bc61 79 protocol_IDNames[protocolHandler->protocolID]
Benoit 1:f4040665bc61 80 ));
Benoit 1:f4040665bc61 81
Benoit 1:f4040665bc61 82 protocolHandlerCount++;
Benoit 1:f4040665bc61 83
Benoit 1:f4040665bc61 84 Exit:
Benoit 1:f4040665bc61 85 return result;
Benoit 1:f4040665bc61 86 }
Benoit 1:f4040665bc61 87
Benoit 1:f4040665bc61 88
Benoit 1:f4040665bc61 89 static int32_t RegisterAPI(Net_API_t *netAPI)
Benoit 1:f4040665bc61 90 {
Benoit 1:f4040665bc61 91 DEBUG_MODULE(DEBUG_LEVEL_INFO, ("Registering %s API", api_IDNames[netAPI->apiID]));
Benoit 1:f4040665bc61 92 netAPI->InitAPI();
Benoit 1:f4040665bc61 93 netAPITable[netAPICount] = netAPI;
Benoit 1:f4040665bc61 94 netAPICount++;
Benoit 1:f4040665bc61 95 return -1;
Benoit 1:f4040665bc61 96 }
Benoit 1:f4040665bc61 97
Benoit 1:f4040665bc61 98
Benoit 5:3cd83fcb1467 99 void Handler(NetIF_t *netIF, NetPacket_t *packet)
Benoit 1:f4040665bc61 100 {
Benoit 1:f4040665bc61 101 int32_t protocolIndex, index;
Benoit 1:f4040665bc61 102 Ethernet_Proto_t protocolNumber;
Benoit 1:f4040665bc61 103 Protocol_Handler_t *protocolHandler;
Benoit 1:f4040665bc61 104 Ethernet_Header_t *ethernetHeader;
Benoit 1:f4040665bc61 105
Benoit 1:f4040665bc61 106 ethernetHeader = (Ethernet_Header_t *)packet->data;
Benoit 1:f4040665bc61 107 protocolNumber = ethernetHeader->protocol;
Benoit 1:f4040665bc61 108 DEBUG_MODULE(DEBUG_LEVEL_INFO, ("frame of %d bytes for protocol %04X (payload + %02d)", packet->length, ntohs(protocolNumber), sizeof(Ethernet_Header_t)));
Benoit 1:f4040665bc61 109 //Debug_DumpBufferHex(packet, length);
Benoit 1:f4040665bc61 110
Benoit 1:f4040665bc61 111 /* Process API if any */
Benoit 1:f4040665bc61 112 for (index = 0; index < netAPICount; index++)
Benoit 1:f4040665bc61 113 {
Benoit 1:f4040665bc61 114 netAPITable[index]->Hook(netIF, Protocol_ID_Ethernet, packet);
Benoit 1:f4040665bc61 115 }
Benoit 1:f4040665bc61 116
Benoit 1:f4040665bc61 117 for (protocolIndex = 0; protocolIndex < protocolHandlerCount; protocolIndex++)
Benoit 1:f4040665bc61 118 {
Benoit 1:f4040665bc61 119 protocolHandler = protocolHandlerTable[protocolIndex];
Benoit 1:f4040665bc61 120 if (protocolHandler->protocolNumber == protocolNumber)
Benoit 1:f4040665bc61 121 {
Benoit 1:f4040665bc61 122 DEBUG_SOURCE(DEBUG_LEVEL_VERBOSE0, ("'%s' frame of %d bytes", protocol_IDNames[protocolHandler->protocolID], packet->length));
Benoit 1:f4040665bc61 123 NetIF_ProtoPush(packet, sizeof(Ethernet_Header_t), Protocol_ID_Ethernet);
Benoit 1:f4040665bc61 124 protocolHandler->HandlePacket(netIF, packet);
Benoit 1:f4040665bc61 125 break;
Benoit 1:f4040665bc61 126 }
Benoit 1:f4040665bc61 127 }
Benoit 1:f4040665bc61 128 return;
Benoit 1:f4040665bc61 129 }
Benoit 1:f4040665bc61 130
Benoit 1:f4040665bc61 131