1 /*
2 * Copyright (C) 2023 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 "compatible_bind_sub_session_common.h"
17
18 #include "group_operation_common.h"
19 #include "group_auth_data_operation.h"
20 #include "hc_dev_info.h"
21 #include "hc_log.h"
22
AddPinCode(const CJson * jsonParams,CompatibleBindSubSession * session)23 static int32_t AddPinCode(const CJson *jsonParams, CompatibleBindSubSession *session)
24 {
25 const char *pinCode = GetStringFromJson(jsonParams, FIELD_PIN_CODE);
26 if (pinCode == NULL) {
27 LOGE("Failed to get pinCode from jsonParams!");
28 return HC_ERR_JSON_GET;
29 }
30 if (AddStringToJson(session->params, FIELD_PIN_CODE, pinCode) != HC_SUCCESS) {
31 LOGE("Failed to add pinCode to params!");
32 return HC_ERR_JSON_ADD;
33 }
34 return HC_SUCCESS;
35 }
36
AddProtocolExpandVal(const CJson * jsonParams,CompatibleBindSubSession * session)37 static int32_t AddProtocolExpandVal(const CJson *jsonParams, CompatibleBindSubSession *session)
38 {
39 int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
40 (void)GetIntFromJson(jsonParams, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
41 if (AddIntToJson(session->params, FIELD_PROTOCOL_EXPAND, protocolExpandVal) != HC_SUCCESS) {
42 LOGE("Failed to add protocol expand val to params!");
43 return HC_ERR_JSON_ADD;
44 }
45 return HC_SUCCESS;
46 }
47
AddGroupId(const char * groupId,CJson * params)48 static int32_t AddGroupId(const char *groupId, CJson *params)
49 {
50 if (AddStringToJson(params, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
51 LOGE("Failed to add groupId to params!");
52 return HC_ERR_JSON_ADD;
53 }
54 return HC_SUCCESS;
55 }
56
AddGroupName(const CJson * jsonParams,CJson * params)57 static int32_t AddGroupName(const CJson *jsonParams, CJson *params)
58 {
59 const char *groupName = GetStringFromJson(jsonParams, FIELD_GROUP_NAME);
60 if (groupName == NULL) {
61 LOGE("Failed to get groupName from jsonParams!");
62 return HC_ERR_JSON_GET;
63 }
64 if (AddStringToJson(params, FIELD_GROUP_NAME, groupName) != HC_SUCCESS) {
65 LOGE("Failed to add groupName to params!");
66 return HC_ERR_JSON_ADD;
67 }
68 return HC_SUCCESS;
69 }
70
AddGroupOwnerIfExist(const CJson * jsonParams,CJson * params)71 static int32_t AddGroupOwnerIfExist(const CJson *jsonParams, CJson *params)
72 {
73 const char *groupOwner = GetStringFromJson(jsonParams, FIELD_GROUP_OWNER);
74 if ((groupOwner != NULL) && (AddStringToJson(params, FIELD_GROUP_OWNER, groupOwner) != HC_SUCCESS)) {
75 LOGE("Failed to add groupOwner to params!");
76 return HC_ERR_JSON_ADD;
77 }
78 return HC_SUCCESS;
79 }
80
AddGroupTypeIfValid(const CJson * jsonParams,CJson * params)81 static int32_t AddGroupTypeIfValid(const CJson *jsonParams, CJson *params)
82 {
83 int32_t groupType = PEER_TO_PEER_GROUP;
84 if (GetIntFromJson(jsonParams, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
85 LOGE("Failed to get groupType from json params!");
86 return HC_ERR_JSON_GET;
87 }
88 if (groupType != PEER_TO_PEER_GROUP) {
89 LOGE("The input groupType is invalid!");
90 return HC_ERR_INVALID_PARAMS;
91 }
92 if (AddIntToJson(params, FIELD_GROUP_TYPE, groupType) != HC_SUCCESS) {
93 LOGE("Failed to add groupType to params!");
94 return HC_ERR_JSON_ADD;
95 }
96 return HC_SUCCESS;
97 }
98
AddGroupVisibilityIfValidOrDefault(const CJson * jsonParams,CJson * params)99 static int32_t AddGroupVisibilityIfValidOrDefault(const CJson *jsonParams, CJson *params)
100 {
101 int32_t groupVisibility = GROUP_VISIBILITY_PUBLIC;
102 (void)GetIntFromJson(jsonParams, FIELD_GROUP_VISIBILITY, &groupVisibility);
103 if (!IsGroupVisibilityValid(groupVisibility)) {
104 LOGE("The input groupVisibility is invalid!");
105 return HC_ERR_INVALID_PARAMS;
106 }
107 if (AddIntToJson(params, FIELD_GROUP_VISIBILITY, groupVisibility) != HC_SUCCESS) {
108 LOGE("Failed to add groupVisibility to params!");
109 return HC_ERR_JSON_ADD;
110 }
111 return HC_SUCCESS;
112 }
113
AddExpireTimeIfValidOrDefault(const CJson * jsonParams,CJson * params)114 static int32_t AddExpireTimeIfValidOrDefault(const CJson *jsonParams, CJson *params)
115 {
116 int32_t expireTime = DEFAULT_EXPIRE_TIME;
117 (void)GetIntFromJson(jsonParams, FIELD_EXPIRE_TIME, &expireTime);
118 if (!IsExpireTimeValid(expireTime)) {
119 LOGE("The input expireTime is invalid!");
120 return HC_ERR_INVALID_PARAMS;
121 }
122 if (AddIntToJson(params, FIELD_EXPIRE_TIME, expireTime) != HC_SUCCESS) {
123 LOGE("Failed to add expireTime to params!");
124 return HC_ERR_JSON_ADD;
125 }
126 return HC_SUCCESS;
127 }
128
AddGroupInfoToSessionParams(const char * groupId,const CJson * jsonParams,CJson * params)129 static int32_t AddGroupInfoToSessionParams(const char *groupId, const CJson *jsonParams, CJson *params)
130 {
131 int32_t result;
132 if (((result = AddGroupId(groupId, params)) != HC_SUCCESS) ||
133 ((result = AddGroupName(jsonParams, params)) != HC_SUCCESS) ||
134 ((result = AddGroupOwnerIfExist(jsonParams, params)) != HC_SUCCESS) ||
135 ((result = AddGroupTypeIfValid(jsonParams, params)) != HC_SUCCESS) ||
136 ((result = AddGroupVisibilityIfValidOrDefault(jsonParams, params)) != HC_SUCCESS) ||
137 ((result = AddExpireTimeIfValidOrDefault(jsonParams, params)) != HC_SUCCESS)) {
138 return result;
139 }
140 return HC_SUCCESS;
141 }
142
CheckAuthIdAndUserTypeValid(int32_t osAccountId,int userType,const char * groupId,const char * authId)143 static int32_t CheckAuthIdAndUserTypeValid(int32_t osAccountId, int userType, const char *groupId, const char *authId)
144 {
145 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
146 return HC_SUCCESS;
147 }
148 char udid[INPUT_UDID_LEN] = { 0 };
149 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
150 if (res != HC_SUCCESS) {
151 LOGE("Failed to get local udid! res: %d", res);
152 return res;
153 }
154 TrustedDeviceEntry *deviceInfo = CreateDeviceEntry();
155 if (deviceInfo == NULL) {
156 LOGE("Failed to allocate deviceInfo memory!");
157 return HC_ERR_ALLOC_MEMORY;
158 }
159 int32_t result = GetTrustedDevInfoById(osAccountId, udid, true, groupId, deviceInfo);
160 if (result != HC_SUCCESS) {
161 LOGE("Failed to obtain the local device information from the database!");
162 DestroyDeviceEntry(deviceInfo);
163 return result;
164 }
165 const char *oriAuthId = StringGet(&deviceInfo->authId);
166 if ((deviceInfo->devType != userType) || ((oriAuthId != NULL) && (strcmp(oriAuthId, authId) != 0))) {
167 LOGE("Once a group is created, the service cannot change the local authId and userType used in the group!");
168 DestroyDeviceEntry(deviceInfo);
169 return HC_ERR_INVALID_PARAMS;
170 }
171 DestroyDeviceEntry(deviceInfo);
172 return HC_SUCCESS;
173 }
174
AddAuthIdAndUserTypeIfValidOrDefault(int32_t osAccountId,const char * groupId,const CJson * jsonParams,CJson * params)175 static int32_t AddAuthIdAndUserTypeIfValidOrDefault(int32_t osAccountId, const char *groupId, const CJson *jsonParams,
176 CJson *params)
177 {
178 int32_t userType = DEVICE_TYPE_ACCESSORY;
179 (void)GetIntFromJson(jsonParams, FIELD_USER_TYPE, &userType);
180 if (!IsUserTypeValid(userType)) {
181 LOGE("The input userType is invalid!");
182 return HC_ERR_INVALID_PARAMS;
183 }
184 const char *authId = GetStringFromJson(jsonParams, FIELD_DEVICE_ID);
185 char udid[INPUT_UDID_LEN] = { 0 };
186 if (authId == NULL) {
187 LOGI("authId is not found, use udid by default!");
188 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
189 if (res != HC_SUCCESS) {
190 LOGE("Failed to get local udid! res: %d", res);
191 return res;
192 }
193 authId = udid;
194 }
195 int32_t result = CheckAuthIdAndUserTypeValid(osAccountId, userType, groupId, authId);
196 if (result != HC_SUCCESS) {
197 return result;
198 }
199 if (AddIntToJson(params, FIELD_USER_TYPE, userType) != HC_SUCCESS) {
200 LOGE("Failed to add userType to params!");
201 return HC_ERR_JSON_ADD;
202 }
203 if (AddStringToJson(params, FIELD_AUTH_ID, authId) != HC_SUCCESS) {
204 LOGE("Failed to add authId to params!");
205 return HC_ERR_JSON_ADD;
206 }
207 return HC_SUCCESS;
208 }
209
AddUdid(CJson * params)210 static int32_t AddUdid(CJson *params)
211 {
212 char udid[INPUT_UDID_LEN] = { 0 };
213 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
214 if (res != HC_SUCCESS) {
215 LOGE("Failed to get local udid! res: %d", res);
216 return res;
217 }
218 if (AddStringToJson(params, FIELD_CONN_DEVICE_ID, udid) != HC_SUCCESS) {
219 LOGE("Failed to add udid to params!");
220 return HC_ERR_JSON_ADD;
221 }
222 return HC_SUCCESS;
223 }
224
AddUserTypeIfValidOrDefault(const CJson * jsonParams,CJson * params)225 static int32_t AddUserTypeIfValidOrDefault(const CJson *jsonParams, CJson *params)
226 {
227 int32_t userType = DEVICE_TYPE_ACCESSORY;
228 (void)GetIntFromJson(jsonParams, FIELD_USER_TYPE, &userType);
229 if (!IsUserTypeValid(userType)) {
230 LOGE("The input userType is invalid!");
231 return HC_ERR_INVALID_PARAMS;
232 }
233 if (AddIntToJson(params, FIELD_USER_TYPE, userType) != HC_SUCCESS) {
234 LOGE("Failed to add userType to params!");
235 return HC_ERR_JSON_ADD;
236 }
237 return HC_SUCCESS;
238 }
239
AddDevInfoToSessionParams(int32_t osAccountId,const char * groupId,const CJson * jsonParams,CJson * params)240 static int32_t AddDevInfoToSessionParams(int32_t osAccountId, const char *groupId, const CJson *jsonParams,
241 CJson *params)
242 {
243 int32_t result;
244 if (((result = AddAuthIdAndUserTypeIfValidOrDefault(osAccountId, groupId, jsonParams, params)) != HC_SUCCESS) ||
245 ((result = AddUdid(params)) != HC_SUCCESS) ||
246 ((result = AddUserTypeIfValidOrDefault(jsonParams, params)) != HC_SUCCESS)) {
247 return result;
248 }
249 return HC_SUCCESS;
250 }
251
GenerateParamsByInput(int32_t osAccountId,const char * groupId,const CJson * jsonParams,CJson * params)252 static int32_t GenerateParamsByInput(int32_t osAccountId, const char *groupId, const CJson *jsonParams, CJson *params)
253 {
254 int32_t result = AddGroupInfoToSessionParams(groupId, jsonParams, params);
255 if (result != HC_SUCCESS) {
256 return result;
257 }
258 return AddDevInfoToSessionParams(osAccountId, groupId, jsonParams, params);
259 }
260
AddGroupInfoToParams(const TrustedGroupEntry * entry,CJson * params)261 static int32_t AddGroupInfoToParams(const TrustedGroupEntry *entry, CJson *params)
262 {
263 if (AddStringToJson(params, FIELD_GROUP_ID, StringGet(&entry->id)) != HC_SUCCESS) {
264 LOGE("Failed to add groupId to json!");
265 return HC_ERR_JSON_ADD;
266 }
267 if (AddIntToJson(params, FIELD_GROUP_TYPE, entry->type) != HC_SUCCESS) {
268 LOGE("Failed to add groupType to json!");
269 return HC_ERR_JSON_ADD;
270 }
271 if (AddStringToJson(params, FIELD_GROUP_NAME, StringGet(&entry->name)) != HC_SUCCESS) {
272 LOGE("Failed to add groupName to json!");
273 return HC_ERR_JSON_ADD;
274 }
275 return HC_SUCCESS;
276 }
277
AddGroupInfoByDatabase(int32_t osAccountId,const char * groupId,CJson * params)278 static int32_t AddGroupInfoByDatabase(int32_t osAccountId, const char *groupId, CJson *params)
279 {
280 TrustedGroupEntry *entry = GetGroupEntryById(osAccountId, groupId);
281 if (entry == NULL) {
282 LOGE("Failed to get groupEntry from db!");
283 return HC_ERR_DB;
284 }
285 int32_t res = AddGroupInfoToParams(entry, params);
286 if (res != HC_SUCCESS) {
287 DestroyGroupEntry(entry);
288 return res;
289 }
290 DestroyGroupEntry(entry);
291 return HC_SUCCESS;
292 }
293
AddDevInfoToParams(const TrustedDeviceEntry * devAuthParams,CJson * params)294 static int32_t AddDevInfoToParams(const TrustedDeviceEntry *devAuthParams, CJson *params)
295 {
296 if (AddStringToJson(params, FIELD_AUTH_ID, StringGet(&devAuthParams->authId)) != HC_SUCCESS) {
297 LOGE("Failed to add authId to params!");
298 return HC_ERR_JSON_ADD;
299 }
300 if (AddStringToJson(params, FIELD_CONN_DEVICE_ID, StringGet(&devAuthParams->udid)) != HC_SUCCESS) {
301 LOGE("Failed to add udid to params!");
302 return HC_ERR_JSON_ADD;
303 }
304 if (AddIntToJson(params, FIELD_USER_TYPE, devAuthParams->devType) != HC_SUCCESS) {
305 LOGE("Failed to add userType to params!");
306 return HC_ERR_JSON_ADD;
307 }
308 return HC_SUCCESS;
309 }
310
AddDevInfoByDatabase(int32_t osAccountId,const char * groupId,CJson * params)311 static int32_t AddDevInfoByDatabase(int32_t osAccountId, const char *groupId, CJson *params)
312 {
313 char udid[INPUT_UDID_LEN] = { 0 };
314 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
315 if (res != HC_SUCCESS) {
316 LOGE("Failed to get local udid! res: %d", res);
317 return res;
318 }
319 TrustedDeviceEntry *devAuthParams = CreateDeviceEntry();
320 if (devAuthParams == NULL) {
321 LOGE("Failed to allocate devEntry memory!");
322 return HC_ERR_ALLOC_MEMORY;
323 }
324 res = GetTrustedDevInfoById(osAccountId, udid, true, groupId, devAuthParams);
325 if (res != HC_SUCCESS) {
326 LOGE("Failed to obtain the device information from the database!");
327 DestroyDeviceEntry(devAuthParams);
328 return res;
329 }
330 res = AddDevInfoToParams(devAuthParams, params);
331 if (res != HC_SUCCESS) {
332 DestroyDeviceEntry(devAuthParams);
333 return res;
334 }
335 DestroyDeviceEntry(devAuthParams);
336 return HC_SUCCESS;
337 }
338
GenerateParamsByDatabase(int32_t osAccountId,const char * groupId,CJson * params)339 static int32_t GenerateParamsByDatabase(int32_t osAccountId, const char *groupId, CJson *params)
340 {
341 int32_t result = AddGroupInfoByDatabase(osAccountId, groupId, params);
342 if (result != HC_SUCCESS) {
343 return result;
344 }
345 return AddDevInfoByDatabase(osAccountId, groupId, params);
346 }
347
AddSelfUpgradeFlag(CJson * params,bool isClient,int32_t osAccountId,const char * groupId,int32_t operationCode)348 static int32_t AddSelfUpgradeFlag(CJson *params, bool isClient, int32_t osAccountId, const char *groupId,
349 int32_t operationCode)
350 {
351 bool isNeeded = (operationCode == MEMBER_INVITE && isClient) || (operationCode == MEMBER_JOIN && !isClient);
352 if (!isNeeded) {
353 LOGI("No need to add self upgrade flag.");
354 return HC_SUCCESS;
355 }
356 TrustedDeviceEntry *selfDeviceEntry = CreateDeviceEntry();
357 if (selfDeviceEntry == NULL) {
358 LOGE("Failed to create self device entry!");
359 return HC_ERR_ALLOC_MEMORY;
360 }
361 int32_t res = GaGetLocalDeviceInfo(osAccountId, groupId, selfDeviceEntry);
362 if (res != HC_SUCCESS) {
363 LOGE("Failed to get self device entry!");
364 DestroyDeviceEntry(selfDeviceEntry);
365 return res;
366 }
367 bool isSelfFromUpgrade = selfDeviceEntry->upgradeFlag == 1;
368 DestroyDeviceEntry(selfDeviceEntry);
369 if (AddBoolToJson(params, FIELD_IS_SELF_FROM_UPGRADE, isSelfFromUpgrade) != HC_SUCCESS) {
370 LOGE("Failed to add self upgrade flag!");
371 return HC_ERR_JSON_ADD;
372 }
373 return HC_SUCCESS;
374 }
375
AddGroupAndDevInfo(int32_t osAccountId,int isClient,const CJson * jsonParams,CompatibleBindSubSession * session)376 static int32_t AddGroupAndDevInfo(int32_t osAccountId, int isClient, const CJson *jsonParams,
377 CompatibleBindSubSession *session)
378 {
379 const char *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID);
380 if (groupId == NULL) {
381 LOGE("Failed to get groupId from jsonParams!");
382 return HC_ERR_JSON_GET;
383 }
384 int32_t res = AddSelfUpgradeFlag(session->params, (isClient == CLIENT), osAccountId, groupId, session->opCode);
385 if (res != HC_SUCCESS) {
386 LOGE("Failed to add self upgrade flag!");
387 return res;
388 }
389 if (IsCreateGroupNeeded(isClient, session->opCode)) {
390 return GenerateParamsByInput(osAccountId, groupId, jsonParams, session->params);
391 } else {
392 return GenerateParamsByDatabase(osAccountId, groupId, session->params);
393 }
394 }
395
AddPeerAuthIdAndUdidIfExist(const CJson * jsonParams,CompatibleBindSubSession * session)396 static int32_t AddPeerAuthIdAndUdidIfExist(const CJson *jsonParams, CompatibleBindSubSession *session)
397 {
398 const char *peerAuthId = GetStringFromJson(jsonParams, FIELD_PEER_DEVICE_ID);
399 if (peerAuthId != NULL && AddStringToJson(session->params, FIELD_PEER_AUTH_ID, peerAuthId) != HC_SUCCESS) {
400 LOGE("Failed to add peerAuthId to params!");
401 return HC_ERR_JSON_ADD;
402 }
403 const char *peerUdid = GetStringFromJson(jsonParams, FIELD_PEER_UDID);
404 if (peerUdid != NULL && AddStringToJson(session->params, FIELD_PEER_UDID, peerUdid) != HC_SUCCESS) {
405 LOGE("Failed to add peerUdid to params!");
406 return HC_ERR_JSON_ADD;
407 }
408 return HC_SUCCESS;
409 }
410
AddGroupAndDevInfoToParams(const CompatibleBindSubSession * session,CJson * moduleParams)411 static int32_t AddGroupAndDevInfoToParams(const CompatibleBindSubSession *session, CJson *moduleParams)
412 {
413 const char *groupId = GetStringFromJson(session->params, FIELD_GROUP_ID);
414 if (groupId == NULL) {
415 LOGE("Failed to get groupId from params!");
416 return HC_ERR_JSON_GET;
417 }
418 const char *authId = GetStringFromJson(session->params, FIELD_AUTH_ID);
419 if (authId == NULL) {
420 LOGE("Failed to get authId from params!");
421 return HC_ERR_JSON_GET;
422 }
423 bool isSelfFromUpgrade = false;
424 (void)GetBoolFromJson(session->params, FIELD_IS_SELF_FROM_UPGRADE, &isSelfFromUpgrade);
425 int32_t userType = DEVICE_TYPE_ACCESSORY;
426 if (GetIntFromJson(session->params, FIELD_USER_TYPE, &userType) != HC_SUCCESS) {
427 LOGE("Failed to get userType from params!");
428 return HC_ERR_JSON_GET;
429 }
430 if (AddBoolToJson(moduleParams, FIELD_IS_SELF_FROM_UPGRADE, isSelfFromUpgrade) != HC_SUCCESS) {
431 LOGE("Failed to add isSelfFromUpgrade to moduleParams!");
432 return HC_ERR_JSON_ADD;
433 }
434 if (AddStringToJson(moduleParams, FIELD_SERVICE_TYPE, groupId) != HC_SUCCESS) {
435 LOGE("Failed to add serviceType to moduleParams!");
436 return HC_ERR_JSON_ADD;
437 }
438 if (AddStringToJson(moduleParams, FIELD_SELF_AUTH_ID, authId) != HC_SUCCESS) {
439 LOGE("Failed to add serviceType to moduleParams!");
440 return HC_ERR_JSON_ADD;
441 }
442 if (AddIntToJson(moduleParams, FIELD_SELF_TYPE, userType) != HC_SUCCESS) {
443 LOGE("Failed to add userType to moduleParams!");
444 return HC_ERR_JSON_ADD;
445 }
446 return HC_SUCCESS;
447 }
448
AddRequestInfoToParams(bool isClient,const CompatibleBindSubSession * session,CJson * moduleParams)449 static int32_t AddRequestInfoToParams(bool isClient, const CompatibleBindSubSession *session, CJson *moduleParams)
450 {
451 if (AddInt64StringToJson(moduleParams, FIELD_REQUEST_ID, session->reqId) != HC_SUCCESS) {
452 LOGE("Failed to add requestId to moduleParams!");
453 return HC_ERR_JSON_ADD;
454 }
455 if (AddIntToJson(moduleParams, FIELD_KEY_LENGTH, DEFAULT_RETURN_KEY_LENGTH) != HC_SUCCESS) {
456 LOGE("Failed to add sessionKeyLength to moduleParams!");
457 return HC_ERR_JSON_ADD;
458 }
459 if (AddBoolToJson(moduleParams, FIELD_IS_CLIENT, isClient) != HC_SUCCESS) {
460 LOGE("Failed to add isClient to moduleParams!");
461 return HC_ERR_JSON_ADD;
462 }
463 /* Use the GroupManager package name. */
464 if (AddStringToJson(moduleParams, FIELD_PKG_NAME, GROUP_MANAGER_PACKAGE_NAME) != HC_SUCCESS) {
465 LOGE("Failed to add pkgName to moduleParams!");
466 return HC_ERR_JSON_ADD;
467 }
468 return HC_SUCCESS;
469 }
470
AddPinCodeToParams(CompatibleBindSubSession * session,CJson * moduleParams)471 static int32_t AddPinCodeToParams(CompatibleBindSubSession *session, CJson *moduleParams)
472 {
473 const char *pinCode = GetStringFromJson(session->params, FIELD_PIN_CODE);
474 if (pinCode == NULL) {
475 LOGE("Failed to get pinCode from params!");
476 return HC_ERR_JSON_GET;
477 }
478 if (AddStringToJson(moduleParams, FIELD_PIN_CODE, pinCode) != HC_SUCCESS) {
479 LOGE("Failed to add pinCode to moduleParams!");
480 return HC_ERR_JSON_ADD;
481 }
482 /* Release the memory in advance to reduce the memory usage. */
483 (void)DeleteItemFromJson(session->params, FIELD_PIN_CODE);
484 return HC_SUCCESS;
485 }
486
AddProtocolExpandValToParams(CompatibleBindSubSession * session,CJson * moduleParams)487 static int32_t AddProtocolExpandValToParams(CompatibleBindSubSession *session, CJson *moduleParams)
488 {
489 int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
490 (void)GetIntFromJson(session->params, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
491 if (AddIntToJson(moduleParams, FIELD_PROTOCOL_EXPAND, protocolExpandVal) != HC_SUCCESS) {
492 LOGE("Failed to add protocol expand val to moduleParams!");
493 return HC_ERR_JSON_ADD;
494 }
495 return HC_SUCCESS;
496 }
497
AddOsAccountIdToParams(CompatibleBindSubSession * session,CJson * moduleParams)498 static int32_t AddOsAccountIdToParams(CompatibleBindSubSession *session, CJson *moduleParams)
499 {
500 if (AddIntToJson(moduleParams, FIELD_OS_ACCOUNT_ID, session->osAccountId) != HC_SUCCESS) {
501 LOGE("Failed to add osAccountId to moduleParams!");
502 return HC_ERR_JSON_ADD;
503 }
504 return HC_SUCCESS;
505 }
506
AddGroupInfoToSendData(const CompatibleBindSubSession * session,CJson * data)507 static int32_t AddGroupInfoToSendData(const CompatibleBindSubSession *session, CJson *data)
508 {
509 const char *groupId = GetStringFromJson(session->params, FIELD_GROUP_ID);
510 if (groupId == NULL) {
511 LOGE("Failed to get groupId from params!");
512 return HC_ERR_JSON_GET;
513 }
514 const char *groupName = GetStringFromJson(session->params, FIELD_GROUP_NAME);
515 if (groupName == NULL) {
516 LOGE("Failed to get groupName from params!");
517 return HC_ERR_JSON_GET;
518 }
519 if (AddStringToJson(data, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
520 LOGE("Failed to add groupId to data!");
521 return HC_ERR_JSON_ADD;
522 }
523 if (AddStringToJson(data, FIELD_GROUP_NAME, groupName) != HC_SUCCESS) {
524 LOGE("Failed to add groupName to data!");
525 return HC_ERR_JSON_ADD;
526 }
527 if (AddIntToJson(data, FIELD_GROUP_OP, session->opCode) != HC_SUCCESS) {
528 LOGE("Failed to add groupOp to data!");
529 return HC_ERR_JSON_ADD;
530 }
531 if (AddIntToJson(data, FIELD_GROUP_TYPE, PEER_TO_PEER_GROUP) != HC_SUCCESS) {
532 LOGE("Failed to add groupType to data!");
533 return HC_ERR_JSON_ADD;
534 }
535 return HC_SUCCESS;
536 }
537
AddDevInfoToSendData(const CompatibleBindSubSession * session,CJson * data)538 static int32_t AddDevInfoToSendData(const CompatibleBindSubSession *session, CJson *data)
539 {
540 const char *authId = GetStringFromJson(session->params, FIELD_AUTH_ID);
541 if (authId == NULL) {
542 LOGE("Failed to get authId from params!");
543 return HC_ERR_JSON_GET;
544 }
545 const char *udid = GetStringFromJson(session->params, FIELD_CONN_DEVICE_ID);
546 if (udid == NULL) {
547 LOGE("Failed to get udid from params!");
548 return HC_ERR_JSON_GET;
549 }
550 if (AddStringToJson(data, FIELD_PEER_DEVICE_ID, authId) != HC_SUCCESS) {
551 LOGE("Failed to add peerDeviceId to data!");
552 return HC_ERR_JSON_ADD;
553 }
554 if (AddStringToJson(data, FIELD_CONN_DEVICE_ID, udid) != HC_SUCCESS) {
555 LOGE("Failed to add connDeviceId to data!");
556 return HC_ERR_JSON_ADD;
557 }
558 return HC_SUCCESS;
559 }
560
AddRequestInfoToSendData(const CompatibleBindSubSession * session,CJson * data)561 static int32_t AddRequestInfoToSendData(const CompatibleBindSubSession *session, CJson *data)
562 {
563 if (AddStringToJson(data, FIELD_APP_ID, session->base.appId) != HC_SUCCESS) {
564 LOGE("Failed to add appId to data!");
565 return HC_ERR_JSON_ADD;
566 }
567 if (AddInt64StringToJson(data, FIELD_REQUEST_ID, session->reqId) != HC_SUCCESS) {
568 LOGE("Failed to add requestId to data!");
569 return HC_ERR_JSON_ADD;
570 }
571 if (AddStringToJson(data, FIELD_OWNER_NAME, "") != HC_SUCCESS) {
572 LOGE("Failed to add ownerName to data!");
573 return HC_ERR_JSON_ADD;
574 }
575 return HC_SUCCESS;
576 }
577
GenerateCompatibleInfo(CJson * groupInfo)578 static int32_t GenerateCompatibleInfo(CJson *groupInfo)
579 {
580 char udid[INPUT_UDID_LEN] = { 0 };
581 int32_t res = HcGetUdid((uint8_t *)udid, INPUT_UDID_LEN);
582 if (res != HC_SUCCESS) {
583 LOGE("Failed to get local udid! res: %d", res);
584 return res;
585 }
586 if (AddStringToJson(groupInfo, FIELD_DEVICE_ID, udid) != HC_SUCCESS) {
587 LOGE("Failed to add deviceId to groupInfo!");
588 return HC_ERR_JSON_ADD;
589 }
590 if (AddBoolToJson(groupInfo, FIELD_IS_UUID, true) != HC_SUCCESS) {
591 LOGE("Failed to add uuIdAsDeviceId to groupInfo!");
592 return HC_ERR_JSON_ADD;
593 }
594 /* To be compatible with packets of earlier versions. */
595 CJson *managers = CreateJsonArray();
596 if (managers == NULL) {
597 LOGE("Failed to allocate managers memory!");
598 return HC_ERR_JSON_CREATE;
599 }
600 if (AddObjToJson(groupInfo, FIELD_GROUP_MANAGERS, managers) != HC_SUCCESS) {
601 LOGE("Failed to add groupManagers to groupInfo!");
602 FreeJson(managers);
603 return HC_ERR_JSON_ADD;
604 }
605 FreeJson(managers);
606 /* Currently, only the public group can be created. */
607 if (AddIntToJson(groupInfo, FIELD_GROUP_VISIBILITY, GROUP_VISIBILITY_PUBLIC) != HC_SUCCESS) {
608 LOGE("Failed to add groupVisibility to groupInfo!");
609 return HC_ERR_JSON_ADD;
610 }
611 return HC_SUCCESS;
612 }
613
AddCompatibleInfoToSendData(bool isNeedCompatibleInfo,CJson * data)614 static int32_t AddCompatibleInfoToSendData(bool isNeedCompatibleInfo, CJson *data)
615 {
616 if (!isNeedCompatibleInfo) {
617 return HC_SUCCESS;
618 }
619 CJson *groupInfo = CreateJson();
620 if (groupInfo == NULL) {
621 LOGE("Failed to allocate groupInfo memory!");
622 return HC_ERR_JSON_CREATE;
623 }
624 int32_t res = GenerateCompatibleInfo(groupInfo);
625 if (res != HC_SUCCESS) {
626 FreeJson(groupInfo);
627 return res;
628 }
629 if (AddObjToJson(data, FIELD_GROUP_INFO, groupInfo) != HC_SUCCESS) {
630 LOGE("Failed to add groupInfo to sendData!");
631 FreeJson(groupInfo);
632 return HC_ERR_JSON_ADD;
633 }
634 FreeJson(groupInfo);
635 return HC_SUCCESS;
636 }
637
IsCreateGroupNeeded(int isClient,int operationCode)638 bool IsCreateGroupNeeded(int isClient, int operationCode)
639 {
640 return ((isClient == CLIENT) && (operationCode == MEMBER_JOIN)) ||
641 ((isClient == SERVER) && (operationCode == MEMBER_INVITE));
642 }
643
GenerateBaseBindParams(int32_t osAccountId,int isClient,const CJson * jsonParams,CompatibleBindSubSession * session)644 int32_t GenerateBaseBindParams(int32_t osAccountId, int isClient, const CJson *jsonParams,
645 CompatibleBindSubSession *session)
646 {
647 if (session->params == NULL) {
648 session->params = CreateJson();
649 if (session->params == NULL) {
650 LOGE("Failed to allocate session params memory!");
651 return HC_ERR_JSON_CREATE;
652 }
653 }
654
655 int32_t result;
656 if (((result = AddPinCode(jsonParams, session)) != HC_SUCCESS) ||
657 ((result = AddProtocolExpandVal(jsonParams, session)) != HC_SUCCESS) ||
658 ((result = AddGroupAndDevInfo(osAccountId, isClient, jsonParams, session)) != HC_SUCCESS) ||
659 ((result = AddPeerAuthIdAndUdidIfExist(jsonParams, session)) != HC_SUCCESS)) {
660 return result;
661 }
662
663 return HC_SUCCESS;
664 }
665
GenerateBaseModuleParams(bool isClient,CompatibleBindSubSession * session,CJson * moduleParams)666 int32_t GenerateBaseModuleParams(bool isClient, CompatibleBindSubSession *session, CJson *moduleParams)
667 {
668 int32_t result;
669 if (((result = AddGroupAndDevInfoToParams(session, moduleParams)) != HC_SUCCESS) ||
670 ((result = AddRequestInfoToParams(isClient, session, moduleParams)) != HC_SUCCESS) ||
671 ((result = AddPinCodeToParams(session, moduleParams)) != HC_SUCCESS) ||
672 ((result = AddProtocolExpandValToParams(session, moduleParams)) != HC_SUCCESS) ||
673 ((result = AddOsAccountIdToParams(session, moduleParams)) != HC_SUCCESS)) {
674 return result;
675 }
676 return HC_SUCCESS;
677 }
678
AddInfoToBindData(bool isNeedCompatibleInfo,const CompatibleBindSubSession * session,CJson * data)679 int32_t AddInfoToBindData(bool isNeedCompatibleInfo, const CompatibleBindSubSession *session, CJson *data)
680 {
681 int32_t result;
682 if (((result = AddGroupInfoToSendData(session, data)) != HC_SUCCESS) ||
683 ((result = AddDevInfoToSendData(session, data)) != HC_SUCCESS) ||
684 ((result = AddRequestInfoToSendData(session, data)) != HC_SUCCESS) ||
685 ((result = AddCompatibleInfoToSendData(isNeedCompatibleInfo, data)) != HC_SUCCESS)) {
686 return result;
687 }
688 return HC_SUCCESS;
689 }