/********************************************************************
   loginconsole.c
   Z-World, 2001

	This program can be used for boards that have a second flash.

	This sample program is similar to the BASICCONSOLE.C sample
	program, but it adds the commands to handling logging in.

	After setting the "LOGIN NAME" and "LOGIN PASSWORD" type
	logout or restart the controller.  You will then be prompted
	for a name and password.

********************************************************************/

/*
 * Size of the buffers for serial port C.  If you want to use
 * another serial port, you should change the buffer macros below
 * appropriately (and change the console_io[] array below).
 */
#define CINBUFSIZE		1023
#define COUTBUFSIZE		255

/*
 * Maximum number of entries in the TCP servers' object list (in
 * this case, the total number of variables and files that can
 * be created)
 */
#define SSPEC_MAXSPEC  5

/*
 * Number of logical extents in the filesystem
 */
#define FS_MAX_LX  2

/*
 * Maximum number of files on the filesystem
 */
#define FS_MAX_FILES (SSPEC_MAXSPEC + 2)

/*
 * Log(base 2) of the desired Logical Sector size.
 */
#define MY_LS_SHIFT 12	// 2^12 == 4096

/*
 * The number of console I/O streams that this program supports.
 * Since we are only supporting serial port C, then there is only
 * one I/O stream.
 */
#define NUM_CONSOLES 1

/*
 * Defines the message that is displayed on all I/O channels when
 * the console starts up.
 */
#define CON_INIT_MESSAGE "BasicConsole Version 1.0\r\n"

#memmap xmem

#use "fs2.lib"

/*
 * Note that all libraries that zconsole.lib needs must be #use'd
 * before #use'ing zconsole.lib .
 */
#use "zconsole.lib"

/*
 * This function prototype is for a custom command, so it must be
 * declared before the console_command[] array.
 */
int hello_world(ConsoleState* state);

/*
 * This array defines which I/O streams for which the console will
 * be available.  The streams included below are defined through
 * macros.  Available macros are CONSOLE_IO_SERA, CONSOLE_IO_SERB,
 * CONSOLE_IO_SERC, CONSOLE_IO_SERD, CONSOLE_IO_TELNET, and
 * CONSOLE_IO_SP (for the slave port).  The parameter for the macro
 * represents the initial baud rate for serial ports, the port
 * number for telnet, or the channel number for the slave port.
 * It is possible for the user to define her own I/O handlers and
 * include them in a ConsoleIO structure in the console_io array.
 * Remember that if you change the number of I/O streams here, you
 * should also change the NUM_CONSOLES macro above.
 */
const ConsoleIO console_io[] =
{
	CONSOLE_IO_SERC(57600)
};

/*
 * This array defines the commands that are available in the console.
 * The first parameter for the ConsoleCommand structure is the
 * command specification--that is, the means by which the console
 * recognizes a command.  The second parameter is the function
 * to call when the command is recognized.  The third parameter is
 * the location of the #ximport'ed help file for the command.
 */
const ConsoleCommand console_commands[] =
{
	{ "LOGIN NAME", con_loginname, 0 },
	{ "LOGIN PASSWORD", con_loginpassword, 0 },
	{ "LOGOUT", con_logout, 0 },
	{ "HELLO WORLD", hello_world, 0 },
	{ "ECHO", con_echo, 0 }
};

/*
 * This array sets up the error messages that can be generated.
 * CON_STANDARD_ERRORS is a macro that expands to the standard
 * errors that the built-in commands in zconsole.lib uses.  Users
 * can define their own errors here, as well.
 */
const ConsoleError console_errors[] = {
	CON_STANDARD_ERRORS
};

/*
 * This array defines the information (such as configuration) that
 * will be saved to the filesystem.  Note that if, for example, the
 * HTTP or SMTP related commands are include in the console_commands
 * array above, then the backup information must be included in
 * this array.  The entries below are macros that expand to the
 * appropriate entry for each set of functionality.  Users can also
 * add their own information to be backed up here by adding more
 * ConsoleBackup structures.
 */
const ConsoleBackup console_backup[] =
{
	CONSOLE_BASIC_BACKUP,
	CONSOLE_LOGIN_BACKUP
};

/*
 * This is a custom command.  Custom commands always take a
 * ConsoleState* as an argument (a pointer to the state structure
 * for the given I/O stream), and return an int.  The return value
 * should be 0 when the command wishes to be called again on the
 * next console_tick(), 1 when the command has successfully
 * finished processing, or -1 when the command has finished due
 * to an error.
 */
int hello_world(ConsoleState* state)
{
	state->conio->puts("Hello, World!\r\n");
	return 1;
}

void main(void)
{
	FSLXnum ext1, ext2;
	long lxsize;
	long backupsize;
	word newsize;

	/* File system setup and partitioning */

	ext1 = fs_get_flash_lx();
	if (ext1 == 0) {
		printf("No flash available!\n");
		exit(1);
	}
	/*
	 * Get the size of the entire flash with the given sector size
	 */
	lxsize = fs_get_lx_size(ext1, 1, MY_LS_SHIFT);
	/*
	 * Calculate how much space we will need for the configuration data.
	 * We will need two files of configuration data with a sector of metadata
	 * for each file.  We also need one extra free sector, and another sector
	 * to ensure rounding down in partitioning does not leave us with too
	 * little space.
	 */
	backupsize = ((con_backup_bytes() + (1 << MY_LS_SHIFT)) * 2) +
	             (1 << MY_LS_SHIFT) + (1 << MY_LS_SHIFT);
	/*
	 * Calculate the fraction of space we need for configuration data
	 */
	newsize = (word)((backupsize * 65536L) / lxsize);
	/*
	 * Partition the filesystem
	 */
	ext2 = fs_setup(ext1, MY_LS_SHIFT, 0, NULL, FS_PARTITION_FRACTION,
	                newsize, MY_LS_SHIFT, 0, NULL);
	if (ext2 == 0) {
		printf("Could not create backup extent!\n");
		exit(1);
	}

	/*
	 * Set the logical extents for the console files and the backup
	 * configuration data.  These are placed in separate logical
	 * extents so that the existence of enough space for the backup
	 * data can be ensured.  These functions must be called before
	 * console_init() so that the console can know where to look for
	 * its backup data.
	 */
	con_set_files_lx(ext1);
	con_set_backup_lx(ext2);

	/*
	 * Initialize the file system.
	 */
	fs_init(0, 0);

	/*
	 * Initialize the console
	 */
	if (console_init() != 0) {
		printf("Console did not initialize!\n");
		lx_format(ext1, 0);
		lx_format(ext2, 0);

		/*
		 * Save the backup information to the console.
		 */
		con_backup();
	}

	while (1) {
		/*
		 * console_tick() drives the console.
		 */
		console_tick();
	}
}
