RDK Documentation (Open Sourced RDK Components)
sleepMode.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22  * @file sleepMode.cpp
23  * @brief This file contains definitions of SleepMode class.
24  */
25 
26 
27 
28 /**
29 * @defgroup devicesettings
30 * @{
31 * @defgroup ds
32 * @{
33 **/
34 
35 
36 #include "sleepMode.hpp"
37 #include "illegalArgumentException.hpp"
38 #include "dsTypes.h"
39 #include "dsUtl.h"
40 #include "dslogger.h"
41 #include <vector>
42 #include "list.hpp"
43 
44 namespace {
45  const char *_names[] = {
46  "LIGHT_SLEEP",
47  "DEEP_SLEEP",
48  };
49 
50  inline const bool isValid(int id) {
51  return dsSleepMode_isValid(id);
52  }
53 }
54 
55 namespace device {
56 
60 static std::vector<SleepMode> _vSleepModes;
61 
62 
63 /**
64  * @fn SleepMode::getInstance(int id)
65  * @brief This function is used to get an instance of SleepMode against the specified id,
66  * only if the id passed is valid.
67  *
68  * @param[in] id Indicates the id against which the SleepMode instance is required.
69  * The valid id's are 0 and 1 which indicates light and deep sleep mode respectively.
70  *
71  * @return Returns an instance of SleepMode against the specified id if the id is valid else
72  * throws an IllegalArgumentException.
73  */
75 {
76  static bool FirstTime = true;
77  if (::isValid(id)) {
78  if(FirstTime)
79  {
80  for(size_t i=0; i < dsHOST_SLEEP_MODE_MAX; i++)
81  {
82  _vSleepModes.push_back(SleepMode(i));
83  }
84  FirstTime = false;
85  }
86  return _vSleepModes.at(id);
87  }
88  else {
90  }
91 }
92 
93 
94 /**
95  * @fn SleepMode::getSleepModes()
96  * @brief This function is used to get all the platform supported types of sleep modes.
97  *
98  * @return Returns sleepModes, which is a list containing all the supported sleep mode id's.
99  */
101 {
102  List<SleepMode> sleepModes;
103  for(std::vector<SleepMode>::const_iterator it = _vSleepModes.begin();
104  it != _vSleepModes.end(); it++)
105  {
106 #ifndef ENABLE_DEEP_SLEEP
107  if (it->getId() == dsHOST_SLEEP_MODE_DEEP) continue;
108 #endif
109  sleepModes.push_back(*it);
110  }
111  return sleepModes;
112 }
113 
114 
115 /**
116  * @fn SleepMode::getInstance(const std::string &name)
117  * @brief This function gets a SleepMode instance of the type specified by 'name' parameter.
118  *
119  * @param[in] name Indicates the name of the sleep mode whose instance is required.
120  * The valid names are "LIGHT_SLEEP" and "DEEP_SLEEP".
121  *
122  * @return Returns the SleepMode instance of specified type only if the name parameter
123  * passed is valid else throws an IllegalArgumentException.
124  */
125 SleepMode & SleepMode::getInstance(const std::string &name)
126 {
127  for (size_t i = 0; i < dsUTL_DIM(_names); i++) {
128  if (name.compare(_names[i]) == 0) {
129  return SleepMode::getInstance(i);
130  }
131  }
132 
133  throw IllegalArgumentException();
134 }
135 
136 
137 /**
138  * @fn SleepMode::SleepMode(int id)
139  * @brief This function is a parameterised constructor of SleepMode class. It initializes
140  * the SleepMode instance with the id specified and the name corresponding to it. If the
141  * id passed is invalid then it throws an IllegalArgumentException.
142  *
143  * @param[in] id Indicates the id against which the SleepMode instance is required.
144  *
145  * @return None
146  */
148 {
149  if (::isValid(id)) {
150  _id = id;
151  _name = std::string(_names[id]);
152  }
153  else {
154  throw IllegalArgumentException();
155  }
156 }
157 
158 
159 /**
160  * @fn SleepMode::~SleepMode()
161  * @brief This function is the default destructor of SleepMode class.
162  *
163  * @return None
164  */
166 {
167 }
168 
169 }
170 
171 
172 /** @} */
173 /** @} */
device::DSConstant::_id
int _id
Indicates the id of the instance inheriting this class.
Definition: dsConstant.hpp:57
device::List
This class is implemented using templates and it is used to maintain a container with the list of sup...
Definition: list.hpp:51
dsHOST_SLEEP_MODE_DEEP
@ dsHOST_SLEEP_MODE_DEEP
Definition: dsTypes.h:922
device::SleepMode::SleepMode
SleepMode(int id)
This function is a parameterised constructor of SleepMode class. It initializes the SleepMode instanc...
Definition: sleepMode.cpp:147
device::SleepMode::getInstance
static SleepMode & getInstance(int id)
This function is used to get an instance of SleepMode against the specified id, only if the id passed...
Definition: sleepMode.cpp:74
dsTypes.h
Device Settings HAL types.
dsSleepMode_isValid
#define dsSleepMode_isValid(t)
Definition: dsTypes.h:992
device::DSConstant::isValid
static bool isValid(int min, int max, int val)
This function checks if the given value lies between min and max values provided.
Definition: dsConstant.hpp:72
device::SleepMode::kLightSleep
static const int kLightSleep
Indicates light sleep mode.
Definition: sleepMode.hpp:54
dsUtl.h
Device Settings HAL utilities.
device::SleepMode::kMax
static const int kMax
Indicates maximum number of sleep modes supported.
Definition: sleepMode.hpp:56
dsHOST_SLEEP_MODE_MAX
@ dsHOST_SLEEP_MODE_MAX
Definition: dsTypes.h:923
device::DSConstant::_name
std::string _name
Indicates the name string of the instance inheriting this class.
Definition: dsConstant.hpp:58
device::SleepMode
This class extends DSConstant to implement SleepMode. It is used to manage the sleep modes for device...
Definition: sleepMode.hpp:51
device::SleepMode::~SleepMode
virtual ~SleepMode()
This function is the default destructor of SleepMode class.
Definition: sleepMode.cpp:165
device::SleepMode::kDeepSleep
static const int kDeepSleep
Indicates deep sleep mode.
Definition: sleepMode.hpp:55
dsHOST_SLEEP_MODE_LIGHT
@ dsHOST_SLEEP_MODE_LIGHT
Definition: dsTypes.h:921
device::List::push_back
void push_back(const T &x)
This is a template function used to push/store the ID of an instance x into the container.
Definition: list.hpp:108
device::SleepMode::getSleepModes
List< SleepMode > getSleepModes()
This function is used to get all the platform supported types of sleep modes.
Definition: sleepMode.cpp:100
device::IllegalArgumentException
This class extends Exception class to manage the expections caused due to illegal arguments.
Definition: illegalArgumentException.hpp:51
dsUTL_DIM
#define dsUTL_DIM(arr)
Device Settings general Array dimension calculation inline definition.
Definition: dsUtl.h:85
sleepMode.hpp
This file defines SleepMode class for device settings.