Exemplo n.º 1
0
        private Structs.SPP_Event_Data Create <T>(StackConsts.SPP_Event_Type code,
                                                  ref T eventDataData) where T : struct
        {
            var eventData = new Structs.SPP_Event_Data(code, Alloc(ref eventDataData));

            return(eventData);
        }
Exemplo n.º 2
0
        //--------
        internal static void RaiseSppEvent(StuffClientBluetopia stuff, Structs.SPP_Event_Data eventData)
        {
            var       done  = new ManualResetEvent(false);
            Exception error = null;

            try {
                ThreadPool.QueueUserWorkItem(delegate {
                    try {
                        stuff.DutConn.HandleSPP_Event_Callback(stuff.StackId,
                                                               ref eventData, 0);
                    } catch (Exception ex) {
                        error = ex;
                    } finally {
                        done.Set();
                    }
                });
            } finally {
                var signalled = done.WaitOne(10 * 1000);
                //done.Close();
                Debug.Assert(signalled, "NOT done.signalled");
            }
            if (error != null)
            {
                throw new System.Reflection.TargetInvocationException(error);
            }
        }
Exemplo n.º 3
0
        private void HandleEventRead(Structs.SPP_Event_Data eventData)
        {
            // TODO OK to do SPP_Data_Read in callback???!!!!
            var data = (Structs.SPP_Data_Indication_Data)
                       Marshal.PtrToStructure(eventData.pEventData,
                                              typeof(Structs.SPP_Data_Indication_Data));
            ushort bufLen = data.DataLength;

            byte[] buf     = new byte[bufLen];
            var    readLen = _fcty.Api.SPP_Data_Read(_fcty.StackId, PortHandle,
                                                     bufLen, buf);

            if (readLen < 0)
            {
                var ret = (BluetopiaError)readLen;
                Debug.WriteLine("SPP_Data_Read error: " + ret);
                Debug.Fail("SPP_Data_Read error: " + ret);
            }
            else if (readLen > 0)
            {
                if (readLen != bufLen)
                {
                    Debug.Fail("SPP_Data_Read different size result!!!");
                }
                else
                {
                    HandlePortReceive(buf);
                }
            }
            else
            {
                Debug.Assert(readLen == 0);
            }
        }
Exemplo n.º 4
0
 internal // for test!
 void HandleSPP_Event_Callback(uint BluetoothStackID,
                               ref Structs.SPP_Event_Data eventData, uint CallbackParameter)
 {
     try {
         HandleSPP_Event_Callback2(BluetoothStackID,
                                   ref eventData, CallbackParameter);
     } catch (Exception ex) {
         Utils.MiscUtils.Trace_WriteLine("Exception in our HandleSPP_Event_Callback_t: " + ex);
     }
 }
Exemplo n.º 5
0
        private Structs.SPP_Event_Data EventConnectInd(Structs.SPP_Event_Data SPP_Event_Data)
        {
            var data = (Structs.SPP_Open_Port_Indication_Data)
                       Marshal.PtrToStructure(SPP_Event_Data.pEventData,
                                              typeof(Structs.SPP_Open_Port_Indication_Data));

            Debug.WriteLine("EventConnectInd portId: " + data.SerialPortID);
            Debug.Assert(_hPortServer.Value == data.SerialPortID, "thought this should be the same id");
            _serverIndAddress = BluetopiaUtils.ToBluetoothAddress(data.BD_ADDR);
            ThreadPool.QueueUserWorkItem(delegate {
                HandleCONNECTED("INDICATE");
            });
            return(SPP_Event_Data);
        }
