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 #define FUNC_LOG \
     39             LOGV("%s", __PRETTY_FUNCTION__)
     40 #define VFUNC_LOG \
     41             LOGV_IF(SensorBase::FUNC_ENTRY, \
     42                     "Entering function '%s'", __PRETTY_FUNCTION__)
     43 #define VHANDLER_LOG \
     44             LOGV_IF(SensorBase::HANDLER_ENTRY, \
     45                     "Entering handler '%s'", __PRETTY_FUNCTION__)
     46 #define CALL_MEMBER_FN(pobject, ptrToMember) ((pobject)->*(ptrToMember))
     47 
     48 #define MAX_SYSFS_NAME_LEN  (100)
     49 #define IIO_BUFFER_LENGTH   (480)
     50 
     51 /*****************************************************************************/
     52 
     53 struct sensors_event_t;
     54 
     55 class SensorBase {
     56 public:
     57     /* Log enablers, each of these independent */
     58     static bool PROCESS_VERBOSE;   /* process log messages */
     59     static bool EXTRA_VERBOSE;     /* verbose log messages */
     60     static bool SYSFS_VERBOSE;     /* log sysfs interactions as cat/echo for
     61                                       repro purpose on a shell */
     62     /* Note that enabling this logs may affect performance */
     63     static bool FUNC_ENTRY;        /* log entry in all one-time functions */
     64     static bool HANDLER_ENTRY;     /* log entry in all handler functions */
     65     static bool ENG_VERBOSE;       /* log a lot more info about the internals */
     66     static bool INPUT_DATA;        /* log the data input from the events */
     67     static bool HANDLER_DATA;      /* log the data fetched from the handlers */
     68 
     69 protected:
     70     const char *dev_name;
     71     const char *data_name;
     72     char input_name[PATH_MAX];
     73     int dev_fd;
     74     int data_fd;
     75 
     76     int openInput(const char* inputName);
     77     static int64_t getTimestamp();
     78     static int64_t timevalToNano(timeval const& t) {
     79         return t.tv_sec * 1000000000LL + t.tv_usec * 1000;
     80     }
     81 
     82     int open_device();
     83     int close_device();
     84 
     85 public:
     86     SensorBase(const char* dev_name, const char* data_name);
     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     virtual int flush(int handle);
     99 };
    100 
    101 /*****************************************************************************/
    102 
    103 #endif  // ANDROID_SENSOR_BASE_H
    104