usbmgmt/usbmgr/device/classdrivers/ncm/classimplementation/ncmpktdrv/pktdrv/src/ncmsharedstatemanager.cpp
branchRCL_3
changeset 15 f92a4f87e424
equal deleted inserted replaced
14:d3e8e7d462dd 15:f92a4f87e424
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "ncmsharedstatemanager.h"
       
    19 #include "OstTraceDefinitions.h"
       
    20 #ifdef OST_TRACE_COMPILER_IN_USE
       
    21 #include "ncmsharedstatemanagerTraces.h"
       
    22 #endif
       
    23 
       
    24 
       
    25 
       
    26 #ifdef _DEBUG
       
    27 _LIT(KPanicSharedState, "SharedStatePanic");
       
    28 #endif
       
    29 
       
    30 CNcmSharedStateManager* CNcmSharedStateManager::NewL(MShareStateObserver& aEngine)
       
    31     {
       
    32     CNcmSharedStateManager* self = new(ELeave) CNcmSharedStateManager(aEngine);
       
    33     CleanupStack::PushL(self);
       
    34     self->ConstructL();
       
    35     CleanupStack::Pop(self);
       
    36     return self;
       
    37     }
       
    38 
       
    39 CNcmSharedStateManager::CNcmSharedStateManager(MShareStateObserver& aEngine) : CActive(CActive::EPriorityStandard),
       
    40                                           iNotifyMsg(RMessage2()), iEngine(aEngine)
       
    41     {
       
    42     CActiveScheduler::Add(this);
       
    43     }
       
    44 
       
    45 
       
    46 CNcmSharedStateManager::~CNcmSharedStateManager()
       
    47     {
       
    48     OstTraceFunctionEntry0(CNCMSHAREDSTATEMANAGER_CNCMSHAREDSTATEMANAGER);
       
    49 
       
    50     Cancel();
       
    51 
       
    52     iProperty.Close();
       
    53     iProperty.Delete(KKeyNcmConnectionEvent);
       
    54 
       
    55     delete iCoexistBearer;
       
    56     OstTraceFunctionExit0(CNCMSHAREDSTATEMANAGER_CNCMSHAREDSTATEMANAGER_DUP01);
       
    57     }
       
    58 
       
    59 
       
    60 void CNcmSharedStateManager::ConstructL()
       
    61     {
       
    62     OstTraceFunctionEntry0(CNCMSHAREDSTATEMANAGER_CONSTRUCTL);
       
    63 
       
    64     TInt  ccStartRequested  = ENcmStartRequested;
       
    65     User::LeaveIfError(RProperty::Get(KUsbmanSvrUid, KKeyNcmSharedState, ccStartRequested));
       
    66     if (ccStartRequested != ENcmStartRequested)
       
    67         {
       
    68         User::Leave(KErrNotReady);
       
    69         }
       
    70 
       
    71     User::LeaveIfError(iProperty.Define(KKeyNcmConnectionEvent, RProperty::EByteArray, KAllowAllPolicy, KNetworkControlPolicy));
       
    72     User::LeaveIfError(iProperty.Attach(RProcess().SecureId(), KKeyNcmConnectionEvent, EOwnerThread));
       
    73 
       
    74     iCoexistBearer = CNcmCoexistBearerWatcher::NewL();
       
    75 
       
    76     iNcmState().iIapId = 0;
       
    77     OstTraceFunctionExit0(CNCMSHAREDSTATEMANAGER_CONSTRUCTL_DUP01);
       
    78     }
       
    79 
       
    80 /**
       
    81 Maintains the shared state between the NCM class controller and packet driver.
       
    82 */
       
    83 void CNcmSharedStateManager::RunL()
       
    84     {
       
    85     TInt err = iStatus.Int();
       
    86 
       
    87     OstTrace1(TRACE_NORMAL, CNCMSHAREDSTATEMANAGER_RUNL, "iStatus.Int()=%d", iStatus.Int());
       
    88 
       
    89     if (KErrNone == err)
       
    90         {
       
    91         DoDhcpProvisionRequest();
       
    92         }
       
    93     else if (KErrCancel != err)
       
    94         {
       
    95         NotifyNcmDisconnected(err);
       
    96         }
       
    97     }
       
    98 
       
    99 /**
       
   100 Stop checking the shared state
       
   101 */
       
   102 void CNcmSharedStateManager::DoCancel()
       
   103     {
       
   104     iCoexistBearer->Cancel();
       
   105     }
       
   106 
       
   107 TInt CNcmSharedStateManager::NotifyDhcpProvisionRequested()
       
   108     {
       
   109     OstTraceFunctionEntry0(CNCMSHAREDSTATEMANAGER_NOTIFYDHCPPROVISIONREQUESTED);
       
   110 
       
   111     TInt ret = iCoexistBearer->SetIpOverUsbActive(iStatus);
       
   112 
       
   113     if (ret == KErrCompletion)
       
   114         {
       
   115         return DoDhcpProvisionRequest();
       
   116         }
       
   117     else if (ret == KErrNone)
       
   118         {
       
   119         //notify the upper apps BTPAN is used by others.
       
   120         NotifyNcmDisconnected(KErrInUse);
       
   121         SetActive();
       
   122         }
       
   123     else
       
   124         {
       
   125         NotifyNcmDisconnected(ret);
       
   126         }
       
   127 
       
   128     OstTraceFunctionEntry0(CNCMSHAREDSTATEMANAGER_NOTIFYDHCPPROVISIONREQUESTED_DUP01);
       
   129     return ret;
       
   130     }
       
   131 
       
   132 TInt CNcmSharedStateManager::DoDhcpProvisionRequest()
       
   133     {
       
   134     if (iNotifyMsg.IsNull())
       
   135         {
       
   136         __ASSERT_DEBUG(0, User::Panic(KPanicSharedState, __LINE__));
       
   137 
       
   138         //return AlreadyExists, so that Engine can start immediately.
       
   139         return KErrAlreadyExists;
       
   140         }
       
   141     else
       
   142         {
       
   143         iNotifyMsg.Complete(KErrNone);
       
   144         iNotifyMsg = RMessage2();
       
   145         }
       
   146 
       
   147     return KErrNone;
       
   148     }
       
   149 
       
   150 
       
   151 void CNcmSharedStateManager::NotifyNcmConnected()
       
   152     {
       
   153     iNcmState().iState = ENcmStateConnected;
       
   154     iProperty.Set(iNcmState);
       
   155     }
       
   156 
       
   157 void CNcmSharedStateManager::NotifyNcmDisconnected(TInt aReason)
       
   158     {
       
   159     iNcmState().iState = ENcmStateDisconnected;
       
   160     iNcmState().iErrCode = aReason;
       
   161     iProperty.Set(iNcmState);
       
   162 
       
   163     if (iCoexistBearer->IsIpOverUsbActived())
       
   164         {
       
   165         iCoexistBearer->SetIpOverUsbDeactive();
       
   166         }
       
   167     else
       
   168         {
       
   169         Cancel();
       
   170         }
       
   171     }
       
   172 
       
   173 TInt CNcmSharedStateManager::SetStateValue(TInt aType, TInt aValue)
       
   174     {
       
   175     OstTraceExt2(TRACE_NORMAL, CNCMSHAREDSTATEMANAGER_SETSTATEVALUE, "SetStateValue: type=%d, val=%d", aType, aValue);
       
   176 
       
   177     const TInt KTypeIapId = 0;
       
   178     const TInt KTypeDhcpReturn = 1;
       
   179     TInt ret = KErrNone;
       
   180     switch(aType)
       
   181         {
       
   182         case KTypeIapId:
       
   183             //don't assign more than once.
       
   184             __ASSERT_DEBUG(iNcmState().iIapId==0, User::Panic(KPanicSharedState, __LINE__));
       
   185             iNcmState().iIapId = aValue;
       
   186             break;
       
   187 
       
   188         case KTypeDhcpReturn:
       
   189             if (KErrNone == aValue)
       
   190                 {
       
   191                 iEngine.NotifyDhcpStarted();
       
   192                 }
       
   193             else
       
   194                 {
       
   195                 OstTrace1(TRACE_WARNING, CNCMSHAREDSTATEMANAGER_SETSTATEVALUE_DUP01, "Dhcp Provision Request return error:%d", aValue);
       
   196                 NotifyNcmDisconnected(aValue);
       
   197                 }
       
   198             break;
       
   199 
       
   200         default:
       
   201             OstTraceExt2(TRACE_ERROR, CNCMSHAREDSTATEMANAGER_SETSTATEVALUE_DUP02, "unknow type:%d, value:%d", aType, aValue);
       
   202             __ASSERT_DEBUG(0, User::Panic(KPanicSharedState, __LINE__));
       
   203             break;
       
   204         }
       
   205 
       
   206     return ret;
       
   207     }
       
   208 
       
   209 TInt CNcmSharedStateManager::RegisterNotify(const RMessage2& aMsg)
       
   210     {
       
   211     if (!iNotifyMsg.IsNull())
       
   212         {
       
   213         return KErrInUse;
       
   214         }
       
   215 
       
   216     iNotifyMsg = aMsg;
       
   217 
       
   218     return KErrNone;
       
   219     }
       
   220 
       
   221 TInt CNcmSharedStateManager::DeRegisterNotify()
       
   222     {
       
   223     if (iNotifyMsg.IsNull())
       
   224         {
       
   225         return KErrNotReady;
       
   226         }
       
   227     else
       
   228         {
       
   229         iNotifyMsg.Complete(KErrCancel);
       
   230         iNotifyMsg = RMessage2();
       
   231         }
       
   232 
       
   233     return KErrNone;
       
   234     }