CompassSensor::CompassSensor()
                  : SensorBase(NULL, NULL),
                    compass_fd(-1),
                    mCompassTimestamp(0),
                    mCompassInputReader(8)
{
    VFUNC_LOG;

    if(!strcmp(COMPASS_NAME, "USE_SYSFS")) {
        int result = find_name_by_sensor_type("in_magn_scale", "iio:device", 
                                              sensor_name);
        if(result) {
            LOGE("HAL:Cannot read secondary device name - (%d)", result);
        }
        dev_name = sensor_name;
    }
    LOGI_IF(PROCESS_VERBOSE, "HAL:Secondary Chip Id: %s", dev_name);

    if(inv_init_sysfs_attributes()) {
        LOGE("Error Instantiating Compass\n");
        return;
    }

    memset(mCachedCompassData, 0, sizeof(mCachedCompassData));

    LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:cat %s (%lld)", 
            compassSysFs.compass_orient, getTimestamp());
    FILE *fptr;
    fptr = fopen(compassSysFs.compass_orient, "r");
    if (fptr != NULL) {
        int om[9];
        if (fscanf(fptr, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 
               &om[0], &om[1], &om[2], &om[3], &om[4], &om[5],
               &om[6], &om[7], &om[8]) < 0 || fclose(fptr) < 0) {
            LOGE("HAL:Could not read compass mounting matrix");
        } else {
            LOGV_IF(EXTRA_VERBOSE, "HAL:compass mounting matrix: "
                    "%+d %+d %+d %+d %+d %+d %+d %+d %+d", om[0], om[1], om[2], 
                    om[3], om[4], om[5], om[6], om[7], om[8]);
            mCompassOrientation[0] = om[0];
            mCompassOrientation[1] = om[1];
            mCompassOrientation[2] = om[2];
            mCompassOrientation[3] = om[3];
            mCompassOrientation[4] = om[4];
            mCompassOrientation[5] = om[5];
            mCompassOrientation[6] = om[6];
            mCompassOrientation[7] = om[7];
            mCompassOrientation[8] = om[8];
        }
    }

    if (!isIntegrated()) {
        enable(ID_M, 0);
    }
}
CompassSensor::CompassSensor() 
                  : SensorBase(NULL, NULL),
                    compass_fd(-1),
                    mCompassTimestamp(0),
                    mCompassInputReader(8)
{
    VFUNC_LOG;

    if(inv_init_sysfs_attributes()) {
        LOGE("Error Instantiating Compass\n");
        return;
    }

    if (!strcmp(COMPASS_NAME, "INV_COMPASS")) {
        mI2CBus = COMPASS_BUS_SECONDARY;
    } else {
        mI2CBus = COMPASS_BUS_PRIMARY;
    }

    memset(mCachedCompassData, 0, sizeof(mCachedCompassData));

    LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:cat %s (%lld)", 
            compassSysFs.compass_orient, getTimestamp());
    FILE *fptr;
    fptr = fopen(compassSysFs.compass_orient, "r");
    if (fptr != NULL) {
        int om[9];
        fscanf(fptr, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 
               &om[0], &om[1], &om[2], &om[3], &om[4], &om[5],
               &om[6], &om[7], &om[8]);
        fclose(fptr);

        LOGV_IF(EXTRA_VERBOSE,
                "HAL:compass mounting matrix: "
                "%+d %+d %+d %+d %+d %+d %+d %+d %+d",
                om[0], om[1], om[2], om[3], om[4], om[5], om[6], om[7], om[8]);

        mCompassOrientation[0] = om[0];
        mCompassOrientation[1] = om[1];
        mCompassOrientation[2] = om[2];
        mCompassOrientation[3] = om[3];
        mCompassOrientation[4] = om[4];
        mCompassOrientation[5] = om[5];
        mCompassOrientation[6] = om[6];
        mCompassOrientation[7] = om[7];
        mCompassOrientation[8] = om[8];
    } else {
        LOGE("HAL:Couldn't read compass mounting matrix");
    }

    if (!isIntegrated()) {
        enable(ID_M, 0);
    }
}
int main(int argc, char **argv)
{
    char data[4];
    int i, res= 0;

    printf("\n"
           "****************************************************************\n"
           "*** NOTE:                                                    ***\n"
           "***       the HAL must be compiled with Low power quaternion ***\n"
           "***           and/or DMP screen orientation support.         ***\n"
           "***       'At least' one of the 4 Android virtual sensors    ***\n"
           "***           must be enabled.                               ***\n"
           "***                                                          ***\n"
           "*** Please perform gestures to see the output.               ***\n"
           "*** Press any key to stop the program.                       ***\n"
           "****************************************************************\n"
           "\n");

    res = inv_init_sysfs_attributes();
    if (res) {
        printf("GT:ERR-Can't allocate mem\n");
        return -1;
    }

    /* init Fds to poll for gesture data */
    init_fds();

    /* on Gesture/DMP supported features */
    if (enable_dmp_features(1) < 0) {
        printf("GT:ERR-Can't enable Gestures\n");
        return -1;
    }

    do {
        for (i = 0; i < NUM_DMP_FEATS; i++)
            read(pfd[i].fd, data, 4);
        poll(pfd, NUM_DMP_FEATS, POLL_TIME);
        parse_events(pfd, NUM_DMP_FEATS);
    } while (!_kbhit());

    /* off Gesture/DMP supported features */
    if (enable_dmp_features(0) < 0) {
        printf("GT:ERR-Can't disable Gestures\n");
        return -1;
    }

    /* release resources */
    close_fds();
    if (sysfs_names_ptr)
        free(sysfs_names_ptr);

    return res;
}
PressureSensor::PressureSensor(const char *sysfs_path) 
                  : SensorBase(NULL, NULL),
                    pressure_fd(-1)
{                                     
    VFUNC_LOG;

    mSysfsPath = sysfs_path;
    LOGI("pressuresensor path: %s", mSysfsPath);
    if(inv_init_sysfs_attributes()) {
        LOGE("Error Instantiating Pressure Sensor\n");
        return;
    } else {
        LOGI("HAL:Secondary Chip Id: %s", CHIP_ID);
    }
}
/*******************************************************************************
 *                       M a i n
 ******************************************************************************/
