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; |
|
|
|
|
|
|
|
|
void create_log_message(SMS& sms,bool hasPlugin) |
|
|
{ |
|
|
ostringstream os; |
|
|
os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); |
|
|
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; |
|
12 |
|
|
|
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!", false); |
|
|
} |
|
|
|
|
|
modem.DeleteSms(sms[i].sms_index); |
|
|
cmn->smsCounter.incomming++; |
|
|
} |
|
|
|
|
|
if (sms.size() == 0) |
|
|
Util::Sleep(10); |
|
|
|
|
|
if (cmn->isDebug && kbhit()) |
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
13 |
|
|
14 |
bool sms_exit(int exitcode) |
bool sms_exit(int exitcode) |
15 |
{ |
{ |
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 |
try |
try |
74 |
|
|
75 |
////////////////////////////////// |
////////////////////////////////// |
76 |
|
|
77 |
cmn->logMessage("SMS daemon started"); |
SmsDaemon daemon(modem); |
|
|
|
|
modem.DeleteAllSms(); |
|
|
|
|
|
main_loop(modem); |
|
78 |
|
|
79 |
cmn->logMessage( cmn->getStatusMessage() ); |
daemon.Start(); |
80 |
|
//returns here when main-loop exits |
81 |
|
|
82 |
if (cmn->isDaemon) |
if (cmn->isDaemon) |
83 |
daemonCleanup(); |
daemonCleanup(); |