/*
 * init.c
 * Description:  This minimal app brings up the network using the RTEMS
 * libbsd network stack and starts the RTEMS shell on the 
 * console and a telnet daemon.  
 *
 * This code has been tested on the xilinx_zynq_a9_qemu BSP running on Qemu 
 * hosted on CentOS 7.
 *
 * Known problems on Zynq qemu:
 *  -The shell reports that it cannont set the terminal attributes.
 *  -After a timeout period RTEMS reports that the ethernet device fails to add
 *   a route.
 * 
 * Version 1.1
 */
#include <rtems.h>
#include <stdio.h>
#include <stdlib.h>

#include <rtems/rtl/rtl-shell.h>
#include <rtems/telnetd.h>
#include <rtems/shell.h>

#include "network_init.h"

rtems_shell_env_t shell_env;

static void telnetd_command( char* device, void *config_table) {
  rtems_shell_dup_current_env(&shell_env);
  shell_env.taskname = "TLNT";
  shell_env.devname = device;
  
  rtems_shell_main_loop(&shell_env);
}

rtems_telnetd_config_table rtems_telnetd_config = {
  telnetd_command,              /* Command */
  NULL,                         /* Argument */
  50,                           /* Priority */
  8 * RTEMS_MINIMUM_STACK_SIZE, /* task stack size */
  0,                            /* Login check */
  0                             /* Keep stdio of the caller */
};

static rtems_task Init(
  rtems_task_argument ignored
) {

  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = initialize_bsd_network();
  if (sc != RTEMS_SUCCESSFUL) 
  {
    fprintf(stderr,
      "Network init failed.  status is %s\n",
      rtems_status_text(sc));
    exit(1);
  }
  
  rtems_shell_init_environment();
  
  sc = rtems_telnetd_initialize();
  if (sc != RTEMS_SUCCESSFUL)
  {
    fprintf(stderr,
      "=== RTEMS telnet daemon failed with status code %s ===\n",
       rtems_status_text(sc));
  }
  else
  {
    fprintf(stdout,"\nTelnet up and running on port 23\n");
  }

  printf(" =========================\n");
  printf(" starting shell\n");
  printf(" =========================\n");
  rtems_shell_init(
    "SHLL",                       /* task name */
    RTEMS_MINIMUM_STACK_SIZE * 4, /* task stack size */
    100,                          /* task priority */
    "/dev/console",               /* device name */
    false,                        /* run forever */
    true,                         /* wait for shell to terminate */
    NULL                          /* login check function,
                                     use NULL to disable a login check */
  );
  
  exit(0);
}

#include <bsp.h> /* for device driver prototypes */

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_UNLIMITED_OBJECTS
#define CONFIGURE_UNIFIED_WORK_AREAS
#define CONFIGURE_MINIMUM_TASK_STACK_SIZE (64 * 1024)

#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 256

#define CONFIGURE_SHELL_COMMANDS_INIT

#define CONFIGURE_SHELL_COMMAND_CPUINFO
#define CONFIGURE_SHELL_COMMAND_CPUUSE
#define CONFIGURE_SHELL_COMMAND_PERIODUSE
#define CONFIGURE_SHELL_COMMAND_STACKUSE
#define CONFIGURE_SHELL_COMMAND_PROFREPORT

#define CONFIGURE_SHELL_COMMAND_CP
#define CONFIGURE_SHELL_COMMAND_PWD
#define CONFIGURE_SHELL_COMMAND_LS
#define CONFIGURE_SHELL_COMMAND_LN
#define CONFIGURE_SHELL_COMMAND_LSOF
#define CONFIGURE_SHELL_COMMAND_CHDIR
#define CONFIGURE_SHELL_COMMAND_CD
#define CONFIGURE_SHELL_COMMAND_MKDIR
#define CONFIGURE_SHELL_COMMAND_RMDIR
#define CONFIGURE_SHELL_COMMAND_CAT
#define CONFIGURE_SHELL_COMMAND_MV
#define CONFIGURE_SHELL_COMMAND_RM
#define CONFIGURE_SHELL_COMMAND_MALLOC_INFO
#define CONFIGURE_SHELL_COMMAND_SHUTDOWN

#include <rtems/shellconfig.h>

#define CONFIGURE_INIT
#include <rtems/confdefs.h>



