Station API

Dependents:   GMCStation

Ether.h

Committer:
yamaguch
Date:
2011-12-12
Revision:
1:a22e390c70b3
Parent:
0:d81f611c59ec
Child:
2:a9d1a9c92927

File content as of revision 1:a22e390c70b3:

#include "EthernetNetIf.h"
#include "Utils.h"

class Ether {
public:
    Ether(bool verbose = false)
            : active(false), verbose(verbose) {
    }

    Ether(IpAddr ip, IpAddr subnet, IpAddr gateway, IpAddr dns, bool verbose = false)
            : eth(ip, subnet, gateway, dns), active(false), verbose(verbose) {
    }

    static Ether create(IpAddr ip, IpAddr subnet, IpAddr gateway, IpAddr dns, bool verbose = false) {
        return Ether(ip, subnet, gateway, dns, verbose);
    }

    static Ether create(char *filename = 0, bool verbose = false) {
        if (filename) {
            char path[32];
            LocalFileSystem local("local");
            sprintf(path, "/local/%s", filename);
            if (FILE *fp = fopen(path, "r")) {
                char b1, b2, b3, b4;
                IpAddr ip = Utils::fgetValues(fp, "ip-address:%hhu.%hhu.%hhu.%hhu", &b1, &b2, &b3, &b4) ? IpAddr(b1, b2, b3, b4) : IpAddr();
                IpAddr subnet = Utils::fgetValues(fp, "subnet-mask:%hhu.%hhu.%hhu.%hhu", &b1, &b2, &b3, &b4) ? IpAddr(b1, b2, b3, b4) : IpAddr();
                IpAddr gateway = Utils::fgetValues(fp, "gateway-address:%hhu.%hhu.%hhu.%hhu", &b1, &b2, &b3, &b4) ? IpAddr(b1, b2, b3, b4) : IpAddr();
                IpAddr dns = Utils::fgetValues(fp, "dns-address:%hhu.%hhu.%hhu.%hhu", &b1, &b2, &b3, &b4) ? IpAddr(b1, b2, b3, b4) : IpAddr();
                fclose(fp);
                if (verbose) {
                    ::printf( "ip-address:%hhu.%hhu.%hhu.%hhu\n", ip[0], ip[1], ip[2], ip[3]);
                    ::printf( "subnet-mask:%hhu.%hhu.%hhu.%hhu\n", subnet[0], subnet[1], subnet[2], subnet[3]);
                    ::printf( "gateway-address:%hhu.%hhu.%hhu.%hhu\n", gateway[0], gateway[1], gateway[2], gateway[3]);
                    ::printf( "dns-address:%hhu.%hhu.%hhu.%hhu\n", dns[0], dns[1], dns[2], dns[3]);
                }
                return Ether(ip, subnet, gateway, dns, verbose);
            }
        }
        return Ether();
    }

    void setup() {
        EthernetErr err = eth.setup();
        active = err == ETH_OK;
        if (verbose) {
            ::printf("EthernetNetIf.setup() = %d\n", err);
        }
    }

    bool isActive() {
        return active;
    }

private:
    EthernetNetIf eth;
    bool active;
    bool verbose;
};