Ejemplo n.º 1
0
void
cmdline(int *argc, char ***argv)
{
	static struct option opts[] = {
		{"address",    required_argument, NULL, 'a'},
		{"device",     required_argument, NULL, 'd'},
		{"prompt",     no_argument,       NULL, 'p'},
		{"retry",      required_argument, NULL, 'r'},
		{"skip",       required_argument, NULL, 's'},
		{"type",       required_argument, NULL, 't'},
		{"wait",       required_argument, NULL, 'w'},
		{"help",       no_argument,       NULL, 'h'},
		{NULL,         no_argument,       NULL,  0 },
	};

	for (;;) {
		int i;

		i = getopt_long(*argc, *argv, "a:d:pr:s:t:w:h", opts, NULL);

		if (i == -1) {
			*argc = optind;			
			break;
		}

		switch (i) {
		case 'a':
			if (isxdigit(*optarg) && strlen(optarg) == 2) {
				int tmp;
				tmp = (eb_htoi(&optarg[0])) * 16 +
					(eb_htoi(&optarg[1]));

				eb_set_qq((unsigned char) tmp);	
			}
			break;
		case 'd':
			device = optarg;
			break;			
		case 'p':
			prompt = YES;
			break;
		case 'r':
			if (isdigit(*optarg)) {
				eb_set_get_retry(atoi(optarg));
			}
			break;
		case 's':
			if (isdigit(*optarg)) {
				eb_set_skip_ack(atoi(optarg));
			}
			break;
		case 't':
			if (isdigit(*optarg))
				type = atoi(optarg);
			break;			
		case 'w':
			if (isdigit(*optarg)) {
				eb_set_max_wait(atol(optarg));
			}
			break;
		case 'h':
		default:
			usage();
			exit(EXIT_FAILURE);
			break;
		}
	}
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[])
{
	int tmp;
	
	/* set progname */
	progname = (const char *)strrchr(argv[0], '/');
	progname = progname ? (progname + 1) : argv[0];

	/* read command line  */
	cmdline(&argc, &argv);
	
	/* set default cfgfile */
	if (*cfgfile == '\0')
		strncpy(cfgfile , DAEMON_CFGFILE, strlen(DAEMON_CFGFILE));

	/* read config file */
	if (cfg_file_read(cfgfile, cfg, cfglen) == -1)
		fprintf(stderr, "can't open cfgfile: %s ==> " \
				"build in settings will be used\n", cfgfile);	

	/* set unset configuration */
	set_unset();

	/* print configuration */
	if (settings == YES)
		cfg_print(cfg, cfglen);

	
	/* set ebus configuration */
	eb_set_nodevicecheck(nodevicecheck);
	eb_set_rawdump(rawdump);
	eb_set_showraw(showraw);

	tmp = (eb_htoi(&address[0])) * 16 + (eb_htoi(&address[1]));
	eb_set_qq((unsigned char) tmp);

	eb_set_get_retry(get_retry);
	eb_set_skip_ack(skip_ack);
	eb_set_max_wait(max_wait);
	eb_set_send_retry(send_retry);
	eb_set_print_size(print_size);

	/* open log */
	log_level(loglevel);
	log_open(logfile, foreground);	

	/* to be daemon */
	if (foreground == NO) {
		log_print(L_ALL, DAEMON_NAME " " DAEMON_VERSION " started");
		syslog(LOG_INFO, DAEMON_NAME " " DAEMON_VERSION " started");
		daemonize();
	}

	/* read ebus command configuration files */
	if (eb_cmd_dir_read(cfgdir, extension) == -1)
		log_print(L_WAR, "error during read command file");

	/* open raw file */
	if (rawdump == YES) {
		if (eb_raw_file_open(rawfile) == -1) {
			log_print(L_ALL, "can't open rawfile: %s", rawfile);
			cleanup(EXIT_FAILURE);
		} else {
			log_print(L_INF, "%s opened", rawfile);
		}

	}

	/* open serial device */
	if (eb_serial_open(device, &serialfd) == -1) {
		log_print(L_ALL, "can't open device: %s", device);
		cleanup(EXIT_FAILURE);
	} else {
		log_print(L_INF, "%s opened", device);
	}


	/* open listing tcp socket */
	if (sock_open(&socketfd, port, localhost) == -1) {
		log_print(L_ALL, "can't open port: %d", port);
		cleanup(EXIT_FAILURE);
	} else {
		log_print(L_INF, "port %d opened", port);
	}

	/* init msg queue */
	if (msg_queue_init() == -1) {
		log_print(L_ALL, "can't initialize msg queue");
		cleanup(EXIT_FAILURE);
	} else {
		msg_queue_on = YES;
		log_print(L_INF, "msg queue initialized");
	}

	/* enter main loop */
	main_loop();

	cleanup(EXIT_SUCCESS);

	return 0;
}
Ejemplo n.º 3
0
int
main(int argc, char *argv[])
{
	int i, j, k, end, ret, val, max_argc, buslen;
	int in[SERIAL_BUFSIZE + 1];
	char byte;
	unsigned char msg[SERIAL_BUFSIZE + 1], bus[TMP_BUFSIZE];

	progname = (const char *)strrchr(argv[0], '/');
	progname = progname ? (progname + 1) : argv[0];

	max_argc = argc;
	cmdline(&argc, &argv);

	val = 0;

	if (prompt) {

		end = 0;
		do {
			byte = '\0';
			i = 0;
			printf("msg: ");
			while ((byte = fgetc(stdin)) != EOF) {

				if (byte == '\n')
					break;

				if (byte == 'q') {
					end = 1;
					break;
				}

				if (i < (int) sizeof(in)) {

					ret = eb_htoi(&byte);
					if (ret != -1) {
						in[i] = ret;
						i++;
					}
			
				} else {
					break;
				}
			}

			if (!end && i > 0) {
				
				memset(msg, '\0', sizeof(msg));
				memset(bus, '\0', sizeof(bus));

				for (j = 0, k = 0; j < i; j += 2, k++)
					msg[k] = (unsigned char)
							(in[j]*16 + in[j+1]);
		
				ret = eb_serial_open(device, &serialfd);
				if (ret < 0)
					fprintf(stdout, "Error open %s.\n", device);
					
				if (ret == 0) {
					ret = eb_send_data(msg, k, type, bus, &buslen);
					if (ret == 0) {
						fprintf(stdout, "res:");
						eb_print_result();
					}
						
				}
				
				ret = eb_serial_close();
				if (ret < 0)
					fprintf(stdout, "Error close %s.\n", device);

			}

		} while (end == 0);
	} else {

		memset(in, '\0', sizeof(in));
		k = 0;
		i = 0;
		for (k = argc; k < max_argc; k++) {
			j = 0;
			while (argv[k][j] != '\0') {
				byte = argv[k][j];
				if (i < (int) sizeof(in)) {

					ret = eb_htoi(&byte);
					if (ret != -1) {
						in[i] = ret;
						i++;
					}
				}
				j++;
			}
		}

		memset(bus, '\0', sizeof(bus));
		memset(msg, '\0', sizeof(msg));
		for (j = 0, k = 0; j < i; j += 2, k++)
			msg[k] = (unsigned char) (in[j]*16 + in[j+1]);

		if (k > 0) {

			ret = eb_serial_open(device, &serialfd);
			if (ret < 0)
				fprintf(stdout, "Error open %s.\n", device);
				
			if (ret == 0) {
				ret = eb_send_data(msg, k, type, bus, &buslen);
				val = ret;
				if (ret == 0) {
					if (type == EBUS_MSG_MASTER_SLAVE)
						eb_print_result();

					if (type == EBUS_MSG_MASTER_MASTER)
						fprintf(stdout, " %d\n", ret);
				}
					
			}
			
			ret = eb_serial_close();
			if (ret < 0)
				fprintf(stdout, "Error close %s.\n", device);
		}
		
	}
	
	return val;
}
Ejemplo n.º 4
0
int main() {

	int i, j, end, ret;

	int bcd, d1b, data[SERIAL_BUFSIZE + 1];
	float d1c, d2b, d2c;

	char byte;
	
	unsigned char hex, tmp, crc, crc_calc[2] ;

	tmp = 0;
	end = 0;
	do {
		i = 0;
		printf("Input: ");
		while ((byte = fgetc(stdin)) != EOF) {
			
			if (byte == '\n') {
				break;
			}
				
			if (byte == 'q') {
				end = 1;
				break;
			}
			
			if (i < sizeof(data)) {
				ret = eb_htoi(&byte);
				if (ret != -1) {
					data[i] = ret;
					i++;
				}
			} else {
				break;
			}
		}		
		
		if (!end) {
		
			for (j = 0; j < i; j += 2) {
				
				bcd = 0;
				d1b = 0;
				ret = 0;
				d1c = 0.0;
				d2b = 0.0;
				d2c = 0.0;

				hex = (unsigned char) (data[j]*16 + data[j+1]);
				
				ret = eb_bcd_to_int(hex, &bcd);
				ret = eb_d1b_to_int(hex, &d1b);
				
				ret = eb_d1c_to_float(hex, &d1c);
				printf("hex %02x ->\tbcd: %3d\td1b: %4d"
					"\td1c: %5.1f", hex, bcd, d1b, d1c);

				
				if (j == 2) {
					ret = eb_d2b_to_float(tmp, hex, &d2b);
					ret = eb_d2c_to_float(tmp, hex, &d2c);
					crc_calc[0] = tmp;
					crc_calc[1] = hex;
					crc = eb_calc_crc(crc_calc,2);
					printf("\td2b: %10.5f\td2c: %12.6f"
						"\tcrc: %02x\n", d2b, d2c, crc);
				}
				else {
					tmp = hex;
					printf("\n");
				}
			}
		}

	} while (end == 0);
	
	return 0;
}