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
|
/*
* Copyright (C) 2007 Chris Tallon
* Copyright (C) 2010 Alwin Esch (Team XBMC)
* http://www.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
*
*/
/*
* This code is taken from VOMP for VDR plugin.
*/
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include "responsepacket.h"
#include "vdrcommand.h"
#include "config.h"
#include "tools.h"
/* Packet format for an RR channel response:
4 bytes = channel ID = 1 (request/response channel)
4 bytes = request ID (from serialNumber)
4 bytes = length of the rest of the packet
? bytes = rest of packet. depends on packet
*/
cResponsePacket::cResponsePacket()
{
buffer = NULL;
bufSize = 0;
bufUsed = 0;
}
cResponsePacket::~cResponsePacket()
{
if (buffer) free(buffer);
}
bool cResponsePacket::init(uint32_t requestID)
{
if (buffer == NULL) {
bufSize = 512;
buffer = (uint8_t*)malloc(bufSize);
}
*(uint32_t*)&buffer[0] = htonl(CHANNEL_REQUEST_RESPONSE); // RR channel
*(uint32_t*)&buffer[4] = htonl(requestID);
*(uint32_t*)&buffer[userDataLenPos] = 0;
bufUsed = headerLength;
return true;
}
bool cResponsePacket::initScan(uint32_t opCode)
{
if(buffer == NULL) {
bufSize = 512;
buffer = (uint8_t*)malloc(bufSize);
}
*(uint32_t*)&buffer[0] = htonl(CHANNEL_SCAN); // RR channel
*(uint32_t*)&buffer[4] = htonl(opCode);
*(uint32_t*)&buffer[userDataLenPos] = 0;
bufUsed = headerLength;
return true;
}
bool cResponsePacket::initStatus(uint32_t opCode)
{
if(buffer == NULL) {
bufSize = 512;
buffer = (uint8_t*)malloc(bufSize);
}
*(uint32_t*)&buffer[0] = htonl(CHANNEL_STATUS); // RR channel
*(uint32_t*)&buffer[4] = htonl(opCode);
*(uint32_t*)&buffer[userDataLenPos] = 0;
bufUsed = headerLength;
return true;
}
bool cResponsePacket::initStream(uint32_t opCode, uint32_t streamID, uint32_t duration, int64_t dts, int64_t pts)
{
if(buffer == NULL) {
bufSize = 512;
buffer = (uint8_t*)malloc(bufSize);
}
*(uint32_t*)&buffer[0] = htonl(CHANNEL_STREAM); // stream channel
*(uint32_t*)&buffer[4] = htonl(opCode); // Stream packet operation code
*(uint32_t*)&buffer[8] = htonl(streamID); // Stream ID (unused here)
*(uint32_t*)&buffer[12] = htonl(duration); // Duration (unused here)
*(int64_t*) &buffer[16] = htonl(dts); // DTS (unused here)
*(int64_t*) &buffer[24] = htonl(pts); // PTS (unused here)
*(uint32_t*)&buffer[userDataLenPosStream] = 0;
bufUsed = headerLengthStream;
return true;
}
void cResponsePacket::finalise()
{
*(uint32_t*)&buffer[userDataLenPos] = htonl(bufUsed - headerLength);
//Log::getInstance()->log("Client", Log::DEBUG, "RP finalise %lu", bufUsed - headerLength);
}
void cResponsePacket::finaliseStream()
{
*(uint32_t*)&buffer[userDataLenPosStream] = htonl(bufUsed - headerLengthStream);
}
bool cResponsePacket::copyin(const uint8_t* src, uint32_t len)
{
if (!checkExtend(len)) return false;
memcpy(buffer + bufUsed, src, len);
bufUsed += len;
return true;
}
uint8_t* cResponsePacket::reserve(uint32_t len) {
if (!checkExtend(len)) return false;
uint8_t* result = buffer + bufUsed;
bufUsed += len;
return result;
}
bool cResponsePacket::unreserve(uint32_t len) {
if(bufUsed < len) return false;
bufUsed -= len;
return true;
}
bool cResponsePacket::add_String(const char* string)
{
uint32_t len = strlen(string) + 1;
if (!checkExtend(len)) return false;
memcpy(buffer + bufUsed, string, len);
bufUsed += len;
return true;
}
bool cResponsePacket::add_U32(uint32_t ul)
{
if (!checkExtend(sizeof(uint32_t))) return false;
*(uint32_t*)&buffer[bufUsed] = htonl(ul);
bufUsed += sizeof(uint32_t);
return true;
}
bool cResponsePacket::add_U8(uint8_t c)
{
if (!checkExtend(sizeof(uint8_t))) return false;
buffer[bufUsed] = c;
bufUsed += sizeof(uint8_t);
return true;
}
bool cResponsePacket::add_S32(int32_t l)
{
if (!checkExtend(sizeof(int32_t))) return false;
*(int32_t*)&buffer[bufUsed] = htonl(l);
bufUsed += sizeof(int32_t);
return true;
}
bool cResponsePacket::add_U64(uint64_t ull)
{
if (!checkExtend(sizeof(uint64_t))) return false;
*(uint64_t*)&buffer[bufUsed] = htonll(ull);
bufUsed += sizeof(uint64_t);
return true;
}
bool cResponsePacket::add_double(double d)
{
if (!checkExtend(sizeof(double))) return false;
uint64_t ull;
memcpy(&ull,&d,sizeof(double));
*(uint64_t*)&buffer[bufUsed] = htonll(ull);
bufUsed += sizeof(uint64_t);
return true;
}
bool cResponsePacket::checkExtend(uint32_t by)
{
if ((bufUsed + by) < bufSize) return true;
if (512 > by) by = 512;
uint8_t* newBuf = (uint8_t*)realloc(buffer, bufSize + by);
if (!newBuf) return false;
buffer = newBuf;
bufSize += by;
return true;
}
|