Fix: wrong period operation without unit multiplicator
authorRomain Forlot <romain.forlot@iot.bzh>
Sun, 2 Apr 2017 21:46:56 +0000 (23:46 +0200)
committerRomain Forlot <romain.forlot@iot.bzh>
Tue, 11 Apr 2017 10:41:42 +0000 (12:41 +0200)
Possible divsion by 0. Don't know if it is good to keep different time unit
instead having one unit for project...

Change-Id: I73cfc1ab72d3420d337f10a9b79bdf305a3a2bc7
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
src/diagnostic/diagnostic-manager.cpp
src/utils/timer.cpp
src/utils/timer.hpp

index 5834c35..e3a78d0 100644 (file)
@@ -448,7 +448,7 @@ bool diagnostic_manager_t::clear_to_send(active_diagnostic_request_t* request) c
 
 int diagnostic_manager_t::reschedule_request(sd_event_source *s, uint64_t usec, active_diagnostic_request_t* adr)
 {
-       usec = usec + (uint64_t)(frequency_clock_t::frequency_to_period(adr->get_frequency_clock().get_frequency())*MICRO);
+       usec = usec + (uint64_t)(adr->get_frequency_clock().frequency_to_period());
        DEBUG(binder_interface, "send_request: Event loop state: %d. usec: %ld", sd_event_get_state(afb_daemon_get_event_loop(binder_interface->daemon)), usec);
        if(sd_event_source_set_time(s, usec) >= 0)
                if(sd_event_source_set_enabled(s, SD_EVENT_ON) >= 0)
index 540cfe0..a35a3e9 100644 (file)
@@ -51,19 +51,19 @@ long long int system_time_s()
 }
 
 frequency_clock_t::frequency_clock_t()
-       : frequency_{10.0}, last_tick_{0}, time_function_{nullptr}
+       : unit_{1000000}, frequency_{10.0}, last_tick_{0}, time_function_{nullptr}
 {}
 
 
 frequency_clock_t::frequency_clock_t(float frequency)
-       : frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
+       : unit_{1000000}, frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
 {}
 
 /// @brief Return the period in ms given the frequency in hertz.
-/// @param[in] frequency - Frequency to convert, in Hertz
-float frequency_clock_t::frequency_to_period(float frequency)
+/// @param[in] frequency - Frequency to convert, in hertz
+float frequency_clock_t::frequency_to_period()
 {
-       return 1 / frequency;
+       return 1 / frequency_ * unit_;
 }
 
 bool frequency_clock_t::started()
@@ -78,7 +78,7 @@ time_function_t frequency_clock_t::get_time_function()
 
 bool frequency_clock_t::elapsed(bool stagger)
 {
-       float period = frequency_to_period(frequency_);
+       float period = frequency_to_period();
        float elapsed_time = 0;
        if(!started() && stagger)
                last_tick_ = get_time_function()() - (rand() % int(period));
index 5be0e3c..f565904 100644 (file)
@@ -32,6 +32,7 @@ long long int system_time_s();
 class frequency_clock_t
 {
 private:
+       float unit_; ///< unit_ - multiplicator to make operation to be in the right unit (milli, micro, nano, etc)
        float frequency_; ///< the clock frequency in Hz.
        unsigned long last_tick_; ///< the last time (in milliseconds since startup) that the clock ticked.
        time_function_t time_function_; ///<  a function returning current time
@@ -42,7 +43,8 @@ public:
        frequency_clock_t(float frequency, unsigned long last_tick, time_function_t time_function);
 
        float get_frequency() const;
-       static float frequency_to_period(float frequency);
+
+       float frequency_to_period();
        bool started();
        time_function_t get_time_function();
        bool elapsed(bool stagger);