Preliminary main mbed library for nexpaq development
features/FEATURE_CLIENT/mbed-client-classic/source/m2mtimerpimpl.cpp@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* |
nexpaq | 0:6c56fb4bc5f0 | 2 | * Copyright (c) 2015-2016 ARM Limited. All rights reserved. |
nexpaq | 0:6c56fb4bc5f0 | 3 | * SPDX-License-Identifier: Apache-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
nexpaq | 0:6c56fb4bc5f0 | 5 | * not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 6 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 7 | * |
nexpaq | 0:6c56fb4bc5f0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 9 | * |
nexpaq | 0:6c56fb4bc5f0 | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
nexpaq | 0:6c56fb4bc5f0 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 13 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 14 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 15 | */ |
nexpaq | 0:6c56fb4bc5f0 | 16 | |
nexpaq | 0:6c56fb4bc5f0 | 17 | #include <assert.h> |
nexpaq | 0:6c56fb4bc5f0 | 18 | #include <time.h> |
nexpaq | 0:6c56fb4bc5f0 | 19 | |
nexpaq | 0:6c56fb4bc5f0 | 20 | #include "mbed-client-classic/m2mtimerpimpl.h" |
nexpaq | 0:6c56fb4bc5f0 | 21 | #include "mbed-client/m2mtimerobserver.h" |
nexpaq | 0:6c56fb4bc5f0 | 22 | #include "mbed-client/m2mvector.h" |
nexpaq | 0:6c56fb4bc5f0 | 23 | |
nexpaq | 0:6c56fb4bc5f0 | 24 | #include "eventOS_event.h" |
nexpaq | 0:6c56fb4bc5f0 | 25 | #include "eventOS_event_timer.h" |
nexpaq | 0:6c56fb4bc5f0 | 26 | #include "eventOS_scheduler.h" |
nexpaq | 0:6c56fb4bc5f0 | 27 | #include "ns_hal_init.h" |
nexpaq | 0:6c56fb4bc5f0 | 28 | #include "mbed-trace/mbed_trace.h" |
nexpaq | 0:6c56fb4bc5f0 | 29 | |
nexpaq | 0:6c56fb4bc5f0 | 30 | #define TRACE_GROUP "mClt" |
nexpaq | 0:6c56fb4bc5f0 | 31 | |
nexpaq | 0:6c56fb4bc5f0 | 32 | #define MBED_CLIENT_TIMER_EVENT 10 |
nexpaq | 0:6c56fb4bc5f0 | 33 | |
nexpaq | 0:6c56fb4bc5f0 | 34 | #ifdef MBED_CONF_MBED_CLIENT_EVENT_LOOP_SIZE |
nexpaq | 0:6c56fb4bc5f0 | 35 | #define MBED_CLIENT_EVENT_LOOP_SIZE MBED_CONF_MBED_CLIENT_EVENT_LOOP_SIZE |
nexpaq | 0:6c56fb4bc5f0 | 36 | #else |
nexpaq | 0:6c56fb4bc5f0 | 37 | #define MBED_CLIENT_EVENT_LOOP_SIZE 1024 |
nexpaq | 0:6c56fb4bc5f0 | 38 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 39 | |
nexpaq | 0:6c56fb4bc5f0 | 40 | int8_t M2MTimerPimpl::_tasklet_id = -1; |
nexpaq | 0:6c56fb4bc5f0 | 41 | |
nexpaq | 0:6c56fb4bc5f0 | 42 | int8_t M2MTimerPimpl::_next_timer_id = 1; |
nexpaq | 0:6c56fb4bc5f0 | 43 | |
nexpaq | 0:6c56fb4bc5f0 | 44 | static m2m::Vector<M2MTimerPimpl*> timer_impl_list; |
nexpaq | 0:6c56fb4bc5f0 | 45 | |
nexpaq | 0:6c56fb4bc5f0 | 46 | extern "C" void tasklet_func(arm_event_s *event) |
nexpaq | 0:6c56fb4bc5f0 | 47 | { |
nexpaq | 0:6c56fb4bc5f0 | 48 | // skip the init event as there will be a timer event after |
nexpaq | 0:6c56fb4bc5f0 | 49 | if (event->event_type == MBED_CLIENT_TIMER_EVENT) { |
nexpaq | 0:6c56fb4bc5f0 | 50 | |
nexpaq | 0:6c56fb4bc5f0 | 51 | bool timer_found = false; |
nexpaq | 0:6c56fb4bc5f0 | 52 | eventOS_scheduler_mutex_wait(); |
nexpaq | 0:6c56fb4bc5f0 | 53 | int timer_count = timer_impl_list.size(); |
nexpaq | 0:6c56fb4bc5f0 | 54 | for (int index = 0; index < timer_count; index++) { |
nexpaq | 0:6c56fb4bc5f0 | 55 | M2MTimerPimpl* timer = timer_impl_list[index]; |
nexpaq | 0:6c56fb4bc5f0 | 56 | if (timer->get_timer_id() == event->event_id) { |
nexpaq | 0:6c56fb4bc5f0 | 57 | eventOS_scheduler_mutex_release(); |
nexpaq | 0:6c56fb4bc5f0 | 58 | timer_found = true; |
nexpaq | 0:6c56fb4bc5f0 | 59 | timer->timer_expired(); |
nexpaq | 0:6c56fb4bc5f0 | 60 | break; |
nexpaq | 0:6c56fb4bc5f0 | 61 | } |
nexpaq | 0:6c56fb4bc5f0 | 62 | } |
nexpaq | 0:6c56fb4bc5f0 | 63 | if(!timer_found) { |
nexpaq | 0:6c56fb4bc5f0 | 64 | eventOS_scheduler_mutex_release(); |
nexpaq | 0:6c56fb4bc5f0 | 65 | } |
nexpaq | 0:6c56fb4bc5f0 | 66 | } |
nexpaq | 0:6c56fb4bc5f0 | 67 | } |
nexpaq | 0:6c56fb4bc5f0 | 68 | |
nexpaq | 0:6c56fb4bc5f0 | 69 | M2MTimerPimpl::M2MTimerPimpl(M2MTimerObserver& observer) |
nexpaq | 0:6c56fb4bc5f0 | 70 | : _observer(observer), |
nexpaq | 0:6c56fb4bc5f0 | 71 | _single_shot(true), |
nexpaq | 0:6c56fb4bc5f0 | 72 | _interval(0), |
nexpaq | 0:6c56fb4bc5f0 | 73 | _type(M2MTimerObserver::Notdefined), |
nexpaq | 0:6c56fb4bc5f0 | 74 | _intermediate_interval(0), |
nexpaq | 0:6c56fb4bc5f0 | 75 | _total_interval(0), |
nexpaq | 0:6c56fb4bc5f0 | 76 | _status(0), |
nexpaq | 0:6c56fb4bc5f0 | 77 | _dtls_type(false) |
nexpaq | 0:6c56fb4bc5f0 | 78 | { |
nexpaq | 0:6c56fb4bc5f0 | 79 | ns_hal_init(NULL, MBED_CLIENT_EVENT_LOOP_SIZE, NULL, NULL); |
nexpaq | 0:6c56fb4bc5f0 | 80 | eventOS_scheduler_mutex_wait(); |
nexpaq | 0:6c56fb4bc5f0 | 81 | if (_tasklet_id < 0) { |
nexpaq | 0:6c56fb4bc5f0 | 82 | _tasklet_id = eventOS_event_handler_create(tasklet_func, MBED_CLIENT_TIMER_EVENT); |
nexpaq | 0:6c56fb4bc5f0 | 83 | assert(_tasklet_id >= 0); |
nexpaq | 0:6c56fb4bc5f0 | 84 | } |
nexpaq | 0:6c56fb4bc5f0 | 85 | |
nexpaq | 0:6c56fb4bc5f0 | 86 | // XXX: this wraps over quite soon |
nexpaq | 0:6c56fb4bc5f0 | 87 | _timer_id = M2MTimerPimpl::_next_timer_id++; |
nexpaq | 0:6c56fb4bc5f0 | 88 | |
nexpaq | 0:6c56fb4bc5f0 | 89 | timer_impl_list.push_back(this); |
nexpaq | 0:6c56fb4bc5f0 | 90 | eventOS_scheduler_mutex_release(); |
nexpaq | 0:6c56fb4bc5f0 | 91 | } |
nexpaq | 0:6c56fb4bc5f0 | 92 | |
nexpaq | 0:6c56fb4bc5f0 | 93 | M2MTimerPimpl::~M2MTimerPimpl() |
nexpaq | 0:6c56fb4bc5f0 | 94 | { |
nexpaq | 0:6c56fb4bc5f0 | 95 | // cancel the timer request, if any is pending |
nexpaq | 0:6c56fb4bc5f0 | 96 | cancel(); |
nexpaq | 0:6c56fb4bc5f0 | 97 | |
nexpaq | 0:6c56fb4bc5f0 | 98 | // there is no turning back, event os does not have eventOS_event_handler_delete() or similar, |
nexpaq | 0:6c56fb4bc5f0 | 99 | // so the tasklet is lost forever. Same goes with timer_impl_list, which leaks now memory. |
nexpaq | 0:6c56fb4bc5f0 | 100 | |
nexpaq | 0:6c56fb4bc5f0 | 101 | // remove the timer from object list |
nexpaq | 0:6c56fb4bc5f0 | 102 | eventOS_scheduler_mutex_wait(); |
nexpaq | 0:6c56fb4bc5f0 | 103 | int timer_count = timer_impl_list.size(); |
nexpaq | 0:6c56fb4bc5f0 | 104 | for (int index = 0; index < timer_count; index++) { |
nexpaq | 0:6c56fb4bc5f0 | 105 | |
nexpaq | 0:6c56fb4bc5f0 | 106 | const M2MTimerPimpl* timer = timer_impl_list[index]; |
nexpaq | 0:6c56fb4bc5f0 | 107 | if (timer->get_timer_id() == _timer_id) { |
nexpaq | 0:6c56fb4bc5f0 | 108 | |
nexpaq | 0:6c56fb4bc5f0 | 109 | timer_impl_list.erase(index); |
nexpaq | 0:6c56fb4bc5f0 | 110 | break; |
nexpaq | 0:6c56fb4bc5f0 | 111 | } |
nexpaq | 0:6c56fb4bc5f0 | 112 | } |
nexpaq | 0:6c56fb4bc5f0 | 113 | eventOS_scheduler_mutex_release(); |
nexpaq | 0:6c56fb4bc5f0 | 114 | } |
nexpaq | 0:6c56fb4bc5f0 | 115 | |
nexpaq | 0:6c56fb4bc5f0 | 116 | void M2MTimerPimpl::start_timer( uint64_t interval, |
nexpaq | 0:6c56fb4bc5f0 | 117 | M2MTimerObserver::Type type, |
nexpaq | 0:6c56fb4bc5f0 | 118 | bool single_shot) |
nexpaq | 0:6c56fb4bc5f0 | 119 | { |
nexpaq | 0:6c56fb4bc5f0 | 120 | assert(interval <= INT32_MAX); |
nexpaq | 0:6c56fb4bc5f0 | 121 | |
nexpaq | 0:6c56fb4bc5f0 | 122 | _dtls_type = false; |
nexpaq | 0:6c56fb4bc5f0 | 123 | _intermediate_interval = 0; |
nexpaq | 0:6c56fb4bc5f0 | 124 | _total_interval = 0; |
nexpaq | 0:6c56fb4bc5f0 | 125 | _status = 0; |
nexpaq | 0:6c56fb4bc5f0 | 126 | _single_shot = single_shot; |
nexpaq | 0:6c56fb4bc5f0 | 127 | _interval = interval; |
nexpaq | 0:6c56fb4bc5f0 | 128 | _type = type; |
nexpaq | 0:6c56fb4bc5f0 | 129 | start(); |
nexpaq | 0:6c56fb4bc5f0 | 130 | } |
nexpaq | 0:6c56fb4bc5f0 | 131 | |
nexpaq | 0:6c56fb4bc5f0 | 132 | void M2MTimerPimpl::start_dtls_timer(uint64_t intermediate_interval, uint64_t total_interval, M2MTimerObserver::Type type) |
nexpaq | 0:6c56fb4bc5f0 | 133 | { |
nexpaq | 0:6c56fb4bc5f0 | 134 | assert(intermediate_interval <= INT32_MAX); |
nexpaq | 0:6c56fb4bc5f0 | 135 | assert(intermediate_interval <= total_interval); |
nexpaq | 0:6c56fb4bc5f0 | 136 | |
nexpaq | 0:6c56fb4bc5f0 | 137 | _dtls_type = true; |
nexpaq | 0:6c56fb4bc5f0 | 138 | _intermediate_interval = intermediate_interval; |
nexpaq | 0:6c56fb4bc5f0 | 139 | _total_interval = total_interval; |
nexpaq | 0:6c56fb4bc5f0 | 140 | _interval = _intermediate_interval; |
nexpaq | 0:6c56fb4bc5f0 | 141 | _status = 0; |
nexpaq | 0:6c56fb4bc5f0 | 142 | _single_shot = false; |
nexpaq | 0:6c56fb4bc5f0 | 143 | _type = type; |
nexpaq | 0:6c56fb4bc5f0 | 144 | start(); |
nexpaq | 0:6c56fb4bc5f0 | 145 | } |
nexpaq | 0:6c56fb4bc5f0 | 146 | |
nexpaq | 0:6c56fb4bc5f0 | 147 | void M2MTimerPimpl::start() |
nexpaq | 0:6c56fb4bc5f0 | 148 | { |
nexpaq | 0:6c56fb4bc5f0 | 149 | int status; |
nexpaq | 0:6c56fb4bc5f0 | 150 | |
nexpaq | 0:6c56fb4bc5f0 | 151 | status = eventOS_event_timer_request(_timer_id, MBED_CLIENT_TIMER_EVENT, |
nexpaq | 0:6c56fb4bc5f0 | 152 | M2MTimerPimpl::_tasklet_id, |
nexpaq | 0:6c56fb4bc5f0 | 153 | _interval); |
nexpaq | 0:6c56fb4bc5f0 | 154 | assert(status == 0); |
nexpaq | 0:6c56fb4bc5f0 | 155 | } |
nexpaq | 0:6c56fb4bc5f0 | 156 | |
nexpaq | 0:6c56fb4bc5f0 | 157 | void M2MTimerPimpl::cancel() |
nexpaq | 0:6c56fb4bc5f0 | 158 | { |
nexpaq | 0:6c56fb4bc5f0 | 159 | eventOS_event_timer_cancel(_timer_id, M2MTimerPimpl::_tasklet_id); |
nexpaq | 0:6c56fb4bc5f0 | 160 | } |
nexpaq | 0:6c56fb4bc5f0 | 161 | |
nexpaq | 0:6c56fb4bc5f0 | 162 | void M2MTimerPimpl::stop_timer() |
nexpaq | 0:6c56fb4bc5f0 | 163 | { |
nexpaq | 0:6c56fb4bc5f0 | 164 | _interval = 0; |
nexpaq | 0:6c56fb4bc5f0 | 165 | _single_shot = true; |
nexpaq | 0:6c56fb4bc5f0 | 166 | cancel(); |
nexpaq | 0:6c56fb4bc5f0 | 167 | } |
nexpaq | 0:6c56fb4bc5f0 | 168 | |
nexpaq | 0:6c56fb4bc5f0 | 169 | void M2MTimerPimpl::timer_expired() |
nexpaq | 0:6c56fb4bc5f0 | 170 | { |
nexpaq | 0:6c56fb4bc5f0 | 171 | _status++; |
nexpaq | 0:6c56fb4bc5f0 | 172 | _observer.timer_expired(_type); |
nexpaq | 0:6c56fb4bc5f0 | 173 | |
nexpaq | 0:6c56fb4bc5f0 | 174 | if ((!_dtls_type) && (!_single_shot)) { |
nexpaq | 0:6c56fb4bc5f0 | 175 | // start next round of periodic timer |
nexpaq | 0:6c56fb4bc5f0 | 176 | start(); |
nexpaq | 0:6c56fb4bc5f0 | 177 | } else if ((_dtls_type) && (!is_total_interval_passed())) { |
nexpaq | 0:6c56fb4bc5f0 | 178 | // if only the intermediate time has passed, we need still wait up to total time |
nexpaq | 0:6c56fb4bc5f0 | 179 | _interval = _total_interval - _intermediate_interval; |
nexpaq | 0:6c56fb4bc5f0 | 180 | start(); |
nexpaq | 0:6c56fb4bc5f0 | 181 | } |
nexpaq | 0:6c56fb4bc5f0 | 182 | } |
nexpaq | 0:6c56fb4bc5f0 | 183 | |
nexpaq | 0:6c56fb4bc5f0 | 184 | bool M2MTimerPimpl::is_intermediate_interval_passed() |
nexpaq | 0:6c56fb4bc5f0 | 185 | { |
nexpaq | 0:6c56fb4bc5f0 | 186 | if (_status > 0) { |
nexpaq | 0:6c56fb4bc5f0 | 187 | return true; |
nexpaq | 0:6c56fb4bc5f0 | 188 | } |
nexpaq | 0:6c56fb4bc5f0 | 189 | return false; |
nexpaq | 0:6c56fb4bc5f0 | 190 | } |
nexpaq | 0:6c56fb4bc5f0 | 191 | |
nexpaq | 0:6c56fb4bc5f0 | 192 | bool M2MTimerPimpl::is_total_interval_passed() |
nexpaq | 0:6c56fb4bc5f0 | 193 | { |
nexpaq | 0:6c56fb4bc5f0 | 194 | if (_status > 1) { |
nexpaq | 0:6c56fb4bc5f0 | 195 | return true; |
nexpaq | 0:6c56fb4bc5f0 | 196 | } |
nexpaq | 0:6c56fb4bc5f0 | 197 | return false; |
nexpaq | 0:6c56fb4bc5f0 | 198 | } |