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 "DebugTransceiver.h" |
9 |
|
#include "SmsToolTransceiver.h" |
10 |
#include "Plugin.h" |
|
11 |
#include "kbhit.h" |
#include "serialport/SerialPort.h" |
12 |
|
#include "SmsDaemon.h" |
13 |
#include "util.h" |
#include "ConfigFile.h" |
14 |
|
|
15 |
using namespace std; |
using namespace std; |
|
using namespace Util; |
|
16 |
|
|
17 |
|
|
18 |
void create_log_message(SMS& sms,bool hasPlugin) |
bool sms_exit(int exitcode) |
19 |
{ |
{ |
20 |
ostringstream os; |
if (Common::instance()->isDaemon) |
21 |
os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms); |
daemonCleanup(); |
|
if (!hasPlugin) |
|
|
os << " -- PLUGIN NOT FOUND"; |
|
22 |
|
|
23 |
Common::instance()->logMessage(os.str()); |
exit(exitcode); |
24 |
} |
} |
25 |
|
|
26 |
|
SerialPort* port = 0; |
27 |
|
ISmsTransceiver* transceiver = 0; |
28 |
|
|
29 |
void main_loop(GsmModem& modem) |
void openModemPort() |
30 |
{ |
{ |
31 |
Common* cmn = Common::instance(); |
Common* cmn = Common::instance(); |
32 |
volatile bool& mainContinue = cmn->mainContinue; |
ConfigFile* config = cmn->GetConfigfile(); |
|
|
|
|
PluginManager& manager = cmn->pluginManager; |
|
33 |
|
|
34 |
mainContinue = true; |
port = new SerialPort ( config->GetValue("smsdaemon","serialport") ); |
35 |
|
try |
|
while (mainContinue) |
|
36 |
{ |
{ |
37 |
vector<SMS> sms = modem.ReadSms(); |
port->Open( SerialPort::BAUD_9600, |
38 |
|
SerialPort::CHAR_SIZE_8, |
39 |
for (unsigned int i=0; i<sms.size(); ++i) |
SerialPort::PARITY_NONE, |
40 |
{ |
SerialPort::STOP_BITS_1, |
41 |
string cmd = GetSmsCommand(sms[i]); |
SerialPort::FLOW_CONTROL_HARD ); |
42 |
|
} |
43 |
cmd = Util::str_tolower(cmd); |
catch(std::exception &e) |
44 |
|
{ |
45 |
Plugin* pl = manager.GetPlugin(cmd); |
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
46 |
|
sms_exit(1); |
47 |
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++; |
|
|
} |
|
|
|
|
|
cmn->taskManager.ExecuteTasks(); |
|
|
|
|
|
|
|
|
if (cmn->isDebug && kbhit()) |
|
|
break; |
|
48 |
|
|
49 |
Util::Sleep(10); |
transceiver = new ModemTransceiver(*port); |
50 |
|
|
51 |
|
try |
52 |
|
{ |
53 |
|
((ModemTransceiver*)transceiver)->Init(); |
54 |
|
} |
55 |
|
catch (std::exception& e) |
56 |
|
{ |
57 |
|
cmn->logMessage( string("ModemTransceiver Exception: ") + e.what() ); |
58 |
|
sms_exit(2); |
59 |
} |
} |
60 |
} |
} |
61 |
|
void closeModemPort() |
|
bool sms_exit(int exitcode) |
|
62 |
{ |
{ |
63 |
if (Common::instance()->isDaemon) |
port->Close(); |
64 |
daemonCleanup(); |
delete port; |
65 |
|
delete transceiver; |
|
exit(exitcode); |
|
66 |
} |
} |
67 |
|
|
68 |
int main(int argc, char* argv[]) |
int main(int argc, char* argv[]) |
75 |
cmn->pidfile = "/var/run/smsdaemon.pid"; |
cmn->pidfile = "/var/run/smsdaemon.pid"; |
76 |
cmn->spooldir = "/var/spool/smsdaemon"; |
cmn->spooldir = "/var/spool/smsdaemon"; |
77 |
|
|
|
cmn->uid = 1000; |
|
|
cmn->gid = 1000; |
|
78 |
|
|
79 |
cmn->loadConfig(argc,argv); |
cmn->loadConfig(argc,argv); |
80 |
cmn->daemonStart = time(0); |
cmn->daemonStart = time(0); |
81 |
|
|
82 |
|
ConfigFile* config = cmn->GetConfigfile(); |
83 |
|
|
84 |
///////////////////// |
bool res = config->Open( cmn->configFilePath ) ; |
85 |
|
if (!res) { |
86 |
|
cmn->logMessage(string("Could not open config file:") + cmn->configFilePath); |
87 |
|
return 1; |
88 |
|
} |
89 |
|
|
|
//Write a delimiter line in the logfile to seperate sessions |
|
90 |
|
|
91 |
if (Common::instance()->isDaemon) |
///////////////////// |
92 |
daemonize(); |
string transconf = config->GetValue("smsdaemon", "transceiver"); |
|
|
|
|
cmn->logMessage("--------------------------------"); |
|
|
|
|
|
cmn->taskManager.LoadTasks(); |
|
|
cmn->pluginManager.LoadPlugins(); |
|
93 |
|
|
94 |
SerialPort port("/dev/ttyS1" ); |
|
95 |
try |
if (transconf == "internal") |
96 |
{ |
{ |
97 |
port.Open( SerialPort::BAUD_9600, |
openModemPort(); |
98 |
SerialPort::CHAR_SIZE_8, |
closeModemPort(); |
99 |
SerialPort::PARITY_NONE, |
} |
100 |
SerialPort::STOP_BITS_1, |
else if ( transconf == "debug" || transconf == "smstools") |
101 |
SerialPort::FLOW_CONTROL_HARD ); |
{ |
102 |
|
//do nothing |
103 |
} |
} |
104 |
catch(std::exception &e) |
else |
105 |
{ |
{ |
106 |
cmn->logMessage( string("PortOpen Exception: ") + e.what() ); |
cmn->logMessage( string("Invalid transceiver setting: ")+transconf); |
107 |
sms_exit(1); |
exit(1); |
108 |
} |
} |
109 |
|
|
110 |
GsmModem modem(port); |
|
111 |
|
|
112 |
try |
if (Common::instance()->isDaemon) |
113 |
{ |
{ |
114 |
modem.Init(); |
lookup_uid_values(); |
115 |
|
daemonize(); |
116 |
|
cmn->daemonized = true; |
117 |
} |
} |
118 |
catch (std::exception& e) |
|
119 |
|
|
120 |
|
if (transconf == "internal") |
121 |
{ |
{ |
122 |
cmn->logMessage( string("GsmModem Exception: ") + e.what() ); |
openModemPort(); |
123 |
sms_exit(2); |
} |
124 |
|
else if (transconf =="smstools") |
125 |
|
{ |
126 |
|
transceiver = new SmsToolTransceiver(); |
127 |
|
} |
128 |
|
else if (transconf == "debug") |
129 |
|
{ |
130 |
|
transceiver = new DebugTransceiver(); |
131 |
} |
} |
|
|
|
132 |
|
|
133 |
////////////////////////////////// |
////////////////////////////////// |
134 |
|
|
135 |
cmn->logMessage("SMS daemon started"); |
SmsDaemon daemon(*transceiver); |
|
|
|
|
modem.DeleteAllSms(); |
|
136 |
|
|
137 |
main_loop(modem); |
daemon.Start(); |
138 |
|
//returns here when main-loop exits |
|
cmn->logMessage( cmn->getStatusMessage() ); |
|
139 |
|
|
140 |
if (cmn->isDaemon) |
if (cmn->isDaemon) |
141 |
daemonCleanup(); |
daemonCleanup(); |
142 |
|
|
143 |
|
if (transconf == "builtin") |
144 |
|
{ |
145 |
|
closeModemPort(); |
146 |
|
} |
147 |
|
|
148 |
return 0; |
return 0; |
149 |
} |
} |
150 |
|
|