Skip to content

Commit

Permalink
Move LCNData to db.h
Browse files Browse the repository at this point in the history
Author: @DimitarCC

Uses some code from openatv/enigma2@be62b2d
  • Loading branch information
Huevos committed Aug 7, 2024
1 parent a2b17e0 commit 572b037
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 56 deletions.
86 changes: 86 additions & 0 deletions lib/dvb/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,65 @@ void eDVBDB::loadServiceListV5(FILE * f)
eDebug("[eDVBDB] loaded %d channels/transponders and %d services", tcount, scount);
}

void eDVBDB::resetLcnDB()
{
m_lcnmap.clear();
FILE *m_lcn_file = fopen(eEnv::resolve("${sysconfdir}/enigma2/lcndb").c_str(), "w");
if (m_lcn_file)
fclose(m_lcn_file);
}

void eDVBDB::saveLcnDB()
{
std::string lfname = eEnv::resolve("${sysconfdir}/enigma2/lcndb");
CFile lf(lfname, "w");
if (lf)
{
for (auto &[key, value] : m_lcnmap)
{
value.write(lf, key);
}
}
}

void eDVBDB::addLcnToDB(int ns, int onid, int tsid, int sid, uint16_t lcn, uint32_t signal)
{
eServiceReferenceDVB s = eServiceReferenceDVB(eDVBNamespace(ns), eTransportStreamID(tsid), eOriginalNetworkID(onid), eServiceID(sid), 0);
std::map<eServiceReferenceDVB, LCNData>::iterator it = m_lcnmap.find(s);
if (it != m_lcnmap.end())
{
it->second.Update(lcn, signal);
}
else
{
LCNData lcndata;
lcndata.Update(lcn, signal);
m_lcnmap.insert(std::pair<eServiceReferenceDVB, LCNData>(s, lcndata));
}
}

void eDVBDB::readLcnDBFile()
{
char line[256];
m_lcnmap.clear();
std::string lfname = eEnv::resolve("${sysconfdir}/enigma2/lcndb");
CFile lf(lfname, "rt");
if(lf)
{
while (!feof(lf))
{
if (!fgets(line, sizeof(line), lf))
break;

LCNData lcndata;
eServiceReferenceDVB s = lcndata.parse(line);
if (s)
m_lcnmap.insert(std::pair<eServiceReferenceDVB, LCNData>(s, lcndata));

}
}
}

void eDVBDB::loadServicelist(const char *file)
{
eDebug("[eDVBDB] ---- opening lame channel db");
Expand Down Expand Up @@ -828,6 +887,8 @@ void eDVBDB::loadServicelist(const char *file)
eDebug("[eDVBDB] services invalid, no transponders");
return;
}

readLcnDBFile();
// clear all transponders
int tcount = 0;
while (!feof(f))
Expand Down Expand Up @@ -1109,6 +1170,7 @@ void eDVBDB::saveServicelist(const char *file)
void eDVBDB::saveServicelist()
{
saveServicelist(eEnv::resolve("${sysconfdir}/enigma2/lamedb").c_str());
saveLcnDB();
}

void eDVBDB::saveIptvServicelist()
Expand Down Expand Up @@ -2120,6 +2182,30 @@ PyObject *eDVBDB::readATSC(ePyObject atsc_list, ePyObject tp_dict)
return Py_True;
}

PyObject *eDVBDB::getLcnDBData()
{
ePyObject dest = PyList_New(0);
if (dest)
{
std::map<eServiceReferenceDVB, LCNData>::iterator it = m_lcnmap.begin();
for (;it != m_lcnmap.end();++it)
{
ePyObject tuple = PyTuple_New(6);
PyTuple_SET_ITEM(tuple, 0, PyLong_FromLongLong((unsigned long)it->second.NS));
PyTuple_SET_ITEM(tuple, 1, PyLong_FromLongLong((unsigned long)it->second.ONID));
PyTuple_SET_ITEM(tuple, 2, PyLong_FromLongLong((unsigned long)it->second.TSID));
PyTuple_SET_ITEM(tuple, 3, PyLong_FromLongLong((unsigned long)it->second.SID));
PyTuple_SET_ITEM(tuple, 4, PyLong_FromLongLong((unsigned long)it->second.LCN));
PyTuple_SET_ITEM(tuple, 5, PyLong_FromLongLong((unsigned long)it->second.SIGNAL));
PyList_Append(dest, tuple);
Py_DECREF(tuple);
}
}
else
Py_RETURN_NONE;
return dest;
}

