Beispiel #1
0
        public bool Init()
        {
            sdkContext = API.BS2_AllocateContext();
            if (sdkContext == IntPtr.Zero)
            {
                _log.Error("环境初始化失败");
                return(false);
            }

            BS2ErrorCode result = (BS2ErrorCode)API.BS2_Initialize(sdkContext);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                _log.ErrorFormat("环境初始化失败 {0}", result);
                API.BS2_ReleaseContext(sdkContext);
                return(false);
            }

            return(true);
        }
        public void runWithoutConnection()
        {
            UInt32 deviceID   = 0;
            IntPtr versionPtr = API.BS2_Version();

            //bool bSsl = false;

            if (title.Length > 0)
            {
                Console.Title = title;
            }

            Console.WriteLine("SDK version : " + Marshal.PtrToStringAnsi(versionPtr));

            sdkContext = API.BS2_AllocateContext();
            if (sdkContext == IntPtr.Zero)
            {
                Console.WriteLine("Can't allocate sdk context.");
                return;
            }

            BS2ErrorCode result = (BS2ErrorCode)API.BS2_Initialize(sdkContext);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("SDK initialization failed with : {0}", result);
                API.BS2_ReleaseContext(sdkContext);
                return;
            }

            runImpl(deviceID);

            API.BS2_ReleaseContext(sdkContext);
            sdkContext = IntPtr.Zero;

            cbOnDeviceFound        = null;
            cbOnDeviceAccepted     = null;
            cbOnDeviceConnected    = null;
            cbOnDeviceDisconnected = null;
            cbOnSendRootCA         = null;
        }
        public void run()
        {
            UInt32 deviceID   = 0;
            IntPtr versionPtr = API.BS2_Version();

            //bool bSsl = false;

            if (title.Length > 0)
            {
                Console.Title = title;
            }

            Console.WriteLine("SDK version : " + Marshal.PtrToStringAnsi(versionPtr));

            sdkContext = API.BS2_AllocateContext();
            if (sdkContext == IntPtr.Zero)
            {
                Console.WriteLine("Can't allocate sdk context.");
                return;
            }

            Console.WriteLine("Do you want to set up ssl configuration? [Y/n]");
            Console.Write(">>>> ");
            if (Util.IsYes())
            {
                cbPreferMethod                = new API.PreferMethod(PreferMethodHandle);
                cbGetRootCaFilePath           = new API.GetRootCaFilePath(GetRootCaFilePathHandle);
                cbGetServerCaFilePath         = new API.GetServerCaFilePath(GetServerCaFilePathHandle);
                cbGetServerPrivateKeyFilePath = new API.GetServerPrivateKeyFilePath(GetServerPrivateKeyFilePathHandle);
                cbGetPassword    = new API.GetPassword(GetPasswordHandle);
                cbOnErrorOccured = new API.OnErrorOccured(OnErrorOccuredHandle);
                //ServicePointManager.SecurityProtocol = (SecurityProtocolType)SecurityProtocolType.Ssl3;

                BS2ErrorCode sdkResult = (BS2ErrorCode)API.BS2_SetSSLHandler(sdkContext, cbPreferMethod, cbGetRootCaFilePath, cbGetServerCaFilePath, cbGetServerPrivateKeyFilePath, cbGetPassword, null);
                if (sdkResult != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("BS2_SetSSLHandler failed with : {0}", sdkResult);
                    //API.BS2_ReleaseContext(sdkContext); //중복
                    return;
                }
                else
                {
                    //bSsl = true;
                }
            }

            BS2ErrorCode result = (BS2ErrorCode)API.BS2_Initialize(sdkContext);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("SDK initialization failed with : {0}", result);
                API.BS2_ReleaseContext(sdkContext);
                return;
            }

            cbOnDeviceFound        = new API.OnDeviceFound(DeviceFound);
            cbOnDeviceAccepted     = new API.OnDeviceAccepted(DeviceAccepted);
            cbOnDeviceConnected    = new API.OnDeviceConnected(DeviceConnected);
            cbOnDeviceDisconnected = new API.OnDeviceDisconnected(DeviceDisconnected);

            result = (BS2ErrorCode)API.BS2_SetDeviceEventListener(sdkContext,
                                                                  cbOnDeviceFound,
                                                                  cbOnDeviceAccepted,
                                                                  cbOnDeviceConnected,
                                                                  cbOnDeviceDisconnected);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Can't register a callback function/method to a sdk.({0})", result);
                API.BS2_ReleaseContext(sdkContext);
                return;
            }

            /*
             * if (bSsl)
             * {
             *  cbOnSendRootCA = new API.OnSendRootCA(SendRootCA);
             *  result = (BS2ErrorCode)API.BS2_SetDeviceSSLEventListener(sdkContext, cbOnSendRootCA);
             * }
             */

            cbDebugExPrint = null;
            Console.WriteLine("Do you want print debug message? [y/n]");
            Console.Write(">>>> ");
            if (Util.IsYes())
            {
                cbDebugExPrint = new API.CBDebugExPrint(DebugExPrint);
                result         = (BS2ErrorCode)API.BS2_SetDebugExCallback(cbDebugExPrint, Constants.DEBUG_LOG_ALL, Constants.DEBUG_MODULE_ALL);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                    return;
                }

                //Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
                //Trace.AutoFlush = true;
                //Trace.Indent();
            }

