summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem.cpp
blob: fe1e9bc47f058befedf3333d38c918d7797af791 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
 * @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 CNSSharedMem.
///           This class provides API to open, read, write and close shared memory
///
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Files
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

#include <native_service/ns_sharedmem.h>
#include <native_service/ns_transmit_log.h>

#include <string>

static CNSSharedMem *g_pTransmitLogSharedBuf = NULL;

#define SHM_HEADER_EXT      "Hdr"

#ifdef __cplusplus
extern "C" {
#endif


////////////////////////////////////////////////////////////////////////////////////////////
/// NSSharedMemTransmitLogOpen
/// Open the shared memory for transmit logging
////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus NSSharedMemTransmitLogOpen() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (NULL == g_pTransmitLogSharedBuf) {
    // Create the instance
    g_pTransmitLogSharedBuf = new(std::nothrow) CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE);
  }

  if (NULL != g_pTransmitLogSharedBuf) {
    if (!g_pTransmitLogSharedBuf->IsOpen()) {
      // maps the shared memory buffer
      l_eStatus = g_pTransmitLogSharedBuf->Open();
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////
/// NSSharedMemTransmitLogClose
/// Close the transmit logging shared memory
////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus NSSharedMemTransmitLogClose() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (NULL != g_pTransmitLogSharedBuf) {
    // un-map the shared memory object
    l_eStatus = g_pTransmitLogSharedBuf->Close();

    delete g_pTransmitLogSharedBuf;
    g_pTransmitLogSharedBuf = NULL;
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////
/// NSSharedMemReadTransmitLog
/// Reads transmit log from the shared memory buffer.
////////////////////////////////////////////////////////////////////////////////////////////
SI_32 NSSharedMemReadTransmitLog(PSTR f_pBuffer, const UI_32 f_uiLength, const BOOL f_bBlock) {
  // no. of bytes read
  SI_32 l_iReadSize = NS_SHM_ERROR;

  if (NULL != g_pTransmitLogSharedBuf && NULL != f_pBuffer) {
    // Writes log data into shared memory buffer
    l_iReadSize = g_pTransmitLogSharedBuf->Read(f_pBuffer, f_uiLength, f_bBlock);
  }

  return l_iReadSize;
}

////////////////////////////////////////////////////////////////////////////////////////////
/// NSSharedMemWriteTransmitLog
/// Write transmit log into the shared memory buffer.
////////////////////////////////////////////////////////////////////////////////////////////
SI_32 NSSharedMemWriteTransmitLog(PCSTR f_pBuffer, const UI_32 f_uiLength) {
  // no. of bytes read
  SI_32 l_iWriteSize = NS_SHM_ERROR;

  if (NULL != g_pTransmitLogSharedBuf && NULL != f_pBuffer) {
    // Writes log data into shared memory buffer
    l_iWriteSize = g_pTransmitLogSharedBuf->Write(f_pBuffer, f_uiLength);
  }

  return l_iWriteSize;
}

////////////////////////////////////////////////////////////////////////////////////////////
/// NSSharedMemDumpTransmitLogToFile
/// Dump transmit log from the shared memory buffer into file.
////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus NSSharedMemDumpTransmitLogToFile(PCSTR f_pPath, PUI_32 f_puiDumpSize) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  if (NULL != g_pTransmitLogSharedBuf && NULL != f_pPath) {
    // Writes log data into shared memory buffer
    l_eStatus = g_pTransmitLogSharedBuf->DumpToFile(f_pPath, f_puiDumpSize);
  }

  return l_eStatus;
}

// Used only in nstest_sharedmem.
#if defined(SHM_UNITTEST_ENABLE)
////////////////////////////////////////////////////////////////////////////////////////////
/// NSSharedMemTransmitLogIsOpen
////////////////////////////////////////////////////////////////////////////////////////////
BOOL NSSharedMemTransmitLogIsOpen() {
  if (g_pTransmitLogSharedBuf != NULL) {
    return g_pTransmitLogSharedBuf->IsOpen();
  } else {
    return FALSE;
  }
}
#endif

#ifdef __cplusplus
}
#endif

