aboutsummaryrefslogtreecommitdiff
path: root/core/nanoapp.cc
blob: a6632f9b1f439bfe7f5622fc5a7d4901df9dfd2b (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (C) 2016 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/core/nanoapp.h"

#include "chre/core/event_loop_manager.h"
#include "chre/platform/assert.h"
#include "chre/platform/fatal_error.h"
#include "chre/platform/log.h"
#include "chre/util/system/debug_dump.h"
#include "chre_api/chre/gnss.h"
#include "chre_api/chre/version.h"

#include <algorithm>

#if CHRE_FIRST_SUPPORTED_API_VERSION < CHRE_API_VERSION_1_5
#define CHRE_GNSS_MEASUREMENT_BACK_COMPAT_ENABLED
#endif

namespace chre {

constexpr size_t Nanoapp::kMaxSizeWakeupBuckets;

Nanoapp::Nanoapp() {
  // Push first bucket onto wakeup bucket queue
  cycleWakeupBuckets(1);
}

Nanoapp::~Nanoapp() {
  const size_t totalAllocatedBytes = getTotalAllocatedBytes();

  if (totalAllocatedBytes > 0) {
    // TODO: Consider asserting here
    LOGE("Nanoapp ID=0x%016" PRIx64 " still has %zu allocated bytes!",
         getAppId(), totalAllocatedBytes);
  }
}

bool Nanoapp::isRegisteredForBroadcastEvent(uint16_t eventType,
                                            uint16_t targetGroupIdMask) const {
  bool registered = false;
  size_t foundIndex = registrationIndex(eventType);
  if (foundIndex < mRegisteredEvents.size()) {
    const EventRegistration &reg = mRegisteredEvents[foundIndex];
    if (targetGroupIdMask & reg.groupIdMask) {
      registered = true;
    }
  }
  return registered;
}

void Nanoapp::registerForBroadcastEvent(uint16_t eventType,
                                        uint16_t groupIdMask) {
  size_t foundIndex = registrationIndex(eventType);
  if (foundIndex < mRegisteredEvents.size()) {
    mRegisteredEvents[foundIndex].groupIdMask |= groupIdMask;
  } else if (!mRegisteredEvents.push_back(
                 EventRegistration(eventType, groupIdMask))) {
    FATAL_ERROR_OOM();
  }
}

void Nanoapp::unregisterForBroadcastEvent(uint16_t eventType,
                                          uint16_t groupIdMask) {
  size_t foundIndex = registrationIndex(eventType);
  if (foundIndex < mRegisteredEvents.size()) {
    EventRegistration &reg = mRegisteredEvents[foundIndex];
    reg.groupIdMask &= ~groupIdMask;
    if (reg.groupIdMask == 0) {
      mRegisteredEvents.erase(foundIndex);
    }
  }
}

void Nanoapp::configureNanoappInfoEvents(bool enable) {
  if (enable) {
    registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
    registerForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
  } else {
    unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STARTED);
    unregisterForBroadcastEvent(CHRE_EVENT_NANOAPP_STOPPED);
  }
}

void Nanoapp::configureHostSleepEvents(bool enable) {
  if (enable) {
    registerForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
    registerForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
  } else {
    unregisterForBroadcastEvent(CHRE_EVENT_HOST_AWAKE);
    unregisterForBroadcastEvent(CHRE_EVENT_HOST_ASLEEP);
  }
}

void Nanoapp::configureDebugDumpEvent(bool enable) {
  if (enable) {
    registerForBroadcastEvent(CHRE_EVENT_DEBUG_DUMP);
  } else {
    unregisterForBroadcastEvent(CHRE_EVENT_DEBUG_DUMP);
  }
}

void Nanoapp::configureUserSettingEvent(uint8_t setting, bool enable) {
  if (enable) {
    registerForBroadcastEvent(CHRE_EVENT_SETTING_CHANGED_FIRST_EVENT + setting);
  } else {
    unregisterForBroadcastEvent(CHRE_EVENT_SETTING_CHANGED_FIRST_EVENT +
                                setting);
  }
}

Event *Nanoapp::processNextEvent() {
  Event *event = mEventQueue.pop();

  CHRE_ASSERT_LOG(event != nullptr, "Tried delivering event, but queue empty");
  if (event != nullptr) {
    if (event->eventType == CHRE_EVENT_GNSS_DATA) {
      handleGnssMeasurementDataEvent(event);
    } else {
      handleEvent(event->senderInstanceId, event->eventType, event->eventData);
    }
  }

  return event;
}

void Nanoapp::blameHostWakeup() {
  if (mWakeupBuckets.back() < UINT16_MAX) ++mWakeupBuckets.back();
}

void Nanoapp::cycleWakeupBuckets(size_t numBuckets) {
  numBuckets = std::min(numBuckets, kMaxSizeWakeupBuckets);
  for (size_t i = 0; i < numBuckets; ++i) {
    if (mWakeupBuckets.full()) {
      mWakeupBuckets.erase(0);
    }
    mWakeupBuckets.push_back(0);
  }
}

void Nanoapp::logStateToBuffer(DebugDumpWrapper &debugDump) const {
  debugDump.print(" Id=%" PRIu32 " 0x%016" PRIx64 " ", getInstanceId(),
                  getAppId());
  PlatformNanoapp::logStateToBuffer(debugDump);
  debugDump.print(" v%" PRIu32 ".%" PRIu32 ".%" PRIu32 " tgtAPI=%" PRIu32
                  ".%" PRIu32 " curAlloc=%zu peakAlloc=%zu",
                  CHRE_EXTRACT_MAJOR_VERSION(getAppVersion()),
                  CHRE_EXTRACT_MINOR_VERSION(getAppVersion()),
                  CHRE_EXTRACT_PATCH_VERSION(getAppVersion()),
                  CHRE_EXTRACT_MAJOR_VERSION(getTargetApiVersion()),
                  CHRE_EXTRACT_MINOR_VERSION(getTargetApiVersion()),
                  getTotalAllocatedBytes(), getPeakAllocatedBytes());
  debugDump.print(" hostWakeups=[ cur->");
  // Get buckets latest -> earliest except last one
  for (size_t i = mWakeupBuckets.size() - 1; i > 0; --i) {
    debugDump.print("%" PRIu16 ", ", mWakeupBuckets[i]);
  }
  // Earliest bucket gets no comma
  debugDump.print("%" PRIu16 " ]\n", mWakeupBuckets.front());
}

bool Nanoapp::permitPermissionUse(uint32_t permission) const {
  return !supportsAppPermissions() ||
         ((getAppPermissions() & permission) == permission);
}

size_t Nanoapp::registrationIndex(uint16_t eventType) const {
  size_t foundIndex = 0;
  for (; foundIndex < mRegisteredEvents.size(); ++foundIndex) {
    const EventRegistration &reg = mRegisteredEvents[foundIndex];
    if (reg.eventType == eventType) {
      break;
    }
  }
  return foundIndex;
}

void Nanoapp::handleGnssMeasurementDataEvent(const Event *event) {
#ifdef CHRE_GNSS_MEASUREMENT_BACK_COMPAT_ENABLED
  const struct chreGnssDataEvent *data =
      static_cast<const struct chreGnssDataEvent *>(event->eventData);
  if (getTargetApiVersion() < CHRE_API_VERSION_1_5 &&
      data->measurement_count > CHRE_GNSS_MAX_MEASUREMENT_PRE_1_5) {
    chreGnssDataEvent localEvent;
    memcpy(&localEvent, data, sizeof(struct chreGnssDataEvent));
    localEvent.measurement_count = CHRE_GNSS_MAX_MEASUREMENT_PRE_1_5;
    handleEvent(event->senderInstanceId, event->eventType, &localEvent);
  } else
#endif  // CHRE_GNSS_MEASUREMENT_BACK_COMPAT_ENABLED
  {
    handleEvent(event->senderInstanceId, event->eventType, event->eventData);
  }
}

}  // namespace chre