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