/*
 * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Intel Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */


enclave{
    include "monotonic_counter_database_types.h"
    include "pse_types.h"
    include "sgx_dh.h"
    include "t_pairing_blob.h"
    trusted {
        public ae_error_t create_session_wrapper(uint64_t tick,
                                        [out] uint32_t *id,
                                        [out] pse_dh_msg1_t *dh_msg1);

        public ae_error_t exchange_report_wrapper(uint64_t  tick,
                                        uint32_t  sid,
                                        [in] sgx_dh_msg2_t *dh_msg2,
                                        [out] pse_dh_msg3_t *dh_msg3);

        public ae_error_t close_session_wrapper(uint32_t sid);

        public ae_error_t invoke_service_wrapper(uint64_t tick,
                                        [size = req_msg_size, in] uint8_t *req_msg,
                                        uint32_t  req_msg_size,
                                        [size = resp_msg_size, out] uint8_t *resp_msg,
                                        uint32_t resp_msg_size);

        public ae_error_t initialize_sqlite_database_file_wrapper(bool is_for_empty_db_creation);

        public ae_error_t ephemeral_session_m2m3_wrapper([in] pairing_blob_t* sealed_blob, 
                                        [in] pse_cse_msg2_t* pse_cse_msg2, 
                                        [out] pse_cse_msg3_t* pse_cse_msg3);

        public ae_error_t ephemeral_session_m4_wrapper([in] pse_cse_msg4_t* pse_cse_msg4);
    };

    untrusted {
        pse_op_error_t sqlite_db_init_hash_tree_table();
        pse_op_error_t sqlite_read_db(uint32_t leaf_id, 
                                        [out] pse_vmc_hash_tree_cache_t* cache);

        pse_op_error_t sqlite_write_db([in] pse_vmc_hash_tree_cache_t* cache, 
                                        uint8_t is_for_update_flag,
                                        [in]op_leafnode_flag_t* op_flag_info);

        pse_op_error_t sqlite_read_children_of_root([out]pse_vmc_children_of_root_t* children);

        pse_op_error_t sqlite_get_empty_leafnode( 
                                        [out] int* leaf_node_id,
                                        [in] sgx_measurement_t* mr_signer);

        ae_error_t psda_invoke_service_ocall([size = psda_req_msg_size, in] uint8_t* psda_req_msg, 
                                        uint32_t psda_req_msg_size, 
                                        [size = psda_resp_msg_size, out] uint8_t* psda_resp_msg, 
                                        uint32_t psda_resp_msg_size);

        pse_op_error_t sqlite_rollback_db_file();
    };
};