eDVBDB::~eDVBDB()
{
instance=NULL;
Expand Down
73 changes: 73 additions & 0 deletions lib/dvb/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,75 @@
#include <lib/base/eptrlist.h>
#include <set>
#include <vector>
#include <sstream>
class ServiceDescriptionSection;

struct LCNData
{
private:
bool FOUND;

std::vector<std::string> split_str(std::string s)
{
std::vector<std::string> tokens;
std::string token;
std::stringstream str(s);
while (getline(str, token, ':')) {
tokens.push_back(token);
}
return tokens;
}

public:
int NS;
int ONID;
int TSID;
int SID;
int SIGNAL;
int LCN;
LCNData()
{
LCN = 0;
SIGNAL = -1;
FOUND = true;
}

eServiceReferenceDVB parse(const char *line)
{

if (sscanf(line, "%x:%x:%x:%x:%d:%d", &NS, &ONID, &TSID, &SID, &LCN, &SIGNAL) == 6)
return eServiceReferenceDVB(eDVBNamespace(NS), eTransportStreamID(TSID), eOriginalNetworkID(ONID), eServiceID(SID), 0);
else
return eServiceReferenceDVB();

}

void Update(uint16_t lcn, uint32_t signal)
{
LCN = lcn;
SIGNAL = signal;
FOUND = true;
}

void write(FILE *lf, const eServiceReferenceDVB &key)
{
if (FOUND)
{
int sid = key.getServiceID().get();
int tsid = key.getTransportStreamID().get();
int onid = key.getOriginalNetworkID().get();
int ns = key.getDVBNamespace().get();
fprintf(lf, "%x:%x:%x:%x:%d:%d\n", ns, onid, tsid, sid, LCN, SIGNAL);
}
}

void resetFound()
{
FOUND = false;
}

};

class eIPTVDBItem
{
public:
Expand Down Expand Up @@ -65,6 +132,7 @@ class eDVBDB: public iDVBChannelList
#endif
private:
void loadServiceListV5(FILE * f);
std::map<eServiceReferenceDVB, LCNData> m_lcnmap;
public:
std::vector<eIPTVDBItem> iptv_services;
// iDVBChannelList
Expand All @@ -83,6 +151,7 @@ class eDVBDB: public iDVBChannelList
PyObject *readTerrestrials(SWIG_PYOBJECT(ePyObject) ter_list, SWIG_PYOBJECT(ePyObject) tp_dict);
PyObject *readCables(SWIG_PYOBJECT(ePyObject) cab_list, SWIG_PYOBJECT(ePyObject) tp_dict);
PyObject *readATSC(SWIG_PYOBJECT(ePyObject) atsc_list, SWIG_PYOBJECT(ePyObject) tp_dict);
PyObject *getLcnDBData();
#ifndef SWIG
RESULT removeFlags(unsigned int flagmask, eDVBChannelID chid, unsigned int orb_pos);
RESULT removeServices(eDVBChannelID chid, unsigned int orb_pos);
Expand All @@ -106,6 +175,9 @@ class eDVBDB: public iDVBChannelList
eDVBDB();
virtual ~eDVBDB();
int renumberBouquet(eBouquet &bouquet, int startChannelNum = 1);
void addLcnToDB(int ns, int onid, int tsid, int sid, uint16_t lcn, uint32_t signal);
void resetLcnDB();
void readLcnDBFile();
#endif
eServiceReference searchReference(int tsid, int onid, int sid);
void setNumberingMode(bool numberingMode);
Expand All @@ -117,6 +189,7 @@ class eDVBDB: public iDVBChannelList
void saveServicelist();
void saveIptvServicelist();
void saveServicelist(const char *file);
void saveLcnDB();
void reloadBouquets();
void parseServiceData(ePtr<eDVBService> s, std::string str);
};
Expand Down
56 changes: 3 additions & 53 deletions lib/dvb/scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ eDVBScan::eDVBScan(iDVBChannel *channel, bool usePAT, bool debug)
if (m_channel->getDemux(m_demux))
SCAN_eDebug("[eDVBScan] failed to allocate demux!");
m_channel->connectStateChange(sigc::mem_fun(*this, &eDVBScan::stateChange), m_stateChanged_connection);
m_lcn_file = NULL;
}

