static void init_iio() { int i, j; char iio_chip[10]; int dev_num; for(j=0; j< CHIP_NUM; j++) { for (i=0; i<strlen(chip_name[j]); i++) { iio_chip[i] = tolower(chip_name[j][i]); } iio_chip[strlen(chip_name[0])] = '\0'; dev_num = find_type_by_name(iio_chip, "iio:device"); if(dev_num >= 0) { iio_initialized = 1; iio_dev_num = dev_num; chip_ind = j; } } }
int main(int argc, char **argv) { int i, j, k, toread; FILE *fp_ev; int fp; char *data; size_t read_size; struct iio_event_data dat; char *BaseDirectoryName, *TriggerDirectoryName, *RingBufferDirectoryName; BaseDirectoryName = find_type_by_name(device_name, "device"); if (BaseDirectoryName == NULL) { printf("Failed to find the %s \n", device_name); return -1; } TriggerDirectoryName = find_type_by_name(trigger_name, "trigger"); if (TriggerDirectoryName == NULL) { printf("Failed to find the %s\n", trigger_name); return -1; } RingBufferDirectoryName = find_ring_subelement(BaseDirectoryName, "ring_buffer"); if (RingBufferDirectoryName == NULL) { printf("Failed to find ring buffer\n"); return -1; } if (write_sysfs_string_and_verify("trigger/current_trigger", BaseDirectoryName, (char *)trigger_name) < 0) { printf("Failed to write current_trigger file \n"); return -1; } /* Setup ring buffer parameters */ if (write_sysfs_int("length", RingBufferDirectoryName, RingLength) < 0) { printf("Failed to open the ring buffer length file \n"); return -1; } /* Enable the ring buffer */ if (write_sysfs_int("ring_enable", RingBufferDirectoryName, 1) < 0) { printf("Failed to open the ring buffer control file \n"); return -1; }; data = malloc(size_from_scanmode(NumVals, scan_ts)*RingLength); if (!data) { printf("Could not allocate space for usespace data store\n"); return -1; } /* Attempt to open non blocking the access dev */ fp = open(ring_access, O_RDONLY | O_NONBLOCK); if (fp == -1) { /*If it isn't there make the node */ printf("Failed to open %s\n", ring_access); return -1; } /* Attempt to open the event access dev (blocking this time) */ fp_ev = fopen(ring_event, "rb"); if (fp_ev == NULL) { printf("Failed to open %s\n", ring_event); return -1; } /* Wait for events 10 times */ for (j = 0; j < 10; j++) { read_size = fread(&dat, 1, sizeof(struct iio_event_data), fp_ev); switch (dat.id) { case IIO_EVENT_CODE_RING_100_FULL: toread = RingLength; break; case IIO_EVENT_CODE_RING_75_FULL: toread = RingLength*3/4; break; case IIO_EVENT_CODE_RING_50_FULL: toread = RingLength/2; break; default: printf("Unexpecteded event code\n"); continue; } read_size = read(fp, data, toread*size_from_scanmode(NumVals, scan_ts)); if (read_size == -EAGAIN) { printf("nothing available \n"); continue; } for (i = 0; i < read_size/size_from_scanmode(NumVals, scan_ts); i++) { for (k = 0; k < NumVals; k++) { __s16 val = *(__s16 *)(&data[i*size_from_scanmode(NumVals, scan_ts) + (k)*2]); printf("%05d ", val); } printf(" %lld\n", *(__s64 *)(&data[(i+1)*size_from_scanmode(NumVals, scan_ts) - sizeof(__s64)])); } } /* Stop the ring buffer */ if (write_sysfs_int("ring_enable", RingBufferDirectoryName, 0) < 0) { printf("Failed to open the ring buffer control file \n"); return -1; }; /* Disconnect from the trigger - writing something that doesn't exist.*/ write_sysfs_string_and_verify("trigger/current_trigger", BaseDirectoryName, "NULL"); free(BaseDirectoryName); free(TriggerDirectoryName); free(RingBufferDirectoryName); free(data); return 0; }
int main(int argc, char **argv) { unsigned long buf_len = 240; int ret, c, i; char *trigger_name = NULL; int datardytrigger = 1; int trig_num; char *dummy; char chip_name[10]; char device_name[10]; char sysfs[100]; gyro_data_is_enabled = 0; accel_data_is_enabled = 0; compass_data_is_enabled = 0; quaternion_data_is_enabled = 0; while ((c = getopt(argc, argv, "lcd:e:rmp")) != -1) { switch (c) { case 'c': has_compass = 1; break; case 'p': has_pressure = 1; break; case 'd': disable_delay = strtoul(optarg, &dummy, 10); break; case 'e': enable_delay = strtoul(optarg, &dummy, 10); break; case 'r': enable_random_delay = 1; break; case 'm': enable_motion_on = 1; break; case '?': return -1; } } inv_get_sysfs_path(sysfs); printf("sss:::%s\n", sysfs); if (inv_get_chip_name(chip_name) != INV_SUCCESS) { printf("get chip name fail\n"); exit(0); } printf("chip_name=%s\n", chip_name); if (INV_SUCCESS != inv_check_key()) printf("key check fail\n"); else printf("key authenticated\n"); for (i=0; i<strlen(chip_name); i++) { device_name[i] = tolower(chip_name[i]); } device_name[strlen(chip_name)] = '\0'; printf("device name: %s\n", device_name); /* Find the device requested */ dev_num = find_type_by_name(device_name, "iio:device"); if (dev_num < 0) { printf("Failed to find the %s\n", device_name); ret = -ENODEV; goto error_ret; } printf("iio device number being used is %d\n", dev_num); asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); printf("allco=%x\n", (int)dev_dir_name); if (trigger_name == NULL) { /* * Build the trigger name. If it is device associated it's * name is <device_name>_dev[n] where n matches the device * number found above */ ret = asprintf(&trigger_name, "%s-dev%d", device_name, dev_num); if (ret < 0) { ret = -ENOMEM; goto error_ret; } } /* Verify the trigger exists */ trig_num = find_type_by_name(trigger_name, "trigger"); if (trig_num < 0) { printf("Failed to find the trigger %s\n", trigger_name); ret = -ENODEV; goto error_free_triggername; } printf("iio trigger number being used is %d\n", trig_num); ret = asprintf(&buf_dir_name, "%siio:device%d/buffer", iio_dir, dev_num); if (ret < 0) { ret = -ENOMEM; goto error_free_triggername; } enable_enable_main(0); ret = write_sysfs_int_and_verify("power_state", dev_dir_name, 1); /* * Parse the files in scan_elements to identify what channels are * present */ ret = 0; setup_dmp(dev_dir_name); printf("%s %s\n", dev_dir_name, trigger_name); /* Set the device trigger to be the data rdy trigger found above */ ret = write_sysfs_string_and_verify("trigger/current_trigger", dev_dir_name, trigger_name); if (ret < 0) { printf("Failed to write current_trigger file\n"); goto error_free_buf_dir_name; } /* Setup ring buffer parameters */ /* length must be even number because iio_store_to_sw_ring is expecting half pointer to be equal to the read pointer, which is impossible when buflen is odd number. This is actually a bug in the code */ ret = write_sysfs_int("length", buf_dir_name, buf_len*2); if (ret < 0) goto exit_here; enable_enable_main(1); inv_create_thread(); exit_here: error_free_buf_dir_name: free(buf_dir_name); error_free_triggername: if (datardytrigger) free(trigger_name); error_ret: return ret; }
int main(int argc, char **argv) { struct iio_event_data event; const char *device_name; char *chrdev_name; int ret; int dev_num; int fd, event_fd; if (argc <= 1) { printf("Usage: %s <device_name>\n", argv[0]); return -1; } device_name = argv[1]; dev_num = find_type_by_name(device_name, "iio:device"); if (dev_num >= 0) { printf("Found IIO device with name %s with device number %d\n", device_name, dev_num); ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num); if (ret < 0) { return -ENOMEM; } } else { /* If we can't find a IIO device by name assume device_name is a IIO chrdev */ chrdev_name = strdup(device_name); if (!chrdev_name) return -ENOMEM; } fd = open(chrdev_name, 0); if (fd == -1) { ret = -errno; fprintf(stdout, "Failed to open %s\n", chrdev_name); goto error_free_chrdev_name; } ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd); if (ret == -1 || event_fd == -1) { ret = -errno; fprintf(stdout, "Failed to retrieve event fd\n"); if (close(fd) == -1) perror("Failed to close character device file"); goto error_free_chrdev_name; } if (close(fd) == -1) { ret = -errno; goto error_free_chrdev_name; } while (true) { ret = read(event_fd, &event, sizeof(event)); if (ret == -1) { if (errno == EAGAIN) { printf("nothing available\n"); continue; } else { ret = -errno; perror("Failed to read event from device"); break; } } print_event(&event); } if (close(event_fd) == -1) perror("Failed to close event file"); error_free_chrdev_name: free(chrdev_name); return ret; }
int CompassSensor::inv_init_sysfs_attributes(void) { VFUNC_LOG; unsigned char i = 0; char sysfs_path[MAX_SYSFS_NAME_LEN], tbuf[2]; char *sptr; char **dptr; int num; const char* compass = dev_full_name; pathP = (char*)malloc( sizeof(char[COMPASS_MAX_SYSFS_ATTRB][MAX_SYSFS_NAME_LEN])); sptr = pathP; dptr = (char**)&compassSysFs; if (sptr == NULL) return -1; do { *dptr++ = sptr; sptr += sizeof(char[MAX_SYSFS_NAME_LEN]); } while (++i < COMPASS_MAX_SYSFS_ATTRB); // get proper (in absolute/relative) IIO path & build sysfs paths sprintf(sysfs_path, "%s%d", "/sys/bus/iio/devices/iio:device", find_type_by_name(compass, "iio:device")); #if defined COMPASS_AK8975 inv_get_input_number(compass, &num); tbuf[0] = num + 0x30; tbuf[1] = 0; sprintf(sysfs_path, "%s%s", "sys/class/input/input", tbuf); strcat(sysfs_path, "/ak8975"); sprintf(compassSysFs.compass_enable, "%s%s", sysfs_path, "/enable"); sprintf(compassSysFs.compass_rate, "%s%s", sysfs_path, "/rate"); sprintf(compassSysFs.compass_scale, "%s%s", sysfs_path, "/scale"); sprintf(compassSysFs.compass_orient, "%s%s", sysfs_path, "/compass_matrix"); #else /* IIO */ sprintf(compassSysFs.chip_enable, "%s%s", sysfs_path, "/buffer/enable"); sprintf(compassSysFs.in_timestamp_en, "%s%s", sysfs_path, "/scan_elements/in_timestamp_en"); sprintf(compassSysFs.buffer_length, "%s%s", sysfs_path, "/buffer/length"); sprintf(compassSysFs.compass_x_fifo_enable, "%s%s", sysfs_path, "/scan_elements/in_magn_x_en"); sprintf(compassSysFs.compass_y_fifo_enable, "%s%s", sysfs_path, "/scan_elements/in_magn_y_en"); sprintf(compassSysFs.compass_z_fifo_enable, "%s%s", sysfs_path, "/scan_elements/in_magn_z_en"); sprintf(compassSysFs.compass_rate, "%s%s", sysfs_path, "/sampling_frequency"); sprintf(compassSysFs.compass_scale, "%s%s", sysfs_path, "/in_magn_scale"); sprintf(compassSysFs.compass_orient, "%s%s", sysfs_path, "/compass_matrix"); if(mYasCompass) { sprintf(compassSysFs.compass_attr_1, "%s%s", sysfs_path, "/overunderflow"); } #endif #if 0 // test print sysfs paths dptr = (char**)&compassSysFs; LOGI("sysfs path base: %s", sysfs_path); for (i = 0; i < COMPASS_MAX_SYSFS_ATTRB; i++) { LOGE("HAL:sysfs path: %s", *dptr++); } #endif return 0; }
void CompassSensor::enable_iio_sysfs() { VFUNC_LOG; int tempFd = 0; char iio_device_node[MAX_CHIP_ID_LEN]; FILE *tempFp = NULL; const char* compass = dev_full_name; LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:echo %d > %s (%lld)", 1, compassSysFs.in_timestamp_en, getTimestamp()); write_sysfs_int(compassSysFs.in_timestamp_en, 1); LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:echo %d > %s (%lld)", IIO_BUFFER_LENGTH, compassSysFs.buffer_length, getTimestamp()); tempFp = fopen(compassSysFs.buffer_length, "w"); if (tempFp == NULL) { LOGE("HAL:could not open buffer length"); } else { if (fprintf(tempFp, "%d", IIO_BUFFER_LENGTH) < 0 || fclose(tempFp) < 0) { LOGE("HAL:could not write buffer length"); } } sprintf(iio_device_node, "%s%d", "/dev/iio:device", find_type_by_name(compass, "iio:device")); compass_fd = open(iio_device_node, O_RDONLY); int res = errno; if (compass_fd < 0) { LOGE("HAL:could not open '%s' iio device node in path '%s' - " "error '%s' (%d)", compass, iio_device_node, strerror(res), res); } else { LOGV_IF(EXTRA_VERBOSE, "HAL:iio %s, compass_fd opened : %d", compass, compass_fd); } /* TODO: need further tests for optimization to reduce context-switch LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:echo 1 > %s (%lld)", compassSysFs.compass_x_fifo_enable, getTimestamp()); tempFd = open(compassSysFs.compass_x_fifo_enable, O_RDWR); res = errno; if (tempFd > 0) { res = enable_sysfs_sensor(tempFd, 1); } else { LOGE("HAL:open of %s failed with '%s' (%d)", compassSysFs.compass_x_fifo_enable, strerror(res), res); } LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:echo 1 > %s (%lld)", compassSysFs.compass_y_fifo_enable, getTimestamp()); tempFd = open(compassSysFs.compass_y_fifo_enable, O_RDWR); res = errno; if (tempFd > 0) { res = enable_sysfs_sensor(tempFd, 1); } else { LOGE("HAL:open of %s failed with '%s' (%d)", compassSysFs.compass_y_fifo_enable, strerror(res), res); } LOGV_IF(SYSFS_VERBOSE, "HAL:sysfs:echo 1 > %s (%lld)", compassSysFs.compass_z_fifo_enable, getTimestamp()); tempFd = open(compassSysFs.compass_z_fifo_enable, O_RDWR); res = errno; if (tempFd > 0) { res = enable_sysfs_sensor(tempFd, 1); } else { LOGE("HAL:open of %s failed with '%s' (%d)", compassSysFs.compass_z_fifo_enable, strerror(res), res); } */ }
CwMcuSensor::CwMcuSensor() : SensorBase(NULL, "CwMcuSensor") , mEnabled(0) , mInputReader(IIO_MAX_BUFF_SIZE) , time_slope(1) , time_offset(0) , init_trigger_done(false) { int rc; memset(last_mcu_timestamp, 0, sizeof(last_mcu_timestamp)); memset(last_cpu_timestamp, 0, sizeof(last_cpu_timestamp)); for (int i=0; i<numSensors; i++) { offset_reset[i] = true; } mPendingEvents[CW_ACCELERATION].version = sizeof(sensors_event_t); mPendingEvents[CW_ACCELERATION].sensor = ID_A; mPendingEvents[CW_ACCELERATION].type = SENSOR_TYPE_ACCELEROMETER; mPendingEvents[CW_ACCELERATION].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH; mPendingEvents[CW_MAGNETIC].version = sizeof(sensors_event_t); mPendingEvents[CW_MAGNETIC].sensor = ID_M; mPendingEvents[CW_MAGNETIC].type = SENSOR_TYPE_MAGNETIC_FIELD; mPendingEvents[CW_GYRO].version = sizeof(sensors_event_t); mPendingEvents[CW_GYRO].sensor = ID_GY; mPendingEvents[CW_GYRO].type = SENSOR_TYPE_GYROSCOPE; mPendingEvents[CW_GYRO].gyro.status = SENSOR_STATUS_ACCURACY_HIGH; mPendingEvents[CW_LIGHT].version = sizeof(sensors_event_t); mPendingEvents[CW_LIGHT].sensor = ID_L; mPendingEvents[CW_LIGHT].type = SENSOR_TYPE_LIGHT; memset(mPendingEvents[CW_LIGHT].data, 0, sizeof(mPendingEvents[CW_LIGHT].data)); mPendingEvents[CW_PRESSURE].version = sizeof(sensors_event_t); mPendingEvents[CW_PRESSURE].sensor = ID_PS; mPendingEvents[CW_PRESSURE].type = SENSOR_TYPE_PRESSURE; memset(mPendingEvents[CW_PRESSURE].data, 0, sizeof(mPendingEvents[CW_PRESSURE].data)); mPendingEvents[CW_ORIENTATION].version = sizeof(sensors_event_t); mPendingEvents[CW_ORIENTATION].sensor = ID_O; mPendingEvents[CW_ORIENTATION].type = SENSOR_TYPE_ORIENTATION; mPendingEvents[CW_ORIENTATION].orientation.status = SENSOR_STATUS_ACCURACY_HIGH; mPendingEvents[CW_ROTATIONVECTOR].version = sizeof(sensors_event_t); mPendingEvents[CW_ROTATIONVECTOR].sensor = ID_RV; mPendingEvents[CW_ROTATIONVECTOR].type = SENSOR_TYPE_ROTATION_VECTOR; mPendingEvents[CW_LINEARACCELERATION].version = sizeof(sensors_event_t); mPendingEvents[CW_LINEARACCELERATION].sensor = ID_LA; mPendingEvents[CW_LINEARACCELERATION].type = SENSOR_TYPE_LINEAR_ACCELERATION; mPendingEvents[CW_GRAVITY].version = sizeof(sensors_event_t); mPendingEvents[CW_GRAVITY].sensor = ID_G; mPendingEvents[CW_GRAVITY].type = SENSOR_TYPE_GRAVITY; mPendingEvents[CW_MAGNETIC_UNCALIBRATED].version = sizeof(sensors_event_t); mPendingEvents[CW_MAGNETIC_UNCALIBRATED].sensor = ID_CW_MAGNETIC_UNCALIBRATED; mPendingEvents[CW_MAGNETIC_UNCALIBRATED].type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED; mPendingEvents[CW_GYROSCOPE_UNCALIBRATED].version = sizeof(sensors_event_t); mPendingEvents[CW_GYROSCOPE_UNCALIBRATED].sensor = ID_CW_GYROSCOPE_UNCALIBRATED; mPendingEvents[CW_GYROSCOPE_UNCALIBRATED].type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED; mPendingEvents[CW_GAME_ROTATION_VECTOR].version = sizeof(sensors_event_t); mPendingEvents[CW_GAME_ROTATION_VECTOR].sensor = ID_CW_GAME_ROTATION_VECTOR; mPendingEvents[CW_GAME_ROTATION_VECTOR].type = SENSOR_TYPE_GAME_ROTATION_VECTOR; mPendingEvents[CW_GEOMAGNETIC_ROTATION_VECTOR].version = sizeof(sensors_event_t); mPendingEvents[CW_GEOMAGNETIC_ROTATION_VECTOR].sensor = ID_CW_GEOMAGNETIC_ROTATION_VECTOR; mPendingEvents[CW_GEOMAGNETIC_ROTATION_VECTOR].type = SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR; mPendingEvents[CW_SIGNIFICANT_MOTION].version = sizeof(sensors_event_t); mPendingEvents[CW_SIGNIFICANT_MOTION].sensor = ID_CW_SIGNIFICANT_MOTION; mPendingEvents[CW_SIGNIFICANT_MOTION].type = SENSOR_TYPE_SIGNIFICANT_MOTION; mPendingEvents[CW_STEP_DETECTOR].version = sizeof(sensors_event_t); mPendingEvents[CW_STEP_DETECTOR].sensor = ID_CW_STEP_DETECTOR; mPendingEvents[CW_STEP_DETECTOR].type = SENSOR_TYPE_STEP_DETECTOR; mPendingEvents[CW_STEP_COUNTER].version = sizeof(sensors_event_t); mPendingEvents[CW_STEP_COUNTER].sensor = ID_CW_STEP_COUNTER; mPendingEvents[CW_STEP_COUNTER].type = SENSOR_TYPE_STEP_COUNTER; mPendingEvents[CW_ACCELERATION_W].version = sizeof(sensors_event_t); mPendingEvents[CW_ACCELERATION_W].sensor = ID_A_W; mPendingEvents[CW_ACCELERATION_W].type = SENSOR_TYPE_ACCELEROMETER; mPendingEvents[CW_ACCELERATION_W].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH; mPendingEvents[CW_MAGNETIC_W].version = sizeof(sensors_event_t); mPendingEvents[CW_MAGNETIC_W].sensor = ID_M_W; mPendingEvents[CW_MAGNETIC_W].type = SENSOR_TYPE_MAGNETIC_FIELD; mPendingEvents[CW_GYRO_W].version = sizeof(sensors_event_t); mPendingEvents[CW_GYRO_W].sensor = ID_GY_W; mPendingEvents[CW_GYRO_W].type = SENSOR_TYPE_GYROSCOPE; mPendingEvents[CW_GYRO_W].gyro.status = SENSOR_STATUS_ACCURACY_HIGH; mPendingEvents[CW_PRESSURE_W].version = sizeof(sensors_event_t); mPendingEvents[CW_PRESSURE_W].sensor = ID_PS_W; mPendingEvents[CW_PRESSURE_W].type = SENSOR_TYPE_PRESSURE; memset(mPendingEvents[CW_PRESSURE_W].data, 0, sizeof(mPendingEvents[CW_PRESSURE_W].data)); mPendingEvents[CW_ORIENTATION_W].version = sizeof(sensors_event_t); mPendingEvents[CW_ORIENTATION_W].sensor = ID_O_W; mPendingEvents[CW_ORIENTATION_W].type = SENSOR_TYPE_ORIENTATION; mPendingEvents[CW_ORIENTATION_W].orientation.status = SENSOR_STATUS_ACCURACY_HIGH; mPendingEvents[CW_ROTATIONVECTOR_W].version = sizeof(sensors_event_t); mPendingEvents[CW_ROTATIONVECTOR_W].sensor = ID_RV_W; mPendingEvents[CW_ROTATIONVECTOR_W].type = SENSOR_TYPE_ROTATION_VECTOR; mPendingEvents[CW_LINEARACCELERATION_W].version = sizeof(sensors_event_t); mPendingEvents[CW_LINEARACCELERATION_W].sensor = ID_LA_W; mPendingEvents[CW_LINEARACCELERATION_W].type = SENSOR_TYPE_LINEAR_ACCELERATION; mPendingEvents[CW_GRAVITY_W].version = sizeof(sensors_event_t); mPendingEvents[CW_GRAVITY_W].sensor = ID_G_W; mPendingEvents[CW_GRAVITY_W].type = SENSOR_TYPE_GRAVITY; mPendingEvents[CW_MAGNETIC_UNCALIBRATED_W].version = sizeof(sensors_event_t); mPendingEvents[CW_MAGNETIC_UNCALIBRATED_W].sensor = ID_CW_MAGNETIC_UNCALIBRATED_W; mPendingEvents[CW_MAGNETIC_UNCALIBRATED_W].type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED; mPendingEvents[CW_GYROSCOPE_UNCALIBRATED_W].version = sizeof(sensors_event_t); mPendingEvents[CW_GYROSCOPE_UNCALIBRATED_W].sensor = ID_CW_GYROSCOPE_UNCALIBRATED_W; mPendingEvents[CW_GYROSCOPE_UNCALIBRATED_W].type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED; mPendingEvents[CW_GAME_ROTATION_VECTOR_W].version = sizeof(sensors_event_t); mPendingEvents[CW_GAME_ROTATION_VECTOR_W].sensor = ID_CW_GAME_ROTATION_VECTOR_W; mPendingEvents[CW_GAME_ROTATION_VECTOR_W].type = SENSOR_TYPE_GAME_ROTATION_VECTOR; mPendingEvents[CW_GEOMAGNETIC_ROTATION_VECTOR_W].version = sizeof(sensors_event_t); mPendingEvents[CW_GEOMAGNETIC_ROTATION_VECTOR_W].sensor = ID_CW_GEOMAGNETIC_ROTATION_VECTOR_W; mPendingEvents[CW_GEOMAGNETIC_ROTATION_VECTOR_W].type = SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR; mPendingEvents[CW_STEP_DETECTOR_W].version = sizeof(sensors_event_t); mPendingEvents[CW_STEP_DETECTOR_W].sensor = ID_CW_STEP_DETECTOR_W; mPendingEvents[CW_STEP_DETECTOR_W].type = SENSOR_TYPE_STEP_DETECTOR; mPendingEvents[CW_STEP_COUNTER_W].version = sizeof(sensors_event_t); mPendingEvents[CW_STEP_COUNTER_W].sensor = ID_CW_STEP_COUNTER_W; mPendingEvents[CW_STEP_COUNTER_W].type = SENSOR_TYPE_STEP_COUNTER; mPendingEventsFlush.version = META_DATA_VERSION; mPendingEventsFlush.sensor = 0; mPendingEventsFlush.type = SENSOR_TYPE_META_DATA; char buffer_access[PATH_MAX]; const char *device_name = "CwMcuSensor"; int rate = 20, dev_num, enabled = 0, i; dev_num = find_type_by_name(device_name, "iio:device"); if (dev_num < 0) dev_num = 0; snprintf(buffer_access, sizeof(buffer_access), "/dev/iio:device%d", dev_num); data_fd = open(buffer_access, O_RDWR); if (data_fd < 0) { ALOGE("CwMcuSensor::CwMcuSensor: open file '%s' failed: %s\n", buffer_access, strerror(errno)); } if (data_fd >= 0) { int i; int fd; int iio_buf_size; ALOGV("%s: 11 Before pthread_mutex_lock()\n", __func__); pthread_mutex_lock(&sys_fs_mutex); ALOGV("%s: 11 Acquired pthread_mutex_lock()\n", __func__); strcpy(fixed_sysfs_path,"/sys/class/htc_sensorhub/sensor_hub/"); fixed_sysfs_path_len = strlen(fixed_sysfs_path); snprintf(mDevPath, sizeof(mDevPath), "%s%s", fixed_sysfs_path, "iio"); snprintf(mTriggerName, sizeof(mTriggerName), "%s-dev%d", device_name, dev_num); ALOGV("CwMcuSensor::CwMcuSensor: mTriggerName = %s\n", mTriggerName); if (sysfs_set_input_attr_by_int("buffer/enable", 0) < 0) { ALOGE("CwMcuSensor::CwMcuSensor: set IIO buffer enable failed00: %s\n", strerror(errno)); } // This is a piece of paranoia that retry for current_trigger for (i = 0; i < INIT_TRIGGER_RETRY; i++) { rc = sysfs_set_input_attr("trigger/current_trigger", mTriggerName, strlen(mTriggerName)); if (rc < 0) { if (sysfs_set_input_attr_by_int("buffer/enable", 0) < 0) { ALOGE("CwMcuSensor::CwMcuSensor: set IIO buffer enable failed11: %s\n", strerror(errno)); } ALOGE("CwMcuSensor::CwMcuSensor: set current trigger failed: rc = %d, strerr() = %s" ", i = %d\n", rc, strerror(errno), i); } else { init_trigger_done = true; break; } } iio_buf_size = IIO_MAX_BUFF_SIZE; for (i = 0; i < IIO_BUF_SIZE_RETRY; i++) { if (sysfs_set_input_attr_by_int("buffer/length", iio_buf_size) < 0) { ALOGE("CwMcuSensor::CwMcuSensor: set IIO buffer length (%d) failed: %s\n", iio_buf_size, strerror(errno)); } else { if (sysfs_set_input_attr_by_int("buffer/enable", 1) < 0) { ALOGE("CwMcuSensor::CwMcuSensor: set IIO buffer enable failed22: %s, " "i = %d, iio_buf_size = %d\n", strerror(errno), i, iio_buf_size); } else { ALOGI("CwMcuSensor::CwMcuSensor: set IIO buffer length success: %d\n", iio_buf_size); break; } } iio_buf_size /= 2; } strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "calibrator_en"); fd = open(fixed_sysfs_path, O_RDWR); if (fd >= 0) { static const char buf[] = "12"; rc = write(fd, buf, sizeof(buf) - 1); if (rc < 0) { ALOGE("%s: write buf = %s, failed: %s", __func__, buf, strerror(errno)); } close(fd); } else { ALOGE("%s open %s failed: %s", __func__, fixed_sysfs_path, strerror(errno)); } pthread_mutex_unlock(&sys_fs_mutex); ALOGV("%s: data_fd = %d", __func__, data_fd); ALOGV("%s: iio_device_path = %s", __func__, buffer_access); ALOGV("%s: ctrl sysfs_path = %s", __func__, fixed_sysfs_path); setEnable(0, 1); // Inside this function call, we use sys_fs_mutex } int gs_temp_data[G_SENSOR_CALIBRATION_DATA_SIZE] = {0}; int compass_temp_data[COMPASS_CALIBRATION_DATA_SIZE] = {0}; ALOGV("%s: 22 Before pthread_mutex_lock()\n", __func__); pthread_mutex_lock(&sys_fs_mutex); ALOGV("%s: 22 Acquired pthread_mutex_lock()\n", __func__); //Sensor Calibration init . Waiting for firmware ready rc = cw_read_calibrator_file(CW_MAGNETIC, SAVE_PATH_MAG, compass_temp_data); if (rc == 0) { ALOGD("Get compass calibration data from data/misc/ x is %d ,y is %d ,z is %d\n", compass_temp_data[0], compass_temp_data[1], compass_temp_data[2]); strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "calibrator_data_mag"); cw_save_calibrator_file(CW_MAGNETIC, fixed_sysfs_path, compass_temp_data); } else { ALOGI("Compass calibration data does not exist\n"); } rc = cw_read_calibrator_file(CW_ACCELERATION, SAVE_PATH_ACC, gs_temp_data); if (rc == 0) { ALOGD("Get g-sensor user calibration data from data/misc/ x is %d ,y is %d ,z is %d\n", gs_temp_data[0],gs_temp_data[1],gs_temp_data[2]); strcpy(&fixed_sysfs_path[fixed_sysfs_path_len], "calibrator_data_acc"); if(!(gs_temp_data[0] == 0 && gs_temp_data[1] == 0 && gs_temp_data[2] == 0 )) { cw_save_calibrator_file(CW_ACCELERATION, fixed_sysfs_path, gs_temp_data); } } else { ALOGI("G-Sensor user calibration data does not exist\n"); } pthread_mutex_unlock(&sys_fs_mutex); pthread_create(&sync_time_thread, (const pthread_attr_t *) NULL, sync_time_thread_run, (void *)this); }
int main(int argc, char **argv) { int ret; int i, j, k, toread; FILE *fp_ev; int fp; char *trigger_name, *dev_dir_name, *buf_dir_name; char *data; size_t read_size; struct iio_event_data dat; int dev_num, trig_num; char *buffer_access, *buffer_event; const char *iio_dir = "/sys/bus/iio/devices/"; int scan_size; float gain = 1; /* Find out which iio device is the accelerometer. */ dev_num = find_type_by_name(device_name, "device"); if (dev_num < 0) { printf("Failed to find the %s\n", device_name); ret = -ENODEV; goto error_ret; } printf("iio device number being used is %d\n", dev_num); asprintf(&dev_dir_name, "%sdevice%d", iio_dir, dev_num); /* * Build the trigger name. * In this case we want the lis3l02dq's data ready trigger * for this lis3l02dq. The naming is lis3l02dq_dev[n], where * n matches the device number found above. */ ret = asprintf(&trigger_name, "%s%d", trigger_name_base, dev_num); if (ret < 0) { ret = -ENOMEM; goto error_free_dev_dir_name; } /* * Find the trigger by name. * This is techically unecessary here as we only need to * refer to the trigger by name and that name is already * known. */ trig_num = find_type_by_name(trigger_name, "trigger"); if (trig_num < 0) { printf("Failed to find the %s\n", trigger_name); ret = -ENODEV; goto error_free_triggername; } printf("iio trigger number being used is %d\n", trig_num); /* * Read in the scale value - in a more generic case, first * check for accel_scale, then the indivual channel scales */ ret = read_sysfs_float("accel_scale", dev_dir_name, &gain); if (ret) goto error_free_triggername;; /* * Construct the directory name for the associated buffer. * As we know that the lis3l02dq has only one buffer this may * be built rather than found. */ ret = asprintf(&buf_dir_name, "%sdevice%d:buffer0", iio_dir, dev_num); if (ret < 0) { ret = -ENOMEM; goto error_free_triggername; } /* Set the device trigger to be the data rdy trigger found above */ ret = write_sysfs_string_and_verify("trigger/current_trigger", dev_dir_name, trigger_name); if (ret < 0) { printf("Failed to write current_trigger file\n"); goto error_free_buf_dir_name; } /* Setup ring buffer parameters */ ret = write_sysfs_int("length", buf_dir_name, buf_len); if (ret < 0) goto error_free_buf_dir_name; /* Enable the buffer */ ret = write_sysfs_int("ring_enable", buf_dir_name, 1); if (ret < 0) goto error_free_buf_dir_name; data = malloc(size_from_scanmode(num_vals, scan_ts)*buf_len); if (!data) { ret = -ENOMEM; goto error_free_buf_dir_name; } ret = asprintf(&buffer_access, "/dev/device%d:buffer0:access0", dev_num); if (ret < 0) { ret = -ENOMEM; goto error_free_data; } ret = asprintf(&buffer_event, "/dev/device%d:buffer0:event0", dev_num); if (ret < 0) { ret = -ENOMEM; goto error_free_data; } /* Attempt to open non blocking the access dev */ fp = open(buffer_access, O_RDONLY | O_NONBLOCK); if (fp == -1) { /*If it isn't there make the node */ printf("Failed to open %s\n", buffer_access); ret = -errno; goto error_free_buffer_event; } /* Attempt to open the event access dev (blocking this time) */ fp_ev = fopen(buffer_event, "rb"); if (fp_ev == NULL) { printf("Failed to open %s\n", buffer_event); ret = -errno; goto error_close_buffer_access; } /* Wait for events 10 times */ for (j = 0; j < num_loops; j++) { read_size = fread(&dat, 1, sizeof(struct iio_event_data), fp_ev); switch (dat.id) { case IIO_EVENT_CODE_RING_100_FULL: toread = buf_len; break; case IIO_EVENT_CODE_RING_75_FULL: toread = buf_len*3/4; break; case IIO_EVENT_CODE_RING_50_FULL: toread = buf_len/2; break; default: printf("Unexpecteded event code\n"); continue; } read_size = read(fp, data, toread*size_from_scanmode(num_vals, scan_ts)); if (read_size == -EAGAIN) { printf("nothing available\n"); continue; } scan_size = size_from_scanmode(num_vals, scan_ts); for (i = 0; i < read_size/scan_size; i++) { for (k = 0; k < num_vals; k++) { __s16 val = *(__s16 *)(&data[i*scan_size + (k)*2]); printf("%05f ", (float)val*gain); } printf(" %lld\n", *(__s64 *)(&data[(i + 1) *size_from_scanmode(num_vals, scan_ts) - sizeof(__s64)])); } } /* Stop the ring buffer */ ret = write_sysfs_int("ring_enable", buf_dir_name, 0); if (ret < 0) goto error_close_buffer_event; /* Disconnect from the trigger - just write a dummy name.*/ write_sysfs_string("trigger/current_trigger", dev_dir_name, "NULL"); error_close_buffer_event: fclose(fp_ev); error_close_buffer_access: close(fp); error_free_data: free(data); error_free_buffer_access: free(buffer_access); error_free_buffer_event: free(buffer_event); error_free_buf_dir_name: free(buf_dir_name); error_free_triggername: free(trigger_name); error_free_dev_dir_name: free(dev_dir_name); error_ret: return ret; }