aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-06 14:06:22 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2014-05-20 21:39:36 +0200
commit6efd2aec302c119f7e88b677d0d1bedf99e8cad9 (patch)
treebab869320067a43da58e549b31b198d7dbee4087 /src
parent05da854addac962e20ff9f1e7bdad3cbacb66905 (diff)
downloaddabmux-6efd2aec302c119f7e88b677d0d1bedf99e8cad9.tar.gz
dabmux-6efd2aec302c119f7e88b677d0d1bedf99e8cad9.tar.bz2
dabmux-6efd2aec302c119f7e88b677d0d1bedf99e8cad9.zip
add AFPacket generator
Diffstat (limited to 'src')
-rw-r--r--src/dabOutput/edi/AFPacket.cpp46
-rw-r--r--src/dabOutput/edi/AFPacket.h6
2 files changed, 44 insertions, 8 deletions
diff --git a/src/dabOutput/edi/AFPacket.cpp b/src/dabOutput/edi/AFPacket.cpp
index f690b92..cd845d7 100644
--- a/src/dabOutput/edi/AFPacket.cpp
+++ b/src/dabOutput/edi/AFPacket.cpp
@@ -24,22 +24,56 @@
along with CRC-DabMux. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
+#include "crc.h"
#include "AFPacket.h"
#include <vector>
#include <string>
#include <stdint.h>
+#include <arpa/inet.h>
-std::vector<uint8_t> AFPacket::Assemble()
+// Header PT field. AF packet contains TAG payload
+#define AFHEADER_PT_TAG 'T'
+
+// AF Packet Major (3 bits) and Minor (4 bits) version
+#define AFHEADER_VERSION 0x8 // MAJ=1, MIN=0
+
+std::vector<uint8_t> AFPacket::Assemble(char protocol_type, std::vector<uint8_t> payload)
{
- header.sync = 'A' << 8 | 'F';
- header.seq = 0;
- header.ar_cf = 0;
header.ar_maj = 1;
header.ar_min = 0;
- header.pt = 'T';
+ header.pt = protocol_type;
- std::string pack_data("Nothingyetpleasecomelater");
+ std::string pack_data("AF"); // SYNC
std::vector<uint8_t> packet(pack_data.begin(), pack_data.end());
+
+ uint32_t taglength = payload.size();
+
+ // write length into packet
+ packet[2] = (taglength >> 24) & 0xFF;
+ packet[3] = (taglength >> 16) & 0xFF;
+ packet[4] = (taglength >> 8) & 0xFF;
+ packet[5] = taglength & 0xFF;
+
+ // fill rest of header
+ packet.push_back(seq >> 8);
+ packet.push_back(seq & 0xFF);
+ packet.push_back((have_crc ? 0x80 : 0) | AFHEADER_VERSION); // ar_cf: CRC=1
+ packet.push_back(AFHEADER_PT_TAG);
+
+ // insert payload
+ packet.insert(packet.end(), payload.begin(), payload.end());
+
+ // calculate CRC over AF Header and payload
+ uint16_t crc = 0xffff;
+ crc = crc16(crc, &(packet.back()), packet.size());
+ crc ^= 0xffff;
+ crc = htons(crc);
+
+ packet.push_back((crc >> 24) & 0xFF);
+ packet.push_back((crc >> 16) & 0xFF);
+ packet.push_back((crc >> 8) & 0xFF);
+ packet.push_back(crc & 0xFF);
+
return packet;
}
diff --git a/src/dabOutput/edi/AFPacket.h b/src/dabOutput/edi/AFPacket.h
index c4f33bf..1fd0d16 100644
--- a/src/dabOutput/edi/AFPacket.h
+++ b/src/dabOutput/edi/AFPacket.h
@@ -49,11 +49,13 @@ class AFPacket
public:
AFPacket();
- std::vector<uint8_t> Assemble();
+ std::vector<uint8_t> Assemble(char protocol_type, std::vector<uint8_t> payload);
private:
+ static const bool have_crc = true;
+
AFHeader header;
- std::vector<uint8_t> packet_;
+ uint16_t seq; //counter that overflows at 0xFFFF
};
#endif