<commit_before>#include "cmd-engine.h"

#include "main.h"
#include "hw-i80.h"
#include "hw-leds.h"
#include "hw-uart.h"
#include "line-editor-uart.h"

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <avr/pgmspace.h>

static void cmd_engine_reset (void);
static void cmd_engine_on_write_ready (int length);
static void cmd_engine_read (const char *aCommand);
static void cmd_engine_write (const char *aCommand);
static void cmd_engine_set_led (const char *aCommand);
static unsigned char glob_ch_to_val (unsigned char ch);
static void cmd_engine_on_cmd_ready (const char *aString);
static unsigned char glob_get_byte (const char *pData);
static void cmd_engine_on_read_ready (int length, const unsigned char *pData);

void cmd_engine_init (void)
{
  hw_i80_init ();
  hw_i80_set_read_callback (cmd_engine_on_read_ready);
  hw_i80_set_write_callback (cmd_engine_on_write_ready);
  line_editor_uart_init ();
  line_editor_uart_set_callback (cmd_engine_on_cmd_ready);
}

void cmd_engine_deinit (void)
{
  hw_i80_deinit ();
  line_editor_uart_deinit ();
}

void cmd_engine_start (void)
{
  line_editor_uart_start ();
}

void cmd_engine_on_cmd_ready (const char *aString)
{
  int start_uart_editor = 1;

  if (!strcmp_P (aString, PSTR("help")))
  {
    /* HELP command */
    hw_uart_write_string_P (PSTR("Supported cmds:\r\n"));
#if __linux__ == 1
    hw_uart_write_string_P (PSTR("> exit/quit - exit\r\n");
#endif /* __linux__ == 1 */
    hw_uart_write_string_P (PSTR("> reset - Reset LCD module\r\n"));
    hw_uart_write_string_P (PSTR("> L <IND> <1/0> - Turn ON/OFF the LEDs\r\n"));
    hw_uart_write_string_P (PSTR("> W <CMD> <DAT> - write <CMD> with <DAT> to I80\r\n"));
    hw_uart_write_string_P (PSTR("> R <CMD> <LEN> - read <LEN> bytes with <CMD> in I80\r\n"));
  }
#ifdef __linux__
  else if (!strcmp_P (aString, PSTR("quit")) || !strcmp_P (aString, PSTR("exit")))
  {
    /* EXIT command */
    main_request_exit ();
    start_uart_editor = 0;
  }
#endif /* __X86__ */
  else if (!strncmp_P (aString, PSTR("W "), 2))
  {
    /* WRITE command */
    cmd_engine_write (&aString[2]);
    start_uart_editor = 0;
  }
  else if (!strncmp_P (aString, PSTR("R "), 2))
  {
    /* READ command */
    cmd_engine_read (&aString[2]);
    start_uart_editor = 0;
  }
  else if (!strncmp_P (aString, PSTR("L "), 2))
  {
    cmd_engine_set_led (&aString[2]);
  }
  else if (!strcmp_P (aString, PSTR("reset")))
  {
    cmd_engine_reset ();
  }
  else if (*aString)
  {
    /* got unrecognized non-empty command */
    hw_uart_write_string_P (PSTR("ENGINE: unrecognized command. Type 'help'.\r\n"));
  }

  if (start_uart_editor)
  {
    line_editor_uart_start ();
  }
}

void cmd_engine_read (const char *aCommand)
{
  int success = 1;
  unsigned char command = 0;
  unsigned char dataLength = 0;

  /* first, retrieve the command code */
  if (isxdigit (aCommand[0]) && isxdigit (aCommand[1]))
  {
    command = strtoul (aCommand, NULL, 16);
    aCommand += 2;
  }
  else
  {
    success = 0;
  }

  if (success)
  {
    /* skip whitespace */
    while (isspace(aCommand[0]))
    {
      ++aCommand;
    }

    const char ch0 = aCommand[0];
    if (ch0 && isxdigit (ch0))
    {
      char *end = NULL;
      dataLength = strtoul (aCommand, &end, 16);
      success = ((dataLength < 11) && end && !(*end));
      if (!success)
      {
        dataLength = 0;
      }
    }
    else if (ch0)
    {
      success = 0;
    }
  }
<commit_msg>Corrected maximum data length of the read command<commit_after>#include "cmd-engine.h"

#include "main.h"
#include "hw-i80.h"
#include "hw-leds.h"
#include "hw-uart.h"
#include "line-editor-uart.h"

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <avr/pgmspace.h>

static void cmd_engine_reset (void);
static void cmd_engine_on_write_ready (int length);
static void cmd_engine_read (const char *aCommand);
static void cmd_engine_write (const char *aCommand);
static void cmd_engine_set_led (const char *aCommand);
static unsigned char glob_ch_to_val (unsigned char ch);
static void cmd_engine_on_cmd_ready (const char *aString);
static unsigned char glob_get_byte (const char *pData);
static void cmd_engine_on_read_ready (int length, const unsigned char *pData);

void cmd_engine_init (void)
{
  hw_i80_init ();
  hw_i80_set_read_callback (cmd_engine_on_read_ready);
  hw_i80_set_write_callback (cmd_engine_on_write_ready);
  line_editor_uart_init ();
  line_editor_uart_set_callback (cmd_engine_on_cmd_ready);
}

void cmd_engine_deinit (void)
{
  hw_i80_deinit ();
  line_editor_uart_deinit ();
}

void cmd_engine_start (void)
{
  line_editor_uart_start ();
}

void cmd_engine_on_cmd_ready (const char *aString)
{
  int start_uart_editor = 1;

  if (!strcmp_P (aString, PSTR("help")))
  {
    /* HELP command */
    hw_uart_write_string_P (PSTR("Supported cmds:\r\n"));
#if __linux__ == 1
    hw_uart_write_string_P (PSTR("> exit/quit - exit\r\n");
#endif /* __linux__ == 1 */
    hw_uart_write_string_P (PSTR("> reset - Reset LCD module\r\n"));
    hw_uart_write_string_P (PSTR("> L <IND> <1/0> - Turn ON/OFF the LEDs\r\n"));
    hw_uart_write_string_P (PSTR("> W <CMD> <DAT> - write <CMD> with <DAT> to I80\r\n"));
    hw_uart_write_string_P (PSTR("> R <CMD> <LEN> - read <LEN> bytes with <CMD> in I80\r\n"));
  }
#ifdef __linux__
  else if (!strcmp_P (aString, PSTR("quit")) || !strcmp_P (aString, PSTR("exit")))
  {
    /* EXIT command */
    main_request_exit ();
    start_uart_editor = 0;
  }
#endif /* __X86__ */
  else if (!strncmp_P (aString, PSTR("W "), 2))
  {
    /* WRITE command */
    cmd_engine_write (&aString[2]);
    start_uart_editor = 0;
  }
  else if (!strncmp_P (aString, PSTR("R "), 2))
  {
    /* READ command */
    cmd_engine_read (&aString[2]);
    start_uart_editor = 0;
  }
  else if (!strncmp_P (aString, PSTR("L "), 2))
  {
    cmd_engine_set_led (&aString[2]);
  }
  else if (!strcmp_P (aString, PSTR("reset")))
  {
    cmd_engine_reset ();
  }
  else if (*aString)
  {
    /* got unrecognized non-empty command */
    hw_uart_write_string_P (PSTR("ENGINE: unrecognized command. Type 'help'.\r\n"));
  }

  if (start_uart_editor)
  {
    line_editor_uart_start ();
  }
}

void cmd_engine_read (const char *aCommand)
{
  int success = 1;
  unsigned char command = 0;
  unsigned char dataLength = 0;

  /* first, retrieve the command code */
  if (isxdigit (aCommand[0]) && isxdigit (aCommand[1]))
  {
    command = strtoul (aCommand, NULL, 16);
    aCommand += 2;
  }
  else
  {
    success = 0;
  }

  if (success)
  {
    /* skip whitespace */
    while (isspace(aCommand[0]))
    {
      ++aCommand;
    }

    const char ch0 = aCommand[0];
    if (ch0 && isxdigit (ch0))
    {
      char *end = NULL;
      dataLength = strtoul (aCommand, &end, 16);
      success = ((dataLength < 17) && end && !(*end));
      if (!success)
      {
        dataLength = 0;
      }
    }
    else if (ch0)
    {
      success = 0;
    }
  }

{'fraction_non_alphanumeric': 0.1455223880597015, 'fraction_numerical': 0.014638346727898967, 'mean_word_length': 3.525324675324675, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5676422', 'n_tokens_mistral': 2764, 'n_tokens_neox': 2670, 'n_words': 621}