summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem_writer.cpp
blob: 25614157fcf7b6939c3a5c6fbe3b4989926382e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NSSharedMemory
/// \brief    This file contains implementation of class CNSSharedMemReader.
///           This class provides API to open, close and perform read operation on shared memory.
///
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Files
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <native_service/ns_sharedmem.h>
#include <native_service/ns_sharedmem_writer.h>
#include <string>

////////////////////////////////////////////////////////////////////////////////////////////////
/// CNSSharedMemWriter
/// Constructor of CNSSharedMemWriter class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSSharedMemWriter::CNSSharedMemWriter(): m_pShmWriter(NULL) {
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CNSSharedMemWriter
/// Parameterized Constructor of CNSSharedMemWriter class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSSharedMemWriter::CNSSharedMemWriter(const std::string &f_cSharedMemName, const UI_32 f_uiSize) {
  m_pShmWriter = new(std::nothrow) CNSSharedMem(f_cSharedMemName, f_uiSize);
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// ~CNSSharedMemWriter
/// Destructor of CNSSharedMemWriter class.
/// Closes the shared memory, if it is open.
////////////////////////////////////////////////////////////////////////////////////////////////
CNSSharedMemWriter::~CNSSharedMemWriter() {
  if (NULL != m_pShmWriter) {
    if (m_pShmWriter->IsOpen()) {
      m_pShmWriter->Close();
    }

    delete m_pShmWriter;
    m_pShmWriter = NULL;
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// Open
/// This function opens and maps the shared memory object.
/// It creates the shared memory if it does not exists.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMemWriter::Open() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (NULL != m_pShmWriter) {
    l_eStatus = m_pShmWriter->Open();
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// IsOpen
/// This function is used to check whether the shared memory buffer is opened or not.
////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CNSSharedMemWriter::IsOpen() {
  BOOL l_bOpen = FALSE;

  if (NULL != m_pShmWriter) {
    l_bOpen = m_pShmWriter->IsOpen();
  }

  return l_bOpen;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// Close
/// This function unmaps the shared memory object.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMemWriter::Close() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (NULL != m_pShmWriter) {
    l_eStatus = m_pShmWriter->Close();
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// Write
/// This function writes the data into the shared memory.
////////////////////////////////////////////////////////////////////////////////////////////////
SI_32 CNSSharedMemWriter::Write(PCSTR buffer, const UI_32 f_uilength) {
  SI_32 l_iWriteSize = NS_SHM_ERROR;

  if ((NULL != m_pShmWriter) && (NULL != buffer) && (0 != f_uilength)) {
    l_iWriteSize = m_pShmWriter->Write(buffer, f_uilength);
  }

  return l_iWriteSize;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// ClearBuf
/// This function clears the shared memory buffer.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMemWriter::ClearBuf() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (NULL != m_pShmWriter) {
    l_eStatus = m_pShmWriter->ClearBuf();
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}