ff_dev_support.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * FreeRTOS+FAT V2.3.3
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. /* FreeRTOS includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. #include "portable.h"
  32. #include "ff_headers.h"
  33. #include "ff_devices.h"
  34. #ifndef ARRAY_SIZE
  35. #define ARRAY_SIZE( x ) ( int ) ( sizeof( x ) / sizeof( x )[ 0 ] )
  36. #endif
  37. #if ( ffconfigDEV_SUPPORT == 0 )
  38. #error No use to include this module if ffconfigDEV_SUPPORT is disabled
  39. #endif /* ffconfigDEV_SUPPORT == 0 */
  40. struct SFileCache
  41. {
  42. char pcFileName[ 16 ];
  43. uint32_t ulFileLength;
  44. uint32_t ulFilePointer;
  45. };
  46. struct SFileCache xFiles[ 16 ];
  47. enum eCACHE_ACTION
  48. {
  49. eCACHE_LOOKUP,
  50. eCACHE_ADD,
  51. eCACHE_REMOVE,
  52. };
  53. const char pcDevicePath[] = ffconfigDEV_PATH;
  54. struct SFileCache * pxFindFile( const char * pcFname,
  55. enum eCACHE_ACTION eAction )
  56. {
  57. BaseType_t xIndex, xFreeIndex = -1;
  58. struct SFileCache * pxResult = NULL;
  59. for( xIndex = 0; xIndex < ARRAY_SIZE( xFiles ); xIndex++ )
  60. {
  61. if( xFiles[ xIndex ].pcFileName[ 0 ] == '\0' )
  62. {
  63. if( xFreeIndex < 0 )
  64. {
  65. xFreeIndex = xIndex;
  66. }
  67. }
  68. else if( strcmp( xFiles[ xIndex ].pcFileName, pcFname ) == 0 )
  69. {
  70. if( eAction == eCACHE_REMOVE )
  71. {
  72. xFiles[ xIndex ].pcFileName[ 0 ] = '\0';
  73. }
  74. pxResult = xFiles + xIndex;
  75. break;
  76. }
  77. }
  78. if( ( pxResult == NULL ) && ( eAction == eCACHE_ADD ) && ( xFreeIndex >= 0 ) )
  79. {
  80. pxResult = xFiles + xFreeIndex;
  81. snprintf( pxResult->pcFileName, sizeof( pxResult->pcFileName ), "%s", pcFname );
  82. pxResult->ulFileLength = 0;
  83. pxResult->ulFilePointer = 0;
  84. }
  85. return pxResult;
  86. }
  87. BaseType_t xCheckDevicePath( const char * pcPath )
  88. {
  89. BaseType_t xDevLength;
  90. BaseType_t xPathLength;
  91. BaseType_t xIsDevice;
  92. xDevLength = sizeof( pcDevicePath ) - 1;
  93. xPathLength = strlen( pcPath );
  94. /* System "/dev" should not match with "/device/etc". */
  95. if( ( xPathLength >= xDevLength ) &&
  96. ( memcmp( pcDevicePath, pcPath, xDevLength ) == 0 ) &&
  97. ( ( pcPath[ xDevLength ] == '\0' ) || ( pcPath[ xDevLength ] == '/' ) ) )
  98. {
  99. xIsDevice = FF_DEV_CHAR_DEV;
  100. }
  101. else
  102. {
  103. xIsDevice = FF_DEV_NO_DEV;
  104. }
  105. return xIsDevice;
  106. }
  107. BaseType_t FF_Device_Open( const char * pcPath,
  108. FF_FILE * pxStream )
  109. {
  110. uint8_t ucIsDevice;
  111. ucIsDevice = xCheckDevicePath( pcPath );
  112. if( ucIsDevice != pdFALSE )
  113. {
  114. const char * pcBaseName = pcPath;
  115. if( memcmp( pcBaseName, pcDevicePath, sizeof( pcDevicePath ) - 1 ) == 0 )
  116. {
  117. pcBaseName = pcBaseName + sizeof( pcDevicePath );
  118. }
  119. pxStream->pxDevNode = pxFindFile( pcBaseName, eCACHE_ADD );
  120. if( pxStream->pxDevNode != NULL )
  121. {
  122. pxStream->pxDevNode->ulFilePointer = 0;
  123. if( ( pxStream->ucMode & ( FF_MODE_WRITE | FF_MODE_APPEND | FF_MODE_CREATE ) ) == 0 )
  124. {
  125. pxStream->ulFileSize = pxStream->pxDevNode->ulFileLength;
  126. }
  127. }
  128. }
  129. return ucIsDevice;
  130. }
  131. void FF_Device_Close( FF_FILE * pxStream )
  132. {
  133. if( pxStream->pxDevNode != NULL )
  134. {
  135. pxStream->ulFileSize = 0ul;
  136. pxStream->ulFilePointer = 0ul;
  137. }
  138. }
  139. size_t FF_Device_Read( void * pvBuf,
  140. size_t lSize,
  141. size_t lCount,
  142. FF_FILE * pxStream )
  143. {
  144. lCount *= lSize;
  145. return lCount;
  146. }
  147. size_t FF_Device_Write( const void * pvBuf,
  148. size_t lSize,
  149. size_t lCount,
  150. FF_FILE * pxStream )
  151. {
  152. lCount *= lSize;
  153. if( pxStream->pxDevNode != NULL )
  154. {
  155. pxStream->pxDevNode->ulFilePointer += lCount;
  156. if( pxStream->pxDevNode->ulFileLength < pxStream->pxDevNode->ulFilePointer )
  157. {
  158. pxStream->pxDevNode->ulFileLength = pxStream->pxDevNode->ulFilePointer;
  159. }
  160. }
  161. return lCount;
  162. }
  163. int FF_Device_Seek( FF_FILE * pxStream,
  164. long lOffset,
  165. int iWhence )
  166. {
  167. if( pxStream->pxDevNode != NULL )
  168. {
  169. if( iWhence == FF_SEEK_SET )
  170. {
  171. pxStream->pxDevNode->ulFilePointer = lOffset;
  172. }
  173. else if( iWhence == FF_SEEK_END )
  174. {
  175. pxStream->pxDevNode->ulFilePointer = pxStream->pxDevNode->ulFileLength - lOffset;
  176. }
  177. }
  178. return 0;
  179. }
  180. int FF_Device_GetDirEnt( const char * pcPath,
  181. FF_DirEnt_t * pxDirEnt )
  182. {
  183. BaseType_t xIsDotDir = 0;
  184. if( pxDirEnt->pcFileName[ 0 ] == '.' )
  185. {
  186. if( ( pxDirEnt->pcFileName[ 1 ] == '.' ) &&
  187. ( pxDirEnt->pcFileName[ 2 ] == '\0' ) )
  188. {
  189. xIsDotDir = 2;
  190. }
  191. else if( pxDirEnt->pcFileName[ 1 ] == '\0' )
  192. {
  193. xIsDotDir = 1;
  194. }
  195. }
  196. if( xIsDotDir == 0 )
  197. {
  198. struct SFileCache * pxDevNode;
  199. pxDevNode = pxFindFile( pxDirEnt->pcFileName, eCACHE_LOOKUP );
  200. pxDirEnt->ucIsDeviceDir = FF_DEV_CHAR_DEV;
  201. if( pxDevNode != NULL )
  202. {
  203. pxDirEnt->ulFileSize = pxDevNode->ulFileLength;
  204. }
  205. else if( pxDirEnt->ulFileSize < 2048 )
  206. {
  207. pxDirEnt->ulFileSize = 2048;
  208. }
  209. }
  210. return 1024;
  211. }