summaryrefslogtreecommitdiff
path: root/implementation/service_discovery/src/service_discovery_fsm.cpp
blob: ba1bd18194f8e30fb73a085b8efc2605cb9408c9 (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
// Copyright (C) 2014-2016 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>

#include "../include/defines.hpp"
#include "../include/service_discovery.hpp"
#include "../include/service_discovery_fsm.hpp"
#include "../../routing/include/serviceinfo.hpp"

#include "../../configuration/include/configuration.hpp"
#include "../../logging/include/logger.hpp"

namespace vsomeip {
namespace sd {

///////////////////////////////////////////////////////////////////////////////
// Machine
///////////////////////////////////////////////////////////////////////////////
namespace _sd {

fsm::fsm(boost::asio::io_service &_io)
        : fsm_base(_io), is_up_(true) {
}

fsm::~fsm() {
}

void fsm::set_fsm(std::shared_ptr<service_discovery_fsm> _fsm) {
    fsm_ = _fsm;
}

void fsm::timer_expired(const boost::system::error_code &_error,
        bool _use_alt_timeout) {
    if (!_error) {
        std::shared_ptr<service_discovery_fsm> its_fsm = fsm_.lock();
        if (its_fsm) {
            if (_use_alt_timeout) {
                its_fsm->process(ev_alt_timeout());
            } else {
                its_fsm->process(ev_timeout());
            }
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
// State "Inactive"
///////////////////////////////////////////////////////////////////////////////
inactive::inactive(my_context context)
        : sc::state<inactive, fsm>(context) {
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        VSOMEIP_TRACE << "sd::inactive";
        outermost_context().run_ = 0;
    }
}

sc::result inactive::react(const ev_none &_event) {
    (void)_event;

    if (outermost_context().is_up_) {
        return transit<active>();
    }

    return discard_event();
}

sc::result inactive::react(const ev_status_change &_event) {
    (void)_event;

    outermost_context().is_up_ = _event.is_up_;
    if (outermost_context().is_up_) {
        return transit<active>();
    }

    return discard_event();
}

///////////////////////////////////////////////////////////////////////////////
// State "Active"
///////////////////////////////////////////////////////////////////////////////
active::active(my_context _context)
        : sc::state<active, fsm, initial>(_context) {
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        VSOMEIP_TRACE << "sd::active";
    }
}

active::~active() {
}

sc::result active::react(const ev_status_change &_event) {
    (void)_event;

    outermost_context().stop_timer();
    outermost_context().is_up_ = _event.is_up_;
    if (!outermost_context().is_up_)
        return transit<inactive>();

    return forward_event();
}

///////////////////////////////////////////////////////////////////////////////
// State "Active.Initial"
///////////////////////////////////////////////////////////////////////////////
initial::initial(my_context _context)
        : sc::state<initial, active>(_context) {
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        VSOMEIP_TRACE << "sd::active.initial";
        outermost_context().start_timer(outermost_context().initial_delay_);
    }
}

sc::result initial::react(const ev_timeout &_event) {
    (void)_event;
    VSOMEIP_TRACE << "sd::active.initial.react";
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        (void)fsm->send(false, false);
    }
    return transit<repeat>();
}

///////////////////////////////////////////////////////////////////////////////
// State "Active.Repeat"
///////////////////////////////////////////////////////////////////////////////
repeat::repeat(my_context _context)
        : sc::state<repeat, active>(_context) {
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        VSOMEIP_TRACE << "sd::active.repeat";
        uint32_t its_timeout = (outermost_context().repetitions_base_delay_
                << outermost_context().run_);
        outermost_context().run_++;
        (void)fsm->send(false);
        outermost_context().start_timer(its_timeout);
    }
}

sc::result repeat::react(const ev_timeout &_event) {
    (void)_event;
    if (outermost_context().run_ < outermost_context().repetitions_max_) {
        return transit<repeat>();
    }
    return transit<main>();
}

sc::result repeat::react(const ev_find_service &_event) {
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    // Answer Find Service messages with unicast offer in repetition phase
    if (fsm) {
        VSOMEIP_TRACE << "sd::active.repeat.react.find";
        fsm->send_unicast_offer_service(_event.info_, _event.service_, _event.instance_,
            _event.major_, _event.minor_);
    }
    return forward_event();
}

///////////////////////////////////////////////////////////////////////////////
// State "Active.Main"
///////////////////////////////////////////////////////////////////////////////
main::main(my_context _context)
    : sc::state<main, active, mpl::list<offer, find> >(_context) {
    VSOMEIP_TRACE << "sd::active.main";
}

///////////////////////////////////////////////////////////////////////////////
// State "Active.Main.Offer"
///////////////////////////////////////////////////////////////////////////////
offer::offer(my_context _context)
        : sc::state<offer, main::orthogonal<0> >(_context) {
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        VSOMEIP_TRACE << "sd::active.main.offer";
        outermost_context().start_timer(
                outermost_context().cyclic_offer_delay_);
        (void)fsm->send(true);
    }
}

sc::result offer::react(const ev_timeout &_event) {
    (void)_event;
    return transit<offer>();
}

sc::result offer::react(const ev_find_service &_event) {
    VSOMEIP_TRACE << "sd::active.main.react.find";
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        if(_event.unicast_flag_) { //SIP_SD_826
            if( !fsm->check_is_multicast_offer()) { // SIP_SD_89
                fsm->send_unicast_offer_service(_event.info_, _event.service_, _event.instance_,
                        _event.major_, _event.minor_);
            } else { // SIP_SD_90
                fsm->send_multicast_offer_service(_event.info_, _event.service_, _event.instance_,
                        _event.major_, _event.minor_);
            }
        } else { // SIP_SD_824
            fsm->send_unicast_offer_service(_event.info_, _event.service_, _event.instance_,
                    _event.major_, _event.minor_);
        }
    }
    return forward_event();
}

