OSDN Git Service

DO NOT MERGE ANYWHERE goldfish: adjust to the refactoring of qemu-pipe
[android-x86/device-generic-goldfish.git] / vibrator / vibrator_qemu.c
1 /*
2  * Copyright (C) 2013 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  */
16
17 #include <errno.h>
18 #include <stdlib.h>
19
20 #include <cutils/log.h>
21
22 #define QEMU_HARDWARE
23 #include <hardware/hardware.h>
24 #include <hardware/vibrator.h>
25 #include <qemu_pipe.h>
26
27 static int sendit(unsigned int timeout_ms)
28 {
29     static int pipe_fd = -2;
30     if (pipe_fd < -1) {
31         pipe_fd = qemu_pipe_open("pipe:qemud:hw-control");
32     }
33     if (pipe_fd < 0) {
34         return -ENOSYS;
35     }
36     char buff[16];
37     snprintf(buff, sizeof(buff), "vibrator:%u", timeout_ms);
38     if (qemu_pipe_frame_send(pipe_fd, buff, strlen(buff)) < 0) {
39         return -errno;
40     }
41     return 0;
42 }
43
44 static int qemu_vibra_on(vibrator_device_t* vibradev __unused,
45                          unsigned int timeout_ms)
46 {
47     return sendit(timeout_ms);
48 }
49
50 static int qemu_vibra_off(vibrator_device_t* vibradev __unused)
51 {
52     return sendit(0);
53 }
54
55 static int qemu_vibra_close(hw_device_t *device)
56 {
57     free(device);
58     return 0;
59 }
60
61 static int qemu_vibra_open(const hw_module_t* module, const char* id __unused,
62                            hw_device_t** device) {
63     vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t));
64
65     if (!vibradev) {
66         ALOGE("No memory available to create Goldfish vibrator device!");
67         return -ENOMEM;
68     }
69
70     vibradev->common.tag = HARDWARE_DEVICE_TAG;
71     vibradev->common.module = (hw_module_t *) module;
72     vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0);
73     vibradev->common.close = qemu_vibra_close;
74
75     vibradev->vibrator_on = qemu_vibra_on;
76     vibradev->vibrator_off = qemu_vibra_off;
77
78     *device = (hw_device_t *) vibradev;
79
80     return 0;
81 }
82
83 /*===========================*/
84 /* Emulator vibrator module  */
85 /*===========================*/
86
87 static struct hw_module_methods_t qemu_vibrator_module_methods = {
88     .open = qemu_vibra_open,
89 };
90
91 struct hw_module_t HAL_MODULE_INFO_SYM = {
92     .tag = HARDWARE_MODULE_TAG,
93     .module_api_version = VIBRATOR_API_VERSION,
94     .hal_api_version = HARDWARE_HAL_API_VERSION,
95     .id = VIBRATOR_HARDWARE_MODULE_ID,
96     .name = "Goldfish vibrator module",
97     .author = "The Android Open Source Project",
98     .methods = &qemu_vibrator_module_methods,
99 };