--- smsdaemon/main.cpp 2008/12/01 12:08:23 118 +++ smsdaemon/main.cpp 2008/12/08 10:42:04 151 @@ -4,10 +4,13 @@ #include "daemon.h" #include "common.h" -#include "GsmModem.h" -#include "SerialPort.h" -#include "SmsDaemon.h" +#include "ModemTransceiver.h" +#include "DebugTransceiver.h" +#include "SmsToolTransceiver.h" +#include "serialport/SerialPort.h" +#include "SmsDaemon.h" +#include "ConfigFile.h" using namespace std; @@ -20,6 +23,48 @@ exit(exitcode); } +SerialPort* port = 0; +ISmsTransceiver* transceiver = 0; + +void openModemPort() +{ + Common* cmn = Common::instance(); + ConfigFile* config = cmn->GetConfigfile(); + + port = new SerialPort ( config->GetValue("smsdaemon","serialport") ); + try + { + port->Open( SerialPort::BAUD_9600, + SerialPort::CHAR_SIZE_8, + SerialPort::PARITY_NONE, + SerialPort::STOP_BITS_1, + SerialPort::FLOW_CONTROL_HARD ); + } + catch(std::exception &e) + { + cmn->logMessage( string("PortOpen Exception: ") + e.what() ); + sms_exit(1); + } + + transceiver = new ModemTransceiver(*port); + + try + { + ((ModemTransceiver*)transceiver)->Init(); + } + catch (std::exception& e) + { + cmn->logMessage( string("ModemTransceiver Exception: ") + e.what() ); + sms_exit(2); + } +} +void closeModemPort() +{ + port->Close(); + delete port; + delete transceiver; +} + int main(int argc, char* argv[]) { Common* cmn = Common::instance(); @@ -30,52 +75,64 @@ cmn->pidfile = "/var/run/smsdaemon.pid"; cmn->spooldir = "/var/spool/smsdaemon"; - cmn->uid = 1000; - cmn->gid = 1000; cmn->loadConfig(argc,argv); cmn->daemonStart = time(0); + ConfigFile* config = cmn->GetConfigfile(); - ///////////////////// + bool res = config->Open( cmn->configFilePath ) ; + if (!res) { + cmn->logMessage(string("Could not open config file:") + cmn->configFilePath); + return 1; + } - //Write a delimiter line in the logfile to seperate sessions - - if (Common::instance()->isDaemon) - daemonize(); + ///////////////////// + string transconf = config->GetValue("smsdaemon", "transceiver"); - SerialPort port("/dev/ttyUSB0" ); - try + + if (transconf == "builtin") { - port.Open( SerialPort::BAUD_9600, - SerialPort::CHAR_SIZE_8, - SerialPort::PARITY_NONE, - SerialPort::STOP_BITS_1, - SerialPort::FLOW_CONTROL_HARD ); + openModemPort(); + closeModemPort(); + } + else if ( transconf == "debug" || transconf == "smstools") + { + //do nothing } - catch(std::exception &e) + else { - cmn->logMessage( string("PortOpen Exception: ") + e.what() ); - sms_exit(1); + cmn->logMessage( string("Invalid transceiver setting: ")+transconf); + exit(1); } - GsmModem modem(port); + - try + if (Common::instance()->isDaemon) { - modem.Init(); + lookup_uid_values(); + daemonize(); + cmn->daemonized = true; } - catch (std::exception& e) + + + if (transconf == "builtin") { - cmn->logMessage( string("GsmModem Exception: ") + e.what() ); - sms_exit(2); + openModemPort(); + } + else if (transconf =="smstools") + { + transceiver = new SmsToolTransceiver(); + } + else if (transconf == "debug") + { + transceiver = new DebugTransceiver(); } - ////////////////////////////////// - SmsDaemon daemon(modem); + SmsDaemon daemon(*transceiver); daemon.Start(); //returns here when main-loop exits @@ -83,6 +140,11 @@ if (cmn->isDaemon) daemonCleanup(); + if (transconf == "builtin") + { + closeModemPort(); + } + return 0; }