--- smsdaemon/main.cpp 2008/06/09 21:36:39 29 +++ smsdaemon/main.cpp 2008/06/16 09:04:05 88 @@ -1,79 +1,24 @@ -#include #include -#include -#include #include "daemon.h" #include "common.h" #include "GsmModem.h" #include "SerialPort.h" +#include "SmsDaemon.h" -#include "Plugin.h" -#include "kbhit.h" - -#include "util.h" using namespace std; -using namespace Util; - - -void create_log_message(SMS& sms,bool hasPlugin) -{ - ostringstream os; - os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); - if (!hasPlugin) - os << " -- PLUGIN NOT FOUND"; - - Common::instance()->logMessage(os.str()); -} -void main_loop(GsmModem& modem) +bool sms_exit(int exitcode) { - Common* cmn = Common::instance(); - volatile bool& mainContinue = cmn->mainContinue; - - PluginManager& manager = cmn->pluginManager; - - mainContinue = true; - - while (mainContinue) - { - vector sms = modem.ReadSms(); - - for (unsigned int i=0; iExecute(modem, sms[i]); - } - else - { - modem.SendSms(sms[i].sender, "Unknown command!"); - } + if (Common::instance()->isDaemon) + daemonCleanup(); - modem.DeleteSms(sms[i].sms_index); - } - - if (sms.size() == 0) - Util::Sleep(10); - - if (cmn->isDebug && kbhit()) - break; - - } + exit(exitcode); } - int main(int argc, char* argv[]) { Common* cmn = Common::instance(); @@ -88,6 +33,7 @@ cmn->gid = 1000; cmn->loadConfig(argc,argv); + cmn->daemonStart = time(0); ///////////////////// @@ -97,31 +43,45 @@ if (Common::instance()->isDaemon) daemonize(); - cmn->logMessage("--------------------------------"); - - cmn->pluginManager.LoadPlugins(); SerialPort port("/dev/ttyS1" ); try { - port.Open( SerialPort::BAUD_9600 ); + 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() ); - exit(1); + sms_exit(1); } GsmModem modem(port); + try + { + modem.Init(); + } + catch (std::exception& e) + { + cmn->logMessage( string("GsmModem Exception: ") + e.what() ); + sms_exit(2); + } + ////////////////////////////////// - cmn->logMessage("SMS daemon started"); + SmsDaemon daemon(modem); - modem.DeleteAllSms(); + daemon.Start(); + //returns here when main-loop exits - main_loop(modem); + if (cmn->isDaemon) + daemonCleanup(); + return 0; }