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

Contents of /smsdaemon/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 82 - (show annotations) (download)
Sun Jun 15 20:06:39 2008 UTC (15 years, 11 months ago) by torben
File size: 2695 byte(s)
Added task infrastructure.

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 cmn->taskManager.ExecuteTasks();
68
69
70 if (cmn->isDebug && kbhit())
71 break;
72
73 Util::Sleep(10);
74
75 }
76 }
77
78 bool sms_exit(int exitcode)
79 {
80 if (Common::instance()->isDaemon)
81 daemonCleanup();
82
83 exit(exitcode);
84 }
85
86 int main(int argc, char* argv[])
87 {
88 Common* cmn = Common::instance();
89
90 //Set default values
91
92 cmn->setLogfile( "/var/log/smsdaemon.log");
93 cmn->pidfile = "/var/run/smsdaemon.pid";
94 cmn->spooldir = "/var/spool/smsdaemon";
95
96 cmn->uid = 1000;
97 cmn->gid = 1000;
98
99 cmn->loadConfig(argc,argv);
100 cmn->daemonStart = time(0);
101
102
103 /////////////////////
104
105 //Write a delimiter line in the logfile to seperate sessions
106
107 if (Common::instance()->isDaemon)
108 daemonize();
109
110 cmn->logMessage("--------------------------------");
111
112 cmn->taskManager.LoadTasks();
113 cmn->pluginManager.LoadPlugins();
114
115 SerialPort port("/dev/ttyS1" );
116 try
117 {
118 port.Open( SerialPort::BAUD_9600,
119 SerialPort::CHAR_SIZE_8,
120 SerialPort::PARITY_NONE,
121 SerialPort::STOP_BITS_1,
122 SerialPort::FLOW_CONTROL_HARD );
123 }
124 catch(std::exception &e)
125 {
126 cmn->logMessage( string("PortOpen Exception: ") + e.what() );
127 sms_exit(1);
128 }
129
130 GsmModem modem(port);
131
132 try
133 {
134 modem.Init();
135 }
136 catch (std::exception& e)
137 {
138 cmn->logMessage( string("GsmModem Exception: ") + e.what() );
139 sms_exit(2);
140 }
141
142
143 //////////////////////////////////
144
145 cmn->logMessage("SMS daemon started");
146
147 modem.DeleteAllSms();
148
149 main_loop(modem);
150
151 cmn->logMessage( cmn->getStatusMessage() );
152
153 if (cmn->isDaemon)
154 daemonCleanup();
155
156 return 0;
157 }

  ViewVC Help
Powered by ViewVC 1.1.20