/**
 * @file      nr_micro_shell_commands.c
 * @author    Nrush
 * @version   V0.1
 * @date      28 Oct 2019
 * *****************************************************************************
 * @attention
 * 
 * MIT License
 * 
 * Copyright (C) 2019 Nrush. or its affiliates.  All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Includes ------------------------------------------------------------------*/
#include "sr_config.h"

#include "nr_micro_shell.h"

#include <ctype.h>

#include <string.h>
#include <stdlib.h>
#include "sr_stddef.h"

/**
 * @brief ls command
 */
int shell_ls_cmd(int argc, char *argv[])
{
	unsigned int i = 0;
	if (argc > 1)
	{
		if (!strcmp("cmd", argv[1]))
		{

			for (i = 0; nr_shell.static_cmd[i].fp != NULL; i++)
			{
				shell_printf("%s",nr_shell.static_cmd[i].cmd);
				shell_printf("\r\n");
			}
		}
		else if (!strcmp("-v", argv[1]))
		{
			shell_printf("ls version 1.0.\r\n");
		}
		else if (!strcmp("-h", argv[1]))
		{
			shell_printf("useage: ls [options]\r\n");
			shell_printf("options: \r\n");
			shell_printf("\t -h \t: show help\r\n");
			shell_printf("\t -v \t: show version\r\n");
			shell_printf("\t cmd \t: show all commands\r\n");
		}
	}
	else
	{
		shell_printf("ls need more arguments!\r\n");
	}

	return 0;
}
NR_SHELL_CMD_EXPORT(ls, shell_ls_cmd);




/**
 * @brief test command
 */
int shell_test_cmd(int argc, char *argv[])
{
	unsigned int i;
	shell_printf("test command:\r\n");
	for (i = 0; i < argc; i++)
	{
		shell_printf("paras %d: %s\r\n", i,argv[i]);
	}

	return  0;
}
NR_SHELL_CMD_EXPORT(test, shell_test_cmd);




/**
 * @brief 打印某块连续地址区域的数据（如数组、RAM或FLASH），会显示地址和对应ascii
 * 
 * @param[in] src  数据地址
 * @param[in] len  要打印的数据长度
 * @param[in] addr 数据的实际地址
 *
 * @return 无
 */
void ShellShowData(uint8_t *src,uint16_t len,uint32_t addr)
{
  	#define __is_print(ch)   ((unsigned int)((ch) - ' ') < 127u - ' ')
		
	uint32_t i, j;
	//uint8_t* addr = src;
  	shell_printf("Start: 0x%08X  Size: %u\r\n", (uint32_t)addr,len);
	shell_printf("Offset (H) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n");
	for (i = 0; i < len; i += 16)
	{
		shell_printf("[%08X] ", (uint32_t)addr + i);
		/* dump hex */
		for (j = 0; j < 16; j++)
		{
			if (i + j < len)
			{
				shell_printf("%02X ", src[i + j]);
			}
			else
			{
				shell_printf("   ");
			}
		}
		/* dump char for hex */
		for (j = 0; j < 16; j++)
		{
			if (i + j < len)
			{
				shell_printf("%c", __is_print(src[i + j]) ? src[i + j] : '.');
			}
		}
		shell_printf("\r\n");
	}
	shell_printf("\r\n");
}


//cmd: show <addr> <len>
int _ShellShow(int argc, char *argv[])
{
	if(argc <3)
	{
		shell_printf("cmd: show <addr> <len>");
		return -1;
	}

	uint8_t *src = (uint8_t *)atoi(argv[1]);
	ShellShowData(src,atoi(argv[2]),(uint32_t)src);
	return  0;
}
NR_SHELL_CMD_EXPORT(show, _ShellShow);



/******************* (C) COPYRIGHT 2019 Nrush *****END OF FILE*****************/
