/*
* This file is part of the libCEC(R) library.
*
- * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
* libCEC(R) is an original work, containing original code.
*
* libCEC(R) is a trademark of Pulse-Eight Limited.
#include "../lib/platform/os.h"
#include "../lib/implementations/CECCommandHandler.h"
#include "../lib/platform/util/StdString.h"
+#include "../lib/platform/threads/threads.h"
using namespace CEC;
using namespace std;
#include "../../include/cecloader.h"
+static void PrintToStdOut(const char *strFormat, ...);
+
ICECCallbacks g_callbacks;
libcec_configuration g_config;
int g_cecLogLevel(-1);
bool g_bExit(false);
bool g_bHardExit(false);
CMutex g_outputMutex;
+ICECAdapter* g_parser;
-inline void PrintToStdOut(const char *strFormat, ...)
+class CReconnect : public PLATFORM::CThread
+{
+public:
+ static CReconnect& Get(void)
+ {
+ static CReconnect _instance;
+ return _instance;
+ }
+
+ virtual ~CReconnect(void) {}
+
+ void* Process(void)
+ {
+ if (g_parser)
+ {
+ g_parser->Close();
+ if (!g_parser->Open(g_strPort.c_str()))
+ {
+ PrintToStdOut("Failed to reconnect\n");
+ g_bExit = true;
+ }
+ }
+ return NULL;
+ }
+
+private:
+ CReconnect(void) {}
+};
+
+static void PrintToStdOut(const char *strFormat, ...)
{
CStdString strLog;
switch (type)
{
case CEC_ALERT_CONNECTION_LOST:
- PrintToStdOut("Connection lost - exiting\n");
- g_bExit = true;
+ if (!CReconnect::Get().IsRunning())
+ {
+ PrintToStdOut("Connection lost - trying to reconnect\n");
+ CReconnect::Get().CreateThread(false);
+ }
break;
default:
break;
void ListDevices(ICECAdapter *parser)
{
- cec_adapter *devices = new cec_adapter[10];
- int8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
+ cec_adapter_descriptor devices[10];
+ int8_t iDevicesFound = parser->DetectAdapters(devices, 10, NULL);
if (iDevicesFound <= 0)
{
PrintToStdOut("Found devices: NONE");
}
else
{
- CStdString strDeviceInfo;
- strDeviceInfo.Format("Found devices: %d\n\n", iDevicesFound);
+ PrintToStdOut("Found devices: %d\n", iDevicesFound);
for (int8_t iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
{
- strDeviceInfo.AppendFormat("device: %d\ncom port: %s\n", iDevicePtr + 1, devices[iDevicePtr].comm);
- libcec_configuration config;
- config.Clear();
+ PrintToStdOut("device: %d", iDevicePtr + 1);
+ PrintToStdOut("com port: %s", devices[iDevicePtr].strComName);
+ PrintToStdOut("vendor id: %04x", devices[iDevicePtr].iVendorId);
+ PrintToStdOut("product id: %04x", devices[iDevicePtr].iProductId);
+ PrintToStdOut("firmware version: %d", devices[iDevicePtr].iFirmwareVersion);
- if (!parser->GetDeviceInformation(devices[iDevicePtr].comm, &config))
- PrintToStdOut("WARNING: unable to open the device on port %s", devices[iDevicePtr].comm);
- else
+ if (devices[iDevicePtr].iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
{
- strDeviceInfo.AppendFormat("firmware version: %d\n", config.iFirmwareVersion);
-
- if (config.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
- {
- time_t buildTime = (time_t)config.iFirmwareBuildDate;
- strDeviceInfo.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime)));
- strDeviceInfo = strDeviceInfo.Left(strDeviceInfo.length() > 1 ? (unsigned)(strDeviceInfo.length() - 1) : 0); // strip \n added by asctime
- strDeviceInfo.append(" +0000\n");
- }
+ time_t buildTime = (time_t)devices[iDevicePtr].iFirmwareBuildDate;
+ CStdString strDeviceInfo;
+ strDeviceInfo.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime)));
+ strDeviceInfo = strDeviceInfo.Left(strDeviceInfo.length() > 1 ? (unsigned)(strDeviceInfo.length() - 1) : 0); // strip \n added by asctime
+ strDeviceInfo.append(" +0000");
+ PrintToStdOut(strDeviceInfo.c_str());
+ }
- if (config.adapterType != ADAPTERTYPE_UNKNOWN)
- {
- strDeviceInfo.AppendFormat("type: %s\n", parser->ToString(config.adapterType));
- }
+ if (devices[iDevicePtr].adapterType != ADAPTERTYPE_UNKNOWN)
+ {
+ PrintToStdOut("type: %s", parser->ToString(devices[iDevicePtr].adapterType));
}
- strDeviceInfo.append("\n");
+
+ PrintToStdOut("");
}
- PrintToStdOut(strDeviceInfo.c_str());
}
}
g_config.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
}
- ICECAdapter *parser = LibCecInitialise(&g_config);
- if (!parser)
+ g_parser = LibCecInitialise(&g_config);
+ if (!g_parser)
{
#ifdef __WINDOWS__
cout << "Cannot load libcec.dll" << endl;
cout << "Cannot load libcec.so" << endl;
#endif
- if (parser)
- UnloadLibCec(parser);
+ if (g_parser)
+ UnloadLibCec(g_parser);
return 1;
}
// init video on targets that need this
- parser->InitVideoStandalone();
+ g_parser->InitVideoStandalone();
if (!g_bSingleCommand)
{
CStdString strLog;
- strLog.Format("CEC Parser created - libCEC version %s", parser->ToString((cec_server_version)g_config.serverVersion));
+ strLog.Format("CEC Parser created - libCEC version %s", g_parser->ToString((cec_server_version)g_config.serverVersion));
cout << strLog.c_str() << endl;
//make stdin non-blocking
if (!g_bSingleCommand)
cout << "no serial port given. trying autodetect: ";
cec_adapter devices[10];
- uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
+ uint8_t iDevicesFound = g_parser->FindAdapters(devices, 10, NULL);
if (iDevicesFound <= 0)
{
if (g_bSingleCommand)
cout << "autodetect ";
cout << "FAILED" << endl;
- UnloadLibCec(parser);
+ UnloadLibCec(g_parser);
return 1;
}
else
PrintToStdOut("opening a connection to the CEC adapter...");
- if (!parser->Open(g_strPort.c_str()))
+ if (!g_parser->Open(g_strPort.c_str()))
{
PrintToStdOut("unable to open the device on port %s", g_strPort.c_str());
- UnloadLibCec(parser);
+ UnloadLibCec(g_parser);
return 1;
}
getline(cin, input);
cin.clear();
- if (ProcessConsoleCommand(parser, input) && !g_bSingleCommand && !g_bExit && !g_bHardExit)
+ if (ProcessConsoleCommand(g_parser, input) && !g_bSingleCommand && !g_bExit && !g_bHardExit)
{
if (!input.empty())
PrintToStdOut("waiting for input");
CEvent::Sleep(50);
}
- parser->Close();
- UnloadLibCec(parser);
+ g_parser->Close();
+ UnloadLibCec(g_parser);
if (g_logOutput.is_open())
g_logOutput.close();