From 81a7106e7dbb4341728fdd998cb2fc02e27ee6ca Mon Sep 17 00:00:00 2001 From: ailu1 Date: Sat, 14 Aug 2021 17:25:29 +0800 Subject: [PATCH 1/7] enchance id check && fix version control Signed-off-by: zikunfu --- hals/inc/common/common_util.h | 1 + hals/inc/common/json_utils.h | 1 + interfaces/innerkits/device_auth_defines.h | 6 +- .../src/group_manager/group_common.c | 2 +- services/module/inc/das_module/das_common.h | 5 +- services/module/inc/dev_auth_module_manager.h | 2 +- services/module/src/das_module/das_common.c | 74 ++++++++++++------- .../module/src/das_module/das_version_util.c | 5 +- .../iso_client_protocol_task.c | 4 +- .../iso_server_protocol_task.c | 6 +- .../src/das_module/iso_task/iso_task_common.c | 1 - .../pake_client_protocol_task.c | 2 +- .../pake_server_protocol_task.c | 2 +- .../common_standard_bind_exchange.c | 16 +--- .../common_standard_unbind_exchange.c | 12 ++- services/module/src/das_module/task_main.c | 13 ++-- services/module/src/dev_auth_module_manager.c | 2 +- .../iso_protocol/iso_protocol_common.c | 10 ++- 18 files changed, 92 insertions(+), 72 deletions(-) diff --git a/hals/inc/common/common_util.h b/hals/inc/common/common_util.h index 0cc0415..a5f071e 100644 --- a/hals/inc/common/common_util.h +++ b/hals/inc/common/common_util.h @@ -17,6 +17,7 @@ #define COMMON_UTIL_H #include +#include "hc_error.h" #define BYTE_TO_HEX_OPER_LENGTH 2 #define DEC 10 diff --git a/hals/inc/common/json_utils.h b/hals/inc/common/json_utils.h index 05c4158..b58aa79 100644 --- a/hals/inc/common/json_utils.h +++ b/hals/inc/common/json_utils.h @@ -21,6 +21,7 @@ #include #include #include "cJSON.h" +#include "hc_error.h" typedef cJSON CJson; diff --git a/interfaces/innerkits/device_auth_defines.h b/interfaces/innerkits/device_auth_defines.h index 1eb615e..ae26f12 100644 --- a/interfaces/innerkits/device_auth_defines.h +++ b/interfaces/innerkits/device_auth_defines.h @@ -29,7 +29,7 @@ enum { HC_ERR_NULL_PTR = 0x00000004, HC_ERR_ALLOC_MEMORY = 0x00000005, HC_ERR_MEMORY_COPY = 0x00000006, - HC_HEX_CONVERT_FAILED = 0x00000007, + HC_ERR_CONVERT_FAILED = 0x00000007, HC_ERR_NOT_SUPPORT = 0x00000008, HC_ERR_TIME_OUT = 0x00000009, HC_ERR_CASE = 0x0000000A, @@ -111,10 +111,10 @@ enum { HC_ERR_LOST_DATA = 0x0000600A, /* error code for broadcast , 0x00007000 ~ 0x00007FFF */ - HC_ERR_LISTENER_NOT_EXIST = 0x000070012, + HC_ERR_LISTENER_NOT_EXIST = 0x00007001, /* error code for channel , 0x00008000 ~ 0x00008FFF */ - HC_ERR_CHANNEL_NOT_EXIST = 0x000080012, + HC_ERR_CHANNEL_NOT_EXIST = 0x00008001, HC_ERR_SOFT_BUS = 0x00008002, /* error code used on DAS service */ diff --git a/services/group_manager/src/group_manager/group_common.c b/services/group_manager/src/group_manager/group_common.c index 340dc39..2f6325a 100644 --- a/services/group_manager/src/group_manager/group_common.c +++ b/services/group_manager/src/group_manager/group_common.c @@ -96,7 +96,7 @@ int32_t GetHashResult(const uint8_t *info, uint32_t infoLen, char *hash, uint32_ if (result == HC_SUCCESS) { if (ByteToHexString(infoHash.val, infoHash.length, hash, hashLen) != HC_SUCCESS) { LOGE("Failed to convert bytes to string!"); - result = HC_HEX_CONVERT_FAILED; + result = HC_ERR_CONVERT_FAILED; } } HcFree(infoHash.val); diff --git a/services/module/inc/das_module/das_common.h b/services/module/inc/das_module/das_common.h index bdb3379..032064b 100644 --- a/services/module/inc/das_module/das_common.h +++ b/services/module/inc/das_module/das_common.h @@ -23,8 +23,9 @@ int32_t GenerateKeyAlias(const Uint8Buff *pkgName, const Uint8Buff *serviceType, const KeyAliasType keyType, const Uint8Buff *authId, Uint8Buff *outKeyAlias); -int32_t GetAndCheckAuthIdPeer(const CJson *in, const Uint8Buff *authIdPeer); -int32_t GetAuthIdPeer(const CJson *in, Uint8Buff *authIdPeer); +int32_t GetIdPeerForParams(const CJson *in, const char *peerIdKey, const Uint8Buff *authIdSelf, Uint8Buff *authIdPeer); +int32_t GetAndCheckAuthIdPeer(const CJson *in, const Uint8Buff *authIdSelf, const Uint8Buff *authIdPeer); +int32_t GetAuthIdPeerFromPayload(const CJson *in, const Uint8Buff *authIdSelf, Uint8Buff *authIdPeer); int32_t GetAndCheckKeyLenOnServer(const CJson *in, uint32_t *returnKeyLen); void SendErrMsgToSelf(const CJson *in, CJson *out, int errCode); diff --git a/services/module/inc/dev_auth_module_manager.h b/services/module/inc/dev_auth_module_manager.h index 700d2ab..a566ac2 100644 --- a/services/module/inc/dev_auth_module_manager.h +++ b/services/module/inc/dev_auth_module_manager.h @@ -29,9 +29,9 @@ void DestroyModules(); int32_t CreateTask(int *taskId, const CJson *in, CJson *out, int moduleType); int32_t ProcessTask(int taskId, const CJson *in, CJson *out, int *status, int moduleType); void DestroyTask(int taskId, int moduleType); +int32_t CheckMsgRepeatability(const CJson *in, int moduleType); // for DAS -int32_t CheckMsgRepeatability(const CJson *in, int moduleType); int32_t RegisterLocalIdentity(const char *pkgName, const char *serviceType, Uint8Buff *authId, int userType, int moduleType); int32_t UnregisterLocalIdentity(const char *pkgName, const char *serviceType, Uint8Buff *authId, int userType, diff --git a/services/module/src/das_module/das_common.c b/services/module/src/das_module/das_common.c index 99c3984..c89f71c 100644 --- a/services/module/src/das_module/das_common.c +++ b/services/module/src/das_module/das_common.c @@ -332,7 +332,36 @@ err: return res; } -int32_t GetAndCheckAuthIdPeer(const CJson *in, const Uint8Buff *authIdPeer) +int32_t GetIdPeerForParams(const CJson *in, const char *peerIdKey, const Uint8Buff *authIdSelf, Uint8Buff *authIdPeer) +{ + const char *authIdStr = GetStringFromJson(in, peerIdKey); + if (authIdStr == NULL) { + LOGE("Get peer id from json failed."); + return HC_ERR_JSON_GET; + } + uint32_t authIdLen = HcStrlen(authIdStr) / BYTE_TO_HEX_OPER_LENGTH; + if (authIdLen == 0 || authIdLen > MAX_AUTH_ID_LEN) { + LOGE("Invalid authIdPeerLen."); + return HC_ERR_INVALID_LEN; + } + int32_t res = InitSingleParam(authIdPeer, authIdLen); + if (res != HC_SUCCESS) { + LOGE("InitSingleParam for peer authId failed, res: %d.", res); + return res; + } + if (HexStringToByte(authIdStr, authIdPeer->val, authIdPeer->length) != HC_SUCCESS) { + LOGE("HexStringToByte for authIdPeer failed."); + return HC_ERR_CONVERT_FAILED; + } + if ((authIdSelf->length == authIdPeer->length) && + memcmp(authIdSelf->val, authIdPeer->val, authIdSelf->length) == 0) { + LOGE("Peer id can not be equal to self id."); + return HC_ERR_INVALID_PARAMS; + } + return HC_SUCCESS; +} + +int32_t GetAndCheckAuthIdPeer(const CJson *in, const Uint8Buff *authIdSelf, const Uint8Buff *authIdPeer) { const CJson *payload = GetObjFromJson(in, FIELD_PAYLOAD); if (payload == NULL) { @@ -354,48 +383,37 @@ int32_t GetAndCheckAuthIdPeer(const CJson *in, const Uint8Buff *authIdPeer) LOGE("Malloc for authIdPeerTmp failed."); return HC_ERR_ALLOC_MEMORY; } - int32_t res = HexStringToByte(authIdStr, authIdPeerTmp, authIdPeerLen); - if (res != HC_SUCCESS) { - LOGE("Convert peer authId from hex string to byte failed, res: %d.", res); - goto err; + if (HexStringToByte(authIdStr, authIdPeerTmp, authIdPeerLen) != HAL_SUCCESS) { + LOGE("Convert peer authId from hex string to byte failed, res: %d.", HC_ERR_CONVERT_FAILED); + HcFree(authIdPeerTmp); + return HC_ERR_CONVERT_FAILED; + } + if ((authIdSelf->length == authIdPeer->length) && + memcmp(authIdSelf->val, authIdPeer->val, authIdSelf->length) == 0) { + LOGE("Peer id can not be equal to self id."); + HcFree(authIdPeerTmp); + return HC_ERR_INVALID_PARAMS; } if (memcmp(authIdPeer->val, authIdPeerTmp, authIdPeer->length) != 0) { LOGE("Peer authId does not match."); - res = HC_ERR_INVALID_PARAMS; - goto err; + HcFree(authIdPeerTmp); + return HC_ERR_INVALID_PARAMS; } -err: HcFree(authIdPeerTmp); - return res; + return HC_SUCCESS; } -int32_t GetAuthIdPeer(const CJson *in, Uint8Buff *authIdPeer) +int32_t GetAuthIdPeerFromPayload(const CJson *in, const Uint8Buff *authIdSelf, Uint8Buff *authIdPeer) { const CJson *payload = GetObjFromJson(in, FIELD_PAYLOAD); if (payload == NULL) { LOGE("Not have payload."); return HC_ERR_INVALID_PARAMS; } - const char *authIdStr = GetStringFromJson(payload, FIELD_PEER_AUTH_ID); - if (authIdStr == NULL) { - LOGE("AuthIdPeer in payload is null."); - return HC_ERR_INVALID_PARAMS; - } - uint32_t authIdLen = HcStrlen(authIdStr) / BYTE_TO_HEX_OPER_LENGTH; - if (authIdLen == 0 || authIdLen > MAX_AUTH_ID_LEN) { - LOGE("Invalid authIdPeerLen"); - return HC_ERR_INVALID_PARAMS; - } - int32_t res = InitSingleParam(authIdPeer, authIdLen); + int res = GetIdPeerForParams(payload, FIELD_PEER_AUTH_ID, authIdSelf, authIdPeer); if (res != HC_SUCCESS) { - LOGE("InitSingleParam for peer authId failed, res: %d.", res); - return res; + LOGE("GetIdPeerForParams failed, res: %d.", res); } - res = HexStringToByte(authIdStr, authIdPeer->val, authIdPeer->length); - if (res != HC_SUCCESS) { - LOGE("HexStringToByte for authIdPeer failed."); - } - return res; } diff --git a/services/module/src/das_module/das_version_util.c b/services/module/src/das_module/das_version_util.c index a641903..9e84c69 100644 --- a/services/module/src/das_module/das_version_util.c +++ b/services/module/src/das_module/das_version_util.c @@ -38,9 +38,7 @@ PriorityMap g_bindPriorityList[BIND_PRIORITY_LEN] = { }; PriorityMap g_authPriorityList[AUTH_PRIORITY_LEN] = { { PSK_SPEKE | NEW_EC_SPEKE, NEW_PAKE }, - { PSK_SPEKE | NEW_DL_SPEKE, NEW_PAKE }, { PSK_SPEKE | EC_SPEKE, PAKE }, - { PSK_SPEKE | DL_SPEKE, PAKE }, { ISO_ALG, ISO } }; @@ -143,7 +141,8 @@ static ProtocolType GetBindPrototolType(VersionStruct *curVersion) static ProtocolType GetAuthPrototolType(VersionStruct *curVersion) { if (IsVersionEqual(curVersion, &g_defaultVersion)) { - return STS; + LOGE("Not support STS."); + return UNSUPPORTED; } else { for (int i = 0; i < AUTH_PRIORITY_LEN; i++) { if ((curVersion->third & g_authPriorityList[i].alg) == g_authPriorityList[i].alg) { diff --git a/services/module/src/das_module/iso_task/iso_protocol_task/iso_client_protocol_task.c b/services/module/src/das_module/iso_task/iso_protocol_task/iso_client_protocol_task.c index 0602992..11207df 100644 --- a/services/module/src/das_module/iso_task/iso_protocol_task/iso_client_protocol_task.c +++ b/services/module/src/das_module/iso_task/iso_protocol_task/iso_client_protocol_task.c @@ -137,9 +137,9 @@ err: static int ParseServerStartMessage(IsoParams *params, const CJson *in, uint8_t *peerToken) { if (params->opCode == OP_BIND) { - RETURN_IF_ERR(GetAuthIdPeer(in, &(params->baseParams.authIdPeer))); + RETURN_IF_ERR(GetAuthIdPeerFromPayload(in, &(params->baseParams.authIdSelf), &(params->baseParams.authIdPeer))); } else { - RETURN_IF_ERR(GetAndCheckAuthIdPeer(in, &(params->baseParams.authIdPeer))); + RETURN_IF_ERR(GetAndCheckAuthIdPeer(in, &(params->baseParams.authIdSelf), &(params->baseParams.authIdPeer))); } RETURN_IF_ERR(GetByteFromJson(in, FIELD_ISO_SALT, params->baseParams.randPeer.val, params->baseParams.randPeer.length)); diff --git a/services/module/src/das_module/iso_task/iso_protocol_task/iso_server_protocol_task.c b/services/module/src/das_module/iso_task/iso_protocol_task/iso_server_protocol_task.c index 0a17cfa..ec95366 100644 --- a/services/module/src/das_module/iso_task/iso_protocol_task/iso_server_protocol_task.c +++ b/services/module/src/das_module/iso_task/iso_protocol_task/iso_server_protocol_task.c @@ -88,9 +88,11 @@ static int IsoServerStart(SymBaseCurTask *task, IsoParams *params, const CJson * } Uint8Buff selfTokenBuf = { selfToken, ISO_TOKEN_LEN }; if (params->opCode == OP_BIND) { - GOTO_ERR_AND_SET_RET(GetAuthIdPeer(in, &(params->baseParams.authIdPeer)), res); + GOTO_ERR_AND_SET_RET(GetAuthIdPeerFromPayload(in, &(params->baseParams.authIdSelf), + &(params->baseParams.authIdPeer)), res); } else { - GOTO_ERR_AND_SET_RET(GetAndCheckAuthIdPeer(in, &(params->baseParams.authIdPeer)), res); + GOTO_ERR_AND_SET_RET(GetAndCheckAuthIdPeer(in, &(params->baseParams.authIdSelf), + &(params->baseParams.authIdPeer)), res); GOTO_ERR_AND_SET_RET(GetAndCheckKeyLenOnServer(in, &(params->keyLen)), res); } GOTO_ERR_AND_SET_RET(GetByteFromJson(in, FIELD_ISO_SALT, params->baseParams.randPeer.val, diff --git a/services/module/src/das_module/iso_task/iso_task_common.c b/services/module/src/das_module/iso_task/iso_task_common.c index 694ba51..5241221 100644 --- a/services/module/src/das_module/iso_task/iso_task_common.c +++ b/services/module/src/das_module/iso_task/iso_task_common.c @@ -184,7 +184,6 @@ int GenEncResult(const IsoParams *params, int message, CJson *out, const char *a GOTO_ERR_AND_SET_RET(AddIntToJson(sendToSelf, FIELD_OPERATION_CODE, params->opCode), res); GOTO_ERR_AND_SET_RET(AddObjToJson(out, FIELD_SEND_TO_PEER, sendToPeer), res); GOTO_ERR_AND_SET_RET(AddObjToJson(out, FIELD_SEND_TO_SELF, sendToSelf), res); - GOTO_ERR_AND_SET_RET(AddIntToJson(out, FIELD_CREDENTIAL_TYPE, SYMMETRIC), res); err: FreeJson(sendToPeer); FreeJson(sendToSelf); diff --git a/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_client_protocol_task.c b/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_client_protocol_task.c index 0797d40..fbd27fb 100644 --- a/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_client_protocol_task.c +++ b/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_client_protocol_task.c @@ -105,7 +105,7 @@ static int ParseMsgForClientConfirm(PakeParams *params, const CJson *in) return res; } if (params->opCode == AUTHENTICATE || params->opCode == OP_UNBIND) { - res = GetAndCheckAuthIdPeer(in, &(params->baseParams.idPeer)); + res = GetAndCheckAuthIdPeer(in, &(params->baseParams.idSelf), &(params->baseParams.idPeer)); if (res != HC_SUCCESS) { LOGE("GetAndCheckAuthIdPeer failed, res: %d.", res); return res; diff --git a/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_server_protocol_task.c b/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_server_protocol_task.c index f7f65ab..6f3c036 100644 --- a/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_server_protocol_task.c +++ b/services/module/src/das_module/pake_task/pake_task/pake_protocol_task/pake_server_protocol_task.c @@ -83,7 +83,7 @@ static int PakeResponse(AsyBaseCurTask *task, PakeParams *params, const CJson *i } // parse differentiated data if (params->opCode == AUTHENTICATE || params->opCode == OP_UNBIND) { - res = GetAndCheckAuthIdPeer(in, &(params->baseParams.idPeer)); + res = GetAndCheckAuthIdPeer(in, &(params->baseParams.idSelf), &(params->baseParams.idPeer)); if (res != HC_SUCCESS) { LOGE("GetAndCheckAuthIdPeer failed, res: %d.", res); return res; diff --git a/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_bind_exchange.c b/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_bind_exchange.c index e97476f..05a2b12 100644 --- a/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_bind_exchange.c +++ b/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_bind_exchange.c @@ -304,20 +304,10 @@ static int32_t ParseAuthInfo(PakeParams *pakeParams, const StandardBindExchangeP GOTO_ERR_AND_SET_RET(GetByteFromJson(authInfoJson, FIELD_AUTH_PK, exchangeParams->pubKeyPeer.val, exchangeParams->pubKeyPeer.length), res); - const char *authId = GetStringFromJson(authInfoJson, FIELD_AUTH_ID); - if (authId == NULL) { - LOGE("get authId failed"); - res = HC_ERROR; - goto err; - } - res = InitSingleParam(&pakeParams->baseParams.idPeer, strlen(authId) / BYTE_TO_HEX_OPER_LENGTH); + res = GetIdPeerForParams(authInfoJson, FIELD_AUTH_ID, + &pakeParams->baseParams.idSelf, &pakeParams->baseParams.idPeer); if (res != HC_SUCCESS) { - LOGE("InitSingleParam for idPeer failed."); - goto err; - } - res = HexStringToByte(authId, pakeParams->baseParams.idPeer.val, pakeParams->baseParams.idPeer.length); - if (res != HC_SUCCESS) { - LOGE("Convert idPeer from hex string to byte failed."); + LOGE("GetIdPeerForParams failed, res: %d.", res); goto err; } err: diff --git a/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_unbind_exchange.c b/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_unbind_exchange.c index 90c71c2..a2b5623 100644 --- a/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_unbind_exchange.c +++ b/services/module/src/das_module/pake_task/standard_exchange_task/common_standard_unbind_exchange.c @@ -163,10 +163,16 @@ static int32_t ParseRmvInfo(PakeParams *pakeParams, StandardUnbindExchangeParams { int32_t res; CJson *rmvInfoJson = CreateJsonFromString((char *)exchangeParams->rmvInfo.val); - + if (rmvInfoJson == NULL) { + LOGE("Create rmvInfoJson failed."); + return HC_ERR_JSON_CREATE; + } GOTO_ERR_AND_SET_RET(GetIntFromJson(rmvInfoJson, FIELD_RMV_TYPE, &(pakeParams->userTypePeer)), res); - GOTO_ERR_AND_SET_RET(GetByteFromJson(rmvInfoJson, FIELD_RMV_ID, pakeParams->baseParams.idPeer.val, - pakeParams->baseParams.idPeer.length), res); + res = GetIdPeerForParams(rmvInfoJson, FIELD_RMV_ID, &pakeParams->baseParams.idSelf, &pakeParams->baseParams.idPeer); + if (res != HC_SUCCESS) { + LOGE("GetIdPeerForParams failed, res: %d.", res); + goto err; + } err: FreeJson(rmvInfoJson); return res; diff --git a/services/module/src/das_module/task_main.c b/services/module/src/das_module/task_main.c index 329dd94..196148f 100644 --- a/services/module/src/das_module/task_main.c +++ b/services/module/src/das_module/task_main.c @@ -210,7 +210,7 @@ static int NegotiateAndProcessTask(Task *task, const CJson *in, CJson *out, int return HC_ERR_UNSUPPORTED_VERSION; } ProtocolType protocolType = GetPrototolType(&(task->versionInfo.curVersion), task->versionInfo.opCode); - LOGI("client create protocolType:%d", protocolType); + LOGI("Client select protocolType:%d", protocolType); SubTaskBase *subTask = NULL; uint32_t index = 0; @@ -227,8 +227,8 @@ static int NegotiateAndProcessTask(Task *task, const CJson *in, CJson *out, int ptr = task->vec.getp(&(task->vec), index); } if (subTask == NULL) { - LOGE("Not find subTask."); - return HC_ERROR; + LOGE("Can't find subTask."); + return HC_ERR_NOT_SUPPORT; } subTask->curVersion = task->versionInfo.curVersion; res = subTask->process(subTask, in, out, status); @@ -341,7 +341,7 @@ static int CreateSingleSubTask(Task *task, const CJson *in, CJson *out) task->versionInfo.versionStatus = VERSION_DECIDED; ProtocolType protocolType = GetPrototolType(&(task->versionInfo.curVersion), task->versionInfo.opCode); - LOGI("server create protocolType:%d", protocolType); + LOGI("Server create protocolType:%d", protocolType); uint32_t index; void **ptr = NULL; @@ -355,11 +355,12 @@ static int CreateSingleSubTask(Task *task, const CJson *in, CJson *out) } subTask->curVersion = task->versionInfo.curVersion; task->vec.pushBackT(&(task->vec), (void *)subTask); - break; + return HC_SUCCESS; } } - return HC_SUCCESS; + LOGE("Can't find protocolType."); + return HC_ERR_NOT_SUPPORT; } Task *CreateTaskT(int *taskId, const CJson *in, CJson *out) diff --git a/services/module/src/dev_auth_module_manager.c b/services/module/src/dev_auth_module_manager.c index a8e1443..f5f8d85 100644 --- a/services/module/src/dev_auth_module_manager.c +++ b/services/module/src/dev_auth_module_manager.c @@ -246,7 +246,7 @@ int32_t ProcessTask(int taskId, const CJson *in, CJson *out, int *status, int mo if (res != HC_SUCCESS) { LOGE("AddSingleVersionToJson failed, res:%d", res); } else { - LOGI("Create task success."); + LOGI("Process task success."); } return res; } diff --git a/services/module/src/protocol/iso_protocol/iso_protocol_common.c b/services/module/src/protocol/iso_protocol/iso_protocol_common.c index c0adfd2..87c6bd7 100644 --- a/services/module/src/protocol/iso_protocol/iso_protocol_common.c +++ b/services/module/src/protocol/iso_protocol/iso_protocol_common.c @@ -129,6 +129,7 @@ int IsoClientCheckAndGenToken(const IsoBaseParams *params, const Uint8Buff *peer return res; } if (memcmp(peerToken->val, outHmac.val, outHmac.length) != 0) { + LOGE("Compare hmac token failed."); return HC_ERR_PROOF_NOT_MATCH; } return IsoCalPeerToken(params, selfToken); @@ -149,6 +150,7 @@ int IsoClientGenSessionKey(IsoBaseParams *params, int returnResult, const uint8_ return res; } if (memcmp(outHmacBuf.val, hmac, hmacLen) != 0) { + LOGE("Compare hmac result failed."); return HC_ERR_PROOF_NOT_MATCH; } @@ -178,6 +180,7 @@ int IsoClientGenSessionKey(IsoBaseParams *params, int returnResult, const uint8_ res = params->loader->computeHkdf(&pskBuf, &hkdfSaltBuf, &keyInfoBuf, ¶ms->sessionKey, false); if (res != 0) { LOGE("compute hkdf failed, res:%d", res); + FreeAndCleanKey(¶ms->sessionKey); } HcFree(hkdfSalt); return res; @@ -201,6 +204,7 @@ int IsoServerGenSessionKeyAndCalToken(IsoBaseParams *params, const Uint8Buff *to return res; } if (memcmp(tokenFromPeer->val, outHmac.val, outHmac.length) != 0) { + LOGE("Compare hmac token failed."); return HC_ERR_PROOF_NOT_MATCH; } @@ -231,8 +235,7 @@ int IsoServerGenSessionKeyAndCalToken(IsoBaseParams *params, const Uint8Buff *to res = params->loader->computeHkdf(&pskBuf, &hkdfSaltBuf, &keyInfoBuf, &(params->sessionKey), false); HcFree(hkdfSalt); if (res != 0) { - HcFree(params->sessionKey.val); - params->sessionKey.val = NULL; + FreeAndCleanKey(¶ms->sessionKey); return res; } @@ -240,8 +243,7 @@ int IsoServerGenSessionKeyAndCalToken(IsoBaseParams *params, const Uint8Buff *to Uint8Buff messageBuf = { (uint8_t *)&returnCode, sizeof(int) }; res = params->loader->computeHmac(&pskBuf, &messageBuf, tokenToPeer, false); if (res != 0) { - HcFree(params->sessionKey.val); - params->sessionKey.val = NULL; + FreeAndCleanKey(¶ms->sessionKey); } return res; } \ No newline at end of file From a95cc951a92c76eb7ebbe3352c17c228ffc7f053 Mon Sep 17 00:00:00 2001 From: fuzikun Date: Wed, 18 Aug 2021 20:15:07 +0800 Subject: [PATCH 2/7] add softbus channel Signed-off-by: fuzikun --- frameworks/src/ipc_sdk.c | 4 +- interfaces/innerkits/device_auth.h | 2 +- interfaces/innerkits/device_auth_defines.h | 1 + services/BUILD.gn | 18 +- .../inc/channel_manager/channel_manager.h | 17 +- .../soft_bus_channel/soft_bus_channel.h | 22 +- services/common/inc/common_defs.h | 1 + .../common/inc/task_manager/task_manager.h | 32 ++ .../src/channel_manager/channel_manager.c | 197 +++------ .../soft_bus_channel/soft_bus_channel.c | 390 +++++++++++------- .../common/src/task_manager/task_manager.c | 71 ++++ services/device_auth.c | 350 ++++------------ services/deviceauth.gni | 4 +- .../inc/group_auth_common_defines.h | 1 - 14 files changed, 525 insertions(+), 585 deletions(-) create mode 100644 services/common/inc/task_manager/task_manager.h create mode 100644 services/common/src/task_manager/task_manager.c diff --git a/frameworks/src/ipc_sdk.c b/frameworks/src/ipc_sdk.c index f40a3b2..2c83afb 100644 --- a/frameworks/src/ipc_sdk.c +++ b/frameworks/src/ipc_sdk.c @@ -823,10 +823,10 @@ static int32_t IpcGmGetRegisterInfo(char **registerInfo) return (*registerInfo != NULL) ? HC_SUCCESS : HC_ERR_NULL_PTR; } -static int32_t IpcGmGetLocalConnectInfo(char **outLocalConnInfo) +static int32_t IpcGmGetLocalConnectInfo(char *returnInfo, int32_t bufLen) { LOGI("starting ..."); - if (outLocalConnInfo == NULL) { + if (returnInfo == NULL) { return HC_ERR_INVALID_PARAMS; } diff --git a/interfaces/innerkits/device_auth.h b/interfaces/innerkits/device_auth.h index 0717824..9b718a5 100644 --- a/interfaces/innerkits/device_auth.h +++ b/interfaces/innerkits/device_auth.h @@ -136,7 +136,7 @@ typedef struct { int32_t (*processKeyAgreeData)(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen); int32_t (*processCredential)(int operationCode, const char *reqJsonStr, char **returnJsonStr); int32_t (*getRegisterInfo)(char **returnRegisterInfo); - int32_t (*getLocalConnectInfo)(char **returnInfo); + int32_t (*getLocalConnectInfo)(char *returnInfo, int32_t bufLen); int32_t (*addGroupManager)(const char *appId, const char *groupId, const char *managerAppId); int32_t (*addGroupFriend)(const char *appId, const char *groupId, const char *friendAppId); int32_t (*deleteGroupManager)(const char *appId, const char *groupId, const char *managerAppId); diff --git a/interfaces/innerkits/device_auth_defines.h b/interfaces/innerkits/device_auth_defines.h index ae26f12..e704f74 100644 --- a/interfaces/innerkits/device_auth_defines.h +++ b/interfaces/innerkits/device_auth_defines.h @@ -42,6 +42,7 @@ enum { HC_ERR_INVALID_PUBLIC_KEY = 0x00001003, HC_ERR_VERIFY_FAILED = 0x00001004, HC_ERR_HASH_FAIL = 0x00001005, + HC_ERR_INIT_FAILED = 0x00001006, /* error code for json util , 0x00002000 ~ 0x00002FFF */ HC_ERR_JSON_FAIL = 0x00002001, diff --git a/services/BUILD.gn b/services/BUILD.gn index 8374189..a2b3977 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -27,7 +27,9 @@ if (defined(ohos_lite)) { "//third_party/cJSON", "//utils/native/lite/include", "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", - "//third_party/bounds_checking_function/include" + "//third_party/bounds_checking_function/include", + "//foundation/communication/dsoftbus/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", ] sources = deviceauth_files @@ -39,6 +41,7 @@ if (defined(ohos_lite)) { "//build/lite/config/component/cJSON:cjson_shared", "//utils/native/lite:utils", "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//foundation/communication/dsoftbus/sdk:softbus_client", "//third_party/bounds_checking_function:libsec_shared", ] } @@ -116,30 +119,33 @@ if (defined(ohos_lite)) { ohos_static_library("deviceauth") { subsystem_name = "security" part_name = "deviceauth_standard" - + include_dirs = inc_path include_dirs += hals_inc_path include_dirs += [ "//third_party/cJSON", "//utils/native/base/include", - "${frameworks_path}/inc/standard" + "${frameworks_path}/inc/standard", ] sources = deviceauth_files - + cflags = [ "-DHILOG_ENABLE" ] cflags += build_flags if (target_cpu == "arm") { cflags += [ "-DBINDER_IPC_32BIT" ] } - + deps = [ "${hals_path}:${hal_module_name}", "//third_party/cJSON:cjson_static", "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "dsoftbus_standard:softbus_client", + ] } ohos_prebuilt_etc("auth_service.rc") { diff --git a/services/common/inc/channel_manager/channel_manager.h b/services/common/inc/channel_manager/channel_manager.h index ba764f4..fd3eabf 100644 --- a/services/common/inc/channel_manager/channel_manager.h +++ b/services/common/inc/channel_manager/channel_manager.h @@ -20,11 +20,12 @@ #include "common_defs.h" #include "json_utils.h" -int32_t InitChannelManager(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()); -void DestroyChannelManager(); +#ifdef __cplusplus +extern "C" { +#endif + +int32_t InitChannelManager(void); +void DestroyChannelManager(void); /* Channel operation interfaces */ ChannelType GetChannelType(const DeviceAuthCallback *callback); @@ -34,5 +35,9 @@ void CloseChannel(ChannelType channelType, int64_t channelId); int32_t SendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, const DeviceAuthCallback *callback, const char *data); void SetAuthResult(ChannelType channelType, int64_t channelId); -int32_t GetLocalConnectInfo(char **returnLocalConnectInfo); +int32_t GetLocalConnectInfo(char *jsonAddrInfo, int32_t bufLen); + +#ifdef __cplusplus +} +#endif #endif diff --git a/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h b/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h index 0cbeaa1..b529fea 100644 --- a/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h +++ b/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h @@ -19,20 +19,24 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { int32_t (*openChannel)(const char *connectParams, int64_t requestId, int64_t *returnChannelId); void (*closeChannel)(int64_t channelId); int32_t (*sendMsg)(int64_t channelId, const uint8_t *data, uint32_t dataLen); - void (*setAuthResult)(int64_t channelId); - char *(*getLocalAuthInfo)(); + void (*notifyResult)(int64_t channelId); + int32_t (*getLocalConnectInfo)(char *jsonAddrInfo, int32_t bufLen); } SoftBus; -int32_t InitSoftBusChannelModule(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()); -void DestroySoftBusChannelModule(); -SoftBus *GetSoftBusInstance(); -bool IsSoftBusChannelSupported(); +int32_t InitSoftBusChannelModule(void); +void DestroySoftBusChannelModule(void); +SoftBus *GetSoftBusInstance(void); +bool IsSoftBusChannelSupported(void); +#ifdef __cplusplus +} +#endif #endif diff --git a/services/common/inc/common_defs.h b/services/common/inc/common_defs.h index afea1c5..9330519 100644 --- a/services/common/inc/common_defs.h +++ b/services/common/inc/common_defs.h @@ -129,6 +129,7 @@ #define DEFAULT_RETURN_KEY_LENGTH 32 #define ERR_AUTH_FORM 0 #define MAX_BUFFER_LEN 1024 +#define MAX_DATA_BUFFER_SIZE 4096 #define MAX_AUTH_ID_LEN 256 typedef enum { diff --git a/services/common/inc/task_manager/task_manager.h b/services/common/inc/task_manager/task_manager.h new file mode 100644 index 0000000..676b229 --- /dev/null +++ b/services/common/inc/task_manager/task_manager.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TASK_MANAGER_H +#define TASK_MANAGER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "hc_task_thread.h" + +int32_t InitTaskManager(void); +void DestroyTaskManager(void); +int32_t PushTask(HcTaskBase *baseTask); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/services/common/src/channel_manager/channel_manager.c b/services/common/src/channel_manager/channel_manager.c index 258ade0..724dd50 100644 --- a/services/common/src/channel_manager/channel_manager.c +++ b/services/common/src/channel_manager/channel_manager.c @@ -14,28 +14,19 @@ */ #include "channel_manager.h" + #include "callback_manager.h" #include "device_auth_defines.h" #include "hc_log.h" #include "hc_types.h" #include "soft_bus_channel.h" -int32_t InitChannelManager(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()) +int32_t InitChannelManager(void) { - if (IsSoftBusChannelSupported()) { - int32_t res = InitSoftBusChannelModule(onChannelOpened, onChannelClosed, onMsgReceived, onServiceDied); - if (res != HC_SUCCESS) { - DestroyChannelManager(); - } - return res; - } - return HC_SUCCESS; + return IsSoftBusChannelSupported() ? InitSoftBusChannelModule() : HC_SUCCESS; } -void DestroyChannelManager() +void DestroyChannelManager(void) { if (IsSoftBusChannelSupported()) { DestroySoftBusChannelModule(); @@ -47,166 +38,94 @@ ChannelType GetChannelType(const DeviceAuthCallback *callback) if ((callback != NULL) && (callback->onTransmit != NULL)) { return SERVICE_CHANNEL; } - if (IsSoftBusChannelSupported()) { - return SOFT_BUS; - } - return NO_CHANNEL; + return IsSoftBusChannelSupported() ? SOFT_BUS : NO_CHANNEL; } bool CanFindValidChannel(ChannelType channelType, const CJson *jsonParams, const DeviceAuthCallback *callback) { - switch (channelType) { - case NO_CHANNEL: + if (channelType == SERVICE_CHANNEL) { + if ((callback == NULL) || (callback->onTransmit != NULL)) { + LOGE("The service channel is unavailable!"); return false; - case SERVICE_CHANNEL: - return ((callback != NULL) && (callback->onTransmit != NULL)) ? true : false; - case SOFT_BUS: - if (GetSoftBusInstance() == NULL) { - LOGE("The soft bus module is unavailable!"); - return false; - } - const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); - if (connectParams == NULL) { - LOGE("Failed to get connectParams from jsonParams!"); - return false; - } - return true; - default: - LOGE("Enter the exception case!"); + } + return true; + } else if (channelType == SOFT_BUS) { + const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); + if (connectParams == NULL) { + LOGE("Failed to get connectParams from jsonParams!"); return false; + } + return true; + } else { + LOGE("No channel is available!"); + return false; } } int32_t OpenChannel(ChannelType channelType, const CJson *jsonParams, int64_t requestId, int64_t *returnChannelId) { - int64_t channelId = DEFAULT_CHANNEL_ID; - const char *connectParams = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGE("No channel!"); + if (channelType == SERVICE_CHANNEL) { + *returnChannelId = DEFAULT_CHANNEL_ID; + return HC_SUCCESS; + } else if (channelType == SOFT_BUS) { + const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); + if (connectParams == NULL) { + LOGE("Failed to get connectParams from jsonParams!"); + return HC_ERR_JSON_GET; + } + int64_t channelId = DEFAULT_CHANNEL_ID; + int32_t result = GetSoftBusInstance()->openChannel(connectParams, requestId, &channelId); + if (result != HC_SUCCESS) { return HC_ERR_CHANNEL_NOT_EXIST; - case SERVICE_CHANNEL: - LOGI("Use service channel, so we do not need to open it!"); - *returnChannelId = channelId; - return HC_SUCCESS; - case SOFT_BUS: - connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); - if (connectParams == NULL) { - LOGE("Failed to get connectParams from jsonParams!"); - return HC_ERR_JSON_GET; - } - SoftBus *softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGE("The soft bus module is unavailable!"); - return HC_ERR_SOFT_BUS; - } - int32_t result = softBusInstance->openChannel(connectParams, requestId, &channelId); - if (result != HC_SUCCESS) { - LOGE("Failed to open soft bus channel!"); - return HC_ERR_CHANNEL_NOT_EXIST; - } - *returnChannelId = channelId; - return HC_SUCCESS; - default: - LOGE("Enter the exception case!"); - return HC_ERR_CASE; + } + *returnChannelId = channelId; + return HC_SUCCESS; + } else { + return HC_ERR_CHANNEL_NOT_EXIST; } } void CloseChannel(ChannelType channelType, int64_t channelId) { - SoftBus *softBusInstance = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGE("No channel!"); - return; - case SERVICE_CHANNEL: - LOGI("Use service channel, so we do not need to close it!"); - return; - case SOFT_BUS: - softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGI("The soft bus module is unavailable!"); - return; - } - softBusInstance->closeChannel(channelId); - return; - default: - LOGI("Enter the exception case!"); - return; + if (channelType == SOFT_BUS) { + GetSoftBusInstance()->closeChannel(channelId); } } int32_t SendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, const DeviceAuthCallback *callback, const char *data) { - SoftBus *softBusInstance = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGE("No channel!"); - return HC_ERR_CHANNEL_NOT_EXIST; - case SERVICE_CHANNEL: - if (ProcessTransmitCallback(requestId, (uint8_t *)data, HcStrlen(data) + 1, callback)) { - return HC_SUCCESS; - } - return HC_ERR_TRANSMIT_FAIL; - case SOFT_BUS: - softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGE("The soft bus module is unavailable!"); - return HC_ERR_SOFT_BUS; - } - return softBusInstance->sendMsg(channelId, (uint8_t *)data, HcStrlen(data) + 1); - default: - LOGE("Enter the exception case!"); - return HC_ERR_CASE; + if (channelType == SERVICE_CHANNEL) { + if (ProcessTransmitCallback(requestId, (uint8_t *)data, HcStrlen(data) + 1, callback)) { + return HC_SUCCESS; + } + return HC_ERR_TRANSMIT_FAIL; + } else if (channelType == SOFT_BUS) { + return GetSoftBusInstance()->sendMsg(channelId, (uint8_t *)data, HcStrlen(data) + 1); + } else { + return HC_ERR_CHANNEL_NOT_EXIST; } } void SetAuthResult(ChannelType channelType, int64_t channelId) { - SoftBus *softBusInstance = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGI("No channel!"); - return; - case SERVICE_CHANNEL: - return; - case SOFT_BUS: - softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGI("The soft bus module is unavailable!"); - return; - } - softBusInstance->setAuthResult(channelId); - return; - default: - LOGI("Enter the exception case!"); - return; + if (channelType == SOFT_BUS) { + GetSoftBusInstance()->notifyResult(channelId); } } -int32_t GetLocalConnectInfo(char **returnInfo) +int32_t GetLocalConnectInfo(char *jsonAddrInfo, int32_t bufLen) { + if ((jsonAddrInfo == NULL) || (bufLen == 0) || (bufLen > MAX_DATA_BUFFER_SIZE)) { + LOGE("Invalid input params!"); + return HC_ERR_INVALID_PARAMS; + } + LOGD("[Start]: GetLocalConnectInfo!"); if (!IsSoftBusChannelSupported()) { LOGE("Soft bus not supported!"); return HC_ERR_NOT_SUPPORT; } - SoftBus *softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGE("The soft bus module is unavailable!"); - return HC_ERR_SOFT_BUS; - } - if (returnInfo != NULL) { - LOGI("We're going to get local connection information!"); - *returnInfo = softBusInstance->getLocalAuthInfo(); - if (*returnInfo == NULL) { - LOGE("Failed to get local connection information!"); - return HC_ERR_SOFT_BUS; - } - LOGI("Get local connection information successfully!"); - return HC_SUCCESS; - } - LOGE("The address of the input returnLocalConnectInfo is NULL!"); - return HC_ERR_INVALID_PARAMS; + int32_t res = GetSoftBusInstance()->getLocalConnectInfo(jsonAddrInfo, bufLen); + LOGD("[End]: GetLocalConnectInfo!"); + return res == HC_SUCCESS ? HC_SUCCESS : HC_ERR_SOFT_BUS; } diff --git a/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c b/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c index 1810f8b..3babf41 100644 --- a/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c +++ b/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c @@ -14,230 +14,310 @@ */ #include "soft_bus_channel.h" + #include "common_defs.h" +#include "device_auth.h" #include "device_auth_defines.h" +#include "group_manager.h" #include "hc_log.h" +#include "session.h" +#include "session_manager.h" +#include "task_manager.h" typedef struct { - void (*onChannelOpened)(int64_t channelId, int64_t requestId, const char *deviceId, - uint32_t deviceIdLen, bool isServer); - void (*onChannelClosed)(int64_t channelId, int64_t requestId); - void (*onMsgReceived)(int64_t channelId, const uint8_t *data, uint32_t dataLen); - void (*onServiceDied)(); -} IAuthMsgListener; + HcTaskBase base; + int64_t requestId; + int64_t channelId; + CJson *params; +} SoftBusTask; typedef struct { - int (*addListener)(const char *packageName, const IAuthMsgListener *listener); - int (*removeListener)(const char *packageName); - int64_t (*openAuthChannel)(const char *packageName, const char *authInfo, int64_t requestId); - int (*closeChannel)(int64_t channelId); - int (*sendMsg)(int64_t channelId, const uint8_t *data, uint32_t len); - int (*setAuthResult)(int64_t channelId, const uint8_t *data, uint32_t len); - char *(*getLocalAuthInfo)(); -} AuthMsgService; + int64_t requestId; + int64_t channelId; +} ChannelEntry; -#define NORMAL_CHANNEL_ID 1 +DECLARE_HC_VECTOR(ChannelEntryVec, ChannelEntry) +IMPLEMENT_HC_VECTOR(ChannelEntryVec, ChannelEntry, 1) +static ChannelEntryVec g_channelVec; +static HcMutex *g_channelMutex = NULL; -static int AddListener(const char *packageName, const IAuthMsgListener *listener) +static int32_t GetReqIdByChannelId(int64_t channelId, int64_t *returnReqId) { - (void)packageName; - (void)listener; + uint32_t index; + ChannelEntry *entry = NULL; + g_channelMutex->lock(g_channelMutex); + FOR_EACH_HC_VECTOR(g_channelVec, index, entry) { + if ((entry != NULL) && (channelId == entry->channelId)) { + *returnReqId = entry->requestId; + g_channelMutex->unlock(g_channelMutex); + return HC_SUCCESS; + } + } + g_channelMutex->unlock(g_channelMutex); + LOGE("Failed to find request by channelId!"); + return HC_ERR_REQUEST_NOT_FOUND; +} + +static int32_t AddChannelEntry(int64_t requestId, int64_t channelId) +{ + int64_t tmpReqId = DEFAULT_REQUEST_ID; + if (GetReqIdByChannelId(channelId, &tmpReqId) == HC_SUCCESS) { + LOGE("A request to use the channel alreay exists!"); + return HC_ERR_REQUEST_EXIST; + } + ChannelEntry entry = { + .channelId = channelId, + .requestId = requestId + }; + g_channelMutex->lock(g_channelMutex); + g_channelVec.pushBack(&g_channelVec, &entry); + g_channelMutex->unlock(g_channelMutex); return HC_SUCCESS; } -static int RemoveListener(const char *packageName) +static void RemoveChannelEntry(int64_t channelId) { - (void)packageName; + uint32_t index; + ChannelEntry *entry = NULL; + g_channelMutex->lock(g_channelMutex); + FOR_EACH_HC_VECTOR(g_channelVec, index, entry) { + if ((entry != NULL) && (channelId == entry->channelId)) { + ChannelEntry tmpEntry; + HC_VECTOR_POPELEMENT(&g_channelVec, &tmpEntry, index); + g_channelMutex->unlock(g_channelMutex); + return; + } + } + g_channelMutex->unlock(g_channelMutex); +} + +static void DoOnChannelOpened(HcTaskBase *baseTask) +{ + if (baseTask == NULL) { + LOGE("The input task is NULL!"); + return; + } + SoftBusTask *task = (SoftBusTask *)baseTask; + OnChannelOpened(task->requestId, task->channelId); +} + +static void InitSoftBusTask(SoftBusTask *task, int64_t requestId, int64_t channelId) +{ + task->base.doAction = DoOnChannelOpened; + task->base.destroy = NULL; + task->channelId = channelId; + task->requestId = requestId; +} + +static char *GenRecvData(int64_t channelId, const void *data, uint32_t dataLen, int64_t *requestId) +{ + char *dataStr = (char *)HcMalloc(dataLen + 1, 0); + if (dataStr == NULL) { + LOGE("Failed to allocate dataStr memory!"); + return NULL; + } + if (memcpy_s(dataStr, dataLen + 1, data, dataLen) != HC_SUCCESS) { + LOGE("Failed to copy data!"); + HcFree(dataStr); + return NULL; + } + CJson *recvData = CreateJsonFromString(dataStr); + HcFree(dataStr); + if (recvData == NULL) { + LOGE("Failed to create recvData from string!"); + return NULL; + } + if (GetInt64FromJson(recvData, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) { + LOGE("Failed to get requestId from recvData!"); + FreeJson(recvData); + return NULL; + } + if (AddByteToJson(recvData, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { + LOGE("Failed to add channelId to recvData!"); + FreeJson(recvData); + return NULL; + } + char *recvDataStr = PackJsonToString(recvData); + FreeJson(recvData); + if (recvDataStr == NULL) { + LOGE("Failed to convert json to string!"); + return NULL; + } + return recvDataStr; +} + +static int OnChannelOpenedCb(int sessionId, int result) +{ + if (GetSessionSide(sessionId) == 0) { + LOGD("Peer device open channel!"); + return HC_SUCCESS; + } + int64_t requestId = 0; + if (GetReqIdByChannelId(sessionId, &requestId) != HC_SUCCESS) { + LOGE("The request corresponding to the channel is not found!"); + return HC_ERR_REQUEST_NOT_FOUND; + } + if (result != HC_SUCCESS) { + LOGE("[SoftBus][Out]: Failed to open channel! res: %d", result); + DestroySession(requestId); + return HC_ERR_SOFT_BUS; + } + LOGD("[Start]: OnChannelOpened! [ReqId]: %lld, [ChannelId]: %d", requestId, sessionId); + SoftBusTask *task = (SoftBusTask *)HcMalloc(sizeof(SoftBusTask), 0); + if (task == NULL) { + LOGE("Failed to allocate task memory!"); + DestroySession(requestId); + return HC_ERR_ALLOC_MEMORY; + } + InitSoftBusTask(task, requestId, sessionId); + if (PushTask((HcTaskBase *)task) != HC_SUCCESS) { + DestroySession(requestId); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } + LOGD("[End]: OnChannelOpened!"); return HC_SUCCESS; } -static int64_t OpenAuthChannel(const char *packageName, const char *connectParams, int64_t requestId) +static void OnBytesReceivedCb(int sessionId, const void *data, unsigned int dataLen) { - (void)packageName; - (void)connectParams; - (void)requestId; - return NORMAL_CHANNEL_ID; -} - -static int CloseChannel(int64_t channelId) -{ - (void)channelId; - return HC_SUCCESS; -} - -static int SendMsg(int64_t channelId, const uint8_t *data, uint32_t dataLen) -{ - (void)channelId; - (void)data; - (void)dataLen; - return HC_SUCCESS; -} - -static int SetAuthResult(int64_t channelId, const uint8_t *data, uint32_t dataLen) -{ - (void)channelId; - (void)data; - (void)dataLen; - return HC_SUCCESS; -} - -static char *GetLocalAuthInfo() -{ - return ""; -} - -static AuthMsgService g_softBusInstance = { - .addListener = AddListener, - .removeListener = RemoveListener, - .openAuthChannel = OpenAuthChannel, - .closeChannel = CloseChannel, - .sendMsg = SendMsg, - .setAuthResult = SetAuthResult, - .getLocalAuthInfo = GetLocalAuthInfo -}; - -static AuthMsgService *GetInstance(const char *packageName) -{ - (void)packageName; - return &g_softBusInstance; + if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) { + LOGE("Invalid input params!"); + return; + } + LOGD("[Start]: OnMsgReceived! [ChannelId]: %d", sessionId); + int64_t requestId = DEFAULT_REQUEST_ID; + char *recvDataStr = GenRecvData(sessionId, data, dataLen, &requestId); + if (recvDataStr == NULL) { + return; + } + (void)GetGmInstance()->processData(requestId, (uint8_t *)recvDataStr, HcStrlen(recvDataStr) + 1); + FreeJsonString(recvDataStr); } static int32_t OpenSoftBusChannel(const char *connectParams, int64_t requestId, int64_t *returnChannelId) { - if (returnChannelId == NULL) { - LOGE("The address of the input returnChannelId is NULL!"); + if ((connectParams == NULL) || (returnChannelId == NULL)) { + LOGE("The input connectParams or returnChannelId is NULL!"); return HC_ERR_NULL_PTR; } - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return HC_ERR_SOFT_BUS; + ConnectionAddr addr; + addr.type = CONNECTION_ADDR_MIX; + if (memcpy_s(addr.info.mixAddr.addr, MIX_ADDR_MAX_LEN, connectParams, HcStrlen(connectParams) + 1) != EOK) { + LOGE("Failed to copy connectParams!"); + return HC_ERR_MEMORY_COPY; } - LOGI("Ready to open the soft bus channel!"); - int64_t channelId = instance->openAuthChannel(GROUP_MANAGER_PACKAGE_NAME, connectParams, requestId); + LOGD("[SoftBus][In]: OpenChannel!"); + int64_t channelId = (int64_t)OpenAuthSession(GROUP_MANAGER_PACKAGE_NAME, &addr); + LOGD("[SoftBus][Out]: OpenChannel! [channelId]: %lld", channelId); /* If the value of channelId is less than 0, the soft bus fails to open the channel */ if (channelId < 0) { LOGE("Failed to open soft bus channel!"); return HC_ERR_SOFT_BUS; } - LOGI("Open soft bus channel successfully!"); + int32_t res = AddChannelEntry(requestId, channelId); + if (res != HC_SUCCESS) { + return res; + } *returnChannelId = channelId; return HC_SUCCESS; } static void CloseSoftBusChannel(int64_t channelId) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return; - } - LOGI("Ready to close the soft bus channel!"); - int32_t result = instance->closeChannel(channelId); - if (result != HC_SUCCESS) { - LOGE("Failed to close soft bus channel!"); - } - LOGI("Close soft bus channel success!"); + RemoveChannelEntry(channelId); + LOGD("[SoftBus][In]: CloseSession!"); + CloseSession(channelId); + LOGD("[SoftBus][Out]: CloseSession!"); } static int32_t SendSoftBusMsg(int64_t channelId, const uint8_t *data, uint32_t dataLen) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); + LOGD("[SoftBus][In]: SendMsg!"); + int32_t res = SendBytes(channelId, data, dataLen); + LOGD("[SoftBus][Out]: SendMsg! res: %d", res); + if (res != HC_SUCCESS) { + LOGE("An error occurs when the softbus sends data!"); return HC_ERR_SOFT_BUS; } - LOGI("The soft bus channel starts to send data!"); - int32_t result = instance->sendMsg(channelId, data, dataLen); - if (result != HC_SUCCESS) { - LOGE("An error occurs when the soft bus channel sends data!"); - return HC_ERR_SOFT_BUS; - } - LOGI("The soft bus channel sends data successfully!"); return HC_SUCCESS; } -static void SetSoftBusAuthResult(int64_t channelId) +static void NotifySoftBusBindResult(int64_t channelId) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return; - } - LOGI("Prepare to notify the soft bus authentication result!"); - int32_t result = instance->setAuthResult(channelId, NULL, 0); - if (result != HC_SUCCESS) { - LOGE("Failed to notify the soft bus of the authentication result!"); - return; - } - LOGI("The soft bus is successfully notified of the authentication result!"); + LOGD("[SoftBus][In]: JoinLNN!"); + // (void)JoinLNN(channelId); + LOGD("[SoftBus][Out]: JoinLNN!"); } -static char *GetSoftBusLocalAuthInfo() +static int32_t GetLocalConnectInfo(char *jsonAddrInfo, int32_t bufLen) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return ""; - } - return instance->getLocalAuthInfo(); + LOGD("[SoftBus][In]: GetLocalConnectionInfo!"); + // int32_t res = GetLocalConnectionInfo(jsonAddrInfo, bufLen); + (void)jsonAddrInfo; + (void)bufLen; + LOGD("[SoftBus][Out]: GetLocalConnectionInfo!"); + // return res; + return HC_SUCCESS; } SoftBus g_softBus = { .openChannel = OpenSoftBusChannel, .closeChannel = CloseSoftBusChannel, .sendMsg = SendSoftBusMsg, - .setAuthResult = SetSoftBusAuthResult, - .getLocalAuthInfo = GetSoftBusLocalAuthInfo + .notifyResult = NotifySoftBusBindResult, + .getLocalConnectInfo = GetLocalConnectInfo }; -int32_t InitSoftBusChannelModule(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()) +int32_t InitSoftBusChannelModule(void) { - IAuthMsgListener softBusListener = { - .onChannelOpened = onChannelOpened, - .onChannelClosed = onChannelClosed, - .onMsgReceived = onMsgReceived, - .onServiceDied = onServiceDied + if (g_channelMutex == NULL) { + g_channelMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0); + if (g_channelMutex == NULL) { + LOGE("Failed to allocate channel mutex memory!"); + return HC_ERR_ALLOC_MEMORY; + } + if (InitHcMutex(g_channelMutex) != HC_SUCCESS) { + LOGE("Init mutex failed!"); + HcFree(g_channelMutex); + g_channelMutex = NULL; + return HC_ERR_INIT_FAILED; + } + } + g_channelVec = CREATE_HC_VECTOR(ChannelEntryVec) + ISessionListener softBusListener = { + .OnSessionOpened = OnChannelOpenedCb, + .OnSessionClosed = NULL, + .OnBytesReceived = OnBytesReceivedCb, + .OnMessageReceived = NULL }; - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return HC_ERR_SOFT_BUS; - } - LOGI("Prepare to add listener callback to soft bus service!"); - int32_t result = instance->addListener(GROUP_MANAGER_PACKAGE_NAME, &softBusListener); - if (result != HC_SUCCESS) { - LOGE("Failed to add listener callback to soft bus service!"); - return HC_ERR_SOFT_BUS; - } - LOGI("Add listener callback to soft bus service successfully!"); - return HC_SUCCESS; + LOGD("[SoftBus][In]: CreateSessionServer!"); + int32_t res = CreateSessionServer(GROUP_MANAGER_PACKAGE_NAME, GROUP_MANAGER_PACKAGE_NAME, &softBusListener); + LOGD("[SoftBus][Out]: CreateSessionServer! res: %d", res); + return res; } -void DestroySoftBusChannelModule() +void DestroySoftBusChannelModule(void) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGI("Failed to get soft bus instance!"); - return; + g_channelMutex->lock(g_channelMutex); + DESTROY_HC_VECTOR(ChannelEntryVec, &g_channelVec) + g_channelMutex->unlock(g_channelMutex); + if (g_channelMutex != NULL) { + DestroyHcMutex(g_channelMutex); + HcFree(g_channelMutex); + g_channelMutex = NULL; } - LOGI("Prepare to remove listener callback from soft bus service!"); - int32_t result = instance->removeListener(GROUP_MANAGER_PACKAGE_NAME); - if (result != HC_SUCCESS) { - LOGI("Failed to remove listener callback from soft bus module!"); - } - LOGI("Remove listener callback from soft bus service successfully!"); + LOGD("[SoftBus][In]: RemoveSessionServer!"); + int32_t res = RemoveSessionServer(GROUP_MANAGER_PACKAGE_NAME, GROUP_MANAGER_PACKAGE_NAME); + LOGD("[SoftBus][Out]: RemoveSessionServer! res: %d", res); } -SoftBus *GetSoftBusInstance() +SoftBus *GetSoftBusInstance(void) { return &g_softBus; } -bool IsSoftBusChannelSupported() +bool IsSoftBusChannelSupported(void) { return true; } \ No newline at end of file diff --git a/services/common/src/task_manager/task_manager.c b/services/common/src/task_manager/task_manager.c new file mode 100644 index 0000000..279f837 --- /dev/null +++ b/services/common/src/task_manager/task_manager.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "task_manager.h" + +#include "device_auth_defines.h" +#include "hc_log.h" + +#define STACK_SIZE 4096 + +static HcTaskThread *g_taskThread = NULL; + +int32_t PushTask(HcTaskBase *baseTask) +{ + if (g_taskThread == NULL) { + LOGE("Task thread is NULL!"); + return HC_ERR_NULL_PTR; + } + g_taskThread->pushTask(g_taskThread, baseTask); + return HC_SUCCESS; +} + +int32_t InitTaskManager(void) +{ + if (g_taskThread != NULL) { + LOGD("Task thread is running!"); + return HC_SUCCESS; + } + g_taskThread = (HcTaskThread *)HcMalloc(sizeof(HcTaskThread), 0); + if (g_taskThread == NULL) { + return HC_ERR_ALLOC_MEMORY; + } + int32_t res = InitHcTaskThread(g_taskThread, STACK_SIZE, "HichainThread"); + if (res != HC_SUCCESS) { + LOGE("Failed to init task thread! res: %d", res); + HcFree(g_taskThread); + g_taskThread = NULL; + return HC_ERR_INIT_FAILED; + } + res = g_taskThread->startThread(g_taskThread); + if (res != HC_SUCCESS) { + DestroyHcTaskThread(g_taskThread); + HcFree(g_taskThread); + g_taskThread = NULL; + LOGE("Failed to start thread! res: %d", res); + return HC_ERR_INIT_FAILED; + } + return HC_SUCCESS; +} + +void DestroyTaskManager(void) +{ + if (g_taskThread != NULL) { + g_taskThread->stopAndClear(g_taskThread); + DestroyHcTaskThread(g_taskThread); + HcFree(g_taskThread); + g_taskThread = NULL; + } +} \ No newline at end of file diff --git a/services/device_auth.c b/services/device_auth.c index 0a0693e..504250e 100644 --- a/services/device_auth.c +++ b/services/device_auth.c @@ -14,23 +14,21 @@ */ #include "device_auth.h" + #include "alg_loader.h" #include "callback_manager.h" #include "channel_manager.h" #include "common_util.h" -#include "database_manager.h" #include "dev_auth_module_manager.h" #include "device_auth_defines.h" #include "group_auth_manager.h" #include "group_manager.h" #include "hc_init_protection.h" #include "hc_log.h" -#include "hc_task_thread.h" #include "json_utils.h" #include "securec.h" #include "session_manager.h" - -#define STACK_SIZE 4096 +#include "task_manager.h" typedef struct { HcTaskBase base; @@ -41,7 +39,6 @@ typedef struct { static GroupAuthManager *g_groupAuthManager = NULL; static DeviceGroupManager *g_groupManagerInstance = NULL; -static HcTaskThread *g_taskThread = NULL; static int32_t BindCallbackToTask(GroupManagerTask *task, const CJson *jsonParams) { @@ -68,42 +65,6 @@ static int32_t BindCallbackToTask(GroupManagerTask *task, const CJson *jsonParam return HC_SUCCESS; } -static char *GenerateRecvData(int64_t channelId, const uint8_t *data, uint32_t dataLen, int64_t *requestId) -{ - char *dataStr = (char *)HcMalloc(dataLen + 1, 0); - if (dataStr == NULL) { - LOGE("Failed to allocate dataStr memory!"); - return NULL; - } - if (memcpy_s(dataStr, dataLen + 1, data, dataLen) != HC_SUCCESS) { - LOGE("Failed to copy data!"); - HcFree(dataStr); - return NULL; - } - CJson *receivedData = CreateJsonFromString(dataStr); - HcFree(dataStr); - if (receivedData == NULL) { - LOGE("Failed to create receivedData json object from string!"); - return NULL; - } - if (GetInt64FromJson(receivedData, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) { - LOGE("Failed to get requestId from receivedData!"); - return NULL; - } - if (AddByteToJson(receivedData, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { - LOGE("Failed to add channelId to recvData!"); - FreeJson(receivedData); - return NULL; - } - char *receivedDataStr = PackJsonToString(receivedData); - FreeJson(receivedData); - if (receivedDataStr == NULL) { - LOGE("Failed to convert json to string!"); - return NULL; - } - return receivedDataStr; -} - static void DoCreateGroup(HcTaskBase *task) { if (task == NULL) { @@ -195,24 +156,6 @@ static void DoConfirmRequest(HcTaskBase *task) OnConfirmationReceived(realTask->requestId, realTask->jsonParams); } -static void OnChannelOpenedAction(HcTaskBase *task) -{ - if (task == NULL) { - LOGE("The input task is NULL!"); - return; - } - GroupManagerTask *realTask = (GroupManagerTask *)task; - LOGI("The task thread starts to execute the task that needs to be executed" - "when the soft bus channel is open! [RequestId]: %" PRId64, realTask->requestId); - CJson *jsonParams = realTask->jsonParams; - int64_t channelId = DEFAULT_CHANNEL_ID; - if (GetByteFromJson(jsonParams, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { - LOGE("Failed to get channelId from json!"); - return; - } - OnChannelOpened(realTask->requestId, channelId); -} - static void DoProcessBindData(HcTaskBase *task) { if (task == NULL) { @@ -307,14 +250,6 @@ static void InitProcessBindDataTask(GroupManagerTask *task, int64_t requestId, C task->jsonParams = jsonParams; } -static void InitChannelOpenedTask(GroupManagerTask *task, int64_t requestId, CJson *jsonParams) -{ - task->base.doAction = OnChannelOpenedAction; - task->base.destroy = DestroyGroupManagerTask; - task->requestId = requestId; - task->jsonParams = jsonParams; -} - static int32_t InitCreateGroupTask(GroupManagerTask *task, int64_t requestId, CJson *jsonParams) { task->base.doAction = DoCreateGroup; @@ -367,10 +302,6 @@ static int32_t AuthDevice(int64_t authReqId, const char *authParams, const Devic LOGE("The input auth params is null!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("The task thread is null!"); - return HC_ERR_SERVICE_NEED_RESTART; - } CJson *jsonParams = CreateJsonFromString(authParams); if (jsonParams == NULL) { LOGE("Create json from params failed!"); @@ -388,7 +319,11 @@ static int32_t AuthDevice(int64_t authReqId, const char *authParams, const Devic HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("Push AuthDevice task successfully."); return HC_SUCCESS; } @@ -401,10 +336,6 @@ static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t data LOGE("Invalid input for ProcessData!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("The task thread is null!"); - return HC_ERR_SERVICE_NEED_RESTART; - } CJson *receivedData = CreateJsonFromString((const char *)data); if (receivedData == NULL) { LOGE("Create Json for input data failed!"); @@ -422,7 +353,11 @@ static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t data HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(receivedData); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("Push ProcessData task successfully."); return HC_SUCCESS; } @@ -518,16 +453,11 @@ static int32_t UnRegGroupManagerCallback(const char *appId) static int32_t RequestCreateGroup(int64_t requestId, const char *appId, const char *createParams) { - LOGI("[Start]: RequestCreateGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (createParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestCreateGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonCreateParams = CreateJsonFromString(createParams); if (jsonCreateParams == NULL) { LOGE("Failed to create json from string!"); @@ -550,23 +480,22 @@ static int32_t RequestCreateGroup(int64_t requestId, const char *appId, const ch HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonCreateParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the creating group task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestDeleteGroup(int64_t requestId, const char *appId, const char *disbandParams) { - LOGI("[Start]: RequestDeleteGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (disbandParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestDeleteGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonDisBandParams = CreateJsonFromString(disbandParams); if (jsonDisBandParams == NULL) { LOGE("Failed to create json from string!"); @@ -589,23 +518,22 @@ static int32_t RequestDeleteGroup(int64_t requestId, const char *appId, const ch HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase *)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonDisBandParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the deleting group task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestAddMemberToGroup(int64_t requestId, const char *appId, const char *addParams) { - LOGI("[Start]: RequestAddMemberToGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (addParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestAddMemberToGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonAddParams = CreateJsonFromString(addParams); if (jsonAddParams == NULL) { LOGE("Failed to create json from string!"); @@ -628,23 +556,22 @@ static int32_t RequestAddMemberToGroup(int64_t requestId, const char *appId, con HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonAddParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the adding member task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestDeleteMemberFromGroup(int64_t requestId, const char *appId, const char *deleteParams) { - LOGI("[Start]: RequestDeleteMemberFromGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (deleteParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestDeleteMemberFromGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonDeleteParams = CreateJsonFromString(deleteParams); if (jsonDeleteParams == NULL) { LOGE("Failed to create json from string!"); @@ -667,23 +594,22 @@ static int32_t RequestDeleteMemberFromGroup(int64_t requestId, const char *appId HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonDeleteParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the deleting member task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestProcessBindData(int64_t requestId, const uint8_t *data, uint32_t dataLen) { - LOGI("[Start]: RequestProcessBindData! [RequestId]: %" PRId64, requestId); - if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { + if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) { LOGE("The input data is invalid!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("The task thread is NULL!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestProcessBindData! [RequestId]: %" PRId64, requestId); CJson *dataJson = CreateJsonFromString((const char *)data); if (dataJson == NULL) { LOGE("Failed to create json from string!"); @@ -698,6 +624,7 @@ static int32_t RequestProcessBindData(int64_t requestId, const uint8_t *data, ui if (tempRequestId != requestId) { LOGE("The requestId transferred by the service is inconsistent with that in the packet! " "[ServiceRequestId]: %" PRId64 ", [RequestId]: %" PRId64, requestId, tempRequestId); + FreeJson(dataJson); return HC_ERR_INVALID_PARAMS; } GroupManagerTask *task = (GroupManagerTask *)HcMalloc(sizeof(GroupManagerTask), 0); @@ -707,23 +634,22 @@ static int32_t RequestProcessBindData(int64_t requestId, const uint8_t *data, ui return HC_ERR_ALLOC_MEMORY; } InitProcessBindDataTask(task, requestId, dataJson); - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(dataJson); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the processing data task successfully! [RequestId]: %" PRId64, requestId); return HC_SUCCESS; } static int32_t ConfirmRequest(int64_t requestId, const char *appId, const char *confirmParams) { - LOGI("[Start]: ConfirmRequest! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (confirmParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: ConfirmRequest! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonConfirmParams = CreateJsonFromString(confirmParams); if (jsonConfirmParams == NULL) { LOGE("Failed to create json from string!"); @@ -731,6 +657,7 @@ static int32_t ConfirmRequest(int64_t requestId, const char *appId, const char * } if (AddStringToJson(jsonConfirmParams, FIELD_APP_ID, appId) != HC_SUCCESS) { LOGE("Failed to add appId to json!"); + FreeJson(jsonConfirmParams); return HC_ERR_JSON_FAIL; } @@ -745,80 +672,16 @@ static int32_t ConfirmRequest(int64_t requestId, const char *appId, const char * HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonConfirmParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the confirming request task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } -static void OnChannelOpenedCallback(int64_t channelId, int64_t requestId, const char *deviceId, - uint32_t deviceIdLen, bool isServer) -{ - if (isServer) { - return; - } - if ((deviceId == NULL) || (deviceIdLen > MAX_DATA_BUFFER_SIZE)) { - LOGE("The input parameters is invalid!"); - return; - } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return; - } - - CJson *jsonParams = CreateJson(); - if (jsonParams == NULL) { - LOGE("Failed to allocate jsonParams memory!"); - return; - } - if (AddByteToJson(jsonParams, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { - LOGE("Failed to add channelId to jsonParams!"); - FreeJson(jsonParams); - return; - } - - GroupManagerTask *task = (GroupManagerTask *)HcMalloc(sizeof(GroupManagerTask), 0); - if (task == NULL) { - LOGE("Failed to allocate task memory!"); - FreeJson(jsonParams); - return; - } - InitChannelOpenedTask(task, requestId, jsonParams); - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); -} - -static void OnChannelClosedCallback(int64_t channelId, int64_t requestId) -{ - (void)channelId; - (void)requestId; - return; -} - -static void OnMsgReceivedCallback(int64_t channelId, const uint8_t *data, uint32_t dataLen) -{ - LOGI("Receive data from the peer end!"); - if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { - LOGE("The input data is invalid!"); - return; - } - - int64_t requestId = DEFAULT_REQUEST_ID; - char *receivedDataStr = GenerateRecvData(channelId, data, dataLen, &requestId); - if (receivedDataStr == NULL) { - return; - } - if (RequestProcessBindData(DEFAULT_REQUEST_ID, (uint8_t *)receivedDataStr, - (uint32_t)(strlen(receivedDataStr) + 1)) != HC_SUCCESS) { - LOGE("Failed to process bind data from softBus!"); - } - FreeJsonString(receivedDataStr); -} - -static void OnServiceDiedCallback(void) -{ - return; -} - static int32_t GetModuleType(const CJson *jsonParams) { bool isAccountBind = false; @@ -1048,16 +911,11 @@ static int32_t AddLiteDataToReceivedData(CJson *receivedData, int64_t requestId, static int32_t RequestProcessLiteData(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen) { - LOGI("[Start]: RequestProcessLiteData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { LOGE("The input data is NULL or dataLen is beyond max size!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestProcessLiteData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *receivedData = CreateJsonFromString((const char *)data); if (receivedData == NULL) { LOGE("Failed to create received json object from string!"); @@ -1079,7 +937,11 @@ static int32_t RequestProcessLiteData(int64_t requestId, const char *appId, cons HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(receivedData); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the processing lite data task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1087,16 +949,11 @@ static int32_t RequestProcessLiteData(int64_t requestId, const char *appId, cons static int32_t RequestBindPeer(int64_t requestId, const char *appId, const char *bindParams) { - LOGI("[Start]: RequestBindPeer! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (bindParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestBindPeer! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonBindParams = CreateJsonFromString(bindParams); if (jsonBindParams == NULL) { LOGE("Failed to create json from string!"); @@ -1119,7 +976,11 @@ static int32_t RequestBindPeer(int64_t requestId, const char *appId, const char HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonBindParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the binding peer device task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1127,16 +988,11 @@ static int32_t RequestBindPeer(int64_t requestId, const char *appId, const char static int32_t RequestUnbindPeer(int64_t requestId, const char *appId, const char *unBindParams) { - LOGI("[Start]: RequestUnbindPeer, [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (unBindParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestUnbindPeer, [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonUnbindParams = CreateJsonFromString(unBindParams); if (jsonUnbindParams == NULL) { LOGE("Failed to create json from string!"); @@ -1159,7 +1015,11 @@ static int32_t RequestUnbindPeer(int64_t requestId, const char *appId, const cha HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonUnbindParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the unbinding peer device task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1167,16 +1027,11 @@ static int32_t RequestUnbindPeer(int64_t requestId, const char *appId, const cha static int32_t RequestAuthKeyAgree(int64_t requestId, const char *appId, const char *agreeParams) { - LOGI("[Start]: RequestAuthKeyAgree! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (agreeParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestAuthKeyAgree! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonAgreeParams = CreateJsonFromString(agreeParams); if (jsonAgreeParams == NULL) { LOGE("Failed to create json from string!"); @@ -1199,23 +1054,22 @@ static int32_t RequestAuthKeyAgree(int64_t requestId, const char *appId, const c HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonAgreeParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the key agreement task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestProcessKeyAgreeData(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen) { - LOGI("[Start]: RequestProcessKeyAgreeData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { LOGE("The input data is NULL or dataLen is beyond max size!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestProcessKeyAgreeData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *receivedData = CreateJsonFromString((const char *)data); if (receivedData == NULL) { LOGE("Failed to create received json object from string!"); @@ -1237,7 +1091,11 @@ static int32_t RequestProcessKeyAgreeData(int64_t requestId, const char *appId, HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(receivedData); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the processing key agreement data task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1605,33 +1463,6 @@ static int32_t InitAlgorithm() return res; } -static int InitAndStartThread() -{ - if (g_taskThread != NULL) { - LOGD("Task thread is not null"); - } - g_taskThread = HcMalloc(sizeof(HcTaskThread), 0); - if (g_taskThread == NULL) { - return HC_ERR_ALLOC_MEMORY; - } - int32_t res = InitHcTaskThread(g_taskThread, STACK_SIZE, "HichainThread"); - if (res != HC_SUCCESS) { - LOGE("Init task thread failed, res:%d", res); - HcFree(g_taskThread); - g_taskThread = NULL; - return HC_ERROR; - } - res = g_taskThread->startThread(g_taskThread); - if (res != HC_SUCCESS) { - DestroyHcTaskThread(g_taskThread); - HcFree(g_taskThread); - g_taskThread = NULL; - LOGE("Start thread failed, res:%d", res); - return HC_ERROR; - } - return res; -} - static void DestroyGmAndGa() { if (g_groupAuthManager != NULL) { @@ -1644,21 +1475,11 @@ static void DestroyGmAndGa() } } -static void StopAndDestroyThread() -{ - if (g_taskThread != NULL) { - g_taskThread->stopAndClear(g_taskThread); - DestroyHcTaskThread(g_taskThread); - HcFree(g_taskThread); - g_taskThread = NULL; - } -} - DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService() { LOGI("[Service]: Start to init device auth service!"); if (CheckInit() == FINISH_INIT) { - LOGI("[End]: [Service]: Init device auth service successfully!"); + LOGI("[End]: [Service]: Device auth service is running!"); return HC_SUCCESS; } int32_t res = InitAlgorithm(); @@ -1680,14 +1501,13 @@ DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService() if (res != HC_SUCCESS) { goto free_module; } - res = InitChannelManager(OnChannelOpenedCallback, - OnChannelClosedCallback, OnMsgReceivedCallback, OnServiceDiedCallback); + res = InitChannelManager(); if (res != HC_SUCCESS) { LOGE("[End]: [Service]: Failed to init channel manage module!"); goto free_group_manager; } InitSessionManager(); - res = InitAndStartThread(); + res = InitTaskManager(); if (res != HC_SUCCESS) { LOGE("[End]: [Service]: Failed to init worker thread!"); goto free_all; @@ -1716,12 +1536,12 @@ DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService() LOGI("[End]: [Service]: The service has not been initialized, so it does not need to be destroyed!"); return; } + DestroyTaskManager(); DestroyChannelManager(); DestroySessionManager(); DestroyGroupManager(); DestroyModules(); DestroyGmAndGa(); - StopAndDestroyThread(); SetDeInitStatus(); LOGI("[End]: [Service]: Destroy device auth service successfully!"); } diff --git a/services/deviceauth.gni b/services/deviceauth.gni index 437f6f6..8e23a3e 100644 --- a/services/deviceauth.gni +++ b/services/deviceauth.gni @@ -22,6 +22,7 @@ inc_path = [ "${services_path}/common/inc/channel_manager", "${services_path}/common/inc/channel_manager/soft_bus_channel", "${services_path}/common/inc/data_base", + "${services_path}/common/inc/task_manager", "${services_path}/group_auth/inc", "${services_path}/group_auth/inc/account_unrelated_group_auth", "${services_path}/group_auth/inc/account_related_group_auth", @@ -56,8 +57,9 @@ deviceauth_files = [ "${services_path}/common/src/broadcast_manager/broadcast_manager.c", "${services_path}/common/src/callback_manager/callback_manager.c", "${services_path}/common/src/channel_manager/channel_manager.c", - "${services_path}/common/src/channel_manager/soft_bus_channel_mock/soft_bus_channel_mock.c", + "${services_path}/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c", "${services_path}/common/src/data_base/database_manager.c", + "${services_path}/common/src/task_manager/task_manager.c", "${services_path}/group_auth/src/group_auth_manager/group_auth_manager.c", "${services_path}/group_auth/src/group_auth_manager/account_unrelated_group_auth/account_unrelated_group_auth.c", diff --git a/services/group_auth/inc/group_auth_common_defines.h b/services/group_auth/inc/group_auth_common_defines.h index 97d276f..0d863c8 100644 --- a/services/group_auth/inc/group_auth_common_defines.h +++ b/services/group_auth/inc/group_auth_common_defines.h @@ -21,7 +21,6 @@ #include "hc_task_thread.h" #include "json_utils.h" -#define MAX_DATA_BUFFER_SIZE 4096 #define MAX_UDID_LEN 64 typedef struct { From 86682d04a1b7d5433fac7d7e13d68d8c7082e0ee Mon Sep 17 00:00:00 2001 From: fuzikun Date: Wed, 18 Aug 2021 20:15:07 +0800 Subject: [PATCH 3/7] add softbus channel Signed-off-by: fuzikun --- frameworks/src/ipc_sdk.c | 4 +- interfaces/innerkits/device_auth.h | 2 +- interfaces/innerkits/device_auth_defines.h | 1 + services/BUILD.gn | 18 +- .../inc/channel_manager/channel_manager.h | 20 +- .../soft_bus_channel/soft_bus_channel.h | 21 +- services/common/inc/common_defs.h | 1 + .../common/inc/task_manager/task_manager.h | 32 ++ .../src/channel_manager/channel_manager.c | 195 ++------ .../soft_bus_channel/soft_bus_channel.c | 436 ++++++++++++------ .../soft_bus_channel_mock.c | 15 +- .../common/src/task_manager/task_manager.c | 71 +++ services/device_auth.c | 367 ++++----------- services/deviceauth.gni | 4 +- .../inc/group_auth_common_defines.h | 1 - .../peer_to_peer_group/peer_to_peer_group.c | 10 +- .../src/bind_session/bind_session_client.c | 6 +- .../bind_session_client_lite.c | 6 +- .../bind_session_common_lite.c | 2 +- 19 files changed, 592 insertions(+), 620 deletions(-) create mode 100644 services/common/inc/task_manager/task_manager.h create mode 100644 services/common/src/task_manager/task_manager.c diff --git a/frameworks/src/ipc_sdk.c b/frameworks/src/ipc_sdk.c index f40a3b2..2c83afb 100644 --- a/frameworks/src/ipc_sdk.c +++ b/frameworks/src/ipc_sdk.c @@ -823,10 +823,10 @@ static int32_t IpcGmGetRegisterInfo(char **registerInfo) return (*registerInfo != NULL) ? HC_SUCCESS : HC_ERR_NULL_PTR; } -static int32_t IpcGmGetLocalConnectInfo(char **outLocalConnInfo) +static int32_t IpcGmGetLocalConnectInfo(char *returnInfo, int32_t bufLen) { LOGI("starting ..."); - if (outLocalConnInfo == NULL) { + if (returnInfo == NULL) { return HC_ERR_INVALID_PARAMS; } diff --git a/interfaces/innerkits/device_auth.h b/interfaces/innerkits/device_auth.h index 0717824..9b718a5 100644 --- a/interfaces/innerkits/device_auth.h +++ b/interfaces/innerkits/device_auth.h @@ -136,7 +136,7 @@ typedef struct { int32_t (*processKeyAgreeData)(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen); int32_t (*processCredential)(int operationCode, const char *reqJsonStr, char **returnJsonStr); int32_t (*getRegisterInfo)(char **returnRegisterInfo); - int32_t (*getLocalConnectInfo)(char **returnInfo); + int32_t (*getLocalConnectInfo)(char *returnInfo, int32_t bufLen); int32_t (*addGroupManager)(const char *appId, const char *groupId, const char *managerAppId); int32_t (*addGroupFriend)(const char *appId, const char *groupId, const char *friendAppId); int32_t (*deleteGroupManager)(const char *appId, const char *groupId, const char *managerAppId); diff --git a/interfaces/innerkits/device_auth_defines.h b/interfaces/innerkits/device_auth_defines.h index ae26f12..e704f74 100644 --- a/interfaces/innerkits/device_auth_defines.h +++ b/interfaces/innerkits/device_auth_defines.h @@ -42,6 +42,7 @@ enum { HC_ERR_INVALID_PUBLIC_KEY = 0x00001003, HC_ERR_VERIFY_FAILED = 0x00001004, HC_ERR_HASH_FAIL = 0x00001005, + HC_ERR_INIT_FAILED = 0x00001006, /* error code for json util , 0x00002000 ~ 0x00002FFF */ HC_ERR_JSON_FAIL = 0x00002001, diff --git a/services/BUILD.gn b/services/BUILD.gn index 8374189..a2b3977 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -27,7 +27,9 @@ if (defined(ohos_lite)) { "//third_party/cJSON", "//utils/native/lite/include", "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog", - "//third_party/bounds_checking_function/include" + "//third_party/bounds_checking_function/include", + "//foundation/communication/dsoftbus/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/kits/common", ] sources = deviceauth_files @@ -39,6 +41,7 @@ if (defined(ohos_lite)) { "//build/lite/config/component/cJSON:cjson_shared", "//utils/native/lite:utils", "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", + "//foundation/communication/dsoftbus/sdk:softbus_client", "//third_party/bounds_checking_function:libsec_shared", ] } @@ -116,30 +119,33 @@ if (defined(ohos_lite)) { ohos_static_library("deviceauth") { subsystem_name = "security" part_name = "deviceauth_standard" - + include_dirs = inc_path include_dirs += hals_inc_path include_dirs += [ "//third_party/cJSON", "//utils/native/base/include", - "${frameworks_path}/inc/standard" + "${frameworks_path}/inc/standard", ] sources = deviceauth_files - + cflags = [ "-DHILOG_ENABLE" ] cflags += build_flags if (target_cpu == "arm") { cflags += [ "-DBINDER_IPC_32BIT" ] } - + deps = [ "${hals_path}:${hal_module_name}", "//third_party/cJSON:cjson_static", "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "dsoftbus_standard:softbus_client", + ] } ohos_prebuilt_etc("auth_service.rc") { diff --git a/services/common/inc/channel_manager/channel_manager.h b/services/common/inc/channel_manager/channel_manager.h index ba764f4..5be60fb 100644 --- a/services/common/inc/channel_manager/channel_manager.h +++ b/services/common/inc/channel_manager/channel_manager.h @@ -20,19 +20,23 @@ #include "common_defs.h" #include "json_utils.h" -int32_t InitChannelManager(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()); -void DestroyChannelManager(); +#ifdef __cplusplus +extern "C" { +#endif + +int32_t InitChannelManager(void); +void DestroyChannelManager(void); /* Channel operation interfaces */ -ChannelType GetChannelType(const DeviceAuthCallback *callback); -bool CanFindValidChannel(ChannelType channelType, const CJson *jsonParams, const DeviceAuthCallback *callback); +ChannelType GetChannelType(const DeviceAuthCallback *callback, const CJson *jsonParams); int32_t OpenChannel(ChannelType channelType, const CJson *jsonParams, int64_t requestId, int64_t *returnChannelId); void CloseChannel(ChannelType channelType, int64_t channelId); int32_t SendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, const DeviceAuthCallback *callback, const char *data); void SetAuthResult(ChannelType channelType, int64_t channelId); -int32_t GetLocalConnectInfo(char **returnLocalConnectInfo); +int32_t GetLocalConnectInfo(char *jsonAddrInfo, int32_t bufLen); + +#ifdef __cplusplus +} +#endif #endif diff --git a/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h b/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h index 0cbeaa1..8b06b57 100644 --- a/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h +++ b/services/common/inc/channel_manager/soft_bus_channel/soft_bus_channel.h @@ -19,20 +19,23 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { int32_t (*openChannel)(const char *connectParams, int64_t requestId, int64_t *returnChannelId); void (*closeChannel)(int64_t channelId); int32_t (*sendMsg)(int64_t channelId, const uint8_t *data, uint32_t dataLen); - void (*setAuthResult)(int64_t channelId); - char *(*getLocalAuthInfo)(); + void (*notifyResult)(int64_t channelId); } SoftBus; -int32_t InitSoftBusChannelModule(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()); -void DestroySoftBusChannelModule(); -SoftBus *GetSoftBusInstance(); -bool IsSoftBusChannelSupported(); +int32_t InitSoftBusChannelModule(void); +void DestroySoftBusChannelModule(void); +SoftBus *GetSoftBusInstance(void); +bool IsSoftBusChannelSupported(void); +#ifdef __cplusplus +} +#endif #endif diff --git a/services/common/inc/common_defs.h b/services/common/inc/common_defs.h index afea1c5..9330519 100644 --- a/services/common/inc/common_defs.h +++ b/services/common/inc/common_defs.h @@ -129,6 +129,7 @@ #define DEFAULT_RETURN_KEY_LENGTH 32 #define ERR_AUTH_FORM 0 #define MAX_BUFFER_LEN 1024 +#define MAX_DATA_BUFFER_SIZE 4096 #define MAX_AUTH_ID_LEN 256 typedef enum { diff --git a/services/common/inc/task_manager/task_manager.h b/services/common/inc/task_manager/task_manager.h new file mode 100644 index 0000000..676b229 --- /dev/null +++ b/services/common/inc/task_manager/task_manager.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TASK_MANAGER_H +#define TASK_MANAGER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "hc_task_thread.h" + +int32_t InitTaskManager(void); +void DestroyTaskManager(void); +int32_t PushTask(HcTaskBase *baseTask); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/services/common/src/channel_manager/channel_manager.c b/services/common/src/channel_manager/channel_manager.c index 258ade0..fbceb5e 100644 --- a/services/common/src/channel_manager/channel_manager.c +++ b/services/common/src/channel_manager/channel_manager.c @@ -14,199 +14,88 @@ */ #include "channel_manager.h" + #include "callback_manager.h" #include "device_auth_defines.h" #include "hc_log.h" #include "hc_types.h" #include "soft_bus_channel.h" -int32_t InitChannelManager(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()) +int32_t InitChannelManager(void) { - if (IsSoftBusChannelSupported()) { - int32_t res = InitSoftBusChannelModule(onChannelOpened, onChannelClosed, onMsgReceived, onServiceDied); - if (res != HC_SUCCESS) { - DestroyChannelManager(); - } - return res; - } - return HC_SUCCESS; + return IsSoftBusChannelSupported() ? InitSoftBusChannelModule() : HC_SUCCESS; } -void DestroyChannelManager() +void DestroyChannelManager(void) { if (IsSoftBusChannelSupported()) { DestroySoftBusChannelModule(); } } -ChannelType GetChannelType(const DeviceAuthCallback *callback) +ChannelType GetChannelType(const DeviceAuthCallback *callback, const CJson *jsonParams) { + if (IsSoftBusChannelSupported()) + { + const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); + if (connectParams != NULL) { + return SOFT_BUS; + } + } if ((callback != NULL) && (callback->onTransmit != NULL)) { return SERVICE_CHANNEL; } - if (IsSoftBusChannelSupported()) { - return SOFT_BUS; - } return NO_CHANNEL; } -bool CanFindValidChannel(ChannelType channelType, const CJson *jsonParams, const DeviceAuthCallback *callback) -{ - switch (channelType) { - case NO_CHANNEL: - return false; - case SERVICE_CHANNEL: - return ((callback != NULL) && (callback->onTransmit != NULL)) ? true : false; - case SOFT_BUS: - if (GetSoftBusInstance() == NULL) { - LOGE("The soft bus module is unavailable!"); - return false; - } - const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); - if (connectParams == NULL) { - LOGE("Failed to get connectParams from jsonParams!"); - return false; - } - return true; - default: - LOGE("Enter the exception case!"); - return false; - } -} - int32_t OpenChannel(ChannelType channelType, const CJson *jsonParams, int64_t requestId, int64_t *returnChannelId) { - int64_t channelId = DEFAULT_CHANNEL_ID; - const char *connectParams = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGE("No channel!"); + if (channelType == SERVICE_CHANNEL) { + *returnChannelId = DEFAULT_CHANNEL_ID; + return HC_SUCCESS; + } else if (channelType == SOFT_BUS) { + const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); + if (connectParams == NULL) { + LOGE("Failed to get connectParams from jsonParams!"); + return HC_ERR_JSON_GET; + } + int64_t channelId = DEFAULT_CHANNEL_ID; + int32_t result = GetSoftBusInstance()->openChannel(connectParams, requestId, &channelId); + if (result != HC_SUCCESS) { return HC_ERR_CHANNEL_NOT_EXIST; - case SERVICE_CHANNEL: - LOGI("Use service channel, so we do not need to open it!"); - *returnChannelId = channelId; - return HC_SUCCESS; - case SOFT_BUS: - connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); - if (connectParams == NULL) { - LOGE("Failed to get connectParams from jsonParams!"); - return HC_ERR_JSON_GET; - } - SoftBus *softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGE("The soft bus module is unavailable!"); - return HC_ERR_SOFT_BUS; - } - int32_t result = softBusInstance->openChannel(connectParams, requestId, &channelId); - if (result != HC_SUCCESS) { - LOGE("Failed to open soft bus channel!"); - return HC_ERR_CHANNEL_NOT_EXIST; - } - *returnChannelId = channelId; - return HC_SUCCESS; - default: - LOGE("Enter the exception case!"); - return HC_ERR_CASE; + } + *returnChannelId = channelId; + return HC_SUCCESS; + } else { + return HC_ERR_CHANNEL_NOT_EXIST; } } void CloseChannel(ChannelType channelType, int64_t channelId) { - SoftBus *softBusInstance = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGE("No channel!"); - return; - case SERVICE_CHANNEL: - LOGI("Use service channel, so we do not need to close it!"); - return; - case SOFT_BUS: - softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGI("The soft bus module is unavailable!"); - return; - } - softBusInstance->closeChannel(channelId); - return; - default: - LOGI("Enter the exception case!"); - return; + if (channelType == SOFT_BUS) { + GetSoftBusInstance()->closeChannel(channelId); } } int32_t SendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, const DeviceAuthCallback *callback, const char *data) { - SoftBus *softBusInstance = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGE("No channel!"); - return HC_ERR_CHANNEL_NOT_EXIST; - case SERVICE_CHANNEL: - if (ProcessTransmitCallback(requestId, (uint8_t *)data, HcStrlen(data) + 1, callback)) { - return HC_SUCCESS; - } - return HC_ERR_TRANSMIT_FAIL; - case SOFT_BUS: - softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGE("The soft bus module is unavailable!"); - return HC_ERR_SOFT_BUS; - } - return softBusInstance->sendMsg(channelId, (uint8_t *)data, HcStrlen(data) + 1); - default: - LOGE("Enter the exception case!"); - return HC_ERR_CASE; + if (channelType == SERVICE_CHANNEL) { + if (ProcessTransmitCallback(requestId, (uint8_t *)data, HcStrlen(data) + 1, callback)) { + return HC_SUCCESS; + } + return HC_ERR_TRANSMIT_FAIL; + } else if (channelType == SOFT_BUS) { + return GetSoftBusInstance()->sendMsg(channelId, (uint8_t *)data, HcStrlen(data) + 1); + } else { + return HC_ERR_CHANNEL_NOT_EXIST; } } void SetAuthResult(ChannelType channelType, int64_t channelId) { - SoftBus *softBusInstance = NULL; - switch (channelType) { - case NO_CHANNEL: - LOGI("No channel!"); - return; - case SERVICE_CHANNEL: - return; - case SOFT_BUS: - softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGI("The soft bus module is unavailable!"); - return; - } - softBusInstance->setAuthResult(channelId); - return; - default: - LOGI("Enter the exception case!"); - return; + if (channelType == SOFT_BUS) { + GetSoftBusInstance()->notifyResult(channelId); } } - -int32_t GetLocalConnectInfo(char **returnInfo) -{ - if (!IsSoftBusChannelSupported()) { - LOGE("Soft bus not supported!"); - return HC_ERR_NOT_SUPPORT; - } - SoftBus *softBusInstance = GetSoftBusInstance(); - if (softBusInstance == NULL) { - LOGE("The soft bus module is unavailable!"); - return HC_ERR_SOFT_BUS; - } - if (returnInfo != NULL) { - LOGI("We're going to get local connection information!"); - *returnInfo = softBusInstance->getLocalAuthInfo(); - if (*returnInfo == NULL) { - LOGE("Failed to get local connection information!"); - return HC_ERR_SOFT_BUS; - } - LOGI("Get local connection information successfully!"); - return HC_SUCCESS; - } - LOGE("The address of the input returnLocalConnectInfo is NULL!"); - return HC_ERR_INVALID_PARAMS; -} diff --git a/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c b/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c index 1810f8b..70e1ab2 100644 --- a/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c +++ b/services/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c @@ -14,230 +14,380 @@ */ #include "soft_bus_channel.h" + #include "common_defs.h" +#include "device_auth.h" #include "device_auth_defines.h" +#include "group_manager.h" #include "hc_log.h" +#include "session.h" +#include "session_manager.h" +#include "task_manager.h" + +#define ETH_IP "ethIp" +#define ETH_PORT "ethPort" +#define WLAN_IP "wifiIp" +#define WLAN_PORT "wifiPort" +#define BR_MAC "brMac" +#define BLE_MAC "bleMac" typedef struct { - void (*onChannelOpened)(int64_t channelId, int64_t requestId, const char *deviceId, - uint32_t deviceIdLen, bool isServer); - void (*onChannelClosed)(int64_t channelId, int64_t requestId); - void (*onMsgReceived)(int64_t channelId, const uint8_t *data, uint32_t dataLen); - void (*onServiceDied)(); -} IAuthMsgListener; + HcTaskBase base; + int64_t requestId; + int64_t channelId; + CJson *params; +} SoftBusTask; typedef struct { - int (*addListener)(const char *packageName, const IAuthMsgListener *listener); - int (*removeListener)(const char *packageName); - int64_t (*openAuthChannel)(const char *packageName, const char *authInfo, int64_t requestId); - int (*closeChannel)(int64_t channelId); - int (*sendMsg)(int64_t channelId, const uint8_t *data, uint32_t len); - int (*setAuthResult)(int64_t channelId, const uint8_t *data, uint32_t len); - char *(*getLocalAuthInfo)(); -} AuthMsgService; + int64_t requestId; + int64_t channelId; +} ChannelEntry; -#define NORMAL_CHANNEL_ID 1 +DECLARE_HC_VECTOR(ChannelEntryVec, ChannelEntry) +IMPLEMENT_HC_VECTOR(ChannelEntryVec, ChannelEntry, 1) +static ChannelEntryVec g_channelVec; +static HcMutex *g_channelMutex = NULL; -static int AddListener(const char *packageName, const IAuthMsgListener *listener) +static int32_t GetReqIdByChannelId(int64_t channelId, int64_t *returnReqId) { - (void)packageName; - (void)listener; + uint32_t index; + ChannelEntry *entry = NULL; + g_channelMutex->lock(g_channelMutex); + FOR_EACH_HC_VECTOR(g_channelVec, index, entry) { + if ((entry != NULL) && (channelId == entry->channelId)) { + *returnReqId = entry->requestId; + g_channelMutex->unlock(g_channelMutex); + return HC_SUCCESS; + } + } + g_channelMutex->unlock(g_channelMutex); + LOGE("Failed to find request by channelId!"); + return HC_ERR_REQUEST_NOT_FOUND; +} + +static int32_t AddChannelEntry(int64_t requestId, int64_t channelId) +{ + int64_t tmpReqId = DEFAULT_REQUEST_ID; + if (GetReqIdByChannelId(channelId, &tmpReqId) == HC_SUCCESS) { + LOGE("A request to use the channel alreay exists!"); + return HC_ERR_REQUEST_EXIST; + } + ChannelEntry entry = { + .channelId = channelId, + .requestId = requestId + }; + g_channelMutex->lock(g_channelMutex); + g_channelVec.pushBack(&g_channelVec, &entry); + g_channelMutex->unlock(g_channelMutex); return HC_SUCCESS; } -static int RemoveListener(const char *packageName) +static void RemoveChannelEntry(int64_t channelId) { - (void)packageName; + uint32_t index; + ChannelEntry *entry = NULL; + g_channelMutex->lock(g_channelMutex); + FOR_EACH_HC_VECTOR(g_channelVec, index, entry) { + if ((entry != NULL) && (channelId == entry->channelId)) { + ChannelEntry tmpEntry; + HC_VECTOR_POPELEMENT(&g_channelVec, &tmpEntry, index); + g_channelMutex->unlock(g_channelMutex); + return; + } + } + g_channelMutex->unlock(g_channelMutex); +} + +static void DoOnChannelOpened(HcTaskBase *baseTask) +{ + if (baseTask == NULL) { + LOGE("The input task is NULL!"); + return; + } + SoftBusTask *task = (SoftBusTask *)baseTask; + OnChannelOpened(task->requestId, task->channelId); +} + +static void InitSoftBusTask(SoftBusTask *task, int64_t requestId, int64_t channelId) +{ + task->base.doAction = DoOnChannelOpened; + task->base.destroy = NULL; + task->channelId = channelId; + task->requestId = requestId; +} + +static char *GenRecvData(int64_t channelId, const void *data, uint32_t dataLen, int64_t *requestId) +{ + char *dataStr = (char *)HcMalloc(dataLen + 1, 0); + if (dataStr == NULL) { + LOGE("Failed to allocate dataStr memory!"); + return NULL; + } + if (memcpy_s(dataStr, dataLen + 1, data, dataLen) != HC_SUCCESS) { + LOGE("Failed to copy data!"); + HcFree(dataStr); + return NULL; + } + CJson *recvData = CreateJsonFromString(dataStr); + HcFree(dataStr); + if (recvData == NULL) { + LOGE("Failed to create recvData from string!"); + return NULL; + } + if (GetInt64FromJson(recvData, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) { + LOGE("Failed to get requestId from recvData!"); + FreeJson(recvData); + return NULL; + } + if (AddByteToJson(recvData, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { + LOGE("Failed to add channelId to recvData!"); + FreeJson(recvData); + return NULL; + } + char *recvDataStr = PackJsonToString(recvData); + FreeJson(recvData); + if (recvDataStr == NULL) { + LOGE("Failed to convert json to string!"); + return NULL; + } + return recvDataStr; +} + +static int OnChannelOpenedCb(int sessionId, int result) +{ + if (GetSessionSide(sessionId) == 0) { + LOGD("Peer device open channel!"); + return HC_SUCCESS; + } + int64_t requestId = 0; + if (GetReqIdByChannelId(sessionId, &requestId) != HC_SUCCESS) { + LOGE("The request corresponding to the channel is not found!"); + return HC_ERR_REQUEST_NOT_FOUND; + } + if (result != HC_SUCCESS) { + LOGE("[SoftBus][Out]: Failed to open channel! res: %d", result); + DestroySession(requestId); + return HC_ERR_SOFT_BUS; + } + LOGD("[Start]: OnChannelOpened! [ReqId]: %lld, [ChannelId]: %d", requestId, sessionId); + SoftBusTask *task = (SoftBusTask *)HcMalloc(sizeof(SoftBusTask), 0); + if (task == NULL) { + LOGE("Failed to allocate task memory!"); + DestroySession(requestId); + return HC_ERR_ALLOC_MEMORY; + } + InitSoftBusTask(task, requestId, sessionId); + if (PushTask((HcTaskBase *)task) != HC_SUCCESS) { + DestroySession(requestId); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } + LOGD("[End]: OnChannelOpened!"); return HC_SUCCESS; } -static int64_t OpenAuthChannel(const char *packageName, const char *connectParams, int64_t requestId) +static void OnBytesReceivedCb(int sessionId, const void *data, unsigned int dataLen) { - (void)packageName; - (void)connectParams; - (void)requestId; - return NORMAL_CHANNEL_ID; + if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) { + LOGE("Invalid input params!"); + return; + } + LOGD("[Start]: OnMsgReceived! [ChannelId]: %d", sessionId); + int64_t requestId = DEFAULT_REQUEST_ID; + char *recvDataStr = GenRecvData(sessionId, data, dataLen, &requestId); + if (recvDataStr == NULL) { + return; + } + (void)GetGmInstance()->processData(requestId, (uint8_t *)recvDataStr, HcStrlen(recvDataStr) + 1); + FreeJsonString(recvDataStr); } -static int CloseChannel(int64_t channelId) +static bool IsEthChannel(const CJson *json) { - (void)channelId; - return HC_SUCCESS; + const char *ethIp = GetStringFromJson(json, ETH_IP); + return (ethIp != NULL) ? true : false; } -static int SendMsg(int64_t channelId, const uint8_t *data, uint32_t dataLen) +static bool IsWlanChannel(const CJson *json) { - (void)channelId; - (void)data; - (void)dataLen; - return HC_SUCCESS; + const char *wlanIp = GetStringFromJson(json, WLAN_IP); + return (wlanIp != NULL) ? true : false; } -static int SetAuthResult(int64_t channelId, const uint8_t *data, uint32_t dataLen) +static bool IsBrChannel(const CJson *json) { - (void)channelId; - (void)data; - (void)dataLen; - return HC_SUCCESS; + const char *brMac = GetStringFromJson(json, BR_MAC); + return (brMac != NULL) ? true : false; } -static char *GetLocalAuthInfo() +static bool IsBleChannel(const CJson *json) { - return ""; + const char *bleMac = GetStringFromJson(json, BLE_MAC); + return (bleMac != NULL) ? true : false; } -static AuthMsgService g_softBusInstance = { - .addListener = AddListener, - .removeListener = RemoveListener, - .openAuthChannel = OpenAuthChannel, - .closeChannel = CloseChannel, - .sendMsg = SendMsg, - .setAuthResult = SetAuthResult, - .getLocalAuthInfo = GetLocalAuthInfo -}; - -static AuthMsgService *GetInstance(const char *packageName) +static int32_t ConvertJsonToAddr(const CJson *json, ConnectionAddr *addr) { - (void)packageName; - return &g_softBusInstance; + if (IsEthChannel(json)) { + int ethPort = 0; + if (GetIntFromJson(json, ETH_PORT, ðPort) != HC_SUCCESS) { + return HC_ERR_INVALID_PARAMS; + } + const char *ethIp = GetStringFromJson(json, ETH_IP); + if (memcpy_s(addr->info.ip.ip, IP_STR_MAX_LEN, ethIp, HcStrlen(ethIp) + 1) != EOK) { + return HC_ERR_MEMORY_COPY; + } + addr->info.ip.port = (uint16_t)ethPort; + addr->type = CONNECTION_ADDR_ETH; + return HC_SUCCESS; + } else if (IsWlanChannel(json)) { + int wlanPort = 0; + if (GetIntFromJson(json, WLAN_PORT, &wlanPort) != HC_SUCCESS) { + return HC_ERR_INVALID_PARAMS; + } + const char *wlanIp = GetStringFromJson(json, WLAN_IP); + if (memcpy_s(addr->info.ip.ip, IP_STR_MAX_LEN, wlanIp, HcStrlen(wlanIp) + 1) != EOK) { + return HC_ERR_MEMORY_COPY; + } + addr->info.ip.port = (uint16_t)wlanPort; + addr->type = CONNECTION_ADDR_WLAN; + return HC_SUCCESS; + } else if(IsBrChannel(json)) { + const char *brMac = GetStringFromJson(json, BR_MAC); + if (memcpy_s(addr->info.br.brMac, BT_MAC_LEN, brMac, HcStrlen(brMac) + 1) != EOK) { + return HC_ERR_MEMORY_COPY; + } + addr->type = CONNECTION_ADDR_BR; + return HC_SUCCESS; + } else if(IsBleChannel(json)) { + const char *bleMac = GetStringFromJson(json, BLE_MAC); + if (memcpy_s(addr->info.ble.bleMac, BT_MAC_LEN, bleMac, HcStrlen(bleMac) + 1) != EOK) { + return HC_ERR_MEMORY_COPY; + } + addr->type = CONNECTION_ADDR_BLE; + return HC_SUCCESS; + } else { + LOGE("The connectParams is invalid!"); + return HC_ERR_INVALID_PARAMS; + } } static int32_t OpenSoftBusChannel(const char *connectParams, int64_t requestId, int64_t *returnChannelId) { - if (returnChannelId == NULL) { - LOGE("The address of the input returnChannelId is NULL!"); + if ((connectParams == NULL) || (returnChannelId == NULL)) { + LOGE("The input connectParams or returnChannelId is NULL!"); return HC_ERR_NULL_PTR; } - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return HC_ERR_SOFT_BUS; + ConnectionAddr addr; + CJson *json = CreateJsonFromString(connectParams); + if (json == NULL) { + LOGE("Failed to create json from string!"); + return HC_ERR_JSON_CREATE; } - LOGI("Ready to open the soft bus channel!"); - int64_t channelId = instance->openAuthChannel(GROUP_MANAGER_PACKAGE_NAME, connectParams, requestId); + int32_t res = ConvertJsonToAddr(json, &addr); + FreeJson(json); + if (res != HC_SUCCESS) { + return res; + } + LOGD("[SoftBus][In]: OpenChannel!"); + int64_t channelId = (int64_t)OpenAuthSession(GROUP_MANAGER_PACKAGE_NAME, &addr); + LOGD("[SoftBus][Out]: OpenChannel! [channelId]: %lld", channelId); /* If the value of channelId is less than 0, the soft bus fails to open the channel */ if (channelId < 0) { LOGE("Failed to open soft bus channel!"); return HC_ERR_SOFT_BUS; } - LOGI("Open soft bus channel successfully!"); + res = AddChannelEntry(requestId, channelId); + if (res != HC_SUCCESS) { + return res; + } *returnChannelId = channelId; return HC_SUCCESS; } static void CloseSoftBusChannel(int64_t channelId) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return; - } - LOGI("Ready to close the soft bus channel!"); - int32_t result = instance->closeChannel(channelId); - if (result != HC_SUCCESS) { - LOGE("Failed to close soft bus channel!"); - } - LOGI("Close soft bus channel success!"); + RemoveChannelEntry(channelId); + LOGD("[SoftBus][In]: CloseSession!"); + CloseSession(channelId); + LOGD("[SoftBus][Out]: CloseSession!"); } static int32_t SendSoftBusMsg(int64_t channelId, const uint8_t *data, uint32_t dataLen) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); + LOGD("[SoftBus][In]: SendMsg!"); + int32_t res = SendBytes(channelId, data, dataLen); + LOGD("[SoftBus][Out]: SendMsg! res: %d", res); + if (res != HC_SUCCESS) { + LOGE("An error occurs when the softbus sends data!"); return HC_ERR_SOFT_BUS; } - LOGI("The soft bus channel starts to send data!"); - int32_t result = instance->sendMsg(channelId, data, dataLen); - if (result != HC_SUCCESS) { - LOGE("An error occurs when the soft bus channel sends data!"); - return HC_ERR_SOFT_BUS; - } - LOGI("The soft bus channel sends data successfully!"); return HC_SUCCESS; } -static void SetSoftBusAuthResult(int64_t channelId) +static void NotifySoftBusBindResult(int64_t channelId) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return; - } - LOGI("Prepare to notify the soft bus authentication result!"); - int32_t result = instance->setAuthResult(channelId, NULL, 0); - if (result != HC_SUCCESS) { - LOGE("Failed to notify the soft bus of the authentication result!"); - return; - } - LOGI("The soft bus is successfully notified of the authentication result!"); -} - -static char *GetSoftBusLocalAuthInfo() -{ - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return ""; - } - return instance->getLocalAuthInfo(); + LOGD("[SoftBus][In]: NotifyAuthSuccess!"); + // NotifyAuthSuccess(channelId); + LOGD("[SoftBus][Out]: NotifyAuthSuccess!"); } SoftBus g_softBus = { .openChannel = OpenSoftBusChannel, .closeChannel = CloseSoftBusChannel, .sendMsg = SendSoftBusMsg, - .setAuthResult = SetSoftBusAuthResult, - .getLocalAuthInfo = GetSoftBusLocalAuthInfo + .notifyResult = NotifySoftBusBindResult, }; -int32_t InitSoftBusChannelModule(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()) +int32_t InitSoftBusChannelModule(void) { - IAuthMsgListener softBusListener = { - .onChannelOpened = onChannelOpened, - .onChannelClosed = onChannelClosed, - .onMsgReceived = onMsgReceived, - .onServiceDied = onServiceDied + if (g_channelMutex == NULL) { + g_channelMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0); + if (g_channelMutex == NULL) { + LOGE("Failed to allocate channel mutex memory!"); + return HC_ERR_ALLOC_MEMORY; + } + if (InitHcMutex(g_channelMutex) != HC_SUCCESS) { + LOGE("Init mutex failed!"); + HcFree(g_channelMutex); + g_channelMutex = NULL; + return HC_ERR_INIT_FAILED; + } + } + g_channelVec = CREATE_HC_VECTOR(ChannelEntryVec) + ISessionListener softBusListener = { + .OnSessionOpened = OnChannelOpenedCb, + .OnSessionClosed = NULL, + .OnBytesReceived = OnBytesReceivedCb, + .OnMessageReceived = NULL }; - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGE("Failed to get soft bus instance!"); - return HC_ERR_SOFT_BUS; - } - LOGI("Prepare to add listener callback to soft bus service!"); - int32_t result = instance->addListener(GROUP_MANAGER_PACKAGE_NAME, &softBusListener); - if (result != HC_SUCCESS) { - LOGE("Failed to add listener callback to soft bus service!"); - return HC_ERR_SOFT_BUS; - } - LOGI("Add listener callback to soft bus service successfully!"); - return HC_SUCCESS; + LOGD("[SoftBus][In]: CreateSessionServer!"); + int32_t res = CreateSessionServer(GROUP_MANAGER_PACKAGE_NAME, GROUP_MANAGER_PACKAGE_NAME, &softBusListener); + LOGD("[SoftBus][Out]: CreateSessionServer! res: %d", res); + return res; } -void DestroySoftBusChannelModule() +void DestroySoftBusChannelModule(void) { - AuthMsgService *instance = GetInstance(GROUP_MANAGER_PACKAGE_NAME); - if (instance == NULL) { - LOGI("Failed to get soft bus instance!"); - return; + g_channelMutex->lock(g_channelMutex); + DESTROY_HC_VECTOR(ChannelEntryVec, &g_channelVec) + g_channelMutex->unlock(g_channelMutex); + if (g_channelMutex != NULL) { + DestroyHcMutex(g_channelMutex); + HcFree(g_channelMutex); + g_channelMutex = NULL; } - LOGI("Prepare to remove listener callback from soft bus service!"); - int32_t result = instance->removeListener(GROUP_MANAGER_PACKAGE_NAME); - if (result != HC_SUCCESS) { - LOGI("Failed to remove listener callback from soft bus module!"); - } - LOGI("Remove listener callback from soft bus service successfully!"); + LOGD("[SoftBus][In]: RemoveSessionServer!"); + int32_t res = RemoveSessionServer(GROUP_MANAGER_PACKAGE_NAME, GROUP_MANAGER_PACKAGE_NAME); + LOGD("[SoftBus][Out]: RemoveSessionServer! res: %d", res); } -SoftBus *GetSoftBusInstance() +SoftBus *GetSoftBusInstance(void) { return &g_softBus; } -bool IsSoftBusChannelSupported() +bool IsSoftBusChannelSupported(void) { return true; } \ No newline at end of file diff --git a/services/common/src/channel_manager/soft_bus_channel_mock/soft_bus_channel_mock.c b/services/common/src/channel_manager/soft_bus_channel_mock/soft_bus_channel_mock.c index c8675b3..b7135d4 100644 --- a/services/common/src/channel_manager/soft_bus_channel_mock/soft_bus_channel_mock.c +++ b/services/common/src/channel_manager/soft_bus_channel_mock/soft_bus_channel_mock.c @@ -14,28 +14,25 @@ */ #include "soft_bus_channel.h" -#include "device_auth_defines.h" -#include "hc_types.h" -int32_t InitSoftBusChannelModule(void (*onChannelOpened)(int64_t, int64_t, const char *, uint32_t, bool), - void (*onChannelClosed)(int64_t, int64_t), - void (*onMsgReceived)(int64_t, const uint8_t *, uint32_t), - void (*onServiceDied)()) +#include "device_auth_defines.h" + +int32_t InitSoftBusChannelModule(void) { return HC_SUCCESS; } -void DestroySoftBusChannelModule() +void DestroySoftBusChannelModule(void) { return; } -SoftBus *GetSoftBusInstance() +SoftBus *GetSoftBusInstance(void) { return NULL; } -bool IsSoftBusChannelSupported() +bool IsSoftBusChannelSupported(void) { return false; } \ No newline at end of file diff --git a/services/common/src/task_manager/task_manager.c b/services/common/src/task_manager/task_manager.c new file mode 100644 index 0000000..279f837 --- /dev/null +++ b/services/common/src/task_manager/task_manager.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "task_manager.h" + +#include "device_auth_defines.h" +#include "hc_log.h" + +#define STACK_SIZE 4096 + +static HcTaskThread *g_taskThread = NULL; + +int32_t PushTask(HcTaskBase *baseTask) +{ + if (g_taskThread == NULL) { + LOGE("Task thread is NULL!"); + return HC_ERR_NULL_PTR; + } + g_taskThread->pushTask(g_taskThread, baseTask); + return HC_SUCCESS; +} + +int32_t InitTaskManager(void) +{ + if (g_taskThread != NULL) { + LOGD("Task thread is running!"); + return HC_SUCCESS; + } + g_taskThread = (HcTaskThread *)HcMalloc(sizeof(HcTaskThread), 0); + if (g_taskThread == NULL) { + return HC_ERR_ALLOC_MEMORY; + } + int32_t res = InitHcTaskThread(g_taskThread, STACK_SIZE, "HichainThread"); + if (res != HC_SUCCESS) { + LOGE("Failed to init task thread! res: %d", res); + HcFree(g_taskThread); + g_taskThread = NULL; + return HC_ERR_INIT_FAILED; + } + res = g_taskThread->startThread(g_taskThread); + if (res != HC_SUCCESS) { + DestroyHcTaskThread(g_taskThread); + HcFree(g_taskThread); + g_taskThread = NULL; + LOGE("Failed to start thread! res: %d", res); + return HC_ERR_INIT_FAILED; + } + return HC_SUCCESS; +} + +void DestroyTaskManager(void) +{ + if (g_taskThread != NULL) { + g_taskThread->stopAndClear(g_taskThread); + DestroyHcTaskThread(g_taskThread); + HcFree(g_taskThread); + g_taskThread = NULL; + } +} \ No newline at end of file diff --git a/services/device_auth.c b/services/device_auth.c index 0a0693e..66c5551 100644 --- a/services/device_auth.c +++ b/services/device_auth.c @@ -14,23 +14,21 @@ */ #include "device_auth.h" + #include "alg_loader.h" #include "callback_manager.h" #include "channel_manager.h" #include "common_util.h" -#include "database_manager.h" #include "dev_auth_module_manager.h" #include "device_auth_defines.h" #include "group_auth_manager.h" #include "group_manager.h" #include "hc_init_protection.h" #include "hc_log.h" -#include "hc_task_thread.h" #include "json_utils.h" #include "securec.h" #include "session_manager.h" - -#define STACK_SIZE 4096 +#include "task_manager.h" typedef struct { HcTaskBase base; @@ -41,7 +39,6 @@ typedef struct { static GroupAuthManager *g_groupAuthManager = NULL; static DeviceGroupManager *g_groupManagerInstance = NULL; -static HcTaskThread *g_taskThread = NULL; static int32_t BindCallbackToTask(GroupManagerTask *task, const CJson *jsonParams) { @@ -68,42 +65,6 @@ static int32_t BindCallbackToTask(GroupManagerTask *task, const CJson *jsonParam return HC_SUCCESS; } -static char *GenerateRecvData(int64_t channelId, const uint8_t *data, uint32_t dataLen, int64_t *requestId) -{ - char *dataStr = (char *)HcMalloc(dataLen + 1, 0); - if (dataStr == NULL) { - LOGE("Failed to allocate dataStr memory!"); - return NULL; - } - if (memcpy_s(dataStr, dataLen + 1, data, dataLen) != HC_SUCCESS) { - LOGE("Failed to copy data!"); - HcFree(dataStr); - return NULL; - } - CJson *receivedData = CreateJsonFromString(dataStr); - HcFree(dataStr); - if (receivedData == NULL) { - LOGE("Failed to create receivedData json object from string!"); - return NULL; - } - if (GetInt64FromJson(receivedData, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) { - LOGE("Failed to get requestId from receivedData!"); - return NULL; - } - if (AddByteToJson(receivedData, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { - LOGE("Failed to add channelId to recvData!"); - FreeJson(receivedData); - return NULL; - } - char *receivedDataStr = PackJsonToString(receivedData); - FreeJson(receivedData); - if (receivedDataStr == NULL) { - LOGE("Failed to convert json to string!"); - return NULL; - } - return receivedDataStr; -} - static void DoCreateGroup(HcTaskBase *task) { if (task == NULL) { @@ -195,24 +156,6 @@ static void DoConfirmRequest(HcTaskBase *task) OnConfirmationReceived(realTask->requestId, realTask->jsonParams); } -static void OnChannelOpenedAction(HcTaskBase *task) -{ - if (task == NULL) { - LOGE("The input task is NULL!"); - return; - } - GroupManagerTask *realTask = (GroupManagerTask *)task; - LOGI("The task thread starts to execute the task that needs to be executed" - "when the soft bus channel is open! [RequestId]: %" PRId64, realTask->requestId); - CJson *jsonParams = realTask->jsonParams; - int64_t channelId = DEFAULT_CHANNEL_ID; - if (GetByteFromJson(jsonParams, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { - LOGE("Failed to get channelId from json!"); - return; - } - OnChannelOpened(realTask->requestId, channelId); -} - static void DoProcessBindData(HcTaskBase *task) { if (task == NULL) { @@ -307,14 +250,6 @@ static void InitProcessBindDataTask(GroupManagerTask *task, int64_t requestId, C task->jsonParams = jsonParams; } -static void InitChannelOpenedTask(GroupManagerTask *task, int64_t requestId, CJson *jsonParams) -{ - task->base.doAction = OnChannelOpenedAction; - task->base.destroy = DestroyGroupManagerTask; - task->requestId = requestId; - task->jsonParams = jsonParams; -} - static int32_t InitCreateGroupTask(GroupManagerTask *task, int64_t requestId, CJson *jsonParams) { task->base.doAction = DoCreateGroup; @@ -367,10 +302,6 @@ static int32_t AuthDevice(int64_t authReqId, const char *authParams, const Devic LOGE("The input auth params is null!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("The task thread is null!"); - return HC_ERR_SERVICE_NEED_RESTART; - } CJson *jsonParams = CreateJsonFromString(authParams); if (jsonParams == NULL) { LOGE("Create json from params failed!"); @@ -388,7 +319,11 @@ static int32_t AuthDevice(int64_t authReqId, const char *authParams, const Devic HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("Push AuthDevice task successfully."); return HC_SUCCESS; } @@ -401,10 +336,6 @@ static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t data LOGE("Invalid input for ProcessData!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("The task thread is null!"); - return HC_ERR_SERVICE_NEED_RESTART; - } CJson *receivedData = CreateJsonFromString((const char *)data); if (receivedData == NULL) { LOGE("Create Json for input data failed!"); @@ -422,7 +353,11 @@ static int32_t ProcessData(int64_t authReqId, const uint8_t *data, uint32_t data HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(receivedData); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("Push ProcessData task successfully."); return HC_SUCCESS; } @@ -518,16 +453,11 @@ static int32_t UnRegGroupManagerCallback(const char *appId) static int32_t RequestCreateGroup(int64_t requestId, const char *appId, const char *createParams) { - LOGI("[Start]: RequestCreateGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (createParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestCreateGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonCreateParams = CreateJsonFromString(createParams); if (jsonCreateParams == NULL) { LOGE("Failed to create json from string!"); @@ -550,23 +480,22 @@ static int32_t RequestCreateGroup(int64_t requestId, const char *appId, const ch HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonCreateParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the creating group task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestDeleteGroup(int64_t requestId, const char *appId, const char *disbandParams) { - LOGI("[Start]: RequestDeleteGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (disbandParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestDeleteGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonDisBandParams = CreateJsonFromString(disbandParams); if (jsonDisBandParams == NULL) { LOGE("Failed to create json from string!"); @@ -589,23 +518,22 @@ static int32_t RequestDeleteGroup(int64_t requestId, const char *appId, const ch HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase *)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonDisBandParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the deleting group task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestAddMemberToGroup(int64_t requestId, const char *appId, const char *addParams) { - LOGI("[Start]: RequestAddMemberToGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (addParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestAddMemberToGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonAddParams = CreateJsonFromString(addParams); if (jsonAddParams == NULL) { LOGE("Failed to create json from string!"); @@ -628,23 +556,22 @@ static int32_t RequestAddMemberToGroup(int64_t requestId, const char *appId, con HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonAddParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the adding member task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestDeleteMemberFromGroup(int64_t requestId, const char *appId, const char *deleteParams) { - LOGI("[Start]: RequestDeleteMemberFromGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (deleteParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestDeleteMemberFromGroup! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonDeleteParams = CreateJsonFromString(deleteParams); if (jsonDeleteParams == NULL) { LOGE("Failed to create json from string!"); @@ -667,23 +594,22 @@ static int32_t RequestDeleteMemberFromGroup(int64_t requestId, const char *appId HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonDeleteParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the deleting member task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestProcessBindData(int64_t requestId, const uint8_t *data, uint32_t dataLen) { - LOGI("[Start]: RequestProcessBindData! [RequestId]: %" PRId64, requestId); - if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { + if ((data == NULL) || (dataLen == 0) || (dataLen > MAX_DATA_BUFFER_SIZE)) { LOGE("The input data is invalid!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("The task thread is NULL!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestProcessBindData! [RequestId]: %" PRId64, requestId); CJson *dataJson = CreateJsonFromString((const char *)data); if (dataJson == NULL) { LOGE("Failed to create json from string!"); @@ -698,6 +624,7 @@ static int32_t RequestProcessBindData(int64_t requestId, const uint8_t *data, ui if (tempRequestId != requestId) { LOGE("The requestId transferred by the service is inconsistent with that in the packet! " "[ServiceRequestId]: %" PRId64 ", [RequestId]: %" PRId64, requestId, tempRequestId); + FreeJson(dataJson); return HC_ERR_INVALID_PARAMS; } GroupManagerTask *task = (GroupManagerTask *)HcMalloc(sizeof(GroupManagerTask), 0); @@ -707,23 +634,22 @@ static int32_t RequestProcessBindData(int64_t requestId, const uint8_t *data, ui return HC_ERR_ALLOC_MEMORY; } InitProcessBindDataTask(task, requestId, dataJson); - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(dataJson); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the processing data task successfully! [RequestId]: %" PRId64, requestId); return HC_SUCCESS; } static int32_t ConfirmRequest(int64_t requestId, const char *appId, const char *confirmParams) { - LOGI("[Start]: ConfirmRequest! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (confirmParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: ConfirmRequest! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonConfirmParams = CreateJsonFromString(confirmParams); if (jsonConfirmParams == NULL) { LOGE("Failed to create json from string!"); @@ -731,6 +657,7 @@ static int32_t ConfirmRequest(int64_t requestId, const char *appId, const char * } if (AddStringToJson(jsonConfirmParams, FIELD_APP_ID, appId) != HC_SUCCESS) { LOGE("Failed to add appId to json!"); + FreeJson(jsonConfirmParams); return HC_ERR_JSON_FAIL; } @@ -745,80 +672,16 @@ static int32_t ConfirmRequest(int64_t requestId, const char *appId, const char * HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonConfirmParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the confirming request task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } -static void OnChannelOpenedCallback(int64_t channelId, int64_t requestId, const char *deviceId, - uint32_t deviceIdLen, bool isServer) -{ - if (isServer) { - return; - } - if ((deviceId == NULL) || (deviceIdLen > MAX_DATA_BUFFER_SIZE)) { - LOGE("The input parameters is invalid!"); - return; - } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return; - } - - CJson *jsonParams = CreateJson(); - if (jsonParams == NULL) { - LOGE("Failed to allocate jsonParams memory!"); - return; - } - if (AddByteToJson(jsonParams, FIELD_CHANNEL_ID, (uint8_t *)&channelId, sizeof(int64_t)) != HC_SUCCESS) { - LOGE("Failed to add channelId to jsonParams!"); - FreeJson(jsonParams); - return; - } - - GroupManagerTask *task = (GroupManagerTask *)HcMalloc(sizeof(GroupManagerTask), 0); - if (task == NULL) { - LOGE("Failed to allocate task memory!"); - FreeJson(jsonParams); - return; - } - InitChannelOpenedTask(task, requestId, jsonParams); - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); -} - -static void OnChannelClosedCallback(int64_t channelId, int64_t requestId) -{ - (void)channelId; - (void)requestId; - return; -} - -static void OnMsgReceivedCallback(int64_t channelId, const uint8_t *data, uint32_t dataLen) -{ - LOGI("Receive data from the peer end!"); - if ((data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { - LOGE("The input data is invalid!"); - return; - } - - int64_t requestId = DEFAULT_REQUEST_ID; - char *receivedDataStr = GenerateRecvData(channelId, data, dataLen, &requestId); - if (receivedDataStr == NULL) { - return; - } - if (RequestProcessBindData(DEFAULT_REQUEST_ID, (uint8_t *)receivedDataStr, - (uint32_t)(strlen(receivedDataStr) + 1)) != HC_SUCCESS) { - LOGE("Failed to process bind data from softBus!"); - } - FreeJsonString(receivedDataStr); -} - -static void OnServiceDiedCallback(void) -{ - return; -} - static int32_t GetModuleType(const CJson *jsonParams) { bool isAccountBind = false; @@ -866,10 +729,9 @@ static void DoBindPeer(HcTaskBase *task) GroupManagerTask *realTask = (GroupManagerTask *)task; LOGI("The task thread starts to execute the binding peer device task! [RequestId]: %" PRId64, realTask->requestId); int32_t result; - ChannelType channelType = GetChannelType(realTask->callback); + ChannelType channelType = GetChannelType(realTask->callback, realTask->jsonParams); do { - if ((channelType == NO_CHANNEL) || - (!CanFindValidChannel(channelType, realTask->jsonParams, realTask->callback))) { + if (channelType == NO_CHANNEL) { LOGE("No available channels found!"); result = HC_ERR_CHANNEL_NOT_EXIST; break; @@ -901,10 +763,9 @@ static void DoUnbindPeer(HcTaskBase *task) LOGI("The task thread starts to execute the unbinding peer device task! [RequestId]: %" PRId64, realTask->requestId); int32_t result; - ChannelType channelType = GetChannelType(realTask->callback); + ChannelType channelType = GetChannelType(realTask->callback, realTask->jsonParams); do { - if ((channelType == NO_CHANNEL) || - (!CanFindValidChannel(channelType, realTask->jsonParams, realTask->callback))) { + if (channelType == NO_CHANNEL) { LOGE("No available channels found!"); result = HC_ERR_CHANNEL_NOT_EXIST; break; @@ -935,10 +796,9 @@ static void DoAuthAgreeKey(HcTaskBase *task) GroupManagerTask *realTask = (GroupManagerTask *)task; LOGI("The task thread starts to execute the key agreement task! [RequestId]: %" PRId64, realTask->requestId); int32_t result; - ChannelType channelType = GetChannelType(realTask->callback); + ChannelType channelType = GetChannelType(realTask->callback, realTask->jsonParams); do { - if ((channelType == NO_CHANNEL) || - (!CanFindValidChannel(channelType, realTask->jsonParams, realTask->callback))) { + if (channelType == NO_CHANNEL) { LOGE("No available channels found!"); result = HC_ERR_CHANNEL_NOT_EXIST; break; @@ -1048,16 +908,11 @@ static int32_t AddLiteDataToReceivedData(CJson *receivedData, int64_t requestId, static int32_t RequestProcessLiteData(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen) { - LOGI("[Start]: RequestProcessLiteData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { LOGE("The input data is NULL or dataLen is beyond max size!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestProcessLiteData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *receivedData = CreateJsonFromString((const char *)data); if (receivedData == NULL) { LOGE("Failed to create received json object from string!"); @@ -1079,7 +934,11 @@ static int32_t RequestProcessLiteData(int64_t requestId, const char *appId, cons HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(receivedData); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the processing lite data task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1087,16 +946,11 @@ static int32_t RequestProcessLiteData(int64_t requestId, const char *appId, cons static int32_t RequestBindPeer(int64_t requestId, const char *appId, const char *bindParams) { - LOGI("[Start]: RequestBindPeer! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (bindParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestBindPeer! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonBindParams = CreateJsonFromString(bindParams); if (jsonBindParams == NULL) { LOGE("Failed to create json from string!"); @@ -1119,7 +973,11 @@ static int32_t RequestBindPeer(int64_t requestId, const char *appId, const char HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonBindParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the binding peer device task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1127,16 +985,11 @@ static int32_t RequestBindPeer(int64_t requestId, const char *appId, const char static int32_t RequestUnbindPeer(int64_t requestId, const char *appId, const char *unBindParams) { - LOGI("[Start]: RequestUnbindPeer, [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (unBindParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestUnbindPeer, [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonUnbindParams = CreateJsonFromString(unBindParams); if (jsonUnbindParams == NULL) { LOGE("Failed to create json from string!"); @@ -1159,7 +1012,11 @@ static int32_t RequestUnbindPeer(int64_t requestId, const char *appId, const cha HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonUnbindParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the unbinding peer device task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1167,16 +1024,11 @@ static int32_t RequestUnbindPeer(int64_t requestId, const char *appId, const cha static int32_t RequestAuthKeyAgree(int64_t requestId, const char *appId, const char *agreeParams) { - LOGI("[Start]: RequestAuthKeyAgree! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (agreeParams == NULL)) { LOGE("The input parameters contains NULL value!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestAuthKeyAgree! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *jsonAgreeParams = CreateJsonFromString(agreeParams); if (jsonAgreeParams == NULL) { LOGE("Failed to create json from string!"); @@ -1199,23 +1051,22 @@ static int32_t RequestAuthKeyAgree(int64_t requestId, const char *appId, const c HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(jsonAgreeParams); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the key agreement task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; } static int32_t RequestProcessKeyAgreeData(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen) { - LOGI("[Start]: RequestProcessKeyAgreeData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); if ((appId == NULL) || (data == NULL) || (dataLen > MAX_DATA_BUFFER_SIZE)) { LOGE("The input data is NULL or dataLen is beyond max size!"); return HC_ERR_INVALID_PARAMS; } - if (g_taskThread == NULL) { - LOGE("Uninitialized task thread!"); - return HC_ERR_SERVICE_NEED_RESTART; - } - + LOGI("[Start]: RequestProcessKeyAgreeData! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); CJson *receivedData = CreateJsonFromString((const char *)data); if (receivedData == NULL) { LOGE("Failed to create received json object from string!"); @@ -1237,7 +1088,11 @@ static int32_t RequestProcessKeyAgreeData(int64_t requestId, const char *appId, HcFree(task); return HC_ERR_INIT_TASK_FAIL; } - g_taskThread->pushTask(g_taskThread, (HcTaskBase*)task); + if (PushTask((HcTaskBase*)task) != HC_SUCCESS) { + FreeJson(receivedData); + HcFree(task); + return HC_ERR_INIT_TASK_FAIL; + } LOGI("[End]: Create the processing key agreement data task successfully! [AppId]: %s, [RequestId]: %" PRId64, appId, requestId); return HC_SUCCESS; @@ -1605,33 +1460,6 @@ static int32_t InitAlgorithm() return res; } -static int InitAndStartThread() -{ - if (g_taskThread != NULL) { - LOGD("Task thread is not null"); - } - g_taskThread = HcMalloc(sizeof(HcTaskThread), 0); - if (g_taskThread == NULL) { - return HC_ERR_ALLOC_MEMORY; - } - int32_t res = InitHcTaskThread(g_taskThread, STACK_SIZE, "HichainThread"); - if (res != HC_SUCCESS) { - LOGE("Init task thread failed, res:%d", res); - HcFree(g_taskThread); - g_taskThread = NULL; - return HC_ERROR; - } - res = g_taskThread->startThread(g_taskThread); - if (res != HC_SUCCESS) { - DestroyHcTaskThread(g_taskThread); - HcFree(g_taskThread); - g_taskThread = NULL; - LOGE("Start thread failed, res:%d", res); - return HC_ERROR; - } - return res; -} - static void DestroyGmAndGa() { if (g_groupAuthManager != NULL) { @@ -1644,21 +1472,11 @@ static void DestroyGmAndGa() } } -static void StopAndDestroyThread() -{ - if (g_taskThread != NULL) { - g_taskThread->stopAndClear(g_taskThread); - DestroyHcTaskThread(g_taskThread); - HcFree(g_taskThread); - g_taskThread = NULL; - } -} - DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService() { LOGI("[Service]: Start to init device auth service!"); if (CheckInit() == FINISH_INIT) { - LOGI("[End]: [Service]: Init device auth service successfully!"); + LOGI("[End]: [Service]: Device auth service is running!"); return HC_SUCCESS; } int32_t res = InitAlgorithm(); @@ -1680,14 +1498,13 @@ DEVICE_AUTH_API_PUBLIC int InitDeviceAuthService() if (res != HC_SUCCESS) { goto free_module; } - res = InitChannelManager(OnChannelOpenedCallback, - OnChannelClosedCallback, OnMsgReceivedCallback, OnServiceDiedCallback); + res = InitChannelManager(); if (res != HC_SUCCESS) { LOGE("[End]: [Service]: Failed to init channel manage module!"); goto free_group_manager; } InitSessionManager(); - res = InitAndStartThread(); + res = InitTaskManager(); if (res != HC_SUCCESS) { LOGE("[End]: [Service]: Failed to init worker thread!"); goto free_all; @@ -1716,12 +1533,12 @@ DEVICE_AUTH_API_PUBLIC void DestroyDeviceAuthService() LOGI("[End]: [Service]: The service has not been initialized, so it does not need to be destroyed!"); return; } + DestroyTaskManager(); DestroyChannelManager(); DestroySessionManager(); DestroyGroupManager(); DestroyModules(); DestroyGmAndGa(); - StopAndDestroyThread(); SetDeInitStatus(); LOGI("[End]: [Service]: Destroy device auth service successfully!"); } @@ -1748,7 +1565,7 @@ DEVICE_AUTH_API_PUBLIC const DeviceGroupManager *GetGmInstance() g_groupManagerInstance->processKeyAgreeData = RequestProcessKeyAgreeData; g_groupManagerInstance->processCredential = ProcessCredential; g_groupManagerInstance->getRegisterInfo = GetRegisterInfo; - g_groupManagerInstance->getLocalConnectInfo = GetLocalConnectInfo; + g_groupManagerInstance->getLocalConnectInfo = NULL; g_groupManagerInstance->addGroupManager = AddGroupManagerIfAccessible; g_groupManagerInstance->deleteGroupManager = DeleteGroupManagerIfAccessible; g_groupManagerInstance->getGroupManagers = GetGroupManagersIfAccessible; diff --git a/services/deviceauth.gni b/services/deviceauth.gni index 437f6f6..8e23a3e 100644 --- a/services/deviceauth.gni +++ b/services/deviceauth.gni @@ -22,6 +22,7 @@ inc_path = [ "${services_path}/common/inc/channel_manager", "${services_path}/common/inc/channel_manager/soft_bus_channel", "${services_path}/common/inc/data_base", + "${services_path}/common/inc/task_manager", "${services_path}/group_auth/inc", "${services_path}/group_auth/inc/account_unrelated_group_auth", "${services_path}/group_auth/inc/account_related_group_auth", @@ -56,8 +57,9 @@ deviceauth_files = [ "${services_path}/common/src/broadcast_manager/broadcast_manager.c", "${services_path}/common/src/callback_manager/callback_manager.c", "${services_path}/common/src/channel_manager/channel_manager.c", - "${services_path}/common/src/channel_manager/soft_bus_channel_mock/soft_bus_channel_mock.c", + "${services_path}/common/src/channel_manager/soft_bus_channel/soft_bus_channel.c", "${services_path}/common/src/data_base/database_manager.c", + "${services_path}/common/src/task_manager/task_manager.c", "${services_path}/group_auth/src/group_auth_manager/group_auth_manager.c", "${services_path}/group_auth/src/group_auth_manager/account_unrelated_group_auth/account_unrelated_group_auth.c", diff --git a/services/group_auth/inc/group_auth_common_defines.h b/services/group_auth/inc/group_auth_common_defines.h index 97d276f..0d863c8 100644 --- a/services/group_auth/inc/group_auth_common_defines.h +++ b/services/group_auth/inc/group_auth_common_defines.h @@ -21,7 +21,6 @@ #include "hc_task_thread.h" #include "json_utils.h" -#define MAX_DATA_BUFFER_SIZE 4096 #define MAX_UDID_LEN 64 typedef struct { 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 b109de0..d7fed7d 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 @@ -380,7 +380,7 @@ static CJson *GenerateGroupErrorMsg(int32_t errorCode, int64_t requestId) static void InformPeerProcessError(int64_t requestId, const CJson *jsonParams, const DeviceAuthCallback *callback, int32_t errorCode) { - ChannelType channelType = GetChannelType(callback); + ChannelType channelType = GetChannelType(callback, jsonParams); int64_t channelId = DEFAULT_CHANNEL_ID; if ((channelType == NO_CHANNEL) || ((channelType == SOFT_BUS) && @@ -535,8 +535,8 @@ static int32_t AddMemberToGroup(int64_t requestId, CJson *jsonParams, const Devi if (result != HC_SUCCESS) { break; } - ChannelType channelType = GetChannelType(callback); - if ((channelType == NO_CHANNEL) || (!CanFindValidChannel(channelType, jsonParams, callback))) { + ChannelType channelType = GetChannelType(callback, jsonParams); + if (channelType == NO_CHANNEL) { LOGE("No available channels found!"); result = HC_ERR_CHANNEL_NOT_EXIST; break; @@ -579,8 +579,8 @@ static int32_t DeleteMemberFromGroupInner(int64_t requestId, CJson *jsonParams, LOGI("The service requires that the device be forcibly unbound locally instead of being unbound online!"); return HandleLocalUnbind(requestId, jsonParams, callback); } - ChannelType channelType = GetChannelType(callback); - if ((channelType == NO_CHANNEL) || (!CanFindValidChannel(channelType, jsonParams, callback))) { + ChannelType channelType = GetChannelType(callback, jsonParams); + if (channelType == NO_CHANNEL) { LOGI("No available channels found!"); if (isForceDelete) { LOGI("The peer device is forcibly unbound because no available channel is found!"); diff --git a/services/session/src/bind_session/bind_session_client.c b/services/session/src/bind_session/bind_session_client.c index 516786e..a2a4b3b 100644 --- a/services/session/src/bind_session/bind_session_client.c +++ b/services/session/src/bind_session/bind_session_client.c @@ -143,9 +143,9 @@ static int32_t PrepareClient(const CJson *jsonParams, BindSession *session) return OpenChannel(session->channelType, jsonParams, session->requestId, &session->channelId); } -static void InitClientChannel(const DeviceAuthCallback *callback, BindSession *session) +static void InitClientChannel(const DeviceAuthCallback *callback, const CJson *jsonParams, BindSession *session) { - session->channelType = GetChannelType(callback); + session->channelType = GetChannelType(callback, jsonParams); } Session *CreateClientBindSession(CJson *jsonParams, const DeviceAuthCallback *callback) @@ -185,7 +185,7 @@ Session *CreateClientBindSession(CJson *jsonParams, const DeviceAuthCallback *ca return NULL; } InitBindSession(TYPE_CLIENT_BIND_SESSION, operationCode, requestId, callback, session); - InitClientChannel(callback, session); + InitClientChannel(callback, jsonParams, session); /* The client bind session needs to receive a message indicating that the channel is open. */ session->onChannelOpened = OnBindChannelOpened; diff --git a/services/session/src/bind_session_lite/bind_session_client_lite.c b/services/session/src/bind_session_lite/bind_session_client_lite.c index da15700..b838bdc 100644 --- a/services/session/src/bind_session_lite/bind_session_client_lite.c +++ b/services/session/src/bind_session_lite/bind_session_client_lite.c @@ -65,9 +65,9 @@ static int32_t LitePrepareClient(const CJson *jsonParams, LiteBindSession *sessi return OpenChannel(session->channelType, jsonParams, session->requestId, &session->channelId); } -static void InitClientChannel(const DeviceAuthCallback *callback, LiteBindSession *session) +static void InitClientChannel(const DeviceAuthCallback *callback, const CJson *jsonParams, LiteBindSession *session) { - session->channelType = GetChannelType(callback); + session->channelType = GetChannelType(callback, jsonParams); } Session *CreateLiteClientBindSession(CJson *jsonParams, const DeviceAuthCallback *callback) @@ -91,7 +91,7 @@ Session *CreateLiteClientBindSession(CJson *jsonParams, const DeviceAuthCallback return NULL; } InitLiteBindSession(TYPE_CLIENT_BIND_SESSION_LITE, operationCode, requestId, session, callback); - InitClientChannel(callback, session); + InitClientChannel(callback, jsonParams, session); /* The client bind session needs to receive a message indicating that the channel is open. */ session->onChannelOpened = OnLiteBindChannelOpened; diff --git a/services/session/src/bind_session_lite/bind_session_common_lite.c b/services/session/src/bind_session_lite/bind_session_common_lite.c index cf9e042..3309bf0 100644 --- a/services/session/src/bind_session_lite/bind_session_common_lite.c +++ b/services/session/src/bind_session_lite/bind_session_common_lite.c @@ -342,7 +342,7 @@ void InitLiteBindSession(int bindType, int operationCode, int64_t requestId, Lit session->base.type = bindType; session->operationCode = operationCode; session->requestId = requestId; - session->channelType = GetChannelType(callback); + session->channelType = NO_CHANNEL; session->channelId = DEFAULT_CHANNEL_ID; session->isWaiting = false; session->params = NULL; From 3e8d142af9d9d0e0253dd856be1b0cead2d907f0 Mon Sep 17 00:00:00 2001 From: fuzikun Date: Wed, 1 Sep 2021 17:11:05 +0800 Subject: [PATCH 4/7] use HcSendMsg instance of SendMsg interface Signed-off-by: fuzikun --- services/common/inc/channel_manager/channel_manager.h | 2 +- services/common/src/channel_manager/channel_manager.c | 2 +- .../account_unrelated/peer_to_peer_group/peer_to_peer_group.c | 2 +- services/session/src/bind_session/bind_session_common.c | 2 +- .../session/src/bind_session_lite/bind_session_common_lite.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/services/common/inc/channel_manager/channel_manager.h b/services/common/inc/channel_manager/channel_manager.h index 5be60fb..2ba41e0 100644 --- a/services/common/inc/channel_manager/channel_manager.h +++ b/services/common/inc/channel_manager/channel_manager.h @@ -31,7 +31,7 @@ void DestroyChannelManager(void); ChannelType GetChannelType(const DeviceAuthCallback *callback, const CJson *jsonParams); int32_t OpenChannel(ChannelType channelType, const CJson *jsonParams, int64_t requestId, int64_t *returnChannelId); void CloseChannel(ChannelType channelType, int64_t channelId); -int32_t SendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, +int32_t HcSendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, const DeviceAuthCallback *callback, const char *data); void SetAuthResult(ChannelType channelType, int64_t channelId); int32_t GetLocalConnectInfo(char *jsonAddrInfo, int32_t bufLen); diff --git a/services/common/src/channel_manager/channel_manager.c b/services/common/src/channel_manager/channel_manager.c index 922c0b7..f0f8592 100644 --- a/services/common/src/channel_manager/channel_manager.c +++ b/services/common/src/channel_manager/channel_manager.c @@ -88,7 +88,7 @@ void CloseChannel(ChannelType channelType, int64_t channelId) } } -int32_t SendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, +int32_t HcSendMsg(ChannelType channelType, int64_t requestId, int64_t channelId, const DeviceAuthCallback *callback, const char *data) { if (channelType == SERVICE_CHANNEL) { 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 d7fed7d..83d26d8 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 @@ -398,7 +398,7 @@ static void InformPeerProcessError(int64_t requestId, const CJson *jsonParams, c LOGE("An error occurred when converting json to string!"); return; } - (void)SendMsg(channelType, requestId, channelId, callback, errorDataStr); + (void)HcSendMsg(channelType, requestId, channelId, callback, errorDataStr); FreeJsonString(errorDataStr); } diff --git a/services/session/src/bind_session/bind_session_common.c b/services/session/src/bind_session/bind_session_common.c index 352a046..4fce519 100644 --- a/services/session/src/bind_session/bind_session_common.c +++ b/services/session/src/bind_session/bind_session_common.c @@ -1255,7 +1255,7 @@ int32_t SendBindSessionData(const BindSession *session, const CJson *sendData) LOGE("An error occurred when converting json to string!"); return HC_ERR_JSON_FAIL; } - int32_t result = SendMsg(session->channelType, session->requestId, + int32_t result = HcSendMsg(session->channelType, session->requestId, session->channelId, session->base.callback, sendDataStr); FreeJsonString(sendDataStr); return result; diff --git a/services/session/src/bind_session_lite/bind_session_common_lite.c b/services/session/src/bind_session_lite/bind_session_common_lite.c index 3309bf0..f219a8e 100644 --- a/services/session/src/bind_session_lite/bind_session_common_lite.c +++ b/services/session/src/bind_session_lite/bind_session_common_lite.c @@ -29,7 +29,7 @@ static int32_t SendLiteBindSessionData(const LiteBindSession *session, const CJs LOGE("An error occurred when converting json to string!"); return HC_ERR_JSON_FAIL; } - int32_t result = SendMsg(session->channelType, session->requestId, + int32_t result = HcSendMsg(session->channelType, session->requestId, session->channelId, session->base.callback, sendDataStr); FreeJsonString(sendDataStr); return result; From cf40435f821f356fdbe1abc35d9ea3a75a233881 Mon Sep 17 00:00:00 2001 From: fuzikun Date: Thu, 2 Sep 2021 09:36:42 +0800 Subject: [PATCH 5/7] change test build.gn Signed-off-by: fuzikun --- test/unittest/deviceauth/BUILD.gn | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/unittest/deviceauth/BUILD.gn b/test/unittest/deviceauth/BUILD.gn index 4790ca9..c0f8249 100644 --- a/test/unittest/deviceauth/BUILD.gn +++ b/test/unittest/deviceauth/BUILD.gn @@ -23,6 +23,9 @@ ohos_unittest("deviceauth_llt") { "./include", "//third_party/json/include", "//utils/native/base/include", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/transport", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", ] include_dirs += inc_path include_dirs += hals_inc_path @@ -60,5 +63,8 @@ ohos_unittest("deviceauth_llt") { "//utils/native/base:utils", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "dsoftbus_standard:softbus_client", + ] } From 13116837ca14b1c859b9263807902a9a4756e726 Mon Sep 17 00:00:00 2001 From: fuzikun Date: Thu, 2 Sep 2021 10:01:26 +0800 Subject: [PATCH 6/7] bug fix: null pointer dereference: ptr Signed-off-by: fuzikun --- services/module/src/das_module/task_main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/module/src/das_module/task_main.c b/services/module/src/das_module/task_main.c index 196148f..80c19d5 100644 --- a/services/module/src/das_module/task_main.c +++ b/services/module/src/das_module/task_main.c @@ -346,8 +346,11 @@ static int CreateSingleSubTask(Task *task, const CJson *in, CJson *out) uint32_t index; void **ptr = NULL; FOR_EACH_HC_VECTOR(g_protocolTypeVec, index, ptr) { + if ((ptr == NULL) || ((*ptr) == NULL)) { + continue; + } DasProtocolType *temp = (DasProtocolType *)(*ptr); - if ((ptr != NULL) && ((*ptr) != NULL) && (temp->type == protocolType)) { + if (temp->type == protocolType) { SubTaskBase *subTask = temp->createSubTask(in, out); if (subTask == NULL) { LOGE("Create subTask failed"); From 0ddc0eaf6f77833f2c2e002b7c2b93b5617850cd Mon Sep 17 00:00:00 2001 From: fuzikun Date: Thu, 2 Sep 2021 11:39:58 +0800 Subject: [PATCH 7/7] bug fix: codecheck Signed-off-by: fuzikun --- services/common/inc/task_manager/task_manager.h | 4 ++-- services/common/src/channel_manager/channel_manager.c | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/services/common/inc/task_manager/task_manager.h b/services/common/inc/task_manager/task_manager.h index 676b229..ddda7b8 100644 --- a/services/common/inc/task_manager/task_manager.h +++ b/services/common/inc/task_manager/task_manager.h @@ -16,12 +16,12 @@ #ifndef TASK_MANAGER_H #define TASK_MANAGER_H +#include "hc_task_thread.h" + #ifdef __cplusplus extern "C" { #endif -#include "hc_task_thread.h" - int32_t InitTaskManager(void); void DestroyTaskManager(void); int32_t PushTask(HcTaskBase *baseTask); diff --git a/services/common/src/channel_manager/channel_manager.c b/services/common/src/channel_manager/channel_manager.c index f0f8592..2a0b6fd 100644 --- a/services/common/src/channel_manager/channel_manager.c +++ b/services/common/src/channel_manager/channel_manager.c @@ -45,8 +45,7 @@ void DestroyChannelManager(void) ChannelType GetChannelType(const DeviceAuthCallback *callback, const CJson *jsonParams) { - if (IsSoftBusChannelSupported()) - { + if (IsSoftBusChannelSupported()) { const char *connectParams = GetStringFromJson(jsonParams, FIELD_CONNECT_PARAMS); if (connectParams != NULL) { return SOFT_BUS;