Auto-detect ascii in model-version vif.

pull/540/head
Fredrik Öhrström 2022-05-07 19:40:46 +02:00
rodzic 19ec79fc41
commit ae75bf96c4
5 zmienionych plików z 74 dodań i 6 usunięć

Wyświetl plik

@ -417,10 +417,11 @@ bool parseDV(Telegram *t,
DVEntry *dve = &(*dv_entries)[key].second;
/*
if (isDebugEnabled())
{
debug("(dvparser) entry %s\n", dve->str().c_str());
}
}*/
assert(key == dve->dif_vif_key.str());
@ -949,10 +950,17 @@ bool DVEntry::extractReadableString(string *out)
t == 0x7 || // 64 Bit Integer/Binary
t == 0xD) // Variable length
{
// For example an enhanced id 32 bits binary looks like:
// 44434241 and will be reversed to: 41424344 and translated using ascii
// to ABCD
v = reverseBinaryAsciiSafeToString(v);
if (isLikelyAscii(v))
{
// For example an enhanced id 32 bits binary looks like:
// 44434241 and will be reversed to: 41424344 and translated using ascii
// to ABCD
v = reverseBinaryAsciiSafeToString(v);
}
else
{
v = reverseBCD(v);
}
}
if (t == 0x9 || // 2 digit BCD
t == 0xA || // 4 digit BCD

Wyświetl plik

@ -48,6 +48,7 @@ void test_hex();
void test_translate();
void test_slip();
void test_dvs();
void test_ascii_detection();
int main(int argc, char **argv)
{
@ -82,6 +83,7 @@ int main(int argc, char **argv)
test_translate();
test_slip();
test_dvs();
test_ascii_detection();
return 0;
}
@ -1254,3 +1256,27 @@ void test_dvs()
printf("ERROR test_dvs 1\n");
}
}
void test_ascii_detection()
{
string s = "000008";
if (isLikelyAscii(s))
{
printf("ERROR >%s< should not be likely ascii\n", s.c_str());
}
s = "41424344";
if (!isLikelyAscii(s))
{
printf("ERROR >%s< should be likely ascii\n", s.c_str());
}
s = "000041424344";
if (!isLikelyAscii(s))
{
printf("ERROR >%s< should be likely ascii\n", s.c_str());
}
s = "000041194300";
if (isLikelyAscii(s))
{
printf("ERROR >%s< should not be likely ascii\n", s.c_str());
}
}

Wyświetl plik

@ -2192,3 +2192,35 @@ void removeSlipFraming(vector<uchar>& from, size_t *frame_length, vector<uchar>
}
}
// Check if hex string is likely to be ascii
bool isLikelyAscii(std::string v)
{
vector<uchar> val;
bool ok = hex2bin(v, &val);
// For example 64 bits:
// 0000 0000 4142 4344
// is probably the string DCBA
if (!ok) return false;
size_t i = 0;
for (; i < val.size(); ++i)
{
if (val[i] != 0) break;
}
if (i == val.size())
{
// Value is all zeroes, this is probably a number.
return false;
}
for (; i < val.size(); ++i)
{
if (val[i] < 20 || val[i] > 126) return false;
}
return true;
}

Wyświetl plik

@ -43,6 +43,8 @@ uchar reverse(uchar c);
std::string reverseBCD(std::string v);
// A hex string encoding ascii chars is reversed and safely translated into a readble string.
std::string reverseBinaryAsciiSafeToString(std::string v);
// Check if hex string is likely to be ascii
bool isLikelyAscii(std::string v);
bool isHexChar(uchar c);

Wyświetl plik

@ -142,7 +142,7 @@ Using driver : multical21(driver should be upgraded) 00/00
019 : 78 tpl-ci-field (EN 13757-3 Application Layer (no tplh))
020 : 02 dif (16 Bit Integer/Binary Instantaneous value)
021 : FF vif (Vendor extension)
022 : 20 vife (per second)
022 : 20 combinable vif (PerSecond)
023 C!: 7100 info codes (DRY(dry 22-31 days))
025 : 04 dif (32 Bit Integer/Binary Instantaneous value)
026 : 13 vif (Volume l)