/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import tv.osmosys.tci.avport.*;

public class AVInfo {
 public static final int PAD = 15;

 private static final String hdcp_props[]={
  "hdcp_state",
  "hdcp_auth",
  "hdcp_ksv"
 };

 private static String get_property(AVPort port,String name) {
  String props[]=port.getProperty(name);
 
  if (props.length>0) return props[0];
   else return null;
 }

 private static void print_hdcp_info(AVPort port) throws Throwable {
  for(int i=0;i<hdcp_props.length;i++) {
   String prop=hdcp_props[i];
   String val=get_property(port,prop);

   if (val!=null) {
    String s="   "+Utils.padded_string(prop,PAD)+" : "+val;
    Output.text(s);
   }
  }
 }

 public static void print() throws Throwable {
  AVPort avptab[]=AVPortFactory.getInstance().getAVPorts();

  if (avptab!=null) {
   for(int i=0;i<avptab.length;i++) {
    AVPort port=avptab[i];

    String s="Port["+Utils.hex_value(i,2)+"] = "+port.getStringType();
    Output.text(s);
                
    String setuptab[]=port.getProperty("setup");

    String prop="connected";
    String val=get_property(port,prop);

    if (val!=null) {
     s=" - "+Utils.padded_string(prop,PAD)+" = "+val;
     Output.text(s);
    }

    for(int j=0;j<setuptab.length;j++) {
     prop=setuptab[j];
     val=get_property(port,prop);

     if (val!=null) {
      if (prop.equals("hdcp")) {
       if (val.equals("0")) {
        val+=" (TV is turned off)";
       } else

       if (val.equals("1")) {
        val+=" (TV is turned on)";
       }
      }

      s=" - "+Utils.padded_string(prop,PAD)+" = "+val;
      Output.text(s);

      if (prop.equals("hdcp")) {
       print_hdcp_info(port);
      }
     }
    }
   }
  }
 }
}
