summaryrefslogtreecommitdiff
path: root/qmap/qmap_ipc.h
blob: 2ded811d1d1e6d022990417d9ad792f3e5373123 (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
#ifndef QMAP_IPC_H
#define QMAP_IPC_H

#include "decode_ipc.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <type_traits>

constexpr std::size_t QMapDecodeCapacity = qmap_decode_ipc::max_rows;
constexpr std::size_t QMapDecodeRowSize = qmap_decode_ipc::row_size;

static_assert (QMapDecodeRowSize == 80,
               "QMAP IPC must use the fixed 80-byte decoder row");

enum class QMapClickAction : std::int32_t
{
  None = 0,
  Disarm = 1,
  Select = 2,
  SelectAndEnableTx = 3,
};

using QMapDecodeBlock = qmap_decode_ipc::DecodeRows;

struct QMapClickMailbox
{
  char selectedDecode[QMapDecodeRowSize];
  QMapClickAction action;
};

struct QMapSharedMemory
{
  QMapDecodeBlock decodes;
  QMapClickMailbox click;
};

static_assert (sizeof (std::int32_t) == 4, "QMAP IPC requires 32-bit integers");
static_assert (std::is_standard_layout<QMapClickMailbox>::value,
               "QMAP click mailbox must have a stable field layout");
static_assert (std::is_trivially_copyable<QMapClickMailbox>::value,
               "QMAP click mailbox must support byte-for-byte copying");
static_assert (offsetof (QMapClickMailbox, action) == QMapDecodeRowSize,
               "QMAP click request offset changed");
static_assert (sizeof (QMapClickMailbox) == QMapDecodeRowSize + sizeof (std::int32_t),
               "QMAP click mailbox size changed");
static_assert (std::is_standard_layout<QMapSharedMemory>::value,
               "QMAP shared memory must have a stable field layout");
static_assert (std::is_trivially_copyable<QMapSharedMemory>::value,
               "QMAP shared memory must support byte-for-byte copying");
static_assert (offsetof (QMapSharedMemory, decodes) == 0,
               "QMAP decode block must start the shared segment");
static_assert (offsetof (QMapSharedMemory, click) == sizeof (QMapDecodeBlock),
               "QMAP click mailbox offset changed");
static_assert (sizeof (QMapSharedMemory) == sizeof (QMapDecodeBlock) + sizeof (QMapClickMailbox),
               "QMAP IPC wire layout changed");

constexpr std::size_t QMapSharedMemorySize {
  ((sizeof (QMapSharedMemory) + 15) / 16) * 16
};

static_assert (QMapSharedMemorySize >= qmap_decode_ipc::shared_memory_size,
               "QMAP shared memory must preserve the decoder segment size");
static_assert (sizeof (QMapSharedMemory) <= QMapSharedMemorySize,
               "QMAP IPC exceeds the shared-memory segment");

constexpr bool qmapDecoderRegionAvailable (std::size_t mappedSize) noexcept
{
  return mappedSize >= qmap_decode_ipc::shared_memory_size;
}

constexpr bool qmapClickMailboxAvailable (std::size_t mappedSize) noexcept
{
  return mappedSize >= sizeof (QMapSharedMemory);
}

constexpr std::size_t qmapClearLength (std::size_t mappedSize) noexcept
{
  return std::min (mappedSize, QMapSharedMemorySize);
}

inline void clearQMapSharedMemory (void * mappedData,
                                   std::size_t mappedSize) noexcept
{
  if (mappedData) std::memset (mappedData, 0, qmapClearLength (mappedSize));
}

inline void publishQMapDecodeBlock (QMapSharedMemory& shared,
                                    QMapDecodeBlock& publication) noexcept
{
  publication.nWDecoderBusy = shared.decodes.nWDecoderBusy;
  publication.nWTransmitting = shared.decodes.nWTransmitting;
  publication.kHzRequested = shared.decodes.kHzRequested;
  shared.decodes = publication;
}

inline bool acknowledgeQMapDecodeBatch (QMapDecodeBlock& shared) noexcept
{
  if (shared.nQDecoderDone == 0) return false;
  shared.ndecodes = 0;
  shared.nQDecoderDone = 0;
  return true;
}

#endif // QMAP_IPC_H