1/*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 *  reader.cpp
26 *  SmartCardServices
27*/
28
29#include "reader.h"
30#include "eventhandler.h"
31#include "pcsclite.h"
32#include <security_utilities/debugging.h>
33
34static PCSCD::Readers *mReaders;
35
36namespace PCSCD {
37
38
39Readers::Readers()
40{
41}
42
43Readers::~Readers()
44{
45}
46
47bool Readers::find(const char *name, XReaderContext &rc) const
48{
49	return false;
50}
51
52bool Readers::find(uint32_t port, const char *name, XReaderContext &rc) const
53{
54	return false;
55}
56
57bool Readers::find(uint32_t id, XReaderContext &rc) const
58{
59	return false;
60}
61
62
63} // end namespace PCSCD
64
65#pragma mark ---------- C Interface ----------
66
67LONG XRFAllocateReaderSpace(DWORD dwAllocNum)
68{
69	try
70	{
71		mReaders = new PCSCD::Readers();
72	}
73	catch (...)
74	{
75		secdebug("pcscd", "failed to allocate Readers");
76		return -1;
77	}
78	return EHInitializeEventStructures();
79}
80
81LONG XRFReaderInfo(LPSTR lpcReader, PREADER_CONTEXT *sReader)
82{
83	// Find a reader given a name
84	PCSCD::XReaderContext rc;	//>>>> use iterator instead
85	if (!sReader)
86		return SCARD_E_INVALID_PARAMETER;
87
88	if (!mReaders->find(lpcReader, rc))
89		return SCARD_E_UNKNOWN_READER;
90
91	*sReader = &rc;	//>>>> WRONG - temporary var
92	return SCARD_S_SUCCESS;
93}
94
95LONG XRFReaderInfoNamePort(DWORD dwPort, LPSTR lpcReader, PREADER_CONTEXT *sReader)
96{
97	// Find a reader given a name
98	PCSCD::XReaderContext rc;
99	if (!sReader)
100		return SCARD_E_INVALID_PARAMETER;
101
102	if (!mReaders->find(dwPort, lpcReader, rc))
103		return SCARD_E_UNKNOWN_READER;
104
105	*sReader = &rc;	//>>>> WRONG - temporary var
106	return SCARD_S_SUCCESS;
107}
108
109LONG XRFReaderInfoById(DWORD dwIdentity, PREADER_CONTEXT * sReader)
110{
111	// Find a reader given a handle
112	PCSCD::XReaderContext rc;
113	if (!sReader)
114		return SCARD_E_INVALID_PARAMETER;
115
116	if (!mReaders->find(dwIdentity, rc))
117		return SCARD_E_INVALID_VALUE;
118
119	*sReader = &rc;	//>>>> WRONG - temporary var
120	return SCARD_S_SUCCESS;
121}
122
123LONG XRFCheckSharing(DWORD hCard)
124{
125	PCSCD::XReaderContext rc;
126	if (!mReaders->find(hCard, rc))
127		return SCARD_E_INVALID_VALUE;
128
129	return (rc.dwLockId == 0 || rc.dwLockId == hCard)?SCARD_S_SUCCESS:SCARD_E_SHARING_VIOLATION;
130}
131
132LONG XRFLockSharing(DWORD hCard)
133{
134	PCSCD::XReaderContext rc;
135	if (!mReaders->find(hCard, rc))
136		return SCARD_E_INVALID_VALUE;
137
138	if (rc.dwLockId != 0 && rc.dwLockId != hCard)
139	{
140		secdebug("pcscd", "XRFLockSharing: Lock ID invalid: %d", rc.dwLockId);
141		return SCARD_E_SHARING_VIOLATION;
142	}
143
144	EHSetSharingEvent(&rc, 1);
145	rc.dwLockId = hCard;
146	return SCARD_S_SUCCESS;
147}
148
149LONG XRFUnlockSharing(DWORD hCard)
150{
151	PCSCD::XReaderContext rc;
152	if (!mReaders->find(hCard, rc))
153		return SCARD_E_INVALID_VALUE;
154
155	if (rc.dwLockId != 0 && rc.dwLockId != hCard)
156	{
157		secdebug("pcscd", "XRFUnlockSharing: Lock ID invalid: %d", rc.dwLockId);
158		return SCARD_E_SHARING_VIOLATION;
159	}
160
161	EHSetSharingEvent(&rc, 0);
162	rc.dwLockId = 0;
163	return SCARD_S_SUCCESS;
164}
165
166