1 /*
2 * Copyright (C) 2021-2022 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 "bind_session_common.h"
17 #include "bind_session_common_util.h"
18 #include "callback_manager.h"
19 #include "channel_manager.h"
20 #include "data_manager.h"
21 #include "dev_auth_module_manager.h"
22 #include "group_operation_common.h"
23 #include "hc_dev_info.h"
24 #include "hc_log.h"
25 #include "hc_types.h"
26 #include "session_common.h"
27
AddGroupInfoToSendData(const BindSession * session,CJson * data)28 static int32_t AddGroupInfoToSendData(const BindSession *session, CJson *data)
29 {
30 const char *groupId = GetStringFromJson(session->params, FIELD_GROUP_ID);
31 if (groupId == NULL) {
32 LOGE("Failed to get groupId from params!");
33 return HC_ERR_JSON_GET;
34 }
35 const char *groupName = GetStringFromJson(session->params, FIELD_GROUP_NAME);
36 if (groupName == NULL) {
37 LOGE("Failed to get groupName from params!");
38 return HC_ERR_JSON_GET;
39 }
40 if (AddStringToJson(data, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
41 LOGE("Failed to add groupId to data!");
42 return HC_ERR_JSON_FAIL;
43 }
44 if (AddStringToJson(data, FIELD_GROUP_NAME, groupName) != HC_SUCCESS) {
45 LOGE("Failed to add groupName to data!");
46 return HC_ERR_JSON_FAIL;
47 }
48 if (AddIntToJson(data, FIELD_GROUP_OP, session->opCode) != HC_SUCCESS) {
49 LOGE("Failed to add groupOp to data!");
50 return HC_ERR_JSON_FAIL;
51 }
52 if (AddIntToJson(data, FIELD_GROUP_TYPE, PEER_TO_PEER_GROUP) != HC_SUCCESS) {
53 LOGE("Failed to add groupType to data!");
54 return HC_ERR_JSON_FAIL;
55 }
56 return HC_SUCCESS;
57 }
58
AddDevInfoToSendData(const BindSession * session,CJson * data)59 static int32_t AddDevInfoToSendData(const BindSession *session, CJson *data)
60 {
61 const char *authId = GetStringFromJson(session->params, FIELD_AUTH_ID);
62 if (authId == NULL) {
63 LOGE("Failed to get authId from params!");
64 return HC_ERR_JSON_GET;
65 }
66 const char *udid = GetStringFromJson(session->params, FIELD_CONN_DEVICE_ID);
67 if (udid == NULL) {
68 LOGE("Failed to get udid from params!");
69 return HC_ERR_JSON_GET;
70 }
71 if (AddStringToJson(data, FIELD_PEER_DEVICE_ID, authId) != HC_SUCCESS) {
72 LOGE("Failed to add peerDeviceId to data!");
73 return HC_ERR_JSON_FAIL;
74 }
75 if (AddStringToJson(data, FIELD_CONN_DEVICE_ID, udid) != HC_SUCCESS) {
76 LOGE("Failed to add connDeviceId to data!");
77 return HC_ERR_JSON_FAIL;
78 }
79 return HC_SUCCESS;
80 }
81
AddRequestInfoToSendData(const BindSession * session,CJson * data)82 static int32_t AddRequestInfoToSendData(const BindSession *session, CJson *data)
83 {
84 if (AddStringToJson(data, FIELD_APP_ID, session->base.appId) != HC_SUCCESS) {
85 LOGE("Failed to add appId to data!");
86 return HC_ERR_JSON_FAIL;
87 }
88 if (AddInt64StringToJson(data, FIELD_REQUEST_ID, session->reqId) != HC_SUCCESS) {
89 LOGE("Failed to add requestId to data!");
90 return HC_ERR_JSON_FAIL;
91 }
92 if (AddStringToJson(data, FIELD_OWNER_NAME, "") != HC_SUCCESS) {
93 LOGE("Failed to add ownerName to data!");
94 return HC_ERR_JSON_FAIL;
95 }
96 return HC_SUCCESS;
97 }
98
GenerateCompatibleInfo(CJson * groupInfo)99 static int32_t GenerateCompatibleInfo(CJson *groupInfo)
100 {
101 char udid[INPUT_UDID_LEN] = { 0 };
102 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
103 if (res != HC_SUCCESS) {
104 LOGE("Failed to get local udid! res: %d", res);
105 return HC_ERR_DB;
106 }
107 if (AddStringToJson(groupInfo, FIELD_DEVICE_ID, udid) != HC_SUCCESS) {
108 LOGE("Failed to add deviceId to groupInfo!");
109 return HC_ERR_JSON_FAIL;
110 }
111 if (AddBoolToJson(groupInfo, FIELD_IS_UUID, true) != HC_SUCCESS) {
112 LOGE("Failed to add uuIdAsDeviceId to groupInfo!");
113 return HC_ERR_JSON_FAIL;
114 }
115 /* To be compatible with packets of earlier versions. */
116 CJson *managers = CreateJsonArray();
117 if (managers == NULL) {
118 LOGE("Failed to allocate managers memory!");
119 return HC_ERR_JSON_FAIL;
120 }
121 if (AddObjToJson(groupInfo, FIELD_GROUP_MANAGERS, managers) != HC_SUCCESS) {
122 LOGE("Failed to add groupManagers to groupInfo!");
123 FreeJson(managers);
124 return HC_ERR_JSON_FAIL;
125 }
126 FreeJson(managers);
127 /* Currently, only the public group can be created. */
128 if (AddIntToJson(groupInfo, FIELD_GROUP_VISIBILITY, GROUP_VISIBILITY_PUBLIC) != HC_SUCCESS) {
129 LOGE("Failed to add groupVisibility to groupInfo!");
130 return HC_ERR_JSON_FAIL;
131 }
132 return HC_SUCCESS;
133 }
134
AddCompatibleInfoToSendData(bool isNeedCompatibleInfo,CJson * data)135 static int32_t AddCompatibleInfoToSendData(bool isNeedCompatibleInfo, CJson *data)
136 {
137 if (!isNeedCompatibleInfo) {
138 return HC_SUCCESS;
139 }
140 CJson *groupInfo = CreateJson();
141 if (groupInfo == NULL) {
142 LOGE("Failed to allocate groupInfo memory!");
143 return HC_ERR_ALLOC_MEMORY;
144 }
145 if (GenerateCompatibleInfo(groupInfo) != HC_SUCCESS) {
146 FreeJson(groupInfo);
147 return HC_ERR_JSON_FAIL;
148 }
149 if (AddObjToJson(data, FIELD_GROUP_INFO, groupInfo) != HC_SUCCESS) {
150 LOGE("Failed to add groupInfo to sendData!");
151 FreeJson(groupInfo);
152 return HC_ERR_JSON_FAIL;
153 }
154 FreeJson(groupInfo);
155 return HC_SUCCESS;
156 }
157
AddGroupAndDevInfoToParams(const BindSession * session,CJson * moduleParams)158 static int32_t AddGroupAndDevInfoToParams(const BindSession *session, CJson *moduleParams)
159 {
160 const char *groupId = GetStringFromJson(session->params, FIELD_GROUP_ID);
161 if (groupId == NULL) {
162 LOGE("Failed to get groupId from params!");
163 return HC_ERR_JSON_GET;
164 }
165 const char *authId = GetStringFromJson(session->params, FIELD_AUTH_ID);
166 if (authId == NULL) {
167 LOGE("Failed to get authId from params!");
168 return HC_ERR_JSON_GET;
169 }
170 int32_t userType = DEVICE_TYPE_ACCESSORY;
171 if (GetIntFromJson(session->params, FIELD_USER_TYPE, &userType) != HC_SUCCESS) {
172 LOGE("Failed to get userType from params!");
173 return HC_ERR_JSON_GET;
174 }
175 if (AddStringToJson(moduleParams, FIELD_SERVICE_TYPE, groupId) != HC_SUCCESS) {
176 LOGE("Failed to add serviceType to moduleParams!");
177 return HC_ERR_JSON_FAIL;
178 }
179 if (AddStringToJson(moduleParams, FIELD_SELF_AUTH_ID, authId) != HC_SUCCESS) {
180 LOGE("Failed to add serviceType to moduleParams!");
181 return HC_ERR_JSON_FAIL;
182 }
183 if (AddIntToJson(moduleParams, FIELD_SELF_TYPE, userType) != HC_SUCCESS) {
184 LOGE("Failed to add userType to moduleParams!");
185 return HC_ERR_JSON_FAIL;
186 }
187 return HC_SUCCESS;
188 }
189
AddRequestInfoToParams(bool isClient,const BindSession * session,CJson * moduleParams)190 static int32_t AddRequestInfoToParams(bool isClient, const BindSession *session, CJson *moduleParams)
191 {
192 if (AddByteToJson(moduleParams, FIELD_REQUEST_ID,
193 (const uint8_t *)&session->reqId, sizeof(int64_t)) != HC_SUCCESS) {
194 LOGE("Failed to add requestId to moduleParams!");
195 return HC_ERR_JSON_FAIL;
196 }
197 if (AddIntToJson(moduleParams, FIELD_KEY_LENGTH, DEFAULT_RETURN_KEY_LENGTH) != HC_SUCCESS) {
198 LOGE("Failed to add sessionKeyLength to moduleParams!");
199 return HC_ERR_JSON_FAIL;
200 }
201 if (AddBoolToJson(moduleParams, FIELD_IS_CLIENT, isClient) != HC_SUCCESS) {
202 LOGE("Failed to add isClient to moduleParams!");
203 return HC_ERR_JSON_FAIL;
204 }
205 /* Use the GroupManager package name. */
206 if (AddStringToJson(moduleParams, FIELD_PKG_NAME, GROUP_MANAGER_PACKAGE_NAME) != HC_SUCCESS) {
207 LOGE("Failed to add pkgName to moduleParams!");
208 return HC_ERR_JSON_FAIL;
209 }
210 return HC_SUCCESS;
211 }
212
AddPinCodeToParamsIfNeed(BindSession * session,CJson * moduleParams)213 static int32_t AddPinCodeToParamsIfNeed(BindSession *session, CJson *moduleParams)
214 {
215 if (session->opCode == MEMBER_DELETE) {
216 return HC_SUCCESS;
217 }
218 const char *pinCode = GetStringFromJson(session->params, FIELD_PIN_CODE);
219 if (pinCode == NULL) {
220 LOGE("Failed to get pinCode from params!");
221 return HC_ERR_JSON_GET;
222 }
223 if (AddStringToJson(moduleParams, FIELD_PIN_CODE, pinCode) != HC_SUCCESS) {
224 LOGE("Failed to add pinCode to moduleParams!");
225 return HC_ERR_JSON_FAIL;
226 }
227 /* Release the memory in advance to reduce the memory usage. */
228 (void)DeleteItemFromJson(session->params, FIELD_PIN_CODE);
229 return HC_SUCCESS;
230 }
231
AddPeerAuthIdToParams(BindSession * session,CJson * moduleParams)232 static int32_t AddPeerAuthIdToParams(BindSession *session, CJson *moduleParams)
233 {
234 const char *peerAuthId = GetStringFromJson(session->params, FIELD_PEER_AUTH_ID);
235 if (peerAuthId == NULL) {
236 LOGE("Failed to get peerAuthId from params!");
237 return HC_ERR_JSON_GET;
238 }
239 if (AddStringToJson(moduleParams, FIELD_PEER_AUTH_ID, peerAuthId) != HC_SUCCESS) {
240 LOGE("Failed to add peerAuthId to moduleParams!");
241 return HC_ERR_JSON_FAIL;
242 }
243 return HC_SUCCESS;
244 }
245
AddPeerUserTypeToParams(BindSession * session,CJson * moduleParams)246 static int32_t AddPeerUserTypeToParams(BindSession *session, CJson *moduleParams)
247 {
248 int32_t peerUserType = DEVICE_TYPE_ACCESSORY;
249 if (GetIntFromJson(session->params, FIELD_PEER_USER_TYPE, &peerUserType) != HC_SUCCESS) {
250 LOGE("Failed to get peerUserType from params!");
251 return HC_ERR_JSON_GET;
252 }
253 if (AddIntToJson(moduleParams, FIELD_PEER_USER_TYPE, peerUserType) != HC_SUCCESS) {
254 LOGE("Failed to add peerUserType to moduleParams!");
255 return HC_ERR_JSON_FAIL;
256 }
257 return HC_SUCCESS;
258 }
259
AddPeerAuthIdAndUserTypeToParamsIfNeed(BindSession * session,CJson * moduleParams)260 static int32_t AddPeerAuthIdAndUserTypeToParamsIfNeed(BindSession *session, CJson *moduleParams)
261 {
262 if (session->opCode != MEMBER_DELETE) {
263 return HC_SUCCESS;
264 }
265 int32_t result = AddPeerAuthIdToParams(session, moduleParams);
266 if (result != HC_SUCCESS) {
267 return result;
268 }
269 return AddPeerUserTypeToParams(session, moduleParams);
270 }
271
TryAddPeerUserTypeToParams(const CJson * jsonParams,BindSession * session)272 static int32_t TryAddPeerUserTypeToParams(const CJson *jsonParams, BindSession *session)
273 {
274 int32_t peerUserType = DEVICE_TYPE_ACCESSORY;
275 if (GetIntFromJson(jsonParams, FIELD_PEER_USER_TYPE, &peerUserType) == HC_SUCCESS) {
276 if (AddIntToJson(session->params, FIELD_PEER_USER_TYPE, peerUserType) != HC_SUCCESS) {
277 LOGE("Failed to add peerUserType to params!");
278 return HC_ERR_JSON_FAIL;
279 }
280 }
281 return HC_SUCCESS;
282 }
283
AddGroupInfoToParams(const TrustedGroupEntry * entry,CJson * params)284 static int32_t AddGroupInfoToParams(const TrustedGroupEntry *entry, CJson *params)
285 {
286 if (AddStringToJson(params, FIELD_GROUP_ID, StringGet(&entry->id)) != HC_SUCCESS) {
287 LOGE("Failed to add groupId to json!");
288 return HC_ERR_JSON_FAIL;
289 }
290 if (AddIntToJson(params, FIELD_GROUP_TYPE, entry->type) != HC_SUCCESS) {
291 LOGE("Failed to add groupType to json!");
292 return HC_ERR_JSON_FAIL;
293 }
294 if (AddStringToJson(params, FIELD_GROUP_NAME, StringGet(&entry->name)) != HC_SUCCESS) {
295 LOGE("Failed to add groupName to json!");
296 return HC_ERR_JSON_FAIL;
297 }
298 return HC_SUCCESS;
299 }
300
AddDevInfoToParams(const TrustedDeviceEntry * devAuthParams,CJson * params)301 static int32_t AddDevInfoToParams(const TrustedDeviceEntry *devAuthParams, CJson *params)
302 {
303 if (AddStringToJson(params, FIELD_AUTH_ID, StringGet(&devAuthParams->authId)) != HC_SUCCESS) {
304 LOGE("Failed to add authId to params!");
305 return HC_ERR_JSON_FAIL;
306 }
307 if (AddStringToJson(params, FIELD_CONN_DEVICE_ID, StringGet(&devAuthParams->udid)) != HC_SUCCESS) {
308 LOGE("Failed to add udid to params!");
309 return HC_ERR_JSON_FAIL;
310 }
311 if (AddIntToJson(params, FIELD_USER_TYPE, devAuthParams->devType) != HC_SUCCESS) {
312 LOGE("Failed to add userType to params!");
313 return HC_ERR_JSON_FAIL;
314 }
315 return HC_SUCCESS;
316 }
317
AddGroupInfoByDatabase(int32_t osAccountId,const char * groupId,CJson * params)318 static int32_t AddGroupInfoByDatabase(int32_t osAccountId, const char *groupId, CJson *params)
319 {
320 TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
321 if (entry == NULL) {
322 LOGE("Failed to get groupEntry from db!");
323 return HC_ERR_DB;
324 }
325 if (AddGroupInfoToParams(entry, params) != HC_SUCCESS) {
326 DestroyGroupEntry(entry);
327 return HC_ERR_JSON_FAIL;
328 }
329 DestroyGroupEntry(entry);
330 return HC_SUCCESS;
331 }
332
AddDevInfoByDatabase(int32_t osAccountId,const char * groupId,CJson * params)333 static int32_t AddDevInfoByDatabase(int32_t osAccountId, const char *groupId, CJson *params)
334 {
335 char udid[INPUT_UDID_LEN] = { 0 };
336 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
337 if (res != HC_SUCCESS) {
338 LOGE("Failed to get local udid! res: %d", res);
339 return HC_ERR_DB;
340 }
341 TrustedDeviceEntry *devAuthParams = CreateDeviceEntry();
342 if (devAuthParams == NULL) {
343 LOGE("Failed to allocate devEntry memory!");
344 return HC_ERR_ALLOC_MEMORY;
345 }
346 if (GetTrustedDevInfoById(osAccountId, udid, true, groupId, devAuthParams) != HC_SUCCESS) {
347 LOGE("Failed to obtain the device information from the database!");
348 DestroyDeviceEntry(devAuthParams);
349 return HC_ERR_DB;
350 }
351 if (AddDevInfoToParams(devAuthParams, params) != HC_SUCCESS) {
352 DestroyDeviceEntry(devAuthParams);
353 return HC_ERR_JSON_FAIL;
354 }
355 DestroyDeviceEntry(devAuthParams);
356 return HC_SUCCESS;
357 }
358
AddGroupId(const char * groupId,CJson * params)359 static int32_t AddGroupId(const char *groupId, CJson *params)
360 {
361 if (AddStringToJson(params, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
362 LOGE("Failed to add groupId to params!");
363 return HC_ERR_JSON_FAIL;
364 }
365 return HC_SUCCESS;
366 }
367
AddGroupName(const CJson * jsonParams,CJson * params)368 static int32_t AddGroupName(const CJson *jsonParams, CJson *params)
369 {
370 const char *groupName = GetStringFromJson(jsonParams, FIELD_GROUP_NAME);
371 if (groupName == NULL) {
372 LOGE("Failed to get groupName from jsonParams!");
373 return HC_ERR_JSON_GET;
374 }
375 if (AddStringToJson(params, FIELD_GROUP_NAME, groupName) != HC_SUCCESS) {
376 LOGE("Failed to add groupName to params!");
377 return HC_ERR_JSON_FAIL;
378 }
379 return HC_SUCCESS;
380 }
381
AddGroupOwnerIfExist(const CJson * jsonParams,CJson * params)382 static int32_t AddGroupOwnerIfExist(const CJson *jsonParams, CJson *params)
383 {
384 const char *groupOwner = GetStringFromJson(jsonParams, FIELD_GROUP_OWNER);
385 if (groupOwner != NULL) {
386 if (AddStringToJson(params, FIELD_GROUP_OWNER, groupOwner) != HC_SUCCESS) {
387 LOGE("Failed to add groupOwner to params!");
388 return HC_ERR_JSON_FAIL;
389 }
390 }
391 return HC_SUCCESS;
392 }
393
AddGroupTypeIfValid(const CJson * jsonParams,CJson * params)394 static int32_t AddGroupTypeIfValid(const CJson *jsonParams, CJson *params)
395 {
396 int32_t groupType = PEER_TO_PEER_GROUP;
397 if (GetIntFromJson(jsonParams, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
398 LOGE("Failed to get groupType from in!");
399 return HC_ERR_JSON_GET;
400 }
401 if (groupType != PEER_TO_PEER_GROUP) {
402 LOGE("The input groupType is invalid!");
403 return HC_ERR_INVALID_PARAMS;
404 }
405 if (AddIntToJson(params, FIELD_GROUP_TYPE, groupType) != HC_SUCCESS) {
406 LOGE("Failed to add groupType to params!");
407 return HC_ERR_JSON_FAIL;
408 }
409 return HC_SUCCESS;
410 }
411
AddGroupVisibilityIfValidOrDefault(const CJson * jsonParams,CJson * params)412 static int32_t AddGroupVisibilityIfValidOrDefault(const CJson *jsonParams, CJson *params)
413 {
414 int32_t groupVisibility = GROUP_VISIBILITY_PUBLIC;
415 (void)GetIntFromJson(jsonParams, FIELD_GROUP_VISIBILITY, &groupVisibility);
416 if (!IsGroupVisibilityValid(groupVisibility)) {
417 LOGE("The input groupVisibility is invalid!");
418 return HC_ERR_INVALID_PARAMS;
419 }
420 if (AddIntToJson(params, FIELD_GROUP_VISIBILITY, groupVisibility) != HC_SUCCESS) {
421 LOGE("Failed to add groupVisibility to params!");
422 return HC_ERR_JSON_FAIL;
423 }
424 return HC_SUCCESS;
425 }
426
AddExpireTimeIfValidOrDefault(const CJson * jsonParams,CJson * params)427 static int32_t AddExpireTimeIfValidOrDefault(const CJson *jsonParams, CJson *params)
428 {
429 int32_t expireTime = DEFAULT_EXPIRE_TIME;
430 (void)GetIntFromJson(jsonParams, FIELD_EXPIRE_TIME, &expireTime);
431 if (!IsExpireTimeValid(expireTime)) {
432 LOGE("The input expireTime is invalid!");
433 return HC_ERR_INVALID_PARAMS;
434 }
435 if (AddIntToJson(params, FIELD_EXPIRE_TIME, expireTime) != HC_SUCCESS) {
436 LOGE("Failed to add expireTime to params!");
437 return HC_ERR_JSON_FAIL;
438 }
439 return HC_SUCCESS;
440 }
441
CheckAuthIdAndUserTypeValid(int32_t osAccountId,int userType,const char * groupId,const char * authId)442 static int32_t CheckAuthIdAndUserTypeValid(int32_t osAccountId, int userType, const char *groupId, const char *authId)
443 {
444 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
445 return HC_SUCCESS;
446 }
447 char udid[INPUT_UDID_LEN] = { 0 };
448 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
449 if (res != HC_SUCCESS) {
450 LOGE("Failed to get local udid! res: %d", res);
451 return HC_ERR_DB;
452 }
453 TrustedDeviceEntry *deviceInfo = CreateDeviceEntry();
454 if (deviceInfo == NULL) {
455 LOGE("Failed to allocate deviceInfo memory!");
456 return HC_ERR_ALLOC_MEMORY;
457 }
458 int32_t result = GetTrustedDevInfoById(osAccountId, udid, true, groupId, deviceInfo);
459 if (result != HC_SUCCESS) {
460 LOGE("Failed to obtain the local device information from the database!");
461 DestroyDeviceEntry(deviceInfo);
462 return result;
463 }
464 const char *oriAuthId = StringGet(&deviceInfo->authId);
465 if ((deviceInfo->devType != userType) || ((oriAuthId != NULL) && (strcmp(oriAuthId, authId) != 0))) {
466 LOGE("Once a group is created, the service cannot change the local authId and userType used in the group!");
467 DestroyDeviceEntry(deviceInfo);
468 return HC_ERR_INVALID_PARAMS;
469 }
470 DestroyDeviceEntry(deviceInfo);
471 return HC_SUCCESS;
472 }
473
AddAuthIdAndUserTypeIfValidOrDefault(int32_t osAccountId,const char * groupId,const CJson * jsonParams,CJson * params)474 static int32_t AddAuthIdAndUserTypeIfValidOrDefault(int32_t osAccountId, const char *groupId, const CJson *jsonParams,
475 CJson *params)
476 {
477 int32_t userType = DEVICE_TYPE_ACCESSORY;
478 (void)GetIntFromJson(jsonParams, FIELD_USER_TYPE, &userType);
479 if (!IsUserTypeValid(userType)) {
480 LOGE("The input userType is invalid!");
481 return HC_ERR_INVALID_PARAMS;
482 }
483 const char *authId = GetStringFromJson(jsonParams, FIELD_DEVICE_ID);
484 char udid[INPUT_UDID_LEN] = { 0 };
485 if (authId == NULL) {
486 LOGD("No authId is found. The default value is udid!");
487 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
488 if (res != HC_SUCCESS) {
489 LOGE("Failed to get local udid! res: %d", res);
490 return HC_ERR_DB;
491 }
492 authId = udid;
493 }
494 int32_t result = CheckAuthIdAndUserTypeValid(osAccountId, userType, groupId, authId);
495 if (result != HC_SUCCESS) {
496 return result;
497 }
498 if (AddIntToJson(params, FIELD_USER_TYPE, userType) != HC_SUCCESS) {
499 LOGE("Failed to add userType to params!");
500 return HC_ERR_JSON_FAIL;
501 }
502 if (AddStringToJson(params, FIELD_AUTH_ID, authId) != HC_SUCCESS) {
503 LOGE("Failed to add authId to params!");
504 return HC_ERR_JSON_FAIL;
505 }
506 return HC_SUCCESS;
507 }
508
AddUdid(CJson * params)509 static int32_t AddUdid(CJson *params)
510 {
511 char udid[INPUT_UDID_LEN] = { 0 };
512 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
513 if (res != HC_SUCCESS) {
514 LOGE("Failed to get local udid! res: %d", res);
515 return HC_ERR_DB;
516 }
517 if (AddStringToJson(params, FIELD_CONN_DEVICE_ID, udid) != HC_SUCCESS) {
518 LOGE("Failed to add udid to params!");
519 return HC_ERR_JSON_FAIL;
520 }
521 return HC_SUCCESS;
522 }
523
AddUserTypeIfValidOrDefault(const CJson * jsonParams,CJson * params)524 static int32_t AddUserTypeIfValidOrDefault(const CJson *jsonParams, CJson *params)
525 {
526 int32_t userType = DEVICE_TYPE_ACCESSORY;
527 (void)GetIntFromJson(jsonParams, FIELD_USER_TYPE, &userType);
528 if (!IsUserTypeValid(userType)) {
529 LOGE("The input userType is invalid!");
530 return HC_ERR_INVALID_PARAMS;
531 }
532 if (AddIntToJson(params, FIELD_USER_TYPE, userType) != HC_SUCCESS) {
533 LOGE("Failed to add userType to params!");
534 return HC_ERR_JSON_FAIL;
535 }
536 return HC_SUCCESS;
537 }
538
AddGroupInfoToSessionParams(const char * groupId,const CJson * jsonParams,CJson * params)539 static int32_t AddGroupInfoToSessionParams(const char *groupId, const CJson *jsonParams, CJson *params)
540 {
541 int32_t result;
542 if (((result = AddGroupId(groupId, params)) != HC_SUCCESS) ||
543 ((result = AddGroupName(jsonParams, params)) != HC_SUCCESS) ||
544 ((result = AddGroupOwnerIfExist(jsonParams, params)) != HC_SUCCESS) ||
545 ((result = AddGroupTypeIfValid(jsonParams, params)) != HC_SUCCESS) ||
546 ((result = AddGroupVisibilityIfValidOrDefault(jsonParams, params)) != HC_SUCCESS) ||
547 ((result = AddExpireTimeIfValidOrDefault(jsonParams, params)) != HC_SUCCESS)) {
548 return result;
549 }
550 return HC_SUCCESS;
551 }
552
AddDevInfoToSessionParams(int32_t osAccountId,const char * groupId,const CJson * jsonParams,CJson * params)553 static int32_t AddDevInfoToSessionParams(int32_t osAccountId, const char *groupId, const CJson *jsonParams,
554 CJson *params)
555 {
556 int32_t result;
557 if (((result = AddAuthIdAndUserTypeIfValidOrDefault(osAccountId, groupId, jsonParams, params)) != HC_SUCCESS) ||
558 ((result = AddUdid(params)) != HC_SUCCESS) ||
559 ((result = AddUserTypeIfValidOrDefault(jsonParams, params)) != HC_SUCCESS)) {
560 return result;
561 }
562 return HC_SUCCESS;
563 }
564
GenerateParamsByInput(int32_t osAccountId,const char * groupId,const CJson * jsonParams,CJson * params)565 static int32_t GenerateParamsByInput(int32_t osAccountId, const char *groupId, const CJson *jsonParams, CJson *params)
566 {
567 int32_t result = AddGroupInfoToSessionParams(groupId, jsonParams, params);
568 if (result != HC_SUCCESS) {
569 return result;
570 }
571 return AddDevInfoToSessionParams(osAccountId, groupId, jsonParams, params);
572 }
573
GenerateParamsByDatabase(int32_t osAccountId,const char * groupId,CJson * params)574 static int32_t GenerateParamsByDatabase(int32_t osAccountId, const char *groupId, CJson *params)
575 {
576 int32_t result = AddGroupInfoByDatabase(osAccountId, groupId, params);
577 if (result != HC_SUCCESS) {
578 return result;
579 }
580 return AddDevInfoByDatabase(osAccountId, groupId, params);
581 }
582
AddIsForceDeleteIfNeed(int isClient,const CJson * jsonParams,BindSession * session)583 static int32_t AddIsForceDeleteIfNeed(int isClient, const CJson *jsonParams, BindSession *session)
584 {
585 if ((isClient == CLIENT) && (session->opCode == MEMBER_DELETE)) {
586 bool isForceDelete = false;
587 (void)GetBoolFromJson(jsonParams, FIELD_IS_FORCE_DELETE, &isForceDelete);
588 if (AddBoolToJson(session->params, FIELD_IS_FORCE_DELETE, isForceDelete) != HC_SUCCESS) {
589 LOGE("Failed to add isForceDelete to params!");
590 return HC_ERR_JSON_FAIL;
591 }
592 }
593 return HC_SUCCESS;
594 }
595
AddChannelIdIfNeed(int isClient,const CJson * jsonParams,BindSession * session)596 static int32_t AddChannelIdIfNeed(int isClient, const CJson *jsonParams, BindSession *session)
597 {
598 if ((isClient == SERVER) && (session->channelType == SOFT_BUS)) {
599 int64_t channelId = DEFAULT_CHANNEL_ID;
600 if (GetByteFromJson(jsonParams, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) {
601 LOGE("Failed to get channelId from jsonParams!");
602 return HC_ERR_JSON_GET;
603 }
604 session->channelId = channelId;
605 }
606 return HC_SUCCESS;
607 }
608
AddPinCodeIfNeed(const CJson * jsonParams,BindSession * session)609 static int32_t AddPinCodeIfNeed(const CJson *jsonParams, BindSession *session)
610 {
611 if (session->opCode != MEMBER_DELETE) {
612 const char *pinCode = GetStringFromJson(jsonParams, FIELD_PIN_CODE);
613 if (pinCode == NULL) {
614 LOGE("Failed to get pinCode from jsonParams!");
615 return HC_ERR_JSON_GET;
616 }
617 if (AddStringToJson(session->params, FIELD_PIN_CODE, pinCode) != HC_SUCCESS) {
618 LOGE("Failed to add pinCode to params!");
619 return HC_ERR_JSON_FAIL;
620 }
621 }
622 return HC_SUCCESS;
623 }
624
AddPeerAuthIdAndUdidIfExist(const CJson * jsonParams,BindSession * session)625 static int32_t AddPeerAuthIdAndUdidIfExist(const CJson *jsonParams, BindSession *session)
626 {
627 const char *peerAuthId = GetStringFromJson(jsonParams, FIELD_PEER_DEVICE_ID);
628 const char *peerUdid = GetStringFromJson(jsonParams, FIELD_PEER_UDID);
629 if (peerAuthId != NULL) {
630 if (AddStringToJson(session->params, FIELD_PEER_AUTH_ID, peerAuthId) != HC_SUCCESS) {
631 LOGE("Failed to add peerAuthId to params!");
632 return HC_ERR_JSON_FAIL;
633 }
634 }
635 if (peerUdid != NULL) {
636 if (AddStringToJson(session->params, FIELD_PEER_UDID, peerUdid) != HC_SUCCESS) {
637 LOGE("Failed to add peerUdid to params!");
638 return HC_ERR_JSON_FAIL;
639 }
640 }
641 return HC_SUCCESS;
642 }
643
AddPeerAuthIdIfDelete(bool isClient,const CJson * jsonParams,BindSession * session)644 static int32_t AddPeerAuthIdIfDelete(bool isClient, const CJson *jsonParams, BindSession *session)
645 {
646 const char *peerAuthId = NULL;
647 if (isClient) {
648 peerAuthId = GetStringFromJson(jsonParams, FIELD_DELETE_ID);
649 } else {
650 peerAuthId = GetStringFromJson(jsonParams, FIELD_PEER_DEVICE_ID);
651 }
652 if (peerAuthId == NULL) {
653 LOGE("Failed to get peerAuthId from jsonParams!");
654 return HC_ERR_JSON_GET;
655 }
656 if (AddStringToJson(session->params, FIELD_PEER_AUTH_ID, peerAuthId) != HC_SUCCESS) {
657 LOGE("Failed to add peerAuthId to params!");
658 return HC_ERR_JSON_FAIL;
659 }
660 return HC_SUCCESS;
661 }
662
AddPeerUserTypeIfDelete(BindSession * session)663 static int32_t AddPeerUserTypeIfDelete(BindSession *session)
664 {
665 const char *peerAuthId = GetStringFromJson(session->params, FIELD_PEER_AUTH_ID);
666 if (peerAuthId == NULL) {
667 LOGE("Failed to get peerAuthId from params!");
668 return HC_ERR_JSON_GET;
669 }
670 const char *groupId = GetStringFromJson(session->params, FIELD_GROUP_ID);
671 if (groupId == NULL) {
672 LOGE("Failed to get groupId from params!");
673 return HC_ERR_JSON_GET;
674 }
675 TrustedDeviceEntry *devAuthParams = CreateDeviceEntry();
676 if (devAuthParams == NULL) {
677 LOGE("Failed to allocate devEntry memory!");
678 return HC_ERR_ALLOC_MEMORY;
679 }
680 if (GetTrustedDevInfoById(session->osAccountId, peerAuthId, false, groupId, devAuthParams) != HC_SUCCESS) {
681 LOGE("Failed to obtain the device information from the database!");
682 DestroyDeviceEntry(devAuthParams);
683 return HC_ERR_DB;
684 }
685 if (AddIntToJson(session->params, FIELD_PEER_USER_TYPE, devAuthParams->devType) != HC_SUCCESS) {
686 DestroyDeviceEntry(devAuthParams);
687 return HC_ERR_JSON_FAIL;
688 }
689 DestroyDeviceEntry(devAuthParams);
690 return HC_SUCCESS;
691 }
692
AddPeerDevInfoIfNeed(bool isClient,const CJson * jsonParams,BindSession * session)693 static int32_t AddPeerDevInfoIfNeed(bool isClient, const CJson *jsonParams, BindSession *session)
694 {
695 if (session->opCode == MEMBER_DELETE) {
696 int32_t result = AddPeerAuthIdIfDelete(isClient, jsonParams, session);
697 if (result != HC_SUCCESS) {
698 return result;
699 }
700 return AddPeerUserTypeIfDelete(session);
701 }
702 return AddPeerAuthIdAndUdidIfExist(jsonParams, session);
703 }
704
AddGroupAndDevInfo(int32_t osAccountId,int isClient,const CJson * jsonParams,BindSession * session)705 static int32_t AddGroupAndDevInfo(int32_t osAccountId, int isClient, const CJson *jsonParams, BindSession *session)
706 {
707 const char *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID);
708 if (groupId == NULL) {
709 LOGE("Failed to get groupId from jsonParams!");
710 return HC_ERR_JSON_GET;
711 }
712 if (NeedCreateGroup(isClient, session->opCode)) {
713 return GenerateParamsByInput(osAccountId, groupId, jsonParams, session->params);
714 } else {
715 return GenerateParamsByDatabase(session->osAccountId, groupId, session->params);
716 }
717 }
718
InteractWithPeer(const BindSession * session,CJson * sendData)719 static int32_t InteractWithPeer(const BindSession *session, CJson *sendData)
720 {
721 int32_t result = AddInfoToSendData(false, session, sendData);
722 if (result != HC_SUCCESS) {
723 LOGE("Failed to generate sendData!");
724 return result;
725 }
726 return SendBindSessionData(session, sendData);
727 }
728
InformSelfBindSuccess(const char * peerAuthId,const char * groupId,const BindSession * session,CJson * out)729 static int32_t InformSelfBindSuccess(const char *peerAuthId, const char *groupId, const BindSession *session,
730 CJson *out)
731 {
732 uint8_t sessionKey[DEFAULT_RETURN_KEY_LENGTH] = { 0 };
733 if (GetByteFromJson(out, FIELD_SESSION_KEY, sessionKey, DEFAULT_RETURN_KEY_LENGTH) == HC_SUCCESS) {
734 ProcessSessionKeyCallback(session->reqId, sessionKey, DEFAULT_RETURN_KEY_LENGTH, session->base.callback);
735 (void)memset_s(sessionKey, DEFAULT_RETURN_KEY_LENGTH, 0, DEFAULT_RETURN_KEY_LENGTH);
736 ClearSensitiveStringInJson(out, FIELD_SESSION_KEY);
737 }
738
739 char *jsonDataStr = NULL;
740 int32_t result = GenerateBindSuccessData(peerAuthId, groupId, &jsonDataStr);
741 if (result != HC_SUCCESS) {
742 LOGE("Failed to generate the data to be sent to the service!");
743 return result;
744 }
745 ProcessFinishCallback(session->reqId, session->opCode, jsonDataStr, session->base.callback);
746 FreeJsonString(jsonDataStr);
747 return HC_SUCCESS;
748 }
749
InformSelfUnbindSuccess(const char * peerAuthId,const char * groupId,const BindSession * session)750 static int32_t InformSelfUnbindSuccess(const char *peerAuthId, const char *groupId, const BindSession *session)
751 {
752 char *jsonDataStr = NULL;
753 int32_t result = GenerateUnbindSuccessData(peerAuthId, groupId, &jsonDataStr);
754 if (result != HC_SUCCESS) {
755 LOGE("Failed to generate the data to be sent to the service!");
756 return result;
757 }
758 ProcessFinishCallback(session->reqId, session->opCode, jsonDataStr, session->base.callback);
759 FreeJsonString(jsonDataStr);
760 return HC_SUCCESS;
761 }
762
SetGroupId(const CJson * params,TrustedGroupEntry * groupParams)763 static int32_t SetGroupId(const CJson *params, TrustedGroupEntry *groupParams)
764 {
765 const char *groupId = GetStringFromJson(params, FIELD_GROUP_ID);
766 if (groupId == NULL) {
767 LOGE("Failed to get groupId from params!");
768 return HC_ERR_JSON_GET;
769 }
770 if (!StringSetPointer(&groupParams->id, groupId)) {
771 LOGE("Failed to copy groupId!");
772 return HC_ERR_MEMORY_COPY;
773 }
774 return HC_SUCCESS;
775 }
776
SetGroupName(const CJson * params,TrustedGroupEntry * groupParams)777 static int32_t SetGroupName(const CJson *params, TrustedGroupEntry *groupParams)
778 {
779 const char *groupName = GetStringFromJson(params, FIELD_GROUP_NAME);
780 if (groupName == NULL) {
781 LOGE("Failed to get groupName from params!");
782 return HC_ERR_JSON_GET;
783 }
784 if (!StringSetPointer(&groupParams->name, groupName)) {
785 LOGE("Failed to copy groupName!");
786 return HC_ERR_MEMORY_COPY;
787 }
788 return HC_SUCCESS;
789 }
790
SetGroupOwner(const char * ownerAppId,TrustedGroupEntry * groupParams)791 static int32_t SetGroupOwner(const char *ownerAppId, TrustedGroupEntry *groupParams)
792 {
793 HcString ownerName = CreateString();
794 if (!StringSetPointer(&ownerName, ownerAppId)) {
795 LOGE("Failed to copy groupOwner!");
796 return HC_ERR_MEMORY_COPY;
797 }
798 if (groupParams->managers.pushBackT(&groupParams->managers, ownerName) == NULL) {
799 LOGE("Failed to push owner to vec!");
800 return HC_ERR_MEMORY_COPY;
801 }
802 return HC_SUCCESS;
803 }
804
SetGroupType(const CJson * params,TrustedGroupEntry * groupParams)805 static int32_t SetGroupType(const CJson *params, TrustedGroupEntry *groupParams)
806 {
807 /* Currently, only peer to peer group is supported. */
808 (void)params;
809 groupParams->type = PEER_TO_PEER_GROUP;
810 return HC_SUCCESS;
811 }
812
SetGroupVisibility(const CJson * params,TrustedGroupEntry * groupParams)813 static int32_t SetGroupVisibility(const CJson *params, TrustedGroupEntry *groupParams)
814 {
815 int32_t groupVisibility = GROUP_VISIBILITY_PUBLIC;
816 (void)GetIntFromJson(params, FIELD_GROUP_VISIBILITY, &groupVisibility);
817 groupParams->visibility = groupVisibility;
818 return HC_SUCCESS;
819 }
820
SetGroupExpireTime(const CJson * params,TrustedGroupEntry * groupParams)821 static int32_t SetGroupExpireTime(const CJson *params, TrustedGroupEntry *groupParams)
822 {
823 int32_t expireTime = DEFAULT_EXPIRE_TIME;
824 (void)GetIntFromJson(params, FIELD_EXPIRE_TIME, &expireTime);
825 groupParams->expireTime = expireTime;
826 return HC_SUCCESS;
827 }
828
ForceDeletePeerKey(CJson * params)829 static int32_t ForceDeletePeerKey(CJson *params)
830 {
831 /* Use the DeviceGroupManager package name. */
832 const char *appId = GROUP_MANAGER_PACKAGE_NAME;
833 const char *peerAuthId = GetStringFromJson(params, FIELD_PEER_AUTH_ID);
834 if (peerAuthId == NULL) {
835 LOGE("Failed to get peerAuthId from params!");
836 return HC_ERR_JSON_GET;
837 }
838 const char *groupId = GetStringFromJson(params, FIELD_GROUP_ID);
839 if (groupId == NULL) {
840 LOGE("Failed to get groupId from params!");
841 return HC_ERR_JSON_GET;
842 }
843 int32_t peerUserType = DEVICE_TYPE_ACCESSORY;
844 (void)GetIntFromJson(params, FIELD_PEER_USER_TYPE, &peerUserType);
845 Uint8Buff peerAuthIdBuff = {
846 .val = (uint8_t *)peerAuthId,
847 .length = HcStrlen(peerAuthId)
848 };
849 return DeletePeerAuthInfo(appId, groupId, &peerAuthIdBuff, peerUserType, DAS_MODULE);
850 }
851
GenerateGroupParams(const BindSession * session,TrustedGroupEntry * groupParams)852 static int32_t GenerateGroupParams(const BindSession *session, TrustedGroupEntry *groupParams)
853 {
854 int32_t result;
855 if (((result = SetGroupId(session->params, groupParams)) != HC_SUCCESS) ||
856 ((result = SetGroupName(session->params, groupParams)) != HC_SUCCESS) ||
857 ((result = SetGroupOwner(session->base.appId, groupParams)) != HC_SUCCESS) ||
858 ((result = SetGroupType(session->params, groupParams)) != HC_SUCCESS) ||
859 ((result = SetGroupVisibility(session->params, groupParams)) != HC_SUCCESS) ||
860 ((result = SetGroupExpireTime(session->params, groupParams)) != HC_SUCCESS)) {
861 return result;
862 }
863 return HC_SUCCESS;
864 }
865
AddGroupToDatabase(const BindSession * session)866 static int32_t AddGroupToDatabase(const BindSession *session)
867 {
868 TrustedGroupEntry *groupParams = CreateGroupEntry();
869 if (groupParams == NULL) {
870 LOGE("Failed to allocate groupParams memory!");
871 return HC_ERR_ALLOC_MEMORY;
872 }
873 int32_t result = GenerateGroupParams(session, groupParams);
874 if (result != HC_SUCCESS) {
875 LOGE("Failed to generate groupParams!");
876 DestroyGroupEntry(groupParams);
877 return HC_ERR_DB;
878 }
879 result = AddGroup(session->osAccountId, groupParams);
880 DestroyGroupEntry(groupParams);
881 if (result != HC_SUCCESS) {
882 LOGE("Failed to add the group to the database!");
883 return HC_ERR_DB;
884 }
885 return HC_SUCCESS;
886 }
887
GenerateDevAuthParams(const char * authId,const char * udid,const char * groupId,int userType,TrustedDeviceEntry * devAuthParams)888 static int32_t GenerateDevAuthParams(const char *authId, const char *udid, const char *groupId,
889 int userType, TrustedDeviceEntry *devAuthParams)
890 {
891 devAuthParams->devType = userType;
892 devAuthParams->source = SELF_CREATED;
893 StringSetPointer(&(devAuthParams->authId), authId);
894 StringSetPointer(&(devAuthParams->udid), udid);
895 StringSetPointer(&(devAuthParams->groupId), groupId);
896 StringSetPointer(&(devAuthParams->serviceType), groupId);
897 return HC_SUCCESS;
898 }
899
AddTrustDevToDatabase(int32_t osAccountId,const char * authId,const char * udid,const char * groupId,int userType)900 static int32_t AddTrustDevToDatabase(int32_t osAccountId, const char *authId, const char *udid, const char *groupId,
901 int userType)
902 {
903 TrustedDeviceEntry *devAuthParams = CreateDeviceEntry();
904 if (devAuthParams == NULL) {
905 LOGE("Failed to allocate devAuthParams memory!");
906 return HC_ERR_ALLOC_MEMORY;
907 }
908 int32_t result = GenerateDevAuthParams(authId, udid, groupId, userType, devAuthParams);
909 if (result != HC_SUCCESS) {
910 LOGE("Failed to generate devAuthParams!");
911 DestroyDeviceEntry(devAuthParams);
912 return result;
913 }
914 result = AddTrustedDevice(osAccountId, devAuthParams);
915 DestroyDeviceEntry(devAuthParams);
916 if (result != HC_SUCCESS) {
917 LOGE("Failed to add the trusted devices to the database!");
918 return HC_ERR_DB;
919 }
920 return HC_SUCCESS;
921 }
922
AddGroupAndLocalDevIfNotExist(const char * groupId,const BindSession * session)923 static int32_t AddGroupAndLocalDevIfNotExist(const char *groupId, const BindSession *session)
924 {
925 if (IsGroupExistByGroupId(session->osAccountId, groupId)) {
926 return HC_SUCCESS;
927 }
928 int32_t result = HC_SUCCESS;
929 char udid[INPUT_UDID_LEN] = { 0 };
930 result = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
931 if (result != HC_SUCCESS) {
932 LOGE("Failed to get local udid! res: %d", result);
933 return HC_ERR_DB;
934 }
935 result = AddGroupToDatabase(session);
936 if (result != HC_SUCCESS) {
937 return result;
938 }
939 const char *authId = GetStringFromJson(session->params, FIELD_AUTH_ID);
940 if (authId == NULL) {
941 LOGI("No authId is found. The default value is udid!");
942 authId = udid;
943 }
944 int32_t userType = DEVICE_TYPE_ACCESSORY;
945 (void)GetIntFromJson(session->params, FIELD_USER_TYPE, &userType);
946 return AddTrustDevToDatabase(session->osAccountId, authId, udid, groupId, userType);
947 }
948
AddPeerDevToGroup(const char * peerAuthId,const char * peerUdid,const char * groupId,const BindSession * session)949 static int32_t AddPeerDevToGroup(const char *peerAuthId, const char *peerUdid,
950 const char *groupId, const BindSession *session)
951 {
952 if (IsTrustedDeviceInGroup(session->osAccountId, groupId, peerUdid, true)) {
953 LOGI("The peer device already exists in the group! RequestId: %" PRId64, session->reqId);
954 QueryDeviceParams params = InitQueryDeviceParams();
955 params.groupId = groupId;
956 params.udid = peerUdid;
957 if (DelTrustedDevice(session->osAccountId, ¶ms) != HC_SUCCESS) {
958 LOGE("Failed to delete the original data! RequestId: %" PRId64, session->reqId);
959 return HC_ERR_DB;
960 }
961 LOGI("Delete the original data successfully! RequestId: %" PRId64, session->reqId);
962 }
963 int32_t peerUserType = DEVICE_TYPE_ACCESSORY;
964 (void)GetIntFromJson(session->params, FIELD_PEER_USER_TYPE, &peerUserType);
965 int32_t result = AddTrustDevToDatabase(session->osAccountId, peerAuthId, peerUdid, groupId, peerUserType);
966 if (result != HC_SUCCESS) {
967 LOGE("Failed to update the peer trusted device information! RequestId: %" PRId64, session->reqId);
968 return result;
969 }
970 LOGI("The peer trusted device is added to the database successfully! RequestId: %" PRId64, session->reqId);
971 return HC_SUCCESS;
972 }
973
AddGroupAndDev(const char * peerAuthId,const char * peerUdid,const char * groupId,const BindSession * session)974 static int32_t AddGroupAndDev(const char *peerAuthId, const char *peerUdid,
975 const char *groupId, const BindSession *session)
976 {
977 int32_t result = AddGroupAndLocalDevIfNotExist(groupId, session);
978 if (result != HC_SUCCESS) {
979 return result;
980 }
981 result = AddPeerDevToGroup(peerAuthId, peerUdid, groupId, session);
982 if (result != HC_SUCCESS) {
983 return result;
984 }
985 return SaveOsAccountDb(session->osAccountId);
986 }
987
HandleBindSuccess(const char * peerAuthId,const char * peerUdid,const char * groupId,const BindSession * session,CJson * out)988 static int32_t HandleBindSuccess(const char *peerAuthId, const char *peerUdid,
989 const char *groupId, const BindSession *session, CJson *out)
990 {
991 int32_t result = AddGroupAndDev(peerAuthId, peerUdid, groupId, session);
992 if (result != HC_SUCCESS) {
993 return result;
994 }
995 return InformSelfBindSuccess(peerAuthId, groupId, session, out);
996 }
997
HandleUnbindSuccess(const char * peerAuthId,const char * groupId,const BindSession * session)998 static int32_t HandleUnbindSuccess(const char *peerAuthId, const char *groupId, const BindSession *session)
999 {
1000 if (IsGroupExistByGroupId(session->osAccountId, groupId)) {
1001 QueryDeviceParams params = InitQueryDeviceParams();
1002 params.groupId = groupId;
1003 params.authId = peerAuthId;
1004 if (DelTrustedDevice(session->osAccountId, ¶ms) != HC_SUCCESS ||
1005 SaveOsAccountDb(session->osAccountId) != HC_SUCCESS) {
1006 LOGE("Failed to unbind device from database!");
1007 return HC_ERR_DB;
1008 }
1009 LOGI("The device is successfully unbound from the database!");
1010 }
1011 return InformSelfUnbindSuccess(peerAuthId, groupId, session);
1012 }
1013
OnBindOrUnbindFinish(const BindSession * session,const CJson * jsonParams,CJson * out)1014 static int32_t OnBindOrUnbindFinish(const BindSession *session, const CJson *jsonParams, CJson *out)
1015 {
1016 const char *peerAuthId = GetStringFromJson(jsonParams, FIELD_PEER_DEVICE_ID);
1017 if (peerAuthId == NULL) {
1018 peerAuthId = GetStringFromJson(session->params, FIELD_PEER_AUTH_ID);
1019 }
1020 if (peerAuthId == NULL) {
1021 LOGE("Failed to get peerAuthId from jsonParams and params!");
1022 return HC_ERR_JSON_GET;
1023 }
1024 const char *peerUdid = GetStringFromJson(jsonParams, FIELD_CONN_DEVICE_ID);
1025 if (peerUdid == NULL) {
1026 peerUdid = GetStringFromJson(session->params, FIELD_PEER_UDID);
1027 }
1028 if (peerUdid == NULL) {
1029 LOGE("Failed to get peerUdid from jsonParams and params!");
1030 return HC_ERR_JSON_GET;
1031 }
1032 const char *groupId = GetStringFromJson(session->params, FIELD_GROUP_ID);
1033 if (groupId == NULL) {
1034 LOGE("Failed to get groupId from session params!");
1035 return HC_ERR_JSON_GET;
1036 }
1037 if (session->opCode == MEMBER_DELETE) {
1038 return HandleUnbindSuccess(peerAuthId, groupId, session);
1039 } else {
1040 return HandleBindSuccess(peerAuthId, peerUdid, groupId, session, out);
1041 }
1042 }
1043
OnSessionFinish(const BindSession * session,CJson * jsonParams,CJson * out)1044 static int32_t OnSessionFinish(const BindSession *session, CJson *jsonParams, CJson *out)
1045 {
1046 int32_t result;
1047 CJson *sendData = GetObjFromJson(out, FIELD_SEND_TO_PEER);
1048 /* The last packet may need to be sent. */
1049 if (sendData != NULL) {
1050 result = InteractWithPeer(session, sendData);
1051 if (result != HC_SUCCESS) {
1052 return result;
1053 }
1054 }
1055 result = OnBindOrUnbindFinish(session, jsonParams, out);
1056 if (result != HC_SUCCESS) {
1057 LOGE("An error occurred when processing different end operations!");
1058 return result;
1059 }
1060 LOGI("The session completed successfully! [ReqId]: %" PRId64, session->reqId);
1061 NotifyBindResult(session->channelType, session->channelId);
1062 CloseChannel(session->channelType, session->channelId);
1063 return HC_SUCCESS;
1064 }
1065
ProcessBindSessionInner(BindSession * session,CJson * jsonParams,int32_t * status,bool * isNeedInform)1066 static int32_t ProcessBindSessionInner(BindSession *session, CJson *jsonParams, int32_t *status, bool *isNeedInform)
1067 {
1068 int32_t result;
1069 if (((result = CheckPeerStatus(jsonParams, isNeedInform)) != HC_SUCCESS) ||
1070 ((result = TryAddPeerUserTypeToParams(jsonParams, session))) != HC_SUCCESS) {
1071 return result;
1072 }
1073
1074 CJson *out = CreateJson();
1075 if (out == NULL) {
1076 LOGE("Failed to allocate out memory!");
1077 return HC_ERR_JSON_FAIL;
1078 }
1079 result = ProcessModule(session, jsonParams, out, status);
1080 if (result != HC_SUCCESS) {
1081 *isNeedInform = false;
1082 InformPeerModuleError(out, session);
1083 FreeJson(out);
1084 return result;
1085 }
1086 if (*status == IGNORE_MSG) {
1087 FreeJson(out);
1088 return HC_SUCCESS;
1089 } else if (*status == CONTINUE) {
1090 DeleteAllItem(jsonParams);
1091 CJson *sendData = DetachItemFromJson(out, FIELD_SEND_TO_PEER);
1092 FreeJson(out);
1093 if (sendData == NULL) {
1094 LOGE("Failed to get sendToPeer from out!");
1095 return HC_ERR_JSON_GET;
1096 }
1097 result = InteractWithPeer(session, sendData);
1098 FreeJson(sendData);
1099 return result;
1100 }
1101 result = OnSessionFinish(session, jsonParams, out);
1102 FreeJson(out);
1103 return result;
1104 }
1105
ProcessBindSession(Session * session,CJson * jsonParams)1106 int32_t ProcessBindSession(Session *session, CJson *jsonParams)
1107 {
1108 if ((session == NULL) || (jsonParams == NULL)) {
1109 LOGE("The input session or jsonParams is NULL!");
1110 return HC_ERR_INVALID_PARAMS;
1111 }
1112 BindSession *realSession = (BindSession *)session;
1113 LOGI("Start to process bind session successfully! [ReqId]: %" PRId64, realSession->reqId);
1114
1115 bool isNeedInform = true;
1116 int32_t status = CONTINUE;
1117 int32_t result = ProcessBindSessionInner(realSession, jsonParams, &status, &isNeedInform);
1118 if (result != HC_SUCCESS) {
1119 LOGE("An error occurs during processing bind session. We need to notify the service!");
1120 InformPeerGroupErrorIfNeed(isNeedInform, result, realSession);
1121 if ((!NeedForceDelete(realSession)) || (ForceUnbindDevice(realSession) != HC_SUCCESS)) {
1122 ProcessErrorCallback(realSession->reqId, realSession->opCode, result, NULL,
1123 realSession->base.callback);
1124 }
1125 CloseChannel(realSession->channelType, realSession->channelId);
1126 return result;
1127 }
1128 LOGI("Process bind session successfully! [ReqId]: %" PRId64, realSession->reqId);
1129 if (status == FINISH) {
1130 return status;
1131 }
1132 return result;
1133 }
1134
NeedCreateGroup(int isClient,int operationCode)1135 bool NeedCreateGroup(int isClient, int operationCode)
1136 {
1137 if (((isClient == CLIENT) && (operationCode == MEMBER_JOIN)) ||
1138 ((isClient == SERVER) && (operationCode == MEMBER_INVITE))) {
1139 return true;
1140 } else {
1141 return false;
1142 }
1143 }
1144
NeedForceDelete(const BindSession * session)1145 bool NeedForceDelete(const BindSession *session)
1146 {
1147 bool needForceDelete = false;
1148 (void)GetBoolFromJson(session->params, FIELD_IS_FORCE_DELETE, &needForceDelete);
1149 return needForceDelete;
1150 }
1151
ForceUnbindDevice(const BindSession * session)1152 int32_t ForceUnbindDevice(const BindSession *session)
1153 {
1154 const char *peerAuthId = GetStringFromJson(session->params, FIELD_PEER_AUTH_ID);
1155 if (peerAuthId == NULL) {
1156 LOGE("Failed to get peerAuthId from jsonParams!");
1157 return HC_ERR_JSON_GET;
1158 }
1159 const char *groupId = GetStringFromJson(session->params, FIELD_GROUP_ID);
1160 if (groupId == NULL) {
1161 LOGE("Failed to get groupId from jsonParams!");
1162 return HC_ERR_JSON_GET;
1163 }
1164 QueryDeviceParams queryDeviceParams = InitQueryDeviceParams();
1165 queryDeviceParams.groupId = groupId;
1166 queryDeviceParams.authId = peerAuthId;
1167 if (DelTrustedDevice(session->osAccountId, &queryDeviceParams) != HC_SUCCESS ||
1168 SaveOsAccountDb(session->osAccountId) != HC_SUCCESS) {
1169 LOGE("Failed to delete trust device from database!");
1170 return HC_ERR_DB;
1171 }
1172 /*
1173 * If the trusted device has been deleted from the database but the peer key fails to be deleted,
1174 * the forcible unbinding is still considered successful. Only logs need to be printed.
1175 */
1176 int32_t result = ForceDeletePeerKey(session->params);
1177 if (result != HC_SUCCESS) {
1178 LOGE("Failed to delete peer key!");
1179 }
1180 LOGI("An error occurs during the online unbinding. Therefore, we forcibly unbind the peer device!");
1181 char *returnDataStr = NULL;
1182 result = GenerateUnbindSuccessData(peerAuthId, groupId, &returnDataStr);
1183 if (result != HC_SUCCESS) {
1184 return result;
1185 }
1186 ProcessFinishCallback(session->reqId, MEMBER_DELETE, returnDataStr, session->base.callback);
1187 FreeJsonString(returnDataStr);
1188 return HC_SUCCESS;
1189 }
1190
GenerateBindParams(int32_t osAccountId,int isClient,const CJson * jsonParams,BindSession * session)1191 int32_t GenerateBindParams(int32_t osAccountId, int isClient, const CJson *jsonParams, BindSession *session)
1192 {
1193 if (session->params == NULL) {
1194 session->params = CreateJson();
1195 if (session->params == NULL) {
1196 LOGE("Failed to allocate session params memory!");
1197 return HC_ERR_ALLOC_MEMORY;
1198 }
1199 }
1200
1201 int32_t result;
1202 if (((result = AddIsForceDeleteIfNeed(isClient, jsonParams, session)) != HC_SUCCESS) ||
1203 ((result = AddChannelIdIfNeed(isClient, jsonParams, session)) != HC_SUCCESS) ||
1204 ((result = AddPinCodeIfNeed(jsonParams, session)) != HC_SUCCESS) ||
1205 ((result = AddGroupAndDevInfo(osAccountId, isClient, jsonParams, session)) != HC_SUCCESS) ||
1206 ((result = AddPeerDevInfoIfNeed(isClient, jsonParams, session)) != HC_SUCCESS)) {
1207 return result;
1208 }
1209 return HC_SUCCESS;
1210 }
1211
AddInfoToSendData(bool isNeedCompatibleInfo,const BindSession * session,CJson * data)1212 int32_t AddInfoToSendData(bool isNeedCompatibleInfo, const BindSession *session, CJson *data)
1213 {
1214 int32_t result;
1215 if (((result = AddGroupInfoToSendData(session, data)) != HC_SUCCESS) ||
1216 ((result = AddDevInfoToSendData(session, data)) != HC_SUCCESS) ||
1217 ((result = AddRequestInfoToSendData(session, data)) != HC_SUCCESS) ||
1218 ((result = AddCompatibleInfoToSendData(isNeedCompatibleInfo, data)) != HC_SUCCESS)) {
1219 return result;
1220 }
1221 return HC_SUCCESS;
1222 }
1223
GenerateBasicModuleParams(bool isClient,BindSession * session,CJson * moduleParams)1224 int32_t GenerateBasicModuleParams(bool isClient, BindSession *session, CJson *moduleParams)
1225 {
1226 int32_t result;
1227 if (((result = AddGroupAndDevInfoToParams(session, moduleParams)) != HC_SUCCESS) ||
1228 ((result = AddRequestInfoToParams(isClient, session, moduleParams)) != HC_SUCCESS) ||
1229 ((result = AddPinCodeToParamsIfNeed(session, moduleParams)) != HC_SUCCESS) ||
1230 ((result = AddPeerAuthIdAndUserTypeToParamsIfNeed(session, moduleParams)) != HC_SUCCESS)) {
1231 return result;
1232 }
1233 return HC_SUCCESS;
1234 }
1235