Skip to content

Commit b7dc94e

Browse files
committed
initial library commit
1 parent 8987543 commit b7dc94e

File tree

6 files changed

+1120
-1
lines changed

6 files changed

+1120
-1
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Micah L. Abelson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+154-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,155 @@
11
# 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.

examples/usage/usage.ino

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Example usage for TftpServer library by Micah L. Abelson.
2+
3+
#include "TftpServer.h"
4+
#include "SdFat.h"
5+
TftpServer tftpServer;
6+
7+
// Primary SPI with DMA
8+
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
9+
//const uint8_t SD_CS_PIN = A2;
10+
//SdFat sd;
11+
12+
// Secondary SPI with DMA
13+
// SCK => D4, MISO => D3, MOSI => D2, SS => D5
14+
const uint8_t SD_CS_PIN = D5;
15+
SdFat sd(1);
16+
17+
void setup() {
18+
19+
// initialize file system.
20+
if (!sd.begin (SD_CS_PIN, SPI_FULL_SPEED)) {
21+
sd.initErrorPrint();
22+
}
23+
24+
// Let's put some data on your card
25+
std::string fileName = "DATATEST.TXT";
26+
27+
if (!sd.exists (fileName.c_str())) {
28+
29+
File myFile;
30+
31+
// open the file for write
32+
if (!myFile.open (fileName.c_str(), O_RDWR | O_CREAT)) {
33+
34+
sd.errorHalt ("opening mytest.txt for write failed");
35+
}
36+
37+
for (int i = 0; i < 1000; ++i) {
38+
myFile.println ("testing 1, 2, 3.");
39+
}
40+
41+
myFile.printlnf ("fileSize: %d", myFile.fileSize());
42+
43+
// close the file:
44+
myFile.close();
45+
}
46+
47+
// start the TFTP server and pass the SdFat file system
48+
tftpServer.begin(&sd);
49+
}
50+
51+
void loop() {
52+
53+
if (tftpServer.checkForPacket()) {
54+
55+
tftpServer.processRequest();
56+
}
57+
}

library.properties

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Fill in information about your library then remove # from the start of lines
2+
# https://docs.particle.io/guide/tools-and-features/libraries/#library-properties-fields
3+
name=TftpServer
4+
version=0.1.0
5+
author=Micah L. Abelson
6+
# license=insert your choice of license here
7+
# sentence=one sentence description of this library
8+
# paragraph=a longer description of this library, always prepended with sentence when shown
9+
# url=the URL of the project, like https://github.com/mygithub_user/my_repo
10+
# repository=git repository for the project, like https://github.com/mygithub_user/my_repo.git
11+
# architectures=a list of supported boards if this library is hardware dependent, like particle-photon,particle-electron

0 commit comments

Comments
 (0)