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

Annotation of /smsdaemon/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 57 - (hide annotations) (download)
Wed Jun 11 16:08:37 2008 UTC (15 years, 11 months ago) by torben
File size: 2664 byte(s)
implement early exit if modem wasn't detected.


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

  ViewVC Help
Powered by ViewVC 1.1.20