#include "tee_internal_api.h"
#include "logging.h"
#include "tima_ioctl.h"
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>#include<string.h>
#include <sys/ioctl.h>
#include <tee_internal_api.h>

#ifdef DEBUG
#define LOG_PREFIX                      "DRIVER_CLIENT_DBG: "
#define print_info(format, args...)     printf(LOG_PREFIX format, ##args)
#else
#define print_info(format, args...)
#endif

#define TAG "TIMA_LOGGING:"

#define BUF_SIZE 256

//static const char drv_name[] = "dev://my_driver";
//static int drv_fd = -1;

int str_len(char *str)
{
	char *s;
	for (s = str; *s; ++s);
	return(s-str);
}
char g_log_msg[LOG_MSG_SIZE];
/*=========================================================================
Function :  tima_log_msg
==========================================================================
*
* Task
* Function to read debug & secure logs and to write message in debug/secure log.
*
* Input Parameter 1:  char *buf : Pointer which points to msg which caller wants to write in WRITE case and  a buffer in which caller wants to receive logs in READ case
* Input Parameter 2: uint32_t *size : For WRITE case not important, For READ case , size of the buffer passed by caller to read logs
* Input Parameter 3: uint32_t cmd_id : Command ID defined in tima_util_log.h
*
* Note: size value may be different when this fuction returns.It can be modified by logging TA(tima_util) with exact log buffer size value.
*/

void tima_log_msg(char *buf,uint32_t *size,uint32_t cmd_id)
{
	//uint32_t paramType;
	//TEE_Param params[4];
	uint32_t strlen;

	switch (cmd_id) {
	case TIMAUTIL_DEBUG_LOG_WRITE :
	case TIMAUTIL_SECURE_LOG_WRITE :
		strlen = str_len(buf);
		if (strlen>LOG_MSG_SIZE)
		{
			printf(TAG "message length is exceeding than LOG_MSG_SIZE in logging.c\n");
			return ;
		}
		//paramType= TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE);
		//params[0].memref.size = strlen;
		break;
	/*case TIMAUTIL_DEBUG_LOG_READ :
	case TIMAUTIL_SECURE_LOG_READ :
		paramType= TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,TEE_PARAM_TYPE_VALUE_OUTPUT,TEE_PARAM_TYPE_NONE,TEE_PARAM_TYPE_NONE);
		if(size != NULL)
			params[0].memref.size = *size;
		break;*/
	default :
		printf(TAG "Wrong CommandID in tima_log_msg. CommandID is %x \n",cmd_id);
		break;
	}

	drv_fd = open(drv_name, O_RDWR, 0);
    if (drv_fd < 0) {
        printf(TAG "drv_client_open(): open error, errno = %d\n", errno);
        return;
    }
	
	int drv_ret = 0;
    struct ioctl_log_data {
        unsigned int len;
        char buf[BUF_SIZE];
    };
	
    struct ioctl_log_data data = { sizeof(struct ioctl_log_data), {""} };
    strcpy(data.buf, buf);
    drv_ret = ioctl(drv_fd, cmd_id, (unsigned long)&data);
    if (drv_ret < 0) {
        printf(TAG "drv_client_write(): write error, errno = %d\n", errno);
        return;
    }
	
    drv_ret = close(drv_fd);
    if (drv_ret < 0) {
        printf(TAG "drv_client_close(): close error, errno = %d\n", errno);
        return;
    }
	return ;
}
