Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DTLSSocketWrapper.cpp Source File

DTLSSocketWrapper.cpp

00001 /*
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "DTLSSocketWrapper.h"
00019 #include "platform/Callback.h"
00020 #include "drivers/Timer.h"
00021 #include "events/mbed_events.h"
00022 #include "rtos/Kernel.h"
00023 
00024 #if defined(MBEDTLS_SSL_CLI_C)
00025 
00026 DTLSSocketWrapper::DTLSSocketWrapper(Socket *transport, const char *hostname, control_transport control) :
00027     TLSSocketWrapper(transport, hostname, control),
00028     _int_ms_tick(0),
00029     _timer_event_id(0),
00030     _timer_expired(false)
00031 {
00032     mbedtls_ssl_conf_transport(get_ssl_config(), MBEDTLS_SSL_TRANSPORT_DATAGRAM);
00033     mbedtls_ssl_set_timer_cb(get_ssl_context(), this, timing_set_delay, timing_get_delay);
00034 }
00035 
00036 void DTLSSocketWrapper::timing_set_delay(void *ctx, uint32_t int_ms, uint32_t fin_ms)
00037 {
00038     DTLSSocketWrapper *context = static_cast<DTLSSocketWrapper *>(ctx);
00039 
00040     if (context->_timer_event_id) {
00041         mbed::mbed_event_queue()->cancel(context->_timer_event_id);
00042         context->_timer_expired = false;
00043     }
00044 
00045     if (fin_ms == 0) {
00046         context->_timer_event_id = 0;
00047         return;
00048     }
00049 
00050     context->_int_ms_tick = rtos::Kernel::get_ms_count() + int_ms;
00051     context->_timer_event_id = mbed::mbed_event_queue()->call_in(fin_ms, context, &DTLSSocketWrapper::timer_event);
00052 }
00053 
00054 int DTLSSocketWrapper::timing_get_delay(void *ctx)
00055 {
00056     DTLSSocketWrapper *context = static_cast<DTLSSocketWrapper *>(ctx);
00057 
00058     /* See documentation of "typedef int mbedtls_ssl_get_timer_t( void * ctx );" from ssl.h */
00059 
00060     if (context->_timer_event_id == 0) {
00061         return -1;
00062     } else if (context->_timer_expired) {
00063         return 2;
00064     } else if (context->_int_ms_tick < rtos::Kernel::get_ms_count()) {
00065         return 1;
00066     } else {
00067         return 0;
00068     }
00069 }
00070 
00071 void DTLSSocketWrapper::timer_event(void)
00072 {
00073     _timer_expired = true;
00074     event();
00075 }
00076 
00077 #endif /* MBEDTLS_SSL_CLI_C */