Java OCAP
by Linden deCarmo


Listing One

import org.ocap.net.*;
import java.io.*;
import org.dvb.dsmcc.*;
import org.davic.net.*;

public class Listing1
{
   public static void main (String [] args )
   {
      // create the carousel object
    ServiceDomain carousel = new ServiceDomain();
    // next, create a Locator that refers to the service
    // that contains our carousel
    // NOTE: you need to use an OCAP locator and not the dvb locator.
    // ********************************
    org.ocap.net.OcapLocator ocapLocator;
    // attach the locator to the carousel so it knows where to pull data
    try
    {
        ocapLocator = new org.ocap.net.OcapLocator("ocap://10.0.0.1");
       carousel.attach(ocapLocator, 1);
            // create the DSMCC object.
            // we're using the theoretical drdobbs directory
            DSMCCObject dsmccObj;
            dsmccObj = new DSMCCObject(carousel.getMountPoint(),
                                       "drdobbs/blocking.txt");
            // next step, create a FileInputStream object from the carousel
            try
            {
                FileInputStream inputStream;
                // and read the file into our buffer. note: this will block
                // the read thread until the read completes.
                byte [] dataFile = new byte[2048];
                inputStream = new FileInputStream(dsmccObj);
               // read the first 2k of the file.
               inputStream.read( dataFile );
            }
            catch (IOException ioProblem )
            {
               // error handling goes here....carousel reads can and
               // do fail occasionally so don't forget to
               System.out.println("Carousel read error occurred " );
            }
      }
        catch (IOException ioProblem )
        {
         System.out.println("IO issues" );
        }
      catch (InvalidLocatorException badLocator)

      {
         System.out.println("locator isn't valid " );
      }

   }
}


Listing Two

import org.ocap.net.*;
import java.io.*;
import org.dvb.dsmcc.*;
import org.davic.net.*;
import java.util.*;

public class Listing2
{
   public class TestHandler implements AsynchronousLoadingEventListener
   {
      public void receiveEvent(AsynchronousLoadingEvent e)
      {
      }
   }
    public static TestHandler asyncHandler;
   public static void main (String [] args )
   {
      // create the carousel object
    ServiceDomain carousel = new ServiceDomain();
    // now create a Locator that refers to the service
    // that contains our carousel
    // ********************************
    try
    {
        org.ocap.net.OcapLocator ocapLocator;
        ocapLocator = new org.ocap.net.OcapLocator("ocap://10.0.0.1");
        // attach the locator to the carousel so it knows where to pull data
        carousel.attach(ocapLocator, 1);
        // create the DSMCC object.
        // we're using the theoretical drdobbs directory
        DSMCCObject dsmccObj;
        dsmccObj=new DSMCCObject(carousel.getMountPoint(),"drdobbs/async.txt");
        // unlike the previous sample, we have have the carousel load
        // the object asynchronously and notify our carouselListener
        // implementation when it is ready.
        // key part is this...the thread isn't blocked at this point.

        dsmccObj.asynchronousLoad(asyncHandler);
      }
        catch (IOException ioProblem )
        {
         System.out.println("IO issues" );
        }
      catch (InvalidLocatorException badLocator)

         System.out.println("locator isn't valid " );
      }
   }
}


Listing Three

import org.dvb.application.*;
import org.ocap.application.*;
import org.ocap.resource.*;

public class Listing3
   extends AppsDatabaseFilter
   implements ResourceContentionHandler
{
   private static final int KNOWN_HACKER = 0xBADD;
   private static final int VOD_APP      = 0xFACE;
   // bare bones class that installs a resource conflict handler.
   public Listing3()
   {
      super();
      ResourceContentionManager rcManager =
                                   ResourceContentionManager.getInstance();
      // tells OCAP that we want to be approve application launching.
      rcManager.setResourceFilter(this,
                             "org.davic.mpeg.sections.SectionFilterGroup");
      // and this tells OCAP stack where to call if/when a conflict occurs.
      rcManager.setResourceContentionHandler(this);
   }
   // here's where we do the dirty work of resolving resource contentions....
   // this is a VERY primitive handler simply for illustration purposes.
   public AppID[] resolveResourceContention( AppID requester, AppID owners[],
                                             java.lang.String resourceProxy)
   {
      AppID result[] = new AppID[owners.length + 1];
      if(resourceProxy.equals("org.davic.mpeg.sections.SectionFilterGroup"))
      {
         // VOD apps have the highest priority
         if(requester.getOID() == VOD_APP)
         {
            result[0] = requester;
            for(int loop=0; loop < owners.length; loop++)
            {
               result[loop+1] = owners[loop];
            }
         }
         else
         {
            // every other request is simply a FIFO
            for(int loop=0; loop<owners.length; loop++)
            {
               result[loop] = owners[loop];
            }
            result[owners.length] = requester;
         }
      }
      return(result);
   }
   // very primitive check to prevent hacker app from being launched.
   public boolean accept(AppID appid)
   {
      boolean bAnswer = true;
      if(appid.getOID() == KNOWN_HACKER)
      {
         bAnswer = false;
      }
      return(bAnswer);
   }
}


