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