Example #1
0
File: conf.c Project: obedmr/MPIaaS
int main(int ac, char **av)
{
	int i = 1;
	const char *name;
	struct stat tmpstat;

	if (ac > i && av[i][0] == '-') {
		switch (av[i++][1]) {
		case 'o':
			input_mode = ask_new;
			break;
		case 's':
			input_mode = ask_silent;
			valid_stdin = isatty(0); //bbox: && isatty(1) && isatty(2);
			break;
		case 'd':
			input_mode = set_default;
			break;
		case 'D':
			input_mode = set_default;
			defconfig_file = av[i++];
			if (!defconfig_file) {
				printf(_("%s: No default config file specified\n"),
					av[0]);
				exit(1);
			}
			break;
		case 'n':
			input_mode = set_no;
			break;
		case 'm':
			input_mode = set_mod;
			break;
		case 'y':
			input_mode = set_yes;
			break;
		case 'r':
			input_mode = set_random;
			srandom(time(NULL));
			break;
		case 'h':
		case '?':
			fprintf(stderr, "See README for usage info\n");
			exit(0);
		}
	}
	name = av[i];
	if (!name) {
		printf(_("%s: Kconfig file missing\n"), av[0]);
	}
	conf_parse(name);
	//zconfdump(stdout);
	switch (input_mode) {
	case set_default:
		if (!defconfig_file)
			defconfig_file = conf_get_default_confname();
		if (conf_read(defconfig_file)) {
			printf("***\n"
				"*** Can't find default configuration \"%s\"!\n"
				"***\n", defconfig_file);
			exit(1);
		}
		break;
	case ask_silent:
		if (stat(".config", &tmpstat)) {
			printf(_("***\n"
				"*** You have not yet configured !\n"
				"***\n"
				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
				"*** \"make menuconfig\" or \"make defconfig\").\n"
				"***\n"));
			exit(1);
		}
	case ask_all:
	case ask_new:
		conf_read(NULL);
		break;
	case set_no:
	case set_mod:
	case set_yes:
	case set_random:
		name = getenv("KCONFIG_ALLCONFIG");
		if (name && !stat(name, &tmpstat)) {
			conf_read_simple(name);
			break;
		}
		switch (input_mode) {
		case set_no:	 name = "allno.config"; break;
		case set_mod:	 name = "allmod.config"; break;
		case set_yes:	 name = "allyes.config"; break;
		case set_random: name = "allrandom.config"; break;
		default: break;
		}
		if (!stat(name, &tmpstat))
			conf_read_simple(name);
		else if (!stat("all.config", &tmpstat))
			conf_read_simple("all.config");
		break;
	default:
		break;
	}

	if (input_mode != ask_silent) {
		rootEntry = &rootmenu;
		conf(&rootmenu);
		if (input_mode == ask_all) {
			input_mode = ask_silent;
			valid_stdin = 1;
		}
	}
	do {
		conf_cnt = 0;
		check_conf(&rootmenu);
	} while (conf_cnt);
	if (conf_write(NULL)) {
		fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
		return 1;
	}
	return 0;
}
static void conf(struct menu *menu)
{
	struct symbol *sym;
	struct property *prop;
	struct menu *child;

	if (!menu_is_visible(menu))
		return;

	sym = menu->sym;
	prop = menu->prompt;
	if (prop) {
		const char *prompt;

		switch (prop->type) {
		case P_MENU:
			if (input_mode == ask_silent && rootEntry != menu) {
				check_conf(menu);
				return;
			}
		case P_COMMENT:
			prompt = menu_get_prompt(menu);
			if (prompt)
				printf("%*c\n%*c %s\n%*c\n",
					indent, '*',
					indent, '*', _(prompt),
					indent, '*');
		default:
			;
		}
	}

	if (!sym)
		goto conf_childs;

	if (sym_is_choice(sym)) {
		conf_choice(menu);
		if (sym->curr.tri != mod)
			return;
		goto conf_childs;
	}

	switch (sym->type) {
	case S_INT:
	case S_HEX:
	case S_STRING:
		conf_string(menu);
		break;
	default:
		conf_sym(menu);
		break;
	}

conf_childs:
	if (sym)
		indent += 2;
	for (child = menu->list; child; child = child->next)
		conf(child);
	if (sym)
		indent -= 2;
}
Example #3
0
int main(int ac, char **av)
{
    int opt;

    const char *name;

    struct stat tmpstat;

    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);

    while ((opt = getopt(ac, av, "osdD:nmyrh")) != -1) {
        switch (opt) {
        case 'o':
            input_mode = ask_silent;
            break;
        case 's':
            input_mode = ask_silent;
            sync_kconfig = 1;
            break;
        case 'd':
            input_mode = set_default;
            break;
        case 'D':
            input_mode = set_default;
            defconfig_file = optarg;
            break;
        case 'n':
            input_mode = set_no;
            break;
        case 'm':
            input_mode = set_mod;
            break;
        case 'y':
            input_mode = set_yes;
            break;
        case 'r':
            {
                struct timeval now;

                unsigned int seed;

                /*
                 * Use microseconds derived seed,
                 * compensate for systems where it may be zero
                 */
                gettimeofday(&now, NULL);

                seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
                srand(seed);

                input_mode = set_random;
                break;
            }
        case 'h':
            printf(_("See README for usage info\n"));
            exit(0);
            break;
        default:
            fprintf(stderr, _("See README for usage info\n"));
            exit(1);
        }
    }
    if (ac == optind) {
        printf(_("%s: Kconfig file missing\n"), av[0]);
        exit(1);
    }
    name = av[optind];
    conf_parse(name);
    if (sync_kconfig) {
        name = conf_get_configname();
        if (stat(name, &tmpstat)) {
            fprintf(stderr, _("***\n"
                              "*** You have not yet configured at91bootstrap!\n"
                              "*** (missing .config file \"%s\")\n"
                              "***\n"
                              "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
                              "*** \"make menuconfig\" or \"make xconfig\").\n"
                              "***\n"), name);
            exit(1);
        }
    }

    switch (input_mode) {
    case set_default:
        if (!defconfig_file)
            defconfig_file = conf_get_default_confname();
        if (conf_read(defconfig_file)) {
            printf(_("***\n"
                     "*** Can't find default configuration \"%s\"!\n"
                     "***\n"), defconfig_file);
            exit(1);
        }
        break;
    case ask_silent:
    case ask_all:
    case ask_new:
        conf_read(NULL);
        break;
    case set_no:
    case set_mod:
    case set_yes:
    case set_random:
        name = getenv("KCONFIG_ALLCONFIG");
        if (name && !stat(name, &tmpstat)) {
            conf_read_simple(name, S_DEF_USER);
            break;
        }
        switch (input_mode) {
        case set_no:
            name = "allno.config";
            break;
        case set_mod:
            name = "allmod.config";
            break;
        case set_yes:
            name = "allyes.config";
            break;
        case set_random:
            name = "allrandom.config";
            break;
        default:
            break;
        }
        if (!stat(name, &tmpstat))
            conf_read_simple(name, S_DEF_USER);
        else if (!stat("all.config", &tmpstat))
            conf_read_simple("all.config", S_DEF_USER);
        break;
    default:
        break;
    }

    if (sync_kconfig) {
        if (conf_get_changed()) {
            name = getenv("KCONFIG_NOSILENTUPDATE");
            if (name && *name) {
                fprintf(stderr,
                        _
                        ("\n*** at91bootstrap configuration requires explicit update.\n\n"));
                return 1;
            }
        }
        valid_stdin = isatty(0) && isatty(1) && isatty(2);
    }

    switch (input_mode) {
    case set_no:
        conf_set_all_new_symbols(def_no);
        break;
    case set_yes:
        conf_set_all_new_symbols(def_yes);
        break;
    case set_mod:
        conf_set_all_new_symbols(def_mod);
        break;
    case set_random:
        conf_set_all_new_symbols(def_random);
        break;
    case set_default:
        conf_set_all_new_symbols(def_default);
        break;
    case ask_new:
    case ask_all:
        rootEntry = &rootmenu;
        conf(&rootmenu);
        input_mode = ask_silent;
        /*
         * fall through 
         */
    case ask_silent:
        /*
         * Update until a loop caused no more changes 
         */
        do {
            conf_cnt = 0;
            check_conf(&rootmenu);
        } while (conf_cnt);
        break;
    }

    if (sync_kconfig) {
        /*
         * silentoldconfig is used during the build so we shall update autoconf.
         * * All other commands are only used to generate a config.
         */
        if (conf_get_changed() && conf_write(NULL)) {
            fprintf(stderr,
                    _
                    ("\n*** Error during writing of the at91bootstrap configuration.\n\n"));
            exit(1);
        }
        if (conf_write_autoconf()) {
            fprintf(stderr,
                    _
                    ("\n*** Error during update of the at91bootstrap configuration.\n\n"));
            return 1;
        }
    } else {
        if (conf_write(NULL)) {
            fprintf(stderr,
                    _
                    ("\n*** Error during writing of the at91bootstrap configuration.\n\n"));
            exit(1);
        }
        if (conf_write_autoconf()) {
            fprintf(stderr,
                    _
                    ("\n*** Error during update of the at91bootstrap configuration.\n\n"));
            return 1;
        }
    }
    return 0;
}
int main(int ac, char **av)
{
	const char *progname = av[0];
	int opt;
	const char *name, *defconfig_file = NULL /* gcc uninit */;
	struct stat tmpstat;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
		input_mode = (enum input_mode)opt;
		switch (opt) {
		case silentoldconfig:
			sync_kconfig = 1;
			break;
		case defconfig:
		case savedefconfig:
			defconfig_file = optarg;
			break;
		case randconfig:
		{
			struct timeval now;
			unsigned int seed;

			/*
			 * Use microseconds derived seed,
			 * compensate for systems where it may be zero
			 */
			gettimeofday(&now, NULL);

			seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
			srand(seed);
			break;
		}
		case oldaskconfig:
		case oldconfig:
		case allnoconfig:
		case allyesconfig:
		case allmodconfig:
		case alldefconfig:
		case listnewconfig:
		case oldnoconfig:
			break;
		case '?':
			conf_usage(progname);
			exit(1);
			break;
		}
	}
	if (ac == optind) {
		printf(_("%s: Kconfig file missing\n"), av[0]);
		conf_usage(progname);
		exit(1);
	}
	name = av[optind];
	conf_parse(name);
	//zconfdump(stdout);
	if (sync_kconfig) {
		name = conf_get_configname();
		if (stat(name, &tmpstat)) {
			fprintf(stderr, _("***\n"
				"*** Configuration file \"%s\" not found!\n"
				"***\n"
				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
				"*** \"make menuconfig\" or \"make xconfig\").\n"
				"***\n"), name);
			exit(1);
		}
	}

	switch (input_mode) {
	case defconfig:
		if (!defconfig_file)
			defconfig_file = conf_get_default_confname();
		if (conf_read(defconfig_file)) {
			printf(_("***\n"
				"*** Can't find default configuration \"%s\"!\n"
				"***\n"), defconfig_file);
			exit(1);
		}
                name = getenv("KCONFIG_SELINUX");
                printf("KCONFIG_SELINUX(%s)\n", name);
                if (name) {
                        if (conf_read_simple(name, S_DEF_USER, false)) {
                                printf(_("***\n"
                                        "*** Can't find selinux configuration \"%s\"!\n"
                                        "***\n"), name);
                                exit(1);
                        }
                }
                name = getenv("KCONFIG_LOG_SELINUX");
                printf("KCONFIG_LOG_SELINUX(%s)\n", name);
                if (name) {
                        if (conf_read_simple(name, S_DEF_USER, false)) {
                                printf(_("***\n"
                                        "*** Can't find selinux log configuration \"%s\"!\n"
                                        "***\n"), name);
                                exit(1);
                        }
                }
		name = getenv("KCONFIG_VARIANT");
		printf("KCONFIG_VARIANT(%s)\n", name);
		if (name) {
			if (conf_read_simple(name, S_DEF_USER, false)) {
				printf(_("***\n"
					"*** Can't find variant configuration \"%s\"!\n"
					"***\n"), name);
				exit(1);
			}
		} else {
			printf(_("***\n"
				"***  You must specify VARIANT_DEFCONFIG !\n"
				"***\n"));
		//VARIANT_DEFCONFIG temp should be fixed;exit(1);
		}
		name = getenv("KCONFIG_DEBUG");
		printf("KCONFIG_DEBUG(%s)\n", name);
		if (name) {
			if (conf_read_simple(name, S_DEF_USER, false)) {
				printf(_("***\n"
					"*** Can't find debug configuration \"%s\"!\n"
					"***\n"), name);
				exit(1);
			}
		}
		break;
	case savedefconfig:
	case silentoldconfig:
	case oldaskconfig:
	case oldconfig:
	case listnewconfig:
	case oldnoconfig:
		conf_read(NULL);
		break;
	case allnoconfig:
	case allyesconfig:
	case allmodconfig:
	case alldefconfig:
	case randconfig:
		name = getenv("KCONFIG_ALLCONFIG");
		if (name && !stat(name, &tmpstat)) {
			conf_read_simple(name, S_DEF_USER, true);
			break;
		}
		switch (input_mode) {
		case allnoconfig:	name = "allno.config"; break;
		case allyesconfig:	name = "allyes.config"; break;
		case allmodconfig:	name = "allmod.config"; break;
		case alldefconfig:	name = "alldef.config"; break;
		case randconfig:	name = "allrandom.config"; break;
		default: break;
		}
		if (!stat(name, &tmpstat))
			conf_read_simple(name, S_DEF_USER, true);
		else if (!stat("all.config", &tmpstat))
			conf_read_simple("all.config", S_DEF_USER, true);
		break;
	default:
		break;
	}

	if (sync_kconfig) {
		if (conf_get_changed()) {
			name = getenv("KCONFIG_NOSILENTUPDATE");
			if (name && *name) {
				fprintf(stderr,
					_("\n*** The configuration requires explicit update.\n\n"));
				return 1;
			}
		}
		valid_stdin = isatty(0) && isatty(1) && isatty(2);
	}

	switch (input_mode) {
	case allnoconfig:
		conf_set_all_new_symbols(def_no);
		break;
	case allyesconfig:
		conf_set_all_new_symbols(def_yes);
		break;
	case allmodconfig:
		conf_set_all_new_symbols(def_mod);
		break;
	case alldefconfig:
		conf_set_all_new_symbols(def_default);
		break;
	case randconfig:
		conf_set_all_new_symbols(def_random);
		break;
	case defconfig:
		conf_set_all_new_symbols(def_default);
		break;
	case savedefconfig:
		break;
	case oldaskconfig:
		rootEntry = &rootmenu;
		conf(&rootmenu);
		input_mode = silentoldconfig;
		/* fall through */
	case oldconfig:
	case listnewconfig:
	case oldnoconfig:
	case silentoldconfig:
		/* Update until a loop caused no more changes */
		do {
			conf_cnt = 0;
			check_conf(&rootmenu);
		} while (conf_cnt &&
			 (input_mode != listnewconfig &&
			  input_mode != oldnoconfig));
		break;
	}

	if (sync_kconfig) {
		/* silentoldconfig is used during the build so we shall update autoconf.
		 * All other commands are only used to generate a config.
		 */
		if (conf_get_changed() && conf_write(NULL)) {
			fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
			exit(1);
		}
		if (conf_write_autoconf()) {
			fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
			return 1;
		}
	} else if (input_mode == savedefconfig) {
		if (conf_write_defconfig(defconfig_file)) {
			fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
			        defconfig_file);
			return 1;
		}
	} else if (input_mode != listnewconfig) {
		if (conf_write(NULL)) {
			fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
			exit(1);
		}
	}
	return 0;
}
Example #5
0
int get_conf_key(char *conf, char *module, char *key, char *value)
{
    if(check_conf(conf) == -1)
    {
        return -1;
    }
    char buf[MAX_LINE];
    FILE *fp = fopen(conf, "r");
    char *p;
    int mflag = 0, kflag = 0;

    if(fp == NULL)
    {
        LOG_PRINT(LOG_ERROR, "Fail to open config file [%s].", conf);
        return -1;
    }
    while(fgets(buf, 100, fp) != NULL)
    {
        buf[strlen(buf) - 1]='\0';
        if(module != NULL)
        {
            if(strchr(buf, '[') == buf && strstr(buf, module) == buf+1 && strchr(buf, ']') == buf+1+strlen(module))
            {
                mflag = 1;
                while(fgets(buf, 100, fp) != NULL)
                {
                    buf[strlen(buf) - 1]='\0';
                    if(strchr(buf, '[') == buf)
                    {
                        LOG_PRINT(LOG_INFO, "Can't find key [%s] in the module [%s].", key, module);
                        fclose(fp);
                        return -1;
                    }
                    else if(strstr(buf, key) == buf && strchr(buf, '=') == buf+strlen(key))
                    {
                        p = buf + strlen(key) + 1;
                        while(isspace(p[0]))
                        {
                            p++;
                        }
                        strcpy(value, p);
                        fclose(fp);
                        LOG_PRINT(LOG_INFO, "Key [%s] = %s", key, value);
                        return 0;
                    }
                }
                LOG_PRINT(LOG_INFO, "Key [%s] Not Found.", key);
                fclose(fp);
                return -1;
            }
        }
        else
        {
            if(strstr(buf, key) == buf && strchr(buf, '=') == buf+strlen(key))
            {
                p = buf + strlen(key) + 1;
                while(isspace(p[0]))
                {
                    p++;
                }
                strcpy(value, p);
                fclose(fp);
                LOG_PRINT(LOG_INFO, "Key [%s] = %s", key, value);
                return 0;
            }
        }
    }
    if(mflag == 0)
    {
        LOG_PRINT(LOG_INFO, "Can't find module [%s] in config file.", module);
    }
    if(kflag == 0)
    {
        LOG_PRINT(LOG_INFO, "Key [%s] Not Found.", key);
    }
    fclose(fp);
    return -1;
}
Example #6
0
int main(int ac, char **av)
{
	int i = 1;
	const char *name;
	struct stat tmpstat;

	if (ac > i && av[i][0] == '-') {
		switch (av[i++][1]) {
		case 'o':
			input_mode = ask_new;
			break;
		case 's':
			input_mode = ask_silent;
			valid_stdin = isatty(0) && isatty(1) && isatty(2);
			break;
		case 'd':
			input_mode = set_default;
			break;
		case 'D':
			input_mode = set_default;
			defconfig_file = av[i++];
			if (!defconfig_file) {
				printf("%s: No default config file specified\n",
					av[0]);
				exit(1);
			}
			break;
		case 'n':
			input_mode = set_no;
			break;
		case 'm':
			input_mode = set_mod;
			break;
		case 'y':
			input_mode = set_yes;
			break;
		case 'r':
			input_mode = set_random;
			srandom(time(NULL));
			break;
		case 'h':
		case '?':
			printf("%s [-o|-s] config\n", av[0]);
			exit(0);
		}
	}
  	name = av[i];
	if (!name) {
		printf("%s: configuration file missing\n", av[0]);
	}
	conf_parse(name);
	//zconfdump(stdout);
	switch (input_mode) {
	case set_default:
		if (!defconfig_file)
			/* Freetz: We don't have .defconfig file, create config from defaults */
			/* defconfig_file = conf_get_default_confname(); */
			break;
	case ask_silent:
		if (stat(".config", &tmpstat)) {
			printf("***\n"
				"*** You have not yet configured Freetz!\n"
				"***\n"
				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
				"*** \"make menuconfig\" or \"make config\").\n"
				"***\n");
			exit(1);
		}
	case ask_all:
	case ask_new:
		conf_read(NULL);
		break;
	default:
		break;
	}

	if (input_mode != ask_silent) {
		rootEntry = &rootmenu;
		conf(&rootmenu);
		if (input_mode == ask_all) {
			input_mode = ask_silent;
			valid_stdin = 1;
		}
	}
	do {
		conf_cnt = 0;
		check_conf(&rootmenu);
	} while (conf_cnt);
	if (conf_write(NULL)) {
		fprintf(stderr, "\n*** Error during writing of the Freetz configuration.\n\n");
		return 1;
	}
	return 0;
}
Example #7
0
int
LIS3MDL::collect()
{
#pragma pack(push, 1)
	struct { /* status register and data as read back from the device */
		uint8_t		x[2];
		uint8_t		y[2];
		uint8_t		z[2];
		uint8_t		t[2];
	}	lis_report;

	struct {
		int16_t		x;
		int16_t		y;
		int16_t		z;
		int16_t		t;
	} report;
#pragma pack(pop)

	int	ret;
//	uint8_t check_counter;

	perf_begin(_sample_perf);
	struct mag_report new_report;
	bool sensor_is_onboard = false;

	float xraw_f;
	float yraw_f;
	float zraw_f;

	/* this should be fairly close to the end of the measurement, so the best approximation of the time */
	new_report.timestamp = hrt_absolute_time();
	new_report.error_count = perf_event_count(_comms_errors);

	/*
	 * @note  We could read the status register here, which could tell us that
	 *        we were too early and that the output registers are still being
	 *        written.  In the common case that would just slow us down, and
	 *        we're better off just never being early.
	 */

	/* get measurements from the device */
	ret = _interface->read(ADDR_OUT_X_L, (uint8_t *)&lis_report, sizeof(lis_report));

	if (ret != OK) {
		perf_count(_comms_errors);
		DEVICE_DEBUG("data/status read error");
		goto out;
	}

	/* convert the data we just received */
	report.x = (((int16_t)lis_report.x[1]) << 8) + lis_report.x[0];
	report.y = (((int16_t)lis_report.y[1]) << 8) + lis_report.y[0];
	report.z = (((int16_t)lis_report.z[1]) << 8) + lis_report.z[0];
	report.t = (((int16_t)lis_report.t[1]) << 8) + lis_report.t[0];

	/* get measurements from the device */
	new_report.temperature = report.t;
	new_report.temperature = 25 + (report.t / (16 * 8.0f));

	// XXX revisit for SPI part, might require a bus type IOCTL

	unsigned dummy;
	sensor_is_onboard = !_interface->ioctl(MAGIOCGEXTERNAL, dummy);

	/*
	 * RAW outputs
	 *
	 */
	new_report.x_raw = report.x;
	new_report.y_raw = report.y;
	new_report.z_raw = report.z;

	/* the LIS3MDL mag on Pixhawk Pro by Drotek has x pointing towards,
	 * y pointing to the right, and z down, therefore no switch needed,
	 * it is better to have no artificial rotation inside the
	 * driver and then use the startup script with -R command with the
	 * real rotation between the sensor and body frame */
	xraw_f = report.x;
	yraw_f = report.y;
	zraw_f = report.z;

	// apply user specified rotation
	rotate_3f(_rotation, xraw_f, yraw_f, zraw_f);

	new_report.x = ((xraw_f * _range_scale) - _scale.x_offset) * _scale.x_scale;
	/* flip axes and negate value for y */
	new_report.y = ((yraw_f * _range_scale) - _scale.y_offset) * _scale.y_scale;
	/* z remains z */
	new_report.z = ((zraw_f * _range_scale) - _scale.z_offset) * _scale.z_scale;

	if (!(_pub_blocked)) {

		if (_mag_topic != nullptr) {
			/* publish it */
			orb_publish(ORB_ID(sensor_mag), _mag_topic, &new_report);

		} else {
			_mag_topic = orb_advertise_multi(ORB_ID(sensor_mag), &new_report,
							 &_orb_class_instance, (sensor_is_onboard) ? ORB_PRIO_HIGH : ORB_PRIO_MAX);

			if (_mag_topic == nullptr) {
				DEVICE_DEBUG("ADVERT FAIL");
			}
		}
	}

	_last_report = new_report;

	/* post a report to the ring */
	if (_reports->force(&new_report)) {
		perf_count(_buffer_overflows);
	}

	/* notify anyone waiting for data */
	poll_notify(POLLIN);

	check_conf();

	ret = OK;

out:
	perf_end(_sample_perf);
	return ret;
}
Example #8
0
int
HMC5883::collect()
{
#pragma pack(push, 1)
	struct { /* status register and data as read back from the device */
		uint8_t		x[2];
		uint8_t		z[2];
		uint8_t		y[2];
	}	hmc_report;
#pragma pack(pop)
	struct {
		int16_t		x, y, z;
	} report;

	int	ret;
	uint8_t check_counter;

	perf_begin(_sample_perf);
	struct mag_report new_report;
	bool sensor_is_onboard = false;

	float xraw_f;
	float yraw_f;
	float zraw_f;

	/* this should be fairly close to the end of the measurement, so the best approximation of the time */
	new_report.timestamp = hrt_absolute_time();
	new_report.error_count = perf_event_count(_comms_errors);
	new_report.range_ga = _range_ga;
	new_report.scaling = _range_scale;
	new_report.device_id = _device_id.devid;

	/*
	 * @note  We could read the status register here, which could tell us that
	 *        we were too early and that the output registers are still being
	 *        written.  In the common case that would just slow us down, and
	 *        we're better off just never being early.
	 */

	/* get measurements from the device */
	ret = _interface->read(ADDR_DATA_OUT_X_MSB, (uint8_t *)&hmc_report, sizeof(hmc_report));

	if (ret != OK) {
		perf_count(_comms_errors);
		DEVICE_DEBUG("data/status read error");
		goto out;
	}

	/* swap the data we just received */
	report.x = (((int16_t)hmc_report.x[0]) << 8) + hmc_report.x[1];
	report.y = (((int16_t)hmc_report.y[0]) << 8) + hmc_report.y[1];
	report.z = (((int16_t)hmc_report.z[0]) << 8) + hmc_report.z[1];

	/*
	 * If any of the values are -4096, there was an internal math error in the sensor.
	 * Generalise this to a simple range check that will also catch some bit errors.
	 */
	if ((abs(report.x) > 2048) ||
	    (abs(report.y) > 2048) ||
	    (abs(report.z) > 2048)) {
		perf_count(_comms_errors);
		goto out;
	}

	/* get measurements from the device */
	new_report.temperature = 0;

	if (_conf_reg & HMC5983_TEMP_SENSOR_ENABLE) {
		/*
		  if temperature compensation is enabled read the
		  temperature too.

		  We read the temperature every 10 samples to avoid
		  excessive I2C traffic
		 */
		if (_temperature_counter++ == 10) {
			uint8_t raw_temperature[2];

			_temperature_counter = 0;

			ret = _interface->read(ADDR_TEMP_OUT_MSB,
					       raw_temperature, sizeof(raw_temperature));

			if (ret == OK) {
				int16_t temp16 = (((int16_t)raw_temperature[0]) << 8) +
						 raw_temperature[1];
				new_report.temperature = 25 + (temp16 / (16 * 8.0f));
				_temperature_error_count = 0;

			} else {
				_temperature_error_count++;

				if (_temperature_error_count == 10) {
					/*
					  it probably really is an old HMC5883,
					  and can't do temperature. Disable it
					*/
					_temperature_error_count = 0;
					DEVICE_DEBUG("disabling temperature compensation");
					set_temperature_compensation(0);
				}
			}

		} else {
			new_report.temperature = _last_report.temperature;
		}
	}

	/*
	 * RAW outputs
	 *
	 * to align the sensor axes with the board, x and y need to be flipped
	 * and y needs to be negated
	 */
	new_report.x_raw = report.y;
	new_report.y_raw = -report.x;
	/* z remains z */
	new_report.z_raw = report.z;

	/* scale values for output */

	// XXX revisit for SPI part, might require a bus type IOCTL
	unsigned dummy;
	sensor_is_onboard = !_interface->ioctl(MAGIOCGEXTERNAL, dummy);
	new_report.is_external = !sensor_is_onboard;

	if (sensor_is_onboard) {
		// convert onboard so it matches offboard for the
		// scaling below
		report.y = -report.y;
		report.x = -report.x;
	}

	/* the standard external mag by 3DR has x pointing to the
	 * right, y pointing backwards, and z down, therefore switch x
	 * and y and invert y */
	xraw_f = -report.y;
	yraw_f = report.x;
	zraw_f = report.z;

	// apply user specified rotation
	rotate_3f(_rotation, xraw_f, yraw_f, zraw_f);

	new_report.x = ((xraw_f * _range_scale) - _scale.x_offset) * _scale.x_scale;
	/* flip axes and negate value for y */
	new_report.y = ((yraw_f * _range_scale) - _scale.y_offset) * _scale.y_scale;
	/* z remains z */
	new_report.z = ((zraw_f * _range_scale) - _scale.z_offset) * _scale.z_scale;

	if (!(_pub_blocked)) {

		if (_mag_topic != nullptr) {
			/* publish it */
			orb_publish(ORB_ID(sensor_mag), _mag_topic, &new_report);

		} else {
			_mag_topic = orb_advertise_multi(ORB_ID(sensor_mag), &new_report,
							 &_orb_class_instance, (sensor_is_onboard) ? ORB_PRIO_HIGH : ORB_PRIO_MAX);

			if (_mag_topic == nullptr) {
				DEVICE_DEBUG("ADVERT FAIL");
			}
		}
	}

	_last_report = new_report;

	/* post a report to the ring */
	_reports->force(&new_report);

	/* notify anyone waiting for data */
	poll_notify(POLLIN);

	/*
	  periodically check the range register and configuration
	  registers. With a bad I2C cable it is possible for the
	  registers to become corrupt, leading to bad readings. It
	  doesn't happen often, but given the poor cables some
	  vehicles have it is worth checking for.
	 */
	check_counter = perf_event_count(_sample_perf) % 256;

	if (check_counter == 0) {
		check_range();
	}

	if (check_counter == 128) {
		check_conf();
	}

	ret = OK;

out:
	perf_end(_sample_perf);
	return ret;
}
Example #9
0
int
HMC5883::collect()
{
#pragma pack(push, 1)
	struct { /* status register and data as read back from the device */
		uint8_t		x[2];
		uint8_t		z[2];
		uint8_t		y[2];
	}	hmc_report;
#pragma pack(pop)
	struct {
		int16_t		x, y, z;
	} report;

	int	ret;
	uint8_t	cmd;
	uint8_t check_counter;

	perf_begin(_sample_perf);
	struct mag_report new_report;

	/* this should be fairly close to the end of the measurement, so the best approximation of the time */
	new_report.timestamp = hrt_absolute_time();
        new_report.error_count = perf_event_count(_comms_errors);

	/*
	 * @note  We could read the status register here, which could tell us that
	 *        we were too early and that the output registers are still being
	 *        written.  In the common case that would just slow us down, and
	 *        we're better off just never being early.
	 */

	/* get measurements from the device */
	cmd = ADDR_DATA_OUT_X_MSB;
	ret = transfer(&cmd, 1, (uint8_t *)&hmc_report, sizeof(hmc_report));

	if (ret != OK) {
		perf_count(_comms_errors);
		debug("data/status read error");
		goto out;
	}

	/* swap the data we just received */
	report.x = (((int16_t)hmc_report.x[0]) << 8) + hmc_report.x[1];
	report.y = (((int16_t)hmc_report.y[0]) << 8) + hmc_report.y[1];
	report.z = (((int16_t)hmc_report.z[0]) << 8) + hmc_report.z[1];

	/*
	 * If any of the values are -4096, there was an internal math error in the sensor.
	 * Generalise this to a simple range check that will also catch some bit errors.
	 */
	if ((abs(report.x) > 2048) ||
	    (abs(report.y) > 2048) ||
	    (abs(report.z) > 2048)) {
		perf_count(_comms_errors);
		goto out;
	}

	/*
	 * RAW outputs
	 *
	 * to align the sensor axes with the board, x and y need to be flipped
	 * and y needs to be negated
	 */
	new_report.x_raw = report.y;
	new_report.y_raw = -report.x;
	/* z remains z */
	new_report.z_raw = report.z;

	/* scale values for output */

#ifdef PX4_I2C_BUS_ONBOARD
	if (_bus == PX4_I2C_BUS_ONBOARD) {
		// convert onboard so it matches offboard for the
		// scaling below
		report.y = -report.y;
		report.x = -report.x;
        }
#endif

	/* the standard external mag by 3DR has x pointing to the
	 * right, y pointing backwards, and z down, therefore switch x
	 * and y and invert y */
	new_report.x = ((-report.y * _range_scale) - _scale.x_offset) * _scale.x_scale;
	/* flip axes and negate value for y */
	new_report.y = ((report.x * _range_scale) - _scale.y_offset) * _scale.y_scale;
	/* z remains z */
	new_report.z = ((report.z * _range_scale) - _scale.z_offset) * _scale.z_scale;

	// apply user specified rotation
	rotate_3f(_rotation, new_report.x, new_report.y, new_report.z);

	if (!(_pub_blocked)) {

		if (_mag_topic != -1) {
			/* publish it */
			orb_publish(_mag_orb_id, _mag_topic, &new_report);
		} else {
			_mag_topic = orb_advertise(_mag_orb_id, &new_report);

			if (_mag_topic < 0)
				debug("ADVERT FAIL");
		}
	}

	_last_report = new_report;

	/* post a report to the ring */
	if (_reports->force(&new_report)) {
		perf_count(_buffer_overflows);
	}

	/* notify anyone waiting for data */
	poll_notify(POLLIN);

	/*
	  periodically check the range register and configuration
	  registers. With a bad I2C cable it is possible for the
	  registers to become corrupt, leading to bad readings. It
	  doesn't happen often, but given the poor cables some
	  vehicles have it is worth checking for.
	 */
	check_counter = perf_event_count(_sample_perf) % 256;
	if (check_counter == 0) {
		check_range();
	}
	if (check_counter == 128) {
		check_conf();
	}

	ret = OK;

out:
	perf_end(_sample_perf);
	return ret;
}
Example #10
0
int main(int ac, char **av)
{
	int opt;
	const char *name;
	struct stat tmpstat;
	bool report = false, update = false;

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	while ((opt = getopt(ac, av, "osdD:nmyrRUh")) != -1) {
		switch (opt) {
		case 'o':
			input_mode = ask_new;
			break;
		case 's':
			input_mode = ask_silent;
			valid_stdin = isatty(0) && isatty(1) && isatty(2);
			break;
		case 'd':
			input_mode = set_default;
			break;
		case 'D':
			input_mode = set_default;
			defconfig_file = optarg;
			break;
		case 'n':
			input_mode = set_no;
			break;
		case 'm':
			input_mode = set_mod;
			break;
		case 'y':
			input_mode = set_yes;
			break;
		case 'r':
			input_mode = set_random;
			srand(time(NULL));
			break;
		case 'R':
			input_mode = set_default;
			report = update = true;
			break;
		case 'U':
			input_mode = set_default;
			update = true;
			break;
		case 'h':
			printf(_("See README for usage info\n"));
			exit(0);
			break;
		default:
			fprintf(stderr, _("See README for usage info\n"));
			exit(1);
		}
	}
	if (ac == optind) {
		printf(_("%s: Kconfig file missing\n"), av[0]);
		exit(1);
	}
	name = av[optind];
	conf_parse(name);
	//zconfdump(stdout);
	switch (input_mode) {
	case set_default:
		if (update)
			conf_read(NULL);
		else {
			if (!defconfig_file)
				defconfig_file = conf_get_default_confname();
			if (conf_read(defconfig_file)) {
				printf("***\n"
					"*** Can't find default configuration \"%s\"!\n"
					"***\n", defconfig_file);
				exit(1);
			}
		}
		break;
	case ask_silent:
		if (stat(".config", &tmpstat)) {
			printf(_("***\n"
				"*** You have not yet configured your kernel!\n"
				"*** (missing kernel .config file)\n"
				"***\n"
				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
				"*** \"make menuconfig\" or \"make xconfig\").\n"
				"***\n"));
			exit(1);
		}
	case ask_all:
	case ask_new:
		conf_read(NULL);
		break;
	case set_no:
	case set_mod:
	case set_yes:
	case set_random:
		name = getenv("KCONFIG_ALLCONFIG");
		if (name && !stat(name, &tmpstat)) {
			conf_read_simple(name, S_DEF_USER);
			break;
		}
		switch (input_mode) {
		case set_no:	 name = "allno.config"; break;
		case set_mod:	 name = "allmod.config"; break;
		case set_yes:	 name = "allyes.config"; break;
		case set_random: name = "allrandom.config"; break;
		default: break;
		}
		if (!stat(name, &tmpstat))
			conf_read_simple(name, S_DEF_USER);
		else if (!stat("all.config", &tmpstat))
			conf_read_simple("all.config", S_DEF_USER);
		break;
	default:
		break;
	}

	if (input_mode != ask_silent) {
		rootEntry = &rootmenu;
		conf(&rootmenu);
		if (input_mode == ask_all) {
			input_mode = ask_silent;
			valid_stdin = 1;
		}
	} else if (conf_get_changed()) {
		name = getenv("KCONFIG_NOSILENTUPDATE");
		if (name && *name) {
			fprintf(stderr, _("\n*** Kernel configuration requires explicit update.\n\n"));
			return 1;
		}
	} else
		goto skip_check;

	do {
		conf_cnt = 0;
		check_conf(&rootmenu);
	} while (conf_cnt);

	if (report)
		report_changes();

	if (conf_write(NULL)) {
		fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
		return 1;
	}
skip_check:
	if (input_mode == ask_silent && conf_write_autoconf()) {
		fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
		return 1;
	}

	return 0;
}
Example #11
0
int main(int argc, char *argv[])
{ 
	configuration_t configuration;
	int i, j;
	struct timeval tm;
	struct timezone tz;
	measurement_t *measurement;
	struct timeval next, wait;
	int subject_id, flow_id;
	unsigned long long packets, bytes;
	double mbps;
	char command[MAX_COMMAND+1];
	char hostname_interface[MAX_HOSTNAME_INTERFACE+1];	/* DiMAPI connect string
															   as "hostname:interface,..." */

	struct timeval tv_start, tv_stop;	/* to measure how fast mapi_read_result()
														responds */
	int tv_diff_pkt, tv_diff_byte;		/* time used by mapi_read_results() */
	int tv_diff_threshold;		/* 1 if threshold was reached */
	mapi_results_t *pkt_counter_res;
	mapi_results_t *byte_counter_res;
	unsigned long long pkt_counter;
	unsigned long long byte_counter;
	int scope_size;
	double pkt_sec;	/* seconds from previous packet result */
	double byte_sec;	/* seconds from previous byte result */
   mapi_flow_info_t info;
   mapi_device_info_t dinfo;

	openlog("abw", LOG_PID, LOG_LOCAL0);
	syslog(LOG_DEBUG, "starting abw");

	memset((void *)&configuration, 0, (size_t)(sizeof(configuration)));

	/* Create global configuration */
	if ((configuration.global=malloc(sizeof(global_t)))==NULL) {
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return -1;
	}
	memset(configuration.global, 0, sizeof(global_t));

	/* Create first subject, scope, parameters and measurement so that they
      can be filled-in by command-line options */

	/* if ((configuration.subject=new_subject())==NULL) {
      fprintf(stderr, "%s: new_subject() failed\n", __func__);
      return -1;
	}
	if ((configuration.scope=new_scope())==NULL) {
      fprintf(stderr, "%s: new_subject() failed\n", __func__);
      return -1;
	}
	if ((configuration.parameters=new_parameters())==NULL) {
      fprintf(stderr, "%s: new_parameters() failed\n", __func__);
      return -1;
	}

	if ((configuration.measurement=new_measurement())==NULL) {
      fprintf(stderr, "%s: new_measurement() failed\n", __func__);
      return -1;
	} */

	/* Read command line */

	if (read_command_line(argc, argv, &configuration)<0) {
		fprintf(stderr, "%s: read_command_line() failed\n", __func__);
		return -1;
	}

	/* Read configuration file */

	if (configuration.global->conf_filename) {
		if (read_conf_file(&configuration)<0) {
			fprintf(stderr, "%s: read_conf_file() failed\n", __func__);
			return -1;
		}
	}

	/* Fill-in local hostname */

	if (get_local_hostname(&(configuration.global->hostname))<0) {
		fprintf(stderr, "%s: get_local_hostname() failed\n", __func__);
		return -1;
	}

	/* Check if specified values are within acceptable limits */

	if (check_conf(&configuration)<0) {
      fprintf(stderr, "%s: check_conf() failed\n", __func__);
      exit(-1);
	}

	/* Print configuration */

	if (debug)
		print_conf(&configuration);

	if (daemonize) {
		printf("Switching to daemon\n");
		if (continue_as_daemon()<0) {
			fprintf(stderr, "%s: continue_as_daemon() failed\n", __func__);
			return -1;
		}
		printf("Continuing as daemon\n");
	}

	/* 
	 * Create RRD files 
	 */

	/* Go over all measurements */

	measurement=configuration.measurement;
   while (measurement) {

		int parameters_id;
		char *filename;

		parameters_id = measurement->parameters_id;

		/* Go over all protocols */

		j=0;
		while (protocols[j].protocol) {
			if ((filename=
				abw_rrd_create_filename(measurement->scope, 
					parameters_id, protocols[j].protocol))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
			}

			if (abw_rrd_create_file(filename)<0) {
				fprintf(stderr, "%s: abw_rrd_create_file() failed\n", __func__);
				return -1;
			}

			j++;
		} /* Go over all protocols */

		/* Go over all tracked protocols */

		j=0;
		while (tracked_protocols[j].protocol) {
			if ((filename=
				abw_rrd_create_filename(measurement->scope, 
					parameters_id, tracked_protocols[j].protocol))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
			}

			if (abw_rrd_create_file(filename)<0) {
				fprintf(stderr, "%s: abw_rrd_create_file() failed\n", __func__);
				return -1;
			}

			j++;
		} /* Go over all tracked protocols */

		/* Create RRD file for "all" protocol (all traffic together) */

		if ((filename=
			abw_rrd_create_filename(measurement->scope, 
				parameters_id, "all"))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
		}

		if (abw_rrd_create_file(filename)<0) {
			fprintf(stderr, "%s: abw_rrd_create_file() failed\n", __func__);
			return -1;
		}

		measurement=measurement->next;

   } /* while (measurement) */

	/* 
	 * Create MAPI flows 
	 */

	flow_id=0;

	/* Go over all measurements */

	measurement=configuration.measurement;
   while (measurement) {

		/* Go over all monitored protocols */

		i=0;
		while (measurement->protocols_array[i] && i<MAX_PROTOCOLS) {

			int parameters_id;
			char *protocol;

			/* Create data structure to maintain MAPI information */

			if (flow_id>=MAX_FLOWS) {
				fprintf(stderr, "%s: more than %d flows requested\n", __func__,
					MAX_FLOWS);
				return -1;
			}

			if ((flow[flow_id]=new_flow())==NULL) {
  				fprintf(stderr, "%s: new_flow() failed\n", __func__);
  				return -1;
  			}
			flow[flow_id]->measurement=measurement;
			flow[flow_id]->protocol=measurement->protocols_array[i];

			parameters_id = measurement->parameters_id;
			protocol = measurement->protocols_array[i];
				
			if ((flow[flow_id]->rrd_filename=
				abw_rrd_create_filename(measurement->scope, 
					parameters_id, protocol))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
			}

			/* 
			 * If scope has only one subject and if hostname is "localhost" or
			 * equal to local hostname, then use MAPI connect string (not DiMAPI)
			 */

			if (!(measurement->scope->subject[1]) && 
				 (!strcmp(measurement->scope->subject[0]->hostname, "localhost") ||
				  !strcmp(measurement->scope->subject[0]->hostname, 
				  		configuration.global->hostname)))

				strcpy(hostname_interface, 
					measurement->scope->subject[0]->interface);

			/* 
			 * Prepare DiMAPI connect string as hostname:interface, ... 
			 */

			else {
				
				j=0; hostname_interface[0]='\0';
      		while (measurement->scope->subject[j] && j<MAX_SUBJECTS) {

					/* Append comma "," */

					if (hostname_interface[0]) {
						if (strlen(hostname_interface)+1>=MAX_HOSTNAME_INTERFACE) {
							fprintf(stderr, "%s: DiMAPI connect string is longer than %d characters\n", __func__, MAX_HOSTNAME_INTERFACE);
							return -1;
						}
						strcat(hostname_interface, ",");
					}

					/* Append next hostname:interface */
					if (strlen(hostname_interface) +
						 strlen(measurement->scope->subject[j]->hostname) +
				 		strlen(measurement->scope->subject[j]->interface) 
							>= MAX_HOSTNAME_INTERFACE) {
         			fprintf(stderr, "%s: DiMAPI connect string is longer than %d characters\n", __func__, MAX_HOSTNAME_INTERFACE);
            		return -1;
         		}
					sprintf(hostname_interface + strlen(hostname_interface), "%s:%s",
						measurement->scope->subject[j]->hostname,
						measurement->scope->subject[j]->interface);
			
					j++;
				} /* while (measurement->scope->subject[j] && j<MAX_SUBJECTS) */

			} /* Creating DiMAPI connect string */

			/* Create a new MAPI flow */

			if (debug)
				printf("%s: mapi_create_flow(%s)\n", __func__, hostname_interface);

  			if ((flow[flow_id]->fd=mapi_create_flow(hostname_interface))<0) {
				fprintf(stderr, "%s: mapi_create_flow(%s) failed\n", __func__,
					hostname_interface);
				fprintf(stderr, "%s: Do you run mapid daemon on the machine where you connect to?\n", __func__);
				fprintf(stderr, "%s: Do you run mapicommd daemon on the machine where you connect to? (if you are connecting to a non-local machine or to multiple machines)\n", __func__);
					return -1;
			}

         /* If this is a MAPI flow (not DiMAPI flow), then set MPLS and VLAN 
				flags according to mapi.conf. Otherwise the flags were set in
				abw.conf */

			if (!strchr(hostname_interface, ':')) {

				if (debug)
					printf("%s: MAPI flow on \"%s\", setting MPLS and VLAN flags from mapi.conf\n", __func__, hostname_interface);

         	if ((mapi_get_flow_info(flow[flow_id]->fd, &info)) < 0){
            	fprintf(stderr, "%s: mapi_get_flow_info() failed\n", __func__);
            	return -1;
         	}

         	if ((mapi_get_device_info(info.devid, &dinfo)) < 0) {
            	fprintf(stderr, "%s: mapi_get_device_info() failed\n", __func__);
            	return -1;
         	}

         	measurement->scope->mpls = dinfo.mpls;
         	measurement->scope->vlan = dinfo.vlan;

			}
			else
				if (debug)
               printf("%s: DiMAPI flow on \"%s\", setting MPLS and VLAN flags from abw.conf\n", __func__, hostname_interface);

			/* Prepare header filter for this protocol */

			if ((flow[flow_id]->tracked_protocol=
				protocol_filter(measurement->parameters->header_filter, 
					flow[flow_id]->protocol, measurement->scope->mpls, 
					measurement->scope->vlan,
					&(flow[flow_id]->header_filter)))<0) {
				fprintf(stderr, "%s: protocol_filter() failed\n", __func__);
				return -1;
			}

			if (debug)
				printf("measurement->parameters->header_filter: %s, flow[flow_id]->protocol: %s, flow[flow_id]->header_filter: %s, track_function: %s\n", (measurement->parameters->header_filter)?measurement->parameters->header_filter:"NULL", flow[flow_id]->protocol, (flow[flow_id]->header_filter)?flow[flow_id]->header_filter:"NULL", (flow[flow_id]->tracked_protocol)?tracked_protocols[flow[flow_id]->tracked_protocol-1].track_function:"none");

			/* Filter based on input port, we can use port number in the first 
				subject of the scope, because all subjects in a scope must have
				the same port number */

			if (measurement->scope->subject[0]->port >= 0) {
				if ((flow[flow_id]->interface_fid=mapi_apply_function(flow[flow_id]->fd, "INTERFACE", measurement->scope->subject[0]->port))<0) {
					fprintf(stderr, "%s: INTERFACE failed\n", __func__);
               return -1;
            }
			}

			/* Note that BPF_FILTER uses compiled header filter that
				selects packets of the given protocol */

			/* BPF_FILTER is applied if a) header_filter was specified in
				[parameters] section or b) protocol other than "all" and other than
				some that requires tracking was specified in [parameters] section or
				c) MPLS is used on links in this [scope] */

			if (flow[flow_id]->header_filter) {
				if (debug)
					printf("%s: mapi_apply_function(%d, BPF_FILTER, \"%s\")\n",
						__func__, flow[flow_id]->fd, flow[flow_id]->header_filter);
				if ((flow[flow_id]->bpf_filter_fid=
					mapi_apply_function(flow[flow_id]->fd, "BPF_FILTER", 
						flow[flow_id]->header_filter))<0) {
						fprintf(stderr, "%s: BPF_FILTER (\"%s\") failed\n", 
							__func__, flow[flow_id]->header_filter);
						return -1;
				}
			}

			/* Track application protocol, BPF_FILTER could have been applied 
				before */

			if (flow[flow_id]->tracked_protocol) {
				if (debug)
					printf("%s: mapi_apply_function(%d, %s)\n", __func__, 
						flow[flow_id]->fd,
						tracked_protocols[flow[flow_id]->tracked_protocol-1].
							track_function);
				if ((flow[flow_id]->track_function_fid=
					mapi_apply_function(flow[flow_id]->fd, 
						tracked_protocols[flow[flow_id]->tracked_protocol-1].
							track_function))<0) {
					fprintf(stderr, "%s: tracking (%s) failed\n", __func__, 
						tracked_protocols[flow[flow_id]->tracked_protocol-1].
							track_function);
					return -1;
				}
			}

			/* Sampling */

			if (measurement->parameters->sau_mode == 'd' && 
				 (unsigned int)(measurement->parameters->sau_threshold) != 1) {
				if ((flow[flow_id]->sample_fid=
					mapi_apply_function(flow[flow_id]->fd, "SAMPLE", 
						measurement->parameters->sau_threshold, PERIODIC))<0) {
					fprintf(stderr, "%s: SAMPLE (PERIODIC, %.02f) failed\n",
						__func__, measurement->parameters->sau_threshold);
					return -1;
				}
			}
			else if (measurement->parameters->sau_mode == 'p' && 
						(unsigned int)(measurement->parameters->sau_threshold) != 1) {
				if ((flow[flow_id]->sample_fid=
        			mapi_apply_function(flow[flow_id]->fd, "SAMPLE", 
						(measurement->parameters->sau_threshold)*100,
						PROBABILISTIC))<0) {
        			fprintf(stderr, "%s: SAMPLE (PROBABILISTIC, %.02f) failed\n", 
						__func__, (measurement->parameters->sau_threshold)*100);
        			return -1;
      		}
			}
	
			/* Payload searching */
	
			if (measurement->parameters->payload_strings[0]) {
				if ((flow[flow_id]->str_search_fid=
        			mapi_apply_function(flow[flow_id]->fd, "STR_SEARCH", 
					measurement->parameters->payload_strings[0], 0, 0))<0) {
           			fprintf(stderr, "%s: STR_SEARCH (%s) failed\n", 
						__func__, measurement->parameters->payload_strings[0]);
        	   		return -1;
       		}
			}

			/* Counting packets and bytes */

			if ((flow[flow_id]->pkt_counter_fid=
        		mapi_apply_function(flow[flow_id]->fd, "PKT_COUNTER"))<0) {
           		fprintf(stderr, "%s: PKT_COUNTER failed\n", __func__);
        		return -1;
  			}

			/* Simultaneous use of PKT_COUNTER and BYTE_COUNTER does not
				work with DAG4.3GE. Temporary hack: always use stflib version */

			if ((flow[flow_id]->byte_counter_fid=
        		mapi_apply_function(flow[flow_id]->fd, "stdflib:BYTE_COUNTER"))<0) {
           		fprintf(stderr, "%s: BYTE_COUNTER failed\n", 
				__func__);
        		return -1;
  			}

			/* Connect to flow */

			if (!configuration.global->no_measure) {
				if (mapi_connect(flow[flow_id]->fd)<0) {
					fprintf(stderr, "%s: mapi_connect() (%s) failed\n", 
						__func__, hostname_interface);
					return -1;
				}

				if ((scope_size=mapi_get_scope_size(flow[flow_id]->fd)) != 
					flow[flow_id]->measurement->scope->subject_no) {
					fprintf(stderr, "%s: mapi_get_scope_size() returned %d for %d subjects\n", __func__, scope_size, flow[flow_id]->measurement->scope->subject_no);
					return -1;
				}
			}

			i++;
			flow_id++;

		} /* while (measurement->protocols_array[i] && i<MAX_PROTOCOLS) */

		measurement=measurement->next;

	} /* while (measurement) */

	if (configuration.global->no_measure || !configuration.measurement)
		return 0;

	/* Periodically get results from MAPI flows */

	while (1) {
		if (gettimeofday(&tm, &tz)<0) {
			fprintf(stderr, "%s: gettimeofday() failed\n", __func__);
			return -1;
		}

		flow_id=0;
		while (flow[flow_id] && flow_id<MAX_FLOWS) {

			int scope_packets, scope_bytes;
	
			if (!configuration.global->no_stdout) {
				printf("%d %u.%u", flow[flow_id]->measurement->scope->id, 
					(unsigned int)(tm.tv_sec), 
					(unsigned int)(tm.tv_usec));
				if (!configuration.global->stdout_simple)
					printf(" %s\n", flow[flow_id]->protocol);
			}

			gettimeofday(&tv_start, NULL);
			if ((pkt_counter_res=mapi_read_results(flow[flow_id]->fd, 
				flow[flow_id]->pkt_counter_fid))==NULL) {
					fprintf(stderr, "%s: mapi_read_results() for flow %d failed\n",
						__func__, flow_id);
			 	return -1;
			}

			gettimeofday(&tv_stop, NULL);
			tv_diff_pkt=timestamp_diff(&tv_start, &tv_stop);

			gettimeofday(&tv_start, NULL);
			if ((byte_counter_res=mapi_read_results(flow[flow_id]->fd, 
				flow[flow_id]->byte_counter_fid))==NULL) {
        			fprintf(stderr, "%s: mapi_read_results() for flow %d failed\n",
             		__func__, flow_id);
          		return -1;
     		}
			gettimeofday(&tv_stop, NULL);
			tv_diff_byte=timestamp_diff(&tv_start, &tv_stop);

			if (tv_diff_pkt>=TV_DIFF_THRESHOLD ||
				 tv_diff_byte>=TV_DIFF_THRESHOLD)
				tv_diff_threshold=1;
			else
				tv_diff_threshold=0;

			if (tv_diff_pkt>=TV_DIFF_THRESHOLD)
				syslog(LOG_DEBUG, "mapi_read_result() for PKT_COUNTER takes %d us for measurement ID %d and protocol %s (threshold %d us reached)", tv_diff_pkt, flow[flow_id]->measurement->id, flow[flow_id]->protocol, TV_DIFF_THRESHOLD);
			if (tv_diff_byte>=TV_DIFF_THRESHOLD)
				syslog(LOG_DEBUG, "mapi_read_result() for BYTE_COUNTER takes %d us for measurement ID %d and protocol %s (threshold %d us reached)", tv_diff_byte, flow[flow_id]->measurement->id, flow[flow_id]->protocol, TV_DIFF_THRESHOLD);

			scope_size = flow[flow_id]->measurement->scope->subject_no;

			scope_packets=0;
			scope_bytes=0;

			for (subject_id=0; subject_id<scope_size; subject_id++) {
	
				pkt_counter=
					*((unsigned long long*)(pkt_counter_res[subject_id].res));
				byte_counter=
					*((unsigned long long*)(byte_counter_res[subject_id].res));

				packets=pkt_counter - flow[flow_id]->pkt_counter[subject_id];
				bytes=byte_counter - flow[flow_id]->byte_counter[subject_id];
				mbps=(double)bytes*8/1000000;

				flow[flow_id]->pkt_counter[subject_id]=pkt_counter;
				flow[flow_id]->byte_counter[subject_id]=byte_counter;

				/* Determine seconds from previous result */

				if (flow[flow_id]->pkt_ts[subject_id])
					pkt_sec=(double)(pkt_counter_res[subject_id].ts -
								flow[flow_id]->pkt_ts[subject_id])/1000000;
				else
					pkt_sec=
						flow[flow_id]->measurement->parameters->interval.tv_sec +
	               (double)(flow[flow_id]->measurement->parameters->interval.tv_usec)/1000000;
		
				if (flow[flow_id]->byte_ts[subject_id])
					byte_sec=(double)(byte_counter_res[subject_id].ts -
						flow[flow_id]->byte_ts[subject_id])/1000000;
				else
					byte_sec=
						flow[flow_id]->measurement->parameters->interval.tv_sec +
	               (double)(flow[flow_id]->measurement->parameters->interval.tv_usec)/1000000;

				scope_packets+=(packets/pkt_sec);
				scope_bytes+=(bytes/byte_sec);

				flow[flow_id]->pkt_ts[subject_id]=
					pkt_counter_res[subject_id].ts;
				flow[flow_id]->byte_ts[subject_id]=
					byte_counter_res[subject_id].ts;

				if (tv_diff_threshold) {
					syslog(LOG_DEBUG, "%s:%s: %.02f seconds from previous result",
						flow[flow_id]->measurement->scope->subject[subject_id]->hostname,
						flow[flow_id]->measurement->scope->subject[subject_id]->interface,
						byte_sec);
				}

				/* Print result */

				if (!configuration.global->no_stdout) {
					if (configuration.global->stdout_simple)
						printf(" %0.2f %0.2f %0.2f", packets/pkt_sec, 
							bytes/byte_sec, mbps/byte_sec);
					else
							printf(" %0.2f packets/s, %0.2f bytes/s, %0.2f Mb/s, time %uus/%uus, interval %0.2fs/%0.2fs\n", 
								packets/pkt_sec, bytes/byte_sec, mbps/byte_sec, 
								tv_diff_pkt, tv_diff_byte, pkt_sec, byte_sec);
				}

			} /* for (subject_id=0; subject_id++; subject_id<scope_size) */

			if (!configuration.global->no_stdout)
				printf("\n");

			/* If interval is at least 1 second, then store results 
			   to RRD file */
				
			if (flow[flow_id]->measurement->parameters->interval.tv_sec) {
				sprintf(command, "rrdtool update %s %u:%lu:%lu:%.6f", 
					 flow[flow_id]->rrd_filename, 
					 (unsigned int)(tm.tv_sec), 
					 (unsigned long)(scope_packets), 
					 (unsigned long)(scope_bytes), 
					 (double)scope_bytes*8/1000000);

				if (configuration.global->debug > 1)
					syslog(LOG_DEBUG, "system(%s)", command);

				if (tm.tv_sec == flow[flow_id]->rrd_ts)
					syslog(LOG_ERR, "duplicate RRD timestamp %u for scope %d\n", (unsigned int)(tm.tv_sec), flow[flow_id]->measurement->scope->id);
				else
					flow[flow_id]->rrd_ts=tm.tv_sec;

				if (debug)
					printf("%s: system(%s)\n", __func__, command);

				if (system(command)<0) {
					fprintf(stderr, "%s: command(%s) failed\n", __func__,
						command);
					return -1;
				}
			}

			flow_id++;

		} /* while (flow[flow_id] && flow_id<MAX_FLOWS) */

		abw_next_timestamp(&(configuration.measurement->parameters->interval), 
			&next, &wait);

		if (!configuration.global->no_stdout && 
			 !configuration.global->stdout_simple) {
     		printf("next.tv_sec: %d, next.tv_usec: %d, wait.tv_sec: %d, wait.tv_usec: %d\n", (int)(next.tv_sec), (int)(next.tv_usec), (int)(wait.tv_sec), (int)(wait.tv_usec));
			printf("===============================================================================\n");
		}

     	usleep(wait.tv_sec * 1000000 + wait.tv_usec);
		 
	} /* while (1) */

	return 0;
} /* main() */
Example #12
0
int main(int ac, char **av)
{
	const char *name;
	struct stat tmpstat;

	if (ac > 1 && av[1][0] == '-') {
		switch (av[1][1]) {
		case 'o':
			input_mode = ask_new;
			break;
		case 's':
			input_mode = ask_silent;
			valid_stdin = isatty(0) && isatty(1) && isatty(2);
			break;
		case 'd':
			input_mode = set_default;
			break;
		case 'n':
			input_mode = set_no;
			break;
		case 'm':
			input_mode = set_mod;
			break;
		case 'y':
			input_mode = set_yes;
			break;
		case 'r':
			input_mode = set_random;
			srandom(time(NULL));
			break;
		case 'x':
              		return mconf_main(ac-1, av + 1);
		case 'h':
		case '?':
			printf("%s [-o|-s] config\n", av[0]);
			exit(0);
		}
		name = av[2];
	} else
		name = av[1];
	conf_parse(name);
	//zconfdump(stdout);
	switch (input_mode) {
	case set_default:
		name = conf_get_default_confname();
		if (conf_read(name)) {
			printf("***\n"
				"*** Can't find default configuration \"%s\"!\n"
				"***\n", name);
			exit(1);
		}
		break;
	case ask_silent:
		if (stat(".config", &tmpstat)) {
			printf("***\n"
				"*** You have not yet configured your kernel!\n"
				"***\n"
				"*** Please run some configurator (e.g. \"make oldconfig\" or\n"
				"*** \"make menuconfig\" or \"make xconfig\").\n"
				"***\n");
			exit(1);
		}
	case ask_all:
	case ask_new:
		conf_read(NULL);
		break;
	default:
		break;
	}

	if (input_mode != ask_silent) {
		rootEntry = &rootmenu;
		conf(&rootmenu);
		if (input_mode == ask_all) {
			input_mode = ask_silent;
			valid_stdin = 1;
		}
	}
	do {
		conf_cnt = 0;
		check_conf(&rootmenu);
	} while (conf_cnt);
	conf_write(NULL);
	return 0;
}
Example #13
0
int main(int argc, char **argv)
{
	struct fb_fix_screeninfo finfo;
    struct stat s;
    int i;
    int do_calib = 1;
    char runme[PROPERTY_VALUE_MAX];
    char name[MAX_LEN] = "";
    char dev[MAX_LEN] = "";
    int found,ret;
	int fd_carstatus = 0;

    /* open log */
    log_fd = open(log, O_WRONLY | O_CREAT | O_TRUNC);

	fd_carstatus = open("/dev/carstatus", O_RDWR);
	if(fd_carstatus == -1){
	    log_write("Failed to open /dev/carstatus\n");
	} else {
        ret = ioctl(fd_carstatus, CARSTATUS_GET_TOUCHPANEL_TYPE, &touch_type);
        log_write("Calibration Touch Panel Type : 0x%x\n", touch_type);        
		if(ret >= 0) {
            if(touch_type == TOUCH_PANEL_TYPE_CAPACITIVE_GOODIX) {
                log_write("No need to do calibration for goodix capacitive\n");
				close(fd_carstatus);
				return 0;
			}
		}
		
		close(fd_carstatus);
	}
    property_get("ro.calibration", runme, "");
    if (runme[0] != '1')
	    return 0;
    
	if (check_conf()){	// if we have the calibration file, skip any following step to save time
		goto err_log;
    }
	umask(0);
	mkdir("/data/system/calibrator",0777);

    /* read framebuffer for resolution */
    fb_fd = open(fb_dev, O_RDWR);
    if (fb_fd <= 0) {
	    log_write("Failed to open %s\n", fb_dev);
	    goto err_log;
    }
    if (-1 == ioctl(fb_fd, FBIOGET_VSCREENINFO, &info)) {
	    log_write("Failed to get screen info\n");
	    goto err_fb;
    }
    log_write("Screen resolution: %dx%d\n", info.xres, info.yres);
    /* map buffer */
    if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
	    log_write("Failed to get screen info: %d\n", errno);
        goto err_fb;
    }
    scrbuf = (__u16*) mmap(0, finfo.smem_len,
			    PROT_READ | PROT_WRITE,
			    MAP_SHARED,
			    fb_fd, 0);
    if (scrbuf== MAP_FAILED) {
	    log_write("Failed to map screen\n");
	    goto err_fb;
    }
    
    /* Clear screen */
    memset(scrbuf, 0xFF, finfo.smem_len);

    /* Find touchscreen device */
    found = find_ts_device(name, dev);
    if (!found) {
        log_write("can not find ts device\n");
        goto err_map;
    }
    
	log_write("dev:%s\n", dev);
    ts_fd = open(dev, O_RDWR);
    if (ts_fd < 0) {
        log_write("Failed to open %s\n", dev);
        goto err_map;
    }
    if(touch_type == TOUCH_PANEL_TYPE_RESISTIVE_MULTIPOINT_ULTRACHIP) {
		do{
			memset(scrbuf, 0xFF, finfo.smem_len);
		}while(ret = do_calibration_uc6811());
	} else {
	    /* Special calibration function for the DA905x PMIC */
	    if (strcmp(name, DA9052_DEV_NAME) == 0) {
	        log_write("do_calibration_da9052\n");
	        do_calibration_da9052(do_calib);
	    } else {
	        log_write("do_calibration\n");
	        if (do_calib) {
				do{
	    			memset(scrbuf, 0xFF, finfo.smem_len);
				}while(ret = do_calibration());
	        }
	    }
	}

    log_write("Calibration done!!\n");
    /* Clear screen */
    memset(scrbuf, 0, finfo.smem_len);
    //test_calibration();

    close(ts_fd);
