[bb95594] | 1 | /*
|
---|
[6028ec8] | 2 | * Copyright (c) 2010-2011 Vojtech Horky
|
---|
[bb95594] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[3b77628] | 29 | /** @addtogroup libusb
|
---|
[bb95594] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[a6add7a] | 33 | * Debugging and logging support.
|
---|
[bb95594] | 34 | */
|
---|
| 35 | #include <fibril_synch.h>
|
---|
[136ff96] | 36 | #include <ddf/log.h>
|
---|
[bb95594] | 37 | #include <usb/debug.h>
|
---|
[1d6dd2a] | 38 | #include <str.h>
|
---|
[bb95594] | 39 |
|
---|
[ff4185e6] | 40 | #define REMAINDER_STR_FMT " (%zu)..."
|
---|
[d1582b50] | 41 | /* string + terminator + number width (enough for 4GB) */
|
---|
[ff4185e6] | 42 | #define REMAINDER_STR_LEN (5 + 1 + 10)
|
---|
[a6add7a] | 43 |
|
---|
| 44 | /** How many bytes to group together. */
|
---|
[ff4185e6] | 45 | #define BUFFER_DUMP_GROUP_SIZE 4
|
---|
[a6add7a] | 46 |
|
---|
| 47 | /** Size of the string for buffer dumps. */
|
---|
[ff4185e6] | 48 | #define BUFFER_DUMP_LEN 240 /* Ought to be enough for everybody ;-). */
|
---|
[a6add7a] | 49 |
|
---|
| 50 | /** Fibril local storage for the dumped buffer. */
|
---|
[6fd7062] | 51 | static fibril_local char buffer_dump[2][BUFFER_DUMP_LEN];
|
---|
| 52 | /** Fibril local storage for buffer switching. */
|
---|
| 53 | static fibril_local int buffer_dump_index = 0;
|
---|
[019cff6] | 54 |
|
---|
| 55 | /** Dump buffer into string.
|
---|
| 56 | *
|
---|
| 57 | * The function dumps given buffer into hexadecimal format and stores it
|
---|
| 58 | * in a static fibril local string.
|
---|
| 59 | * That means that you do not have to deallocate the string (actually, you
|
---|
[d78a32f] | 60 | * can not do that) and you do not have to guard it against concurrent
|
---|
[019cff6] | 61 | * calls to it.
|
---|
[6fd7062] | 62 | * The only limitation is that each second call rewrites the buffer again
|
---|
| 63 | * (internally, two buffer are used in cyclic manner).
|
---|
[019cff6] | 64 | * Thus, it is necessary to copy the buffer elsewhere (that includes printing
|
---|
| 65 | * to screen or writing to file).
|
---|
| 66 | * Since this function is expected to be used for debugging prints only,
|
---|
| 67 | * that is not a big limitation.
|
---|
| 68 | *
|
---|
[6fd7062] | 69 | * @warning You cannot use this function more than twice in the same printf
|
---|
[019cff6] | 70 | * (see detailed explanation).
|
---|
| 71 | *
|
---|
| 72 | * @param buffer Buffer to be printed (can be NULL).
|
---|
| 73 | * @param size Size of the buffer in bytes (can be zero).
|
---|
| 74 | * @param dumped_size How many bytes to actually dump (zero means all).
|
---|
| 75 | * @return Dumped buffer as a static (but fibril local) string.
|
---|
| 76 | */
|
---|
[1efe89b] | 77 | const char *usb_debug_str_buffer(const uint8_t *buffer, size_t size,
|
---|
[019cff6] | 78 | size_t dumped_size)
|
---|
| 79 | {
|
---|
[ff4185e6] | 80 | /*
|
---|
[6fd7062] | 81 | * Remove previous string.
|
---|
[ff4185e6] | 82 | */
|
---|
[acdb5bac] | 83 | memset(buffer_dump[buffer_dump_index], 0, BUFFER_DUMP_LEN);
|
---|
[ff4185e6] | 84 |
|
---|
[136ff96] | 85 | /* Do the actual dump. */
|
---|
| 86 | ddf_dump_buffer(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN,
|
---|
| 87 | buffer, 1, size, dumped_size);
|
---|
[019cff6] | 88 |
|
---|
[6fd7062] | 89 | /* Next time, use the other buffer. */
|
---|
| 90 | buffer_dump_index = 1 - buffer_dump_index;
|
---|
| 91 |
|
---|
| 92 | /* Need to take the old one due to previous line. */
|
---|
| 93 | return buffer_dump[1 - buffer_dump_index];
|
---|
[019cff6] | 94 | }
|
---|
| 95 |
|
---|
[bb95594] | 96 | /**
|
---|
| 97 | * @}
|
---|
| 98 | */
|
---|