/** * emDriverManager.h - Related funciton declaration for device driver * * Copyright (C) 2011-2020 carl.wu * All rights reserved. * E-MAIL: carljianhua@yeah.net * **/ #ifndef EM_DRIVER_MANAGER_2011_02_11_H #define EM_DRIVER_MANAGER_2011_02_11_H #ifdef __cplusplus extern "C" { #endif // #include "Common.h" // typedef enum { emDriver_no_error = 0x0000, emDriver_invalid_param = 0x0001, emDriver_invalid_pointer = 0x0002, emDriver_no_register = 0x0004, emDriver_no_use = 0x0010, emDriver_try_lock = 0x0020, emDriver_already_use = 0x0040, emDriver_already_full = 0x0080, emDriver_unknow_error = 0x0100, Eof_DriverStatus = 0x1000 }eDriverStatus; // Define the length of driver name #define DRIVER_NAME_LEN 8 #define INVALID_HANDLE_VALUE -1 /* Define device identity. */ typedef INT32 device_t; /* Define driver open interface. */ typedef int (*driver_open)(char* szdriverName); /* Define driver read interface. */ typedef int (*driver_read)(device_t device, char* szBuff, int* pSize); /* Define driver write interface. */ typedef int (*driver_write)(device_t device, const char* szBuff, int* pSize); /* Define driver control interface. */ typedef int (*driver_ioctl)(device_t device, INT32 nCmomand, arg_t args); /* Define driver close interface. */ typedef int (*driver_close)(device_t device); /* Define driver operation structure. */ typedef struct _struct_driver_operations { /* Open request procedure. */ driver_open open; /* Read request procedure. */ driver_read read; /* Write request procedure. */ driver_write write; /* Special functions procedure. */ driver_ioctl ioctl; /* Close request procedure. */ driver_close close; } t_driver_operatons; typedef struct _struct_device { // the handle of device. device_t tHandle; // the name of device char szName[DRIVER_NAME_LEN]; // the status of device driver eDriverStatus status; // the operations of device driver t_driver_operatons* pDriverOperations; // the options of device driver INT8 options; }tDevice; // device driver Module initialization extern void __driver_component_init(void); // register and unregister device driver extern eDriverStatus devRegister(char* szName, t_driver_operatons* fileOperations); extern eDriverStatus devUnRegister(char* szName); // uniform interface of device operations extern int Open(char* szName); extern int Write(device_t device, const char* szBuff, int* pSize); extern int Read(device_t device, char* szBuff, int* pSize); extern int Ioctl(device_t device, INT16 operate, arg_t arg); extern int Close(device_t device); #ifdef __cplusplus } #endif #endif
/** * emDriverManager.h - Related funciton definiton for device driver * * Copyright (C) 2011-2020 carl.wu * All rights reserved. * E-MAIL: carljianhua@yeah.net **/ #include <string.h> #include "emDriverManager.h" // static device driver #define CFG_DEVICE_SIZE 6 static tDevice s_device_driver[CFG_DEVICE_SIZE]; /*<FUNC>*********************************************************************** Function: __driver_init Description: initialize the driver component. Parameters: Returns: Notes: **<FUNC>**********************************************************************/ void __driver_component_init(void) { INT8 i = 0; // initialize the driver component for (i=0; i<CFG_DEVICE_SIZE; i++) { // s_device_driver[i].tHandle = INVALID_HANDLE_VALUE; // strcpy(s_device_driver[i].szName, ""); // s_device_driver[i].status = 0; // s_device_driver[i].pDriverOperations = NULL; // s_device_driver[i].options = 0; } // } /*<FUNC>*********************************************************************** Function: devRegister Description: register one kind of device driver. Parameters: szName -- the name of the device. fileOperations -- the device driver operation pointer. Returns: if register is successful, then return emDriver_no_error, else return the error msg. Notes: **<FUNC>**********************************************************************/ eDriverStatus devRegister(char* szName, t_driver_operatons* fileOperations) { INT8 i = 0; // check the pointer of input arguments if ((szName == NULL) || (fileOperations == NULL)) { return emDriver_invalid_pointer; } // find the idle device driver for (i=0; i<CFG_DEVICE_SIZE; i++) { if ((s_device_driver[i].tHandle == INVALID_HANDLE_VALUE) &&(s_device_driver[i].status == emDriver_no_use) &&(s_device_driver[i].pDriverOperations == NULL)) { break; } } // check if all device driver is already used if (i >= CFG_DEVICE_SIZE) { return emDriver_already_full; } else { // initialization the device driver s_device_driver[i].tHandle = (device_t)(&s_device_driver[i]); s_device_driver[i].pDriverOperations = fileOperations; s_device_driver[i].options = emDriver_no_use; strcpy(s_device_driver[i].szName, szName); } return emDriver_no_error; } /*<FUNC>*********************************************************************** Function: devUnRegister Description: unregister one kind of device driver. Parameters: szName -- the name of the device. Returns: if register is successful, then return emDriver_no_error, else return the error msg. Notes: **<FUNC>**********************************************************************/ eDriverStatus devUnRegister(char* szName) { INT8 i = 0; // check the name pointer is valid if (szName == NULL) { return emDriver_invalid_pointer; } // find the right driver pointer for (i=0; i<CFG_DEVICE_SIZE; i++) { if (strcmp(szName, s_device_driver[i].szName) == 0) { break; } } // if (i >= CFG_DEVICE_SIZE) { return emDriver_no_register; } else { // if (s_device_driver[i].tHandle == ((device_t)&s_device_driver[i])) { // s_device_driver[i].tHandle = INVALID_HANDLE_VALUE; strcpy(s_device_driver[i].szName, ""); s_device_driver[i].status = 0; s_device_driver[i].pDriverOperations = NULL; s_device_driver[i].options = 0; return emDriver_no_error; } else { return emDriver_unknow_error; } } } /*<FUNC>*********************************************************************** Function: Open Description: open one kind of device driver. Parameters: szName -- the name of the device. Returns: if Open is successful, then return device driver handle. Notes: **<FUNC>**********************************************************************/ int Open(char* szName) { INT8 i = 0; // check the name pointer if (szName == NULL) { return INVALID_HANDLE_VALUE; } // else { // find the right driver pointer for (i=0; i<CFG_DEVICE_SIZE; i++) { if (strcmp(szName, s_device_driver[i].szName) == 0) { break; } } // if (i >= CFG_DEVICE_SIZE) { return INVALID_HANDLE_VALUE; } else { // if (s_device_driver[i].options & emDriver_no_use) { s_device_driver[i].options |= emDriver_already_use; // if (s_device_driver[i].pDriverOperations->open != NULL) { (*s_device_driver[i].pDriverOperations->open)(szName); return (&s_device_driver[i]); } else { return INVALID_HANDLE_VALUE; } } else { return INVALID_HANDLE_VALUE; } } } } /*<FUNC>*********************************************************************** Function: Write Description: write the buffer to the phsical port. Parameters: device -- the index of the device. szBuff -- the pointer of buffer needed to write. pSize -- the size pointer of buffer. Returns: if write is successful, then return the bytes of write to device. otherwise return -1. Notes: **<FUNC>**********************************************************************/ int Write(device_t device, const char* szBuff, int* pSize) { INT16 nWrittenBytes = 0; tDevice* pDevice = NULL; // check the input param if ((szBuff == NULL) || (device == INVALID_HANDLE_VALUE) || (pSize != NULL)) { return emDriver_invalid_pointer; } else { // get the pointer of device driver pDevice = (tDevice*)device; if (pDevice->options & emDriver_already_use) { pDevice->options |= emDriver_try_lock; // if (pDevice->pDriverOperations->write != NULL) { nWrittenBytes = (*pDevice->pDriverOperations->write)(device, szBuff, pSize); pDevice->options &= ~emDriver_try_lock; } } } // return nWrittenBytes; } /*<FUNC>*********************************************************************** Function: Read Description: Read the buffer from physical port. Parameters: device -- the index of the device. szBuff -- the pointer of buffer needed to read. pSize -- the size pointer of buffer. Returns: if read is successful, then return the bytes of write to device. otherwise return -1. Notes: **<FUNC>**********************************************************************/ int Read(device_t device, char* szBuff, int* pSize) { INT16 nReadBytes = 0; tDevice* pDevice = NULL; // check the input param if ((szBuff == NULL) || (device == INVALID_HANDLE_VALUE) || (pSize != NULL)) { return emDriver_invalid_pointer; } else { // get the pointer of device driver pDevice = (tDevice*)device; if (pDevice->options & emDriver_already_use) { pDevice->options |= emDriver_try_lock; // if (pDevice->pDriverOperations->read != NULL) { nReadBytes = (*pDevice->pDriverOperations->read)(device, szBuff, pSize); pDevice->options &= ~emDriver_try_lock; } } } // return nReadBytes; } /*<FUNC>*********************************************************************** Function: Ioctl Description: IO control physical port. Parameters: device -- the index of the device. operate -- the opearation command. arg -- the argument. Returns: if IOCtrl is successful, then return the return code from device. otherwise return -1. Notes: **<FUNC>**********************************************************************/ int Ioctl(device_t device, INT16 operate, arg_t arg) { INT16 nIOCtrlReturnCode = 0; tDevice* pDevice = NULL; // if (device == INVALID_HANDLE_VALUE) { return emDriver_invalid_pointer; } else { // get the pointer of device driver pDevice = (tDevice*)device; if (pDevice->options & emDriver_already_use) { pDevice->options |= emDriver_try_lock; // if (pDevice->pDriverOperations->ioctl != NULL) { nIOCtrlReturnCode = (*pDevice->pDriverOperations->ioctl)(device, operate, arg); pDevice->options &= ~emDriver_try_lock; } } } return nIOCtrlReturnCode; } /*<FUNC>*********************************************************************** Function: Close Description: Close physical port. Parameters: device -- the index of the device. Returns: if Close is successful, then return the return code from device. otherwise return -1. Notes: **<FUNC>**********************************************************************/ int Close(device_t device) { INT16 nCloseReturnCode = 0; tDevice* pDevice = NULL; // if (device == INVALID_HANDLE_VALUE) { return emDriver_invalid_pointer; } else { // get the pointer of device driver pDevice = (tDevice*)device; if (pDevice->options & emDriver_already_use) { pDevice->options |= emDriver_try_lock; // if (pDevice->pDriverOperations->close != NULL) { nCloseReturnCode = (*pDevice->pDriverOperations->close)(device); pDevice->options &= ~emDriver_try_lock; pDevice->options &= ~emDriver_already_use; } } } // return nCloseReturnCode; } int main() { return 0; };