--- smsdaemon/main.cpp 2008/12/01 12:08:23 118 +++ smsdaemon/main.cpp 2008/12/21 22:08:20 211 @@ -1,87 +1,157 @@ #include #include +#include #include "daemon.h" -#include "common.h" +#include "Common.h" +#include "Logger.h" -#include "GsmModem.h" -#include "SerialPort.h" -#include "SmsDaemon.h" +#include "ModemTransceiver.h" +#include "DebugTransceiver.h" +#include "SmsToolTransceiver.h" +#include "ProxyTransceiver.h" +#include "serialport/SerialPort.h" +#include "SmsDaemon.h" +#include "ConfigFile.h" using namespace std; bool sms_exit(int exitcode) { - if (Common::instance()->isDaemon) - daemonCleanup(); + if (Common::instance()->isDaemon) + daemonCleanup(); exit(exitcode); } +SerialPort* port = 0; +ISmsTransceiver* transceiver = 0; + +void openModemPort() +{ + Common* cmn = Common::instance(); + ConfigFile* config = cmn->GetConfigfile(); + + port = new SerialPort ( config->GetValue("gsmmodem","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) + { + Logger::logMessage( string("PortOpen Exception: ") + e.what() ); + sms_exit(1); + } + + transceiver = new ModemTransceiver(*port); + + try + { + ((ModemTransceiver*)transceiver)->Init(); + } + catch (std::exception& e) + { + Logger::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(); //Set default values - cmn->setLogfile( "/var/log/smsdaemon.log"); + Logger::setLogfile( "/var/log/smsdaemon.log"); 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) + { + cout << "Could not open config file:" << cmn->configFilePath << endl; + return 1; + } + + Logger::initLog(); + + ///////////////////// - - //Write a delimiter line in the logfile to seperate sessions + string transconf = config->GetValue("smsdaemon", "transceiver"); - if (Common::instance()->isDaemon) - daemonize(); - - SerialPort port("/dev/ttyUSB0" ); - try + if (transconf == "internal") { - port.Open( SerialPort::BAUD_9600, - SerialPort::CHAR_SIZE_8, - SerialPort::PARITY_NONE, - SerialPort::STOP_BITS_1, - SerialPort::FLOW_CONTROL_HARD ); + openModemPort(); + closeModemPort(); } - catch(std::exception &e) + else if ( transconf == "debug" || transconf == "smstools") { - cmn->logMessage( string("PortOpen Exception: ") + e.what() ); - sms_exit(1); + //do nothing + } + else + { + Logger::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 == "internal") { - 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(); } + ProxyTransceiver proxy(*transceiver); ////////////////////////////////// - SmsDaemon daemon(modem); + SmsDaemon daemon(proxy); - daemon.Start(); + daemon.Start(); //returns here when main-loop exits - if (cmn->isDaemon) - daemonCleanup(); + if (cmn->isDaemon) + daemonCleanup(); + + if (transconf == "builtin") + { + closeModemPort(); + } return 0; }