aboutsummaryrefslogtreecommitdiffstats
path: root/cphidgetgeneric.c
blob: 45343552c4193cf9ea8f4bbd90164b5f9144e144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"
#include "cphidgetgeneric.h"
#include "cusb.h"
#include "csocket.h"
#include "cthread.h"

// === Internal Functions === //

//clearVars - sets all device variables to unknown state
CPHIDGETCLEARVARS(Generic)
	ZEROMEM(phid->lastPacket, MAX_IN_PACKET_SIZE);

	return EPHIDGET_OK;
}

//initAfterOpen - sets up the initial state of an object, reading in packets from the device if needed
//				  used during attach initialization - on every attach
CPHIDGETINIT(Generic)
	TESTPTR(phid);

	//Make sure no old writes are still pending
	phid->outputPacketLen = 0;

	ZEROMEM(phid->lastPacket, MAX_IN_PACKET_SIZE);

	phid->out = phid->phid.outputReportByteLength;
	phid->in = phid->phid.inputReportByteLength;

	
#ifdef _WINDOWS
	phid->out--;
	phid->in--;
#endif

	return EPHIDGET_OK;
}

//dataInput - parses device packets
CPHIDGETDATA(Generic)
	if (length<0) return EPHIDGET_INVALIDARG;
	TESTPTR(phid);
	TESTPTR(buffer);

#ifdef _WINDOWS
	length--;
#endif

	FIRE(Packet, buffer, length);
	memcpy(phid->lastPacket, buffer, length);

	return EPHIDGET_OK;
}

//eventsAfterOpen - sends out an event for all valid data, used during attach initialization
CPHIDGETINITEVENTS(Generic)
	TESTPTR(phid);
	return EPHIDGET_OK;
}

//getPacket - used by write thread to get the next packet to send to device
CGETPACKET_BUF(Generic)

//sendpacket - sends a packet to the device asynchronously, blocking if the 1-packet queue is full
CSENDPACKET_BUF(Generic)

// === Exported Functions === //

//create and initialize a device structure
CCREATE(Generic, PHIDCLASS_GENERIC)

//event setup functions
CFHANDLE(Generic, Packet,const unsigned char *, int)

CGET(Generic,INPacketLength,int)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_GENERIC)
	TESTATTACHED
	MASGN(in)
}

CGET(Generic,OUTPacketLength,int)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_GENERIC)
	TESTATTACHED
	MASGN(out)
}

int CCONV
CPhidgetGeneric_setPacket(CPhidgetGenericHandle phid, unsigned char *packet, int length)
{
	int ret = 0;

	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_GENERIC)
	TESTATTACHED

	if(length!=phid->out)
		return EPHIDGET_INVALIDARG;

	CThread_mutex_lock(&phid->phid.writelock);
	ret = CPhidgetGeneric_sendpacket(phid, packet);
	CThread_mutex_unlock(&phid->phid.writelock);
	if(ret) return ret;

	return EPHIDGET_OK;
}

int CCONV
CPhidgetGeneric_getLastPacket(CPhidgetGenericHandle phid, const unsigned char **packet, int *length)
{
	TESTPTRS(phid, packet)
	TESTPTR(length)
	TESTDEVICETYPE(PHIDCLASS_GENERIC)
	TESTATTACHED

	*packet = phid->lastPacket;
	*length = phid->in;
	return EPHIDGET_OK;
}