Browse Source

Fix this so that it compiles.


			
			
				master
			
			
		
DrZiplok@gmail.com 15 years ago
parent
commit
bd2ba2fcc9
  1. 18
      libraries/APM_BinComm/APM_BinComm.cpp
  2. 137
      libraries/APM_BinComm/APM_BinComm.h
  3. 546
      libraries/APM_BinComm/protocol/protocol.h
  4. 20
      libraries/APM_BinComm/protocol/protogen.awk

18
libraries/APM_BinComm/APM_BinComm.cpp

@ -27,15 +27,15 @@
/// @brief Implementation of the ArduPilot Mega binary communications /// @brief Implementation of the ArduPilot Mega binary communications
/// library. /// library.
#include "WProgram.h"
#include "APM_BinComm.h" #include "APM_BinComm.h"
#include "WProgram.h"
/// @name decoder state machine phases /// @name decoder state machine phases
//@{ //@{
#define DEC_WAIT_P1 0 #define DEC_WAIT_P1 0
#define DEC_WAIT_P2 1 #define DEC_WAIT_P2 1
#define DEC_WAIT_HEADER 2 #define DEC_WAIT_HEADER 2
#define DEC_WAIT_PACKET 3 #define DEC_WAIT_MESSAGE 3
#define DEC_WAIT_SUM_A 4 #define DEC_WAIT_SUM_A 4
#define DEC_WAIT_SUM_B 5 #define DEC_WAIT_SUM_B 5
//@} //@}
@ -44,7 +44,7 @@
/// inter-byte timeout for decode (ms) /// inter-byte timeout for decode (ms)
#define DEC_MESSAGE_TIMEOUT 100 #define DEC_MESSAGE_TIMEOUT 100
BinComm::BinComm(const BinComm::BCMessageHandler *handlerTable, BinComm::BinComm(const BinComm::MessageHandler *handlerTable,
FastSerial *interface) : FastSerial *interface) :
_handlerTable(handlerTable), _handlerTable(handlerTable),
_interface(interface) _interface(interface)
@ -119,7 +119,7 @@ BinComm::_decode(uint8_t inByte)
// prepare for the header // prepare for the header
_bytesIn = 0; _bytesIn = 0;
_bytesExpected = sizeof(struct msg_packet_header); _bytesExpected = sizeof(MessageHeader);
// intialise the checksum accumulators // intialise the checksum accumulators
_sumA = _sumB = 0; _sumA = _sumB = 0;
@ -163,7 +163,7 @@ BinComm::_decode(uint8_t inByte)
// receiving payload data // receiving payload data
// //
case DEC_WAIT_PACKET: case DEC_WAIT_MESSAGE:
// do checksum accumulation // do checksum accumulation
_sumA += inByte; _sumA += inByte;
_sumB += _sumA; _sumB += _sumA;
@ -181,7 +181,7 @@ BinComm::_decode(uint8_t inByte)
// //
case DEC_WAIT_SUM_A: case DEC_WAIT_SUM_A:
if (inByte != _sumA) { if (inByte != _sumA) {
badPacketsReceived++; badMessagesReceived++;
_decodePhase = DEC_WAIT_P1; _decodePhase = DEC_WAIT_P1;
} else { } else {
_decodePhase++; _decodePhase++;
@ -189,8 +189,8 @@ BinComm::_decode(uint8_t inByte)
break; break;
case DEC_WAIT_SUM_B: case DEC_WAIT_SUM_B:
if (inByte == _sumB) { if (inByte == _sumB) {
// if we got this far, we have a packet // if we got this far, we have a message
packetsReceived++; messagesReceived++;
// call any handler interested in this message // call any handler interested in this message
for (tableIndex = 0; MSG_NULL != _handlerTable[tableIndex].messageID; tableIndex++) for (tableIndex = 0; MSG_NULL != _handlerTable[tableIndex].messageID; tableIndex++)
@ -198,7 +198,7 @@ BinComm::_decode(uint8_t inByte)
(_handlerTable[tableIndex].messageID == MSG_ANY)) (_handlerTable[tableIndex].messageID == MSG_ANY))
_handlerTable[tableIndex].handler(_handlerTable[tableIndex].arg, _messageID, _messageVersion, &_decodeBuf); _handlerTable[tableIndex].handler(_handlerTable[tableIndex].arg, _messageID, _messageVersion, &_decodeBuf);
} else { } else {
badPacketsReceived++; badMessagesReceived++;
} }
_decodePhase = DEC_WAIT_P1; _decodePhase = DEC_WAIT_P1;
break; break;

