/*****************************************************************************
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.
*****************************************************************************/
// BTCon_AppUi.cpp
//
//
#include <eikenv.h> // CEikonEnv
#include <QBTselectdlg.h> // CQBTUISelectDialog
#include <btsdp.h> // CSdpAgent
#include "BTCon_AppView.h"
#include "BTCon_MsgClient.h"
_LIT8(KTest, "Connection Test");
_LIT(KSending, "sending: ");
const TInt KServiceClass = 0x1101; // SerialPort
CMsgClient* CMsgClient::NewL(CBTConAppView* aView)
{
CMsgClient* self = NewLC(aView);
CleanupStack::Pop(self);
return self;
}
CMsgClient* CMsgClient::NewLC(CBTConAppView* aView)
{
CMsgClient* self = new (ELeave) CMsgClient(aView);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CMsgClient::~CMsgClient()
{
Disconnect(EFalse);
Deque();
}
void CMsgClient::ConnectL()
{
if(iCurrentState == EInitStage && !IsActive())
{
iCurrentState = ESearchForDevice;
TBTDevAddr devAddr;
TBTDeviceName devName;
TBTDeviceClass devClass;
TInt serviceFilter = -1;
CQBTUISelectDialog* dialog = new (ELeave) CQBTUISelectDialog(
devAddr,
devName,
devClass,
CQBTUISelectDialog::EQBTDeviceFilterAll,
serviceFilter);
// Launch "Select Bluetooth device" dialog.
if (dialog->LaunchSingleSelectDialogLD())
{
iResultPckg().SetDeviceAddress(devAddr);
iResultPckg().SetDeviceName(devName);
iResultPckg().SetDeviceClass(devClass);
iStatus = KErrNone;
RunL();
}
else
{
iStatus = KErrCancel; // Error or cancel
RunL();
}
}
else
{
iPtrView->UpdateEditText(_L("Client busy"));
User::Leave(KErrInUse);
}
}
void CMsgClient::Disconnect(TBool aDisplayInfo)
{
switch(iCurrentState)
{
case ESearchForService:
{
iCurrentState = EDisconnecting;
TRequestStatus* status = &iStatus;
User::RequestComplete(status, KErrNotFound);
return;
}
break;
case EConnecting :
iSocket.CancelConnect();
break;
case EReading:
iSocket.CancelRecv();
break;
case EWriting:
case ESendingTestMsg:
iSocket.CancelWrite();
break;
default:
break;
}
iSocket.Close();
iSocketServ.Close();
iCurrentState = EInitStage;
if( iWriteMsgBuf )
{
delete iWriteMsgBuf;
iWriteMsgBuf = 0;
}
if(aDisplayInfo)
iPtrView->UpdateEditText(_L("Disconected"));
}
void CMsgClient::SendMsgL(const TDesC& aMsg)
{
// check if is not already doing 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 )
{
iSocket.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, iSocket.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 CMsgClient::DoSendMsgAfterCanceling()
{
// give info: sending msg
TBuf<30> msg;
msg.Copy(*iWriteMsgBuf);
TBuf<50> buf;
buf.Append(KSending);
buf.Append(msg);
iPtrView->UpdateEditText(buf);
iCurrentState = EWriting;
iBusyWriting = ETrue;
TRAPD(err, iSocket.Write(*iWriteMsgBuf, iStatus));
if(err != KErrNone)
{
TBuf<256> buf;
buf.Format(_L("SendMsgErr: %d"), err);
iPtrView->UpdateEditText(buf);
}
if( !IsActive() )
SetActive();
}
void CMsgClient::AttributeRequestComplete(TSdpServRecordHandle /*aHandle*/,
TInt aError)
{
if (aError != KErrNone)
{
TBuf<256> buf;
buf.Format(_L("AttReqCompl, connect failed %d"), aError);
iPtrView->UpdateEditText(buf);
TRequestStatus* status = &iStatus;
User::RequestComplete(status, aError);
return;
}
if(!iHasFoundService)
{
iPtrView->UpdateEditText(_L("Making another SDP request"));
iSdpAgent->NextRecordRequestL();
}
else
{
if (aError == KErrNone && !iHasFoundService)
{
aError = KErrNotFound;
}
TBuf<256> buf;
buf.Format(_L("AttReqCompl finished, %d"), aError);
iPtrView->UpdateEditText(buf);
TRequestStatus* status = &iStatus;
User::RequestComplete(status, aError);
}
}
void CMsgClient::AttributeRequestResult(TSdpServRecordHandle /*aHandle*/,
TSdpAttributeID aAttrID,
CSdpAttrValue* aAttrValue)
{
TBuf<256> buf;
buf.Format(_L("Got attrib: id: %d type: %d"), aAttrID, aAttrValue->Type());
iPtrView->UpdateEditText(buf);
aAttrValue->AcceptVisitorL(*this);
}
void CMsgClient::NextRecordRequestComplete(TInt aError,
TSdpServRecordHandle aHandle,
TInt aTotalRecordsCount)
{
if( aError != KErrNone )
{
TBuf<256> buf;
buf.Format(_L("ConnectionFailed %d"), aError);
iPtrView->UpdateEditText(buf);
TRequestStatus* status = &iStatus;
User::RequestComplete(status, aError);
}
else if(aTotalRecordsCount == 0)
{
iPtrView->UpdateEditText(_L("NRRC No records"));
TRequestStatus* status = &iStatus;
User::RequestComplete(status, KErrNotFound);
return;
}
else
{
iSdpAgent->AttributeRequestL(aHandle, KSdpAttrIdProtocolDescriptorList);
}
}
void CMsgClient::VisitAttributeValueL(CSdpAttrValue& aValue,
TSdpElementType aType)
{
TBuf<256> buf;
buf.Format(_L("VisAttVal type: %d"), aType);
if (aValue.Type() == ETypeUUID)
{
TUUID uuid = aValue.UUID();
TBuf<256> uuidBuf;
uuidBuf.Copy(uuid.ShortestForm());
uuidBuf.SetMax();
buf.Format(_L(" UUID %02X.%02X.%02X.%02X"),
uuidBuf[0], uuidBu