1 |
#include <string> |
2 |
#include <stdlib.h> |
3 |
|
4 |
#include "daemon.h" |
5 |
#include "common.h" |
6 |
#include "Logger.h" |
7 |
|
8 |
#include "ModemTransceiver.h" |
9 |
#include "DebugTransceiver.h" |
10 |
#include "SmsToolTransceiver.h" |
11 |
|
12 |
#include "serialport/SerialPort.h" |
13 |
#include "SmsDaemon.h" |
14 |
#include "ConfigFile.h" |
15 |
|
16 |
using namespace std; |
17 |
|
18 |
|
19 |
bool sms_exit(int exitcode) |
20 |
{ |
21 |
if (Common::instance()->isDaemon) |
22 |
daemonCleanup(); |
23 |
|
24 |
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[]) |
70 |
{ |
71 |
Common* cmn = Common::instance(); |
72 |
|
73 |
//Set default values |
74 |
|
75 |
Logger::setLogfile( "/var/log/smsdaemon.log"); |
76 |
cmn->pidfile = "/var/run/smsdaemon.pid"; |
77 |
cmn->spooldir = "/var/spool/smsdaemon"; |
78 |
|
79 |
|
80 |
cmn->loadConfig(argc,argv); |
81 |
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 |
|
91 |
|
92 |
///////////////////// |
93 |
string transconf = config->GetValue("smsdaemon", "transceiver"); |
94 |
|
95 |
|
96 |
if (transconf == "internal") |
97 |
{ |
98 |
openModemPort(); |
99 |
closeModemPort(); |
100 |
} |
101 |
else if ( transconf == "debug" || transconf == "smstools") |
102 |
{ |
103 |
//do nothing |
104 |
} |
105 |
else |
106 |
{ |
107 |
Logger::logMessage( string("Invalid transceiver setting: ")+transconf); |
108 |
exit(1); |
109 |
} |
110 |
|
111 |
|
112 |
|
113 |
if (Common::instance()->isDaemon) |
114 |
{ |
115 |
lookup_uid_values(); |
116 |
daemonize(); |
117 |
cmn->daemonized = true; |
118 |
} |
119 |
|
120 |
|
121 |
if (transconf == "internal") |
122 |
{ |
123 |
openModemPort(); |
124 |
} |
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(*transceiver); |
137 |
|
138 |
daemon.Start(); |
139 |
//returns here when main-loop exits |
140 |
|
141 |
if (cmn->isDaemon) |
142 |
daemonCleanup(); |
143 |
|
144 |
if (transconf == "builtin") |
145 |
{ |
146 |
closeModemPort(); |
147 |
} |
148 |
|
149 |
return 0; |
150 |
} |