Esempio n. 1
0
static void
pm_lcd_on(void)
{
    struct pm_softc *sc = pm_softc;

    DPRINTF(("pm: LCD on\n"));

    if (sc->lcd_dev != NODEV && !sc->lcd_on) {
        device_control(sc->lcd_dev, DEVCTL_PM_LCDON, NULL);
        pm_update_timer();
        sc->lcd_on = 1;
    }
}
Esempio n. 2
0
static void
pm_lcd_off(void)
{
    struct pm_softc *sc = pm_softc;

    DPRINTF(("pm: LCD off\n"));

    if (sc->lcd_dev != NODEV && sc->lcd_on) {
        device_control(sc->lcd_dev, DEVCTL_PM_LCDOFF, NULL);
        if (sc->sustime == 0)
            pm_stop_timer();
        sc->lcd_on = 0;
    }
}
int main(void)
{
	int stage = 0;	/*In stage 0 the server sends the client the list of titles.  In stage 1 the server burns a specific title*/
	int temp = 0, current_index = 0;
	int mobile_app_address_length = 0;
	bool user_input_error = false;
	int i = 0, j = 0, socket_file_desc_1 = 0, socket_file_desc_2 = 0, acceptsocket_file_desc_1 = 0, err_check = 0, title_ID_number = 0, disc_burn = 0, disc_return = 0;
	char *token;
	char read_buffer[MAXCHARSIZE], write_buffer[MAXCHARSIZE], user_input[MAXCHARSIZE], title_name[NUM_OF_TITLES];
	char mobile_app_instruction_1[MAXCHARSIZE], mobile_app_instruction_2[MAXCHARSIZE]; 
	char user_instruction, database[NUM_OF_TITLES][MAXCHARSIZE] = {0}, temp_buffer[MAXCHARSIZE], status_message_from_server[MAXCHARSIZE];
	bool carousel_is_empty = false, carousel_is_full = false;
	
	bool disc_occupancy[NUM_OF_DISC_SLOTS];

	struct sockaddr_in server_address, mobile_app_address;		/*sockaddr_in is a structure that contains Internet address information*/
	struct hostent *server, *app;		/*'server' is a pointer to the hostent structure, which defines a host computer on the Internet (address information in struct members)*/
	
	
	
	//Memory Mapping
	memfd = open("/dev/mem", O_RDWR | O_SYNC);

	if(memfd == -1)
	{
		printf("Error: cannot open memory.");
		return 0;
	}

	base0 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_b0 & ~MAP_MASK);
	cbase0 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_c0 & ~MAP_MASK);
	cbase1 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_c1 & ~MAP_MASK);
	cbase2 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_c2 & ~MAP_MASK);
	cbase3 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_c3 & ~MAP_MASK);
	mbase0 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_m0 & ~MAP_MASK);
	mbase1 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_m1 & ~MAP_MASK);
	mbase2 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_m2 & ~MAP_MASK);
	mbase3 = mmap(0, MAP_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, memfd, dev_m3 & ~MAP_MASK);		

	if(base0 == (void*)-1||cbase0 == (void*)-1||cbase1 == (void*)-1||cbase2 == (void*)-1||cbase3 == (void*)-1||mbase0 == (void*)-1||mbase1 == (void*)-1||mbase2 == (void*)-1||mbase3 == (void*)-1)
	{
		printf("Error: cannot map memory.");
		return 0;
	}

	datareg0 = base0 + (dev_data0 & MAP_MASK);
	trisreg0 = base0 + (dev_tris0 & MAP_MASK);
	
	
	*((volatile int *)datareg0) = ONE;
	//End Memory Mapping
	
	
	

	socket_file_desc_2 = socket(AF_INET, SOCK_STREAM, 0);
	if(socket_file_desc_2 < 0)
		printf("ERROR: Mobile application socket could not be opened.\n");
	//setsockopt(socket_file_desc_2, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption, sizeof(iSetOption));	/*Violates TCP/IP protocol but allows the program to be restarted without TIME_WAIT socket delay*/
		
	bzero(&mobile_app_address, sizeof(mobile_app_address));	/*Initializing the mobile address buffer to 0*/

	/***Setting the fields in mobile_app_address***/
	/*The sockaddr_in structure specifies the address family, IP address, and port for the socket that is being bound.*/
	mobile_app_address.sin_family = AF_INET;
	mobile_app_address.sin_port = htons(APPPORTNUMBER);	/*htons() converts a port number in host byte order to a port number in network byte order*/
	mobile_app_address.sin_addr.s_addr = INADDR_ANY;
	
	
	/***Setting up connection to mobile application***/
	mobile_app_address_length = sizeof(mobile_app_address);
	err_check = bind(socket_file_desc_2, (struct sockaddr *) &mobile_app_address, sizeof(mobile_app_address));	/*Binds socket to server IP address and port*/
	if(err_check < 0)
		perror("ERROR: Failure on binding listening socket.");
	
	
	
	

	memset(disc_occupancy, false, NUM_OF_DISC_SLOTS);	/*Assuming all slots are empty initially*/

	while(1)
	{
		socket_file_desc_1 = socket(AF_INET, SOCK_STREAM, 0);		/*Creating the socket: AF_INET for Internet address domain, SOCK_STREAM for continuous stream socket type, and 0 to choose TCP as protocol for stream*/
		if(socket_file_desc_1 < 0)
			printf("ERROR: Server socket could not be opened.\n");
			
		server = NULL;	
		while(server == NULL)
		{		
			server = gethostbyname("192.168.0.100");		/*Takes name of server and returns a pointer to a hostent structure (defined 'server') containing information about the host*/
		}	/*end while*/

		memset(&server_address, 0, sizeof(server_address));	/*Initializing the server address buffer to 0*/

		/***Setting the fields in server_address***/
		server_address.sin_family = AF_INET;
		memmove(&server_address.sin_addr.s_addr,
			server->h_addr,		/*h_addr is defined as the first address in the array h_addr_list*/
			server->h_length);

		server_address.sin_port = htons(PORTNUMBER);		/*htons() converts a port number in host byte order (little-endian) to a port number in network byte order (big-endian)*/
		
		
		
	

		
		
		
		
		/*Stage 0 prompts user for instruction and asks server for list of titles.  Stage 1 asks server to burn specific title.*/
		
		if(stage == 0)
		{
			/*****STAGE 0*****/
			/*User inputs value to either burn disc or return previously burned disc*/
			user_input_error = true;
			while(user_input_error == true)
			{
				memset(&read_buffer, '\0', sizeof(read_buffer));	
				printf("Waiting for instructions from mobile application...\n");
				listen(socket_file_desc_2, 5);	/*Server listens to the socket for connections*/
				

				acceptsocket_file_desc_1 = accept(socket_file_desc_2,  (struct sockaddr *) &mobile_app_address, &mobile_app_address_length);	/*accept() pauses the server process until a client connection is made*/
				if(acceptsocket_file_desc_1 < 0)
					printf("ERROR: accept() command failed.\n");
					
				err_check = read(acceptsocket_file_desc_1, read_buffer, MAXCHARSIZE);
				if(err_check < 0)
				{
					perror("ERROR: Failure receiving data on socket.");
					close(acceptsocket_file_desc_1);	/*Closes the socket*/
					continue;
				}	/*end if*/			

				
				
				/*Tokenizing app command and image number*/
				//strtok(temp_buffer, " ");	/*Resetting strtok() function*/
				token = strtok(read_buffer, " ");
				strcpy(mobile_app_instruction_1, token);
				token = strtok(NULL, " ");	/*NULL allows strok to continue scanning from where it previously left off*/
				strtok(NULL, " ");
				strcpy(mobile_app_instruction_2, token);
				title_ID_number = atoi(mobile_app_instruction_2);
				
				printf("instruction 1 = %s", mobile_app_instruction_1);
				printf("instruction 2 = %d", title_ID_number);
				
				if((!strcmp(mobile_app_instruction_1, "BURN") || !strcmp(mobile_app_instruction_1, "RETURN") || !strcmp(mobile_app_instruction_1, "PANIC")) && (title_ID_number >= 0 && title_ID_number <= 24))
					user_input_error = false;
				else
					printf("ERROR: Command from mobile application is invalid.\n");
				
			}	/*end while*/
			
			if(!strcmp(mobile_app_instruction_1, "RETURN"))
			{
				printf("Please wait while disc carousel rotates in position...\n");
				for(i = 0; i < NUM_OF_DISC_SLOTS; i++)	/*Checking for the first available slot*/
				{
					carousel_is_full = true;
					if(i == 0 || i == 101 || i == 102 || i == 104)	/*These particular slots on the carousel are covered by plastic and can't be used to store discs*/
						continue;
							
					else if(disc_occupancy[i] == false)
					{	
						disc_return = i;	/*'disc_return' will hold the value of the slot that needs to point out to the customer for disc return*/
						disc_occupancy[i] = true;
						carousel_is_full = false;
						break;
					}	/*end if*/				
				}	/*end for*/
			
				/*Rotate disc carousel into position*/
				if(carousel_is_full)
				{
					printf("ERROR: Unable to return disc.  Disc carousel is full.\n");
					continue;
				}
				else
				{
					current_index = device_control(disc_return, current_index, 0, 0);
					current_index = device_control(153, current_index, 0, 0);	/*User has 5 seconds to return disc*/
				}
					
				continue;				
			}	/*end if*/
			
			if(!strcmp(mobile_app_instruction_1, "PANIC"))
			{
				printf("PANIC!\n");
				current_index = device_control(998, current_index , 0, 0);
				continue;
			}	/*end if*/
			
			
			
			
			
			
			
			
			
			
			
			
			
			/*Client retrieves list of titles from server*/
			if(connect(socket_file_desc_1, (sockaddr *) &server_address, sizeof(server_address)) < 0)		/*Establishing connection to server with error checking*/
			{
				printf("ERROR: Socket could not be connected to server.\n");
				close(socket_file_desc_1);	/*If connect() fails, state of socket is undefined and so should be closed*/
				continue;
			}	/*end if*/
				
			strcpy(write_buffer, "0 0 NULL");
			err_check = write(socket_file_desc_1, write_buffer, sizeof(write_buffer));	/*Tells server to upload title database to client*/
			if(err_check < 0)
			{
				printf("ERROR: Failure writing database upload command on socket.");
				close(socket_file_desc_1);
			}	/*end if*/
			
			
			for(i = 0; i < NUM_OF_TITLES; i++)
			{
				err_check = read(socket_file_desc_1, read_buffer, sizeof(read_buffer));
				if(err_check < 0)
				{
					printf("ERROR: Failure reading title database from socket.\n");
					close(socket_file_desc_1);
					break;
				}	/*end if*/
			
				strcpy(database[i], read_buffer); 
				printf("%s", database[i]);
			}	/*end for*/
			
			
			/*Instructing server to burn particular image*/
			printf("Desired title is image %d.\n", title_ID_number);				
			if(title_ID_number < 0 || title_ID_number > NUM_OF_TITLES)
			{
				printf("ERROR:  Entered value is invalid.\n");
				continue;
			}	/*end if*/
			
			
			
			for(i = 0; i < NUM_OF_DISC_SLOTS; i++)	/*Checking for the first available disc*/
			{
				carousel_is_empty = true;
				if(i == 0 || i == 101 || i == 102 || i == 104);	/*These particular slots on the carousel are covered by plastic and can't be used to store discs*/
				

				else if(disc_occupancy[i] == true)
				{
					disc_burn = i;
					disc_occupancy[i] = false;
					carousel_is_empty = false;
					break;
				}	/*end if*/				
			}	/*end for*/
			
			/*Rotate disc carousel into position*/
			if(carousel_is_empty)
			{
				printf("ERROR: Unable to burn disc.  Disc carousel is empty.\n");
				continue;
			}	/*end if*/
			else
			{
				//disc_carousel_rotation(disc_burn);
				//insert_disc();	/*Inserts disc into DVD burner*/
				printf("disc_burn = %d\n", disc_burn);
				current_index = device_control(disc_burn, current_index, 0, 0);	/*Rotates disc carousel into position to load DVD*/
				current_index = device_control(150, current_index, 0, 0);	/*Loads disc into DVD burner*/
			}	/*end else*/
			
			stage = 1;
		}	/*end if*/
				
	/******************************************************************************************************************************************************************/		
			
		else if(stage == 1)
		{
			/*****STAGE 1*****/
			/*Client requests specific title to be burned*/
			
			
			/*CLIENT DATA SYNTAX: [STAGE		ID			NAME] */
			for(i = 0; i < MAXCHARSIZE; i++)	/*Checks for the first letter in the string which should be the beginning of the title name and copies until null character is met*/
			{
				if(isalpha(database[title_ID_number][i]))
				{
					for(j = 0; i < MAXCHARSIZE; i++)
					{					
						title_name[j] = database[title_ID_number][i];
						
						if(database[title_ID_number][i] == '\0')
							break;
							
						j++;
					}	/*end while*/
					
					break;
				}	/*end if*/
			}	/*end for*/
			
			sprintf(write_buffer, "1 %d %s", title_ID_number, title_name);
			
			if(connect(socket_file_desc_1, (sockaddr *) &server_address, sizeof(server_address)) < 0)		/*Establishing connection to server with error checking*/
			{
				printf("ERROR: Socket could not be connected to server.\n");
				close(socket_file_desc_1);	/*If connect() fails, state of socket is undefined and so should be closed*/
				continue;
			}	/*end if*/
			
			err_check = write(socket_file_desc_1, write_buffer, MAXCHARSIZE);	/*Writing burn command to the socket*/
			if(err_check < 0)
			{
				printf("ERROR: Failure writing burn command on socket.\n");
				close(socket_file_desc_1);
			}	/*end if*/
			
			
			/*****FUNCTION TO RETURN DISC TO CUSTOMER*****/
			//Wait for message from server that burning operation is done.  Then return disc to customer.
			err_check = read(socket_file_desc_1, status_message_from_server, MAXCHARSIZE);
			if(err_check < 0)
			{
				printf("ERROR: Failure writing burn command on socket.\n");
				close(socket_file_desc_1);
				continue;
			}	/*end if*/
			

			/*if(!strcmp(status_message_from_server, "Burn complete.  Disc is ready."));
			
			else if(!strcmp(status_message_from_server, "Burn failed."))
			{
				printf("Burn failed.\n");
				break;
			}	/*end if*/
			
			/*else
				printf("Invalid message sent back from server.\n");*/
			
			current_index = device_control(151, current_index, 0, 0);	/*Unload disc from DVD burner to place DVD back into slot*/
			current_index = device_control(152, current_index, 0, 0);	/*Rotate carousel and eject disc from service slot*/
		
			
			stage = 0;
		}	/*end else if*/
		
		close(socket_file_desc_1);

	}	/*end while*/
	
	
	return(0);
}	/*end main*/