From 010124717365c98a10e0b704bee30e0728134bcf Mon Sep 17 00:00:00 2001 From: fuzikun Date: Mon, 7 Nov 2022 10:47:31 +0800 Subject: [PATCH] add appId field in error message Signed-off-by: fuzikun --- .../peer_to_peer_group/peer_to_peer_group.c | 14 +++- .../inc/bind_session/bind_session_common.h | 12 ++-- .../src/bind_session/bind_session_client.c | 22 ++---- .../src/bind_session/bind_session_common.c | 69 +++++++++++++------ .../src/bind_session/bind_session_server.c | 20 ++---- 5 files changed, 78 insertions(+), 59 deletions(-) diff --git a/services/group_manager/src/group_manager/account_unrelated/peer_to_peer_group/peer_to_peer_group.c b/services/group_manager/src/group_manager/account_unrelated/peer_to_peer_group/peer_to_peer_group.c index 83d26d8a..7fd95cdc 100644 --- a/services/group_manager/src/group_manager/account_unrelated/peer_to_peer_group/peer_to_peer_group.c +++ b/services/group_manager/src/group_manager/account_unrelated/peer_to_peer_group/peer_to_peer_group.c @@ -357,8 +357,13 @@ static int32_t CheckClientStatus(int operationCode, const CJson *jsonParams) } } -static CJson *GenerateGroupErrorMsg(int32_t errorCode, int64_t requestId) +static CJson *GenerateGroupErrorMsg(int32_t errorCode, int64_t requestId, const CJson *jsonParams) { + const char *appId = GetStringFromJson(jsonParams, FIELD_APP_ID); + if (appId == NULL) { + LOGE("Failed to get appId from jsonParams!"); + return NULL; + } CJson *errorData = CreateJson(); if (errorData == NULL) { LOGE("Failed to allocate errorData memory!"); @@ -369,6 +374,11 @@ static CJson *GenerateGroupErrorMsg(int32_t errorCode, int64_t requestId) FreeJson(errorData); return NULL; } + if (AddStringToJson(errorData, FIELD_APP_ID, appId) != HC_SUCCESS) { + LOGE("Failed to add appId to errorData!"); + FreeJson(errorData); + return NULL; + } if (AddInt64StringToJson(errorData, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) { LOGE("Failed to add requestId to errorData!"); FreeJson(errorData); @@ -388,7 +398,7 @@ static void InformPeerProcessError(int64_t requestId, const CJson *jsonParams, c LOGE("No available channels found!"); return; } - CJson *errorData = GenerateGroupErrorMsg(errorCode, requestId); + CJson *errorData = GenerateGroupErrorMsg(errorCode, requestId, jsonParams); if (errorData == NULL) { return; } diff --git a/services/session/inc/bind_session/bind_session_common.h b/services/session/inc/bind_session/bind_session_common.h index 237a49bd..2d7fef60 100644 --- a/services/session/inc/bind_session/bind_session_common.h +++ b/services/session/inc/bind_session/bind_session_common.h @@ -20,10 +20,14 @@ #include "common_defs.h" #include "hc_types.h" +typedef void (*OnChannelOpenedFunc)(Session *, int64_t, int64_t); +typedef void (*OnConfirmedFunc)(Session *, CJson *); + typedef struct { Session base; - void (*onChannelOpened)(Session *, int64_t channelId, int64_t requestId); - void (*onConfirmationReceived)(Session *, CJson *returnData); + char *appId; + OnChannelOpenedFunc onChannelOpened; + OnConfirmedFunc onConfirmationReceived; int curTaskId; int operationCode; ChannelType channelType; @@ -39,8 +43,8 @@ void DestroyBindSession(Session *session); int32_t SendBindSessionData(const BindSession *session, const CJson *out); void InformPeerProcessErrorIfNeed(bool isModuleError, int32_t errorCode, const BindSession *session); void InformPeerModuleErrorIfNeed(CJson *out, const BindSession *session); -void InitBindSession(int bindType, int operationCode, int64_t requestId, const DeviceAuthCallback *callback, - BindSession *session); +BindSession *CreateBaseBindSession(int32_t sessionType, int32_t opCode, const CJson *params, + const DeviceAuthCallback *callback); int32_t CreateAndProcessModule(BindSession *session, const CJson *in, CJson *out); int32_t ProcessModule(const BindSession *session, const CJson *in, CJson *out, int *status); int32_t AddInfoToSendData(bool isNeedCompatibleInfo, const BindSession *session, CJson *data); diff --git a/services/session/src/bind_session/bind_session_client.c b/services/session/src/bind_session/bind_session_client.c index a2a4b3be..d3c3d5c9 100644 --- a/services/session/src/bind_session/bind_session_client.c +++ b/services/session/src/bind_session/bind_session_client.c @@ -150,24 +150,17 @@ static void InitClientChannel(const DeviceAuthCallback *callback, const CJson *j Session *CreateClientBindSession(CJson *jsonParams, const DeviceAuthCallback *callback) { - int64_t requestId = DEFAULT_REQUEST_ID; - if (GetInt64FromJson(jsonParams, FIELD_REQUEST_ID, &requestId) != HC_SUCCESS) { - LOGE("Failed to get requestId from jsonParams!"); + int32_t opCode = MEMBER_INVITE; + if (GetIntFromJson(jsonParams, FIELD_OPERATION_CODE, &opCode) != HC_SUCCESS) { + LOGE("Failed to get opCode from json!"); return NULL; } - int operationCode = MEMBER_INVITE; - if (GetIntFromJson(jsonParams, FIELD_OPERATION_CODE, &operationCode) != HC_SUCCESS) { - LOGE("Failed to get operationCode from jsonParams!"); - return NULL; - } - LOGI("Start to create client bind session! [RequestId]: %" PRId64 ", [OperationCode]: %d", - requestId, operationCode); /* * If service want to join the peer group, * the identity key pair of the corresponding group needs to be generated here. */ int32_t result; - if (NeedCreateGroup(CLIENT, operationCode)) { + if (NeedCreateGroup(CLIENT, opCode)) { const char *groupId = GetStringFromJson(jsonParams, FIELD_GROUP_ID); if (groupId == NULL) { LOGE("Failed to get groupId from jsonParams!"); @@ -179,12 +172,11 @@ Session *CreateClientBindSession(CJson *jsonParams, const DeviceAuthCallback *ca } } - BindSession *session = (BindSession *)HcMalloc(sizeof(BindSession), 0); + BindSession *session = CreateBaseBindSession(TYPE_CLIENT_BIND_SESSION, opCode, + jsonParams, callback); if (session == NULL) { - LOGE("Failed to allocate session memory!"); return NULL; } - InitBindSession(TYPE_CLIENT_BIND_SESSION, operationCode, requestId, callback, session); InitClientChannel(callback, jsonParams, session); /* The client bind session needs to receive a message indicating that the channel is open. */ session->onChannelOpened = OnBindChannelOpened; @@ -194,7 +186,5 @@ Session *CreateClientBindSession(CJson *jsonParams, const DeviceAuthCallback *ca DestroyBindSession((Session *)session); return NULL; } - LOGI("Create client bind session successfully! [RequestId]: %" PRId64 ", [OperationCode]: %d", - requestId, operationCode); return (Session *)session; } \ No newline at end of file diff --git a/services/session/src/bind_session/bind_session_common.c b/services/session/src/bind_session/bind_session_common.c index 4fce5194..be2864ea 100644 --- a/services/session/src/bind_session/bind_session_common.c +++ b/services/session/src/bind_session/bind_session_common.c @@ -1082,24 +1082,21 @@ static int32_t CheckPeerStatus(const CJson *jsonParams, bool *isNeedInform) return HC_SUCCESS; } -static CJson *GenerateGroupErrorMsg(int32_t errorCode, int64_t requestId) +static int32_t GenerateGroupErrorMsg(int32_t errorCode, const BindSession *session, CJson *errorData) { - CJson *errorData = CreateJson(); - if (errorData == NULL) { - LOGE("Failed to allocate errorData memory!"); - return NULL; - } if (AddIntToJson(errorData, FIELD_GROUP_ERROR_MSG, errorCode) != HC_SUCCESS) { LOGE("Failed to add errorCode to errorData!"); - FreeJson(errorData); - return NULL; + return HC_ERR_JSON_ADD; + } + if (AddStringToJson(errorData, FIELD_APP_ID, session->appId) != HC_SUCCESS) { + LOGE("Failed to add appId to errorData!"); + return HC_ERR_JSON_ADD; } - if (AddInt64StringToJson(errorData, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) { + if (AddInt64StringToJson(errorData, FIELD_REQUEST_ID, session->requestId) != HC_SUCCESS) { LOGE("Failed to add requestId to errorData!"); - FreeJson(errorData); - return NULL; + return HC_ERR_JSON_ADD; } - return errorData; + return HC_SUCCESS; } static int32_t ProcessBindSessionInner(BindSession *session, CJson *jsonParams, int32_t *status, bool *isNeedInform) @@ -1266,11 +1263,17 @@ void InformPeerProcessErrorIfNeed(bool isNeedInform, int32_t errorCode, const Bi if (!isNeedInform) { return; } - CJson *errorData = GenerateGroupErrorMsg(errorCode, session->requestId); + CJson *errorData = CreateJson(); if (errorData == NULL) { + LOGE("Failed to allocate errorData memory!"); return; } - int32_t result = SendBindSessionData(session, errorData); + int32_t res = GenerateGroupErrorMsg(errorCode, session, errorData); + if (res != HC_SUCCESS) { + FreeJson(errorData); + return; + } + res = SendBindSessionData(session, errorData); FreeJson(errorData); if (result != HC_SUCCESS) { LOGE("An error occurred when notifying the peer service!"); @@ -1285,6 +1288,10 @@ void InformPeerModuleErrorIfNeed(CJson *out, const BindSession *session) if (errorData == NULL) { return; } + if (AddStringToJson(errorData, FIELD_APP_ID, session->appId) != HC_SUCCESS) { + LOGE("Failed to add appId to errorData!"); + return HC_ERR_JSON_ADD; + } if (AddInt64StringToJson(errorData, FIELD_REQUEST_ID, session->requestId) != HC_SUCCESS) { LOGE("Failed to add requestId to errorData!"); return; @@ -1364,24 +1371,42 @@ int32_t GenerateBasicModuleParams(bool isClient, BindSession *session, CJson *mo return HC_SUCCESS; } -void InitBindSession(int bindType, int operationCode, int64_t requestId, const DeviceAuthCallback *callback, - BindSession *session) +BindSession *CreateBaseBindSession(int32_t sessionType, int32_t opCode, const CJson *params, + const DeviceAuthCallback *callback) { + int64_t reqId = DEFAULT_REQUEST_ID; + if (GetInt64FromJson(params, FIELD_REQUEST_ID, &reqId) != HC_SUCCESS) { + LOGE("Failed to get reqId from json!"); + return NULL; + } + BindSession *session = (BindSession *)HcMalloc(sizeof(BindSession), 0); + if (session == NULL) { + LOGE("Failed to allocate session memory!"); + return NULL; + } + int32_t res = GenerateSessionOrTaskId(&session->base.sessionId); + if (res != HC_SUCCESS) { + LOGE("Failed to generate session id! res: %d", res); + HcFree(session); + return NULL; + } + session->appId = GetDuplicateAppId(params); + if (session->appId == NULL) { + HcFree(session); + return NULL; + } session->base.process = ProcessBindSession; session->base.destroy = DestroyBindSession; session->curTaskId = 0; session->base.callback = callback; - int res = GenerateSessionOrTaskId(&session->base.sessionId); - if (res != 0) { - return; - } session->base.type = bindType; session->channelType = NO_CHANNEL; - session->operationCode = operationCode; - session->requestId = requestId; + session->operationCode = opCode; + session->requestId = reqId; session->channelId = DEFAULT_CHANNEL_ID; session->isWaiting = HC_FALSE; session->params = NULL; session->onChannelOpened = NULL; session->onConfirmationReceived = NULL; + return session; } \ No newline at end of file diff --git a/services/session/src/bind_session/bind_session_server.c b/services/session/src/bind_session/bind_session_server.c index 0714d390..5fe9231b 100644 --- a/services/session/src/bind_session/bind_session_server.c +++ b/services/session/src/bind_session/bind_session_server.c @@ -466,25 +466,17 @@ static void InitServerChannel(const CJson *jsonParams, BindSession *session) Session *CreateServerBindSession(CJson *jsonParams, const DeviceAuthCallback *callback) { - int64_t requestId = DEFAULT_REQUEST_ID; - if (GetInt64FromJson(jsonParams, FIELD_REQUEST_ID, &requestId) != HC_SUCCESS) { - LOGE("Failed to get requestId from jsonParams!"); + int32_t opCode = MEMBER_INVITE; + if (GetIntFromJson(jsonParams, FIELD_GROUP_OP, &opCode) != HC_SUCCESS) { + LOGE("Failed to get opCode from jsonParams!"); return NULL; } - int operationCode = MEMBER_INVITE; - if (GetIntFromJson(jsonParams, FIELD_GROUP_OP, &operationCode) != HC_SUCCESS) { - LOGE("Failed to get operationCode from jsonParams!"); - return NULL; - } - LOGI("Start to create server bind session! [RequestId]: %" PRId64 ", [OperationCode]: %d", - requestId, operationCode); - BindSession *session = (BindSession *)HcMalloc(sizeof(BindSession), 0); + BindSession *session = CreateBaseBindSession(TYPE_SERVER_BIND_SESSION, opCode, + jsonParams, callback); if (session == NULL) { - LOGE("Failed to allocate session memory!"); return NULL; } - InitBindSession(TYPE_SERVER_BIND_SESSION, operationCode, requestId, callback, session); InitServerChannel(jsonParams, session); /* The server may receive the confirm request message. */ session->onConfirmationReceived = OnBindConfirmationReceived; @@ -499,7 +491,5 @@ Session *CreateServerBindSession(CJson *jsonParams, const DeviceAuthCallback *ca DestroyBindSession((Session *)session); return NULL; } - LOGI("Create server bind session successfully! [RequestId]: %" PRId64 ", [OperationCode]: %d", - requestId, operationCode); return (Session *)session; } \ No newline at end of file -- Gitee