-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmac_util.c
364 lines (302 loc) · 7.48 KB
/
mac_util.c
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/******************************************************************************
@file mac_util.c
@brief Utility functions commonly used by TIMAC applications
Group: WCS LPC
$Target Device: DEVICES $
******************************************************************************
$License: BSD3 2016 $
******************************************************************************
$Release Name: PACKAGE NAME $
$Release Date: PACKAGE RELEASE DATE $
*****************************************************************************/
/******************************************************************************
Includes
*****************************************************************************/
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#ifndef __unix__
#include <ti/drivers/dpl/HwiP.h>
#else
#include "stdlib.h"
#include "compiler.h"
#include "hlos_specific.h"
#endif
#include "mac_util.h"
#include "api_mac.h"
/******************************************************************************
Typedefs
*****************************************************************************/
#define UTIL_SADDR_EXT_LEN 8
/******************************************************************************
Public Functions
*****************************************************************************/
/*!
Converts from a uint16 to ascii hex string.
Public function defined in mac_util.h
*/
void Util_uint16toa(uint16_t u, char *string)
{
if(string == NULL)
{
return;
}
/* add preceding zeros */
if(u < 0x1000)
{
*string++ = '0';
}
if(u < 0x0100)
{
*string++ = '0';
}
if(u < 0x0010)
{
*string++ = '0';
}
Util_ltoa( (unsigned long)u, (unsigned char *)string, 16 );
}
/*!
Convert a 16bit number to ASCII
Public function defined in mac_util.h
*/
void Util_itoa(uint16_t num, uint8_t *buf, uint8_t radix)
{
char c, i;
uint8_t *p, rst[5];
p = rst;
for(i = 0; i < 5; i++, p++)
{
c = num % radix; /* Isolate a digit */
*p = c + ( (c < 10) ? '0' : '7' ); /* Convert to Ascii */
num /= radix;
if(!num)
{
break;
}
}
for(c = 0; c <= i; c++)
{
*buf++ = *p--; /* Reverse character order */
}
*buf = '\0';
}
/*!
Convert a long unsigned int to a string.
Public function defined in mac_util.h
*/
unsigned char *Util_ltoa(uint32_t l, uint8_t *buf, uint8_t radix)
{
#if defined (__GNUC__) && !defined(__unix__)
return( (char *)ltoa(l, buf, radix) );
#else
unsigned char tmp1[10] = "", tmp2[10] = "", tmp3[10] = "";
unsigned short num1, num2, num3;
unsigned char i;
buf[0] = '\0';
if(radix == 10)
{
num1 = l % 10000;
num2 = (l / 10000) % 10000;
num3 = (unsigned short)(l / 100000000);
if(num3)
{
Util_itoa(num3, tmp3, 10);
}
if(num2)
{
Util_itoa(num2, tmp2, 10);
}
if(num1)
{
Util_itoa(num1, tmp1, 10);
}
if(num3)
{
strcpy( (char *)buf, (char const *)tmp3 );
for(i = 0; i < 4 - strlen( (char const *)tmp2 ); i++)
{
strcat( (char *)buf, "0" );
}
}
strcat( (char *)buf, (char const *)tmp2 );
if(num3 || num2)
{
for(i = 0; i < 4 - strlen( (char const *)tmp1 ); i++)
{
strcat( (char *)buf, "0" );
}
}
strcat( (char *)buf, (char const *)tmp1 );
if(!num3 && !num2 && !num1)
{
strcpy( (char *)buf, "0" );
}
}
else if(radix == 16)
{
num1 = l & 0x0000FFFF;
num2 = l >> 16;
if(num2)
{
Util_itoa(num2, tmp2, 16);
}
if(num1)
{
Util_itoa(num1, tmp1, 16);
}
if(num2)
{
strcpy( (char *)buf, (char const *)tmp2 );
for(i = 0; i < 4 - strlen( (char const *)tmp1 ); i++)
{
strcat( (char *)buf, "0" );
}
}
strcat( (char *)buf, (char const *)tmp1 );
if(!num2 && !num1)
{
strcpy( (char *)buf, "0" );
}
}
else
{
return(NULL);
}
return(buf);
#endif
}
/*!
Get the high byte of a uint16_t variable
Public function defined in mac_util.h
*/
uint8_t Util_hiUint16(uint16_t a)
{
return((a >> 8) & 0xFF);
}
/*!
Get the low byte of a uint16_t variable
Public function defined in mac_util.h
*/
uint8_t Util_loUint16(uint16_t a)
{
return((a) & 0xFF);
}
/*!
Build a uint16_t out of 2 uint8_t variables
Public function defined in mac_util.h
*/
uint16_t Util_buildUint16(uint8_t loByte, uint8_t hiByte)
{
return((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)));
}
/*!
Build a uint32_t out of 4 uint8_t variables
Public function defined in mac_util.h
*/
uint32_t Util_buildUint32(uint8_t byte0, uint8_t byte1, uint8_t byte2,
uint8_t byte3)
{
return((uint32_t)((uint32_t)((byte0) & 0x00FF) +
((uint32_t)((byte1) & 0x00FF) << 8) +
((uint32_t)((byte2) & 0x00FF) << 16) +
((uint32_t)((byte3) & 0x00FF) << 24)));
}
/*!
Pull 1 uint8_t out of a uint32_t
Public function defined in mac_util.h
*/
uint8_t Util_breakUint32(uint32_t var, int byteNum)
{
return(uint8_t)((uint32_t)(((var) >> ((byteNum) * 8)) & 0x00FF));
}
/*!
Build a uint16_t from a uint8_t array
Public function defined in mac_util.h
*/
uint16_t Util_parseUint16(uint8_t *pArray)
{
return(Util_buildUint16(pArray[0], pArray[1]));
}
/*!
Build a uint32_t from a uint8_t array
Public function defined in mac_util.h
*/
uint32_t Util_parseUint32(uint8_t *pArray)
{
return(Util_buildUint32(pArray[0], pArray[1], pArray[2], pArray[3]));
}
/*!
Break and buffer a uint16_t value - LSB first
Public function defined in mac_util.h
*/
uint8_t *Util_bufferUint16(uint8_t *pBuf, uint16_t val)
{
*pBuf++ = Util_loUint16(val);
*pBuf++ = Util_hiUint16(val);
return(pBuf);
}
/*!
Break and buffer a uint32_t value - LSB first
Public function defined in mac_util.h
*/
uint8_t *Util_bufferUint32(uint8_t *pBuf, uint32_t val)
{
*pBuf++ = Util_breakUint32(val, 0);
*pBuf++ = Util_breakUint32(val, 1);
*pBuf++ = Util_breakUint32(val, 2);
*pBuf++ = Util_breakUint32(val, 3);
return(pBuf);
}
/*!
Utility function to clear an event
Public function defined in mac_util.h
*/
void Util_clearEvent(uint16_t *pEvent, uint16_t event)
{
#ifndef __unix__
uint32_t key;
/* Enter critical section */
key = HwiP_disable();
#else
_ATOMIC_global_lock();
#endif
/* Clear the event */
*pEvent &= ~(event);
/* Exit critical section */
#ifndef __unix__
HwiP_restore(key);
#else
_ATOMIC_global_unlock();
#endif
}
/*!
Utility function to set an event
Public function defined in mac_util.h
*/
void Util_setEvent(uint16_t *pEvent, uint16_t event)
{
#ifndef __unix__
uint32_t key;
/* Enter critical section */
key = HwiP_disable();
#else
_ATOMIC_global_lock();
#endif
/* Set the event */
*pEvent |= event;
/* Exit critical section */
#ifndef __unix__
HwiP_restore(key);
#else
_ATOMIC_global_unlock();
#endif
}
/*!
Utility function to copy the extended address
Public function defined in mac_util.h
*/
void Util_copyExtAddr(void *pSrcAddr, void *pDstAddr)
{
memcpy(pSrcAddr, pDstAddr, (UTIL_SADDR_EXT_LEN));
}