Example #1
0
// store scene to sdcard at name
void files_store_scene_name(const char* name, u8 ext) {
  //u32 i;
  void* fp;
  char namebuf[64] = SCENES_PATH;
  u8* pScene;

  app_pause();

  strcat(namebuf, name);

  if(ext) {
    // weird..
    strip_space(namebuf, 32);
    strcat(namebuf, ".scn");
  }

  print_dbg("\r\n opening scene file for writing: ");
  print_dbg(namebuf);

  // fill the scene RAM buffer from current state of system
  scene_write_buf(); 
  // open FP for writing
  fp = fl_fopen(namebuf, "wb");
  pScene = (u8*)sceneData;
  fl_fwrite((const void*)pScene, sizeof(sceneData_t), 1, fp);
  fl_fclose(fp);
  // rescan
  list_scan(&sceneList, SCENES_PATH);
  delay_ms(10);

  app_resume();
}
int fl_fputs(const char * str, void *f)
{
	int len = (int)strlen(str);
	int res = fl_fwrite(str, 1, len, f);

	if (res == len)
		return len;
	else
		return res;
}
int fl_fputc(int c, void *f)
{
	unsigned char data = (unsigned char)c;
	int res;

	res = fl_fwrite(&data, 1, 1, f);
	if (res == 1)
		return c;
	else
		return res;
}
Example #4
0
// store scene to sdcard at name
void files_store_scene_name(const char* name, u8 ext) {
  //u32 i;
  void* fp;
  char namebuf[64] = SCENES_PATH;
  u8* pScene;

  app_pause();

  strcat(namebuf, name);

  if(ext) {
    // weird..
    strip_space(namebuf, 32);
    strcat(namebuf, ".scn");
  }

  print_dbg("\r\n opening scene file for writing: ");
  print_dbg(namebuf);

  // fill the scene RAM buffer from current state of system
  scene_write_buf(); 
  print_dbg("\r\n filled scene binary buffer");

  // open FP for writing
  fp = fl_fopen(namebuf, "wb");
  print_dbg("\r\n opened file for binary write at 0x");
  print_dbg_hex((u32)fp);

  pScene = (u8*)sceneData;
  print_dbg("\r\n writing data from scene buffer at 0x");
  print_dbg_hex((u32)pScene);
  print_dbg(", size : ");
  print_dbg_hex(sizeof(sceneData_t));
  

  // dump the scene data to debug output...

  fl_fwrite((const void*)pScene, sizeof(sceneData_t), 1, fp);
  fl_fclose(fp);

  print_dbg("\r\n ... finished writing, closed file pointer");

  // rescan
  list_scan(&sceneList, SCENES_PATH);
  delay_ms(10);

  print_dbg("\r\n re-scanned scene file list and waited.");

  app_resume();
}
Example #5
0
File: ottos.c Project: ottos/ottos
void fs_test() {
  file_t* file;
  char buffer[512];
  //char text[] = { 't', 'e', 's', 't', '\n', '\r' };
  char text[] = "new file\n\r\0";

  devices_init();
  mmchs_init();
  fs_init();
  fl_listdirectory("/");

  file = (file_t*) fl_fopen("/test/thenewest.txt", "a");
  fl_fwrite(text, sizeof(text), sizeof(text), file);
  fl_fclose(file);

  fl_listdirectory("/test/");

  file = (file_t*) fl_fopen("/test/thenewest.txt", "r");
  fl_fread(buffer, 512, sizeof(text), file);
  kernel_print(buffer);
  fl_fclose(file);


}
Example #6
0
int
cmd_dd_exec(CLI *cli, char *argv[]) {
	int verbose = 0, seek = 0, skip = 0, count = -1, block = 32;
	int size, rc, err = 0, argc = cli_num_args(argv);
	char infile[PATH_MAX], outfile[PATH_MAX];
	char *inname = NULL, *outname = NULL;
	FL_FILE *in = NULL, *out = NULL;
	int c, opt_ind;
	//char *buffer = NULL;

	for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
		c = getopt_long(argc, argv, "vi:o:b:c:s:k:", long_options, &opt_ind);

		switch (c) {
			case 'v': verbose =           1;  break;
			case 'i': inname  =      optarg;  break;
			case 'o': outname =      optarg;  break;
			case 'b': block   = atoi(optarg); break;
			case 's': skip    = atoi(optarg); break;
			case 'c': count   = atoi(optarg); break;
			case 'k': seek    = atoi(optarg); break;
		}
	}

	if (verbose) {
		printf("inname  = %s\n", inname);
		printf("outname = %s\n", outname);
		printf("block   = %i\n", block);
		printf("skip    = %i\n", skip);
		printf("seek    = %i\n", seek);
		printf("count   = %i\n", count);
	}

	//buff = (char *)mall c(block);
	char buffer[block];

	if (argc - optind != 1) {
		printf("dd - incorrect number of arguments. Try 'man dd'\n");
		err++; goto cleanup;
	}

	if (buffer == NULL) {
		printf("dd: out of memory.\n");
		err++; goto cleanup;
	}

	if (inname) {
		cli_clean_path(cli, inname, infile);

		if (fl_is_dir(infile)) {
			printf("dd: '%s' is a directory\n", infile);
			err++; goto cleanup;
		}

		in = (FL_FILE *)fl_fopen(infile, "r");

		if (!in) {
			printf("dd: could not open '%s' for reading\n", infile);
			err++; goto cleanup;
		}

		fl_fseek(in, skip * block, SEEK_SET);
	} else {
		printf("dd: no input file specified - dumping zeros\n");
		memset(buffer, 0, block);
	}

	if (outname) {
		cli_clean_path(cli, outname, outfile);

		if (fl_is_dir(outfile)) {
			printf("dd: '%s' is a directory\n", outfile);
			err++; goto cleanup;
		}

		out = (FL_FILE *)fl_fopen(outfile, "r+");

		if (out) {
			rc = fl_fseek(out, seek * block, SEEK_SET);
			printf("WARNING: seeking in outfile beyond EOF is unsupported!\n");
		} else {
			out = (FL_FILE *)fl_fopen(outfile, "w");
		}

		if (!out) {
			printf("dd: could not open '%s' for writeing\n", outfile);
			err++; goto cleanup;
		}
	} else {
		printf("dd: no output file specified - using stdout\n");
	}

	if (count >= 0) {
		size = count;
	} else {
		// TODO size of (infile - skip) / block
	}

	while (size > 0) {
		int blk = count - size;

		if (verbose) printf("dd:  reading block %i\n", blk);

		if (in) {
			rc = fl_fread(buffer, block, 1, in);

			if (rc < 0) {
				printf("dd: Error reading block %i\n", blk);
				err++; goto cleanup;
			} else {
				// short read -> set block size to number of bytes read
				block = rc;
			}
		}

		if (verbose) printf("dd: writeing block %i\n", blk);

		if (out) {
			rc = fl_fwrite(buffer, block, 1, out);

			if (rc < 0) {
				printf("dd: Error writeing block %i\n", blk);
				err++; goto cleanup;
			}
		} else {
			printf("%s\n", buffer);
		}

		size--;
	}

	// TODO print summary

