utility/buffer_dump/main.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
/* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/utility/buffer-dump
*/
#include "gos.h"
#define APPLICATION_START_LINE "\r\nBuffer Dump example starting ..."
GOS_CMD_CREATE_COMMAND(buffer_dump, print_help, "usage", "?", 0, 0, true);
GOS_CMD_CREATE_COMMAND(buffer_dump, dump, "dump", "dump", 0, 12, false);
/*************************************************************************************************/
void gos_app_init(void)
{
GOS_LOG(APPLICATION_START_LINE);
GOS_LOG("Issue command: 'usage' for more details");
}
/*************************************************************************************************/
GOS_DEFINE_COMMAND(buffer_dump, print_help)
{
static const char help_message[] =
"Usage:\r\n" \
"Dump a dummy buffer with specified formatting\r\n" \
"dump [-r <1-32>] [-w <1-4] [-s <0/1>] [-a <0/1>] [-c <0/1>] [-e <b/l>] [-l <1-1024>]\r\n" \
"where:\r\n" \
"-r <1-32> : Number of words per row: 1,2,4,8,16,32. Default: 8\r\n" \
"-w <1-4> : Bytes per word: 1,2,4. Default: 4\r\n" \
"-s <0/1> : Add space between words. Default: 1\r\n" \
"-a <0/1> : Print addresses. Default: 1\r\n" \
"-c <0/1> : Print row ascii. Default: 1\r\n" \
"-e <b/l> : Word endianess, b = big, l = little. Default: little\r\n" \
"-l <1-1024> : Size of dummy buffer. Default: 128 bytes\r\n";
gos_cmd_write_response(GOS_CMD_SUCCESS, help_message, sizeof(help_message)-1);
}
/*************************************************************************************************/
GOS_DEFINE_COMMAND(buffer_dump, dump)
{
uint16_t dump_size = 128;
uint32_t dump_flags = GOS_DUMP_FLAGS(8, 4, LITTLE, ADD_SPACE, PRINT_ADDRESSES, PRINT_ASCII);
for (; argc > 0; argc -= 2, argv += 2)
{
if(argc < 2 || argv[0][0] != '-')
{
}
switch(argv[0][1])
{
case 'l':
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, dump_size, argv[1], 1, 1024);
}
break;
case 'r':
{
GOS_CMD_PARSE_INT_ARG(uint8_t, row_len, argv[1], 1, 32);
dump_flags &= ~GOS_DUMP_ROW_MASK;
dump_flags |= (gos_log2_uint32(row_len) << GOS_DUMP_ROW_LEN_OFFSET);
}
break;
case 'w':
{
GOS_CMD_PARSE_INT_ARG(uint8_t, word_len, argv[1], 1, 4);
dump_flags &= ~GOS_DUMP_WORD_LEN_MASK;
dump_flags |= (gos_log2_uint32(word_len) << GOS_DUMP_WORD_LEN_OFFSET);
}
break;
case 's':
{
dump_flags &= ~GOS_DUMP_SPACE_MASK;
dump_flags |= ((argv[1][0] == '1') << GOS_DUMP_SPACE_OFFSET);
}
break;
case 'a':
{
dump_flags &= ~GOS_DUMP_ADDRESS_MASK;
dump_flags |= ((argv[1][0] == '1') << GOS_DUMP_ADDRESS_OFFSET);
}
break;
case 'c':
{
dump_flags &= ~GOS_DUMP_ASCII_MASK;
dump_flags |= ((argv[1][0] == '1') << GOS_DUMP_ASCII_OFFSET);
}
break;
case 'e':
{
dump_flags &= ~GOS_DUMP_ENDIAN_MASK;
dump_flags |= ((argv[1][0] == 'b') << GOS_DUMP_ENDIAN_OFFSET);
}
break;
}
}
char *buffer;
gos_malloc("buf_dump", (uint8_t**)&buffer, dump_size);
char *ptr = buffer;
char c = 32;
for (int remaining = dump_size; remaining > 0; --remaining)
{
*ptr++ = c++;
if (c == 127)
{
c = 32;
}
}
GOS_LOG("Flags: %03X", dump_flags);
gos_dump_buffer(buffer, dump_size, "Buffer dump", dump_flags);
gos_free(buffer);
}