kaytat / stoker_socket_test

Stoker Socket Test

This URL has Read+Write access

stoker_socket_test / stoker_command.h
100644 396 lines (324 sloc) 12.46 kb
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
Stoker command object.
 
Each command object has a reference to a response. The user
of the command is expected to allocate the response object
and pass it the command object.
*/
#ifndef _STOKER_COMMAND_H_
#define _STOKER_COMMAND_H_
 
#include "stoker_response.h"
 
using namespace std;
 
class Stoker_Command {
public:
    Stoker_Command(Stoker_Cmd_Id cmd_id, Stoker_Response& rsp) : cmd_id(cmd_id), payload_sz(1), checksum(0), sig(STOKER_CMD_SIG), rsp(rsp) {
        ser_num = rand();
        this->rsp.set_ser_num(ser_num);
    }
 
    virtual ~Stoker_Command() {
    }
 
    static unsigned int header_size() {
        return STOKER_CMD_HEADER_SIZE;
    }
 
    virtual unsigned int size() {
        return payload_sz + header_size();
    }
 
    virtual void set_bytes(unsigned char* bytes, unsigned int& offset) {
        set_bytes_header(bytes, offset);
        set_bytes_payload(bytes, offset);
    }
 
    virtual Stoker_Response& get_rsp() {
        return rsp;
    }
 
    friend ostream& operator<<(ostream& o, const Stoker_Command& cmd) {
        ios_base::fmtflags f = o.flags(ios::hex);
        o << setw(20) << "----------" << endl;
        o << setw(20) << "Signature:" << cmd.sig << endl;
        o << setw(20) << "Serial Number:" << cmd.ser_num << endl;
        o << setw(20) << "Payload size:" << cmd.payload_sz << " (" << dec << cmd.payload_sz << ")" << endl;
        o << setw(20) << "checksum:" << cmd.checksum << endl;
 
        o.flags(f);
 
        cmd.dump_payload(o);
 
        return o << setw(20) << "----------" << endl;
    }
 
protected:
    unsigned int sig;
    unsigned int ser_num;
    unsigned int payload_sz;
    unsigned int checksum;
    Stoker_Cmd_Id cmd_id;
    Stoker_Response& rsp;
 
    void set_bytes_header(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_short(bytes, offset, sig);
        Stream_Util::set_short(bytes, offset, ser_num);
        Stream_Util::set_short(bytes, offset, payload_sz);
        Stream_Util::set_byte(bytes, offset, checksum);
    }
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        return o << setw(20) << "CMD ID:" << cmd_id << endl;
    }
 
private:
    Stoker_Command& operator=(const Stoker_Command&) {
    }
};
 
class Stoker_Command_Byte_Param : public Stoker_Command {
public:
    Stoker_Command_Byte_Param(Stoker_Cmd_Id cmd_id, bool bool_param, Stoker_Response& rsp) : Stoker_Command(cmd_id, rsp) {
        payload_sz = 2;
        p = (bool_param) ? 1 : 0;
    }
 
    Stoker_Command_Byte_Param(Stoker_Cmd_Id cmd_id, unsigned int param, Stoker_Response& rsp) : Stoker_Command(cmd_id, rsp) {
        payload_sz = 2;
        p = param;
    }
 
protected:
    unsigned int p;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_byte(bytes, offset, p);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        return o << setw(20) << "BYTE:" << p << endl;
    }
};
 
class Stoker_Command_Short_Param : public Stoker_Command {
public:
    Stoker_Command_Short_Param(Stoker_Cmd_Id cmd_id, unsigned int param, Stoker_Response& rsp) : Stoker_Command(cmd_id, rsp) {
        payload_sz = 3;
        p = param;
    }
 
protected:
    unsigned int p;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_short(bytes, offset, p);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        return o << setw(20) << "SHORT:" << p << endl;
    }
};
 
class Stoker_Command_Name_Change : public Stoker_Command {
public:
    Stoker_Command_Name_Change(Stoker_Response& rsp) : Stoker_Command(STOKER_CMD_ID_NAME_CHANGE, rsp), name("") {
    }
 
    Stoker_Command_Name_Change(Ow_Id& ow_id, const char* new_name, Stoker_Response& rsp) : Stoker_Command(STOKER_CMD_ID_NAME_CHANGE, rsp), name("") {
        set_params(ow_id, new_name);
    }
 
