⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs_x_win32.c

📁 嵌入式文件系统 ucfs.包含全部源代码
💻 C
字号:
/*
**********************************************************************
*                          Micrium, Inc.
*                      949 Crestview Circle
*                     Weston,  FL 33327-1848
*
*                            uC/FS
*
*             (c) Copyright 2001 - 2003, Micrium, Inc.
*                      All rights reserved.
*
***********************************************************************

----------------------------------------------------------------------
File        : fs_x_win32.c
Purpose     : Win32 API OS Layer for the file system
----------------------------------------------------------------------
Known problems or limitations with current version
----------------------------------------------------------------------
None.
---------------------------END-OF-HEADER------------------------------
*/

/*********************************************************************
*
*             #include Section
*
**********************************************************************
*/

#include "fs_api.h"
#include "fs_dev.h"
#include "fs_os.h"
#include "fs_conf.h"

#if (FS_OS_WINDOWS)

#include <time.h>
#include <windows.h>
#include <stdio.h>

/*********************************************************************
*
*             Local Variables        
*
**********************************************************************
*/

static HANDLE _FS_fh_sema =NULL;
static HANDLE _FS_fop_sema=NULL;
static HANDLE _FS_mem_sema=NULL;
static HANDLE _FS_dop_sema=NULL;

#if FS_POSIX_DIR_SUPPORT
static HANDLE _FS_dirh_sema=NULL;
static HANDLE _FS_dirop_sema=NULL;
#endif  /* FS_POSIX_DIR_SUPPORT */

static HWND _hWnd;

/*********************************************************************
*
*             Global functions section
*
**********************************************************************
*/

/*********************************************************************
*
*             FS_X_OS_LockFileHandle
*
  Lock global table _FS_filehandle (fs_info.c).
*/

void FS_X_OS_LockFileHandle(void) {
  if (_FS_fh_sema==NULL) {
    return;
  }
  WaitForSingleObject(_FS_fh_sema,INFINITE);
}


/*********************************************************************
*
*             FS_X_OS_UnlockFileHandle
*
  Unlock global table _FS_filehandle (fs_info.c).
*/

void FS_X_OS_UnlockFileHandle(void) {
  if (_FS_fh_sema==NULL) {
    return;
  }
  ReleaseSemaphore(_FS_fh_sema, 1,NULL);
}


/*********************************************************************
*
*       FS_X_OS_LockFileOp
*
*  The filesystem does allow unlimited number of file access 
*  operations at the same time.
*  However, access needs to be serialized.
*  The granularity of the locking depends on  the code in this lock
*  and the corresponding Unlock routine.
*  The default implementation allows just one file operation at a time,
*  because it uses just one semaphore for file operations.
*/

void FS_X_OS_LockFileOp(FS_FILE *pFile) {
  FS_USE_PARA(pFile);
  if (_FS_fop_sema==NULL) {
    return;
  }
  WaitForSingleObject(_FS_fop_sema,INFINITE);
}


/*********************************************************************
*
*             FS_X_OS_UnlockFileOp
*
  Please see FS_X_OS_LockFileOp.
*/

void FS_X_OS_UnlockFileOp(FS_FILE *pFile) {
  FS_USE_PARA(pFile);
  if (_FS_fop_sema==NULL) {
    return;
  }
  ReleaseSemaphore(_FS_fop_sema, 1,NULL);
}


/*********************************************************************
*
*             FS_X_OS_LockMem
*
  Lock global table _FS_memblock (fat_misc.c).
*/

void FS_X_OS_LockMem(void) {
  if (_FS_mem_sema==NULL) {
    return;
  }
  WaitForSingleObject(_FS_mem_sema,INFINITE);
}


/*********************************************************************
*
*             FS_X_OS_UnlockMem
*
    Unlock global table _FS_memblock (fat_misc.c).
*/

void FS_X_OS_UnlockMem(void) {
  if (_FS_mem_sema==NULL) {
    return;
  }
  ReleaseSemaphore(_FS_mem_sema, 1,NULL);
}


/*********************************************************************
*
*             FS_X_OS_LockDeviceOp
*
  The filesystem does allow unlimited number of device access 
  operations at the same time. It is not allowed to access
  the same driver and unit during an operation to it is already
  pending. 
   
  This implementation does allow only one device access
  operation at the same time.
*/

