aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Portable/pmp_wifi/WifiDevice.cpp
blob: 0b97b96e36ced4df21e7c650bdff2d27cc45e6cf (plain) (blame)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "WifiDevice.h"
#include "api.h"
#include "device.h"
#include "nu/ns_wc.h"
#include "resource.h"
#include "Pair.h"
#include "images.h"
#include "modelInfo.h"
#include "SongListDownloader.h"
#include <strsafe.h>

WifiDevice::WifiDevice(const char *root_url, const DeviceInfo *in_device_info)
: url(strdup(root_url))
{
	DeviceInfo_Copy(&device_info, in_device_info);
		InitializeCriticalSection(&register_lock);
	dead=0;
	connect_active=false;
	pmp_device=0;
	StringCbPrintfA(id_string, sizeof(id_string), "%016I64x", device_info.id);
	if (IsPaired(device_info.id))
	{
		char full_url[256] = {0};
		StringCbPrintfA(full_url, sizeof(full_url), "%s/library", url);
		WAC_API_DOWNLOADMANAGER->DownloadEx(full_url, new SongListDownloader(url, this), api_downloadManager::DOWNLOADEX_CALLBACK);
	}
	else
	{
		ifc_device *device = this;
		AGAVE_API_DEVICEMANAGER->DeviceRegister(&device, 1);
	}
}

WifiDevice::~WifiDevice()
{
	DeleteCriticalSection(&register_lock);
}

/* ifc_device stuff */
int WifiDevice::QueryInterface(GUID interface_guid, void **object)
{
	if (interface_guid == IFC_Device)
	{
		AddRef();
		*object = (ifc_device *)this;
		return 0;
	}
	return 1;
}

const char *WifiDevice::GetName()
{
	return id_string;
}

HRESULT WifiDevice::GetDisplayName(wchar_t *buffer, size_t bufferSize)
{
	StringCchCopyW(buffer, bufferSize, device_info.name);
	return 0;
}

const char *WifiDevice::GetType()
{
	return "portable";
}

const char *WifiDevice::GetConnection()
{
	return "wifi";
}

extern ifc_devicesupportedcommandenum *command_enum;
extern ifc_devicesupportedcommandstore *command_store;
extern ifc_deviceeventmanager *device_event_manager;

HRESULT WifiDevice::EnumerateCommands(ifc_devicesupportedcommandenum **enumerator, DeviceCommandContext context)
{
	if (connect_active)
		return E_NOTIMPL;

	return command_store->Enumerate(enumerator);
}

HRESULT WifiDevice::SendCommand(const char *command, HWND hostWindow, ULONG_PTR param)
{
	if (!strcmp(command, "attach"))
	{
		return Attach(hostWindow);
	}

	return 0;
}

BOOL WifiDevice::GetAttached()
{
	return FALSE;
}

HRESULT WifiDevice::Attach(HWND hostWindow)
{
	if (!connect_active)
	{
		connect_active = true;
		device_event_manager->Notify_ActivityStarted(this, &connect_activity);

		char full_url[256] = {0};
		StringCbPrintfA(full_url, sizeof(full_url), "%s/pair", url);
		WAC_API_DOWNLOADMANAGER->DownloadEx(full_url, new PairDownloader(this), api_downloadManager::DOWNLOADEX_CALLBACK);
	}

	return S_OK;
}

HRESULT WifiDevice::Detach(HWND hostWindow)
{
	return S_OK;
}

HRESULT WifiDevice::Advise(ifc_deviceevent *handler)
{
	return device_event_manager->Advise(handler);
}

HRESULT WifiDevice::Unadvise(ifc_deviceevent *handler)
{
	return device_event_manager->Unadvise(handler);
}

HRESULT WifiDevice::GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
{
	return ModelInfo_GetIconPath(device_info.modelInfo, width, height, buffer, bufferSize, TRUE);
}

