dhcp attempts

This commit is contained in:
2024-10-13 05:32:44 +06:00
parent ec9dcc76f7
commit 480a4a1ca5
12 changed files with 329 additions and 63 deletions

View File

@@ -2,38 +2,26 @@
#include <DHCP/dhcp.h>
#include <DNS/dns.h>
#include <MQTT/MQTTClient.h>
#include <W5500/w5500.h>
#include <socket.h>
#include <stdio.h>
#include <string.h>
#include "ch32v003fun.h"
// #include "spi.h"
#include "config.h"
#include "debug.h"
#include "spi_dma.h"
// Definitions for socket indexes
#define DHCP_SOCKET 0
#define DNS_SOCKET 1
#define HTTP_SOCKET 2
// Global variables
volatile uint32_t count = 0;
volatile int ip_assigned = 0;
// Global buffers for DHCP and DNS
uint8_t dhcp_buffer[1024];
uint8_t dns_buffer[1024];
void w5500_select(void) {
GPIOA->BCR = (1 << 4); // Set PA4 (CS) low
}
void w5500_unselect(void) {
GPIOA->BSHR = (1 << 4); // Set PA4 (CS) high
}
static uint8_t dhcp_buffer[512];
static uint8_t dns_buffer[512];
// Function to handle IP assignment details
void handle_ip_assigned(void) {
printf("IP Assigned!\n");
DEBUG_PRINT("IP Assigned!\n");
wiz_NetInfo net_info;
getIPfromDHCP(net_info.ip);
@@ -43,7 +31,7 @@ void handle_ip_assigned(void) {
uint8_t dns[4];
getDNSfromDHCP(dns);
printf(
DEBUG_PRINT(
"IP: %d.%d.%d.%d\nGW: %d.%d.%d.%d\nNet: %d.%d.%d.%d\nDNS: "
"%d.%d.%d.%d\n",
net_info.ip[0], net_info.ip[1], net_info.ip[2], net_info.ip[3],
@@ -56,14 +44,15 @@ void handle_ip_assigned(void) {
// Callback functions
void callback_ip_assigned(void) {
printf("Callback: IP assigned! Leased time: %u sec\n", getDHCPLeasetime());
DEBUG_PRINT("Callback: IP assigned! Leased time: %lu sec\n",
getDHCPLeasetime());
ip_assigned = 1;
}
void callback_ip_conflict(void) { printf("Callback: IP conflict!\n"); }
void callback_ip_conflict(void) { DEBUG_PRINT("Callback: IP conflict!\n"); }
void configure_network(void) {
printf("Starting network configuration...\n");
DEBUG_PRINT("Starting network configuration...\n");
// Setup chip select and SPI callbacks
reg_wizchip_cs_cbfunc(spi_select, spi_unselect);
@@ -85,21 +74,24 @@ void configure_network(void) {
// Attempt to acquire an IP address using DHCP
const uint32_t max_attempts = 1e5;
// todo: run in a loop by a timer or sth
for (uint32_t attempt = 0; !ip_assigned && attempt < max_attempts;
++attempt) {
DHCP_run();
}
if (!ip_assigned) {
printf("\r\nIP was not assigned :(\r\n");
DEBUG_PRINT("\r\nIP was not assigned :(\r\n");
return;
}
handle_ip_assigned();
}
// todo: rm
void resolve_domain_name(const char* domain_name) {
printf("Resolving domain name \"%s\"...\n", domain_name);
DEBUG_PRINT("Resolving domain name \"%s\"...\n", domain_name);
DNS_init(DNS_SOCKET, dns_buffer);
// cloudflare dns
@@ -113,11 +105,75 @@ void resolve_domain_name(const char* domain_name) {
res = DNS_run(dns, (uint8_t*)domain_name, addr);
if (res == 1) {
printf("Result: %d.%d.%d.%d\n", addr[0], addr[1], addr[2], addr[3]);
DEBUG_PRINT("Result: %d.%d.%d.%d\n", addr[0], addr[1], addr[2], addr[3]);
break;
} else {
printf("DNS_run() failed, res = %d. Retries: %u\n", res, retries);
DEBUG_PRINT("DNS_run() failed, res = %d. Retries: %u\n", res, retries);
}
retries++;
}
}
MQTTClient setup_mqtt_client(Network* network, ch32_mqtt_options_t* opts) {
static unsigned char tx_buffer[MQTT_TX_BUFFER_SIZE];
static unsigned char rx_buffer[MQTT_RX_BUFFER_SIZE];
MQTTClient client;
int rc;
uint8_t target_ip[] = MQTT_TARGET_IP;
NewNetwork(network, TCP_SOCKET);
if (ConnectNetwork(network, target_ip, MQTT_TARGET_PORT) != SOCK_OK) {
DEBUG_PRINT("Network connection failed.\n");
return (MQTTClient){0}; // Return an empty client on failure
}
// Initialize the MQTT client
MQTTClientInit(&client, network, MQTT_COMMAND_TIMEOUT_MS, tx_buffer,
sizeof(tx_buffer), rx_buffer, sizeof(rx_buffer));
// Setup MQTT connection data
MQTTPacket_connectData connect_data = MQTTPacket_connectData_initializer;
connect_data.willFlag = 0;
connect_data.MQTTVersion = 3;
connect_data.clientID.cstring = opts->clientid;
connect_data.username.cstring = opts->username;
connect_data.password.cstring = opts->password;
connect_data.keepAliveInterval = MQTT_KEEP_ALIVE_INTERVAL;
connect_data.cleansession = 1;
// Connect to MQTT broker
rc = MQTTConnect(&client, &connect_data);
if (rc != 0) {
DEBUG_PRINT("Failed to connect: %d\n", rc);
return (MQTTClient){0}; // Return an empty client on failure
}
return client;
}
int subscribe_to_topic(MQTTClient* client, const char* topic, enum QoS qos,
messageHandler message_callback) {
int rc = MQTTSubscribe(client, topic, qos, message_callback);
if (rc != 0) {
DEBUG_PRINT("Failed to subscribe to %s: %d\n", topic, rc);
return rc;
}
return 0; // Success
}
void publish_message(MQTTClient* client, const char* payload,
const char* topic) {
MQTTMessage message = {.qos = QOS0,
.retained = 0,
.dup = 0,
.payload = (void*)payload,
.payloadlen = strlen(payload)
};
if (MQTTPublish(client, topic, &message) != 0) {
DEBUG_PRINT("Publish failed\n");
} else {
DEBUG_PRINT("Message published successfully\n");
}
}