void FS_X_OS_LockDeviceOp(const FS_DEVICE *pDevice) {
  FS_USE_PARA(pDevice);
  if (_FS_dop_sema==NULL) {
    return;
  }
  WaitForSingleObject(_FS_dop_sema,INFINITE);
}


/*********************************************************************
*
*             FS_X_OS_UnlockDeviceOp
*
*  Description:
*    Unlock specified device.
*
*    Please see FS_X_OS_LockDeviceOp.
*
*  Parameters:
*    Driver       - Pointer to a FS__DEVICE_TYPE structure
*    DevIndex     - Device Index number
*
*  Return value:
*    None.
*/

void FS_X_OS_UnlockDeviceOp(const FS_DEVICE *pDevice) {
  FS_USE_PARA(pDevice);
  if (_FS_dop_sema==NULL) {
    return;
  }
  ReleaseSemaphore(_FS_dop_sema, 1,NULL);
}


#if FS_POSIX_DIR_SUPPORT

/*********************************************************************
*
*             FS_X_OS_LockDirHandle
*
*   Lock global table _FS_dirhandle (api_dir.c).
*
*  Parameters:
*    None.
*
*  Return value:
*    None.
*/

void FS_X_OS_LockDirHandle(void) {
  if (_FS_dirh_sema==NULL) {
    return;
  }
  WaitForSingleObject(_FS_dirh_sema,INFINITE);
}


/*********************************************************************
*
*             FS_X_OS_UnlockDirHandle
*
*  Unlock global table _FS_dirhandle (api_dir.c).
*
*  Parameters:
*    None.
*
*  Return value:
*    None.
*/

void FS_X_OS_UnlockDirHandle(void) {
  if (_FS_dirh_sema==NULL) {
    return;
  }
  ReleaseSemaphore(_FS_dirh_sema, 1,NULL);
}


/*********************************************************************
*
*             FS_X_OS_LockDirOp
*
*  Description:
*    Lock specified directory.
*
*    The filesystem allows unlimited number of directory access 
*    operations at the same time. It is not allowed to access
*    a directory during a pending operation. Because different
*    directory pointers may access the same directory at the same time,
*    your implementation has to make sure not to access the same directory with two
*    different handles at the same time.
*
*    This implementation does allow only one directory access
*    operation at the same time.
*
*
*  Parameters:
*    Driver       - Pointer to a FS__DEVICE_TYPE structure
*    DevIndex     - Device Index number
*
*  Return value:
*    None.
*/

void FS_X_OS_LockDirOp(FS_DIR *dirp) {
  FS_USE_PARA(dirp);
  if (_FS_dirop_sema==NULL) {
    return;
  }
  WaitForSingleObject(_FS_dirop_sema,INFINITE);
}


/*********************************************************************
*
*             FS_X_OS_UnlockDirOp
*
*  Description:
*    Lock specified directory.
*
*  Parameters:
*    Driver       - Pointer to a FS__DEVICE_TYPE structure
*    DevIndex     - Device Index number
*
*  Return value:
*    None.
*/

void FS_X_OS_UnlockDirOp(FS_DIR *dirp) {
  FS_USE_PARA(dirp);
  if (_FS_dirop_sema==NULL) {
    return;
  }
  ReleaseSemaphore(_FS_dirop_sema, 1,NULL);
}

#endif  /* FS_POSIX_DIR_SUPPORT */


/*********************************************************************
*
*             FS_X_OS_GetDate
*
*  Description:
*    Current date as FS_u16 in a format suitable for the FAT file system.
*    The format of the date is bit-wise arranged.
*
*    Bit 0-4: Day of month (1-31)
*    Bit 5-8: Month of year (1-12)
*    Bit 9-15: Count of years from 1980 (0-127)
*
*  Parameters:
*    None.
*
*  Return value:
*    None.
*/

FS_U16 FS_X_OS_GetDate(void) {
  FS_U16 fdate;
  time_t t;
  struct tm *ltime;

  time(&t);
  ltime = localtime(&t);
  fdate = ltime->tm_mday;
  fdate += ((FS_U16) (ltime->tm_mon+1) << 5);
  fdate += ((FS_U16) (ltime->tm_year-80) << 9);
  return fdate;
}


/*********************************************************************
*
*             FS_X_OS_GetTime
*
*  Description:
*    Current time as FS_U16 in a format suitable for the FAT file system.
*
*    Bit 0-4: 2-second count (0-29)
*    Bit 5-10: Minutes (0-59)
*    Bit 11-15: Hours (0-23)
*
*  Parameters:
*    None.
*
*  Return value:
*    None.
*/

