Home | History | Annotate | Download | only in libsensors_iio
      1 /*
      2 * Copyright (C) 2014 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 || defined ANDROID_KITKAT
     26 //build for Jellybean or KitKat
     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     static bool DEBUG_BATCHING;    /* log the data for debugging batching */
     69 
     70 protected:
     71     const char *dev_name;
     72     const char *data_name;
     73     char input_name[PATH_MAX];
     74     int dev_fd;
     75     int data_fd;
     76 
     77     int openInput(const char* inputName);
     78     static int64_t getTimestamp();
     79     static int64_t timevalToNano(timeval const& t) {
     80         return t.tv_sec * 1000000000LL + t.tv_usec * 1000;
     81     }
     82 
     83     int open_device();
     84     int close_device();
     85 
     86 public:
     87     SensorBase(const char* dev_name, const char* data_name);
     88     virtual ~SensorBase();
     89 
     90     virtual int readEvents(sensors_event_t* data, int count) = 0;
     91     int readSample(long *data, int64_t *timestamp);
     92     int readRawSample(float *data, int64_t *timestamp);
     93     virtual bool hasPendingEvents() const;
     94     virtual int getFd() const;
     95     virtual int setDelay(int32_t handle, int64_t ns);
     96     virtual int enable(int32_t handle, int enabled);
     97     virtual int query(int what, int* value);
     98     virtual int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
     99     virtual int flush(int handle);
    100 };
    101 
    102 /*****************************************************************************/
    103 
    104 #endif  // ANDROID_SENSOR_BASE_H
    105