Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mark Qvist
committed
Apr 4, 2014
1 parent
6b496c3
commit 77ba514
Showing
6 changed files
with
150 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include "mp1.h" | ||
#include <string.h> | ||
//#include <ctype.h> | ||
|
||
static void mp1Decode(MP1 *mp1) { | ||
MP1Packet packet; // A decoded packet struct | ||
uint8_t *buffer = mp1->buffer; // Get the buffer from the protocol context | ||
|
||
packet.dataLength = mp1->packetLength; | ||
packet.data = buffer; | ||
|
||
if (mp1->callback) mp1->callback(&packet); | ||
} | ||
|
||
void mp1Poll(MP1 *mp1) { | ||
int byte; | ||
|
||
// Read bytes from the modem until we reach EOF | ||
while ((byte = kfile_getc(mp1->modem)) != EOF) { | ||
if (!mp1->escape && byte == HDLC_FLAG) { | ||
// We are not in an escape sequence and we | ||
// found a HDLC_FLAG. This can mean two things: | ||
if (mp1->packetLength >= MP1_MIN_FRAME_LENGTH) { | ||
// We already have more data than the minimum | ||
// frame length, which means the flag signifies | ||
// the end of the packet. Pass control to the | ||
// decoder. | ||
mp1Decode(mp1); | ||
} | ||
// If the above is not the case, this must be the | ||
// beginning of a frame | ||
mp1->reading = true; | ||
mp1->packetLength = 0; | ||
// We have indicated that we are reading, | ||
// and reset the length counter. Now we'll | ||
// continue to the next byte. | ||
continue; | ||
} | ||
|
||
if (!mp1->escape && byte == HDLC_RESET) { | ||
// Not good, we got a reset. The transmitting | ||
// party may have encountered an error. We'll | ||
// stop receiving this packet immediately. | ||
mp1->reading = false; | ||
continue; | ||
} | ||
|
||
if (!mp1->escape && byte == AX25_ESC) { | ||
// We found an escape character. We'll set | ||
// the escape seqeunce indicator so we don't | ||
// interpret the next byte as a reset or flag | ||
mp1->escape = true; | ||
continue; | ||
} | ||
|
||
// Now let's get to the actual reading of the data | ||
if (mp1->reading) { | ||
if (mp1->packetLength < MP1_MAX_FRAME_LENGTH) { | ||
// If the length of the current incoming frame is | ||
// still less than our max length, put the incoming | ||
// byte in the buffer. | ||
mp1->buffer[mp1->packetLength++] = byte; | ||
} else { | ||
// If not, we have a problem: The buffer has overrun | ||
// We need to stop receiving, and the packet will be | ||
// dropped :( | ||
mp1->reading = false; | ||
} | ||
} | ||
// We need to set the escape sequence indicator back | ||
// to false after each byte. | ||
mp1->escape = false; | ||
} | ||
|
||
if (kfile_error(mp1->modem)) { | ||
// If there was an error from the modem, we'll be rude | ||
// and just reset it. No error handling is done for now. | ||
kfile_clearerr(mp1->modem); | ||
} | ||
} | ||
|
||
void mp1Init(MP1 *mp1, KFile *modem, mp1_callback_t callback) { | ||
// Allocate memory for our protocol "object" | ||
memset(mp1, 0, sizeof(*mp1)); | ||
mp1->modem = modem; | ||
mp1->callback = callback; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef PROTOCOL_MP1 | ||
#define PROTOCOL_MP1 | ||
|
||
#include <cfg/compiler.h> | ||
#include <io/kfile.h> | ||
|
||
// Frame sizing | ||
#define MP1_MIN_FRAME_LENGTH 1 | ||
#define MP1_MAX_FRAME_LENGTH 300 | ||
|
||
// We need to know some basic HDLC flag bytes | ||
#define HDLC_FLAG 0x7E | ||
#define HDLC_RESET 0x7F | ||
#define AX25_ESC 0x1B | ||
|
||
// Just a forward declaration that this struct exists | ||
struct MP1Packet; | ||
|
||
// The type of a callback function for passing | ||
// back a decoded packet | ||
typedef void (*mp1_callback_t)(struct MP1Packet *packet); | ||
|
||
// Struct for a protocol context | ||
typedef struct MP1 { | ||
uint8_t buffer[MP1_MAX_FRAME_LENGTH]; // A buffer for incoming packets | ||
KFile *modem; // KFile access to the modem | ||
size_t packetLength; // Counter for received packet length | ||
mp1_callback_t callback; // The function to call when a packet has been received | ||
bool reading; // True when we have seen a HDLC flag | ||
bool escape; // We need to know if we are in an escape sequence | ||
} MP1; | ||
|
||
// A struct encapsulating a network packet | ||
typedef struct MP1Packet { | ||
const uint8_t *data; // Pointer to the actual data in the packet | ||
size_t dataLength; // The length of the received data | ||
} MP1Packet; | ||
|
||
void mp1Init(MP1 *mp1, KFile *modem, mp1_callback_t callback); | ||
void mp1Poll(MP1 *mp1); | ||
void mp1Send(MP1 *mp1, const void *_buffer, size_t length); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#define VERS_BUILD 126 | ||
#define VERS_BUILD 136 | ||
#define VERS_HOST "vixen" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#!/bin/bash | ||
avrdude -p m328p -c arduino -P /dev/ttyACM$1 -b 115200 -F -U flash:w:images/Modem.hex | ||
avrdude -p m328p -c arduino -P /dev/tty$1 -b 115200 -F -U flash:w:images/Modem.hex |