Home | History | Annotate | Download | only in linux
      1 /*
      2  $License:
      3     Copyright (C) 2012 InvenSense Corporation, All Rights Reserved.
      4  $
      5  */
      6 
      7 /*******************************************************************************
      8  *
      9  * $Id: mlos_linux.c 5629 2011-06-11 03:13:08Z mcaramello $
     10  *
     11  ******************************************************************************/
     12 
     13 /**
     14  *  @defgroup MLOS
     15  *  @brief OS Interface.
     16  *
     17  *  @{
     18  *      @file mlos.c
     19  *      @brief OS Interface.
     20  */
     21 
     22 /* ------------- */
     23 /* - Includes. - */
     24 /* ------------- */
     25 
     26 #include <sys/time.h>
     27 #include <unistd.h>
     28 #include <pthread.h>
     29 #include <stdlib.h>
     30 #include <errno.h>
     31 
     32 #include "stdint_invensense.h"
     33 #include "mlos.h"
     34 
     35 
     36 /* -------------- */
     37 /* - Functions. - */
     38 /* -------------- */
     39 
     40 /**
     41  *  @brief  Allocate space
     42  *  @param  num_bytes  number of bytes
     43  *  @return pointer to allocated space
     44  */
     45 void *inv_malloc(unsigned int num_bytes)
     46 {
     47     // Allocate space.
     48     void *alloc_ptr = malloc(num_bytes);
     49     return alloc_ptr;
     50 }
     51 
     52 
     53 /**
     54  *  @brief  Free allocated space
     55  *  @param  ptr pointer to space to deallocate
     56  *  @return error code.
     57  */
     58 inv_error_t inv_free(void *ptr)
     59 {
     60     if (ptr)
     61         free(ptr);
     62     return INV_SUCCESS;
     63 }
     64 
     65 
     66 /**
     67  *  @brief  Mutex create function
     68  *  @param  mutex   pointer to mutex handle
     69  *  @return error code.
     70  */
     71 inv_error_t inv_create_mutex(HANDLE *mutex)
     72 {
     73     int res;
     74     pthread_mutex_t *pm = malloc(sizeof(pthread_mutex_t));
     75     if(pm == NULL)
     76         return INV_ERROR;
     77 
     78     res = pthread_mutex_init(pm, NULL);
     79     if(res == -1) {
     80         free(pm);
     81         return INV_ERROR_OS_CREATE_FAILED;
     82     }
     83 
     84     *mutex = (HANDLE)pm;
     85 
     86     return INV_SUCCESS;
     87 }
     88 
     89 
     90 /**
     91  *  @brief  Mutex lock function
     92  *  @param  mutex   Mutex handle
     93  *  @return error code.
     94  */
     95 inv_error_t inv_lock_mutex(HANDLE mutex)
     96 {
     97     int res;
     98     pthread_mutex_t *pm = (pthread_mutex_t *)mutex;
     99 
    100     res = pthread_mutex_lock(pm);
    101     if(res == -1)
    102         return INV_ERROR_OS_LOCK_FAILED;
    103 
    104     return INV_SUCCESS;
    105 }
    106 
    107 
    108 /**
    109  *  @brief  Mutex unlock function
    110  *  @param  mutex   mutex handle
    111  *  @return error code.
    112  */
    113 inv_error_t inv_unlock_mutex(HANDLE mutex)
    114 {
    115     int res;
    116     pthread_mutex_t *pm = (pthread_mutex_t *)mutex;
    117 
    118     res = pthread_mutex_unlock(pm);
    119     if(res == -1)
    120         return INV_ERROR_OS_LOCK_FAILED;
    121 
    122     return INV_SUCCESS;
    123 }
    124 
    125 
    126 /**
    127  *  @brief  open file
    128  *  @param  filename    name of the file to open.
    129  *  @return error code.
    130  */
    131 FILE *inv_fopen(char *filename)
    132 {
    133     FILE *fp = fopen(filename, "r");
    134     return fp;
    135 }
    136 
    137 
    138 /**
    139  *  @brief  close the file.
    140  *  @param  fp  handle to file to close.
    141  *  @return error code.
    142  */
    143 void inv_fclose(FILE *fp)
    144 {
    145     fclose(fp);
    146 }
    147 
    148 /**
    149  *  @brief  Close Handle
    150  *  @param  handle  handle to the resource.
    151  *  @return Zero if success, an error code otherwise.
    152  */
    153 inv_error_t inv_destroy_mutex(HANDLE handle)
    154 {
    155     int error;
    156     pthread_mutex_t *pm = (pthread_mutex_t *)handle;
    157     error = pthread_mutex_destroy(pm);
    158     if (error)
    159         return errno;
    160     free((void*) handle);
    161 
    162     return INV_SUCCESS;}
    163 
    164 
    165 /**
    166  *  @brief  Sleep function.
    167  */
    168 void inv_sleep(int m_secs)
    169 {
    170     usleep(m_secs * 1000);
    171 }
    172 
    173 
    174 /**
    175  *  @brief  get system's internal tick count.
    176  *          Used for time reference.
    177  *  @return current tick count.
    178  */
    179 unsigned long inv_get_tick_count()
    180 {
    181     struct timeval tv;
    182 
    183     if (gettimeofday(&tv, NULL) != 0)
    184         return 0;
    185 
    186     return (long)((tv.tv_sec * 1000000LL + tv.tv_usec) / 1000LL);
    187 }
    188 
    189 /** @} */
    190 
    191