#if SDK_AUTO_CONNECTION
            result = (BS2ErrorCode)API.BS2_SetAutoConnection(sdkContext, 1);
#endif

            Console.WriteLine("+-----------------------------------------------------------+");
            Console.WriteLine("| 1. Search and connect device                              |");
            Console.WriteLine("| 2. Connect to device via Ip                               |");
            Console.WriteLine("| 3. Server mode test                                       |");
            Console.WriteLine("+-----------------------------------------------------------+");
            Console.WriteLine("How to connect to device? [2(default)]");
            Console.Write(">>>> ");
            int selection = Util.GetInput(2);

            switch (selection)
            {
            case 1:
                if (!SearchAndConnectDevice(ref deviceID))
                {
                    deviceID = 0;
                }
                break;

            case 2:
                if (!ConnectToDevice(ref deviceID))
                {
                    deviceID = 0;
                }
                break;

            case 3:
            {
                if (deviceIDForServerMode == 0)
                {
                    Console.WriteLine("Waiting for client connection");
                    eventWaitHandle.WaitOne();
                }


                deviceID = deviceIDForServerMode;

                /*
                 * result = (BS2ErrorCode)API.BS2_ConnectDevice(sdkContext, deviceID);
                 *
                 * if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                 * {
                 *  Console.WriteLine("Can't connect to device(errorCode : {0}).", result);
                 *  deviceID = 0;
                 * }
                 * else
                 * {
                 *  Console.WriteLine(">>>> Successfully connected to the device[{0}].", deviceID);
                 * }
                 */
            }
            break;

            default:
                Console.WriteLine("Invalid parameter : {0}", selection);
                break;
            }

            if (deviceID > 0)
            {
                Console.Title = String.Format("{0} connected deviceID[{1}]", title, deviceID);

#if !SDK_AUTO_CONNECTION
                reconnectionTask = new ReconnectionTask(sdkContext);
                reconnectionTask.start();
#endif
                runImpl(deviceID);
#if !SDK_AUTO_CONNECTION
                reconnectionTask.stop();
                reconnectionTask = null;
#endif

                Console.WriteLine("Trying to discconect device[{0}].", deviceID);
                result = (BS2ErrorCode)API.BS2_DisconnectDevice(sdkContext, deviceID);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                }
            }


            eventWaitHandle.Close();
            API.BS2_ReleaseContext(sdkContext);
            sdkContext = IntPtr.Zero;

            cbOnDeviceFound        = null;
            cbOnDeviceAccepted     = null;
            cbOnDeviceConnected    = null;
            cbOnDeviceDisconnected = null;
            cbOnSendRootCA         = null;
        }