Diffstat (limited to 'noncore/securityplugins/blueping/bluepingplugin.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/securityplugins/blueping/bluepingplugin.cpp | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/noncore/securityplugins/blueping/bluepingplugin.cpp b/noncore/securityplugins/blueping/bluepingplugin.cpp new file mode 100644 index 0000000..af0448b --- a/dev/null +++ b/noncore/securityplugins/blueping/bluepingplugin.cpp | |||
@@ -0,0 +1,198 @@ | |||
1 | #include "bluepingplugin.h" | ||
2 | |||
3 | #include <opie2/oapplication.h> | ||
4 | #include <opie2/odebug.h> | ||
5 | #include <opie2/odevice.h> | ||
6 | |||
7 | #include <qdialog.h> | ||
8 | #include <qlayout.h> | ||
9 | #include <qhbox.h> | ||
10 | #include <qlabel.h> | ||
11 | #include <qpushbutton.h> | ||
12 | #include <qtimer.h> | ||
13 | |||
14 | using namespace Opie::Core; | ||
15 | |||
16 | /// creates and initializes the m_config Config object | ||
17 | BluepingPlugin::BluepingPlugin() : MultiauthPluginObject(), m_ping(0) { | ||
18 | m_config = new Config("Security"); | ||
19 | m_config->setGroup("BluepingPlugin"); | ||
20 | bluetoothAlreadyRestarted = false; | ||
21 | } | ||
22 | |||
23 | /// deletes the m_config Config object and noticeW if necessary | ||
24 | BluepingPlugin::~BluepingPlugin() { | ||
25 | delete m_config; | ||
26 | if (m_ping != 0) | ||
27 | delete m_ping; | ||
28 | } | ||
29 | |||
30 | /// Simply return its name (Blueping plugin) | ||
31 | QString BluepingPlugin::pluginName() const { | ||
32 | return "Blueping plugin"; | ||
33 | } | ||
34 | |||
35 | /// no configuration widget for the moment | ||
36 | MultiauthConfigWidget * BluepingPlugin::configWidget(QWidget * parent) { | ||
37 | return 0l; | ||
38 | } | ||
39 | QString BluepingPlugin::pixmapNameWidget() const { | ||
40 | return "security/bluepingplugin"; | ||
41 | } | ||
42 | QString BluepingPlugin::pixmapNameConfig() const { | ||
43 | return 0l; | ||
44 | } | ||
45 | |||
46 | /// Emit the MultiauthPluginObject::Success emitCode | ||
47 | void BluepingPlugin::success() { | ||
48 | emit emitCode(MultiauthPluginObject::Success); | ||
49 | } | ||
50 | |||
51 | /// Emit the MultiauthPluginObject::Failure emitCode | ||
52 | void BluepingPlugin::failure() { | ||
53 | emit emitCode(MultiauthPluginObject::Failure); | ||
54 | } | ||
55 | |||
56 | /// Emit the MultiauthPluginObject::Skip emitCode | ||
57 | void BluepingPlugin::skip() { | ||
58 | emit emitCode(MultiauthPluginObject::Skip); | ||
59 | } | ||
60 | |||
61 | /// do the actual ping | ||
62 | void BluepingPlugin::ping() { | ||
63 | m_ping = new OProcess(); | ||
64 | odebug << "pinging device: " << macToPing << oendl; | ||
65 | *m_ping << "l2ping" << "-c 1" << macToPing; | ||
66 | |||
67 | // starting to ping in the background | ||
68 | /// \todo as soon as ping is launched, check RSSI (signal strength) and check | ||
69 | /// it's high enough, meaning the device is close enough? | ||
70 | /// \todo make it optionally pollable, so don't finish the ping and call | ||
71 | /// Opie suspend if l2ping timeouts? | ||
72 | if ( !m_ping->start() ) { | ||
73 | oerr << "could not start l2ping" << oendl; | ||
74 | this->skip(); | ||
75 | } | ||
76 | QObject::connect(m_ping, SIGNAL(processExited(Opie::Core::OProcess*)), | ||
77 | this, SLOT(pingFinished(Opie::Core::OProcess*)) ); | ||
78 | } | ||
79 | |||
80 | /// Deals with m_ping result | ||
81 | void BluepingPlugin::pingFinished(OProcess * ping) { | ||
82 | if ( ping->normalExit() && (ping->exitStatus() == 0) ) | ||
83 | { | ||
84 | odebug << "Successful Bluetooth ping!" << oendl; | ||
85 | success(); | ||
86 | } | ||
87 | else | ||
88 | { | ||
89 | odebug << "Failed Bluetooth ping..." << oendl; | ||
90 | failure(); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | /// Make one authentication attempt with this plugin | ||
95 | /** | ||
96 | * (very simple "success" / "failure" buttons in a dialog) | ||
97 | * \return The outcome code of this authentication | ||
98 | */ | ||
99 | int BluepingPlugin::authenticate() { | ||
100 | |||
101 | Config cfg("Security"); | ||
102 | cfg.setGroup("BluepingPlugin"); | ||
103 | macToPing = cfg.readEntry("mac"); | ||
104 | if (!macToPing.isEmpty()) | ||
105 | { | ||
106 | /* Standard, inescapable authentication dialog | ||
107 | */ | ||
108 | QDialog bluepingDialog(0, | ||
109 | "Blueping dialog", | ||
110 | TRUE, | ||
111 | Qt::WStyle_NoBorder | Qt::WStyle_Customize | Qt::WStyle_StaysOnTop); | ||
112 | |||
113 | QRect desk = oApp->desktop()->geometry(); | ||
114 | bluepingDialog.setGeometry( 0, 0, desk.width(), desk.height() ); | ||
115 | |||
116 | // Creation of the particular widgets of our Blueping user interface | ||
117 | QVBoxLayout *layout = new QVBoxLayout(&bluepingDialog); | ||
118 | layout->setSpacing(11); | ||
119 | layout->setMargin(11); | ||
120 | layout->setAlignment( Qt::AlignTop ); | ||
121 | |||
122 | QLabel title("<center><h1>\"Blueping\" <br />plugin</h1></center>", &bluepingDialog); | ||
123 | QLabel subTitle("<center><h2>Trying to reach your configured bluetooth device...</h2></center>", &bluepingDialog); | ||
124 | QLabel subTitle2("<center>You can skip this step and use another authentication way with the following button</center>", &bluepingDialog); | ||
125 | QPushButton pbSkip("Skip", &bluepingDialog); | ||
126 | layout->addWidget(&title); | ||
127 | layout->addWidget(&subTitle); | ||
128 | layout->addWidget(&subTitle2); | ||
129 | layout->addWidget(&pbSkip, 0, Qt::AlignHCenter); | ||
130 | |||
131 | // connect the skip button to the skip signal emitting function | ||
132 | QObject::connect(&pbSkip, SIGNAL(clicked()), this, SLOT(skip())); | ||
133 | // connect the signal emitting functions to the bluepingDialog done(int) finishing function | ||
134 | QObject::connect(this, SIGNAL(emitCode(int)), &bluepingDialog, SLOT(done(int))); | ||
135 | |||
136 | if (!bluetoothAlreadyRestarted) | ||
137 | { | ||
138 | // we have just started or resumed the device, so Bluetooth has to be (re)started | ||
139 | OProcess killB; | ||
140 | killB << "killall" << "hciattach"; | ||
141 | odebug << "killing Bluetooth..." << oendl; | ||
142 | if ( !killB.start(OProcess::Block) ) { | ||
143 | oerr << "could not kill bluetooth" << oendl; | ||
144 | } | ||
145 | |||
146 | OProcess startB; | ||
147 | switch ( ODevice::inst()->model() ) { | ||
148 | case Model_iPAQ_H39xx: | ||
149 | startB << "/sbin/hciattach" << "/dev/tts/1" << "bcsp" << "921600"; | ||
150 | break; | ||
151 | |||
152 | case Model_iPAQ_H5xxx: | ||
153 | startB << "/sbin/hciattach" << "/dev/tts/1" << "any" << "921600"; | ||
154 | break; | ||
155 | |||
156 | default: | ||
157 | startB << "/sbin/hciattach" << "/dev/ttySB0" << "bcsp" << "230400"; | ||
158 | break; | ||
159 | } // end switch on device models | ||
160 | |||
161 | if ( !startB.start(OProcess::Block) ) { | ||
162 | oerr << "could not (re)start bluetooth" << oendl; | ||
163 | return MultiauthPluginObject::Skip; | ||
164 | } | ||
165 | else | ||
166 | { | ||
167 | if ( startB.normalExit() && (startB.exitStatus() == 0) ) | ||
168 | { | ||
169 | odebug << "hciattach exited normally."<< oendl; | ||
170 | bluetoothAlreadyRestarted = true; | ||
171 | // 500 ms timer, so l2ping won't try to find a route before bluetooth has \em really started | ||
172 | QTimer::singleShot( 500, this, SLOT(ping()) ); | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | owarn << "hciattach exited anormally (error code: " << startB.exitStatus() << ")" << oendl; | ||
177 | } // end if startBluetooth exit status == 0 | ||
178 | } // end if startBluetooth started | ||
179 | } | ||
180 | else | ||
181 | { | ||
182 | // we don't need to wait, since bluetooth has been started long enough ago | ||
183 | ping(); | ||
184 | } // end if bluetooth not restarted | ||
185 | |||
186 | |||
187 | // start the dialog event loop, while the ping is starting (or will start soon) in the background | ||
188 | return bluepingDialog.exec(); | ||
189 | |||
190 | |||
191 | } | ||
192 | else | ||
193 | { | ||
194 | owarn << "No Bluetooth device has been set!" << oendl; | ||
195 | owarn << "We will consider it as a successful authentication though." << oendl; | ||
196 | return MultiauthPluginObject::Success; | ||
197 | } // end if mac defined | ||
198 | } | ||