Merge "nanohub HAL: define magic value for nanoapp" into nyc-dev
am: d6c2deb560
* commit 'd6c2deb560a13a5161742b1cc7026926c03f97bb':
nanohub HAL: define magic value for nanoapp
Change-Id: I9602e5b54e2df95ca9ca245a47b0217a7438f8f2
diff --git a/include/hardware/nvram.h b/include/hardware/nvram.h
index a1868b5..859ea47 100644
--- a/include/hardware/nvram.h
+++ b/include/hardware/nvram.h
@@ -21,6 +21,7 @@
#include <sys/cdefs.h>
#include <hardware/hardware.h>
+#include <hardware/nvram_defs.h>
__BEGIN_DECLS
@@ -30,30 +31,7 @@
/* The version of this module. */
#define NVRAM_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
-#define NVRAM_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION(0, 1)
-
-/* Values returned by nvram_device methods. */
-typedef uint32_t nvram_result_t;
-
-const nvram_result_t NV_RESULT_SUCCESS = 0;
-const nvram_result_t NV_RESULT_INTERNAL_ERROR = 1;
-const nvram_result_t NV_RESULT_ACCESS_DENIED = 2;
-const nvram_result_t NV_RESULT_INVALID_PARAMETER = 3;
-const nvram_result_t NV_RESULT_SPACE_DOES_NOT_EXIST = 4;
-const nvram_result_t NV_RESULT_SPACE_ALREADY_EXISTS = 5;
-const nvram_result_t NV_RESULT_OPERATION_DISABLED = 6;
-
-/* Values describing available access controls. */
-typedef uint32_t nvram_control_t;
-
-const nvram_control_t NV_CONTROL_PERSISTENT_WRITE_LOCK = 1;
-const nvram_control_t NV_CONTROL_BOOT_WRITE_LOCK = 2;
-const nvram_control_t NV_CONTROL_BOOT_READ_LOCK = 3;
-const nvram_control_t NV_CONTROL_WRITE_AUTHORIZATION = 4;
-const nvram_control_t NV_CONTROL_READ_AUTHORIZATION = 5;
-const nvram_control_t NV_CONTROL_WRITE_EXTEND = 6;
-
-const uint32_t NV_UNLIMITED_SPACES = 0xFFFFFFFF;
+#define NVRAM_DEVICE_API_VERSION_1_1 HARDWARE_DEVICE_API_VERSION(1, 1)
struct nvram_module {
/**
@@ -99,6 +77,17 @@
const struct nvram_device* device, uint64_t* available_size);
/**
+ * Outputs the maximum number of bytes that can be allocated for a single
+ * space. This will always be at least 32. If an implementation does not
+ * limit the maximum size it may provide the total size.
+ *
+ * device - The nvram_device instance.
+ * max_space_size - Receives the output. Cannot be NULL.
+ */
+ nvram_result_t (*get_max_space_size_in_bytes)(
+ const struct nvram_device* device, uint64_t* max_space_size);
+
+ /**
* Outputs the maximum total number of spaces that may be allocated.
* This will always be at least 8. Outputs NV_UNLIMITED_SPACES if any
* number of spaces are supported (limited only to available NVRAM
diff --git a/include/hardware/nvram_defs.h b/include/hardware/nvram_defs.h
new file mode 100644
index 0000000..0256a3c
--- /dev/null
+++ b/include/hardware/nvram_defs.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * This file contains data type definitions and constants that are useful to
+ * code interacting with and implementing the NVRAM HAL, even though it doesn't
+ * use the actual NVRAM HAL module interface. Keeping this in a separate file
+ * simplifies inclusion in low-level code which can't easily include the heavier
+ * hardware.h due to lacking standard headers.
+ */
+
+#ifndef ANDROID_HARDWARE_NVRAM_DEFS_H
+#define ANDROID_HARDWARE_NVRAM_DEFS_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+/* Values returned by nvram_device methods. */
+typedef uint32_t nvram_result_t;
+
+const nvram_result_t NV_RESULT_SUCCESS = 0;
+const nvram_result_t NV_RESULT_INTERNAL_ERROR = 1;
+const nvram_result_t NV_RESULT_ACCESS_DENIED = 2;
+const nvram_result_t NV_RESULT_INVALID_PARAMETER = 3;
+const nvram_result_t NV_RESULT_SPACE_DOES_NOT_EXIST = 4;
+const nvram_result_t NV_RESULT_SPACE_ALREADY_EXISTS = 5;
+const nvram_result_t NV_RESULT_OPERATION_DISABLED = 6;
+
+/* Values describing available access controls. */
+typedef uint32_t nvram_control_t;
+
+const nvram_control_t NV_CONTROL_PERSISTENT_WRITE_LOCK = 1;
+const nvram_control_t NV_CONTROL_BOOT_WRITE_LOCK = 2;
+const nvram_control_t NV_CONTROL_BOOT_READ_LOCK = 3;
+const nvram_control_t NV_CONTROL_WRITE_AUTHORIZATION = 4;
+const nvram_control_t NV_CONTROL_READ_AUTHORIZATION = 5;
+const nvram_control_t NV_CONTROL_WRITE_EXTEND = 6;
+
+const uint32_t NV_UNLIMITED_SPACES = 0xFFFFFFFF;
+
+#ifdef __cplusplus
+} // extern "C"
+#endif // __cplusplus
+
+#endif // ANDROID_HARDWARE_NVRAM_DEFS_H
diff --git a/modules/vibrator/vibrator.c b/modules/vibrator/vibrator.c
index 6b3ce57..c3c2951 100644
--- a/modules/vibrator/vibrator.c
+++ b/modules/vibrator/vibrator.c
@@ -21,11 +21,15 @@
#include <malloc.h>
#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
+#define TIMEOUT_STR_LEN 20
+
static const char THE_DEVICE[] = "/sys/class/timed_output/vibrator/enable";
static int vibra_exists() {
@@ -33,7 +37,6 @@
fd = TEMP_FAILURE_RETRY(open(THE_DEVICE, O_RDWR));
if(fd < 0) {
- ALOGE("Vibrator file does not exist : %d", fd);
return 0;
}
@@ -41,27 +44,24 @@
return 1;
}
-static int sendit(unsigned int timeout_ms)
+static int write_value(const char *file, const char *value)
{
int to_write, written, ret, fd;
- char value[20]; /* large enough for millions of years */
-
- fd = TEMP_FAILURE_RETRY(open(THE_DEVICE, O_RDWR));
- if(fd < 0) {
+ fd = TEMP_FAILURE_RETRY(open(file, O_WRONLY));
+ if (fd < 0) {
return -errno;
}
- to_write = snprintf(value, sizeof(value), "%u\n", timeout_ms);
+ to_write = strlen(value) + 1;
written = TEMP_FAILURE_RETRY(write(fd, value, to_write));
-
if (written == -1) {
ret = -errno;
} else if (written != to_write) {
/* even though EAGAIN is an errno value that could be set
by write() in some cases, none of them apply here. So, this return
value can be clearly identified when debugging and suggests the
- caller that it may try to call vibraror_on() again */
+ caller that it may try to call vibrator_on() again */
ret = -EAGAIN;
} else {
ret = 0;
@@ -73,6 +73,14 @@
return ret;
}
+static int sendit(unsigned int timeout_ms)
+{
+ char value[TIMEOUT_STR_LEN]; /* large enough for millions of years */
+
+ snprintf(value, sizeof(value), "%u", timeout_ms);
+ return write_value(THE_DEVICE, value);
+}
+
static int vibra_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
{
/* constant on, up to maximum allowed time */
@@ -84,6 +92,43 @@
return sendit(0);
}
+static const char LED_DEVICE[] = "/sys/class/leds/vibrator";
+
+static int write_led_file(const char *file, const char *value)
+{
+ char file_str[50];
+
+ snprintf(file_str, sizeof(file_str), "%s/%s", LED_DEVICE, file);
+ return write_value(file_str, value);
+}
+
+static int vibra_led_exists()
+{
+ return !write_led_file("trigger", "transient");
+}
+
+static int vibra_led_on(vibrator_device_t* vibradev __unused, unsigned int timeout_ms)
+{
+ int ret;
+ char value[TIMEOUT_STR_LEN]; /* large enough for millions of years */
+
+ ret = write_led_file("state", "1");
+ if (ret)
+ return ret;
+
+ snprintf(value, sizeof(value), "%u\n", timeout_ms);
+ ret = write_led_file("duration", value);
+ if (ret)
+ return ret;
+
+ return write_led_file("activate", "1");
+}
+
+static int vibra_led_off(vibrator_device_t* vibradev __unused)
+{
+ return write_led_file("activate", "0");
+}
+
static int vibra_close(hw_device_t *device)
{
free(device);
@@ -92,7 +137,15 @@
static int vibra_open(const hw_module_t* module, const char* id __unused,
hw_device_t** device __unused) {
- if (!vibra_exists()) {
+ bool use_led;
+
+ if (vibra_exists()) {
+ ALOGD("Vibrator using timed_output");
+ use_led = false;
+ } else if (vibra_led_exists()) {
+ ALOGD("Vibrator using LED trigger");
+ use_led = true;
+ } else {
ALOGE("Vibrator device does not exist. Cannot start vibrator");
return -ENODEV;
}
@@ -109,8 +162,13 @@
vibradev->common.version = HARDWARE_DEVICE_API_VERSION(1,0);
vibradev->common.close = vibra_close;
- vibradev->vibrator_on = vibra_on;
- vibradev->vibrator_off = vibra_off;
+ if (use_led) {
+ vibradev->vibrator_on = vibra_led_on;
+ vibradev->vibrator_off = vibra_led_off;
+ } else {
+ vibradev->vibrator_on = vibra_on;
+ vibradev->vibrator_off = vibra_off;
+ }
*device = (hw_device_t *) vibradev;