1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
/*
* This file is part of buteo-sync-plugins package
*
* Copyright (C) 2013 Jolla Ltd. and/or its subsidiary(-ies).
*
* Author: Sateesh Kavuri <sateesh.kavuri@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "USBConnection.h"
#include "SyncMLPluginLogging.h"
#include <QDateTime>
#include <fcntl.h>
#include <sys/socket.h>
#include <termios.h>
#include <unistd.h>
#ifdef GLIB_FD_WATCH
#include <glib.h>
#endif
USBConnection::USBConnection () :
mFd (-1), mMutex (QMutex::Recursive), mDisconnected (true), mFdWatching (false),
#ifdef GLIB_FD_WATCH
mIOChannel (0), mIdleEventSource (0), mFdWatchEventSource (0)
#else
mReadNotifier (0), mWriteNotifier (0), mExceptionNotifier (0)
#endif
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}
USBConnection::~USBConnection ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
#ifdef GLIB_FD_WATCH
// Make sure glibc does not try to deliver more events to us.
// This also frees mIOChannel by removing the last ref, if any.
removeFdListener();
#else
if (mReadNotifier)
{
delete mReadNotifier;
mReadNotifier = 0;
}
if (mWriteNotifier)
{
delete mWriteNotifier;
mWriteNotifier = 0;
}
if (mExceptionNotifier)
{
delete mExceptionNotifier;
mExceptionNotifier = 0;
}
#endif
}
int
USBConnection::connect ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
QMutexLocker lock (&mMutex);
if (isConnected ())
{
qCDebug(lcSyncMLPlugin) << "Already connected. Returning fd";
} else
{
mFd = openUSBDevice ();
addFdListener ();
}
return mFd;
}
void
USBConnection::disconnect ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
QMutexLocker lock (&mMutex);
#ifdef GLIB_FD_WATCH
removeEventSource ();
#endif
removeFdListener ();
// In server mode, we do not disconnect the connection, since
// the host (PC/device/...) might initiate sync again
}
bool
USBConnection::isConnected () const
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
if (mFd == -1)
return false;
else
return true;
}
int
USBConnection::openUSBDevice ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
QMutexLocker lock (&mMutex);
if (isConnected ()) {
qCWarning(lcSyncMLPlugin) << "USB connection already open with fd " << mFd;
return mFd;
}
const QString USB_DEVICE ("/dev/ttyGS1");
mFd = open (USB_DEVICE.toLocal8Bit ().constData (),
O_RDWR | O_NOCTTY);
if (mFd < 0) {
qCWarning(lcSyncMLPlugin) << "Count not open USB device";
return -1;
}
long flags = fcntl (mFd, F_GETFL);
fcntl (mFd, F_SETFL, flags & ~O_NONBLOCK);
struct termios opts;
tcgetattr (mFd, &opts);
cfmakeraw (&opts);
opts.c_oflag &= ~ONLCR;
tcsetattr (mFd, TCSANOW, &opts);
int arg = fcntl (mFd, F_GETFL);
if (arg < 0)
{
qCWarning(lcSyncMLPlugin) << "Unable to get file attributes";
close (mFd);
return -1;
}
arg |= O_NONBLOCK;
if (fcntl (mFd, F_SETFL, arg) < 0)
{
qCWarning(lcSyncMLPlugin) << "Could not set file attributes";
close (mFd);
return -1;
}
qCDebug(lcSyncMLPlugin) << "Opened USB device with fd " << mFd;
return mFd;
}
void
USBConnection::closeUSBDevice ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
QMutexLocker lock (&mMutex);
if (isConnected ())
{
qCDebug(lcSyncMLPlugin) << "Closing USB device with fd " << mFd;
shutdown (mFd, SHUT_RDWR);
close (mFd);
mFd = -1;
mDisconnected = true;
}
}
void
USBConnection::handleSyncFinished (bool isSyncInError)
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
QMutexLocker lock (&mMutex);
if (isSyncInError == true)
{
// What needs to be done here? Should we close the USB?
// For now completely closing USB device and reopening again
// syncml-ds-tool sends LINKERR for closing USB connection. This causes
// buteosyncml stack to think that a link error has occured and marks the
// sync session as error
// The following would ensure that the server plugin would return back
// to sane state incase of any unexpected sync errors
removeFdListener ();
closeUSBDevice ();
openUSBDevice ();
addFdListener ();
} else
{
// No errors during sync. Just add channel watcher,
// which was removed at the start of the sync
qCDebug(lcSyncMLPlugin) << "Handling sync finished. Adding fd listener";
addFdListener ();
}
}
void
USBConnection::addFdListener ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
QMutexLocker lock (&mMutex);
if ((mFdWatching == false) && isConnected ())
{
#ifdef GLIB_FD_WATCH
mIOChannel = g_io_channel_unix_new (mFd);
g_io_channel_set_close_on_unref (mIOChannel, FALSE);
mFdWatchEventSource = g_io_add_watch_full (mIOChannel,
G_PRIORITY_DEFAULT,
(GIOCondition) (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL),
handleIncomingUSBEvent,
this,
NULL);
g_io_channel_unref (mIOChannel);
qCDebug(lcSyncMLPlugin) << "Added fd listner for fd " << mFd << " with event source " << mFdWatchEventSource;
#else
mReadNotifier = new QSocketNotifier (mFd, QSocketNotifier::Read);
mWriteNotifier = new QSocketNotifier (mFd, QSocketNotifier::Write);
mExceptionNotifier = new QSocketNotifier (mFd, QSocketNotifier::Exception);
mReadNotifier->setEnabled (true);
mWriteNotifier->setEnabled (true);
mExceptionNotifier->setEnabled (true);
QObject::connect (mReadNotifier, SIGNAL (activated (int)),
this, SLOT (handleUSBActivated (int)), Qt::BlockingQueuedConnection);
QObject::connect (mWriteNotifier, SIGNAL (activated (int)),
this, SLOT (handleUSBActivated (int)), Qt::BlockingQueuedConnection);
QObject::connect (mExceptionNotifier, SIGNAL (activated (int)),
this, SLOT (handleUSBError (int)), Qt::BlockingQueuedConnection);
#endif
mFdWatching = true;
mDisconnected = false;
}
}
void
USBConnection::removeFdListener ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
QMutexLocker lock (&mMutex);
#ifdef GLIB_FD_WATCH
if (mFdWatchEventSource > 0)
{
gboolean removed = g_source_remove (mFdWatchEventSource);
if (removed)
{
qCDebug(lcSyncMLPlugin) << "Removed fd listener with event source " << mFdWatchEventSource;
mFdWatchEventSource = 0;
}
}
#else
mWriteNotifier->setEnabled (false);
mReadNotifier->setEnabled (false);
mExceptionNotifier->setEnabled (false);
QObject::disconnect (mReadNotifier, SIGNAL (activated (int)),
this, SLOT (handleUSBActivated (int)));
QObject::disconnect (mWriteNotifier, SIGNAL (activated (int)),
this, SLOT (handleUSBActivated (int)));
QObject::disconnect (mExceptionNotifier, SIGNAL (activated (int)),
this, SLOT (handleUSBError (int)));
#endif
mFdWatching = false;
}
void
USBConnection::signalNewSession ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
emit usbConnected (mFd);
}
#ifdef GLIB_FD_WATCH
void
USBConnection::setFdWatchEventSource (guint fdEventSource)
{
mFdWatchEventSource = fdEventSource;
}
void
USBConnection::setIdleEventSource (guint idleEventSource)
{
mIdleEventSource = idleEventSource;
}
guint
USBConnection::fdWatchEventSource ()
{
return mFdWatchEventSource;
}
guint
USBConnection::idleEventSource ()
{
return mIdleEventSource;
}
void
USBConnection::removeEventSource ()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
if ((mIdleEventSource > 0) && (g_source_remove (mIdleEventSource)))
{
qCDebug(lcSyncMLPlugin) << "Removing idle event source " << mIdleEventSource;
mIdleEventSource = 0;
}
}
gboolean
USBConnection::handleIncomingUSBEvent (GIOChannel *ioChannel, GIOCondition condition, gpointer user_data)
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
USBConnection* connection = (USBConnection*) user_data;
int fd = g_io_channel_unix_get_fd (ioChannel);
qCDebug(lcSyncMLPlugin) << "Channel fd: " << fd;
if (condition & (G_IO_HUP | G_IO_ERR))
{
// Receved a hangup or an error. Remove the watchers and
// also disconnect the listeners
if (connection->isConnected ())
{
guint eventSource = connection->idleEventSource ();
if ((eventSource > 0) && g_source_remove (eventSource))
{
qCDebug(lcSyncMLPlugin) << "Removed event source " << eventSource;
connection->setIdleEventSource (0);
}
// Add an idle loop to reopen USB incase of HUP or ERR
eventSource = g_idle_add (reopenUSB, connection);
connection->setIdleEventSource (eventSource);
qCDebug(lcSyncMLPlugin) << "Added watch on the idle event source " << eventSource;
}
// If in error, remove the fd listner and also close the USB device
connection->removeFdListener ();
connection->closeUSBDevice ();
} else if (condition & G_IO_IN)
{
// Some incoming data. Better remove the listener
connection->removeFdListener ();
// Signal a new session
connection->signalNewSession ();
}
connection->setFdWatchEventSource (0);
return false;
}
gboolean
USBConnection::reopenUSB (gpointer data)
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
USBConnection* connection = (USBConnection*) data;
if ((connection->mDisconnected == true) && !connection->isConnected ())
{
qCDebug(lcSyncMLPlugin) << "USB Not disconnected and not listening";
connection->openUSBDevice ();
connection->addFdListener ();
} else
{
qCDebug(lcSyncMLPlugin) << "Already listening. No need to reopen";
}
connection->setIdleEventSource (0);
return false;
}
#else
void
USBConnection::handleUSBActivated (int fd)
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
qCDebug(lcSyncMLPlugin) << "USB is activated. Emitting signal to handle incoming data";
emit usbConnected (fd);
// Disable the event notifier
removeFdListener ();
}
void
USBConnection::handleUSBError (int fd)
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
qCDebug(lcSyncMLPlugin) << "Error in USB connection";
removeFdListener ();
closeUSBDevice ();
openUSBDevice ();
addFdListener ();
}
#endif
|