int main(int argc, char **argv)
{
    FILE *fptr;
    int self_test_status = 0;
    inv_error_t result;
    bias_dtype gyro_bias[3];
    bias_dtype gyro_scale;
    bias_dtype accel_bias[3];
    bias_dtype accel_scale;
    int scale_ratio;
    size_t packet_sz;
    int axis_sign = 1;
    unsigned char *buffer;
    long timestamp;
    long long temperature = 0;
    bool compass_present = true;
    int c;

    result= inv_init_sysfs_attributes();
    if (result)
        return -1;

    // Self-test for Non-Hub
    inv_init_storage_manager();

    // Register packet to be saved.
    result = inv_register_load_store(
                inv_db_load_func, inv_db_save_func,
                sizeof(save_data), INV_DB_SAVE_KEY);

    // Self-test for Android Hub
    if (android_hub() == true) {
        printf(" android_hub() == true \n");
        fptr = fopen(mpu.self_test, "r");
        if (fptr) {
            fscanf(fptr, "%d", &self_test_status);
            printf("\nSelf-Test:Hub:Self test result - "
                   "Gyro passed= %x, Accel passed= %x, Compass passed= %x\n",
                   (self_test_status & GYRO_PASS_STATUS_BIT), 
                   (self_test_status & ACCEL_PASS_STATUS_BIT) >> 1,
                   (self_test_status & COMPASS_PASS_STATUS_BIT) >> 2);
            fclose(fptr);
            result = 0;
        } else {
int main(int argc, char **argv)
{
    char data[4];
    int i, res= 0;

    res = inv_init_sysfs_attributes();
    if (res) {
        printf("GT:ERR-Can't allocate mem");
        return -1;
    }

    /* On Gesture/DMP supported features */
    enableDMPFeatures(1);

    /* init Fds to poll for Gesture data */
    initFds();

    /* prompt user to make gesture and how to stop program */
    printf("\n**Please make Gesture to see data.  Press any key to stop Prog**\n\n");

    do {
        for (i=0; i< numDMPFeatures; i++) {
            read(pfd[i].fd, data, 4);
        }

        poll(pfd, numDMPFeatures, POLL_TIME);

        for (i=0; i< numDMPFeatures; i++) {
           if(pfd[i].revents != 0) {
               switch(i) {
                   case tap:
                       tapHandler();
                       break;

                   case flick:
                       flickHandler();
                       break;

                   case gOrient:
                       googleOrientHandler();
                       break;

                   case orient:
                       orientHandler();
                       break;

                   default:
                       printf("GT:ERR-Not supported");
                       break;
               }
               pfd[i].revents= 0;	//no need. reset anyway
           }
        }

    } while (!_kbhit());

    /* Off DMP features */
    enableDMPFeatures(0);

    /* release resources */
    closeFds();
    if (sysfs_names_ptr) {
        free(sysfs_names_ptr);
    }

    printf("\nThank You!\n");

    return res;
}
CompassSensor::CompassSensor() 
                  : SensorBase(COMPASS_NAME, NULL),
                    mCompassTimestamp(0),
                    mCompassInputReader(8),
                    mCoilsResetFd(0)
{
    FILE *fptr;

    VFUNC_LOG;

    mYasCompass = false;
    if(!strcmp(dev_name, "USE_SYSFS")) {
        char sensor_name[20]; 
        find_name_by_sensor_type("in_magn_x_raw", "iio:device", sensor_name);
        strncpy(dev_full_name, sensor_name,
                sizeof(dev_full_name) / sizeof(dev_full_name[0]));
        if(!strncmp(dev_full_name, "yas", 3)) {
            mYasCompass = true;
        }
    } else {

#ifdef COMPASS_YAS53x
        /* for YAS53x compasses, dev_name is just a prefix, 
           we need to find the actual name */
        if (fill_dev_full_name_by_prefix(dev_name, 
                dev_full_name, sizeof(dev_full_name) / sizeof(dev_full_name[0]))) {
            LOGE("Cannot find Yamaha device with prefix name '%s' - "
                 "magnetometer will likely not work.", dev_name);
        } else {
            mYasCompass = true;
        }
#else
        strncpy(dev_full_name, dev_name,
                sizeof(dev_full_name) / sizeof(dev_full_name[0]));
#endif

}

    if (inv_init_sysfs_attributes()) {
        LOGE("Error Instantiating Compass\n");
        return;
    }

    if (!strcmp(dev_full_name, "INV_COMPASS")) {
        mI2CBus = COMPASS_BUS_SECONDARY;
    } else {
        mI2CBus = COMPASS_BUS_PRIMARY;
    }

    memset(mCachedCompassData, 0, sizeof(mCachedCompassData));

    if (!isIntegrated()) {
        enable(ID_M, 0);
    }

    LOGV_IF(SYSFS_VERBOSE, "HAL:compass name: %s", dev_full_name);
    enable_iio_sysfs();

    LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:cat %s (%lld)", 
            compassSysFs.compass_orient, getTimestamp());
    fptr = fopen(compassSysFs.compass_orient, "r");
    if (fptr != NULL) {
        int om[9];
        if (fscanf(fptr, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 
               &om[0], &om[1], &om[2], &om[3], &om[4], &om[5],
               &om[6], &om[7], &om[8]) < 0 || fclose(fptr)) {
            LOGE("HAL:could not read compass mounting matrix");
        } else {

            LOGV_IF(EXTRA_VERBOSE,
                    "HAL:compass mounting matrix: "
                    "%+d %+d %+d %+d %+d %+d %+d %+d %+d",
                    om[0], om[1], om[2], om[3], om[4], om[5], om[6], om[7], om[8]);

            mCompassOrientation[0] = om[0];
            mCompassOrientation[1] = om[1];
            mCompassOrientation[2] = om[2];
            mCompassOrientation[3] = om[3];
            mCompassOrientation[4] = om[4];
            mCompassOrientation[5] = om[5];
            mCompassOrientation[6] = om[6];
            mCompassOrientation[7] = om[7];
            mCompassOrientation[8] = om[8];
        }
    }

    if(mYasCompass) {
        mCoilsResetFd = fopen(compassSysFs.compass_attr_1, "r+");
        if (fptr == NULL) {
            LOGE("HAL:Could not open compass overunderflow");
        }
    }
}