#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/scsi/scsi_pass.h>
 
#define XPT_DEVICE          "/dev/xpt0"
#define N_RESULTS   100
 
int main(int argc, char* argv[])
{
    union ccb ccb;
    struct dev_match_result matches[N_RESULTS];
   
    int res;
    int req_unit;
    int found_path = -1;
   
    int xpt;
   
    if(argc < 2)
    {
        printf("not enough arguments\n");
        return 1;
    }
   
    res = sscanf(argv[1], "umass%d", &req_unit);
    if(!res || res==-1)
    {
        printf("bad usb driver name\n");
        return 1;
    }
 
    if ((xpt = open(XPT_DEVICE, O_RDWR)) == -1) {
        printf("couldn't open xpt device: %s\t", strerror(errno));
        return 1;
    }
 
    bzero(&ccb, sizeof(union ccb));
 
    ccb.ccb_h.path_id = CAM_XPT_PATH_ID;
    ccb.ccb_h.target_id = CAM_TARGET_WILDCARD;
    ccb.ccb_h.target_lun = CAM_LUN_WILDCARD;
 
    ccb.ccb_h.func_code = XPT_DEV_MATCH;
    ccb.cdm.match_buf_len = sizeof(struct dev_match_result) * N_RESULTS;
    ccb.cdm.matches = matches;
    ccb.cdm.num_matches = 0;
 
    /*
     * Fetch all nodes
     */
    ccb.cdm.num_patterns = 0;
    ccb.cdm.pattern_buf_len = 0;
 
    /*
     * We do the ioctl multiple times if necessary, in case there are
     * more than 100 nodes in the EDT.
     */
    do
    {
        if (ioctl(xpt, CAMIOCOMMAND, &ccb) == -1)
        {
            printf("error sending CAMIOCOMMAND ioctl: %s", strerror(errno));
            return 1;
        }
 
        if ((ccb.ccb_h.status != CAM_REQ_CMP)
            || ((ccb.cdm.status != CAM_DEV_MATCH_LAST)
            && (ccb.cdm.status != CAM_DEV_MATCH_MORE)))
        {
            printf("got CAM error %#x, CDM error %d\n",
                   ccb.ccb_h.status, ccb.cdm.status);
            return 1;
        }
 
        for (int i = 0; i < ccb.cdm.num_matches; i++) {
            switch (ccb.cdm.matches[i].type)
            {
            case DEV_MATCH_BUS:
                {
                    struct bus_match_result *match =
                        &ccb.cdm.matches[i].result.bus_result;
                    if(!strcmp("umass-sim", match->dev_name)
                    && req_unit == match->unit_number)
                        found_path = match->path_id;
 
                    break;
                }
            default: break;
            }
        }
 
        if (found_path == -1)
        {
            printf("not found\n");
            return 1;
        }
           
        for (int i = 0; i < ccb.cdm.num_matches; i++)
            if(ccb.cdm.matches[i].type == DEV_MATCH_PERIPH)
            {
                struct periph_match_result *match =
                    &ccb.cdm.matches[i].result.periph_result;
                   
                if(match->path_id == found_path
                   && strcmp("pass", match->periph_name))
                    printf("%s%i", match->periph_name, match->unit_number);
            }
    }
    while ((ccb.ccb_h.status == CAM_REQ_CMP)
        && (ccb.cdm.status == CAM_DEV_MATCH_MORE));
 
    close(xpt);
    return 0;
}
