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"
44 #include "vsleeptimer.h"
46 void sighandler(int signalReceived);
49 void shutdown(int code);
53 int ticonfig_main(int, char**);
56 // Global variables --------------------------------------------------------------------------------------------------
69 Sleeptimer* sleeptimer;
71 // Linux MVP main function and sighandler
73 int main(int argc, char** argv)
75 if (strstr(argv[0], "ticonfig")) return ticonfig_main(argc, argv);
77 bool daemonize = true;
78 bool debugEnabled = false;
80 char* setServer = NULL;
83 while ((c = getopt(argc, argv, "cdns:")) != -1)
91 debugEnabled = true; // and...
99 printf("Unknown option\n");
102 printf("Option error\n");
107 // Init global vars ------------------------------------------------------------------------------------------------
110 timers = new Timers();
113 remote = new RemoteMVP();
116 audio = new AudioMVP();
117 video = new VideoMVP();
118 boxstack = new BoxStack();
119 command = new Command();
121 sleeptimer = new Sleeptimer();
123 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
125 printf("Could not create objects. Memory problems?\n");
129 // Get logging module started --------------------------------------------------------------------------------------
131 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
133 printf("Could not initialise log object. Aborting.\n");
137 logger->log("Core", Log::INFO, "Starting up...");
139 // Daemonize if not -d
144 pid_t forkTest = fork();
146 { printf("Cannot fork (1).\n"); exit(1); }
147 if (forkTest != 0) _exit(0); // PID returned, I am the parent
148 // otherwise, I am the child
152 { printf("Cannot fork (2).\n"); exit(1); }
153 if (forkTest != 0) _exit(0); // PID returned, I am the parent
154 // otherwise, I am the child
160 // Set up signal handling ------------------------------------------------------------------------------------------
162 sighandler_t sigtest;
164 sigtest = signal(SIGPIPE, SIG_IGN);
165 if (sigtest == SIG_ERR)
167 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
170 sigtest = signal(SIGINT, sighandler);
171 if (sigtest == SIG_ERR)
173 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
176 sigtest = signal(SIGTERM, sighandler);
177 if (sigtest == SIG_ERR)
179 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
182 sigtest = signal(SIGUSR1, sighandler);
183 if (sigtest == SIG_ERR)
185 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
189 sigtest = signal(SIGUSR2, sighandler);
190 if (sigtest == SIG_ERR)
192 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
196 sigtest = signal(SIGURG, sighandler);
197 if (sigtest == SIG_ERR)
199 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
203 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
206 // Init modules ----------------------------------------------------------------------------------------------------
209 success = remote->init("/dev/rawir");
212 logger->log("Core", Log::INFO, "Remote module initialised");
216 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
220 success = led->init(((RemoteMVP*)remote)->getDevice());
223 logger->log("Core", Log::INFO, "LED module initialised");
227 logger->log("Core", Log::EMERG, "LED module failed to initialise");
231 success = mtd->init();
234 logger->log("Core", Log::INFO, "Mtd module initialised");
238 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
242 success = timers->init();
245 logger->log("Core", Log::INFO, "Timers module initialised");
249 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
253 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
254 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
255 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
256 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
258 success = video->init(videoFormat);
261 logger->log("Core", Log::INFO, "Video module initialised");
265 logger->log("Core", Log::EMERG, "Video module failed to initialise");
269 success = osd->init((void*)("/dev/stbgfx"));
272 logger->log("Core", Log::INFO, "OSD module initialised");
276 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
280 success = audio->init(Audio::MPEG2_PES);
283 logger->log("Core", Log::INFO, "Audio module initialised");
287 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
291 success = vdr->init(3024);
294 logger->log("Core", Log::INFO, "VDR module initialised");
298 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
302 success = boxstack->init();
305 logger->log("Core", Log::INFO, "BoxStack module initialised");
309 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
313 success = command->init(crashed, setServer);
316 logger->log("Core", Log::INFO, "Command module initialised");
320 logger->log("Core", Log::EMERG, "Command module failed to initialise");
324 // Other init ------------------------------------------------------------------------------------------------------
326 logger->log("Core", Log::NOTICE, "Startup successful");
328 // Run main loop ---------------------------------------------------------------------------------------------------
330 // Ok, all major device components and other bits are loaded and ready
333 // When that returns quit ------------------------------------------------------------------------------------------
339 // -------------------------------------------------------------------------------------------------------------------
341 void sighandler(int signalReceived)
343 logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived);
345 switch (signalReceived)
349 logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
350 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
355 logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
356 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
367 logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
368 logger->upLogLevel();
373 logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
374 logger->downLogLevel();
380 logger->log("Core", Log::DEBUG, "SIGURG caught");
387 // -------------------------------------------------------------------------------------------------------------------
389 void shutdown(int code)
393 boxstack->shutdown();
395 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
398 // FIXME, send a del all to boxstack first, then get rid of it after command?
399 if (command) // shut down command here in case views have posted messages
403 logger->log("Core", Log::NOTICE, "Command module shut down");
410 logger->log("Core", Log::NOTICE, "VDR module shut down");
417 logger->log("Core", Log::NOTICE, "OSD module shut down");
424 logger->log("Core", Log::NOTICE, "Audio module shut down");
431 logger->log("Core", Log::NOTICE, "Video module shut down");
438 logger->log("Core", Log::NOTICE, "Timers module shut down");
445 logger->log("Core", Log::NOTICE, "MTD module shut down");
452 logger->log("Core", Log::NOTICE, "LED module shut down");
459 logger->log("Core", Log::NOTICE, "Remote module shut down");
465 logger->log("Core", Log::NOTICE, "WOL module shut down");
471 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
476 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
484 // -------------------------------------------------------------------------------------------------------------------
486 ULLONG ntohll(ULLONG a)
491 ULLONG htonll(ULLONG a)
493 #if BYTE_ORDER == BIG_ENDIAN
498 b = ((a << 56) & 0xFF00000000000000ULL)
499 | ((a << 40) & 0x00FF000000000000ULL)
500 | ((a << 24) & 0x0000FF0000000000ULL)
501 | ((a << 8) & 0x000000FF00000000ULL)
502 | ((a >> 8) & 0x00000000FF000000ULL)
503 | ((a >> 24) & 0x0000000000FF0000ULL)
504 | ((a >> 40) & 0x000000000000FF00ULL)
505 | ((a >> 56) & 0x00000000000000FFULL) ;
511 void MILLISLEEP(ULONG a)
514 struct timespec delayTime;
515 delayTime.tv_sec = a / 1000;
516 delayTime.tv_nsec = (a % 1000) * 1000000;
517 nanosleep(&delayTime, NULL);
523 int min(UINT a, int b)
529 int max(int a, int b)