Beispiel #1
0
static int tl_parse_line(char *line, struct mac_addr *mac)
{
	int ret;
	char *p;
	char *tok;

	p = strpbrk(line, "0123456789abcdef");
	if (!p) {
		fprintf(stderr, "Error: Can't find hex in: %s\n", line);
		return -EINVAL;
	}

	tok = strtok(p, " ");
	if (!tok) {
		fprintf(stderr, "Error: Can't find end of string': %s\n", line);
		return -EINVAL;
	}

	ret = mac_aton(p, mac);
	if (!ret) {
		fprintf(stderr, "Error: mac_aton failed on: %s\n", p);
		return -EINVAL;
	}

	return 0;
}
Beispiel #2
0
Config::Config(){
    // Simplest possible defaults
    use_dhcp = 1;
    mac_aton("de:ad:be:ef:55:44", mac);
    ntp = IPAddress(195, 113, 56, 8);
    mail_from = NULL;
    sys_name = (char *) malloc(10);
    strlcpy(sys_name, "growduino", 10);
    smtp_port = 25;
    time_zone = 2;
    ups_trigger_level = 255;
#ifdef USE_CO2_SENSOR
    co2_400 = CO2_400;
    co2_40k = CO2_40k;
#endif
#ifdef USE_PH_SENSOR
    ph_4 = PH_4;
    ph_7 = PH_7;
#endif
#ifdef USE_EC_SENSOR
    ec_low_ion = EC_LOW_ION;
    ec_high_ion = EC_HIGH_ION;
#endif
}
Beispiel #3
0
int call_getopt(int argc, char **argv){
	int c=0;
	int i=0;

	int option_index = 0;
	static struct option long_options[] =
	{
		{"serverip",       required_argument,0,'s'},
		{"requestedip",    required_argument,0,'r'},
		{"timeout",        required_argument,0,'t'},
		{"interface",      required_argument,0,'i'},
		{"mac",            required_argument,0,'m'},
		{"unicast",        no_argument,      0,'u'},
		{"verbose",        no_argument,      0,'v'},
		{"version",        no_argument,      0,'V'},
		{"help",           no_argument,      0,'h'},
		{0,0,0,0}
	};

	while(1){
		c=getopt_long(argc,argv,"+hVvt:s:r:t:i:m:u",long_options,&option_index);

		i++;

		if(c==-1||c==EOF||c==1)
			break;

		switch(c){
		case 'w':
		case 'r':
		case 't':
		case 'i':
			i++;
			break;
		default:
			break;
		        }

		switch(c){

		case 's': /* DHCP server address */
			resolve_host(optarg,&dhcp_ip);
			add_requested_server(dhcp_ip);
			break;

		case 'r': /* address we are requested from DHCP servers */
			resolve_host(optarg,&requested_address);
			request_specific_address=TRUE;
			break;

		case 't': /* timeout */

			/*
			if(is_intnonneg(optarg))
			*/
			if(atoi(optarg)>0)
				dhcpoffer_timeout=atoi(optarg);
			/*
			else
				usage("Time interval must be a nonnegative integer\n");
			*/
			break;

		case 'm': /* MAC address */

			if((user_specified_mac=mac_aton(optarg)) == NULL)
				usage("Cannot parse MAC address.\n");
			if(verbose)
				print_hardware_address(user_specified_mac);

			break;

		case 'i': /* interface name */

			strncpy(network_interface_name,optarg,sizeof(network_interface_name)-1);
			network_interface_name[sizeof(network_interface_name)-1]='\x0';

			break;

		case 'u': /* unicast testing */
			unicast=1;
			break;

		case 'V': /* version */
			print_revision(progname, NP_VERSION);
			exit(STATE_OK);

		case 'h': /* help */
			print_help();
			exit(STATE_OK);

		case 'v': /* verbose */
			verbose=1;
			break;

		case '?': /* help */
			usage5 ();
			break;

		default:
			break;
		        }
	        }

	return i;
        }
