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