2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 #include "remotemvp.h"
46 void sighandler(int signalReceived);
49 void shutdown(int code);
53 int ticonfig_main(int, char**);
56 // Global variables --------------------------------------------------------------------------------------------------
70 // Linux MVP main function and sighandler
72 int main(int argc, char** argv)
74 if (strstr(argv[0], "ticonfig")) return ticonfig_main(argc, argv);
76 bool daemonize = true;
77 bool debugEnabled = false;
79 char* setServer = NULL;
82 while ((c = getopt(argc, argv, "cdns:")) != -1)
90 debugEnabled = true; // and...
98 printf("Unknown option\n");
101 printf("Option error\n");
106 // Init global vars ------------------------------------------------------------------------------------------------
109 timers = new Timers();
112 remote = new RemoteMVP();
115 audio = new AudioMVP();
116 video = new VideoMVP();
117 boxstack = new BoxStack();
118 command = new Command();
121 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command || !wol)
123 printf("Could not create objects. Memory problems?\n");
127 // Get logging module started --------------------------------------------------------------------------------------
129 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
131 printf("Could not initialise log object. Aborting.\n");
135 logger->log("Core", Log::INFO, "Starting up...");
137 // Daemonize if not -d
142 pid_t forkTest = fork();
144 { printf("Cannot fork (1).\n"); exit(1); }
145 if (forkTest != 0) _exit(0); // PID returned, I am the parent
146 // otherwise, I am the child
150 { printf("Cannot fork (2).\n"); exit(1); }
151 if (forkTest != 0) _exit(0); // PID returned, I am the parent
152 // otherwise, I am the child
158 // Set up signal handling ------------------------------------------------------------------------------------------
160 sighandler_t sigtest;
162 sigtest = signal(SIGPIPE, SIG_IGN);
163 if (sigtest == SIG_ERR)
165 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
168 sigtest = signal(SIGINT, sighandler);
169 if (sigtest == SIG_ERR)
171 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
174 sigtest = signal(SIGTERM, sighandler);
175 if (sigtest == SIG_ERR)
177 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
180 sigtest = signal(SIGUSR1, sighandler);
181 if (sigtest == SIG_ERR)
183 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
187 sigtest = signal(SIGUSR2, sighandler);
188 if (sigtest == SIG_ERR)
190 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
194 sigtest = signal(SIGURG, sighandler);
195 if (sigtest == SIG_ERR)
197 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
201 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
204 // Init modules ----------------------------------------------------------------------------------------------------
207 success = remote->init("/dev/rawir");
210 logger->log("Core", Log::INFO, "Remote module initialised");
214 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
218 success = led->init(((RemoteMVP*)remote)->getDevice());
221 logger->log("Core", Log::INFO, "LED module initialised");
225 logger->log("Core", Log::EMERG, "LED module failed to initialise");
229 success = mtd->init();
232 logger->log("Core", Log::INFO, "Mtd module initialised");
236 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
240 success = timers->init();
243 logger->log("Core", Log::INFO, "Timers module initialised");
247 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
251 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
252 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
253 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
254 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
256 success = video->init(videoFormat);
259 logger->log("Core", Log::INFO, "Video module initialised");
263 logger->log("Core", Log::EMERG, "Video module failed to initialise");
267 success = osd->init((void*)("/dev/stbgfx"));
270 logger->log("Core", Log::INFO, "OSD module initialised");
274 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
278 success = audio->init(Audio::MPEG2_PES);
281 logger->log("Core", Log::INFO, "Audio module initialised");
285 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
289 success = vdr->init(3024);
292 logger->log("Core", Log::INFO, "VDR module initialised");
296 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
300 success = boxstack->init();
303 logger->log("Core", Log::INFO, "BoxStack module initialised");
307 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
311 success = command->init(crashed, setServer);
314 logger->log("Core", Log::INFO, "Command module initialised");
318 logger->log("Core", Log::EMERG, "Command module failed to initialise");
322 // Other init ------------------------------------------------------------------------------------------------------
324 logger->log("Core", Log::NOTICE, "Startup successful");
326 // Run main loop ---------------------------------------------------------------------------------------------------
328 // Ok, all major device components and other bits are loaded and ready
331 // When that returns quit ------------------------------------------------------------------------------------------
337 // -------------------------------------------------------------------------------------------------------------------
339 void sighandler(int signalReceived)
341 logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived);
343 switch (signalReceived)
347 logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
348 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
353 logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
354 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
365 logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
366 logger->upLogLevel();
371 logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
372 logger->downLogLevel();
378 logger->log("Core", Log::DEBUG, "SIGURG caught");
385 // -------------------------------------------------------------------------------------------------------------------
387 void shutdown(int code)
391 boxstack->shutdown();
393 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
396 // FIXME, send a del all to boxstack first, then get rid of it after command?
397 if (command) // shut down command here in case views have posted messages
401 logger->log("Core", Log::NOTICE, "Command module shut down");
408 logger->log("Core", Log::NOTICE, "VDR module shut down");
415 logger->log("Core", Log::NOTICE, "OSD module shut down");
422 logger->log("Core", Log::NOTICE, "Audio module shut down");
429 logger->log("Core", Log::NOTICE, "Video module shut down");
436 logger->log("Core", Log::NOTICE, "Timers module shut down");
443 logger->log("Core", Log::NOTICE, "MTD module shut down");
450 logger->log("Core", Log::NOTICE, "LED module shut down");
457 logger->log("Core", Log::NOTICE, "Remote module shut down");
463 logger->log("Core", Log::NOTICE, "WOL module shut down");
468 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
476 // -------------------------------------------------------------------------------------------------------------------
478 ULLONG ntohll(ULLONG a)
483 ULLONG htonll(ULLONG a)
485 #if BYTE_ORDER == BIG_ENDIAN
490 b = ((a << 56) & 0xFF00000000000000ULL)
491 | ((a << 40) & 0x00FF000000000000ULL)
492 | ((a << 24) & 0x0000FF0000000000ULL)
493 | ((a << 8) & 0x000000FF00000000ULL)
494 | ((a >> 8) & 0x00000000FF000000ULL)
495 | ((a >> 24) & 0x0000000000FF0000ULL)
496 | ((a >> 40) & 0x000000000000FF00ULL)
497 | ((a >> 56) & 0x00000000000000FFULL) ;
503 void MILLISLEEP(ULONG a)
506 struct timespec delayTime;
507 delayTime.tv_sec = a / 1000;
508 delayTime.tv_nsec = (a % 1000) * 1000000;
509 nanosleep(&delayTime, NULL);
515 int max(int a, int b)