Adds file transfer capability to SimpleSerialProtocol

Dependents:   SerialFileReceiver

Committer:
p3p
Date:
Wed Aug 27 17:59:05 2014 +0000
Revision:
0:31dff1f79b4f
Moved to library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
p3p 0:31dff1f79b4f 1 #ifndef _SFT_PROTOCOL_H
p3p 0:31dff1f79b4f 2 #define _SFT_PROTOCOL_H
p3p 0:31dff1f79b4f 3
p3p 0:31dff1f79b4f 4 #include <mbed.h>
p3p 0:31dff1f79b4f 5 #include <SimpleSerialProtocol/Protocol.h>
p3p 0:31dff1f79b4f 6
p3p 0:31dff1f79b4f 7 //class will receive a packet and echo it back out
p3p 0:31dff1f79b4f 8 class SFTProtocol {
p3p 0:31dff1f79b4f 9 public:
p3p 0:31dff1f79b4f 10 SFTProtocol() { //LED1 to 4 for a status led, NC to disable
p3p 0:31dff1f79b4f 11 transfer_in_progress = false;
p3p 0:31dff1f79b4f 12 chunks_total = 0;
p3p 0:31dff1f79b4f 13 chunks_last = 0;
p3p 0:31dff1f79b4f 14 packet_retry_attempts = 0;
p3p 0:31dff1f79b4f 15 fp = 0;
p3p 0:31dff1f79b4f 16 }
p3p 0:31dff1f79b4f 17 virtual ~SFTProtocol() {};
p3p 0:31dff1f79b4f 18
p3p 0:31dff1f79b4f 19 //the callbacks and packet sending methods
p3p 0:31dff1f79b4f 20 virtual void update(SimpleSerialProtocol::Protocol* comms);
p3p 0:31dff1f79b4f 21 void onFileStart(SimpleSerialProtocol::Protocol* comms, SimpleSerialProtocol::Packet* packet);
p3p 0:31dff1f79b4f 22 void ack(SimpleSerialProtocol::Protocol* comms, uint8_t ack_type);
p3p 0:31dff1f79b4f 23 void onFileStream(SimpleSerialProtocol::Protocol* comms, SimpleSerialProtocol::Packet* packet);
p3p 0:31dff1f79b4f 24
p3p 0:31dff1f79b4f 25 //variables to control the transfer and save the file
p3p 0:31dff1f79b4f 26 bool transfer_in_progress;
p3p 0:31dff1f79b4f 27 int chunks_total;
p3p 0:31dff1f79b4f 28 int chunks_last;
p3p 0:31dff1f79b4f 29 Timer last_chunk_timer;
p3p 0:31dff1f79b4f 30 int packet_retry_attempts;
p3p 0:31dff1f79b4f 31 FILE *fp;
p3p 0:31dff1f79b4f 32
p3p 0:31dff1f79b4f 33
p3p 0:31dff1f79b4f 34 //This packed is used to start a file transfer it is sent by the client
p3p 0:31dff1f79b4f 35 //once received a file is created and an ack packet sent to start the file stream
p3p 0:31dff1f79b4f 36 class FileStartPacket : public SimpleSerialProtocol::Packet {
p3p 0:31dff1f79b4f 37 public:
p3p 0:31dff1f79b4f 38 FileStartPacket() {}
p3p 0:31dff1f79b4f 39 virtual ~FileStartPacket() {}
p3p 0:31dff1f79b4f 40
p3p 0:31dff1f79b4f 41 #pragma pack(push, 1) //must pack the structure to byte boundary for raw recast to work reliably
p3p 0:31dff1f79b4f 42 struct Interface {
p3p 0:31dff1f79b4f 43 Interface() {
p3p 0:31dff1f79b4f 44 type = 1; // initialise the type
p3p 0:31dff1f79b4f 45 }
p3p 0:31dff1f79b4f 46 uint8_t type;
p3p 0:31dff1f79b4f 47 uint8_t name[9];
p3p 0:31dff1f79b4f 48 uint8_t ext[4];
p3p 0:31dff1f79b4f 49 uint32_t chunks;
p3p 0:31dff1f79b4f 50 } interface;
p3p 0:31dff1f79b4f 51 #pragma pack(pop)
p3p 0:31dff1f79b4f 52
p3p 0:31dff1f79b4f 53 };
p3p 0:31dff1f79b4f 54
p3p 0:31dff1f79b4f 55 //A very simple Ack packet used to inform the client that we need more data, and other commands based on the ack_type value
p3p 0:31dff1f79b4f 56 class AckFileStartPacket : public SimpleSerialProtocol::Packet {
p3p 0:31dff1f79b4f 57 public:
p3p 0:31dff1f79b4f 58 AckFileStartPacket() {}
p3p 0:31dff1f79b4f 59 virtual ~AckFileStartPacket() {}
p3p 0:31dff1f79b4f 60
p3p 0:31dff1f79b4f 61 #pragma pack(push, 1) //must pack the structure to byte boundary for raw recast to work reliably
p3p 0:31dff1f79b4f 62 struct Interface {
p3p 0:31dff1f79b4f 63 Interface() {
p3p 0:31dff1f79b4f 64 type = 2; // initialise the type
p3p 0:31dff1f79b4f 65 }
p3p 0:31dff1f79b4f 66 uint8_t type;
p3p 0:31dff1f79b4f 67 uint8_t ack_type;
p3p 0:31dff1f79b4f 68 } interface;
p3p 0:31dff1f79b4f 69 #pragma pack(pop)
p3p 0:31dff1f79b4f 70
p3p 0:31dff1f79b4f 71 };
p3p 0:31dff1f79b4f 72
p3p 0:31dff1f79b4f 73 //This is the packet used to send the file data 128 bytes at a time
p3p 0:31dff1f79b4f 74 //its posible to send upto 256 bytes but this was causing my xbees to drop a lot of data
p3p 0:31dff1f79b4f 75 class FileStreamPacket : public SimpleSerialProtocol::Packet {
p3p 0:31dff1f79b4f 76 public:
p3p 0:31dff1f79b4f 77 FileStreamPacket() { }
p3p 0:31dff1f79b4f 78 virtual ~FileStreamPacket() {}
p3p 0:31dff1f79b4f 79
p3p 0:31dff1f79b4f 80 #pragma pack(push, 1) //must pack the structure to byte boundary for raw recast to work reliably
p3p 0:31dff1f79b4f 81 struct Interface {
p3p 0:31dff1f79b4f 82 Interface() {
p3p 0:31dff1f79b4f 83 type = 3; // initialise the type
p3p 0:31dff1f79b4f 84 }
p3p 0:31dff1f79b4f 85 uint8_t type;
p3p 0:31dff1f79b4f 86 uint16_t chunk;
p3p 0:31dff1f79b4f 87 uint8_t length;
p3p 0:31dff1f79b4f 88 uint8_t data[128];
p3p 0:31dff1f79b4f 89
p3p 0:31dff1f79b4f 90 } interface;
p3p 0:31dff1f79b4f 91 #pragma pack(pop)
p3p 0:31dff1f79b4f 92
p3p 0:31dff1f79b4f 93 };
p3p 0:31dff1f79b4f 94
p3p 0:31dff1f79b4f 95 };
p3p 0:31dff1f79b4f 96
p3p 0:31dff1f79b4f 97 #endif