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
/*      File: slave_device_pimpl.cpp
*       This file is part of the program ethercatcpp-core
*       Program description : EtherCAT driver libraries for UNIX
*       Copyright (C) 2017-2024 -  Robin Passama (LIRMM / CNRS) Arnaud Meline (LIRMM / CNRS) Benjamin Navarro (LIRMM / CNRS). All Right reserved.
*
*       This software is free software: you can redistribute it and/or modify
*       it under the terms of the CeCILL-C license as published by
*       the CEA CNRS INRIA, either version 1
*       of the License, or (at your option) any later version.
*       This software is distributed in the hope that it will be useful,
*       but WITHOUT ANY WARRANTY without even the implied warranty of
*       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*       CeCILL-C License for more details.
*
*       You should have received a copy of the CeCILL-C License
*       along with this software. If not, it can be found on the official website
*       of the CeCILL licenses family (http://www.cecill.info/index.en.html).
*/
#include "slave_device_pimpl.h"

#include <pid/log/ethercatcpp-core_ethercatcpp-core.h>

namespace ethercatcpp {

SlaveDevice::Impl::Impl(SlaveDevice* self)
    : slave_{self},
      nb_physical_buffer_(0),
      buffer_length_in_(0),
      buffer_length_out_(0),
      nb_command_PDO_mapping_(0),
      nb_status_PDO_mapping_(0),
      buffers_out_(),
      buffers_in_() {
}

void SlaveDevice::Impl::set_serial_number(uint32_t serial_number) {
    slave_.set_serial_number(serial_number);
}

uint32_t SlaveDevice::Impl::eep_manufacturer() const {
    return slave_.eep_manufacturer();
}
uint32_t SlaveDevice::Impl::eep_device() const {
    return slave_.eep_device();
}
uint32_t SlaveDevice::Impl::serial_number() const {
    return slave_.serial_number();
}

// accessor for slave_ptr
SlaveInfo* SlaveDevice::Impl::slave_address() {
    return &slave_;
}

// Printing slave informations
void SlaveDevice::Impl::print_slave_info() const {
    pid_log << pid::info << "Slave Informations" << pid::endl;
    pid_log << "Slave bus position : " << slave_.ec_bus_position() << pid::endl;
    pid_log << "Slave name : " << slave_.name() << pid::endl;
    pid_log << "Slave Manufacturer ID: 0x" << std::hex
            << slave_.eep_manufacturer() << "Slave ID: 0x" << std::hex
            << slave_.eep_device() << pid::endl;
    pid_log << "Slave state : " << std::dec << slave_.state() << pid::endl;
    pid_log << "Slave output size : " << slave_.output_size_bits() << " bits"
            << pid::endl;
    pid_log << "Slave input size : " << slave_.input_size_bits() << " bits"
            << pid::endl;
    pid_log << "Slave Delay : " << slave_.delay() << " [ns]" << pid::endl;
    pid_log << "Slave has DC : " << slave_.has_dc() << pid::endl;
    // if (slave_.get_Hasdc()) pid_log << pid::info << "Slave DCParentport
    // : " << slave_.get_DC_Parent_Port() << pid::endl;
    pid_log << "Slave configured EtherCar address : 0x" << std::hex
            << slave_.configured_addr() << pid::endl;

    // Print all configured SyncManager
    for (int nSM = 0; nSM < slave_.max_SMs(); ++nSM) {
        if (slave_.SM_start_address(nSM) > 0)
            pid_log << "SM" << nSM << " : Start Addr: 0x" << std::hex
                    << slave_.SM_start_address(nSM) << " Length: " << std::dec
                    << slave_.SM_length(nSM) << " Flags: 0x" << std::hex
                    << slave_.SM_flags(nSM) << " Type: " << std::dec
                    << slave_.SM_type(nSM) << pid::endl;
    }

    // Print all configured FMMU
    for (int nFMMU = 0; nFMMU < slave_.max_FMMUs(); ++nFMMU) {
        if (slave_.FMMU_active(nFMMU))
            pid_log << "FMMU" << nFMMU << pid::endl
                    << "Logical Start addr: 0x" << std::hex
                    << slave_.FMMU_logical_start(nFMMU)
                    << " Log length: " << std::dec
                    << slave_.FMMU_logical_length(nFMMU)
                    << " Log start bit: " << std::dec
                    << slave_.FMMU_logical_start_bit(nFMMU)
                    << " Log end bit: " << std::dec
                    << slave_.FMMU_logical_end_bit(nFMMU) << pid::endl
                    << "Physical start addr: 0x" << std::hex
                    << slave_.FMMU_physical_start(nFMMU)
                    << " Physical start bit: " << std::dec
                    << slave_.FMMU_physical_start_bit(nFMMU) << pid::endl
                    << "Type: 0x" << std::hex << slave_.FMMU_type(nFMMU)
                    << " Active: 0x" << std::hex << slave_.FMMU_active(nFMMU)
                    << pid::endl;
    }
    pid_log << pid::flush;
}

void SlaveDevice::Impl::add_run_step(std::function<void()>&& pre,
                                     std::function<void()>&& post) {
    run_steps_.push_back(std::make_pair(std::move(pre), std::move(post)));
}

void SlaveDevice::Impl::add_init_step(std::function<void()>&& pre,
                                      std::function<void()>&& post) {
    init_steps_.push_back(std::make_pair(std::move(pre), std::move(post)));
}

void SlaveDevice::Impl::add_end_step(std::function<void()>&& pre,
                                     std::function<void()>&& post) {
    end_steps_.push_back(std::make_pair(std::move(pre), std::move(post)));
}

uint8_t SlaveDevice::Impl::run_steps() {
    return static_cast<uint8_t>(run_steps_.size());
}

void SlaveDevice::Impl::pre_run_step(uint8_t step) {
    if (step < run_steps()) { // there is a step to run
        run_steps_[step].first();
    }
}

void SlaveDevice::Impl::post_run_step(uint8_t step) {
    if (step < run_steps()) { // there is a step to run
        run_steps_[step].second();
    }
}

uint8_t SlaveDevice::Impl::init_steps() {
    return static_cast<uint8_t>(init_steps_.size());
}

void SlaveDevice::Impl::pre_init_step(uint8_t step) {
    if (step < init_steps()) { // there is a step to run
        init_steps_[step].first();
    }
}

void SlaveDevice::Impl::post_init_step(uint8_t step) {
    if (step < init_steps()) { // there is a step to run
        init_steps_[step].second();
    }
}

uint8_t SlaveDevice::Impl::end_steps() {
    return static_cast<uint8_t>(end_steps_.size());
}

void SlaveDevice::Impl::pre_end_step(uint8_t step) {
    if (step < end_steps()) { // there is a step to run
        end_steps_[step].first();
    }
}

void SlaveDevice::Impl::post_end_step(uint8_t step) {
    if (step < end_steps()) { // there is a step to run
        end_steps_[step].second();
    }
}

uint8_t* SlaveDevice::Impl::input_buffer(uint16_t start_addr) {
    return buffers_in_[buffer_in_by_address_.at(start_addr)].first;
}

uint8_t* SlaveDevice::Impl::output_buffer(uint16_t start_addr) {
    return buffers_out_[buffer_out_by_address_.at(start_addr)].first;
}

void SlaveDevice::Impl::define_physical_buffer(syncmanager_buffer_t type,
                                               uint16_t start_addr,
                                               uint32_t flags,
                                               uint16_t length) {

    // legnth of buffer type in bits !! not in bytes so " * 8"
    uint16_t bits_number = length * 8;
    switch (type) {
    case ASYNCHROS_OUT: // Buffer in an asynchro mailbox out (from master to
                        // slave) Normaly automaticaly define by device
                        // hardware
        break;
    case ASYNCHROS_IN: // Buffer in an asynchro mailbox in (from slave to
                       // master) Normaly automaticaly define by device
                       // hardware
        break;
    case SYNCHROS_OUT: // Buffer is a synchro buffer out (from master to
                       // slave)
        buffer_out_by_address_[start_addr] =
            static_cast<uint16_t>(buffers_out_.size());
        buffers_out_.push_back(std::make_pair(nullptr, bits_number));
        buffer_length_out_ += bits_number;
        break;
    case SYNCHROS_IN: // Buffer is a synchro buffer in (from slave to
                      // master)
        buffer_in_by_address_[start_addr] =
            static_cast<uint16_t>(buffers_in_.size());
        buffers_in_.push_back(std::make_pair(nullptr, bits_number));
        buffer_length_in_ += bits_number;
        break;
    }

    // re-set size of logical buffers with all new definition of a physical
    // buffer
    set_input_buffer_size(buffer_length_in_);
    set_output_buffer_size(buffer_length_out_);

    slave_.set_SM_length(nb_physical_buffer_,
                         length); // SM length define in bytes !!
    slave_.set_SM_start_address(nb_physical_buffer_, start_addr);
    slave_.set_SM_flags(nb_physical_buffer_, flags);
    slave_.set_SM_type(nb_physical_buffer_, static_cast<uint8_t>(type));
    ++nb_physical_buffer_;
}

/*******************************************************************************/
/*              End user functions used to describe devices */
/*******************************************************************************/

void SlaveDevice::Impl::set_id(const std::string& name, uint32_t man_id,<--- Function 'set_id' argument 2 names different: declaration 'manufacturer' definition 'man_id'.
                               uint32_t model_id) {<--- Function 'set_id' argument 3 names different: declaration 'model' definition 'model_id'.
    slave_.set_name(name);
    slave_.set_eep_manufacturer(man_id);
    slave_.set_eep_device(model_id);

    // indicate that this slave is configured.
    slave_.set_configured_index(1);
}

void SlaveDevice::Impl::set_input_buffer_size(uint16_t size) {
    slave_.set_input_size_bits(size);
    if (size > 7) {
        slave_.set_input_size(size / 8);
    } else {
        slave_.set_input_size(0);
    }
}

void SlaveDevice::Impl::set_output_buffer_size(uint16_t size) {
    slave_.set_output_size_bits(size);
    if (size > 7) {
        slave_.set_output_size(size / 8);
    } else {
        slave_.set_output_size(0);
    }
}

void SlaveDevice::Impl::define_distributed_clock(bool have_dc) {
    slave_.set_dc(have_dc);
    slave_.activate_dc(0); // Activate by Master if used
}

void SlaveDevice::Impl::define_period_for_non_cyclic_steps(int period) {
    slave_.set_step_wait_time(period);
}

void SlaveDevice::Impl::update_buffers() {
    uint8_t* start_output_buffer = slave_.output_address();
    uint8_t* start_input_buffer = slave_.input_address();
    // Update buffer address with the start_address and update new start
    // address by add size of buffer
    if (buffers_in_.size() != 0) {
        for (auto& buffer : buffers_in_) {
            buffer.first = start_input_buffer;
            start_input_buffer +=
                (buffer.second /
                 8); // buffer.second is in bits and needs bytes so "/8"
        }
    }
    // Update buffer address with the start_address and update new start
    // address by add size of buffer
    if (buffers_out_.size() != 0) {
        for (auto& buffer : buffers_out_) {
            buffer.first = start_output_buffer;
            start_output_buffer +=
                (buffer.second /
                 8); // buffer.second is in bits and needs bytes so "/8"
        }
    }
}
int SlaveDevice::Impl::read_sdo(uint16_t index, uint8_t sub_index,
                                int32_t buffer_size, void* buffer_ptr) {
    return slave_.read_sdo(index, sub_index, buffer_size, buffer_ptr);
}

int SlaveDevice::Impl::write_sdo(uint16_t index, uint8_t sub_index,
                                 int32_t buffer_size, void* buffer_ptr) {
    return slave_.write_sdo(index, sub_index, buffer_size, buffer_ptr);
}

void SlaveDevice::Impl::set_command_mappings(uint8_t mappings) {
    nb_command_PDO_mapping_ = mappings;
}

uint8_t SlaveDevice::Impl::get_command_mappings() const {
    return nb_command_PDO_mapping_;
}
void SlaveDevice::Impl::set_status_mappings(uint8_t mappings) {
    nb_status_PDO_mapping_ = mappings;
}
uint8_t SlaveDevice::Impl::get_status_mappings() const {
    return nb_status_PDO_mapping_;
}
// ------ CanOpen over ethercat configuration and communication function
// --------
void SlaveDevice::Impl::launch_init_configuration() {
    if (preop_init_) {
        preop_init_();
    }
}

void SlaveDevice::Impl::launch_end_configuration() {
    if (preop_end_) {
        preop_end_();
    }
}

void SlaveDevice::Impl::configure_at_init(std::function<void()>&& func) {
    preop_init_ = func;
}

void SlaveDevice::Impl::configure_at_end(std::function<void()>&& func) {
    preop_end_ = func;
}

// --------  function to use and define DC synchro signal 0 and 1--------

void SlaveDevice::Impl::configure_dc_sync0(uint32_t cycle_time_0,
                                           int32_t cycle_shift) {
    slave_.config_sync0(cycle_time_0, cycle_shift);
}

void SlaveDevice::Impl::configure_dc_sync0_1(uint32_t cycle_time_0,
                                             uint32_t cycle_time_1,
                                             int32_t cycle_shift) {
    slave_.config_sync0_1(cycle_time_0, cycle_time_1, cycle_shift);
}

// --------  functions for file access--------

int32_t SlaveDevice::Impl::read_file(std::string_view filename,
                                     uint32_t password, int32_t size,
                                     uint8_t* buffer) {
    return slave_.read_file(filename, password, size, buffer);
}
bool SlaveDevice::Impl::write_file(std::string_view filename, uint32_t password,
                                   int32_t size, uint8_t* buffer) {
    return slave_.write_file(filename, password, size, buffer);
}

bool SlaveDevice::Impl::read_sercos(uint8_t drive, uint8_t flags, uint16_t idn,
                                    int32_t size, uint8_t* buffer) {
    return slave_.read_sercos(drive, flags, idn, size, buffer);
}
bool SlaveDevice::Impl::write_sercos(uint8_t drive, uint8_t flags, uint16_t idn,
                                     int32_t size, uint8_t* buffer) {
    return slave_.write_sercos(drive, flags, idn, size, buffer);
}

#ifdef EOE_AVAILABLE

bool SlaveDevice::Impl::detect_ethernet_configuration(
    uint8_t port, EthernetConfiguration& config) {

    return slave_.detect_ethernet_configuration(port, config);
}

bool SlaveDevice::Impl::configure_ethernet(
    uint8_t port, const EthernetConfiguration& config) {
    return slave_.configure_ethernet(port, config);
}

int32_t SlaveDevice::Impl::read_ethernet(uint8_t port, int32_t size,
                                         uint8_t* buffer) {
    return slave_.read_ethernet(port, size, buffer);
}

bool SlaveDevice::Impl::write_ethernet(uint8_t port, int32_t size,
                                       uint8_t* buffer) {
    return slave_.write_ethernet(port, size, buffer);
}

#endif
} // namespace ethercatcpp