oddělení generatoru od readeru

This commit is contained in:
2024-01-29 14:50:12 +01:00
parent 36a799057c
commit a7fc519138
62 changed files with 1147 additions and 932 deletions

View File

@@ -23,8 +23,6 @@ const string cidFilePath = "";
class SDCard
{
struct SDCardData
{
uint8_t isCorrectLoad = 0; // SD karta je korektne nactena //bool vs byte ?

View File

@@ -1,9 +1,12 @@
#ifndef LICENCE_COMMON_H_
#define LICENCE_COMMON_H_
//---------------- společná hlavička pro všechny licence ----------------
#include <cstring>
#include <string>
#include <map>
#include "pugixml.hpp"
#include <stdint.h>
using namespace std;
@@ -15,6 +18,11 @@ const int cidSize = 32;
const int csdSize = 32;
const int generatorVersion = 1;
typedef uint8_t BYTE;
typedef uint16_t WORD;
typedef uint32_t DWORD;
typedef uint32_t DATE;
enum class GeneralError
{
GeneralError = 1,
@@ -27,7 +35,9 @@ enum class GeneralError
ELCMismatch = 8,
CRCMismatch = 9,
EncryptError = 10,
DecryptError = 11
DecryptError = 11,
ParamMissing = 12,
IvanlidParam = 13
};
enum class ELCType
@@ -91,7 +101,7 @@ struct LicenceIdentification
string licTypeName = "";
PlcType licPlcType;
uint8_t licenceVersion = 1; // verze licence, určitě kodování, pojmenování souborů
uint8_t licenceVersion = 1; // verze licence, určuje kodování, pojmenování souborů
uint8_t licenceIndex = 0;
uint8_t revision;
uint8_t licCompatibility = 1; // identikator hlavního ELC
@@ -100,19 +110,24 @@ struct LicenceIdentification
string cid_cds_path = "";
string licenceFilePath = "";
};
struct LicData
// struct LicData
// {
// string station = "";
// string distributor = "";
// string licenceName = "";
// string projectDescription = "";
// string cid = ""; // CID z SD karty
// string csd = ""; // CSD z SD karty
// string uid = ""; // jedinečný identifikátor z jiného systému
// pugi::xml_document *doc;
// };
struct LicenceItem
{
string station = "";
string distributor = "";
string licenceName = "";
string projectDescription = "";
string cid = ""; // CID z SD karty
string csd = ""; // CSD z SD karty
string uid = ""; // jedinečný identifikátor z jiného systému
pugi::xml_document *doc;
int protocolId = -1;
int dataPointsCount = 0;
};
struct LicenceItem11
@@ -133,29 +148,35 @@ struct LicenceItem31
int dataPointsCount = 0;
};
struct LicenceInfo
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
struct LicenceInfo11
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
struct LicenceInfo21
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
struct LicenceInfo21
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
struct LicenceInfo31
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
struct LicenceInfo31
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
struct LicenceInfoGeneral
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
struct LicenceInfoGeneral
{
int reqDataPointsCount = 0;
map<int, int> licences = {};
};
class LicenceException : public std::exception
{

View File

@@ -0,0 +1,107 @@
#ifndef PLC_LICENCE2_COMMON_H
#define PLC_LICENCE2_COMMON_H
#include <stdint.h>
#include <unordered_map>
#include <vector>
#include "licenceCommon.h"
#include "SDCard.h"
/// @brief společná třída pro pro reader i generator licence typu ELC2
class LicenceELC2
{
public:
LicenceELC2();
LicenceELC2(LicenceIdentification &licIdentification);
~LicenceELC2();
string getLicenceName();
string cid_cdsPath = "";
string licenceFilePath = "";
ErrorMessage errorMessage;
enum class Error
{
SDCardReadError = 50,
LicenceReadError = 51,
LicenceSizeMismatch = 52,
LicenceSizeCardMismatch = 53,
LicenceMismatch = 54,
ItemsCountMismatch = 61
};
unordered_map<Error, string> mapErrors = {
{Error::SDCardReadError, "Nepodařilo se načíst SD kartu."},
{Error::ItemsCountMismatch, "Nesouhlasí počet položek licence."},
{Error::LicenceSizeMismatch, "Nesouhlasí velikost souboru licence."},
{Error::LicenceSizeCardMismatch, "Nesouhlasí velikost SD karty."},
{Error::LicenceMismatch, "Nesouhlasí licence."},
{Error::ItemsCountMismatch, "Nesouhlasí počet položek licence."},
};
struct LicenceId
{
char licIdent[5] = {'E', 'L', 'C', '0', '_'};
};
// struct __attribute__((__packed__)) LicencePublicHeader
struct LicenceIdentDataHeader
{
BYTE licenceType = 0; // EOSEOV, DRT ...
BYTE licenceTypeVersion = 1; // verze licence, urcuje nuance sifrování a pojmenování souborů
BYTE licenceIndex = 0; // puvodní post fix, identifikátor pro více licencí
BYTE compatibilityVersion = 1; // udava verzi komplet PrivateContent
BYTE licItemCount = 0; // počet licenčních bodů
WORD publicHeaderLength = 0; // délka veřejné hlavičy
WORD cardSize = 0; // velikost SD karty
DWORD serialNumber = 0; // seriove cislo karty
};
///
struct licDataItem
{
WORD protoId = 0; // id protokolu pro ktery je licence
WORD licCount = 0; // pocet datovych bodu licence
char dummy[64] = {}; // dummy pro větší velikost licence v případě méně licenčních bodů
};
struct PublicHeader
{
string version = "";
string projectDescription = "";
string date = "";
string licenceType = "";
int crc = 0;
};
struct PrivateContent // privátní šifrovaná část
{
LicenceIdentDataHeader licenceIdentHeader;
vector<licDataItem> dataItems;
WORD crc = 0;
};
struct LicenceBody
{
LicenceId licId;
LicenceIdentDataHeader licenceIdentHeader;
string publicHeader = ""; // JSON
PrivateContent privateContent;
};
LicenceBody licBody;
SDCard sdCard;
LicenceIdentification lIdentification;
vector<unsigned char> cryptPrivateContent(const std::vector<unsigned char> &content);
vector<unsigned char> decryptPrivateContent(const std::vector<unsigned char> &content);
void initVector(BYTE (&iVector)[], BYTE (&key)[]);
private:
};
#endif

View File

@@ -27,7 +27,7 @@ struct DateAndTime {
void generatePause();
std::string right(const std::string& sourceString, size_t numChars);
void getCharsFromString(string source, char *charArray);
void getCharsFromString(const string source, char *charArray);
uint16_t calculateCRC16(const uint8_t* data, size_t length);
WORD getCurrentDateAsCODESYSDate();
DWORD getCurrentDateAsCODESYSDateDword();

View File

@@ -0,0 +1,9 @@
#ifndef ELC1_GENERATOR__H
#define ELC1_GENERATOR__H
namespace Generator
{
}
#endif

View File

@@ -0,0 +1,9 @@
#ifndef ELC2_GENERATOR__H
#define ELC2_GENERATOR__H
namespace Generator
{
}
#endif

View File

@@ -0,0 +1,9 @@
#ifndef ELC3_GENERATOR__H
#define ELC3_GENERATOR__H
namespace Generator
{
}
#endif

View File

@@ -11,16 +11,33 @@
#include <time.h>
#include <map>
#include <stdint.h>
#include "pugixml.hpp"
#include "licenceCommon.h"
#include "licenceELC11.h"
#include "licenceELC21.h"
#include "licenceELC31.h"
#include "licGenELC1.h"
#include "licGenELC2.h"
#include "licGenELC3.h"
using namespace std;
class LicenceGenerator
{
struct LicData
{
string station = "";
string distributor = "";
string licenceName = "";
string projectDescription = "";
string cid = ""; // CID z SD karty
string csd = ""; // CSD z SD karty
string uid = ""; // jedinečný identifikátor z jiného systému
pugi::xml_document *doc;
};
public:
int elcSwitchType;
string operationErrors = "";
@@ -34,17 +51,15 @@ public:
LicenceGenerator();
~LicenceGenerator();
LicenceGenerator(string uid, string cid, string csd, string dataFileName);
LicenceGenerator(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
void createLicenceFile();
void readLicenceFile();
int getDataPointsCount(int protocolId);
bool getLicenceInfo(int protocolId, void *returnStructure);
bool getLicenceItemInfo(int protocolId, void *returnItemStructure);
LicenceInfoGeneral licenceInfo;
private:
LicenceIdentification licIdentification;
LicData licData;
@@ -58,9 +73,6 @@ private:
void createLicenceELC2();
void createLicenceELC3();
void readLicenceELC1();
void readLicenceELC2();
void readLicenceELC3();
};
#endif

View File

@@ -5,6 +5,7 @@
#include <stdint.h>
#include <unordered_map>
#include "licenceCommon.h"
#include "pugixml.hpp"
#include "SDCard.h"
using namespace std;
@@ -12,22 +13,33 @@ using namespace std;
class LicenceELC21
{
struct LicData
{
string station = "";
string distributor = "";
string licenceName = "";
string projectDescription = "";
string cid = ""; // CID z SD karty
string csd = ""; // CSD z SD karty
string uid = ""; // jedinečný identifikátor z jiného systému
pugi::xml_document *doc;
};
public:
ErrorMessage errorMessage;
LicenceInfo21 licenceInfo;
string cid_cdsPath = ""; //"c:\\_projects\\LicenceGenerator\\output\\"; ///sys/block/mmcblk0/device/
string cid_cdsPath = ""; //"c:\\_projects\\LicenceGenerator\\output\\"; ///sys/block/mmcblk0/device/
string licenceFilePath = ""; //"c:\\_projects\\LicenceGenerator\\output\\";
LicenceELC21();
~LicenceELC21();
LicenceELC21(LicenceIdentification & licIdentification, LicData & icData);
LicenceELC21(LicenceIdentification & licIdentification);
LicenceELC21(LicData & icData);
LicenceELC21(LicenceIdentification &licIdentification, LicData &licData);
LicenceELC21(LicenceIdentification &licIdentification);
LicenceELC21(LicData &licData);
bool createLicence();
bool readLicence(LicenceInfoGeneral * licences);
bool readLicence(LicenceInfoGeneral *licences);
int getDataPointsCount(int protocolId);
bool getLicenceInfo(void * ptr);
bool getLicenceInfo(void *ptr);
bool getLicenceItemInfo(int protocolId, void *returnItemStructure);
enum class Error21
@@ -48,31 +60,31 @@ public:
{Error21::LicenceMismatch, "Nesouhlasí licence."},
{Error21::ItemsCountMismatch, "Nesouhlasí počet položek licence."},
};
struct LicenceId
{
char licIdent[5] = { 'E', 'L', 'C', '0','_'};
char licIdent[5] = {'E', 'L', 'C', '0', '_'};
};
//struct __attribute__((__packed__)) LicencePublicHeader
// struct __attribute__((__packed__)) LicencePublicHeader
struct LicenceIdentDataHeader
{
BYTE licenceType = 0; //EOSEOV, DRT ...
BYTE licenceTypeVersion = 1; //verze licence, urcuje nuance sifrování a pojmenování souborů
BYTE licenceIndex = 0; //puvodní post fix, identifikátor pro více licencí
BYTE compatibilityVersion = 1; //udava verzi komplet PrivateContent
BYTE licItemCount = 0; //počet licenčních bodů
WORD publicHeaderLength = 0; //délka veřejné hlavičy
WORD cardSize = 0; //velikost SD karty
DWORD serialNumber = 0;//seriove cislo karty
BYTE licenceType = 0; // EOSEOV, DRT ...
BYTE licenceTypeVersion = 1; // verze licence, urcuje nuance sifrování a pojmenování souborů
BYTE licenceIndex = 0; // puvodní post fix, identifikátor pro více licencí
BYTE compatibilityVersion = 1; // udava verzi komplet PrivateContent
BYTE licItemCount = 0; // počet licenčních bodů
WORD publicHeaderLength = 0; // délka veřejné hlavičy
WORD cardSize = 0; // velikost SD karty
DWORD serialNumber = 0; // seriove cislo karty
};
///
///
struct licDataItem
{
WORD protoId = 0; // id protokolu pro ktery je licence
WORD licCount = 0; // pocet datovych bodu licence
char dummy[64] = {};
WORD protoId = 0; // id protokolu pro ktery je licence
WORD licCount = 0; // pocet datovych bodu licence
char dummy[64] = {}; // dummy pro větší velikost licence v případě méně licenčních bodů
};
struct PublicHeader
@@ -84,7 +96,7 @@ public:
int crc = 0;
};
struct PrivateContent //privátní šifrovaná část
struct PrivateContent // privátní šifrovaná část
{
LicenceIdentDataHeader licenceIdentHeader;
vector<licDataItem> dataItems;
@@ -95,12 +107,11 @@ public:
{
LicenceId licId;
LicenceIdentDataHeader licenceIdentHeader;
string publicHeader = ""; //JSON
PrivateContent privateContent;
string publicHeader = ""; // JSON
PrivateContent privateContent;
};
private:
private:
int licItemCount = 0;
LicenceBody licBody;
LicenceIdentification lIdentification;
@@ -110,11 +121,10 @@ public:
string getVersion(int middleVersion);
void getLicenceItems();
bool processInputConfiguration();
vector<unsigned char> cryptPrivateContent(const std::vector<unsigned char>& content);
vector<unsigned char> decryptPrivateContent(const std::vector<unsigned char>& content);
vector<unsigned char> cryptPrivateContent(const std::vector<unsigned char> &content);
vector<unsigned char> decryptPrivateContent(const std::vector<unsigned char> &content);
void initVector(BYTE (&iVector)[], BYTE (&key)[]);
string getLicenceName();
};
#endif

View File

@@ -6,11 +6,24 @@
#include <vector>
#include "utils.h"
#include "licenceCommon.h"
#include "pugixml.hpp"
using namespace std;
class LicenceELC31
{
struct LicData
{
string station = "";
string distributor = "";
string licenceName = "";
string projectDescription = "";
string cid = ""; // CID z SD karty
string csd = ""; // CSD z SD karty
string uid = ""; // jedinečný identifikátor z jiného systému
pugi::xml_document *doc;
};
LicData lData;
bool processInputConfiguration();
LicenceIdentification lIdentification;

View File

@@ -0,0 +1,9 @@
#ifndef ELC1_READER__H
#define ELC1_READER__H
namespace Reader
{
}
#endif

View File

@@ -0,0 +1,34 @@
#ifndef ELC2_READER__H
#define ELC2_READER__H
#include "utils.h"
#include "licenceCommon.h"
#include "licenceELC2.h"
#include "SDCard.h"
using namespace std;
namespace Reader
{
class Licence2 : public LicenceELC2
{
public:
LicenceInfo21 licenceInfo;
Licence2();
~Licence2();
Licence2(LicenceIdentification &licIdentification);
bool readLicence(LicenceInfoGeneral *licences);
int getDataPointsCount(int protocolId);
bool getLicenceInfo(void *ptr);
bool getLicenceItemInfo(int protocolId, void *returnItemStructure);
private:
};
}
#endif

View File

@@ -0,0 +1,9 @@
#ifndef ELC3_READER__H
#define ELC3_READER__H
namespace Reader
{
}
#endif

View File

@@ -2,12 +2,16 @@
#define LICENCE_READER_H_
#include "utils.h"
// #include "licenceCommon.h"
#include "licenceELC11.h"
#include "licenceELC21.h"
#include "licenceELC31.h"
#include "licReaderELC1.h"
#include "licReaderELC2.h"
#include "licReaderELC3.h"
using namespace std;
using namespace Reader;
struct InitStructure
{
@@ -28,18 +32,13 @@ public:
bool argumentsCorrect = false;
ErrorMessage error;
LicenceELC11 *licence11;
LicenceELC21 *licence21;
LicenceELC31 *licence31;
Licence2 *licence2;
LicenceReader();
~LicenceReader();
bool init(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
bool init(int elcType, InitStructure &initStructure);
bool initread(int elcType, int licenceType, int licenceVersion, int licenceIndex, int compatibility);
bool initread(int elcType, InitStructure &initStructure);
void readLicenceFile();
bool getLicenceInfo(void *returnStructure);
bool getLicenceItemInfo(int protocolId, void *returnItemStructure);
@@ -47,14 +46,9 @@ public:
private:
LicenceIdentification licIdentification;
LicData licData;
string configFileName = "";
void *licence = nullptr;
void readLicenceELC1();
void readLicenceELC2();
void readLicenceELC3();
};
#endif