mbed Weather Platform プログラミングの勘所

mbed Weather Platform プログラミングの勘所

設定ファイル

設定ファイルを読み込んで、動的にIPアドレス等を設定する。

使用ライブラリ:/users/shintamainjp/notebook/configfile_ja/

#include "mbed.h"
#include "EthernetNetIf.h"
#include "ConfigFile.h"

EthernetNetIf *eth;
IpAddr ipaddr, netmask, gateway, nameserver;
  :
  :
int config (char *file) {
    ConfigFile cfg;
    char buf[80];
    int ip0, ip1, ip2, ip3;

    if (! cfg.read(file)) {
        return -1;
    }

    if (cfg.getValue("IPADDRESS", buf, sizeof(buf))) {
        if (strcmp(buf, "DHCP") == 0) {
            ipaddr = IpAddr(255, 255, 255, 255);
        } else {
            sscanf(buf, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
            ipaddr = IpAddr(ip0, ip1, ip2, ip3);
        }
    }
    if (cfg.getValue("NETMASK", buf, sizeof(buf))) {
        sscanf(buf, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
        netmask = IpAddr(ip0, ip1, ip2, ip3);
    }
    if (cfg.getValue("GATEWAY", buf, sizeof(buf))) {
        sscanf(buf, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
        gateway = IpAddr(ip0, ip1, ip2, ip3);
    }
    if (cfg.getValue("NAMESERVER", buf, sizeof(buf))) {
        sscanf(buf, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
        nameserver = IpAddr(ip0, ip1, ip2, ip3);
    }

    if (ipaddr[0] == 255) {
        // dhcp ip address
        eth = new EthernetNetIf;
    } else {
        // static ip address
        eth = new EthernetNetIf(ipaddr, netmask, gateway, nameserver);
    }

    ethErr = eth->setup();
    if (ethErr) {
        // error
        return -1;
    }
    return 0;
}


Please log in to post comments.