void onGetScanApiVersion(long result, ISktScanObject scanObj)
 {
     if (SktScanErrors.SKTSUCCESS(result))
     {
         StringBuilder version = new StringBuilder("ScanAPI Version: ");
         version.AppendFormat("{0:x}.{1:x}.{2:x}.{3:x} {4:x}/{5:x2}/{6:x2} {7:x}:{8:x}",
                              scanObj.Property.Version.Major,
                              scanObj.Property.Version.Middle,
                              scanObj.Property.Version.Minor,
                              scanObj.Property.Version.Build,
                              scanObj.Property.Version.Month,
                              scanObj.Property.Version.Day,
                              scanObj.Property.Version.Year,
                              scanObj.Property.Version.Hour,
                              scanObj.Property.Version.Minute);
         info.Text = version.ToString();
     }
 }
 private void TriggerCompleteCallback(long result, ISktScanObject scanObj)
 {
     //MessageBox.Show("In trigger complete callback");
 }
 /// <summary>
 /// This method calls OnScanApiTerminated if the set ScanAPI abort
 /// failed. If the set ScanAPI abort, the onScanApiTerminated is called
 /// upon reception of the Terminate event from ScanAPI
 /// </summary>
 /// <param name="result"></param>
 /// <param name="scanObj"></param>
 public void onSetScanApiAbort(long result,ISktScanObject scanObj)
 {
     if (!SktScanErrors.SKTSUCCESS(result))
     {
         if (_notification!=null)
             _notification.OnScanApiTerminated();
     }
 }
 public void DoCallback(long result,ISktScanObject scanObj)
 {
     _status=statusCompleted;
     if(_callback!=null)
         _callback(result,scanObj);
 }
 public CommandContext(bool getOperation,ISktScanObject scanObj,ISktScanDevice scanDevice,DeviceInfo deviceInfo,ICommandContextCallback callback)
 {
     this._getOperation=getOperation;
     scanObj.Property.Context=this;
     this._scanObj=scanObj;
     this._callback=callback;
     this._status=statusReady;
     this._scanDevice=scanDevice;
     this._retries=0;
     this._deviceInfo=deviceInfo;
     this.SymbologyId=0;
 }
 /**
  * HandleScanObject
  * This method is called each time this application receives a
  * ScanObject from ScanAPI.
  * It returns true is the caller can safely close ScanAPI and
  * terminate its ScanAPI consumer.
  */
 private bool HandleScanObject(ISktScanObject scanObject)
 {
     bool closeScanApi = false;
     switch (scanObject.Msg.ID)
     {
         case ISktScanMsg.kSktScanMsgIdDeviceArrival:
             HandleDeviceArrival(scanObject);
             break;
         case ISktScanMsg.kSktScanMsgIdDeviceRemoval:
             HandleDeviceRemoval(scanObject);
             break;
         case ISktScanMsg.kSktScanMsgGetComplete:
         case ISktScanMsg.kSktScanMsgSetComplete:
             DoGetOrSetComplete(scanObject);
             break;
         case ISktScanMsg.kSktScanMsgIdTerminate:
             Debug.MSG(Debug.kLevelTrace, "Receive a Terminate event, ask to close ScanAPI");
             closeScanApi = true;
             break;
         case ISktScanMsg.kSktScanMsgEvent:
             HandleEvent(scanObject);
             break;
     }
     return closeScanApi;
 }
        /**
         * HandleEvent
         *
         * This method handles asynchronous events coming from ScanAPI
         * including decoded data
         */
        private void HandleEvent(ISktScanObject scanObject)
        {
            ISktScanEvent Event=scanObject.Msg.Event;
            ISktScanDevice iDevice=scanObject.Msg.DeviceInterface;
            switch(Event.EventID)
            {
            case ISktScanEvent.id.kSktScanEventError:
                if(_notification!=null){
                    _notification.OnError(scanObject.Msg.Result,
                        Event.DataType == ISktScanEvent.types.kSktScanEventDataTypeString ? Event.DataString.Value : null);
                }
                break;
            case ISktScanEvent.id.kSktScanEventDecodedData:
                ISktScanDecodedData decodedData = Event.DataDecodedData;
                DeviceInfo deviceInfo=GetDeviceInfo(iDevice);
                if(_notification!=null){
                    _notification.OnDecodedData(deviceInfo,decodedData);
                }

                break;
            case ISktScanEvent.id.kSktScanEventPower:
                break;
            case ISktScanEvent.id.kSktScanEventButtons:
                break;
            }
        }
        /**
         * HandleDeviceRemoval
         * This method is called each time a device is disconnected from the host.
         * Usually this will be a good opportunity to close the device
         */
        private void HandleDeviceRemoval(ISktScanObject scanObject)
        {
            ISktScanDevice iDevice=scanObject.Msg.DeviceInterface;
            DeviceInfo deviceFound=null;
            lock(_devicesList){
                foreach (DeviceInfo device in _devicesList)
                {
                    if(device.SktScanDevice==iDevice)
                    {
                        deviceFound=device;
                        break;
                    }
                }

                // let's notify whatever UI we might have
                if(deviceFound!=null)
                {
                    RemoveCommands(deviceFound);
                    _devicesList.Remove(deviceFound);
                    if(_devicesList.Count==0){
                        if(_noDeviceConnected.Name.Length>0)
                            _devicesList.Add(_noDeviceConnected);
                    }
                    if(_notification!=null)
                        _notification.OnDeviceRemoval(deviceFound);
                }
            }
            iDevice.Close();
        }
 /**
  * HandleDeviceArrival
  * This method is called each time a device connect to the host.
  *
  * We create a device info object to hold all the necessary
  * information about this device, including its interface
  * which is used as handle
  */
 private void HandleDeviceArrival(ISktScanObject scanObject)
 {
     String friendlyName=scanObject.Msg.DeviceName;
     String deviceGuid=scanObject.Msg.DeviceGuid;
     long type=scanObject.Msg.DeviceType;
     ISktScanDevice device=SktClassFactory.createDeviceInstance(_scanApi);
     DeviceInfo newDevice=null;
     long result=device.Open(deviceGuid);
     if(SktScanErrors.SKTSUCCESS(result))
     {
         // add the new device into the list
         newDevice=new DeviceInfo(friendlyName,device,type);
         lock(_devicesList){
             _devicesList.Add(newDevice);
             _devicesList.Remove(_noDeviceConnected);
         }
     }
     if(_notification!=null)
         _notification.OnDeviceArrival(result,newDevice);
 }
        /**
         * DoGetOrSetComplete
         * "Get Complete" events arrive asynchonously via code in the timer handler of the Scanner List dialog. Even
         * though they may arrive asynchonously, they only arrive as the result of a successful corresponding "Get"
         * request.
         *
         * This function examines the get complete event given in the pScanObj arg, and dispatches it to the correct
         * handler depending on the Property ID it contains.
         *
         * Each property handler must return ESKT_NOERROR if it has successfully performed its processing.
         */
        private long DoGetOrSetComplete(ISktScanObject scanObj)
        {
            long result=SktScanErrors.ESKT_NOERROR;
            bool remove=true;
            bool doCallback=true;
            if (scanObj != null)
            {
                result=scanObj.Msg.Result;
                CommandContext command=(CommandContext)scanObj.Property.Context;
                Debug.MSG(Debug.kLevelTrace,"Complete event received for Context:"+command+"\n");
                if(command!=null){
                    if(!SktScanErrors.SKTSUCCESS(result)&&
                        (scanObj.Property.ID!=ISktScanProperty.propId.kSktScanPropIdSetPowerOffDevice)){
                        if(command.Retries>=MAX_RETRIES){
                            remove=true;
                        }
                        else{
                            remove=false;// don't remove the command for a retry
                            doCallback=false;// don't call the callback for a silent retry
                            result=SktScanErrors.ESKT_NOERROR;
                        }
                    }

                    if(doCallback)
                        command.DoCallback(result,scanObj);

                    if(remove)
                    {
                        lock(_commandContexts){
                            Debug.MSG(Debug.kLevelTrace,"Remove command from the list\n");
                            _commandContexts.Remove(command);
                        }
                    }
                    else
                    {
                        command.Status=CommandContext.statusReady;
                    }
                }
                if(SktScanErrors.SKTSUCCESS(result))
                {
                    result=SendNextCommand();
                }
            }
            return result;
        }
        /**
         * HandleEvent
         * 
         * This method handles asynchronous events coming from ScanAPI
         * including decoded data
         */
        private void HandleEvent(ISktScanObject scanObject)
        {
		    ISktScanEvent Event=scanObject.Msg.Event;
		    ISktScanDevice iDevice=scanObject.Msg.DeviceInterface;
		    switch(Event.EventID)
		    {
		    case ISktScanEvent.id.kSktScanEventError:
			    if(_notification!=null){
			        _notification.OnError(scanObject.Msg.Result,
			            Event.DataType == ISktScanEvent.types.kSktScanEventDataTypeString ? Event.DataString.Value : null);
			    }
			    break;
		    case ISktScanEvent.id.kSktScanEventDecodedData:
                ISktScanDecodedData decodedData = Event.DataDecodedData;
			    DeviceInfo deviceInfo=GetDeviceInfo(iDevice);
			    if(_notification!=null){
				    _notification.OnDecodedData(deviceInfo,decodedData);
			    }

			    // if the Data Confirmation mode is set to App
			    // then confirm Data here
			    if(_dataConfirmationMode==
				    ISktScanProperty.values.confirmationMode.kSktScanDataConfirmationModeApp)
			    {
				    PostSetDataConfirmation(deviceInfo,null);
    				
			    }
			    break;
		    case ISktScanEvent.id.kSktScanEventPower:
			    break;
		    case ISktScanEvent.id.kSktScanEventButtons:
			    break;
		    }
        }
 public void OnScanApiVersion(long result, ISktScanObject scanObj)
 {
     if (SktScanErrors.SKTSUCCESS(result))
     {
         _strScanApiVersion =
              Convert.ToString(scanObj.Property.Version.dwMajor, 16) + "." +
              Convert.ToString(scanObj.Property.Version.dwMiddle, 16) + "." +
              Convert.ToString(scanObj.Property.Version.dwMinor, 16) + " " +
              scanObj.Property.Version.dwBuild + " " +
              Convert.ToString(scanObj.Property.Version.wYear, 16) + "/" +
              Convert.ToString(scanObj.Property.Version.wMonth, 16) + "/" +
              Convert.ToString(scanObj.Property.Version.wDay, 16) + " " +
              Convert.ToString(scanObj.Property.Version.wHour, 16) + ":" +
              Convert.ToString(scanObj.Property.Version.wMinute, 16);
     }
 }
 public void OnGetFirmwareVersion(long result, ISktScanObject scanObj)
 {
     if (SktScanErrors.SKTSUCCESS(result))
     {
         _device.Version =
              Convert.ToString(scanObj.Property.Version.dwMajor, 16) + "." +
              Convert.ToString(scanObj.Property.Version.dwMiddle, 16) + "." +
              Convert.ToString(scanObj.Property.Version.dwMinor, 16) + " " +
              scanObj.Property.Version.dwBuild + " " +
              Convert.ToString(scanObj.Property.Version.wYear, 16) + "/" +
              Convert.ToString(scanObj.Property.Version.wMonth, 16) + "/" +
              Convert.ToString(scanObj.Property.Version.wDay, 16) + " " +
              Convert.ToString(scanObj.Property.Version.wHour, 16) + ":" +
              Convert.ToString(scanObj.Property.Version.wMinute, 16);
     }
     // done with ScanAPI, so go ahead and close it now...
     _scanApiHelper.Close();
     // Even if we couldn't get the firmware, we can go ahead and try...
     // start a thread to begin the dci/cvi process...
     var thread = new Thread(DoWarrantyUpdateThread) {Name = "RestThread"};
     thread.Start();
 }
 // OnDeviceArrival will pass this as the callback for the PostGetBtAddress call
 public void OnGetBdAddress(long result, ISktScanObject scanObj)
 {
     if (SktScanErrors.SKTSUCCESS(result))
     {
         byte[] bd = scanObj.Property.Array.Value;
         // Format the BD address the way we need it for the URLs
         _device.BdAddress = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}{5:X2}", bd[0], bd[1], bd[2], bd[3], bd[4], bd[5]);
         UpdateStatusText("New Scanner: " + _device.Name + " - " + _device.BdAddress);
         _scanApiHelper.PostGetFirmware(_device, OnGetFirmwareVersion);
     }
     else
     {
         UpdateStatusText(String.Format("Unable to get Bluetooth address - error = %d"));
         MessageBox.Show(@"We are sorry, but your scanner is not supported for the Warranty Extension program");
     }
 }