Home | History | Annotate | Download | only in libsensors_iio
      1 /*
      2 * Copyright (C) 2012 Invensense, Inc.
      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 #ifndef ANDROID_SENSOR_BASE_H
     18 #define ANDROID_SENSOR_BASE_H
     19 
     20 #include <stdint.h>
     21 #include <errno.h>
     22 #include <sys/cdefs.h>
     23 #include <sys/types.h>
     24 
     25 #if defined ANDROID_JELLYBEAN
     26 //build for Jellybean
     27 #define LOGV_IF ALOGV_IF
     28 #define LOGE_IF ALOGE_IF
     29 #define LOGI_IF ALOGI_IF
     30 #define LOGI    ALOGI
     31 #define LOGE    ALOGE
     32 #define LOGV    ALOGV
     33 #define LOGW    ALOGW
     34 #else
     35 //build for ICS or earlier version
     36 #endif
     37 
     38 /* Log enablers, each of these independent */
     39 
     40 #define PROCESS_VERBOSE (0) /* process log messages */
     41 #define EXTRA_VERBOSE   (0) /* verbose log messages */
     42 #define SYSFS_VERBOSE   (0) /* log sysfs interactions as cat/echo for repro
     43                                purpose on a shell */
     44 #define FUNC_ENTRY      (0) /* log entry in all one-time functions */
     45 
     46 /* Note that enabling this logs may affect performance */
     47 #define HANDLER_ENTRY   (0) /* log entry in all handler functions */
     48 #define ENG_VERBOSE     (0) /* log some a lot more info about the internals */
     49 #define INPUT_DATA      (0) /* log the data input from the events */
     50 #define HANDLER_DATA    (0) /* log the data fetched from the handlers */
     51 
     52 #define FUNC_LOG \
     53             LOGV("%s", __PRETTY_FUNCTION__)
     54 #define VFUNC_LOG \
     55             LOGV_IF(FUNC_ENTRY, "Entering function '%s'", __PRETTY_FUNCTION__)
     56 #define VHANDLER_LOG \
     57             LOGV_IF(HANDLER_ENTRY, "Entering handler '%s'", __PRETTY_FUNCTION__)
     58 #define CALL_MEMBER_FN(pobject, ptrToMember) ((pobject)->*(ptrToMember))
     59 
     60 #define MAX_SYSFS_NAME_LEN  (100)
     61 #define IIO_BUFFER_LENGTH   (480)
     62 
     63 /*****************************************************************************/
     64 
     65 struct sensors_event_t;
     66 
     67 class SensorBase {
     68 protected:
     69     const char *dev_name;
     70     const char *data_name;
     71     char input_name[PATH_MAX];
     72     int dev_fd;
     73     int data_fd;
     74 
     75     int openInput(const char* inputName);
     76     static int64_t getTimestamp();
     77     static int64_t timevalToNano(timeval const& t) {
     78         return t.tv_sec * 1000000000LL + t.tv_usec * 1000;
     79     }
     80 
     81     int open_device();
     82     int close_device();
     83 
     84 public:
     85     SensorBase(const char* dev_name, const char* data_name);
     86 
     87     virtual ~SensorBase();
     88 
     89     virtual int readEvents(sensors_event_t* data, int count) = 0;
     90     int readSample(long *data, int64_t *timestamp);
     91     int readRawSample(float *data, int64_t *timestamp);
     92     virtual bool hasPendingEvents() const;
     93     virtual int getFd() const;
     94     virtual int setDelay(int32_t handle, int64_t ns);
     95     virtual int enable(int32_t handle, int enabled);
     96     virtual int query(int what, int* value);
     97     virtual int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
     98 };
     99 
    100 /*****************************************************************************/
    101 
    102 #endif  // ANDROID_SENSOR_BASE_H
    103