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