Exemplo n.º 1
0
        private void BleHostProcessMsg()
        {
            if (_isRun)
            {
                Warning("[CSR]:BleHostProcessMsg is Running...");
                return;
            }

            Debug("[CSR]:BleHostProcessMsg Start...");
            _isRun = true;
            var m      = new MSG();
            var handle = new IntPtr(-1);
            int ret;

            while ((ret = User32.GetMessage(ref m, handle, 0, 0)) != 0 && _isRun)
            {
                if (ret == -1)
                {
                    //-1 indicates an error
                }
                else
                {
                    Trace("[CSRMSG]:MSG.message - " + m.message.ToString("X4"));
                    //if (m.message == 50159)
                    {
                        switch ((uint)m.wParam)
                        {
                        //init
                        case CsrBleDefine.CSR_BLE_HOST_READY:
                            if (!OnHostReady(m.lParam > 0))
                            {
                                Debug("[CSRMSG]:CSR_BLE_HOST_READY ERROR!");
                                return;
                            }
                            Debug("[CSRMSG]:CSR_BLE_HOST_READY");
                            break;

                        //search result
                        case CsrBleDefine.CSR_BLE_HOST_SEARCH_RESULT:
                            var sr =
                                (CSR_BLE_DEVICE_SEARCH_RESULT)
                                Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                       typeof(CSR_BLE_DEVICE_SEARCH_RESULT));
                            OnHostSearchResult(sr);
                            Trace("[CSRMSG]:CSR_BLE_HOST_SEARCH_RESULT");
                            break;

                        //search stopped
                        case CsrBleDefine.CSR_BLE_HOST_SEARCH_STOPPED:
                            Debug("[CSRMSG]:CSR_BLE_HOST_SEARCH_STOPPED");
                            break;


                        //connect result
                        case CsrBleDefine.CSR_BLE_HOST_CONNECT_RESULT:
                            var cr =
                                (CSR_BLE_CONNECT_RESULT)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_CONNECT_RESULT));
                            OnHostConnectResult(cr);
                            Debug("[CSRMSG]:CSR_BLE_HOST_CONNECT_RESULT");
                            break;

                        //disconnect
                        case CsrBleDefine.CSR_BLE_HOST_DISCONNECTED:
                            var dr =
                                (CSR_BLE_DISCONNECTED)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_DISCONNECTED));
                            OnHostDisconnectResult(dr);
                            Trace("[CSRMSG]:CSR_BLE_HOST_DISCONNECTED");
                            break;


                        //discover database
                        case CsrBleDefine.CSR_BLE_CLIENT_DATABASE_DISCOVERY_RESULT:
                            var dbr = (CSR_BLE_DATABASE_DISCOVERY_RESULT)
                                      Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                             typeof(CSR_BLE_DATABASE_DISCOVERY_RESULT));
                            OnClientDatabaseDiscoveryResult(dbr);
                            Debug("[CSRMSG]:CSR_BLE_CLIENT_DATABASE_DISCOVERY_RESULT");
                            break;


                        //connection update request
                        case CsrBleDefine.CSR_BLE_HOST_CONNECTION_UPDATE_REQUEST:
                            var cur =
                                (CSR_BLE_CONNECTION_UPDATE_REQUEST)
                                Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                       typeof(CSR_BLE_CONNECTION_UPDATE_REQUEST));
                            CsrBleDll.CsrBleHostAcceptConnUpdate(cur.connectHandle,
                                                                 cur.id, true);
                            Debug("[CSRMSG]:CSR_BLE_HOST_CONNECTION_UPDATE_REQUEST");
                            break;

                        //connection update
                        case CsrBleDefine.CSR_BLE_HOST_CONNECTION_UPDATED:
                            var cu =
                                (CSR_BLE_CONNECTION_UPDATED)
                                Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                       typeof(CSR_BLE_CONNECTION_UPDATED));
                            OnHostConnectionUpdated(cu);
                            Debug("[CSRMSG]:CSR_BLE_HOST_CONNECTION_UPDATED");
                            break;


                        //host just works
                        case CsrBleDefine.CSR_BLE_HOST_JUSTWORKS_REQUEST:
                            var jwr =
                                (CSR_BLE_JUSTWORKS_REQUEST)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_JUSTWORKS_REQUEST));
                            CsrBleDll.CsrBleHostJustWorksResult(jwr.deviceAddress, true, true);
                            Debug("[CSRMSG]:CSR_BLE_HOST_JUSTWORKS_REQUEST");
                            break;


                        //scanning
                        case CsrBleDefine.CSR_BLE_HOST_LE_SCAN_STATUS:
                            Debug("[CSRMSG]:CSR_BLE_HOST_LE_SCAN_STATUS");
                            break;

                        //read result
                        case CsrBleDefine.CSR_BLE_CLIENT_CHAR_READ_RESULT:
                            var read = (CSR_BLE_CHAR_READ_RESULT)
                                       Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_CHAR_READ_RESULT));
                            OnClientCharReadResult(read);
                            Debug("[CSRMSG]:CSR_BLE_CLIENT_CHAR_READ_RESULT");
                            break;

                        //write result
                        case CsrBleDefine.CSR_BLE_CLIENT_CHAR_WRITE_RESULT:
                            var write = (CSR_BLE_WRITE_RESULT)
                                        Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_WRITE_RESULT));
                            OnClientCharWriteResult(write);
                            Debug("[CSRMSG]:CSR_BLE_CLIENT_CHAR_WRITE_RESULT");
                            break;

                        //char notify
                        case CsrBleDefine.CSR_BLE_CLIENT_CHAR_NOTIFICATION:
                            var notify =
                                (CSR_BLE_CHAR_NOTIFICATION)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_CHAR_NOTIFICATION));
                            OnClientCharNofity(notify);
                            Trace("[CSRMSG]:CSR_BLE_CLIENT_CHAR_NOTIFICATION");
                            break;


                        case CsrBleDefine.CSR_BLE_HOST_SECURITY_RESULT:
                            //bool encrypting = false;
                            //// security result.

                            //var secRes =
                            //    (CSR_BLE_SECURITY_RESULT)
                            //        Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_SECURITY_RESULT));

                            //if (secRes.result != CsrBleDefine.CSR_BLE_SECURITY_BONDING_ESTABLISHED)
                            //{
                            //    CsrBleDll.CsrBleHostCancel();
                            //    CsrBleDll.CsrBleHostDisconnect(CurDevInfo.ConnHandle);
                            //}
                            //else
                            //{
                            //    encrypting = true;
                            //}
                            Trace("[CSRMSG]:CSR_BLE_HOST_SECURITY_RESULT");
                            break;

                        default:
                            Error($"[CSRMSG]:Unhandled uEnergyHost MSG {(uint)m.wParam:X}");
                            break;
                        }
                    }
                }
            }

            Debug("[CSR]:BleHostThread Finish.");
        }
