RDK Documentation (Open Sourced RDK Components)
AampIonMemorySystem.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2020 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  * @file AampIonMemorySystem.cpp
22  * @brief Handles ION memory management segments
23  */
24 
25 #include "AampIonMemorySystem.h"
26 
27 // For Ion memory
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 
32 #include "AampConfig.h"
33 
34 /**
35  * @brief AampIonMemorySystem constructor
36  */
38 }
39 
40 /**
41  * @brief AampIonMemorySystem distructor
42  */
44 }
45 
46 
47 AampIonMemorySystem::AampIonMemoryContext::AampIonMemoryContext(AampLogManager *logObj) : fd_(0), handle_(0), mLogObj(logObj) {};
48 
49 bool AampIonMemorySystem::AampIonMemoryContext::createBuffer(size_t len) {
50  fd_ = ion_open();
51  if (fd_ < 0) {
52  AAMPLOG_WARN("Calling ion_open(): %d", fd_);
53  return false;
54  }
55  AAMPLOG_INFO("Got %d from ion_open()", fd_);
56 
57  int ret = ion_alloc(fd_, len,
58  AAMP_ION_MEMORY_ALIGN,
59  AAMP_ION_MEMORY_REGION,
60  AAMP_ION_MEMORY_FLAGS, &handle_);
61 
62  if (ret != 0) {
63  AAMPLOG_WARN("Calling ion_alloc(): %d", fd_);
64  ion_close(fd_);
65  fd_ = 0;
66  }
67  return (ret == 0);
68 }
69 
70 /**
71  * @class AampIonMemorySystemCloser
72  * @brief Class to handle close the ION memory
73  */
74 
76 private:
78  bool closeOnDestruct_;
79 public:
81  : context_(context)
82  , closeOnDestruct_(true) {}
84  if (closeOnDestruct_) {
85  context_.close();
86  }
87  }
88  void setNoCloseOnDestruct() {
89  closeOnDestruct_ = false;
90  }
91 };
92 
93 AampIonMemorySystem::AampIonMemoryContext::~AampIonMemoryContext() {
94  close();
95 }
96 
97 void AampIonMemorySystem::AampIonMemoryContext::close() {
98  if (handle_ != 0) {
99  int ret = ion_free(fd_, handle_);
100  handle_ = 0;
101  if (ret) {
102  AAMPLOG_ERR("ion_free_buffer failed");
103  }
104  }
105  if (fd_ != 0) {
106  ion_close(fd_);
107  fd_ = 0;
108  }
109  AAMPLOG_INFO("Closed ION memory ok");
110 }
111 
112 bool AampIonMemorySystem::AampIonMemoryContext::share(int& shareFd) {
113  int status = ion_share(fd_, handle_, &shareFd);
114  return status == 0;
115 }
116 
117 bool AampIonMemorySystem::AampIonMemoryContext::phyAddr(unsigned long *phyAddr) {
118  unsigned int size;
119  int status = ion_phys(fd_, handle_, phyAddr, &size);
120  return status == 0;
121 }
122 
123 /**
124  * @brief Encode a block of data to send over the divide
125  */
126 bool AampIonMemorySystem::encode(const uint8_t *dataIn, uint32_t dataInSz, std::vector<uint8_t>& dataOut)
127 {
128  AampIonMemorySystemCloser closer(context_);
129 
130  if (!context_.createBuffer(dataInSz))
131  {
132  AAMPLOG_WARN("Failed to create Ion memory object: %d", errno);
133  return false;
134  }
135 
136  int share_fd;
137  if (!context_.share(share_fd))
138  {
139  AAMPLOG_WARN("Failed to share Ion memory object: %d", errno);
140  return false;
141  }
142 
143  AampMemoryHandleCloser mc(share_fd);
144 
145  void *dataWr = mmap(NULL, dataInSz, PROT_WRITE | PROT_READ, MAP_SHARED, share_fd, 0);
146  if (dataWr == 0)
147  {
148  AAMPLOG_WARN("Failed to map the Ion memory object %d", errno);
149  return false;
150  }
151 
152  memmove(dataWr, dataIn, dataInSz);
153 
154  int status = munmap(dataWr, dataInSz);
155  if (status < 0)
156  {
157  AAMPLOG_WARN("Failed to unmap the Ion memory object %d", errno);
158  return false;
159  }
160  // Only send the size of the Ion memory, nothing else
162  if (!context_.phyAddr(&ib.phyAddr)) {
163  AAMPLOG_WARN("Failed to find the physical address of the Ion memory object %d", errno);
164  return false;
165  }
166  ib.size = sizeof(ib);
167  ib.dataSize = dataInSz;
168 
169  // Look away now, this is horrid
170  dataOut.resize(sizeof(ib));
171  memcpy(dataOut.data(), &ib, sizeof(ib));
172  closer.setNoCloseOnDestruct();
173 
174  return true;
175 }
176 
177 /**
178  * @brief Decode from getting back
179  */
180 bool AampIonMemorySystem::decode(const uint8_t * dataIn, uint32_t dataInSz, uint8_t* dataOut, uint32_t dataOutSz)
181 {
182  AampIonMemorySystemCloser closer(context_);
183 
184  const AampIonMemoryInterchangeBuffer *pib = reinterpret_cast<const AampIonMemoryInterchangeBuffer *>(dataIn);
185  if (dataInSz != sizeof(AampIonMemoryInterchangeBuffer))
186  {
187  AAMPLOG_WARN("Wrong data packet size, expected %d, got %d", sizeof(AampIonMemoryInterchangeBuffer), dataInSz);
188  return false;
189  }
190  unsigned long phyAddr;
191  if (!context_.phyAddr(&phyAddr)) {
192  AAMPLOG_WARN("Failed to get physical address of ION memory: %d", errno);
193  return false;
194  }
195  if (pib->phyAddr != phyAddr) {
196  AAMPLOG_WARN("Physical address of ION memory not what was sent... %d", errno);
197  return false;
198  }
199 
200  int share_fd;
201  if (!context_.share(share_fd))
202  {
203  AAMPLOG_WARN("Failed to share Ion memory object: %d", errno);
204  return false;
205  }
206 
207  AampMemoryHandleCloser mc(share_fd);
208 
209  uint32_t packetSize = pib->dataSize;
210  void *dataRd = mmap(NULL, packetSize, PROT_READ, MAP_SHARED, share_fd, 0);
211  if (dataRd == 0)
212  {
213  AAMPLOG_WARN("Failed to map the Ion memory object %d", errno);
214  return false;
215  }
216 
217  if (packetSize > dataOutSz)
218  {
219  AAMPLOG_WARN("Received data is bigger than provided buffer. %d > %d", packetSize, dataOutSz);
220  }
221  memmove(dataOut, dataRd, std::min(packetSize, dataOutSz));
222 
223  int status = munmap(dataRd, packetSize);
224  if (status < 0)
225  {
226  AAMPLOG_WARN("Failed to unmap the shared memory object %d", errno);
227  return false;
228  }
229 
230  // This time the closer can close the ION memory buffer
231  return true;
232 }
233 
234 /**
235  * @brief Call this if there's an failure external to the MS and it needs to tidy up unexpectedly
236  */
238  AAMPLOG_WARN("closing transfer early");
239  context_.close();
240 }
AampIonMemorySystem::~AampIonMemorySystem
virtual ~AampIonMemorySystem()
AampIonMemorySystem distructor.
Definition: AampIonMemorySystem.cpp:43
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
AampIonMemoryInterchangeBuffer::phyAddr
unsigned long phyAddr
Definition: AampIonMemorySystem.h:41
AampIonMemorySystem.h
Controls the ION memory for aamp.
AampIonMemoryInterchangeBuffer::dataSize
uint32_t dataSize
Definition: AampIonMemorySystem.h:40
AampConfig.h
Configurations for AAMP.
AampMemoryHandleCloser
This just closes a file on descope.
Definition: AampMemorySystem.h:72
AampIonMemorySystem::encode
virtual bool encode(const uint8_t *dataIn, uint32_t dataInSz, std::vector< uint8_t > &dataOut) override
Encode a block of data to send over the divide.
Definition: AampIonMemorySystem.cpp:126
AampIonMemorySystem::terminateEarly
virtual void terminateEarly() override
Call this if there's an failure external to the MS and it needs to tidy up unexpectedly.
Definition: AampIonMemorySystem.cpp:237
AampIonMemorySystem::AampIonMemoryContext
Definition: AampIonMemorySystem.h:83
AampIonMemorySystem::AampIonMemorySystem
AampIonMemorySystem(AampLogManager *logObj)
AampIonMemorySystem constructor.
Definition: AampIonMemorySystem.cpp:37
AampIonMemorySystem::decode
virtual bool decode(const uint8_t *dataIn, uint32_t dataInSz, uint8_t *dataOut, uint32_t dataOutSz) override
Decode from getting back.
Definition: AampIonMemorySystem.cpp:180
AampIonMemoryInterchangeBuffer
Stores the information on ION Memory to store data.
Definition: AampIonMemorySystem.h:38
AampIonMemorySystemCloser
Class to handle close the ION memory.
Definition: AampIonMemorySystem.cpp:75
AAMPMemorySystem
Handles the operations for AAMP memory managemnts.
Definition: AampMemorySystem.h:37