Skip to content

Commit dea8b9f

Browse files
Merge pull request #16 from NikolasK-source/main
Release 1.2.1
2 parents 26a61f7 + 733a4b3 commit dea8b9f

File tree

7 files changed

+47
-66
lines changed

7 files changed

+47
-66
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "libs/libmodbus"]
55
path = libs/libmodbus
66
url = https://github.com/stephane/libmodbus
7+
[submodule "libs/cxxshm"]
8+
path = libs/cxxshm
9+
url = https://github.com/NikolasK-source/cxxshm.git

libs/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ target_link_libraries(${Target} PRIVATE rt)
1010

1111
add_subdirectory(cxxopts EXCLUDE_FROM_ALL)
1212
target_link_libraries(${Target} PRIVATE cxxopts)
13+
14+
add_subdirectory(cxxshm EXCLUDE_FROM_ALL)
15+
target_link_libraries(${Target} PRIVATE cxxshm)

libs/cxxshm

Submodule cxxshm added at dfed3b0

src/license.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,4 +560,29 @@ void print_licenses(std::ostream &o) {
560560
o << " Ty Coon, President of Vice" << std::endl;
561561
o << std::endl;
562562
o << " That's all there is to it!" << std::endl;
563+
o << std::endl;
564+
o << std::endl;
565+
o << "cxxshm Library (https://github.com/NikolasK-source/cxxshm)" << std::endl;
566+
o << std::endl;
567+
o << " MIT License" << std::endl;
568+
o << std::endl;
569+
o << " Copyright (c) 2022 Nikolas Koesling" << std::endl;
570+
o << std::endl;
571+
o << " Permission is hereby granted, free of charge, to any person obtaining a copy" << std::endl;
572+
o << " of this software and associated documentation files (the \"Software\"), to deal" << std::endl;
573+
o << " in the Software without restriction, including without limitation the rights" << std::endl;
574+
o << " to use, copy, modify, merge, publish, distribute, sublicense, and/or sell" << std::endl;
575+
o << " copies of the Software, and to permit persons to whom the Software is" << std::endl;
576+
o << " furnished to do so, subject to the following conditions:" << std::endl;
577+
o << std::endl;
578+
o << " The above copyright notice and this permission notice shall be included in all" << std::endl;
579+
o << " copies or substantial portions of the Software." << std::endl;
580+
o << std::endl;
581+
o << " THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" << std::endl;
582+
o << " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY," << std::endl;
583+
o << " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE" << std::endl;
584+
o << " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER" << std::endl;
585+
o << " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," << std::endl;
586+
o << " OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE" << std::endl;
587+
o << " SOFTWARE." << std::endl;
563588
}

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ int main(int argc, char **argv) {
189189
std::cout << "This application uses the following libraries:" << std::endl;
190190
std::cout << " - cxxopts by jarro2783 (https://github.com/jarro2783/cxxopts)" << std::endl;
191191
std::cout << " - libmodbus by Stéphane Raimbault (https://github.com/stephane/libmodbus)" << std::endl;
192+
std::cout << " - cxxshm (https://github.com/NikolasK-source/cxxshm)" << std::endl;
192193
return EX_OK;
193194
}
194195

