]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/osi/osi_arch.c
1eda38f99fd6355a53c1ac57fd0a128212413ddb
[esp-idf] / components / bt / bluedroid / osi / osi_arch.c
1 /*
2  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the bluedroid stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32
33 #include "osi_arch.h"
34
35 /** Create a new mutex
36  * @param mutex pointer to the mutex to create
37  * @return a new mutex */
38 int
39 osi_mutex_new(osi_mutex_t *pxMutex)
40 {
41   int xReturn = -1;
42
43   *pxMutex = xSemaphoreCreateMutex();
44
45   if (*pxMutex != NULL) {
46     xReturn = 0;
47   }
48
49   //LWIP_DEBUGF(THREAD_SAFE_DEBUG, ("osi_mutex_new: m=%p\n", *pxMutex));
50
51   return xReturn;
52 }
53
54 /** Lock a mutex
55  * @param mutex the mutex to lock */
56 void
57 osi_mutex_lock(osi_mutex_t *pxMutex)
58 {
59   while (xSemaphoreTake(*pxMutex, portMAX_DELAY) != pdPASS);
60 }
61
62 int
63 osi_mutex_trylock(osi_mutex_t *pxMutex)
64 {
65   if (xSemaphoreTake(*pxMutex, 0) == pdPASS) return 0;
66   else return -1;
67 }
68
69 /** Unlock a mutex
70  * @param mutex the mutex to unlock */
71 void
72 osi_mutex_unlock(osi_mutex_t *pxMutex)
73 {
74   xSemaphoreGive(*pxMutex);
75 }
76
77 /** Delete a semaphore
78  * @param mutex the mutex to delete */
79 void
80 osi_mutex_free(osi_mutex_t *pxMutex)
81 {
82   //LWIP_DEBUGF(THREAD_SAFE_DEBUG, ("osi_mutex_free: m=%p\n", *pxMutex));
83   vQueueDelete(*pxMutex);
84 }
85
86 /*-----------------------------------------------------------------------------------*/
87 //  Creates and returns a new semaphore. The "count" argument specifies
88 //  the initial state of the semaphore. TBD finish and test
89 int
90 osi_sem_new(osi_sem_t *sem, uint8_t count)
91 {
92   int xReturn = -1;
93   vSemaphoreCreateBinary(*sem);
94
95   if ((*sem) != NULL) {
96     if (count == 0) {   // Means it can't be taken
97       xSemaphoreTake(*sem, 1);
98     }
99
100     xReturn = 0;
101   } else {
102     ;   // TBD need assert
103   }
104
105   return xReturn;
106 }
107
108 /*-----------------------------------------------------------------------------------*/
109 // Signals a semaphore
110 void
111 osi_sem_signal(osi_sem_t *sem)
112 {
113   xSemaphoreGive(*sem);
114 }
115
116 /*-----------------------------------------------------------------------------------*/
117 /*
118   Blocks the thread while waiting for the semaphore to be
119   signaled. If the "timeout" argument is non-zero, the thread should
120   only be blocked for the specified time (measured in
121   milliseconds).
122
123   If the timeout argument is non-zero, the return value is the number of
124   milliseconds spent waiting for the semaphore to be signaled. If the
125   semaphore wasn't signaled within the specified time, the return value is
126   OSI_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
127   (i.e., it was already signaled), the function may return zero.
128
129   Notice that lwIP implements a function with a similar name,
130   osi_sem_wait(), that uses the osi_arch_sem_wait() function.
131 */
132 uint32_t
133 osi_sem_wait(osi_sem_t *sem, uint32_t timeout)
134 {
135   portTickType StartTime, EndTime, Elapsed;
136   unsigned long ulReturn;
137
138   StartTime = xTaskGetTickCount();
139
140   if (timeout != 0) {
141     if (xSemaphoreTake(*sem, timeout / portTICK_RATE_MS) == pdTRUE) {
142       EndTime = xTaskGetTickCount();
143       Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
144
145       if (Elapsed == 0) {
146         Elapsed = 1;
147       }
148
149       ulReturn = Elapsed;
150     } else {
151       ulReturn = OSI_ARCH_TIMEOUT;
152     }
153   } else { // must block without a timeout
154     while (xSemaphoreTake(*sem, portMAX_DELAY) != pdTRUE);
155
156     EndTime = xTaskGetTickCount();
157     Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
158
159     if (Elapsed == 0) {
160       Elapsed = 1;
161     }
162
163     ulReturn = Elapsed;
164   }
165
166   return ulReturn ; // return time blocked
167 }
168
169 /*-----------------------------------------------------------------------------------*/
170 // Deallocates a semaphore
171 void
172 osi_sem_free(osi_sem_t *sem)
173 {
174   vSemaphoreDelete(*sem);
175 }
176
177 /*-----------------------------------------------------------------------------------*/
178 // Initialize osi arch
179 void
180 osi_arch_init(void)
181 {
182 }
183
184 /*-----------------------------------------------------------------------------------*/
185 uint32_t
186 osi_now(void)
187 {
188   return xTaskGetTickCount();
189 }
190
191
192 void osi_delay_ms(uint32_t ms)
193 {
194   vTaskDelay(ms/portTICK_RATE_MS);
195 }
196
197