Listing Four

import org.ocap.hardware.*;

public class Listing4
{
   public static void main (String [] args )
   {
        boolean  bConstrainVideo = true;
    try
    {
       // this call will constrain ALL outputs, not just the
       // component video output use with caution!!!
       org.ocap.hardware.CopyControl.setDownRes(bConstrainVideo);
    }
    catch (java.lang.SecurityException securityExcept)
    {
       // you will get this exception if you don't have the monitor
       //app permission "setDownRes"
         System.out.println("you don't have monitor  app permission: setDownRes " );
    }
   }
}


Listing Five

import org.ocap.hardware.pod.*;
public class Listing5
{
   public static void main (String [] args )
   {
   int cciBits = 0;
   // figure out what the current CCI settings is for the stream......
    cciBits = org.ocap.hardware.CopyControl.getCCIBits();
   System.out.println("CCI bits are: " + cciBits );
   }
}


Listing Six

import org.ocap.hardware.*;
import java.lang.*;
import java.util.*;

public class Listing6
{
   public static void main (String [] args )
   {
      Host              settopBox;
      VideoOutputPort   videoOutput;
      settopBox = org.ocap.hardware.Host.getInstance();
      Enumeration   videooutputPorts = settopBox.getVideoOutputPorts();
      // this is a very simple list walker
      while ( videooutputPorts.hasMoreElements() )
      {
         videoOutput = (VideoOutputPort)videooutputPorts.nextElement();
         if (videoOutput.getType() == VideoOutputPort.AV_OUTPUT_PORT_TYPE_DVI)
         {
            System.out.println("This is a DVI port!");
            if ( videoOutput.queryCapability
                   (VideoOutputPort.CAPABILITY_TYPE_HDCP)  ==  Boolean.TRUE  )
            {
               System.out.println("This video port offers HDCP security!");
            }
         }
         else  if ( videoOutput.getType() ==
                       VideoOutputPort.AV_OUTPUT_PORT_TYPE_COMPONENT_VIDEO )
         {
            System.out.println("This is an analog copmonent video port!");
         }
      }
   }
}


Listing Seven

import org.ocap.hardware.*;
import java.lang.*;
import java.util.*;

public class
{
   public static void main (String [] args )
      Host                                settopBox;
      org.ocap.hardware.VideoOutputPort   videoOutput;
      settopBox = org.ocap.hardware.Host.getInstance();
      Enumeration   videooutputPorts = settopBox.getVideoOutputPorts();
      // this is a very simple list walker
      while ( videooutputPorts.hasMoreElements() )
      {
         videoOutput = (VideoOutputPort)videooutputPorts.nextElement();
         if ( videoOutput.getType() ==
                             VideoOutputPort.AV_OUTPUT_PORT_TYPE_1394  )
         {
            System.out.println("This is a 1394 port!");
            org.ocap.hardware.IEEE1394Node[] firewireNodes =
                                         videoOutput.getIEEE1394Node();
            for (int loop = 0; loop < firewireNodes.length; loop++ )
            {
               // first node is the OCAP stack in the set-top box ignore it.
               if ( loop == 0 )
               {
               }
               else
               {
                  // for this sample, grab and use the first valid port
                  try
                  {
                     short[] subunits = firewireNodes[ loop].getSubunitType();

                     // tell OCAP stack to use this device for video output
                     videoOutput.selectIEEE1394Sink(firewireNodes
                                         [ loop].getEUI64(), subunits[0] );
                     // if everything ok, break out of the loop, given that
                     // we successfully set up the video output.
                     break;
                  }
                  catch(SecurityException SecurityException )
                  {
                     System.out.println("You don't have the
                                            right permission (setVideoPort)");
                  }
               }
            }
         }
      }
   }
}


Listing Eight

import org.ocap.hardware.pod.*;
public class Listing8
{
   public static void main (String [] args )
   {
      // this information can be found in the HOST-POD interface
      // specification (SCTE 28)
      int parentPIN = 2;
      // it gets hairy talking with a CableCARD given that all the parameters
      // are datastructures, but in this case, the first field is the length
      // and the second field is the value.
      byte[] parentalPin = {0X1, 0X7F};
      byte[] value = pod.getHostParam(parentPIN);
   }
}


Listing Nine

import org.ocap.hardware.pod.*;
public class Listing9
{
   public static void main (String [] args )
   {
      POD cableCARD = POD.getInstance();
      PODApplication[] apps = cableCARD.getApplications();
      for ( int i = 0; i < apps.length; i++ )
      {
         System.out.println("Application name is: " + apps[i].getName());
         // is this a conditional access application????
         if (apps[i].getType() == PODApplication.TYPE_CA )
         {
            // yep!  We found our target app, so break out of the loop
            System.out.println("Found a conditional access application"  + apps[i].getName() );
            break;
         }
      }
   }
}


