EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
rfs.c
Go to the documentation of this file.
1 /***************************************************************************/
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 
21 #include "thunderboard/rfs/rfs.h"
22 
23 #define debug_printf(x, ...)
24 
25 /***************************************************************************/
32 /***************************************************************************/
39 extern const uint32_t RFS_fileCount;
40 extern const uint8_t *RFS_fileNames[];
41 extern const uint32_t RFS_fileLength[];
42 extern const uint8_t *RFS_fileData[];
47 /***************************************************************************/
54 /***************************************************************************/
61 uint32_t RFS_getFileCount( void )
62 {
63 
64  return RFS_fileCount;
65 
66 }
67 
68 /***************************************************************************/
79 int32_t RFS_getFileLengthByIndex( uint32_t index )
80 {
81 
82  if( index >= RFS_fileCount ) {
83  return -1;
84  }
85  else {
86  /*debug_printf(" fl = %d \n", RFS_fileLength[index] ); */
87  return RFS_fileLength[index];
88  }
89 
90 }
91 
92 /***************************************************************************/
102 int32_t RFS_getFileLength( RFS_FileHandle *fileHandle )
103 {
104 
105  return RFS_getFileLengthByIndex( fileHandle->fileIndex );
106 
107 }
108 
109 /***************************************************************************/
121 uint8_t *RFS_getFileNameByIndex( uint32_t index )
122 {
123 
124  if( index >= RFS_fileCount ) {
125  return NULL ;
126  }
127  else {
128  return (uint8_t *) RFS_fileNames[index];
129  }
130 
131 }
132 
133 /***************************************************************************/
144 uint8_t *RFS_getFileName( RFS_FileHandle *fileHandle )
145 {
146 
147  return RFS_getFileNameByIndex( fileHandle->fileIndex );
148 
149 }
150 
151 /***************************************************************************/
162 int16_t RFS_getFileIndex( uint8_t name[] )
163 {
164 
165  int i;
166  int ret;
167 
168  ret = -1;
169  for( i = 0; i < RFS_getFileCount(); i++ ) {
170 
171  /*debug_printf("[%d]: %s %s\n", i, name, RFS_getFileNameByIndex(i) );*/
172  if( strncmp( (char const*) name, (char const *) RFS_getFileNameByIndex( i ), RFS_MAX_FILE_NAME_SIZE ) == 0 ) {
173  ret = i;
174  /*debug_printf("[%d]: MATCH \n", ret );*/
175  break;
176  }
177 
178  }
179 
180  return ret;
181 
182 }
183 
184 /***************************************************************************/
197 int32_t RFS_fileOpen( RFS_FileHandle *fileHandle, uint8_t name[] )
198 {
199 
200  int index;
201 
202  index = RFS_getFileIndex( name );
203  if( index < 0 )
204  return index;
205 
206  fileHandle->fileIndex = index;
207  fileHandle->currentIndex = 0;
208 
209  return 1;
210 
211 }
212 
213 /***************************************************************************/
234 int32_t RFS_fileSeek( RFS_FileHandle *fileHandle, int32_t offset, uint32_t whence )
235 {
236 
237  /* Check file handle state */
238 
239  /*printf("fI = %d offset = %d whence = %08Xh\n", fileHandle->currentIndex, offset, whence );*/
240 
241  if( whence == RFS_SEEK_SET ) {
242  fileHandle->currentIndex = offset;
243  /*printf( "fH->cI = %d\n",fileHandle->currentIndex );*/
244  }
245  else if( whence == RFS_SEEK_CUR ) {
246  fileHandle->currentIndex += offset;
247  /*printf( "fH->cI = %d\n",fileHandle->currentIndex );*/
248  }
249  else if( whence == RFS_SEEK_END ) {
250  fileHandle->currentIndex = ( RFS_getFileLengthByIndex( fileHandle->fileIndex ) + offset );
251  /*printf( "fH->cI = %d\n",fileHandle->currentIndex );*/
252  }
253  else {
254  return -1;
255  }
256 
257  /* Check for out of bounds error on index before returning */
258 
259  return fileHandle->currentIndex;
260 
261 }
262 
263 /***************************************************************************/
282 int32_t RFS_fileRead( uint8_t *buf, uint32_t size, uint32_t nmemb, RFS_FileHandle *fileHandle )
283 {
284 
285  int32_t i;
286  int32_t fileIndex = -1;
287  uint32_t byteIndex;
288  uint32_t totalByteCount;
289  uint8_t *src = (uint8_t *) -1, *dst = (uint8_t *) -1, *end = (uint8_t *) -1;
290 
291  /* Get file index */
292  fileIndex = fileHandle->fileIndex;
293  debug_printf("RFS_fileRead(): fileIndex = %d \n", fileIndex );
294 
295  if( fileIndex < 0 ) {
296  return fileIndex;
297  }
298 
299  if( fileIndex >= RFS_getFileCount() ) {
300  return -1;
301  }
302 
303  /* Find data index */
304  byteIndex = fileHandle->currentIndex;
305 
306  src = (unsigned char *) RFS_fileData[fileIndex];
307  src += byteIndex;
308 
309  debug_printf(" endindex = %d\n", RFS_getFileLengthByIndex(fileIndex) );
310 
311  end = (unsigned char *) RFS_fileData[fileIndex];
312  end += RFS_getFileLengthByIndex( fileIndex ) - 1;
313 
314  dst = (uint8_t *) buf;
315  debug_printf("RFS_fileRead(): byteIndex = %d src = %08Xh end = %08Xh dst = %08Xh \n",
316  byteIndex, src, end, dst );
317 
318  totalByteCount = size * nmemb;
319 
320  for( i = 0; ( i < totalByteCount ) && ( src <= end ); i++ )
321  *dst++ = *src++;
322 
323  debug_printf("RFS_fileRead(): Bytes copied: %d [%d]\n", i, totalByteCount );
324 
325  fileHandle->currentIndex += i;
326 
327  return 1;
328 
329 }
330 
331 /***************************************************************************/
342 uint8_t *RFS_fileGetRawData( RFS_FileHandle *fileHandle )
343 {
344 
345  int32_t fileIndex = -1;
346 
347  /* Get file index */
348  fileIndex = fileHandle->fileIndex;
349  if( fileIndex < 0 )
350  return 0;
351  if( fileIndex >= RFS_getFileCount() )
352  return 0;
353 
354  return (unsigned char *) RFS_fileData[fileIndex];
355 
356 }
357 
ROM File System Drive.