sc::result offer::react(const ev_offer_change &_event) {
    (void)_event;
    return transit<offer>();
}

///////////////////////////////////////////////////////////////////////////////
// State "Active.Announce.Main.Find"
///////////////////////////////////////////////////////////////////////////////
find::find(my_context _context)
    : sc::state<find, main::orthogonal<1>, idle>(_context) {
    VSOMEIP_TRACE << "sd::active.main.find";
}

///////////////////////////////////////////////////////////////////////////////
// State "Active.Announce.Idle"
///////////////////////////////////////////////////////////////////////////////
idle::idle(my_context _context)
    : sc::state<idle, find>(_context) {
    VSOMEIP_TRACE << "sd::active.main.find.idle";
    context<find>().run_ = 0;
}

sc::result idle::react(const ev_request_service &_event) {
    (void)_event;
    return transit<send>();
}

///////////////////////////////////////////////////////////////////////////////
// State "Active.Announce.Main.Find.Send"
///////////////////////////////////////////////////////////////////////////////
send::send(my_context _context)
    : sc::state<send, find>(_context) {
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        VSOMEIP_TRACE << "sd::active.main.find.send";
        if( context<find>().run_ == 0)
        {
            outermost_context().start_timer(outermost_context().initial_delay_, true);
        } else if ( context<find>().run_ < fsm->get_repetition_max() + 1 ) {
            // Increment to the maximum run value (which is repetition_max-1)
            // As new request might be added in the meantime, this will be
            // used to calculate the maximum cycle time.
            uint32_t its_timeout = (outermost_context().repetitions_base_delay_
                    << context<find>().run_);
            outermost_context().start_timer(its_timeout, true);
        }
        else {
            post_event(ev_none());
        }
    } else {
        post_event(ev_none());
    }
}

sc::result send::react(const ev_alt_timeout &_event) {
    (void)_event;
    std::shared_ptr < service_discovery_fsm > fsm =
            outermost_context().fsm_.lock();
    if (fsm) {
        VSOMEIP_TRACE << "sd::active.main.find.initial";
        context<find>().run_++;
        (void)fsm->send(true, true);
    }
    return transit<send>();
}

sc::result send::react(const ev_none &_event) {
    (void)_event;
    return transit<idle>();
}

} // namespace _sd