void WifiDevice::OnPaired()
{
	char full_url[256] = {0};
	StringCbPrintfA(full_url, sizeof(full_url), "%s/library", url);
	WAC_API_DOWNLOADMANAGER->DownloadEx(full_url, new SongListDownloader(url, this), api_downloadManager::DOWNLOADEX_CALLBACK);
	SetPaired(device_info.id, true);
}

void WifiDevice::OnConnected(TemplateDevice *device)
{
	EnterCriticalSection(&register_lock);
	pmp_device = device;
	connect_active = false;
	device_event_manager->Notify_ActivityFinished(this, &connect_activity);
	AGAVE_API_DEVICEMANAGER->DeviceUnregister(id_string);
	// if we disconnected/timed out on the listen server while connecting, go ahead and close the device out
	if (dead && pmp_device)
	{
		pmp_device->CloseAsync();
		pmp_device = 0;
	}
	LeaveCriticalSection(&register_lock);
}

void WifiDevice::OnDisconnect()
{
	// TODO: might actually need a crit sec here
	EnterCriticalSection(&register_lock);
	dead=1;
	if (pmp_device)
	{
		pmp_device->CloseAsync();
		pmp_device = 0;
	}
	else
	{
		AGAVE_API_DEVICEMANAGER->DeviceUnregister(id_string);
	}
	LeaveCriticalSection(&register_lock);
}

void WifiDevice::OnConnectionFailed()
{
	EnterCriticalSection(&register_lock);
	delete pmp_device;
	pmp_device = 0;
	ifc_device *device = NULL;
	bool device_exist = false;

	// see if we're already registered (e.g. we started in unpaired state)
	if (AGAVE_API_DEVICEMANAGER->DeviceFind(id_string, &device) == S_OK)
	{
		if (device == this)
			device_exist = true;

		device->Release();
	}

	if (device_exist)
	{ // if we are, then notify about activity being done
		connect_active = false;
		device_event_manager->Notify_ActivityFinished(this, &connect_activity);
	}
	else if (!dead)
	{ // if we weren't registered, we thought we were paired but failed
		device = this;
		AGAVE_API_DEVICEMANAGER->DeviceRegister(&device, 1);
	}


	LeaveCriticalSection(&register_lock);
}

HRESULT WifiDevice::GetActivity(ifc_deviceactivity **activity)
{
	if (connect_active)
	{
		*activity = &connect_activity;
		return S_OK;
	}
	else
	{
		return E_FAIL;
	}
}

HRESULT WifiDevice::GetTotalSpace(uint64_t *size)
{
#if 0
	if (device_info.total_space)
	{
		*size = device_info.total_space;
		return S_OK;
	}
#endif
	return E_NOTIMPL;
}

HRESULT WifiDevice::GetUsedSpace(uint64_t *size)
{
#if 0
	if (device_info.used_space)
	{
		*size = device_info.used_space;
		return S_OK;
	}
#endif
	return E_NOTIMPL;
}

HRESULT WifiDevice::GetModel(wchar_t *buffer, size_t bufferSize)
{
	return ModelInfo_CopyDisplayName(device_info.modelInfo, buffer, bufferSize);
}

#define CBCLASS WifiDevice
START_DISPATCH;
CB(QUERYINTERFACE, QueryInterface);
CB(API_GETNAME, GetName);
CB(API_GETICON, GetIcon);
CB(API_GETDISPLAYNAME, GetDisplayName);
CB(API_GETTOTALSPACE, GetTotalSpace);
CB(API_GETUSEDSPACE, GetUsedSpace);
CB(API_GETTYPE, GetType);
CB(API_GETCONNECTION, GetConnection);
CB(API_ENUMERATECOMMANDS, EnumerateCommands);
CB(API_SENDCOMMAND, SendCommand);
CB(API_GETATTACHED, GetAttached);
CB(API_ATTACH, Attach);
CB(API_DETACH, Detach);
CB(API_GETACTIVITY, GetActivity);
CB(API_ADVISE, Advise);
CB(API_UNADVISE, Unadvise);
CB(API_GETMODEL, GetModel);
REFERENCE_COUNTED;
END_DISPATCH;