Even more v2.2

This commit is contained in:
Gunnar Skjold
2022-12-02 20:38:56 +01:00
parent 0927cab8e2
commit b07ed075f4
38 changed files with 68 additions and 25 deletions

View File

@@ -0,0 +1,92 @@
#ifndef _COSEM_H
#define _COSEM_H
#include "lwip/def.h"
// Blue book, Table 2
enum CosemType {
CosemTypeNull = 0x00,
CosemTypeArray = 0x01,
CosemTypeStructure = 0x02,
CosemTypeOctetString = 0x09,
CosemTypeString = 0x0A,
CosemTypeDLongSigned = 0x05,
CosemTypeDLongUnsigned = 0x06,
CosemTypeLongSigned = 0x10,
CosemTypeLongUnsigned = 0x12,
CosemTypeLong64Signed = 0x14,
CosemTypeLong64Unsigned = 0x15,
CosemTypeDateTime = 0x19
};
struct CosemBasic {
uint8_t type;
uint8_t length;
} __attribute__((packed));
struct CosemString {
uint8_t type;
uint8_t length;
uint8_t data[];
} __attribute__((packed));
struct CosemLongSigned {
uint8_t type;
int16_t data;
} __attribute__((packed));
struct CosemLongUnsigned {
uint8_t type;
uint16_t data;
} __attribute__((packed));
struct CosemDLongSigned {
uint8_t type;
int32_t data;
} __attribute__((packed));
struct CosemDLongUnsigned {
uint8_t type;
uint32_t data;
} __attribute__((packed));
struct CosemLong64Signed {
uint8_t type;
int64_t data;
} __attribute__((packed));
struct CosemLong64Unsigned {
uint8_t type;
uint64_t data;
} __attribute__((packed));
struct CosemDateTime {
uint8_t type;
uint16_t year;
uint8_t month;
uint8_t dayOfMonth;
uint8_t dayOfWeek;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint8_t hundredths;
int16_t deviation;
uint8_t status;
} __attribute__((packed));
typedef union {
struct CosemBasic base;
struct CosemString str;
struct CosemString oct;
struct CosemLongSigned ls;
struct CosemLongUnsigned lu;
struct CosemDLongSigned dls;
struct CosemDLongUnsigned dlu;
struct CosemLong64Signed l64s;
struct CosemLong64Unsigned l64u;
struct CosemDateTime dt;
} CosemData;
time_t decodeCosemDateTime(CosemDateTime timestamp);
#endif

View File

@@ -0,0 +1,31 @@
#ifndef _DATAPASERSER_H
#define _DATAPASERSER_H
#define DATA_TAG_NONE 0x00
#define DATA_TAG_AUTO 0x01
#define DATA_TAG_HDLC 0x7E
#define DATA_TAG_LLC 0xE6
#define DATA_TAG_DLMS 0x0F
#define DATA_TAG_DSMR 0x2F
#define DATA_TAG_MBUS 0x68
#define DATA_TAG_GBT 0xE0
#define DATA_TAG_GCM 0xDB
#define DATA_PARSE_OK 0
#define DATA_PARSE_FAIL -1
#define DATA_PARSE_INCOMPLETE -2
#define DATA_PARSE_BOUNDRY_FLAG_MISSING -3
#define DATA_PARSE_HEADER_CHECKSUM_ERROR -4
#define DATA_PARSE_FOOTER_CHECKSUM_ERROR -5
#define DATA_PARSE_INTERMEDIATE_SEGMENT -6
#define DATA_PARSE_FINAL_SEGMENT -7
#define DATA_PARSE_UNKNOWN_DATA -9
struct DataParserContext {
uint8_t type;
uint16_t length;
time_t timestamp;
uint8_t system_title[8];
};
#endif

View File

@@ -0,0 +1,13 @@
#ifndef _DATAPASERSERS_H
#define _DATAPASERSERS_H
#include "HdlcParser.h"
#include "DlmsParser.h"
#include "DsmrParser.h"
#include "MbusParser.h"
#include "GbtParser.h"
#include "GcmParser.h"
#include "LlcParser.h"
#endif

View File

@@ -0,0 +1,12 @@
#ifndef _DLMSPARSER_H
#define _DLMSPARSER_H
#include "Arduino.h"
#include "DataParser.h"
class DLMSParser {
public:
int8_t parse(uint8_t *buf, DataParserContext &ctx);
};
#endif

