1/*
2 * Copyright (C) 2013 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#define LOG_TAG "ConsumerIrService"
18
19#include "jni.h"
20#include "JNIHelp.h"
21#include "android_runtime/AndroidRuntime.h"
22
23#include <stdlib.h>
24#include <utils/misc.h>
25#include <utils/Log.h>
26#include <android/hardware/ir/1.0/IConsumerIr.h>
27#include <ScopedPrimitiveArray.h>
28
29using ::android::hardware::ir::V1_0::IConsumerIr;
30using ::android::hardware::ir::V1_0::ConsumerIrFreqRange;
31using ::android::hardware::hidl_vec;
32
33namespace android {
34
35static sp<IConsumerIr> mHal;
36
37static jboolean halOpen(JNIEnv* /* env */, jobject /* obj */) {
38    // TODO(b/31632518)
39    mHal = IConsumerIr::getService();
40    return mHal != nullptr;
41}
42
43static jint halTransmit(JNIEnv *env, jobject /* obj */, jint carrierFrequency,
44   jintArray pattern) {
45    ScopedIntArrayRO cPattern(env, pattern);
46    if (cPattern.get() == NULL) {
47        return -EINVAL;
48    }
49    hidl_vec<int32_t> patternVec;
50    patternVec.setToExternal(const_cast<int32_t*>(cPattern.get()), cPattern.size());
51
52    bool success = mHal->transmit(carrierFrequency, patternVec);
53    return success ? 0 : -1;
54}
55
56static jintArray halGetCarrierFrequencies(JNIEnv *env, jobject /* obj */) {
57    int len;
58    hidl_vec<ConsumerIrFreqRange> ranges;
59    bool success;
60
61    auto cb = [&](bool s, hidl_vec<ConsumerIrFreqRange> vec) {
62            ranges = vec;
63            success = s;
64    };
65    mHal->getCarrierFreqs(cb);
66
67    if (!success) {
68        return NULL;
69    }
70    len = ranges.size();
71
72    int i;
73    ScopedIntArrayRW freqsOut(env, env->NewIntArray(len*2));
74    jint *arr = freqsOut.get();
75    if (arr == NULL) {
76        return NULL;
77    }
78    for (i = 0; i < len; i++) {
79        arr[i*2] = static_cast<jint>(ranges[i].min);
80        arr[i*2+1] = static_cast<jint>(ranges[i].max);
81    }
82
83    return freqsOut.getJavaArray();
84}
85
86static const JNINativeMethod method_table[] = {
87    { "halOpen", "()Z", (void *)halOpen },
88    { "halTransmit", "(I[I)I", (void *)halTransmit },
89    { "halGetCarrierFrequencies", "()[I", (void *)halGetCarrierFrequencies},
90};
91
92int register_android_server_ConsumerIrService(JNIEnv *env) {
93    return jniRegisterNativeMethods(env, "com/android/server/ConsumerIrService",
94            method_table, NELEM(method_table));
95}
96
97};
98