Ejemplo n.º 1
0
size_t DiskCachingFileLoaderCache::ReadFromCache(s64 pos, size_t bytes, void *data) {
	lock_guard guard(lock_);

	if (!f_) {
		return 0;
	}

	s64 cacheStartPos = pos / blockSize_;
	s64 cacheEndPos = (pos + bytes - 1) / blockSize_;
	size_t readSize = 0;
	size_t offset = (size_t)(pos - (cacheStartPos * (u64)blockSize_));
	u8 *p = (u8 *)data;

	for (s64 i = cacheStartPos; i <= cacheEndPos; ++i) {
		auto &info = index_[i];
		if (info.block == INVALID_BLOCK) {
			return readSize;
		}
		info.generation = generation_;
		if (info.hits < std::numeric_limits<u16>::max()) {
			++info.hits;
		}

		size_t toRead = std::min(bytes - readSize, (size_t)blockSize_ - offset);
		if (!ReadBlockData(p + readSize, info, offset, toRead)) {
			return readSize;
		}
		readSize += toRead;

		// Don't need an offset after the first read.
		offset = 0;
	}
	return readSize;
}
int main(int argc, char* argv[])
{
	int retval;
	int ii;
	int jj;
	int this_arg = 1;
	int found = 0;
	int readings = 1;
	int reading;;
	int cartesian = 0;
	int patience = PATIENCE;
	int report_type;
	int report_size;
	int rx_num;
	int tx_num;
	char *report_data_8;
	short *report_data_16;
	int *report_data_32;
	unsigned char data_8;
	struct stat st;

	if (argc == 1) {
		usage(argv[0]);
		error_exit(EINVAL);
	}

	for (ii = 0; ii < NUMBER_OF_INPUTS_TO_SCAN; ii++) {
		memset(input_detect, 0x00, MAX_STRING_LEN);
		snprintf(input_detect, MAX_STRING_LEN, "%s%d/%s", INPUT_PATH,
				(unsigned int)ii, DETECT_FILENAME);
		retval = stat(input_detect, &st);
		if (retval == 0) {
			snprintf(mySensor, MAX_STRING_LEN, "%s%d", INPUT_PATH,
					(unsigned int)ii);
			found = 1;
			break;
		}
	}

	if (!found) {
		printf("ERROR: input driver not found\n");
		error_exit(ENODEV);
	}

	while (this_arg < argc) {
		if (!strcmp((const char *)argv[this_arg], "-n")) {
			this_arg++;
			readings = (unsigned int)strtoul(argv[this_arg], NULL, 0);
		} else if (!strcmp((const char *)argv[this_arg], "-c")) {
			cartesian = 1;
		} else {
			report_type = strtoul(argv[this_arg], NULL, 0);
		}
		this_arg++;
	}

	if (cartesian) {
		rx_num = GetRxElectrodes();
		tx_num = GetTxElectrodes();
	}

	switch (report_type) {
	case F54_16BIT_IMAGE:
	case F54_RAW_16BIT_IMAGE:
	case F54_SENSOR_SPEED:
	case F54_ADC_RANGE:
	case F54_ABS_CAP:
	case F54_ABS_DELTA:
		break;
	default:
		DoPreparation(1);
		break;
	}

	for (reading = 0; reading < readings; reading++) {
		patience = PATIENCE;
		SetReportType(report_type);
		GetReport(1);
		do {
			if (GetStatus() == 0)
				break;
		} while (--patience > 0);

		report_size = ReadReportSize();
		if (report_size == 0) {
			printf("ERROR: unable to read report\n");
			DoReset(1);
			error_exit(EINVAL);
		}

		if (!data_buffer) {
			data_buffer = malloc(report_size);
			if (!data_buffer) {
				printf("ERROR: failed to allocate report data buffer\n");
				DoReset(1);
				error_exit(ENOMEM);
			}
		}
		ReadBlockData((char *)&data_buffer[0], report_size);

		printf("Reading %d\r\n", reading + 1);

		switch (report_type) {
		case F54_8BIT_IMAGE:
			report_data_8 = (char *)data_buffer;
			for (ii = 0; ii < report_size; ii++) {
				printf("%03d: %d\r\n", ii, *report_data_8);
				report_data_8++;
			}
			break;
		case F54_16BIT_IMAGE:
		case F54_RAW_16BIT_IMAGE:
		case F54_TRUE_BASELINE:
		case F54_FULL_RAW_CAP:
		case F54_FULL_RAW_CAP_RX_COUPLING_COMP:
			report_data_16 = (short *)data_buffer;
			if (cartesian) {
				printf("   ");
				for (ii = 0; ii < rx_num; ii++)
					printf("     %2d", ii);
				printf("\r\n");

				for (ii = 0; ii < tx_num; ii++) {
					printf("%2d ", ii);
					for (jj = 0; jj < rx_num; jj++) {
						printf("  %5d", *report_data_16);
						report_data_16++;
					}
					printf("\r\n");
				}
			} else {
				for (ii = 0; ii < report_size; ii += 2) {
					printf("%03d: %d\r\n", ii / 2, *report_data_16);
					report_data_16++;
				}
			}
			break;
		case F54_HIGH_RESISTANCE:
		case F54_FULL_RAW_CAP_MIN_MAX:
		case F54_SENSOR_SPEED:
		case F54_ADC_RANGE:
			report_data_16 = (short *)data_buffer;
			for (ii = 0; ii < report_size; ii += 2) {
				printf("%03d: %d\r\n", ii / 2, *report_data_16);
				report_data_16++;
			}
			break;
		case F54_ABS_CAP:
		case F54_ABS_DELTA:
			report_data_32 = (int *)data_buffer;
			if (cartesian) {
				printf("Rx ");
				for (ii = 0; ii < rx_num; ii++)
					printf("     %2d", ii);
				printf("\r\n");

				printf("   ");
				for (ii = 0; ii < rx_num; ii++) {
					printf("  %5d", *report_data_32);
					report_data_32++;
				}
				printf("\r\n");

				printf("Tx ");
				for (ii = 0; ii < tx_num; ii++)
					printf("     %2d", ii);
				printf("\r\n");

				printf("   ");
				for (ii = 0; ii < tx_num; ii++) {
					printf("  %5d", *report_data_32);
					report_data_32++;
				}
				printf("\r\n");
			} else {
				for (ii = 0; ii < report_size; ii += 4) {
					printf("%03d: %d\r\n", ii / 4, *report_data_32);
					report_data_32++;
				}
			}
			break;
		case F54_ABS_ADC:
			report_data_16 = (short *)data_buffer;
			for (ii = 0; ii < report_size; ii += 2) {
				data_8 = (unsigned char)*report_data_16;
				printf("%03d: %d\r\n", ii / 2, data_8);
				report_data_16++;
			}
			break;
		default:
			for (ii = 0; ii < report_size; ii++)
				printf("%03d: 0x%02x\r\n", ii, data_buffer[ii]);
			break;
		}
	}

	switch (report_type) {
	case F54_16BIT_IMAGE:
	case F54_RAW_16BIT_IMAGE:
	case F54_SENSOR_SPEED:
	case F54_ADC_RANGE:
	case F54_ABS_CAP:
	case F54_ABS_DELTA:
		ResumeTouch(1);
		break;
	default:
		DoReset(1);
		break;
	}

	if (data_buffer)
		free(data_buffer);

	return 0;
}