From fbfe22bc022a54fa3a092b4977f578128f261398 Mon Sep 17 00:00:00 2001 From: James Maki Date: Thu, 9 Sep 2010 15:46:52 -0500 Subject: initial commit --- src/Makefile.am | 8 ++ src/eeprom.h | 6 + src/eeprom_main.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 47 ++++++++ src/mts_io.h | 52 +++++++++ 5 files changed, 439 insertions(+) create mode 100644 src/Makefile.am create mode 100644 src/eeprom.h create mode 100644 src/eeprom_main.c create mode 100644 src/log.h create mode 100644 src/mts_io.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..2e9242e --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,8 @@ +AUTOMAKE_OPTIONS = gnu +AM_CFLAGS = -Wall +bin_PROGRAMS = mts-id-eeprom +mts_id_eeprom_SOURCES = eeprom_main.c +noinst_HEADERS = eeprom.h mts_io.h + +EXTRA_DIST = + diff --git a/src/eeprom.h b/src/eeprom.h new file mode 100644 index 0000000..77c44ec --- /dev/null +++ b/src/eeprom.h @@ -0,0 +1,6 @@ +#ifndef __HELLOWORLD_H +#define __HELLOWORLD_H + +#include "config.h" + +#endif /* ~__HELLOWORLD_H */ diff --git a/src/eeprom_main.c b/src/eeprom_main.c new file mode 100644 index 0000000..c82b7f1 --- /dev/null +++ b/src/eeprom_main.c @@ -0,0 +1,326 @@ +/* + * Create an image file for the MTCDP ID EEPROM + * + * Copyright (C) 2010 by Multi-Tech Systems + * + * Author: James Maki + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "eeprom.h" +#include "mts_io.h" + +static int hwaddr_aton(const char *str, uint8_t *buf, size_t len) +{ + size_t count = 0; + + for (;;) { + unsigned acc; + char ch; + + acc = 0; + + while ((ch = *str) != ':' && ch != 0) { + if (ch >= '0' && ch <= '9') { + ch -= '0'; + } else if (ch >= 'a' && ch <= 'f') { + ch -= 'a' - 10; + } else if (ch >= 'A' && ch <= 'F') { + ch -= 'A' - 10; + } else { + return 0; + } + acc = (acc << 4) + ch; + str++; + } + + if (acc > 255) { + return 0; + } + + if (count >= len) { + return 0; + } + + buf[count] = acc; + count++; + + if (ch == 0) { + break; + } + str++; + } + + if (count < len) { + return 0; + } + + return 1; +} + +static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom) +{ + log_info("sizeof: %lu", sizeof(struct mts_id_eeprom_layout)); + log_info("vendor-id: %.32s", id_eeprom->vendor_id); + log_info("product-id: %.32s", id_eeprom->product_id); + log_info("device-id: %.32s", id_eeprom->device_id); + log_info("hw-version: %.32s", id_eeprom->hw_version); + log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X", + 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]); + log_info("imei: %.32s", id_eeprom->imei); + log_info("capa-gps: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_GPS) ? "yes" : "no"); + log_info("capa-din: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_DIN) ? "yes" : "no"); + log_info("capa-dout: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_DOUT) ? "yes" : "no"); + log_info("capa-adc: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_ADC) ? "yes" : "no"); +} + +static void print_version(const char *name) { + printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name); + printf("Copyright (C) 2010 by Multi-Tech Systems\n"); + printf( +"This program is free software; you may redistribute it under the terms of\n" +"the GNU General Public License version 2 or (at your option) any later version.\n" +"This program has absolutely no warranty.\n"); +} + +static void usage(FILE *out) { + fprintf(out, "usage: mts-id-eeprom [ OPTIONS ... ]\n"); + fprintf(out, "where OPTIONS := { \n"); + fprintf(out, " --in-file |\n"); + fprintf(out, " --out-file |\n"); + fprintf(out, " --vendor-id |\n"); + fprintf(out, " --product-id |\n"); + fprintf(out, " --device-id |\n"); + fprintf(out, " --hw-version |\n"); + fprintf(out, " --mac-addr |\n"); + fprintf(out, " --imei |\n"); + fprintf(out, " --capa-gps |\n"); + fprintf(out, " --capa-din |\n"); + fprintf(out, " --capa-dout |\n"); + fprintf(out, " --capa-adc |\n"); + fprintf(out, " --capa |\n"); + fprintf(out, " }\n"); + fprintf(out, "\n"); +} + +enum { + CMD_OPT_IN_FILE = 128, + CMD_OPT_OUT_FILE, + CMD_OPT_VENDOR_ID, + CMD_OPT_PRODUCT_ID, + CMD_OPT_DEVICE_ID, + CMD_OPT_HW_VERSION, + CMD_OPT_MAC_ADDR, + CMD_OPT_IMEI, + CMD_OPT_CAPA_GPS, + CMD_OPT_CAPA_DIN, + CMD_OPT_CAPA_DOUT, + CMD_OPT_CAPA_ADC, + CMD_OPT_CAPA, + CMD_OPT_VERSION, + CMD_OPT_HELP, +}; + +static char *short_options = "f:"; +static struct option long_options[] = { + {"in-file", 1, NULL, CMD_OPT_IN_FILE}, + {"out-file", 1, NULL, CMD_OPT_OUT_FILE}, + {"vendor-id", 1, NULL, CMD_OPT_VENDOR_ID}, + {"product-id", 1, NULL, CMD_OPT_PRODUCT_ID}, + {"device-id", 1, NULL, CMD_OPT_DEVICE_ID}, + {"hw-version", 1, NULL, CMD_OPT_HW_VERSION}, + {"mac-addr", 1, NULL, CMD_OPT_MAC_ADDR}, + {"imei", 1, NULL, CMD_OPT_IMEI}, + {"capa-gps", 1, NULL, CMD_OPT_CAPA_GPS}, + {"capa-din", 1, NULL, CMD_OPT_CAPA_DIN}, + {"capa-dout", 1, NULL, CMD_OPT_CAPA_DOUT}, + {"capa-adc", 1, NULL, CMD_OPT_CAPA_ADC}, + {"capa", 1, NULL, CMD_OPT_CAPA}, + {"version", 0, NULL, CMD_OPT_VERSION}, + {"help", 0, NULL, CMD_OPT_HELP}, + {0, 0, 0, 0}, +}; + +int main(int argc, char *argv[]) { + int i; + int option_index; + int tmp; + char *in_file = NULL; + char *out_file = NULL; + int fd; + + struct mts_id_eeprom_layout id_eeprom; + + memset(&id_eeprom, 0, sizeof(id_eeprom)); + +#if 0 + strncpy(id_eeprom.vendor_id, VENDOR_ID_MULTITECH, sizeof(id_eeprom.vendor_id) - 1); + strncpy(id_eeprom.product_id, PRODUCT_ID_MTCDP_E1_DK, sizeof(id_eeprom.product_id) - 1); + strncpy(id_eeprom.device_id, "12345678", sizeof(id_eeprom.device_id) - 1); + strncpy(id_eeprom.hw_version, HW_VERSION_MTCDP_1_0, sizeof(id_eeprom.hw_version) - 1); + + hwaddr_aton("00:d0:a0:02:0d:e1", id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr)); + strncpy(id_eeprom.imei, "353626020834450", sizeof(id_eeprom.imei) - 1); + + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_GPS); + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DIN); + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DOUT); + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_ADC); +#endif + + while((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) { + switch(i) { + case 0: + break; + + case CMD_OPT_IN_FILE: + in_file = optarg; + fd = open(in_file, O_RDONLY); + if (fd < 0) { + log_error("could not open in-file %s: %m", in_file); + exit(1); + } + + tmp = read(fd, (char *) &id_eeprom, sizeof(id_eeprom)); + if (tmp < 0) { + log_error("reading %s failed: %m", in_file); + exit(1); + } else if (tmp != sizeof(id_eeprom)) { + log_error("only read %d bytes from %s", tmp, in_file); + exit(1); + } + + close(fd); + + log_info("loaded eeprom from %s successfully", in_file); + mts_id_eeprom_inspect(&id_eeprom); + + break; + + case CMD_OPT_OUT_FILE: + out_file = optarg; + break; + + case CMD_OPT_VENDOR_ID: + strncpy(id_eeprom.vendor_id, optarg, sizeof(id_eeprom.vendor_id) - 1); + break; + + case CMD_OPT_PRODUCT_ID: + strncpy(id_eeprom.product_id, optarg, sizeof(id_eeprom.product_id) - 1); + break; + + case CMD_OPT_DEVICE_ID: + strncpy(id_eeprom.device_id, optarg, sizeof(id_eeprom.device_id) - 1); + break; + + case CMD_OPT_HW_VERSION: + strncpy(id_eeprom.hw_version, optarg, sizeof(id_eeprom.hw_version) - 1); + break; + + case CMD_OPT_MAC_ADDR: + tmp = hwaddr_aton(optarg, id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr)); + if (!tmp) { + log_error("invalid mac-addr %s", optarg); + usage(stderr); + exit(1); + } + break; + + case CMD_OPT_IMEI: + strncpy(id_eeprom.imei, optarg, sizeof(id_eeprom.imei) - 1); + break; + + case CMD_OPT_CAPA_GPS: + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_GPS); + break; + + case CMD_OPT_CAPA_DIN: + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DIN); + break; + + case CMD_OPT_CAPA_DOUT: + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DOUT); + break; + + case CMD_OPT_CAPA_ADC: + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_ADC); + break; + + case CMD_OPT_CAPA: + log_error("capa option not implemented"); + exit(1); + break; + + case CMD_OPT_VERSION: + print_version("mts-id-eeprom"); + exit(0); + break; + + case CMD_OPT_HELP: + usage(stdout); + exit(0); + break; + + default: + usage(stderr); + exit(1); + } + } + + + 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); + 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; +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..d53b795 --- /dev/null +++ b/src/log.h @@ -0,0 +1,47 @@ +#ifndef __LOG_H +#define __LOG_H + +#include "config.h" + +#if CONFIG_USE_SYSLOG +# include +# define LOG_FACILITY LOG_USER + +# define __log(level, name, format, args...) \ + syslog(LOG_FACILITY | level, "[" name "] %s:%s:%d: " format "\n" , \ + __FILE__ , __func__ , __LINE__ , ## args) +#else +# define __log(level, name, format, args...) \ + fprintf(stderr, "[" name "] %s:%s:%d: " format "\n" , \ + __FILE__ , __func__ , __LINE__ , ## args) +#endif + +#define log_emerg(format, args...) __log(LOG_EMERG, "EMERG", format , ## args) +#define log_alert(format, args...) __log(LOG_ALERT, "ALERT", format , ## args) +#define log_crit(format, args...) __log(LOG_CRIT, "CRIT", format , ## args) +#define log_error(format, args...) __log(LOG_ERR, "ERROR", format , ## args) +#define log_warning(format, args...) __log(LOG_WARNING, "WARNING", format , ## args) +#define log_notice(format, args...) __log(LOG_NOTICE, "NOTICE", format , ## args) +#define log_info(format, args...) __log(LOG_INFO, "INFO", format , ## args) +#if DEBUG +# define log_debug(format, args...) __log(LOG_DEBUG, "DEBUG", format , ## args) +#else +# define log_debug(format, args...) do {} while (0) +#endif + +#if DEBUG +# define debug_buffer(msg, buf, len) \ +do { \ + __log(LOG_DEBUG, "DEBUG", msg " \"%.*J\"", len, buf); \ +} while (0) +#else +# define debug_buffer(_msg, _buf, _len) do { } while (0) +#endif + +#define BUG(format, args...) \ +do { \ + log_emerg("BUG: " format , ## args); \ + exit(124); \ +} while (0) + +#endif /* ~__LOG_H */ diff --git a/src/mts_io.h b/src/mts_io.h new file mode 100644 index 0000000..fa84e53 --- /dev/null +++ b/src/mts_io.h @@ -0,0 +1,52 @@ + +#ifndef __MTS_IO_H +#define __MTS_IO_H + +#include + +#ifndef BIT +#define BIT(nr) (1UL << (nr)) +#endif + +#define VENDOR_ID_MULTITECH "Multi-Tech Systems" +#define PRODUCT_ID_MTCDP_E1_DK "MTCDP-E1-DK" + +#define HW_VERSION_MTCBA2_2_0 "MTCBA2-2.0" +#define HW_VERSION_MTCDP_0_0 "MTCDP-0.0" +#define HW_VERSION_MTCDP_1_0 "MTCDP-1.0" + +#define DEVICE_CAPA_INDEX(c) (((c) & 0xFF) >> 3) +#define DEVICE_CAPA_MASK(c) BIT((c) & 0x07) + +#define DEVICE_CAPA(capa_buf, c) ((capa_buf)[DEVICE_CAPA_INDEX(c)] & DEVICE_CAPA_MASK(c)) + +#define DEVICE_CAPA_SET(capa_buf, c) \ +do { \ + (capa_buf)[DEVICE_CAPA_INDEX(c)] |= DEVICE_CAPA_MASK(c); \ +}while (0) + +#define DEVICE_CAPA_CLEAR(capa_buf, c) \ +do { \ + (capa_buf)[DEVICE_CAPA_INDEX(c)] &= ~DEVICE_CAPA_MASK(c); \ +} while (0) + +#define DEVICE_CAPA_VALUE(index, bit) ((((index) & 0x1F) << 3) | ((bit) & 0x07)) + +#define CAPA_GPS DEVICE_CAPA_VALUE(0, 7) +#define CAPA_DIN DEVICE_CAPA_VALUE(0, 6) +#define CAPA_DOUT DEVICE_CAPA_VALUE(0, 5) +#define CAPA_ADC DEVICE_CAPA_VALUE(0, 4) + +struct mts_id_eeprom_layout { + char vendor_id[32]; + char product_id[32]; + char device_id[32]; + char hw_version[32]; + uint8_t mac_addr[6]; + char imei[32]; + uint8_t capa[32]; + uint8_t reserved[314]; +}; + +#endif /* ~__MTS_IO_H */ + -- cgit v1.2.3