RDK Documentation (Open Sourced RDK Components)
Header.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 trm
23 * @{
24 * @defgroup common
25 * @{
26 **/
27 
28 
29 #include "trm/TRM.h"
30 #include "trm/Header.h"
31 #include "Util.h"
32 
33 TRM_BEGIN_NAMESPACE
34 const char Header::kProtocol[4] = {'T','R', 'M', 'S'};
35 
36 Header::Header(MessageType type, int clientId, size_t payloadLength)
37 : type(type), clientId(clientId), payloadLength(payloadLength)
38 {
39 }
40 
41 void Header::serialize(std::vector<uint8_t> &out) const
42 {
43  out.resize(Header::kHeaderLength, 0);
44 
45  /* First 4 bytes are 'T'R'M'S */
46  out[0] = kProtocol[0];
47  out[1] = kProtocol[1];
48  out[2] = kProtocol[2];
49  out[3] = kProtocol[3];
50  /* Next 4 bytes are Message Type */
51  out[4] = (type & 0xFF000000) >> 24;
52  out[5] = (type & 0x00FF0000) >> 16;
53  out[6] = (type & 0x0000FF00) >> 8;
54  out[7] = (type & 0x000000FF) >> 0;
55  /* Next 4 bytes are transportId () */
56  out[8] = (clientId & 0xFF000000) >> 24;
57  out[9] = (clientId & 0x00FF0000) >> 16;
58  out[10] = (clientId & 0x0000FF00) >> 8;
59  out[11] = (clientId & 0x000000FF) >> 0;
60  /* Last 4 bytes are Payload Length (Unused) */
61  out[12] = (payloadLength & 0xFF000000) >> 24;
62  out[13] = (payloadLength & 0x00FF0000) >> 16;
63  out[14] = (payloadLength & 0x0000FF00) >> 8;
64  out[15] = (payloadLength & 0x000000FF) >> 0;
65 }
66 
67 void Header::deserialize(const std::vector<uint8_t> &in)
68 {
69  HexDump(in);
70  if (in.size() >= 16) {
71  uint8_t *ptr = (uint8_t *)&in[0];
72  /* First 4 bytes are 'T'R'M'S */
73  if (ptr[0] == kProtocol[0] &&
74  ptr[1] == kProtocol[1] &&
75  ptr[2] == kProtocol[2] &&
76  ptr[3] == kProtocol[3]) {
77  /* Next 4 bytes are Message Type */
78  type = (MessageType)
79  (((ptr[4]) << 24) |
80  ((ptr[5]) << 16) |
81  ((ptr[6]) << 8) |
82  ((ptr[7]) << 0));
83  /* Next 4 bytes are Message Id (Unused) */
84  clientId =
85  (((ptr[8]) << 24) |
86  ((ptr[9]) << 16) |
87  ((ptr[10]) << 8) |
88  ((ptr[11]) << 0));
89 
90  /* Last 4 bytes are Payload Length (Unused) */
91  payloadLength=(((ptr[12]) << 24) |
92  ((ptr[13]) << 16) |
93  ((ptr[14]) << 8) |
94  ((ptr[15]) << 0));
95  }
96  else {
97  //@TODO: discard invalid message.
98  Assert(0);
99  }
100  }
101  else {
102  Assert(0);
103  }
104 }
105 
106 void Header::setPayloadLength(size_t payloadLength)
107 {
108  this->payloadLength = payloadLength;
109 }
110 
111 size_t Header::getPayloadLength(void) const
112 {
113  return payloadLength;
114 }
115 
116 void Header::setClientId(uint32_t clientId)
117 {
118  this->clientId = clientId;
119 }
120 
121 uint32_t Header::getClientId(void) const
122 {
123  return clientId;
124 }
125 
126 TRM_END_NAMESPACE
127 
128 
129 
130 
131 /** @} */
132 /** @} */
Header.h