Exemplo n.º 6
0
        void HandleSPP_Event_Callback2(uint BluetoothStackID,
                                       ref Structs.SPP_Event_Data eventData, uint CallbackParameter)
        {
            //Debug.WriteLine(string.Format(CultureInfo.InvariantCulture,
            //    "HandleSPP_Event_Callback2: {0}=0x{1:X}",
            //    eventData.Event_Data_Type, (int)eventData.Event_Data_Type));
            switch (eventData.Event_Data_Type)
            {
            case StackConsts.SPP_Event_Type.Port_Open_Indication:
                EventConnectInd(eventData);
                break;

            case StackConsts.SPP_Event_Type.Port_Open_Confirmation:
                EventConnectConfOrNeg(eventData);
                break;

            case StackConsts.SPP_Event_Type.Port_Close_Port_Indication:
                HandleCONNECT_ERR("REMOTE-CLOSE", null);
                break;

            case StackConsts.SPP_Event_Type.Port_Status_Indication:
                break;

            case StackConsts.SPP_Event_Type.Port_Data_Indication:
                HandleEventRead(eventData);
                break;

            case StackConsts.SPP_Event_Type.Port_Transmit_Buffer_Empty_Indication:
                FreePendingWrites();
                break;

            case StackConsts.SPP_Event_Type.Port_Line_Status_Indication:
            case StackConsts.SPP_Event_Type.Port_Send_Port_Information_Indication:
            case StackConsts.SPP_Event_Type.Port_Send_Port_Information_Confirmation:
            case StackConsts.SPP_Event_Type.Port_Query_Port_Information_Indication:
            case StackConsts.SPP_Event_Type.Port_Query_Port_Information_Confirmation:
                // NO-OP
                break;

            case StackConsts.SPP_Event_Type.Port_Open_Request_Indication:
                // We use automatic accept mode so we won't see this event.
                Debug.Fail("Don't expect Port_Open_Request_Indication event.");
                break;

            default:
                Debug.Fail("unknown event: " + eventData.Event_Data_Type);
                break;
            }
        }
Exemplo n.º 7
0
        private Structs.SPP_Event_Data EventConnectConfOrNeg(Structs.SPP_Event_Data SPP_Event_Data)
        {
            var data = (Structs.SPP_Open_Port_Confirmation_Data)
                       Marshal.PtrToStructure(SPP_Event_Data.pEventData,
                                              typeof(Structs.SPP_Open_Port_Confirmation_Data));
            int?socketErrorCode;

            Debug.WriteLine(string.Format(CultureInfo.InvariantCulture,
                                          "EventConnectConf: {0}=0x{1:X}",
                                          data.PortOpenStatus, (int)data.PortOpenStatus));
            switch (data.PortOpenStatus)
            {
            case StackConsts.SPP_OPEN_PORT_STATUS.Success:
                socketErrorCode = null;
                break;

            case StackConsts.SPP_OPEN_PORT_STATUS.ConnectionRefused:
                socketErrorCode = SocketError_ConnectionRefused;
                break;

            case StackConsts.SPP_OPEN_PORT_STATUS.ConnectionTimeout:
                socketErrorCode = SocketError_TimedOut;
                break;

            case StackConsts.SPP_OPEN_PORT_STATUS.UnknownError:
                socketErrorCode = SocketError_InvalidArgument;
                break;

            default:
                Debug.WriteLine("Unknown SPP_Open_Port_Confirmation_Data_t code: 0x"
                                + ((int)data.PortOpenStatus).ToString("X"));
                socketErrorCode = SocketError_InvalidArgument;
                break;
            }
            ThreadPool.QueueUserWorkItem(delegate {
                if (socketErrorCode == null)
                {
                    HandleCONNECTED(data.PortOpenStatus.ToString());
                }
                else
                {
                    HandleCONNECT_ERR(data.PortOpenStatus.ToString(), socketErrorCode);
                }
            });
            return(SPP_Event_Data);
        }
