📄 abl_api.c
字号:
(api[fd].devid, buffer, max_bytes); /* Return the number of bytes read */ return (n_bytes);}/************************************************************************* Function: abl_write** Purpose: * write data to a registered device** Processing:* This routine writes data to a registered api device by using the * callback method that has been bound to a driver. If the device * is not registered -1 is returned. If the device is registered a * generic pointer and the number of bytes represented by the pointer * are being passed to the * * Parameters:* fd - device file descriptor.* buffer - generic arg.* n_bytes - number of bytes contained in the arg.** Outputs: * None** Returns: * -1 if the write operation failed* number of bytes written.** Notes: * See sma_iosys.h for structure definitions*************************************************************************/INT_32 abl_write (INT_32 fd, CHAR* buffer, INT_32 n_bytes){ INT_32 actual_bytes = 0; /* Sanity check */ if (fd > MAX_API_DEVS) { /* Non valid file descriptor */ return (-1); } /* Method not bound to system */ if (api[fd].driver.write == NULL) { return (0); } /* Write the data to the device */ actual_bytes = (*(PFI)(api[fd].driver.write)) (api[fd].devid, buffer, n_bytes); /* Return the number of bytes written */ return (actual_bytes);}/************************************************************************* Function: abl_ioctl** Purpose: * device io control routine** Processing:* This routine controls the associated device driver via the * callback method that has been bound to a driver. If the device * is not registered -1 is returned else return code by the driver * ioctl is returned.* * Parameters:* fd - device file descriptor.* cmd - command to execute.* arg - generic arg.** Outputs: * None** Returns: * _ERROR if the operation failed* return code of the ioctl associated with the io system.** Notes: * See abl_api.h for structure definitions*************************************************************************/STATUS abl_ioctl (INT_32 fd, INT_32 cmd, INT_32 arg){ STATUS status = _ERROR; /* Sanity check */ if (fd > MAX_API_DEVS) { /* Non valid file descriptor */ return (status); } /* Perform the requested operation on the device driver */ status = (*(PFI)(api[fd].driver.ioctl)) (api[fd].devid, cmd, arg); /* return the status */ return (status);}/************************************************************************* Function: api_find_device** Purpose: * To find a device using a numerical representation** Processing:* Search the device table for an id and return return the * index of the device in the table.* * Parameters: * id - device id.** Outputs: * None** Returns: * index of the device bound to the id* -1 if the device does not exist** Notes: * See abl_api.h for structure definitions*************************************************************************/STATIC INT_32 api_find_device (INT_32 id){ INT_32 fd = 0; /* Find the device id in the table */ for (fd = 0; fd < MAX_API_DEVS; fd++) { /* Look for device id in the table */ if (api[fd].id == id) { /* Return index into the routing table */ return (fd); } } /* If we have got to here the device is not in the table */ return (-1);}/************************************************************************* Function: api_find_empty** Purpose: * To find a vacant table entry** Processing:* Search the device table for a vacant space and return the * index in the table.* * Parameters: * None** Outputs: * None** Returns: * index of the device bound to the name* -1 if the device does not exist** Notes: * See abl_api.h for structure definitions*************************************************************************/STATIC INT_32 api_find_empty (void){ UNS_32 fd = 0; /* Find the name on the table */ for (fd = 0; fd < MAX_API_DEVS; fd++) { /* Look for device id in the table */ if (api[fd].id == 0) { /* Return unused index into the table */ return (fd); } } /* If we have got to here the table is full */ return (-1);}/************************************************************************* Function: api_add_device** Purpose: * To add a device to the api table** Processing:* This function checks for a device id collision in the api system. * If the id is valid it looks for a vacant entry. If the table is not * full it binds itself to the api system.* * Parameters: * id - Device id* open - Driver open method* close - Driver close method* read - Driver read method* write - Driver write method* ioctl - Driver ioctl method** Outputs: * None** Returns: * _NO_ERROR if the device is added to the io system.* _ERROR if the table is full or the name is not valid.** Notes: * See abl_api.h for structure definitions*************************************************************************/STATIC STATUS api_add_device (INT_32 id, void* open, void* close, void* read, void* write, void* ioctl){ INT_32 idx = 0; /* Check for a name space collision */ if (api_find_device (id) != -1) { /* We have found a collision */ return (_ERROR); } /* Add the device to the table */ if ((idx = api_find_empty ()) == -1) { /* table is full */ return (_ERROR); } /* Add the device the table */ api[idx].id = id; api[idx].fd = idx; api[idx].driver.open = (PFI)open; api[idx].driver.close = (PFI)close; api[idx].driver.read = (PFI)read; api[idx].driver.write = (PFI)write; api[idx].driver.ioctl = (PFI)ioctl; /* If we have made it to here the device has been added */ return (_NO_ERROR);}/************************************************************************* Function: api_remove_device** Purpose: * To remove a device from the api table** Processing:* This function finds the table entry that is associated with the * devid. Once the entry is found it is cleared which will set it to* the idle state. When a table entry is in the idle state a new* device my use this entry to bind itself to the system.* * Parameters: * id - Device id** Outputs: * None** Returns: * _NO_ERROR on success* _ERROR on error** Notes: * See abl_api.h for structure definitions*************************************************************************/STATIC STATUS api_remove_device (INT_32 id){ INT_32 fd = 0; INT_32 idx = 0; CHAR* dst = NULL; /* Find the device in the device system */ if ((fd = api_find_device (id)) == -1) { /* The device is not in the table */ return (_ERROR); } /* Remove the device from the table */ dst = (CHAR*)&api[fd]; for (idx = 0; idx < sizeof (API_TABLE_T); idx++) { *dst++ = 0; } /* Return OK */ return (_NO_ERROR);}/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -