- Added setBaud() function - Added CheckNetworkStatus() function - Improved messaging system

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WiflyInterface by Components

WiflyInterface.cpp

Committer:
defrost
Date:
2016-03-31
Revision:
25:36b2d76ca8d9
Parent:
14:5a9561156acc
Child:
26:eaaedb036df1

File content as of revision 25:36b2d76ca8d9:

#include "WiflyInterface.h"

#define DEBUG
//Debug is disabled by default
#ifdef DEBUG
#define DBG(x, ...) pc.printf("[WiflyInterface : DBG] "x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) pc.printf("[WiflyInterface : WARN] "x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) pc.printf("[WiflyInterface : ERR] "x"\r\n", ##__VA_ARGS__);
#else
#define DBG(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#endif

#ifdef DEBUG
#define INFO(x, ...) pc.printf("[WiflyInterface : INFO] "x"\r\n", ##__VA_ARGS__);
#else
#define INFO(x, ...)
#endif

WiflyInterface::WiflyInterface( PinName tx, PinName rx, PinName reset, PinName tcp_status,
                                const char * ssid, const char * phrase, Security sec) :
    Wifly(tx, rx, reset, tcp_status, ssid, phrase, sec)
{
    ip_set = false;
}

int WiflyInterface::init()
{
    state.dhcp = true;
    reset();
    INFO("WiflyInterface Initialized.");
    return 0;
}

int WiflyInterface::init(const char* ip, const char* mask, const char* gateway)
{
    state.dhcp = false;
    this->ip = ip;
    strcpy(ip_string, ip);
    ip_set = true;
    this->netmask = mask;
    this->gateway = gateway;
    reset();

    return 0;
}

int WiflyInterface::connect()
{
    // join() returns a boolean, it does not like it all the time, thus casting it as int
    int ii = (int) join();
    INFO("join() complete, return value: %d",ii);
    return ii;
}

int WiflyInterface::disconnect()
{
    return Wifly::disconnect();
}

char * WiflyInterface::getIPAddress()
{
    char * match = 0;
    if (!ip_set) {
        if (!sendCommand("get ip a\r", NULL, ip_string))
            return NULL;
        exit();
        flush();
        match = strstr(ip_string, "<");
        if (match != NULL) {
            *match = '\0';
        }
        if (strlen(ip_string) < 6) {
            match = strstr(ip_string, ">");
            if (match != NULL) {
                int len = strlen(match + 1);
                memcpy(ip_string, match + 1, len);
            }
        }
        ip_set = true;
    }
    return ip_string;
}