⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btcon_msgserver.cpp

📁 C++实现的BlueTooth
💻 CPP
字号:
/*****************************************************************************

 COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2005.



 The software is the copyrighted work of Sony Ericsson Mobile Communications AB.

 The use of the software is subject to the terms of the end-user license

 agreement which accompanies or is included with the software. The software is

 provided "as is" and Sony Ericsson specifically disclaim any warranty or

 condition whatsoever regarding merchantability or fitness for a specific

 purpose, title or non-infringement. No warranty of any kind is made in

 relation to the condition, suitability, availability, accuracy, reliability,

 merchantability and/or non-infringement of the software provided herein.



 *****************************************************************************/


#include <bt_sock.h>

#include "BTCon_Application.h"
#include "BTCon_AppView.h"
#include "BTCon_MsgServer.h"
#include "BTCon_SdpManager.h"


_LIT(KServerTransportName,"RFCOMM");
_LIT(KSending, "sending: ");


CMsgServer* CMsgServer::NewL(CBTConAppView* aView)
  {
  CMsgServer* self = NewLC(aView);
  CleanupStack::Pop(self);
  return self;
  }

CMsgServer* CMsgServer::NewLC(CBTConAppView* aView)
  {
  CMsgServer* self = new (ELeave) CMsgServer(aView);
  CleanupStack::PushL(self);
  self->ConstructL();
  return self;
  }

CMsgServer::~CMsgServer()
  {
  Disconnect(EFalse);

  if(iSdpManager)
    {
    delete iSdpManager;
    iSdpManager = NULL;
    }

  Deque();
  }

void CMsgServer::ConnectL()
  {
  if( iCurrentState != EInitAndDisconnected)
    {
    iPtrView->UpdateEditText(_L("Server busy, disconnect first"));
    User::Leave(KErrInUse);
    }
  iBusyWriting = EFalse;

  TBuf<256> buf;
// SDK: "How to listen for connections from remote devices"
  // Connect to the socket server
  User::LeaveIfError(iSocketServ.Connect());
  TRAPD(err, iSocketListen.Open(iSocketServ, KServerTransportName));
  if( err != KErrNone )
    {
    buf.Format(_L("Connecting failed %d"), err);
    iPtrView->UpdateEditText(buf);

    iSocketServ.Close();
    User::Leave(err);
    }

  // Get the next available RFCOMM server channel
  TInt servChannel;
  User::LeaveIfError(iSocketListen.GetOpt(KRFCOMMGetAvailableServerChannel,
                                          KSolBtRFCOMM,
                                          servChannel));
  buf.Format(_L("Using servChannel: %d"), servChannel);
  iPtrView->UpdateEditText(buf);

  // Sets the local address of a socket.
  // It will leave if the channel is already in use
  TBTSockAddr listeningAddress;
  listeningAddress.SetPort(servChannel);
  User::LeaveIfError(iSocketListen.Bind(listeningAddress));

  // Open a listening socket
  User::LeaveIfError(iSocketListen.Listen(1));

  // just in case, close old connection
  iSocketAccept.Close();
  // open accept socket and wait for connection
  User::LeaveIfError(iSocketAccept.Open(iSocketServ));
  iSocketListen.Accept(iSocketAccept, iStatus);

  iCurrentState = EConnecting;
  iPtrView->UpdateEditText(_L("Waiting for connection..."));
  SetActive();

  // add security settings for this connection
  SetSecurityL(servChannel);

  iSdpManager->ConnectAndSetToSdpL(servChannel);
  iSdpManager->UpdateAvailabilityL(ETrue);
  }

void CMsgServer::Disconnect(TBool aDisplayInfo)
  {
  if( iCurrentState != EInitAndDisconnected )
    {
    if(iSdpManager->IsAdvertising())
      iSdpManager->StopConnectionToSdpL();

    iSocketAccept.Close();
    iSocketListen.Close();
    iSocketServ.Close();
    }

  iCurrentState = EInitAndDisconnected;

  if( iWriteMsgBuf )
    {
    delete iWriteMsgBuf;
    iWriteMsgBuf = 0;
    }

  if(aDisplayInfo)
    iPtrView->UpdateEditText(_L("Disconnected"));
  }

void CMsgServer::SendMsgL(const TDesC& aMsg)
  {
  // check if is not already writing or canceling reading
  if( iBusyWriting != EFalse || iCurrentState == ECancelReading )
    {
    iPtrView->UpdateEditText(_L("SendMsgL err: writing..."));
    return;
    }
  // if in reading state, cancel reading first
  if( iCurrentState == EReading )
    {
    // cancelRecv will give KErrCancel after finishing
    iSocketAccept.CancelRecv();
    iCurrentState = ECancelReading;
    iPtrView->UpdateEditText(_L("Reading was started: canceling..."));
    if( iWriteMsgBuf != NULL )
      {
      delete iWriteMsgBuf;
      iWriteMsgBuf = NULL;
      }
    iWriteMsgBuf = HBufC8::NewL(aMsg.Length());
    TPtr8 bufPtr = iWriteMsgBuf->Des();
    bufPtr.Append(aMsg);
    if( !IsActive() )
      SetActive();
    }
  // if in writing state, just simply write
  else if( iCurrentState == EWriting )
    {
    // give info: sending msg
    TBuf<50> buf;
    buf.Append(KSending);
    buf.Append(aMsg);
    iPtrView->UpdateEditText(buf);

    iCurrentState = EWriting;
    iBusyWriting = ETrue;

    HBufC8* msgH = HBufC8::NewL(aMsg.Length());
    TPtr8 bufPtr = msgH->Des();
    bufPtr.Append(aMsg);

    TRAPD(err, iSocketAccept.Write(*msgH, iStatus));
    if(err != KErrNone)
      {
      TBuf<256> buf;
      buf.Format(_L("SendMsgErr: %d"), err);
      iPtrView->UpdateEditText(buf);
      }
    if( !IsActive() )
      SetActive();
    }
  // do not try to send when in idle or connecting state
  }

void CMsgServer::DoCancel()
  {
  }

void CMsgServer::RunL()
  {
  // list of errors can be found:
  // UIQ 2.1 SDK 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -