1/*
2 * Copyright 2020, J��r��me Duval, jerome.duval@gmail.com.
3 * Copyright 2008-2011, Michael Lotz <mmlr@mlotz.ch>
4 * Distributed under the terms of the MIT license.
5 */
6#ifndef I2C_HID_DEVICE_H
7#define I2C_HID_DEVICE_H
8
9
10#include <i2c.h>
11
12#include "HIDParser.h"
13#include "I2CHIDProtocol.h"
14
15
16class ProtocolHandler;
17
18
19class HIDDevice {
20public:
21								HIDDevice(uint16 descriptorAddress, i2c_device_interface* i2c,
22									i2c_device i2cCookie);
23								~HIDDevice();
24
25			status_t			InitCheck() const { return fStatus; }
26
27			bool				IsOpen() const { return fOpenCount > 0; }
28			status_t			Open(ProtocolHandler *handler, uint32 flags);
29			status_t			Close(ProtocolHandler *handler);
30			int32				OpenCount() const { return fOpenCount; }
31
32			void				Removed();
33			bool				IsRemoved() const { return fRemoved; }
34
35			status_t			MaybeScheduleTransfer(HIDReport *report);
36
37			status_t			SendReport(HIDReport *report);
38
39			HIDParser &			Parser() { return fParser; }
40			ProtocolHandler *	ProtocolHandlerAt(uint32 index) const;
41
42private:
43	static	void				_TransferCallback(void *cookie,
44									status_t status, void *data,
45									size_t actualLength);
46	static	void				_UnstallCallback(void *cookie,
47									status_t status, void *data,
48									size_t actualLength);
49
50			status_t			_Reset();
51			status_t			_SetPower(uint8 power);
52			status_t			_FetchBuffer(uint8* cmd, size_t cmdLength,
53									void* buffer, size_t bufferLength);
54			status_t			_FetchReport(uint8 type, uint8 id,
55									size_t reportSize);
56			status_t			_ExecCommand(i2c_op op, uint8* cmd,
57									size_t cmdLength, void* buffer,
58									size_t bufferLength);
59
60private:
61			status_t			fStatus;
62
63
64			bigtime_t			fTransferLastschedule;
65			int32				fTransferScheduled;
66			size_t				fTransferBufferSize;
67			uint8 *				fTransferBuffer;
68
69			int32				fOpenCount;
70			bool				fRemoved;
71
72			HIDParser			fParser;
73
74			uint32				fProtocolHandlerCount;
75			ProtocolHandler *	fProtocolHandlerList;
76
77			uint16				fDescriptorAddress;
78			i2c_hid_descriptor	fDescriptor;
79
80			uint8*				fReportDescriptor;
81
82			i2c_device_interface*	fI2C;
83			i2c_device				fI2CCookie;
84
85};
86
87
88#endif // I2C_HID_DEVICE_H
89