FreeRDP
Loading...
Searching...
No Matches
smartcard_main.c
1
25#include <freerdp/config.h>
26
27#include <winpr/crt.h>
28#include <winpr/smartcard.h>
29#include <winpr/environment.h>
30
31#include <freerdp/freerdp.h>
32#include <freerdp/channels/rdpdr.h>
33#include <freerdp/channels/scard.h>
34#include <freerdp/utils/smartcard_call.h>
35#include <freerdp/utils/smartcard_operations.h>
36#include <freerdp/utils/rdpdr_utils.h>
37
38#include "smartcard_main.h"
39
40#define CAST_FROM_DEVICE(device) cast_device_from(device, __func__, __FILE__, __LINE__)
41
42typedef struct
43{
44 SMARTCARD_OPERATION operation;
45 IRP* irp;
46} scard_irp_queue_element;
47
48static void smartcard_context_free(void* pCtx);
49
50static UINT smartcard_complete_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled);
51
52static SMARTCARD_DEVICE* cast_device_from(DEVICE* device, const char* fkt, const char* file,
53 size_t line)
54{
55 if (!device)
56 {
57 WLog_ERR(TAG, "%s [%s:%" PRIuz "] Called smartcard channel with nullptr device", fkt, file,
58 line);
59 return nullptr;
60 }
61
62 if (device->type != RDPDR_DTYP_SMARTCARD)
63 {
64 WLog_ERR(TAG,
65 "%s [%s:%" PRIuz "] Called smartcard channel with invalid device of type %" PRIx32,
66 fkt, file, line, device->type);
67 return nullptr;
68 }
69
70 return (SMARTCARD_DEVICE*)device;
71}
72
73static DWORD smartcard_context_queue_loop(SMARTCARD_CONTEXT* pContext, wMessageQueue* queue)
74{
75 DWORD nCount = 0;
76 DWORD waitStatus = 0;
77 HANDLE hEvents[2] = WINPR_C_ARRAY_INIT;
78 wMessage message = WINPR_C_ARRAY_INIT;
79 SMARTCARD_DEVICE* smartcard = nullptr;
80 UINT error = CHANNEL_RC_OK;
81 smartcard = pContext->smartcard;
82
83 hEvents[nCount++] = MessageQueue_Event(queue);
84
85 while (1)
86 {
87 waitStatus = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
88
89 if (waitStatus == WAIT_FAILED)
90 {
91 error = GetLastError();
92 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
93 break;
94 }
95
96 waitStatus = WaitForSingleObject(MessageQueue_Event(queue), 0);
97
98 if (waitStatus == WAIT_FAILED)
99 {
100 error = GetLastError();
101 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
102 break;
103 }
104
105 if (waitStatus == WAIT_OBJECT_0)
106 {
107 scard_irp_queue_element* element = nullptr;
108
109 if (!MessageQueue_Peek(queue, &message, TRUE))
110 {
111 WLog_ERR(TAG, "MessageQueue_Peek failed!");
112 error = ERROR_INTERNAL_ERROR;
113 break;
114 }
115
116 if (message.id == WMQ_QUIT)
117 break;
118
119 element = (scard_irp_queue_element*)message.wParam;
120
121 if (element)
122 {
123 BOOL handled = FALSE;
124 WINPR_ASSERT(smartcard);
125
126 const LONG status =
127 smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
128 &element->irp->IoStatus, &element->operation);
129 if (status)
130 {
131 element->irp->Discard(element->irp);
132 smartcard_operation_free(&element->operation, TRUE);
133 WLog_ERR(TAG,
134 "smartcard_irp_device_control_call failed with error %s [%" PRId32 "]",
135 NtStatus2Tag(status), status);
136 error = (UINT)status;
137 break;
138 }
139
140 error = smartcard_complete_irp(smartcard, element->irp, &handled);
141 if (!handled)
142 element->irp->Discard(element->irp);
143 smartcard_operation_free(&element->operation, TRUE);
144
145 if (error)
146 {
147 WLog_ERR(TAG, "smartcard_complete_irp failed with %s [%" PRIu32 "]",
148 WTSErrorToString(error), error);
149 break;
150 }
151 }
152 }
153 }
154
155 if (error && smartcard->rdpcontext)
156 setChannelError(smartcard->rdpcontext, error, "smartcard_context_thread reported an error");
157
158 return error;
159}
160
161static DWORD WINAPI smartcard_context_thread(LPVOID arg)
162{
163 SMARTCARD_CONTEXT* pContext = (SMARTCARD_CONTEXT*)arg;
164 const DWORD error = smartcard_context_queue_loop(pContext, pContext->IrpQueue);
165 ExitThread(error);
166 return error;
167}
168
169static DWORD WINAPI smartcard_context_tx_thread(LPVOID arg)
170{
171 SMARTCARD_CONTEXT* pContext = (SMARTCARD_CONTEXT*)arg;
172 const DWORD error = smartcard_context_queue_loop(pContext, pContext->TxQueue);
173 ExitThread(error);
174 return error;
175}
176
177static void smartcard_operation_queue_free(void* obj)
178{
179 wMessage* msg = obj;
180 if (!msg)
181 return;
182 if (msg->id != 0)
183 return;
184
185 scard_irp_queue_element* element = (scard_irp_queue_element*)msg->wParam;
186 if (!element)
187 return;
188 WINPR_ASSERT(element->irp);
189 WINPR_ASSERT(element->irp->Discard);
190 element->irp->Discard(element->irp);
191 smartcard_operation_free(&element->operation, TRUE);
192}
193
194static void* smartcard_context_new(void* smartcard, SCARDCONTEXT hContext)
195{
196 SMARTCARD_CONTEXT* pContext = (SMARTCARD_CONTEXT*)calloc(1, sizeof(SMARTCARD_CONTEXT));
197
198 WLog_VRB(TAG, "smartcard context create %p", (const void*)pContext);
199 if (!pContext)
200 {
201 WLog_ERR(TAG, "calloc failed!");
202 return pContext;
203 }
204
205 pContext->smartcard = smartcard;
206 pContext->hContext = hContext;
207 pContext->IrpQueue = MessageQueue_New(nullptr);
208
209 if (!pContext->IrpQueue)
210 {
211 WLog_ERR(TAG, "MessageQueue_New failed!");
212 goto fail;
213 }
214
215 {
216 wObject* obj = MessageQueue_Object(pContext->IrpQueue);
217 WINPR_ASSERT(obj);
218 obj->fnObjectFree = smartcard_operation_queue_free;
219 }
220
221 pContext->thread = CreateThread(nullptr, 0, smartcard_context_thread, pContext, 0, nullptr);
222
223 if (!pContext->thread)
224 {
225 WLog_ERR(TAG, "CreateThread failed!");
226 goto fail;
227 }
228
229 pContext->TxQueue = MessageQueue_New(nullptr);
230
231 if (!pContext->TxQueue)
232 {
233 WLog_ERR(TAG, "MessageQueue_New failed!");
234 goto fail;
235 }
236
237 {
238 wObject* obj = MessageQueue_Object(pContext->TxQueue);
239 WINPR_ASSERT(obj);
240 obj->fnObjectFree = smartcard_operation_queue_free;
241 }
242
243 pContext->txThread =
244 CreateThread(nullptr, 0, smartcard_context_tx_thread, pContext, 0, nullptr);
245
246 if (!pContext->txThread)
247 {
248 WLog_ERR(TAG, "CreateThread failed!");
249 goto fail;
250 }
251
252 return pContext;
253fail:
254 smartcard_context_free(pContext);
255 return nullptr;
256}
257
258void smartcard_context_free(void* pCtx)
259{
260 SMARTCARD_CONTEXT* pContext = pCtx;
261
262 WLog_VRB(TAG, "smartcard context destroy %p", pCtx);
263 if (!pContext)
264 return;
265
266 /* cancel blocking calls like SCardGetStatusChange */
267 WINPR_ASSERT(pContext->smartcard);
268 smartcard_call_cancel_context(pContext->smartcard->callctx, pContext->hContext);
269
270 if (pContext->IrpQueue)
271 {
272 if (MessageQueue_PostQuit(pContext->IrpQueue, 0))
273 {
274 if (WaitForSingleObject(pContext->thread, INFINITE) == WAIT_FAILED)
275 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
276
277 (void)CloseHandle(pContext->thread);
278 }
279 MessageQueue_Free(pContext->IrpQueue);
280 }
281 if (pContext->TxQueue)
282 {
283 if (pContext->txThread && MessageQueue_PostQuit(pContext->TxQueue, 0))
284 {
285 if (WaitForSingleObject(pContext->txThread, INFINITE) == WAIT_FAILED)
286 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
287
288 (void)CloseHandle(pContext->txThread);
289 }
290 MessageQueue_Free(pContext->TxQueue);
291 }
292 smartcard_call_release_context(pContext->smartcard->callctx, pContext->hContext);
293 free(pContext);
294}
295
301static void smartcard_cancel_all_context(SMARTCARD_DEVICE* smartcard)
302{
303 WINPR_ASSERT(smartcard);
304
305 EnterCriticalSection(&smartcard->lock);
306 smartcard_call_cancel_all_context(smartcard->callctx);
307 LeaveCriticalSection(&smartcard->lock);
308}
309
310static UINT smartcard_free_(SMARTCARD_DEVICE* smartcard)
311{
312 if (!smartcard)
313 return CHANNEL_RC_OK;
314
315 if (smartcard->IrpQueue)
316 {
317 MessageQueue_Free(smartcard->IrpQueue);
318 (void)CloseHandle(smartcard->thread);
319 }
320
321 Stream_Free(smartcard->device.data, TRUE);
322 ListDictionary_Free(smartcard->rgOutstandingMessages);
323
324 smartcard_call_context_free(smartcard->callctx);
325 DeleteCriticalSection(&smartcard->lock);
326
327 free(smartcard);
328 return CHANNEL_RC_OK;
329}
335static UINT smartcard_free(DEVICE* device)
336{
337 SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
338
339 WLog_VRB(TAG, "smartcard device destroy: %p", (const void*)smartcard);
340 if (!smartcard)
341 return ERROR_INVALID_PARAMETER;
342
347 smartcard_cancel_all_context(smartcard);
348
349 /* Stopping all threads and cancelling all IRPs */
350
351 if (smartcard->IrpQueue)
352 {
353 if (MessageQueue_PostQuit(smartcard->IrpQueue, 0) &&
354 (WaitForSingleObject(smartcard->thread, INFINITE) == WAIT_FAILED))
355 {
356 DWORD error = GetLastError();
357 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
358 return error;
359 }
360 }
361
362 return smartcard_free_(smartcard);
363}
364
375static UINT smartcard_init(DEVICE* device)
376{
377 SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
378
379 WLog_VRB(TAG, "smartcard device init %p", (const void*)smartcard);
380 if (!smartcard)
381 return ERROR_INVALID_PARAMETER;
382
383 smartcard_cancel_all_context(smartcard);
384 return CHANNEL_RC_OK;
385}
386
392UINT smartcard_complete_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
393{
394 WINPR_ASSERT(smartcard);
395 WINPR_ASSERT(irp);
396 WINPR_ASSERT(handled);
397
398 uintptr_t key = (uintptr_t)irp->CompletionId + 1;
399 ListDictionary_Remove(smartcard->rgOutstandingMessages, (void*)key);
400
401 WINPR_ASSERT(irp->Complete);
402 *handled = TRUE;
403 return irp->Complete(irp);
404}
405
416static UINT smartcard_process_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
417{
418 LONG status = 0;
419 BOOL asyncIrp = FALSE;
420 SMARTCARD_CONTEXT* pContext = nullptr;
421
422 WINPR_ASSERT(smartcard);
423 WINPR_ASSERT(handled);
424 WINPR_ASSERT(irp);
425 WINPR_ASSERT(irp->Complete);
426
427 uintptr_t key = (uintptr_t)irp->CompletionId + 1;
428
429 if (!ListDictionary_Add(smartcard->rgOutstandingMessages, (void*)key, irp))
430 {
431 WLog_ERR(TAG, "ListDictionary_Add failed!");
432 return ERROR_INTERNAL_ERROR;
433 }
434
435 if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL)
436 {
437 scard_irp_queue_element* element = calloc(1, sizeof(scard_irp_queue_element));
438 if (!element)
439 return ERROR_OUTOFMEMORY;
440
441 element->irp = irp;
442 element->operation.completionID = irp->CompletionId;
443
444 status = smartcard_irp_device_control_decode_request(irp->input, irp->CompletionId,
445 irp->FileId, &element->operation);
446
447 if (status != SCARD_S_SUCCESS)
448 {
449 UINT error = 0;
450
451 smartcard_operation_free(&element->operation, TRUE);
452 irp->IoStatus = STATUS_UNSUCCESSFUL;
453
454 if ((error = smartcard_complete_irp(smartcard, irp, handled)))
455 {
456 WLog_ERR(TAG, "Queue_Enqueue failed!");
457 return error;
458 }
459
460 return CHANNEL_RC_OK;
461 }
462
463 asyncIrp = TRUE;
464
465 switch (element->operation.ioControlCode)
466 {
467 case SCARD_IOCTL_ESTABLISHCONTEXT:
468 case SCARD_IOCTL_RELEASECONTEXT:
469 case SCARD_IOCTL_ISVALIDCONTEXT:
470 case SCARD_IOCTL_CANCEL:
471 case SCARD_IOCTL_ACCESSSTARTEDEVENT:
472 case SCARD_IOCTL_RELEASETARTEDEVENT:
473 asyncIrp = FALSE;
474 break;
475
476 case SCARD_IOCTL_LISTREADERGROUPSA:
477 case SCARD_IOCTL_LISTREADERGROUPSW:
478 case SCARD_IOCTL_LISTREADERSA:
479 case SCARD_IOCTL_LISTREADERSW:
480 case SCARD_IOCTL_INTRODUCEREADERGROUPA:
481 case SCARD_IOCTL_INTRODUCEREADERGROUPW:
482 case SCARD_IOCTL_FORGETREADERGROUPA:
483 case SCARD_IOCTL_FORGETREADERGROUPW:
484 case SCARD_IOCTL_INTRODUCEREADERA:
485 case SCARD_IOCTL_INTRODUCEREADERW:
486 case SCARD_IOCTL_FORGETREADERA:
487 case SCARD_IOCTL_FORGETREADERW:
488 case SCARD_IOCTL_ADDREADERTOGROUPA:
489 case SCARD_IOCTL_ADDREADERTOGROUPW:
490 case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
491 case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
492 case SCARD_IOCTL_LOCATECARDSA:
493 case SCARD_IOCTL_LOCATECARDSW:
494 case SCARD_IOCTL_LOCATECARDSBYATRA:
495 case SCARD_IOCTL_LOCATECARDSBYATRW:
496 case SCARD_IOCTL_READCACHEA:
497 case SCARD_IOCTL_READCACHEW:
498 case SCARD_IOCTL_WRITECACHEA:
499 case SCARD_IOCTL_WRITECACHEW:
500 case SCARD_IOCTL_GETREADERICON:
501 case SCARD_IOCTL_GETDEVICETYPEID:
502 case SCARD_IOCTL_GETSTATUSCHANGEA:
503 case SCARD_IOCTL_GETSTATUSCHANGEW:
504 case SCARD_IOCTL_CONNECTA:
505 case SCARD_IOCTL_CONNECTW:
506 case SCARD_IOCTL_RECONNECT:
507 case SCARD_IOCTL_DISCONNECT:
508 case SCARD_IOCTL_BEGINTRANSACTION:
509 case SCARD_IOCTL_ENDTRANSACTION:
510 case SCARD_IOCTL_STATE:
511 case SCARD_IOCTL_STATUSA:
512 case SCARD_IOCTL_STATUSW:
513 case SCARD_IOCTL_TRANSMIT:
514 case SCARD_IOCTL_CONTROL:
515 case SCARD_IOCTL_GETATTRIB:
516 case SCARD_IOCTL_SETATTRIB:
517 case SCARD_IOCTL_GETTRANSMITCOUNT:
518 asyncIrp = TRUE;
519 break;
520 default:
521 break;
522 }
523
524 pContext = smartcard_call_get_context(smartcard->callctx, element->operation.hContext);
525
526 if (!pContext)
527 asyncIrp = FALSE;
528
529 if (!asyncIrp)
530 {
531 UINT error = 0;
532
533 status =
534 smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
535 &element->irp->IoStatus, &element->operation);
536 smartcard_operation_free(&element->operation, TRUE);
537
538 if (status)
539 {
540 WLog_ERR(TAG, "smartcard_irp_device_control_call failed with error %" PRId32 "!",
541 status);
542 return (UINT32)status;
543 }
544
545 if ((error = smartcard_complete_irp(smartcard, irp, handled)))
546 {
547 WLog_ERR(TAG, "Queue_Enqueue failed!");
548 return error;
549 }
550 }
551 else
552 {
553 if (pContext)
554 {
555 wMessageQueue* queue = pContext->IrpQueue;
556
557 switch (element->operation.ioControlCode)
558 {
559 /* may wait for the transaction of another handle */
560 case SCARD_IOCTL_BEGINTRANSACTION:
561 case SCARD_IOCTL_CONNECTA:
562 case SCARD_IOCTL_CONNECTW:
563 case SCARD_IOCTL_RECONNECT:
564 queue = pContext->TxQueue;
565 break;
566 default:
567 break;
568 }
569
570 if (!MessageQueue_Post(queue, nullptr, 0, (void*)element, nullptr))
571 {
572 smartcard_operation_free(&element->operation, TRUE);
573 WLog_ERR(TAG, "MessageQueue_Post failed!");
574 return ERROR_INTERNAL_ERROR;
575 }
576 *handled = TRUE;
577 }
578 }
579 }
580 else
581 {
582 UINT ustatus = 0;
583 WLog_ERR(TAG, "Unexpected SmartCard IRP: MajorFunction %s, MinorFunction: 0x%08" PRIX32 "",
584 rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction);
585 irp->IoStatus = STATUS_NOT_SUPPORTED;
586
587 if ((ustatus = smartcard_complete_irp(smartcard, irp, handled)))
588 {
589 WLog_ERR(TAG, "Queue_Enqueue failed!");
590 return ustatus;
591 }
592 }
593
594 return CHANNEL_RC_OK;
595}
596
597static DWORD WINAPI smartcard_thread_func(LPVOID arg)
598{
599 IRP* irp = nullptr;
600 DWORD nCount = 0;
601 DWORD status = 0;
602 HANDLE hEvents[1] = WINPR_C_ARRAY_INIT;
603 wMessage message = WINPR_C_ARRAY_INIT;
604 UINT error = CHANNEL_RC_OK;
605 SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(arg);
606
607 if (!smartcard)
608 return ERROR_INVALID_PARAMETER;
609
610 hEvents[nCount++] = MessageQueue_Event(smartcard->IrpQueue);
611
612 while (1)
613 {
614 status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
615
616 if (status == WAIT_FAILED)
617 {
618 error = GetLastError();
619 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
620 break;
621 }
622
623 if (status == WAIT_OBJECT_0)
624 {
625 if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE))
626 {
627 WLog_ERR(TAG, "MessageQueue_Peek failed!");
628 error = ERROR_INTERNAL_ERROR;
629 break;
630 }
631
632 if (message.id == WMQ_QUIT)
633 break;
634
635 irp = (IRP*)message.wParam;
636
637 if (irp)
638 {
639 BOOL handled = FALSE;
640
641 /* the contexts may be cleared by DEVICE::Init on the channel thread */
642 EnterCriticalSection(&smartcard->lock);
643 error = smartcard_process_irp(smartcard, irp, &handled);
644 LeaveCriticalSection(&smartcard->lock);
645
646 if (error)
647 {
648 WLog_ERR(TAG, "smartcard_process_irp failed with error %" PRIu32 "!", error);
649 goto out;
650 }
651 if (!handled)
652 {
653 WINPR_ASSERT(irp->Discard);
654 irp->Discard(irp);
655 }
656 }
657 }
658 }
659
660out:
661
662 if (error && smartcard->rdpcontext)
663 setChannelError(smartcard->rdpcontext, error, "smartcard_thread_func reported an error");
664
665 ExitThread(error);
666 return error;
667}
668
674static UINT smartcard_irp_request(DEVICE* device, IRP* irp)
675{
676 SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
677
678 if (!smartcard)
679 {
680 irp->Discard(irp);
681 return ERROR_INVALID_PARAMETER;
682 }
683
684 if (!MessageQueue_Post(smartcard->IrpQueue, nullptr, 0, (void*)irp, nullptr))
685 {
686 WLog_ERR(TAG, "MessageQueue_Post failed!");
687 irp->Discard(irp);
688 return ERROR_INTERNAL_ERROR;
689 }
690
691 return CHANNEL_RC_OK;
692}
693
694static void smartcard_free_irp(void* obj)
695{
696 wMessage* msg = obj;
697 if (!msg)
698 return;
699 if (msg->id != 0)
700 return;
701
702 IRP* irp = (IRP*)msg->wParam;
703 if (!irp)
704 return;
705 WINPR_ASSERT(irp->Discard);
706 irp->Discard(irp);
707}
708
709/* smartcard is always built-in */
710#define DeviceServiceEntry smartcard_DeviceServiceEntry
711
717FREERDP_ENTRY_POINT(UINT VCAPITYPE DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))
718{
719 BOOL registered = FALSE;
720 UINT error = CHANNEL_RC_NO_MEMORY;
721
722 SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*)calloc(1, sizeof(SMARTCARD_DEVICE));
723 WLog_VRB(TAG, "smartcard device create: %p", (const void*)smartcard);
724 if (!smartcard)
725 {
726 WLog_ERR(TAG, "calloc failed!");
727 return CHANNEL_RC_NO_MEMORY;
728 }
729
730 smartcard->device.type = RDPDR_DTYP_SMARTCARD;
731 smartcard->device.name = "SCARD";
732 smartcard->device.IRPRequest = smartcard_irp_request;
733 smartcard->device.Init = smartcard_init;
734 smartcard->device.Free = smartcard_free;
735 smartcard->rdpcontext = pEntryPoints->rdpcontext;
736
737 if (!InitializeCriticalSectionEx(&smartcard->lock, 0, 0))
738 {
739 WLog_ERR(TAG, "InitializeCriticalSectionEx failed!");
740 goto fail;
741 }
742
743 smartcard->IrpQueue = MessageQueue_New(nullptr);
744
745 if (!smartcard->IrpQueue)
746 {
747 WLog_ERR(TAG, "MessageQueue_New failed!");
748 goto fail;
749 }
750
751 wObject* obj = MessageQueue_Object(smartcard->IrpQueue);
752 WINPR_ASSERT(obj);
753 obj->fnObjectFree = smartcard_free_irp;
754
755 smartcard->rgOutstandingMessages = ListDictionary_New(TRUE);
756
757 if (!smartcard->rgOutstandingMessages)
758 {
759 WLog_ERR(TAG, "ListDictionary_New failed!");
760 goto fail;
761 }
762
763 smartcard->callctx = smartcard_call_context_new_with_context(smartcard->rdpcontext);
764 if (!smartcard->callctx)
765 goto fail;
766
767 if (!smarcard_call_set_callbacks(smartcard->callctx, smartcard, smartcard_context_new,
768 smartcard_context_free))
769 goto fail;
770
771 error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &smartcard->device);
772 if (error)
773 {
774 WLog_ERR(TAG, "RegisterDevice failed!");
775 goto fail;
776 }
777
778 registered = TRUE;
779 smartcard->thread =
780 CreateThread(nullptr, 0, smartcard_thread_func, smartcard, CREATE_SUSPENDED, nullptr);
781
782 if (!smartcard->thread)
783 {
784 WLog_ERR(TAG, "CreateThread failed!");
785 error = ERROR_INTERNAL_ERROR;
786 goto fail;
787 }
788
789 ResumeThread(smartcard->thread);
790
791 if (pEntryPoints->device->Name)
792 {
793 if (!smartcard_call_context_add(smartcard->callctx, pEntryPoints->device->Name))
794 goto fail;
795 }
796
797 return CHANNEL_RC_OK;
798fail:
799 if (!registered)
800 smartcard_free_(smartcard);
801 return error;
802}
This struct contains function pointer to initialize/free objects.
Definition collections.h:52
OBJECT_FREE_FN fnObjectFree
Definition collections.h:59