-
Notifications
You must be signed in to change notification settings - Fork 34
/
versionStuff.ino
98 lines (95 loc) · 2.94 KB
/
versionStuff.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// attempt to get version info from .hex file
// modified hex file parser from OTGWSerial.cpp
//
// -tjfs
#define byteswap(val) ((val << 8) | (val >> 8))
static const char banner[] = "OpenTherm Gateway ";
String GetVersion(const String hexfile){
char hexbuf[48]={0};
int len, addr, tag, data, offs, linecnt = 0;
char datamem[256]={0}; // prevent buffer overrun
unsigned short ptr;
File f;
DebugTf(PSTR("GetVersion opening %s\r\n"),hexfile.c_str());
f = LittleFS.open(hexfile, "r");
if (f) // only proceed if file exists
{
while (f.readBytesUntil('\n', hexbuf, sizeof(hexbuf)) != 0) {
linecnt++;
// DebugTf(PSTR("reading line %d %s\n"),linecnt,hexbuf);
if (sscanf(hexbuf, ":%2x%4x%2x", &len, &addr, &tag) != 3)
{
DebugTf(PSTR("Parse error on line %d\n"),linecnt);// Parse error
break;
}
// DebugTf(PSTR("Read in %2x %4x %2x\n"),len,addr,tag);
if (len & 1)
{
DebugTf(PSTR("Invalid data size on line %d\n"),linecnt);// Invalid data size
break;
}
offs = 9;
len >>= 1;
if (tag == 0)
{
// DebugTf(PSTR("Checking address %4x on line %d\r\n"),addr,linecnt);// Invalid data size
if (addr >= 0x4200 && addr <= 0x4300)
{
// Data memory 16f88
addr = (addr - 0x4200) >> 1;
while (len > 0)
{
if (sscanf(hexbuf + offs, "%04x", &data) != 1)
break;
// if (!bitRead(datamap, addr / 64)) weight += WEIGHT_DATAPROG;
// bitSet(datamap, addr / 64);
//DebugTf(PSTR("storing %x in %x\r\n"),data,addr);
datamem[addr++] = byteswap(data);
offs += 4;
len--;
}
} else if (addr >= 0xe000 && addr <= 0xe100)
{
// Data memory 16f1847
addr = (addr - 0xe000) >> 1;
while (len > 0)
{
if (sscanf(hexbuf + offs, "%04x", &data) != 1)
break;
// if (!bitRead(datamap, addr / 64)) weight += WEIGHT_DATAPROG;
// bitSet(datamap, addr / 64);
//DebugTf(PSTR("storing %x in %x\n"),data,addr);
datamem[addr++] = byteswap(data);
offs += 4;
len--;
}
}
//if (len) {
//DebugTf("len>0\n");
//break;
//}
} else if (tag == 1) {
//DebugTf("tag==1\n");
break;
}
}
f.close();
//DebugTf("closing file and hunting for banner\n");
ptr = 0;
while (ptr < 256)
{
//DebugTf(PSTR("checking for %s at char pos %d\r\n"),banner,ptr);
char *s = strstr((char *)datamem + ptr, banner);
if (!s)
{
//DebugTf("did not find the banner\r\n");
ptr += strnlen((char *)datamem + ptr, 256 - ptr) + 1;
} else {
//DebugTf(PSTR("hit the banner! returning version string %s\r\n"),s);
s += sizeof(banner) - 1;
return String(s);
}
}
}
return ("");
}