1/*
2 $License:
3   Copyright 2011 InvenSense, Inc.
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16  $
17 */
18/*******************************************************************************
19 *
20 * $Id: mlos_linux.c 5629 2011-06-11 03:13:08Z mcaramello $
21 *
22 *******************************************************************************/
23
24/**
25 *  @defgroup MLOS
26 *  @brief OS Interface.
27 *
28 *  @{
29 *      @file mlos.c
30 *      @brief OS Interface.
31**/
32
33/* ------------- */
34/* - Includes. - */
35/* ------------- */
36
37#include <sys/time.h>
38#include <unistd.h>
39#include <pthread.h>
40#include <stdlib.h>
41
42#include "stdint_invensense.h"
43
44#include "mlos.h"
45#include <errno.h>
46
47
48/* -------------- */
49/* - Functions. - */
50/* -------------- */
51
52/**
53 *  @brief  Allocate space
54 *  @param  numBytes  number of bytes
55 *  @return pointer to allocated space
56**/
57void *inv_malloc(unsigned int numBytes)
58{
59    // Allocate space.
60    void *allocPtr = malloc(numBytes);
61    return allocPtr;
62}
63
64
65/**
66 *  @brief  Free allocated space
67 *  @param  ptr pointer to space to deallocate
68 *  @return error code.
69**/
70inv_error_t inv_free(void *ptr)
71{
72    // Deallocate space.
73    free(ptr);
74
75    return INV_SUCCESS;
76}
77
78
79/**
80 *  @brief  Mutex create function
81 *  @param  mutex   pointer to mutex handle
82 *  @return error code.
83 */
84inv_error_t inv_create_mutex(HANDLE *mutex)
85{
86    int res;
87    pthread_mutex_t *pm = malloc(sizeof(pthread_mutex_t));
88    if(pm == NULL)
89        return INV_ERROR;
90
91    res = pthread_mutex_init(pm, NULL);
92    if(res == -1) {
93        free(pm);
94        return INV_ERROR_OS_CREATE_FAILED;
95    }
96
97    *mutex = (HANDLE)pm;
98
99    return INV_SUCCESS;
100}
101
102
103/**
104 *  @brief  Mutex lock function
105 *  @param  mutex   Mutex handle
106 *  @return error code.
107 */
108inv_error_t inv_lock_mutex(HANDLE mutex)
109{
110    int res;
111    pthread_mutex_t *pm = (pthread_mutex_t*)mutex;
112
113    res = pthread_mutex_lock(pm);
114    if(res == -1)
115        return INV_ERROR_OS_LOCK_FAILED;
116
117    return INV_SUCCESS;
118}
119
120
121/**
122 *  @brief  Mutex unlock function
123 *  @param  mutex   mutex handle
124 *  @return error code.
125**/
126inv_error_t inv_unlock_mutex(HANDLE mutex)
127{
128    int res;
129    pthread_mutex_t *pm = (pthread_mutex_t*)mutex;
130
131    res = pthread_mutex_unlock(pm);
132    if(res == -1)
133        return INV_ERROR_OS_LOCK_FAILED;
134
135    return INV_SUCCESS;
136}
137
138
139/**
140 *  @brief  open file
141 *  @param  filename    name of the file to open.
142 *  @return error code.
143 */
144FILE *inv_fopen(char *filename)
145{
146    FILE *fp = fopen(filename,"r");
147    return fp;
148}
149
150
151/**
152 *  @brief  close the file.
153 *  @param  fp  handle to file to close.
154 *  @return error code.
155 */
156void inv_fclose(FILE *fp)
157{
158    fclose(fp);
159}
160
161/**
162 *  @brief  Close Handle
163 *  @param  handle  handle to the resource.
164 *  @return Zero if success, an error code otherwise.
165 */
166inv_error_t inv_destroy_mutex(HANDLE handle)
167{
168    int error;
169    pthread_mutex_t *pm = (pthread_mutex_t*)handle;
170    error = pthread_mutex_destroy(pm);
171    if (error) {
172        return errno;
173    }
174    free((void*) handle);
175
176    return INV_SUCCESS;}
177
178
179/**
180 *  @brief  Sleep function.
181 */
182void inv_sleep(int mSecs)
183{
184    usleep(mSecs*1000);
185}
186
187
188/**
189 *  @brief  get system's internal tick count.
190 *          Used for time reference.
191 *  @return current tick count.
192 */
193unsigned long inv_get_tick_count()
194{
195    struct timeval tv;
196
197    if (gettimeofday(&tv, NULL) !=0)
198        return 0;
199
200    return (long)((tv.tv_sec * 1000000LL + tv.tv_usec) / 1000LL);
201}
202
203  /**********************/
204 /** @} */ /* defgroup */
205/**********************/
206
207