Home | History | Annotate | Download | only in jni
      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 <hardware/hardware.h>
     27 #include <hardware/consumerir.h>
     28 #include <ScopedPrimitiveArray.h>
     29 
     30 namespace android {
     31 
     32 static jint halOpen(JNIEnv *env, jobject obj) {
     33     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 
     50     return reinterpret_cast<jint>(dev);
     51 }
     52 
     53 static jint halTransmit(JNIEnv *env, jobject obj, jint halObject,
     54    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 
     69 static jintArray halGetCarrierFrequencies(JNIEnv *env, jobject obj,
     70     jint halObject) {
     71     consumerir_device_t *dev = (consumerir_device_t *) halObject;
     72     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 
     81     len = dev->get_carrier_freqs(dev, len, ranges);
     82     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 
    103 static JNINativeMethod method_table[] = {
    104     { "halOpen", "()I", (void *)halOpen },
    105     { "halTransmit", "(II[I)I", (void *)halTransmit },
    106     { "halGetCarrierFrequencies", "(I)[I", (void *)halGetCarrierFrequencies},
    107 };
    108 
    109 int register_android_server_ConsumerIrService(JNIEnv *env) {
    110     return jniRegisterNativeMethods(env, "com/android/server/ConsumerIrService",
    111             method_table, NELEM(method_table));
    112 }
    113 
    114 };
    115