/*******************************************************************************
 * @file
 * @brief  OpenThread wrapper functions for OpenThread Dataset APIs
 *   allowing access to the thread stack in a multi-threaded environment.
 *******************************************************************************
 * # License
 * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
 *******************************************************************************
 *
 * SPDX-License-Identifier: Zlib
 *
 * The licensor of this software is Silicon Laboratories Inc.
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 *
 ******************************************************************************/

#include "sl_ot_rtos_adaptation.h"
#include <openthread/dataset.h>

#if defined(__GNUC__)
#define REAL_NAME(function) __real_##function
#define WRAPPER_NAME(function) __wrap_##function
#define OT_API_REAL_NAME(function) REAL_NAME(function)
#define OT_API_WRAPPER_NAME(function) WRAPPER_NAME(function)
// #elif defined(__IAR_SYSTEMS_ICC__)
//     #define REAL_NAME(function)             $Super$$##function
//     #define WRAPPER_NAME(function)          $Sub$$##function
//     #define OT_API_REAL_NAME(function)      $Super$$__iar_dl##function
//     #define OT_API_WRAPPER_NAME(function)   $Sub$$__iar_dl##function
#else
#error Unsupported compiler
#endif

extern bool    OT_API_REAL_NAME(otDatasetIsCommissioned)(otInstance *aInstance);
extern otError OT_API_REAL_NAME(otDatasetGeneratePskc)(const char            *aPassPhrase,
                                                       const otNetworkName   *aNetworkName,
                                                       const otExtendedPanId *aExtPanId,
                                                       otPskc                *aPskc);
extern otError OT_API_REAL_NAME(otDatasetGetActive)(otInstance *aInstance, otOperationalDataset *aDataset);
extern otError OT_API_REAL_NAME(otDatasetGetActiveTlvs)(otInstance *aInstance, otOperationalDatasetTlvs *aDataset);
extern otError OT_API_REAL_NAME(otDatasetGetPending)(otInstance *aInstance, otOperationalDataset *aDataset);
extern otError OT_API_REAL_NAME(otDatasetGetPendingTlvs)(otInstance *aInstance, otOperationalDatasetTlvs *aDataset);
extern otError OT_API_REAL_NAME(otDatasetParseTlvs)(const otOperationalDatasetTlvs *aDatasetTlvs,
                                                    otOperationalDataset           *aDataset);
extern otError OT_API_REAL_NAME(otDatasetSendMgmtActiveGet)(otInstance                           *aInstance,
                                                            const otOperationalDatasetComponents *aDatasetComponents,
                                                            const uint8_t                        *aTlvTypes,
                                                            uint8_t                               aLength,
                                                            const otIp6Address                   *aAddress);
extern otError OT_API_REAL_NAME(otDatasetSendMgmtActiveSet)(otInstance                 *aInstance,
                                                            const otOperationalDataset *aDataset,
                                                            const uint8_t              *aTlvs,
                                                            uint8_t                     aLength,
                                                            otDatasetMgmtSetCallback    aCallback,
                                                            void                       *aContext);
extern otError OT_API_REAL_NAME(otDatasetSendMgmtPendingGet)(otInstance                           *aInstance,
                                                             const otOperationalDatasetComponents *aDatasetComponents,
                                                             const uint8_t                        *aTlvTypes,
                                                             uint8_t                               aLength,
                                                             const otIp6Address                   *aAddress);
extern otError OT_API_REAL_NAME(otDatasetSendMgmtPendingSet)(otInstance                 *aInstance,
                                                             const otOperationalDataset *aDataset,
                                                             const uint8_t              *aTlvs,
                                                             uint8_t                     aLength,
                                                             otDatasetMgmtSetCallback    aCallback,
                                                             void                       *aContext);
extern otError OT_API_REAL_NAME(otDatasetSetActive)(otInstance *aInstance, const otOperationalDataset *aDataset);
extern otError OT_API_REAL_NAME(otDatasetSetActiveTlvs)(otInstance                     *aInstance,
                                                        const otOperationalDatasetTlvs *aDataset);
extern otError OT_API_REAL_NAME(otDatasetSetPending)(otInstance *aInstance, const otOperationalDataset *aDataset);
extern otError OT_API_REAL_NAME(otDatasetSetPendingTlvs)(otInstance                     *aInstance,
                                                         const otOperationalDatasetTlvs *aDataset);
extern otError OT_API_REAL_NAME(otDatasetUpdateTlvs)(const otOperationalDataset *aDataset,
                                                     otOperationalDatasetTlvs   *aDatasetTlvs);
