EnigmaIOT  0.9.3
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 //#include <sstream>
11 #ifdef ESP32
12 #include <esp_wifi.h>
13 #endif
14 
15 #define MAX_STR_LEN 1000
16 
17 char* printHexBuffer (const uint8_t* buffer, uint16_t len) {
18  static char tempStr[MAX_STR_LEN];
19  int charIndex = 0;
20 
21  memset (tempStr, 0, MAX_STR_LEN);
22 
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  return tempStr;
29 }
30 
31 void initWiFi (uint8_t channel, const char* networkName, const char* networkKey, uint8_t role) {
32  DEBUG_DBG ("initWifi");
33  if (role == 0) { // Node
34  WiFi.mode (WIFI_STA);
35 #ifdef ESP32
36  esp_err_t err_ok;
37  if ((err_ok = esp_wifi_set_promiscuous (true))) {
38  DEBUG_ERROR ("Error setting promiscuous mode: %s", esp_err_to_name (err_ok));
39  }
40  if ((err_ok = esp_wifi_set_channel (channel, WIFI_SECOND_CHAN_NONE))) {
41  DEBUG_ERROR ("Error setting wifi channel: %s", esp_err_to_name (err_ok));
42  }
43  if ((err_ok = esp_wifi_set_promiscuous (false))) {
44  DEBUG_ERROR ("Error setting promiscuous mode off: %s", esp_err_to_name (err_ok));
45  }
46 #endif
47  WiFi.disconnect ();
48 #ifdef ESP8266
49  wifi_set_channel (channel);
50 #endif
51  DEBUG_DBG ("Mode set to STA. Channel %u", channel);
52  } else { // Gateway
53  WiFi.mode (WIFI_AP);
54  WiFi.softAP (networkName, networkKey, channel);
55  DEBUG_DBG ("Mode set to AP in channel %u", channel);
56  }
57 
58  DEBUG_INFO ("AP MAC address of this device is %s", WiFi.softAPmacAddress ().c_str ());
59  DEBUG_INFO ("STA MAC address of this device is %s", WiFi.macAddress ().c_str ());
60 
61 }
62 
63 uint32_t calculateCRC32 (const uint8_t* data, size_t length) {
64  uint32_t crc = 0xffffffff;
65  while (length--) {
66  uint8_t c = *data++;
67  for (uint32_t i = 0x80; i > 0; i >>= 1) {
68  bool bit = crc & 0x80000000;
69  if (c & i) {
70  bit = !bit;
71  }
72  crc <<= 1;
73  if (bit) {
74  crc ^= 0x04c11db7;
75  }
76  }
77  }
78  return crc;
79 }
80 
81 #undef MACSTR
82 #define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X"
83 
84 char* mac2str (const uint8_t* mac, char* buffer) {
85  if (mac && buffer) {
86  //DEBUG_DBG ("mac2str %02x:%02x:%02x:%02x:%02x:%02x",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
87  snprintf (buffer, 18, MACSTR, MAC2STR (mac));
88  //DEBUG_DBG ("Address: %s", buffer);
89  return buffer;
90  }
91  return NULL;
92 }
93 
94 uint8_t* str2mac (const char* macAddrString, uint8_t* macBytes) {
95  const char cSep = ':';
96 
97  for (int i = 0; i < 6; ++i) {
98  unsigned int iNumber = 0;
99  char ch;
100 
101  //Convert letter into lower case.
102  ch = tolower (*macAddrString++);
103 
104  if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f')) {
105  return NULL;
106  }
107 
108  //Convert into number.
109  // a. If character is digit then ch - '0'
110  // b. else (ch - 'a' + 10) it is done
111  // because addition of 10 takes correct value.
112  iNumber = isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
113  ch = tolower (*macAddrString);
114 
115  if ((i < 5 && ch != cSep) ||
116  (i == 5 && ch != '\0' && !isspace (ch))) {
117  ++macAddrString;
118 
119  if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f')) {
120  return NULL;
121  }
122 
123  iNumber <<= 4;
124  iNumber += isdigit (ch) ? (ch - '0') : (ch - 'a' + 10);
125  ch = *macAddrString;
126 
127  if (i < 5 && ch != cSep) {
128  return NULL;
129  }
130  }
131  /* Store result. */
132  macBytes[i] = (unsigned char)iNumber;
133  /* Skip cSep. */
134  ++macAddrString;
135  }
136  return macBytes;
137 }
138 
139 #ifdef ESP8266
140 const char* IRAM_ATTR extractFileName (const char* path) {
141  size_t i = 0;
142  size_t pos = 0;
143  char* p = (char*)path;
144  while (*p) {
145  i++;
146  if (*p == '/' || *p == '\\') {
147  pos = i;
148  }
149  p++;
150  }
151  return path + pos;
152 }
153 #endif
154 
155 //int str2mac (const char* mac, uint8_t* values) {
156 // int error = std::sscanf (mac, "%02x:%02x:%02x:%02x:%02x:%02x", &values[0], &values[1], &values[2], &values[3], &values[4], &values[5]);
157 // Serial.printf ("Error: %d", error);
158 // if (error == 6) {
159 // for (int i = 0; i < 6; i++) {
160 // Serial.println (values[i]);
161 // }
162 // return 1;
163 // }
164 // else {
165 // return 0;
166 // }
167 //}
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:31
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:94
MAX_STR_LEN
#define MAX_STR_LEN
Key length used by selected crypto algorythm.
Definition: helperFunctions.cpp:15
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:17
mac2str
char * mac2str(const uint8_t *mac, char *buffer)
Debug helper function that generates a string that represent a MAC address.
Definition: helperFunctions.cpp:84
MACSTR
#define MACSTR
Definition: helperFunctions.cpp:82
data
@ data
Definition: GwOutput_generic.h:23
helperFunctions.h
Auxiliary function definition.
calculateCRC32
uint32_t calculateCRC32(const uint8_t *data, size_t length)
Calculates CRC32 of a buffer.
Definition: helperFunctions.cpp:63