/**
 * Copyright (C) 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Mobile Communication Division,
 * Digital Media & Communications Business, Samsung Electronics Co., Ltd.
 *
 * This software and its documentation are confidential and proprietary
 * information of Samsung Electronics Co., Ltd.  No part of the software and
 * documents may be copied, reproduced, transmitted, translated, or reduced to
 * any electronic medium or machine-readable form without the prior written
 * consent of Samsung Electronics.
 *
 * Samsung Electronics makes no representations with respect to the contents,
 * and assumes no responsibility for any errors that might appear in the
 * software and documents. This publication and the contents hereof are subject
 * to change without notice.
 *
 */
/**
 * @file hdcp2_repeater.cpp
 * @author
 * @date
 * @brief This file contains Authentication messages functions for Repeater which interact with TrustZone.
 */

#include <hdcp2.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include "properties.h"

static int type = -1;
#ifdef USE_MOBICORE
static int USE_BUFFER_PROTECTION = 1;
#endif /* USE_MOBICORE*/

/**
 * @fn nt RepeaterAuth_Send_ReceiverId_List_Rep(HDCP2_Ctx *hdcp, u8 *payload)
 * @brief This function sends the message containing the information regarding the receivers connected to the repeater
 * @param hdcp - pointer to HDCP context
 * @param received - pointer to the REPEATERAUTH_SEND_RECEIVER_ID_LIST21 message send by the repeater
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int RepeaterAuth_Send_ReceiverId_List_Rep(HDCP2_Ctx *hdcp, u8 *payload)
{
	int ret = 0;

	if (hdcp->version == HDCP2_VERSION_2_0)   {
		ret = TZ_COMMAND_R(CMD_REPEATERAUTH_SEND_RECEIVER_ID_LIST, NULL, 0, payload, sizeof(REPEATERAUTH_SEND_RECEIVER_ID_LIST20));
		HDCP2_LogHex("[R>>>T] RepeaterAuth_Send_ReceiverId_List(2.0)", payload, sizeof(REPEATERAUTH_SEND_RECEIVER_ID_LIST20));
		return ret;
	} else { /*for HDCP2.1 and HDCP 2.2*/
		ret = TZ_COMMAND_R(CMD_REPEATERAUTH_SEND_RECEIVER_ID_LIST, NULL, 0, payload, sizeof(REPEATERAUTH_SEND_RECEIVER_ID_LIST21));
		HDCP2_LogHex("[R>>>T] RepeaterAuth_Send_ReceiverId_List(2.1/2.2)", payload, sizeof(REPEATERAUTH_SEND_RECEIVER_ID_LIST21));
		return ret;
	}
}

/**
 * @fn int RepeaterAuth_Send_Ack_Rep(HDCP2_Ctx *hdcp, u8 *received)
 * @brief This function receives an acknowledgment from the transmitter
 * @param hdcp - pointer to HDCP context
 * @param received - pointer to  REPEATERAUTH_SEND_ACK message that is sent by the transmitter as an acknowledgment to REPEATERAUTH_SEND_RECEIVER_ID_LIST21 sent in the previous message.
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int RepeaterAuth_Send_Ack_Rep(HDCP2_Ctx *hdcp, u8 *received)
{
	int ret = 0;
	HDCP2_LogHex("[R<<<T] RepeaterAuth_Send_Ack", received, sizeof(REPEATERAUTH_SEND_ACK));
	ret = TZ_COMMAND_R(CMD_REPEATERAUTH_SEND_ACK, received, sizeof(REPEATERAUTH_SEND_ACK), NULL, 0);
	return ret;
}

/**
 * @fn int Receiver_AuthStatus_Rep(HDCP2_Ctx *hdcp, u8 *payload)
 * @brief This function lets the transmitter know about the authentication status of receiver with the repeater
 * @param hdcp - pointer to HDCP context
 * @param payload - pointer to RECEIVER_AUTHSTATUS message sent by the repeater
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int Receiver_AuthStatus_Rep(HDCP2_Ctx *hdcp, u8 *payload)
{
	int ret = 0;
	ret = TZ_COMMAND_R(CMD_RECEIVER_AUTHSTATUS, NULL, 0, payload, sizeof(RECEIVER_AUTHSTATUS));
	HDCP2_LogHex("[R>>>T] Receiver_AuthStatus", payload, sizeof(RECEIVER_AUTHSTATUS));
	return ret;
}

/**
 * @fn int RepeaterAuth_Stream_Manage_Rep(HDCP2_Ctx *hdcp, u8 *received)
 * @brief This function propagates content stream management information, which includes Type values assigned to the content streams
 * @param hdcp - pointer to HDCP context
 * @param received - pointer to REPEATERAUTH_STREAM_MANAGE message received by the repeater
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int RepeaterAuth_Stream_Manage_Rep(HDCP2_Ctx *hdcp, u8 *received)
{
	int ret = 0;
	HDCP2_LogHex("[R<<<T] RepeaterAuth_Stream_Manage", received, sizeof(REPEATERAUTH_STREAM_MANAGE));
	ret = TZ_COMMAND_R(CMD_REPEATERAUTH_STREAM_MANAGE, received, sizeof(REPEATERAUTH_STREAM_MANAGE), NULL, 0);
	return ret;
}

/**
 * @fn int RepeaterAuth_Stream_Ready_Rep(HDCP2_Ctx *hdcp, u8 *payload)
 * @brief This function sends M- prime to the transmitter
 * @param hdcp - pointer to HDCP context
 * @param payload - pointer to REPEATERAUTH_STREAM_READY message sent by the transmitter
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int RepeaterAuth_Stream_Ready_Rep(HDCP2_Ctx *hdcp, u8 *payload)
{
	int ret = 0;
	ret = TZ_COMMAND_R(CMD_REPEATERAUTH_STREAM_READY, NULL, 0, payload, sizeof(REPEATERAUTH_STREAM_READY));
	HDCP2_LogHex("[R>>>T] RepeaterAuth_Stream_Ready", payload, sizeof(REPEATERAUTH_STREAM_READY));
	return ret;
}

/**
 * @fn int HDCP2_Get_Repeater_Downstream_Status(HDCP2_Ctx *hdcp, u8 *repeater20, u8 *receiver1x)
 * @brief gets the status for downstream propagation in repeater
 * @param hdcp - pointer to HDCP context
 * @param repeater20 - pointer to  info that whether a repeater is connected downstream and 2.0 repeater
 * @param receiver1x - pointer to  info that whether a repeater is connected downstream and 1.x rx device
 * @return int - HDCP2_OK in case of success, else error code corresponding to the error
 */
int HDCP2_Get_Repeater_Downstream_Status(HDCP2_Ctx *hdcp, u8 *repeater20, u8 *receiver1x)
{
	if (!hdcp)
		return HDCP2_ERR_INVALID_INPUT;

	*repeater20 = hdcp->hdcp2_legacy_device_downstream;
	*receiver1x = hdcp->hdcp1_device_downstream;

	return HDCP2_OK;
}
