/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <devmon/driver_abi.h>
#include <mc/log.h>

#include "chip.h"

using namespace dev;

// 导出信息实现
extern "C" {

driver_handle_t create_chip(void* service, const char* name)
{
    try {
        if (service == nullptr || name == nullptr) {
            return nullptr;
        }
        complex_object* device = new complex_object();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating Chip: ${error}", ("error", e.what()));
        return nullptr;
    }
    return nullptr;
}

status_t init_chip(driver_handle_t device, void* csr_object, void* connector)
{
    if (device == nullptr || csr_object == nullptr || connector == nullptr) {
        return STATUS_ERROR;
    }
    complex_object*   device_object  = static_cast<complex_object*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    mc::dict*         connector_ptr  = static_cast<mc::dict*>(connector);
    bool              ret            = device_object->init(*csr_object_ptr, *connector_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_chip(driver_handle_t device)
{
    return STATUS_OK;
}

status_t stop_chip(driver_handle_t device)
{
    return STATUS_OK;
}

const char* dump_chip(driver_handle_t device)
{
    static thread_local std::string buffer;
    buffer.clear();

    if (device == nullptr) {
        elog("Error dumping complex chip: device is null");
        return buffer.c_str();
    }

    try {
        complex_object* device_object = static_cast<complex_object*>(device);
        buffer                        = device_object->dump();
        return buffer.c_str();
    } catch (const std::exception& e) {
        elog("dump_chip failed: ${exception}", ("exception", e.what()));
        buffer.clear();
        return buffer.c_str();
    } catch (...) {
        elog("dump_chip failed: unknown exception");
        buffer.clear();
        return buffer.c_str();
    }
}

device_driver_t chip_device_driver = {.device_name = "Chip",
                                      .ctor        = create_chip,
                                      .init        = init_chip,
                                      .start       = start_chip,
                                      .stop        = stop_chip,
                                      .dump        = dump_chip};

device_driver_t chip_device_driver_list[] = {chip_device_driver};
status_t        register_device_driver(device_driver_t** device_driver, uint8_t* count)
{
    *device_driver = chip_device_driver_list;
    *count         = sizeof(chip_device_driver_list) / sizeof(chip_device_driver_list[0]);
    return STATUS_OK;
}

} // extern "C"