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 |
|
|
if (sms.size() == 0) |
68 |
|
|
Util::Sleep(10); |
69 |
|
|
|
70 |
|
|
if (cmn->isDebug && kbhit()) |
71 |
|
|
break; |
72 |
|
|
|
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
torben |
57 |
bool sms_exit(int exitcode) |
77 |
|
|
{ |
78 |
|
|
if (Common::instance()->isDaemon) |
79 |
|
|
daemonCleanup(); |
80 |
torben |
26 |
|
81 |
torben |
57 |
exit(exitcode); |
82 |
|
|
} |
83 |
|
|
|
84 |
torben |
26 |
int main(int argc, char* argv[]) |
85 |
|
|
{ |
86 |
|
|
Common* cmn = Common::instance(); |
87 |
|
|
|
88 |
|
|
//Set default values |
89 |
|
|
|
90 |
|
|
cmn->setLogfile( "/var/log/smsdaemon.log"); |
91 |
|
|
cmn->pidfile = "/var/run/smsdaemon.pid"; |
92 |
|
|
cmn->spooldir = "/var/spool/smsdaemon"; |
93 |
|
|
|
94 |
torben |
29 |
cmn->uid = 1000; |
95 |
|
|
cmn->gid = 1000; |
96 |
torben |
26 |
|
97 |
|
|
cmn->loadConfig(argc,argv); |
98 |
torben |
36 |
cmn->daemonStart = time(0); |
99 |
torben |
26 |
|
100 |
|
|
|
101 |
|
|
///////////////////// |
102 |
|
|
|
103 |
|
|
//Write a delimiter line in the logfile to seperate sessions |
104 |
|
|
|
105 |
|
|
if (Common::instance()->isDaemon) |
106 |
|
|
daemonize(); |
107 |
|
|
|
108 |
|
|
cmn->logMessage("--------------------------------"); |
109 |
|
|
|
110 |
|
|
cmn->pluginManager.LoadPlugins(); |
111 |
|
|
|
112 |
torben |
29 |
SerialPort port("/dev/ttyS1" ); |
113 |
|
|
try |
114 |
|
|
{ |
115 |
torben |
33 |
port.Open( SerialPort::BAUD_9600, |
116 |
|
|
SerialPort::CHAR_SIZE_8, |
117 |
|
|
SerialPort::PARITY_NONE, |
118 |
|
|
SerialPort::STOP_BITS_1, |
119 |
|
|
SerialPort::FLOW_CONTROL_HARD ); |
120 |
torben |
29 |
} |
121 |
|
|
catch(std::exception &e) |
122 |
|
|
{ |
123 |
|
|
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
124 |
torben |
57 |
sms_exit(1); |
125 |
torben |
29 |
} |
126 |
torben |
26 |
|
127 |
|
|
GsmModem modem(port); |
128 |
|
|
|
129 |
torben |
57 |
try |
130 |
|
|
{ |
131 |
|
|
modem.Init(); |
132 |
|
|
} |
133 |
|
|
catch (std::exception& e) |
134 |
|
|
{ |
135 |
|
|
cmn->logMessage( string("GsmModem Exception: ") + e.what() ); |
136 |
|
|
sms_exit(2); |
137 |
|
|
} |
138 |
torben |
26 |
|
139 |
torben |
57 |
|
140 |
torben |
26 |
////////////////////////////////// |
141 |
|
|
|
142 |
torben |
29 |
cmn->logMessage("SMS daemon started"); |
143 |
torben |
26 |
|
144 |
|
|
modem.DeleteAllSms(); |
145 |
|
|
|
146 |
|
|
main_loop(modem); |
147 |
|
|
|
148 |
torben |
36 |
cmn->logMessage( cmn->getStatusMessage() ); |
149 |
|
|
|
150 |
torben |
30 |
if (cmn->isDaemon) |
151 |
|
|
daemonCleanup(); |
152 |
|
|
|
153 |
|
|
return 0; |
154 |
torben |
26 |
} |