/
IRServerSuite.cpp
417 lines (385 loc) · 10.3 KB
/
IRServerSuite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright (C) 2005-2008 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "include.h"
#include "IRServerSuite.h"
#include "IrssMessage.h"
#include "ButtonTranslator.h"
#include "log.h"
#include "Settings.h"
#define IRSS_PORT 24000
CRemoteControl g_RemoteControl;
CRemoteControl::CRemoteControl()
{
m_bInitialized = false;
m_isConnecting = false;
Reset();
}
CRemoteControl::~CRemoteControl()
{
Close();
}
void CRemoteControl::Close()
{
if (m_socket)
{
if (m_bInitialized)
{
m_bInitialized = false;
CIrssMessage message(IRSSMT_UnregisterClient, IRSSMF_Request | IRSSMF_ForceNotRespond);
SendPacket(message);
}
shutdown(m_socket, SD_BOTH);
closesocket(m_socket);
}
}
void CRemoteControl::Reset()
{
m_isHolding = false;
m_button = 0;
}
bool CRemoteControl::Initialize()
{
//trying to connect when there is nothing to connect to is kinda slow so kick it off in a thread.
Create();
return true;
}
void CRemoteControl::Process()
{
Connect();
}
bool CRemoteControl::Connect()
{
m_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET)
{
return false; //Couldn't create the socket
}
// Get the local host information
hostent* localHost = gethostbyname("");
char* localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);
SOCKADDR_IN target;
memset(&target, 0, sizeof(SOCKADDR_IN));
target.sin_family = AF_INET;
target.sin_addr.s_addr = inet_addr(localIP);
target.sin_port = htons (IRSS_PORT);
if (connect(m_socket, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
{
return false; //Couldn't connect, irss not available
}
u_long iMode = 1; //non-blocking
ioctlsocket(m_socket, FIONBIO, &iMode);
//register
CIrssMessage mess(IRSSMT_RegisterClient, IRSSMF_Request);
if (!SendPacket(mess))
{
CLog::Log(LOGERROR, "IRServerSuite: failed to send RegisterClient packet.");
return false;
}
m_isConnecting = true;
return true;
}
bool CRemoteControl::SendPacket(CIrssMessage& message)
{
int iSize = 0;
char* bytes = message.ToBytes(iSize);
char buffer[4];
uint32_t len = htonl(iSize);
memcpy(&buffer[0], &len, 4);
bool bResult = WriteN(&buffer[0], 4);
if (bResult)
{
bResult = WriteN(bytes, iSize);
}
delete[] bytes;
if (!bResult)
{
Close();
return false;
}
return true;
}
void CRemoteControl::Update()
{
if (!m_bInitialized && !m_isConnecting)
{
return;
}
CIrssMessage mess;
if (!ReadPacket(mess))
{
return;
}
switch (mess.GetType())
{
case IRSSMT_RegisterClient:
m_isConnecting = false;
if ((mess.GetFlags() & IRSSMF_Success) != IRSSMF_Success)
{
//uh oh, it failed to register
Close();
CLog::Log(LOGERROR, "IRServerSuite: failed to register XBMC as a client.");
}
else
{
m_bInitialized = true;
//request info about receivers
CIrssMessage mess(IRSSMT_DetectedReceivers, IRSSMF_Request);
if (!SendPacket(mess))
{
CLog::Log(LOGERROR, "IRServerSuite: failed to send AvailableReceivers packet.");
}
mess.SetType(IRSSMT_AvailableReceivers);
if (!SendPacket(mess))
{
CLog::Log(LOGERROR, "IRServerSuite: failed to send AvailableReceivers packet.");
}
mess.SetType(IRSSMT_ActiveReceivers);
if (!SendPacket(mess))
{
CLog::Log(LOGERROR, "IRServerSuite: failed to send AvailableReceivers packet.");
}
}
break;
case IRSSMT_RemoteEvent:
HandleRemoteEvent(mess);
break;
case IRSSMT_Error:
//I suppose the errormessage is in the packet somewhere...
CLog::Log(LOGERROR, "IRServerSuite: we got an error message.");
break;
case IRSSMT_ServerShutdown:
Close();
break;
case IRSSMT_ServerSuspend:
//should we do something?
break;
case IRSSMT_ServerResume:
//should we do something?
break;
case IRSSMT_AvailableReceivers:
{
uint32_t size = mess.GetDataSize();
if (size > 0)
{
char* data = mess.GetData();
char* availablereceivers = new char[size + 1];
memcpy(availablereceivers, data, size);
availablereceivers[size] = '\0';
CLog::Log(LOGINFO, "IRServerSuite: Available receivers: %s", availablereceivers);
delete[] availablereceivers;
}
}
break;
case IRSSMT_DetectedReceivers:
{
uint32_t size = mess.GetDataSize();
if (size > 0)
{
char* data = mess.GetData();
char* detectedreceivers = new char[size + 1];
memcpy(detectedreceivers, data, size);
detectedreceivers[size] = '\0';
CLog::Log(LOGINFO, "IRServerSuite: Detected receivers: %s", detectedreceivers);
delete[] detectedreceivers;
}
}
break;
case IRSSMT_ActiveReceivers:
{
uint32_t size = mess.GetDataSize();
if (size > 0)
{
char* data = mess.GetData();
char* activereceivers = new char[size + 1];
memcpy(activereceivers, data, size);
activereceivers[size] = '\0';
CLog::Log(LOGINFO, "IRServerSuite: Active receivers: %s", activereceivers);
delete[] activereceivers;
}
}
break;
}
}
bool CRemoteControl::HandleRemoteEvent(CIrssMessage& message)
{
try
{
//flag should be notify, maybe check it?
char* data = message.GetData();
uint32_t datalen = message.GetDataSize();
char* deviceName;
char* keycode;
uint32_t devicenamelength;
uint32_t keycodelength;
if (datalen == 0)
{
CLog::Log(LOGERROR, "IRServerSuite: no data in remote message.");
return false;
}
if (datalen <= 8)
{
//seems to be version 1.0.4.1, only keycode is sent, use Microsoft MCE mapping??
devicenamelength = 13;
deviceName = new char[devicenamelength + 1];
sprintf(deviceName, "Microsoft MCE");
keycodelength = datalen;
keycode = new char[keycodelength + 1];
memcpy(keycode, data, keycodelength);
}
else
{
//first 4 bytes is devicename length
memcpy(&devicenamelength, data, 4);
//devicename itself
if (datalen < 4 + devicenamelength)
{
CLog::Log(LOGERROR, "IRServerSuite: invalid data in remote message (size: %u).", datalen);
return false;
}
deviceName = new char[devicenamelength + 1];
memcpy(deviceName, data + 4, devicenamelength);
if (datalen < 8 + devicenamelength)
{
CLog::Log(LOGERROR, "IRServerSuite: invalid data in remote message (size: %u).", datalen);
return false;
}
//next 4 bytes is keycode length
memcpy(&keycodelength, data + 4 + devicenamelength, 4);
//keycode itself
if (datalen < 8 + devicenamelength + keycodelength)
{
CLog::Log(LOGERROR, "IRServerSuite: invalid data in remote message (size: %u).", datalen);
delete[] deviceName;
return false;
}
keycode = new char[keycodelength + 1];
memcpy(keycode, data + 8 + devicenamelength, keycodelength);
}
deviceName[devicenamelength] = '\0';
keycode[keycodelength] = '\0';
//translate to a buttoncode xbmc understands
m_button = g_buttonTranslator.TranslateLircRemoteString(deviceName, keycode);
if (g_advancedSettings.m_logLevel == LOG_LEVEL_DEBUG_FREEMEM)
{
CLog::Log(LOGINFO, "IRServerSuite, RemoteEvent: %s %s", deviceName, keycode);
}
delete[] deviceName;
delete[] keycode;
return true;
}
catch(...)
{
CLog::Log(LOGERROR, "IRServerSuite: exception while processing RemoteEvent.");
return false;
}
}
int CRemoteControl::ReadN(char *buffer, int n)
{
int nOriginalSize = n;
memset(buffer, 0, n);
char *ptr = buffer;
while (n > 0)
{
int nBytes = 0;
nBytes = recv(m_socket, ptr, n, 0);
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
return nOriginalSize - n;
}
if (nBytes < 0)
{
CLog::Log(LOGERROR, "%s, IRServerSuite recv error %d", __FUNCTION__, GetLastError());
Close();
return -1;
}
if (nBytes == 0)
{
CLog::Log(LOGDEBUG,"%s, IRServerSuite socket closed by server", __FUNCTION__);
Close();
break;
}
n -= nBytes;
ptr += nBytes;
}
return nOriginalSize - n;
}
bool CRemoteControl::WriteN(const char *buffer, int n)
{
const char *ptr = buffer;
while (n > 0)
{
int nBytes = send(m_socket, ptr, n, 0);
if (nBytes < 0)
{
CLog::Log(LOGERROR, "%s, IRServerSuite send error %d (%d bytes)", __FUNCTION__, GetLastError(), n);
Close();
return false;
}
if (nBytes == 0)
break;
n -= nBytes;
ptr += nBytes;
}
return n == 0;
}
bool CRemoteControl::ReadPacket(CIrssMessage &message)
{
try
{
char sizebuf[4];
int iRead = ReadN(&sizebuf[0], 4);
if (iRead == 0) return false; //nothing to read
if (iRead != 4)
{
CLog::Log(LOGERROR, "IRServerSuite: failed to read packetsize.");
return false;
}
uint32_t size = 0;
memcpy(&size, &sizebuf[0], 4);
size = ntohl(size);
char* messagebytes = new char[size];
if (ReadN(messagebytes, size) != size)
{
CLog::Log(LOGERROR, "IRServerSuite: failed to read packet.");
return false;
}
if (!CIrssMessage::FromBytes(messagebytes, size, message))
{
CLog::Log(LOGERROR, "IRServerSuite: invalid packet received (size: %u).", size);
return false;
}
delete[] messagebytes;
return true;
}
catch(...)
{
CLog::Log(LOGERROR, "IRServerSuite: exception while processing packet.");
return false;
}
}
WORD CRemoteControl::GetButton()
{
return m_button;
}
bool CRemoteControl::IsHolding()
{
return m_isHolding;
}