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.
32 #ifdef USE_BARRY_SOCKETS
38 bool __data_dump_mode__
;
40 std::ostream
*LogStream
= &std::cout
;
41 pthread_mutex_t LogStreamMutex
;
47 /// Barry library initializer. Call this before anything else.
48 /// This takes care of initializing the lower level libusb.
50 /// This function is safe to be called multiple times. The
51 /// data_dump_mode and the log stream will be updated each time
52 /// it is called, but the USB library will not be re-initialized.
54 /// \param[in] data_dump_mode If set to true, the protocol conversation
55 /// will be sent to the logStream specified
56 /// in the second argument.
57 /// \param[in] LogStream Pointer to std::ostream object to use for
58 /// debug output and logging. Defaults to
61 void Init(bool data_dump_mode
, std::ostream
*logStream
)
63 static bool initialized
= false;
65 #ifdef USE_BARRY_SOCKETS
66 Usb::LibraryInterface::SetDataDump(data_dump_mode
);
69 // perform one-time initalization
71 // initialize i18n gettext directory
72 // the rest is done in i18n.h
73 bindtextdomain(PACKAGE
, LOCALEDIR
);
75 #ifdef USE_BARRY_SOCKETS
76 // Should call Usb::Uninit at some point,
77 // but there isn't currently a deinit call.
79 if( !Usb::LibraryInterface::Init(&err
) ) {
80 eout(_("USB library failed to initialise with libusb error: ") << err
);
81 throw Error(_("Failed to initialise USB"));
86 // only need to initialize this once
87 pthread_mutex_init(&LogStreamMutex
, NULL
);
93 __data_dump_mode__
= data_dump_mode
;
94 LogStream
= logStream
;
100 /// This API call lets the application enable / disable verbose debug
101 /// output on the fly.
103 /// \param[in] data_dump_mode If set to true, the protocol conversation
104 /// will be sent to the logStream specified
105 /// in the Barry::Init() call.
107 void Verbose(bool data_dump_mode
)
109 __data_dump_mode__
= data_dump_mode
;
111 #ifdef USE_BARRY_SOCKETS
112 Usb::LibraryInterface::SetDataDump(data_dump_mode
);
119 /// Returns true if data dump mode is enabled.
123 return __data_dump_mode__
;
126 // The following code is based on the example from the vsnprintf(3) manpage
127 std::string
string_vprintf(const char *fmt
, ...)
129 // Guess we need no more than 200 bytes.
135 if ((p
= (char*)malloc(size
)) == NULL
)
139 // Try to print in the allocated space.
141 n
= vsnprintf(p
, size
, fmt
, ap
);
144 // If that worked, return the string.
145 if (n
> -1 && n
< size
) {
151 // Else try again with more space.
152 if (n
> -1) // glibc 2.1
153 size
= n
+1; // precisely what is needed
155 size
*= 2; // twice the old size
157 if ((np
= (char*)realloc (p
, size
)) == NULL
) {