1 /*
2  * Copyright (c) 2023-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 "device_manager_impl_lite_m.h"
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include "cJSON.h"
23 
24 #include "device_manager_common.h"
25 #include "hichain_adapter.h"
26 #include "softbus_adapter.h"
27 
28 static const char * const FILED_PKG_NAME = "pkgName";
29 static const char * const FILED_BIND_TYPE = "bindType";
30 static const char * const FILED_SUBSCRIBE_ID = "subscribeId";
31 
32 static volatile bool g_deviceManagerInitFlag = false;
33 
InitDeviceManager(void)34 int InitDeviceManager(void)
35 {
36     DMLOGI("init device manager start.");
37     if (g_deviceManagerInitFlag) {
38         DMLOGE("device manager module has been initialized.");
39         return DM_OK;
40     }
41     int retValue = InitSoftbusModle();
42     if (retValue != DM_OK) {
43         DMLOGE("failed to init softbus with ret: %d.", retValue);
44         return retValue;
45     }
46     retValue = InitHichainModle();
47     if (retValue != DM_OK) {
48         DMLOGE("failed to init hichain with ret: %d.", retValue);
49         return retValue;
50     }
51     g_deviceManagerInitFlag = true;
52     return DM_OK;
53 }
54 
UnInitDeviceManager(void)55 int UnInitDeviceManager(void)
56 {
57     DMLOGI("device manager UnInitDeviceManager start.");
58     if (!g_deviceManagerInitFlag) {
59         DMLOGI("device manager module not initialized.");
60         return DM_OK;
61     }
62     UnInitSoftbusModle();
63     UnInitHichainModle();
64     g_deviceManagerInitFlag = false;
65     return DM_OK;
66 }
67 
RegisterDevStateCallback(const char * pkgName,const char * extra,DevStatusCallback callback)68 int RegisterDevStateCallback(const char *pkgName, const char *extra, DevStatusCallback callback)
69 {
70     DMLOGI("device manager RegisterDevStateCallback start.");
71     (void)extra;
72     if (!g_deviceManagerInitFlag) {
73         DMLOGE("device manager module is not initialized.");
74         return ERR_DM_NO_INIT;
75     }
76     return RegisterSoftbusDevStateCallback(pkgName, callback);
77 }
78 
UnRegisterDevStateCallback(const char * pkgName)79 int UnRegisterDevStateCallback(const char *pkgName)
80 {
81     DMLOGI("device manager UnRegisterDevStateCallback start.");
82     if (!g_deviceManagerInitFlag) {
83         DMLOGE("device manager module is not initialized.");
84         return ERR_DM_NO_INIT;
85     }
86     return UnRegisterSoftbusDevStateCallback(pkgName);
87 }
88 
GetTrustedList(const char * pkgName,DmDeviceBasicInfo * deviceList,const int deviceListLen,int * trustListLen)89 int GetTrustedList(const char *pkgName, DmDeviceBasicInfo *deviceList, const int deviceListLen, int *trustListLen)
90 {
91     DMLOGI("device manager GetTrustedList start.");
92     if (!g_deviceManagerInitFlag) {
93         DMLOGE("device manager module is not initialized.");
94         return ERR_DM_NO_INIT;
95     }
96     return GetSoftbusTrustedDeviceList(pkgName, deviceList, deviceListLen, trustListLen);
97 }
98 
StartAdvertising(const char * advParam,const char * filterOption,OnAdvertisingResult cb)99 int StartAdvertising(const char *advParam, const char *filterOption, OnAdvertisingResult cb)
100 {
101     DMLOGI("device manager StartAdvertising start.");
102     (void)filterOption;
103     if (!g_deviceManagerInitFlag) {
104         DMLOGE("device manager module is not initialized.");
105         return ERR_DM_NO_INIT;
106     }
107     if (advParam == NULL) {
108         DMLOGE("input param is invalid.");
109         return ERR_DM_INPUT_INVALID_VALUE;
110     }
111     cJSON *obj = cJSON_Parse(advParam);
112     if (obj == NULL) {
113         DMLOGE("StartAdvertising parse advParam failed.");
114         return ERR_DM_INPUT_INVALID_VALUE;
115     }
116     cJSON *pkgNameObj = cJSON_GetObjectItem(obj, FILED_PKG_NAME);
117     if (pkgNameObj == NULL || !cJSON_IsString(pkgNameObj)) {
118         DMLOGE("parse pkgName failed.");
119         cJSON_Delete(obj);
120         return ERR_DM_INPUT_INVALID_VALUE;
121     }
122     char *pkgName = pkgNameObj->valuestring;
123     int ret = StartSoftbusPublish(pkgName, cb);
124     cJSON_Delete(obj);
125     return ret;
126 }
127 
StopAdvertising(const char * pkgName)128 int StopAdvertising(const char *pkgName)
129 {
130     DMLOGI("device manager stop advertising start.");
131     if (!g_deviceManagerInitFlag) {
132         DMLOGE("device manager module is not initialized.");
133         return ERR_DM_NO_INIT;
134     }
135     return StopSoftbusPublish(pkgName);
136 }
137 
StartDiscovering(const char * discoverParam,const char * filterOption,OnTargetFound callback)138 int StartDiscovering(const char *discoverParam, const char *filterOption, OnTargetFound callback)
139 {
140     DMLOGI("device manager start discovering start.");
141     if (!g_deviceManagerInitFlag) {
142         DMLOGE("device manager module is not initialized.");
143         return ERR_DM_NO_INIT;
144     }
145     if (discoverParam == NULL) {
146         DMLOGE("input param is invalid.");
147         return ERR_DM_INPUT_INVALID_VALUE;
148     }
149     cJSON *obj = cJSON_Parse(discoverParam);
150     if (obj == NULL) {
151         DMLOGE("StartDiscovering parse advParam failed.");
152         return ERR_DM_INPUT_INVALID_VALUE;
153     }
154     cJSON *pkgNameObj = cJSON_GetObjectItem(obj, FILED_PKG_NAME);
155     if (pkgNameObj == NULL || !cJSON_IsString(pkgNameObj)) {
156         cJSON_Delete(obj);
157         DMLOGE("parse pkgName failed.");
158         return ERR_DM_INPUT_INVALID_VALUE;
159     }
160     char *pkgName = pkgNameObj->valuestring;
161     cJSON *subscribeIdObj = cJSON_GetObjectItem(obj, FILED_SUBSCRIBE_ID);
162     if (subscribeIdObj == NULL || !cJSON_IsNumber(subscribeIdObj)) {
163         DMLOGE("parse subscrilbe id failed.");
164         cJSON_Delete(obj);
165         return ERR_DM_INPUT_INVALID_VALUE;
166     }
167     int subscribeId = subscribeIdObj->valueint;
168     int ret = StartSoftbusDiscovery(pkgName, subscribeId, filterOption, callback);
169     cJSON_Delete(obj);
170     return ret;
171 }
172 
StopDiscovering(const char * pkgName,const int subscribeId)173 int StopDiscovering(const char *pkgName, const int subscribeId)
174 {
175     DMLOGI("device manager StopDiscovering start.");
176     if (!g_deviceManagerInitFlag) {
177         DMLOGE("device manager module is not initialized.");
178         return ERR_DM_NO_INIT;
179     }
180     return StopSoftbusDiscovery(pkgName, subscribeId);
181 }
182 
BindTarget(const char * pkgName,const char * deviceId,const char * bindParam,OnBindResult cb)183 int BindTarget(const char *pkgName, const char *deviceId, const char *bindParam, OnBindResult cb)
184 {
185     DMLOGI("device manager BindTarget start.");
186     if (!g_deviceManagerInitFlag) {
187         DMLOGE("device manager module is not initialized.");
188         return ERR_DM_NO_INIT;
189     }
190     if (bindParam == NULL) {
191         DMLOGE("bind target bind param is null.");
192         return ERR_DM_POINT_NULL;
193     }
194     cJSON *obj = cJSON_Parse(bindParam);
195     if (obj == NULL) {
196         DMLOGE("StartDiscovering parse advParam failed.");
197         return ERR_DM_CJSON_PARSE_STRING;
198     }
199     cJSON *bindTypeObj = cJSON_GetObjectItem(obj, FILED_BIND_TYPE);
200     if (bindTypeObj == NULL || !cJSON_IsNumber(bindTypeObj)) {
201         DMLOGE("parse bind type failed.");
202         cJSON_Delete(obj);
203         return ERR_DM_INPUT_INVALID_VALUE;
204     }
205     int bindType = bindTypeObj->valueint;
206     int ret = SoftbusBindTarget(pkgName, deviceId, bindType, cb);
207     cJSON_Delete(obj);
208     return ret;
209 }
210 
UnBindTarget(const char * pkgName,const char * networkId)211 int UnBindTarget(const char *pkgName, const char *networkId)
212 {
213     DMLOGI("device manager UnBindTarget start.");
214     if (!g_deviceManagerInitFlag) {
215         DMLOGE("device manager module is not initialized.");
216         return ERR_DM_NO_INIT;
217     }
218     return SoftbusUnBindTarget(pkgName, networkId);
219 }