/**
 * @file
 * @brief Trusted User Interface Low Level API
 * @copyright (C) 2020, Samsung Electronics Co., Ltd.
 */

#include <fops.h>
#include <string.h>
#include <tee_internal_api.h>
#include <tee_ta_destructor.h>

#include <tee_tui_low_api.h>
#include <tuill_defs.h>
#include "tuill_drv.h"
#include "tuill_log.h"
#include "tuill_touch_drv.h"
#include "tuiHal.h"
#include "touch_drv_engine.h"

//TODO: uncomment after SDK update
//TA_EXPORT const char *log_tag = "[tuill_touchdrv]";

void init_drv_ctx(void)
{
    TUILL_CALL_TRACE();
    memset(&drv_ctx, 0, sizeof(struct tuill_client_drv_context));
    drv_ctx.drv_state_machine = DRV_STATE_DISCONNECTED;
    drv_ctx.drv_name = TOUCH_DRIVER_NAME;
#ifdef USE_TOUCH_SYNAP
    drv_ctx.drv_id = ACC_PERM_SPI;
#else
    drv_ctx.drv_id = ACC_PERM_I2C;
#endif
    drv_ctx.drv_type  = TEE_PERIPHERAL_TOUCH;
    drv_ctx.drv_ioctl = touch_ioctl;
    drv_ctx.drv_cb    = &touch_cb;

    drv_ctx.file_ops.open = tuilld_open;
    drv_ctx.file_ops.close = tuilld_close;
    drv_ctx.file_ops.ioctl_iov = tuilld_ioctl;
    drv_ctx.file_ops.fsync = tuilld_fsync;
}

int clear_HAL(void)
{
    TUILL_CALL_TRACE();

    if (drv_ctx.tuilldrv.tuill_state & TEE_PERIPHERAL_FLAG_LOCKED) {
        stop_reading_thread();
        tuiHalTouchClose();
        drv_ctx.tuilldrv.tuill_state &= ~TEE_PERIPHERAL_FLAG_LOCKED;
    }

    return 0;
}
