summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_RingBuffer/src/ns_ringbuffer.cpp
blob: 1fbc5b5724e9717e06f331be20b500f0ebee0dec (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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
/*
 * @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_NSRingBuffer
/// \brief    This file contains implementation of class CNSRingBuffer.
///           This class provides API to open, read, write and close ring buffer
///
////////////////////////////////////////////////////////////////////////////////////////////////////

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

#include <native_service/ns_ringbuffer.h>
#include <native_service/cl_lock.h>

#include <string>

#define RBUF_HEADER_EXT "Hdr"
#define RBUF_RETRY_SLEEP (1000)
#define RBUF_RETRY_COUNT (8)
#define RBUF_PAGE_SIZE (4096)
#define RBUF_PAGE_ALIGN(p) ((p / RBUF_PAGE_SIZE) * RBUF_PAGE_SIZE)

////////////////////////////////////////////////////////////////////////////////////////////////
/// CNSRingBuffer
/// Parameterized Constructor of CNSRingBuffer class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSRingBuffer::CNSRingBuffer(const std::string &f_cMappedFilePath, const UI_32 f_uiSize, int f_lid):
  m_cMappedFilePath(f_cMappedFilePath), m_uiRingBuffSize(f_uiSize), m_pRbufMtx(NULL), m_pRbufHdr(NULL), m_pRbuf(NULL),
  m_lid(f_lid), m_pLockAddr(NULL), m_siProcessLastWrtPage(-1) {  // LCOV_EXCL_BR_LINE 11: except branch
  m_cMappedFileHdrPath = f_cMappedFilePath;
  m_cMappedFileHdrPath.append(RBUF_HEADER_EXT);  // LCOV_EXCL_BR_LINE 11: except branch

  m_cRbufMtxName = f_cMappedFilePath;
  std::size_t found = m_cRbufMtxName.find_first_of('/');
  while (found != std::string::npos) {
    m_cRbufMtxName[found] = '_';
    found = m_cRbufMtxName.find_first_of('/', found + 1);
  }
  m_cRbufMtxName[0] = '/';
  pthread_mutex_init(&m_tOpenMutex, NULL);
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CNSRingBuffer
/// Constructor of CNSRingBuffer class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSRingBuffer::CNSRingBuffer():
  m_cMappedFilePath(""), m_uiRingBuffSize(0), m_pRbufMtx(NULL), m_pRbufHdr(NULL), m_pRbuf(NULL), m_lid(-1),
  m_pLockAddr(NULL), m_siProcessLastWrtPage(-1) {
  pthread_mutex_init(&m_tOpenMutex, NULL);
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// ~CNSRingBuffer
/// Destructor of CNSRingBuffer class
////////////////////////////////////////////////////////////////////////////////////////////////
CNSRingBuffer::~CNSRingBuffer() {
  if (NULL != m_pRbufHdr) {
    // un-map the ring buffer object
    Close();

    m_pRbufHdr = NULL;
    m_pRbufMtx = NULL;
  }
  pthread_mutex_destroy(&m_tOpenMutex);
}

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

  pthread_mutex_lock(&m_tOpenMutex);

  if (NULL == m_pRbufMtx) {
    // Open ring buffer mutex
    if (eFrameworkunifiedStatusOK != (l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pRbufMtx),
      m_cRbufMtxName, sizeof(NSRingBufferMtx)))) {
      if (ENOENT == errno) {
        errno = EOK;


//        if (eFrameworkunifiedStatusOK == CreateRBMutex()) {
        EFrameworkunifiedStatus l_eStatus1 = CreateRBMutex();
        if ((l_eStatus1 == eFrameworkunifiedStatusOK) || (l_eStatus1 == eFrameworkunifiedStatusDuplicate)) {

          l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pRbufMtx), m_cRbufMtxName, sizeof(NSRingBufferMtx));
        }
      }
    } else {
      if (m_pRbufMtx->m_lid != m_lid) {
        // Different Lock ID in constructor parameter and mutex object
        l_eStatus = eFrameworkunifiedStatusInvldParam;
      }
    }
  }

  if (eFrameworkunifiedStatusOK == l_eStatus) {
    if (m_lid >= 0 && NULL == m_pLockAddr) {
      CL_LockProcessInit();
      if ((m_pLockAddr = CL_LockMap(m_lid)) == MAP_FAILED) {
        l_eStatus = eFrameworkunifiedStatusFail;
      }
    }
  }

  if (eFrameworkunifiedStatusOK == l_eStatus) {
    if (NULL == m_pRbufHdr) {
      // Open header ring buffer
      if (eFrameworkunifiedStatusOK != (l_eStatus = Map(reinterpret_cast<PVOID *>(&m_pRbufHdr),
        m_cMappedFileHdrPath, sizeof(NSRingBufferHdr)))) {
        if (ENOENT == errno) {    // ring buffer is not created yet
          errno = EOK;

          // Create ring buffer

//          if (eFrameworkunifiedStatusOK == CreateRBHeader()) {
          EFrameworkunifiedStatus l_eStatus1 = CreateRBHeader();
          if ((l_eStatus1 == eFrameworkunifiedStatusOK) || (l_eStatus1 == eFrameworkunifiedStatusDuplicate)) {

            // Retry to open
            l_eStatus = Map(reinterpret_cast<PVOID *>(&m_pRbufHdr),
              m_cMappedFileHdrPath, sizeof(NSRingBufferHdr));

//          }
          } else {
            l_eStatus = l_eStatus1;
          }

        }
      } else {

#if 0
        LockMtx();

        struct stat st;
        int ret = stat(m_cMappedFilePath.c_str(), &st);

        if (m_uiRingBuffSize == 0) {
          if (ret == 0) {   // LCOV_EXCL_BR_LINE 5:stat's error case.
            // LCOV_EXCL_START 5: stat's error case.
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            if ((UI_32)st.st_size != m_pRbufHdr->m_uiRingBufferSize || (UI_32)st.st_size < m_pRbufHdr->m_uiReadPtr
                || (UI_32)st.st_size < m_pRbufHdr->m_uiWritePtr) {
              memset(m_pRbufHdr, 0, sizeof(NSRingBufferHdr));
              m_pRbufHdr->m_uiRingBufferSize = static_cast<UI_32>(st.st_size);
              unlink(m_cMappedFilePath.c_str());
            }
            m_uiRingBuffSize = static_cast<UI_32>(st.st_size);
            // LCOV_EXCL_STOP
          }
        } else {
          if (m_uiRingBuffSize != m_pRbufHdr->m_uiRingBufferSize || m_uiRingBuffSize < m_pRbufHdr->m_uiReadPtr
              || m_uiRingBuffSize < m_pRbufHdr->m_uiWritePtr || (ret == 0 && m_uiRingBuffSize != (UI_32)st.st_size)) {
            memset(m_pRbufHdr, 0, sizeof(NSRingBufferHdr));
            m_pRbufHdr->m_uiRingBufferSize = m_uiRingBuffSize;
            unlink(m_cMappedFilePath.c_str());
          }
        }

        UnlockMtx();
#endif
        int ret;
        if ( (ret = LockMtx()) == 0) {
          struct stat st;
          int ret = stat(m_cMappedFilePath.c_str(), &st);

          if (m_uiRingBuffSize == 0) {
            if (ret == 0) {
              if ((UI_32)st.st_size != m_pRbufHdr->m_uiRingBufferSize || (UI_32)st.st_size < m_pRbufHdr->m_uiReadPtr ||
                  (UI_32)st.st_size < m_pRbufHdr->m_uiWritePtr) {
                memset(m_pRbufHdr, 0, sizeof(NSRingBufferHdr));
                m_pRbufHdr->m_uiRingBufferSize = static_cast<UI_32>(st.st_size);
                unlink(m_cMappedFilePath.c_str());
              }
              m_uiRingBuffSize = static_cast<UI_32>(st.st_size);
            }
          } else {
            if (m_uiRingBuffSize != m_pRbufHdr->m_uiRingBufferSize || m_uiRingBuffSize < m_pRbufHdr->m_uiReadPtr ||
                m_uiRingBuffSize < m_pRbufHdr->m_uiWritePtr || (ret == 0 && m_uiRingBuffSize != (UI_32)st.st_size)) {
              memset(m_pRbufHdr, 0, sizeof(NSRingBufferHdr));
              m_pRbufHdr->m_uiRingBufferSize = m_uiRingBuffSize;
              unlink(m_cMappedFilePath.c_str());
            }
          }

          UnlockMtx();
        } else {
          fprintf(stderr, "[%s] LockMtx error: %s\n", __PRETTY_FUNCTION__, strerror(ret));
          munmap(m_pRbufHdr, sizeof(NSRingBufferHdr));
          m_pRbufHdr = NULL;
          l_eStatus = eFrameworkunifiedStatusFail;
        }

      }
    }
  }

  if (eFrameworkunifiedStatusOK == l_eStatus && 0 != m_uiRingBuffSize) {
    if (NULL == m_pRbuf) {
      // Open ring buffer data buffer, create if not exists
      if (eFrameworkunifiedStatusOK != (l_eStatus = Map(reinterpret_cast<PVOID *>(&m_pRbuf),
        m_cMappedFilePath, m_uiRingBuffSize))) {
        if (ENOENT == errno) {   // ring buffer is not created yet
          // Create ring buffer

//          if (eFrameworkunifiedStatusOK == CreateRBDataBuffer()) {
          EFrameworkunifiedStatus l_eStatus1 = CreateRBDataBuffer();
          if ((l_eStatus1 == eFrameworkunifiedStatusOK) || (l_eStatus1 == eFrameworkunifiedStatusDuplicate)) {

            // Retry to open
            l_eStatus = Map(reinterpret_cast<PVOID *>(&m_pRbuf),
              m_cMappedFilePath, m_uiRingBuffSize);
          }
        }
      }
    }
  }

  pthread_mutex_unlock(&m_tOpenMutex);

  return l_eStatus;
}

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

////////////////////////////////////////////////////////////////////////////////////////////////
/// Close
/// This function closes the ring buffer object.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::Close() {
  if (NULL != m_pLockAddr) {
    CL_LockUnmap(m_pLockAddr);
  }

  EFrameworkunifiedStatus l_eStatus1 = UnMap(m_pRbufHdr, sizeof(NSRingBufferHdr));
  m_pRbufHdr = NULL;

  EFrameworkunifiedStatus l_eStatus2 = eFrameworkunifiedStatusOK;
  if (0 != m_uiRingBuffSize) {
    l_eStatus2 = UnMap(m_pRbuf, m_uiRingBuffSize);
    m_pRbuf = NULL;
  }

  EFrameworkunifiedStatus l_eStatus3 = UnMap(m_pRbufMtx, sizeof(NSRingBufferMtx));
  m_pRbufMtx = NULL;

  return (eFrameworkunifiedStatusOK != l_eStatus1 || eFrameworkunifiedStatusOK != l_eStatus2 ||
          eFrameworkunifiedStatusOK != l_eStatus3) ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// Read
/// This function reads data from the ring buffer.
////////////////////////////////////////////////////////////////////////////////////////////////
SI_32 CNSRingBuffer::Read(PSTR f_pBuffer, const UI_32 f_uilength) {
  SI_32 l_iReadSize = -1;

  if ((NULL != f_pBuffer) && (NULL != m_pRbufHdr) && (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
    LockMtx();

    // if ring buffer size is changed by some other process, remap the updated buffer size in this process
    // ring buffer size can only be changed if the initial size is 0.
    if (m_uiRingBuffSize != m_pRbufHdr->m_uiRingBufferSize) {
      if (eFrameworkunifiedStatusOK == Map(reinterpret_cast<PVOID *>(&m_pRbuf),
        m_cMappedFilePath, m_pRbufHdr->m_uiRingBufferSize)) {
        m_uiRingBuffSize = m_pRbufHdr->m_uiRingBufferSize;
      }
    }

    if (NULL != m_pRbuf) {
      l_uiRemainSize = m_uiRingBuffSize - m_pRbufHdr->m_uiReadPtr;

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

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

        m_pRbufHdr->m_uiReadPtr += l_uiDataSizeToRead;

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

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

      l_iReadSize = l_uiDataSizeToRead;
    }

    UnlockMtx();
#endif
    int ret;
    if ( (ret = LockMtx()) == 0) {
      // if ring buffer size is changed by some other process, remap the updated buffer size in this process
      // ring buffer size can only be changed if the initial size is 0.
      if (m_uiRingBuffSize != m_pRbufHdr->m_uiRingBufferSize) {
        if (eFrameworkunifiedStatusOK == Map(reinterpret_cast<PVOID *>(&m_pRbuf),
          m_cMappedFilePath, m_pRbufHdr->m_uiRingBufferSize)) {
          m_uiRingBuffSize = m_pRbufHdr->m_uiRingBufferSize;
        }
      }

      if (NULL != m_pRbuf) {
        l_uiRemainSize = m_uiRingBuffSize - m_pRbufHdr->m_uiReadPtr;

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

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

            m_pRbufHdr->m_uiReadPtr += l_uiDataSizeToRead;

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

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

          l_iReadSize = l_uiDataSizeToRead;
        }

        UnlockMtx();
      } else {
        fprintf(stderr, "[%s] LockMtx error: %s\n", __PRETTY_FUNCTION__, strerror(ret));
        l_iReadSize = 0;
    }

  }

  return l_iReadSize;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// Write
/// This function writes the data into the ring buffer.
////////////////////////////////////////////////////////////////////////////////////////////////
SI_32 CNSRingBuffer::Write(PCSTR f_pBuffer, const UI_32 f_uilength) {
  SI_32 l_iWriteSize = -1;

  // size available in buffer
  UI_32 l_uiRemainSize = 0;

  if (NULL != m_pRbufHdr && NULL != m_pRbuf && NULL != f_pBuffer && f_uilength <= m_uiRingBuffSize) {  // LCOV_EXCL_BR_LINE 11:except branch
#if 0
    LockMtx();

    UI_32 l_uiLastPage = RBUF_PAGE_ALIGN(m_pRbufHdr->m_uiWritePtr);
    l_uiRemainSize = m_uiRingBuffSize - m_pRbufHdr->m_uiWritePtr;

    if (m_siProcessLastWrtPage >= 0) {
      if ((UI_32)m_siProcessLastWrtPage != l_uiLastPage) {
        if (madvise(m_pRbuf + m_siProcessLastWrtPage, RBUF_PAGE_SIZE, MADV_DONTNEED) < 0) {
          fprintf(stderr, "[CNSRingBuffer::Write] madvise(%p) error: %s\n",
            m_pRbuf + m_siProcessLastWrtPage, strerror(errno));
        }
      }
    }

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

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

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

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

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

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

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

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

    while (l_uiLastPage != RBUF_PAGE_ALIGN(m_pRbufHdr->m_uiWritePtr)) {
      if (madvise(m_pRbuf + l_uiLastPage, RBUF_PAGE_SIZE, MADV_DONTNEED) < 0) {
        fprintf(stderr, "[CNSRingBuffer::Write] madvise(%p) error: %s\n", m_pRbuf + l_uiLastPage, strerror(errno));
      }
      l_uiLastPage += RBUF_PAGE_SIZE;
      if (l_uiLastPage >= m_uiRingBuffSize) {
        l_uiLastPage = 0;
      }
    }
    m_siProcessLastWrtPage = (SI_32)l_uiLastPage;

    UnlockMtx();

    l_iWriteSize = f_uilength;
#endif
    int ret;
    if ( (ret = LockMtx()) == 0) {
      UI_32 l_uiLastPage = RBUF_PAGE_ALIGN(m_pRbufHdr->m_uiWritePtr);
      l_uiRemainSize = m_uiRingBuffSize - m_pRbufHdr->m_uiWritePtr;

      if (m_siProcessLastWrtPage >= 0) {
        if ((UI_32)m_siProcessLastWrtPage != l_uiLastPage) {
          if (madvise(m_pRbuf + m_siProcessLastWrtPage, RBUF_PAGE_SIZE, MADV_DONTNEED) < 0) {
            fprintf(stderr, "[CNSRingBuffer::Write] madvise(%p) error: %s\n",
                    m_pRbuf + m_siProcessLastWrtPage, strerror(errno));
          }
        }
      }

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

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

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

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

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

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

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

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

      while (l_uiLastPage != RBUF_PAGE_ALIGN(m_pRbufHdr->m_uiWritePtr)) {
        if (madvise(m_pRbuf + l_uiLastPage, RBUF_PAGE_SIZE, MADV_DONTNEED) < 0) {
          fprintf(stderr, "[CNSRingBuffer::Write] madvise(%p) error: %s\n",
                  m_pRbuf + l_uiLastPage, strerror(errno));
        }
        l_uiLastPage += RBUF_PAGE_SIZE;
        if (l_uiLastPage >= m_uiRingBuffSize) {
          l_uiLastPage = 0;
        }
      }
      m_siProcessLastWrtPage = (SI_32)l_uiLastPage;

      UnlockMtx();

      l_iWriteSize = f_uilength;
    } else {
      fprintf(stderr, "[%s] LockMtx error: %s\n", __PRETTY_FUNCTION__, strerror(ret));
      l_iWriteSize = 0;
    }

  }

  return l_iWriteSize;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// DumpToFile
/// This function writes all the data in the buffer into provided file f_pPath.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::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_pRbufHdr) {
      // Open file
      if (-1 != (fd = open(f_pPath, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0640))) {
        if (NULL != m_pRbuf && 0 != m_uiRingBuffSize) {

#if 0
          LockMtx();
          // Write buffer data to file
          if (m_pRbufHdr->m_bIsFull) {
            // Buffer has full of data (read data from write pointer)
            if (-1 != (l_iSize = write(fd, m_pRbuf + m_pRbufHdr->m_uiWritePtr,
              m_uiRingBuffSize - m_pRbufHdr->m_uiWritePtr))) {
              *f_uiDumpSize += static_cast<UI_32>(l_iSize);
            } else {
              l_eStatus = eFrameworkunifiedStatusErrOther;
            }
          }

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

          UnlockMtx();
#endif
          int ret;
          if ( (ret = LockMtx()) == 0) {
            // Write buffer data to file
            if (m_pRbufHdr->m_bIsFull) {
              // Buffer has full of data (read data from write pointer)
              if (-1 != (l_iSize = write(fd, m_pRbuf + m_pRbufHdr->m_uiWritePtr,
                m_uiRingBuffSize - m_pRbufHdr->m_uiWritePtr))) {

//                *f_uiDumpSize += l_iSize;
                *f_uiDumpSize += static_cast<UI_32>(l_iSize);
              } else {
                l_eStatus = eFrameworkunifiedStatusErrOther;
              }
            }

            if (-1 != (l_iSize = write(fd, m_pRbuf, m_pRbufHdr->m_uiWritePtr))) {

//              *f_uiDumpSize += l_iSize;
              *f_uiDumpSize += static_cast<UI_32>(l_iSize);
            }

            UnlockMtx();
          } else {
            fprintf(stderr, "[%s] LockMtx error: %s\n", __PRETTY_FUNCTION__, strerror(ret));
          }

        } else if (NULL == m_pRbuf && 0 != m_uiRingBuffSize) {
          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 CNSRingBuffer::GetSize() {
  SI_32 l_uiReadSize = -1;

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

  return l_uiReadSize;
}

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

  if (NULL != m_pRbufHdr) {

#if 0
    LockMtx();

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

    UnlockMtx();
#endif
    int ret;
    if ((ret = LockMtx()) == 0) {
      // Initialize the r/w pointers
      m_pRbufHdr->m_uiReadPtr = 0;
      m_pRbufHdr->m_uiWritePtr = 0;
      m_pRbufHdr->m_uiUnReadSize = 0;
      m_pRbufHdr->m_bIsFull = FALSE;

      UnlockMtx();
    } else {
      fprintf(stderr, "[%s] LockMtx error: %s\n", __PRETTY_FUNCTION__, strerror(ret));
    }

  } else {
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}

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

  if (NULL != m_pRbufHdr) {

#if 0
    LockMtx();

    // Initialize the r/w pointers
    m_pRbufHdr->m_uiReadPtr = m_pRbufHdr->m_uiWritePtr;
    m_pRbufHdr->m_uiUnReadSize = 0;

    UnlockMtx();
#endif
    int ret;
    if ( (ret = LockMtx()) == 0) {
      // Initialize the r/w pointers
      m_pRbufHdr->m_uiReadPtr = m_pRbufHdr->m_uiWritePtr;
      m_pRbufHdr->m_uiUnReadSize = 0;

      UnlockMtx();
    } else {
      fprintf(stderr, "[%s] LockMtx error: %s\n", __PRETTY_FUNCTION__, strerror(ret));
    }

  } else {
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}
//
////////////////////////////////////////////////////////////////////////////////////////////////
/// CreateRBMutex
/// This function creates the shared memory object for mutex.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::CreateRBMutex() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

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

  // ring buffer mutex
  NSRingBufferMtx *l_pRbufMtx = NULL;

  if ((!m_cRbufMtxName.empty()) || (m_cRbufMtxName.size() <= NAME_MAX)) {
    // Try to create shared memory
    l_siId = shm_open(m_cRbufMtxName.c_str(), O_CREAT | O_EXCL | O_RDWR, 0640);

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

        if (MAP_FAILED != l_pRbufMtx) {
          if (m_lid == -1) {
            // mutex attribute
            pthread_mutexattr_t l_tMtxAttr = {};

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

            l_eStatus = eFrameworkunifiedStatusOK;
          } else {
            // CL_Lock
            l_eStatus = eFrameworkunifiedStatusOK;
          }
          l_pRbufMtx->m_lid = m_lid;

          // Once initialized un-map the shared memory
          munmap(l_pRbufMtx, sizeof(NSRingBufferMtx));
        }
      }

      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;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CreateRBHeader
/// This function creates the ring buffer object for header.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::CreateRBHeader() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  // file descriptor of ring buffer
  SI_32 l_siFd = -1;

  // ring buffer headers
  NSRingBufferHdr *l_pRbufHdr = NULL;

  if (!m_cMappedFileHdrPath.empty()) {  // LCOV_EXCL_BR_LINE 11: except branch
    // Try to create ring buffer
    l_siFd = open(m_cMappedFileHdrPath.c_str(), O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0640);

    if (-1 != l_siFd) {
      // Set the size of ring buffer
      if (-1 != ftruncate(l_siFd, sizeof(NSRingBufferHdr))) {
        // Map the ring buffer
        l_pRbufHdr = reinterpret_cast<NSRingBufferHdr *>(mmap(NULL,
          sizeof(NSRingBufferHdr), (PROT_READ | PROT_WRITE), MAP_SHARED, l_siFd, 0));

        if (MAP_FAILED != l_pRbufHdr) {

#if 0
          LockMtx();

          // Initialize the r/w pointers
          l_pRbufHdr->m_uiReadPtr = 0;
          l_pRbufHdr->m_uiWritePtr = 0;
          l_pRbufHdr->m_uiUnReadSize = 0;
          l_pRbufHdr->m_bIsFull = FALSE;
          l_pRbufHdr->m_uiRingBufferSize = m_uiRingBuffSize;

          UnlockMtx();
#endif
          int ret;
          if ( (ret = LockMtx()) == 0) {
            // Initialize the r/w pointers
            l_pRbufHdr->m_uiReadPtr = 0;
            l_pRbufHdr->m_uiWritePtr = 0;
            l_pRbufHdr->m_uiUnReadSize = 0;
            l_pRbufHdr->m_bIsFull = FALSE;
            l_pRbufHdr->m_uiRingBufferSize = m_uiRingBuffSize;

            UnlockMtx();
            l_eStatus = eFrameworkunifiedStatusOK;
          } else {
            fprintf(stderr, "[%s] LockMtx error: %s\n", __PRETTY_FUNCTION__, strerror(ret));
            l_eStatus = eFrameworkunifiedStatusFail;
          }


          // Once initialized un-map the ring buffer
          munmap(l_pRbufHdr, sizeof(NSRingBufferHdr));
        }
      }

      close(l_siFd);
    } else if (EEXIST == errno) {
      // ring buffer is already created
      l_eStatus = eFrameworkunifiedStatusDuplicate;
    } else {
      // do nothing
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CreateRBDataBuffer
/// This function creates the ring buffer object for data buffer.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::CreateRBDataBuffer() {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  // file descriptor of ring buffer
  SI_32 l_siFd = -1;

  if (!m_cMappedFilePath.empty()) {
    // Try to create ring buffer
    l_siFd = open(m_cMappedFilePath.c_str(), O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0640);

    if (-1 != l_siFd) {
      // Set the size of ring buffer
      if (-1 != ftruncate(l_siFd, m_uiRingBuffSize)) {
        l_eStatus = eFrameworkunifiedStatusOK;
      }

      close(l_siFd);
    } else if (EEXIST == errno) {
      // ring buffer is already created
      l_eStatus = eFrameworkunifiedStatusDuplicate;
    } else {
      // do nothing
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// MapRBHeader
/// This function open and maps in process space.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::Map(PVOID *f_pRbuf, const std::string &f_cMappedFile, const UI_32 f_uiRbufSize) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;

  // file descriptor of ring buffer
  SI_32 l_siFd = -1;

  // ring buffer headers
  PVOID l_pRbuf = NULL;

  // Open ring buffer
  l_siFd = open(f_cMappedFile.c_str(), O_RDWR | O_CLOEXEC, NULL);

  if (-1 != l_siFd) {
    if (CheckSize(l_siFd, f_uiRbufSize) == eFrameworkunifiedStatusOK) {
      // Map the ring buffer into its memory space
      l_pRbuf = mmap(NULL, f_uiRbufSize, (PROT_READ | PROT_WRITE), MAP_SHARED, l_siFd, 0);

      if (MAP_FAILED != l_pRbuf) {
        *f_pRbuf = l_pRbuf;
        l_eStatus = eFrameworkunifiedStatusOK;
      }
    }

    close(l_siFd);
  }

  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// CheckSize
/// This function check mmap size
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::CheckSize(int fd, off_t size) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
  struct stat st;
  int i;

  for (i = 0; i < RBUF_RETRY_COUNT; i++) {
    if (fstat(fd, &st) < 0) {
      fprintf(stderr, "[CNSRingBuffer::CheckSize] fstat error: %s\n", strerror(errno));
      goto out;
    }
    if (st.st_size == size) {
      l_eStatus = eFrameworkunifiedStatusOK;
      break;
    }
    usleep(RBUF_RETRY_SLEEP);
  }
  if (i >= RBUF_RETRY_COUNT) {
    errno = ENOMEM;
  }

out:
  return l_eStatus;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// MapSM
/// This function shm_open and maps the shared memory in process space.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::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, 0640);

  if (-1 != l_siId) {
    if (CheckSize(l_siId, f_uiShmSize) == eFrameworkunifiedStatusOK) {
      // 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;
}

////////////////////////////////////////////////////////////////////////////////////////////////
/// UnMap
/// This function unmaps object.
////////////////////////////////////////////////////////////////////////////////////////////////
EFrameworkunifiedStatus CNSRingBuffer::UnMap(PVOID f_pRbuf, const UI_32 f_uiRbufSize) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  // Un-map the ring buffer
  if (NULL != f_pRbuf) {
    if (0 != munmap(f_pRbuf, f_uiRbufSize)) {
      l_eStatus = eFrameworkunifiedStatusFail;
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}


//void CNSRingBuffer::LockMtx() {
//  if (m_lid == -1) {
//    pthread_mutex_lock(&m_pRbufMtx->m_tBufMutex);
//  } else {
//    CL_LockGet(m_pLockAddr);
//  }
//}
int CNSRingBuffer::LockMtx() {
  int ret = 0;

  if (m_lid == -1) {
    ret = pthread_mutex_lock(&m_pRbufMtx->m_tBufMutex);
  } else {
    ret = CL_LockGet(m_pLockAddr);
  }

  return ret;
}


void CNSRingBuffer::UnlockMtx() {
  if (m_lid == -1) {
    pthread_mutex_unlock(&m_pRbufMtx->m_tBufMutex);
  } else {
    CL_LockRelease(m_pLockAddr);
  }
}