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