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

Diff of /smsdaemon/main.cpp

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

revision 118 by torben, Mon Dec 1 12:08:23 2008 UTC revision 175 by torben, Wed Dec 10 22:02:28 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 "GsmModem.h"  #include "ModemTransceiver.h"
10  #include "SerialPort.h"  #include "DebugTransceiver.h"
11  #include "SmsDaemon.h"  #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;
18    
# Line 20  bool sms_exit(int exitcode) Line 25  bool sms_exit(int exitcode)
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  {  {
72          Common* cmn = Common::instance();          Common* cmn = Common::instance();
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();
85                    
86          /////////////////////          bool res = config->Open( cmn->configFilePath ) ;
87                    if (!res) {
88          //Write a delimiter line in the logfile to seperate sessions                  cout << "Could not open config file:" << cmn->configFilePath << endl;
89                    return 1;
90            }
91    
92          if (Common::instance()->isDaemon)          Logger::initLog();
93                  daemonize();          
94    
95            /////////////////////
96            string transconf = config->GetValue("smsdaemon", "transceiver");
97                    
98          SerialPort port("/dev/ttyUSB0" );  
99          try          if (transconf == "internal")
100          {          {
101                  port.Open( SerialPort::BAUD_9600,                  openModemPort();
102                                     SerialPort::CHAR_SIZE_8,                  closeModemPort();
103                                     SerialPort::PARITY_NONE,          }
104                                     SerialPort::STOP_BITS_1,          else if ( transconf == "debug" || transconf == "smstools")
105                                     SerialPort::FLOW_CONTROL_HARD );          {
106                    //do nothing
107          }          }
108          catch(std::exception &e)          else
109          {          {
110                  cmn->logMessage( string("PortOpen Exception: ") + e.what() );                  Logger::logMessage( string("Invalid transceiver setting: ")+transconf);
111                  sms_exit(1);                  exit(1);
112          }          }
113    
114          GsmModem modem(port);          
115    
116          try          if (Common::instance()->isDaemon)
117          {          {
118                  modem.Init();                  lookup_uid_values();
119                    daemonize();
120                    cmn->daemonized = true;
121          }          }
122          catch (std::exception& e)  
123    
124            if (transconf == "internal")
125          {          {
126                  cmn->logMessage( string("GsmModem Exception: ") + e.what() );                  openModemPort();
127                  sms_exit(2);          }
128            else if (transconf =="smstools")
129            {
130                    transceiver = new SmsToolTransceiver();
131            }
132            else if (transconf == "debug")
133            {
134                    transceiver = new DebugTransceiver();
135          }          }
   
136    
137          //////////////////////////////////          //////////////////////////////////
138    
139          SmsDaemon daemon(modem);          SmsDaemon daemon(*transceiver);
140    
141          daemon.Start();          daemon.Start();
142          //returns here when main-loop exits          //returns here when main-loop exits
# Line 83  int main(int argc, char* argv[]) Line 144  int main(int argc, char* argv[])
144      if (cmn->isDaemon)      if (cmn->isDaemon)
145          daemonCleanup();          daemonCleanup();
146    
147            if (transconf == "builtin")
148            {
149                    closeModemPort();
150            }
151    
152          return 0;          return 0;
153  }  }
154    

Legend:
Removed from v.118  
changed lines
  Added in v.175

  ViewVC Help
Powered by ViewVC 1.1.20