int AccelSensor::calibrate(int32_t, struct cal_cmd_t *para, struct cal_result_t *cal_result) { int fd; char temp[ARRAY][LENGTH]; char buf[ARRAY * LENGTH]; char *token, *strsaveptr, *endptr; int i, err; off_t offset; int para1 = 0; if (para == NULL || cal_result == NULL) { ALOGE("Null pointer calibrate parameters\n"); return -1; } para1 = CMD_CAL(para->axis, para->apply_now); strlcpy(&input_sysfs_path[input_sysfs_path_len], SYSFS_CALIBRATE, SYSFS_MAXLEN); fd = open(input_sysfs_path, O_RDWR); if (fd >= 0) { snprintf(buf, sizeof(buf), "%d", para1); write(fd, buf, strlen(buf)+1); } else { ALOGE("open %s failed\n", input_sysfs_path); return -1; } offset = lseek(fd, 0, SEEK_SET); char *p = buf; memset(buf, 0, sizeof(buf)); err = read(fd, buf, sizeof(buf)-1); if(err < 0) { ALOGE("read error\n"); close(fd); return err; } for(i = 0; i < ARRAY; i++, p = NULL) { token = strtok_r(p, ",", &strsaveptr); if(token == NULL) break; if(strlen(token) > LENGTH - 1) { ALOGE("token is too long\n"); close(fd); return -1; } strlcpy(temp[i], token, sizeof(temp[i])); } close(fd); for(int i = 0; i < ARRAY; i++) { cal_result->offset[i] = strtol(temp[i], &endptr, 0); if (endptr == temp[i]) { ALOGE("No digits were found\n"); return -1; } } return 0; }
int ProximitySensor::calibrate(int32_t, struct cal_cmd_t *para, struct cal_result_t *cal_result) { int fd; char temp[3][LENGTH]; char buf[3 * LENGTH]; char *token, *strsaveptr, *endptr; int i, err; off_t offset; int para1 = 0; if (para == NULL || cal_result == NULL) { ALOGE("Null pointer calibrate parameters\n"); return -1; } para1 = CMD_CAL(para->axis, para->apply_now); strlcpy(&input_sysfs_path[input_sysfs_path_len], SYSFS_CALIBRATE, SYSFS_MAXLEN); fd = open(input_sysfs_path, O_RDWR); if (fd >= 0) { snprintf(buf, sizeof(buf), "%d", para1); err = write(fd, buf, strlen(buf)+1); if(err < 0) { ALOGE("write error\n"); close(fd); return err; } } else { return -1; } if (fd >= 0) { offset = lseek(fd, 0, SEEK_SET); char *p = buf; memset(buf, 0, sizeof(buf)); err = read(fd, buf, sizeof(buf)-1); if(err < 0) { ALOGE("proximity read error\n"); close(fd); return err; } for(i = 0; i < (int)(sizeof(temp) / LENGTH); i++, p = NULL) { token = strtok_r(p, ",", &strsaveptr); if(token == NULL) break; if(strlen(token) > LENGTH - 1) { ALOGE("token is too long\n"); close(fd); return -1; } strlcpy(temp[i], token, sizeof(temp[i])); } close(fd); if (para->axis == 0) { mThreshold_h = strtol(temp[0], &endptr, 0); if (mThreshold_h == LONG_MAX || mThreshold_h == LONG_MIN) { ALOGE("mThreshold_h error value\n"); return -1; } if (endptr == temp[0]) { ALOGE("No digits were found\n"); return -1; } } else if (para->axis == 1) { mThreshold_l = strtol(temp[1], &endptr, 0); if (mThreshold_l == LONG_MAX || mThreshold_l == LONG_MIN) { ALOGE("mThreshold_l error value\n"); return -1; } if (endptr == temp[1]) { ALOGE("No digits were found\n"); return -1; } } else if (para->axis == 2) { mBias = strtol(temp[2], &endptr, 0); if (mBias == LONG_MAX || mBias == LONG_MIN) { ALOGE("mBias error value\n"); return -1; } if (endptr == temp[2]) { ALOGE("No digits were found\n"); return -1; } } cal_result->threshold_h = mThreshold_h; cal_result->threshold_l = mThreshold_l; cal_result->bias = mBias; return 0; } else { ALOGE("open %s error\n", input_sysfs_path); return -1; } return 0; }