BLE-Stack APIs  3.00.00
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
osal.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  * @file osal.h
34  * @brief This API allows the software components in the Z-Stack to be written
35  * independently of the specifics of the operating system, kernel, or
36  * tasking environment (including control loops or connect-to-interrupt
37  * systems).
38  */
39 
40 #ifndef OSAL_H
41 #define OSAL_H
42 
43 #ifdef __cplusplus
44 extern "C"
45 {
46 #endif
47 
48 /*********************************************************************
49  * INCLUDES
50  */
51 
52 #include <limits.h>
53 
54 #include "comdef.h"
55 #include "OSAL_Memory.h"
56 #include "OSAL_Timers.h"
57 
58 #ifdef USE_ICALL
59 #include <icall.h>
60 #endif /* USE_ICALL */
61 
62 /*********************************************************************
63  * MACROS
64  */
65 #if ( UINT_MAX == 65535 ) /* 8-bit and 16-bit devices */
66  #define osal_offsetof(type, member) ((uint16) &(((type *) 0)->member))
67 #else /* 32-bit devices */
68  #define osal_offsetof(type, member) ((uint32) &(((type *) 0)->member))
69 #endif
70 
71 #define OSAL_MSG_NEXT(msg_ptr) ((osal_msg_hdr_t *) (msg_ptr) - 1)->next
72 
73 #define OSAL_MSG_Q_INIT(q_ptr) *(q_ptr) = NULL
74 
75 #define OSAL_MSG_Q_EMPTY(q_ptr) (*(q_ptr) == NULL)
76 
77 #define OSAL_MSG_Q_HEAD(q_ptr) (*(q_ptr))
78 
79 #define OSAL_MSG_LEN(msg_ptr) ((osal_msg_hdr_t *) (msg_ptr) - 1)->len
80 
81 #define OSAL_MSG_ID(msg_ptr) ((osal_msg_hdr_t *) (msg_ptr) - 1)->dest_id
82 
83 /*********************************************************************
84  * CONSTANTS
85  */
86 
87 /*** Interrupts ***/
88 #define INTS_ALL 0xFF
89 
90 /*********************************************************************
91  * TYPEDEFS
92  */
93 #ifdef USE_ICALL
94 typedef ICall_MsgHdr osal_msg_hdr_t;
95 #else /* USE_ICALL */
96 typedef struct
97 {
98  void *next;
99 #ifdef OSAL_PORT2TIRTOS
100  /* Limited OSAL port to TI-RTOS requires compatibility with ROM
101  * code compiled with USE_ICALL compile flag. */
102  uint32 reserved;
103 #endif /* OSAL_PORT2TIRTOS */
104  uint16 len;
105  uint8 dest_id;
106 } osal_msg_hdr_t;
107 #endif /* USE_ICALL */
108 
110 typedef struct
111 {
112  uint8 event;
113  uint8 status;
115 
116 typedef void * osal_msg_q_t;
117 
118 #ifdef USE_ICALL
119 /* High resolution timer callback function type */
120 typedef void (*osal_highres_timer_cback_t)(void *arg);
121 #endif /* USE_ICALL */
122 
123 #ifdef ICALL_LITE
124 typedef void (*osal_icallMsg_hook_t)(void * param);
125 #endif /* ICALL_LITE */
126 
127 /*********************************************************************
128  * GLOBAL VARIABLES
129  */
130 #ifdef USE_ICALL
131 #ifdef ICALL_EVENTS
132 extern ICall_SyncHandle osal_syncHandle;
133 #else /* !ICALL_EVENTS */
134 extern ICall_Semaphore osal_semaphore;
135 #endif /* ICALL_EVENTS */
136 extern ICall_EntityID osal_entity;
137 extern uint_least32_t osal_tickperiod;
138 extern void (*osal_eventloop_hook)(void);
139 #endif /* USE_ICALL */
140 
141 
142 /*********************************************************************
143  * FUNCTIONS
144  */
145 
146 /*** Message Management ***/
147 
148  /*
149  * Task Message Allocation
150  */
151  extern uint8 * osal_msg_allocate(uint16 len );
152 
153  /*
154  * Task Message Deallocation
155  */
156  extern uint8 osal_msg_deallocate( uint8 *msg_ptr );
157 
158  /*
159  * Send a Task Message
160  */
161  extern uint8 osal_msg_send( uint8 destination_task, uint8 *msg_ptr );
162 
163  /*
164  * Push a Task Message to head of queue
165  */
166  extern uint8 osal_msg_push_front( uint8 destination_task, uint8 *msg_ptr );
167 
168  /*
169  * Receive a Task Message
170  */
171  extern uint8 *osal_msg_receive( uint8 task_id );
172 
173  /*
174  * Find in place a matching Task Message / Event.
175  */
176  extern osal_event_hdr_t *osal_msg_find(uint8 task_id, uint8 event);
177 
178  /*
179  * Count the number of queued OSAL messages matching Task ID / Event.
180  */
181  extern uint8 osal_msg_count(uint8 task_id, uint8 event);
182 
183  /*
184  * Enqueue a Task Message
185  */
186  extern void osal_msg_enqueue( osal_msg_q_t *q_ptr, void *msg_ptr );
187 
188  /*
189  * Enqueue a Task Message Up to Max
190  */
191  extern uint8 osal_msg_enqueue_max( osal_msg_q_t *q_ptr, void *msg_ptr, uint8 max );
192 
193  /*
194  * Dequeue a Task Message
195  */
196  extern void *osal_msg_dequeue( osal_msg_q_t *q_ptr );
197 
198  /*
199  * Push a Task Message to head of queue
200  */
201  extern void osal_msg_push( osal_msg_q_t *q_ptr, void *msg_ptr );
202 
203  /*
204  * Extract and remove a Task Message from queue
205  */
206  extern void osal_msg_extract( osal_msg_q_t *q_ptr, void *msg_ptr, void *prev_ptr );
207 
208 #ifdef USE_ICALL
209  extern ICall_Errno osal_service_entry(ICall_FuncArgsHdr *args);
210 #endif /* USE_ICALL */
211 
212 
213 /*** Task Synchronization ***/
214 
215  /*
216  * Set a Task Event
217  */
218  extern uint8 osal_set_event( uint8 task_id, uint16 event_flag );
219 
220 
221  /*
222  * Clear a Task Event
223  */
224  extern uint8 osal_clear_event( uint8 task_id, uint16 event_flag );
225 
226 
227 /*** Interrupt Management ***/
228 
229  /*
230  * Register Interrupt Service Routine (ISR)
231  */
232  extern uint8 osal_isr_register( uint8 interrupt_id, void (*isr_ptr)( uint8* ) );
233 
234  /*
235  * Enable Interrupt
236  */
237  extern uint8 osal_int_enable( uint8 interrupt_id );
238 
239  /*
240  * Disable Interrupt
241  */
242  extern uint8 osal_int_disable( uint8 interrupt_id );
243 
244 
245 /*** Task Management ***/
246 
247 #ifdef USE_ICALL
248  /*
249  * Enroll dispatcher registered entity ID
250  */
251  extern void osal_enroll_dispatchid(uint8 taskid,
252  ICall_EntityID dispatchid);
253 
254  /*
255  * Enroll an OSAL task to use another OSAL task's enrolled entity ID
256  * when sending a message.
257  */
258  extern void osal_enroll_senderid(uint8 taskid, ICall_EntityID dispatchid);
259 
260  /*
261  * Enroll entity ID to be used as sender entity ID for non OSAL task
262  */
263  extern void osal_enroll_notasksender(ICall_EntityID dispatchid);
264 
265 #ifdef ICALL_JT
266  /*
267  * Initialize osal timer module variable at init,
268  * based on parameter send by the application.
269  */
270  void osal_timer_init(uint_least32_t tickPeriod, uint_least32_t osalMaxMsecs);
271 #endif /* ICALL_JT */
272 
273 #endif /* USE_ICALL */
274 
275  /*
276  * Initialize the Task System
277  */
278  extern uint8 osal_init_system( void );
279 
280  /*
281  * System Processing Loop
282  */
283 #if defined (ZBIT)
284  extern __declspec(dllexport) void osal_start_system( void );
285 #else
286  extern void osal_start_system( void );
287 #endif
288 
289  /*
290  * One Pass Through the OSAL Processing Loop
291  */
292  extern void osal_run_system( void );
293 
294  /*
295  * Get the active task ID
296  */
297  extern uint8 osal_self( void );
298 
299 
300 /*** Helper Functions ***/
301 
302  /*
303  * String Length
304  */
305  extern int osal_strlen( char *pString );
306 
307  /*
308  * Memory copy
309  */
310  extern void *osal_memcpy( void*, const void GENERIC *, unsigned int );
311 
312  /*
313  * Memory Duplicate - allocates and copies
314  */
315  extern void *osal_memdup( const void GENERIC *src, unsigned int len );
316 
317  /*
318  * Reverse Memory copy
319  */
320  extern void *osal_revmemcpy( void*, const void GENERIC *, unsigned int );
321 
322  /*
323  * Memory compare
324  */
325  extern uint8 osal_memcmp( const void GENERIC *src1, const void GENERIC *src2, unsigned int len );
326 
327  /*
328  * Memory set
329  */
330  extern void *osal_memset( void *dest, uint8 value, int len );
331 
332  /*
333  * Build a uint16 out of 2 bytes (0 then 1).
334  */
335  extern uint16 osal_build_uint16( uint8 *swapped );
336 
337  /*
338  * Build a uint32 out of sequential bytes.
339  */
340  extern uint32 osal_build_uint32( uint8 *swapped, uint8 len );
341 
342  /*
343  * Convert long to ascii string
344  */
345  #if !defined ( ZBIT ) && !defined ( ZBIT2 ) && !defined (UBIT)
346  extern uint8 *_ltoa( uint32 l, uint8 * buf, uint8 radix );
347  #endif
348 
349  /*
350  * Random number generator
351  */
352  extern uint16 osal_rand( void );
353 
354  /*
355  * Buffer an uint32 value - LSB first.
356  */
357  extern uint8* osal_buffer_uint32( uint8 *buf, uint32 val );
358 
359  /*
360  * Buffer an uint24 value - LSB first
361  */
362  extern uint8* osal_buffer_uint24( uint8 *buf, uint24 val );
363 
364  /*
365  * Is all of the array elements set to a value?
366  */
367  extern uint8 osal_isbufset( uint8 *buf, uint8 val, uint8 len );
368 
369 #ifdef ICALL_LITE
370  /*
371  * set the callback to parse icall lite message.
372  */
373  extern void osal_set_icall_hook( osal_icallMsg_hook_t param );
374 
375  /*
376  * translate alien task Id to osal task id.
377  */
378  extern uint8 osal_alien2proxy(ICall_EntityID entity);
379 
380 #endif /* ICALL_LITE */
381 
382 /*********************************************************************
383 *********************************************************************/
384 
385 #ifdef __cplusplus
386 }
387 #endif
388 
389 #endif /* OSAL_H */
void * ICall_SyncHandle
Synchronization object data type.
Definition: icall.h:443
int_fast16_t ICall_Errno
Error code data type.
Definition: icall.h:431
Common Defines.
Common service function arguments.
Definition: icall.h:476
OSAL Event Header.
Definition: osal.h:110
ICall layer interface.
uint_least8_t ICall_EntityID
Entity id data type.
Definition: icall.h:453
Copyright 2016, Texas Instruments Incorporated