eDVBScan::~eDVBScan()
{
if (m_lcn_file)
fclose(m_lcn_file);
}

int eDVBScan::isValidONIDTSID(int orbital_position, eOriginalNetworkID onid, eTransportStreamID tsid)
Expand Down Expand Up @@ -585,39 +582,6 @@ void eDVBScan::addKnownGoodChannel(const eDVBChannelID &chid, iDVBFrontendParame
m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
}

void eDVBScan::addLcnToDB(eDVBNamespace ns, eOriginalNetworkID onid, eTransportStreamID tsid, eServiceID sid, uint16_t lcn, uint32_t signal)
{
if (m_lcn_file)
{
int size = 0;
char row[40];
bool added = false;
sprintf(row, "%08x:%04x:%04x:%04x:%05d:%08d\n", ns.get(), onid.get(), tsid.get(), sid.get(), lcn, signal);
fseek(m_lcn_file, 0, SEEK_END);
size = ftell(m_lcn_file);

for (int i = 0; i < size / 39; i++)
{
char tmp[40];
fseek(m_lcn_file, i*39, SEEK_SET);
fread (tmp, 1, 39, m_lcn_file);
if (memcmp(tmp, row, 23) == 0)
{
fseek(m_lcn_file, i*39, SEEK_SET);
fwrite(row, 1, 39, m_lcn_file);
added = true;
break;
}
}

if (!added)
{
fseek(m_lcn_file, 0, SEEK_END);
fwrite(row, 1, 39, m_lcn_file);
}
}
}

void eDVBScan::addChannelToScan(iDVBFrontendParameters *feparm)
{
/* check if we don't already have that channel ... */
Expand Down Expand Up @@ -992,7 +956,7 @@ void eDVBScan::channelDone()
LogicalChannel *ch = *it;
if (ch->getVisibleServiceFlag())
{
addLcnToDB(ns, onid, tsid, eServiceID(ch->getServiceId()), ch->getLogicalChannelNumber(), signal);
eDVBDB::getInstance()->addLcnToDB(ns.get(), onid.get(), tsid.get(), eServiceID(ch->getServiceId()).get(), ch->getLogicalChannelNumber(), signal);
SCAN_eDebug("NAMESPACE: %08x ONID: %04x TSID: %04x SID: %04x LCN: %05d SIGNAL: %08d", ns.get(), onid.get(), tsid.get(), ch->getServiceId(), ch->getLogicalChannelNumber(), signal);
}
}
Expand Down Expand Up @@ -1266,26 +1230,12 @@ void eDVBScan::start(const eSmartPtrList<iDVBFrontendParameters> &known_transpon
transponderlist = &m_ch_blindscan;
}

if (m_lcn_file)
fclose(m_lcn_file);

if (m_flags & scanRemoveServices)
{
m_lcn_file = fopen(eEnv::resolve("${sysconfdir}/enigma2/lcndb").c_str(), "w");
if (!m_lcn_file)
eDebug("couldn't open file lcndb");
}
else
{
m_lcn_file = fopen(eEnv::resolve("${sysconfdir}/enigma2/lcndb").c_str(), "r+");
if (!m_lcn_file)
{
m_lcn_file = fopen(eEnv::resolve("${sysconfdir}/enigma2/lcndb").c_str(), "w");
if (!m_lcn_file)
eDebug("couldn't open file lcndb");
}
eDVBDB::getInstance()->resetLcnDB();
}


for (eSmartPtrList<iDVBFrontendParameters>::const_iterator i(known_transponders.begin()); i != known_transponders.end(); ++i)
{
bool exist=false;
Expand Down
3 changes: 0 additions & 3 deletions lib/dvb/scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ class eDVBScan: public sigc::trackable, public iObject
int m_networkid;
bool m_usePAT;
bool m_scan_debug;

FILE *m_lcn_file;
void addLcnToDB(eDVBNamespace ns, eOriginalNetworkID onid, eTransportStreamID tsid, eServiceID sid, uint16_t lcn, uint32_t signal);
public:
eDVBScan(iDVBChannel *channel, bool usePAT=true, bool debug=true );
~eDVBScan();
Expand Down

0 comments on commit 572b037

Please sign in to comment.