Ejemplo n.º 1
0
        /// <summary>
        /// Waits for a slot event, such as token insertion or token removal, to occur
        /// </summary>
        /// <param name="waitType">Type of waiting for a slot event</param>
        /// <param name="eventOccured">Flag indicating whether event occured</param>
        /// <param name="slotId">PKCS#11 handle of slot that the event occurred in</param>
        public void WaitForSlotEvent(WaitType waitType, out bool eventOccured, out ulong slotId)
        {
            if (this._disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            _logger.Debug("Pkcs11Library({0})::WaitForSlotEvent", _libraryPath);

            NativeULong flags = (waitType == WaitType.NonBlocking) ? CKF.CKF_DONT_BLOCK : 0;

            NativeULong slotId_ = 0;
            CKR         rv      = _pkcs11Library.C_WaitForSlotEvent(flags, ref slotId_, IntPtr.Zero);

            if (waitType == WaitType.NonBlocking)
            {
                if (rv == CKR.CKR_OK)
                {
                    eventOccured = true;
                    slotId       = ConvertUtils.UInt32ToUInt64(slotId_);
                }
                else if (rv == CKR.CKR_NO_EVENT)
                {
                    eventOccured = false;
                    slotId       = ConvertUtils.UInt32ToUInt64(slotId_);
                }
                else
                {
                    throw new Pkcs11Exception("C_WaitForSlotEvent", rv);
                }
            }
            else
            {
                if (rv == CKR.CKR_OK)
                {
                    eventOccured = true;
                    slotId       = ConvertUtils.UInt32ToUInt64(slotId_);
                }
                else
                {
                    throw new Pkcs11Exception("C_WaitForSlotEvent", rv);
                }
            }
        }