Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 17 | #define LOG_TAG "LightsService" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
| 19 | #include "jni.h" |
| 20 | #include "JNIHelp.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include "android_runtime/AndroidRuntime.h" |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/misc.h> |
| 24 | #include <utils/Log.h> |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 25 | #include <hardware/hardware.h> |
| 26 | #include <hardware/lights.h> |
| 27 | |
| 28 | #include <stdio.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android |
| 31 | { |
| 32 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 33 | // These values must correspond with the LIGHT_ID constants in |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 34 | // LightsService.java |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 35 | enum { |
| 36 | LIGHT_INDEX_BACKLIGHT = 0, |
| 37 | LIGHT_INDEX_KEYBOARD = 1, |
| 38 | LIGHT_INDEX_BUTTONS = 2, |
| 39 | LIGHT_INDEX_BATTERY = 3, |
| 40 | LIGHT_INDEX_NOTIFICATIONS = 4, |
| 41 | LIGHT_INDEX_ATTENTION = 5, |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 42 | LIGHT_INDEX_BLUETOOTH = 6, |
| 43 | LIGHT_INDEX_WIFI = 7, |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 44 | LIGHT_COUNT |
| 45 | }; |
| 46 | |
| 47 | struct Devices { |
| 48 | light_device_t* lights[LIGHT_COUNT]; |
| 49 | }; |
| 50 | |
| 51 | static light_device_t* get_device(hw_module_t* module, char const* name) |
| 52 | { |
| 53 | int err; |
| 54 | hw_device_t* device; |
| 55 | err = module->methods->open(module, name, &device); |
| 56 | if (err == 0) { |
| 57 | return (light_device_t*)device; |
| 58 | } else { |
| 59 | return NULL; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static jint init_native(JNIEnv *env, jobject clazz) |
| 64 | { |
| 65 | int err; |
| 66 | hw_module_t* module; |
| 67 | Devices* devices; |
| 68 | |
| 69 | devices = (Devices*)malloc(sizeof(Devices)); |
| 70 | |
| 71 | err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module); |
| 72 | if (err == 0) { |
| 73 | devices->lights[LIGHT_INDEX_BACKLIGHT] |
| 74 | = get_device(module, LIGHT_ID_BACKLIGHT); |
| 75 | devices->lights[LIGHT_INDEX_KEYBOARD] |
| 76 | = get_device(module, LIGHT_ID_KEYBOARD); |
| 77 | devices->lights[LIGHT_INDEX_BUTTONS] |
| 78 | = get_device(module, LIGHT_ID_BUTTONS); |
| 79 | devices->lights[LIGHT_INDEX_BATTERY] |
| 80 | = get_device(module, LIGHT_ID_BATTERY); |
| 81 | devices->lights[LIGHT_INDEX_NOTIFICATIONS] |
| 82 | = get_device(module, LIGHT_ID_NOTIFICATIONS); |
| 83 | devices->lights[LIGHT_INDEX_ATTENTION] |
| 84 | = get_device(module, LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 85 | devices->lights[LIGHT_INDEX_BLUETOOTH] |
| 86 | = get_device(module, LIGHT_ID_BLUETOOTH); |
| 87 | devices->lights[LIGHT_INDEX_WIFI] |
| 88 | = get_device(module, LIGHT_ID_WIFI); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 89 | } else { |
| 90 | memset(devices, 0, sizeof(Devices)); |
| 91 | } |
| 92 | |
| 93 | return (jint)devices; |
| 94 | } |
| 95 | |
| 96 | static void finalize_native(JNIEnv *env, jobject clazz, int ptr) |
| 97 | { |
| 98 | Devices* devices = (Devices*)ptr; |
| 99 | if (devices == NULL) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | free(devices); |
| 104 | } |
| 105 | |
| 106 | static void setLight_native(JNIEnv *env, jobject clazz, int ptr, |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 107 | int light, int colorARGB, int flashMode, int onMS, int offMS, int brightnessMode) |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 108 | { |
| 109 | Devices* devices = (Devices*)ptr; |
| 110 | light_state_t state; |
| 111 | |
| 112 | if (light < 0 || light >= LIGHT_COUNT || devices->lights[light] == NULL) { |
| 113 | return ; |
| 114 | } |
| 115 | |
| 116 | memset(&state, 0, sizeof(light_state_t)); |
| 117 | state.color = colorARGB; |
| 118 | state.flashMode = flashMode; |
| 119 | state.flashOnMS = onMS; |
| 120 | state.flashOffMS = offMS; |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 121 | state.brightnessMode = brightnessMode; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 122 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 123 | { |
| 124 | ALOGD_IF_SLOW(50, "Excessive delay setting light"); |
| 125 | devices->lights[light]->set_light(devices->lights[light], &state); |
| 126 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 127 | } |
| 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | static JNINativeMethod method_table[] = { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 130 | { "init_native", "()I", (void*)init_native }, |
Kenny | 19b8d8a | 2009-08-05 00:30:58 +0800 | [diff] [blame] | 131 | { "finalize_native", "(I)V", (void*)finalize_native }, |
Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 132 | { "setLight_native", "(IIIIIII)V", (void*)setLight_native }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 135 | int register_android_server_LightsService(JNIEnv *env) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 137 | return jniRegisterNativeMethods(env, "com/android/server/LightsService", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | method_table, NELEM(method_table)); |
| 139 | } |
| 140 | |
| 141 | }; |