diff options
-rw-r--r-- | configure.in | 2 | ||||
-rw-r--r-- | src/eeprom_main.c | 233 |
2 files changed, 155 insertions, 80 deletions
diff --git a/configure.in b/configure.in index 39a67c4..1b9f1fb 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ AC_INIT([src/eeprom_main.c]) -AM_INIT_AUTOMAKE([mts-id-eeprom], [0.4.3]) +AM_INIT_AUTOMAKE([mts-id-eeprom], [0.4.5]) AM_CONFIG_HEADER([config.h]) AC_PROG_CC diff --git a/src/eeprom_main.c b/src/eeprom_main.c index 5446750..27aede3 100644 --- a/src/eeprom_main.c +++ b/src/eeprom_main.c @@ -297,29 +297,21 @@ static int mtcdt3b_yaml_out(const char *name, struct mtcdt3b_eeprom_layout *mtcd mtcdt3b_eeprom->mac_addr[4], mtcdt3b_eeprom->mac_addr[5]); fprintf(file, "capa-lora: %s\n", DEVICE_CAPA(mtcdt3b_eeprom->capa, MTCDT3B_CAPA_LORA) ? "true" : "false"); - fprintf(file, "lora-device-id: \"%.32s\"\n", mtcdt3b_eeprom->lora_device_id); - fprintf(file, "lora-eui: \"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\"\n", - mtcdt3b_eeprom->lora_eui[0], - mtcdt3b_eeprom->lora_eui[1], - mtcdt3b_eeprom->lora_eui[2], - mtcdt3b_eeprom->lora_eui[3], - mtcdt3b_eeprom->lora_eui[4], - mtcdt3b_eeprom->lora_eui[5], - mtcdt3b_eeprom->lora_eui[6], - mtcdt3b_eeprom->lora_eui[7]); - fprintf(file, "lora-device-id2: \"%.32s\"\n", mtcdt3b_eeprom->lora_device_id2); - fprintf(file, "lora-eui2: \"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\"\n", - mtcdt3b_eeprom->lora_eui2[0], - mtcdt3b_eeprom->lora_eui2[1], - mtcdt3b_eeprom->lora_eui2[2], - mtcdt3b_eeprom->lora_eui2[3], - mtcdt3b_eeprom->lora_eui2[4], - mtcdt3b_eeprom->lora_eui2[5], - mtcdt3b_eeprom->lora_eui2[6], - mtcdt3b_eeprom->lora_eui2[7]); fprintf(file, "lora-product-id: \"%.32s\"\n", mtcdt3b_eeprom->lora_product_id); fprintf(file, "lora-hw-version: \"%.32s\"\n", mtcdt3b_eeprom->lora_hw_version); - + for(i=0;i<2;i++) { + fprintf(file,"slot %d:\n",i); + fprintf(file, " lora-device-id: \"%.32s\"\n", mtcdt3b_eeprom->slot[i].device_id); + fprintf(file, " lora-eui: \"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\"\n", + mtcdt3b_eeprom->slot[i].lora_eui[0], + mtcdt3b_eeprom->slot[i].lora_eui[1], + mtcdt3b_eeprom->slot[i].lora_eui[2], + mtcdt3b_eeprom->slot[i].lora_eui[3], + mtcdt3b_eeprom->slot[i].lora_eui[4], + mtcdt3b_eeprom->slot[i].lora_eui[5], + mtcdt3b_eeprom->slot[i].lora_eui[6], + mtcdt3b_eeprom->slot[i].lora_eui[7]); + } fprintf(file, "...\n"); fclose(file); @@ -464,7 +456,7 @@ static void mtcdt3b_eeprom_inspect(struct mtcdt3b_eeprom_layout *mtcdt3b_eeprom) } log_info("uuid: %s", (char*)buf); - log_info("mac-addr: \"%02X:%02X:%02X:%02X:%02X:%02X\"\n", + log_info("mac-addr: \"%02X:%02X:%02X:%02X:%02X:%02X\"", mtcdt3b_eeprom->mac_addr[0], mtcdt3b_eeprom->mac_addr[1], mtcdt3b_eeprom->mac_addr[2], @@ -472,28 +464,21 @@ static void mtcdt3b_eeprom_inspect(struct mtcdt3b_eeprom_layout *mtcdt3b_eeprom) mtcdt3b_eeprom->mac_addr[4], mtcdt3b_eeprom->mac_addr[5]); log_info("mtcdt3b-capa-lora: %s", DEVICE_CAPA(mtcdt3b_eeprom->capa, MTCDT3B_CAPA_LORA) ? "yes" : "no"); - log_info("lora-device-id: %.32s", mtcdt3b_eeprom->lora_device_id); - log_info("lora-eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", - mtcdt3b_eeprom->lora_eui[0], - mtcdt3b_eeprom->lora_eui[1], - mtcdt3b_eeprom->lora_eui[2], - mtcdt3b_eeprom->lora_eui[3], - mtcdt3b_eeprom->lora_eui[4], - mtcdt3b_eeprom->lora_eui[5], - mtcdt3b_eeprom->lora_eui[6], - mtcdt3b_eeprom->lora_eui[7]); - log_info("lora-device-id-2: %.32s", mtcdt3b_eeprom->lora_device_id2); - log_info("lora-eui2: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", - mtcdt3b_eeprom->lora_eui2[0], - mtcdt3b_eeprom->lora_eui2[1], - mtcdt3b_eeprom->lora_eui2[2], - mtcdt3b_eeprom->lora_eui2[3], - mtcdt3b_eeprom->lora_eui2[4], - mtcdt3b_eeprom->lora_eui2[5], - mtcdt3b_eeprom->lora_eui2[6], - mtcdt3b_eeprom->lora_eui2[7]); log_info("lora-product-id: %.32s", mtcdt3b_eeprom->lora_product_id); log_info("lora-hw-version: %.32s", mtcdt3b_eeprom->lora_hw_version); + for(i=0;i<2;i++) { + log_info("slot %d:",i); + log_info(" lora-device-id: \"%.32s\"", mtcdt3b_eeprom->slot[i].device_id); + log_info(" lora-eui: \"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\"", + mtcdt3b_eeprom->slot[i].lora_eui[0], + mtcdt3b_eeprom->slot[i].lora_eui[1], + mtcdt3b_eeprom->slot[i].lora_eui[2], + mtcdt3b_eeprom->slot[i].lora_eui[3], + mtcdt3b_eeprom->slot[i].lora_eui[4], + mtcdt3b_eeprom->slot[i].lora_eui[5], + mtcdt3b_eeprom->slot[i].lora_eui[6], + mtcdt3b_eeprom->slot[i].lora_eui[7]); + } } #endif @@ -520,11 +505,6 @@ static void usage(FILE *out) { fprintf(out, " --mac-bluetooth <mac-addr> |\n"); fprintf(out, " --mac-wifi <mac-addr> |\n"); fprintf(out, " --lora-eui <EUI-64> |\n"); -#ifdef MTCDT3B - fprintf(out, " --lora-eui2 <EUI-64> |\n"); - fprintf(out, " --lora-device-id <lora-device-id> |\n"); - fprintf(out, " --lora-device-id2 <lora-device-id2> |\n"); -#endif fprintf(out, " --lora-hw-version <hw-version> |\n"); fprintf(out, " --lora-product-id <product-id> |\n"); fprintf(out, " --imei <imei> |\n"); @@ -543,6 +523,7 @@ static void usage(FILE *out) { fprintf(out, " --accessory-card\n"); #ifdef MTCDT3B fprintf(out, " --base-board\n"); + fprintf(out, " --slot i=0,device-id=<device-id>,lora-eui=<EUI-64>,i=1,...\n"); #endif fprintf(out, " }\n"); fprintf(out, "\n"); @@ -577,9 +558,7 @@ enum { CMD_OPT_ACCESSORY_CARD, #ifdef MTCDT3B CMD_OPT_BASE_BOARD, - CMD_OPT_LORA_DEVICE_ID, - CMD_OPT_LORA_DEVICE_ID2, - CMD_OPT_LORA_EUI2, + CMD_OPT_SLOTS, #endif CMD_OPT_VERSION, CMD_OPT_HELP, @@ -617,22 +596,33 @@ static struct option long_options[] = { {"accessory-card", 0, NULL, CMD_OPT_ACCESSORY_CARD}, #ifdef MTCDT3B {"base-board", 0, NULL, CMD_OPT_BASE_BOARD}, - {"device-id", 1, NULL, CMD_OPT_DEVICE_ID}, - {"lora-device-id", 1, NULL, CMD_OPT_LORA_DEVICE_ID}, - {"lora-device-id2", 1, NULL, CMD_OPT_LORA_DEVICE_ID2}, - {"lora-eui2", 1, NULL, CMD_OPT_LORA_EUI2}, + {"slots", 1, NULL, CMD_OPT_SLOTS}, #endif {"version", 0, NULL, CMD_OPT_VERSION}, {"help", 0, NULL, CMD_OPT_HELP}, {0, 0, 0, 0}, }; +enum { + SLOT_INDEX = 0, + SLOT_DEVICE_ID, + SLOT_LORA_EUI, +}; + +char *slot_tokens[] = { + [SLOT_INDEX] = "i", + [SLOT_DEVICE_ID] = "device-id", + [SLOT_LORA_EUI] = "lora-eui", + NULL +}; + int main(int argc, char *argv[]) { int i; int option_index; int tmp; int fd; - + int length; + int init; char *in_file = NULL; char *out_file = "-"; char *out_format = "yaml"; @@ -640,9 +630,7 @@ int main(int argc, char *argv[]) { int accessory_card = 0; #ifdef MTCDT3B int base_board = 0; - char *lora_eui2 = NULL; - char *lora_device_id = NULL; - char *lora_device_id2 = NULL; + char *slots = NULL; #endif char *vendor_id = NULL; char *product_id = NULL; @@ -764,17 +752,33 @@ int main(int argc, char *argv[]) { break; #ifdef MTCDT3B - case CMD_OPT_BASE_BOARD: + case CMD_OPT_BASE_BOARD: // handled above break; - case CMD_OPT_LORA_EUI2: - lora_eui2 = optarg; - break; - case CMD_OPT_LORA_DEVICE_ID: - lora_device_id = optarg; - break; - case CMD_OPT_LORA_DEVICE_ID2: - lora_device_id2 = optarg; + case CMD_OPT_SLOTS: + // concatenate slots arguments + if (base_board) { + length = 0; + init = 1; + if (slots) { + length = strlen(slots) + 1; + init = 0; // Already initialized + } + + if (optarg) + length += strlen(optarg) + 1; + slots = realloc(slots,length); + if (init) + slots[0] = 0; + else + strcat(slots,","); + + slots = strcat(slots,optarg); + } else { + log_error("Only the base board can have slots"); + usage(stderr); + exit(1); + } break; #endif @@ -854,7 +858,7 @@ int main(int argc, char *argv[]) { break; case CMD_OPT_IMEI: - strncpy(id_eeprom.imei, optarg, sizeof(id_eeprom.imei) - 1); + strncpy(id_eeprom.imei, optarg, sizeof(id_eeprom.imei) - 1); break; case CMD_OPT_CAPA_GPS: @@ -1023,8 +1027,11 @@ int main(int argc, char *argv[]) { if (accessory_card) tmp = hwaddr_aton(lora_eui, ap_eeprom.eui, sizeof(ap_eeprom.eui)); #ifdef MTCDT3B - else if (base_board) - tmp = hwaddr_aton(lora_eui, mtcdt3b_eeprom.lora_eui, sizeof(mtcdt3b_eeprom.lora_eui)); + else if(base_board) { + log_error("--lora-eui option is not supported on base board eeprom"); + usage(stderr); + exit(1); + } #endif else tmp = hwaddr_aton(lora_eui, id_eeprom.lora_eui, sizeof(id_eeprom.lora_eui)); @@ -1036,16 +1043,84 @@ int main(int argc, char *argv[]) { } } + #ifdef MTCDT3B - if (lora_eui2) - if (base_board) - tmp = hwaddr_aton(lora_eui2, mtcdt3b_eeprom.lora_eui2, sizeof(mtcdt3b_eeprom.lora_eui2)); - if (lora_device_id) - if (base_board) - EEPROM_SET(mtcdt3b_eeprom,lora_device_id); - if (lora_device_id2) - if (base_board) - EEPROM_SET(mtcdt3b_eeprom,lora_device_id2); +#define EEPROM_SETP(eeprom,field,value) (strncpy(eeprom->field, value, sizeof(eeprom->field) - 1)); + if (slots) { // Populate slots on main board + int slot_index = -1; + char *value; + char *saved; + struct lora_slot *sp; + int slot_count, numtoken; + + slot_count = sizeof mtcdt3b_eeprom.slot / sizeof mtcdt3b_eeprom.slot[0]; + + while(1) { + char *saved = slots; + + numtoken=getsubopt(&slots,slot_tokens,&value); + if (numtoken == -1 && *slots == 0) + break; // Out of tokens + switch(numtoken) { + case SLOT_INDEX: + if (value == NULL) { + log_error("Missing have index value (i=NUM) for slot"); + usage(stderr); + exit(1); + } + slot_index = atoi(value); + if((slot_index < 0) || (slot_index+1 > slot_count)) { + log_error("Slot index: %s is not >=0 or < %d",value,slot_count); + usage(stderr); + exit(1); + } + break; + case SLOT_LORA_EUI: + if(slot_index == -1) { + log_error("Slot index (i=NUM) is not set before setting %s",slot_tokens[numtoken]); + usage(stderr); + exit(1); + } + sp = &(mtcdt3b_eeprom.slot[slot_index]); + if(sp->lora_eui[0]) { + log_error("Slot index %d already has %s set",slot_index,slot_tokens[numtoken]); + usage(stderr); + exit(1); + } + if (value == NULL) { + log_error("Must value %s=? for slot %d",slot_tokens[numtoken],slot_index); + usage(stderr); + exit(1); + } + EEPROM_SETP(sp,lora_eui,value) + break; + case SLOT_DEVICE_ID: + if(slot_index == -1) { + log_error("SLOT_DEVICE_ID: %d, numtoken=%d, %s",SLOT_DEVICE_ID,numtoken,slot_tokens[numtoken]); + log_error("Slot index (i=NUM) is not set before setting %s",slot_tokens[numtoken]); + usage(stderr); + exit(1); + } + sp = &(mtcdt3b_eeprom.slot[slot_index]); + if(sp->device_id[0]) { + log_error("Slot index %d already has %s set",slot_index,slot_tokens[numtoken]); + usage(stderr); + exit(1); + } + if (value == NULL) { + log_error("Must have value %s=? for slot %d",slot_tokens[numtoken],slot_index); + usage(stderr); + exit(1); + } + EEPROM_SETP(sp,device_id,value) + break; + default: + log_error("Unknown suboption %s",saved); + usage(stderr); + exit(1); + } // END switch on slot suboptions + } // END while more slots + } // END Populate slots on main board #endif // updating eeprom in place, force bin format |