le_basics.h

Go to the documentation of this file.
1 /**
2  * @page c_basics Basic Type and Constant Definitions
3  *
4  * Cardinal types and commonly-used constants form the basic
5  * foundation on which everything else is built. These include
6  * error codes, portable integer types, and helpful macros that make things easier to use.
7  *
8  * See @ref le_basics.h for basic cardinal types and commonly-used constants info.
9  *
10  * <HR>
11  *
12  * Copyright (C) Sierra Wireless Inc.
13  */
14 
15 
16 /** @file le_basics.h
17  *
18  * Legato @ref c_basics include file.
19  *
20  * Copyright (C) Sierra Wireless Inc.
21  */
22 
23 #ifndef LEGATO_BASICS_INCLUDE_GUARD
24 #define LEGATO_BASICS_INCLUDE_GUARD
25 
26 //--------------------------------------------------------------------------------------------------
27 /**
28  * Standard result codes.
29  *
30  * @note All error codes are negative integers. They allow functions with signed
31  * integers to return non-negative values when successful or standard error codes on failure.
32  * @deprecated the result code LE_NOT_POSSIBLE is scheduled to be removed.
33  */
34 //--------------------------------------------------------------------------------------------------
35 typedef enum
36 {
37  LE_OK = 0, ///< Successful.
38  LE_NOT_FOUND = -1, ///< Referenced item does not exist or could not be found.
39  LE_NOT_POSSIBLE = -2, ///< @deprecated It is not possible to perform the requested action.
40  LE_OUT_OF_RANGE = -3, ///< An index or other value is out of range.
41  LE_NO_MEMORY = -4, ///< Insufficient memory is available.
42  LE_NOT_PERMITTED = -5, ///< Current user does not have permission to perform requested action.
43  LE_FAULT = -6, ///< Unspecified internal error.
44  LE_COMM_ERROR = -7, ///< Communications error.
45  LE_TIMEOUT = -8, ///< A time-out occurred.
46  LE_OVERFLOW = -9, ///< An overflow occurred or would have occurred.
47  LE_UNDERFLOW = -10, ///< An underflow occurred or would have occurred.
48  LE_WOULD_BLOCK = -11, ///< Would have blocked if non-blocking behaviour was not requested.
49  LE_DEADLOCK = -12, ///< Would have caused a deadlock.
50  LE_FORMAT_ERROR = -13, ///< Format error.
51  LE_DUPLICATE = -14, ///< Duplicate entry found or operation already performed.
52  LE_BAD_PARAMETER = -15, ///< Parameter is invalid.
53  LE_CLOSED = -16, ///< The resource is closed.
54  LE_BUSY = -17, ///< The resource is busy.
55  LE_UNSUPPORTED = -18, ///< The underlying resource does not support this operation.
56  LE_IO_ERROR = -19, ///< An IO operation failed.
57  LE_NOT_IMPLEMENTED = -20, ///< Unimplemented functionality.
58  LE_UNAVAILABLE = -21, ///< A transient or temporary loss of a service or resource.
59  LE_TERMINATED = -22, ///< The process, operation, data stream, session, etc. has stopped.
60 }
62 
63 
64 //--------------------------------------------------------------------------------------------------
65 /**
66  * ON/OFF type.
67  *
68  */
69 //--------------------------------------------------------------------------------------------------
70 typedef enum
71 {
72  LE_OFF = 0,
73  LE_ON = 1,
74 }
76 
77 
78 //--------------------------------------------------------------------------------------------------
79 /** @name Bit Masks
80  *
81  * Single byte bit definitions that can be used for bit masking.
82  * @{
83  */
84 //--------------------------------------------------------------------------------------------------
85 #define BIT0 0x01
86 #define BIT1 0x02
87 #define BIT2 0x04
88 #define BIT3 0x08
89 #define BIT4 0x10
90 #define BIT5 0x20
91 #define BIT6 0x40
92 #define BIT7 0x80
93 // @}
94 
95 //--------------------------------------------------------------------------------------------------
96 /**
97  * Find the address of a containing structure or union, based on the address of one of its members.
98  *
99  * If @c countPtr points to the @c count member of an object type
100  * @c my_class_t, then a pointer to that object should use this:
101  *
102  * @code
103  * my_class_t* myObjPtr = CONTAINER_OF(countPtr, my_class_t, count);
104  * @endcode
105  */
106 //--------------------------------------------------------------------------------------------------
107 #define CONTAINER_OF(memberPtr, type, member) \
108  ((type*)(((uint8_t*)(memberPtr))-((size_t)(&(((type*)0)->member)))))
109 
110 
111 //--------------------------------------------------------------------------------------------------
112 /**
113  * Computes number of members in an array at compile time.
114  *
115  * @warning Does NOT work for pointers to arrays.
116  *
117  * Here's a code sample:
118  *
119  * @code
120  * const char message[] = "Hello world!";
121  *
122  * size_t x = NUM_ARRAY_MEMBERS(message);
123  *
124  * printf("%lu\n", x);
125  * @endcode
126  *
127  * Will print @c 13 on a 32-bit target.
128  *
129  * But this example is @b incorrect:
130  *
131  * @code
132  * const char message[] = "Hello world!";
133  * const char* messagePtr = &message;
134  *
135  * size_t x = NUM_ARRAY_MEMBERS(messagePtr); // NO! BAD PROGRAMMER! BAD!
136  *
137  * printf("%lu\n", x);
138  * @endcode
139  */
140 //--------------------------------------------------------------------------------------------------
141 #define NUM_ARRAY_MEMBERS(array) \
142  (sizeof(array) / sizeof((array)[0]))
143 
144 
145 //--------------------------------------------------------------------------------------------------
146 /**
147  * Computes the index of a member within an array.
148  *
149  * This code sample prints out "The 'w' is at index 6.":
150  *
151  * @code
152  * const char message[] = "Hello world!";
153  * const char* charPtr;
154  * int i;
155  *
156  * for (i = 0; i < sizeof(message); i++)
157  * {
158  * if (message[i] == 'w')
159  * {
160  * charPtr = &message[i];
161  * }
162  * }
163  *
164  * printf("The 'w' is at index %zu.\n", INDEX_OF_ARRAY_MEMBER(message, charPtr));
165  * @endcode
166  **/
167 //--------------------------------------------------------------------------------------------------
168 #define INDEX_OF_ARRAY_MEMBER(array, memberPtr) \
169  ((((size_t)memberPtr) - ((size_t)array)) / sizeof(*(memberPtr)))
170 
171 
172 //--------------------------------------------------------------------------------------------------
173 /**
174  * This function takes the characters as an argument and puts quotes around them.
175  *
176  * Code sample:
177  *
178  * @code
179  * const char name[] = STRINGIZE(foo);
180  * @endcode
181  *
182  * Is seen by the compiler as
183  *
184  * @code
185  * const char name[] = "foo";
186  * @endcode
187  *
188  * The STRINGIZE() macro function passes names like macro definitions
189  * on the compiler's command-line. If the above code were changed to:
190  *
191  * @code
192  * const char name[] = STRINGIZE(NAME);
193  * @endcode
194  *
195  * then compiling it using the following command line makes it equivalent to the
196  * example above:
197  *
198  * @code
199  * $ gcc -c -DNAME=foo file.c
200  * @endcode
201  *
202  * The <c>-DNAME=foo</c> defines a macro called <c>NAME</c> with a value <c>foo</c>.
203  * The C preprocessor then replaces <c>STRINGIZE(NAME)</c> with <c>"foo"</c>.
204  */
205 //--------------------------------------------------------------------------------------------------
206 #define STRINGIZE(x) STRINGIZE_EXPAND(x)
207 
208 //--------------------------------------------------------------------------------------------------
209 /**
210  * Helper macro for @ref STRINGIZE(x).
211  */
212 //--------------------------------------------------------------------------------------------------
213 #define STRINGIZE_EXPAND(x) #x // Needed to expand macros.
214 
215 
216 //--------------------------------------------------------------------------------------------------
217 /**
218  * Macro used to declare that a symbol should be shared outside the dynamic shared object in
219  * which it is defined.
220  *
221  * This can be used with either a declaration or a definition.
222  *
223  * E.g., with a declaration (perhaps in a header file):
224  *
225  * @code
226  * LE_SHARED void my_Function();
227  * @endcode
228  *
229  * E.g., with a definition (in a .c file):
230  *
231  * @code
232  * LE_SHARED void my_OtherFunction()
233  * {
234  * LE_INFO("Hello world.");
235  * }
236  * @endcode
237  *
238  **/
239 //--------------------------------------------------------------------------------------------------
240 #define LE_SHARED __attribute__((visibility ("default")))
241 
242 // CLang feature check macros -- define to return sensible defaults if macro is not available.
243 #ifndef __is_identifier
244 # define __is_identifier(x) 1
245 #endif
246 
247 #ifndef __has_warning
248 # define __has_warning(x) 0
249 #endif
250 
251 #if !__is_identifier(_Nonnull)
252 
253 // Nullability information is not complete. Normally clang will warn in that case.
254 # if __has_warning("-Wnullability-completeness")
255 # pragma clang diagnostic ignored "-Wnullability-completeness"
256 # endif
257 
258 //--------------------------------------------------------------------------------------------------
259 /**
260  * Mark a parameter or return value as never NULL.
261  */
262 //--------------------------------------------------------------------------------------------------
263 # define LE_NONNULL _Nonnull
264 
265 //--------------------------------------------------------------------------------------------------
266 /**
267  * Mark a parameter or return value as potentially NULL.
268  */
269 //--------------------------------------------------------------------------------------------------
270 # define LE_NULLABLE _Nullable
271 #else
272 # define LE_NONNULL
273 # define LE_NULLABLE
274 #endif
275 
276 #endif // LEGATO_BASICS_INCLUDE_GUARD
Format error.
Definition: le_basics.h:50
The underlying resource does not support this operation.
Definition: le_basics.h:55
Current user does not have permission to perform requested action.
Definition: le_basics.h:42
Unspecified internal error.
Definition: le_basics.h:43
Duplicate entry found or operation already performed.
Definition: le_basics.h:51
Insufficient memory is available.
Definition: le_basics.h:41
le_result_t
Definition: le_basics.h:35
An IO operation failed.
Definition: le_basics.h:56
Would have caused a deadlock.
Definition: le_basics.h:49
Successful.
Definition: le_basics.h:37
Unimplemented functionality.
Definition: le_basics.h:57
An overflow occurred or would have occurred.
Definition: le_basics.h:46
A time-out occurred.
Definition: le_basics.h:45
An underflow occurred or would have occurred.
Definition: le_basics.h:47
Referenced item does not exist or could not be found.
Definition: le_basics.h:38
The resource is closed.
Definition: le_basics.h:53
A transient or temporary loss of a service or resource.
Definition: le_basics.h:58
Definition: le_basics.h:39
The resource is busy.
Definition: le_basics.h:54
An index or other value is out of range.
Definition: le_basics.h:40
Parameter is invalid.
Definition: le_basics.h:52
Would have blocked if non-blocking behaviour was not requested.
Definition: le_basics.h:48
The process, operation, data stream, session, etc. has stopped.
Definition: le_basics.h:59
Communications error.
Definition: le_basics.h:44
le_onoff_t
Definition: le_basics.h:70