RDK Documentation (Open Sourced RDK Components)
CECFrame.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 * @defgroup hdmicec
23 * @{
24 * @defgroup ccec
25 * @{
26 **/
27 
28 
29 #include <cstdio>
30 #include <stdexcept>
31 #include "ccec/CECFrame.hpp"
32 #include "ccec/Util.hpp"
33 
34 namespace {
35 }
36 
37 CCEC_BEGIN_NAMESPACE
38 
39 CECFrame::CECFrame(const uint8_t *buf, size_t len) : len_() {
40  reset();
41  if (buf && len > 0) append(buf, len);
42 }
43 
44 CECFrame CECFrame::subFrame(size_t start, size_t len) const {
45  CECFrame frame;
46  if (len == 0) len = MAX_LENGTH;
47  while(start < len_ && len--) {
48  frame.append(at(start));
49  start++;
50  }
51  return frame;
52 }
53 
54 void CECFrame::append(uint8_t byte) {
55  if (len_ == MAX_LENGTH)
56  throw std::out_of_range("Frame grows beyond maximum");
57  buf_[len_++] = byte;
58 }
59 
60 void CECFrame::append(const uint8_t *buf, size_t len) {
61  for (size_t i = 0; i < (len); i++) {
62  append(buf[i]);
63  }
64 }
65 
66 void CECFrame::append(const CECFrame &frame) {
67  const uint8_t *buf = NULL;
68  size_t len = 0;
69  frame.getBuffer(&buf, &len);
70  append(buf, len);
71 }
72 
73 void CECFrame::reset(void) {
74  len_ = 0;
75 }
76 
77 void CECFrame::getBuffer(const uint8_t **buf, size_t *len) const {
78  *len = this->len_;
79  *buf = this->buf_;
80 
81 }
82 
83 const uint8_t * CECFrame::getBuffer(void) const {
84  return buf_;
85 }
86 
87 const uint8_t CECFrame::at(size_t i) const {
88  if (i >= len_) {
89  CCEC_LOG( LOG_DEBUG, "Frame i=%d, len=%d\r\n", i, len_);
90  //int *p = NULL;
91  //*p = 0xACACACAC;
92  throw std::out_of_range("Frame reads beyond maximum");
93  }
94 
95  return buf_[i];
96 }
97 
98 uint8_t & CECFrame::operator[] (size_t i) {
99  if (i >= len_) {
100  throw std::out_of_range("Frame access beyond maximum");
101  }
102 
103  return buf_[i];
104 }
105 
106 const size_t CECFrame::length(void) const {
107  return len_;
108 }
109 
110 void CECFrame::hexDump(int level) const {
111  if (1) {
112  CCEC_LOG( level, "FRAME DUMP========================: \r\n");
113  for (size_t i = 0; i < len_; i++) {
114  CCEC_LOG( level, "%02X ", (int) buf_[i]);
115  }
116  CCEC_LOG( level, "\r\n================================== \r\n");
117  }
118 }
119 
120 
121 CCEC_END_NAMESPACE
122 
123 
124 
125 /** @} */
126 /** @} */
Bus::start
void start(void)
This function starts the threads and gets the instance for Bus.
Definition: Bus.cpp:83
CECFrame
Definition: CECFrame.hpp:40
CCEC_LOG
void CCEC_LOG(int level, const char *format ...)
This function is used to gets the logs depending on the level of log and print these to standard outp...
Definition: Util.cpp:120