aboutsummaryrefslogtreecommitdiff
path: root/platform/shared/nanoapp_load_manager.cc
blob: 67de637d61651161d781dde6914aea2d4d9c2907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * 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 "chre/platform/shared/nanoapp_load_manager.h"

namespace chre {

bool NanoappLoadManager::prepareForLoad(uint16_t hostClientId,
                                        uint32_t transactionId, uint64_t appId,
                                        uint32_t appVersion, uint32_t appFlags,
                                        size_t totalBinaryLen,
                                        uint32_t targetApiVersion) {
  if (hasPendingLoadTransaction()) {
    LOGW(
        "Pending load transaction already exists. Overriding previous"
        " transaction.");
  }

  mCurrentLoadInfo.hostClientId = hostClientId;
  mCurrentLoadInfo.transactionId = transactionId;
  mCurrentLoadInfo.nextFragmentId = 1;
  mNanoapp = MakeUnique<Nanoapp>();

  bool success = false;
  if (mNanoapp.isNull()) {
    LOG_OOM();
  } else {
    success = mNanoapp->reserveBuffer(appId, appVersion, appFlags,
                                      totalBinaryLen, targetApiVersion);
  }

  if (!success) {
    markFailure();
  }

  return success;
}

bool NanoappLoadManager::copyNanoappFragment(uint16_t hostClientId,
                                             uint32_t transactionId,
                                             uint32_t fragmentId,
                                             const void *buffer,
                                             size_t bufferLen) {
  bool success = false;
  if (validateFragment(hostClientId, transactionId, fragmentId)) {
    success = mNanoapp->copyNanoappFragment(buffer, bufferLen);
    if (success) {
      mCurrentLoadInfo.nextFragmentId++;
    } else {
      markFailure();
    }
  }

  return success;
}

bool NanoappLoadManager::validateFragment(uint16_t hostClientId,
                                          uint32_t transactionId,
                                          uint32_t fragmentId) const {
  bool valid = false;
  if (!hasPendingLoadTransaction()) {
    LOGE("No pending load transaction exists");
  } else {
    const FragmentedLoadInfo &info = mCurrentLoadInfo;
    valid = (info.hostClientId == hostClientId &&
             info.transactionId == transactionId &&
             info.nextFragmentId == fragmentId);
    if (!valid) {
      LOGE("Unexpected load fragment: expected host %" PRIu16
           "transaction %" PRIu32 " fragment %" PRIu32
           ", received host %" PRIu16 " transaction %" PRIu32
           " fragment %" PRIu32,
           info.hostClientId, info.transactionId, info.nextFragmentId,
           hostClientId, transactionId, fragmentId);
    }
  }

  return valid;
}

}  // namespace chre