aboutsummaryrefslogtreecommitdiffstats
path: root/cphidgetsbc.c
blob: 3b6b94ffffaf00a8a10c79923f4e015968b4fdf1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "stdafx.h"
#include "cphidgetsbc.h"
#include "cphidget.h"
#include "cthread.h"
#include "cphidgetlist.h"
#include "csocket.h"
#include "zeroconf.h"

//PhidgetSBCManager

//Private
void CPhidgetSBCManager_free(void *arg)
{
	CPhidgetSBCManagerHandle sbcm = (CPhidgetSBCManagerHandle)arg;
	
	if(!sbcm) return;
	free(sbcm); sbcm = NULL;
	return;
}

//Public
int CCONV CPhidgetSBCManager_create(CPhidgetSBCManagerHandle *sbcm)
{
	CPhidgetSBCManagerHandle sbcmtemp = 0;
	
	TESTPTR(sbcm)
	if(!(sbcmtemp = (CPhidgetSBCManagerHandle)malloc(sizeof(CPhidgetSBCManager))))
		return EPHIDGET_NOMEMORY;
	ZEROMEM(sbcmtemp, sizeof(CPhidgetSBCManager));

	*sbcm = sbcmtemp;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBCManager_stop(CPhidgetSBCManagerHandle sbcm)
{
	TESTPTR(sbcm)

	if(sbcm->state == PHIDGETMANAGER_ACTIVE)
	{
		sbcm->state = PHIDGETMANAGER_INACTIVE;
		unregisterSBCManager(sbcm);
	}

	return EPHIDGET_OK;
}

int CCONV CPhidgetSBCManager_delete(CPhidgetSBCManagerHandle sbcm)
{
	CPhidgetSBCManager_free(sbcm);
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBCManager_set_OnAttach_Handler(CPhidgetSBCManagerHandle sbcm, int (CCONV *fptr)(CPhidgetSBCHandle sbc, void *userPtr), void *userPtr)
{
	TESTPTR(sbcm)
	sbcm->fptrAttachChange = fptr; 
	sbcm->fptrAttachChangeptr = userPtr; 
	return EPHIDGET_OK; 
}

int CCONV CPhidgetSBCManager_set_OnDetach_Handler(CPhidgetSBCManagerHandle sbcm, int (CCONV *fptr)(CPhidgetSBCHandle sbc, void *userPtr), void *userPtr)
{
	TESTPTR(sbcm)
	sbcm->fptrDetachChange = fptr; 
	sbcm->fptrDetachChangeptr = userPtr; 
	return EPHIDGET_OK; 
}

int CCONV CPhidgetSBCManager_set_OnError_Handler(CPhidgetSBCManagerHandle sbcm, 
	int(CCONV *fptr)(CPhidgetSBCManagerHandle, void *, int, const char *), void *userPtr)
{
	TESTPTR(sbcm)
	sbcm->fptrError = fptr;
	sbcm->fptrErrorptr = userPtr;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBCManager_getAttachedSBCs(CPhidgetSBCManagerHandle sbcm, CPhidgetSBCHandle *sbcArray[], int *count)
{
	TESTPTRS(sbcArray, count)
	TESTPTR(sbcm)

	return EPHIDGET_UNSUPPORTED; 
}


//PhidgetSBC

//Private
int CCONV CPhidgetSBC_areExtraEqual(void *arg1, void *arg2)
{
	CPhidgetSBCHandle sbc1 = (CPhidgetSBCHandle)arg1;
	CPhidgetSBCHandle sbc2 = (CPhidgetSBCHandle)arg2;
	
	TESTPTRS(sbc1, sbc2)
	
	if(!strcmp(sbc1->mac, sbc2->mac) 
	   && !strcmp(sbc1->fversion, sbc2->fversion) 
	   && sbc1->hversion == sbc2->hversion
	   && !strcmp(sbc1->hostname?sbc1->hostname:"", sbc2->hostname?sbc2->hostname:""))
		return PTRUE;
	return PFALSE;
}

int CCONV CPhidgetSBC_areEqual(void *arg1, void *arg2)
{
	CPhidgetSBCHandle sbc1 = (CPhidgetSBCHandle)arg1;
	CPhidgetSBCHandle sbc2 = (CPhidgetSBCHandle)arg2;
	
	if(!sbc1 || !sbc2)
		return PFALSE;
		
	if(!strcmp(sbc1->mac, sbc2->mac))
		return PTRUE;
	return PFALSE;
}

void CCONV CPhidgetSBC_free(void *arg)
{
	CPhidgetSBCHandle sbc = (CPhidgetSBCHandle)arg;
	if (!sbc)
		return;

	CPhidgetRemote_free(sbc->networkInfo);

	free(sbc); sbc = NULL;
	return;
}

//Public
int CCONV CPhidgetSBC_create(CPhidgetSBCHandle *sbc)
{
	CPhidgetSBCHandle sbctemp = 0;
	
	TESTPTR(sbc)
	if(!(sbctemp = (CPhidgetSBCHandle)malloc(sizeof(CPhidgetSBC))))
		return EPHIDGET_NOMEMORY;
	ZEROMEM(sbctemp, sizeof(CPhidgetSBC));
	
	// Version 1 doesn't support hostname variable
	sbctemp->txtver = 1;

	*sbc = sbctemp;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBC_delete(CPhidgetSBCHandle sbc)
{
	CPhidgetSBC_free(sbc); sbc = NULL;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBC_getFirmwareVersion(CPhidgetSBCHandle sbc, const char **version)
{
	TESTPTRS(sbc, version)

	*version = (char *)sbc->fversion;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBC_getHardwareVersion(CPhidgetSBCHandle sbc, int *version)
{
	TESTPTRS(sbc, version)

	*version = sbc->hversion;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBC_getMacAddress(CPhidgetSBCHandle sbc, const char **mac)
{
	TESTPTRS(sbc, mac)

	*mac = (char *)sbc->mac;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBC_getDeviceName(CPhidgetSBCHandle sbc, const char **name)
{
	TESTPTRS(sbc, name)

	*name = (char *)sbc->deviceName;
	return EPHIDGET_OK;
}

int CCONV CPhidgetSBC_getHostname(CPhidgetSBCHandle sbc, const char **hostname)
{
	TESTPTRS(sbc, hostname)

#ifdef USE_ZEROCONF
	if(sbc->txtver >= 2)
	{
		refreshZeroconfSBC(sbc);
		*hostname = (char *)sbc->hostname;
		return EPHIDGET_OK;
	}
	else
#endif
	{
		*hostname = NULL;
		return EPHIDGET_UNSUPPORTED;
	}
}

int CCONV CPhidgetSBC_getAddress(CPhidgetSBCHandle sbc, const char **ipAddr)
{
	TESTPTRS(sbc, ipAddr)
	if (!sbc->networkInfo)
		return EPHIDGET_NETWORK_NOTCONNECTED;
	if(!sbc->networkInfo->mdns) //not mdns
	{
		return EPHIDGET_UNEXPECTED;
	}
#ifdef USE_ZEROCONF
	if(getZeroconfHostPort(sbc->networkInfo))
		return EPHIDGET_NETWORK;
	if(!sbc->networkInfo->zeroconf_host)
	{
		return EPHIDGET_NETWORK;
	}
	*ipAddr = (char *)sbc->networkInfo->zeroconf_host;
	return EPHIDGET_OK;
#else
	return EPHIDGET_UNEXPECTED;
#endif
}

int CCONV CPhidgetSBC_getIPAddressList(CPhidgetSBCHandle sbc, long *list, unsigned int *size)
{
	const char *addr;
	struct hostent *addr_lookup;
	CPhidgetSBC_getAddress(sbc, &addr);
	/* this will resolve to an IP address, including .local hostnames (for SBC, because it can't resolve .local hostnames on its own) */
#ifdef ZEROCONF_LOOKUP
	addr_lookup = mdns_gethostbyname(addr);
#else
	addr_lookup = gethostbyname(addr);
#endif
	if (addr_lookup == NULL)
	{
	   return EPHIDGET_UNKNOWNVAL;
	}
	else
	{
	   unsigned int i = 0;
	   while ( addr_lookup -> h_addr_list[i] != NULL && i<*size) {
		   struct in_addr inaddr = *(( struct in_addr*)( addr_lookup -> h_addr_list[i]));
		   list[i] = inaddr.s_addr;
		   i++;
	   }
	   *size = i;
	}

	return EPHIDGET_OK;
}