mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbed-client-classic/source/m2mtimerpimpl.cpp@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* |
mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
mbedAustin | 11:cada08fc8a70 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbedAustin | 11:cada08fc8a70 | 5 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 6 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 7 | * |
mbedAustin | 11:cada08fc8a70 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 9 | * |
mbedAustin | 11:cada08fc8a70 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 14 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 15 | */ |
mbedAustin | 11:cada08fc8a70 | 16 | #include "mbed-client-classic/m2mtimerpimpl.h" |
mbedAustin | 11:cada08fc8a70 | 17 | #include "mbed-client/m2mtimerobserver.h" |
mbedAustin | 11:cada08fc8a70 | 18 | #include <cstdio> |
mbedAustin | 11:cada08fc8a70 | 19 | |
mbedAustin | 11:cada08fc8a70 | 20 | M2MTimerPimpl::M2MTimerPimpl(M2MTimerObserver& observer) |
mbedAustin | 11:cada08fc8a70 | 21 | : _observer(observer), |
mbedAustin | 11:cada08fc8a70 | 22 | _single_shot(true), |
mbedAustin | 11:cada08fc8a70 | 23 | _interval(0), |
mbedAustin | 11:cada08fc8a70 | 24 | _type(M2MTimerObserver::Notdefined), |
mbedAustin | 11:cada08fc8a70 | 25 | _intermediate_interval(0), |
mbedAustin | 11:cada08fc8a70 | 26 | _total_interval(0), |
mbedAustin | 11:cada08fc8a70 | 27 | _status(0), |
mbedAustin | 11:cada08fc8a70 | 28 | _dtls_type(false), |
mbedAustin | 11:cada08fc8a70 | 29 | _thread(0) |
mbedAustin | 11:cada08fc8a70 | 30 | { |
mbedAustin | 11:cada08fc8a70 | 31 | |
mbedAustin | 11:cada08fc8a70 | 32 | } |
mbedAustin | 11:cada08fc8a70 | 33 | |
mbedAustin | 11:cada08fc8a70 | 34 | M2MTimerPimpl::~M2MTimerPimpl() |
mbedAustin | 11:cada08fc8a70 | 35 | { |
mbedAustin | 11:cada08fc8a70 | 36 | stop_timer(); |
mbedAustin | 11:cada08fc8a70 | 37 | } |
mbedAustin | 11:cada08fc8a70 | 38 | |
mbedAustin | 11:cada08fc8a70 | 39 | void M2MTimerPimpl::start_timer(uint64_t interval, |
mbedAustin | 11:cada08fc8a70 | 40 | M2MTimerObserver::Type type, |
mbedAustin | 11:cada08fc8a70 | 41 | bool single_shot) |
mbedAustin | 11:cada08fc8a70 | 42 | { |
mbedAustin | 11:cada08fc8a70 | 43 | _dtls_type = false; |
mbedAustin | 11:cada08fc8a70 | 44 | _intermediate_interval = 0; |
mbedAustin | 11:cada08fc8a70 | 45 | _total_interval = 0; |
mbedAustin | 11:cada08fc8a70 | 46 | _status = 0; |
mbedAustin | 11:cada08fc8a70 | 47 | _single_shot = single_shot; |
mbedAustin | 11:cada08fc8a70 | 48 | _interval = interval; |
mbedAustin | 11:cada08fc8a70 | 49 | _type = type; |
mbedAustin | 11:cada08fc8a70 | 50 | _running = true; |
mbedAustin | 11:cada08fc8a70 | 51 | |
mbedAustin | 11:cada08fc8a70 | 52 | _thread = rtos::create_thread<M2MTimerPimpl, &M2MTimerPimpl::timer_run>(this); |
mbedAustin | 11:cada08fc8a70 | 53 | } |
mbedAustin | 11:cada08fc8a70 | 54 | |
mbedAustin | 11:cada08fc8a70 | 55 | void M2MTimerPimpl::start_dtls_timer(uint64_t intermediate_interval, uint64_t total_interval, M2MTimerObserver::Type type) |
mbedAustin | 11:cada08fc8a70 | 56 | { |
mbedAustin | 11:cada08fc8a70 | 57 | _dtls_type = true; |
mbedAustin | 11:cada08fc8a70 | 58 | _intermediate_interval = intermediate_interval; |
mbedAustin | 11:cada08fc8a70 | 59 | _total_interval = total_interval; |
mbedAustin | 11:cada08fc8a70 | 60 | _status = 0; |
mbedAustin | 11:cada08fc8a70 | 61 | _type = type; |
mbedAustin | 11:cada08fc8a70 | 62 | _running = true; |
mbedAustin | 11:cada08fc8a70 | 63 | |
mbedAustin | 11:cada08fc8a70 | 64 | _thread = rtos::create_thread<M2MTimerPimpl, &M2MTimerPimpl::timer_run>(this); |
mbedAustin | 11:cada08fc8a70 | 65 | } |
mbedAustin | 11:cada08fc8a70 | 66 | |
mbedAustin | 11:cada08fc8a70 | 67 | void M2MTimerPimpl::stop_timer() |
mbedAustin | 11:cada08fc8a70 | 68 | { |
mbedAustin | 11:cada08fc8a70 | 69 | _running = false; |
mbedAustin | 11:cada08fc8a70 | 70 | |
mbedAustin | 11:cada08fc8a70 | 71 | if (_thread) { |
mbedAustin | 11:cada08fc8a70 | 72 | delete _thread; |
mbedAustin | 11:cada08fc8a70 | 73 | _thread = NULL; |
mbedAustin | 11:cada08fc8a70 | 74 | } |
mbedAustin | 11:cada08fc8a70 | 75 | } |
mbedAustin | 11:cada08fc8a70 | 76 | |
mbedAustin | 11:cada08fc8a70 | 77 | void M2MTimerPimpl::timer_run() |
mbedAustin | 11:cada08fc8a70 | 78 | { |
mbedAustin | 11:cada08fc8a70 | 79 | if (!_dtls_type) { |
mbedAustin | 11:cada08fc8a70 | 80 | while (_running) { |
mbedAustin | 11:cada08fc8a70 | 81 | _thread->wait(_interval); |
mbedAustin | 11:cada08fc8a70 | 82 | if (!_running) return; |
mbedAustin | 11:cada08fc8a70 | 83 | _observer.timer_expired(_type); |
mbedAustin | 11:cada08fc8a70 | 84 | if (_single_shot) return; |
mbedAustin | 11:cada08fc8a70 | 85 | } |
mbedAustin | 11:cada08fc8a70 | 86 | } else { |
mbedAustin | 11:cada08fc8a70 | 87 | _thread->wait(_intermediate_interval); |
mbedAustin | 11:cada08fc8a70 | 88 | if (!_running) return; |
mbedAustin | 11:cada08fc8a70 | 89 | _status++; |
mbedAustin | 11:cada08fc8a70 | 90 | _thread->wait(_total_interval - _intermediate_interval); |
mbedAustin | 11:cada08fc8a70 | 91 | if (!_running) return; |
mbedAustin | 11:cada08fc8a70 | 92 | _status++; |
mbedAustin | 11:cada08fc8a70 | 93 | _observer.timer_expired(_type); |
mbedAustin | 11:cada08fc8a70 | 94 | } |
mbedAustin | 11:cada08fc8a70 | 95 | } |
mbedAustin | 11:cada08fc8a70 | 96 | |
mbedAustin | 11:cada08fc8a70 | 97 | bool M2MTimerPimpl::is_intermediate_interval_passed() |
mbedAustin | 11:cada08fc8a70 | 98 | { |
mbedAustin | 11:cada08fc8a70 | 99 | if (_status > 0) { |
mbedAustin | 11:cada08fc8a70 | 100 | return true; |
mbedAustin | 11:cada08fc8a70 | 101 | } |
mbedAustin | 11:cada08fc8a70 | 102 | return false; |
mbedAustin | 11:cada08fc8a70 | 103 | } |
mbedAustin | 11:cada08fc8a70 | 104 | |
mbedAustin | 11:cada08fc8a70 | 105 | bool M2MTimerPimpl::is_total_interval_passed() |
mbedAustin | 11:cada08fc8a70 | 106 | { |
mbedAustin | 11:cada08fc8a70 | 107 | if (_status > 1) { |
mbedAustin | 11:cada08fc8a70 | 108 | return true; |
mbedAustin | 11:cada08fc8a70 | 109 | } |
mbedAustin | 11:cada08fc8a70 | 110 | return false; |
mbedAustin | 11:cada08fc8a70 | 111 | } |