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;
81 HANDLE hEvent =
nullptr;
82 DWORD BytesReturned = 0;
83 PULONG pSessionId =
nullptr;
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,
151 UINT error = CHANNEL_RC_OK;
153 WINPR_ASSERT(context);
154 WINPR_ASSERT(header);
163 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
164 return ERROR_NO_DATA;
167 .DeviceName = Stream_Pointer(s),
168 .VirtualChannelName =
nullptr };
171 bool unicodeNull =
false;
172 while (Stream_GetRemainingLength(s) >=
sizeof(WCHAR))
174 const WCHAR wc = Stream_Get_UINT16(s);
183 WLog_ERR(TAG,
"enumerator_server_recv_device_added_notification: Invalid DeviceName!");
184 return ERROR_INVALID_DATA;
188 pdu.VirtualChannelName = Stream_PointerAs(s,
char);
189 bool ansiNull =
false;
190 while (Stream_GetRemainingLength(s) >=
sizeof(CHAR))
192 const CHAR wc = Stream_Get_INT8(s);
202 "enumerator_server_recv_device_added_notification: Invalid VirtualChannelName!");
203 return ERROR_INVALID_DATA;
206 const size_t rem = Stream_GetRemainingLength(s);
208 WLog_WARN(TAG,
"Unparsed data: %" PRIuz
" bytes remain", rem);
210 IFCALLRET(context->DeviceAddedNotification, error, context, &pdu);
212 WLog_ERR(TAG,
"context->DeviceAddedNotification failed with error %" PRIu32
"", error);
217static UINT enumerator_server_recv_device_removed_notification(CamDevEnumServerContext* context,
221 UINT error = CHANNEL_RC_OK;
223 WINPR_ASSERT(context);
224 WINPR_ASSERT(header);
226 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
227 return ERROR_NO_DATA;
230 .VirtualChannelName = Stream_PointerAs(s,
char) };
233 bool ansiNull =
false;
234 while (Stream_GetRemainingLength(s) >=
sizeof(CHAR))
236 const CHAR c = Stream_Get_INT8(s);
246 "enumerator_server_recv_device_removed_notification: Invalid VirtualChannelName!");
247 return ERROR_INVALID_DATA;
250 IFCALLRET(context->DeviceRemovedNotification, error, context, &pdu);
252 WLog_ERR(TAG,
"context->DeviceRemovedNotification failed with error %" PRIu32
"", error);
257static UINT enumerator_process_message(enumerator_server* enumerator)
260 UINT error = ERROR_INTERNAL_ERROR;
261 ULONG BytesReturned = 0;
265 WINPR_ASSERT(enumerator);
266 WINPR_ASSERT(enumerator->enumerator_channel);
268 s = enumerator->buffer;
271 Stream_ResetPosition(s);
272 rc = WTSVirtualChannelRead(enumerator->enumerator_channel, 0,
nullptr, 0, &BytesReturned);
276 if (BytesReturned < 1)
278 error = CHANNEL_RC_OK;
282 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
284 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
285 error = CHANNEL_RC_NO_MEMORY;
289 if (WTSVirtualChannelRead(enumerator->enumerator_channel, 0, Stream_BufferAs(s,
char),
290 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
292 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
296 if (!Stream_SetLength(s, BytesReturned))
297 return ERROR_INTERNAL_ERROR;
299 if (!Stream_CheckAndLogRequiredLength(TAG, s, CAM_HEADER_SIZE))
300 return ERROR_NO_DATA;
302 Stream_Read_UINT8(s, header.Version);
304 const UINT8
id = Stream_Get_UINT8(s);
305 if (!rdpecam_valid_messageId(
id))
306 return ERROR_INVALID_DATA;
307 header.MessageId = (CAM_MSG_ID)
id;
310 switch (header.MessageId)
312 case CAM_MSG_ID_SelectVersionRequest:
314 enumerator_server_handle_select_version_request(&enumerator->context, s, &header);
316 case CAM_MSG_ID_DeviceAddedNotification:
318 enumerator_server_recv_device_added_notification(&enumerator->context, s, &header);
320 case CAM_MSG_ID_DeviceRemovedNotification:
321 error = enumerator_server_recv_device_removed_notification(&enumerator->context, s,
325 WLog_ERR(TAG,
"enumerator_process_message: unknown or invalid MessageId %" PRIu8
"",
332 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
337static UINT enumerator_server_context_poll_int(CamDevEnumServerContext* context)
339 enumerator_server* enumerator = (enumerator_server*)context;
340 UINT error = ERROR_INTERNAL_ERROR;
342 WINPR_ASSERT(enumerator);
344 switch (enumerator->state)
346 case ENUMERATOR_INITIAL:
347 error = enumerator_server_open_channel(enumerator);
349 WLog_ERR(TAG,
"enumerator_server_open_channel failed with error %" PRIu32
"!",
352 enumerator->state = ENUMERATOR_OPENED;
354 case ENUMERATOR_OPENED:
355 error = enumerator_process_message(enumerator);
364static HANDLE enumerator_server_get_channel_handle(enumerator_server* enumerator)
366 void* buffer =
nullptr;
367 DWORD BytesReturned = 0;
368 HANDLE ChannelEvent =
nullptr;
370 WINPR_ASSERT(enumerator);
372 if (WTSVirtualChannelQuery(enumerator->enumerator_channel, WTSVirtualEventHandle, &buffer,
373 &BytesReturned) == TRUE)
375 if (BytesReturned ==
sizeof(HANDLE))
376 ChannelEvent = *(HANDLE*)buffer;
378 WTSFreeMemory(buffer);
384static DWORD WINAPI enumerator_server_thread_func(LPVOID arg)
387 HANDLE events[2] = WINPR_C_ARRAY_INIT;
388 enumerator_server* enumerator = (enumerator_server*)arg;
389 UINT error = CHANNEL_RC_OK;
392 WINPR_ASSERT(enumerator);
395 events[nCount++] = enumerator->stopEvent;
397 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
399 switch (enumerator->state)
401 case ENUMERATOR_INITIAL:
402 error = enumerator_server_context_poll_int(&enumerator->context);
403 if (error == CHANNEL_RC_OK)
405 events[1] = enumerator_server_get_channel_handle(enumerator);
409 case ENUMERATOR_OPENED:
410 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
415 case WAIT_OBJECT_0 + 1:
417 error = enumerator_server_context_poll_int(&enumerator->context);
422 error = ERROR_INTERNAL_ERROR;
431 (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
432 enumerator->enumerator_channel =
nullptr;
434 if (error && enumerator->context.rdpcontext)
435 setChannelError(enumerator->context.rdpcontext, error,
436 "enumerator_server_thread_func reported an error");
442static UINT enumerator_server_open(CamDevEnumServerContext* context)
444 enumerator_server* enumerator = (enumerator_server*)context;
446 WINPR_ASSERT(enumerator);
448 if (!enumerator->externalThread && (enumerator->thread ==
nullptr))
450 enumerator->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
451 if (!enumerator->stopEvent)
453 WLog_ERR(TAG,
"CreateEvent failed!");
454 return ERROR_INTERNAL_ERROR;
458 CreateThread(
nullptr, 0, enumerator_server_thread_func, enumerator, 0,
nullptr);
459 if (!enumerator->thread)
461 WLog_ERR(TAG,
"CreateThread failed!");
462 (void)CloseHandle(enumerator->stopEvent);
463 enumerator->stopEvent =
nullptr;
464 return ERROR_INTERNAL_ERROR;
467 enumerator->isOpened = TRUE;
469 return CHANNEL_RC_OK;
472static UINT enumerator_server_close(CamDevEnumServerContext* context)
474 UINT error = CHANNEL_RC_OK;
475 enumerator_server* enumerator = (enumerator_server*)context;
477 WINPR_ASSERT(enumerator);
479 if (!enumerator->externalThread && enumerator->thread)
481 (void)SetEvent(enumerator->stopEvent);
483 if (WaitForSingleObject(enumerator->thread, INFINITE) == WAIT_FAILED)
485 error = GetLastError();
486 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
490 (void)CloseHandle(enumerator->thread);
491 (void)CloseHandle(enumerator->stopEvent);
492 enumerator->thread =
nullptr;
493 enumerator->stopEvent =
nullptr;
495 if (enumerator->externalThread)
497 if (enumerator->state != ENUMERATOR_INITIAL)
499 (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
500 enumerator->enumerator_channel =
nullptr;
501 enumerator->state = ENUMERATOR_INITIAL;
504 enumerator->isOpened = FALSE;
509static UINT enumerator_server_context_poll(CamDevEnumServerContext* context)
511 enumerator_server* enumerator = (enumerator_server*)context;
513 WINPR_ASSERT(enumerator);
515 if (!enumerator->externalThread)
516 return ERROR_INTERNAL_ERROR;
518 return enumerator_server_context_poll_int(context);
521static BOOL enumerator_server_context_handle(CamDevEnumServerContext* context, HANDLE* handle)
523 enumerator_server* enumerator = (enumerator_server*)context;
525 WINPR_ASSERT(enumerator);
526 WINPR_ASSERT(handle);
528 if (!enumerator->externalThread)
530 if (enumerator->state == ENUMERATOR_INITIAL)
533 *handle = enumerator_server_get_channel_handle(enumerator);
538static UINT enumerator_server_packet_send(CamDevEnumServerContext* context,
wStream* s)
540 enumerator_server* enumerator = (enumerator_server*)context;
541 UINT error = CHANNEL_RC_OK;
544 const size_t len = Stream_GetPosition(s);
545 WINPR_ASSERT(len <= UINT32_MAX);
546 if (!WTSVirtualChannelWrite(enumerator->enumerator_channel, Stream_BufferAs(s,
char),
547 (UINT32)len, &written))
549 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
550 error = ERROR_INTERNAL_ERROR;
554 if (written < Stream_GetPosition(s))
556 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
557 Stream_GetPosition(s));
561 Stream_Free(s, TRUE);
565static UINT enumerator_send_select_version_response_pdu(
570 s = Stream_New(
nullptr, CAM_HEADER_SIZE);
573 WLog_ERR(TAG,
"Stream_New failed!");
574 return ERROR_NOT_ENOUGH_MEMORY;
577 Stream_Write_UINT8(s, selectVersionResponse->Header.Version);
578 Stream_Write_UINT8(s,
579 WINPR_ASSERTING_INT_CAST(uint8_t, selectVersionResponse->Header.MessageId));
581 return enumerator_server_packet_send(context, s);
584CamDevEnumServerContext* cam_dev_enum_server_context_new(HANDLE vcm)
586 enumerator_server* enumerator = (enumerator_server*)calloc(1,
sizeof(enumerator_server));
591 enumerator->context.vcm = vcm;
592 enumerator->context.Initialize = enumerator_server_initialize;
593 enumerator->context.Open = enumerator_server_open;
594 enumerator->context.Close = enumerator_server_close;
595 enumerator->context.Poll = enumerator_server_context_poll;
596 enumerator->context.ChannelHandle = enumerator_server_context_handle;
598 enumerator->context.SelectVersionResponse = enumerator_send_select_version_response_pdu;
600 enumerator->buffer = Stream_New(
nullptr, 4096);
601 if (!enumerator->buffer)
604 return &enumerator->context;
606 WINPR_PRAGMA_DIAG_PUSH
607 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
608 cam_dev_enum_server_context_free(&enumerator->context);
609 WINPR_PRAGMA_DIAG_POP
613void cam_dev_enum_server_context_free(CamDevEnumServerContext* context)
615 enumerator_server* enumerator = (enumerator_server*)context;
619 enumerator_server_close(context);
620 Stream_Free(enumerator->buffer, TRUE);