fix: StandardError: eDVBCIInterfaces::setDescrambleRules entry in caid list is not...
[vuplus_dvbapp] / lib / dvb_ci / dvbci_session.cpp
index 1bd3d18..e5a669f 100644 (file)
@@ -1,5 +1,6 @@
 /* DVB CI Transport Connection */
 
+#include <lib/base/eerror.h>
 #include <lib/dvb_ci/dvbci_session.h>
 #include <lib/dvb_ci/dvbci_resmgr.h>
 #include <lib/dvb_ci/dvbci_appmgr.h>
@@ -7,7 +8,9 @@
 #include <lib/dvb_ci/dvbci_datetimemgr.h>
 #include <lib/dvb_ci/dvbci_mmi.h>
 
-eDVBCISession *eDVBCISession::sessions[SLMS];
+DEFINE_REF(eDVBCISession);
+
+ePtr<eDVBCISession> eDVBCISession::sessions[SLMS];
 
 int eDVBCISession::buildLengthField(unsigned char *pkt, int len)
 {
@@ -28,7 +31,7 @@ int eDVBCISession::buildLengthField(unsigned char *pkt, int len)
                return 3;
        } else
        {
-               printf("too big length\n");
+               eDebug("too big length");
                exit(0);
        }
 }
@@ -88,7 +91,7 @@ void eDVBCISession::sendOpenSessionResponse(eDVBCISlot *slot, unsigned char sess
 {
        char pkt[6];
        pkt[0]=session_status;
-       printf("sendOpenSessionResponse\n");
+       eDebug("sendOpenSessionResponse");
        memcpy(pkt + 1, resource_identifier, 4);
        sendSPDU(slot, 0x92, pkt, 5, session_nb);
 }
@@ -98,19 +101,29 @@ void eDVBCISession::recvCreateSessionResponse(const unsigned char *data)
        status = data[0];
        state = stateStarted;
        action = 1;
-       printf("create Session Response, status %x\n", status);
+       eDebug("create Session Response, status %x", status);
 }
 
 void eDVBCISession::recvCloseSessionRequest(const unsigned char *data)
 {
        state = stateInDeletion;
        action = 1;
-       printf("close Session Request\n");
+       eDebug("close Session Request");
+}
+
+void eDVBCISession::deleteSessions(const eDVBCISlot *slot)
+{
+       ePtr<eDVBCISession> ptr;
+       for (unsigned short session_nb=0; session_nb < SLMS; ++session_nb)
+       {
+               ptr = sessions[session_nb];
+               if (ptr && ptr->slot == slot)
+                       sessions[session_nb]=0;
+       }
 }
 
-eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned char *resource_identifier, unsigned char &status)
+void eDVBCISession::createSession(eDVBCISlot *slot, const unsigned char *resource_identifier, unsigned char &status, ePtr<eDVBCISession> &session)
 {
-       eDVBCISession *session;
        unsigned long tag;
        unsigned short session_nb;
 
@@ -120,7 +133,7 @@ eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned cha
        if (session_nb == SLMS)
        {
                status=0xF3;
-               return 0;
+               return;
        }
 
        tag = resource_identifier[0] << 24;
@@ -132,42 +145,42 @@ eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned cha
        {
        case 0x00010041:
                session=new eDVBCIResourceManagerSession;
-               printf("RESOURCE MANAGER\n");
-               printf("session: %p\n",session);
+               eDebug("RESOURCE MANAGER");
                break;
        case 0x00020041:
-               session=slot->application_manager = new eDVBCIApplicationManagerSession(slot);
-               printf("APPLICATION MANAGER\n");
+               session=new eDVBCIApplicationManagerSession(slot);
+               eDebug("APPLICATION MANAGER");
                break;
        case 0x00030041:
                session = new eDVBCICAManagerSession(slot);
-               printf("CA MANAGER\n");
+               eDebug("CA MANAGER");
                break;
        case 0x00240041:
                session=new eDVBCIDateTimeSession;
-               printf("DATE-TIME\n");
+               eDebug("DATE-TIME");
                break;
        case 0x00400041:
                session = new eDVBCIMMISession(slot);
-               printf("MMI - create session\n");
+               eDebug("MMI - create session");
                break;
        case 0x00100041:
 //             session=new eDVBCIAuthSession;
-               printf("AuthSession\n");
-               break;
+               eDebug("AuthSession");
+//             break;
        case 0x00200041:
        default:
-               printf("unknown resource type %02x %02x %02x %02x\n", resource_identifier[0], resource_identifier[1], resource_identifier[2],resource_identifier[3]);
+               eDebug("unknown resource type %02x %02x %02x %02x", resource_identifier[0], resource_identifier[1], resource_identifier[2],resource_identifier[3]);
                session=0;
                status=0xF0;
        }
 
        if (!session)
        {
-               printf("unknown session.. expect crash\n");
-               return 0;
+               eDebug("unknown session.. expect crash");
+               return;
        }
-       printf("new session_nb: %d\n", session_nb);
+
+       eDebug("new session nb %d %p", session_nb, &(*session));
        session->session_nb = session_nb;
 
        if (session)
@@ -177,8 +190,6 @@ eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned cha
                status = 0;
        }
        session->state = stateInCreation;
