20 #include <freerdp/config.h>
22 #include <freerdp/freerdp.h>
23 #include <freerdp/channels/log.h>
24 #include <freerdp/server/rdpecam-enumerator.h>
26 #define TAG CHANNELS_TAG("rdpecam-enumerator.server")
32 } eEnumeratorChannelState;
36 CamDevEnumServerContext context;
41 void* enumerator_channel;
49 eEnumeratorChannelState state;
54 static UINT enumerator_server_initialize(CamDevEnumServerContext* context, BOOL externalThread)
56 UINT error = CHANNEL_RC_OK;
57 enumerator_server* enumerator = (enumerator_server*)context;
59 WINPR_ASSERT(enumerator);
61 if (enumerator->isOpened)
63 WLog_WARN(TAG,
"Application error: Camera Device Enumerator channel already initialized, "
64 "calling in this state is not possible!");
65 return ERROR_INVALID_STATE;
68 enumerator->externalThread = externalThread;
73 static UINT enumerator_server_open_channel(enumerator_server* enumerator)
75 CamDevEnumServerContext* context = &enumerator->context;
76 DWORD Error = ERROR_SUCCESS;
78 DWORD BytesReturned = 0;
79 PULONG pSessionId = NULL;
83 WINPR_ASSERT(enumerator);
85 if (WTSQuerySessionInformationA(enumerator->context.vcm, WTS_CURRENT_SESSION, WTSSessionId,
86 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
88 WLog_ERR(TAG,
"WTSQuerySessionInformationA failed!");
89 return ERROR_INTERNAL_ERROR;
92 enumerator->SessionId = (DWORD)*pSessionId;
93 WTSFreeMemory(pSessionId);
94 hEvent = WTSVirtualChannelManagerGetEventHandle(enumerator->context.vcm);
96 if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED)
98 Error = GetLastError();
99 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", Error);
103 enumerator->enumerator_channel = WTSVirtualChannelOpenEx(
104 enumerator->SessionId, RDPECAM_CONTROL_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC);
105 if (!enumerator->enumerator_channel)
107 Error = GetLastError();
108 WLog_ERR(TAG,
"WTSVirtualChannelOpenEx failed with error %" PRIu32
"!", Error);
112 channelId = WTSChannelGetIdByHandle(enumerator->enumerator_channel);
114 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
117 WLog_ERR(TAG,
"context->ChannelIdAssigned failed!");
118 return ERROR_INTERNAL_ERROR;
124 static UINT enumerator_server_handle_select_version_request(CamDevEnumServerContext* context,
129 UINT error = CHANNEL_RC_OK;
131 WINPR_ASSERT(context);
132 WINPR_ASSERT(header);
134 pdu.Header = *header;
136 IFCALLRET(context->SelectVersionRequest, error, context, &pdu);
138 WLog_ERR(TAG,
"context->SelectVersionRequest failed with error %" PRIu32
"", error);
143 static UINT enumerator_server_recv_device_added_notification(CamDevEnumServerContext* context,
148 UINT error = CHANNEL_RC_OK;
149 size_t remaining_length = 0;
150 WCHAR* channel_name_start = 0;
152 WINPR_ASSERT(context);
153 WINPR_ASSERT(header);
155 pdu.Header = *header;
164 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
165 return ERROR_NO_DATA;
167 pdu.DeviceName = Stream_Pointer(s);
169 remaining_length = Stream_GetRemainingLength(s);
170 channel_name_start = Stream_Pointer(s);
174 for (; i < remaining_length; i +=
sizeof(WCHAR), ++channel_name_start)
176 if (*channel_name_start == L
'\0')
180 if (*channel_name_start != L
'\0')
182 WLog_ERR(TAG,
"enumerator_server_recv_device_added_notification: Invalid DeviceName!");
183 return ERROR_INVALID_DATA;
186 pdu.VirtualChannelName = (
char*)++channel_name_start;
189 if (i >= remaining_length || *pdu.VirtualChannelName ==
'\0')
192 "enumerator_server_recv_device_added_notification: Invalid VirtualChannelName!");
193 return ERROR_INVALID_DATA;
196 char* tmp = pdu.VirtualChannelName;
197 for (; i < remaining_length; ++i, ++tmp)
206 "enumerator_server_recv_device_added_notification: Invalid VirtualChannelName!");
207 return ERROR_INVALID_DATA;
210 IFCALLRET(context->DeviceAddedNotification, error, context, &pdu);
212 WLog_ERR(TAG,
"context->DeviceAddedNotification failed with error %" PRIu32
"", error);
217 static UINT enumerator_server_recv_device_removed_notification(CamDevEnumServerContext* context,
222 UINT error = CHANNEL_RC_OK;
223 size_t remaining_length = 0;
225 WINPR_ASSERT(context);
226 WINPR_ASSERT(header);
228 pdu.Header = *header;
230 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
231 return ERROR_NO_DATA;
233 pdu.VirtualChannelName = Stream_Pointer(s);
235 remaining_length = Stream_GetRemainingLength(s);
236 char* tmp = pdu.VirtualChannelName + 1;
238 for (
size_t i = 1; i < remaining_length; ++i, ++tmp)
247 "enumerator_server_recv_device_removed_notification: Invalid VirtualChannelName!");
248 return ERROR_INVALID_DATA;
251 IFCALLRET(context->DeviceRemovedNotification, error, context, &pdu);
253 WLog_ERR(TAG,
"context->DeviceRemovedNotification failed with error %" PRIu32
"", error);
258 static UINT enumerator_process_message(enumerator_server* enumerator)
261 UINT error = ERROR_INTERNAL_ERROR;
262 ULONG BytesReturned = 0;
266 WINPR_ASSERT(enumerator);
267 WINPR_ASSERT(enumerator->enumerator_channel);
269 s = enumerator->buffer;
272 Stream_SetPosition(s, 0);
273 rc = WTSVirtualChannelRead(enumerator->enumerator_channel, 0, NULL, 0, &BytesReturned);
277 if (BytesReturned < 1)
279 error = CHANNEL_RC_OK;
283 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
285 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
286 error = CHANNEL_RC_NO_MEMORY;
290 if (WTSVirtualChannelRead(enumerator->enumerator_channel, 0, Stream_BufferAs(s,
char),
291 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
293 WLog_ERR(TAG,
"WTSVirtualChannelRead failed!");
297 Stream_SetLength(s, BytesReturned);
298 if (!Stream_CheckAndLogRequiredLength(TAG, s, CAM_HEADER_SIZE))
299 return ERROR_NO_DATA;
301 Stream_Read_UINT8(s, header.Version);
302 Stream_Read_UINT8(s, header.MessageId);
304 switch (header.MessageId)
306 case CAM_MSG_ID_SelectVersionRequest:
308 enumerator_server_handle_select_version_request(&enumerator->context, s, &header);
310 case CAM_MSG_ID_DeviceAddedNotification:
312 enumerator_server_recv_device_added_notification(&enumerator->context, s, &header);
314 case CAM_MSG_ID_DeviceRemovedNotification:
315 error = enumerator_server_recv_device_removed_notification(&enumerator->context, s,
319 WLog_ERR(TAG,
"enumerator_process_message: unknown or invalid MessageId %" PRIu8
"",
326 WLog_ERR(TAG,
"Response failed with error %" PRIu32
"!", error);
331 static UINT enumerator_server_context_poll_int(CamDevEnumServerContext* context)
333 enumerator_server* enumerator = (enumerator_server*)context;
334 UINT error = ERROR_INTERNAL_ERROR;
336 WINPR_ASSERT(enumerator);
338 switch (enumerator->state)
340 case ENUMERATOR_INITIAL:
341 error = enumerator_server_open_channel(enumerator);
343 WLog_ERR(TAG,
"enumerator_server_open_channel failed with error %" PRIu32
"!",
346 enumerator->state = ENUMERATOR_OPENED;
348 case ENUMERATOR_OPENED:
349 error = enumerator_process_message(enumerator);
358 static HANDLE enumerator_server_get_channel_handle(enumerator_server* enumerator)
361 DWORD BytesReturned = 0;
362 HANDLE ChannelEvent = NULL;
364 WINPR_ASSERT(enumerator);
366 if (WTSVirtualChannelQuery(enumerator->enumerator_channel, WTSVirtualEventHandle, &buffer,
367 &BytesReturned) == TRUE)
369 if (BytesReturned ==
sizeof(HANDLE))
370 CopyMemory(&ChannelEvent, buffer,
sizeof(HANDLE));
372 WTSFreeMemory(buffer);
378 static DWORD WINAPI enumerator_server_thread_func(LPVOID arg)
381 HANDLE events[2] = { 0 };
382 enumerator_server* enumerator = (enumerator_server*)arg;
383 UINT error = CHANNEL_RC_OK;
386 WINPR_ASSERT(enumerator);
389 events[nCount++] = enumerator->stopEvent;
391 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
393 switch (enumerator->state)
395 case ENUMERATOR_INITIAL:
396 error = enumerator_server_context_poll_int(&enumerator->context);
397 if (error == CHANNEL_RC_OK)
399 events[1] = enumerator_server_get_channel_handle(enumerator);
403 case ENUMERATOR_OPENED:
404 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
409 case WAIT_OBJECT_0 + 1:
411 error = enumerator_server_context_poll_int(&enumerator->context);
416 error = ERROR_INTERNAL_ERROR;
425 (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
426 enumerator->enumerator_channel = NULL;
428 if (error && enumerator->context.rdpcontext)
429 setChannelError(enumerator->context.rdpcontext, error,
430 "enumerator_server_thread_func reported an error");
436 static UINT enumerator_server_open(CamDevEnumServerContext* context)
438 enumerator_server* enumerator = (enumerator_server*)context;
440 WINPR_ASSERT(enumerator);
442 if (!enumerator->externalThread && (enumerator->thread == NULL))
444 enumerator->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
445 if (!enumerator->stopEvent)
447 WLog_ERR(TAG,
"CreateEvent failed!");
448 return ERROR_INTERNAL_ERROR;
452 CreateThread(NULL, 0, enumerator_server_thread_func, enumerator, 0, NULL);
453 if (!enumerator->thread)
455 WLog_ERR(TAG,
"CreateThread failed!");
456 (void)CloseHandle(enumerator->stopEvent);
457 enumerator->stopEvent = NULL;
458 return ERROR_INTERNAL_ERROR;
461 enumerator->isOpened = TRUE;
463 return CHANNEL_RC_OK;
466 static UINT enumerator_server_close(CamDevEnumServerContext* context)
468 UINT error = CHANNEL_RC_OK;
469 enumerator_server* enumerator = (enumerator_server*)context;
471 WINPR_ASSERT(enumerator);
473 if (!enumerator->externalThread && enumerator->thread)
475 (void)SetEvent(enumerator->stopEvent);
477 if (WaitForSingleObject(enumerator->thread, INFINITE) == WAIT_FAILED)
479 error = GetLastError();
480 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
484 (void)CloseHandle(enumerator->thread);
485 (void)CloseHandle(enumerator->stopEvent);
486 enumerator->thread = NULL;
487 enumerator->stopEvent = NULL;
489 if (enumerator->externalThread)
491 if (enumerator->state != ENUMERATOR_INITIAL)
493 (void)WTSVirtualChannelClose(enumerator->enumerator_channel);
494 enumerator->enumerator_channel = NULL;
495 enumerator->state = ENUMERATOR_INITIAL;
498 enumerator->isOpened = FALSE;
503 static UINT enumerator_server_context_poll(CamDevEnumServerContext* context)
505 enumerator_server* enumerator = (enumerator_server*)context;
507 WINPR_ASSERT(enumerator);
509 if (!enumerator->externalThread)
510 return ERROR_INTERNAL_ERROR;
512 return enumerator_server_context_poll_int(context);
515 static BOOL enumerator_server_context_handle(CamDevEnumServerContext* context, HANDLE* handle)
517 enumerator_server* enumerator = (enumerator_server*)context;
519 WINPR_ASSERT(enumerator);
520 WINPR_ASSERT(handle);
522 if (!enumerator->externalThread)
524 if (enumerator->state == ENUMERATOR_INITIAL)
527 *handle = enumerator_server_get_channel_handle(enumerator);
532 static UINT enumerator_server_packet_send(CamDevEnumServerContext* context,
wStream* s)
534 enumerator_server* enumerator = (enumerator_server*)context;
535 UINT error = CHANNEL_RC_OK;
538 const size_t len = Stream_GetPosition(s);
539 WINPR_ASSERT(len <= UINT32_MAX);
540 if (!WTSVirtualChannelWrite(enumerator->enumerator_channel, Stream_BufferAs(s,
char),
541 (UINT32)len, &written))
543 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
544 error = ERROR_INTERNAL_ERROR;
548 if (written < Stream_GetPosition(s))
550 WLog_WARN(TAG,
"Unexpected bytes written: %" PRIu32
"/%" PRIuz
"", written,
551 Stream_GetPosition(s));
555 Stream_Free(s, TRUE);
559 static UINT enumerator_send_select_version_response_pdu(
564 s = Stream_New(NULL, CAM_HEADER_SIZE);
567 WLog_ERR(TAG,
"Stream_New failed!");
568 return ERROR_NOT_ENOUGH_MEMORY;
571 Stream_Write_UINT8(s, selectVersionResponse->Header.Version);
572 Stream_Write_UINT8(s, selectVersionResponse->Header.MessageId);
574 return enumerator_server_packet_send(context, s);
577 CamDevEnumServerContext* cam_dev_enum_server_context_new(HANDLE vcm)
579 enumerator_server* enumerator = (enumerator_server*)calloc(1,
sizeof(enumerator_server));
584 enumerator->context.vcm = vcm;
585 enumerator->context.Initialize = enumerator_server_initialize;
586 enumerator->context.Open = enumerator_server_open;
587 enumerator->context.Close = enumerator_server_close;
588 enumerator->context.Poll = enumerator_server_context_poll;
589 enumerator->context.ChannelHandle = enumerator_server_context_handle;
591 enumerator->context.SelectVersionResponse = enumerator_send_select_version_response_pdu;
593 enumerator->buffer = Stream_New(NULL, 4096);
594 if (!enumerator->buffer)
597 return &enumerator->context;
599 WINPR_PRAGMA_DIAG_PUSH
600 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
601 cam_dev_enum_server_context_free(&enumerator->context);
602 WINPR_PRAGMA_DIAG_POP
606 void cam_dev_enum_server_context_free(CamDevEnumServerContext* context)
608 enumerator_server* enumerator = (enumerator_server*)context;
612 enumerator_server_close(context);
613 Stream_Free(enumerator->buffer, TRUE);