15.12.2011 - version 0.1.1
This commit is contained in:
137
piprotocol.cpp
137
piprotocol.cpp
@@ -12,12 +12,12 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
return;
|
||||
}
|
||||
int ps;
|
||||
bool ok, has_dev = false;
|
||||
bool ok, gok, has_dev = false;
|
||||
PIFlags<PISerial::Parameters> pp;
|
||||
PIConfig::Entry & b(conf.getValue(name)),
|
||||
& rb(b.getValue("receiver")),
|
||||
& sb(b.getValue("sender"));
|
||||
PIString dev;
|
||||
PIString dev, gdev;
|
||||
|
||||
/// receiver section
|
||||
if (rb.isEntryExists("ip") && rb.isEntryExists("device")) {
|
||||
@@ -26,7 +26,14 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
return;
|
||||
}
|
||||
dev = rb.getValue("ip", "", &ok);
|
||||
if (ok) {
|
||||
gdev = b.getValue("ip", "", &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
ps = rb.getValue("port", 0, &ok);
|
||||
if (!ok) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.port\" in \"" << config << "\"!" << endl;
|
||||
@@ -43,7 +50,14 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
|
||||
}
|
||||
dev = rb.getValue("device", "", &ok);
|
||||
if (ok) {
|
||||
gdev = b.getValue("device", "", &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver type in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
ps = rb.getValue("speed", 0, &ok);
|
||||
if (!ok) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.speed\" in \"" << config << "\"!" << endl;
|
||||
@@ -66,15 +80,41 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (recDataSize == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
|
||||
}
|
||||
float freq = rb.getValue("frequency", -1.f);
|
||||
if (freq > 0. && !has_dev)
|
||||
history_write_rec = rb.getValue("writeHistory", false, &ok);
|
||||
bool ghist = b.getValue("writeHistory", false, &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) history_write_rec = ghist;
|
||||
if (gok && ok && (history_write_rec != ghist)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous receiver history in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
if (history_write_rec) {
|
||||
history_path_rec = rb.getValue("historyFile", "./history_" + protName + "_rec_" +
|
||||
date2string(currentDate(), "__dd_mm_yyyy_") +
|
||||
time2string(currentTime(), "_hh_mm_ss_")).value();
|
||||
history_id_rec = b.getValue("historyID", 0, &ok);
|
||||
if (!ok) {
|
||||
history_id_rec = protName.toByteArray().checksumCRC16();
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no receiver history ID defined, write with ID = " << history_id_rec << endl;
|
||||
}
|
||||
history_file_rec.open(history_path_rec, PIFile::Write | PIFile::New);
|
||||
}
|
||||
}
|
||||
float freq = rb.getValue("frequency", -1.f, &ok), gfreq = b.getValue("frequency", -1.f, &gok);
|
||||
if (gok && !ok) freq = gfreq;
|
||||
if (gok && ok && (freq != gfreq)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous expected frequency in \"" << config << "\"!" << endl;
|
||||
devReceiverState = "Config error";
|
||||
return;
|
||||
}
|
||||
if (freq > 0.f && !has_dev)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no receiver device and not null expected frequency!" << endl;
|
||||
float tm = b.getValue("disconnectTimeout", 3.f);
|
||||
if (tm <= 0.)
|
||||
if (tm <= 0.f)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: diconnect timeout <= 0 s!" << endl;
|
||||
timeout_ = (tm < 0.) ? 0. : tm;
|
||||
timeout_ = (tm < 0.f) ? 0.f : tm;
|
||||
setExpectedFrequency(freq);
|
||||
changeDisconnectTimeout();
|
||||
|
||||
/// sender section
|
||||
if (sb.isEntryExists("ip") && sb.isEntryExists("device")) {
|
||||
@@ -83,8 +123,15 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
return;
|
||||
}
|
||||
dev = sb.getValue("ip", "", &ok);
|
||||
gdev = b.getValue("ip", "", &gok);
|
||||
has_dev = false;
|
||||
if (ok) {
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
ps = sb.getValue("port", 0, &ok);
|
||||
if (!ok) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.port\" in \"" << config << "\"!" << endl;
|
||||
@@ -101,7 +148,14 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
|
||||
}
|
||||
dev = sb.getValue("device", "", &ok);
|
||||
if (ok) {
|
||||
gdev = b.getValue("device", "", &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) dev = gdev;
|
||||
if (gok && ok && (dev != gdev)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender type in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
ps = sb.getValue("speed", 0, &ok);
|
||||
if (!ok) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.speed\" in \"" << config << "\"!" << endl;
|
||||
@@ -122,8 +176,36 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
if (sendDataSize_ == 0)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
|
||||
}
|
||||
freq = sb.getValue("frequency", -1.f);
|
||||
if (freq > 0. && !has_dev)
|
||||
history_write_send = sb.getValue("writeHistory", false, &ok);
|
||||
ghist = b.getValue("writeHistory", false, &gok);
|
||||
if (ok || gok) {
|
||||
if (gok && !ok) history_write_send = ghist;
|
||||
if (gok && ok && (history_write_send != ghist)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender history in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
if (history_write_send) {
|
||||
history_path_send = sb.getValue("historyFile", "./history_" + protName + "_send_" +
|
||||
date2string(currentDate(), "__dd_mm_yyyy_") +
|
||||
time2string(currentTime(), "_hh_mm_ss_")).value();
|
||||
history_id_send = b.getValue("historyID", 0, &ok);
|
||||
if (!ok) {
|
||||
history_id_send = protName.toByteArray().checksumCRC16() + 1;
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no sender history ID defined, write with ID = " << history_id_send << endl;
|
||||
}
|
||||
history_file_send.open(history_path_send, PIFile::Write | PIFile::New);
|
||||
}
|
||||
}
|
||||
freq = sb.getValue("frequency", -1.f, &ok);
|
||||
gfreq = b.getValue("frequency", -1.f, &gok);
|
||||
if (gok && !ok) freq = gfreq;
|
||||
if (gok && ok && (freq != gfreq)) {
|
||||
cout << "[PIProtocol \"" << name << "\"] Ambiguous sender frequency in \"" << config << "\"!" << endl;
|
||||
devSenderState = "Config error";
|
||||
return;
|
||||
}
|
||||
if (freq > 0.f && !has_dev)
|
||||
cout << "[PIProtocol \"" << name << "\"] Warning: no sender device and not null send frequency!" << endl;
|
||||
setSenderFrequency(freq);
|
||||
|
||||
@@ -145,6 +227,20 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
|
||||
|
||||
PIProtocol::~PIProtocol() {
|
||||
//cout << "prot " << protName << " delete\n";
|
||||
if (history_write_rec) {
|
||||
if (history_file_rec.isEmpty()) {
|
||||
history_file_rec.close();
|
||||
history_file_rec.remove();
|
||||
}
|
||||
history_file_rec.close();
|
||||
}
|
||||
if (history_write_send) {
|
||||
if (history_file_send.isEmpty()) {
|
||||
history_file_send.close();
|
||||
history_file_send.remove();
|
||||
}
|
||||
history_file_send.close();
|
||||
}
|
||||
delete diagTimer;
|
||||
delete sendTimer;
|
||||
if (packet != 0) delete packet;
|
||||
@@ -154,7 +250,7 @@ PIProtocol::~PIProtocol() {
|
||||
|
||||
|
||||
void PIProtocol::init() {
|
||||
work = new_mp_prot = false;
|
||||
work = new_mp_prot = history_write_rec = history_write_send = false;
|
||||
eth = 0;
|
||||
ser = 0;
|
||||
ret_func = 0;
|
||||
@@ -173,6 +269,7 @@ void PIProtocol::init() {
|
||||
type_rec = type_send = PIProtocol::None;
|
||||
devSenderState = devReceiverState = "Unknown";
|
||||
devSenderName = devReceiverName = "no device";
|
||||
history_rsize_rec = history_rsize_send = "no file";
|
||||
/*addEvent("receiver started");
|
||||
addEvent("receiver stopped");
|
||||
addEvent("sender started");
|
||||
@@ -287,6 +384,10 @@ bool PIProtocol::receiveEvent(void * t, char * data, int size) {
|
||||
p->work = true;
|
||||
//p->lock();
|
||||
if (p->validate()) {
|
||||
if (p->history_write_rec) {
|
||||
p->history_file_rec.writeToBinLog(p->history_id_rec, data, size);
|
||||
p->history_rsize_rec.setReadableSize(p->history_file_rec.pos());
|
||||
}
|
||||
raiseEvent<bool>(p, "received", true);
|
||||
//p->unlock();
|
||||
p->receive_count++;
|
||||
@@ -389,6 +490,10 @@ void PIProtocol::check_state() {
|
||||
void PIProtocol::send(const void * data, int size) {
|
||||
if (data == 0 || size == 0) return;
|
||||
if (!aboutSend()) return;
|
||||
if (history_write_send) {
|
||||
history_file_send.writeToBinLog(history_id_send, data, size);
|
||||
history_rsize_send.setReadableSize(history_file_send.pos());
|
||||
}
|
||||
if (type_send == PIProtocol::Serial)
|
||||
if (ser->send((char * )data, size))
|
||||
send_count++;
|
||||
@@ -404,6 +509,10 @@ void PIProtocol::send() {
|
||||
//unlock();
|
||||
if (sendDataPtr == 0 || sendDataSize == 0) return;
|
||||
if (!aboutSend()) return;
|
||||
if (history_write_send) {
|
||||
history_file_send.writeToBinLog(history_id_send, sendDataPtr, sendDataSize);
|
||||
history_rsize_send.setReadableSize(history_file_send.pos());
|
||||
}
|
||||
if (type_send == PIProtocol::Serial)
|
||||
if (ser->send((char * )sendDataPtr, sendDataSize))
|
||||
send_count++;
|
||||
|
||||
Reference in New Issue
Block a user