Пример #1
0
int write_config_file(struct config_tree *cft, const char *file,
		      int argc, char **argv)
{
	const struct config_node *cn;
	int r = 1;
	struct output_line outline;
	outline.fp = NULL;
	outline.putline = NULL;

	if (!file)
		file = "stdout";
	else if (!(outline.fp = fopen(file, "w"))) {
		log_sys_error("open", file);
		return 0;
	}

	if (!(outline.mem = dm_pool_create("config_line", 1024))) {
		r = 0;
		goto_out;
	}

	log_verbose("Dumping configuration to %s", file);
	if (!argc) {
		if (!_write_config(cft->root, 0, &outline, 0)) {
			log_error("Failure while writing to %s", file);
			r = 0;
		}
	} else while (argc--) {
		if ((cn = find_config_node(cft->root, *argv))) {
			if (!_write_config(cn, 1, &outline, 0)) {
				log_error("Failure while writing to %s", file);
				r = 0;
			}
		} else {
			log_error("Configuration node %s not found", *argv);
			r = 0;
		}
		argv++;
	}

	dm_pool_destroy(outline.mem);

out:
	if (outline.fp && lvm_fclose(outline.fp, file)) {
		stack;
		r = 0;
	}

	return r;
}
Пример #2
0
static int _write_config(const struct dm_config_node *n, int only_one,
			 struct config_output *out, int level)
{
	char space[MAX_INDENT + 1];
	int l = (level < MAX_INDENT) ? level : MAX_INDENT;
	int i;

	if (!n)
		return 1;

	for (i = 0; i < l; i++)
		space[i] = '\t';
	space[i] = '\0';

	do {
		if (out->spec && out->spec->prefix_fn)
			out->spec->prefix_fn(n, space, out->baton);

		if (!_line_start(out))
			return_0;
		line_append("%s%s", space, n->key);
		if (!n->v) {
			/* it's a sub section */
			line_append(" {");
			if (!_line_end(n, out))
				return_0;
			_write_config(n->child, 0, out, level + 1);
			if (!_line_start(out))
				return_0;
			line_append("%s}", space);
		} else {
			/* it's a value */
			const struct dm_config_value *v = n->v;
			line_append("=");
			if (v->next) {
				line_append("[");
				while (v && v->type != DM_CFG_EMPTY_ARRAY) {
					if (!_write_value(out, v))
						return_0;
					v = v->next;
					if (v && v->type != DM_CFG_EMPTY_ARRAY)
						line_append(", ");
				}
				line_append("]");
			} else
				if (!_write_value(out, v))
					return_0;
		}
		if (!_line_end(n, out))
			return_0;

		if (out->spec && out->spec->suffix_fn)
			out->spec->suffix_fn(n, space, out->baton);

		n = n->sib;
	} while (n && !only_one);
	/* FIXME: add error checking */
	return 1;
}
void ESPSerialWiFiManager::_save_config(String ssid, String pass, bool enc,
                 bool advanced,
                 IPAddress ip, IPAddress gateway, IPAddress subnet,
                 IPAddress dns1, IPAddress dns2){
    _set_config(ssid, pass, enc, advanced, ip, gateway, subnet, dns1, dns2);
    _write_config();
    _read_config();
    OFL("Choose Commit Config for changes to persist reboot.\n");
}
void ESPSerialWiFiManager::_read_config(){
    if(EEPROM.read(_eeprom_offset) != CONFIGCHECK)
    {
        _reset_config();
        _write_config();
        EEPROM.write(_eeprom_offset, CONFIGCHECK);
        EEPROM.commit();
    }

    ERA(_eeprom_offset + 1, _network_config);
}
Пример #5
0
int write_config_node(const struct config_node *cn, putline_fn putline, void *baton)
{
	struct output_line outline;
	outline.fp = NULL;
	if (!(outline.mem = dm_pool_create("config_line", 1024)))
		return_0;
	outline.putline = putline;
	outline.putline_baton = baton;
	if (!_write_config(cn, 0, &outline, 0)) {
		dm_pool_destroy(outline.mem);
		return_0;
	}
	dm_pool_destroy(outline.mem);
	return 1;
}
void ESPSerialWiFiManager::run_menu(int timeout){
    bool first_run = true;
    static const uint8_t _main_menu_size = 7;
    static String _main_menu[_main_menu_size] = {
        F("Scan"),
        F("Enter SSID"),
        F("WPS Connect"),
        F("Disconnect"),
        F("Commit Config"),
        F("Display Network Details"),
        F("Quit")
        //"Disconnect"
    };

    static int i;
    NL();
    OFL("ESP Serial WiFi Manager");
    OFL("=======================");

    while(true){
        NL();
        OFL("================");
        OFL("MAIN MENU");
        OFL("================");
        i = _print_menu(_main_menu, _main_menu_size, first_run ? timeout : 0);
        if(i == -1) return; //timeout ocurred, exit
        first_run = false;
        switch(i){
            case 1:
                _scan_for_networks();
                break;
            case 2:
                if(_connect_manual())
                    _disp_network_details();
                break;
            case 3: //WPS Connect
                _connect_wps();
                break;
            case 4:
                if(WiFi.status() == WL_CONNECTED){
                    _disconnect();
                    OFL("Complete");
                    _reset_config();
                    _write_config();
                    OFL("Choose Commit Config to prevent reconnect on reboot.");
                }
                else{
                    OFL("Not currently connected...");
                }
                break;
            case 5:
                _commit_config();
                break;
            case 6:
                _disp_network_details();
                break;
            case 7:
                if(_dirty_config){
                    OFL("Your current config is unsaved.");
                    String opt = _prompt("Save Before Quit? y/n");
                    if(CHAROPT(opt[0], 'y')) _commit_config();
                }
                return;
        }
        delay(1);
    }
}
Пример #7
0
static int _write_node(const struct dm_config_node *cn, int only_one,
		       dm_putline_fn putline,
		       const struct dm_config_node_out_spec *out_spec,
		       void *baton)
{
	struct config_output out = {
		.mem = dm_pool_create("config_output", 1024),
		.putline = putline,
		.spec = out_spec,
		.baton = baton
	};

	if (!out.mem)
		return_0;

	if (!_write_config(cn, only_one, &out, 0)) {
		dm_pool_destroy(out.mem);
		return_0;
	}
	dm_pool_destroy(out.mem);
	return 1;
}

int dm_config_write_one_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton)
{
	return _write_node(cn, 1, putline, NULL, baton);
}

int dm_config_write_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton)
{
	return _write_node(cn, 0, putline, NULL, baton);
}

int dm_config_write_one_node_out(const struct dm_config_node *cn,
				 const struct dm_config_node_out_spec *out_spec,
				 void *baton)
{
	return _write_node(cn, 1, NULL, out_spec, baton);
}

int dm_config_write_node_out(const struct dm_config_node *cn,
			     const struct dm_config_node_out_spec *out_spec,
			     void *baton)
{
	return _write_node(cn, 0, NULL, out_spec, baton);
}

/*
 * parser
 */
static char *_dup_string_tok(struct parser *p)
{
	char *str;

	p->tb++, p->te--;	/* strip "'s */

	if (p->te < p->tb) {
		log_error("Parse error at byte %" PRIptrdiff_t " (line %d): "
			  "expected a string token.",
			  p->tb - p->fb + 1, p->line);
		return NULL;
	}

	if (!(str = _dup_tok(p)))
		return_NULL;

	p->te++;

	return str;
}