View File

@@ -0,0 +1,13 @@
#ifndef _DSMRPARSER_H
#define _DSMRPARSER_H
#include "Arduino.h"
#include "DataParser.h"
class DSMRParser {
public:
int8_t parse(uint8_t *buf, DataParserContext &ctx, bool verified);
private:
};
#endif

View File

@@ -0,0 +1,26 @@
#ifndef _GBTPARSER_H
#define _GBTPARSER_H
#include "Arduino.h"
#include "DataParser.h"
#define GBT_TAG 0xE0
typedef struct GBTHeader {
uint8_t flag;
uint8_t control;
uint16_t sequence;
uint16_t sequenceAck;
uint8_t size;
} __attribute__((packed)) GBTHeader;
class GBTParser {
public:
int8_t parse(uint8_t *buf, DataParserContext &ctx);
private:
uint8_t lastSequenceNumber = 0;
uint16_t pos = 0;
uint8_t *buf = NULL;
};
#endif

View File

@@ -0,0 +1,27 @@
#ifndef _GCMPARSER_H
#define _GCMPARSER_H
#include "Arduino.h"
#include "DataParser.h"
#define GCM_TAG 0xDB
#define GCM_AUTH_FAILED -51
#define GCM_DECRYPT_FAILED -52
#define GCM_ENCRYPTION_KEY_FAILED -53
typedef struct GCMSizeDef {
uint8_t flag;
uint16_t format;
} __attribute__((packed)) GCMSizeDef;
class GCMParser {
public:
GCMParser(uint8_t *encryption_key, uint8_t *authentication_key);
int8_t parse(uint8_t *buf, DataParserContext &ctx);
private:
uint8_t encryption_key[16];
uint8_t authentication_key[16];
};
#endif

View File

@@ -0,0 +1,29 @@
#ifndef _HDLCPARSER_H
#define _HDLCPARSER_H
#include "Arduino.h"
#include "DataParser.h"
#define HDLC_FLAG 0x7E
typedef struct HDLCHeader {
uint8_t flag;
uint16_t format;
} __attribute__((packed)) HDLCHeader;
typedef struct HDLCFooter {
uint16_t fcs;
uint8_t flag;
} __attribute__((packed)) HDLCFooter;
typedef struct HDLC3CtrlHcs {
uint8_t control;
uint16_t hcs;
} __attribute__((packed)) HDLC3CtrlHcs;
class HDLCParser {
public:
int8_t parse(uint8_t *buf, DataParserContext &ctx);
};
#endif

View File

@@ -0,0 +1,18 @@
#ifndef _LLCPARSER_H
#define _LLCPARSER_H
#include "Arduino.h"
#include "DataParser.h"
typedef struct LLCHeader {
uint8_t dst;
uint8_t src;
uint8_t control;
} __attribute__((packed)) LLCHeader;
class LLCParser {
public:
int8_t parse(uint8_t *buf, DataParserContext &ctx);
};
#endif

View File

@@ -0,0 +1,34 @@
#ifndef _MBUSPARSER_H
#define _MBUSPARSER_H
#include "Arduino.h"
#include "DataParser.h"
#define MBUS_START 0x68
#define MBUS_END 0x16
#define MBUS_FRAME_LENGTH_NOT_EQUAL -41
typedef struct MbusHeader {
uint8_t flag1;
uint8_t len1;
uint8_t len2;
uint8_t flag2;
} __attribute__((packed)) MbusHeader;
typedef struct MbusFooter {
uint8_t fcs;
uint8_t flag;
} __attribute__((packed)) MbusFooter;
class MBUSParser {
public:
int8_t parse(uint8_t *buf, DataParserContext &ctx);
uint16_t write(const uint8_t* d, DataParserContext &ctx);
private:
uint8_t lastSequenceNumber = 0;
uint16_t pos = 0;
uint8_t *buf = NULL;
uint8_t checksum(const uint8_t* p, int len);
};
#endif

View File

@@ -0,0 +1,9 @@
#ifndef _CRC_H
#define _CRC_H
#include "Arduino.h"
#include <stdint.h>
uint16_t crc16_x25(const uint8_t* p, int len);
#endif

View File

@@ -0,0 +1,8 @@
#ifndef _NTOHLL_H
#define _NTOHLL_H
#include "lwip/def.h"
uint64_t ntohll(uint64_t x);
#endif