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.h
26 *  SmartCardServices
27 */
28
29#ifndef _H_PCSCD_READER
30#define _H_PCSCD_READER
31
32#include <MacTypes.h>
33#include "wintypes.h"
34#include "pcsclite.h"
35#include "readerfactory.h"
36#include <security_utilities/refcount.h>
37#include <security_cdsa_utilities/handleobject.h>
38#include <map>
39
40#if 0
41	struct ReaderContext
42	{
43		char lpcReader[MAX_READERNAME];	/* Reader Name */
44		char lpcLibrary[MAX_LIBNAME];	/* Library Path */
45		PCSCLITE_THREAD_T pthThread;	/* Event polling thread */
46		PCSCLITE_MUTEX_T mMutex;	/* Mutex for this connection */
47		RDR_CAPABILITIES psCapabilites;	/* Structure of reader
48						   capabilities */
49		PROT_OPTIONS psProtOptions;	/* Structure of protocol options */
50		RDR_CLIHANDLES psHandles[PCSCLITE_MAX_CONTEXTS];
51                                         /* Structure of connected handles */
52		FCT_MAP psFunctions;	/* Structure of function pointers */
53		UCHAR ucAtr[MAX_ATR_SIZE];	/* Atr for inserted card */
54		DWORD dwAtrLen;			/* Size of the ATR */
55		LPVOID vHandle;			/* Dlopen handle */
56		DWORD dwVersion;		/* IFD Handler version number */
57		DWORD dwPort;			/* Port ID */
58		DWORD dwProtocol;		/* Currently used protocol */
59		DWORD dwSlot;			/* Current Reader Slot */
60		DWORD dwBlockStatus;	/* Current blocking status */
61		DWORD dwStatus;			/* Current Status Mask */
62		DWORD dwLockId;			/* Lock Id */
63		DWORD dwIdentity;		/* Shared ID High Nibble */
64		DWORD dwContexts;		/* Number of open contexts */
65		DWORD dwPublicID;		/* Public id of public state struct */
66		PDWORD dwFeeds;			/* Number of shared client to lib */
67	};
68#endif
69
70#if defined(__cplusplus)
71
72namespace PCSCD {
73
74//
75// The server object itself. This is the "go to" object for anyone who wants
76// to access the server's global state. It runs the show.
77// There is only one Server, and its name is Server::active().
78//
79
80//
81// A PODWrapper for the PCSC READER_CONTEXT structure
82//
83class XReaderContext : public PodWrapper<XReaderContext, READER_CONTEXT>
84{
85public:
86	void set(const char *name, unsigned long known = SCARD_STATE_UNAWARE);
87
88	const char *name() const	{ return lpcReader; }
89//	void name(const char *s)	{ szReader = s; }
90
91//	unsigned long lastKnown() const { return dwStatus; }
92	void lastKnown(unsigned long s);
93
94	unsigned long state() const { return 0; }	//fix
95	bool state(unsigned long it) const { return state() & it; }
96	bool changed() const		{ return state(SCARD_STATE_CHANGED); }
97
98//	template <class T>
99//	T * &userData() { return reinterpret_cast<T * &>(pvUserData); }
100
101	// DataOid access to the ATR data
102//	const void *data() const { return ucAtr; }
103//	size_t length() const { return dwAtrLen; }
104	void setATR(const void *atr, size_t size);
105
106	IFDUMP(void dump());
107};
108
109
110class Reader : public HandleObject, public RefCount
111{
112public:
113	Reader(const char *bootstrapName);
114	~Reader();
115private:
116	// mach bootstrap registration name
117	std::string mBootstrapName;
118	mutable Mutex mLock;
119};
120
121class Readers
122{
123public:
124	Readers();
125	~Readers();
126
127	typedef std::map<uint32_t, RefPointer<PCSCD::Reader> > ReaderMap;
128	ReaderMap mReaders;
129
130	bool find(uint32_t id, XReaderContext &rc) const;
131	bool find(const char *name, XReaderContext &rc) const;
132	bool find(uint32_t port, const char *name, XReaderContext &rc) const;
133
134	mutable Mutex mReaderMapLock;
135
136	void insert(pair<uint32_t, RefPointer<PCSCD::Reader> > readerpair) { StLock<Mutex> _(mReaderMapLock); mReaders.insert(readerpair); }
137	void remove(ReaderMap::iterator it) { StLock<Mutex> _(mReaderMapLock); mReaders.erase(it); }
138
139private:
140	mutable Mutex mLock;
141};
142
143} // end namespace PCSCD
144
145#endif /* __cplusplus__ */
146
147#endif //_H_PCSCD_READER
148
149