Smartconfig over ESP32 ?

Bunshee
Posts: 3
Joined: Fri Nov 01, 2019 11:30 pm

Smartconfig over ESP32 ?

Postby Bunshee » Wed Apr 22, 2020 6:31 pm

Hello,

I have a question, I have a Controller with a ESP32, and 5 Modules with an ESP32, this Modules should connect to the Controller over WiFi.
The Modules are standard in SmartConfig mode, and the ESP32 Controller should send the WiFi Credentials to the other 5 modules.

Now i found a Solution to send the ESP32-SmartConfig Packets from my Raspberry with this: https://github.com/CaptainDouche/esp_sm ... smartcfg.c and it works....

So, i ported this Code to my ESP32 (Platform.IO) the ESP32 sends the Packets, but it don't works, the clients are not receive the Smartconfig Packets...

Have any one a Idea to send Wifi Credentials to the Modules?

Here is my ESP32 Smartconfig Code:

Code: Select all

#include <Arduino.h>
#include <smartconfig.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include <WiFiUdp.h>
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include <lwip/netdb.h>
#include <sys/times.h>

extern volatile bool run;
sockaddr_in txaddr;
byte packet[1500];

WiFiUDP udp;

SmartConfig::SmartConfig(void)
{

}

uint8_t SmartConfig::_crc8_update(uint8_t crc, uint8_t data)
{
	uint8_t i;

	crc = crc ^ data;
	for (i = 0; i < 8; i++)
	{
		if (crc & 0x01)
			crc = (crc >> 1) ^ 0x8C;
		else
			crc >>= 1;
	}

	return crc;
}

uint8_t SmartConfig::_crc8_update_buf(uint8_t crc, const uint8_t* data, int len)
{
	while (len--)
	{
		crc = _crc8_update(crc, *data++);
	}
	
	return crc;
}

uint8_t SmartConfig::_xor_update_buf(uint8_t xor_, const uint8_t* data, int len)
{
	while (len--)
	{
		xor_ ^= *data++;
	}
	
	return xor_;
}

int SmartConfig::tricode(uint8_t data, uint8_t index, uint16_t* tricode, const char* comment)
{
	uint8_t crc = 0x00;
	crc = _crc8_update(crc, data);
	crc = _crc8_update(crc, index);

	tricode[0] = MKINT16(0x00, MKBYTE(HINIB(crc), HINIB(data)));
	tricode[1] = MKINT16(0x01, index);
	tricode[2] = MKINT16(0x00, MKBYTE(LONIB(crc), LONIB(data)));

	Serial.printf("TriCode: data=%3d (0x%02x) index=%3d (0x%02x) %-10s -> ", data, data, index, index, comment);
	Serial.printf("%04x, %04x, %04x; \n", tricode[0], tricode[1], tricode[2]);

	return 3;
}

int SmartConfig::make_datumcode(uint16_t* datumcode,const uint8_t* essid_str,const char* bssid_str,const uint8_t* passwd_str,const char* ownaddr_str,bool ishidden)
{
	Serial.printf("------------------------------\n");
	Serial.printf("ESSID    = \"%s\"\n", essid_str);
	Serial.printf("BSSID    = %s\n",     bssid_str);
	Serial.printf("Password = \"%s\"\n", passwd_str);
	Serial.printf("Ip-Addr  = %s\n",     ownaddr_str);
	Serial.printf("Hidden   = %s\n",     ishidden ? "yes" : "no");

	uint8_t total_xor;

	uint8_t bssid_bytes[MAC_LEN];
	parse_macstr(bssid_str, bssid_bytes);

	uint8_t ownaddr_bytes[IPADDR_LEN];
	parse_ipstr(ownaddr_str, ownaddr_bytes);

	// essid_str and passwd_str could be encrypted here, using "AES/ECB/PKCS5Padding"
	// nice small AES implementation: https://github.com/kokke/tiny-AES-c/

	uint8_t essid_len	= strlen((const char*)essid_str);
	uint8_t essid_crc	= _crc8_update_buf(0x00, essid_str, essid_len);
	uint8_t bssid_crc	= _crc8_update_buf(0x00, bssid_bytes, MAC_LEN);
	uint8_t passwd_len	= strlen((const char*)passwd_str);

	uint8_t total_len	=
		sizeof(total_len ) +
		sizeof(passwd_len) +
		sizeof(essid_crc) +
		sizeof(bssid_crc ) +
		sizeof(total_xor ) +
		IPADDR_LEN +
		passwd_len +
		essid_len;

	total_xor  = 0x00;
	total_xor ^= total_len;	
	total_xor ^= passwd_len;
	total_xor ^= essid_crc;
	total_xor ^= bssid_crc;
	total_xor = _xor_update_buf(total_xor, ownaddr_bytes, IPADDR_LEN);
	total_xor = _xor_update_buf(total_xor, passwd_str, passwd_len);
	total_xor = _xor_update_buf(total_xor, essid_str, essid_len);	

	Serial.printf("essid_crc  = 0x%02x (%3d)\n", essid_crc,  essid_crc);
	Serial.printf("bssid_crc  = 0x%02x (%3d)\n", bssid_crc,  bssid_crc);
	Serial.printf("essid_len  = 0x%02x (%3d)\n", essid_len,  essid_len);
	Serial.printf("passwd_len = 0x%02x (%3d)\n", passwd_len, passwd_len);
	Serial.printf("total_len  = 0x%02x (%3d)\n", total_len,  total_len);
	Serial.printf("total_xor  = 0x%02x (%3d)\n", total_xor,  total_xor);

	
	memset(datumcode, 0, DATUMCODE_MAX);

	int n = 0; // datacode index
	int t = 0; // tricode index

	n += tricode(total_len,  t++, datumcode + n, "total_len");  // 0
	n += tricode(passwd_len, t++, datumcode + n, "passwd_len"); // 1
	n += tricode(essid_crc,  t++, datumcode + n, "essid_crc"); // 2
	n += tricode(bssid_crc,  t++, datumcode + n, "bssid_crc");  // 3
	n += tricode(total_xor,  t++, datumcode + n, "total_xor");  // 4

	for (int i=0; i<IPADDR_LEN; i++)
	{
		n += tricode(ownaddr_bytes[i], t++, datumcode + n, "ipaddr");
	}
	
	for (int i=0; i<passwd_len; i++)
	{
		n += tricode(passwd_str[i], t++, datumcode + n, "ap_pwd");
	}

	if (ishidden)
	{
		for (int i=0; i<essid_len; i++)
		{
			n += tricode(essid_str[i], t++, datumcode + n, "essid");
		}
	}

	for (int i=0; i<MAC_LEN; i++)
	{
		n += tricode(bssid_bytes[i], t++, datumcode + n, "bssid");
	}

	Serial.printf("dcode_len  = %d\n", n);
	Serial.printf("------------------------------\n");

	return n;
}

