1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <chrono>
17 #include <thread>
18 
19 #include "common.h"
20 #include "nativetoken_kit.h"
21 #include "securec.h"
22 #include "softbus_bus_center.h"
23 #include "token_setproc.h"
24 
25 namespace OHOS {
26 static char g_networkId[NETWORK_ID_BUF_LEN] = { 0 };
27 
OnDefNodeOnline(NodeBasicInfo * info)28 static void OnDefNodeOnline(NodeBasicInfo *info)
29 {
30     if (info == nullptr) {
31         LOGI("Online: info is null...");
32         return;
33     }
34     (void)strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, info->networkId, NETWORK_ID_BUF_LEN);
35     LOGI("Online {networkId=%s, deviceName=%s, device type=%d}", info->networkId, info->deviceName, info->deviceTypeId);
36 }
37 
OnDefNodeOffline(NodeBasicInfo * info)38 static void OnDefNodeOffline(NodeBasicInfo *info)
39 {
40     if (info == nullptr) {
41         LOGI("Offline: info is null...");
42         return;
43     }
44     LOGI(
45         "Offline {networkId=%s, deviceName=%s, device type=%d}", info->networkId, info->deviceName, info->deviceTypeId);
46 }
47 
OnDefNodeBasicInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)48 static void OnDefNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info)
49 {
50     if (info == nullptr) {
51         LOGI("InfoChanged: info is null, type=%d", type);
52         return;
53     }
54     LOGI("InfoChanged {networkId=%s, deviceName=%s}", info->networkId, info->deviceName);
55 }
56 
onDefNodeStatusChanged(NodeStatusType type,NodeStatus * status)57 static void onDefNodeStatusChanged(NodeStatusType type, NodeStatus *status)
58 {
59     if (status == nullptr) {
60         LOGI("StatusChanged: info is null, type=%d", type);
61         return;
62     }
63     LOGI("InfoChanged {networkId=%s, authStatus=%d", status->basicInfo.networkId, status->authStatus);
64 }
65 
66 static INodeStateCb g_defNodeStateCallback = {
67     .events = EVENT_NODE_STATE_MASK,
68     .onNodeOnline = OnDefNodeOnline,
69     .onNodeOffline = OnDefNodeOffline,
70     .onNodeBasicInfoChanged = OnDefNodeBasicInfoChanged,
71     .onNodeStatusChanged = onDefNodeStatusChanged,
72 };
73 
AddPermission()74 void AddPermission()
75 {
76     uint64_t tokenId;
77     const char *perms[] = {
78         OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER,
79         OHOS_PERMISSION_DISTRIBUTED_DATASYNC,
80     };
81     uint32_t permsSize = sizeof(perms) / sizeof(perms[0]);
82     NativeTokenInfoParams infoTnstance = {
83         .dcapsNum = 0,
84         .permsNum = permsSize,
85         .aclsNum = 0,
86         .dcaps = nullptr,
87         .perms = perms,
88         .acls = nullptr,
89         .processName = "dsoftbus_service",
90         .aplStr = "system_core",
91     };
92     tokenId = GetAccessTokenId(&infoTnstance);
93     SetSelfTokenID(tokenId);
94 }
95 
CheckRemoteDeviceIsNull(bool isSetNetId)96 static int CheckRemoteDeviceIsNull(bool isSetNetId)
97 {
98     int nodeNum = 0;
99     NodeBasicInfo *nodeInfo = nullptr;
100     int ret = GetAllNodeDeviceInfo(PKG_NAME, &nodeInfo, &nodeNum);
101     LOGI("[check]get node number=%d, ret=%d", nodeNum, ret);
102     if (nodeInfo != nullptr && nodeNum > 0) {
103         LOGI("[check]get netid is=%s", nodeInfo->networkId);
104         if (isSetNetId) {
105             (void)strncpy_s(g_networkId, NETWORK_ID_BUF_LEN, nodeInfo->networkId, NETWORK_ID_BUF_LEN);
106         }
107         FreeNodeInfo(nodeInfo);
108         return SOFTBUS_OK;
109     } else {
110         LOGI("[check]get nodeInfo is null");
111         return ret;
112     }
113 }
114 
TestInit()115 int32_t TestInit()
116 {
117     AddPermission();
118     std::this_thread::sleep_for(std::chrono::seconds(1));
119     int ret = RegNodeDeviceStateCb(PKG_NAME, &g_defNodeStateCallback);
120     if (ret != SOFTBUS_OK) {
121         LOGE("call reg node state callback fail, ret=%d", ret);
122         return ret;
123     }
124 
125     ret = CheckRemoteDeviceIsNull(true);
126     if (ret != SOFTBUS_OK) {
127         LOGE("get node fail,please check network, ret=%d", ret);
128         return ret;
129     }
130 
131     return SOFTBUS_OK;
132 }
133 
TestDeInit()134 int32_t TestDeInit()
135 {
136     UnregNodeDeviceStateCb(&g_defNodeStateCallback);
137     return SOFTBUS_OK;
138 }
139 
WaitOnLineAndGetNetWorkId()140 char *WaitOnLineAndGetNetWorkId()
141 {
142     while (g_networkId[0] == '\0') {
143         LOGI("wait online...");
144         std::this_thread::sleep_for(std::chrono::seconds(1));
145     }
146 
147     LOGI("JoinLnn, networkId:%s", g_networkId);
148     return g_networkId;
149 }
150 } // namespace OHOS