#include <stdio.h>
#include <scsi/sg.h>
#include <scsi/scsi.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void)
{
	struct sg_io_hdr transport;
	unsigned char cmd[] = {
		0x5a, //opcode -- mode sense(10)
		0x08, //dbd, llbaa -- dbd=1
		0x2a, //page code -- BRASERO_SPC_PAGE_STATUS
		      // spc-3 says thats "CD capabilities and mechanical status"
			  //
		0x00, //brasero says reserved, spc3 says subpage code
		0x00, //reserved
		0x00, //reserved
		0x00, //alloc len
		0x0a, //alloc len
		0x00, //ctl
	};
	unsigned char buffer[10];
	unsigned char sense_data[19];
	int r;
	int i;
	int fd = open("/dev/sr0", O_RDONLY|O_NONBLOCK);
	if (fd < 0) {
		printf("open failed\n");
		exit(1);
	}

	memset(&transport, 0, sizeof(transport));
	memset(buffer, 0, sizeof(buffer));
	memset(sense_data, 0, sizeof(sense_data));

	transport.interface_id = 'S';
	transport.cmdp = cmd;
	transport.cmd_len = sizeof(cmd);
	transport.dxferp = buffer;
	transport.dxfer_len = sizeof(buffer);
	transport.sbp = sense_data;
	transport.mx_sb_len = sizeof(sense_data);
	transport.dxfer_direction = SG_DXFER_FROM_DEV;

	r = ioctl(fd, SG_IO, &transport);
	printf("result %d\n", r);
	if ((transport.masked_status & CHECK_CONDITION) && transport.sb_len_wr) {
		printf("check sense data:\n");
		for (i = 0; i < sizeof(sense_data); i++)
			printf("%02x ", sense_data[i]);
		printf("\n");
	}
}