Exemplo n.º 8
0
        internal static StuffClientBluetopia DoOpen(StuffClientBluetopia stuff, Action beforeEndConnect)
        {
            var cli       = stuff.DutClient;
            var cli2      = (BluetopiaClient)cli;
            var conn      = (BluetopiaRfcommStream)cli2.Testing_GetConn();
            var behaviour = stuff.Behaviour;
            //
            BluetoothEndPoint remote;

            if (behaviour.ToPortNumber)
            {
                remote = new BluetoothEndPoint(Addr1, BluetoothService.Empty, Port5);
            }
            else
            {
                remote = new BluetoothEndPoint(Addr1, BluetoothService.VideoSource);
            }
            //
            bool ourCallbackCalled = false;
            var  ourCallback       = (AsyncCallback) delegate { ourCallbackCalled = true; };
            //
            var ar = cli.BeginConnect(remote, ourCallback, null);

            Assert_IsConnected(
                IsConnectedState.Closed,
                conn, cli, "BB");
            stuff.Mockery_VerifyAllExpectationsHaveBeenMet();
            //
            Assert_IsConnected(
                IsConnectedState.Closed,
                conn, cli, "CC0");
            if (behaviour.SdpQueryResultPort.HasValue)
            {
                var listAllocs = new List <IntPtr>();
                IntPtr /*"SDP_Response_Data *"*/ pSdp = BluetopiaSdpParseTests
                                                        .ProtoDListMake_InSDPResponse_Data(listAllocs, behaviour.SdpQueryResultPort.Value);
                var  sdpQuery          = cli2.Testing_GetSdpQuery();
                uint SDPRequestID      = 0;
                uint CallbackParameter = 0;
                //
                stuff.AddExpectOpenRemotePort(null, null);
                // TODO raise callback on thread pool
                sdpQuery.HandleSDP_Response_Callback(stuff.StackId, SDPRequestID,
                                                     pSdp, CallbackParameter);
                BluetopiaSdpParseTests.Free(listAllocs);
                Thread.Sleep(2000);//HACK
            }
            //
            Assert_IsConnected(
                IsConnectedState.Closed,
                conn, cli, "CC");
            stuff.Mockery_VerifyAllExpectationsHaveBeenMet();
            var openConfData = new Structs.SPP_Open_Port_Confirmation_Data(
                conn.Testing_GetPortId(), behaviour.ConnConfStatusCode);

            using (var ctor = new SppEventCreator()) {
                Structs.SPP_Event_Data eventData = ctor.CreateOpenConfirmation(
                    conn.Testing_GetPortId(), behaviour.ConnConfStatusCode);
                RaiseSppEvent(stuff, eventData);
            }
            SafeWait(ar); //NEW
            Assert_IsConnected(
                behaviour.EndConnectSuccess ? IsConnectedState.Connected : IsConnectedState.Closed,
                conn, cli, "DD");
            Assert.IsTrue(ar.IsCompleted, "ar.IsCompleted before");
            if (beforeEndConnect != null)
            {
                beforeEndConnect();
            }
            if (behaviour.EndConnectSuccess)
            {
                Debug.Assert(!behaviour.SocketError.HasValue, "Behaviour settings: Success BUT errorCode!!");
                cli.EndConnect(ar);
            }
            else
            {
                Debug.Assert(behaviour.SocketError.HasValue, "Behaviour settings: not Success BUT NO errorCode!!");
                try {
                    cli.EndConnect(ar);
                    Assert.Fail("should have thrown!");
                } catch (SocketException ex) {
                    //TODO Assert.AreEqual(SocketError.ConnectionRefused, ex.SocketErrorCode, "SocketErrorCode");
                    Assert.AreEqual(behaviour.SocketError ?? 0, ex.ErrorCode, "(Socket)ErrorCode");
                }
            }
            Thread.Sleep(200); // let the async async-callback run
            Assert.IsTrue(ourCallbackCalled, "ourCallbackCalled");
            //
            if (behaviour.EndConnectSuccess)
            {
                Assert_IsConnected(
                    IsConnectedState.Connected,
                    conn, cli, "DD2");
                BluetoothEndPoint expectedRemote = new BluetoothEndPoint(Addr1, BluetoothService.Empty, Port5);
                Assert.AreEqual(expectedRemote, cli.RemoteEndPoint, "cli.RemoteEndPoint");
            }
            //
            stuff.Mockery_VerifyAllExpectationsHaveBeenMet();
            return(stuff);
        }