lib: changed Usb::Error exception to deal in libusb_errcode instead of system
[barry/progweb.git] / src / common.cc
blobdade76710b5f40efe4f5cdcf6f818185e61213a0
1 ///
2 /// \file common.cc
3 /// General Barry interface routines
4 ///
6 /*
7 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "common.h"
23 #include <pthread.h>
24 #include "debug.h"
25 #include "config.h"
27 #ifdef USE_BARRY_SOCKETS
28 #include "usbwrap.h"
29 #endif
31 namespace Barry {
33 bool __data_dump_mode__;
35 std::ostream *LogStream = &std::cout;
36 pthread_mutex_t LogStreamMutex;
40 // Init
42 /// Barry library initializer. Call this before anything else.
43 /// This takes care of initializing the lower level libusb.
44 ///
45 /// This function is safe to be called multiple times. The
46 /// data_dump_mode and the log stream will be updated each time
47 /// it is called, but the USB library will not be re-initialized.
48 ///
49 /// \param[in] data_dump_mode If set to true, the protocol conversation
50 /// will be sent to the logStream specified
51 /// in the second argument.
52 /// \param[in] LogStream Pointer to std::ostream object to use for
53 /// debug output and logging. Defaults to
54 /// std::cout.
55 ///
56 void Init(bool data_dump_mode, std::ostream *logStream)
58 static bool initialized = false;
60 #ifdef USE_BARRY_SOCKETS
61 Usb::LibraryInterface::SetDataDump(data_dump_mode);
62 #endif
64 // perform one-time initalization
65 if( !initialized ) {
66 #ifdef USE_BARRY_SOCKETS
67 // Should call Usb::Uninit at some point,
68 // but there isn't currently a deinit call.
69 int err = Usb::LibraryInterface::Init();
70 if( err ) {
71 eout("USB library failed to initialise with error: " << err);
72 throw Error("Failed to initialise USB");
73 return;
75 #endif
77 // only need to initialize this once
78 pthread_mutex_init(&LogStreamMutex, NULL);
80 // done
81 initialized = true;
84 __data_dump_mode__ = data_dump_mode;
85 LogStream = logStream;
89 // Verbose
91 /// This API call lets the application enable / disable verbose debug
92 /// output on the fly.
93 ///
94 /// \param[in] data_dump_mode If set to true, the protocol conversation
95 /// will be sent to the logStream specified
96 /// in the Barry::Init() call.
97 ///
98 void Verbose(bool data_dump_mode)
100 __data_dump_mode__ = data_dump_mode;
102 #ifdef USE_BARRY_SOCKETS
103 Usb::LibraryInterface::SetDataDump(data_dump_mode);
104 #endif
108 // IsVerbose
110 /// Returns true if data dump mode is enabled.
112 bool IsVerbose()
114 return __data_dump_mode__;
117 } // namespace Barry