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

Diff of /smsdaemon/main.cpp

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

revision 149 by torben, Sun Dec 7 20:58:41 2008 UTC revision 196 by torben, Thu Dec 18 06:53:29 2008 UTC
# Line 1  Line 1 
1  #include <string>  #include <string>
2  #include <stdlib.h>  #include <stdlib.h>
3    #include <iostream>
4    
5  #include "daemon.h"  #include "daemon.h"
6  #include "common.h"  #include "Common.h"
7    #include "Logger.h"
8    
9  #include "ModemTransceiver.h"  #include "ModemTransceiver.h"
10    #include "DebugTransceiver.h"
11    #include "SmsToolTransceiver.h"
12    
13  #include "serialport/SerialPort.h"  #include "serialport/SerialPort.h"
14  #include "SmsDaemon.h"  #include "SmsDaemon.h"
15  #include "ConfigFile.h"  #include "ConfigFile.h"
# Line 14  using namespace std; Line 19  using namespace std;
19    
20  bool sms_exit(int exitcode)  bool sms_exit(int exitcode)
21  {  {
22      if (Common::instance()->isDaemon)          if (Common::instance()->isDaemon)
23          daemonCleanup();                  daemonCleanup();
24    
25          exit(exitcode);          exit(exitcode);
26  }  }
27    
28    SerialPort* port = 0;
29    ISmsTransceiver* transceiver = 0;
30    
31    void openModemPort()
32    {
33            Common* cmn = Common::instance();
34            ConfigFile* config = cmn->GetConfigfile();
35    
36            port = new SerialPort ( config->GetValue("gsmmodem","serialport") );
37            try
38            {
39                    port->Open( SerialPort::BAUD_9600,
40                                SerialPort::CHAR_SIZE_8,
41                                SerialPort::PARITY_NONE,
42                                SerialPort::STOP_BITS_1,
43                                SerialPort::FLOW_CONTROL_HARD );
44            }
45            catch (std::exception &e)
46            {
47                    Logger::logMessage( string("PortOpen Exception: ") + e.what() );
48                    sms_exit(1);
49            }
50    
51            transceiver = new ModemTransceiver(*port);
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()
64    {
65            port->Close();
66            delete port;
67            delete transceiver;
68    }
69    
70  int main(int argc, char* argv[])  int main(int argc, char* argv[])
71  {  {
# Line 27  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    
# Line 36  int main(int argc, char* argv[]) Line 82  int main(int argc, char* argv[])
82          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
83    
84          ConfigFile* config = cmn->GetConfigfile();          ConfigFile* config = cmn->GetConfigfile();
85            
86          bool res = config->Open( cmn->configFilePath ) ;          bool res = config->Open( cmn->configFilePath ) ;
87          if (!res) {          if (!res)
88                  cmn->logMessage(string("Could not open config file:") + cmn->configFilePath);          {
89                    cout << "Could not open config file:" << cmn->configFilePath << endl;
90                  return 1;                  return 1;
91          }          }
92            
93            Logger::initLog();
94    
95    
96          /////////////////////          /////////////////////
97                    string transconf = config->GetValue("smsdaemon", "transceiver");
98          //Write a delimiter line in the logfile to seperate sessions  
99    
100          if (Common::instance()->isDaemon)          if (transconf == "internal")
101          {          {
102                  lookup_uid_values();                  openModemPort();
103                  daemonize();                  closeModemPort();
104          }          }
105            else if ( transconf == "debug" || transconf == "smstools")
           
         SerialPort port( config->GetValue("smsdaemon","serialport") );  
         try  
106          {          {
107                  port.Open( SerialPort::BAUD_9600,                  //do nothing
                                    SerialPort::CHAR_SIZE_8,  
                                    SerialPort::PARITY_NONE,  
                                    SerialPort::STOP_BITS_1,  
                                    SerialPort::FLOW_CONTROL_HARD );  
108          }          }
109          catch(std::exception &e)          else
110          {          {
111                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
112                  sms_exit(1);                  exit(1);
113          }          }
114    
         ModemTransceiver 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("ModemTransceiver 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          SmsDaemon daemon(modem);          SmsDaemon daemon(*transceiver);
141    
142          daemon.Start();          daemon.Start();
143          //returns here when main-loop exits          //returns here when main-loop exits
144    
145      if (cmn->isDaemon)          if (cmn->isDaemon)
146          daemonCleanup();                  daemonCleanup();
147    
148            if (transconf == "builtin")
149            {
150                    closeModemPort();
151            }
152    
153          return 0;          return 0;
154  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20