3 /// General Barry interface routines
7 Copyright (C) 2005-2013, 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.
31 #ifdef USE_BARRY_SOCKETS
37 bool __data_dump_mode__
;
39 std::ostream
*LogStream
= &std::cout
;
40 pthread_mutex_t LogStreamMutex
;
46 /// Barry library initializer. Call this before anything else.
47 /// This takes care of initializing the lower level libusb.
49 /// This function is safe to be called multiple times. The
50 /// data_dump_mode and the log stream will be updated each time
51 /// it is called, but the USB library will not be re-initialized.
53 /// \param[in] data_dump_mode If set to true, the protocol conversation
54 /// will be sent to the logStream specified
55 /// in the second argument.
56 /// \param[in] LogStream Pointer to std::ostream object to use for
57 /// debug output and logging. Defaults to
60 void Init(bool data_dump_mode
, std::ostream
*logStream
)
62 static bool initialized
= false;
64 #ifdef USE_BARRY_SOCKETS
65 Usb::LibraryInterface::SetDataDump(data_dump_mode
);
68 // perform one-time initalization
70 // initialize i18n gettext directory
71 // the rest is done in i18n.h
72 bindtextdomain(PACKAGE
, LOCALEDIR
);
74 #ifdef USE_BARRY_SOCKETS
75 // Should call Usb::Uninit at some point,
76 // but there isn't currently a deinit call.
78 if( !Usb::LibraryInterface::Init(&err
) ) {
79 eout(_("USB library failed to initialise with libusb error: ") << err
);
80 throw Error(_("Failed to initialise USB"));
85 // only need to initialize this once
86 pthread_mutex_init(&LogStreamMutex
, NULL
);
92 __data_dump_mode__
= data_dump_mode
;
93 LogStream
= logStream
;
99 /// This API call lets the application enable / disable verbose debug
100 /// output on the fly.
102 /// \param[in] data_dump_mode If set to true, the protocol conversation
103 /// will be sent to the logStream specified
104 /// in the Barry::Init() call.
106 void Verbose(bool data_dump_mode
)
108 __data_dump_mode__
= data_dump_mode
;
110 #ifdef USE_BARRY_SOCKETS
111 Usb::LibraryInterface::SetDataDump(data_dump_mode
);
118 /// Returns true if data dump mode is enabled.
122 return __data_dump_mode__
;
125 // The following code is based on the example from the vsnprintf(3) manpage
126 std::string
string_vprintf(const char *fmt
, ...)
128 // Guess we need no more than 200 bytes.
134 if ((p
= (char*)malloc(size
)) == NULL
)
138 // Try to print in the allocated space.
140 n
= vsnprintf(p
, size
, fmt
, ap
);
143 // If that worked, return the string.
144 if (n
> -1 && n
< size
) {
150 // Else try again with more space.
151 if (n
> -1) // glibc 2.1
152 size
= n
+1; // precisely what is needed
154 size
*= 2; // twice the old size
156 if ((np
= (char*)realloc (p
, size
)) == NULL
) {