tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c

Source file repositories/reference/linux-study-clean/tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c

File Facts

System
Linux kernel
Corpus path
tools/arch/x86/dell-uart-backlight-emulator/dell-uart-backlight-emulator.c
Extension
.c
Size
3956 bytes
Lines
164
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (idx == 0) {
			switch (buf[0]) {
			/* 3 MSB bits: cmd-len + 01010 SOF marker */
			case 0x6a: len = 3; break;
			case 0x8a: len = 4; break;
			default:
				fprintf(stderr, "Error unexpected first byte: 0x%02x\n", buf[0]);
				continue; /* Try to sync up with sender */
			}
		}

		/* Process msg when len bytes have been received */
		if (idx != (len - 1)) {
			idx++;
			continue;
		}

		/* Reset idx for next command */
		idx = 0;

		csum = dell_uart_checksum(buf, len - 1);
		if (buf[len - 1] != csum) {
			fprintf(stderr, "Error checksum mismatch got 0x%02x expected 0x%02x\n",
				buf[len - 1], csum);
			continue;
		}

		switch ((buf[0] << 8) | buf[1]) {
		case 0x6a06: /* cmd = 0x06, get version */
			len = strlen(version_str);
			strcpy((char *)&response[2], version_str);
			printf("Get version, reply: %s\n", version_str);
			break;
		case 0x8a0b: /* cmd = 0x0b, set brightness */
			if (buf[2] > 100) {
				fprintf(stderr, "Error invalid brightness param: %d\n", buf[2]);
				continue;
			}

			len = 0;
			brightness = buf[2];
			printf("Set brightness %d\n", brightness);
			break;
		case 0x6a0c: /* cmd = 0x0c, get brightness */
			len = 1;
			response[2] = brightness;
			printf("Get brightness, reply: %d\n", brightness);
			break;
		case 0x8a0e: /* cmd = 0x0e, set backlight power */
			if (buf[2] != 0 && buf[2] != 1) {
				fprintf(stderr, "Error invalid set power param: %d\n", buf[2]);
				continue;
			}

			len = 0;
			printf("Set power %d\n", buf[2]);
			break;
		default:
			fprintf(stderr, "Error unknown cmd 0x%04x\n",
				(buf[0] << 8) | buf[1]);
			continue;
		}

		/* Respond with <total-len> <cmd> <data...> <csum> */
		response[0] = len + 3; /* response length in bytes */
		response[1] = buf[1];  /* ack cmd */
		csum = dell_uart_checksum(response, len + 2);
		response[len + 2] = csum;
		ret = write(serial_fd, response, response[0]);
		if (ret != (response[0]))
			fprintf(stderr, "Error writing %d bytes: %d\n",
				response[0], ret);
	}

	ret = 0;
out_restore:
	tcsetattr(serial_fd, TCSANOW, &saved_tty);
out_close:
	close(serial_fd);
	return ret;
}

Annotation

Implementation Notes