Changeset 03f7952 in mainline for uspace/lib/usb/src/debug.c


Ignore:
Timestamp:
2011-03-04T21:59:20Z (14 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
80fdffe, f43c4d2, f5246b6
Parents:
4e900c1 (diff), ff4185e6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Small improvements

USB mouse stops polling after first failed transmission
(dirty hack for unplug).

Added function for dumping buffers (usb_debug_str_buffer).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usb/src/debug.c

    r4e900c1 r03f7952  
    252252}
    253253
     254
     255#define REMAINDER_STR_FMT " (%zu)..."
     256/* string + terminator + number width (enough for 4GB)*/
     257#define REMAINDER_STR_LEN (5 + 1 + 10)
     258#define BUFFER_DUMP_GROUP_SIZE 4
     259#define BUFFER_DUMP_LEN 240 /* Ought to be enough for everybody ;-). */
     260static fibril_local char buffer_dump[BUFFER_DUMP_LEN];
     261
     262/** Dump buffer into string.
     263 *
     264 * The function dumps given buffer into hexadecimal format and stores it
     265 * in a static fibril local string.
     266 * That means that you do not have to deallocate the string (actually, you
     267 * can not do that) and you do not have to save it agains concurrent
     268 * calls to it.
     269 * The only limitation is that each call rewrites the buffer again.
     270 * Thus, it is necessary to copy the buffer elsewhere (that includes printing
     271 * to screen or writing to file).
     272 * Since this function is expected to be used for debugging prints only,
     273 * that is not a big limitation.
     274 *
     275 * @warning You cannot use this function twice in the same printf
     276 * (see detailed explanation).
     277 *
     278 * @param buffer Buffer to be printed (can be NULL).
     279 * @param size Size of the buffer in bytes (can be zero).
     280 * @param dumped_size How many bytes to actually dump (zero means all).
     281 * @return Dumped buffer as a static (but fibril local) string.
     282 */
     283const char *usb_debug_str_buffer(uint8_t *buffer, size_t size,
     284    size_t dumped_size)
     285{
     286        /*
     287         * Remove previous string (that might also reveal double usage of
     288         * this function).
     289         */
     290        bzero(buffer_dump, BUFFER_DUMP_LEN);
     291
     292        if (buffer == NULL) {
     293                return "(null)";
     294        }
     295        if (size == 0) {
     296                return "(empty)";
     297        }
     298        if ((dumped_size == 0) || (dumped_size > size)) {
     299                dumped_size = size;
     300        }
     301
     302        /* How many bytes are available in the output buffer. */
     303        size_t buffer_remaining_size = BUFFER_DUMP_LEN - 1 - REMAINDER_STR_LEN;
     304        char *it = buffer_dump;
     305
     306        size_t index = 0;
     307
     308        while (index < size) {
     309                /* Determine space before the number. */
     310                const char *space_before;
     311                if (index == 0) {
     312                        space_before = "";
     313                } else if ((index % BUFFER_DUMP_GROUP_SIZE) == 0) {
     314                        space_before = "  ";
     315                } else {
     316                        space_before = " ";
     317                }
     318
     319                /*
     320                 * Add the byte as a hexadecimal number plus the space.
     321                 * We do it into temporary buffer to ensure that always
     322                 * the whole byte is printed.
     323                 */
     324                int val = buffer[index];
     325                char current_byte[16];
     326                int printed = snprintf(current_byte, 16,
     327                    "%s%02x", space_before, val);
     328                if (printed < 0) {
     329                        break;
     330                }
     331
     332                if ((size_t) printed > buffer_remaining_size) {
     333                        break;
     334                }
     335
     336                /* We can safely add 1, because space for end 0 is reserved. */
     337                str_append(it, buffer_remaining_size + 1, current_byte);
     338
     339                buffer_remaining_size -= printed;
     340                /* Point at the terminator 0. */
     341                it += printed;
     342                index++;
     343
     344                if (index >= dumped_size) {
     345                        break;
     346                }
     347        }
     348
     349        /* Add how many bytes were not printed. */
     350        if (index < size) {
     351                snprintf(it, REMAINDER_STR_LEN,
     352                    REMAINDER_STR_FMT, size - index);
     353        }
     354
     355        return buffer_dump;
     356}
     357
     358
    254359/**
    255360 * @}
Note: See TracChangeset for help on using the changeset viewer.