20#include <winpr/cast.h>
22#include <freerdp/config.h>
24#include <freerdp/freerdp.h>
25#include <freerdp/channels/log.h>
26#include <freerdp/server/rdpecam-enumerator.h>
28#include "rdpecam-utils.h"
30#define TAG CHANNELS_TAG("rdpecam-enumerator.server")
36} eEnumeratorChannelState;
40 CamDevEnumServerContext context;
45 void* enumerator_channel;
53 eEnumeratorChannelState state;
58static UINT enumerator_server_initialize(CamDevEnumServerContext* context, BOOL externalThread)
60 UINT error = CHANNEL_RC_OK;
61 enumerator_server* enumerator = (enumerator_server*)context;
63 WINPR_ASSERT(enumerator);
65 if (enumerator->isOpened)
67 WLog_WARN(TAG,
"Application error: Camera Device Enumerator channel already initialized, "
68 "calling in this state is not possible!");
69 return ERROR_INVALID_STATE;
72 enumerator->externalThread = externalThread;
77static UINT enumerator_server_open_channel(enumerator_server* enumerator)
79 CamDevEnumServerContext* context = &enumerator->context;
80 DWORD Error = ERROR_SUCCESS;
82 DWORD BytesReturned = 0;
83 PULONG pSessionId = NULL;
87 WINPR_ASSERT(enumerator);
89 if (WTSQuerySessionInformationA(enumerator->context.vcm, WTS_CURRENT_SESSION, WTSSessionId,
90 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
92 WLog_ERR(TAG,
"WTSQuerySessionInformationA failed!");
93 return ERROR_INTERNAL_ERROR;
96 enumerator->SessionId = (DWORD)*pSessionId;
97 WTSFreeMemory(pSessionId);
98 hEvent = WTSVirtualChannelManagerGetEventHandle(enumerator->context.vcm);
100 if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED)
102 Error = GetLastError();
103 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", Error);
107 enumerator->enumerator_channel = WTSVirtualChannelOpenEx(
108 enumerator->SessionId, RDPECAM_CONTROL_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC);
109 if (!enumerator->enumerator_channel)
111 Error = GetLastError();
112 WLog_ERR(TAG,
"WTSVirtualChannelOpenEx failed with error %" PRIu32
"!", Error);
116 channelId = WTSChannelGetIdByHandle(enumerator->enumerator_channel);
118 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
121 WLog_ERR(TAG,
"context->ChannelIdAssigned failed!");
122 return ERROR_INTERNAL_ERROR;
128static UINT enumerator_server_handle_select_version_request(CamDevEnumServerContext* context,
133 UINT error = CHANNEL_RC_OK;
135 WINPR_ASSERT(context);
136 WINPR_ASSERT(header);
138 pdu.Header = *header;
140 IFCALLRET(context->SelectVersionRequest, error, context, &pdu);
142 WLog_ERR(TAG,
"context->SelectVersionRequest failed with error %" PRIu32
"", error);
147static UINT enumerator_server_recv_device_added_notification(CamDevEnumServerContext* context,
152 UINT error = CHANNEL_RC_OK;
153 size_t remaining_length = 0;
154 WCHAR* channel_name_start = 0;
156 WINPR_ASSERT(context);
157 WINPR_ASSERT(header);
159 pdu.Header = *header;
168 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
169 return ERROR_NO_DATA;
171 pdu.DeviceName = Stream_Pointer(s);
173 remaining_length = Stream_GetRemainingLength(s);
174 channel_name_start = Stream_Pointer(s);
178 for (; i < remaining_length; i +=
sizeof(WCHAR), ++channel_name_start)
180 if (*channel_name_start == L
'\0')
184 if (*channel_name_start != L
'\0')
186 WLog_ERR(TAG,
"enumerator_server_recv_device_added_notification: Invalid DeviceName!");
187 return ERROR_INVALID_DATA;
190 pdu.VirtualChannelName = (
char*)++channel_name_start;
193 if (i >= remaining_length || *pdu.VirtualChannelName ==
'\0')
196 "enumerator_server_recv_device_added_notification: Invalid VirtualChannelName!");
197 return ERROR_INVALID_DATA;
200 char* tmp = pdu.VirtualChannelName;
201 for (; i < remaining_length; ++i, ++tmp)
210 "enumerator_server_recv_device_added_notification: Invalid VirtualChannelName!");
211 return ERROR_INVALID_DATA;
214 IFCALLRET(context->DeviceAddedNotification, error, context, &pdu);
216 WLog_ERR(TAG,
"context->DeviceAddedNotification failed with error %" PRIu32
"", error);
221static UINT enumerator_server_recv_device_removed_notification(CamDevEnumServerContext* context,
226 UINT error = CHANNEL_RC_OK;
227 size_t remaining_length = 0;
229 WINPR_ASSERT(context);
230 WINPR_ASSERT(header);
232 pdu.Header = *header;
234 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
235 return ERROR_NO_DATA;
237 pdu.VirtualChannelName = Stream_Pointer(s);
239 remaining_length = Stream_GetRemainingLength(s);
240 char* tmp = pdu.VirtualChannelName + 1;
242 for (
size_t i = 1; i < remaining_length; ++i, ++tmp)
251 "enumerator_server_recv_device_removed_notification: Invalid VirtualChannelName!");
252 return ERROR_INVALID_DATA;
255 IFCALLRET(context->DeviceRemovedNotification, error, context, &pdu);
257 WLog_ERR(TAG,
"context->DeviceRemovedNotification failed with error %" PRIu32
"", error);
262static UINT enumerator_process_message(enumerator_server* enumerator)
265 UINT error = ERROR_INTERNAL_ERROR;
266 ULONG BytesReturned = 0;
270 WINPR_ASSERT(enumerator);
271 WINPR_ASSERT(enumerator->enumerator_channel);
273 s = enumerator->buffer;
276 Stream_SetPosition(s, 0);
277 rc = WTSVirtualChannelRead(enumerator->enumerator_channel, 0, NULL, 0, &BytesReturned);
281 if (BytesReturned < 1)
283 error = CHANNEL_RC_OK;
287 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
289 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
290 error = CHANNEL_RC_NO_MEMORY;
294 if (WTSVirtualChannelRead(enumerator->enumerator_channel, 0, Stream_BufferAs(s,
char),
295 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
297 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
301 Stream_SetLength(s, BytesReturned);
302 if (!Stream_CheckAndLogRequiredLength(TAG, s, CAM_HEADER_SIZE))
303 return ERROR_NO_DATA;
305 Stream_Read_UINT8(s, header.Version);
307 const UINT8
id = Stream_Get_UINT8(s);
308 if (!rdpecam_valid_messageId(
id))
309 return ERROR_INVALID_DATA;
310 header.MessageId = (CAM_MSG_ID)
id;
313 switch (header.MessageId)
315 case CAM_MSG_ID_SelectVersionRequest:
317 enumerator_server_handle_select_version_request(&enumerator->context, s, &header);
319 case CAM_MSG_ID_DeviceAddedNotification:
321 enumerator_server_recv_device_added_notification(&enumerator->context, s, &header);
323 case CAM_MSG_ID_DeviceRemovedNotification:
324 error = enumerator_server_recv_device_removed_notification(&enumerator->context, s,
328 WLog_ERR(TAG,
"enumerator_process_message: unknown or invalid MessageId %" PRIu8
"",
335 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
340static UINT enumerator_server_context_poll_int(CamDevEnumServerContext* context)
342 enumerator_server* enumerator = (enumerator_server*)context;
343 UINT error = ERROR_INTERNAL_ERROR;
345 WINPR_ASSERT(enumerator);
347 switch (enumerator->state)
349 case ENUMERATOR_INITIAL:
350 error = enumerator_server_open_channel(enumerator);
352 WLog_ERR(TAG,
"enumerator_server_open_channel failed with error %" PRIu32
"!",
355 enumerator->state = ENUMERATOR_OPENED;
357 case ENUMERATOR_OPENED:
358 error = enumerator_process_message(enumerator);
367static HANDLE enumerator_server_get_channel_handle(enumerator_server* enumerator)
370 DWORD BytesReturned = 0;
371 HANDLE ChannelEvent = NULL;
373 WINPR_ASSERT(enumerator);
375 if (WTSVirtualChannelQuery(enumerator->enumerator_channel, WTSVirtualEventHandle, &buffer,
376 &BytesReturned) == TRUE)
378 if (BytesReturned ==
sizeof(HANDLE))
379 ChannelEvent = *(HANDLE*)buffer;
381 WTSFreeMemory(buffer);
387static DWORD WINAPI enumerator_server_thread_func(LPVOID arg)
390 HANDLE events[2] = { 0 };
391 enumerator_server* enumerator = (enumerator_server*)arg;
392 UINT error = CHANNEL_RC_OK;
395 WINPR_ASSERT(enumerator);
398 events[nCount++] = enumerator->stopEvent;
400 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
402 switch (enumerator->state)
404 case ENUMERATOR_INITIAL:
405 error = enumerator_server_context_poll_int(&enumerator->context);
406 if (error == CHANNEL_RC_OK)
408 events[1] = enumerator_server_get_channel_handle(enumerator);
412 case ENUMERATOR_OPENED:
413 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
418 case WAIT_OBJECT_0 + 1:
420 error = enumerator_server_context_poll_int(&enumerator->context);
425 error = ERROR_INTERNAL_ERROR;
434 (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
435 enumerator->enumerator_channel = NULL;
437 if (error && enumerator->context.rdpcontext)
438 setChannelError(enumerator->context.rdpcontext, error,
439 "enumerator_server_thread_func reported an error");
445static UINT enumerator_server_open(CamDevEnumServerContext* context)
447 enumerator_server* enumerator = (enumerator_server*)context;
449 WINPR_ASSERT(enumerator);
451 if (!enumerator->externalThread && (enumerator->thread == NULL))
453 enumerator->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
454 if (!enumerator->stopEvent)
456 WLog_ERR(TAG,
"CreateEvent failed!");
457 return ERROR_INTERNAL_ERROR;
461 CreateThread(NULL, 0, enumerator_server_thread_func, enumerator, 0, NULL);
462 if (!enumerator->thread)
464 WLog_ERR(TAG,
"CreateThread failed!");
465 (void)CloseHandle(enumerator->stopEvent);
466 enumerator->stopEvent = NULL;
467 return ERROR_INTERNAL_ERROR;
470 enumerator->isOpened = TRUE;
472 return CHANNEL_RC_OK;
475static UINT enumerator_server_close(CamDevEnumServerContext* context)
477 UINT error = CHANNEL_RC_OK;
478 enumerator_server* enumerator = (enumerator_server*)context;
480 WINPR_ASSERT(enumerator);
482 if (!enumerator->externalThread && enumerator->thread)
484 (void)SetEvent(enumerator->stopEvent);
486 if (WaitForSingleObject(enumerator->thread, INFINITE) == WAIT_FAILED)
488 error = GetLastError();
489 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
493 (void)CloseHandle(enumerator->thread);
494 (void)CloseHandle(enumerator->stopEvent);
495 enumerator->thread = NULL;
496 enumerator->stopEvent = NULL;
498 if (enumerator->externalThread)
500 if (enumerator->state != ENUMERATOR_INITIAL)
502 (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
503 enumerator->enumerator_channel = NULL;
504 enumerator->state = ENUMERATOR_INITIAL;
507 enumerator->isOpened = FALSE;
512static UINT enumerator_server_context_poll(CamDevEnumServerContext* context)
514 enumerator_server* enumerator = (enumerator_server*)context;
516 WINPR_ASSERT(enumerator);
518 if (!enumerator->externalThread)
519 return ERROR_INTERNAL_ERROR;
521 return enumerator_server_context_poll_int(context);
524static BOOL enumerator_server_context_handle(CamDevEnumServerContext* context, HANDLE* handle)
526 enumerator_server* enumerator = (enumerator_server*)context;
528 WINPR_ASSERT(enumerator);
529 WINPR_ASSERT(handle);
531 if (!enumerator->externalThread)
533 if (enumerator->state == ENUMERATOR_INITIAL)
536 *handle = enumerator_server_get_channel_handle(enumerator);
541static UINT enumerator_server_packet_send(CamDevEnumServerContext* context,
wStream* s)
543 enumerator_server* enumerator = (enumerator_server*)context;
544 UINT error = CHANNEL_RC_OK;
547 const size_t len = Stream_GetPosition(s);
548 WINPR_ASSERT(len <= UINT32_MAX);
549 if (!WTSVirtualChannelWrite(enumerator->enumerator_channel, Stream_BufferAs(s,
char),
550 (UINT32)len, &written))
552 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
553 error = ERROR_INTERNAL_ERROR;
557 if (written < Stream_GetPosition(s))
559 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
560 Stream_GetPosition(s));
564 Stream_Free(s, TRUE);
568static UINT enumerator_send_select_version_response_pdu(
573 s = Stream_New(NULL, CAM_HEADER_SIZE);
576 WLog_ERR(TAG,
"Stream_New failed!");
577 return ERROR_NOT_ENOUGH_MEMORY;
580 Stream_Write_UINT8(s, selectVersionResponse->Header.Version);
581 Stream_Write_UINT8(s,
582 WINPR_ASSERTING_INT_CAST(uint8_t, selectVersionResponse->Header.MessageId));
584 return enumerator_server_packet_send(context, s);
587CamDevEnumServerContext* cam_dev_enum_server_context_new(HANDLE vcm)
589 enumerator_server* enumerator = (enumerator_server*)calloc(1,
sizeof(enumerator_server));
594 enumerator->context.vcm = vcm;
595 enumerator->context.Initialize = enumerator_server_initialize;
596 enumerator->context.Open = enumerator_server_open;
597 enumerator->context.Close = enumerator_server_close;
598 enumerator->context.Poll = enumerator_server_context_poll;
599 enumerator->context.ChannelHandle = enumerator_server_context_handle;
601 enumerator->context.SelectVersionResponse = enumerator_send_select_version_response_pdu;
603 enumerator->buffer = Stream_New(NULL, 4096);
604 if (!enumerator->buffer)
607 return &enumerator->context;
609 WINPR_PRAGMA_DIAG_PUSH
610 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
611 cam_dev_enum_server_context_free(&enumerator->context);
612 WINPR_PRAGMA_DIAG_POP
616void cam_dev_enum_server_context_free(CamDevEnumServerContext* context)
618 enumerator_server* enumerator = (enumerator_server*)context;
622 enumerator_server_close(context);
623 Stream_Free(enumerator->buffer, TRUE);