-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.c
More file actions
132 lines (118 loc) · 3.15 KB
/
util.c
File metadata and controls
132 lines (118 loc) · 3.15 KB
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
/**
* @file util.h
* @brief Miscellaneous Utility methods
* @details
* @author Noah Huetter (noahhuetter@gmail.com)
* @date 1 December 2016
*
*
* @addtogroup UTIL
* @brief Utility finctions
* @{
*/
#include "util.h"
#include "ch.h"
#include "hal.h"
#include "defs.h"
#include "chprintf.h"
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
/*===========================================================================*/
/* Module public functions. */
/*===========================================================================*/
/**
* @brief Dump a memory block in a readable hex format to the output
* stream
*
* @param bss output stream
* @param mem start memory
* @param[in] len memory length
*/
void hexdump(BaseSequentialStream * bss, void *mem, unsigned int len)
{
unsigned int i, j;
if (bss)
{
for(i = 0; i < len + ((len % 16) ? (16 - len % 16) : 0); i++)
{
/* print offset */
if(i % 16 == 0)
chprintf(bss, "0x%06x: ", i);
/* print hex data */
if(i < len)
chprintf(bss,"%02x ", 0xFF & ((char*)mem)[i]);
else /* end of block, just aligning for ASCII dump */
chprintf(bss, " ");
/* print ASCII dump */
if(i % 16 == (16 - 1))
{
for(j = i - (16 - 1); j <= i; j++)
{
if(j >= len) /* end of block, not really printing */
chprintf(bss," ");
else if( ((((char*)mem)[j]) > 0x20) && ((((char*)mem)[j]) < 0x7F)) /* printable char */
chprintf(bss, "%c", 0xFF & ((char*)mem)[j]);
else /* other char */
chprintf(bss, ".");
}
chprintf(bss, "\r\n");
}
}
chprintf(bss,"\r\n");
}
}
/**
* @brief strip trailing stripchar from string
*
* @param str string
* @param[in] trimchar character to trim
*
* @return charpointer to a new string from beginning to stripchar
*/
char * rtrim(char * str, char trimchar)
{
char *pos;
pos=strrchr(str, trimchar);
if (pos != NULL)
*pos = '\0';
return str;
}
/**
* @brief Tries to lock a mutex for the given time and exits
*
* @param mtx The mtx
* param[in] t timeout in ms to wait
*
* @return true if mutex got locked succesfully, false if mutex is already locked
*/
bool lockMtx(mutex_t* mtx, uint32_t t)
{
uint32_t ctr = 0;
while(ctr <= t)
{
if(chMtxTryLock(mtx))
{
return true;
}
chThdSleepMilliseconds(1);
ctr++;
}
return false;
}
/**
* @brief Returns the WA Size of the given thread
*
* @param tp thread pointer
*
* @return The thd wa size.
*/
size_t getThdWaSize(thread_t *tp)
{
const char* nm = chRegGetThreadNameX(tp);
if(strcmp(nm,DEFS_THD_IDLE_NAME) == 0) return (size_t)DEFS_THD_IDLE_WA_SIZE;
if(strcmp(nm,DEFS_THD_SHELL_NAME) == 0) return (size_t)DEFS_THD_SHELL_WA_SIZE;
return 0;
}
/** @} */