javaextensions/sensor/src.s60/sensorconnectionlist.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 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:  List of SensorConnections
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INTERNAL INCLUDES
       
    20 #include "sensorconnectionlist.h"
       
    21 #include "sensorconnection.h"
       
    22 #include "logger.h"
       
    23 
       
    24 // OS SPECIFIC INCLUDE
       
    25 #include "csensorfinder.h"
       
    26 
       
    27 SensorConnectionList::SensorConnectionList() :
       
    28         iSensors(NULL)
       
    29 {
       
    30     JELOG2(ESensor);
       
    31     iFinder = new CSensorFinder();
       
    32 }
       
    33 
       
    34 SensorConnectionList::~SensorConnectionList()
       
    35 {
       
    36     JELOG2(ESensor);
       
    37     delete iFinder;
       
    38     // Delete all connections
       
    39     SensorList* list = iSensors;
       
    40     while (list && list->iNext)
       
    41     {
       
    42         delete list->iSensorConnection;
       
    43         SensorList* next = list->iNext;
       
    44         delete list;
       
    45         list = next;
       
    46     }
       
    47     delete list;
       
    48 }
       
    49 
       
    50 unsigned short** SensorConnectionList::InitSensors(int* aSensorCount)
       
    51 {
       
    52     JELOG2(ESensor);
       
    53     return iFinder->FindSensors(aSensorCount);
       
    54 }
       
    55 
       
    56 SensorConnection* SensorConnectionList::CreateSensorConnection(int aSensorIndex)
       
    57 {
       
    58     JELOG2(ESensor);
       
    59     if (!iSensors)
       
    60     {
       
    61         iSensors = new SensorList();
       
    62         iSensors->iNext = NULL;
       
    63         iSensors->iSensorConnection = NULL;
       
    64     }
       
    65     SensorList* senList = iSensors;
       
    66     // Find last
       
    67     while (senList->iNext)
       
    68     {
       
    69         senList = senList->iNext;
       
    70     }
       
    71     // Create next object to list
       
    72     SensorList* next = new SensorList();
       
    73     if (!next)
       
    74     {
       
    75         return NULL;
       
    76     }
       
    77     next->iNext = NULL;
       
    78     next->iSensorConnection = NULL;
       
    79 
       
    80     //  Create actual sensor
       
    81     Sensor* sensor = iFinder->CreateSensor(aSensorIndex);
       
    82     if (!sensor)
       
    83     {
       
    84         delete next;
       
    85         return NULL;
       
    86     }
       
    87 
       
    88     // Create sensor connection, and assign values to current list item
       
    89     senList->iSensorConnection = new SensorConnection(sensor);
       
    90     if (!senList->iSensorConnection)
       
    91     {
       
    92         delete sensor;
       
    93         delete next;
       
    94         return NULL;
       
    95     }
       
    96     senList->iNext = next;
       
    97     return senList->iSensorConnection;
       
    98 }
       
    99 
       
   100 void SensorConnectionList::RemoveConnection(SensorConnection* aConnection)
       
   101 {
       
   102     JELOG2(ESensor);
       
   103     SensorList* currP = iSensors;
       
   104     SensorList* prevP = NULL;
       
   105     for (; currP != NULL; prevP = currP, currP = currP->iNext)
       
   106     {
       
   107         if (currP->iSensorConnection == aConnection)
       
   108         {
       
   109             if (prevP == NULL)
       
   110             {
       
   111                 iSensors = currP->iNext;
       
   112             }
       
   113             else
       
   114             {
       
   115                 prevP->iNext = currP->iNext;
       
   116             }
       
   117             delete currP->iSensorConnection; // Delete connection
       
   118             delete currP; // Delete node
       
   119             return;
       
   120         }
       
   121     }
       
   122 }
       
   123 
       
   124 // End of file