It's MXCHIP WIFI demo.

MXCHIP_WIFI_Driver/MXCHIP/MXCHIP.cpp

Committer:
lizhibo32
Date:
2017-03-21
Revision:
0:cd7c54f07d25

File content as of revision 0:cd7c54f07d25:


#include "MXCHIP.h"


MXCHIP::MXCHIP(PinName tx, PinName rx, bool debug): _serial(tx, rx, 1024), _parser(_serial), _packets(0), _packets_end(&_packets)
{
    _serial.baud(115200);
    _parser.debugOn(debug);
}

bool MXCHIP::startup(int mode)
{
    //only 3 valid modes
    if(mode < 1 || mode > 3) 
    {
        return false;
    }

    bool success = reset()
        && _parser.send("AT+CWMODE=%d", mode)
        && _parser.recv("OK")
        && _parser.send("AT+CIPMUX=1")
        && _parser.recv("OK");

    _parser.oob("+IPD", this, &MXCHIP::_packet_handler);

    return success;
}

bool MXCHIP::reset(void)
{
    for (int i = 0; i < 2; i++) 
    {
        if (_parser.send("AT+RST")
            && _parser.recv("OK\r\nready")) 
        {
            return true;
        }
    }

    return false;
}

bool MXCHIP::dhcp(bool enabled, int mode)
{
    //only 3 valid modes
    if(mode < 0 || mode > 2) 
    {
        return false;
    }

    return _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode)
        && _parser.recv("OK");
}

bool MXCHIP::connect(const char *ap, const char *passPhrase)
{
    return _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase)
        && _parser.recv("OK");
}

bool MXCHIP::disconnect(void)
{
    return _parser.send("AT+CWQAP") && _parser.recv("OK");
}

const char *MXCHIP::getIPAddress(void)
{
    if (!(_parser.send("AT+CIFSR")
        && _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer)
        && _parser.recv("OK"))) {
        return 0;
    }

    return _ip_buffer;
}

const char *MXCHIP::getMACAddress(void)
{
    if (!(_parser.send("AT+CIFSR")
        && _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer)
        && _parser.recv("OK"))) {
        return 0;
    }

    return _mac_buffer;
}

const char *MXCHIP::getGateway()
{
    if (!(_parser.send("AT+CIPSTA?")
        && _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer)
        && _parser.recv("OK"))) {
        return 0;
    }

    return _gateway_buffer;
}

const char *MXCHIP::getNetmask()
{
    if (!(_parser.send("AT+CIPSTA?")
        && _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer)
        && _parser.recv("OK"))) 
    {
        return 0;
    }

    return _netmask_buffer;
}