/*********************************************************************
*
*	TITLE:	TOPMENU
*
*	DATE:	10-05-90
*
*	ABSTRACT: This module contains menu features of the demo
*             program for the DOSRMX Real Time Extensions.
*
*
*   MODIFIED: 
*
***********************************************************************/

/*
 *			 INTEL CORPORATION PROPRIETARY INFORMATION
 *
 *		  This software is supplied under the terms of a 
 *		  license agreement or nondisclosure agreement with
 *		  Intel Corporation and may not be copied or disclosed
 *		  except in accordance with the terms of that agreement.
 *
 */
#include <stdio.h>
#include <stdlib.h>

#define PAGE_SIZE 25
#define TRUE 0xff
#define FALSE 0x00
static char menu_0a[] = {
"\n\n"
"                  DOS/iRMX Real Time Extensions Demo Program\n"
"                  ==========================================\n\n"
"                  1. Mailboxes (Objects) Functions\n\n"
"                  2. Mailboxes (data) Functions\n\n"
"                  3. Semaphore Functions\n\n"
"                  4. Segment Functions\n\n"};
static char menu_0b[] = {
"                  5. Descriptor Functions\n\n"
"                  6. Data Transfer Functions\n\n"
"                  7. Display Help on above functions\n\n"
"                  8. Exit (terminate program)\n\n"
"                  Enter option (1 to 8) :- "};

static char menu_1[] = {
"\n\n"
"                  Object Mailbox Functions\n"
"                  ========================\n\n"
"                  1. Send object to mailbox\n\n"
"                  2. Receive object from mailbox\n\n"
"                  3. RETURN to previous memu\n\n"
"                  Enter option (1 to 3) :- "};

static char menu_2[] = {
"\n\n"
"                  Data Mailbox Functions\n"
"                  ======================\n\n"
"                  1. Send data to mailbox\n\n"
"                  2. Receive data from mailbox\n\n"
"                  3. RETURN to previous memu\n\n"
"                  Enter option (1 to 3) :- "};

static char menu_3[] = {
"\n\n"
"                  Semaphore Functions\n"
"                  ===================\n\n"
"                  1. Send units to a semaphore\n\n"
"                  2. Receive units from a semaphore\n\n"
"                  3. RETURN to previous memu\n\n"
"                  Enter option (1 to 3) :- "};

static char menu_4[] = {
"\n\n"
"                  PVAM Segment Functions\n"
"                  ======================\n\n"
"                  1. Create a PVAM Segment\n\n"
"                  2. Delete a PVAM Segment\n\n"
"                  3. Display a PVAM Segment\n\n"
"                  4. RETURN to previous memu\n\n"
"                  Enter option (1 to 4) :- "};

static char menu_5[] = {
"\n\n"
"                  Descriptor Functions\n"
"                  ====================\n\n"
"                  1. Create a Descriptor\n\n"
"                  2. Delete a Descriptor\n\n"
"                  3. Display a Descriptor\n\n"
"                  4. RETURN to previous memu\n\n"
"                  Enter option (1 to 4) :- "};

static char menu_6[] = {
"\n\n"
"                  REAL MODE/PVAM Copy Functions\n"
"                  =============================\n\n"
"                  1. Copy PVAM segment to real mode address\n\n"
"                  2. Copy Real mode address to PVAM segment\n\n"
"                  3. RETURN to previous memu\n\n"
"                  Enter option (1 to 3) :- "};

static char help_menu_0[] = {
"\n\n"
"                           Help Menu\n"
"                           =========\n\n"
"                  1. Mailboxes (Objects) Functions\n\n"
"                  2. Mailboxes (data) Functions\n\n"
"                  3. Semaphore Functions\n\n"
"                  4. Segment Functions\n\n"
"                  5. Descriptor Functions\n\n"
"                  6. Data Transfer Functions\n\n"
"                  7. RETURN to previous menu\n\n"
"                  Enter option (1 to 7) :- "};

static char error_message_0[] = {
"\n\n"
"Only characters between '1' and '8' are valid\n"};

static char error_message_1[] = {
"\n\n"
"Only characters between '1' and '3' are valid\n"};

static char error_message_2[] = {
"\n\n"
"Only characters between '1' and '4' are valid\n"};

int debug_char;

/*
** Common error message routine
*/
void error_message(char error_text[])
{
int character;

	printf("\n");

	printf("%s",error_text);

	printf("press any key to continue");

	character = getche(); /* wait for character */

	printf("\n");
}

/******************************************************************
*
*    TITLE: OBJECT_MAILBOX_FUNCTIONS
*
*    CALLING SEQUENCE: object_mailbox_functions();
*
*    INTERFACE VARIABLES: NONE
*
*    PARAMETERS:
*
*    GLOBAL VARIABLES:
*
*
*    ABSTRACT: This function provides the object mailbox functions 
*    and menu.
*
******************************************************************/
void object_mailbox_functions()
{
int choice;
int loop;

	loop = TRUE;

	while(loop !=FALSE)
	{
		clear_screen();
		printf("%s",menu_1); /* sub menu 1 */
		debug_char = getche();
		switch (debug_char)
		{
		case '1':	send_object_to_mailbox();
					break;

		case '2':	receive_object_from_mailbox();
					break;

		case '3':	loop = FALSE;
					break;

		default:	error_message(error_message_1);
					break;
		}
	}
}