Beispiel #4
0
void Config::load(aJsonObject * json){
    //loads values from json. Values not in json are not touched
    byte tmpmac[6];
    char debug_out[32];
    IPAddress tmpip;
    int json_strlen;

    aJsonObject* cnfobj = aJson.getObjectItem(json, "use_dhcp");
    if (cnfobj) {
        if (strcmp(cnfobj->valuestring, "0") == 0 || cnfobj->valueint == 0) {
            use_dhcp = 0;
        } else {
            use_dhcp = 1;
        }
    }

    cnfobj = aJson.getObjectItem(json, "mac");
    if (cnfobj && cnfobj->type == aJson_String && mac_aton(cnfobj->valuestring, tmpmac) == 1) {
        mac_aton(cnfobj->valuestring, mac);
        Serial.print(F("mac "));
        mac_ntoa(mac, debug_out);
        Serial.println(debug_out);

    }

    if (use_dhcp == 0) {  // Static IP config
        cnfobj = aJson.getObjectItem(json, "ip");
        if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) {
            inet_aton(cnfobj->valuestring, ip);
            Serial.print(F("ip "));
            inet_ntoa(ip, debug_out);
            Serial.println(debug_out);
        }

        cnfobj = aJson.getObjectItem(json, "netmask");
        if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) {
            inet_aton(cnfobj->valuestring, netmask);
            Serial.print(F("netmask "));
            inet_ntoa(netmask, debug_out);
            Serial.println(debug_out);
        }

        cnfobj = aJson.getObjectItem(json, "gateway");
        if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) {
            inet_aton(cnfobj->valuestring, gateway);
            Serial.print(F("gateway "));
            inet_ntoa(gateway, debug_out);
            Serial.println(debug_out);
        }
    }

    cnfobj = aJson.getObjectItem(json, "smtp");
    if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) {
        inet_aton(cnfobj->valuestring, smtp);
        Serial.print(F("smtp "));
        inet_ntoa(smtp, debug_out);
        Serial.println(debug_out);
    }

    cnfobj = aJson.getObjectItem(json, "smtp_port");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &smtp_port);
    } else {
        smtp_port = 25;
    }

    cnfobj = aJson.getObjectItem(json, "mail_from");
    if (cnfobj->type == aJson_String)  {
        if (mail_from != NULL) {
            free(mail_from);
            mail_from = NULL;
        }
        json_strlen = strnlen(cnfobj->valuestring, 31);
        mail_from = (char *) malloc(json_strlen + 1);
        if (mail_from == NULL) {
            Serial.println(F("OOM on config load (mail_from)"));
        } else {
            strlcpy(mail_from, cnfobj->valuestring, json_strlen + 1);
        }


    }

    cnfobj = aJson.getObjectItem(json, "ntp");
    if (cnfobj && cnfobj->type == aJson_String && inet_aton(cnfobj->valuestring, tmpip) == 1) {
        inet_aton(cnfobj->valuestring, ntp);
        Serial.print(F("ntp "));
        inet_ntoa(ntp, debug_out);
        Serial.println(debug_out);
    }

    cnfobj = aJson.getObjectItem(json, "sys_name");
    if (cnfobj->type == aJson_String)  {
        if (sys_name != NULL) {
            free(sys_name);
            sys_name = NULL;
        }
        json_strlen = strnlen(cnfobj->valuestring, 31);
        sys_name = (char *) malloc(json_strlen + 1);
        if (sys_name == NULL) {
            Serial.println(F("OOM on config load (sys_name)"));
        } else {
            strlcpy(sys_name, cnfobj->valuestring, json_strlen + 1);
        }
    }
    cnfobj = aJson.getObjectItem(json, "time_zone");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &time_zone);
    } else {
        time_zone = 2;
    }
    cnfobj = aJson.getObjectItem(json, "ups_trigger_level");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &ups_trigger_level);
    } else {
        ups_trigger_level = 255;
    }

#ifdef USE_CO2_SENSOR
    cnfobj = aJson.getObjectItem(json, "co2_400");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &co2_400);
    } else {
        co2_400 = CO2_400;
    }

    cnfobj = aJson.getObjectItem(json, "co2_40k");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &co2_40k);
    } else {
        co2_40k = CO2_40k;
    }
#endif

#ifdef USE_PH_SENSOR
    cnfobj = aJson.getObjectItem(json, "ph_4");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &ph_4);
    } else {
        ph_4 = PH_4;
    }

    cnfobj = aJson.getObjectItem(json, "ph_7");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &ph_7);
    } else {
        ph_7 = PH_7;
    }
#endif

#ifdef USE_EC_SENSOR
    cnfobj = aJson.getObjectItem(json, "ec_low_ion");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &ec_low_ion);
    } else {
        ec_low_ion = EC_LOW_ION;
    }

    cnfobj = aJson.getObjectItem(json, "ec_high_ion");
    if (cnfobj) {
        sscanf(cnfobj->valuestring, "%d", &ec_high_ion);
    } else {
        ec_high_ion = EC_HIGH_ION;
    }
#endif
}