Skip to content

Instantly share code, notes, and snippets.

@e10s
Created November 2, 2015 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e10s/0c868ef00dfdce71e33a to your computer and use it in GitHub Desktop.
Save e10s/0c868ef00dfdce71e33a to your computer and use it in GitHub Desktop.
A simple program to print whether a CD tray is ejectable, on FreeBSD.
/*
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 ejectable, 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 GET_CONFIGURETION_CMD_LEN 12
#define GET_CONFIGURATION_RESPONSE_BUF_LEN 16
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 get_configuration_response_buf[GET_CONFIGURATION_RESPONSE_BUF_LEN];
unsigned char get_configuration_cmd [GET_CONFIGURETION_CMD_LEN] =
{0x46, 0, 0, 0x03, 0, 0, 0, 0, GET_CONFIGURATION_RESPONSE_BUF_LEN, 0, 0, 0};
cam_fill_csio(&ccb.csio, 1, NULL, CAM_DIR_IN, MSG_SIMPLE_Q_TAG, get_configuration_response_buf,
GET_CONFIGURATION_RESPONSE_BUF_LEN, 0, GET_CONFIGURETION_CMD_LEN, 5000);
memcpy(ccb.csio.cdb_io.cdb_bytes, get_configuration_cmd, GET_CONFIGURETION_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 %sejectable!\n",get_configuration_response_buf[12] & 0b00001000 ? "" : "not ");
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