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