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
|
/*
* vdr-plugin-vnsi - KODI server plugin for VDR
*
* Copyright (C) 2007 Chris Tallon
* Copyright (C) 2010 Alwin Esch (Team XBMC)
* Copyright (C) 2010, 2011 Alexander Pipelka
* Copyright (C) 2015 Team KODI
*
* http://kodi.tv
*
* 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 KODI; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is taken from VOMP for VDR plugin.
*/
#include "responsepacket.h"
#include "vnsicommand.h"
#include "config.h"
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#ifndef __FreeBSD__
#include <asm/byteorder.h>
#else
#include <sys/endian.h>
#define __be64_to_cpu be64toh
#define __cpu_to_be64 htobe64
#endif
/* 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()
: bufSize( 512 )
, buffer( static_cast<uint8_t*>(malloc(bufSize)) )
, bufUsed( 0 )
{
}
cResponsePacket::~cResponsePacket()
{
free(buffer);
buffer = nullptr;
}
void cResponsePacket::init(uint32_t requestID)
{
bufUsed = 0;
uint32_t ul;
ul = htonl(VNSI_CHANNEL_REQUEST_RESPONSE); // RR channel
memcpy(&buffer[0], &ul, sizeof(uint32_t));
ul = htonl(requestID);
memcpy(&buffer[4], &ul, sizeof(uint32_t));
ul = 0;
memcpy(&buffer[userDataLenPos], &ul, sizeof(uint32_t));
bufUsed = headerLength;
}
void cResponsePacket::initScan(uint32_t opCode)
{
bufUsed = 0;
uint32_t ul;
ul = htonl(VNSI_CHANNEL_SCAN); // RR channel
memcpy(&buffer[0], &ul, sizeof(uint32_t));
ul = htonl(opCode);
memcpy(&buffer[4], &ul, sizeof(uint32_t));
ul = 0;
memcpy(&buffer[userDataLenPos], &ul, sizeof(uint32_t));
bufUsed = headerLength;
}
void cResponsePacket::initStatus(uint32_t opCode)
{
bufUsed = 0;
uint32_t ul;
ul = htonl(VNSI_CHANNEL_STATUS); // RR channel
memcpy(&buffer[0], &ul, sizeof(uint32_t));
ul = htonl(opCode);
memcpy(&buffer[4], &ul, sizeof(uint32_t));
ul = 0;
memcpy(&buffer[userDataLenPos], &ul, sizeof(uint32_t));
bufUsed = headerLength;
}
void cResponsePacket::initStream(uint32_t opCode, uint32_t streamID, uint32_t duration, int64_t pts, int64_t dts, uint32_t serial)
{
bufUsed = 0;
uint32_t ul;
uint64_t ull;
ul = htonl(VNSI_CHANNEL_STREAM); // stream channel
memcpy(&buffer[0], &ul, sizeof(uint32_t));
ul = htonl(opCode); // Stream packet operation code
memcpy(&buffer[4], &ul, sizeof(uint32_t));
ul = htonl(streamID); // Stream ID
memcpy(&buffer[8], &ul, sizeof(uint32_t));
ul = htonl(duration); // Duration
memcpy(&buffer[12], &ul, sizeof(uint32_t));
ull = __cpu_to_be64(pts); // PTS
memcpy(&buffer[16], &ull, sizeof(uint64_t));
ull = __cpu_to_be64(dts); // DTS
memcpy(&buffer[24], &ull, sizeof(uint64_t));
ul = htonl(serial);
memcpy(&buffer[32], &ul, sizeof(uint32_t));
ul = 0;
memcpy(&buffer[userDataLenPosStream], &ul, sizeof(uint32_t));
bufUsed = headerLengthStream;
}
void cResponsePacket::initOsd(uint32_t opCode, int32_t wnd, int32_t color, int32_t x0, int32_t y0, int32_t x1, int32_t y1)
{
bufUsed = 0;
uint32_t ul;
int32_t l;
ul = htonl(VNSI_CHANNEL_OSD); // stream OSD
memcpy(&buffer[0], &ul, sizeof(uint32_t));
ul = htonl(opCode); // OSD operation code
memcpy(&buffer[4], &ul, sizeof(uint32_t));
l = htonl(wnd); // Window
memcpy(&buffer[8], &l, sizeof(int32_t));
l = htonl(color); // Color
memcpy(&buffer[12], &l, sizeof(int32_t));
l = htonl(x0); // x0
memcpy(&buffer[16], &l, sizeof(int32_t));
l = htonl(y0); // y0
memcpy(&buffer[20], &l, sizeof(int32_t));
l = htonl(x1); // x1
memcpy(&buffer[24], &l, sizeof(int32_t));
l = htonl(y1); // y1
memcpy(&buffer[28], &l, sizeof(int32_t));
ul = 0;
memcpy(&buffer[userDataLenPosOSD], &ul, sizeof(uint32_t));
bufUsed = headerLengthOSD;
}
void cResponsePacket::finalise()
{
uint32_t ul = htonl(bufUsed - headerLength);
memcpy(&buffer[userDataLenPos], &ul, sizeof(uint32_t));
}
void cResponsePacket::finaliseStream()
{
uint32_t ul = htonl(bufUsed - headerLengthStream);
memcpy(&buffer[userDataLenPosStream], &ul, sizeof(uint32_t));
}
void cResponsePacket::finaliseOSD()
{
uint32_t ul = htonl(bufUsed - headerLengthOSD);
memcpy(&buffer[userDataLenPosOSD], &ul, sizeof(uint32_t));
}
void cResponsePacket::copyin(const uint8_t* src, uint32_t len)
{
checkExtend(len);
memcpy(buffer + bufUsed, src, len);
bufUsed += len;
}
uint8_t* cResponsePacket::reserve(uint32_t len) {
checkExtend(len);
uint8_t* result = buffer + bufUsed;
bufUsed += len;
return result;
}
bool cResponsePacket::unreserve(uint32_t len) {
if(bufUsed < len) return false;
bufUsed -= len;
return true;
}
void cResponsePacket::add_String(const char* string)
{
uint32_t len = strlen(string) + 1;
checkExtend(len);
memcpy(buffer + bufUsed, string, len);
bufUsed += len;
}
void cResponsePacket::add_U32(uint32_t ul)
{
checkExtend(sizeof(uint32_t));
uint32_t tmp = htonl(ul);
memcpy(&buffer[bufUsed], &tmp, sizeof(uint32_t));
bufUsed += sizeof(uint32_t);
}
void cResponsePacket::add_U8(uint8_t c)
{
checkExtend(sizeof(uint8_t));
buffer[bufUsed] = c;
bufUsed += sizeof(uint8_t);
}
void cResponsePacket::add_S32(int32_t l)
{
checkExtend(sizeof(int32_t));
int32_t tmp = htonl(l);
memcpy(&buffer[bufUsed], &tmp, sizeof(int32_t));
bufUsed += sizeof(int32_t);
}
void cResponsePacket::add_U64(uint64_t ull)
{
checkExtend(sizeof(uint64_t));
uint64_t tmp = __cpu_to_be64(ull);
memcpy(&buffer[bufUsed], &tmp, sizeof(uint64_t));
bufUsed += sizeof(uint64_t);
}
void cResponsePacket::add_double(double d)
{
checkExtend(sizeof(double));
uint64_t ull;
memcpy(&ull, &d, sizeof(double));
ull = __cpu_to_be64(ull);
memcpy(&buffer[bufUsed], &ull, sizeof(uint64_t));
bufUsed += sizeof(uint64_t);
}
void cResponsePacket::checkExtend(uint32_t by)
{
if ((bufUsed + by) < bufSize)
{
return;
}
uint8_t* newBuf = (uint8_t*)realloc(buffer, bufSize + by);
if (!newBuf)
{
throw std::bad_alloc();
}
buffer = newBuf;
bufSize += by;
}
|