1cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project/*
2cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
3cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project *
4cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * you may not use this file except in compliance with the License.
6cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * You may obtain a copy of the License at
7cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project *
8cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project *
10cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * See the License for the specific language governing permissions and
14cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project * limitations under the License.
15cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project */
16cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#include <hardware_legacy/vibrator.h>
17cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#include "qemu.h"
18cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
19cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#include <stdio.h>
20cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#include <unistd.h>
21cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#include <fcntl.h>
22cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#include <errno.h>
23cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
24cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#define THE_DEVICE "/sys/class/timed_output/vibrator/enable"
25cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
267314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackbornint vibrator_exists()
277314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn{
287314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn    int fd;
297314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn
307314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn#ifdef QEMU_HARDWARE
317314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn    if (qemu_check()) {
327314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn        return 1;
337314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn    }
347314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn#endif
357314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn
367314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn    fd = open(THE_DEVICE, O_RDWR);
377314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn    if(fd < 0)
387314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn        return 0;
397314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn    close(fd);
407314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn    return 1;
417314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn}
427314fe03b6cc7c16d49aacbe59840d8724f6852eDianne Hackborn
43cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Projectstatic int sendit(int timeout_ms)
44cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project{
45cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    int nwr, ret, fd;
46cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    char value[20];
47cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
48cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#ifdef QEMU_HARDWARE
49cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    if (qemu_check()) {
50cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project        return qemu_control_command( "vibrator:%d", timeout_ms );
51cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    }
52cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project#endif
53cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
54cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    fd = open(THE_DEVICE, O_RDWR);
55cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    if(fd < 0)
56cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project        return errno;
57cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
58cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    nwr = sprintf(value, "%d\n", timeout_ms);
59cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    ret = write(fd, value, nwr);
60cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
61cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    close(fd);
62cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
63cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    return (ret == nwr) ? 0 : -1;
64cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project}
65cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
66cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Projectint vibrator_on(int timeout_ms)
67cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project{
68cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    /* constant on, up to maximum allowed time */
69cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    return sendit(timeout_ms);
70cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project}
71cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project
72cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Projectint vibrator_off()
73cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project{
74cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project    return sendit(0);
75cc490161f6af9e4a6842ee827e4bfc43bc4509d5The Android Open Source Project}
76