/*
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