RDK Documentation (Open Sourced RDK Components)
AampSharedMemorySystem.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 AampSharedMemorySystem.cpp
22  * @brief Handles the functionalities for shared memory Aamp DRM
23  */
24 
25 #include "AampSharedMemorySystem.h"
26 
27 // For shared memory
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 
32 #include "AampConfig.h"
33 
34 /**
35  * @brief AampSharedMemorySystem constructor
36  */
38 }
39 
40 /**
41  * @brief AampSharedMemorySystem Destructor
42  */
44 }
45 
46 /**
47  * @brief Encode a block of data to send over the divide
48  */
49 bool AampSharedMemorySystem::encode(const uint8_t *dataIn, uint32_t dataInSz, std::vector<uint8_t>& dataOut)
50 {
51  int shmHandle = shm_open(AAMP_SHARED_MEMORY_NAME.c_str(), AAMP_SHARED_MEMORY_CREATE_OFLAGS, AAMP_SHARED_MEMORY_MODE);
52 
53  if (shmHandle < 0)
54  {
55  AAMPLOG_WARN("Failed to create Shared memory object: %d", errno);
56  return false;
57  }
58 
59  // This will close the SM object regardless
60  AampMemoryHandleCloser mc(shmHandle);
61 
62  int status = ftruncate(shmHandle, dataInSz);
63  if (status < 0)
64  {
65  AAMPLOG_WARN("Failed to truncate the Shared memory object %d", status);
66  return false;
67  }
68 
69  void *dataWr = mmap(NULL, dataInSz, PROT_WRITE | PROT_READ, MAP_SHARED, shmHandle, 0);
70  if (dataWr == 0)
71  {
72  AAMPLOG_WARN("Failed to map the Shared memory object %d", errno);
73  return false;
74  }
75 
76  memmove(dataWr, dataIn, dataInSz);
77 
78  status = munmap(dataWr, dataInSz);
79  if (status < 0)
80  {
81  AAMPLOG_WARN("Failed to unmap the Shared memory object %d", errno);
82  return false;
83  }
84  // Only send the size of the shared memory, nothing else
86 #ifdef AAMP_SHMEM_USE_SIZE_AND_INSTANCE
87  ib.size = sizeof(ib);
88  ib.instanceNo = 0;
89 #endif
90  ib.dataSize = dataInSz;
91 
92  // Look away now, this is horrid
93  dataOut.resize(sizeof(ib));
94  memcpy(dataOut.data(), &ib, sizeof(ib));
95 
96  return true;
97 }
98 
99 /**
100  * @brief Decode from getting back
101  */
102 bool AampSharedMemorySystem::decode(const uint8_t * dataIn, uint32_t dataInSz, uint8_t* dataOut, uint32_t dataOutSz)
103 {
104  int shmHandle = shm_open(AAMP_SHARED_MEMORY_NAME.c_str(), AAMP_SHARED_MEMORY_READ_OFLAGS, AAMP_SHARED_MEMORY_MODE);
105 
106  if (shmHandle < 0)
107  {
108  AAMPLOG_WARN("Failed to create Shared memory object: %d", errno);
109  return false;
110  }
111 
112  if (dataInSz != sizeof(AampSharedMemoryInterchangeBuffer))
113  {
114  AAMPLOG_WARN("Wrong data packet size, expected %d, got %d", sizeof(AampSharedMemoryInterchangeBuffer), dataInSz);
115  return false;
116  }
117  // This will close the SM object regardless
118  AampMemoryHandleCloser mc(shmHandle);
119 
120  const AampSharedMemoryInterchangeBuffer *pib = reinterpret_cast<const AampSharedMemoryInterchangeBuffer *>(dataIn);
121  uint32_t packetSize = pib->dataSize;
122  void *dataRd = mmap(NULL, packetSize, PROT_READ, MAP_SHARED, shmHandle, 0);
123  if (dataRd == 0)
124  {
125  AAMPLOG_WARN("Failed to map the Shared memory object %d", errno);
126  return false;
127  }
128 
129  if (packetSize > dataOutSz)
130  {
131  AAMPLOG_WARN("Received data is bigger than provided buffer. %d > %d", packetSize, dataOutSz);
132  }
133  memmove(dataOut, dataRd, std::min(packetSize, dataOutSz));
134 
135  int status = munmap(dataRd, packetSize);
136  if (status < 0)
137  {
138  AAMPLOG_WARN("Failed to unmap the Shared memory object %d", errno);
139  return false;
140  }
141 
142  return true;
143 }
AampLogManager
AampLogManager Class.
Definition: AampLogManager.h:150
AampConfig.h
Configurations for AAMP.
AampMemoryHandleCloser
This just closes a file on descope.
Definition: AampMemorySystem.h:72
AampSharedMemorySystem.h
Handles the functionalities for shared memory communication.
AampSharedMemorySystem::encode
virtual bool encode(const uint8_t *dataIn, uint32_t dataInSz, std::vector< uint8_t > &dataOut)
Encode a block of data to send over the divide.
Definition: AampSharedMemorySystem.cpp:49
AampSharedMemoryInterchangeBuffer::dataSize
uint32_t dataSize
Definition: AampSharedMemorySystem.h:43
AampSharedMemorySystem::AampSharedMemorySystem
AampSharedMemorySystem(AampLogManager *logObj)
AampSharedMemorySystem constructor.
Definition: AampSharedMemorySystem.cpp:37
AampSharedMemorySystem::~AampSharedMemorySystem
virtual ~AampSharedMemorySystem()
AampSharedMemorySystem Destructor.
Definition: AampSharedMemorySystem.cpp:43
AampSharedMemoryInterchangeBuffer
Holds the Shared memory details.
Definition: AampSharedMemorySystem.h:38
AAMPMemorySystem
Handles the operations for AAMP memory managemnts.
Definition: AampMemorySystem.h:37
AampSharedMemorySystem::decode
virtual bool decode(const uint8_t *dataIn, uint32_t dataInSz, uint8_t *dataOut, uint32_t dataOutSz)
Decode from getting back.
Definition: AampSharedMemorySystem.cpp:102