Exemplo n.º 1
0
int main()
{
	int i, fd; // fd is file descripter
	int pages;
	char* recv_data;
	char* sent_data;
	unsigned long position;
	int option;
	int res,pos, usr_res, ren;
	char* random_string = NULL;

	fd= open(DEVICE,O_RDWR);
	if(fd==-1)
	{
     	printf("file %s either does not exit or is currently used by an another user\n", DEVICE);
     	exit(-1);
	}
	printf ("\nUser has given Open command\n");

/******************************************************************************************************/
	while (1) {
		option = 0;
		printf("Enter option you want to choose:\n Press 1 to set page\n Press 2 to write\n Press 3 to read\n Press 4 to get the current page position\n Press 5 to erase\n Press 6 to get status of EEPROM\n Press 7 to exit\n");
		scanf("%d",&option);
		switch (option)
		{
			case 1:
				printf("Enter the current page position you wish to set (0 to 511)\t");
				scanf("%ld",&position);
				ioctl(fd,IOCTL_FLASHSETP, position);  //ioctl call
			break;
			case 2:
				printf ("\n How many pages you want to write?(1 to 512)\t");
				scanf("%d",&pages);
				sent_data = (char*)malloc(sizeof(char)*64*pages);
				random_string = (char *)malloc(sizeof(char) * pages * 64);
				random_string_gen(random_string, pages * 64);
				strcpy(sent_data, random_string);
				write(fd,sent_data, pages);
				printf("\ndata to be sent is:%s\n",sent_data);
				free (sent_data);
		
			break;
			case 3:
				printf ("\n How many pages you want to read? (1 to 512)\t");
				scanf("%d",&pages);
				recv_data = (char*)malloc(sizeof(char)*64*pages);
				printf("Your messsage is being enqueued or EEPROM is busy. Please wait...");

				
				// to poll till data is ready. Note that because of polling pointer will not return to user space immediately. A user needs to make at least two read() calls to get the data back. In two different cases, pointer will retrun immediately.

				do		
				{
					res = read(fd, recv_data, pages);
					usleep(1);
				}while(res!= 0);	
				recv_data[64*pages] = '\0';
				printf("\nReturn value is:%d\n", res);
				printf("\nMessage from process is:%s\n", recv_data);
				free (recv_data);
			break;
			case 4:
				ioctl(fd,IOCTL_FLASHGETP, &pos );  				//ioctl call
				printf("Current pointer is at %d\n", pos);
			break;
			case 5:
				ren = ioctl(fd,IOCTL_FLASHERASE, 1);  				//ioctl call
				if ( ren != 0)
					printf ("Wait.! EEPROM is Busy\n");
				else
					printf("The EEPROM is erased\n");
			break;
			case 6:
				usr_res = ioctl(fd,IOCTL_FLASHGETS, 1);
				if ( usr_res != 0)
					printf ("Status of EEPROM is - Busy\n");
				else
					printf ("Status of EEPROM is - Available\n");

			break;
			case 7:
				close(fd);
				printf("User has given close command\n");
				return 0;
			default:
				printf("Invalid Option. Try Again!\n");
				break;
		}
	}
	
}
int main(int argc,char** argv,char** uenv)
{
	int i;
	int j;
	int res;
	int fd;
	char* data;
	char* recv_data;
	char* page_data;
	int send_pages = 10;
	int recv_pages = 10;
	int page_size = 64;


	// Seeding for the rand function
	srand(time(NULL));

	data	  = (char*)malloc(sizeof(char)*page_size*send_pages);
	recv_data = (char*)malloc(sizeof(char)*page_size*recv_pages);
	page_data = (char*)malloc(sizeof(char)*page_size+1);


	// Opening the device file
        fd = open(FILENAME,O_RDWR);
        if(fd == -1){
                printf("Error opening %s: %s \n",FILENAME,strerror(errno));
                return -1;
        }	
	
	res = ioctl(fd, I2C_SLAVE,DEVICE_ADDR);
	if(res < 0){
		printf("Error setting the device address \n");
		return -1;
	}

	//Creating random string
	random_string_gen(data,send_pages);

	printf("DATA SEND \n");
	print_string_pagewise(data,send_pages);
	printf("\n---------------------------------------------------------\n");
	// Writing the data
	res = write(fd ,data,send_pages);
	if(res == -1){
		printf("Error writing to EEPROM \n");
		return -1;
	}
	


	// Seeking
	res = lseek(fd,0,SEEK_SET);
	if(res == -1){
                printf("Error seeking EEPROM: \n");
                return -1;
        }

	
	// Reading the data
	res = read(fd,recv_data,recv_pages);
	if(res == -1){
		printf("Error writing to EEPROM: \n");
                return -1;
        }


	printf("RECEIVED DATA: \n");
	print_string_pagewise(recv_data,recv_pages);

	close(fd);
	return 0;
}