use boundFunction from tools
[enigma2.git] / lib / dvb_ci / dvbci_session.cpp
index 1e59d2ad4816a13b7210e466f64a853a4b072ade..bb8ba8691ac5c46f3a2079cd6419902b9ae92148 100644 (file)
@@ -1,11 +1,13 @@
 /* DVB CI Transport Connection */
 
 #include <lib/dvb_ci/dvbci_session.h>
+#include <lib/dvb_ci/dvbci_resmgr.h>
+#include <lib/dvb_ci/dvbci_appmgr.h>
+#include <lib/dvb_ci/dvbci_camgr.h>
+#include <lib/dvb_ci/dvbci_datetimemgr.h>
+#include <lib/dvb_ci/dvbci_mmi.h>
 
-eDVBCISession::eDVBCISession(eDVBCISlot *cislot)
-{
-       slot = cislot;
-}
+eDVBCISession *eDVBCISession::sessions[SLMS];
 
 int eDVBCISession::buildLengthField(unsigned char *pkt, int len)
 {
@@ -47,6 +49,22 @@ int eDVBCISession::parseLengthField(const unsigned char *pkt, int &len)
        return (pkt[0] & 0x7F) + 1;
 }
 
+void eDVBCISession::sendAPDU(const unsigned char *tag, const void *data, int len)
+{
+       unsigned char pkt[len+3+4];
+       int l;
+       memcpy(pkt, tag, 3);
+       l=buildLengthField(pkt+3, len);
+       if (data)
+               memcpy(pkt+3+l, data, len);
+       sendSPDU(0x90, 0, 0, pkt, len+3+l);
+}
+
+void eDVBCISession::sendSPDU(unsigned char tag, const void *data, int len, const void *apdu, int alen)
+{
+       sendSPDU(slot, tag, data, len, session_nb, apdu, alen);
+}
+
 void eDVBCISession::sendSPDU(eDVBCISlot *slot, unsigned char tag, const void *data, int len, unsigned short session_nb, const void *apdu,int alen)
 {
        unsigned char pkt[4096];
@@ -63,7 +81,7 @@ void eDVBCISession::sendSPDU(eDVBCISlot *slot, unsigned char tag, const void *da
                memcpy(ptr, apdu, alen);
 
        ptr+=alen;
-       slot->write(pkt, ptr - pkt);
+       slot->send(pkt, ptr - pkt);
 }
 
 void eDVBCISession::sendOpenSessionResponse(eDVBCISlot *slot, unsigned char session_status, const unsigned char *resource_identifier, unsigned short session_nb)
@@ -75,11 +93,27 @@ void eDVBCISession::sendOpenSessionResponse(eDVBCISlot *slot, unsigned char sess
        sendSPDU(slot, 0x92, pkt, 5, session_nb);
 }
 
+void eDVBCISession::recvCreateSessionResponse(const unsigned char *data)
+{
+       status = data[0];
+       state = stateStarted;
+       action = 1;
+       printf("create Session Response, status %x\n", status);
+}
+
+void eDVBCISession::recvCloseSessionRequest(const unsigned char *data)
+{
+       state = stateInDeletion;
+       action = 1;
+       printf("close Session Request\n");
+}
+
 eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned char *resource_identifier, unsigned char &status)
 {
        eDVBCISession *session;
        unsigned long tag;
        unsigned short session_nb;
+
        for (session_nb=1; session_nb < SLMS; ++session_nb)
                if (!sessions[session_nb-1])
                        break;
@@ -97,24 +131,25 @@ eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned cha
        switch (tag)
        {
        case 0x00010041:
-//             session=new eDVBCIResourceManagerSession;
+               session=new eDVBCIResourceManagerSession;
                printf("RESOURCE MANAGER\n");
+               printf("session: %p\n",session);
                break;
        case 0x00020041:
-//             session=eDVBCIModule::getInstance()->application_manager = new eDVBCIApplicationManagerSession;
+               session=slot->application_manager = new eDVBCIApplicationManagerSession(slot);
                printf("APPLICATION MANAGER\n");
                break;
        case 0x00030041:
-//             session=eDVBCIModule::getInstance()->ca_manager=new eDVBCICAManagerSession;
+               session=slot->ca_manager=new eDVBCICAManagerSession;
                printf("CA MANAGER\n");
                break;
        case 0x00240041:
-//             session=new eDVBCIDateTimeSession;
+               session=new eDVBCIDateTimeSession;
                printf("DATE-TIME\n");
                break;
        case 0x00400041:
-//             session=new eDVBCIMMISession;
-               printf("MMI\n");
+               session = new eDVBCIMMISession(slot);
+               printf("MMI - create session\n");
                break;
        case 0x00100041:
 //             session=new eDVBCIAuthSession;
@@ -134,27 +169,59 @@ eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned cha
        }
        printf("new session_nb: %d\n", session_nb);
        session->session_nb = session_nb;
+
        if (session)
        {
-               printf("session ok, status %02x\n", session->status);
-               status = session->getStatus();
-               if (status)
-               {
-                       delete session;
-                       session = 0;
-               }
                sessions[session_nb - 1] = session;
                session->slot = slot;
+               status = 0;
        }
        session->state = stateInCreation;
+
        return session;
 }
 
