|
#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 "ModemTransceiver.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; |
|
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"; |
|
19 |
|
|
20 |
Common::instance()->logMessage(os.str()); |
exit(exitcode); |
|
} |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
21 |
} |
} |
22 |
|
|
23 |
|
|
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); |
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->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, |
62 |
|
SerialPort::CHAR_SIZE_8, |
63 |
|
SerialPort::PARITY_NONE, |
64 |
|
SerialPort::STOP_BITS_1, |
65 |
|
SerialPort::FLOW_CONTROL_HARD ); |
66 |
} |
} |
67 |
catch(std::exception &e) |
catch(std::exception &e) |
68 |
{ |
{ |
69 |
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
70 |
exit(1); |
sms_exit(1); |
71 |
} |
} |
72 |
|
|
73 |
GsmModem modem(port); |
ModemTransceiver modem(port); |
74 |
|
|
75 |
|
try |
76 |
|
{ |
77 |
|
modem.Init(); |
78 |
|
} |
79 |
|
catch (std::exception& e) |
80 |
|
{ |
81 |
|
cmn->logMessage( string("ModemTransceiver Exception: ") + e.what() ); |
82 |
|
sms_exit(2); |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
////////////////////////////////// |
////////////////////////////////// |
87 |
|
|
88 |
cmn->logMessage("SMS daemon started"); |
SmsDaemon daemon(modem); |
89 |
|
|
90 |
modem.DeleteAllSms(); |
daemon.Start(); |
91 |
|
//returns here when main-loop exits |
92 |
|
|
93 |
main_loop(modem); |
if (cmn->isDaemon) |
94 |
|
daemonCleanup(); |
95 |
|
|
96 |
|
return 0; |
97 |
} |
} |
98 |
|
|