/*
 * src/operations.c
 *
 * Copyright (C) 2013, Samsung Electronics Co., Ltd.
 *
 * TEE storage support
 */

#include "operations.h"
#include <stdlib.h>

#include <misc_defs.h>
#include <gpapi_log.h>

TAILQ_HEAD(operationsListHead, operation_node) operationsList = TAILQ_HEAD_INITIALIZER(operationsList);

int in_operations_list(TEE_OperationHandle operation)
{
    struct operation_node *item;
    TAILQ_FOREACH(item, &operationsList, node) {
        if (item->hndl == operation) {
            return 1;
        }
    }

    return 0;
}

int add_to_operations_list(TEE_OperationHandle operation)
{
    struct operation_node * item = TEE_Malloc(sizeof(struct operation_node), HINT_FILL_WITH_ZEROS);
    if (!item) {
        return -1;
    }

    item->hndl = operation;
    TAILQ_INSERT_TAIL(&operationsList, item, node);

    return 0;
}

void remove_from_operations_list(TEE_OperationHandle operation)
{
    struct operation_node *item, *tmp;

    TAILQ_FOREACH_SAFE(item, &operationsList, node, tmp) {
        if (item->hndl == operation) {
            TAILQ_REMOVE(&operationsList, item, node);
            TEE_Free(item);
            return;
        }
    }

    MB_LOGE("Panic Reason: operation wasn't found\n");
    TEE_Panic(0);
}