-
-       return session;
 }
 
 void eDVBCISession::handleClose()
@@ -197,7 +208,6 @@ int eDVBCISession::pollAll()
                        if (sessions[session_nb-1]->state == stateInDeletion)
                        {
                                sessions[session_nb-1]->handleClose();
-                               delete sessions[session_nb-1];
                                sessions[session_nb-1]=0;
                                r=1;
                        } else
@@ -215,23 +225,21 @@ void eDVBCISession::receiveData(eDVBCISlot *slot, const unsigned char *ptr, size
        unsigned char tag = *pkt++;
        int llen, hlen;
 
-       printf("slot: %p\n",slot);
-       
-       int i;
-       
-       for(i=0;i<len;i++)
-               printf("%02x ",ptr[i]);
-       printf("\n");
+       eDebug("slot: %p",slot);
+
+       for(unsigned int i=0;i<len;i++)
+               eDebugNoNewLine("%02x ",ptr[i]);
+       eDebug("");
        
        llen = parseLengthField(pkt, hlen);
        pkt += llen;
        
-       eDVBCISession *session = NULL;
+       ePtr<eDVBCISession> session;
        
        if(tag == 0x91)
        {
                unsigned char status;
-               session = createSession(slot, pkt, status);
+               createSession(slot, pkt, status, session);
                sendOpenSessionResponse(slot, status, pkt, session?session->session_nb:0);
                
                if (session)
@@ -248,14 +256,14 @@ void eDVBCISession::receiveData(eDVBCISlot *slot, const unsigned char *ptr, size
                
                if ((!session_nb) || (session_nb >= SLMS))
                {
-                       printf("PROTOCOL: illegal session number %x\n", session_nb);
+                       eDebug("PROTOCOL: illegal session number %x", session_nb);
                        return;
                }
                
                session=sessions[session_nb-1];
                if (!session)
                {
-                       printf("PROTOCOL: data on closed session %x\n", session_nb);
+                       eDebug("PROTOCOL: data on closed session %x", session_nb);
                        return;
                }
 
@@ -267,11 +275,11 @@ void eDVBCISession::receiveData(eDVBCISlot *slot, const unsigned char *ptr, size
                        session->recvCreateSessionResponse(pkt);
                        break;
                case 0x95:
-                       printf("recvCloseSessionRequest\n");
+                       eDebug("recvCloseSessionRequest");
                        session->recvCloseSessionRequest(pkt);
                        break;
                default:
-                       printf("INTERNAL: nyi, tag %02x.\n", tag);
+                       eDebug("INTERNAL: nyi, tag %02x.", tag);
                        return;
                }
        }
@@ -296,7 +304,7 @@ void eDVBCISession::receiveData(eDVBCISlot *slot, const unsigned char *ptr, size
                        {
                                if (((len-alen) > 0) && ((len - alen) < 3))
                                {
-                                       printf("WORKAROUND: applying work around MagicAPDULength\n");
+                                       eDebug("WORKAROUND: applying work around MagicAPDULength");
                                        alen=len;
                                }
                        }
@@ -307,10 +315,11 @@ void eDVBCISession::receiveData(eDVBCISlot *slot, const unsigned char *ptr, size
                }
                
        if (len)
-               printf("PROTOCOL: warning, TL-Data has invalid length\n");
+               eDebug("PROTOCOL: warning, TL-Data has invalid length");
 }
 
 eDVBCISession::~eDVBCISession()
 {
+//     eDebug("destroy %p", this);
 }