    void set_params(Ow_Id& ow_id, const char* new_name) {
        this->ow_id = ow_id;
        name = new_name;
 
        // 1 for id + 8 for the OW_ID + 2 for length + string + null termination character
        payload_sz = 1 + 8 + 2 + strlen(name) + 1;
    }
 
protected:
    Ow_Id ow_id;
    const char* name;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_ow_id(bytes, offset, ow_id);
        Stream_Util::set_short(bytes, offset, strlen(name) + 1);
        Stream_Util::set_string(bytes, offset, name);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        o << setw(20) << "OW_ID:" << ow_id << endl;
        o << setw(20) << "strlen:" << (unsigned int)(strlen(name) + 1) << endl;
        return o << setw(20) << "name:" << name << endl;
    }
};
 
class Stoker_Command_Alarm_Mode : public Stoker_Command {
public:
    Stoker_Command_Alarm_Mode(Stoker_Response& rsp) :
        Stoker_Command(STOKER_CMD_ID_ALARM_MODE, rsp),
        alarm_mode(STOKER_ALARM_MODE_NONE) {
        payload_sz = 10;
    }
 
    Stoker_Command_Alarm_Mode(Ow_Id& ow_id, Stoker_Alarm_Mode alarm_mode, Stoker_Response& rsp) :
        Stoker_Command(STOKER_CMD_ID_ALARM_MODE, rsp),
        alarm_mode(STOKER_ALARM_MODE_NONE) {
        payload_sz = 10;
        set_params(ow_id, alarm_mode);
    }
 
    void set_params(Ow_Id& ow_id, Stoker_Alarm_Mode alarm_mode) {
        this->ow_id = ow_id;
        this->alarm_mode = alarm_mode;
    }
 
protected:
    Ow_Id ow_id;
    Stoker_Alarm_Mode alarm_mode;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_ow_id(bytes, offset, ow_id);
        Stream_Util::set_byte(bytes, offset, alarm_mode);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        o << setw(20) << "OW_ID:" << ow_id << endl;
        return o << setw(20) << "mode:" << alarm_mode << endl;
    }
};
 
class Stoker_Command_Temp_Change : public Stoker_Command {
public:
    Stoker_Command_Temp_Change(Stoker_Response& rsp) :
        Stoker_Command(STOKER_CMD_ID_TEMP_CHANGE, rsp),
        temp_id(STOKER_TEMP_ID_TARGET),
        t(0.0) {
        payload_sz = 14;
    }
 
    Stoker_Command_Temp_Change(Ow_Id& ow_id, Stoker_Temp_Id temp_id, double t, Stoker_Response& rsp) :
        Stoker_Command(STOKER_CMD_ID_TEMP_CHANGE, rsp),
        temp_id(STOKER_TEMP_ID_TARGET),
        t(0.0) {
        payload_sz = 14;
        set_params(ow_id, temp_id, t);
    }
 
    void set_params(Ow_Id& ow_id, Stoker_Temp_Id temp_id, double t) {
        this->ow_id = ow_id;
        this->temp_id = temp_id;
        this->t = t;
    }
 
protected:
    Ow_Id ow_id;
    Stoker_Temp_Id temp_id;
    double t;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_ow_id(bytes, offset, ow_id);
        Stream_Util::set_byte(bytes, offset, temp_id);
        Stream_Util::set_double(bytes, offset, t);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        o << setw(20) << "OW_ID:" << ow_id << endl;
        o << setw(20) << "TEMP_ID:" << temp_id << endl;
        return o << setw(20) << "temp:" << t << endl;
    }
};
 
class Stoker_Command_Switch_Change : public Stoker_Command {
public:
    Stoker_Command_Switch_Change(Stoker_Response& rsp) : Stoker_Command(STOKER_CMD_ID_SWITCH_CHANGE, rsp) {
        payload_sz = 17;
    }
 
    Stoker_Command_Switch_Change(Ow_Id& sr_ow_id, Ow_Id& sw_ow_id, Stoker_Response& rsp) : Stoker_Command(STOKER_CMD_ID_SWITCH_CHANGE, rsp) {
        payload_sz = 17;
        set_params(sr_ow_id, sw_ow_id);
    }
 
    void set_params(Ow_Id& sr_ow_id, Ow_Id& sw_ow_id) {
        this->sr_ow_id = sr_ow_id;
        this->sw_ow_id = sw_ow_id;
    }
 
protected:
    Ow_Id sr_ow_id;
    Ow_Id sw_ow_id;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_ow_id(bytes, offset, sr_ow_id);
        Stream_Util::set_ow_id(bytes, offset, sw_ow_id);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        o << setw(20) << "Sensor OW_ID:" << sr_ow_id << endl;
        return o << setw(20) << "Switch OW_ID:" << sw_ow_id << endl;
    }
};
 
