|
1 | 1 | # TftpServer
|
2 |
| -TFTP Server for Particle devices |
| 2 | + |
| 3 | +A Particle library for TftpServer |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +Connect an SD card on SPI or SPI1, add the TftpServer library to your project and follow this example: |
| 8 | + |
| 9 | +NOTE: comment out or uncomment the appropriate SdFat instance depending on whether you are using SPI or SPI1 |
| 10 | + |
| 11 | +``` |
| 12 | +#include "TftpServer.h" |
| 13 | +#include "SdFat.h" |
| 14 | +TftpServer tftpServer; |
| 15 | +
|
| 16 | +// Primary SPI with DMA |
| 17 | +// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default) |
| 18 | +//const uint8_t SD_CS_PIN = A2; |
| 19 | +//SdFat sd; |
| 20 | +
|
| 21 | +// Secondary SPI with DMA |
| 22 | +// SCK => D4, MISO => D3, MOSI => D2, SS => D5 |
| 23 | +const uint8_t SD_CS_PIN = D5; |
| 24 | +SdFat sd(1); |
| 25 | +
|
| 26 | +void setup() { |
| 27 | +
|
| 28 | + // initialize file system. |
| 29 | + if (!sd.begin (SD_CS_PIN, SPI_FULL_SPEED)) { |
| 30 | + sd.initErrorPrint(); |
| 31 | + } |
| 32 | + |
| 33 | + // Let's put some data on your card |
| 34 | + std::string fileName = "DATATEST.TXT"; |
| 35 | +
|
| 36 | + if (!sd.exists (fileName.c_str())) { |
| 37 | +
|
| 38 | + File myFile; |
| 39 | +
|
| 40 | + // open the file for write |
| 41 | + if (!myFile.open (fileName.c_str(), O_RDWR | O_CREAT)) { |
| 42 | + |
| 43 | + sd.errorHalt ("opening mytest.txt for write failed"); |
| 44 | + } |
| 45 | +
|
| 46 | + for (int i = 0; i < 1000; ++i) { |
| 47 | + myFile.println ("testing 1, 2, 3."); |
| 48 | + } |
| 49 | +
|
| 50 | + myFile.printlnf ("fileSize: %d", myFile.fileSize()); |
| 51 | +
|
| 52 | + // close the file: |
| 53 | + myFile.close(); |
| 54 | + } |
| 55 | +
|
| 56 | + // start the TFTP server and pass the SdFat file system |
| 57 | + tftpServer.begin(&sd); |
| 58 | +} |
| 59 | +
|
| 60 | +void loop() { |
| 61 | + |
| 62 | + if (tftpServer.checkForPacket()) { |
| 63 | + |
| 64 | + tftpServer.processRequest(); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Using a TFTP client, connect to the IP address assigned to your device and use the get command |
| 70 | + |
| 71 | +See the [examples](examples) folder for more details. |
| 72 | + |
| 73 | +## Documentation |
| 74 | + |
| 75 | +This is a VERY minimal implementation of a TFTP Server for the Particle |
| 76 | +environment (Tested on the P0 and P1). The initial version will only support |
| 77 | +the GET methods and does not implement any methods for PUT. This made the most |
| 78 | +sense during development since it seemed like there was more need to get files |
| 79 | +off of an SD card than there were to put them on. |
| 80 | + |
| 81 | +The server is opened on port 69 by default. The clientConnected method should |
| 82 | +be run in loop as often as possible to improve responsiveness and to avoid having |
| 83 | +the client send duplicate requests due to retransmission timeouts. Once TRUE is |
| 84 | +returned by clientConnected() it means a UDP packet was received on port 69 and |
| 85 | +you can call handleClientRequest() to do the rest. The function will block until |
| 86 | +the client request is taken care of and then control will pass back to the calling |
| 87 | +function. |
| 88 | + |
| 89 | +In order to have files to send, this library relies on the SdFat-Particle |
| 90 | +library. A pointer to an SdFat object is passed as part of begin() so the |
| 91 | +TFTP server will have access to the SD card without having to create it's own |
| 92 | +instance of the file system. This also makes the library agnostic as to which |
| 93 | +SPI instance is used by SdFat (SPI vs. SPI1). |
| 94 | + |
| 95 | +It would be best to ensure that no files are open prior to passing control off to |
| 96 | +the TFTP server. Since files are opened and closed as part of the GET/PUT process, |
| 97 | +it could potentially cause file corruption if there was already a file open. |
| 98 | + |
| 99 | +The attempt has been made to stick as close to the TFTP protocol as possible. |
| 100 | +Timeouts were implemented as best I could figure out because the |
| 101 | +specification doesn't cover timeouts and retransmission explicitly. Also, to make |
| 102 | +like simpler with the limited number of sockets available, the transfer ID for the |
| 103 | +server is kept at 69 rather than choosing a new random port number. |
| 104 | + |
| 105 | +The specification can be located at: https://tools.ietf.org/html/rfc1350 |
| 106 | + |
| 107 | +@note A buffer size of 516 bytes is allocated for TFTP transfers |
| 108 | +@note Library developed using ARM GCC 5.3 |
| 109 | + |
| 110 | +From RFC 1350: |
| 111 | + |
| 112 | +TFTP Formats |
| 113 | + |
| 114 | + Type Op # Format without header |
| 115 | + |
| 116 | + 2 bytes string 1 byte string 1 byte |
| 117 | + ----------------------------------------------- |
| 118 | + RRQ/ | 01/02 | Filename | 0 | Mode | 0 | |
| 119 | + WRQ ----------------------------------------------- |
| 120 | + 2 bytes 2 bytes n bytes |
| 121 | + --------------------------------- |
| 122 | + DATA | 03 | Block # | Data | |
| 123 | + --------------------------------- |
| 124 | + 2 bytes 2 bytes |
| 125 | + ------------------- |
| 126 | + ACK | 04 | Block # | |
| 127 | + -------------------- |
| 128 | + 2 bytes 2 bytes string 1 byte |
| 129 | + ---------------------------------------- |
| 130 | + ERROR | 05 | ErrorCode | ErrMsg | 0 | |
| 131 | + ---------------------------------------- |
| 132 | + |
| 133 | +## LICENSE |
| 134 | + |
| 135 | +Licensed under the MIT License |
| 136 | + |
| 137 | +Copyright (c) 2017 Micah L. Abelson |
| 138 | + |
| 139 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 140 | +of this software and associated documentation files (the "Software"), to deal |
| 141 | +in the Software without restriction, including without limitation the rights |
| 142 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 143 | +copies of the Software, and to permit persons to whom the Software is |
| 144 | +furnished to do so, subject to the following conditions: |
| 145 | + |
| 146 | +The above copyright notice and this permission notice shall be included in all |
| 147 | +copies or substantial portions of the Software. |
| 148 | + |
| 149 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 150 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 151 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 152 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 153 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 154 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 155 | +SOFTWARE. |
0 commit comments