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 <qemu.h>
24 #include <hardware/hardware.h>
25 #include <hardware/vibrator.h>
26 
sendit(unsigned int timeout_ms)27 static int sendit(unsigned int timeout_ms)
28 {
29     if (qemu_check()) {
30         if (qemu_control_command("vibrator:%u", timeout_ms) < 0) {
31             return -errno;
32         }
33         return 0;
34     }
35 
36     return -ENOSYS;
37 }
38 
qemu_vibra_on(vibrator_device_t * vibradev __unused,unsigned int timeout_ms)39 static int qemu_vibra_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
40 {
41     return sendit(timeout_ms);
42 }
43 
qemu_vibra_off(vibrator_device_t * vibradev __unused)44 static int qemu_vibra_off(vibrator_device_t* vibradev __unused)
45 {
46     return sendit(0);
47 }
48 
qemu_vibra_close(hw_device_t * device)49 static int qemu_vibra_close(hw_device_t *device)
50 {
51     free(device);
52     return 0;
53 }
54 
qemu_vibra_open(const hw_module_t * module,const char * id __unused,hw_device_t ** device)55 static int qemu_vibra_open(const hw_module_t* module, const char* id __unused,
56                            hw_device_t** device) {
57     vibrator_device_t *vibradev = calloc(1, sizeof(vibrator_device_t));
58 
59     if (!vibradev) {
60         ALOGE("No memory available to create Goldfish vibrator device!");
61         return -ENOMEM;
62     }
63 
64     vibradev->common.tag = HARDWARE_DEVICE_TAG;
65     vibradev->common.module = (hw_module_t *) module;
66     vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0);
67     vibradev->common.close = qemu_vibra_close;
68 
69     vibradev->vibrator_on = qemu_vibra_on;
70     vibradev->vibrator_off = qemu_vibra_off;
71 
72     *device = (hw_device_t *) vibradev;
73 
74     return 0;
75 }
76 
77 /*===========================*/
78 /* Emulator vibrator module  */
79 /*===========================*/
80 
81 static struct hw_module_methods_t qemu_vibrator_module_methods = {
82     .open = qemu_vibra_open,
83 };
84 
85 struct hw_module_t HAL_MODULE_INFO_SYM = {
86     .tag = HARDWARE_MODULE_TAG,
87     .module_api_version = VIBRATOR_API_VERSION,
88     .hal_api_version = HARDWARE_HAL_API_VERSION,
89     .id = VIBRATOR_HARDWARE_MODULE_ID,
90     .name = "Goldfish vibrator module",
91     .author = "The Android Open Source Project",
92     .methods = &qemu_vibrator_module_methods,
93 };
94