29.12.2011 - license GPLv3

This commit is contained in:
peri4
2011-12-29 12:25:17 +03:00
parent f141bab1c8
commit 92691981fb
56 changed files with 1830 additions and 59 deletions

View File

@@ -1,3 +1,22 @@
/*
PIP - Platform Independent Primitives
Protocol, input/output channel (COM, UDP)
Copyright (C) 2011 Ivan Pelipenko peri4@rambler.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piprotocol.h"
@@ -11,8 +30,8 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
devReceiverState = devSenderState = "Config error";
return;
}
int ps;
bool ok, gok, has_dev = false;
int ps, gps;
bool ok, gok, flag, gflag, has_dev = false;
PIFlags<PISerial::Parameters> pp;
PIConfig::Entry & b(conf.getValue(name)),
& rb(b.getValue("receiver")),
@@ -35,19 +54,27 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
return;
}
ps = rb.getValue("port", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.port\" in \"" << config << "\"!" << endl;
gps = b.getValue("port", 0, &gok);
if (ok || gok) {
if (gok && !ok) ps = gps;
if (gok && ok && (ps != gps)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive port in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
type_rec = PIProtocol::Ethernet;
eth = new PIEthernet(dev, ps, this, receiveEvent);
setReceiverAddress(dev, ps);
has_dev = true;
if (recDataPtr == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
if (recDataSize == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
} else {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.port\" or \"" << name << ".port\" in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
type_rec = PIProtocol::Ethernet;
eth = new PIEthernet(dev, ps, this, receiveEvent);
setReceiverAddress(dev, ps);
has_dev = true;
if (recDataPtr == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
if (recDataSize == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
}
dev = rb.getValue("device", "", &ok);
gdev = b.getValue("device", "", &gok);
@@ -59,26 +86,54 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
return;
}
ps = rb.getValue("speed", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.speed\" in \"" << config << "\"!" << endl;
gps = b.getValue("speed", 0, &gok);
if (ok || gok) {
if (gok && !ok) ps = gps;
if (gok && ok && (ps != gps)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"speed\" in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
flag = rb.getValue("parity", false, &ok);
gflag = b.getValue("parity", false, &gok);
if (ok || gok) {
if (gok && !ok) flag = gflag;
if (gok && ok && (flag != gflag)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"parity\" in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
pp |= PISerial::ParityControl;
}
flag = rb.getValue("twoStopBits", false, &ok);
gflag = b.getValue("twoStopBits", false, &gok);
if (ok || gok) {
if (gok && !ok) flag = gflag;
if (gok && ok && (flag != gflag)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous receive \"twoStopBits\" parity in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
pp |= PISerial::TwoStopBits;
}
type_rec = PIProtocol::Serial;
type_send = PIProtocol::Serial;
ser = new PISerial(dev, this, receiveEvent);
setReceiverDevice(dev, (PISerial::Speed)ps);
setSenderDevice(dev, (PISerial::Speed)ps);
ser->setInSpeed((PISerial::Speed)ps);
ser->setParameters(pp);
ser->setReadData(recHeaderPtr, recHeaderSize, recDataSize);
has_dev = true;
if (recDataPtr == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
if (recDataSize == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
} else {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".receiver.speed\" or \"" << name << ".speed\" in \"" << config << "\"!" << endl;
devReceiverState = "Config error";
return;
}
if (rb.getValue("parity", false)) pp |= PISerial::ParityControl;
if (rb.getValue("twoStopBits", false)) pp |= PISerial::TwoStopBits;
type_rec = PIProtocol::Serial;
type_send = PIProtocol::Serial;
ser = new PISerial(dev, this, receiveEvent);
setReceiverDevice(dev, (PISerial::Speed)ps);
setSenderDevice(dev, (PISerial::Speed)ps);
ser->setInSpeed((PISerial::Speed)ps);
ser->setParameters(pp);
ser->setReadData(recHeaderPtr, recHeaderSize, recDataSize);
has_dev = true;
if (recDataPtr == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data pointer!" << endl;
if (recDataSize == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null receive data size!" << endl;
}
history_write_rec = rb.getValue("writeHistory", false, &ok);
bool ghist = b.getValue("writeHistory", false, &gok);
@@ -93,7 +148,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
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);
history_id_rec = rb.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;
@@ -133,19 +188,27 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
return;
}
ps = sb.getValue("port", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.port\" in \"" << config << "\"!" << endl;
gps = b.getValue("port", 0, &gok);
if (ok || gok) {
if (gok && !ok) ps = gps;
if (gok && ok && (ps != gps)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous send port in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
type_send = PIProtocol::Ethernet;
if (eth == 0) eth = new PIEthernet(dev, ps, this, receiveEvent);
setSenderAddress(dev, ps);
has_dev = true;
if (sendDataPtr_ == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null send data pointer!" << endl;
if (sendDataSize_ == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
} else {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.port\" or \"" << name << ".port\" in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
type_send = PIProtocol::Ethernet;
if (eth == 0) eth = new PIEthernet(dev, ps, this, receiveEvent);
setSenderAddress(dev, ps);
has_dev = true;
if (sendDataPtr_ == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null send data pointer!" << endl;
if (sendDataSize_ == 0)
cout << "[PIProtocol \"" << name << "\"] Warning: null send data size!" << endl;
}
dev = sb.getValue("device", "", &ok);
gdev = b.getValue("device", "", &gok);
@@ -157,13 +220,41 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
return;
}
ps = sb.getValue("speed", 0, &ok);
if (!ok) {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.speed\" in \"" << config << "\"!" << endl;
gps = b.getValue("speed", 0, &gok);
if (ok || gok) {
if (gok && !ok) ps = gps;
if (gok && ok && (ps != gps)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous send \"speed\" in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
flag = sb.getValue("parity", false, &ok);
gflag = b.getValue("parity", false, &gok);
if (ok || gok) {
if (gok && !ok) flag = gflag;
if (gok && ok && (flag != gflag)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous send \"parity\" in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
pp |= PISerial::ParityControl;
}
flag = sb.getValue("twoStopBits", false, &ok);
gflag = b.getValue("twoStopBits", false, &gok);
if (ok || gok) {
if (gok && !ok) flag = gflag;
if (gok && ok && (flag != gflag)) {
cout << "[PIProtocol \"" << name << "\"] Ambiguous send \"twoStopBits\" parity in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
pp |= PISerial::TwoStopBits;
}
} else {
cout << "[PIProtocol \"" << name << "\"] Can`t find \"" << name << ".sender.speed\" or \"" << name << ".speed\" in \"" << config << "\"!" << endl;
devSenderState = "Config error";
return;
}
if (sb.getValue("parity", false)) pp |= PISerial::ParityControl;
if (sb.getValue("twoStopBits", false)) pp |= PISerial::TwoStopBits;
type_send = PIProtocol::Serial;
if (ser == 0) ser = new PISerial(dev, this, receiveEvent);
setSenderDevice(dev, (PISerial::Speed)ps);
@@ -189,7 +280,7 @@ PIProtocol::PIProtocol(const PIString & config, const PIString & name, void * re
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);
history_id_send = sb.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;