| 1 | /* |
| 2 | * Copyright (C) 2021 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 <android/binder_parcel.h> |
| 18 | #include <android/binder_parcel_jni.h> |
| 19 | #include <android/binder_parcel_utils.h> |
| 20 | #include <android_runtime/Log.h> |
| 21 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 22 | |
| 23 | #include <cstring> |
| 24 | |
| 25 | #include "LongArrayMultiStateCounter.h" |
| 26 | #include "core_jni_helpers.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | static jlong native_init(jint stateCount, jint arrayLength) { |
| 31 | battery::LongArrayMultiStateCounter *counter = |
| 32 | new battery::LongArrayMultiStateCounter(stateCount, std::vector<uint64_t>(arrayLength)); |
| 33 | return reinterpret_cast<jlong>(counter); |
| 34 | } |
| 35 | |
| 36 | static void native_dispose(void *nativePtr) { |
| 37 | battery::LongArrayMultiStateCounter *counter = |
| 38 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 39 | delete counter; |
| 40 | } |
| 41 | |
| 42 | static jlong native_getReleaseFunc() { |
| 43 | return reinterpret_cast<jlong>(native_dispose); |
| 44 | } |
| 45 | |
| 46 | static void native_setEnabled(jlong nativePtr, jboolean enabled, jlong timestamp) { |
| 47 | battery::LongArrayMultiStateCounter *counter = |
| 48 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 49 | counter->setEnabled(enabled, timestamp); |
| 50 | } |
| 51 | |
| 52 | static void native_setState(jlong nativePtr, jint state, jlong timestamp) { |
| 53 | battery::LongArrayMultiStateCounter *counter = |
| 54 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 55 | counter->setState(state, timestamp); |
| 56 | } |
| 57 | |
| 58 | static void native_updateValues(jlong nativePtr, jlong longArrayContainerNativePtr, |
| 59 | jlong timestamp) { |
| 60 | battery::LongArrayMultiStateCounter *counter = |
| 61 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 62 | std::vector<uint64_t> *vector = |
| 63 | reinterpret_cast<std::vector<uint64_t> *>(longArrayContainerNativePtr); |
| 64 | |
| 65 | counter->updateValue(*vector, timestamp); |
| 66 | } |
| 67 | |
| 68 | static void native_addCounts(jlong nativePtr, jlong longArrayContainerNativePtr) { |
| 69 | battery::LongArrayMultiStateCounter *counter = |
| 70 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 71 | std::vector<uint64_t> *vector = |
| 72 | reinterpret_cast<std::vector<uint64_t> *>(longArrayContainerNativePtr); |
| 73 | counter->addValue(*vector); |
| 74 | } |
| 75 | |
| 76 | static void native_reset(jlong nativePtr) { |
| 77 | battery::LongArrayMultiStateCounter *counter = |
| 78 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 79 | counter->reset(); |
| 80 | } |
| 81 | |
| 82 | static void native_getCounts(jlong nativePtr, jlong longArrayContainerNativePtr, jint state) { |
| 83 | battery::LongArrayMultiStateCounter *counter = |
| 84 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 85 | std::vector<uint64_t> *vector = |
| 86 | reinterpret_cast<std::vector<uint64_t> *>(longArrayContainerNativePtr); |
| 87 | |
| 88 | *vector = counter->getCount(state); |
| 89 | } |
| 90 | |
| 91 | static jobject native_toString(JNIEnv *env, jobject self, jlong nativePtr) { |
| 92 | battery::LongArrayMultiStateCounter *counter = |
| 93 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 94 | return env->NewStringUTF(counter->toString().c_str()); |
| 95 | } |
| 96 | |
| 97 | static void throwWriteRE(JNIEnv *env, binder_status_t status) { |
| 98 | ALOGE("Could not write LongArrayMultiStateCounter to Parcel, status = %d", status); |
| 99 | jniThrowRuntimeException(env, "Could not write LongArrayMultiStateCounter to Parcel"); |
| 100 | } |
| 101 | |
| 102 | #define THROW_ON_WRITE_ERROR(expr) \ |
| 103 | { \ |
| 104 | binder_status_t status = expr; \ |
| 105 | if (status != STATUS_OK) { \ |
| 106 | throwWriteRE(env, status); \ |
| 107 | } \ |
| 108 | } |
| 109 | |
| 110 | static void native_writeToParcel(JNIEnv *env, jobject self, jlong nativePtr, jobject jParcel, |
| 111 | jint flags) { |
| 112 | battery::LongArrayMultiStateCounter *counter = |
| 113 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 114 | ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); |
| 115 | |
| 116 | uint16_t stateCount = counter->getStateCount(); |
| 117 | THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); |
| 118 | |
| 119 | // LongArrayMultiStateCounter has at least state 0 |
| 120 | const std::vector<uint64_t> &anyState = counter->getCount(0); |
| 121 | THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), anyState.size())); |
| 122 | |
| 123 | for (battery::state_t state = 0; state < stateCount; state++) { |
| 124 | THROW_ON_WRITE_ERROR(ndk::AParcel_writeVector(parcel.get(), counter->getCount(state))); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void throwReadRE(JNIEnv *env, binder_status_t status) { |
| 129 | ALOGE("Could not read LongArrayMultiStateCounter from Parcel, status = %d", status); |
| 130 | jniThrowRuntimeException(env, "Could not read LongArrayMultiStateCounter from Parcel"); |
| 131 | } |
| 132 | |
| 133 | #define THROW_ON_READ_ERROR(expr) \ |
| 134 | { \ |
| 135 | binder_status_t status = expr; \ |
| 136 | if (status != STATUS_OK) { \ |
| 137 | throwReadRE(env, status); \ |
| 138 | } \ |
| 139 | } |
| 140 | |
| 141 | static jlong native_initFromParcel(JNIEnv *env, jclass theClass, jobject jParcel) { |
| 142 | ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); |
| 143 | |
| 144 | int32_t stateCount; |
| 145 | THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); |
| 146 | |
| 147 | int32_t arrayLength; |
| 148 | THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &arrayLength)); |
| 149 | |
| 150 | battery::LongArrayMultiStateCounter *counter = |
| 151 | new battery::LongArrayMultiStateCounter(stateCount, std::vector<uint64_t>(arrayLength)); |
| 152 | |
| 153 | std::vector<uint64_t> value; |
| 154 | value.reserve(arrayLength); |
| 155 | |
| 156 | for (battery::state_t state = 0; state < stateCount; state++) { |
| 157 | THROW_ON_READ_ERROR(ndk::AParcel_readVector(parcel.get(), &value)); |
| 158 | counter->setValue(state, value); |
| 159 | } |
| 160 | |
| 161 | return reinterpret_cast<jlong>(counter); |
| 162 | } |
| 163 | |
| 164 | static jint native_getStateCount(jlong nativePtr) { |
| 165 | battery::LongArrayMultiStateCounter *counter = |
| 166 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 167 | return counter->getStateCount(); |
| 168 | } |
| 169 | |
| 170 | static jint native_getArrayLength(jlong nativePtr) { |
| 171 | battery::LongArrayMultiStateCounter *counter = |
| 172 | reinterpret_cast<battery::LongArrayMultiStateCounter *>(nativePtr); |
| 173 | |
| 174 | // LongArrayMultiStateCounter has at least state 0 |
| 175 | const std::vector<uint64_t> &anyState = counter->getCount(0); |
| 176 | return anyState.size(); |
| 177 | } |
| 178 | |
| 179 | static jlong native_init_LongArrayContainer(jint length) { |
| 180 | return reinterpret_cast<jlong>(new std::vector<uint64_t>(length)); |
| 181 | } |
| 182 | |
| 183 | static const JNINativeMethod g_LongArrayMultiStateCounter_methods[] = { |
| 184 | // @CriticalNative |
| 185 | {"native_init", "(II)J", (void *)native_init}, |
| 186 | // @CriticalNative |
| 187 | {"native_getReleaseFunc", "()J", (void *)native_getReleaseFunc}, |
| 188 | // @CriticalNative |
| 189 | {"native_setEnabled", "(JZJ)V", (void *)native_setEnabled}, |
| 190 | // @CriticalNative |
| 191 | {"native_setState", "(JIJ)V", (void *)native_setState}, |
| 192 | // @CriticalNative |
| 193 | {"native_updateValues", "(JJJ)V", (void *)native_updateValues}, |
| 194 | // @CriticalNative |
| 195 | {"native_addCounts", "(JJ)V", (void *)native_addCounts}, |
| 196 | // @CriticalNative |
| 197 | {"native_reset", "(J)V", (void *)native_reset}, |
| 198 | // @CriticalNative |
| 199 | {"native_getCounts", "(JJI)V", (void *)native_getCounts}, |
| 200 | // @FastNative |
| 201 | {"native_toString", "(J)Ljava/lang/String;", (void *)native_toString}, |
| 202 | // @FastNative |
| 203 | {"native_writeToParcel", "(JLandroid/os/Parcel;I)V", (void *)native_writeToParcel}, |
| 204 | // @FastNative |
| 205 | {"native_initFromParcel", "(Landroid/os/Parcel;)J", (void *)native_initFromParcel}, |
| 206 | // @CriticalNative |
| 207 | {"native_getStateCount", "(J)I", (void *)native_getStateCount}, |
| 208 | // @CriticalNative |
| 209 | {"native_getArrayLength", "(J)I", (void *)native_getArrayLength}, |
| 210 | }; |
| 211 | |
| 212 | /////////////////////// LongArrayMultiStateCounter.LongArrayContainer //////////////////////// |
| 213 | |
| 214 | static void native_dispose_LongArrayContainer(jlong nativePtr) { |
| 215 | std::vector<uint64_t> *vector = reinterpret_cast<std::vector<uint64_t> *>(nativePtr); |
| 216 | delete vector; |
| 217 | } |
| 218 | |
| 219 | static jlong native_getReleaseFunc_LongArrayContainer() { |
| 220 | return reinterpret_cast<jlong>(native_dispose_LongArrayContainer); |
| 221 | } |
| 222 | |
| 223 | static void native_setValues_LongArrayContainer(JNIEnv *env, jobject self, jlong nativePtr, |
| 224 | jlongArray jarray) { |
| 225 | std::vector<uint64_t> *vector = reinterpret_cast<std::vector<uint64_t> *>(nativePtr); |
| 226 | ScopedLongArrayRO scopedArray(env, jarray); |
| 227 | const uint64_t *array = reinterpret_cast<const uint64_t *>(scopedArray.get()); |
| 228 | uint8_t size = scopedArray.size(); |
| 229 | |
| 230 | // Boundary checks are performed in the Java layer |
| 231 | std::copy(array, array + size, vector->data()); |
| 232 | } |
| 233 | |
| 234 | static void native_getValues_LongArrayContainer(JNIEnv *env, jobject self, jlong nativePtr, |
| 235 | jlongArray jarray) { |
| 236 | std::vector<uint64_t> *vector = reinterpret_cast<std::vector<uint64_t> *>(nativePtr); |
| 237 | ScopedLongArrayRW scopedArray(env, jarray); |
| 238 | |
| 239 | // Boundary checks are performed in the Java layer |
| 240 | std::copy(vector->data(), vector->data() + vector->size(), scopedArray.get()); |
| 241 | } |
| 242 | |
| 243 | static const JNINativeMethod g_LongArrayContainer_methods[] = { |
| 244 | // @CriticalNative |
| 245 | {"native_init", "(I)J", (void *)native_init_LongArrayContainer}, |
| 246 | // @CriticalNative |
| 247 | {"native_getReleaseFunc", "()J", (void *)native_getReleaseFunc_LongArrayContainer}, |
| 248 | // @FastNative |
| 249 | {"native_setValues", "(J[J)V", (void *)native_setValues_LongArrayContainer}, |
| 250 | // @FastNative |
| 251 | {"native_getValues", "(J[J)V", (void *)native_getValues_LongArrayContainer}, |
| 252 | }; |
| 253 | |
| 254 | int register_com_android_internal_os_LongArrayMultiStateCounter(JNIEnv *env) { |
| 255 | // 0 represents success, thus "|" and not "&" |
| 256 | return RegisterMethodsOrDie(env, "com/android/internal/os/LongArrayMultiStateCounter", |
| 257 | g_LongArrayMultiStateCounter_methods, |
| 258 | NELEM(g_LongArrayMultiStateCounter_methods)) | |
| 259 | RegisterMethodsOrDie(env, |
| 260 | "com/android/internal/os/LongArrayMultiStateCounter" |
| 261 | "$LongArrayContainer", |
| 262 | g_LongArrayContainer_methods, NELEM(g_LongArrayContainer_methods)); |
| 263 | } |
| 264 | |
| 265 | } // namespace android |