Station API
Diff: Ether.h
- Revision:
- 0:d81f611c59ec
- Child:
- 1:a22e390c70b3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ether.h Wed Nov 30 04:35:45 2011 +0000 @@ -0,0 +1,86 @@ +/* +Copyright (c) 2011, Senio Networks, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef ETHER_H +#define ETHER_H + +#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 = fgetValues(fp, "ip-address:%hhu.%hhu.%hhu.%hhu", &b1, &b2, &b3, &b4) ? IpAddr(b1, b2, b3, b4) : IpAddr(); + IpAddr subnet = fgetValues(fp, "subnet-mask:%hhu.%hhu.%hhu.%hhu", &b1, &b2, &b3, &b4) ? IpAddr(b1, b2, b3, b4) : IpAddr(); + IpAddr gateway = fgetValues(fp, "gateway-address:%hhu.%hhu.%hhu.%hhu", &b1, &b2, &b3, &b4) ? IpAddr(b1, b2, b3, b4) : IpAddr(); + IpAddr dns = 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]); + } + if (!ip.isNull()) + 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); + } + } + + operator bool() { + return active; + } + +private: + EthernetNetIf eth; + bool active; + bool verbose; +}; + +#endif \ No newline at end of file