extern otError OT_API_REAL_NAME(otNetworkNameFromString)(otNetworkName *aNetworkName, const char *aNameString);
extern void    OT_API_REAL_NAME(otDatasetConvertToTlvs)(const otOperationalDataset *aDataset,
                                                     otOperationalDatasetTlvs   *aDatasetTlvs);

bool OT_API_WRAPPER_NAME(otDatasetIsCommissioned)(otInstance *aInstance)
{
    sl_ot_rtos_acquire_stack_mutex();
    bool ret = OT_API_REAL_NAME(otDatasetIsCommissioned)(aInstance);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetGeneratePskc)(const char            *aPassPhrase,
                                                   const otNetworkName   *aNetworkName,
                                                   const otExtendedPanId *aExtPanId,
                                                   otPskc                *aPskc)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetGeneratePskc)(aPassPhrase, aNetworkName, aExtPanId, aPskc);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetGetActive)(otInstance *aInstance, otOperationalDataset *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetGetActive)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetGetActiveTlvs)(otInstance *aInstance, otOperationalDatasetTlvs *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetGetActiveTlvs)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetGetPending)(otInstance *aInstance, otOperationalDataset *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetGetPending)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetGetPendingTlvs)(otInstance *aInstance, otOperationalDatasetTlvs *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetGetPendingTlvs)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetParseTlvs)(const otOperationalDatasetTlvs *aDatasetTlvs,
                                                otOperationalDataset           *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetParseTlvs)(aDatasetTlvs, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSendMgmtActiveGet)(otInstance                           *aInstance,
                                                        const otOperationalDatasetComponents *aDatasetComponents,
                                                        const uint8_t                        *aTlvTypes,
                                                        uint8_t                               aLength,
                                                        const otIp6Address                   *aAddress)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret =
        OT_API_REAL_NAME(otDatasetSendMgmtActiveGet)(aInstance, aDatasetComponents, aTlvTypes, aLength, aAddress);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSendMgmtActiveSet)(otInstance                 *aInstance,
                                                        const otOperationalDataset *aDataset,
                                                        const uint8_t              *aTlvs,
                                                        uint8_t                     aLength,
                                                        otDatasetMgmtSetCallback    aCallback,
                                                        void                       *aContext)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret =
        OT_API_REAL_NAME(otDatasetSendMgmtActiveSet)(aInstance, aDataset, aTlvs, aLength, aCallback, aContext);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSendMgmtPendingGet)(otInstance                           *aInstance,
                                                         const otOperationalDatasetComponents *aDatasetComponents,
                                                         const uint8_t                        *aTlvTypes,
                                                         uint8_t                               aLength,
                                                         const otIp6Address                   *aAddress)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret =
        OT_API_REAL_NAME(otDatasetSendMgmtPendingGet)(aInstance, aDatasetComponents, aTlvTypes, aLength, aAddress);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSendMgmtPendingSet)(otInstance                 *aInstance,
                                                         const otOperationalDataset *aDataset,
                                                         const uint8_t              *aTlvs,
                                                         uint8_t                     aLength,
                                                         otDatasetMgmtSetCallback    aCallback,
                                                         void                       *aContext)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret =
        OT_API_REAL_NAME(otDatasetSendMgmtPendingSet)(aInstance, aDataset, aTlvs, aLength, aCallback, aContext);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSetActive)(otInstance *aInstance, const otOperationalDataset *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetSetActive)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSetActiveTlvs)(otInstance *aInstance, const otOperationalDatasetTlvs *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetSetActiveTlvs)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSetPending)(otInstance *aInstance, const otOperationalDataset *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetSetPending)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetSetPendingTlvs)(otInstance *aInstance, const otOperationalDatasetTlvs *aDataset)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetSetPendingTlvs)(aInstance, aDataset);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otDatasetUpdateTlvs)(const otOperationalDataset *aDataset,
                                                 otOperationalDatasetTlvs   *aDatasetTlvs)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otDatasetUpdateTlvs)(aDataset, aDatasetTlvs);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

otError OT_API_WRAPPER_NAME(otNetworkNameFromString)(otNetworkName *aNetworkName, const char *aNameString)
{
    sl_ot_rtos_acquire_stack_mutex();
    otError ret = OT_API_REAL_NAME(otNetworkNameFromString)(aNetworkName, aNameString);
    sl_ot_rtos_release_stack_mutex();
    return ret;
}

void OT_API_WRAPPER_NAME(otDatasetConvertToTlvs)(const otOperationalDataset *aDataset,
                                                 otOperationalDatasetTlvs   *aDatasetTlvs)
{
    sl_ot_rtos_acquire_stack_mutex();
    OT_API_REAL_NAME(otDatasetConvertToTlvs)(aDataset, aDatasetTlvs);
    sl_ot_rtos_release_stack_mutex();
}