FS_U16 FS_X_OS_GetTime(void) {
  FS_U16 ftime;
  time_t t;
  struct tm *ltime;

  time(&t);
  ltime = localtime(&t);
  ftime = ltime->tm_sec/2;
  ftime += ((FS_U16) ltime->tm_min << 5);
  ftime += ((FS_U16) ltime->tm_hour << 11);
  return ftime;
}


/*********************************************************************
*
*             FS_X_OS_init
*
*  Description:
*    Initializes the OS resources. Specifically, you will need to
*    create four binary semaphores. This function is called by
*    FS_Init(). You should create all resources required by the
*    OS to support multithreading of the file system.
*
*  Parameters:
*    None.
*
*  Return value:
*    0    - on success
*    -1   - on failure.
*/

int FS_X_OS_Init(void) {
  /*  Get Handle of the console windows */
  if(_hWnd == 0) {
    char ac[200];
    GetConsoleTitle(ac, sizeof(ac));
    _hWnd = FindWindow("ConsoleWindowClass", ac);
  }

  _FS_fh_sema = CreateSemaphore(NULL, 1, 1, NULL);
  if (_FS_fh_sema == NULL) {
    return -1;
  }
  _FS_fop_sema = CreateSemaphore(NULL, 1, 1, NULL);
  if (_FS_fop_sema == NULL) {
    return -1;
  }
  _FS_mem_sema = CreateSemaphore(NULL, 1, 1, NULL);
  if (_FS_mem_sema == NULL) {
    return -1;
  }
  _FS_dop_sema = CreateSemaphore(NULL, 1, 1, NULL);
  if (_FS_dop_sema == NULL) {
    return -1;
  }
#if FS_POSIX_DIR_SUPPORT
  _FS_dirh_sema = CreateSemaphore(NULL, 1, 1, NULL);
  if (_FS_dirh_sema == NULL) {
    return -1;
  }
  _FS_dirop_sema = CreateSemaphore(NULL, 1, 1, NULL);
  if (_FS_dirop_sema == NULL) {
    return -1;
  }
#endif  /* FS_POSIX_DIR_SUPPORT */
  return 0;
}


/*********************************************************************
*
*             FS_X_OS_Exit
*
*  Description:
*    Deletes OS ressources. For example, delete the binary semaphores
*    used by the file system.
*
*  Parameters:
*    None.
*
*  Return value:
*    0    - on success
*    -1   - on failure.
*/

int FS_X_OS_Exit(void) {
  if (_FS_fh_sema!=NULL) {
    CloseHandle(_FS_fh_sema);
  }
  if (_FS_fop_sema!=NULL) {
    CloseHandle(_FS_fop_sema);
  }
  if (_FS_mem_sema!=NULL) {
    CloseHandle(_FS_mem_sema);
  }
  if (_FS_dop_sema!=NULL) {
    CloseHandle(_FS_dop_sema);
  }
#if FS_POSIX_DIR_SUPPORT
  if (_FS_dirh_sema!=NULL) {
    CloseHandle(_FS_dirh_sema);
  }
  if (_FS_dirop_sema!=NULL) {
    CloseHandle(_FS_dirop_sema);
  }
#endif  /* FS_POSIX_DIR_SUPPORT */
  return 0;
}

/*********************************************************************
*
*      Logging: OS dependent

Note:
  Logging is used in higher debug levels only. The typical target
  build does not use logging and does therefor not require any of
  the logging routines below. For a release build without logging
  the routines below may be eliminated to save some space.
  (If the linker is not function aware and eliminates unreferenced
  functions automatically)

*/
void FS_X_Warn(const char *s) {
  if (_hWnd) {
    MessageBox(_hWnd, s, "File System Warning", MB_ICONWARNING | MB_OK | MB_APPLMODAL);
    printf("File System Warning: %s", s);
  } else {
    printf("File System Warning: %s", s);
  }
}

void FS_X_ErrorOut(const char *s) {
  if (_hWnd) {
    MessageBox(_hWnd, s, "File System Error", MB_ICONEXCLAMATION | MB_OK | MB_APPLMODAL);
    printf("File System Error: %s", s);
  } else {
    printf("File System Error: %s", s);
  }
}


void FS_X_Log(const char *s) {
  printf("%s", s);
}


#endif /* FS_OS_WINDOWS */

/*************************** End of file ****************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -