rtc关机闹钟7 jni层 com_android_server_AlarmManagerService

时间:2022-07-26 08:20:08
frameworks/base/services/core/jni/com_android_server_AlarmManagerService.cpp
int AlarmImplAlarmDriver::set(int type, struct timespec *ts)
{
return ioctl(fds[0], ANDROID_ALARM_SET(type), ts);
}

其实是调用了./kernel/kernel/drivers/rtc/alarm-dev.c 的

static long alarm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int rv = ;
    unsigned long flags;
    struct timespec new_alarm_time;
    struct timespec new_rtc_time;
    struct timespec tmp_time;
    enum android_alarm_type alarm_type = ANDROID_ALARM_IOCTL_TO_TYPE(cmd);
    uint32_t alarm_type_mask = 1U << alarm_type;
    printk(">>%s cmd == %d\n",__FUNCTION__,cmd);
    if (alarm_type >= ANDROID_ALARM_TYPE_COUNT)
        return -EINVAL;     if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_GET_TIME()) {
        if ((file->f_flags & O_ACCMODE) == O_RDONLY)
            return -EPERM;
        if (file->private_data == NULL &&
            cmd != ANDROID_ALARM_SET_RTC) {
            spin_lock_irqsave(&alarm_slock, flags);
            if (alarm_opened) {
                spin_unlock_irqrestore(&alarm_slock, flags);
                return -EBUSY;
            }
            alarm_opened = ;
            file->private_data = (void *);
            spin_unlock_irqrestore(&alarm_slock, flags);
        }
    }     switch (ANDROID_ALARM_BASE_CMD(cmd)) {
    case ANDROID_ALARM_CLEAR():
        spin_lock_irqsave(&alarm_slock, flags);
        pr_alarm(IO, "alarm %d clear\n", alarm_type);
        alarm_try_to_cancel(&alarms[alarm_type]);
        if (alarm_pending) {
            alarm_pending &= ~alarm_type_mask;
            if (!alarm_pending && !wait_pending)
                wake_unlock(&alarm_wake_lock);
        }
        alarm_enabled &= ~alarm_type_mask;
        spin_unlock_irqrestore(&alarm_slock, flags);
        break;     case ANDROID_ALARM_SET_OLD:
    case ANDROID_ALARM_SET_AND_WAIT_OLD:
        if (get_user(new_alarm_time.tv_sec, (int __user *)arg)) {
            rv = -EFAULT;
            goto err1;
        }
        new_alarm_time.tv_nsec = ;
        goto from_old_alarm_set;     case ANDROID_ALARM_SET_AND_WAIT():
    case ANDROID_ALARM_SET():
        if (copy_from_user(&new_alarm_time, (void __user *)arg,
            sizeof(new_alarm_time))) {
            rv = -EFAULT;
            goto err1;
        }
from_old_alarm_set:
        spin_lock_irqsave(&alarm_slock, flags);
        pr_alarm(IO, "alarm %d set %ld.%09ld\n", alarm_type,
            new_alarm_time.tv_sec, new_alarm_time.tv_nsec);
        alarm_enabled |= alarm_type_mask;
        alarm_start_range(&alarms[alarm_type],
            timespec_to_ktime(new_alarm_time),
            timespec_to_ktime(new_alarm_time));
        spin_unlock_irqrestore(&alarm_slock, flags);
        if (ANDROID_ALARM_BASE_CMD(cmd) != ANDROID_ALARM_SET_AND_WAIT()
            && cmd != ANDROID_ALARM_SET_AND_WAIT_OLD)
            break;
        /* fall though */
    case ANDROID_ALARM_WAIT:
        spin_lock_irqsave(&alarm_slock, flags);
        pr_alarm(IO, "alarm wait\n");
        if (!alarm_pending && wait_pending) {
            wake_unlock(&alarm_wake_lock);
            wait_pending = ;
        }
        spin_unlock_irqrestore(&alarm_slock, flags);
        rv = wait_event_interruptible(alarm_wait_queue, alarm_pending);
        if (rv)
            goto err1;
        spin_lock_irqsave(&alarm_slock, flags);
        rv = alarm_pending;
        wait_pending = ;
        alarm_pending = ;
        spin_unlock_irqrestore(&alarm_slock, flags);
        break;
    case ANDROID_ALARM_SET_RTC:
        if (copy_from_user(&new_rtc_time, (void __user *)arg,
            sizeof(new_rtc_time))) {
            rv = -EFAULT;
            goto err1;
        }
        rv = alarm_set_rtc(new_rtc_time);
        spin_lock_irqsave(&alarm_slock, flags);
        alarm_pending |= ANDROID_ALARM_TIME_CHANGE_MASK;
        wake_up(&alarm_wait_queue);
        spin_unlock_irqrestore(&alarm_slock, flags);
        if (rv < )
            goto err1;
        break;
    case ANDROID_ALARM_GET_TIME():
        switch (alarm_type) {
        case ANDROID_ALARM_RTC_WAKEUP:
        case ANDROID_ALARM_RTC:
            getnstimeofday(&tmp_time);
            break;
        case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP:
        case ANDROID_ALARM_ELAPSED_REALTIME:
            tmp_time =
                ktime_to_timespec(alarm_get_elapsed_realtime());
            break;
        case ANDROID_ALARM_TYPE_COUNT:
        case ANDROID_ALARM_SYSTEMTIME:
            ktime_get_ts(&tmp_time);
            break;
        }
        if (copy_to_user((void __user *)arg, &tmp_time,
            sizeof(tmp_time))) {
            rv = -EFAULT;
            goto err1;
        }
        break;     default:
        rv = -EINVAL;
        goto err1;
    }
err1:
    return rv;

}