chore: bloatify

This commit is contained in:
2024-11-09 03:34:43 +06:00
parent 40f1cab745
commit 80cf21f143
10 changed files with 253 additions and 142 deletions

View File

@@ -11,46 +11,10 @@
#include "config.h"
#include "debug.h"
#include "spi_dma.h"
#include "systick.h"
volatile uint32_t count = 0;
volatile int ip_assigned = 0;
// Global buffers for DHCP and DNS
static uint8_t dhcp_buffer[512];
static uint8_t dns_buffer[512];
// Function to handle IP assignment details
void handle_ip_assigned(void) {
DEBUG_PRINT("IP Assigned!\n");
wiz_NetInfo net_info;
getIPfromDHCP(net_info.ip);
getGWfromDHCP(net_info.gw);
getSNfromDHCP(net_info.sn);
uint8_t dns[4];
getDNSfromDHCP(dns);
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],
net_info.gw[0], net_info.gw[1], net_info.gw[2], net_info.gw[3],
net_info.sn[0], net_info.sn[1], net_info.sn[2], net_info.sn[3], dns[0],
dns[1], dns[2], dns[3]);
wizchip_setnetinfo(&net_info);
}
// Callback functions
void callback_ip_assigned(void) {
DEBUG_PRINT("Callback: IP assigned! Leased time: %lu sec\n",
getDHCPLeasetime());
ip_assigned = 1;
}
void callback_ip_conflict(void) { DEBUG_PRINT("Callback: IP conflict!\n"); }
void configure_network(void) {
DEBUG_PRINT("===\n");
DEBUG_PRINT("Starting network configuration...\n");
@@ -65,36 +29,7 @@ void configure_network(void) {
wizchip_init(rx_tx_buff_sizes, rx_tx_buff_sizes);
}
void configure_dhcp(void) {
wiz_NetInfo net_info = {.mac = {0xEA, 0x11, 0x22, 0x33, 0x44, 0xEA},
.dhcp = NETINFO_DHCP};
setSHAR(net_info.mac);
DHCP_init(DHCP_SOCKET, dhcp_buffer);
// Register DHCP callbacks
reg_dhcp_cbfunc(callback_ip_assigned, callback_ip_assigned,
callback_ip_conflict);
// Attempt to acquire an IP address using DHCP
// retry
uint8_t retries = 0;
while (!ip_assigned && retries < 30) {
DHCP_run();
// Delay_Ms(100);
DEBUG_PRINT("DHCP_run()...\n");
retries++;
}
if (!ip_assigned) {
DEBUG_PRINT("\r\nIP was not assigned\r\n");
return;
}
handle_ip_assigned();
}
// todo: rm
// todo: rm !!!
void resolve_domain_name(const char* domain_name) {
DEBUG_PRINT("Resolving domain name \"%s\"...\n", domain_name);
@@ -119,21 +54,23 @@ void resolve_domain_name(const char* domain_name) {
}
}
MQTTClient setup_mqtt_client(Network* network, ch32_mqtt_options_t* opts) {
int setup_mqtt_client(Network* network, ch32_mqtt_options_t* opts,
MQTTClient* client) {
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;
// Initialize network
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
rc = ConnectNetwork(network, target_ip, MQTT_TARGET_PORT);
if (rc != SOCK_OK) {
DEBUG_PRINT("Network connection failed: %d\n", rc);
return -1;
}
// Initialize the MQTT client
MQTTClientInit(&client, network, MQTT_COMMAND_TIMEOUT_MS, tx_buffer,
MQTTClientInit(client, network, MQTT_COMMAND_TIMEOUT_MS, tx_buffer,
sizeof(tx_buffer), rx_buffer, sizeof(rx_buffer));
// Setup MQTT connection data
@@ -147,13 +84,13 @@ MQTTClient setup_mqtt_client(Network* network, ch32_mqtt_options_t* opts) {
connect_data.cleansession = 1;
// Connect to MQTT broker
rc = MQTTConnect(&client, &connect_data);
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 -2;
}
return client;
return 0;
}
int subscribe_to_topic(MQTTClient* client, const char* topic, enum QoS qos,
@@ -163,7 +100,7 @@ int subscribe_to_topic(MQTTClient* client, const char* topic, enum QoS qos,
DEBUG_PRINT("Failed to subscribe to %s: %d\n", topic, rc);
return rc;
}
return 0; // Success
return 0;
}
void publish_message(MQTTClient* client, const char* payload,