10704ebd2Sopenharmony_ci /*
20704ebd2Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License.
50704ebd2Sopenharmony_ci * You may obtain a copy of the License at
60704ebd2Sopenharmony_ci *
70704ebd2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
80704ebd2Sopenharmony_ci *
90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and
130704ebd2Sopenharmony_ci * limitations under the License.
140704ebd2Sopenharmony_ci */
150704ebd2Sopenharmony_ci
160704ebd2Sopenharmony_ci#include "napi/native_api.h"
170704ebd2Sopenharmony_ci#include <cstdlib>
180704ebd2Sopenharmony_ci#include <js_native_api_types.h>
190704ebd2Sopenharmony_ci#include <tuple>
200704ebd2Sopenharmony_ci#include <unistd.h>
210704ebd2Sopenharmony_ci#include "device_manager.h"
220704ebd2Sopenharmony_ci#include "distributed_file_daemon_manager.h"
230704ebd2Sopenharmony_ci
240704ebd2Sopenharmony_ciclass FileIoClient : public OHOS::DistributedHardware::DmInitCallback {
250704ebd2Sopenharmony_ci    void OnRemoteDied() {
260704ebd2Sopenharmony_ci    }
270704ebd2Sopenharmony_ci};
280704ebd2Sopenharmony_ci
290704ebd2Sopenharmony_cistatic napi_value DeviceOpenP2PConnection(napi_env env, napi_callback_info info)
300704ebd2Sopenharmony_ci{
310704ebd2Sopenharmony_ci    std::shared_ptr<OHOS::DistributedHardware::DmInitCallback> initCallback = std::make_shared<FileIoClient>();
320704ebd2Sopenharmony_ci    auto &deviceManager = OHOS::DistributedHardware::DeviceManager::GetInstance();
330704ebd2Sopenharmony_ci    deviceManager.InitDeviceManager("com.acts.fileio.test.server", initCallback);
340704ebd2Sopenharmony_ci    std::vector<OHOS::DistributedHardware::DmDeviceInfo> deviceList;
350704ebd2Sopenharmony_ci    deviceManager.GetTrustedDeviceList("com.acts.fileio.test.server", "", deviceList);
360704ebd2Sopenharmony_ci    auto &dfsmanager = OHOS::Storage::DistributedFile::DistributedFileDaemonManager::GetInstance();
370704ebd2Sopenharmony_ci    int32_t ret = dfsmanager.OpenP2PConnection(deviceList[0]);
380704ebd2Sopenharmony_ci    napi_value result = nullptr;
390704ebd2Sopenharmony_ci    napi_create_int32(env, ret, &result);
400704ebd2Sopenharmony_ci    return result;
410704ebd2Sopenharmony_ci}
420704ebd2Sopenharmony_ci
430704ebd2Sopenharmony_ciEXTERN_C_START
440704ebd2Sopenharmony_cistatic napi_value Init(napi_env env, napi_value exports)
450704ebd2Sopenharmony_ci{
460704ebd2Sopenharmony_ci    napi_property_descriptor desc[] = {
470704ebd2Sopenharmony_ci        {"DeviceOpenP2PConnection", nullptr, DeviceOpenP2PConnection, nullptr, nullptr, nullptr, napi_default, nullptr},
480704ebd2Sopenharmony_ci    };
490704ebd2Sopenharmony_ci
500704ebd2Sopenharmony_ci    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
510704ebd2Sopenharmony_ci    return exports;
520704ebd2Sopenharmony_ci}
530704ebd2Sopenharmony_ciEXTERN_C_END
540704ebd2Sopenharmony_ci
550704ebd2Sopenharmony_cistatic napi_module demoModule = {
560704ebd2Sopenharmony_ci    .nm_version = 1,
570704ebd2Sopenharmony_ci    .nm_flags = 0,
580704ebd2Sopenharmony_ci    .nm_filename = nullptr,
590704ebd2Sopenharmony_ci    .nm_register_func = Init,
600704ebd2Sopenharmony_ci    .nm_modname = "libdevicemanager",
610704ebd2Sopenharmony_ci    .nm_priv = ((void *)0),
620704ebd2Sopenharmony_ci    .reserved = {0},
630704ebd2Sopenharmony_ci};
640704ebd2Sopenharmony_ci
650704ebd2Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
66