Exemplo n.º 2
0
        public void BleHostProcessMsg()
        {
            if (isRun)
            {
                OnLogEvent("BleHostThread is Running...");
                return;
            }
            OnLogEvent("BleHostThread Start...");

            isRun = true;
            MSG    m      = new MSG();
            IntPtr handle = new IntPtr(-1);
            int    ret;

            while ((ret = User32.GetMessage(ref m, handle, 0, 0)) != 0 && isRun)
            {
                if (ret == -1)
                {
                    //-1 indicates an error
                }
                else
                {
                    Trace("[BleMsg]:MSG.message - " + m.message.ToString("X4"));
                    //if (m.message == 50159)
                    {
                        switch ((uint)m.wParam)
                        {
                        //init
                        case CsrBleDefine.CSR_BLE_HOST_READY:
                            isBleInitialized = m.lParam > 0;
                            if (isBleInitialized)
                            {
                                Info("[BleMsg]:uEnergy Host library is initialized");
                                BleHostScanStart();
                                Info("[BleMsg]:CSR_BLE_HOST_READY");
                            }
                            else
                            {
                                //exit thread
                                isRun = false;
                                Error("Dongle failed to initialize! Please insert the dongle to computer!");
                                //MessageBox.Show("蓝牙初始化失败!");
                                return;
                            }
                            break;

                        //search result
                        case CsrBleDefine.CSR_BLE_HOST_SEARCH_RESULT:
                            var sr =
                                (CSR_BLE_DEVICE_SEARCH_RESULT)
                                Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                       typeof(CSR_BLE_DEVICE_SEARCH_RESULT));
                            OnSearchResult(sr);
                            Trace("[BleMsg]:CSR_BLE_HOST_SEARCH_RESULT");
                            break;

                        //search stopped
                        case CsrBleDefine.CSR_BLE_HOST_SEARCH_STOPPED:
                            Info("[BleMsg]:CSR_BLE_HOST_SEARCH_STOPPED");
                            break;


                        //connect result
                        case CsrBleDefine.CSR_BLE_HOST_CONNECT_RESULT:
                            var cr =
                                (CSR_BLE_CONNECT_RESULT)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_CONNECT_RESULT));
                            if (cr.connectHandle != 0)
                            {
                                OnConnectResult(cr);
                            }
                            Trace("[BleMsg]:CSR_BLE_HOST_CONNECT_RESULT");
                            break;

                        //disconnect
                        case CsrBleDefine.CSR_BLE_HOST_DISCONNECTED:
                            var dr =
                                (CSR_BLE_DISCONNECTED)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_DISCONNECTED));
                            if (dr.connectHandle != 0)
                            {
                                OnDisconnectResult(dr);
                            }
                            Trace("[BleMsg]:CSR_BLE_HOST_DISCONNECTED");
                            break;


                        //discover database
                        case CsrBleDefine.CSR_BLE_CLIENT_DATABASE_DISCOVERY_RESULT:
                            var dbRes = (CSR_BLE_DATABASE_DISCOVERY_RESULT)
                                        Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                               typeof(CSR_BLE_DATABASE_DISCOVERY_RESULT));
                            if (dbRes.result == 0)
                            {
                                OnDatabaseDiscoveryResult(dbRes);
                            }

                            Trace("[BleMsg]:CSR_BLE_CLIENT_DATABASE_DISCOVERY_RESULT");
                            break;


                        //connection update req
                        case CsrBleDefine.CSR_BLE_HOST_CONNECTION_UPDATE_REQUEST:
                            var connUpdateRequest =
                                (CSR_BLE_CONNECTION_UPDATE_REQUEST)
                                Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                       typeof(CSR_BLE_CONNECTION_UPDATE_REQUEST));
                            CsrBleDll.CsrBleHostAcceptConnUpdate(connUpdateRequest.connectHandle,
                                                                 connUpdateRequest.id, true);
                            Trace("[BleMsg]:CSR_BLE_HOST_CONNECTION_UPDATE_REQUEST");

                            break;

                        //connection update
                        case CsrBleDefine.CSR_BLE_HOST_CONNECTION_UPDATED:
                            var connUpdated =
                                (CSR_BLE_CONNECTION_UPDATED)
                                Marshal.PtrToStructure(new IntPtr(m.lParam),
                                                       typeof(CSR_BLE_CONNECTION_UPDATED));
                            OnConnectionUpdated(connUpdated);
                            Trace("[BleMsg]:CSR_BLE_HOST_CONNECTION_UPDATED");
                            break;


                        //host just works
                        case CsrBleDefine.CSR_BLE_HOST_JUSTWORKS_REQUEST:
                            var justworksRequest =
                                (CSR_BLE_JUSTWORKS_REQUEST)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_JUSTWORKS_REQUEST));
                            CsrBleDll.CsrBleHostJustWorksResult(justworksRequest.deviceAddress, true, true);
                            Trace("[BleMsg]:CSR_BLE_HOST_JUSTWORKS_REQUEST");
                            break;


                        //scanning
                        case CsrBleDefine.CSR_BLE_HOST_LE_SCAN_STATUS:
                            Trace("[BleMsg]:CSR_BLE_HOST_LE_SCAN_STATUS");
                            break;

                        //read result
                        case CsrBleDefine.CSR_BLE_CLIENT_CHAR_READ_RESULT:
                            var readResult = (CSR_BLE_CHAR_READ_RESULT)
                                             Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_CHAR_READ_RESULT));
                            OnCharReadResult(readResult);
                            Trace("[BleMsg]:CSR_BLE_CLIENT_CHAR_READ_RESULT");
                            break;

                        //write result
                        case CsrBleDefine.CSR_BLE_CLIENT_CHAR_WRITE_RESULT:
                            var writeResult = (CSR_BLE_WRITE_RESULT)
                                              Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_WRITE_RESULT));
                            OnCharWriteResult(writeResult);
                            Trace("[BleMsg]:CSR_BLE_CLIENT_CHAR_WRITE_RESULT");
                            break;

                        //char notify
                        case CsrBleDefine.CSR_BLE_CLIENT_CHAR_NOTIFICATION:
                            var charData =
                                (CSR_BLE_CHAR_NOTIFICATION)
                                Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_CHAR_NOTIFICATION));
                            OnClientCharNofity(charData);
                            Trace("[BleMsg]:CSR_BLE_CLIENT_CHAR_NOTIFICATION");
                            break;


                        case CsrBleDefine.CSR_BLE_HOST_SECURITY_RESULT:
                            //bool encrypting = false;
                            //// security result.

                            //var secRes =
                            //    (CSR_BLE_SECURITY_RESULT)
                            //        Marshal.PtrToStructure(new IntPtr(m.lParam), typeof(CSR_BLE_SECURITY_RESULT));

                            //if (secRes.result != CsrBleDefines.CSR_BLE_SECURITY_BONDING_ESTABLISHED)
                            //{
                            //    CsrBleDll.CsrBleHostCancel();
                            //    CsrBleDll.CsrBleHostDisconnect(CurDevInfo.ConnHandle);
                            //}
                            //else
                            //{
                            //    encrypting = true;
                            //}
                            Trace("[BleMsg]:CSR_BLE_HOST_SECURITY_RESULT");
                            break;

                        default:
                            Error(string.Format("[BleMsg]:Unhandled uEnergy Host library MSG {0:X}",
                                                (uint)m.wParam));
                            break;
                        }
                    }
                }
            }

            OnLogEvent("BleHostThread Finish.");
        }