////////////////////////////////////////////////////////////////////////////////////////////////
/// CNSSharedMem
/// Parameterized Constructor of CNSSharedMem class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSSharedMem::CNSSharedMem(const std::string &f_cSharedMemName, const UI_32 f_uiSize):
  m_cShmName(f_cSharedMemName), m_uiShmBuffSize(f_uiSize), m_pShmHdr(NULL), m_pShmBuff(NULL) {
  m_cShmHdrName = f_cSharedMemName;
  m_cShmHdrName.append(SHM_HEADER_EXT);
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CNSSharedMem
/// Constructor of CNSSharedMem class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSSharedMem::CNSSharedMem():
  m_cShmName(""), m_uiShmBuffSize(0), m_pShmHdr(NULL), m_pShmBuff(NULL) {
  // do nothing
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// ~CNSSharedMem
/// Destructor of CNSSharedMem class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSSharedMem::~CNSSharedMem() {
  if (NULL != m_pShmHdr) {
    // un-map the shared memory object
    Close();

    m_pShmHdr = NULL;
  }

  // TODO(framework_unified): currently shared memory is not being unlinked,
  // we need to find a method where we can unlink the sharedmem
  // shm_unlink(m_cShmName.c_str());
}

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

// Used only in nstest_sharedmem.
#if defined(SHM_UNITTEST_ENABLE)
  if (getenv(NSTEST_FAIL_SHAREDMEM_OPEN) != NULL) {
    return l_eStatus;
  }
#endif

  if (NULL == m_pShmHdr) {
    // Open header shared memory
    if (eFrameworkunifiedStatusOK != (l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmHdr),
      m_cShmHdrName, sizeof(NSSharedBufferHdr)))) {
      if (ENOENT == errno) {  // Shared memory is not created yet
        errno = EOK;

        // Create shared memory
        if (eFrameworkunifiedStatusOK == CreateSMHeader()) {
          // Retry to open
          l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmHdr), m_cShmHdrName, sizeof(NSSharedBufferHdr));
        }
      }
    } else {
      pthread_mutex_lock(&m_pShmHdr->m_tBufMutex);

      // if shared memory buffer is created with size 0, then set new size in header if any change
      if (0 == m_pShmHdr->m_uiShMemSize && 0 < m_uiShmBuffSize) {
        m_pShmHdr->m_uiShMemSize = m_uiShmBuffSize;
      } else if (0 < m_pShmHdr->m_uiShMemSize && 0 == m_uiShmBuffSize) {
        m_uiShmBuffSize = m_pShmHdr->m_uiShMemSize;
      } else {
        // do nothing
      }

      pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex);
    }
  }

  if (eFrameworkunifiedStatusOK == l_eStatus && 0 != m_uiShmBuffSize) {
    if (NULL == m_pShmBuff) {
      // Open shared memory data buffer, create if not exists
      if (eFrameworkunifiedStatusOK != (l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmBuff), m_cShmName, m_uiShmBuffSize))) {
        if (ENOENT == errno) {  // Shared memory is not created yet
          // Create shared memory
          if (eFrameworkunifiedStatusOK == CreateSMDataBuffer()) {
            // Retry to open
            l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmBuff), m_cShmName, m_uiShmBuffSize);
          }
        }
      }
    } else {
      l_eStatus = eFrameworkunifiedStatusErrOther;
    }
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// IsOpen
/// This function is used to check whether the shared memory buffer is opened or not.
////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CNSSharedMem::IsOpen() {
  return NULL == m_pShmHdr ? FALSE : TRUE;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// Close
/// This function closes the shared memory object.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMem::Close() {
  EFrameworkunifiedStatus l_eStatus1 = UnMapSM(m_pShmHdr, sizeof(NSSharedBufferHdr));
  m_pShmHdr = NULL;

  EFrameworkunifiedStatus l_eStatus2 = eFrameworkunifiedStatusOK;
  if (0 != m_uiShmBuffSize) {
    l_eStatus2 = UnMapSM(m_pShmBuff, m_uiShmBuffSize);
    m_pShmBuff = NULL;
  }
  /*
   * todo
   * Even if an error occurs due to eFrameworkunifiedStatusNullPointer when UnMapSM fails,
   * the error type cannot be determined by the caller because it is rounded to eFrameworkunifiedStatusFail
   * if the API final determination is not OK.
   */
  return (eFrameworkunifiedStatusOK != l_eStatus1 || eFrameworkunifiedStatusOK != l_eStatus2) ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// Read
/// This function reads data from the shared memory.
////////////////////////////////////////////////////////////////////////////////////////////////
SI_32 CNSSharedMem::Read(PSTR f_pBuffer, const UI_32 f_uilength, const BOOL f_bBlock) {
  SI_32 l_iReadSize = NS_SHM_ERROR;

  if ((NULL != f_pBuffer) && (NULL != m_pShmHdr) && (0 != f_uilength)) {
    UI_32 l_uiDataSizeToRead = 0;

    // Remaining buffer size from read pointer to end of the buffer
    UI_32 l_uiRemainSize = 0;

    if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) {
      if ((TRUE == f_bBlock) && (0 == m_pShmHdr->m_uiUnReadSize)) {
        pthread_cond_wait(&m_pShmHdr->m_tCondVar, &m_pShmHdr->m_tBufMutex);
      }

      // if shared memory buffer size is changed by some other process, remap the updated buffer size in this process
      // shared memory buffer size can only be changed if the initial size is 0.
      if (m_uiShmBuffSize != m_pShmHdr->m_uiShMemSize) {
        if (eFrameworkunifiedStatusOK == MapSM(reinterpret_cast<PVOID *>(&m_pShmBuff), m_cShmName, m_pShmHdr->m_uiShMemSize)) {
          m_uiShmBuffSize = m_pShmHdr->m_uiShMemSize;
        }
      }

      if (NULL != m_pShmBuff) {
        l_uiRemainSize = m_uiShmBuffSize - m_pShmHdr->m_uiReadPtr;

        // Round read data size depending on un-read data size in the buffer
        l_uiDataSizeToRead = m_pShmHdr->m_uiUnReadSize < f_uilength ? m_pShmHdr->m_uiUnReadSize : f_uilength;

        if (l_uiRemainSize < l_uiDataSizeToRead) {
          // Wrapping read
          memcpy(f_pBuffer, m_pShmBuff + m_pShmHdr->m_uiReadPtr, l_uiRemainSize);
          memcpy(f_pBuffer + l_uiRemainSize, m_pShmBuff, l_uiDataSizeToRead - l_uiRemainSize);
          m_pShmHdr->m_uiReadPtr = l_uiDataSizeToRead - l_uiRemainSize;
        } else {
          memcpy(f_pBuffer, m_pShmBuff + m_pShmHdr->m_uiReadPtr, l_uiDataSizeToRead);

          m_pShmHdr->m_uiReadPtr += l_uiDataSizeToRead;

          // Read pointer is the end of the buffer
          if (m_pShmHdr->m_uiReadPtr == m_uiShmBuffSize) {
            m_pShmHdr->m_uiReadPtr = 0;
          }
        }

        m_pShmHdr->m_uiUnReadSize -= l_uiDataSizeToRead;  // Update un-read data size

        l_iReadSize = l_uiDataSizeToRead;
      }

      pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex);
    }
  }

  return l_iReadSize;
}

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

  // size available in buffer
  UI_32 l_uiRemainSize = 0;

  if (NULL != m_pShmHdr && NULL != m_pShmBuff && NULL != f_pBuffer && f_uilength <= m_uiShmBuffSize) {
    if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) {
      l_uiRemainSize = m_uiShmBuffSize - m_pShmHdr->m_uiWritePtr;

      // Write data to the buffer
      if (l_uiRemainSize < f_uilength) {
        // Wrapping write
        memcpy(m_pShmBuff + m_pShmHdr->m_uiWritePtr, f_pBuffer, l_uiRemainSize);
        memcpy(m_pShmBuff, f_pBuffer + l_uiRemainSize, f_uilength - l_uiRemainSize);

        // Update the write pointer
        m_pShmHdr->m_uiWritePtr = f_uilength - l_uiRemainSize;

        // The buffer is full of valid data
        m_pShmHdr->m_bIsFull = TRUE;
      } else {
        memcpy(m_pShmBuff + m_pShmHdr->m_uiWritePtr, f_pBuffer, f_uilength);

        // Update the write pointer
        m_pShmHdr->m_uiWritePtr += f_uilength;

        // Write pointer is the end of the buffer
        if (m_pShmHdr->m_uiWritePtr == m_uiShmBuffSize) {
          m_pShmHdr->m_uiWritePtr = 0;

          // The buffer is full of valid data
          m_pShmHdr->m_bIsFull = TRUE;
        }
      }

      // Update un-read data size
      m_pShmHdr->m_uiUnReadSize += f_uilength;

      // Set read pointer to be same as write pointer if write pointer exceeds the read pointer
      if (m_uiShmBuffSize < m_pShmHdr->m_uiUnReadSize) {
        m_pShmHdr->m_uiReadPtr = m_pShmHdr->m_uiWritePtr;
        m_pShmHdr->m_uiUnReadSize = m_uiShmBuffSize;
      }

      pthread_cond_signal(&m_pShmHdr->m_tCondVar);

      pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex);

      l_iWriteSize = f_uilength;
    }
  }

  return l_iWriteSize;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// DumpToFile
