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