|
#include <iostream> |
|
1 |
#include <string> |
#include <string> |
2 |
|
#include <stdlib.h> |
3 |
|
|
|
#include <cctype> |
|
|
#include <sstream> |
|
4 |
#include "daemon.h" |
#include "daemon.h" |
5 |
#include "common.h" |
#include "common.h" |
6 |
|
|
7 |
#include "GsmModem.h" |
#include "GsmModem.h" |
8 |
#include "SerialPort.h" |
#include "SerialPort.h" |
9 |
|
#include "SmsDaemon.h" |
10 |
|
|
|
#include "Plugin.h" |
|
|
#include "kbhit.h" |
|
|
|
|
|
#include "util.h" |
|
11 |
|
|
12 |
using namespace std; |
using namespace std; |
|
using namespace Util; |
|
13 |
|
|
14 |
|
|
15 |
void create_log_message(SMS& sms,bool hasPlugin) |
bool sms_exit(int exitcode) |
16 |
{ |
{ |
17 |
ostringstream os; |
if (Common::instance()->isDaemon) |
18 |
os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); |
daemonCleanup(); |
|
if (!hasPlugin) |
|
|
os << " -- PLUGIN NOT FOUND"; |
|
|
|
|
|
Common::instance()->logMessage(os.str()); |
|
|
} |
|
|
|
|
|
|
|
|
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; |
|
19 |
|
|
20 |
} |
exit(exitcode); |
21 |
} |
} |
22 |
|
|
|
|
|
23 |
int main(int argc, char* argv[]) |
int main(int argc, char* argv[]) |
24 |
{ |
{ |
25 |
Common* cmn = Common::instance(); |
Common* cmn = Common::instance(); |
34 |
cmn->gid = 1000; |
cmn->gid = 1000; |
35 |
|
|
36 |
cmn->loadConfig(argc,argv); |
cmn->loadConfig(argc,argv); |
37 |
|
cmn->daemonStart = time(0); |
38 |
|
|
39 |
|
|
40 |
///////////////////// |
///////////////////// |
44 |
if (Common::instance()->isDaemon) |
if (Common::instance()->isDaemon) |
45 |
daemonize(); |
daemonize(); |
46 |
|
|
|
cmn->logMessage("--------------------------------"); |
|
|
|
|
|
cmn->pluginManager.LoadPlugins(); |
|
47 |
|
|
48 |
SerialPort port("/dev/ttyS1" ); |
SerialPort port("/dev/ttyS1" ); |
49 |
try |
try |
57 |
catch(std::exception &e) |
catch(std::exception &e) |
58 |
{ |
{ |
59 |
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
60 |
exit(1); |
sms_exit(1); |
61 |
} |
} |
62 |
|
|
63 |
GsmModem modem(port); |
GsmModem modem(port); |
64 |
|
|
65 |
|
try |
66 |
|
{ |
67 |
|
modem.Init(); |
68 |
|
} |
69 |
|
catch (std::exception& e) |
70 |
|
{ |
71 |
|
cmn->logMessage( string("GsmModem Exception: ") + e.what() ); |
72 |
|
sms_exit(2); |
73 |
|
} |
74 |
|
|
|
////////////////////////////////// |
|
75 |
|
|
76 |
cmn->logMessage("SMS daemon started"); |
////////////////////////////////// |
77 |
|
|
78 |
modem.DeleteAllSms(); |
SmsDaemon daemon(modem); |
79 |
|
|
80 |
main_loop(modem); |
daemon.Start(); |
81 |
|
//returns here when main-loop exits |
82 |
|
|
83 |
if (cmn->isDaemon) |
if (cmn->isDaemon) |
84 |
daemonCleanup(); |
daemonCleanup(); |