1 /*
2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "cm_response.h"
17
18 #include <dlfcn.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
23 #include "ipc_skeleton.h"
24
25 #include "cm_log.h"
26 #include "cm_mem.h"
27 #include "os_account_manager.h"
28
29 #define MAX_SIZE_CAPACITY 819200 /* Prevent sendrequest from failing to send due to memory limitations */
30
31 using namespace OHOS;
32
IsBetween(int32_t result,int32_t resultBegin,int32_t resultEnd)33 static bool IsBetween(int32_t result, int32_t resultBegin, int32_t resultEnd)
34 {
35 if ((result > resultEnd) && (result < resultBegin)) {
36 return true;
37 }
38 return false;
39 }
40
ConvertErrorCode(int32_t result)41 static int32_t ConvertErrorCode(int32_t result)
42 {
43 if (IsBetween(result, CMR_ERROR_INVALID_ARGUMENT_BEGIN, CMR_ERROR_INVALID_ARGUMENT_END)) {
44 return CMR_ERROR_INVALID_ARGUMENT;
45 }
46
47 if (IsBetween(result, CMR_ERROR_KEY_OPERATION_BEGIN, CMR_ERROR_KEY_OPERATION_END)) {
48 return CMR_ERROR_KEY_OPERATION_FAILED;
49 }
50
51 if (IsBetween(result, CMR_ERROR_AUTH_FAILED_BEGIN, CMR_ERROR_AUTH_FAILED_END)) {
52 return CMR_ERROR_AUTH_CHECK_FAILED;
53 }
54
55 return result;
56 }
57
CmSendResponse(const struct CmContext * context,int32_t result,const struct CmBlob * response)58 void CmSendResponse(const struct CmContext *context, int32_t result, const struct CmBlob *response)
59 {
60 if (context == nullptr) {
61 CM_LOG_E("SendResponse NULL Pointer");
62 return;
63 }
64
65 int32_t ret = ConvertErrorCode(result);
66 MessageParcel *reply = reinterpret_cast<MessageParcel *>(const_cast<CmContext *>(context));
67 reply->WriteInt32(ret);
68 if (ret != CM_SUCCESS) {
69 CM_LOG_E("SendResponse result is %d.", ret);
70 return;
71 }
72 if (response == nullptr) {
73 reply->WriteUint32(0);
74 } else {
75 reply->SetMaxCapacity(MAX_SIZE_CAPACITY);
76 reply->WriteUint32(response->size);
77 reply->WriteBuffer(response->data, static_cast<size_t>(response->size));
78 }
79 }
80
CmGetProcessInfoForIPC(struct CmContext * cmContext)81 int32_t CmGetProcessInfoForIPC(struct CmContext *cmContext)
82 {
83 if (cmContext == nullptr) {
84 CM_LOG_D("CmGetProcessInfoForIPC Paramset is Invalid");
85 return CMR_ERROR_NULL_POINTER;
86 }
87
88 int userId = 0;
89 auto callingUid = IPCSkeleton::GetCallingUid();
90
91 OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId);
92
93 cmContext->uid = (uint32_t)callingUid;
94 cmContext->userId = static_cast<uint32_t>(userId);
95
96 return CM_SUCCESS;
97 }
98