/// This function writes all the data in the buffer into provided file f_pPath.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMem::DumpToFile(PCSTR f_pPath, PUI_32 f_uiDumpSize) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  ssize_t l_iSize = 0;

  SI_32 fd = -1;

  if (NULL == f_uiDumpSize) {
    return eFrameworkunifiedStatusNullPointer;
  }
  *f_uiDumpSize = 0;

  if (NULL != f_pPath) {
    if (NULL != m_pShmHdr) {
      // Open file
      if (-1 != (fd = open(f_pPath, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRWXU | S_IRWXG | S_IRWXO))) {
        if (NULL != m_pShmBuff && 0 != m_uiShmBuffSize) {
          if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) {
            // Write buffer data to file
            if (m_pShmHdr->m_bIsFull) {
              // Buffer has full of data (read data from write pointer)
              if (-1 != (l_iSize = write(fd, m_pShmBuff + m_pShmHdr->m_uiWritePtr,
                m_uiShmBuffSize - m_pShmHdr->m_uiWritePtr))) {
                *f_uiDumpSize += static_cast<UI_32>(l_iSize);
              } else {
                l_eStatus = eFrameworkunifiedStatusErrOther;
              }
            }

            if (-1 != (l_iSize = write(fd, m_pShmBuff, m_pShmHdr->m_uiWritePtr))) {
              *f_uiDumpSize += static_cast<UI_32>(l_iSize);
            }

            if (0 != pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex)) {
              l_eStatus = eFrameworkunifiedStatusSemUnlockFail;
            }
          } else {
            l_eStatus = eFrameworkunifiedStatusSemLockFail;
          }
        } else if (NULL == m_pShmBuff && 0 != m_uiShmBuffSize) {
          l_eStatus = eFrameworkunifiedStatusFail;
        } else {
          // do nothing
        }

        // Sync the file to force I/O operation completed
        fsync(fd);

        close(fd);
      } else {
        l_eStatus = eFrameworkunifiedStatusFileLoadError;
      }
    } else {
      l_eStatus = eFrameworkunifiedStatusFail;
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// GetSize
/// This function returns the number of unread bytes which can be read by Read().
////////////////////////////////////////////////////////////////////////////////////////////////
SI_32 CNSSharedMem::GetSize() {
  SI_32 l_uiReadSize = NS_SHM_ERROR;

  if (NULL != m_pShmHdr) {
    l_uiReadSize = m_pShmHdr->m_uiUnReadSize;
  }

  return l_uiReadSize;
}

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

  if (NULL != m_pShmHdr) {
    if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) {
      // Initialize the r/w pointers
      m_pShmHdr->m_uiReadPtr = 0;
      m_pShmHdr->m_uiWritePtr = 0;
      m_pShmHdr->m_uiUnReadSize = 0;
      m_pShmHdr->m_bIsFull = FALSE;

      if (0 != pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex)) {
        l_eStatus = eFrameworkunifiedStatusSemUnlockFail;
      }
    } else {
      l_eStatus = eFrameworkunifiedStatusSemLockFail;
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// SetReadPtrToWritePtr
/// This function sets the position of read ptr to write ptr in buffer.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMem::SetReadPtrToWritePtr() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (NULL != m_pShmHdr) {
    if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) {  // LCOV_EXCL_BR_LINE 5: pthread_mutex_lock's error case
      // Initialize the r/w pointers
      m_pShmHdr->m_uiReadPtr = m_pShmHdr->m_uiWritePtr;
      m_pShmHdr->m_uiUnReadSize = 0;

      if (0 != pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex)) {  // LCOV_EXCL_BR_LINE 5: pthread_mutex_unlock's error
        l_eStatus = eFrameworkunifiedStatusSemUnlockFail;
      }
    } else {
      l_eStatus = eFrameworkunifiedStatusSemLockFail;
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CreateSMHeader
/// This function creates the shared memory object for header.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMem::CreateSMHeader() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  // file descriptor of shared memory
  SI_32 l_siId = -1;

  // shared memory buffer headers
  NSSharedBufferHdr *l_pShmHdr = NULL;

  if ((!m_cShmHdrName.empty()) && (m_cShmHdrName.size() <= NAME_MAX)) {
    // Try to create shared memory
    l_siId = shm_open(m_cShmHdrName.c_str(), O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);

    if (-1 != l_siId) {
      // Set the size of shared memory
      if (-1 != ftruncate(l_siId, sizeof(NSSharedBufferHdr))) {
        // Map the shared memory
        l_pShmHdr = reinterpret_cast<NSSharedBufferHdr *>(mmap(NULL, sizeof(NSSharedBufferHdr),
          (PROT_READ | PROT_WRITE), MAP_SHARED, l_siId, 0));

        if (MAP_FAILED != l_pShmHdr) {
          // mutex attribute
          pthread_mutexattr_t l_tMtxAttr = {};
          pthread_condattr_t l_tCondAttr = {};

          // Initialize mutex
          pthread_mutexattr_init(&l_tMtxAttr);
          pthread_mutexattr_setpshared(&l_tMtxAttr, PTHREAD_PROCESS_SHARED);
          pthread_mutex_init(&l_pShmHdr->m_tBufMutex, &l_tMtxAttr);
          pthread_mutexattr_destroy(&l_tMtxAttr);

          pthread_condattr_init(&l_tCondAttr);
          pthread_condattr_setpshared(&l_tCondAttr, PTHREAD_PROCESS_SHARED);
          pthread_cond_init(&l_pShmHdr->m_tCondVar, &l_tCondAttr);
          pthread_condattr_destroy(&l_tCondAttr);

          pthread_mutex_lock(&l_pShmHdr->m_tBufMutex);

          // Initialize the r/w pointers
          l_pShmHdr->m_uiReadPtr = 0;
          l_pShmHdr->m_uiWritePtr = 0;
          l_pShmHdr->m_uiUnReadSize = 0;
          l_pShmHdr->m_bIsFull = FALSE;
          l_pShmHdr->m_uiShMemSize = m_uiShmBuffSize;

          pthread_mutex_unlock(&l_pShmHdr->m_tBufMutex);

          // Once initialized un-map the shared memory
          munmap(l_pShmHdr, sizeof(NSSharedBufferHdr));

          l_eStatus = eFrameworkunifiedStatusOK;
        }
      }

      close(l_siId);
    } else if (EEXIST == errno) {
      // Shared memory is already created
      l_eStatus = eFrameworkunifiedStatusDuplicate;
    } else {
      // do nothing
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CreateSMDataBuffer
/// This function creates the shared memory object for data buffer.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMem::CreateSMDataBuffer() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  // file descriptor of shared memory
  SI_32 l_siId = -1;

  if ((!m_cShmName.empty()) && (m_cShmName.size() <= NAME_MAX)) {
    // Try to create shared memory
    l_siId = shm_open(m_cShmName.c_str(), O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);

    if (-1 != l_siId) {
      // Set the size of shared memory
      if (-1 != ftruncate(l_siId, m_uiShmBuffSize)) {
        l_eStatus = eFrameworkunifiedStatusOK;
      }

      close(l_siId);
    } else if (EEXIST == errno) {
      // Shared memory is already created
      l_eStatus = eFrameworkunifiedStatusDuplicate;
    } else {
      // do nothing
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// MapSMHeader
/// This function open and maps the shared memory header in process space.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMem::MapSM(PVOID *f_pShMem, const std::string &f_cShmName, const UI_32 f_uiShmSize) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  // file descriptor of shared memory
  SI_32 l_siId = -1;

  // shared memory buffer headers
  PVOID l_pShmBuf = NULL;

  // Open shared memory
  l_siId = shm_open(f_cShmName.c_str(), O_RDWR, 0);

  if (-1 != l_siId) {
    // Map the shared memory into its memory space
    l_pShmBuf = mmap(NULL, f_uiShmSize, (PROT_READ | PROT_WRITE), MAP_SHARED, l_siId, 0);

    if (MAP_FAILED != l_pShmBuf) {
      *f_pShMem = l_pShmBuf;
      l_eStatus = eFrameworkunifiedStatusOK;
    }

    close(l_siId);
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// UnMapSM
/// This function unmaps the shared memory object.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSSharedMem::UnMapSM(PVOID f_pShMem, const UI_32 f_uiShmSize) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  // Un-map the shared memory
  if (NULL != f_pShMem) {
    if (0 != munmap(f_pShMem, f_uiShmSize)) {
      l_eStatus = eFrameworkunifiedStatusFail;
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}