xref: /aosp_15_r20/system/chre/platform/shared/nanoapp_load_manager.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "chre/platform/shared/nanoapp_load_manager.h"
18 
19 namespace chre {
20 
prepareForLoad(uint16_t hostClientId,uint32_t transactionId,uint64_t appId,uint32_t appVersion,uint32_t appFlags,size_t totalBinaryLen,uint32_t targetApiVersion)21 bool NanoappLoadManager::prepareForLoad(uint16_t hostClientId,
22                                         uint32_t transactionId, uint64_t appId,
23                                         uint32_t appVersion, uint32_t appFlags,
24                                         size_t totalBinaryLen,
25                                         uint32_t targetApiVersion) {
26   mCurrentLoadInfo.hostClientId = hostClientId;
27   mCurrentLoadInfo.transactionId = transactionId;
28   mCurrentLoadInfo.nextFragmentId = 1;
29   mNanoapp = MakeUnique<Nanoapp>();
30 
31   bool success = false;
32   if (mNanoapp.isNull()) {
33     LOG_OOM();
34   } else {
35     success = mNanoapp->reserveBuffer(appId, appVersion, appFlags,
36                                       totalBinaryLen, targetApiVersion);
37   }
38 
39   if (!success) {
40     markFailure();
41   }
42 
43   return success;
44 }
45 
copyNanoappFragment(uint16_t hostClientId,uint32_t transactionId,uint32_t fragmentId,const void * buffer,size_t bufferLen)46 bool NanoappLoadManager::copyNanoappFragment(uint16_t hostClientId,
47                                              uint32_t transactionId,
48                                              uint32_t fragmentId,
49                                              const void *buffer,
50                                              size_t bufferLen) {
51   bool success = false;
52   if (validateFragment(hostClientId, transactionId, fragmentId)) {
53     success = mNanoapp->copyNanoappFragment(buffer, bufferLen);
54     if (success) {
55       mCurrentLoadInfo.nextFragmentId++;
56     } else {
57       markFailure();
58     }
59   }
60 
61   return success;
62 }
63 
validateFragment(uint16_t hostClientId,uint32_t transactionId,uint32_t fragmentId) const64 bool NanoappLoadManager::validateFragment(uint16_t hostClientId,
65                                           uint32_t transactionId,
66                                           uint32_t fragmentId) const {
67   bool valid = false;
68   if (!hasPendingLoadTransaction()) {
69     LOGE("No pending load transaction exists");
70   } else {
71     const FragmentedLoadInfo &info = mCurrentLoadInfo;
72     valid = (info.hostClientId == hostClientId &&
73              info.transactionId == transactionId &&
74              info.nextFragmentId == fragmentId);
75     if (!valid) {
76       LOGE("Unexpected load fragment: expected host %" PRIu16
77            "transaction %" PRIu32 " fragment %" PRIu32
78            ", received host %" PRIu16 " transaction %" PRIu32
79            " fragment %" PRIu32,
80            info.hostClientId, info.transactionId, info.nextFragmentId,
81            hostClientId, transactionId, fragmentId);
82     }
83   }
84 
85   return valid;
86 }
87 
88 }  // namespace chre
89