Changeset 019cff6 in mainline


Ignore:
Timestamp:
2011-03-04T20:11:22Z (14 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ff4185e6
Parents:
3faf416c
Message:

Add buffer dumping function usb_debug_str_buffer

Example usage in USB mouse driver.

Checking etc. will be added in later revisions.

Location:
uspace
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbmouse/main.c

    r3faf416c r019cff6  
    7474int main(int argc, char *argv[])
    7575{
    76         usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
     76        usb_log_enable(USB_LOG_LEVEL_DEBUG2, NAME);
    7777
    7878        return ddf_driver_main(&mouse_driver);
  • uspace/drv/usbmouse/mouse.c

    r3faf416c r019cff6  
    9494                }
    9595
     96                usb_log_debug2("got buffer: %s.\n",
     97                    usb_debug_str_buffer(buffer, buffer_size, 0));
     98
    9699                uint8_t butt = buffer[0];
    97100                char str_buttons[4] = {
  • uspace/lib/usb/include/usb/debug.h

    r3faf416c r019cff6  
    7979        usb_log_printf(USB_LOG_LEVEL_DEBUG2, format, ##__VA_ARGS__)
    8080
     81const char *usb_debug_str_buffer(uint8_t *, size_t, size_t);
    8182
    8283
  • uspace/lib/usb/src/debug.c

    r3faf416c r019cff6  
    252252}
    253253
     254
     255#define BUFFER_DUMP_LEN 512
     256static fibril_local char buffer_dump[BUFFER_DUMP_LEN];
     257
     258/** Dump buffer into string.
     259 *
     260 * The function dumps given buffer into hexadecimal format and stores it
     261 * in a static fibril local string.
     262 * That means that you do not have to deallocate the string (actually, you
     263 * can not do that) and you do not have to save it agains concurrent
     264 * calls to it.
     265 * The only limitation is that each call rewrites the buffer again.
     266 * Thus, it is necessary to copy the buffer elsewhere (that includes printing
     267 * to screen or writing to file).
     268 * Since this function is expected to be used for debugging prints only,
     269 * that is not a big limitation.
     270 *
     271 * @warning You cannot use this function twice in the same printf
     272 * (see detailed explanation).
     273 *
     274 * @param buffer Buffer to be printed (can be NULL).
     275 * @param size Size of the buffer in bytes (can be zero).
     276 * @param dumped_size How many bytes to actually dump (zero means all).
     277 * @return Dumped buffer as a static (but fibril local) string.
     278 */
     279const char *usb_debug_str_buffer(uint8_t *buffer, size_t size,
     280    size_t dumped_size)
     281{
     282        if (buffer == NULL) {
     283                return "(null)";
     284        }
     285        if (size == 0) {
     286                return "(empty)";
     287        }
     288        if ((dumped_size == 0) || (dumped_size > size)) {
     289                dumped_size = size;
     290        }
     291
     292        char *it = buffer_dump;
     293
     294        size_t index = 0;
     295
     296        while (1) {
     297                /* FIXME: add range checking of the buffer size. */
     298                snprintf(it, 4, "%02X ", (int) buffer[index]);
     299                it += 3;
     300                index++;
     301                if (index >= dumped_size) {
     302                        break;
     303                }
     304        }
     305
     306        /* Remove the last space */
     307        it--;
     308        *it = 0;
     309
     310        return buffer_dump;
     311}
     312
     313
    254314/**
    255315 * @}
Note: See TracChangeset for help on using the changeset viewer.