+
+static int interactive_server(int flags)
+{
+ if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
+
+void hmlan_syntax(char *prog)
+{
+ fprintf(stderr, "Syntax: %s options\n\n", prog);
+ fprintf(stderr, "Possible options:\n");
+ fprintf(stderr, "\t-D\t\tdebug mode\n");
+ fprintf(stderr, "\t-d\t\tdaemon mode\n");
+ fprintf(stderr, "\t-h\t\tthis help\n");
+ fprintf(stderr, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
+ fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
+ fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
+ fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n");
+ fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
+ fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
+ fprintf(stderr, "\t-r n\t\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u if FW < 0.967, 0 otherwise)\n", DEFAULT_REBOOT_SECONDS);
+ fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
+ fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n");
+ fprintf(stderr, "\t-v\t\tverbose mode\n");
+ fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
+
+}
+
+int main(int argc, char **argv)
+{
+ int port = 1000;
+ char *iface = NULL;
+ int interactive = 0;
+ int flags = 0;
+ char *ep;
+ int opt;
+
+ while((opt = getopt(argc, argv, "DdhIiPp:Rr:l:L:S:vV")) != -1) {
+ switch (opt) {
+ case 'D':
+ debug = 1;
+ verbose = 1;
+ break;
+ case 'd':
+ flags |= FLAG_DAEMON;
+ break;
+ case 'I':
+ impersonate_hmlanif = 1;
+ break;
+ case 'i':
+ interactive = 1;
+ break;
+ case 'P':
+ flags |= FLAG_PID_FILE;
+ break;
+ case 'p':
+ port = strtoul(optarg, &ep, 10);
+ if (*ep != '\0') {
+ fprintf(stderr, "Can't parse port!\n");
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'R':
+ fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
+ break;
+ case 'r':
+ reboot_seconds = strtoul(optarg, &ep, 10);
+ if (*ep != '\0') {
+ if (*ep == ':') {
+ reboot_at_hour = reboot_seconds;
+ ep++;
+ reboot_at_minute = strtoul(ep, &ep, 10);
+ if (*ep != '\0') {
+ fprintf(stderr, "Can't parse reboot-time!\n");
+ exit(EXIT_FAILURE);
+ }
+
+ reboot_seconds = 0;
+ } else {
+ fprintf(stderr, "Can't parse reboot-timeout!\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ reboot_set = 1;
+ break;
+ case 'l':
+ iface = optarg;
+ break;
+ case 'L':
+ logfile = fopen(optarg, "a");
+ if (!logfile) {
+ perror("fopen(logfile)");
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'S':
+ serial = optarg;
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ case 'V':
+ printf("hmland " VERSION "\n");
+ printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
+ exit(EXIT_SUCCESS);
+ case 'h':
+ case ':':
+ case '?':
+ default:
+ hmlan_syntax(argv[0]);
+ exit(EXIT_FAILURE);
+ break;
+ }
+ }
+
+ if (interactive) {
+ return interactive_server(flags);
+ } else {
+ return socket_server(iface, port, flags);
+ }
+}