/******************************************************************
*
*    TITLE: DATA_MAILBOX_FUNCTIONS
*
*    CALLING SEQUENCE: data_mailbox_functions();
*
*    INTERFACE VARIABLES: NONE
*
*    PARAMETERS:
*
*    GLOBAL VARIABLES:
*
*
*    ABSTRACT: This function provides the data mailbox functions 
*    and menu.
*
******************************************************************/
void data_mailbox_functions()
{
int choice;
int loop;

	loop = TRUE;

	while(loop !=FALSE)
	{
		clear_screen();
		printf("%s",menu_2); /* sub menu 2 */
		debug_char = getche();
		switch (debug_char)
		{
		case '1':	send_data_to_mailbox();
					break;

		case '2':	receive_data_from_mailbox();
					break;

		case '3':	loop = FALSE;
					break;

		default:	error_message(error_message_1);
					break;
		}
	}
}

/******************************************************************
*
*    TITLE: SEMAPHORE_FUNCTIONS
*
*    CALLING SEQUENCE: semaphore_functions();
*
*    INTERFACE VARIABLES: NONE
*
*    PARAMETERS:
*
*    GLOBAL VARIABLES:
*
*
*    ABSTRACT: This function provides the semaphore functions 
*    and menu.
*
******************************************************************/
void semaphore_functions()
{
int choice;
int loop;

	loop = TRUE;

	while(loop !=FALSE)
	{
		clear_screen();
		printf("%s",menu_3); /* sub menu 3 */
		debug_char = getche();
		switch (debug_char)
		{
		case '1':	send_units_to_semaphore();
					break;

		case '2':	receive_units_from_semaphore();
					break;

		case '3':	loop = FALSE;
					break;

		default:	error_message(error_message_1);
					break;
		}
	}
}

/******************************************************************
*
*    TITLE: SEGMENT_FUNCTIONS
*
*    CALLING SEQUENCE: segment_functions();
*
*    INTERFACE VARIABLES: NONE
*
*    PARAMETERS:
*
*    GLOBAL VARIABLES:
*
*
*    ABSTRACT: This function provides the segment functions and
*    menu.
*
******************************************************************/
void segment_functions()
{
int choice;
int loop;

	loop = TRUE;

	while(loop !=FALSE)
	{
		clear_screen();
		printf("%s",menu_4); /* sub menu 4 */
		debug_char = getche();
		switch (debug_char)
		{
		case '1':	create_PVAM_segment();
					break;

		case '2':	delete_PVAM_segment();
					break;

		case '3':	display_segment();
					break;

		case '4':	loop = FALSE;
					break;

		default:	error_message(error_message_2);
					break;
		}
	}
}

/******************************************************************
*
*    TITLE: DESCRIPTOR_FUNCTIONS
*
*    CALLING SEQUENCE: descriptor_functions();
*
*    INTERFACE VARIABLES: NONE
*
*    PARAMETERS:
*
*    GLOBAL VARIABLES:
*
*
*    ABSTRACT: This function provides the descriptor functions 
*    and menu.
*
******************************************************************/
void descriptor_functions()
{
int choice;
int loop;

	loop = TRUE;

	while(loop !=FALSE)
	{
		clear_screen();
		printf("%s",menu_5); /* sub menu 5 */
		debug_char = getche();
		switch (debug_char)
		{
		case '1':	create_descriptor();
					break;

		case '2':	delete_descriptor();
					break;

		case '3':	display_segment();
					break;

		case '4':	loop = FALSE;
					break;

		default:	error_message(error_message_2);
					break;
		}
	}
}

/******************************************************************
*
*    TITLE: DATA_TRANSFER_FUNCTIONS
*
*    CALLING SEQUENCE: data_transfer_functions();
*
*    INTERFACE VARIABLES: NONE
*
*    PARAMETERS:
*
*    GLOBAL VARIABLES:
*
*
*    ABSTRACT: This function provides the data transfer functions 
*    and menu.
*
******************************************************************/
void data_transfer_functions()
{
int choice;
int loop;

	loop = TRUE;

	while(loop !=FALSE)
	{
		clear_screen();
		printf("%s",menu_6); /* sub menu 6 */
		debug_char = getche();
		switch (debug_char)
		{
		case '1':	copy_PVAM_to_real_mem();
					break;

		case '2':	copy_real_to_PVAM_mem();
					break;

		case '3':	loop = FALSE;
					break;

		default:	error_message(error_message_1);
					break;
		}
	}
}


/******************************************************************
*
*    TITLE: MAIN
*
*    CALLING SEQUENCE: main(int argc, char * argv[]);
*
*    INTERFACE VARIABLES: argc INT - the number of parameters passed
*                                    to the program.
*                         argv[] * - an array of pointer to the parameters
*                                    passed to the program. The parameters
*                                    are of text string format.
*
*    PARAMETERS:
*
*    GLOBAL VARIABLES:
*
*
*    ABSTRACT: This function is the main entry point of the demo
*    program.
*
******************************************************************/
int main (int argc, char * argv[])
{
int forever;

	initialisation();

	forever = 1;

	while (forever)
	{
		clear_screen();

		printf("%s",menu_0a);
		printf("%s",menu_0b);

		
		switch (getche())
		{
			case '1':	object_mailbox_functions();
						break;

			case '2':	data_mailbox_functions();
						break;

			case '3':	semaphore_functions();
						break;

			case '4':	segment_functions();
						break;

			case '5':	descriptor_functions();
						break;

			case '6':	data_transfer_functions();
						break;

			case '7':	help_functions();
						break;

			case '8':	forever = 0;
						remove_all_objects();
						remove_my_name();
						printf("\n");
						break;

			default:	error_message(error_message_0);
						break;
		}
	}
	exit(0);
}
