RDK Documentation (Open Sourced RDK Components)
ledmgrbase.cpp
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 #include <stdexcept>
20 #include "ledmgrbase.hpp"
21 #include "libIBus.h"
22 
23 static blinkOp_t g_blink_pattern_slow_blink[] = {{500, true}, {1000, false}};
24 static blinkOp_t g_blink_pattern_double_blink[] = {{200, true}, {100, false}, {200, true}, {1000, false}};
25 static blinkOp_t g_blink_pattern_fast_blink[] = {{200, true}, {100, false}};
26 
27 /**
28  * @addtogroup LED_APIS
29  * @{
30  */
31 
32 /**
33  * @brief This API prints pattern details include id, sequence.
34  */
36 {
37  std::cout<<"Size of the pattern list is "<<m_patterns.size()<<"\n";
38  for(int i = 0; i < m_patterns.size(); i++)
39  {
40  DEBUG("%x -- %x -- 0x%x\n", m_patterns[i].id, m_patterns[i].num_sequences, (unsigned int)m_patterns[i].sequence);
41  }
42 }
43 
44 /**
45  * @brief This API search for the matching indicator and return the indicator.
46  *
47  * @return Returns matching indicator.
48  *
49  * Note: throws std::invalid_argument exception
50  */
51 indicator& ledMgrBase::getIndicator(const std::string &name)
52 {
53  std::vector <indicator>::iterator iter;
54  for(iter = m_indicators.begin(); iter != m_indicators.end(); iter++)
55  {
56  if(0 == name.compare(iter->getName()))
57  {
58  break;
59  }
60  }
61 
62  if(iter == m_indicators.end())
63  {
64  ERROR("No matching indicator found!\n")
65  throw std::invalid_argument("No matching indicator found!");
66  }
67  return *iter;
68 }
69 
70 /**
71  * @brief Constructor function performs initialization.
72  */
74 {
75  m_is_powered_on = false;
76  m_error_flags = 0;
77  pthread_mutexattr_t mutex_attribute;
78  REPORT_IF_UNEQUAL(0, pthread_mutexattr_init(&mutex_attribute));
79  REPORT_IF_UNEQUAL(0, pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_ERRORCHECK));
80  REPORT_IF_UNEQUAL(0, pthread_mutex_init(&m_mutex, &mutex_attribute));
81 
82  REPORT_IF_UNEQUAL(0, IARM_Bus_Init(IARMBUS_OWNER_NAME));
83  REPORT_IF_UNEQUAL(0, IARM_Bus_Connect());
84 }
85 
86 /**
87  * @brief Destructor API.
88  */
90 {
91  pthread_mutex_destroy(&m_mutex);
92  REPORT_IF_UNEQUAL(0, IARM_Bus_Disconnect());
93  REPORT_IF_UNEQUAL(0, IARM_Bus_Term());
94 }
95 
96 /**
97  * @brief This function sets the power state.
98  *
99  * @param[in] state power state.
100  */
102 {
103  REPORT_IF_UNEQUAL(0, pthread_mutex_lock(&m_mutex));
104  m_is_powered_on = state;
105  REPORT_IF_UNEQUAL(0, pthread_mutex_unlock(&m_mutex));
106 }
107 
108 /**
109  * @brief This function used to get the power state.
110  *
111  * @return Returns power state.
112  */
114 {
115  int state;
116  REPORT_IF_UNEQUAL(0, pthread_mutex_lock(&m_mutex));
117  state = m_is_powered_on;
118  REPORT_IF_UNEQUAL(0, pthread_mutex_unlock(&m_mutex));
119  return state;
120 }
121 
122 /**
123  * @brief This API stores the error and returns the transition state in order to call appropriate ledmgr indicator api.
124  *
125  * @param[in] position error position which points to the error type.
126  * @param[in] value error state which points to true or false.
127  *
128  * @return Returns error state transition.
129  */
130 bool ledMgrBase::setError(unsigned int position, bool value)
131 {
132  if(32 > position)
133  {
134  bool transition_detected = false;
135 
136  REPORT_IF_UNEQUAL(0, pthread_mutex_lock(&m_mutex));
137  if(true == value)
138  {
139  if(0 == m_error_flags)
140  {
141  /*We're going from no errors to error-state*/
142  transition_detected = true;
143  }
144  m_error_flags |= (0x01 << position);
145  }
146  else
147  {
148  unsigned int prev_flags = m_error_flags;
149  m_error_flags &= (0xFFFFFFFE << position);
150  if((0 != prev_flags) && (0 == m_error_flags))
151  {
152  /*We're going from error-state to no errors.*/
153  transition_detected = true;
154  }
155  }
156  REPORT_IF_UNEQUAL(0, pthread_mutex_unlock(&m_mutex));
157  return transition_detected;
158  }
159  else
160  {
161  ERROR("Position marker too large!\n");
162  return false;
163  }
164 }
165 
166 /**
167  * @brief This API creates blink patterns using the pattern type, duration, sequence … etc. as parameters.
168  */
170 {
171  m_patterns.resize(NUM_PATTERNS);
172  m_patterns[STATE_SLOW_BLINK] = {STATE_SLOW_BLINK, 2, g_blink_pattern_slow_blink};
173  m_patterns[STATE_DOUBLE_BLINK] = {STATE_DOUBLE_BLINK, 4, g_blink_pattern_double_blink};
174  m_patterns[STATE_FAST_BLINK] = {STATE_FAST_BLINK, 2, g_blink_pattern_fast_blink};
175  INFO("Complete\n");
176  return 0;
177 }
178 
179 /**
180  * @brief This API return the desired pattern info with respect to pattern type.
181  *
182  * @param[in] type Blink pattern type.
183  *
184  * @return Returns corresponding blink pattern info structure.
185  */
187 {
188  return &m_patterns[type];
189 }
190 
191 
192 /** @} */ //END OF GROUP LED_APIS
ledMgrBase::setError
bool setError(unsigned int position, bool value)
This API stores the error and returns the transition state in order to call appropriate ledmgr indica...
Definition: ledmgrbase.cpp:130
blinkPatternType_t
blinkPatternType_t
Definition: fp_profile.hpp:28
IARM_Bus_Term
IARM_Result_t IARM_Bus_Term(void)
This API is used to terminate the IARM-Bus library.
STATE_SLOW_BLINK
@ STATE_SLOW_BLINK
Definition: fp_profile.hpp:30
ledMgrBase::getPowerState
int getPowerState()
This function used to get the power state.
Definition: ledmgrbase.cpp:113
NUM_PATTERNS
#define NUM_PATTERNS
Definition: fp_profile.hpp:26
indicator
Definition: indicator.hpp:26
IARM_Bus_Disconnect
IARM_Result_t IARM_Bus_Disconnect(void)
This API disconnect Application from IARM Bus so the application will not receive any IARM event or R...
ledMgrBase::getIndicator
indicator & getIndicator(const std::string &name)
This API search for the matching indicator and return the indicator.
Definition: ledmgrbase.cpp:51
libIBus.h
RDK IARM-Bus API Declarations.
ledMgrBase::setPowerState
void setPowerState(int state)
This function sets the power state.
Definition: ledmgrbase.cpp:101
ledMgrBase::getPattern
const blinkPattern_t * getPattern(blinkPatternType_t pattern) const
This API return the desired pattern info with respect to pattern type.
Definition: ledmgrbase.cpp:186
ledMgrBase::createBlinkPatterns
virtual int createBlinkPatterns()
This API creates blink patterns using the pattern type, duration, sequence … etc. as parameters.
Definition: ledmgrbase.cpp:169
ledMgrBase::ledMgrBase
ledMgrBase()
Constructor function performs initialization.
Definition: ledmgrbase.cpp:73
IARM_Bus_Connect
IARM_Result_t IARM_Bus_Connect(void)
This API is used to connect application to the IARM bus daemon. After connected, the application can ...
Definition: iarmMgrMocks.cpp:33
ledMgrBase::diagnostics
void diagnostics()
This API prints pattern details include id, sequence.
Definition: ledmgrbase.cpp:35
STATE_DOUBLE_BLINK
@ STATE_DOUBLE_BLINK
Definition: fp_profile.hpp:31
STATE_FAST_BLINK
@ STATE_FAST_BLINK
Definition: fp_profile.hpp:32
IARM_Bus_Init
IARM_Result_t IARM_Bus_Init(const char *name)
This API is used to initialize the IARM-Bus library.
Definition: iarmMgrMocks.cpp:38
ledMgrBase::~ledMgrBase
~ledMgrBase()
Destructor API.
Definition: ledmgrbase.cpp:89