From e579256d0a2ebcaa407016a4813e91f908ff4651 Mon Sep 17 00:00:00 2001 From: James Maki Date: Fri, 10 Sep 2010 10:33:49 -0500 Subject: add yaml output and make it the default to stdout --- src/eeprom_main.c | 120 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 100 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/eeprom_main.c b/src/eeprom_main.c index c82b7f1..f2c76dc 100644 --- a/src/eeprom_main.c +++ b/src/eeprom_main.c @@ -84,6 +84,83 @@ static int hwaddr_aton(const char *str, uint8_t *buf, size_t len) return 1; } +static int yaml_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) +{ + FILE *file; + + if (!strcmp(name, "-")) { + file = stdout; + } else { + file = fopen(name, "w+"); + if (!file) { + log_error("could not open %s: %m", name); + return -1; + } + } + + fprintf(file, "---\n"); + fprintf(file, "\n"); + + fprintf(file, "vendor-id: %.32s\n", id_eeprom->vendor_id); + fprintf(file, "product-id: %.32s\n", id_eeprom->product_id); + fprintf(file, "device-id: %.32s\n", id_eeprom->device_id); + fprintf(file, "hw-version: %.32s\n", id_eeprom->hw_version); + fprintf(file, "mac-addr: %02X:%02X:%02X:%02X:%02X:%02X\n", + id_eeprom->mac_addr[0], + id_eeprom->mac_addr[1], + id_eeprom->mac_addr[2], + id_eeprom->mac_addr[3], + id_eeprom->mac_addr[4], + id_eeprom->mac_addr[5]); + fprintf(file, "imei: %.32s\n", id_eeprom->imei); + fprintf(file, "capa-gps: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_GPS) ? "yes" : "no"); + fprintf(file, "capa-din: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_DIN) ? "yes" : "no"); + fprintf(file, "capa-dout: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_DOUT) ? "yes" : "no"); + fprintf(file, "capa-adc: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_ADC) ? "yes" : "no"); + + fprintf(file, "capa: \""); + int i; + for (i = 0; i < sizeof(id_eeprom->capa); i++) { + fprintf(file, "\\x%02X", id_eeprom->capa[i]); + } + fprintf(file, "\"\n"); + + fprintf(file, "...\n"); + + fclose(file); + + return 0; +} + +static int bin_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) +{ + int fd; + int tmp; + + if (!strcmp(name, "-")) { + fd = fileno(stdout); + } else { + fd = open(name, O_CREAT | O_WRONLY, 0644); + if (fd < 0) { + log_error("could not open %s: %m", name); + return fd; + } + } + + tmp = write(fd, (char *) id_eeprom, sizeof(*id_eeprom)); + if (tmp < 0) { + log_error("writing %s failed: %m", name); + return fd; + } else if (tmp != sizeof(*id_eeprom)) { + log_error("only wrote %d bytes to %s", tmp, name); + return -1; + } + + close(fd); + + return tmp; +} + static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom) { log_info("sizeof: %lu", sizeof(struct mts_id_eeprom_layout)); @@ -130,6 +207,7 @@ static void usage(FILE *out) { fprintf(out, " --capa-dout |\n"); fprintf(out, " --capa-adc |\n"); fprintf(out, " --capa |\n"); + fprintf(out, " --out-format { bin | yaml (default) } |\n"); fprintf(out, " }\n"); fprintf(out, "\n"); } @@ -148,6 +226,7 @@ enum { CMD_OPT_CAPA_DOUT, CMD_OPT_CAPA_ADC, CMD_OPT_CAPA, + CMD_OPT_OUT_FORMAT, CMD_OPT_VERSION, CMD_OPT_HELP, }; @@ -167,6 +246,7 @@ static struct option long_options[] = { {"capa-dout", 1, NULL, CMD_OPT_CAPA_DOUT}, {"capa-adc", 1, NULL, CMD_OPT_CAPA_ADC}, {"capa", 1, NULL, CMD_OPT_CAPA}, + {"out-format", 1, NULL, CMD_OPT_OUT_FORMAT}, {"version", 0, NULL, CMD_OPT_VERSION}, {"help", 0, NULL, CMD_OPT_HELP}, {0, 0, 0, 0}, @@ -176,10 +256,12 @@ int main(int argc, char *argv[]) { int i; int option_index; int tmp; - char *in_file = NULL; - char *out_file = NULL; int fd; + char *in_file = NULL; + char *out_file = "-"; + char *out_format = "yaml"; + struct mts_id_eeprom_layout id_eeprom; memset(&id_eeprom, 0, sizeof(id_eeprom)); @@ -282,6 +364,16 @@ int main(int argc, char *argv[]) { exit(1); break; + case CMD_OPT_OUT_FORMAT: + if (strcmp(optarg, "bin") && strcmp(optarg, "yaml")) { + log_error("invalid out-format %s", optarg); + usage(stderr); + exit(1); + } + out_format = optarg; + + break; + case CMD_OPT_VERSION: print_version("mts-id-eeprom"); exit(0); @@ -298,27 +390,15 @@ int main(int argc, char *argv[]) { } } - if (out_file) { - fd = open(out_file, O_CREAT | O_WRONLY, 0644); - if (fd < 0) { - log_error("could not open out-file %s: %m", out_file); + if (!strcmp(out_format, "bin")) { + bin_out(out_file, &id_eeprom); + } else if (!strcmp(out_format, "yaml")) { + yaml_out(out_file, &id_eeprom); + } else { + log_error("un-handled out-format"); exit(1); } - - tmp = write(fd, (char *) &id_eeprom, sizeof(id_eeprom)); - if (tmp < 0) { - log_error("writing %s failed: %m", out_file); - exit(1); - } else if (tmp != sizeof(id_eeprom)) { - log_error("only wrote %d bytes to %s", tmp, out_file); - exit(1); - } - - close(fd); - - log_info("wrote eeprom to %s successfully", out_file); - mts_id_eeprom_inspect(&id_eeprom); } return 0; -- cgit v1.2.3