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
|
/*
* Copyright (C) 2014 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.
*/
#define LOG_TAG "McuHal"
//#define LOG_NDEBUG 0
#include "JNIHelp.h"
#include "jni.h"
#include <ScopedUtfChars.h>
#include <ScopedPrimitiveArray.h>
#include <utils/Errors.h>
#include <utils/Log.h>
#include <hardware/mcu.h>
namespace android {
static jlong nativeOpen(JNIEnv* env, jclass clazz) {
mcu_module_t* module = NULL;
status_t err = hw_get_module(MCU_HARDWARE_MODULE_ID,
(hw_module_t const**)&module);
if (err) {
ALOGE("Couldn't load %s module (%s)", MCU_HARDWARE_MODULE_ID, strerror(-err));
return 0;
}
err = module->init(module);
if (err) {
ALOGE("Couldn't initialize %s module (%s)", MCU_HARDWARE_MODULE_ID, strerror(-err));
return 0;
}
return reinterpret_cast<jlong>(module);
}
static jbyteArray nativeSendMessage(JNIEnv* env, jclass clazz,
jlong ptr, jstring msgStr, jbyteArray argArray) {
mcu_module_t* module = reinterpret_cast<mcu_module_t*>(ptr);
ScopedUtfChars msg(env, msgStr);
ALOGV("Sending message %s to MCU", msg.c_str());
void* result = NULL;
size_t resultSize = 0;
status_t err;
if (argArray) {
ScopedByteArrayRO arg(env, argArray);
err = module->sendMessage(module, msg.c_str(), arg.get(), arg.size(),
&result, &resultSize);
} else {
err = module->sendMessage(module, msg.c_str(), NULL, 0, &result, &resultSize);
}
if (err) {
ALOGE("Couldn't send message to MCU (%s)", strerror(-err));
return NULL;
}
if (!result) {
return NULL;
}
jbyteArray resultArray = env->NewByteArray(resultSize);
if (resultArray) {
env->SetByteArrayRegion(resultArray, 0, resultSize, static_cast<jbyte*>(result));
}
free(result);
return resultArray;
}
static JNINativeMethod gMcuHalMethods[] = {
/* name, signature, funcPtr */
{ "nativeOpen", "()J",
(void*) nativeOpen },
{ "nativeSendMessage", "(JLjava/lang/String;[B)[B",
(void*) nativeSendMessage },
};
int register_android_server_dreams_McuHal(JNIEnv* env) {
int res = jniRegisterNativeMethods(env, "com/android/server/dreams/McuHal",
gMcuHalMethods, NELEM(gMcuHalMethods));
LOG_FATAL_IF(res < 0, "Unable to register native methods.");
return 0;
}
} /* namespace android */
|