// 秒寻科技
// JS模块与WASM模块交换数据模块(js向wasm模块注入数据，js从wasm模块读取数据)
// zt
// 2023-02-27

#include "../includes/includes.h"

int16 *gExch_pwAudioDataSaveAddress = NULL;
int32 *gExch_pdwInstallBtsSaveAddress = NULL;
int32 *gExch_pdwRouteBtsSaveAddress = NULL;
int32 *pExch_pdwCurrentLocationCoord = NULL;
int32 *pExch_pdwAccAddress = NULL; // 加速度数据地址

/*************************************以下js与wasm交互接口******************************/
// js查询wasm模块安装基站存储地址
EM_PORT_API(int32 *)
exchange_js_get_install_bts_save_address()
{
    // 获得本地基站列表存储地址
    gExch_pdwInstallBtsSaveAddress = (int32 *)bts_main_get_install_bts_list();
    return gExch_pdwInstallBtsSaveAddress;
}

// js查询wasm模块路由基站存储地址
EM_PORT_API(int32 *)
exchange_js_get_route_bts_save_address()
{
    gExch_pdwRouteBtsSaveAddress = (int32 *)route_main_get_route_bts_list_save_address();
    return gExch_pdwRouteBtsSaveAddress;
}

// js查询wasm模块音频采样数据存储地址
EM_PORT_API(int16 *)
exchange_js_get_audio_data_save_address()
{
    gExch_pwAudioDataSaveAddress = (int16 *)dmdl_main_get_audio_sample_data();
    return gExch_pwAudioDataSaveAddress;
}

// js通知wasm模块开始工作
//  返回值，1：成功，0：失败
EM_PORT_API(uint8)
exchange_js_notice_wasm_start_work()
{
    uint8 byFlag = 0;

    // 开始处理音频数据
    byFlag = sysmain_audio_signal_process();
    if (!byFlag)
    {
        printf("lct err\n");
    }

    return byFlag;
}

// js查询wasm模块当前定位坐标数据存储地址
EM_PORT_API(int32 *)
exchange_js_get_current_location_coord_save_address()
{
    pExch_pdwCurrentLocationCoord = sysmain_get_current_location_coord();
    return pExch_pdwCurrentLocationCoord;
}

// js查询wasm模块加速度数据存储地址
EM_PORT_API(int32 *)
exchange_js_get_acc_save_address()
{
    pExch_pdwAccAddress = imu_get_acc_address_exchange();
    return pExch_pdwAccAddress;
}

// js通知wasm模块保存加速度数据
EM_PORT_API(void)
exchange_js_notice_wasm_save_acc()
{
    imu_save_acc_list_exchange();
}

// js查询wasm模块软件名称以及版本的数据存储地址
EM_PORT_API(uint8 *)
exchange_js_get_soft_name_and_version_save_address()
{
    return sysmain_get_soft_name_and_version_data_address();
}

/*************************************以下是释放内存******************************/
// js通知wasm模块释放内存
EM_PORT_API(void)
exchange_js_notice_wasm_free_memory()
{
    free(gExch_pdwInstallBtsSaveAddress);
    free(gExch_pdwRouteBtsSaveAddress);

    free(gExch_pwAudioDataSaveAddress);
    free(pExch_pdwCurrentLocationCoord);

    free(pExch_pdwAccAddress);

    imu_free_memory(); // 释放imu模块内存

    route_main_free_memory(); // 释放route模块内存

    sysmain_free_memory(); // 释放sysmain模块内存
}

/*************************************以下是测试用接口，发布后要去除******************************/
EM_PORT_API(void)
exchange_js_notice_wasm_show_route_bts()
{
    route_main_show_route_bts();
    return;
}

EM_PORT_API(void)
exchange_js_notice_wasm_show_install_bts()
{
    bts_main_show_install_bts();
    return;
}