-void eDVBCISession::receiveData(const unsigned char *ptr, size_t len)
+void eDVBCISession::handleClose()
+{
+       unsigned char data[1]={0x00};
+       sendSPDU(0x96, data, 1, 0, 0);
+}
+
+int eDVBCISession::pollAll()
+{
+       for (int session_nb=1; session_nb < SLMS; ++session_nb)
+               if (sessions[session_nb-1])
+               {
+                       int r;
+
+                       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
+                               r=sessions[session_nb-1]->poll();
+
+                       if (r)
+                               return 1;
+               }
+       return 0;
+}
+
+void eDVBCISession::receiveData(eDVBCISlot *slot, const unsigned char *ptr, size_t len)
 {
        const unsigned char *pkt = (const unsigned char*)ptr;
        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");
        
        llen = parseLengthField(pkt, hlen);
        pkt += llen;
@@ -166,11 +233,84 @@ void eDVBCISession::receiveData(const unsigned char *ptr, size_t len)
                unsigned char status;
                session = createSession(slot, pkt, status);
                sendOpenSessionResponse(slot, status, pkt, session?session->session_nb:0);
-
+               
                if (session)
                {
                        session->state=stateStarted;
                        session->action=1;
                }
        }
+       else
+       {
+               unsigned session_nb;
+               session_nb=pkt[hlen-2]<<8;
+               session_nb|=pkt[hlen-1]&0xFF;
+               
+               if ((!session_nb) || (session_nb >= SLMS))
+               {
+                       printf("PROTOCOL: illegal session number %x\n", session_nb);
+                       return;
+               }
+               
+               session=sessions[session_nb-1];
+               if (!session)
+               {
+                       printf("PROTOCOL: data on closed session %x\n", session_nb);
+                       return;
+               }
+
+               switch (tag)
+               {
+               case 0x90:
+                       break;
+               case 0x94:
+                       session->recvCreateSessionResponse(pkt);
+                       break;
+               case 0x95:
+                       printf("recvCloseSessionRequest\n");
+                       session->recvCloseSessionRequest(pkt);
+                       break;
+               default:
+                       printf("INTERNAL: nyi, tag %02x.\n", tag);
+                       return;
+               }
+       }
+       
+       hlen += llen + 1; // lengthfield and tag
+
+       pkt = ((const unsigned char*)ptr) + hlen;
+       len -= hlen;
+
+       if (session)
+               while (len > 0)
+               {
+                       int alen;
+                       const unsigned char *tag=pkt;
+                       pkt+=3; // tag
+                       len-=3;
+                       hlen=parseLengthField(pkt, alen);
+                       pkt+=hlen;
+                       len-=hlen;
+
+                       //if (eDVBCIModule::getInstance()->workarounds_active & eDVBCIModule::workaroundMagicAPDULength)
+                       //{
+                       //      if (((len-alen) > 0) && ((len - alen) < 3))
+                       //      {
+                       //              printf("WORKAROUND: applying work around MagicAPDULength\n");
+                       //              alen=len;
+                       //      }
+                       //}
+                       if (session->receivedAPDU(tag, pkt, alen))
+                               session->action = 1;
+                       pkt+=alen;
+                       len-=alen;
+               }
+               
+       if (len)
+               printf("PROTOCOL: warning, TL-Data has invalid length\n");
 }
+
+eDVBCISession::~eDVBCISession()
+{
+}
+