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