/[projects]/smsdaemon/main.cpp
ViewVC logotype

Contents of /smsdaemon/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (show annotations) (download)
Fri Jun 13 10:10:06 2008 UTC (15 years, 11 months ago) by torben
File size: 2651 byte(s)
Make gsmmodem::readsms use pdu mode 

Cleaned up some unneeded cout and <iostream>


1 #include <string>
2
3 #include <cctype>
4 #include <sstream>
5 #include "daemon.h"
6 #include "common.h"
7
8 #include "GsmModem.h"
9 #include "SerialPort.h"
10
11 #include "Plugin.h"
12 #include "kbhit.h"
13
14 #include "util.h"
15
16 using namespace std;
17 using namespace Util;
18
19
20 void create_log_message(SMS& sms,bool hasPlugin)
21 {
22 ostringstream os;
23 os << "Recieved sms from " << sms.sender << " ; command=" << GetSmsCommand(sms);
24 if (!hasPlugin)
25 os << " -- PLUGIN NOT FOUND";
26
27 Common::instance()->logMessage(os.str());
28 }
29
30
31 void main_loop(GsmModem& modem)
32 {
33 Common* cmn = Common::instance();
34 volatile bool& mainContinue = cmn->mainContinue;
35
36 PluginManager& manager = cmn->pluginManager;
37
38 mainContinue = true;
39
40 while (mainContinue)
41 {
42 vector<SMS> sms = modem.ReadSms();
43
44 for (unsigned int i=0; i<sms.size(); ++i)
45 {
46 string cmd = GetSmsCommand(sms[i]);
47
48 cmd = Util::str_tolower(cmd);
49
50 Plugin* pl = manager.GetPlugin(cmd);
51
52 create_log_message(sms[i], pl != 0);
53
54 if (pl)
55 {
56 pl->Execute(modem, sms[i]);
57 }
58 else
59 {
60 modem.SendSms(sms[i].sender, "Unknown command!", false);
61 }
62
63 modem.DeleteSms(sms[i].sms_index);
64 cmn->smsCounter.incomming++;
65 }
66
67 if (sms.size() == 0)
68 Util::Sleep(10);
69
70 if (cmn->isDebug && kbhit())
71 break;
72
73 }
74 }
75
76 bool sms_exit(int exitcode)
77 {
78 if (Common::instance()->isDaemon)
79 daemonCleanup();
80
81 exit(exitcode);
82 }
83
84 int main(int argc, char* argv[])
85 {
86 Common* cmn = Common::instance();
87
88 //Set default values
89
90 cmn->setLogfile( "/var/log/smsdaemon.log");
91 cmn->pidfile = "/var/run/smsdaemon.pid";
92 cmn->spooldir = "/var/spool/smsdaemon";
93
94 cmn->uid = 1000;
95 cmn->gid = 1000;
96
97 cmn->loadConfig(argc,argv);
98 cmn->daemonStart = time(0);
99
100
101 /////////////////////
102
103 //Write a delimiter line in the logfile to seperate sessions
104
105 if (Common::instance()->isDaemon)
106 daemonize();
107
108 cmn->logMessage("--------------------------------");
109
110 cmn->pluginManager.LoadPlugins();
111
112 SerialPort port("/dev/ttyS1" );
113 try
114 {
115 port.Open( SerialPort::BAUD_9600,
116 SerialPort::CHAR_SIZE_8,
117 SerialPort::PARITY_NONE,
118 SerialPort::STOP_BITS_1,
119 SerialPort::FLOW_CONTROL_HARD );
120 }
121 catch(std::exception &e)
122 {
123 cmn->logMessage( string("PortOpen Exception: ") + e.what() );
124 sms_exit(1);
125 }
126
127 GsmModem modem(port);
128
129 try
130 {
131 modem.Init();
132 }
133 catch (std::exception& e)
134 {
135 cmn->logMessage( string("GsmModem Exception: ") + e.what() );
136 sms_exit(2);
137 }
138
139
140 //////////////////////////////////
141
142 cmn->logMessage("SMS daemon started");
143
144 modem.DeleteAllSms();
145
146 main_loop(modem);
147
148 cmn->logMessage( cmn->getStatusMessage() );
149
150 if (cmn->isDaemon)
151 daemonCleanup();
152
153 return 0;
154 }

  ViewVC Help
Powered by ViewVC 1.1.20