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