class Stoker_Command_Switch_Enable : public Stoker_Command {
public:
    Stoker_Command_Switch_Enable(Stoker_Response& rsp) : Stoker_Command(STOKER_CMD_ID_SWITCH_ENABLE, rsp), en(false) {
        payload_sz = 10;
    }
 
    Stoker_Command_Switch_Enable(Ow_Id& ow_id, bool en, Stoker_Response& rsp) : Stoker_Command(STOKER_CMD_ID_SWITCH_ENABLE, rsp), en(false) {
        payload_sz = 10;
        set_params(ow_id, en);
    }
 
    void set_params(Ow_Id& ow_id, bool en) {
        this->ow_id = ow_id;
        this->en = en;
    }
 
protected:
    Ow_Id ow_id;
    bool en;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_ow_id(bytes, offset, ow_id);
        Stream_Util::set_byte(bytes, offset, en);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        o << setw(20) << "OW_ID:" << ow_id << endl;
        return o << setw(20) << "EN:" << (en?"TRUE":"FALSE") << endl;
    }
};
 
class Stoker_Command_Duty_Cycle_Min_Max : public Stoker_Command {
public:
    Stoker_Command_Duty_Cycle_Min_Max(Stoker_Response& rsp) :
      Stoker_Command(STOKER_CMD_ID_DUTY_CYCLE_MIN_MAX, rsp), dmin(0), dmax(0) {
        payload_sz = 11;
    }
 
    Stoker_Command_Duty_Cycle_Min_Max(
        Ow_Id& ow_id,
        unsigned int dmin,
        unsigned int dmax,
        Stoker_Response& rsp) : Stoker_Command(STOKER_CMD_ID_DUTY_CYCLE_MIN_MAX, rsp) {
        payload_sz = 11;
        set_params(ow_id, dmin, dmax);
    }
 
    void set_params(Ow_Id& ow_id, unsigned int dmin, unsigned int dmax) {
        this->ow_id = ow_id;
        this->dmin = dmin;
        this->dmax = dmax;
    }
 
protected:
    Ow_Id ow_id;
    unsigned int dmin;
    unsigned int dmax;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_ow_id(bytes, offset, ow_id);
        Stream_Util::set_byte(bytes, offset, dmin);
        Stream_Util::set_byte(bytes, offset, dmax);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        o << setw(20) << "OW_ID:" << ow_id << endl;
        o << setw(20) << "MIN:" << dmin << endl;
        return o << setw(20) << "MAX:" << dmax << endl;
    }
};
 
class Stoker_Command_Duty_Cycle_Time_Slice_Multiplier : public Stoker_Command {
public:
    Stoker_Command_Duty_Cycle_Time_Slice_Multiplier(Stoker_Response& rsp) :
      Stoker_Command(STOKER_CMD_ID_DUTY_CYCLE_TIME_SLICE_MULTIPLIER, rsp), mult(1) {
        payload_sz = 11;
    }
 
    Stoker_Command_Duty_Cycle_Time_Slice_Multiplier(
        Ow_Id& ow_id,
        unsigned int mult,
        Stoker_Response& rsp
        ) :
      Stoker_Command(STOKER_CMD_ID_DUTY_CYCLE_TIME_SLICE_MULTIPLIER, rsp) {
        payload_sz = 11;
        set_params(ow_id, mult);
    }
 
    void set_params(Ow_Id& ow_id, unsigned int mult) {
        this->ow_id = ow_id;
        this->mult = mult;
    }
 
protected:
    Ow_Id ow_id;
    unsigned int mult;
 
    virtual void set_bytes_payload(unsigned char* bytes, unsigned int& offset) {
        Stream_Util::set_byte(bytes, offset, cmd_id);
        Stream_Util::set_ow_id(bytes, offset, ow_id);
        Stream_Util::set_short(bytes, offset, mult);
    }
 
    virtual ostream& dump_payload(ostream& o) const {
        o << setw(20) << "CMD ID:" << cmd_id << endl;
        o << setw(20) << "OW_ID:" << ow_id << endl;
        return o << setw(20) << "MULT:" << mult << endl;
    }
};
 
#endif