simple reliable networking over ethernet. Provides IPv4 ARP, ICMP echo reply, and UDP unicast. Does NOT provide TCP, that's not simple :-).
snet.cpp@0:6df0a6ed91d4, 2015-03-04 (annotated)
- Committer:
- altasoul
- Date:
- Wed Mar 04 02:04:32 2015 +0000
- Revision:
- 0:6df0a6ed91d4
- Child:
- 1:9c211ac06a12
refactor into Snet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
altasoul | 0:6df0a6ed91d4 | 1 | #include "snet.h" |
altasoul | 0:6df0a6ed91d4 | 2 | |
altasoul | 0:6df0a6ed91d4 | 3 | //extern MODSERIAL pc; |
altasoul | 0:6df0a6ed91d4 | 4 | |
altasoul | 0:6df0a6ed91d4 | 5 | //extern void print_hex(uint8_t *p, int len); |
altasoul | 0:6df0a6ed91d4 | 6 | |
altasoul | 0:6df0a6ed91d4 | 7 | //volatile uint32_t enet_rx_cnt; |
altasoul | 0:6df0a6ed91d4 | 8 | //volatile int enet_rx_balance; |
altasoul | 0:6df0a6ed91d4 | 9 | |
altasoul | 0:6df0a6ed91d4 | 10 | extern uint8_t my_ip[4]; |
altasoul | 0:6df0a6ed91d4 | 11 | |
altasoul | 0:6df0a6ed91d4 | 12 | |
altasoul | 0:6df0a6ed91d4 | 13 | //uint8_t correspondent_mac[6]; // correspondent's MAC |
altasoul | 0:6df0a6ed91d4 | 14 | const uint8_t Snet::broadcast_mac[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
altasoul | 0:6df0a6ed91d4 | 15 | //const char ourEtherType[] = { 0x82, 0x48 }; |
altasoul | 0:6df0a6ed91d4 | 16 | const uint8_t Snet::ipEtherType[] = { 0x08, 0x00 }; |
altasoul | 0:6df0a6ed91d4 | 17 | const uint8_t Snet::arp_req_payload_prefix[] = { 0x0, 0x1, 0x8, 0x0, 0x6, 0x4, 0x0, 0x1 }; |
altasoul | 0:6df0a6ed91d4 | 18 | |
altasoul | 0:6df0a6ed91d4 | 19 | extern void interpret_inet_packet(uint8_t *buf, int len); |
altasoul | 0:6df0a6ed91d4 | 20 | extern void send_packet_to_radio(uint8_t const *, int); |
altasoul | 0:6df0a6ed91d4 | 21 | |
altasoul | 0:6df0a6ed91d4 | 22 | |
altasoul | 0:6df0a6ed91d4 | 23 | |
altasoul | 0:6df0a6ed91d4 | 24 | Snet::Snet(): eth() |
altasoul | 0:6df0a6ed91d4 | 25 | { |
altasoul | 0:6df0a6ed91d4 | 26 | eth.address((char *) my_mac); |
altasoul | 0:6df0a6ed91d4 | 27 | printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
altasoul | 0:6df0a6ed91d4 | 28 | my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5]); |
altasoul | 0:6df0a6ed91d4 | 29 | printf("no IP\r\n"); |
altasoul | 0:6df0a6ed91d4 | 30 | #if 0 // FIXME |
altasoul | 0:6df0a6ed91d4 | 31 | LPC_EMAC->IntEnable = 0; // disable all Ethernet interrupts |
altasoul | 0:6df0a6ed91d4 | 32 | LPC_EMAC->IntClear = ~0; // clear all interrupts |
altasoul | 0:6df0a6ed91d4 | 33 | |
altasoul | 0:6df0a6ed91d4 | 34 | //enet_rx_balance = 0; |
altasoul | 0:6df0a6ed91d4 | 35 | // Enable Ethernet TX and RX Packet Interrupts: |
altasoul | 0:6df0a6ed91d4 | 36 | //LPC_EMAC->IntEnable |= (ETH_RxFinishedInt | ETH_TxFinishedInt); |
altasoul | 0:6df0a6ed91d4 | 37 | LPC_EMAC->IntEnable |= (ETH_RxDoneInt | ETH_TxDoneInt); |
altasoul | 0:6df0a6ed91d4 | 38 | NVIC_SetPriority(ENET_IRQn, 0); // lower numbers have priority |
altasoul | 0:6df0a6ed91d4 | 39 | |
altasoul | 0:6df0a6ed91d4 | 40 | // Enable the interrupt. |
altasoul | 0:6df0a6ed91d4 | 41 | NVIC_EnableIRQ(ENET_IRQn); |
altasoul | 0:6df0a6ed91d4 | 42 | #endif |
altasoul | 0:6df0a6ed91d4 | 43 | } |
altasoul | 0:6df0a6ed91d4 | 44 | |
altasoul | 0:6df0a6ed91d4 | 45 | Snet::Snet(const uint8_t *ip): eth() |
altasoul | 0:6df0a6ed91d4 | 46 | { |
altasoul | 0:6df0a6ed91d4 | 47 | eth.address((char *) my_mac); |
altasoul | 0:6df0a6ed91d4 | 48 | printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
altasoul | 0:6df0a6ed91d4 | 49 | my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5]); |
altasoul | 0:6df0a6ed91d4 | 50 | memcpy(my_ip, ip, 4); |
altasoul | 0:6df0a6ed91d4 | 51 | printf("IP %d.%d.%d.%d\r\n", |
altasoul | 0:6df0a6ed91d4 | 52 | my_ip[0], my_ip[1], my_ip[2], my_ip[3]); |
altasoul | 0:6df0a6ed91d4 | 53 | #if 0 // FIXME |
altasoul | 0:6df0a6ed91d4 | 54 | LPC_EMAC->IntEnable = 0; // disable all Ethernet interrupts |
altasoul | 0:6df0a6ed91d4 | 55 | LPC_EMAC->IntClear = ~0; // clear all interrupts |
altasoul | 0:6df0a6ed91d4 | 56 | |
altasoul | 0:6df0a6ed91d4 | 57 | //enet_rx_balance = 0; |
altasoul | 0:6df0a6ed91d4 | 58 | // Enable Ethernet TX and RX Packet Interrupts: |
altasoul | 0:6df0a6ed91d4 | 59 | //LPC_EMAC->IntEnable |= (ETH_RxFinishedInt | ETH_TxFinishedInt); |
altasoul | 0:6df0a6ed91d4 | 60 | LPC_EMAC->IntEnable |= (ETH_RxDoneInt | ETH_TxDoneInt); |
altasoul | 0:6df0a6ed91d4 | 61 | NVIC_SetPriority(ENET_IRQn, 0); // lower numbers have priority |
altasoul | 0:6df0a6ed91d4 | 62 | |
altasoul | 0:6df0a6ed91d4 | 63 | // Enable the interrupt. |
altasoul | 0:6df0a6ed91d4 | 64 | NVIC_EnableIRQ(ENET_IRQn); |
altasoul | 0:6df0a6ed91d4 | 65 | #endif |
altasoul | 0:6df0a6ed91d4 | 66 | } |
altasoul | 0:6df0a6ed91d4 | 67 | |
altasoul | 0:6df0a6ed91d4 | 68 | |
altasoul | 0:6df0a6ed91d4 | 69 | |
altasoul | 0:6df0a6ed91d4 | 70 | void Snet::handle_arp_request(uint8_t *buf, int len) { |
altasoul | 0:6df0a6ed91d4 | 71 | #if 0 |
altasoul | 0:6df0a6ed91d4 | 72 | printf("Arp from %02x:%02x:%02x:%02x:%02x:%02x ", |
altasoul | 0:6df0a6ed91d4 | 73 | buf[ENET_SMAC_O+0], buf[ENET_SMAC_O+1], |
altasoul | 0:6df0a6ed91d4 | 74 | buf[ENET_SMAC_O+2], buf[ENET_SMAC_O+3], |
altasoul | 0:6df0a6ed91d4 | 75 | buf[ENET_SMAC_O+4], buf[ENET_SMAC_O+5]);*/ |
altasoul | 0:6df0a6ed91d4 | 76 | #endif |
altasoul | 0:6df0a6ed91d4 | 77 | eth.read((char *) &buf[ENET_PAYLOAD_O], len-ENET_PAYLOAD_O); |
altasoul | 0:6df0a6ed91d4 | 78 | /*print_hex((uint8_t *) &buf[ENET_PAYLOAD_O], len-ENET_PAYLOAD_O);*/ |
altasoul | 0:6df0a6ed91d4 | 79 | if (!(memcmp(&buf[ENET_PAYLOAD_O], arp_req_payload_prefix, sizeof(arp_req_payload_prefix)) // ARP request |
altasoul | 0:6df0a6ed91d4 | 80 | || memcmp(&buf[ENET_ARP_TPA_O], my_ip, sizeof(my_ip)))) { // for me |
altasoul | 0:6df0a6ed91d4 | 81 | //ARP request for me. Build reply. |
altasoul | 0:6df0a6ed91d4 | 82 | // Ethernet packet addresses: |
altasoul | 0:6df0a6ed91d4 | 83 | memcpy(&buf[ENET_DMAC_O], &buf[ENET_SMAC_O], 6); // Back to sender |
altasoul | 0:6df0a6ed91d4 | 84 | memcpy(&buf[ENET_SMAC_O], my_mac, 6); // from me |
altasoul | 0:6df0a6ed91d4 | 85 | // ARP protocol packet: |
altasoul | 0:6df0a6ed91d4 | 86 | buf[ENET_ARP_OPER_O+1] = 0x02; // we are replying |
altasoul | 0:6df0a6ed91d4 | 87 | memcpy(&buf[ENET_ARP_THA_O], &buf[ENET_ARP_SHA_O], 6); // to hardware address of request sender |
altasoul | 0:6df0a6ed91d4 | 88 | memcpy(&buf[ENET_ARP_TPA_O], &buf[ENET_ARP_SPA_O], 4); // to IP address of request sender |
altasoul | 0:6df0a6ed91d4 | 89 | memcpy(&buf[ENET_ARP_SHA_O], my_mac, 6); // from my hardware address |
altasoul | 0:6df0a6ed91d4 | 90 | memcpy(&buf[ENET_ARP_SPA_O], my_ip, 4); // and my IP address |
altasoul | 0:6df0a6ed91d4 | 91 | //printf("ARP replying\r\n"); |
altasoul | 0:6df0a6ed91d4 | 92 | //print_hex((uint8_t *) buf, len); |
altasoul | 0:6df0a6ed91d4 | 93 | eth.write((char *) buf, len); |
altasoul | 0:6df0a6ed91d4 | 94 | eth.send(); |
altasoul | 0:6df0a6ed91d4 | 95 | }; |
altasoul | 0:6df0a6ed91d4 | 96 | }; |
altasoul | 0:6df0a6ed91d4 | 97 | |
altasoul | 0:6df0a6ed91d4 | 98 | void Snet::got_broadcast(uint8_t *buf, int len) { |
altasoul | 0:6df0a6ed91d4 | 99 | #if 0 |
altasoul | 0:6df0a6ed91d4 | 100 | printf("Broadcast from: %02x:%02x:%02x:%02x:%02x:%02x type %02x%02x count %d\r\n", |
altasoul | 0:6df0a6ed91d4 | 101 | buf[ENET_SMAC_O+0], buf[ENET_SMAC_O+1], |
altasoul | 0:6df0a6ed91d4 | 102 | buf[ENET_SMAC_O+2], buf[ENET_SMAC_O+3], |
altasoul | 0:6df0a6ed91d4 | 103 | buf[ENET_SMAC_O+4], buf[ENET_SMAC_O+5], |
altasoul | 0:6df0a6ed91d4 | 104 | buf[ENET_ETHERTYPE_O+0], buf[ENET_ETHERTYPE_O+1], |
altasoul | 0:6df0a6ed91d4 | 105 | enet_rx_int_cnt); |
altasoul | 0:6df0a6ed91d4 | 106 | #endif |
altasoul | 0:6df0a6ed91d4 | 107 | // ARP request? |
altasoul | 0:6df0a6ed91d4 | 108 | if (buf[ENET_ETHERTYPE_O+0] == 0x08 && buf[ENET_ETHERTYPE_O+1] == 0x06) |
altasoul | 0:6df0a6ed91d4 | 109 | handle_arp_request(buf, len); |
altasoul | 0:6df0a6ed91d4 | 110 | }; |
altasoul | 0:6df0a6ed91d4 | 111 | |
altasoul | 0:6df0a6ed91d4 | 112 | void Snet::got_unicast(uint8_t *buf, int len) { |
altasoul | 0:6df0a6ed91d4 | 113 | if (memcmp(&buf[ENET_ETHERTYPE_O], ipEtherType, 2)) return; // IP packets only from this point |
altasoul | 0:6df0a6ed91d4 | 114 | // printf("Unicast from: %02x:%02x:%02x:%02x:%02x:%02x %d\r\n", |
altasoul | 0:6df0a6ed91d4 | 115 | // buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], enet_rx_int_cnt); |
altasoul | 0:6df0a6ed91d4 | 116 | memcpy(correspondent_mac, &buf[ENET_SMAC_O], sizeof(correspondent_mac)); |
altasoul | 0:6df0a6ed91d4 | 117 | eth.read((char *) &buf[ENET_PAYLOAD_O], len-ENET_PAYLOAD_O); // Read in the whole rest of the packet |
altasoul | 0:6df0a6ed91d4 | 118 | interpret_inet_packet(buf, len); |
altasoul | 0:6df0a6ed91d4 | 119 | } |
altasoul | 0:6df0a6ed91d4 | 120 | |
altasoul | 0:6df0a6ed91d4 | 121 | int Snet::rx_and_process_one_packet() { |
altasoul | 0:6df0a6ed91d4 | 122 | #if 0 |
altasoul | 0:6df0a6ed91d4 | 123 | // if the damn Ethernet worked correctly: |
altasoul | 0:6df0a6ed91d4 | 124 | uint8_t buf[0x600]; |
altasoul | 0:6df0a6ed91d4 | 125 | #else |
altasoul | 0:6df0a6ed91d4 | 126 | uint8_t buf[750+6+6+2]; // Experimentally-derived maximum packet size is 750 at the IP layer |
altasoul | 0:6df0a6ed91d4 | 127 | #endif |
altasoul | 0:6df0a6ed91d4 | 128 | int rv = 0; |
altasoul | 0:6df0a6ed91d4 | 129 | int len; |
altasoul | 0:6df0a6ed91d4 | 130 | |
altasoul | 0:6df0a6ed91d4 | 131 | while ((len = eth.receive()) > 0) { |
altasoul | 0:6df0a6ed91d4 | 132 | eth.read((char *) buf, ENET_PAYLOAD_O); // destination MAC, source MAC, EtherType, lengcode |
altasoul | 0:6df0a6ed91d4 | 133 | enet_rx_cnt++; |
altasoul | 0:6df0a6ed91d4 | 134 | if (memcmp(&buf[ENET_DMAC_O], my_mac, 6) == 0) { |
altasoul | 0:6df0a6ed91d4 | 135 | got_unicast(buf, len); |
altasoul | 0:6df0a6ed91d4 | 136 | rv = 1; |
altasoul | 0:6df0a6ed91d4 | 137 | } else if (memcmp(&buf[ENET_DMAC_O], broadcast_mac, 6) == 0) { |
altasoul | 0:6df0a6ed91d4 | 138 | got_broadcast(buf, len); |
altasoul | 0:6df0a6ed91d4 | 139 | rv = 2; |
altasoul | 0:6df0a6ed91d4 | 140 | } |
altasoul | 0:6df0a6ed91d4 | 141 | } |
altasoul | 0:6df0a6ed91d4 | 142 | return rv; |
altasoul | 0:6df0a6ed91d4 | 143 | } |
altasoul | 0:6df0a6ed91d4 | 144 |