summaryrefslogtreecommitdiffstats
path: root/libsensors_iio/MPLSupport.cpp
diff options
context:
space:
mode:
authorJP Abgrall <jpa@google.com>2012-06-15 23:36:18 -0700
committerJP Abgrall <jpa@google.com>2012-06-15 23:36:18 -0700
commit7494581689b0fc1d8addd016b1c92d74d01f5ad4 (patch)
tree682aa496e29e15d7b98f0f4b069e47a019ccef49 /libsensors_iio/MPLSupport.cpp
parent895401859313187f15a800e62d43e6bcbf48fada (diff)
downloadandroid_hardware_invensense-7494581689b0fc1d8addd016b1c92d74d01f5ad4.tar.gz
android_hardware_invensense-7494581689b0fc1d8addd016b1c92d74d01f5ad4.tar.bz2
android_hardware_invensense-7494581689b0fc1d8addd016b1c92d74d01f5ad4.zip
HACK: libsensors: Initial attempt at MotionApps 5.1 for MPU6050 + AKM8975
The code in this patch still needs work. But checking it in will allow other parts of the sensor stack to have something to work with. 1. Have sensor HAL for IIO driver, which uses MPU6050 + AKM8975 (on 2nd bus). 2. Include MPL binaries (libmllite.so/libmplmpu.so). 3. Include necessary header files. 4. remove light sensor dependency. 5. add missing include file. 6. modify the module name into "manta". 7. remove mlsdk directory. 8. Fix some known issues. 9. Sync up to June.12. 10. Tweak slightly so that it can be used with other sensors on manta and doesn't break other devices that need a non IIO libsensors. Change-Id: I0913a6df56fb0e99e9bae9ecc40ab03884d68124 Signed-off-by: Mars Lee <mlee@invensense.com>
Diffstat (limited to 'libsensors_iio/MPLSupport.cpp')
-rw-r--r--libsensors_iio/MPLSupport.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/libsensors_iio/MPLSupport.cpp b/libsensors_iio/MPLSupport.cpp
new file mode 100644
index 0000000..a961d78
--- /dev/null
+++ b/libsensors_iio/MPLSupport.cpp
@@ -0,0 +1,144 @@
+#include <MPLSupport.h>
+#include <string.h>
+#include <stdio.h>
+#include "log.h"
+#include "SensorBase.h"
+#include <fcntl.h>
+
+int inv_read_data(char *fname, long *data)
+{
+ VFUNC_LOG;
+
+ char buf[sizeof(long) * 4];
+ int count, fd;
+
+ fd = open(fname, O_RDONLY);
+ if(fd < 0) {
+ LOGE("HAL:Error opening %s", fname);
+ return -1;
+ }
+ memset(buf, 0, sizeof(buf));
+ count = read_attribute_sensor(fd, buf, sizeof(buf));
+ if(count < 1) {
+ close(fd);
+ return -1;
+ } else {
+ count = sscanf(buf, "%ld", data);
+ if(count)
+ LOGV_IF(EXTRA_VERBOSE, "HAL:Data= %ld", *data);
+ }
+ close(fd);
+
+ return 0;
+}
+
+/* This one DOES NOT close FDs for you */
+int read_attribute_sensor(int fd, char* data, unsigned int size)
+{
+ VFUNC_LOG;
+
+ int count = 0;
+ if (fd > 0) {
+ count = pread(fd, data, size, 0);
+ if(count < 1) {
+ LOGE("HAL:read fails with error code=%d", count);
+ }
+ }
+ return count;
+}
+
+/**
+ * @brief Enable a sensor through the sysfs file descriptor
+ * provided.
+ * @note this function one closes FD after the write
+ * @param fd
+ * the file descriptor to write into
+ * @param en
+ * the value to write, typically 1 or 0
+ * @return the errno whenever applicable.
+ */
+int enable_sysfs_sensor(int fd, int en)
+{
+ VFUNC_LOG;
+
+ int nb = -1;
+ int err = 0;
+
+ if (fd >= 0) {
+ char buf[2];
+ if (en) {
+ buf[0] = '1';
+ nb = write(fd, buf, 1);
+ } else {
+ buf[0] = '0';
+ nb = write(fd, buf, 1);
+ }
+ buf[1] = '\0';
+
+ if (nb <= 0) {
+ err = errno;
+ LOGE("HAL:enable_sysfs_sensor - write %c returned %d (%s / %d)",
+ buf[0], nb, strerror(err), err);
+ }
+ close(fd);
+ } else {
+ LOGV_IF(EXTRA_VERBOSE, "HAL:enable_sysfs_sensor - fd<0");
+ }
+
+ return err;
+}
+
+/* This one closes FDs for you */
+int write_attribute_sensor(int fd, long data)
+{
+ VFUNC_LOG;
+
+ int num_b = 0;
+
+ if (fd >= 0) {
+ char buf[80];
+ sprintf(buf, "%ld", data);
+ num_b = write(fd, buf, strlen(buf) + 1);
+ if (num_b <= 0) {
+ int err = errno;
+ LOGE("HAL:write fd %d returned '%s' (%d)", fd, strerror(err), err);
+ } else {
+ LOGV_IF(EXTRA_VERBOSE, "HAL:fd=%d write attribute to %ld", fd, data);
+ }
+ close(fd);
+ }
+
+ return num_b;
+}
+
+int read_sysfs_int(char *filename, int *var)
+{
+ int res=0;
+ FILE *sysfsfp;
+
+ sysfsfp = fopen(filename, "r");
+ if (sysfsfp!=NULL) {
+ fscanf(sysfsfp, "%d\n", var);
+ fclose(sysfsfp);
+ } else {
+ LOGE("HAL:ERR open file to read");
+ res= -1;
+ }
+ return res;
+}
+
+int write_sysfs_int(char *filename, int var)
+{
+ int res=0;
+ FILE *sysfsfp;
+
+ sysfsfp = fopen(filename, "w");
+ if (sysfsfp!=NULL) {
+ fprintf(sysfsfp, "%d\n", var);
+ fclose(sysfsfp);
+ } else {
+ LOGE("HAL:ERR open file to write");
+ res= -1;
+ }
+ return res;
+}