/[projects]/smsdaemon/main.cpp
ViewVC logotype

Diff of /smsdaemon/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 57 by torben, Wed Jun 11 16:08:37 2008 UTC revision 196 by torben, Thu Dec 18 06:53:29 2008 UTC
# Line 1  Line 1 
 #include <iostream>  
1  #include <string>  #include <string>
2    #include <stdlib.h>
3    #include <iostream>
4    
 #include <cctype>  
 #include <sstream>  
5  #include "daemon.h"  #include "daemon.h"
6  #include "common.h"  #include "Common.h"
7    #include "Logger.h"
 #include "GsmModem.h"  
 #include "SerialPort.h"  
   
 #include "Plugin.h"  
 #include "kbhit.h"  
8    
9  #include "util.h"  #include "ModemTransceiver.h"
10    #include "DebugTransceiver.h"
11    #include "SmsToolTransceiver.h"
12    
13    #include "serialport/SerialPort.h"
14    #include "SmsDaemon.h"
15    #include "ConfigFile.h"
16    
17  using namespace std;  using namespace std;
 using namespace Util;  
18    
19    
20  void create_log_message(SMS& sms,bool hasPlugin)  bool sms_exit(int exitcode)
21  {  {
22          ostringstream os;          if (Common::instance()->isDaemon)
23          os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);                  daemonCleanup();
         if (!hasPlugin)  
                 os << " -- PLUGIN NOT FOUND";  
24    
25          Common::instance()->logMessage(os.str());          exit(exitcode);
26  }  }
27    
28    SerialPort* port = 0;
29    ISmsTransceiver* transceiver = 0;
30    
31  void main_loop(GsmModem& modem)  void openModemPort()
32  {  {
33          Common* cmn = Common::instance();          Common* cmn = Common::instance();
34          volatile bool& mainContinue = cmn->mainContinue;          ConfigFile* config = cmn->GetConfigfile();
   
         PluginManager& manager = cmn->pluginManager;  
35    
36          mainContinue = true;          port = new SerialPort ( config->GetValue("gsmmodem","serialport") );
37            try
         while (mainContinue)  
38          {          {
39                  vector<SMS> sms = modem.ReadSms();                  port->Open( SerialPort::BAUD_9600,
40                                SerialPort::CHAR_SIZE_8,
41                  for (unsigned int i=0; i<sms.size(); ++i)                              SerialPort::PARITY_NONE,
42                  {                              SerialPort::STOP_BITS_1,
43                          string cmd = GetSmsCommand(sms[i]);                              SerialPort::FLOW_CONTROL_HARD );
44                                    }
45                          cmd = Util::str_tolower(cmd);          catch (std::exception &e)
46            {
47                          Plugin* pl = manager.GetPlugin(cmd);                  Logger::logMessage( string("PortOpen Exception: ") + e.what() );
48                    sms_exit(1);
49                          create_log_message(sms[i], pl != 0);          }
   
                         if (pl)  
                         {  
                                 pl->Execute(modem, sms[i]);  
                         }  
                         else  
                         {  
                                 modem.SendSms(sms[i].sender, "Unknown command!");  
                         }  
   
                         modem.DeleteSms(sms[i].sms_index);  
                         cmn->smsCounter.incomming++;  
                 }  
   
                 if (sms.size() == 0)  
                         Util::Sleep(10);  
50    
51                  if (cmn->isDebug && kbhit())          transceiver = new ModemTransceiver(*port);
                         break;  
52    
53            try
54            {
55                    ((ModemTransceiver*)transceiver)->Init();
56            }
57            catch (std::exception& e)
58            {
59                    Logger::logMessage( string("ModemTransceiver Exception: ") + e.what() );
60                    sms_exit(2);
61          }          }
62  }  }
63    void closeModemPort()
 bool sms_exit(int exitcode)  
64  {  {
65      if (Common::instance()->isDaemon)          port->Close();
66          daemonCleanup();          delete port;
67            delete transceiver;
         exit(exitcode);  
68  }  }
69    
70  int main(int argc, char* argv[])  int main(int argc, char* argv[])
# Line 88  int main(int argc, char* argv[]) Line 73  int main(int argc, char* argv[])
73    
74          //Set default values          //Set default values
75    
76          cmn->setLogfile( "/var/log/smsdaemon.log");          Logger::setLogfile( "/var/log/smsdaemon.log");
77          cmn->pidfile = "/var/run/smsdaemon.pid";          cmn->pidfile = "/var/run/smsdaemon.pid";
78          cmn->spooldir = "/var/spool/smsdaemon";          cmn->spooldir = "/var/spool/smsdaemon";
79    
         cmn->uid = 1000;  
         cmn->gid = 1000;  
80    
81          cmn->loadConfig(argc,argv);          cmn->loadConfig(argc,argv);
82          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
83    
84                    ConfigFile* config = cmn->GetConfigfile();
         /////////////////////  
           
         //Write a delimiter line in the logfile to seperate sessions  
85    
86          if (Common::instance()->isDaemon)          bool res = config->Open( cmn->configFilePath ) ;
87                  daemonize();          if (!res)
88            {
89                    cout << "Could not open config file:" << cmn->configFilePath << endl;
90                    return 1;
91            }
92    
93          cmn->logMessage("--------------------------------");          Logger::initLog();
94    
95          cmn->pluginManager.LoadPlugins();  
96                    /////////////////////
97          SerialPort port("/dev/ttyS1" );          string transconf = config->GetValue("smsdaemon", "transceiver");
98          try  
99    
100            if (transconf == "internal")
101          {          {
102                  port.Open( SerialPort::BAUD_9600,                  openModemPort();
103                                     SerialPort::CHAR_SIZE_8,                  closeModemPort();
                                    SerialPort::PARITY_NONE,  
                                    SerialPort::STOP_BITS_1,  
                                    SerialPort::FLOW_CONTROL_HARD );  
104          }          }
105          catch(std::exception &e)          else if ( transconf == "debug" || transconf == "smstools")
106          {          {
107                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  //do nothing
108                  sms_exit(1);          }
109            else
110            {
111                    Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
112                    exit(1);
113          }          }
114    
         GsmModem modem(port);  
115    
116          try  
117            if (Common::instance()->isDaemon)
118          {          {
119                  modem.Init();                  lookup_uid_values();
120                    daemonize();
121                    cmn->daemonized = true;
122          }          }
123          catch (std::exception& e)  
124    
125            if (transconf == "internal")
126          {          {
127                  cmn->logMessage( string("GsmModem Exception: ") + e.what() );                  openModemPort();
128                  sms_exit(2);          }
129            else if (transconf =="smstools")
130            {
131                    transceiver = new SmsToolTransceiver();
132            }
133            else if (transconf == "debug")
134            {
135                    transceiver = new DebugTransceiver();
136          }          }
   
137    
138          //////////////////////////////////          //////////////////////////////////
139    
140          cmn->logMessage("SMS daemon started");          SmsDaemon daemon(*transceiver);
141    
142          modem.DeleteAllSms();          daemon.Start();
143            //returns here when main-loop exits
144    
145          main_loop(modem);          if (cmn->isDaemon)
146                    daemonCleanup();
147    
148          cmn->logMessage( cmn->getStatusMessage() );          if (transconf == "builtin")
149            {
150      if (cmn->isDaemon)                  closeModemPort();
151          daemonCleanup();          }
152    
153          return 0;          return 0;
154  }  }

Legend:
Removed from v.57  
changed lines
  Added in v.196

  ViewVC Help
Powered by ViewVC 1.1.20