EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
helperFunctions.cpp
Go to the documentation of this file.
1 
9 #include "helperFunctions.h"
10 #ifdef ESP32
11 #include <esp_wifi.h>
12 #endif
13 
14 #define MAX_STR_LEN 1000
15 
16 char* printHexBuffer (const uint8_t* buffer, uint16_t len) {
17  static char tempStr[MAX_STR_LEN];
18  int charIndex = 0;
19 
20  memset (tempStr, 0, MAX_STR_LEN);
21 
22  if (buffer) {
23  for (int i = 0; i < len; i++) {
24  if (charIndex < MAX_STR_LEN - 2) {
25  charIndex += sprintf (tempStr + charIndex, "%02X ", buffer[i]);
26  }
27  }
28  }
29  return tempStr;
30 }
31 
32 void initWiFi (uint8_t channel, const char* networkName, const char* networkKey, uint8_t role) {
33  DEBUG_DBG ("initWifi");
34  if (role == 0) { // Node
35  WiFi.mode (WIFI_STA);
36 #ifdef ESP32
37  esp_err_t err_ok;
38  if ((err_ok = esp_wifi_set_promiscuous (true))) {
39  DEBUG_ERROR ("Error setting promiscuous mode: %s", esp_err_to_name (err_ok));
40  }
41  if ((err_ok = esp_wifi_set_channel (channel, WIFI_SECOND_CHAN_NONE))) {
42  DEBUG_ERROR ("Error setting wifi channel: %s", esp_err_to_name (err_ok));
43  }
44  if ((err_ok = esp_wifi_set_promiscuous (false))) {
45  DEBUG_ERROR ("Error setting promiscuous mode off: %s", esp_err_to_name (err_ok));
46  }
47 #endif
48  WiFi.disconnect ();
49 #ifdef ESP8266
50  wifi_set_channel (channel);
51 #endif
52  DEBUG_DBG ("Mode set to STA. Channel %u", channel);
53  } else { // Gateway
54  WiFi.mode (WIFI_AP);
55  WiFi.softAP (networkName, networkKey, channel);
56  DEBUG_DBG ("Mode set to AP in channel %u", channel);
57  }
58 
59  DEBUG_INFO ("AP MAC address of this device is %s", WiFi.softAPmacAddress ().c_str ());
60  DEBUG_INFO ("STA MAC address of this device is %s", WiFi.macAddress ().c_str ());
61 
62 }
63 
64 uint32_t calculateCRC32 (const uint8_t* data, size_t length) {
65  uint32_t crc = 0xffffffff;
66  while (length--) {
67  uint8_t c = *data++;
68  for (uint32_t i = 0x80; i > 0; i >>= 1) {
69  bool bit = crc & 0x80000000;
70  if (c & i) {
71  bit = !bit;
72  }
73  crc <<= 1;
74  if (bit) {
75  crc ^= 0x04c11db7;
76  }
77  }
78  }
79  return crc;
80 }
81 
82 #undef MACSTR
83 #define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X"
84 
85 char* mac2str (const uint8_t* mac, char* extBuffer) {
86  char* buffer;
87  static char staticBuffer[ENIGMAIOT_ADDR_LEN * 3];
88 
89  if (!extBuffer){
90  buffer = staticBuffer;
91  } else {
92  buffer = extBuffer;
93  }
94 
95  if (mac && buffer) {
96  //DEBUG_DBG ("mac2str %02x:%02x:%02x:%02x:%02x:%02x",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
97  snprintf (buffer, ENIGMAIOT_ADDR_LEN * 3, MACSTR, MAC2STR (mac));
98  //DEBUG_DBG ("Address: %s", buffer);
99  return buffer;
100  }
101  return NULL;
102 }
103 
104 uint8_t* str2mac (const char* macAddrString, uint8_t* macBytes) {
105  const char cSep = ':';
106 
107  if (!macBytes) {
108  return NULL;
109  }
110 
111  for (int i = 0; i < 6; ++i) {
112  unsigned int iNumber = 0;
113  char ch;
114 
115  //Convert letter into lower case.
116  ch = tolower (*macAddrString++);
117 
118  if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f')) {
119  return NULL;
120  }
121 
122  //Convert into number.
123  // a. If character is digit then ch - '0'
124  // b. else (ch - 'a' + 10) it is done
125  // because addition of 10 takes correct value.
126  iNumber = isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
127  ch = tolower (*macAddrString);
128 
129  if ((i < 5 && ch != cSep) ||
130  (i == 5 && ch != '\0' && !isspace (ch))) {
131  ++macAddrString;
132 
133  if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f')) {
134  return NULL;
135  }
136 
137  iNumber <<= 4;
138  iNumber += isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
139  ch = *macAddrString;
140 
141  if (i < 5 && ch != cSep) {
142  return NULL;
143  }
144  }
145  /* Store result. */
146  macBytes[i] = (unsigned char)iNumber;
147  /* Skip cSep. */
148  ++macAddrString;
149  }
150  return macBytes;
151 }
152 
153 #ifdef ESP8266
154 const char* IRAM_ATTR extractFileName (const char* path) {
155  size_t i = 0;
156  size_t pos = 0;
157  char* p = (char*)path;
158  while (*p) {
159  i++;
160  if (*p == '/' || *p == '\\') {
161  pos = i;
162  }
163  p++;
164  }
165  return path + pos;
166 }
167 #endif
168 
169 bool isNumber (const char* input) {
170  unsigned int index = 0;
171  size_t len = strlen (input);
172 
173  if (!len) {
174  return false;
175  }
176 
177  while (index < len) {
178  if (!isDigit (input[index])) {
179  return false;
180  }
181  index++;
182  }
183  return true;
184 }
185 
186 bool isNumber (const char* input, size_t len) {
187  unsigned int index = 0;
188 
189  if (!len) {
190  return false;
191  }
192 
193  while (input[index] != '\0' && index < len) {
194  if (!isDigit (input[index])) {
195  return false;
196  }
197  index++;
198  }
199  return true;
200 }
201 
202 bool isNumber (String input) {
203  unsigned int index = 0;
204  size_t len = input.length ();
205 
206  if (!len) {
207  return false;
208  }
209 
210  while (index < len) {
211  if (!isDigit (input[index])) {
212  return false;
213  }
214  index++;
215  }
216  return true;
217 }
218 
219 //int str2mac (const char* mac, uint8_t* values) {
220 // int error = std::sscanf (mac, "%02x:%02x:%02x:%02x:%02x:%02x", &values[0], &values[1], &values[2], &values[3], &values[4], &values[5]);
221 // Serial.printf ("Error: %d", error);
222 // if (error == 6) {
223 // for (int i = 0; i < 6; i++) {
224 // Serial.println (values[i]);
225 // }
226 // return 1;
227 // }
228 // else {
229 // return 0;
230 // }
231 //}
ENIGMAIOT_ADDR_LEN
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
Definition: EnigmaIoTconfigAdvanced.h:23
initWiFi
void initWiFi(uint8_t channel, const char *networkName, const char *networkKey, uint8_t role)
Initalizes WiFi interfaces on ESP8266 or ESP32.
Definition: helperFunctions.cpp:32
str2mac
uint8_t * str2mac(const char *macAddrString, uint8_t *macBytes)
Debug helper function that creates MAC address byte array from text representation.
Definition: helperFunctions.cpp:104
MAX_STR_LEN
#define MAX_STR_LEN
Key length used by selected crypto algorythm.
Definition: helperFunctions.cpp:14
printHexBuffer
char * printHexBuffer(const uint8_t *buffer, uint16_t len)
Debug helper function that generates a string that represent a buffer hexadecimal values.
Definition: helperFunctions.cpp:16
MACSTR
#define MACSTR
Definition: helperFunctions.cpp:83
data
@ data
Definition: GwOutput_generic.h:23
mac2str
char * mac2str(const uint8_t *mac, char *extBuffer)
Debug helper function that generates a string that represent a MAC address.
Definition: helperFunctions.cpp:85
helperFunctions.h
Auxiliary function definition.
calculateCRC32
uint32_t calculateCRC32(const uint8_t *data, size_t length)
Calculates CRC32 of a buffer.
Definition: helperFunctions.cpp:64
isNumber
bool isNumber(const char *input)
Checks if input string is numeric.
Definition: helperFunctions.cpp:169