Created
November 2, 2015 06:52
-
-
Save e10s/3b8deb8ea5b140c04c18 to your computer and use it in GitHub Desktop.
A simple program to print whether a CD tray is open or closed, on FreeBSD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright electrolysis 2015. | |
Distributed under the Boost Software License, Version 1.0. | |
(See copy at http://www.boost.org/LICENSE_1_0.txt) | |
*/ | |
// A simple program to print whether a CD tray is open or closed, on FreeBSD. | |
#include <stdio.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <camlib.h> // requires -lcam | |
#include <cam/scsi/scsi_message.h> // MSG_SIMPLE_Q_TAG | |
#define MECHANISM_STATUS_CMD_LEN 12 | |
#define MECHANISM_STATUS_RESPONSE_BUF_LEN 8 | |
int main(){ | |
const char* path = "/dev/cd0"; | |
char dev_name[DEV_IDLEN + 1]; | |
int unit; | |
int cgd = cam_get_device(path, dev_name, DEV_IDLEN, &unit); | |
if(cgd == -1){ | |
puts("cam_get_device: failed"); | |
puts(cam_errbuf); | |
return -1; | |
} | |
struct cam_device* cam_dev = cam_open_spec_device(dev_name, unit, O_RDWR, NULL); | |
if(!cam_dev){ | |
puts("cam_open_spec_device: failed"); | |
puts(cam_errbuf); | |
return -2; | |
} | |
union ccb ccb; | |
unsigned char mechanism_status_response_buf[MECHANISM_STATUS_RESPONSE_BUF_LEN]; | |
unsigned char mechanism_status_cmd [MECHANISM_STATUS_CMD_LEN] = | |
{0xBD, 0, 0, 0, 0, 0, 0, 0, 0, MECHANISM_STATUS_RESPONSE_BUF_LEN, 0, 0}; | |
cam_fill_csio(&ccb.csio, 1, NULL, CAM_DIR_IN, MSG_SIMPLE_Q_TAG, mechanism_status_response_buf, | |
MECHANISM_STATUS_RESPONSE_BUF_LEN, 0, MECHANISM_STATUS_CMD_LEN, 5000); | |
memcpy(ccb.csio.cdb_io.cdb_bytes, mechanism_status_cmd, MECHANISM_STATUS_CMD_LEN); | |
int csc = cam_send_ccb(cam_dev, &ccb); | |
if(csc == -1){ | |
cam_close_device(cam_dev); | |
puts("cam_send_ccb: failed"); | |
puts(cam_errbuf); | |
return -3; | |
} | |
printf("Tray is %s!\n", mechanism_status_response_buf[1] & 0b00010000 ? "open" : "closed"); | |
cam_close_device(cam_dev); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment