void XQAccessPointManagerPrivate::storeWPADataL(const TInt aIapId, const TDesC& aPresharedKey, const XQWLAN& aWlan)
{
    CCommsDbTableView* wLanServiceTable;
    
    CApUtils* apUtils = CApUtils::NewLC(*ipCommsDB);
    TUint32 iapId = apUtils->IapIdFromWapIdL(aIapId);
    CleanupStack::PopAndDestroy(apUtils);

    TUint32 serviceId;

    CCommsDbTableView* iapTable = ipCommsDB->OpenViewMatchingUintLC(TPtrC(IAP),
                                                                    TPtrC(COMMDB_ID),
                                                                    iapId);
    User::LeaveIfError(iapTable->GotoFirstRecord());
    iapTable->ReadUintL(TPtrC(IAP_SERVICE), serviceId);
    CleanupStack::PopAndDestroy( iapTable );

    wLanServiceTable = ipCommsDB->OpenViewMatchingUintLC(TPtrC(XQ_WLAN_SERVICE),
                                                         TPtrC(XQ_WLAN_SERVICE_ID),
                                                         serviceId );
    TInt errorCode = wLanServiceTable->GotoFirstRecord();
    if (errorCode == KErrNone) {
        User::LeaveIfError(wLanServiceTable->UpdateRecord());
    } else {
        TUint32 dummyUid(0);
        User::LeaveIfError(wLanServiceTable->InsertRecord(dummyUid));
        wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_SERVICE_ID), aIapId);
    }
    CleanupCancelPushL(*wLanServiceTable);

    TBool usesPsk(aWlan.usesPreSharedKey());
    // Save WPA Mode
    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_ENABLE_WPA_PSK), usesPsk); 
    
    // Save security mode
    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_SECURITY_MODE),
                                 fromQtSecurityModeToS60SecurityMode(aWlan.securityMode())); 

    // Save PreShared Key
    TBuf8<KWpaKeyMaxLength> keyWPA;
    
    //convert to 8 bit
    keyWPA.Copy(aPresharedKey);
    wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_WPA_PRE_SHARED_KEY),keyWPA);

    // Check and save PreShared Key Length
    TInt len(keyWPA.Length());
        
    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_WPA_KEY_LENGTH),len);

    User::LeaveIfError(wLanServiceTable->PutRecordChanges());

    CleanupStack::Pop(wLanServiceTable); // table rollback...
    CleanupStack::PopAndDestroy(wLanServiceTable);
}
TUint32 XQAccessPointManagerPrivate::createAccessPointL(const TDesC& aAccessPointName,
                                                          const XQWLAN& aWlan, 
                                                          const TDesC& aPresharedKey)
{
    TUint32 iapId;

    if (isS60VersionGreaterThan3_1()) {
        if (fromQtSecurityModeToS60SecurityMode(aWlan.securityMode()) != EOpen )
            {
            User::LeaveIfError(ipCommsDB->BeginTransaction());
            }
    } else {
        User::LeaveIfError(ipCommsDB->BeginTransaction());
    }

    CleanupRollbackPushL(*ipCommsDB);
    
    iapId = makeIapL(aAccessPointName, aWlan);
    switch (fromQtSecurityModeToS60SecurityMode(aWlan.securityMode()))
        {
        case EWep:
            {
            storeWEPDataL(iapId, aPresharedKey);
            break;
            }
        case E802_1x:
        case EWpa:
        case EWpa2:
            {
            storeWPADataL(iapId, aPresharedKey, aWlan);
            break;
            }
        case EOpen: // fall through on purpose though we're done for open network
        default:
            break;
            }

    CleanupStack::Pop(); // db rollback..

    if (isS60VersionGreaterThan3_1()) { 
        if (fromQtSecurityModeToS60SecurityMode(aWlan.securityMode()) != EOpen) {
            ipCommsDB->CommitTransaction();
        }
    } else {
        ipCommsDB->CommitTransaction();
    }

    CApUtils* apUtils = CApUtils::NewLC(*ipCommsDB);
    TUint32 iapTableiapId = apUtils->IapIdFromWapIdL(iapId);
    CleanupStack::PopAndDestroy( apUtils );
    return iapTableiapId;   
}
void CJavaDebugAgentConnector::ConnectL()
{
    if (iState == EIdle)
    {
        TDesC* host = iDriver->GetPCHost();
        if (host && host->Length() > 0)
        {
            // NOTE: iApId is apparently so called "WAP id". It's different
            // from Internet AP id, which is what TCommDbConnPref wants.
            TUint32 iap = KNoAccessPoint;
            TBuf<KCommsDbSvrMaxFieldLength> apName;

            if (iApId != KNoAccessPoint)
            {
                // We will need the connection
                User::LeaveIfError(iConnection.Open(SocketServ(), KAfInet));

                // Get IAP id and connection name
                CCommsDatabase* db = CCommsDatabase::NewL();
                CleanupStack::PushL(db);
                CApUtils* apUtils = CApUtils::NewLC(*db);
                iap = apUtils->IapIdFromWapIdL(iApId);
                apUtils->NameL(iApId, apName);
                CleanupStack::PopAndDestroy(2); // apUtils, db
            }
            if (iap == KNoAccessPoint || AlreadyConnected(iap))
            {
                DoResolveL();
            }
            else
            {
                // Initiate the connection
                LOG1("Starting %S connection...",&apName);
                iState = EStarting;
                iStatus = KRequestPending;
                SetActive();

                iConnPref.SetIapId(iap);
                iConnPref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
                iConnection.Start(iConnPref, iStatus);
            }
        }
        else
        {
            iState = EFailed;
            iDriver->Log(_L("PC host is not set!"));
            User::LeaveIfError(KErrArgument);
        }
    }
}
void XQAccessPointManagerPrivate::storeWEPDataL(const TInt aIapId, const TDesC& aPresharedKey)
{
    CCommsDbTableView* wLanServiceTable;

    CApUtils* apUtils = CApUtils::NewLC(*ipCommsDB);
    TUint32 iapId = apUtils->IapIdFromWapIdL(aIapId);
    CleanupStack::PopAndDestroy(apUtils);

    TUint32 serviceId;

    CCommsDbTableView* iapTable = ipCommsDB->OpenViewMatchingUintLC(TPtrC(IAP),
                                                                    TPtrC(COMMDB_ID),
                                                                    iapId);
                            
    User::LeaveIfError(iapTable->GotoFirstRecord());
    iapTable->ReadUintL(TPtrC(IAP_SERVICE), serviceId);
    CleanupStack::PopAndDestroy(iapTable);

    wLanServiceTable = ipCommsDB->OpenViewMatchingUintLC(TPtrC(XQ_WLAN_SERVICE),
                                                         TPtrC(XQ_WLAN_SERVICE_ID),
                                                         serviceId);
    TInt errorCode = wLanServiceTable->GotoFirstRecord();
    if (errorCode == KErrNone) {
        User::LeaveIfError(wLanServiceTable->UpdateRecord());
    }
    else {
        TUint32 dummyUid = 0;
        User::LeaveIfError(wLanServiceTable->InsertRecord(dummyUid));
        wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_SERVICE_ID), aIapId);
    }
    
    CleanupCancelPushL(*wLanServiceTable);

    // Save index of key in use
    TUint32 keyInUse(KFirstWepKey);
    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_WEP_INDEX), keyInUse);

    // Save authentication mode
    TUint32 auth(0); // set to open... 
    if (isS60VersionGreaterThan3_1()) {  
        //TODO: wLanServiceTable->WriteUintL(TPtrC(NU_WLAN_AUTHENTICATION_MODE), auth);
    } else {
        wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_AUTHENTICATION_MODE), auth);
    }
    // not we need to convert the key.... to 8bit and to hex... and again detect the required bits..
    TBuf8<KMaxWepKeyLen> key;
    
    //convert to 8 bit
    key.Copy(aPresharedKey);

    TBool useHex(EFalse);
    TWepKeyLength keyLength;
    TBool validKey = validWepKeyLength(aPresharedKey, useHex, keyLength);
    
    if (!useHex) {
        // Must be converted to hexa and stored as a hexa
        // Ascii key is half the length of Hex
        HBufC8* buf8Conv = HBufC8::NewLC(key.Length() * 2);
        asciiToHex(key, buf8Conv);
        
        if (isS60VersionGreaterThan3_1()) {  
            wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_HEX_WEP_KEY1), buf8Conv->Des());
        } else {
            wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_WEP_KEY1), buf8Conv->Des());
        }
        CleanupStack::PopAndDestroy(buf8Conv);
    } else if (isHex(aPresharedKey)) {
        //already in hexa format
        if (isS60VersionGreaterThan3_1()) {  
            wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_HEX_WEP_KEY1), key);
        } else {
            wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_WEP_KEY1), key);
        }
    }
  
    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_WEP_KEY1_FORMAT), useHex);
  
    key.Zero();
    // write default values to the rest of the columns
    if (isS60VersionGreaterThan3_1()) {  
        wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_HEX_WEP_KEY2), 
                                     key);
    } else { 
        wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_WEP_KEY2), 
                                     key );
    }
    // Save third WEP key
    if (isS60VersionGreaterThan3_1()) {  
        wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_HEX_WEP_KEY3), 
                                     key);
    } else { 
        wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_WEP_KEY3), 
                                     key);
    }
    // Save fourth WEP key
    if (isS60VersionGreaterThan3_1()) {  
        //TODO: wLanServiceTable->WriteTextL(TPtrC(NU_WLAN_WEP_KEY4), 
        //                             key);
    } else {
        wLanServiceTable->WriteTextL(TPtrC(XQ_WLAN_WEP_KEY4), 
                                     key);
    }
    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_WEP_KEY2_FORMAT), 
                                 (TUint32&)useHex);

    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_WEP_KEY3_FORMAT), 
                                 (TUint32&)useHex);

    wLanServiceTable->WriteUintL(TPtrC(XQ_WLAN_WEP_KEY4_FORMAT), 
                                 (TUint32&)useHex);
 
    wLanServiceTable->PutRecordChanges();

    CleanupStack::Pop(wLanServiceTable); // table rollback...
    CleanupStack::PopAndDestroy(wLanServiceTable);
}