int main(int argc, char **argv) { close_fds(); daemon(0, 0); open_log_file(); get_lock(); create_pipe(); int serial_fd = open_serial_port(); write (serial_fd, "$$$ALL,OFF\r", 11); usleep(200000); write (serial_fd, "$$$SPEED80\r", 11); //printf("STARTED - awaiting connections\n"); int listen_fd = listen_network(); char msg[256]; int msg_len; while (1) { msg_len = get_msg(listen_fd, msg, sizeof(msg) - 1); msg[msg_len] = '\0'; //printf("Msg:|%s|\n", msg); int batch; char *ptr = msg; while(msg_len) { batch = (msg_len > MAX_BATCH_SIZE ? MAX_BATCH_SIZE : msg_len); char c; int i, led_width = 0; for (i = 0; i < batch; i++) { c = *(ptr + i); if (c == 10) { led_width += 4; *(ptr + i) = ' '; } else if (c < 32) led_width += 0; else if (c > 128) led_width += 0; else if (c == 128) // Big empty square. led_width += 8; else if (c == 176) // temp symbol. led_width += 2; else if (strchr("ABCDEFGHKLMNOPQRSTUVWXYZmvwxz", c)) led_width += 6; else if (strchr("abcdefghknopqrsuyJ<>", c)) led_width += 5; else if (strchr(" 1I\"()jt[]`", c)) led_width += 4; else if (strchr(",.:;", c)) led_width += 3; else if (strchr("!il'", c)) led_width += 2; else { led_width += 6; } } write(serial_fd, ptr, batch); msg_len -= batch; ptr += batch; assert(msg_len >= 0); assert(ptr <= msg + sizeof(msg)); usleep(LED_HOLD_TIME * led_width); } } // Pause the scroll. //write(serial_fd, "$$$", 3); //write(serial_fd, "\r\r", 2); exit(0); }
int main(int argc, char *argv[]) { /* getopt */ int next_option; int ret; int will_block = 0; /*! short options. See man getopt */ const char *const short_options = "hc:l:L:b:f:xp:s:"; /*! the array for the long options. */ const struct option long_options[] = { {"help", 0, NULL, 'h'}, {"config", 1, NULL, 'c'}, {"logfile", 1, NULL, 'L'}, {"background", 1, NULL, 'b'}, {"foreground", 1, NULL, 'f'}, {"logo", 1, NULL, 'l'}, {"blocked", 0, NULL, 'x'}, {"port", 1, NULL, 'p'}, {"server", 1, NULL, 's'}, {NULL, 0, NULL, 0} }; /* Initializing exit function */ if (atexit(f_atexit) != 0) { fprintf(stderr, "failed to register f_atexit() exit function\n"); exit(EXIT_FAILURE); } if (signal(SIGINT, sig_ctrlc) == SIG_ERR) { fprintf(stderr, "Warning - Could not set CTRL-C handler\n"); } /* First, set defaults */ set_listen_port(PORT); set_foreground(FOREGROUND_IMAGE); set_logo(LOGO_IMAGE); /* Second, read entries from default config file. * They will eventually be replaced by command line settings. */ read_config_file(CONFIGFILE); /* Then, overwrite with command line options... */ do { next_option = getopt_long(argc, argv, short_options, long_options, NULL); switch (next_option) { case 'h': /* -h or --help */ print_usage(); exit(1); case 'c':/* -c or --config */ /* Things like "mdclient -p 7000 -c /etc/mymedusa.conf" * when port is set to a different value in /etc/mymedusa.conf * may result in an unpredictable value for listen_port. Just don't do that. */ read_config_file(strdup(optarg)); break; case 'p': /* -p or --port */ set_listen_port(strdup(optarg)); break; case 'x': /* -x or --blocked */ will_block = 1; break; case 'b': /* -b or --background */ set_background(strdup(optarg)); break; case 'l': /* -l or --logo */ set_logo(strdup(optarg)); break; case 'f': /* -f or --foreground */ set_foreground(strdup(optarg)); break; case 'L': /* -L or --logfile */ close_log(); open_log(strdup(optarg)); break; case 's': /* -s or --server */ ret = set_server(strdup(optarg)); if (ret) { /* error */ fflush(stderr); fflush(stdout); fprintf(stderr, "Server IP unknown.\n"); exit(1); } break; case '?':/* The user specified an invalid option */ /* Write usage info on the screen */ print_usage(); exit(1); case -1: /* No more options */ break; default: /* Garbage */ abort(); } } while (next_option != -1); if (will_block) { if (lockScreen() < 0) { write_log_fmt("Could not open display.\n"); exit(EXIT_FAILURE); } } write_log_fmt("Starting...\n"); listen_network(); close_log(); return 0; }