forked from horchi/linux-p4d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp4io.h
330 lines (244 loc) · 8.04 KB
/
p4io.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//***************************************************************************
// p4d / Linux - Heizungs Manager
// File p4io.h
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file LICENSE for details.
// Date 04.11.2010 - 19.11.2013 Jörg Wendel
//***************************************************************************
#ifndef _IO_P4_H_
#define _IO_P4_H_
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include "lib/serial.h"
#include "service.h"
//***************************************************************************
// Class P4 Packet
//***************************************************************************
class P4Packet : public FroelingService, public Serial
{
public:
// declarations
enum Error
{
wrnEndOfPacket = -100
};
// object
P4Packet();
int read();
int set(const char* data);
int evaluate();
std::vector<Parameter>* getParameters() { return ¶meters; }
Parameter* getParameter(int index)
{
std::vector<Parameter>::iterator it;
for (it = parameters.begin(); it != parameters.end(); it++)
if ((*it).index == index)
return &(*it);
return 0;
}
const char* all() { return buffer; }
protected:
// functions
int readPacket(const char*& p);
int getItem(Parameter* p);
int getToken(char* token);
// data
char buffer[sizeMaxPacket+TB];
const char* pBuf;
std::vector<Parameter> parameters;
char rbuffer[sizeMaxPacket+TB];
};
//***************************************************************************
// Request
//***************************************************************************
class P4Request : public FroelingService
{
public:
P4Request(Serial* aSerial) { s = aSerial; text = 0; clear(); }
virtual ~P4Request() { clear(); }
class RequestClean
{
public:
RequestClean(P4Request* aReq)
{
req = aReq;
}
~RequestClean()
{
int count = 0;
byte b;
while (req->readByte(b, yes, 10) == success)
count++;
if (count)
{
tell(eloAlways, "Got %d unexpected bytes", count);
req->show("<- ");
}
}
private:
P4Request* req;
};
int clear()
{
free(text);
text = 0;
sizeBufferContent = 0;
sizeDecodedContent = 0;
memset(&header, 0, sizeof(Header));
memset(buffer, 0, sizeof(buffer));
memset(decoded, 0, sizeof(buffer));
clearBytes();
clearAddresses();
return done;
}
void clearAddresses()
{
memset(addresses, 0, sizeof(addresses));
addressCount = 0;
}
int addAddress(word address)
{
if (addressCount >= maxAddresses)
return fail;
addresses[addressCount++] = htons(address);
return success;
}
void clearBytes()
{
memset(bytes, 0, sizeof(bytes));
byteCount = 0;
}
int addByte(byte b)
{
if (byteCount >= maxBytes)
return fail;
bytes[byteCount++] = b;
return success;
}
int addText(const char* t)
{
free(text);
text = strdup(t);
return success;
}
int request(byte command)
{
header.id = htons(commId);
header.command = command;
prepareRequest();
show("-> ");
if (!s || !s->isOpen())
return fail;
return s->write(buffer, sizeBufferContent);
}
void show(const char* prefix = "", int elo = eloDebug)
{
char tmp[1000];
*tmp = 0;
sprintf(tmp+strlen(tmp), "%s", prefix);
for (int i = 0; i < sizeBufferContent; i++)
sprintf(tmp+strlen(tmp), "%2.2X ", buffer[i]);
sprintf(tmp+strlen(tmp), " ");
for (int i = 0; i < sizeBufferContent; i++)
sprintf(tmp+strlen(tmp), "%c", isprint(buffer[i]) ? buffer[i] : '.');
tell(elo, "%s", tmp);
}
void showDecoded(const char* prefix = "")
{
char tmp[1000];
*tmp = 0;
sprintf(tmp+strlen(tmp), "%s", prefix);
for (int i = 0; i < sizeDecodedContent; i++)
sprintf(tmp+strlen(tmp), "%2.2X ", decoded[i]);
sprintf(tmp+strlen(tmp), " ");
for (int i = 0; i < sizeDecodedContent; i++)
sprintf(tmp+strlen(tmp), "%c", isprint(decoded[i]) ? decoded[i] : '.');
tell(eloDebug2, "%s", tmp);
}
Header* getHeader() { return &header; }
int readHeader(int tms = 2000)
{
int status;
clear();
if (!s || !s->isOpen())
{
tell(eloAlways, "Line not open, aborting read");
return fail;
}
if ((status = readWord(header.id, no, tms)) != success)
{
tell(eloAlways, "Read word failed, aborting");
return status;
}
if (header.id != commId)
{
tell(eloAlways, "Got wrong communication id %4.4x "
"expected %4.4x", header.id, commId);
return fail;
}
if ((status = readWord(header.size, yes, tms)) != success)
{
tell(eloAlways, "Read size failed, status was %d", status);
return status;
}
if ((status = readByte(header.command, yes, tms)) != success)
{
tell(eloAlways, "Read command failed, status was %d", status);
return status;
}
return success;
}
// interface
int getStatus(Status* s);
int syncTime(int offset = 0);
int getParameter(ConfigParameter* p);
int setParameter(ConfigParameter* p);
int getFirstTimeRanges(TimeRanges* t) { return getTimeRanges(t, yes); }
int getNextTimeRanges(TimeRanges* t) { return getTimeRanges(t, no); }
int setTimeRanges(TimeRanges* t);
int getValue(Value* v);
int getDigitalOut(IoValue* v);
int getDigitalIn(IoValue* v);
int getAnalogOut(IoValue* v);
int getFirstError(ErrorInfo* e) { return getError(e, yes); }
int getNextError(ErrorInfo* e) { return getError(e, no); }
int getFirstValueSpec(ValueSpec* v) { return getValueSpec(v, yes); }
int getNextValueSpec(ValueSpec* v) { return getValueSpec(v, no); }
int getFirstMenuItem(MenuItem* m) { return getMenuItem(m, yes); }
int getNextMenuItem(MenuItem* m) { return getMenuItem(m, no); }
int getUser(byte cmd);
int getItem(int first);
int check();
protected:
int prepareRequest();
int getError(ErrorInfo* e, int first);
int getValueSpec(ValueSpec* v, int first);
int getMenuItem(MenuItem* m, int first);
int getTimeRanges(TimeRanges* t, int first);
int readByte(byte& v, int decode = yes, int tms = 1000);
int readWord(word& v, int decode = yes, int tms = 1000);
int readWord(sword& v, int decode = yes, int tms = 1000);
int readTime(time_t& t); // 3 byte
int readDate(time_t& t); // 3 byte
int readDateExt(time_t& t); // 4 byte
int readTimeDate(time_t& t); // 6 byte
int readTimeDateExt(time_t& t); // 7 byte
int readText(char*& s, int size);
// data
Header header;
char* text;
word addresses[maxAddresses];
int addressCount;
byte bytes[maxBytes];
int byteCount;
byte buffer[sizeMaxRequest*2+TB];
int sizeBufferContent;
byte decoded[sizeMaxRequest*2+TB]; // for debug
int sizeDecodedContent;
Serial* s;
};
//***************************************************************************
#endif // _IO_P4_H_