--- /dev/null
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include <libgen.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <trbnet.h>
+#include <trberror.h>
+
+static const char trbdhcp_version[] = "$Revision: 1.1 $";
+
+typedef struct {
+ uint16_t address;
+ uint64_t uid;
+ uint8_t endPoint;
+} Trb_Endpoint;
+
+static Trb_Endpoint* endPointList = NULL;
+static unsigned int listSize = 0;
+static uint32_t* trbBuffer = NULL;
+
+#define NUM_ENDPOINTS 2048
+
+static int readConfigFile(const char* fileName)
+{
+ FILE* configFile = NULL;
+ char *line = NULL;
+ int lineLen = 0;
+ unsigned int lineCtr = 0;
+ unsigned int counter = 0;
+ uint32_t address;
+ uint64_t uid;
+ uint32_t endPoint;
+
+ listSize = 0;
+
+ if (endPointList == NULL) return -1;
+
+ /* Open scriptFile if requested */
+ configFile = fopen(fileName, "r");
+ if (configFile == NULL) {
+ fprintf(stderr, "Error opening configFile '%s': %s\n",
+ fileName, strerror(errno));
+ return -1;
+ }
+
+ while(1) {
+ char *c = NULL;
+ lineCtr++;
+ if (getline(&line, &lineLen, configFile) == -1) {
+ if (feof(configFile) != 0) {
+ /* EOF reached */
+ break;
+ } else {
+ /* Error reading line */
+ fprintf(stderr, "Error reading configFile '%s' line %d\n",
+ fileName, lineCtr);
+ return -1;
+ }
+ }
+
+ /* Remove newline and comments */
+ if ((c = strchr(line, '\n')) != NULL) {
+ *c = '\0';
+ }
+ if ((c = strchr(line, '#')) != NULL) {
+ *c = '\0';
+ }
+
+ if (sscanf(line, "%x %llx %x", &address, &uid, &endPoint) != 3) {
+ continue;
+ }
+ endPointList[counter].address = address;
+ endPointList[counter].uid = uid;
+ endPointList[counter].endPoint = endPoint;
+
+ counter++;
+ }
+
+ free(line);
+
+ listSize = counter;
+
+ return counter;
+}
+
+static void dumpList()
+{
+ unsigned int i;
+ for (i = 0; i < listSize; i++) {
+ fprintf(stdout, "Entry #%d: 0x%04x 0x%08llx 0x%02x\n", i,
+ endPointList[i].address,
+ endPointList[i].uid,
+ endPointList[i].endPoint);
+ }
+}
+
+/* ------ MAIN ---------------------------------------------------------- */
+
+void usage(const char *progName)
+{
+ printf("Usage: %s [-h] [-f configFile] [-V]\n", progName);
+ printf("Options:\n");
+ printf(" -h give this help\n");
+ printf(" -f name of configFile (default: trbdhcp.conf)\n");
+ printf(" -a trb-address (default: 0xffff)\n");
+ printf(" -V Version number\n");
+ printf("\nConfigFile Format:\n");
+ printf("# New-Address UID EndPoint\n\n");
+}
+
+int main(int argc, char ** argv)
+{
+ char configFileName[256] = "trbdhcp.conf";
+ uint16_t trbAddress = 0xffff;
+ int status;
+ unsigned int i;
+ int resetAll = 0;
+ int opt;
+
+ trb_debug = 0;
+
+ /* Parse Arguments */
+ while ((opt = getopt(argc, argv, "+hd:f:a:rV")) != -1) {
+ switch (opt) {
+ case '?':
+ usage(basename(argv[0]));
+ exit(EXIT_FAILURE);
+ case 'h':
+ usage(basename(argv[0]));
+ exit(EXIT_SUCCESS);
+ case 'd':
+ trb_debug = strtoul(optarg, NULL, 0);
+ break;
+ case 'f':
+ strncpy(configFileName, optarg, 256);
+ break;
+ case 'a':
+ trbAddress = strtoul(optarg, NULL, 0);
+ break;
+ case 'r':
+ resetAll = 1;
+ break;
+ case 'V':
+ printf("%s %s, using libtrbnet %s\n",
+ basename(argv[0]), trbdhcp_version, trbnet_version);
+ exit(EXIT_SUCCESS);
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* Allocat Memory */
+ if ((endPointList = malloc(sizeof(Trb_Endpoint) * NUM_ENDPOINTS)) == NULL) {
+ abort();
+ }
+
+ if ((trbBuffer = malloc(sizeof(uint32_t) * 4 * NUM_ENDPOINTS)) == NULL) {
+ abort();
+ }
+
+ /* Read Config-File */
+ if (readConfigFile(configFileName) <= 0) {
+ exit(EXIT_FAILURE);
+ }
+ if (trb_debug > 0) {
+ dumpList();
+ }
+
+ /* Open ports */
+ init_ports();
+
+ /* Read present UIDs */
+ status = trb_read_uid(trbAddress, trbBuffer,
+ sizeof(uint32_t) * 4 * NUM_ENDPOINTS);
+ if (status == -1) {
+ trb_error("read_uid failed");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Assign addresses */
+ for (i = 0; i < status; i += 4) {
+ uint16_t currentAddress = 0;
+ uint64_t uid = 0;
+ uint8_t endPoint = 0;
+ unsigned int j;
+
+ uid = ((uint64_t)trbBuffer[i] << 32) | trbBuffer[i + 1];
+ endPoint = trbBuffer[i + 2];
+ currentAddress = trbBuffer[i + 3];
+
+ for (j = 0; j < listSize; j++) {
+ if ((uid == endPointList[j].uid) &&
+ (endPoint == endPointList[j].endPoint)) {
+
+ if ((currentAddress != endPointList[j].address) &&
+ (((currentAddress & 0xf000) == 0xf000) || (resetAll == 1))
+ ) {
+
+ if (trb_debug > 0) {
+ fprintf(stderr, "set address: 0x%04x 0x%08llx 0x%02x\n",
+ currentAddress, uid, endPoint);
+ }
+
+ /* Set new Address */
+ if (trb_set_address(uid, endPoint, endPointList[j].address) == -1) {
+ trb_error("Set address failed");
+ continue;
+ }
+ }
+ }
+ }
+ }
+
+ /* Cleanup */
+ free(endPointList);
+ free(trbBuffer);
+
+
+ exit(EXIT_SUCCESS);
+}