• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   if (hasPendingLoadTransaction()) {
27     LOGW(
28         "Pending load transaction already exists. Overriding previous"
29         " transaction.");
30   }
31 
32   mCurrentLoadInfo.hostClientId = hostClientId;
33   mCurrentLoadInfo.transactionId = transactionId;
34   mCurrentLoadInfo.nextFragmentId = 1;
35   mNanoapp = MakeUnique<Nanoapp>();
36 
37   bool success = false;
38   if (mNanoapp.isNull()) {
39     LOG_OOM();
40   } else {
41     success = mNanoapp->reserveBuffer(appId, appVersion, appFlags,
42                                       totalBinaryLen, targetApiVersion);
43   }
44 
45   if (!success) {
46     markFailure();
47   }
48 
49   return success;
50 }
51 
copyNanoappFragment(uint16_t hostClientId,uint32_t transactionId,uint32_t fragmentId,const void * buffer,size_t bufferLen)52 bool NanoappLoadManager::copyNanoappFragment(uint16_t hostClientId,
53                                              uint32_t transactionId,
54                                              uint32_t fragmentId,
55                                              const void *buffer,
56                                              size_t bufferLen) {
57   bool success = false;
58   if (validateFragment(hostClientId, transactionId, fragmentId)) {
59     success = mNanoapp->copyNanoappFragment(buffer, bufferLen);
60     if (success) {
61       mCurrentLoadInfo.nextFragmentId++;
62     } else {
63       markFailure();
64     }
65   }
66 
67   return success;
68 }
69 
validateFragment(uint16_t hostClientId,uint32_t transactionId,uint32_t fragmentId) const70 bool NanoappLoadManager::validateFragment(uint16_t hostClientId,
71                                           uint32_t transactionId,
72                                           uint32_t fragmentId) const {
73   bool valid = false;
74   if (!hasPendingLoadTransaction()) {
75     LOGE("No pending load transaction exists");
76   } else {
77     const FragmentedLoadInfo &info = mCurrentLoadInfo;
78     valid = (info.hostClientId == hostClientId &&
79              info.transactionId == transactionId &&
80              info.nextFragmentId == fragmentId);
81     if (!valid) {
82       LOGE("Unexpected load fragment: expected host %" PRIu16
83            "transaction %" PRIu32 " fragment %" PRIu32
84            ", received host %" PRIu16 " transaction %" PRIu32
85            " fragment %" PRIu32,
86            info.hostClientId, info.transactionId, info.nextFragmentId,
87            hostClientId, transactionId, fragmentId);
88     }
89   }
90 
91   return valid;
92 }
93 
94 }  // namespace chre
95