src/modbus_shm.cpp

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -35,71 +35,17 @@ Shm_Mapping::Shm_Mapping(std::size_t nb_bits,
3535
mapping.nb_registers = static_cast<int>(nb_registers);
3636
mapping.nb_input_registers = static_cast<int>(nb_input_registers);
3737

38-
// calculate shm object size
39-
shm_data[DO].size = nb_bits;
40-
shm_data[DI].size = nb_input_bits;
41-
shm_data[AO].size = nb_registers * 2;
42-
shm_data[AI].size = nb_input_registers * 2;
43-
44-
// create shm object names
45-
shm_data[DO].name = prefix + "DO";
46-
shm_data[DI].name = prefix + "DI";
47-
shm_data[AO].name = prefix + "AO";
48-
shm_data[AI].name = prefix + "AI";
49-
50-
// create and map shm objects
51-
for (std::size_t i = 0; i < reg_index_t::REG_COUNT; ++i) {
52-
auto &shm = shm_data[i];
53-
54-
int flags = O_RDWR | O_CREAT;
55-
if (!force) flags |= O_EXCL;
56-
57-
// create shm object
58-
shm.fd = shm_open(shm.name.c_str(), flags, 0660);
59-
if (shm.fd < 0) {
60-
throw std::system_error(
61-
errno, std::generic_category(), "Failed to create shared memory '" + shm.name + '\'');
62-
}
63-
64-
// set size of shm object
65-
if (ftruncate(shm.fd, static_cast<__off_t>(shm.size))) {
66-
throw std::system_error(
67-
errno, std::generic_category(), "Failed to resize shared memory '" + shm.name + '\'');
68-
}
69-
70-
// map shm object
71-
shm.addr = mmap(nullptr, shm.size, PROT_WRITE | PROT_READ, MAP_SHARED, shm.fd, 0);
72-
if (shm.addr == nullptr && shm.addr == MAP_FAILED) {
73-
shm.addr = nullptr;
74-
throw std::system_error(errno, std::generic_category(), "Failed to map shared memory '" + shm.name + '\'');
75-
}
76-
}
38+
// create shm objects
39+
shm_data[DO] = std::make_unique<cxxshm::SharedMemory>(prefix + "DO", nb_bits, false, !force);
40+
shm_data[DI] = std::make_unique<cxxshm::SharedMemory>(prefix + "DI", nb_input_bits, false, !force);
41+
shm_data[AO] = std::make_unique<cxxshm::SharedMemory>(prefix + "AO", 2 * nb_registers, false, !force);
42+
shm_data[AI] = std::make_unique<cxxshm::SharedMemory>(prefix + "AI", nb_input_registers, false, !force);
7743

7844
// set shm objects as modbus register storage
79-
mapping.tab_bits = static_cast<uint8_t *>(shm_data[DO].addr);
80-
mapping.tab_input_bits = static_cast<uint8_t *>(shm_data[DI].addr);
81-
mapping.tab_registers = static_cast<uint16_t *>(shm_data[AO].addr);
82-
mapping.tab_input_registers = static_cast<uint16_t *>(shm_data[AI].addr);
83-
}
84-
85-
Shm_Mapping::~Shm_Mapping() {
86-
// unmap and delete shm objects
87-
for (std::size_t i = 0; i < reg_index_t::REG_COUNT; ++i) {
88-
auto &shm = shm_data[i];
89-
if (shm.addr) {
90-
if (munmap(shm.addr, shm.size)) { perror(("Failed to unmap shared memory '" + shm.name + '\'').c_str()); }
91-
}
92-
93-
if (shm.fd != -1) {
94-
if (close(shm.fd)) {
95-
perror(("Failed to close shared memory file descriptor '" + shm.name + '\'').c_str());
96-
}
97-
98-
if (shm_unlink(shm.name.c_str())) {
99-
perror(("Failed to unlink shared memory '" + shm.name + '\'').c_str());
100-
}
101-
}
102-
}
45+
mapping.tab_bits = static_cast<uint8_t *>(shm_data[DO]->get_addr());
46+
mapping.tab_input_bits = static_cast<uint8_t *>(shm_data[DI]->get_addr());
47+
mapping.tab_registers = static_cast<uint16_t *>(shm_data[AO]->get_addr());
48+
mapping.tab_input_registers = static_cast<uint16_t *>(shm_data[AI]->get_addr());
10349
}
10450

10551
} // namespace shm

src/modbus_shm.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
#pragma once
77

8+
#include "cxxshm.hpp"
89
#include "modbus/modbus.h"
910
#include <array>
1011
#include <cstddef>
12+
#include <memory>
1113
#include <string>
1214

1315
namespace Modbus {
@@ -17,7 +19,7 @@ namespace shm {
1719
*
1820
* All required shm objects are created on construction and and deleted on destruction.
1921
*/
20-
class Shm_Mapping {
22+
class Shm_Mapping final {
2123
private:
2224
enum reg_index_t : std::size_t { DO, DI, AO, AI, REG_COUNT };
2325

@@ -33,7 +35,7 @@ class Shm_Mapping {
3335
modbus_mapping_t mapping {};
3436

3537
//! info for all shared memory objects
36-
std::array<shm_data_t, reg_index_t::REG_COUNT> shm_data;
38+
std::array<std::unique_ptr<cxxshm::SharedMemory>, reg_index_t::REG_COUNT> shm_data;
3739

3840
public:
3941
/*! \brief creates a new modbus_mapping_t. Like modbus_mapping_new(), but creates shared memory objects to store its
@@ -59,7 +61,7 @@ class Shm_Mapping {
5961
const std::string &shm_name_prefix = "modbus_",
6062
bool force = false);
6163

62-
~Shm_Mapping();
64+
~Shm_Mapping() = default;
6365

6466
/*! \brief get a pointer to the created modbus_mapping_t object
6567
*

0 commit comments

Comments
 (0)