Legacy_EgtsEncoderDecoder_F.../Src/egts_common.c

117 lines
3.1 KiB
C

/*
* egts.c
*
* Created on: Jun 4, 2021
* Author: zemon
*/
#include "egts.h"
#include "egts_crc.h"
#include "string.h"
#define EGTS_TIMESTAMP_EPOCH_OFFSET 1262304000 // epoch timestamp of Fri, 01 Jan 2010 00:00:00 GMT
uint32_t toEgtsTimestamp(uint32_t ts) {
return ts - EGTS_TIMESTAMP_EPOCH_OFFSET;
}
uint16_t vEgtsPackSrv(
uint8_t *out,
uint8_t flags,
eEgtsServiceId sourceServiceId,
eEgtsServiceId recipientServiceId,
uint32_t epochTimestamp,
eEgtsSubRecordId subRecordType,
tEgtsServiceSubRecordGenerator subRecGen,
void *subRecArgs
) {
uint16_t subRecordLength = subRecGen(out + 14, subRecArgs);
*(uint16_t *) (out + 0) = subRecordLength + 3; //data length
*(uint16_t *) (out + 2) = 0x0001; //counter
////////////////////////////////////////////////////////////////////////////////////////////////////////
// | flags |
// Sender Rcver Group Prior hasTime hasEvntID hasObjId
// DEV SRV None value exist not not
// 1 0 0 10 1 0 0 => 10010100 => 0x94
out[4] = flags;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//time
*(uint32_t *) (out + 5) = toEgtsTimestamp(epochTimestamp);
//SourceServType
*(uint8_t *) (out + 9) = (uint8_t) sourceServiceId;
//RecipientServType
*(uint8_t *) (out + 10) = (uint8_t) recipientServiceId;
//SRT
*(uint8_t *) (out + 11) = subRecordType; //(uint8_t) SRT;
//SRL
*(uint16_t *) (out + 12) = subRecordLength;
return subRecordLength + 14;
}
uint16_t vEgtsPackTransport(
uint16_t cid,
uint16_t packetId,
uint8_t *out,
uint8_t srvFlags,
eEgtsPacketTypeId packetTypeId,
eEgtsServiceId sourceServiceId,
eEgtsServiceId recipientServiceId,
uint32_t timestamp,
eEgtsSubRecordId subRecordType,
tEgtsServiceSubRecordGenerator subRecGen,
void *subRecArgs
) {
uint8_t ptOffset = 0;
if (packetTypeId == EGTS_PT_RESPONSE) {
ptOffset = 3;
}
uint16_t srvLen = ptOffset + vEgtsPackSrv(
out + TRANSPORT_HEADER_LENGTH+ptOffset,
srvFlags,
sourceServiceId,
recipientServiceId,
timestamp,
subRecordType,
subRecGen,
subRecArgs
);
out[0] = 0x01;//version
out[1] = 0x00;//secure key
out[2] = 0x01;//flags
out[3] = 0x0B;//header length
out[4] = 0x00;//header encoding
*((uint16_t *) (out + 5)) = srvLen;
*((uint16_t *) (out + 7)) = packetId;//package Id
out[9] = packetTypeId;//Packet type
out[10] = CRC8EGTS(out, 10);//Header CRC
if (packetTypeId == EGTS_PT_RESPONSE) {
*((uint16_t *) (out + 11)) = cid;// response packet id
out[13] = 0b0000;//Packet type
}
*((uint16_t *) (out + TRANSPORT_HEADER_LENGTH + srvLen)) = CRC16EGTS(out + TRANSPORT_HEADER_LENGTH, srvLen);
return TRANSPORT_HEADER_LENGTH + srvLen + 2;
}