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

Diff of /smsdaemon/main.cpp

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

revision 88 by torben, Mon Jun 16 09:04:05 2008 UTC revision 158 by torben, Mon Dec 8 21:49:49 2008 UTC
# Line 1  Line 1 
1  #include <string>  #include <string>
2    #include <stdlib.h>
3    
4  #include "daemon.h"  #include "daemon.h"
5  #include "common.h"  #include "Common.h"
6    #include "Logger.h"
7    
8  #include "GsmModem.h"  #include "ModemTransceiver.h"
9  #include "SerialPort.h"  #include "DebugTransceiver.h"
10  #include "SmsDaemon.h"  #include "SmsToolTransceiver.h"
11    
12    #include "serialport/SerialPort.h"
13    #include "SmsDaemon.h"
14    #include "ConfigFile.h"
15    
16  using namespace std;  using namespace std;
17    
# Line 19  bool sms_exit(int exitcode) Line 24  bool sms_exit(int exitcode)
24          exit(exitcode);          exit(exitcode);
25  }  }
26    
27    SerialPort* port = 0;
28    ISmsTransceiver* transceiver = 0;
29    
30    void openModemPort()
31    {
32            Common* cmn = Common::instance();
33            ConfigFile* config = cmn->GetConfigfile();
34    
35            port = new SerialPort ( config->GetValue("smsdaemon","serialport") );
36            try
37            {
38                    port->Open( SerialPort::BAUD_9600,
39                                       SerialPort::CHAR_SIZE_8,
40                                       SerialPort::PARITY_NONE,
41                                       SerialPort::STOP_BITS_1,
42                                       SerialPort::FLOW_CONTROL_HARD );
43            }
44            catch(std::exception &e)
45            {
46                    Logger::logMessage( string("PortOpen Exception: ") + e.what() );
47                    sms_exit(1);
48            }
49    
50            transceiver = new ModemTransceiver(*port);
51    
52            try
53            {
54                    ((ModemTransceiver*)transceiver)->Init();
55            }
56            catch (std::exception& e)
57            {
58                    Logger::logMessage( string("ModemTransceiver Exception: ") + e.what() );
59                    sms_exit(2);
60            }
61    }
62    void closeModemPort()
63    {
64            port->Close();
65            delete port;
66            delete transceiver;
67    }
68    
69  int main(int argc, char* argv[])  int main(int argc, char* argv[])
70  {  {
71          Common* cmn = Common::instance();          Common* cmn = Common::instance();
72    
73          //Set default values          //Set default values
74    
75          cmn->setLogfile( "/var/log/smsdaemon.log");          Logger::setLogfile( "/var/log/smsdaemon.log");
76          cmn->pidfile = "/var/run/smsdaemon.pid";          cmn->pidfile = "/var/run/smsdaemon.pid";
77          cmn->spooldir = "/var/spool/smsdaemon";          cmn->spooldir = "/var/spool/smsdaemon";
78    
         cmn->uid = 1000;  
         cmn->gid = 1000;  
79    
80          cmn->loadConfig(argc,argv);          cmn->loadConfig(argc,argv);
81          cmn->daemonStart = time(0);          cmn->daemonStart = time(0);
82    
83            ConfigFile* config = cmn->GetConfigfile();
84                    
85          /////////////////////          bool res = config->Open( cmn->configFilePath ) ;
86            if (!res) {
87                    Logger::logMessage(string("Could not open config file:") + cmn->configFilePath);
88                    return 1;
89            }
90                    
         //Write a delimiter line in the logfile to seperate sessions  
   
         if (Common::instance()->isDaemon)  
                 daemonize();  
91    
92            /////////////////////
93            string transconf = config->GetValue("smsdaemon", "transceiver");
94                    
95          SerialPort port("/dev/ttyS1" );  
96          try          if (transconf == "internal")
97          {          {
98                  port.Open( SerialPort::BAUD_9600,                  openModemPort();
99                                     SerialPort::CHAR_SIZE_8,                  closeModemPort();
100                                     SerialPort::PARITY_NONE,          }
101                                     SerialPort::STOP_BITS_1,          else if ( transconf == "debug" || transconf == "smstools")
102                                     SerialPort::FLOW_CONTROL_HARD );          {
103                    //do nothing
104          }          }
105          catch(std::exception &e)          else
106          {          {
107                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
108                  sms_exit(1);                  exit(1);
109          }          }
110    
111          GsmModem modem(port);          
112    
113          try          if (Common::instance()->isDaemon)
114          {          {
115                  modem.Init();                  lookup_uid_values();
116                    daemonize();
117                    cmn->daemonized = true;
118          }          }
119          catch (std::exception& e)  
120    
121            if (transconf == "internal")
122          {          {
123                  cmn->logMessage( string("GsmModem Exception: ") + e.what() );                  openModemPort();
124                  sms_exit(2);          }
125            else if (transconf =="smstools")
126            {
127                    transceiver = new SmsToolTransceiver();
128            }
129            else if (transconf == "debug")
130            {
131                    transceiver = new DebugTransceiver();
132          }          }
   
133    
134          //////////////////////////////////          //////////////////////////////////
135    
136          SmsDaemon daemon(modem);          SmsDaemon daemon(*transceiver);
137    
138          daemon.Start();          daemon.Start();
139          //returns here when main-loop exits          //returns here when main-loop exits
# Line 82  int main(int argc, char* argv[]) Line 141  int main(int argc, char* argv[])
141      if (cmn->isDaemon)      if (cmn->isDaemon)
142          daemonCleanup();          daemonCleanup();
143    
144            if (transconf == "builtin")
145            {
146                    closeModemPort();
147            }
148    
149          return 0;          return 0;
150  }  }
151    

Legend:
Removed from v.88  
changed lines
  Added in v.158

  ViewVC Help
Powered by ViewVC 1.1.20