00001 /// 00002 /// \file common.cc 00003 /// General Barry interface routines 00004 /// 00005 00006 /* 00007 Copyright (C) 2005-2011, 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 <usb.h> 00024 #include <pthread.h> 00025 #include "debug.h" 00026 00027 namespace Barry { 00028 00029 bool __data_dump_mode__; 00030 00031 std::ostream *LogStream = &std::cout; 00032 pthread_mutex_t LogStreamMutex; 00033 00034 00035 // 00036 // Init 00037 // 00038 /// Barry library initializer. Call this before anything else. 00039 /// This takes care of initializing the lower level libusb. 00040 /// 00041 /// This function is safe to be called multiple times. The 00042 /// data_dump_mode and the log stream will be updated each time 00043 /// it is called, but the USB library will not be re-initialized. 00044 /// 00045 /// \param[in] data_dump_mode If set to true, the protocol conversation 00046 /// will be sent to the logStream specified 00047 /// in the second argument. 00048 /// \param[in] LogStream Pointer to std::ostream object to use for 00049 /// debug output and logging. Defaults to 00050 /// std::cout. 00051 /// 00052 void Init(bool data_dump_mode, std::ostream *logStream) 00053 { 00054 static bool initialized = false; 00055 00056 // set usb debug mode first, so that USB's initialization 00057 // is captured too 00058 if( data_dump_mode ) 00059 usb_set_debug(9); 00060 00061 // perform one-time initalization 00062 if( !initialized ) { 00063 // if the environment variable USB_DEBUG is set, that 00064 // level value will be used instead of our 9 above... 00065 // if you need to *force* this to 9, call Verbose(true) 00066 // after Init() 00067 usb_init(); 00068 00069 // only need to initialize this once 00070 pthread_mutex_init(&LogStreamMutex, NULL); 00071 00072 // done 00073 initialized = true; 00074 } 00075 00076 __data_dump_mode__ = data_dump_mode; 00077 LogStream = logStream; 00078 } 00079 00080 // 00081 // Verbose 00082 // 00083 /// This API call lets the application enable / disable verbose debug 00084 /// output on the fly. 00085 /// 00086 /// \param[in] data_dump_mode If set to true, the protocol conversation 00087 /// will be sent to the logStream specified 00088 /// in the Barry::Init() call. 00089 /// 00090 void Verbose(bool data_dump_mode) 00091 { 00092 __data_dump_mode__ = data_dump_mode; 00093 00094 if( data_dump_mode ) 00095 usb_set_debug(9); 00096 else 00097 usb_set_debug(0); 00098 } 00099 00100 // 00101 // IsVerbose 00102 // 00103 /// Returns true if data dump mode is enabled. 00104 /// 00105 bool IsVerbose() 00106 { 00107 return __data_dump_mode__; 00108 } 00109 00110 } // namespace Barry 00111