IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
util_string.c
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file util_string.c
3  * @brief String (char array) conversion functionality.
4  * @version 1.0
5  * @author Brecht Van Eeckhoudt
6  *
7  * ******************************************************************************
8  *
9  * @section Versions
10  *
11  * @li v1.0: Initial version.
12  *
13  * ******************************************************************************
14  *
15  * @section License
16  *
17  * **Copyright (C) 2020 - Brecht Van Eeckhoudt**
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the **GNU General Public License** as published by
21  * the Free Software Foundation, either **version 3** of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * *A copy of the GNU General Public License can be found in the `LICENSE`
30  * file along with this source code.*
31  *
32  * @n
33  *
34  * Some methods use code obtained from examples from [Silicon Labs' GitHub](https://github.com/SiliconLabs/peripheral_examples).
35  * These sections are licensed under the Silabs License Agreement. See the file
36  * "Silabs_License_Agreement.txt" for details. Before using this software for
37  * any purpose, you must agree to the terms of that agreement.
38  *
39  ******************************************************************************/
40 
41 
42 #include <stdint.h> /* (u)intXX_t */
43 #include <stdbool.h> /* "bool", "true", "false" */
44 #include "util_string.h" /* Corresponding header file */
45 
46 
47 /**************************************************************************//**
48  * @brief
49  * Convert an ASCII char array (string) to a hexadecimal char array.
50  *
51  * @note
52  * If the input is not a string (ex.: `"Hello world!"`) but a char array,
53  * the input message (array) needs to end with NULL (`"\0"`)!
54  *
55  * @param[in] inbuf
56  * The given ASCII string to convert to a hexadecimal string.
57  *
58  * @param[out] outbuf
59  * The buffer to put the resulting string in.@n
60  * **This needs to have twice the length of the input message + 1!**
61  * (two HEX characters for one ASCII character + NULL termination)
62  *****************************************************************************/
63 void charASCII_to_charHex (char *inbuf, char *outbuf)
64 {
65  /* Lookup table array */
66  char hex_lut[] = "0123456789abcdef";
67 
68  /* Calculate length of the given string */
69  uint8_t length = stringLength(inbuf);
70 
71  /* Convert each character */
72  for (uint16_t i = 0; i < length; i++)
73  {
74  /* Only get necessary nibble by ANDing with a mask and
75  * shifting one nibble (4 bits) per position */
76  outbuf[(i*2)] = hex_lut[(inbuf[i] & 0xF0) >> 4 ];
77  outbuf[(i*2)+1] = hex_lut[(inbuf[i] & 0x0F) ];
78  }
79 
80  outbuf[length*2] = '\0'; /* NULL termination character */
81 }
82 
83 
84 /**************************************************************************//**
85  * @brief
86  * Convert a `uint32_t` value to a decimal char array (string).
87  *
88  * @param[in] value
89  * The `uint32_t` value to convert to a string.
90  *
91  * @param[out] outbuf
92  * The buffer to put the resulting string in.@n
93  * **This needs to have a length of 10: `char buf[10];`!**
94  *****************************************************************************/
95 void uint32_to_charDec (uint32_t value, char *outbuf)
96 {
97  if (value == 0)
98  {
99  outbuf[0] = '0';
100  outbuf[1] = '\0'; /* NULL termination character */
101  }
102  else
103  {
104  /* MAX uint32_t value = FFFFFFFFh = 4294967295d (10 decimal chars) */
105  char backwardsBuf[10];
106 
107  uint32_t calcval = value;
108  uint8_t length = 0;
109  uint8_t lengthCounter = 0;
110 
111 
112  /* Loop until the value is zero (separate characters 0-9) and calculate length */
113  while (calcval)
114  {
115  uint32_t rem = calcval % 10;
116  backwardsBuf[length] = '0' + rem; /* Convert to ASCII character */
117  length++;
118 
119  calcval = calcval - rem;
120  calcval = calcval / 10;
121  }
122 
123  /* Backwards counter */
124  lengthCounter = length;
125 
126  /* Reverse the characters in the buffer for the final string */
127  for (uint8_t i = 0; i < length; i++)
128  {
129  outbuf[i] = backwardsBuf[lengthCounter-1];
130  lengthCounter--;
131  }
132 
133  /* Add NULL termination character */
134  outbuf[length] = '\0';
135  }
136 }
137 
138 
139 /**************************************************************************//**
140  * @brief
141  * Convert a `uint32_t` value to a hexadecimal char array (string).
142  *
143  * @param[in] value
144  * The `uint32_t` value to convert to a string.
145  *
146  * @param[out] outbuf
147  * The buffer to put the resulting string in.@n
148  * **This needs to have a length of 9: `char buf[9];`!**
149  *****************************************************************************/
150 void uint32_to_charHex (uint32_t value, char *outbuf)
151 {
152  /* Lookup table array */
153  char hex_lut[] = "0123456789abcdef";
154 
155  /* 4 nibble HEX representation */
156  if (value <= 0xFFFF)
157  {
158  /* Only get necessary nibble by ANDing with a mask and
159  * shifting one nibble (4 bits) per position */
160  outbuf[0] = hex_lut[(value & 0xF000) >> 12];
161  outbuf[1] = hex_lut[(value & 0x0F00) >> 8 ];
162  outbuf[2] = hex_lut[(value & 0x00F0) >> 4 ];
163  outbuf[3] = hex_lut[(value & 0x000F) ];
164  outbuf[4] = '\0'; /* NULL termination character */
165  }
166 
167  /* 8 nibble HEX representation */
168  else
169  {
170  /* Only get necessary nibble by ANDing with a mask and
171  * shifting one nibble (4 bits) per position */
172  outbuf[0] = hex_lut[(value & 0xF0000000) >> 28];
173  outbuf[1] = hex_lut[(value & 0x0F000000) >> 24];
174  outbuf[2] = hex_lut[(value & 0x00F00000) >> 20];
175  outbuf[3] = hex_lut[(value & 0x000F0000) >> 16];
176  outbuf[4] = hex_lut[(value & 0x0000F000) >> 12];
177  outbuf[5] = hex_lut[(value & 0x00000F00) >> 8 ];
178  outbuf[6] = hex_lut[(value & 0x000000F0) >> 4 ];
179  outbuf[7] = hex_lut[(value & 0x0000000F) ];
180  outbuf[8] = '\0'; /* NULL termination character */
181  }
182 }
183 
184 
185 /**************************************************************************//**
186  * @brief
187  * Check if a given char array (string) starts with a given sequence.
188  *
189  * @note
190  * If the input is not a string (ex.: `"Hello world!"`) but a char array,
191  * the input message (array) needs to end with NULL (`"\0"`)!
192  *
193  * @param[in] inbuf
194  * The given char array (string) to check a sequence in.
195  *
196  * @param[in] sequence
197  * The sequence to check in the given char array.
198  *
199  * @return
200  * @li `true` - The given sequence is found in the char array.
201  * @li `false` -The given sequence is not found in the char array.
202  *****************************************************************************/
203 bool stringStartsWith (char *inbuf, char *sequence)
204 {
205  /* Go through each character of "sequence" */
206  for (uint8_t i = 0; i < stringLength(sequence); i++ )
207  {
208  /* Return false if the value at the memory location (pointer, '*') of
209  * (inbuf+i) isn't equal to the value at the memory location of (sequence+i) */
210  if (*(inbuf+i) != *(sequence+i)) return (false);
211  }
212 
213  return (true);
214 }
215 
216 
217 /**************************************************************************//**
218  * @brief
219  * Returns the length of the given null-terminated byte string.
220  *
221  * @param[in] inbuf
222  * The given char array (string).
223  *****************************************************************************/
224 uint8_t stringLength (char *inbuf)
225 {
226  uint8_t length = 0;
227 
228  /* Count up until we reach a NULL */
229  while (inbuf[length] != '\0') length++;
230 
231  return (length);
232 }
uint8_t stringLength(char *inbuf)
Returns the length of the given null-terminated byte string.
Definition: util_string.c:224
void charASCII_to_charHex(char *inbuf, char *outbuf)
Convert an ASCII char array (string) to a hexadecimal char array.
Definition: util_string.c:63
void uint32_to_charHex(uint32_t value, char *outbuf)
Convert a uint32_t value to a hexadecimal char array (string).
Definition: util_string.c:150
String (char array) conversion functionality.
bool stringStartsWith(char *inbuf, char *sequence)
Check if a given char array (string) starts with a given sequence.
Definition: util_string.c:203
void uint32_to_charDec(uint32_t value, char *outbuf)
Convert a uint32_t value to a decimal char array (string).
Definition: util_string.c:95
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:98
unsigned int uint32_t
Typedef defining 32 bit unsigned int type. The developer should modify this to suit the platform bein...
Definition: vl53l1_types.h:78