<commit_before>/*
 * Copyright (C) 2014 <NAME>
 * Copyright (C) 2014 Synaptics Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <errno.h>

#include "rmidevice.h"

#define RMI_DEVICE_PDT_ENTRY_SIZE		6
#define RMI_DEVICE_PAGE_SELECT_REGISTER		0xFF
#define RMI_DEVICE_MAX_PAGE			0xFF
#define RMI_DEVICE_PAGE_SIZE			0x100
#define RMI_DEVICE_PAGE_SCAN_START		0x00e9
#define RMI_DEVICE_PAGE_SCAN_END		0x0005
#define RMI_DEVICE_F01_BASIC_QUERY_LEN		21
#define RMI_DEVICE_F01_PRODUCTINFO_MASK		0x7f
#define RMI_DEVICE_F01_QRY5_YEAR_MASK		0x1f
#define RMI_DEVICE_F01_QRY6_MONTH_MASK		0x0f
#define RMI_DEVICE_F01_QRY7_DAY_MASK		0x1f

#define RMI_DEVICE_F01_QRY1_HAS_LTS		(1 << 2)
#define RMI_DEVICE_F01_QRY1_HAS_SENSOR_ID	(1 << 3)
#define RMI_DEVICE_F01_QRY1_HAS_CHARGER_INP	(1 << 4)
#define RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE	(1 << 5)
#define RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE_HOFF	(1 << 6)
#define RMI_DEVICE_F01_QRY1_HAS_PROPS_2		(1 << 7)

#define RMI_DEVICE_F01_LTS_RESERVED_SIZE	19

#define RMI_DEVICE_F01_QRY42_DS4_QUERIES	(1 << 0)
#define RMI_DEVICE_F01_QRY42_MULTI_PHYS		(1 << 1)

#define RMI_DEVICE_F01_QRY43_01_PACKAGE_ID     (1 << 0)
#define RMI_DEVICE_F01_QRY43_01_BUILD_ID       (1 << 1)

#define PACKAGE_ID_BYTES			4
#define BUILD_ID_BYTES				3

#define RMI_F01_CMD_DEVICE_RESET	1
#define RMI_F01_DEFAULT_RESET_DELAY_MS	100

int RMIDevice::SetRMIPage(unsigned char page)
{
	int rc;

	if (m_page == page)
		return 0;

	m_page = page;
	rc = Write(RMI_DEVICE_PAGE_SELECT_REGISTER, &page, 1);
	if (rc < 0) {
		m_page = -1;
		return rc;
	}
	return 0;
}

int RMIDevice::QueryBasicProperties()
{
	int rc;
	unsigned char basicQuery[RMI_DEVICE_F01_BASIC_QUERY_LEN];
	unsigned short queryAddr;
	unsigned char infoBuf[4];
	unsigned short prodInfoAddr;
	RMIFunction f01;

	if (GetFunction(f01, 1)) {
		queryAddr = f01.GetQueryBase();

		rc = Read(queryAddr, basicQuery, RMI_DEVICE_F01_BASIC_QUERY_LEN);
		if (rc < 0) {
			fprintf(stderr, "Failed to read the basic query: %s\n", strerror(errno));
			return rc;
		}
		m_manufacturerID = basicQuery[0];
		m_hasLTS = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_LTS;
		m_hasSensorID = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_SENSOR_ID;
		m_hasAdjustableDoze = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE;
		m_hasAdjustableDozeHoldoff = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE_HOFF;
		m_hasQuery42 = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_PROPS_2;
		m_productInfo = ((basicQuery[2] & RMI_DEVICE_F01_PRODUCTINFO_MASK) << 7) |
				(basicQuery[3] & RMI_DEVICE_F01_PRODUCTINFO_MASK);

		snprintf(m_dom, sizeof(m_dom), "20%02d/%02d/%02d",
				basicQuery[5] & RMI_DEVICE_F01_QRY5_YEAR_MASK,
		 		basicQuery[6] & RMI_DEVICE_F01_QRY6_MONTH_MASK,
		 		basicQuery[7] & RMI_DEVICE_F01_QRY7_DAY_MASK);

		memcpy(m_productID, &basicQuery[11], RMI_PRODUCT_ID_LENGTH);
		m_productID[RMI_PRODUCT_ID_LENGTH] = '\0';

		queryAddr += 11;
		prodInfoAddr = queryAddr + 6;
		queryAddr += 10;

		if (m_hasLTS)
			++queryAddr;

		if (m_hasSensorID) {
			rc = Read(queryAddr++, &m_sensorID, 1);
			if (rc < 0) {
				fprintf(stderr, "Failed to read sensor id: %s\n", strerror(errno));
				return rc;
			}
		}

		if (m_hasLTS)
			queryAddr += RMI_DEVICE_F01_LTS_RESERVED_SIZE;

		if (m_hasQuery42) {
			rc = Read(queryAddr++, infoBuf, 1);
			if (rc < 0) {
				fprintf(stderr, "Failed to read query 42: %s\n", strerror(errno));
				return rc;
			}
<commit_msg>Read the Product ID separately.<commit_after>/*
 * Copyright (C) 2014 <NAME>
 * Copyright (C) 2014 Synaptics Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <errno.h>

#include "rmidevice.h"

#define RMI_DEVICE_PDT_ENTRY_SIZE		6
#define RMI_DEVICE_PAGE_SELECT_REGISTER		0xFF
#define RMI_DEVICE_MAX_PAGE			0xFF
#define RMI_DEVICE_PAGE_SIZE			0x100
#define RMI_DEVICE_PAGE_SCAN_START		0x00e9
#define RMI_DEVICE_PAGE_SCAN_END		0x0005
#define RMI_DEVICE_F01_BASIC_QUERY_LEN		11
#define RMI_DEVICE_F01_PRODUCTINFO_MASK		0x7f
#define RMI_DEVICE_F01_QRY5_YEAR_MASK		0x1f
#define RMI_DEVICE_F01_QRY6_MONTH_MASK		0x0f
#define RMI_DEVICE_F01_QRY7_DAY_MASK		0x1f

#define RMI_DEVICE_F01_QRY1_HAS_LTS		(1 << 2)
#define RMI_DEVICE_F01_QRY1_HAS_SENSOR_ID	(1 << 3)
#define RMI_DEVICE_F01_QRY1_HAS_CHARGER_INP	(1 << 4)
#define RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE	(1 << 5)
#define RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE_HOFF	(1 << 6)
#define RMI_DEVICE_F01_QRY1_HAS_PROPS_2		(1 << 7)

#define RMI_DEVICE_F01_LTS_RESERVED_SIZE	19

#define RMI_DEVICE_F01_QRY42_DS4_QUERIES	(1 << 0)
#define RMI_DEVICE_F01_QRY42_MULTI_PHYS		(1 << 1)

#define RMI_DEVICE_F01_QRY43_01_PACKAGE_ID     (1 << 0)
#define RMI_DEVICE_F01_QRY43_01_BUILD_ID       (1 << 1)

#define PACKAGE_ID_BYTES			4
#define BUILD_ID_BYTES				3

#define RMI_F01_CMD_DEVICE_RESET	1
#define RMI_F01_DEFAULT_RESET_DELAY_MS	100

int RMIDevice::SetRMIPage(unsigned char page)
{
	int rc;

	if (m_page == page)
		return 0;

	m_page = page;
	rc = Write(RMI_DEVICE_PAGE_SELECT_REGISTER, &page, 1);
	if (rc < 0) {
		m_page = -1;
		return rc;
	}
	return 0;
}

int RMIDevice::QueryBasicProperties()
{
	int rc;
	unsigned char basicQuery[RMI_DEVICE_F01_BASIC_QUERY_LEN];
	unsigned short queryAddr;
	unsigned char infoBuf[4];
	unsigned short prodInfoAddr;
	RMIFunction f01;

	if (GetFunction(f01, 1)) {
		queryAddr = f01.GetQueryBase();

		rc = Read(queryAddr, basicQuery, RMI_DEVICE_F01_BASIC_QUERY_LEN);
		if (rc < 0) {
			fprintf(stderr, "Failed to read the basic query: %s\n", strerror(errno));
			return rc;
		}
		m_manufacturerID = basicQuery[0];
		m_hasLTS = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_LTS;
		m_hasSensorID = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_SENSOR_ID;
		m_hasAdjustableDoze = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE;
		m_hasAdjustableDozeHoldoff = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_ADJ_DOZE_HOFF;
		m_hasQuery42 = basicQuery[1] & RMI_DEVICE_F01_QRY1_HAS_PROPS_2;
		m_productInfo = ((basicQuery[2] & RMI_DEVICE_F01_PRODUCTINFO_MASK) << 7) |
				(basicQuery[3] & RMI_DEVICE_F01_PRODUCTINFO_MASK);

		snprintf(m_dom, sizeof(m_dom), "20%02d/%02d/%02d",
				basicQuery[5] & RMI_DEVICE_F01_QRY5_YEAR_MASK,
		 		basicQuery[6] & RMI_DEVICE_F01_QRY6_MONTH_MASK,
		 		basicQuery[7] & RMI_DEVICE_F01_QRY7_DAY_MASK);

		queryAddr += 11;
		rc = Read(queryAddr, m_productID, RMI_PRODUCT_ID_LENGTH);
		if (rc < 0) {
			fprintf(stderr, "Failed to read the product id: %s\n", strerror(errno));
			return rc;
		}
		m_productID[RMI_PRODUCT_ID_LENGTH] = '\0';

		prodInfoAddr = queryAddr + 6;
		queryAddr += 10;

		if (m_hasLTS)
			++queryAddr;

		if (m_hasSensorID) {
			rc = Read(queryAddr++, &m_sensorID, 1);
			if (rc < 0) {
				fprintf(stderr, "Failed to read sensor id: %s\n", strerror(errno));
				return rc;
			}
		}

		if (m_hasLTS)
			queryAddr += RMI_DEVICE_F01_LTS_RESERVED_SIZE;

		if (m_hasQuery42) {
			rc = Read(queryAddr++, infoBuf, 1);
			if (rc < 0) {
				fprintf(stderr, "Failed to read query 42: %s\n", strerror(errno));
				return rc;
			}

{'fraction_non_alphanumeric': 0.09755188268920094, 'fraction_numerical': 0.052566173729340124, 'mean_word_length': 5.0511278195488725, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3590438', 'n_tokens_mistral': 3809, 'n_tokens_neox': 3395, 'n_words': 703}