diff -r 89d6a7a84779 -r 25a17d01db0c Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/panconnection_8cpp-source.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/panconnection_8cpp-source.html Fri Jan 22 18:26:19 2010 +0000 @@ -0,0 +1,1014 @@ + + +TB10.1 Example Applications: examples/Bluetooth/BTExample1/src/panconnection.cpp Source File + + + + +

examples/Bluetooth/BTExample1/src/panconnection.cpp

00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
+00002 // All rights reserved.
+00003 // This component and the accompanying materials are made available
+00004 // under the terms of "Eclipse Public License v1.0"
+00005 // which accompanies this distribution, and is available
+00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
+00007 //
+00008 // Initial Contributors:
+00009 // Nokia Corporation - initial contribution.
+00010 //
+00011 // Contributors:
+00012 //
+00013 // Description:
+00014 //
+00015 
+00016 
+00017 #include <e32cons.h>
+00018 #include <comms-infras/nifif.h>
+00019 #include <comms-infras/nifagt.h>
+00020 #include <panprog.h>
+00021 #include <panctrl.h>
+00022 #include <commbbconnpref.h>
+00023 #include <bt_sock.h>
+00024 #include <es_sock.h>
+00025 #include <in_sock.h>
+00026 #include <c32comm.h>
+00027 
+00028 
+00029 #include "panconnection.h"
+00030 #include "gameconstants.h"
+00031 
+00032 
+00033 
+00037 CPanConnections* CPanConnections::NewL(CConsoleBase & aConsole)
+00038         {
+00039         CPanConnections* self = new CPanConnections(aConsole);
+00040         return self;
+00041         }
+00042 
+00046 CPanConnections::CPanConnections(CConsoleBase & aConsole) 
+00047         : CActive(EPriorityNormal),
+00048         iIapStarted(EFalse), 
+00049         iActivePanConn(KErrNotFound), 
+00050         iConsole(aConsole), 
+00051         iRemoteIpAddr(0)
+00052         {       
+00053         CActiveScheduler::Add(this);
+00054         }
+00055 
+00059 CPanConnections::~CPanConnections()
+00060         {
+00061 
+00062         for (TInt i=0; i < iSocketArray.Count();i++)
+00063                 {
+00064                 iSocketArray[i].Close();
+00065                 }       
+00066         iSocketArray.Close();
+00067         iSocket.Close();
+00068         
+00069         if (iIapStarted != EFalse)
+00070                 {
+00071                 iConnection.Stop();
+00072                 iConnection.Close();
+00073                 iIapStarted = EFalse;
+00074                 }
+00075         
+00076         iIapLoading = EFalse;
+00077 
+00078         iActiveConnections.Close();
+00079         iSockSvr.Close();
+00080         iSdp.Close();
+00081         iSdpdb.Close();
+00082         
+00083 
+00084 
+00085         }
+00086 
+00090 TInt CPanConnections::Initialise()
+00091         {
+00092         
+00093         #if defined (__WINS__)
+00094                 _LIT(KPDDName, "ECDRV");
+00095         #else           
+00096                 _LIT(KPDDName, "EUART1");               
+00097         #endif
+00098                 _LIT(KLDDName, "ECOMM");        
+00099         TInt rerr = KErrNone;
+00100         rerr = StartC32();// For any Serial comms
+00101         // Load required drivers
+00102         rerr = User::LoadPhysicalDevice(KPDDName);
+00103         if(rerr != KErrNone && rerr != KErrAlreadyExists)
+00104                 {
+00105                 return rerr;
+00106                 }
+00107                 
+00108         rerr = User::LoadLogicalDevice(KLDDName);
+00109         if(rerr != KErrNone && rerr != KErrAlreadyExists)
+00110                 {
+00111                 return rerr;
+00112                 }
+00113 
+00114         rerr = iSockSvr.Connect();
+00115         if(rerr)
+00116                 {
+00117                 return rerr;
+00118                 }
+00119 
+00120         TProtocolDesc iProtocolInfo;
+00121         TRequestStatus status;
+00122 
+00123         rerr = iSockSvr.FindProtocol(_L("L2CAP"), iProtocolInfo);
+00124         if(rerr)
+00125                 {
+00126                 return rerr;
+00127                 }
+00128         // After Socket server is connected, start L2CAP protocol
+00129         iSockSvr.StartProtocol(iProtocolInfo.iAddrFamily, iProtocolInfo.iSockType, iProtocolInfo.iProtocol, status);
+00130         User::WaitForRequest(status);
+00131         // Register SDP record.
+00132         TRAP(rerr, RegisterSdpL());
+00133         
+00134         return rerr;
+00135         }
+00136 
+00137 void CPanConnections::RegisterSdpL()
+00138         {
+00139         User::LeaveIfError(iSdp.Connect());
+00140         User::LeaveIfError(iSdpdb.Open(iSdp));
+00141                 
+00142         TSdpServRecordHandle handle = 0;
+00143                 
+00144         switch (iLocalRole)
+00145                 {
+00146                 case KPanUUUID:
+00147                         iSdpdb.CreateServiceRecordL(KPanUUUID, handle);
+00148                 break;
+00149                 
+00150                 case KPanGnUUID:
+00151                         iSdpdb.CreateServiceRecordL(KPanGnUUID, handle);
+00152                 break;
+00153                 
+00154                 default:
+00155                         iSdpdb.CreateServiceRecordL(KPanUUUID, handle);
+00156                 break;
+00157                 }
+00158         
+00159                 
+00160         // A list of protocols supported
+00161         CSdpAttrValueDES* protocols = CSdpAttrValueDES::NewDESL(0);
+00162         CleanupStack::PushL(protocols);
+00163                 protocols
+00164                 ->StartListL()
+00165                         ->BuildDESL()
+00166                         ->StartListL()
+00167                                 ->BuildUUIDL(KL2CAPUUID)
+00168                                 ->BuildUUIDL(KBnepUUID)
+00169                                 ->BuildUUIDL(KIpUUID)
+00170                                 ->BuildUUIDL(KTcpUUID) 
+00171                         ->EndListL()
+00172                         
+00173                 ->EndListL();
+00174                 
+00175                 
+00176         iSdpdb.UpdateAttributeL(handle, KSdpAttrIdProtocolDescriptorList,*protocols);
+00177         CleanupStack::PopAndDestroy(protocols);
+00178                 
+00179         // Service Name attribute, a universal attribute that defines the name of this service.
+00180         iSdpdb.UpdateAttributeL(handle, KSdpAttrIdServiceName, KServiceName);
+00181         // Service description attribute
+00182         iSdpdb.UpdateAttributeL(handle, KSdpAttrIdServiceDescription, KServiceDescription);                     
+00183         
+00184         }
+00185 
+00186 
+00190 void CPanConnections::IapStarted()
+00191         {               
+00192         TInt rerr;
+00193         if (!iIapLoading)
+00194                 {
+00195 
+00196                 }
+00197         
+00198         iIapLoading = EFalse;
+00199         iIapStarted = ETrue;
+00200         // Open TCP socket with interface to existing RConnection
+00201 
+00202         if (iUseTcpTransport)
+00203                 {
+00204                 rerr = iSocket.Open(iSockSvr, KAfInet, KSockStream, KProtocolInetTcp, iConnection);
+00205                 }
+00206         else
+00207                 {
+00208                 // User wants a UDP socket
+00209                 rerr = iSocket.Open(iSockSvr, KAfInet, KSockDatagram, KProtocolInetUdp, iConnection);
+00210                 }
+00211                 
+00212         if (rerr != KErrNone)
+00213                 {
+00214                 return;
+00215                 }
+00216         // Start TCP connection sequence                        
+00217         if (iListening && iUseTcpTransport)
+00218                 {
+00219                 TcpIpBindAndListen();
+00220                 AcceptNewSocket();
+00221                 }
+00222         // If we are a UDP socket then bind, a wait is required 
+00223         if (!iUseTcpTransport)
+00224                 {
+00225                 TInt duration;
+00226                 if (iLocalRole == (TUint32)KPanGnUUID)
+00227                         {
+00228                         duration = 4000000;
+00229                         }
+00230                 else 
+00231                         {
+00232                         duration = 6000000;
+00233                         }
+00234                         
+00235                 RTimer timer;
+00236                 TRequestStatus status;
+00237                 timer.CreateLocal();
+00238                 timer.After(status, duration);
+00239                 User::WaitForRequest(status);
+00240                 timer.Close();
+00241                 rerr = UdpBind();
+00242                 if (iLocalRole == (TUint32)KPanGnUUID)
+00243                         {
+00244                         ReceiveRemoteName();
+00245                         }
+00246                 else
+00247                         {
+00248                 
+00249                         TPckgBuf<TInt> buff = 1;
+00250                         TRequestStatus status;
+00251                         TInetAddr addr(KDealerIpAddr,KGenericPort);
+00252                         iSocket.SendTo(buff, addr, 0, status);
+00253                         User::WaitForRequest(status); 
+00254                         iConsole.Printf(_L("\nJoined the PAN, press ENTER to continue..."));
+00255                         }
+00256                         
+00257                 
+00258                 }
+00259                 
+00260 
+00261         }
+00262 
+00263 
+00268 void CPanConnections::RunL()
+00269         {
+00270         // Something wrong with RConnection start
+00271         if (iStatus != KErrNone)
+00272                 {
+00273                 // PRINT SOMETHING HERE
+00274                                 iConsole.Printf(_L("\nError eith iStatus : %d\n"), iStatus.Int());
+00275                 }
+00276         else
+00277                 {
+00278                 switch (iActiveMode)
+00279                         {
+00280                         case EIAPStart:
+00281                                 // Start the IAP
+00282                                 IapStarted();
+00283                         break;
+00284                 
+00285                         case ESocketAccept:
+00286                                 TcpIpListenComplete();
+00287                         break;
+00288                         
+00289                         case ESocketConnect:
+00290                                 TcpIpConnectionComplete();
+00291                         break;
+00292                 
+00293                         case EUdpReceive:
+00294                                 iRemoteNames.AppendL(iRemoteName);
+00295                                 iConsole.Printf(_L("\nReady to connect another device"));
+00296                         break;
+00297                 
+00298                         default:
+00299                         break;
+00300                         }
+00301 
+00302                 }
+00303 
+00304         }
+00305 
+00310 void CPanConnections::DoCancel()
+00311         {
+00312         CancelIap();
+00313         }
+00314                 
+00315 
+00316 
+00317 
+00321 TInt CPanConnections::StartConnection(TBTDevAddr* aDevAddr, TBool aUsePANNotifier, TBool aUseTcpTransport)
+00322         {
+00323         TInt err = KErrNone;
+00324         iUseTcpTransport = aUseTcpTransport;
+00325         
+00326         // Check if there is an existing IAP.
+00327         if(iIapLoading == EFalse && iIapStarted == EFalse)
+00328                 {
+00329                 // No existing connection so configure IAP in CommDb and start IAP,
+00330                 // this is done by overriding RConnection's preferences with our IAP
+00331                 // information
+00332                 TRAP(err, ConfigureIAPL(ETrue, aDevAddr, aUsePANNotifier));
+00333                 if(err != KErrNone)
+00334                         {
+00335                         iConsole.Printf(_L("IAP Configuration Failed - %d.\n"), err);
+00336                         }
+00337                 else
+00338                         {
+00339                         err = StartIAP();
+00340                         if(err != KErrNone)
+00341                                 {
+00342                                 iConsole.Printf(_L("Failed to start IAP - %d.\n"), err);
+00343                                 }
+00344                         }
+00345                 }
+00346         else
+00347                 {
+00348                 #ifdef __NO_CONTROL_DURING_START__
+00349                         if(iIapLoading && aDevAddr != NULL)
+00350                                 {
+00351                                 // A new connection is required but a listening IAP is pending.
+00352                                 Cancel();
+00353                                 iConnection.Close();
+00354                         
+00355                                 TRAP(err, ConfigureIAPL(ETrue, aDevAddr, aUsePANNotifier));
+00356                                 if(err != KErrNone)
+00357                                         {
+00358                                         iConsole.Printf(_L("IAP Configuration Failed - %d.\n"), err);
+00359                                         }
+00360                                 else
+00361                                         {
+00362                                         err = StartIAP();
+00363                                         if(err != KErrNone)
+00364                                                 {
+00365                                                 iConsole.Printf(_L("Failed to start IAP - %d.\n"), err);
+00366                                                 }
+00367                                         }
+00368                                 return err;
+00369                                 }
+00370                 #endif                  
+00371                 // An IAP already exists.
+00372                 // Make sure that we can store another connection
+00373                 if(iActiveConnections.Count() == 7)
+00374                         {
+00375                         err = KErrOverflow;
+00376                         }
+00377                 else if(aDevAddr)
+00378                         {
+00379                         // Add the BT address of the device to the existing RConnection
+00380                         iControlAddr = *aDevAddr;
+00381                         TPtr8 ptr = iControlAddr.Des();
+00382                         err = iConnection.Control(KCOLAgent, KCOAgentPanConnectDevice, ptr);
+00383                         if(err != KErrNone)
+00384                                 {
+00385                                 __DEBUGGER();
+00386                                 }
+00387                         if (!iUseTcpTransport)
+00388                                 {
+00389                                 ReceiveRemoteName();
+00390                                 }
+00391                         }
+00392                 }       
+00393         return err;
+00394         }
+00395         
+00396         
+00400 TInt CPanConnections::StartIAP()
+00401         {
+00402         TInt rerr = KErrNone;
+00403         // Create a set of connection preferences to override existing
+00404         // set in CommDb when RConnection::Start is called.
+00405         TCommDbConnPref connPref;
+00406         PanProfileIAPs iap(EGeneralIAP);
+00407                 
+00408         connPref.SetIapId(iap);
+00409         connPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
+00410         
+00411         // load BT stack        
+00412         rerr = Initialise();
+00413         if(rerr == KErrNone)
+00414                 {
+00415                 // Open the connection
+00416                 rerr = iConnection.Open(iSockSvr);
+00417                 if(rerr == KErrNone)
+00418                         {
+00419                         // Start the connection
+00420                         // use the iStatus of active object, if it doesn't complete (we are listening)
+00421                         // then the active object deals with it, otherwise AO calls IapStarted function on this class
+00422                         // Something wrong with RConnection start
+00423                         iActiveMode = EIAPStart;        
+00424                         iConnection.Start(connPref, iStatus);
+00425                         SetActive();
+00426                         iIapLoading = ETrue;
+00427                         }
+00428                 }
+00429         return rerr;
+00430         }
+00431         
+00432         
+00437 void CPanConnections::ConfigureIAPL(TBool aIsListening, TBTDevAddr* aDevAddr, TBool aUsePANNotifier)
+00438         {
+00439         // Open CommDb and get a view of the PAN service extentions table.
+00440         CCommsDatabase* db = CCommsDatabase::NewL();
+00441         CleanupStack::PushL(db);
+00442         CCommsDbTableView* tableView = db->OpenTableLC(TPtrC(PAN_SERVICE_EXTENSIONS));
+00443 
+00444         TBuf<KMaxBufferSize> tableName;
+00445         TInt err = tableView->GotoFirstRecord();
+00446         if(err == KErrNone)
+00447                 {
+00448                 tableView->ReadTextL(TPtrC(COMMDB_NAME), tableName);
+00449                 if(tableName == TPtrC(_S("PANServiceExtensionsTable1")))
+00450                         {
+00451                         
+00452                         User::LeaveIfError(tableView->UpdateRecord());// Start update
+00453                         //enable listening mode 
+00454                         tableView->WriteBoolL(TPtrC(PAN_ALLOW_INCOMING),aIsListening);
+00455                         tableView->WriteBoolL(TPtrC(PAN_DISABLE_SDP_QUERY), 0);
+00456 
+00457                         tableView->WriteBoolL(TPtrC(PAN_PROMPT_FOR_REMOTE_DEVICES), aUsePANNotifier);
+00458 
+00459                         if(aDevAddr)
+00460                                 {
+00461                                 TBuf<KMaxBufferSize> buf;
+00462                                 aDevAddr->GetReadable(buf);
+00463 
+00464                                 tableView->WriteTextL(TPtrC(PAN_PEER_MAC_ADDRESSES), buf);
+00465                                 }
+00466                         else
+00467                                 {
+00468                                 tableView->WriteTextL(TPtrC(PAN_PEER_MAC_ADDRESSES), _L(""));
+00469                                 }
+00470                         // Finalise changes made.
+00471                         User::LeaveIfError(tableView->PutRecordChanges());// Finish update
+00472                         User::LeaveIfError(db->CommitTransaction());
+00473 
+00474                         CleanupStack::PopAndDestroy(2); // db & tableView
+00475                         return;
+00476                         }
+00477                 }
+00478         User::Leave(KErrNotFound);
+00479         }
+00480         
+00481         
+00487 void CPanConnections::SetLocalIpAddrL(TUint32 addr)
+00488         {
+00489         iLocalIpAddr = addr;
+00490         iSrcAddr.SetAddress(iLocalIpAddr);
+00491         CCommsDatabase* db = CCommsDatabase::NewL();
+00492         CleanupStack::PushL(db);
+00493         // Get the LAN service table
+00494         CCommsDbTableView* tableView = db->OpenTableLC(TPtrC(LAN_SERVICE));
+00495 
+00496         TBuf<KMaxBufferSize> tableName;
+00497         TInt err = tableView->GotoFirstRecord();
+00498         if(err == KErrNone)
+00499                 {
+00500                 // Get the name of the table
+00501                 tableView->ReadTextL(TPtrC(COMMDB_NAME), tableName);
+00502                 if(tableName == TPtrC(_S("BluetoothPANProfile")))
+00503                         {
+00504                         TInetAddr tempAddr;
+00505                         TBuf<KMaxBufferSize> dispBuf;
+00506                         tempAddr.SetAddress(iLocalIpAddr);
+00507                         tempAddr.Output(dispBuf);
+00508                         
+00509                         User::LeaveIfError(tableView->UpdateRecord());  
+00510                         tableView->WriteTextL(_L("IpAddr"), dispBuf);
+00511                 
+00512                         User::LeaveIfError(tableView->PutRecordChanges());
+00513                         User::LeaveIfError(db->CommitTransaction());    
+00514                         }
+00515                 }
+00516         else
+00517                 {
+00518                 User::Leave(KErrNotFound);
+00519                 }
+00520         CleanupStack::PopAndDestroy(2);//db & tableView 
+00521         }
+00522         
+00523         
+00524         
+00530 void CPanConnections::SetFixedRoleL(TSide aSide, TUint aRole)
+00531         {
+00532         if (aSide == ELocalRole)
+00533                 iLocalRole = aRole;
+00534         else
+00535                 iPeerRole = aRole;
+00536         
+00537         CCommsDatabase* db = CCommsDatabase::NewL();
+00538         CleanupStack::PushL(db);
+00539         CCommsDbTableView* tableView = db->OpenTableLC(TPtrC(PAN_SERVICE_EXTENSIONS));
+00540 
+00541         TBuf<KMaxBufferSize> tableName;
+00542         TInt err = tableView->GotoFirstRecord();
+00543         if(err == KErrNone)
+00544                 {
+00545                 tableView->ReadTextL(TPtrC(COMMDB_NAME), tableName);
+00546                 if(tableName == TPtrC(_S("PANServiceExtensionsTable1")))
+00547                         {
+00548                         User::LeaveIfError(tableView->UpdateRecord());  
+00549                         if(aSide == ELocalRole)
+00550                                 {
+00551                                 tableView->WriteBoolL(TPtrC(PAN_LOCAL_ROLE), aRole);
+00552                                 }
+00553                         else 
+00554                                 {
+00555                                 tableView->WriteBoolL(TPtrC(PAN_PEER_ROLE), aRole);
+00556                                 }
+00557                         User::LeaveIfError(tableView->PutRecordChanges());
+00558                         User::LeaveIfError(db->CommitTransaction());
+00559 
+00560                         CleanupStack::PopAndDestroy(2);
+00561                         return;
+00562                         }
+00563                 }
+00564         User::Leave(KErrNotFound);
+00565         }
+00566         
+00571 void CPanConnections::PrintIAPL()
+00572         {
+00573         CCommsDatabase* db = CCommsDatabase::NewL();
+00574         CleanupStack::PushL(db);
+00575         CCommsDbTableView* tableView = db->OpenTableLC(TPtrC(PAN_SERVICE_EXTENSIONS));
+00576 
+00577         TInt err = tableView->GotoFirstRecord();
+00578         
+00579         if(err == KErrNone)
+00580                 {
+00581                 // Print the IAP
+00582                 TUint32 uVal;
+00583                 TBool bVal;
+00584                 TBuf<KMaxBufferSize> sVal;
+00585         
+00586 
+00587                 iConsole.Printf(_L("------------------- CURRENT IAP ------------------\n"));
+00588                 
+00589                 tableView->ReadUintL(TPtrC(PAN_LOCAL_ROLE), uVal);
+00590                 iConsole.Printf(_L("Local Role: %d, "), uVal);                  
+00591                 tableView->ReadUintL(TPtrC(PAN_PEER_ROLE), uVal);
+00592                 iConsole.Printf(_L("Peer Role: %d\n"), uVal);
+00593                 tableView->ReadBoolL(TPtrC(PAN_PROMPT_FOR_REMOTE_DEVICES), bVal);
+00594                 iConsole.Printf(_L("Peer Prompt: %d, "), bVal);                 
+00595                 tableView->ReadBoolL(TPtrC(PAN_DISABLE_SDP_QUERY), bVal);
+00596                 iConsole.Printf(_L("Disable SDP: %d, "), bVal);                 
+00597                 tableView->ReadBoolL(TPtrC(PAN_ALLOW_INCOMING), bVal);
+00598                 iConsole.Printf(_L("Listening: %d\n"), bVal);                   
+00599                 tableView->ReadTextL(TPtrC(PAN_PEER_MAC_ADDRESSES), sVal);
+00600                 iConsole.Printf(_L("Peer MAC Addr: %S\n"), &sVal);
+00601 
+00602                 iConsole.Printf(_L("--------------------------------------------------\n"));
+00603                 CleanupStack::PopAndDestroy(2);
+00604                 return;
+00605                 }
+00606         User::Leave(KErrNotFound);
+00607         }
+00608 
+00609 
+00613 TInt CPanConnections::StopIAP()
+00614         {
+00615         TInt rerr = KErrNone;
+00616         if(iIapLoading)
+00617                 {
+00618                 Cancel();
+00619                 }
+00620         else
+00621                 {
+00622                 if(iIapStarted)
+00623                         {
+00624                         rerr = iConnection.Stop();
+00625                         if(rerr != KErrNone)
+00626                                 {
+00627                                 iConsole.Printf(_L("Failed to STOP IAP.  Err %d.\n"), rerr);
+00628                                 }
+00629                         iIapStarted = EFalse;
+00630                         }
+00631                 }
+00632         return rerr;
+00633         }
+00634         
+00635         
+00640 void CPanConnections::CancelIap()
+00641         {
+00642         iIapLoading = EFalse;
+00643         iConnection.Stop();
+00644         }
+00645         
+00652 TInt CPanConnections::StopConnection()
+00653         {
+00654         TInt rerr = KErrNone;
+00655         if(iActivePanConn >= iActiveConnections.Count())
+00656                 {
+00657                 rerr = KErrArgument;
+00658                 }
+00659         else if(iActivePanConn != KErrNotFound)
+00660                 {
+00661                 // get the deivce we wish to disconnect
+00662                 TPtr8 ptr = iActiveConnections[iActivePanConn].Des();
+00663                 rerr = iConnection.Control(KCOLAgent, KCOAgentPanDisconnectDevice, ptr);
+00664                 if(rerr != KErrNone)
+00665                         {
+00666                         __DEBUGGER();
+00667                         }
+00668                 }
+00669         else 
+00670                 {
+00671                 rerr = KErrNotFound;
+00672                 }
+00673         return rerr;
+00674         }
+00675         
+00676         
+00682 TInt CPanConnections::SetActivePanConn(TUint aConnIndex)
+00683         {
+00684         TInt rerr = KErrArgument;
+00685         
+00686         if((TInt)aConnIndex < iActiveConnections.Count())
+00687                 {
+00688                 iActivePanConn = aConnIndex;
+00689                 rerr = KErrNone;
+00690                 }
+00691         return rerr;
+00692         }
+00693 
+00694 
+00695 
+00700 TInt CPanConnections::UpdateCurrentConnections()
+00701         {
+00702         TInt rerr = KErrNone;
+00703         HBufC8* buffer=0;
+00704         const TInt KAddrLen = sizeof(TBTDevAddr);
+00705 
+00706         TRAP(rerr, buffer = HBufC8::NewL(7*KAddrLen));// create a buffer to house the device address
+00707         if(rerr == KErrNone)
+00708                 {
+00709                 TPtr8 ptr = buffer->Des();
+00710                 // use RConnection to enumerate all the devices
+00711                 rerr = iConnection.Control(KCOLAgent, KCOAgentPanEnumerateDevices, ptr);
+00712                 if(rerr == KErrNone)
+00713                         {
+00714                         iActiveConnections.Reset();
+00715                         while(ptr.Length()>=KBTDevAddrSize)
+00716                                 {
+00717                                 // inspect the addr's in the descriptor and append them to the array.
+00718                                 TBTDevAddr parsedAddr(ptr.Mid(ptr.Length()-KAddrLen, KBTDevAddrSize));
+00719                                 ptr.SetLength(Max(ptr.Length()-KAddrLen, 0));
+00720                                 iActiveConnections.Append(parsedAddr);
+00721                                 }
+00722                         }
+00723                 }
+00724         delete buffer;
+00725         return rerr;
+00726         }
+00727 
+00731 void CPanConnections::PrintConns()
+00732         {
+00733         iConsole.Printf(_L("\nRemotenames : %d\n"), iRemoteNames.Count());
+00734         PrintTcpSocketConnections();
+00735         iConsole.Printf(_L("--------------------------------------------------\n"));
+00736         iConsole.Printf(_L("Listening Mode: "));
+00737         iListening ? iConsole.Printf(_L("Enabled        ")) : iConsole.Printf(_L("Disabled        "));
+00738         if(iIapStarted)
+00739                 {
+00740                 iConsole.Printf(_L("       IAP Started"));
+00741 
+00742                 TInt err = UpdateCurrentConnections();
+00743                 if(err != KErrNone)
+00744                         {
+00745                         iConsole.Printf(_L("FAILED TO ENUMERATE CONNECTIONS. Err %d\n"), err);
+00746                         }
+00747                 else
+00748                         {
+00749                         iConsole.Printf(_L("\n------------------ CONNECTIONS -------------------\n"));
+00750                         iConsole.Printf(_L("Index  Remote Addr\n"));
+00751                         
+00752                         
+00753                         for(TInt i=0;i<iActiveConnections.Count();i++)
+00754                                 {
+00755                                 TBuf<KMaxBufferSize> dispBuf;
+00756                                 iActiveConnections[i].GetReadable(dispBuf);
+00757 
+00758                                 if(iActivePanConn == i)
+00759                                         {
+00760                                         iConsole.Printf(_L("-> "));
+00761                                         }
+00762                                 else
+00763                                         {
+00764                                         iConsole.Printf(_L("   "));
+00765                                         }
+00766                                 iConsole.Printf(_L("%d   0x%S\n"), i, &dispBuf);                                
+00767                                 }
+00768                         }
+00769                 }
+00770         
+00771         if(iLocalIpAddr == 0)
+00772                 {
+00773                 iConsole.Printf(_L("\nNo local IP address set\n"));
+00774                 }
+00775         else
+00776                 {
+00777                 TInetAddr tempAddr;
+00778                 TBuf<KMaxBufferSize> dispBuf;
+00779                 
+00780                 tempAddr.SetAddress(iLocalIpAddr);
+00781                 tempAddr.Output(dispBuf);
+00782                 
+00783                 iConsole.Printf(_L("\nLocal IP address: %S\n"), &dispBuf);
+00784                 }
+00785 
+00786         }
+00787 
+00791 TUint32 CPanConnections::GetLocalIpAddr()
+00792         {
+00793         return iLocalIpAddr;
+00794         }
+00795 
+00796 
+00797         
+00802 void CPanConnections::TcpIpConnectionComplete()
+00803         {
+00804         if (iStatus == KErrNone)
+00805                 {
+00806                 iConsole.Printf(_L("\nSuccessfully joined PAN\n"));
+00807                 }
+00808         else
+00809                 {
+00810                 iConsole.Printf(_L("Join PAN Failed!, Socket error : %d"), iStatus.Int());
+00811                 }
+00812 
+00813         }
+00814         
+00815         
+00816 
+00817         
+00821 void CPanConnections::ConnectDoCancel()
+00822         {
+00823         iConsole.Printf(_L("TCP/IP connection Canceled!\n"));
+00824         iSocket.CancelConnect();
+00825         }
+00826         
+00831 RArray<TInetAddr>& CPanConnections::RemoteNames()
+00832         {
+00833         return iRemoteNames;
+00834         }
+00835         
+00836 RSocket& CPanConnections::GetSocket()
+00837         {
+00838         return iSocket;
+00839         }
+00840         
+00841 RArray<RSocket>& CPanConnections::GetSocketArray()
+00842         {
+00843         return iSocketArray;
+00844         }
+00845         
+00852 void CPanConnections::TcpIpListenComplete()
+00853         {
+00854         // iPanEstablished ????
+00855         if (iStatus == KErrNone)
+00856                 iConsole.Printf(_L("Player Joined\n"));
+00857         else
+00858                 iConsole.Printf(_L("TCP socket failed : %d\n"), iStatus.Int());
+00859         // Call accept for the next device to connect to
+00860         TInt err = AcceptNewSocket();
+00861         if (err!=KErrNone)
+00862                 iConsole.Printf(_L("AcceptNewSocket() Failed : %d\n"),err);
+00863         }
+00864         
+00865         
+00870 void CPanConnections::ListenDoCancel()
+00871         {
+00872         iConsole.Printf(_L("\nTCP/IP accept Canceled!\n"));
+00873         iSocket.CancelAccept(); 
+00874         }
+00875         
+00876         
+00881 void CPanConnections::SetRemoteIpAddr(TUint32 aAddr)
+00882         {
+00883         iRemoteIpAddr = aAddr;
+00884         }
+00885 
+00889 TUint32 CPanConnections::GetRemoteIpAddr() const
+00890         {
+00891         return iRemoteIpAddr;
+00892         }
+00893         
+00894 TInt CPanConnections::UdpBind()
+00895         {
+00896         iSrcAddr.SetPort(KGenericPort);
+00897         iSrcAddr.SetAddress(iLocalIpAddr);
+00898         return iSocket.Bind(iSrcAddr);
+00899         }
+00900         
+00901 // This function will be used if a PAN-Gn
+00902 void CPanConnections::ReceiveRemoteName()
+00903         {
+00904         // Attempt to receive, this will happen one by one due to authorisation 
+00905 //      if (iRemoteNames.Count() < iActiveConnections.Count())
+00906 //              {
+00907                 
+00908                 iActiveMode = EUdpReceive;
+00909                 iSocket.RecvFrom(iBuff,iRemoteName, 0, iStatus);
+00910                 SetActive();
+00911 //              }
+00912 //      else
+00913 //              {
+00914 //              iConsole.Printf(_L("\nAll devices have told us:!!!!!!"));
+00915 //              }
+00916         }
+00917 
+00918 void CPanConnections::CancelRecvFrom()
+00919         {
+00920         iSocket.CancelRecv();
+00921         }
+00926 TInt CPanConnections::TcpIpBindAndConnect()
+00927         {
+00928         TInt rerr;
+00929 
+00930         if (iRemoteIpAddr == 0)
+00931                 {
+00932                 iConsole.Printf(_L("You need to set a destination IP address.\n"));
+00933                 return (KErrArgument);
+00934                 }
+00935                 
+00936         if (!IsActive()/* && iTcpIpMode != ETcpReceive*/)
+00937                 {
+00938                 // Setup the TInetAddrs inc. port number
+00939                 iDstAddr.SetPort(KGenericPort);
+00940                 iDstAddr.SetAddress(iRemoteIpAddr);
+00941                 iSrcAddr.SetPort(KGenericPort);
+00942                 iSrcAddr.SetAddress(iLocalIpAddr);
+00943 
+00944                 rerr = iSocket.Bind(iSrcAddr);// Bind the socket
+00945                 if (rerr != KErrNone)
+00946                         {
+00947                         iConsole.Printf(_L("Failed : %d"), rerr);
+00948                         return (rerr);
+00949                         }
+00950                 iActiveMode = ESocketConnect; // Set the mode for the active object
+00951                 iSocket.Connect(iDstAddr, iStatus);// Attempt connection to dest. addr
+00952                 SetActive();
+00953                 }
+00954         return KErrNone;
+00955         }
+00956         
+00960 TInt CPanConnections::TcpIpBindAndListen()
+00961         {
+00962         TInt rerr;
+00963         iDstAddr.SetPort(KGenericPort);
+00964         iDstAddr.SetAddress(iRemoteIpAddr);
+00965         iSrcAddr.SetPort(KGenericPort);
+00966         iSrcAddr.SetAddress(iLocalIpAddr);
+00967         
+00968         rerr = iSocket.Bind(iSrcAddr);
+00969         if (rerr != KErrNone)
+00970                 {
+00971                 iConsole.Printf(_L("Failed to bind with socket : %d"), rerr);
+00972                 return (rerr);
+00973                 }
+00974         rerr = iSocket.Listen(7);// maximum devices is 7
+00975         if (rerr != KErrNone)
+00976                 {
+00977                 iConsole.Printf(_L("Failed to listen on socket : %d"), rerr);
+00978                 return (rerr);
+00979                 }
+00980         return (KErrNone);
+00981         }
+00982 
+00990 TInt CPanConnections::AcceptNewSocket()
+00991         {
+00992 
+00993 // Accept new socket will only be called if using TCP sockets   
+00994         TInt currentSocketCount = iSocketArray.Count();
+00995         TInt rerr;
+00996         if (currentSocketCount == iActiveConnections.Count())
+00997                 {
+00998                 // We already have the same number of connections as 
+00999                 // devices on the PAN, move on to dish out player numbers
+01000                 iConsole.Printf(_L("\nAll players on PAN have joined!!\n"));
+01001                 TRequestStatus status;
+01002                 for (TInt i=0;i<iSocketArray.Count();i++)
+01003                         {
+01004                         status = KRequestPending;
+01005                 //      iPlayerNum = i;
+01006                 //      iSocketArray[i].Send(iPlayerNum, 0, status);
+01007                         User::WaitForRequest(status);
+01008                         if (status != KErrNone)
+01009                                 {
+01010                                 // We have failed to send player number to the player
+01011                                 // Remove them from the socket array and the network
+01012                                 iSocketArray[i].Close();
+01013                                 iSocketArray.Remove(i);
+01014                                 TPtr8 ptr = iActiveConnections[i].Des();
+01015                                 iConnection.Control(KCOLAgent, KCOAgentPanDisconnectDevice, ptr);
+01016                                 i--;
+01017                                 }
+01018                         }
+01019                 ListenDoCancel();// Cancel any oustanding Accepts.
+01020                 return (KErrNone);
+01021                 }
+01022         else
+01023                 {
+01024                 RSocket socket;
+01025                 iSocketArray.Append(socket);
+01026                 TInt last = iSocketArray.Count()-1;
+01027                 rerr = iSocketArray[last].Open(iSockSvr);
+01028                 if (rerr != KErrNone)
+01029                         {
+01030                         iConsole.Printf(_L("Failed to open 'Service' Socket : %d"), rerr);
+01031                         return (rerr);
+01032                         }
+01033                 iActiveMode = ESocketAccept;// set the mode for the active object 
+01034                 iSocket.Accept(iSocketArray[last], iStatus);
+01035                 SetActive();    
+01036                 return (rerr);
+01037                 }
+01038         }
+01039         
+01043 TInt CPanConnections::CloseTcpIpSocketConnect()
+01044         {
+01045         TRequestStatus status;
+01046         // Shutdown immediately
+01047         iSocket.Shutdown(RSocket::EImmediate, status);
+01048         User::WaitForRequest(status);
+01049         iConsole.Printf(_L("\nConnection closed with error: %d"), status.Int());
+01050         return (status.Int());
+01051         }
+01052         
+01057 TInt CPanConnections::CloseAllTcpIpSockets()
+01058         {
+01059         TRequestStatus status;
+01060         for (TInt i=0; i<iSocketArray.Count(); i++)
+01061                 {
+01062                 iSocketArray[i].Shutdown(RSocket::EImmediate, status);
+01063                 User::WaitForRequest(status);
+01064                 iConsole.Printf(_L("\nConnection to %d closed with error: %d"), i, status.Int());
+01065                 }
+01066         return (KErrNone); 
+01067         }
+01068         
+01072 TInt CPanConnections::CloseCurrentTcpIpSocket()
+01073         {
+01074         TRequestStatus status;
+01075         iSocketArray[iActiveTcpSocket].Shutdown(RSocket::EImmediate, status);
+01076         User::WaitForRequest(status);
+01077         iConsole.Printf(_L("\nConnection closed with error: %d"), status.Int());
+01078         return (status.Int());
+01079         }
+01080         
+01084 void CPanConnections::PrintTcpSocketConnections()
+01085         {
+01086         iConsole.Printf(_L("---------------- UDP CONNECTIONS -----------------\n"));
+01087         iConsole.Printf(_L("Devices on Piconet\n"));
+01088 //      if (iSocketArray.Count() <= 1 && IsActive())
+01089 //              {
+01090 //              // We only have one socket, make sure it is connected
+01091                 // as it might still be accepting.
+01092 //              iConsole.Printf(_L("\n                  NO DEVICES\n"));
+01093 //              }
+01094 //      else
+01095 //              {
+01096                 TInt count;
+01097                 if (IsActive() && iActiveMode == ESocketAccept)
+01098                         count = iRemoteNames.Count()-1;
+01099                 else
+01100                         count = iRemoteNames.Count();
+01101                 for(TInt i=0;i<count;i++)
+01102                         {
+01103                         TBuf<KMaxBufferSize> dispBuf;
+01104                         //TInetAddr address;
+01105                 //      iRemoteNames[i].RemoteName(address);
+01106                         iRemoteNames[i].Output(dispBuf);
+01107                         iConsole.Printf(_L("            %d - %S\n"), i, &dispBuf);                              
+01108                         }
+01109 //              }
+01110         iConsole.Printf(_L("--------------------------------------------------\n"));
+01111         }
+01112         
+01116 TInt CPanConnections::GetLocalRole() const
+01117         {
+01118         return (iLocalRole);
+01119         }
+01120         
+01121 
+01122 
+01126 TInt CPanConnections::ActiveConnCount()
+01127         {
+01128         return iActiveConnections.Count();
+01129         }
+01130 
+01131 
+01132 void CPanConnections::CancelAllOperations()
+01133         {
+01134         for (TInt i=0;i<iSocketArray.Count();i++)
+01135                 {
+01136                 iSocketArray[i].CancelAll();
+01137                 }
+01138         }
+

Generated on Thu Jan 21 10:32:57 2010 for TB10.1 Example Applications by  + +doxygen 1.5.3
+ +