137
libraries/APM_BinComm/APM_BinComm.h

@ -30,10 +30,10 @@
#ifndef APM_BinComm_h #ifndef APM_BinComm_h
#define APM_BinComm_h #define APM_BinComm_h
#include <FastSerial.h>
#include "WProgram.h" #include "WProgram.h"
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <FastSerial.h>
/// ///
/// @class BinComm /// @class BinComm
@ -41,11 +41,71 @@
/// Mega binary telemetry protocol. /// Mega binary telemetry protocol.
/// ///
class BinComm { class BinComm {
private:
/// OTA message header
struct MessageHeader {
uint8_t length;
uint8_t messageID;
uint8_t messageVersion;
};
/// Incoming header/packet buffer
/// XXX we could make this smaller
union {
uint8_t bytes[0];
MessageHeader header;
uint8_t payload[256];
} _decodeBuf;
/// Outgoing header/packet buffer
/// XXX we could make this smaller
struct {
MessageHeader header;
uint8_t payload[256 - sizeof(MessageHeader)];
} _encodeBuf;
//////////////////////////////////////////////////////////////////////
/// @name Message pack/unpack utility functions
///
//@{
inline void _pack(uint8_t *&ptr, const uint8_t x) { *(uint8_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const uint16_t x) { *(uint16_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const int16_t x) { *(int16_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const uint32_t x) { *(uint32_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const int32_t x) { *(int32_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const char *msg, uint8_t size) { strlcpy((char *)ptr, msg, size); ptr += size; };
inline void _pack(uint8_t *&ptr, const uint8_t *values, uint8_t count) { memcpy(ptr, values, count); ptr += count; };
inline void _pack(uint8_t *&ptr, const uint16_t *values, uint8_t count) { memcpy(ptr, values, count * 2); ptr += count * 2; };
inline void _unpack(uint8_t *&ptr, uint8_t &x) { x = *(uint8_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, uint16_t &x) { x = *(uint16_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, int16_t &x) { x = *(int16_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, uint32_t &x) { x = *(uint32_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, int32_t &x) { x = *(int32_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, char *msg, uint8_t size) { strlcpy(msg, (char *)ptr, size); ptr += size; };
inline void _unpack(uint8_t *&ptr, uint8_t *values, uint8_t count) { memcpy(values, ptr, count); ptr += count; };
inline void _unpack(uint8_t *&ptr, uint16_t *values, uint8_t count) { memcpy(values, ptr, count * 2); ptr += count * 2; };
//@}
public: public:
//////////////////////////////////////////////////////////////////////
/// @name Protocol definition
///
/// The protocol definition, including structures describing messages,
/// MessageID values and helper functions for packing messages are
/// automatically generated.
//@{
#include "protocol/protocol.h"
//@}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/// @name Protocol magic numbers /// @name Protocol magic numbers
/// ///
/// @note The messageID enum is automatically generated and thus not described here. /// @note The MessageID enum is automatically generated and thus not described here.
/// ///
//@{ //@{
@ -166,9 +226,8 @@ public:
private: private:
const MessageHandler *_handlerTable; ///< callout table
/// Serial port we send/receive using. FastSerial *_interface; ///< Serial port we send/receive using.
FastSerial *_interface;
/// Various magic numbers /// Various magic numbers
enum MagicNumbers { enum MagicNumbers {
@ -177,9 +236,9 @@ private:
MSG_VERSION_1 = 1, MSG_VERSION_1 = 1,
MSG_VARIABLE_LENGTH = 0xff MSG_VARIABLE_LENGTH = 0xff
}; };
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/// @name Decoder /// @name Decoder state
//@{ //@{
uint8_t _decodePhase; ///< decoder state machine phase uint8_t _decodePhase; ///< decoder state machine phase
uint8_t _bytesIn; ///< bytes received in the current phase uint8_t _bytesIn; ///< bytes received in the current phase
@ -191,75 +250,17 @@ private:
uint8_t _messageVersion;///< messageVersion from the packet being received uint8_t _messageVersion;///< messageVersion from the packet being received
unsigned long _lastReceived; ///< timestamp of last byte reception unsigned long _lastReceived; ///< timestamp of last byte reception
//@}
/// Incoming header/packet buffer /// Decoder state machine.
/// XXX we could make this smaller
union {
uint8_t bytes[0];
msg_packet_header header;
uint8_t payload[256];
} _decodeBuf;
const BCMessageHandler *_handlerTable; ///< callout table
/// Decoder state machine
/// ///
/// @param inByte The byte to process. /// @param inByte The byte to process.
/// ///
void _decode(uint8_t inByte); void _decode(uint8_t inByte);
//@} /// Send the packet in the encode buffer.
//////////////////////////////////////////////////////////////////////
/// @name Encoder
//@{
/// Send the packet in the encode buffer
/// ///
void _sendMessage(void); void _sendMessage(void);
/// Outgoing header/packet buffer
/// XXX we could make this smaller
struct {
msg_packet_header header;
uint8_t payload[256 - sizeof(msg_packet_header)];
} _encodeBuf;
//////////////////////////////////////////////////////////////////////
/// @name Message pack/unpack utility functions
///
//@{
inline void _pack(uint8_t *&ptr, const uint8_t x) { (uint8_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const uint16 x) { (uint16_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const int16 x) { (int16_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const uint32_t x) { (uint32_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const int32_t x) { (int32_t *)ptr = x; ptr += sizeof(x); };
inline void _pack(uint8_t *&ptr, const char *msg, uint8_t size) { strlcpy(ptr, msg, size); ptr += size; };
inline void _pack(uint8_t *&ptr, const uint16_t *values, uint8_t count) { memcpy(ptr, values, count * 2); ptr += count * 2; };
inline void _unpack(uint8_t *&ptr, uint8_t &x) { x = *(uint8_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, uint16_t &x) { x = *(uint16_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, int16_t &x) { x = *(int16_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, uint32_t &x) { x = *(uint32_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, int32_t &x) { x = *(int32_t *)ptr; ptr += sizeof(x); };
inline void _unpack(uint8_t *&ptr, char *msg, uint8_t size) { strlcpy(msg, ptr, size); ptr += size; };
inline void _unpack(uint8_t *&ptr, uint16_t *values, uint8_t count) { memcpy(values, ptr, count * 2); ptr += count * 2; };
//@}
public:
//////////////////////////////////////////////////////////////////////
/// @name Protocol definition
///
/// The protocol definition, including structures describing messages,
/// messageID values and helper functions for packing messages are
/// automatically generated.
//@{
#include "protocol/protocol.h"
//@}
}; };
#endif // BinComm_h #endif // BinComm_h

546
libraries/APM_BinComm/protocol/protocol.h

File diff suppressed because it is too large Load Diff

20
libraries/APM_BinComm/protocol/protogen.awk

@ -16,14 +16,14 @@ END {
EMIT_MESSAGE() EMIT_MESSAGE()
# #
# emit the messageID enum # emit the MessageID enum
# #
# XXX it would be elegant to sort the array here, but not # XXX it would be elegant to sort the array here, but not
# everyone has GNU awk. # everyone has GNU awk.
# #
printf("\n//////////////////////////////////////////////////////////////////////\n") printf("\n//////////////////////////////////////////////////////////////////////\n")
printf("/// Message ID values\n") printf("/// Message ID values\n")
printf("enum messageID {\n") printf("enum MessageID {\n")
for (opcode in opcodes) { for (opcode in opcodes) {
printf("\t%s = 0x%x,\n", opcodes[opcode], opcode) printf("\t%s = 0x%x,\n", opcodes[opcode], opcode)
} }
@ -60,7 +60,7 @@ function EMIT_MESSAGE(payloadSize)
# emit a routine to pack the message payload from a set of variables and send it # emit a routine to pack the message payload from a set of variables and send it
# #
printf("/// Send a %s message\n", currentMessage) printf("/// Send a %s message\n", currentMessage)
printf("inline void\nBinComm::send_%s(\n", tolower(currentMessage)) printf("inline void\nsend_%s(\n", tolower(currentMessage))
for (i = 0; i < fieldCount; i++) { for (i = 0; i < fieldCount; i++) {
if (counts[i]) { if (counts[i]) {
printf("\tconst %s (&%s)[%d]", types[i], names[i], counts[i]) printf("\tconst %s (&%s)[%d]", types[i], names[i], counts[i])
@ -71,14 +71,14 @@ function EMIT_MESSAGE(payloadSize)
printf(",\n"); printf(",\n");
} }
printf(")\n{\n") printf(")\n{\n")
printf("\tuint8_t *p = &_encodeBuf.payload;\n") printf("\tuint8_t *__p = &_encodeBuf.payload[0];\n")
payloadSize = 0; payloadSize = 0;
for (i = 0; i < fieldCount; i++) { for (i = 0; i < fieldCount; i++) {
if (counts[i]) { if (counts[i]) {
printf("\t_pack(p, %s, %s);\n", names[i], counts[i]) printf("\t_pack(__p, %s, %s);\n", names[i], counts[i])
payloadSize += sizes[i] * counts[i] payloadSize += sizes[i] * counts[i]
} else { } else {
printf("\t_pack(p, %s);\n", names[i]) printf("\t_pack(__p, %s);\n", names[i])
payloadSize += sizes[i] payloadSize += sizes[i]
} }
} }
@ -92,7 +92,7 @@ function EMIT_MESSAGE(payloadSize)
# emit a routine to unpack the current message into a set of variables # emit a routine to unpack the current message into a set of variables
# #
printf("/// Unpack a %s message\n", currentMessage) printf("/// Unpack a %s message\n", currentMessage)
printf("inline void\nBinComm::unpack_%s(\n", tolower(currentMessage)) printf("inline void\nunpack_%s(\n", tolower(currentMessage))
for (i = 0; i < fieldCount; i++) { for (i = 0; i < fieldCount; i++) {
if (counts[i]) { if (counts[i]) {
printf("\t%s (&%s)[%d]", types[i], names[i], counts[i]) printf("\t%s (&%s)[%d]", types[i], names[i], counts[i])
@ -103,13 +103,13 @@ function EMIT_MESSAGE(payloadSize)
printf(",\n"); printf(",\n");
} }
printf(")\n{\n") printf(")\n{\n")
printf("\tuint8_t *p = &_decodeBuf.payload;\n") printf("\tuint8_t *__p = &_decodeBuf.payload[0];\n")
for (i = 0; i < fieldCount; i++) { for (i = 0; i < fieldCount; i++) {
if (counts[i]) { if (counts[i]) {
printf("\t_unpack(p, %s, %s);\n", names[i], counts[i]) printf("\t_unpack(__p, %s, %s);\n", names[i], counts[i])
payloadSize += sizes[i] * counts[i] payloadSize += sizes[i] * counts[i]
} else { } else {
printf("\t_unpack(p, %s);\n", names[i]) printf("\t_unpack(__p, %s);\n", names[i])
payloadSize += sizes[i] payloadSize += sizes[i]
} }
} }

Loading…
Cancel
Save