--- smsdaemon/main.cpp 2008/06/12 12:43:29 63 +++ smsdaemon/main.cpp 2008/12/08 10:42:04 151 @@ -1,85 +1,68 @@ -#include #include +#include -#include -#include #include "daemon.h" #include "common.h" -#include "GsmModem.h" -#include "SerialPort.h" - -#include "Plugin.h" -#include "kbhit.h" - -#include "util.h" +#include "ModemTransceiver.h" +#include "DebugTransceiver.h" +#include "SmsToolTransceiver.h" + +#include "serialport/SerialPort.h" +#include "SmsDaemon.h" +#include "ConfigFile.h" using namespace std; -using namespace Util; -void create_log_message(SMS& sms,bool hasPlugin) +bool sms_exit(int exitcode) { - ostringstream os; - os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); - if (!hasPlugin) - os << " -- PLUGIN NOT FOUND"; + if (Common::instance()->isDaemon) + daemonCleanup(); - Common::instance()->logMessage(os.str()); + exit(exitcode); } +SerialPort* port = 0; +ISmsTransceiver* transceiver = 0; -void main_loop(GsmModem& modem) +void openModemPort() { Common* cmn = Common::instance(); - volatile bool& mainContinue = cmn->mainContinue; - - PluginManager& manager = cmn->pluginManager; + ConfigFile* config = cmn->GetConfigfile(); - mainContinue = true; - - while (mainContinue) + port = new SerialPort ( config->GetValue("smsdaemon","serialport") ); + try { - vector sms = modem.ReadSms(); - - for (unsigned int i=0; iExecute(modem, sms[i]); - } - else - { - modem.SendSms(sms[i].sender, "Unknown command!", false); - } - - modem.DeleteSms(sms[i].sms_index); - cmn->smsCounter.incomming++; - } - - if (sms.size() == 0) - Util::Sleep(10); + 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); + } - if (cmn->isDebug && kbhit()) - break; + transceiver = new ModemTransceiver(*port); + try + { + ((ModemTransceiver*)transceiver)->Init(); + } + catch (std::exception& e) + { + cmn->logMessage( string("ModemTransceiver Exception: ") + e.what() ); + sms_exit(2); } } - -bool sms_exit(int exitcode) +void closeModemPort() { - if (Common::instance()->isDaemon) - daemonCleanup(); - - exit(exitcode); + port->Close(); + delete port; + delete transceiver; } int main(int argc, char* argv[]) @@ -92,65 +75,76 @@ 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(); - - cmn->logMessage("--------------------------------"); - - cmn->pluginManager.LoadPlugins(); + ///////////////////// + string transconf = config->GetValue("smsdaemon", "transceiver"); - SerialPort port("/dev/ttyS1" ); - 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(); } - ////////////////////////////////// - cmn->logMessage("SMS daemon started"); - - modem.DeleteAllSms(); + SmsDaemon daemon(*transceiver); - main_loop(modem); - - cmn->logMessage( cmn->getStatusMessage() ); + daemon.Start(); + //returns here when main-loop exits if (cmn->isDaemon) daemonCleanup(); + if (transconf == "builtin") + { + closeModemPort(); + } + return 0; }