int32 OS_open (const char *path, int32 access, uint32 mode) { int status; char local_path[OS_MAX_LOCAL_PATH_LEN]; uint32 PossibleFD; int perm; /* ** Check to see if the path pointer is NULL */ if (path == NULL) { return OS_FS_ERR_INVALID_POINTER; } /* ** Check to see if the path is too long */ if (strlen(path) >= OS_MAX_PATH_LEN) { return OS_FS_ERR_PATH_TOO_LONG; } /* ** check if the name of the file is too long */ if (OS_check_name_length(path) != OS_FS_SUCCESS) { return OS_FS_ERR_NAME_TOO_LONG; } /* ** Check for a valid access mode */ switch(access) { case OS_READ_ONLY: perm = O_RDONLY; break; case OS_WRITE_ONLY: perm = O_WRONLY | O_CREAT; break; case OS_READ_WRITE: perm = O_RDWR | O_CREAT; break; default: return OS_FS_ERROR; } /* ** Translate the path */ if ( OS_TranslatePath(path, (char *)local_path) != OS_FS_SUCCESS ) { return OS_FS_ERR_PATH_INVALID; } semTake(OS_FDTableMutex,WAIT_FOREVER); for ( PossibleFD = 0; PossibleFD < OS_MAX_NUM_OPEN_FILES; PossibleFD++) { if( OS_FDTable[PossibleFD].IsValid == FALSE) { break; } } if (PossibleFD >= OS_MAX_NUM_OPEN_FILES) { semGive(OS_FDTableMutex); return OS_FS_ERR_NO_FREE_FDS; } /* Mark the table entry as valid so no other * task can take that ID */ OS_FDTable[PossibleFD].IsValid = TRUE; semGive(OS_FDTableMutex); /* Open the file */ status = open(local_path, (int) perm, (int) mode); semTake(OS_FDTableMutex,WAIT_FOREVER); if (status != ERROR) { /* fill in the table before returning */ OS_FDTable[PossibleFD].OSfd = status; strncpy(OS_FDTable[PossibleFD].Path, path, OS_MAX_PATH_LEN); OS_FDTable[PossibleFD].User = OS_FindCreator(); semGive(OS_FDTableMutex); return PossibleFD; } else { OS_FDTable[PossibleFD].IsValid = FALSE; semGive(OS_FDTableMutex); return OS_FS_ERROR; } } /* end OS_open */
int32 OS_open (const char *path, int32 access, uint32 mode) { int status; rtems_status_code rtems_sc; char local_path[OS_MAX_LOCAL_PATH_LEN]; int perm; uint32 PossibleFD; /* ** Check to see if the path pointer is NULL */ if (path == NULL) { return OS_FS_ERR_INVALID_POINTER; } /* ** Check to see if the path is too long */ if (strlen(path) >= OS_MAX_PATH_LEN) { return OS_FS_ERR_PATH_TOO_LONG; } /* ** check if the name of the file is too long */ if (OS_check_name_length(path) != OS_FS_SUCCESS) { return OS_FS_ERR_NAME_TOO_LONG; } /* ** Check for a valid access mode */ switch(access) { case OS_READ_ONLY: perm = O_RDONLY; break; case OS_WRITE_ONLY: perm = O_WRONLY; break; case OS_READ_WRITE: perm = O_RDWR; break; default: return OS_FS_ERROR; } /* ** Translate the path */ if ( OS_TranslatePath(path, (char *)local_path) != OS_FS_SUCCESS ) { return OS_FS_ERR_PATH_INVALID; } rtems_sc = rtems_semaphore_obtain (OS_FDTableSem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); for ( PossibleFD = 0; PossibleFD < OS_MAX_NUM_OPEN_FILES; PossibleFD++) { if( OS_FDTable[PossibleFD].IsValid == FALSE) { break; } } if (PossibleFD >= OS_MAX_NUM_OPEN_FILES) { rtems_sc = rtems_semaphore_release (OS_FDTableSem); return OS_FS_ERR_NO_FREE_FDS; } /* ** Mark the table entry as valid so no other ** task can take that ID */ OS_FDTable[PossibleFD].IsValid = TRUE; rtems_sc = rtems_semaphore_release (OS_FDTableSem); /* open the file */ status = open(local_path, perm, mode); if (status != ERROR) { rtems_sc = rtems_semaphore_obtain (OS_FDTableSem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); /* fill in the table before returning */ OS_FDTable[PossibleFD].OSfd = status; strncpy(OS_FDTable[PossibleFD].Path, path, OS_MAX_PATH_LEN); OS_FDTable[PossibleFD].User = OS_FindCreator(); rtems_sc = rtems_semaphore_release (OS_FDTableSem); return PossibleFD; } else { rtems_sc = rtems_semaphore_obtain (OS_FDTableSem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); /* Operation failed, so reset to false */ OS_FDTable[PossibleFD].IsValid = FALSE; rtems_sc = rtems_semaphore_release (OS_FDTableSem); return OS_FS_ERROR; } } /* end OS_open */
/****************************************************************************** ** Function: OS_TimerCreate ** ** Purpose: Create a new OSAL Timer ** ** Arguments: ** ** Return: */ int32 OS_TimerCreate(uint32 *timer_id, const char *timer_name, uint32 *clock_accuracy, OS_TimerCallback_t callback_ptr) { rtems_status_code status; rtems_name RtemsTimerName; uint32 possible_tid; int32 i; if ( timer_id == NULL || timer_name == NULL) { return OS_INVALID_POINTER; } /* ** we don't want to allow names too long ** if truncated, two names might be the same */ if (strlen(timer_name) > OS_MAX_API_NAME) { return OS_ERR_NAME_TOO_LONG; } /* ** Check Parameters */ status = rtems_semaphore_obtain (OS_timer_table_sem, RTEMS_WAIT, RTEMS_NO_TIMEOUT); for(possible_tid = 0; possible_tid < OS_MAX_TIMERS; possible_tid++) { if (OS_timer_table[possible_tid].free == TRUE) break; } if( possible_tid >= OS_MAX_TIMERS || OS_timer_table[possible_tid].free != TRUE) { status = rtems_semaphore_release (OS_timer_table_sem); return OS_ERR_NO_FREE_IDS; } /* ** Check to see if the name is already taken */ for (i = 0; i < OS_MAX_TIMERS; i++) { if ((OS_timer_table[i].free == FALSE) && strcmp ((char*) timer_name, OS_timer_table[i].name) == 0) { status = rtems_semaphore_release (OS_timer_table_sem); return OS_ERR_NAME_TAKEN; } } /* ** Verify callback parameter */ if (callback_ptr == NULL ) { status = rtems_semaphore_release (OS_timer_table_sem); return OS_TIMER_ERR_INVALID_ARGS; } /* ** Set the possible timer Id to not free so that ** no other task can try to use it */ OS_timer_table[possible_tid].free = FALSE; status = rtems_semaphore_release (OS_timer_table_sem); OS_timer_table[possible_tid].creator = OS_FindCreator(); OS_timer_table[possible_tid].start_time = 0; OS_timer_table[possible_tid].interval_time = 0; OS_timer_table[possible_tid].callback_ptr = callback_ptr; /* ** Create an interval timer */ RtemsTimerName = rtems_build_name('T','M','E','R'); status = rtems_timer_create(RtemsTimerName, &(OS_timer_table[possible_tid].host_timerid)); if ( status != RTEMS_SUCCESSFUL ) { OS_timer_table[possible_tid].free = TRUE; return(OS_TIMER_ERR_UNAVAILABLE); } /* ** Return the clock accuracy to the user */ *clock_accuracy = os_clock_accuracy; /* ** Return timer ID */ *timer_id = possible_tid; return OS_SUCCESS; }
int32 OS_creat (const char *path, int32 access) { int status; char local_path[OS_MAX_LOCAL_PATH_LEN]; int perm; mode_t mode; uint32 PossibleFD; /* ** Check to see if the path pointer is NULL */ if (path == NULL) { return OS_FS_ERR_INVALID_POINTER; } /* ** Check to see if the path is too long */ if (strlen(path) >= OS_MAX_PATH_LEN) { return OS_FS_ERR_PATH_TOO_LONG; } /* ** check if the name of the file is too long */ if (OS_check_name_length(path) != OS_FS_SUCCESS) { return OS_FS_ERR_NAME_TOO_LONG; } /* ** Check for a valid access mode ** For creating a file, OS_READ_ONLY does not make sense */ switch(access) { case OS_WRITE_ONLY: perm = O_WRONLY; break; case OS_READ_WRITE: perm = O_RDWR; break; default: return OS_FS_ERROR; } /* ** Translate the path */ if ( OS_TranslatePath(path, (char *)local_path) != OS_FS_SUCCESS ) { return OS_FS_ERR_PATH_INVALID; } pthread_mutex_lock(&OS_FDTableMutex); for ( PossibleFD = 0; PossibleFD < OS_MAX_NUM_OPEN_FILES; PossibleFD++) { if( OS_FDTable[PossibleFD].IsValid == FALSE) { break; } } if (PossibleFD >= OS_MAX_NUM_OPEN_FILES) { pthread_mutex_unlock(&OS_FDTableMutex); return OS_FS_ERR_NO_FREE_FDS; } /* Mark the table entry as valid so no other * task can take that ID */ OS_FDTable[PossibleFD].IsValid = TRUE; pthread_mutex_unlock(&OS_FDTableMutex); mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; status = open(local_path, perm | O_CREAT | O_TRUNC, mode); pthread_mutex_lock(&OS_FDTableMutex); if (status != ERROR) { /* fill in the table before returning */ OS_FDTable[PossibleFD].OSfd = status; strncpy(OS_FDTable[PossibleFD].Path, path, OS_MAX_PATH_LEN); OS_FDTable[PossibleFD].User = OS_FindCreator(); pthread_mutex_unlock(&OS_FDTableMutex); return PossibleFD; } else { /* Operation failed, so reset to false */ OS_FDTable[PossibleFD].IsValid = FALSE; pthread_mutex_unlock(&OS_FDTableMutex); return OS_FS_ERROR; } } /* end OS_creat */
int32 OS_open (const char *path, int32 access, uint32 mode) { int status; char local_path[OS_MAX_PATH_LEN]; int perm; uint32 PossibleFD; if(path == NULL) return OS_FS_ERR_INVALID_POINTER; if (strlen(path) >= OS_MAX_PATH_LEN) return OS_FS_ERR_PATH_TOO_LONG; /* make a local copy of the path */ strcpy(local_path, path); if(OS_NameChange(local_path) != OS_FS_SUCCESS) { return OS_FS_ERR_PATH_INVALID; } /* check if the name of the file is too long */ if (OS_check_name_length(local_path) == OS_FS_ERROR) return OS_FS_ERR_NAME_TOO_LONG; pthread_mutex_lock(&OS_FDTableMutex); for ( PossibleFD = 0; PossibleFD < OS_MAX_NUM_OPEN_FILES; PossibleFD++) { if( OS_FDTable[PossibleFD].IsValid == FALSE) { break; } } if (PossibleFD >= OS_MAX_NUM_OPEN_FILES) { pthread_mutex_unlock(&OS_FDTableMutex); return OS_FS_ERR_NO_FREE_FDS; } /* Mark the table entry as valid so no other * task can take that ID */ OS_FDTable[PossibleFD].IsValid = TRUE; pthread_mutex_unlock(&OS_FDTableMutex); switch(access) { case OS_READ_ONLY: perm = O_RDONLY; break; case OS_WRITE_ONLY: perm = O_WRONLY; break; case OS_READ_WRITE: perm = O_RDWR; break; default: return OS_FS_ERROR; } /* open the file with the R/W permissions */ status = open(local_path,perm); pthread_mutex_lock(&OS_FDTableMutex); if (status != ERROR) { /* fill in the table before returning */ OS_FDTable[PossibleFD].OSfd = status; strncpy(OS_FDTable[PossibleFD].Path, path, OS_MAX_API_NAME); OS_FDTable[PossibleFD].User = OS_FindCreator(); pthread_mutex_unlock(&OS_FDTableMutex); return PossibleFD; } else { /* Operation failed, so reset to false */ OS_FDTable[PossibleFD].IsValid = FALSE; pthread_mutex_unlock(&OS_FDTableMutex); return OS_FS_ERROR; } } /* end OS_open */
/****************************************************************************** ** Function: OS_TimerCreate ** ** Purpose: Create a new OSAL Timer ** ** Arguments: ** ** Return: */ int32 OS_TimerCreate(uint32 *timer_id, const char *timer_name, uint32 *clock_accuracy, OS_TimerCallback_t callback_ptr) { uint32 possible_tid; int32 i; int status; if ( timer_id == NULL || timer_name == NULL || clock_accuracy == NULL ) { return OS_INVALID_POINTER; } /* ** we don't want to allow names too long ** if truncated, two names might be the same */ if (strlen(timer_name) > OS_MAX_API_NAME) { return OS_ERR_NAME_TOO_LONG; } /* ** Check Parameters */ semTake(OS_timer_table_sem,WAIT_FOREVER); for(possible_tid = 0; possible_tid < OS_MAX_TIMERS; possible_tid++) { if (OS_timer_table[possible_tid].free == TRUE) break; } if( possible_tid >= OS_MAX_TIMERS || OS_timer_table[possible_tid].free != TRUE) { semGive(OS_timer_table_sem); return OS_ERR_NO_FREE_IDS; } /* ** Check to see if the name is already taken */ for (i = 0; i < OS_MAX_TIMERS; i++) { if ((OS_timer_table[i].free == FALSE) && strcmp ((char*) timer_name, OS_timer_table[i].name) == 0) { semGive(OS_timer_table_sem); return OS_ERR_NAME_TAKEN; } } /* ** Verify callback parameter */ if (callback_ptr == NULL ) { semGive(OS_timer_table_sem); return OS_TIMER_ERR_INVALID_ARGS; } /* ** Set the possible timer Id to not free so that ** no other task can try to use it */ OS_timer_table[possible_tid].free = FALSE; semGive(OS_timer_table_sem); OS_timer_table[possible_tid].creator = OS_FindCreator(); strncpy(OS_timer_table[possible_tid].name, timer_name, OS_MAX_API_NAME); OS_timer_table[possible_tid].start_time = 0; OS_timer_table[possible_tid].interval_time = 0; OS_timer_table[possible_tid].callback_ptr = callback_ptr; /* ** Create the timer */ status = timer_create(CLOCK_REALTIME, NULL, (timer_t *)&(OS_timer_table[possible_tid].host_timerid)); if (status < 0) { OS_timer_table[possible_tid].free = TRUE; return ( OS_TIMER_ERR_UNAVAILABLE); } /* ** Connect the timer to the callback function. This is non-posix, but it make the timer ** setup easier. */ status = timer_connect((timer_t)(OS_timer_table[possible_tid].host_timerid), OS_TimerSignalHandler, possible_tid ); if (status < 0 ) { status = timer_delete((timer_t)(OS_timer_table[possible_tid].host_timerid)); return(OS_TIMER_ERR_UNAVAILABLE); } /* ** Return the clock accuracy to the user */ *clock_accuracy = os_clock_accuracy; /* ** Return timer ID */ *timer_id = possible_tid; return OS_SUCCESS; }