RDK Documentation (Open Sourced RDK Components)
LibCCEC.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 
20 
21 
22 /**
23 * @defgroup hdmicec
24 * @{
25 * @defgroup ccec
26 * @{
27 **/
28 
29 /**
30  * @defgroup hdmi_ccec HDMI-CEC CCEC
31  * @ingroup HDMI_CEC
32  * @ingroup hdmi_ccec
33  * @{
34  */
35 
36 
37 #include <string.h>
38 #include <iostream>
39 
40 #include "osal/Thread.hpp"
41 #include "osal/Mutex.hpp"
42 
43 #include "ccec/LibCCEC.hpp"
44 #include "ccec/Exception.hpp"
45 
46 #include "ccec/Driver.hpp"
47 #include "Bus.hpp"
48 
50 using CCEC_OSAL::Thread;
51 
52 CCEC_BEGIN_NAMESPACE
53 
54 extern char _CEC_LOG_PREFIX[64];
55 
56 /**
57  * @brief This function is used to create the instance for CEC.
58  *
59  * @return libCCEC Instance of CEC.
60  */
62 {
63  static LibCCEC libCCEC;
64  return libCCEC;
65 }
66 
67 /**
68  * @brief This is Constructor for LibCEC class. It initializes variables with
69  * default values.
70  */
72 : initialized(false), connected(false)
73 {
74 }
75 
76 /**
77  * @brief This function is used to initialize CEC by starting the driver and
78  * doing host-specific initialization.
79  *
80  * @param[in] name Name of CEC log prefix.
81  *
82  * @return None
83  */
84 void LibCCEC::init(const char *name)
85 {AutoLock lock_(mutex);
86 
87  if (initialized) {
88  throw InvalidStateException();
89  }
90 
91  if (name != NULL) {
92  strncpy(_CEC_LOG_PREFIX, name, sizeof(_CEC_LOG_PREFIX) - 2);
93  _CEC_LOG_PREFIX[sizeof(_CEC_LOG_PREFIX) - 1] = '\0';
94  }
95  else {
96  _CEC_LOG_PREFIX[0] = '\0';
97  }
98 
99  /* Add Host-specific Initialization*/
100  Driver::getInstance().open();
102  initialized = true;
103 }
104 
105 /**
106  * @brief This function is used to stop CEC by terminating the connection and
107  * stoping the driver.
108  *
109  * @return None
110  */
112 {AutoLock lock_(mutex);
113 
114  if (!initialized) {
115  throw InvalidStateException();
116  }
117 
119  Driver::getInstance().close();
120  initialized = false;
121 }
122 
123 /**
124  * @brief This function is used to add logical address
125  * to the driver, so that it can ACK if there a direct messages received.
126  *
127  * @param[in] logical address of device.
128  *
129  * @return success or throw exceptions if there is error.
130  */
132 {
133  //printf("Entered LibCCEC::getLogicalAddress \n");
134  if (!initialized) {
135  throw InvalidStateException();
136  }
137 
138  Driver::getInstance().addLogicalAddress(source);
139 
140  return true;
141 }
142 
143 
144 /**
145  * @brief This function is used to get CEC device logical address
146  * starting the connection.
147  *
148  * @param[in] devType Device whose logical address has to be found.
149  *
150  * @return logicalAddress Logical address of the device.
151  */
153 {
154  //printf("Entered LibCCEC::getLogicalAddress \n");
155  int logicalAddress = 0;
156  if (!initialized) {
157  throw InvalidStateException();
158  }
159 
160  logicalAddress = Driver::getInstance().getLogicalAddress(devType);
161 
162  if(0 == logicalAddress)
163  {
164  throw InvalidStateException();
165  }
166 
167  //printf("LibCCEC::getLogicalAddress got logicalAddress: %d \n",logicalAddress);
168  return logicalAddress;
169 }
170 
171 void LibCCEC::getPhysicalAddress(unsigned int *physicalAddress)
172 {
173  //printf("Entered LibCCEC::getPhysicalAddress\n");
174  if (!initialized) {
175  throw InvalidStateException();
176  }
177 
178  Driver::getInstance().getPhysicalAddress(physicalAddress);
179 
180  //printf("LibCCEC::getPhysicalAddress got getPhysicallAddress : %x \n",physicalAddress);
181  return;
182 }
183 
184 CCEC_END_NAMESPACE
185 
186 
187 /** @} */
188 /** @} */
189 /** @} */
CCEC_OSAL::Thread
Definition: Thread.hpp:64
CCEC_OSAL::AutoLock
Definition: Mutex.hpp:121
Bus::stop
void stop(void)
This function stops the reader & writer threads and removes the instance for Bus.
Definition: Bus.cpp:106
LibCCEC::getInstance
static LibCCEC & getInstance(void)
This function is used to create the instance for CEC.
Definition: LibCCEC.cpp:61
LibCCEC::init
void init(const char *name=0)
This function is used to initialize CEC by starting the driver and doing host-specific initialization...
Definition: LibCCEC.cpp:84
Bus::getInstance
static Bus & getInstance(void)
This function is used to create the instance of Bus class.
Definition: Bus.cpp:58
Thread.hpp
This file defines interface of Thread class.
LogicalAddress
Definition: Operands.hpp:409
LibCCEC
Definition: LibCCEC.hpp:42
LibCCEC::LibCCEC
LibCCEC(void)
This is Constructor for LibCEC class. It initializes variables with default values.
Definition: LibCCEC.cpp:71
LibCCEC::addLogicalAddress
int addLogicalAddress(const LogicalAddress &source)
This function is used to add logical address to the driver, so that it can ACK if there a direct mess...
Definition: LibCCEC.cpp:131
InvalidStateException
Definition: Exception.hpp:81
Bus::start
void start(void)
This function starts the threads and gets the instance for Bus.
Definition: Bus.cpp:83
LibCCEC::getLogicalAddress
int getLogicalAddress(int devType)
This function is used to get CEC device logical address starting the connection.
Definition: LibCCEC.cpp:152
Mutex.hpp
This file defines interface of Mutex class.
LibCCEC::term
void term(void)
This function is used to stop CEC by terminating the connection and stoping the driver.
Definition: LibCCEC.cpp:111