Skip to content

Instantly share code, notes, and snippets.

@kahagon
Last active December 15, 2015 06:09
Show Gist options
  • Select an option

  • Save kahagon/5214383 to your computer and use it in GitHub Desktop.

Select an option

Save kahagon/5214383 to your computer and use it in GitHub Desktop.
<?php
$context = null;
$vendor_id = 0x04a9;
$product_id = 0x10d3;
$result_init = usb_init($context);
if ($result_init != USB_SUCCESS) {
die('failed to usb_init(). ' . usb_error_name($result_init));
}
$config_descriptor = null;
$device_handle = usb_open_device_with_vid_pid($context, $vendor_id, $product_id);
if ($device_handle == null) {
printf("failed to open printer(vid: 0x%04x, pid: 0x%04x)\n", $vendor_id, $product_id);
goto out;
}
$device = usb_get_device($device_handle);
usb_get_active_config_descriptor($device, $config_descriptor);
$status = get_port_status($device_handle, 0);
if ($status->not_error) {
$data = file_get_contents('bulk.out'); /* load captured data with wireshark */
$endpoint = $config_descriptor->interface[0]->altsetting[0]->endpoint[0]->bEndpointAddress;
$transferred = print_ip2700($device_handle, 0, $endpoint, $data);
printf("transferred: %d\n", $transferred);
}
out:
if ($device_handle) usb_close($device_handle);
if ($context) usb_exit($context);
function check_kernel_driver($device_handle, $interface_index) {
if (usb_kernel_driver_active($device_handle, $interface_index)) {
return usb_detach_kernel_driver($device_handle, $interface_index);
}
return USB_SUCCESS;
}
function get_protocol_status_information($device_handle, $interface_index, $endpoint) {
$data = null;
$transferred = 0;
if (($res = check_kernel_driver($device_handle, $interface_index)) != USB_SUCCESS) {
printf("failed to detach kernel driver. error: %s\n", usb_error_name($res));
return null;
}
$res = usb_bulk_transfer($device_handle, $endpoint, $data, -1, $transferred, 1000);
if ($res < 0) {
printf("failed to get_protocol_status_information(). error : %s\n", usb_error_name($res));
return null;
}
return $data;
}
function print_ip2700($device_handle, $interface_index, $endpoint, $script) {
$data = null;
$transferred = 0;
if (($res = check_kernel_driver($device_handle, $interface_index)) != USB_SUCCESS) {
printf("failed to detach kernel driver. error: %s\n", usb_error_name($res));
return $res;
}
$res = usb_bulk_transfer($device_handle, $endpoint, $script, strlen($script), $transferred, 0);
if ($res < 0) {
printf("failed to print_ip2700(). error : %s\n", usb_error_name($res));
return $res;
}
return $transferred;
}
function get_port_status($device_handle, $interface_index) {
/* Printer Class Specific Request GET_PORT_STATUS */
$data = null;
$bmRequestType = 0b10100001;
$bRequest = 1;
$wValue = 0;
$wIndex = $interface_index;
if (($res = check_kernel_driver($device_handle, $interface_index)) != USB_SUCCESS) {
printf("failed to detach kernel driver. error: %s\n", usb_error_name($res));
return null;
}
$res = usb_control_transfer($device_handle, $bmRequestType, $bRequest, $wValue, $wIndex, $data, -1, 1000);
if ($res < 1) {
printf("failed to get_port_status(). error : %s\n", usb_error_name($res));
return null;
}
$data = ord($data);
return (object)[
'paper_empty' => $data & (1<<5),
'select' => $data & (1<<4),
'not_error' => $data & (1<<3)];
}
function get_device_id($device_handle, $config_index, $interface_index, $alt_setting_index) {
/* Printer Class Specific Request GET_DEVICE_ID */
$device_id = null;
$bmRequestType = 0b10100001;
$bRequest = 0;
$wValue = $config_index;
$wIndex = ($interface_index<<4) & $alt_setting_index;
if (($res = check_kernel_driver($device_handle, $interface_index)) != USB_SUCCESS) {
printf("failed to detach kernel driver. error: %s\n", usb_error_name($res));
return null;
}
$res = usb_control_transfer($device_handle, $bmRequestType, $bRequest, $wValue, $wIndex, $device_id, -1, 1000);
if ($res < 1) {
printf("failed to get_device_id(). error : %s\n", usb_error_name($res));
return null;
}
/* the first two bytes is length of device id string which include those. */
return substr($device_id, 2, $res - 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment