/*---------------------------------------------------------------------------*/ static int alsps_update_info(struct lps_priv *lps) { int err = -EINVAL; unsigned int als_dat, ps_dat; unsigned int offset_data=0; unsigned int ps_offset; if (!bUpToDate) { read_offset(&offset_data); ps_offset = offset_data; FLPLOGE("alsps_update_info set ps_offset: %d\n", ps_offset); if ((err = ioctl(lps->fd, ALSPS_SET_PS_CALI, &ps_offset))) { FLPLOGE("set ps_offset: %d(%s)\n", errno, strerror(errno)); } else { bUpToDate = true; } } if (lps->fd == -1) { FLPLOGE("invalid fd\n"); return -EINVAL; } else if ((err = ioctl(lps->fd, ALSPS_GET_PS_RAW_DATA, &ps_dat))) { FLPLOGE("read ps raw: %d(%s)\n", errno, strerror(errno)); return err; } else if ((err = ioctl(lps->fd, ALSPS_GET_ALS_RAW_DATA, &als_dat))) { FLPLOGE("read als raw: %d(%s)\n", errno, strerror(errno)); return err; } lps->als_raw = als_dat; lps->ps_raw = ps_dat; return 0; }
/*---------------------------------------------------------------------------*/ static int alsps_open(struct lps_priv *lps) { int err, max_retry = 3, retry_period = 100, retry; unsigned int flags = 1; if (lps->fd == -1) { lps->fd = open("/dev/als_ps", O_RDONLY); if (lps->fd < 0) { FLPLOGE("Couldn't open '%s' (%s)", lps->dev, strerror(errno)); return -1; } retry = 0; while ((err = ioctl(lps->fd, ALSPS_SET_PS_MODE, &flags)) && (retry ++ < max_retry)) usleep(retry_period*1000); if (err) { FLPLOGE("enable ps fail: %s", strerror(errno)); return -1; } retry = 0; while ((err = ioctl(lps->fd, ALSPS_SET_ALS_MODE, &flags)) && (retry ++ < max_retry)) usleep(retry_period*1000); if (err) { FLPLOGE("enable als fail: %s", strerror(errno)); return -1; } } FLPLOGD("%s() %d\n", __func__, lps->fd); return 0; }
/*---------------------------------------------------------------------------*/ static void *alsps_update_iv_thread(void *priv) { struct lps_data *dat = (struct lps_data *)priv; struct lps_priv *lps = &dat->lps; struct itemview *iv = dat->iv; int err = 0, len = 0; char *status; LOGD(TAG "%s: Start\n", __FUNCTION__); if ((err = alsps_open(lps))) { memset(dat->info, 0x00, sizeof(dat->info)); sprintf(dat->info, uistr_info_sensor_init_fail); iv->redraw(iv); FLPLOGE("alsps() err = %d(%s)\n", err, dat->info); pthread_exit(NULL); return NULL; } while (1) { if (dat->exit_thd) break; if ((err = alsps_update_info(lps))) continue; len = 0; len += snprintf(dat->info+len, sizeof(dat->info)-len, "ALS: %4Xh (0:dark; +:bright) \n", lps->als_raw); len += snprintf(dat->info+len, sizeof(dat->info)-len, "PS : %4Xh (0:far ; +:close) \n", lps->ps_raw); if(lps->ps_threshold_high == 0){ len += snprintf(dat->info+len, sizeof(dat->info)-len, "%s: %s\n", uistr_info_sensor_alsps_thres_high, uistr_info_sensor_alsps_check_command);//yucong add for factory mode test support }else{ len += snprintf(dat->info+len, sizeof(dat->info)-len, "%s: %4Xh\n", uistr_info_sensor_alsps_thres_high, lps->ps_threshold_high); } if(lps->ps_threshold_low == 0){ len += snprintf(dat->info+len, sizeof(dat->info)-len, "%s: %s\n", uistr_info_sensor_alsps_thres_low, uistr_info_sensor_alsps_check_command);//yucong add for factory mode test support }else{ len += snprintf(dat->info+len, sizeof(dat->info)-len, "%s: %4Xh\n", uistr_info_sensor_alsps_thres_low, lps->ps_threshold_low); } if(lps->ps_threshold_value == 2){ len += snprintf(dat->info+len, sizeof(dat->info)-len, "%s: %s\n", uistr_info_sensor_alsps_result, uistr_info_sensor_alsps_check_command);//yucong add for factory mode test support }else{ len += snprintf(dat->info+len, sizeof(dat->info)-len, "%s: %s\n", uistr_info_sensor_alsps_result, (lps->ps_threshold_value == 1)?uistr_info_sensor_pass:uistr_info_sensor_fail );//yucong add for factory mode test support } iv->set_text(iv, &dat->text); iv->redraw(iv); pthread_mutex_lock (&alsps_mutex); if(support_ata) { thread_exit = true; pthread_mutex_unlock (&alsps_mutex); break; } pthread_mutex_unlock (&alsps_mutex); } alsps_close(lps); LOGD(TAG "%s: Exit\n", __FUNCTION__); pthread_exit(NULL); return NULL; }
// xiangfei.peng 20130513 extern int phil_add_alsps_cali_in_entry(struct lps_priv *lps) { int err = -EINVAL; unsigned int offset_data = 0; read_offset(&offset_data); FLPLOGD("[phil] cali in entry get ps_offset: %d\n", offset_data); if(lps->fd < 0) { FLPLOGE("[phil] alsps open failed!\n"); return err; } if((err = ioctl(lps->fd, ALSPS_SET_PS_CALI, &offset_data))) { FLPLOGE("set ps_offset: %d(%s)\n", errno, strerror(errno)); } return 0; }
/*---------------------------------------------------------------------------*/ static int alsps_update_info(struct lps_priv *lps) { int err = -EINVAL; int als_dat, ps_dat; if (lps->fd == -1) { FLPLOGE("invalid fd\n"); return -EINVAL; } else if ((err = ioctl(lps->fd, ALSPS_GET_PS_RAW_DATA, &ps_dat))) { FLPLOGE("read ps raw: %d(%s)\n", errno, strerror(errno)); return err; } else if ((err = ioctl(lps->fd, ALSPS_GET_ALS_RAW_DATA, &als_dat))) { FLPLOGE("read als raw: %d(%s)\n", errno, strerror(errno)); return err; } lps->als_raw = als_dat; lps->ps_raw = ps_dat; return 0; }
/*---------------------------------------------------------------------------*/ static int alsps_close(struct lps_priv *lps) { unsigned int flags = 0; int err; if (lps->fd != -1) { if ((err = ioctl(lps->fd, ALSPS_SET_PS_MODE, &flags))) { FLPLOGE("disable ps fail: %s", strerror(errno)); return -1; } else if ((err = ioctl(lps->fd, ALSPS_SET_ALS_MODE, &flags))) { FLPLOGE("disable als fail: %s", strerror(errno)); return -1; } close(lps->fd); } memset(lps, 0x00, sizeof(*lps)); lps->fd = -1; lps->dev = "/dev/als_ps"; return 0; }
/****************************************************************************** * Functions *****************************************************************************/ static int read_write(unsigned int *data) { int result; F_ID test_nvram_id; int file_lid = AP_CFG_REEB_PRODUCT_INFO_LID; int test_fd; int rec_size=0 , rec_num = 0; bool IsRead = true,IsWrite = false; FLPLOGD("--@read:\n"); test_nvram_id = NVM_GetFileDesc(file_lid, &rec_size, &rec_num,IsRead);//IsRead=true if(test_nvram_id.iFileDesc < 0){ FLPLOGE("--@NVM_GetFileDesc failed\n"); return -1; } result = read(test_nvram_id.iFileDesc,&test_struct,rec_num*rec_size); if(result != rec_num*rec_size){ FLPLOGE("--@Get file failed\n"); return -1; } //读取nvram的数据 FLPLOGD("--@---------read---------\n"); //FLPLOGD("--@perso1[0]:%d\n",test_struct.trace_nvram_data.info_name_perso1[0]); FLPLOGD("--@perso1[0]:%d\n",test_struct.ps_cali_nvram_data[0]); FLPLOGD("--@------------------\n"); if(!NVM_CloseFileDesc(test_nvram_id)) { FLPLOGE("NVM_CloseFileDesc failed\n"); } FLPLOGD("write:\n"); test_nvram_id = NVM_GetFileDesc(file_lid, &rec_size, &rec_num,IsWrite);//IsWrite=false if(test_nvram_id.iFileDesc < 0){ FLPLOGE("NVM_GetFileDesc failed\n"); return -1; } // test_struct.ps_cali_nvram_data[0] = *data; // test_struct.trace_nvram_data.info_name_perso1[0] = *data; test_struct.ps_cali_nvram_data[0] = *data & 0xff; test_struct.ps_cali_nvram_data[1] = (*data & 0xff00) >> 8; FLPLOGD("--------write----------\n"); //FLPLOGD("--@perso1[0]:%d\n",test_struct.trace_nvram_data.info_name_perso1[0]); FLPLOGD("--@perso1[0]:%d\n",test_struct.ps_cali_nvram_data[0]); FLPLOGD("--@------------------\n"); result = write(test_nvram_id.iFileDesc,&test_struct,rec_num*rec_size); if(result != rec_num*rec_size){ FLPLOGE("write file failed\n"); return -1; } if(!NVM_CloseFileDesc(test_nvram_id)) { FLPLOGE("NVM_CloseFileDesc failed\n"); return -1; } return 0; }
/*---------------------------------------------------------------------------*/ static void *alsps_update_iv_thread(void *priv) { struct lps_data *dat = (struct lps_data *)priv; struct lps_priv *lps = &dat->lps; struct itemview *iv = dat->iv; int err = 0, len = 0; char *status; LOGD(TAG "%s: Start\n", __FUNCTION__); if ((err = alsps_open(lps))) { memset(dat->info, 0x00, sizeof(dat->info)); sprintf(dat->info, "INIT FAILED\n"); iv->redraw(iv); FLPLOGE("alsps() err = %d(%s)\n", err, dat->info); pthread_exit(NULL); return NULL; } while (1) { if (dat->exit_thd) break; if ((err = alsps_update_info(lps))) continue; len = 0; len += snprintf(dat->info+len, sizeof(dat->info)-len, "ALS: %4Xh (0:dark; +:bright)\n", lps->als_raw); len += snprintf(dat->info+len, sizeof(dat->info)-len, "PS : %4Xh (0:far ; +:close)\n", lps->ps_raw); iv->set_text(iv, &dat->text); iv->redraw(iv); } alsps_close(lps); LOGD(TAG "%s: Exit\n", __FUNCTION__); pthread_exit(NULL); return NULL; }
static void reset_ps_cali(struct lps_priv *lps){ unsigned int flags; int err=0; int i=0; if(lps->fd<0){ FLPLOGD(TAG "wrong file descriptor.\n"); return 0; } flags = 1; if(err = ioctl(lps->fd, ALSPS_RESET_PS, &flags)) { FLPLOGE("read als raw: %d(%s)\n", errno, strerror(errno)); return err; } //prox_threshold_hi=((prox_max-prox_mean)*200+50)/100+prox_mean; //prox_threshold_lo=((prox_max-prox_mean)*170+50)/100+prox_mean; // FLPLOGD(TAG "prox_max:%d, prox_mean:%d,prox_threshold_hi=%d,prox_threshold_lo=%d.\n",prox_max,prox_mean,prox_threshold_hi,prox_threshold_lo); //Only prox_threshold_hi used //if(set_ps_cali(offset_data)){ // return 0; //} //return prox_threshold_hi; }
/* * Description : Calculate the threshold and store it into the file * Para: * * Return Value: greater than 0 if no error * Equal/less than 0 if error */ static unsigned int do_ps_cali(struct lps_priv *lps){ unsigned int offset_data=0; int err=0; int i=0; if(lps->fd<0){ FLPLOGD(TAG "wrong file descriptor.\n"); return 0; } if(err = ioctl(lps->fd, ALSPS_GET_PS_CALI, &offset_data)) { FLPLOGE("read ps raw: %d(%s)\n", errno, strerror(errno)); return err; } return offset_data; }
/*---------------------------------------------------------------------------*/ static int alsps_update_info(struct lps_priv *lps) { int err = -EINVAL; int als_dat, ps_dat, ps_threshold_dat, ps_high, ps_low;//yucong add for factory mode test support if (lps->fd == -1) { FLPLOGE("invalid fd\n"); return -EINVAL; }else if ((err = ioctl(lps->fd, ALSPS_GET_PS_RAW_DATA, &ps_dat))) { FLPLOGE("read ps raw: %d(%s)\n", errno, strerror(errno)); return err; }else if ((err = ioctl(lps->fd, ALSPS_GET_ALS_RAW_DATA, &als_dat))) { FLPLOGE("read als raw: %d(%s)\n", errno, strerror(errno)); return err; }else if ((err = ioctl(lps->fd, ALSPS_GET_PS_TEST_RESULT, &ps_threshold_dat))) {//yucong add for factory mode test support FLPLOGE("get thresheld infr: %d(%s)\n", errno, strerror(errno)); ps_threshold_dat = 2; } if ((err = ioctl(lps->fd, ALSPS_GET_PS_THRESHOLD_HIGH, &ps_high))) {//yucong add for factory mode test support FLPLOGE("get thresheld high infr: %d(%s)\n", errno, strerror(errno)); ps_high = 0; } if ((err = ioctl(lps->fd, ALSPS_GET_PS_THRESHOLD_LOW, &ps_low))) {//yucong add for factory mode test support FLPLOGE("get thresheld low infr: %d(%s)\n", errno, strerror(errno)); ps_low = 0; } lps->als_raw = als_dat; lps->ps_raw = ps_dat; lps->ps_threshold_value = ps_threshold_dat;//yucong add for factory mode test support lps->ps_threshold_high = ps_high;//yucong add for factory mode test support lps->ps_threshold_low = ps_low;//yucong add for factory mode test support //add sensor data to struct sp_ata_data for PC side return_data.alsps.als = lps->als_raw; return_data.alsps.ps = lps->ps_raw; return 0; }
/*---------------------------------------------------------------------------*/ static void *ps_update_iv_thread(void *priv) { struct lps_data *dat = (struct lps_data *)priv; struct lps_priv *lps = &dat->lps; struct itemview *iv = dat->iv; int err = 0, len = 0; char *status; static int op = -1; unsigned int cleardata = 0; FLPLOGD(TAG "%s: Start\n", __FUNCTION__); if ((err = alsps_open(lps))) { memset(dat->info, 0x00, sizeof(dat->info)); sprintf(dat->info, "INIT FAILED\n"); iv->redraw(iv); FLPLOGE("alsps() err = %d(%s)\n", err, dat->info); pthread_exit(NULL); return NULL; } while (1) { if (dat->exit_thd) break; pthread_mutex_lock(&dat->lps.evtmutex); if(op != dat->lps.pending_op) { op = dat->lps.pending_op; FLPLOGD("op: %d\n", dat->lps.pending_op); } pthread_mutex_unlock(&dat->lps.evtmutex); err = 0; if(op == PS_OP_CLEAR){ //TODO Clear Part if(read_write(&cleardata)==0){ //snprintf(dat->lps.status, sizeof(dat->lps.status), "清除成功!\n"); snprintf(dat->lps.status, sizeof(dat->lps.status), "clear successful!\n"); } bUpToDate = false; pthread_mutex_lock(&dat->lps.evtmutex); dat->lps.pending_op = PS_OP_NONE; pthread_mutex_unlock(&dat->lps.evtmutex); } else if(op == PS_OP_CALI_PRE){ //snprintf(dat->lps.status, sizeof(dat->lps.status), "执行校准中,请勿触碰!\n"); snprintf(dat->lps.status, sizeof(dat->lps.status), "cali ongoing,don't touch!\n"); pthread_mutex_lock(&dat->lps.evtmutex); dat->lps.pending_op = PS_OP_CALI; dat->lps.cali_delay= 50; //50ms dat->lps.cali_num = 20; pthread_mutex_unlock(&dat->lps.evtmutex); } else if(op == PS_OP_CALI){ //TODO Calibration Part unsigned int value=do_ps_cali(&(dat->lps)); if(0 == read_write(&value)){ //len = snprintf(dat->lps.status, sizeof(dat->lps.status), "校准: 成功! 偏移寄存器设置为:%d\n",value); len = snprintf(dat->lps.status, sizeof(dat->lps.status), "cali successful! the offset_data is:%d\n",value); dat->mod->test_result = FTM_TEST_PASS; } else{ //len = snprintf(dat->lps.status, sizeof(dat->lps.status), "校准: 失败!\n"); len = snprintf(dat->lps.status, sizeof(dat->lps.status), "cali: fail!\n"); dat->mod->test_result = FTM_TEST_FAIL; } bUpToDate = false; pthread_mutex_lock(&dat->lps.evtmutex); dat->lps.pending_op = PS_OP_NONE; pthread_mutex_unlock(&dat->lps.evtmutex); } if ((err = alsps_update_info(lps))) continue; len = 0; //Remove ALS part //len += snprintf(dat->info+len, sizeof(dat->info)-len, "ALS: %4Xh (0:dark; +:bright)\n", lps->als_raw); len += snprintf(dat->info+len, sizeof(dat->info)-len, "distance: %4d(0: faraway; +: close)\n", lps->ps_raw); len += snprintf(dat->info+len, sizeof(dat->info)-len, "%s", lps->status); iv->set_text(iv, &dat->text); iv->redraw(iv); } alsps_close(lps); FLPLOGD(TAG "%s: Exit\n", __FUNCTION__); pthread_exit(NULL); return NULL; }