bool SmartConfig::parse_ipstr(const char* str, uint8_t* ip)
{
	int n = sscanf(str, "%hhu.%hhu.%hhu.%hhu", ip+0, ip+1, ip+2, ip+3);
	return (n == 4);
}

bool SmartConfig::parse_macstr(const char* str, uint8_t* mac)
{
	int n = sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", mac+0, mac+1, mac+2, mac+3, mac+4, mac+5);
	return (n == 6);
}

int SmartConfig::send_sized(int size)
{
	size_t n = size;
	//int n = sendto(txsock, packet, size, 0, (sockaddr_t*)(&txaddr), sizeof(sockaddr_in_t));
	udp.beginPacket("255.255.255.255",7001);
	udp.write(packet, n);
	udp.endPacket();
}

bool SmartConfig::peek_reply(void)
{	
	static uint8_t buf[PACKET_MAX];
	/*int rxlen = recvfrom(rxsock, buf, sizeof(buf)-1, 0, (struct sockaddr*)(&remoteaddr), &addrlen);

	if (rxlen > 0)
	{
		printf("\nrecvd %d bytes from %s \n", rxlen, inet_ntoa(remoteaddr.sin_addr));

		if (rxlen == (sizeof(char) + MAC_LEN + IPADDR_LEN))
		{
			// expecting here: buf[0] == strlen(essid_str) + strlen(passwd_str)

			const uint8_t* mac = buf + 1;
			const uint8_t* ip  = mac + MAC_LEN;

			printf("- IP:  %hhu.%hhu.%hhu.%hhu \n", ip[0], ip[1], ip[2], ip[3]);
			printf("- MAC: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx \n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

			run = false;
			return true;
		}
	}
*/
	return false;
}

void SmartConfig::send_guidecode(void)
{
	Serial.printf("G");
	unsigned long t_end = timestamp_ms() + GUIDECODE_TOTAL_MS;

	while ((timestamp_ms() < t_end) && run)
	{
		send_sized(515);
		delay_ms(GUIDECODE_DLY_MS);
		send_sized(514);
		delay_ms(GUIDECODE_DLY_MS);
		send_sized(513);
		delay_ms(GUIDECODE_DLY_MS);
		send_sized(512);
		delay_ms(GUIDECODE_DLY_MS);

		peek_reply();
	}
}

void SmartConfig::send_datumcode(const uint16_t* datumcode, int dc_len)
{
	Serial.printf("D");
	unsigned long t_end = timestamp_ms() + DATACODE_TOTAL_MS;
	
	int i=0;
	while ((timestamp_ms() < t_end) && run)
	{
		send_sized(datumcode[i] + 40);
		delay_ms(DATACODE_DLY_MS);

		i++;
		if (i == dc_len)
		{
			i = 0;
			peek_reply();
		}
	}
}

bool SmartConfig::esp_smartcfg_init(void)
{
	return true;
}

void SmartConfig::esp_smartcfg_deinit(void)
{
}

void SmartConfig::esp_smartcfg_run(const uint8_t* essid_str,const char* bssid_str,const uint8_t* passwd_str,const char* ownaddr_str,bool ishidden,int timeout)
{
	memset(packet, '1', sizeof(packet));
	uint16_t datumcode[DATUMCODE_MAX];
	int dc_len = make_datumcode(datumcode, essid_str, bssid_str, passwd_str, ownaddr_str, ishidden);

	unsigned long t_end = timestamp_ms() + (1000 * timeout);

	//eprintf("sending packets: ");

	while ((timeout == 0) || (timestamp_ms() < t_end))
	{
		send_guidecode();
		if (!run) break;
		send_datumcode(datumcode, dc_len);
		if (!run) break;
	}

	esp_smartcfg_deinit();
}

unsigned long SmartConfig::timestamp_ms(void)
{
	return millis();
}

Who is online

Users browsing this forum: No registered users and 47 guests