err_map:
    munmap(scrbuf, finfo.smem_len);
err_fb:
    close(fb_fd);
err_log:
    close(log_fd);

    return 0;
}
Example #14
0
int main(int ac, char **av)
{
    const char *progname = av[0];
    int opt;
    const char *name, *defconfig_file = NULL /* gcc uninit */;
    struct stat tmpstat;

    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);

    while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
        input_mode = (enum input_mode)opt;
        switch (opt) {
        case defconfig:
        case savedefconfig:
            defconfig_file = optarg;
            break;
        case randconfig:
        {
            struct timeval now;
            unsigned int seed;

            /*
             * Use microseconds derived seed,
             * compensate for systems where it may be zero
             */
            gettimeofday(&now, NULL);

            seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
            srand(seed);
            break;
        }
        case oldaskconfig:
        case oldconfig:
        case allnoconfig:
        case allyesconfig:
        case allmodconfig:
        case alldefconfig:
        case listnewconfig:
        case oldnoconfig:
            break;
        case '?':
            conf_usage(progname);
            exit(1);
            break;
        }
    }
    if (ac == optind) {
        printf(_("%s: Kconfig file missing\n"), av[0]);
        conf_usage(progname);
        exit(1);
    }
    name = av[optind];
    conf_parse(name);
    //zconfdump(stdout);

    switch (input_mode) {
    case defconfig:
        if (!defconfig_file)
            defconfig_file = conf_get_default_confname();
        if (conf_read(defconfig_file)) {
            printf(_("***\n"
                     "*** Can't find default configuration \"%s\"!\n"
                     "***\n"), defconfig_file);
            exit(1);
        }
        break;
    case savedefconfig:
    case oldaskconfig:
    case oldconfig:
    case listnewconfig:
    case oldnoconfig:
        conf_read(NULL);
        break;
    case allnoconfig:
    case allyesconfig:
    case allmodconfig:
    case alldefconfig:
    case randconfig:
        name = getenv("KCONFIG_ALLCONFIG");
        if (name && !stat(name, &tmpstat)) {
            conf_read_simple(name, S_DEF_USER);
            break;
        }
        switch (input_mode) {
        case allnoconfig:
            name = "allno.config";
            break;
        case allyesconfig:
            name = "allyes.config";
            break;
        case allmodconfig:
            name = "allmod.config";
            break;
        case alldefconfig:
            name = "alldef.config";
            break;
        case randconfig:
            name = "allrandom.config";
            break;
        default:
            break;
        }
        if (!stat(name, &tmpstat))
            conf_read_simple(name, S_DEF_USER);
        else if (!stat("all.config", &tmpstat))
            conf_read_simple("all.config", S_DEF_USER);
        break;
    default:
        break;
    }

    valid_stdin = isatty(0) && isatty(1) && isatty(2);

    switch (input_mode) {
    case allnoconfig:
        conf_set_all_new_symbols(def_no);
        break;
    case allyesconfig:
        conf_set_all_new_symbols(def_yes);
        break;
    case allmodconfig:
        conf_set_all_new_symbols(def_mod);
        break;
    case alldefconfig:
        conf_set_all_new_symbols(def_default);
        break;
    case randconfig:
        conf_set_all_new_symbols(def_random);
        break;
    case defconfig:
        conf_set_all_new_symbols(def_default);
        break;
    case savedefconfig:
        break;
    case oldaskconfig:
        rootEntry = &rootmenu;
        conf(&rootmenu);
        input_mode = oldconfig;
    /* fall through */
    case oldconfig:
    case listnewconfig:
    case oldnoconfig:
        /* Update until a loop caused no more changes */
        do {
            conf_cnt = 0;
            check_conf(&rootmenu);
        } while (conf_cnt &&
                 (input_mode != listnewconfig &&
                  input_mode != oldnoconfig));
        break;
    }

    if (input_mode == savedefconfig) {
        if (conf_write_defconfig(defconfig_file)) {
            fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
                    defconfig_file);
            return 1;
        }
    } else if (input_mode != listnewconfig) {
        /*
         * build so we shall update autoconf.
         */
        if (conf_write(NULL)) {
            fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
            exit(1);
        }
        if (conf_write_autoconf()) {
            fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
            return 1;
        }
    }
    return 0;
}
/* 
 * Fairly basic main.
 */
