common.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       common.cc
00003 ///             General Barry interface routines
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "common.h"
00023 #include <pthread.h>
00024 #include "debug.h"
00025 #include "config.h"
00026 
00027 #ifdef USE_BARRY_SOCKETS
00028 #include "usbwrap.h"
00029 #endif
00030 
00031 namespace Barry {
00032 
00033 bool __data_dump_mode__;
00034 
00035 std::ostream *LogStream = &std::cout;
00036 pthread_mutex_t LogStreamMutex;
00037 
00038 
00039 //
00040 // Init
00041 //
00042 /// Barry library initializer.  Call this before anything else.
00043 /// This takes care of initializing the lower level libusb.
00044 ///
00045 /// This function is safe to be called multiple times.  The
00046 /// data_dump_mode and the log stream will be updated each time
00047 /// it is called, but the USB library will not be re-initialized.
00048 ///
00049 /// \param[in]  data_dump_mode  If set to true, the protocol conversation
00050 ///                             will be sent to the logStream specified
00051 ///                             in the second argument.
00052 /// \param[in]  LogStream       Pointer to std::ostream object to use for
00053 ///                             debug output and logging.  Defaults to
00054 ///                             std::cout.
00055 ///
00056 void Init(bool data_dump_mode, std::ostream *logStream)
00057 {
00058         static bool initialized = false;
00059 
00060 #ifdef USE_BARRY_SOCKETS
00061         Usb::LibraryInterface::SetDataDump(data_dump_mode);
00062 #endif
00063 
00064         // perform one-time initalization
00065         if( !initialized ) {
00066 #ifdef USE_BARRY_SOCKETS
00067                 // Should call Usb::Uninit at some point,
00068                 // but there isn't currently a deinit call.
00069                 int err = 0;
00070                 if( !Usb::LibraryInterface::Init(&err) ) {
00071                         eout("USB library failed to initialise with libusb error: " << err);
00072                         throw Error("Failed to initialise USB");
00073                         return;
00074                 }
00075 #endif
00076 
00077                 // only need to initialize this once
00078                 pthread_mutex_init(&LogStreamMutex, NULL);
00079 
00080                 // done
00081                 initialized = true;
00082         }
00083 
00084         __data_dump_mode__ = data_dump_mode;
00085         LogStream = logStream;
00086 }
00087 
00088 //
00089 // Verbose
00090 //
00091 /// This API call lets the application enable / disable verbose debug
00092 /// output on the fly.
00093 ///
00094 /// \param[in]  data_dump_mode  If set to true, the protocol conversation
00095 ///                             will be sent to the logStream specified
00096 ///                             in the Barry::Init() call.
00097 ///
00098 void Verbose(bool data_dump_mode)
00099 {
00100         __data_dump_mode__ = data_dump_mode;
00101 
00102 #ifdef USE_BARRY_SOCKETS
00103         Usb::LibraryInterface::SetDataDump(data_dump_mode);
00104 #endif
00105 }
00106 
00107 //
00108 // IsVerbose
00109 //
00110 /// Returns true if data dump mode is enabled.
00111 ///
00112 bool IsVerbose()
00113 {
00114         return __data_dump_mode__;
00115 }
00116 
00117 } // namespace Barry
00118