source: thomson/elec/CrO2/software/device_bekit.cpp@ e017851

main
Last change on this file since e017851 was e017851, checked in by Adrien Destugues <pulkomandy@…>, 11 years ago

Port to Haiku. Untested.

git-svn-id: svn://localhost/thomson@28 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/* CrO2 datassette emulator
2 * Copyright 2013, Adrien Destugues <pulkomandy@pulkomandy.tk>
3 *
4 * Distributed under the terms of the MIT licence.
5 *
6 * Handles device communication through libusb
7 */
8
9#include "device_bekit.h"
10#include "k5.h"
11
12#include <typeinfo>
13#include <USBKit.h>
14
15
16// Gets the device instance. Throws an error message if something bad happens.
17Device& Device::getDevice() throw(const char*)
18{
19 if (instance == NULL) {
20 instance = new HaikuDevice();
21 }
22
23 return *instance;
24}
25
26
27HaikuDevice::HaikuDevice() throw(const char*)
28{
29 // At this point, either we have found a device and handle is pointing to it,
30 // or we failed and handle is NULL.
31 if (!handle)
32 throw "Device not found. Is the USB cable plugged correctly?";
33}
34
35
36HaikuDevice::~HaikuDevice()
37{
38 delete handle;
39}
40
41
42int HaikuDevice::read(uint8_t* buffer, size_t max)
43{
44 return handle->ControlTransfer(USB_REQTYPE_VENDOR | USB_REQTYPE_ENDPOINT_IN,
45 PSCMD_GET, 0,0, max, (char*)buffer);
46
47}
48
49
50int HaikuDevice::write(const uint8_t* buffer, size_t size, int blktype)
51{
52 int rqtype = (size == 0) ? USB_REQTYPE_ENDPOINT_IN:USB_REQTYPE_ENDPOINT_OUT;
53 return handle->ControlTransfer(USB_REQTYPE_VENDOR | rqtype,
54 PSCMD_PUT, blktype,0 /*checksum*/, size, (char*)buffer);
55
56}
57
58
59uint8_t HaikuDevice::getStatus()
60{
61 uint8_t status;
62 handle->ControlTransfer(USB_REQTYPE_VENDOR | USB_REQTYPE_ENDPOINT_IN,
63 PSCMD_STATUS, 0,0, 1, (char*)&status);
64 // TODO handle errors (return value)
65 return status;
66}
67
68
69status_t HaikuDevice::DeviceAdded(BUSBDevice* device)
70{
71 if (handle != NULL) {
72 // We already have a device !
73 return B_ERROR;
74 }
75
76 // Check if the device matches what we can handle
77
78 if (device->VendorID() != vid && device->ProductID() != pid)
79 return B_ERROR;
80
81 // Since we use V-USB shared vid/pid pair, we also have to check the
82 // manufacturer and product strings to actually identify the device amongst
83 // other users of the pair.
84 if (strcmp(device->ManufacturerString(), vendor) != 0)
85 return B_ERROR;
86
87 if (strcmp(device->ProductString(), product) != 0)
88 return B_ERROR;
89
90 // Ok, we have found our device !
91 handle = device;
92 return B_OK;
93}
94
95
96void HaikuDevice::DeviceRemoved(BUSBDevice* device)
97{
98 // This is only called for devices we accepted in DeviceAdded. We accept
99 // only one, so we can safely remove it.
100 handle = NULL;
101}
102
Note: See TracBrowser for help on using the repository browser.