/* * Copyright (C) 2006-2009 Alex Lasnier * * This file is part of Radio Info * * 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 of the License, 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 this program. If not, see . */ #include "receiver.h" #include "tools.h" #include "config.h" /////////////////////////////////////////////////////////////////////////////// cRadioInfoReceiver::cRadioInfoReceiver(int Pid, const cChannel *Channel, cRadioInfoData* Rid) : cReceiver(Channel, -1) { dprint(L_REC, "Receiver : Created."); radioInfoData = Rid; songId = 0; infoVers = 0; attachedDevice = NULL; } //----------------------------------------------------------------------------- void cRadioInfoReceiver::Attach(cDevice* device) { if (!attachedDevice) { attachedDevice = device; if (attachedDevice) { attachedDevice->AttachReceiver(this); dprint(L_REC, "Receiver : Attached."); } } } //----------------------------------------------------------------------------- void cRadioInfoReceiver::Detach(void) { if (attachedDevice) { attachedDevice->Detach(this); attachedDevice = NULL; dprint(L_REC, "Receiver : Detached."); } } //----------------------------------------------------------------------------- #define TS_ADAPT_FIELD_EXISTS 0x20 void cRadioInfoReceiver::Receive(uchar* data, int length) { int offset = (data[3] & TS_ADAPT_FIELD_EXISTS) ? data[4] + 5 : 4; data += offset; length -= offset; if (length < 10) return; if (data[0] != 0x00 || data[1] != 0xD3 || data[2] != 0xB0) { dprint(L_ERR, "Receiver : not radio info data"); Detach(); return; } bool newSong = false; if (songId == 0) songId = data[3]; if (songId != data[3]) { dprint(L_MSG, "New song!"); songId = data[3]; newSong = true; } if (infoVers == data[6]) return; infoVers = data[6]; dprint(L_MSG, "Updating info"); data += 9; length -= 9; radioInfoData->Lock(); char* tok = strtok((char*) data,"\x0A"); int line=0; while (tok && line < INFO_LINES) { strncpy(radioInfoData->infoData[line], tok, MAX_STR_LEN); radioInfoData->infoData[line][MAX_STR_LEN-1] = '\0'; // Just in case tok = strtok(NULL, "\x0A"); line++; } radioInfoData->newSong = newSong; radioInfoData->Unlock(); // Last 4 bytes after 00 are CRC ??? } ///////////////////////////////////////////////////////////////////////////////