|
#include <iostream> |
|
1 |
#include <string> |
#include <string> |
2 |
|
|
|
#include <cctype> |
|
|
#include <sstream> |
|
3 |
#include "daemon.h" |
#include "daemon.h" |
4 |
#include "common.h" |
#include "common.h" |
5 |
|
|
6 |
#include "GsmModem.h" |
#include "GsmModem.h" |
7 |
#include "SerialPort.h" |
#include "SerialPort.h" |
8 |
|
#include "SmsDaemon.h" |
9 |
|
|
|
#include "Plugin.h" |
|
|
#include "kbhit.h" |
|
|
|
|
|
#include "util.h" |
|
10 |
|
|
11 |
using namespace std; |
using namespace std; |
|
using namespace Util; |
|
12 |
|
|
13 |
|
|
14 |
void create_log_message(SMS& sms,bool hasPlugin) |
bool sms_exit(int exitcode) |
15 |
{ |
{ |
16 |
ostringstream os; |
if (Common::instance()->isDaemon) |
17 |
os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); |
daemonCleanup(); |
|
if (!hasPlugin) |
|
|
os << " -- PLUGIN NOT FOUND"; |
|
18 |
|
|
19 |
Common::instance()->logMessage(os.str()); |
exit(exitcode); |
20 |
} |
} |
21 |
|
|
|
|
|
|
void main_loop(GsmModem& modem) |
|
|
{ |
|
|
Common* cmn = Common::instance(); |
|
|
volatile bool& mainContinue = cmn->mainContinue; |
|
|
|
|
|
PluginManager& manager = cmn->pluginManager; |
|
|
|
|
|
mainContinue = true; |
|
|
|
|
|
while (mainContinue) |
|
|
{ |
|
|
vector<SMS> sms = modem.ReadSms(); |
|
|
|
|
|
for (unsigned int i=0; i<sms.size(); ++i) |
|
|
{ |
|
|
string cmd = GetSmsCommand(sms[i]); |
|
|
|
|
|
cmd = Util::str_tolower(cmd); |
|
|
|
|
|
Plugin* pl = manager.GetPlugin(cmd); |
|
|
|
|
|
create_log_message(sms[i], pl != 0); |
|
|
|
|
|
if (pl) |
|
|
{ |
|
|
pl->Execute(modem, sms[i]); |
|
|
} |
|
|
else |
|
|
{ |
|
|
modem.SendSms(sms[i].sender, "Unknown command!"); |
|
|
} |
|
|
|
|
|
modem.DeleteSms(sms[i].sms_index); |
|
|
} |
|
|
|
|
|
if (sms.size() == 0) |
|
|
Util::Sleep(10); |
|
|
|
|
|
if (cmn->isDebug && kbhit()) |
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
22 |
int main(int argc, char* argv[]) |
int main(int argc, char* argv[]) |
23 |
{ |
{ |
24 |
Common* cmn = Common::instance(); |
Common* cmn = Common::instance(); |
29 |
cmn->pidfile = "/var/run/smsdaemon.pid"; |
cmn->pidfile = "/var/run/smsdaemon.pid"; |
30 |
cmn->spooldir = "/var/spool/smsdaemon"; |
cmn->spooldir = "/var/spool/smsdaemon"; |
31 |
|
|
32 |
cmn->uid = 500; |
cmn->uid = 1000; |
33 |
cmn->gid = 500; |
cmn->gid = 1000; |
34 |
|
|
35 |
cmn->loadConfig(argc,argv); |
cmn->loadConfig(argc,argv); |
36 |
|
cmn->daemonStart = time(0); |
37 |
|
|
38 |
|
|
39 |
///////////////////// |
///////////////////// |
43 |
if (Common::instance()->isDaemon) |
if (Common::instance()->isDaemon) |
44 |
daemonize(); |
daemonize(); |
45 |
|
|
|
cmn->logMessage("--------------------------------"); |
|
|
|
|
|
cmn->pluginManager.LoadPlugins(); |
|
46 |
|
|
|
|
|
47 |
SerialPort port("/dev/ttyS1" ); |
SerialPort port("/dev/ttyS1" ); |
48 |
port.Open( SerialPort::BAUD_9600 ); |
try |
49 |
|
{ |
50 |
|
port.Open( SerialPort::BAUD_9600, |
51 |
|
SerialPort::CHAR_SIZE_8, |
52 |
|
SerialPort::PARITY_NONE, |
53 |
|
SerialPort::STOP_BITS_1, |
54 |
|
SerialPort::FLOW_CONTROL_HARD ); |
55 |
|
} |
56 |
|
catch(std::exception &e) |
57 |
|
{ |
58 |
|
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
59 |
|
sms_exit(1); |
60 |
|
} |
61 |
|
|
62 |
GsmModem modem(port); |
GsmModem modem(port); |
63 |
|
|
64 |
|
try |
65 |
|
{ |
66 |
|
modem.Init(); |
67 |
|
} |
68 |
|
catch (std::exception& e) |
69 |
|
{ |
70 |
|
cmn->logMessage( string("GsmModem Exception: ") + e.what() ); |
71 |
|
sms_exit(2); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
////////////////////////////////// |
////////////////////////////////// |
76 |
|
|
77 |
Common::instance()->logMessage("SMS daemon started"); |
SmsDaemon daemon(modem); |
78 |
|
|
79 |
modem.DeleteAllSms(); |
daemon.Start(); |
80 |
|
//returns here when main-loop exits |
81 |
|
|
82 |
main_loop(modem); |
if (cmn->isDaemon) |
83 |
|
daemonCleanup(); |
84 |
|
|
85 |
|
return 0; |
86 |
} |
} |
87 |
|
|