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/SerialPort.h" |
9 |
|
#include "SmsDaemon.h" |
10 |
#include "Plugin.h" |
#include "ConfigFile.h" |
|
#include "kbhit.h" |
|
|
|
|
|
#include "util.h" |
|
11 |
|
|
12 |
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; |
|
|
|
|
|
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); |
|
13 |
|
|
|
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++; |
|
|
} |
|
|
|
|
|
cmn->taskManager.ExecuteTasks(); |
|
|
|
|
|
|
|
|
if (cmn->isDebug && kbhit()) |
|
|
break; |
|
|
|
|
|
Util::Sleep(10); |
|
|
|
|
|
} |
|
|
} |
|
14 |
|
|
15 |
bool sms_exit(int exitcode) |
bool sms_exit(int exitcode) |
16 |
{ |
{ |
20 |
exit(exitcode); |
exit(exitcode); |
21 |
} |
} |
22 |
|
|
23 |
|
|
24 |
int main(int argc, char* argv[]) |
int main(int argc, char* argv[]) |
25 |
{ |
{ |
26 |
Common* cmn = Common::instance(); |
Common* cmn = Common::instance(); |
31 |
cmn->pidfile = "/var/run/smsdaemon.pid"; |
cmn->pidfile = "/var/run/smsdaemon.pid"; |
32 |
cmn->spooldir = "/var/spool/smsdaemon"; |
cmn->spooldir = "/var/spool/smsdaemon"; |
33 |
|
|
|
cmn->uid = 1000; |
|
|
cmn->gid = 1000; |
|
34 |
|
|
35 |
cmn->loadConfig(argc,argv); |
cmn->loadConfig(argc,argv); |
36 |
cmn->daemonStart = time(0); |
cmn->daemonStart = time(0); |
37 |
|
|
38 |
|
ConfigFile* config = cmn->GetConfigfile(); |
39 |
|
|
40 |
|
bool res = config->Open( cmn->configFilePath ) ; |
41 |
|
if (!res) { |
42 |
|
cmn->logMessage(string("Could not open config file:") + cmn->configFilePath); |
43 |
|
return 1; |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
///////////////////// |
///////////////////// |
48 |
|
|
49 |
//Write a delimiter line in the logfile to seperate sessions |
//Write a delimiter line in the logfile to seperate sessions |
50 |
|
|
51 |
if (Common::instance()->isDaemon) |
if (Common::instance()->isDaemon) |
52 |
|
{ |
53 |
|
lookup_uid_values(); |
54 |
daemonize(); |
daemonize(); |
55 |
|
} |
56 |
|
|
|
cmn->logMessage("--------------------------------"); |
|
|
|
|
|
cmn->taskManager.LoadTasks(); |
|
|
cmn->pluginManager.LoadPlugins(); |
|
57 |
|
|
58 |
SerialPort port("/dev/ttyS1" ); |
SerialPort port( config->GetValue("smsdaemon","serialport") ); |
59 |
try |
try |
60 |
{ |
{ |
61 |
port.Open( SerialPort::BAUD_9600, |
port.Open( SerialPort::BAUD_9600, |
85 |
|
|
86 |
////////////////////////////////// |
////////////////////////////////// |
87 |
|
|
88 |
cmn->logMessage("SMS daemon started"); |
SmsDaemon daemon(modem); |
|
|
|
|
modem.DeleteAllSms(); |
|
|
|
|
|
main_loop(modem); |
|
89 |
|
|
90 |
cmn->logMessage( cmn->getStatusMessage() ); |
daemon.Start(); |
91 |
|
//returns here when main-loop exits |
92 |
|
|
93 |
if (cmn->isDaemon) |
if (cmn->isDaemon) |
94 |
daemonCleanup(); |
daemonCleanup(); |