///////////////////////////////////////////////////////////////////////////////
// Interface
///////////////////////////////////////////////////////////////////////////////
service_discovery_fsm::service_discovery_fsm(
        std::shared_ptr<service_discovery> _discovery)
        : discovery_(_discovery), fsm_(
                std::make_shared < _sd::fsm > (_discovery->get_io())) {
    std::shared_ptr < service_discovery > discovery = discovery_.lock();
    if (discovery) {
        std::shared_ptr < configuration > its_configuration =
                discovery->get_configuration();

        int32_t its_initial_delay_min
            = its_configuration->get_sd_initial_delay_min();
        if (its_initial_delay_min < 0)
            its_initial_delay_min = VSOMEIP_SD_DEFAULT_INITIAL_DELAY_MIN;

        int32_t its_initial_delay_max
            = its_configuration->get_sd_initial_delay_max();
        if (its_initial_delay_max <= 0)
            its_initial_delay_max = VSOMEIP_SD_DEFAULT_INITIAL_DELAY_MAX;

        if (its_initial_delay_min > its_initial_delay_max) {
            int32_t tmp_initial_delay = its_initial_delay_min;
            its_initial_delay_min = its_initial_delay_max;
            its_initial_delay_max = tmp_initial_delay;
        }

        VSOMEIP_TRACE << "Inital delay [" << its_initial_delay_min << ", "
                << its_initial_delay_max << "]";

        boost::random::mt19937 its_generator;
        boost::random::uniform_int_distribution<> its_distribution(
                its_initial_delay_min, its_initial_delay_max);
        fsm_->initial_delay_ = its_distribution(its_generator);

        fsm_->repetitions_base_delay_
            = its_configuration->get_sd_repetitions_base_delay();
        if (fsm_->repetitions_base_delay_ <= 0)
                fsm_->repetitions_base_delay_
                    = VSOMEIP_SD_DEFAULT_REPETITIONS_BASE_DELAY;
        fsm_->repetitions_max_
            = its_configuration->get_sd_repetitions_max();
        if (fsm_->repetitions_max_ <= 0)
            fsm_->repetitions_max_ = VSOMEIP_SD_DEFAULT_REPETITIONS_MAX;

        fsm_->cyclic_offer_delay_
            = its_configuration->get_sd_cyclic_offer_delay();
        if (fsm_->cyclic_offer_delay_ <= 0)
            fsm_->cyclic_offer_delay_ = VSOMEIP_SD_DEFAULT_CYCLIC_OFFER_DELAY;

        VSOMEIP_INFO << "SD configuration [" << std::dec << fsm_->initial_delay_ << ":"
                << std::dec << fsm_->repetitions_base_delay_ << ":"
                << std::dec << (int) fsm_->repetitions_max_ << ":"
                << std::dec << fsm_->cyclic_offer_delay_ << "]";
    } else {
        VSOMEIP_ERROR << "SD initialization failed";
    }
}

void service_discovery_fsm::start() {
    fsm_->set_fsm(shared_from_this());
    fsm_->initiate();
}

void service_discovery_fsm::stop() {
}

bool service_discovery_fsm::send(bool _is_announcing, bool _is_find) {
    std::shared_ptr < service_discovery > discovery = discovery_.lock();
    if (discovery) {
        return discovery->send(_is_announcing, _is_find);
    }
    return false;
}

void service_discovery_fsm::send_unicast_offer_service(
        const std::shared_ptr<const serviceinfo> &_info,
        service_t _service, instance_t _instance,
        major_version_t _major,
        minor_version_t _minor) {
    std::shared_ptr < service_discovery > discovery = discovery_.lock();
    if (discovery) {
        discovery->send_unicast_offer_service(_info, _service,
                _instance, _major, _minor );
    }
}

void service_discovery_fsm::send_multicast_offer_service(
        const std::shared_ptr<const serviceinfo> &_info, service_t _service,
        instance_t _instance, major_version_t _major, minor_version_t _minor) {
    std::shared_ptr < service_discovery > discovery = discovery_.lock();
    if (discovery) {
        discovery->send_multicast_offer_service(_info, _service,
                _instance, _major, _minor );
    }
}

std::chrono::milliseconds service_discovery_fsm::get_elapsed_offer_timer() {
    //get remaining time to next offer since last offer
    std::chrono::milliseconds remaining =
            std::chrono::milliseconds(fsm_->expired_from_now(false));

    if( std::chrono::milliseconds(0) > remaining) {
        remaining = std::chrono::milliseconds(fsm_->cyclic_offer_delay_);
    }
    return std::chrono::milliseconds(fsm_->cyclic_offer_delay_) - remaining;
}

bool service_discovery_fsm::check_is_multicast_offer() {
    bool is_multicast(false);
    std::chrono::milliseconds elapsed_ = get_elapsed_offer_timer();
    uint32_t half_cyclic_offer_delay_ = fsm_->cyclic_offer_delay_ / 2;

    if( elapsed_ >= std::chrono::milliseconds(half_cyclic_offer_delay_)) {
        // Response must be a multicast offer (SIP_SD_90)
        is_multicast = true;
    }
    return is_multicast;
}

} // namespace sd
} // namespace vsomeip