int main (int argc, char *argv[])
{
	int status = 0; /* Will be non-zero on error (NOT warning) */
    int ch;
	int option_index = 0;
	int i = 0;
	int free_config = 0;
	static struct option long_options[] =
    {
        {"config",  required_argument, 0, 'c'},
        {"help",    no_argument,       0, 'h'},
        {"kasp",  required_argument, 0, 'k'},
        {"version", no_argument,       0, 'V'},
        {"verbose", no_argument,       0, 'v'},
        {0,0,0,0}
    };

	/* The program name is the last component of the program file name */
    if ((progname = strrchr(argv[0], '/'))) {	/* EQUALS */
        ++progname;			/* Point to character after last "/" */
	}
	else {
		progname = argv[0];
	}

    while ((ch = getopt_long(argc, argv, "c:hk:Vv", long_options, &option_index)) != -1) {
        switch (ch) {
            case 'c':
				config = StrStrdup(optarg);
				free_config = 1;
                break;
			case 'h':
				usage();
				exit(0);
				break;
            case 'k':
				kasp = StrStrdup(optarg);
                break;
			case 'V':
                printf("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
                exit(0);
                break;
			case 'v':
				verbose = 1;
				break;
		}
	}

	/* 0) Some basic setup */
	log_init(DEFAULT_LOG_FACILITY, progname);

	/* 1) Check on conf.xml - set kasp.xml (if -k flag not given) */
	status = check_conf(&kasp);

	/* 2) Checks on kasp.xml */
	status += check_kasp();

	if (verbose) {
		dual_log("DEBUG: finished %d\n", status);
	}

	xmlCleanupParser();

	for (i = 0; i < repo_count; i++) {
		StrFree(repo_list[i]);
	}
	StrFree(repo_list);
	if (free_config) {
		StrFree(config);
	}
	StrFree(kasp);

	return status;
}