blob: 7104870349b5b657c4a4c4b3770b08f6a4209b43 [file] [log] [blame]
Erik Gilling51e95df2013-06-26 11:06:51 -07001/*
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 <hardware/hardware.h>
27#include <hardware/consumerir.h>
28#include <ScopedPrimitiveArray.h>
29
30namespace android {
31
Andreas Gampe26872f42014-09-24 21:39:34 -070032static jlong halOpen(JNIEnv* /* env */, jobject /* obj */) {
Erik Gilling51e95df2013-06-26 11:06:51 -070033 hw_module_t const* module;
34 consumerir_device_t *dev;
35 int err;
36
37 err = hw_get_module(CONSUMERIR_HARDWARE_MODULE_ID, &module);
38 if (err != 0) {
39 ALOGE("Can't open consumer IR HW Module, error: %d", err);
40 return 0;
41 }
42
43 err = module->methods->open(module, CONSUMERIR_TRANSMITTER,
44 (hw_device_t **) &dev);
45 if (err < 0) {
46 ALOGE("Can't open consumer IR transmitter, error: %d", err);
47 return 0;
48 }
49
Ashok Bhat0d552f72014-02-07 12:26:17 +000050 return reinterpret_cast<jlong>(dev);
Erik Gilling51e95df2013-06-26 11:06:51 -070051}
52
Andreas Gampe26872f42014-09-24 21:39:34 -070053static jint halTransmit(JNIEnv *env, jobject /* obj */, jlong halObject,
Erik Gilling51e95df2013-06-26 11:06:51 -070054 jint carrierFrequency, jintArray pattern) {
55 int ret;
56
57 consumerir_device_t *dev = reinterpret_cast<consumerir_device_t*>(halObject);
58 ScopedIntArrayRO cPattern(env, pattern);
59 if (cPattern.get() == NULL) {
60 return -EINVAL;
61 }
62 jsize patternLength = cPattern.size();
63
64 ret = dev->transmit(dev, carrierFrequency, cPattern.get(), patternLength);
65
66 return reinterpret_cast<jint>(ret);
67}
68
Andreas Gampe26872f42014-09-24 21:39:34 -070069static jintArray halGetCarrierFrequencies(JNIEnv *env, jobject /* obj */,
Ashok Bhat0d552f72014-02-07 12:26:17 +000070 jlong halObject) {
71 consumerir_device_t *dev = reinterpret_cast<consumerir_device_t*>(halObject);
Erik Gilling51e95df2013-06-26 11:06:51 -070072 consumerir_freq_range_t *ranges;
73 int len;
74
75 len = dev->get_num_carrier_freqs(dev);
76 if (len <= 0)
77 return NULL;
78
79 ranges = new consumerir_freq_range_t[len];
80
Alex Ray5022f272013-09-11 16:21:25 -070081 len = dev->get_carrier_freqs(dev, len, ranges);
Erik Gilling51e95df2013-06-26 11:06:51 -070082 if (len <= 0) {
83 delete[] ranges;
84 return NULL;
85 }
86
87 int i;
88 ScopedIntArrayRW freqsOut(env, env->NewIntArray(len*2));
89 jint *arr = freqsOut.get();
90 if (arr == NULL) {
91 delete[] ranges;
92 return NULL;
93 }
94 for (i = 0; i < len; i++) {
95 arr[i*2] = ranges[i].min;
96 arr[i*2+1] = ranges[i].max;
97 }
98
99 delete[] ranges;
100 return freqsOut.getJavaArray();
101}
102
Daniel Micay76f6a862015-09-19 17:31:01 -0400103static const JNINativeMethod method_table[] = {
Ashok Bhat0d552f72014-02-07 12:26:17 +0000104 { "halOpen", "()J", (void *)halOpen },
105 { "halTransmit", "(JI[I)I", (void *)halTransmit },
106 { "halGetCarrierFrequencies", "(J)[I", (void *)halGetCarrierFrequencies},
Erik Gilling51e95df2013-06-26 11:06:51 -0700107};
108
109int register_android_server_ConsumerIrService(JNIEnv *env) {
110 return jniRegisterNativeMethods(env, "com/android/server/ConsumerIrService",
111 method_table, NELEM(method_table));
112}
113
114};