1 |
torben |
26 |
#include <string> |
2 |
|
|
|
3 |
|
|
#include <cctype> |
4 |
|
|
#include <sstream> |
5 |
|
|
#include "daemon.h" |
6 |
|
|
#include "common.h" |
7 |
|
|
|
8 |
|
|
#include "GsmModem.h" |
9 |
|
|
#include "SerialPort.h" |
10 |
|
|
|
11 |
|
|
#include "Plugin.h" |
12 |
|
|
#include "kbhit.h" |
13 |
|
|
|
14 |
|
|
#include "util.h" |
15 |
|
|
|
16 |
|
|
using namespace std; |
17 |
|
|
using namespace Util; |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
void create_log_message(SMS& sms,bool hasPlugin) |
21 |
|
|
{ |
22 |
|
|
ostringstream os; |
23 |
|
|
os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); |
24 |
|
|
if (!hasPlugin) |
25 |
|
|
os << " -- PLUGIN NOT FOUND"; |
26 |
|
|
|
27 |
|
|
Common::instance()->logMessage(os.str()); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
|
31 |
|
|
void main_loop(GsmModem& modem) |
32 |
|
|
{ |
33 |
|
|
Common* cmn = Common::instance(); |
34 |
|
|
volatile bool& mainContinue = cmn->mainContinue; |
35 |
|
|
|
36 |
|
|
PluginManager& manager = cmn->pluginManager; |
37 |
|
|
|
38 |
|
|
mainContinue = true; |
39 |
|
|
|
40 |
|
|
while (mainContinue) |
41 |
|
|
{ |
42 |
|
|
vector<SMS> sms = modem.ReadSms(); |
43 |
|
|
|
44 |
|
|
for (unsigned int i=0; i<sms.size(); ++i) |
45 |
|
|
{ |
46 |
|
|
string cmd = GetSmsCommand(sms[i]); |
47 |
|
|
|
48 |
|
|
cmd = Util::str_tolower(cmd); |
49 |
|
|
|
50 |
|
|
Plugin* pl = manager.GetPlugin(cmd); |
51 |
|
|
|
52 |
|
|
create_log_message(sms[i], pl != 0); |
53 |
|
|
|
54 |
|
|
if (pl) |
55 |
|
|
{ |
56 |
|
|
pl->Execute(modem, sms[i]); |
57 |
|
|
} |
58 |
|
|
else |
59 |
|
|
{ |
60 |
torben |
63 |
modem.SendSms(sms[i].sender, "Unknown command!", false); |
61 |
torben |
26 |
} |
62 |
|
|
|
63 |
|
|
modem.DeleteSms(sms[i].sms_index); |
64 |
torben |
36 |
cmn->smsCounter.incomming++; |
65 |
torben |
26 |
} |
66 |
|
|
|
67 |
torben |
82 |
cmn->taskManager.ExecuteTasks(); |
68 |
torben |
26 |
|
69 |
torben |
82 |
|
70 |
torben |
26 |
if (cmn->isDebug && kbhit()) |
71 |
|
|
break; |
72 |
|
|
|
73 |
torben |
82 |
Util::Sleep(10); |
74 |
|
|
|
75 |
torben |
26 |
} |
76 |
|
|
} |
77 |
|
|
|
78 |
torben |
57 |
bool sms_exit(int exitcode) |
79 |
|
|
{ |
80 |
|
|
if (Common::instance()->isDaemon) |
81 |
|
|
daemonCleanup(); |
82 |
torben |
26 |
|
83 |
torben |
57 |
exit(exitcode); |
84 |
|
|
} |
85 |
|
|
|
86 |
torben |
26 |
int main(int argc, char* argv[]) |
87 |
|
|
{ |
88 |
|
|
Common* cmn = Common::instance(); |
89 |
|
|
|
90 |
|
|
//Set default values |
91 |
|
|
|
92 |
|
|
cmn->setLogfile( "/var/log/smsdaemon.log"); |
93 |
|
|
cmn->pidfile = "/var/run/smsdaemon.pid"; |
94 |
|
|
cmn->spooldir = "/var/spool/smsdaemon"; |
95 |
|
|
|
96 |
torben |
29 |
cmn->uid = 1000; |
97 |
|
|
cmn->gid = 1000; |
98 |
torben |
26 |
|
99 |
|
|
cmn->loadConfig(argc,argv); |
100 |
torben |
36 |
cmn->daemonStart = time(0); |
101 |
torben |
26 |
|
102 |
|
|
|
103 |
|
|
///////////////////// |
104 |
|
|
|
105 |
|
|
//Write a delimiter line in the logfile to seperate sessions |
106 |
|
|
|
107 |
|
|
if (Common::instance()->isDaemon) |
108 |
|
|
daemonize(); |
109 |
|
|
|
110 |
|
|
cmn->logMessage("--------------------------------"); |
111 |
|
|
|
112 |
torben |
82 |
cmn->taskManager.LoadTasks(); |
113 |
torben |
26 |
cmn->pluginManager.LoadPlugins(); |
114 |
|
|
|
115 |
torben |
29 |
SerialPort port("/dev/ttyS1" ); |
116 |
|
|
try |
117 |
|
|
{ |
118 |
torben |
33 |
port.Open( SerialPort::BAUD_9600, |
119 |
|
|
SerialPort::CHAR_SIZE_8, |
120 |
|
|
SerialPort::PARITY_NONE, |
121 |
|
|
SerialPort::STOP_BITS_1, |
122 |
|
|
SerialPort::FLOW_CONTROL_HARD ); |
123 |
torben |
29 |
} |
124 |
|
|
catch(std::exception &e) |
125 |
|
|
{ |
126 |
|
|
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
127 |
torben |
57 |
sms_exit(1); |
128 |
torben |
29 |
} |
129 |
torben |
26 |
|
130 |
|
|
GsmModem modem(port); |
131 |
|
|
|
132 |
torben |
57 |
try |
133 |
|
|
{ |
134 |
|
|
modem.Init(); |
135 |
|
|
} |
136 |
|
|
catch (std::exception& e) |
137 |
|
|
{ |
138 |
|
|
cmn->logMessage( string("GsmModem Exception: ") + e.what() ); |
139 |
|
|
sms_exit(2); |
140 |
|
|
} |
141 |
torben |
26 |
|
142 |
torben |
57 |
|
143 |
torben |
26 |
////////////////////////////////// |
144 |
|
|
|
145 |
torben |
29 |
cmn->logMessage("SMS daemon started"); |
146 |
torben |
26 |
|
147 |
|
|
modem.DeleteAllSms(); |
148 |
torben |
85 |
|
149 |
|
|
try |
150 |
|
|
{ |
151 |
|
|
main_loop(modem); |
152 |
|
|
} |
153 |
|
|
catch (std::exception& e) |
154 |
|
|
{ |
155 |
|
|
cmn->logMessage( e.what() ); |
156 |
|
|
} |
157 |
|
|
catch (...) |
158 |
|
|
{ |
159 |
|
|
cmn->logMessage( "Caught unknown exception" ); |
160 |
|
|
} |
161 |
torben |
26 |
|
162 |
torben |
36 |
cmn->logMessage( cmn->getStatusMessage() ); |
163 |
|
|
|
164 |
torben |
30 |
if (cmn->isDaemon) |
165 |
|
|
daemonCleanup(); |
166 |
|
|
|
167 |
|
|
return 0; |
168 |
torben |
26 |
} |