callbacks.c 2.34 KB
#include "callbacks.h"

void dhcp_connect_result_cb(int result)
{
	libwismart_ip_addr_t ip;
	if(result==LIBWISMART_DHCP_ADDRESS_ASSIGNED)
	{
		libwismart_GetCurrentIP(&ip,NULL,NULL);
		printf("IP: %d.%d.%d.%d \r\n",ip.addr[3],ip.addr[2],ip.addr[1],ip.addr[0]);
		connected = 1;
        retries = 0;
	}
	else if(result==LIBWISMART_DHCP_TIMEOUT)
	{
		printf("DHCP timeout\r\n");
        timeout = 1;
	}
	else
	{
		printf("DHCP error\r\n");
        timeout = 1;
	}
	
}

void wifi_connect_result_cb(int result)
{
	if(result==WISMART_WIFI_CONN_TIMEOUT)
    {
		timeout=1;
	}
    printf("WiFi Connect indication: ");
    if(result == WISMART_WIFI_CONNECTED)
    {
        printf("Connected\r\n");
        retries = 0;
    }
    else
    {
        printf("Failed\r\n");
        if(++retries == MAX_RETRIES)
        {
            timeout = 1;
        }
    }
}

void softapMode_apStartedCb(int result)
{
    if (result == WISMART_WIFI_CONNECTED)
    {
        printWifiInfo(WIFI_MODE_SOFTAP);
        configServer_connect();
    }
}

void softapMode_clientIndicationCb(wismart_softap_cb_t reason, const uint8_t *mac, const libwismart_ip_addr_t *ip)
{
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"

    switch(reason)
    {
        case WISMART_WIFI_AP_CLIENT_CONNECTED:
            printf("Client: "MACSTR" connected\r\n",MAC2STR(mac));
            break;
        case WISMART_WIFI_AP_CLIENT_DISCONNECTED:
            printf("Client: "MACSTR" disconnected\r\n",MAC2STR(mac));
            break;
        case WISMART_WIFI_AP_CLIENT_EXPIRED:
            printf("Client: "MACSTR" connection have expired\r\n",MAC2STR(mac));
            break;
        case WISMART_WIFI_AP_CLIENT_GET_IP:
            printf("Client: "MACSTR" got ip: %s\r\n", MAC2STR(mac), inet_ntoa(*ip));
            break;
    }
    
#undef MAC2STR
#undef MACSTR
}

void printWifiInfo(uint8_t wifiMode)
{   
    printf("\r\n\r\n");
    printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n");
    printf("| Network Is Ready!\r\n");
    printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n");
    printf("| Mode : %s\r\n", (wifiMode == WIFI_MODE_CLIENT) ? "Client":"Soft Access Point");
    printf("| IP   : 192.168.1.1\r\n");
    printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n");
}