blob: 401e1aaf8851e195d07aac2adf7f26f2d7601e8b [file] [log] [blame]
Mike Lockwood3a322132009-11-24 00:30:52 -05001/*
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 Project9066cfe2009-03-03 19:31:44 -080016
Mike Lockwood3a322132009-11-24 00:30:52 -050017#define LOG_TAG "LightsService"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
19#include "jni.h"
20#include "JNIHelp.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include "android_runtime/AndroidRuntime.h"
The Android Open Source Project10592532009-03-18 17:39:46 -070022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <utils/misc.h>
24#include <utils/Log.h>
The Android Open Source Project10592532009-03-18 17:39:46 -070025#include <hardware/hardware.h>
26#include <hardware/lights.h>
27
28#include <stdio.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30namespace android
31{
32
The Android Open Source Project10592532009-03-18 17:39:46 -070033// These values must correspond with the LIGHT_ID constants in
Mike Lockwood3a322132009-11-24 00:30:52 -050034// LightsService.java
The Android Open Source Project10592532009-03-18 17:39:46 -070035enum {
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 Lockwood3cb67a32009-11-27 14:25:58 -050042 LIGHT_INDEX_BLUETOOTH = 6,
43 LIGHT_INDEX_WIFI = 7,
The Android Open Source Project10592532009-03-18 17:39:46 -070044 LIGHT_COUNT
45};
46
47struct Devices {
48 light_device_t* lights[LIGHT_COUNT];
49};
50
51static 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
63static 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 Lockwood3cb67a32009-11-27 14:25:58 -050085 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 Project10592532009-03-18 17:39:46 -070089 } else {
90 memset(devices, 0, sizeof(Devices));
91 }
92
93 return (jint)devices;
94}
95
96static 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
106static void setLight_native(JNIEnv *env, jobject clazz, int ptr,
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -0500107 int light, int colorARGB, int flashMode, int onMS, int offMS, int brightnessMode)
The Android Open Source Project10592532009-03-18 17:39:46 -0700108{
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 Lockwoodcc9a63d2009-11-10 07:50:28 -0500121 state.brightnessMode = brightnessMode;
The Android Open Source Project10592532009-03-18 17:39:46 -0700122
Jeff Brown96307042012-07-27 15:51:34 -0700123 {
124 ALOGD_IF_SLOW(50, "Excessive delay setting light");
125 devices->lights[light]->set_light(devices->lights[light], &state);
126 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700127}
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129static JNINativeMethod method_table[] = {
The Android Open Source Project10592532009-03-18 17:39:46 -0700130 { "init_native", "()I", (void*)init_native },
Kenny19b8d8a2009-08-05 00:30:58 +0800131 { "finalize_native", "(I)V", (void*)finalize_native },
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -0500132 { "setLight_native", "(IIIIIII)V", (void*)setLight_native },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133};
134
Mike Lockwood3a322132009-11-24 00:30:52 -0500135int register_android_server_LightsService(JNIEnv *env)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136{
Mike Lockwood3a322132009-11-24 00:30:52 -0500137 return jniRegisterNativeMethods(env, "com/android/server/LightsService",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 method_table, NELEM(method_table));
139}
140
141};