Exemplo n.º 1
0
void platform_uart_get_config(unsigned id, uint32_t *baudp, uint32_t *databitsp, uint32_t *parityp, uint32_t *stopbitsp) {
  UartConfig config =  uart_get_config(id);
  int i;

  int offset = config.baut_rate;

  for (i = 0; i < sizeof(uart_bitrates) / sizeof(uart_bitrates[0]); i++) {
    int diff = config.baut_rate - uart_bitrates[i];

    if (diff < 0) {
      diff = -diff;
    }

    if (diff < offset) {
       offset = diff;
       *baudp = uart_bitrates[i];
    }
  }

  switch( config.data_bits )
  {
    case FIVE_BITS:
      *databitsp = 5;
      break;
    case SIX_BITS:
      *databitsp = 6;
      break;
    case SEVEN_BITS:
      *databitsp = 7;
      break;
    case EIGHT_BITS:
    default:
      *databitsp = 8;
      break;
  }

  switch (config.stop_bits)
  {
    case ONE_HALF_STOP_BIT:
      *stopbitsp = PLATFORM_UART_STOPBITS_1_5;
      break;
    case TWO_STOP_BIT:
      *stopbitsp = PLATFORM_UART_STOPBITS_2;
      break;
    default:
      *stopbitsp = PLATFORM_UART_STOPBITS_1;
      break;
  }

  if (config.exist_parity == STICK_PARITY_DIS) {
    *parityp = PLATFORM_UART_PARITY_NONE;
  } else if (config.parity == EVEN_BITS) {
    *parityp = PLATFORM_UART_PARITY_EVEN;
  } else {
    *parityp = PLATFORM_UART_PARITY_ODD;
  }
}
Exemplo n.º 2
0
int s2e_web_get_sb_selected(char buf[],
                            int app_state,
                            int connection_state,
                            int stop_bits)
{
    chanend c_uart_config = (chanend) ((app_state_t *) app_state)->c_uart_config;

    int id = get_id_param(connection_state);
    if (id == -1)
        return 0;

    uart_config_data_t *data = uart_get_config(id);
    if (data->stop_bits == stop_bits)
    {
        char selstr[] = "selected";
        strcpy(buf, selstr);
        return strlen(selstr);
    }

    return 0;
}
Exemplo n.º 3
0
int s2e_web_configure(char buf[], int app_state, int connection_state)
{
    int err;
    int val;
    uart_config_data_t data;
    int telnet_port;
    chanend c_uart_config = (chanend) ((app_state_t *) app_state)->c_uart_config;
    chanend c_flash_data = (chanend) ((app_state_t *) app_state)->c_flash;
    char *err_msg;
    char *web_form_req;
    int flash_result;

    if (!web_server_is_post(connection_state))
        return 0;

    web_form_req = web_server_get_param("form_action", connection_state);

    if (strcmp(web_form_req, "Get") == 0)
    {
        return output_msg(buf, success_msg);
    }
    else if (strcmp(web_form_req, "Set") == 0)
    {
        val = get_int_param("id", connection_state, &err);
        if (err)
            return output_msg(buf, s2e_validation_bad_channel_id);

        data.channel_id = val;

        // Received Set request from web page
        val = get_int_param("pc", connection_state, &err);
        if (err)
            return output_msg(buf, s2e_validation_bad_parity_msg);
        data.parity = val;

        val = get_int_param("sb", connection_state, &err);
        if (err)
            return output_msg(buf, s2e_validation_bad_stop_bits_msg);
        data.stop_bits = val;

        val = get_int_param("br", connection_state, &err);
        if (err)
            return output_msg(buf, s2e_validation_bad_baudrate_msg);
        data.baud = val;

        val = get_int_param("cl", connection_state, &err);
        if (err)
            return output_msg(buf, s2e_validation_bad_char_len_msg);
        data.char_len = val;

        val = get_int_param("tp", connection_state, &err);
        if (err)
            return output_msg(buf, s2e_validation_bad_telnet_port_msg);

        telnet_port = val;

        data.polarity = 0;

        err_msg = s2e_validate_uart_config(&data);

        if (err_msg)
            return output_msg(buf, err_msg);

        err_msg = s2e_validate_telnet_port(data.channel_id, telnet_port);

        if (err_msg)
            return output_msg(buf, err_msg);

        // Do the setting

        uart_config_data_t *config = uart_get_config(data.channel_id);
        *config = data;
        uart_set_config(c_uart_config, &data);

        // We have to delay the changing of the telnet port until after the
        // page is rendered so we can use the tcp channel
        pending_telnet_port_change_id = data.channel_id;
        pending_telnet_port_change_port = telnet_port;

        return output_msg(buf, success_msg);

    } // Set
    else if (strcmp(web_form_req, "Save") == 0)
    {
        // Received Save request from web page
        send_cmd_to_flash_thread(c_flash_data, UART_CONFIG, FLASH_CMD_SAVE);

        for(int i = 0; i < NUM_UART_CHANNELS; i++)
        {
            uart_config_data_t *data1 = uart_get_config(i);
            send_data_to_flash_thread(c_flash_data, data1);
        }

        flash_result = get_flash_access_result(c_flash_data);

        if (flash_result == S2E_FLASH_OK)
        {
            return output_msg(buf, success_msg);
        }
        else
        {
            return output_msg(buf, error_msg);
        }

    } // Save
    else if (strcmp(web_form_req, "Restore") == 0)
    {
        uart_config_data_t data1;
        int telnet_port1;

        // Received Restore request from web page
        send_cmd_to_flash_thread(c_flash_data, UART_CONFIG, FLASH_CMD_RESTORE);

        flash_result = get_flash_access_result(c_flash_data);

        if (flash_result == S2E_FLASH_OK)
        {
            for (int i = 0; i < NUM_UART_CHANNELS; i++)
            {
                get_data_from_flash_thread(c_flash_data, &data1, &telnet_port1);

                uart_config_data_t *config = uart_get_config(data1.channel_id);
                *config = data1;
                uart_set_config(c_uart_config, &data1);

                // We have to delay the changing of the telnet port until after the
                // page is rendered so we can use the tcp channel
                pending_telnet_port_change_id = data1.channel_id;
                pending_telnet_port_change_port = telnet_port1;
            }
            return output_msg(buf, success_msg);
        }
        else
        {
            return output_msg(buf, error_msg);
        }

    } // Restore
    else
    {
        // invalid request
        return output_msg(buf, error_msg);
    }

    return 0;
}
static void execute_command(chanend c_xtcp,
                            chanend c_uart_config,
                            chanend c_flash_data,
                            xtcp_connection_t *conn,
                            connection_state_t *st)
{
  int out_channel_id;
  int flash_result;
  uart_config_data_t data1;
  int telnet_port1;

  switch (st->cmd)
    {
    case TELNET_CONFIG_CMD_SET:
      st->err = s2e_validate_uart_config(&st->config_in);
      if (st->err) {
        xtcp_init_send(c_xtcp, conn);
        return;
      }

      st->err = s2e_validate_telnet_port(st->config_in.channel_id,
                                         st->telnet_port_in);
      if (st->err) {
        xtcp_init_send(c_xtcp, conn);
        return;
      }

      uart_config_data_t *config = uart_get_config(st->config_in.channel_id);
      *config = st->config_in;
      uart_set_config(c_uart_config, &st->config_in);
      telnet_to_uart_set_port(c_xtcp,
                              st->config_in.channel_id,
                              st->telnet_port_in);

      out_channel_id = st->config_in.channel_id;
      break;
    case TELNET_CONFIG_CMD_GET:
      st->err = s2e_validate_channel_id(st->config_in.channel_id);
      if (st->err) {
        xtcp_init_send(c_xtcp, conn);
        return;
      }
      out_channel_id = st->config_in.channel_id;
      break;
    case TELNET_CONFIG_CMD_SAVE:
        // Received Save request from web page
        send_cmd_to_flash_thread(c_flash_data, UART_CONFIG, FLASH_CMD_SAVE);

        for(int i = 0; i < NUM_UART_CHANNELS; i++)
        {
            uart_config_data_t *data1 = uart_get_config(i);
            send_data_to_flash_thread(c_flash_data, data1);
        }

        flash_result = get_flash_access_result(c_flash_data);

        if (flash_result != S2E_FLASH_OK)
        {
            st->err = flash_err_msg;
            xtcp_init_send(c_xtcp, conn);
            return;
        }
        out_channel_id = 7;
        break;

    case TELNET_CONFIG_CMD_RESTORE:
        // Received Restore request from web page
        send_cmd_to_flash_thread(c_flash_data, UART_CONFIG, FLASH_CMD_RESTORE);
        flash_result = get_flash_access_result(c_flash_data);

        if (flash_result == S2E_FLASH_OK)
        {
            for (int i = 0; i < NUM_UART_CHANNELS; i++)
            {
                get_data_from_flash_thread(c_flash_data, &data1, &telnet_port1);
                uart_config_data_t *config = uart_get_config(data1.channel_id);
                *config = data1;
                uart_set_config(c_uart_config, &data1);
                telnet_to_uart_set_port(c_xtcp, data1.channel_id, telnet_port1);
            }
        }
        else
        {
            st->err = flash_err_msg;
            xtcp_init_send(c_xtcp, conn);
            return;
        }

        out_channel_id = 7;
        break;

    default:
      st->err = invalid_cmd_msg;
      xtcp_init_send(c_xtcp, conn);
      return;
      break;
  }
  st->config_out = *uart_get_config(out_channel_id);
  st->telnet_port_out = telnet_to_uart_get_port(out_channel_id);
  st->cmd_out = st->cmd;
  st->sending_ack = 1;
  xtcp_init_send(c_xtcp, conn);
}