cleanup:
	fl_fclose(in);
	fl_fclose(out);
	//free(buffer);

	return (err ? -1 : 0);
}
Example #7
0
//-----------------------------------------------------------------
// Main: Test bench file to create 5 files with psuedo random
// sequences in of varying lengths - read them back and complete
// then remove them.
//-----------------------------------------------------------------
void main()
{
	int i,j,x;

	FL_FILE * files[5];
	FL_FILE *readFile;
	char filenames[5][260];
	BYTE fileData[5][10000];
	BYTE readBuffer[10000];
	int fileLengths[5];
	BYTE *massiveData;
	time_t timeStart, timeEnd;

#define TESTFILES 6
	char *testfile[] = { "X:\\1", "X:\\1.bin", "X:\\12345678.321",
						 "X:\\mylongfilename", "X:\\mylongfilename.bin",
						 "X:\\the Quick Brown Fox jumped over the lazy dog.elf.binfile.jar"	};

	srand(time(NULL));

	// Initialise
	FAT32_InitDrive();

	fl_init();

	if (fl_attach_media(FAT_ReadSector, FAT_WriteSector) != FAT_INIT_OK)
		return;

	// List directory
	fl_listdirectory("C:\\");
//	return ;

test_start:

	// Generate 5 random files
	memset(filenames, 0x00, 260*5);
	for (j=0;j<5;j++)
	{
		// Length
		fileLengths[j] = GetRandom(9999);

		// Data
		for (x=0;x<fileLengths[j];x++)
			fileData[j][x] = (BYTE)GetRandom(255);

		// Names
		sprintf(filenames[j], "X:\\Auto Generated Filename Number %d", j+1);
	}

	// Create some files
	for (j=0;j<5;j++)
	{
		printf("Creating File: %s [%d bytes]\n", filenames[j], fileLengths[j]);

		// Create File
		files[j] = fl_fopen(filenames[j], "w");
		if (files[j]!=NULL)
		{
			if (fl_fwrite(fileData[j], 1, fileLengths[j], files[j])!=fileLengths[j])
			{
				printf("ERROR: File Write Block Failed File %s Length %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}
		}
		else
		{
			printf("ERROR: Error Creating File %s\n", filenames[j]);
			fl_assert(0);
		}

		fl_fclose(files[j]);

		// Clear buffer
		for (i=0;i<sizeof(readBuffer);i++)
			readBuffer[i] = 0;

		// Verify File
		readFile = fl_fopen(filenames[j], "r");
		if (readFile!=NULL)
		{
			int failed = FALSE;

			printf("File %s Read Check (fread whole file) [%d bytes]\n", filenames[j], fileLengths[j]);

			if (fl_fread(readBuffer, 1, fileLengths[j], readFile)!=fileLengths[j])
			{
				printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}

			for (i=0;i<fileLengths[j];i++)
				if ( fileData[j][i] != (BYTE)readBuffer[i] )
					failed = TRUE;

			if (failed)
			{
				printf("ERROR: File %s Data Verify Failed\n", filenames[j]);
				fl_assert(0);
			}
		}
		fl_fclose(readFile);

		// Clear buffer
		for (i=0;i<sizeof(readBuffer);i++)
			readBuffer[i] = 0;

		// Verify File using fgetc
		readFile = fl_fopen(filenames[j], "r");
		if (readFile!=NULL)
		{
			int failed = FALSE;

			printf("File %s Read Check (fgetc) [%d bytes]\n", filenames[j], fileLengths[j]);

			i = 0;
			while (i < fileLengths[j])
			{
				int res = fl_fgetc(readFile);
				if (res == -1)
					break;

				readBuffer[i++] = (BYTE)res;
			}

			if (i != fileLengths[j])
			{
				printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}

			for (i=0;i<fileLengths[j];i++)
				if ( fileData[j][i] != (BYTE)readBuffer[i] )
					failed = TRUE;

			if (failed)
			{
				printf("ERROR: File %s Data Verify Failed\n", filenames[j]);
				fl_assert(0);
			}
		}
		fl_fclose(readFile);

		// Clear buffer
		for (i=0;i<sizeof(readBuffer);i++)
			readBuffer[i] = 0;

		// Verify File chunks
		readFile = fl_fopen(filenames[j], "r");
		if (readFile!=NULL)
		{
			int failed = FALSE;

			printf("File %s Read Check (fread chunks) [%d bytes]\n", filenames[j], fileLengths[j]);

			i = 0;
			while (i < fileLengths[j])
			{
				int read_length = GetRandom(1025);

				if (read_length > (fileLengths[j] - i))
					read_length = fileLengths[j] - i;

				if (fl_fread(readBuffer + i, 1, read_length, readFile) != read_length)
				{
					printf("ERROR: File %s fread error\n", filenames[j]);
					fl_assert(0);
					break;
				}

				i += read_length;
			}

			if (i != fileLengths[j])
			{
				printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]);
				fl_assert(0);
			}

			for (i=0;i<fileLengths[j];i++)
				if ( fileData[j][i] != (BYTE)readBuffer[i] )
				{
					failed = TRUE;
					break;
				}

			if (failed)
			{
				printf("ERROR: File %s Data Verify Failed at %d\n", filenames[j], i);
				fl_assert(0);
			}
		}
		fl_fclose(readFile);

		// Delete File
		if (fl_remove(filenames[j])<0)
			printf("ERROR: Delete File %s Failed\n", filenames[j]);

		// Verify file is no longer present!
		readFile = fl_fopen(filenames[j], "r");
		if (readFile != NULL)
		{
			printf("ERROR: File %s still present after delete!\n", filenames[j]);
			fl_assert(0);
			fl_fclose(readFile);
		}
	}

	// Create folder
	fl_createdirectory("C:\\folder1");

#if 0

	// Create massive file
#define MASSIVE_FILE_LEN (1024 * 1024)
	printf("Creating Massive File [%d bytes]\n", MASSIVE_FILE_LEN);

	massiveData = malloc(MASSIVE_FILE_LEN);
	if (!massiveData)
	{
		printf("ERROR: Could not allocate memory for massive array!\n");
		fl_assert(0);
		fl_shutdown();
		return ;
	}

	// Create random data for file
	for (x=0;x<MASSIVE_FILE_LEN;x++)
		massiveData[x] = (BYTE)GetRandom(255);

	// Remove if it already exists!
	fl_remove("X:\\folder1\\massive file.bin");

	timeStart = time(NULL);

	// Create Large File
	readFile = fl_fopen("X:\\folder1\\massive file.bin", "w");
	if (readFile != NULL)
	{
		if (fl_fwrite(massiveData, 1, MASSIVE_FILE_LEN, readFile) != MASSIVE_FILE_LEN)
		{
			printf("ERROR: File Write Block Failed for massive file (Length %d)\n", MASSIVE_FILE_LEN);
			fl_assert(0);
		}
	}
	else
	{
		printf("ERROR: Error Creating massive file\n");
		fl_assert(0);
	}

	fl_fclose(readFile);

	// Verify Massive File
	readFile = fl_fopen("X:\\folder1\\massive file.bin", "r");
	if (readFile!=NULL)
	{
		int failed = FALSE;

		printf("File Massive File Read Check (fread whole file) [%d bytes]\n", MASSIVE_FILE_LEN);

		i = 0;
		while (i < MASSIVE_FILE_LEN)
		{
			int read_length = GetRandom(2048) + 128;

			if (read_length > (MASSIVE_FILE_LEN - i))
				read_length = MASSIVE_FILE_LEN - i;

			if (fl_fread(readBuffer, 1, read_length, readFile) != read_length)
			{
				printf("ERROR: File massive file fread error\n");
				fl_assert(0);
				break;
			}

			for (j=0;j<read_length;j++)
				if ( massiveData[i+j] != (BYTE)readBuffer[j] )
				{
					printf("ERROR: File Massive File Data Verify Failed at %d\n", i+j);
					fl_assert(0);
					break;
				}

			i += read_length;
		}

		if (i != MASSIVE_FILE_LEN)
		{
			printf("ERROR: File massive file Read Length Error %d\n", MASSIVE_FILE_LEN);
			fl_assert(0);
		}
	}
	fl_fclose(readFile);

	timeEnd = time(NULL);
	printf("Time taken %d seconds\n", (int)(timeEnd-timeStart));

	free(massiveData);
#endif

	// Filename test
	for (i=0;i<TESTFILES;i++)
	{
		// Create File
		readFile = fl_fopen(testfile[i], "w");
		if (readFile != NULL)
			;
		else
		{
			printf("ERROR: Error Creating File %s\n", testfile[i]);
			fl_assert(0);
		}

		fl_fputc(0, readFile);
		fl_fclose(readFile);

		readFile = fl_fopen(testfile[i], "r");
		assert(readFile);
		fl_fclose(readFile);
	}

	// List directory
	fl_listdirectory("C:\\");

	for (i=0;i<TESTFILES;i++)
	{
		// Delete File
		if (fl_remove(testfile[i])<0)
		{
			printf("ERROR: Delete File %s Failed\n", testfile[i]);
			fl_assert(0);
		}
	}

	fl_shutdown();

	printf("\r\nCompleted\r\n");

	// List directory
	fl_listdirectory("C:\\");
}
Example #8
0
int32_t file_write(void* buffer, int32_t size, int32_t count, file_handle_t handle)
{
    return fl_fwrite(buffer, size, count, handle);
}
Example #9
0
void LOS_proc(unsigned char tid, void* msg)
{
    //static int file_num = 0;
	//static int size;
	static LOS_state_t last_st;
	int i;
    
#ifndef __LOCAL_STORE_ACCEL_MODE__
	if (LOS_queue.numBytes > 0)
#else
    if (LS_num_accel_queue > 0)
#endif
	{
		switch (st)
		{
		case LOS_NONE:
			//probe_high();
			last_st = st;
			st = LOS_CREATE_START;
		case LOS_CREATE_START:
			i = fl_fopen("/file.txt", "w", (void**)&mFile[LOS_open_file_index], TRUE);
			switch (i)
			{
			case 0:
				st = LOS_OPEN_START;
				break;
			case -1:
				return;
			default:
				if (mFile[LOS_open_file_index])
				{
					last_st = st;
					st = LOS_CREATE_DONE;
				}
				else
				{
					st = LOS_OPEN_START;
				}
			}
		case LOS_OPEN_START:
			if (st == LOS_OPEN_START)
			{
#ifdef PROBE_ENABLED
				probe_low();
#endif
				i = fl_fopen("/file.txt", "w", (void**)mFile[LOS_open_file_index], TRUE);
				switch (i)
				{
				case 0:
					st = LOS_NONE;
					return;
				case -1:
					return;
				default:
					if (mFile[LOS_open_file_index])
					{
						last_st = st;
						st = LOS_OPEN_DONE;
					}
					else
					{
						st = LOS_NONE;
						return;
					}
				}
			}
		case LOS_CREATE_DONE:
		case LOS_OPEN_DONE:
			//probe_low();
			last_st = st;
#ifndef __LOCAL_STORE_ACCEL_MODE__
			if (LOS_queue.outCount == LOS_queue.inCount)
			{
				st = LOS_WRITE_HEAD;
			}
			else if (LOS_queue.outCount > LOS_queue.inCount)
			{
				size = LOS_queue.maxBytes - LOS_queue.outCount;
				st = LOS_WRITE_HEAD;
			}
			else
			{
				size = LOS_queue.numBytes;
				st = LOS_WRITE_ALL;
			}
		case LOS_WRITE_HEAD:
			if (st == LOS_WRITE_HEAD)
			{
				i = fl_fwrite(&LOS_queue.buffer[LOS_queue.outCount], 1, size, mFile[LOS_open_file_index]);
				switch (i)
				{
				case -1:
					printf("ERROR: Write file failed\n");
					st = last_st;
					return;
				case -2:
					return;
				}
				st = last_st;
				if (i == size)
				{
					LOS_queue.numBytes -= size;
					LOS_queue.outCount = 0;
				}
				else
				{
					printf("ERROR: Write file failed\n");
				}
				return;
			}
		case LOS_WRITE_ALL:
			if (st == LOS_WRITE_ALL)
			{
				i = fl_fwrite(&LOS_queue.buffer[LOS_queue.outCount], 1, size, mFile[LOS_open_file_index]);
				switch (i)
				{
				case -1:
					printf("ERROR: Write file failed\n");
					st = last_st;
					return;
				case -2:
					return;
				default:
					if (i == size)
					{
						LOS_queue.numBytes -= size;
						LOS_queue.outCount += size;
#ifdef PROBE_ENABLED
						probe2_change(0);
#endif
						if (mFile[LOS_open_file_index]->filelength > MAX_FILE_SIZE)
						{
							st = LOS_CLOSE_START;
						}
						else
						{
							st = last_st;
							return;
						}
					}
					else
					{
						printf("ERROR: Write file failed\n");
						st = last_st;
						return;
					}
				}
			}
#else 
            if (LS_get_accel_string())
            {
                st = LOS_WRITE;
            }
            else
            {
                st = last_st;
                return;
            }
        case LOS_WRITE:
            if (st == LOS_WRITE)
            {
                i = fl_fwrite(LS_accel_string, 1, LS_accel_string_size, mFile[LOS_open_file_index]);
                switch (i)
                {
                case -1:
                    printf("ERROR: Write file failed\n");
                    st = last_st;
                    return;
                case -2:
                    return;
                }
                st = last_st;
                if (i == LS_accel_string_size)
                {
                    LS_accel_queue_out_count = (LS_accel_queue_out_count + 1) % MAX_LS_ACCEL_ITEMS;
                    --LS_num_accel_queue;
#ifdef PROBE_ENABLED
                    probe2_change(0);
#endif
					if (mFile[LOS_open_file_index]->filelength > MAX_FILE_SIZE)
					{
						st = LOS_CLOSE_START;
					}
					else
					{
						st = last_st;
						return;
					}
                }
                else
                {
                    st = last_st;
                    return;
                }
            }
#endif
		case LOS_CLOSE_START:
			i = fl_fclose(mFile[LOS_open_file_index]);
			switch (i)
			{
			case 0:
				printf("ERROR: Close file failed\n");
				return;
			case -1:
				i = 0;
				return;
			}
			st = LOS_NONE;
		}
	}
}