Super lightweight, not at all robust, TFTP server for FRDM-K64F eval board.

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of FRDM_K64F-Ethernet by Rangel Alvarado

Super lightweight, not at all robust, TFTP server for FRDM-K64F eval board. This tool supports read-only access to two files. It does not support NACK responses or timeouts. The tool is intended for use by our test department to check out Ethernet functionality on our main processor board.

Revision:
2:28ddb9b073ec
Parent:
1:2944c0d494ff
--- a/main.cpp	Thu Oct 11 15:25:58 2018 +0000
+++ b/main.cpp	Thu Oct 11 20:45:38 2018 +0000
@@ -1,8 +1,8 @@
 /*
 ** Super lightweight, not at all robust, TFTP server for FRDM-K64F eval board.
-** This tool supports read-only access to one file. It does not support NACK responses
-** or timeouts. The tool is intended for use by our test department to check out Ethernet
-** functionality on our main processor board.
+** This tool supports read-only access to a couple of files. It does not support
+** NACK responses or timeouts. The tool is intended for use by our test department
+** to check out Ethernet functionality on our main processor board.
 **
 ** 11 October 2018
 ** - am
@@ -73,11 +73,12 @@
 
 int main (void) {
 
+    // Ethernet infrastructure
     EthernetInterface eth;
     UDPSocket tftp_server;
     Endpoint client;
 
-    // create data packet storage    
+    // data packet storage    
     tftp_packet_t packet;
     char *packet_ptr = (char *)&packet;
 
@@ -98,12 +99,12 @@
         led_waiting = LED_ON;
         tftp_server.bind(TFTP_PORT);
         tftp_server.receiveFrom(client, packet_ptr, sizeof(packet));
+        led_waiting = LED_OFF;
 
         // if it's a read request
         if (ntohs(packet.opcode) == RRQ) {
             // and it's for the environment file
             if (!strncmp(packet.request.filename_and_mode, "/production/u-boot-env_txt", 26)) {
-                led_waiting = LED_OFF;
                 // send file
                 int i_block = 1;
                 int i_string = 0;
@@ -129,11 +130,40 @@
                     led_error = LED_OFF;
                 };
             }
-            // error, we only support the u-boot-env.txt file
-            else tftp_send_error(tftp_server, client, 0, "this test board only supports reading the u-boot-env.txt file");
+            // or the binary test file
+            else if (!strncmp(packet.request.filename_and_mode, "/production/ethtest_bin", 23)) {
+                // send a 4M byte file (8192 512-byte blocks)
+                for (int block = 1; block < 8194; block++) { // blocks 1-8192 are full, block 8193 is empty (end of file)
+                    led_connected = LED_ON;
+                    packet.opcode = htons(DATA);
+                    packet.data.block_number = htons(block);
+                    // fill packet with Gray code
+                    for (uint16_t w=0; w<256; w++) {
+                        packet.data.data[w] = (char)((w>>1)^w);
+                        packet.data.data[256+w] = ~packet.data.data[w];
+                    }
+                    tftp_server.sendTo(client, packet_ptr, (block<8193) ? 516 : 4); // 4 = sizeof(opcode + block_number)
+                    led_connected = LED_OFF;
+                    led_error = LED_ON;
+                    tftp_server.receiveFrom(client, packet_ptr, sizeof(packet)); // wait for ACK
+                    led_error = LED_OFF;
+                }
+            }
+            // error, we only support a couple of files
+            else {
+                led_error = LED_ON;
+                tftp_send_error(tftp_server, client, 0, "file not found, this board only has u-boot-env_txt and ethtest_bin");
+                wait(3); // three seconds
+                led_error = LED_OFF;
+            }
         }
         // error, we only support read requests
-        else tftp_send_error(tftp_server, client, 0, "this test board only supports reading the u-boot-env.txt file");
+        else {
+            led_error = LED_ON;
+            tftp_send_error(tftp_server, client, 0, "this test board only supports read requests");
+            wait(3); // three seconds
+            led_error = LED_OFF;
+        }
 
         led_connected = LED_ON;
         led_waiting = LED_ON;