blob: e80dd042123b36064952ed232783304c965bf195 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_server_AlarmManagerService.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Elliott Hughesdd66bcb2011-04-12 11:28:59 -07005** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughesdd66bcb2011-04-12 11:28:59 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070011** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
18#define LOG_TAG "AlarmManagerService"
19
20#include "JNIHelp.h"
21#include "jni.h"
Mathias Agopian25ba5b62009-05-18 15:08:03 -070022#include <utils/Log.h>
23#include <utils/misc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <fcntl.h>
26#include <stdio.h>
27#include <string.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <arpa/inet.h>
31#include <netinet/in.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <linux/ioctl.h>
36#include <linux/android_alarm.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038namespace android {
39
40static jint android_server_AlarmManagerService_setKernelTimezone(JNIEnv* env, jobject obj, jint fd, jint minswest)
41{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 struct timezone tz;
43
44 tz.tz_minuteswest = minswest;
45 tz.tz_dsttime = 0;
46
47 int result = settimeofday(NULL, &tz);
48 if (result < 0) {
49 LOGE("Unable to set kernel timezone to %d: %s\n", minswest, strerror(errno));
50 return -1;
51 } else {
52 LOGD("Kernel timezone updated to %d minutes west of GMT\n", minswest);
53 }
54
55 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056}
57
58static jint android_server_AlarmManagerService_init(JNIEnv* env, jobject obj)
59{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 return open("/dev/alarm", O_RDWR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061}
62
63static void android_server_AlarmManagerService_close(JNIEnv* env, jobject obj, jint fd)
64{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 close(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066}
67
Jeff Brown11c5f1a2010-03-31 15:29:40 -070068static void android_server_AlarmManagerService_set(JNIEnv* env, jobject obj, jint fd, jint type, jlong seconds, jlong nanoseconds)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 struct timespec ts;
Jeff Brown11c5f1a2010-03-31 15:29:40 -070071 ts.tv_sec = seconds;
72 ts.tv_nsec = nanoseconds;
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 int result = ioctl(fd, ANDROID_ALARM_SET(type), &ts);
75 if (result < 0)
76 {
Jeff Brown11c5f1a2010-03-31 15:29:40 -070077 LOGE("Unable to set alarm to %lld.%09lld: %s\n", seconds, nanoseconds, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079}
80
81static jint android_server_AlarmManagerService_waitForAlarm(JNIEnv* env, jobject obj, jint fd)
82{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 int result = 0;
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 do
86 {
87 result = ioctl(fd, ANDROID_ALARM_WAIT);
88 } while (result < 0 && errno == EINTR);
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 if (result < 0)
91 {
92 LOGE("Unable to wait on alarm: %s\n", strerror(errno));
93 return 0;
94 }
Elliott Hughesdd66bcb2011-04-12 11:28:59 -070095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097}
98
99static JNINativeMethod sMethods[] = {
100 /* name, signature, funcPtr */
101 {"init", "()I", (void*)android_server_AlarmManagerService_init},
102 {"close", "(I)V", (void*)android_server_AlarmManagerService_close},
Jeff Brown11c5f1a2010-03-31 15:29:40 -0700103 {"set", "(IIJJ)V", (void*)android_server_AlarmManagerService_set},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 {"waitForAlarm", "(I)I", (void*)android_server_AlarmManagerService_waitForAlarm},
105 {"setKernelTimezone", "(II)I", (void*)android_server_AlarmManagerService_setKernelTimezone},
106};
107
108int register_android_server_AlarmManagerService(JNIEnv* env)
109{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 return jniRegisterNativeMethods(env, "com/android/server/AlarmManagerService",
111 sMethods, NELEM(sMethods));
112}
113
114} /* namespace android */