Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 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 #include "mbed-client-classic/m2mconnectionhandlerpimpl.h"
nexpaq 0:6c56fb4bc5f0 17 #include "mbed-client/m2mconnectionobserver.h"
nexpaq 0:6c56fb4bc5f0 18 #include "mbed-client/m2mconstants.h"
nexpaq 0:6c56fb4bc5f0 19 #include "mbed-client/m2msecurity.h"
nexpaq 0:6c56fb4bc5f0 20 #include "mbed-client/m2mconnectionhandler.h"
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 #include "NetworkInterface.h"
nexpaq 0:6c56fb4bc5f0 23 #include "UDPSocket.h"
nexpaq 0:6c56fb4bc5f0 24 #include "TCPSocket.h"
nexpaq 0:6c56fb4bc5f0 25
nexpaq 0:6c56fb4bc5f0 26 #include "eventOS_event.h"
nexpaq 0:6c56fb4bc5f0 27 #include "eventOS_scheduler.h"
nexpaq 0:6c56fb4bc5f0 28
nexpaq 0:6c56fb4bc5f0 29 #include "mbed-trace/mbed_trace.h"
nexpaq 0:6c56fb4bc5f0 30 #include "mbed.h"
nexpaq 0:6c56fb4bc5f0 31
nexpaq 0:6c56fb4bc5f0 32 #define TRACE_GROUP "mClt"
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 M2MConnectionHandlerPimpl::_tasklet_id = -1;
nexpaq 0:6c56fb4bc5f0 41
nexpaq 0:6c56fb4bc5f0 42 static MemoryPool<M2MConnectionHandlerPimpl::TaskIdentifier, MBED_CLIENT_EVENT_LOOP_SIZE/64> memory_pool;
nexpaq 0:6c56fb4bc5f0 43
nexpaq 0:6c56fb4bc5f0 44 extern "C" void connection_tasklet_event_handler(arm_event_s *event)
nexpaq 0:6c56fb4bc5f0 45 {
nexpaq 0:6c56fb4bc5f0 46 tr_debug("M2MConnectionHandlerPimpl::connection_tasklet_event_handler");
nexpaq 0:6c56fb4bc5f0 47 M2MConnectionHandlerPimpl::TaskIdentifier *task_id = (M2MConnectionHandlerPimpl::TaskIdentifier*)event->data_ptr;
nexpaq 0:6c56fb4bc5f0 48 M2MConnectionHandlerPimpl* pimpl = (M2MConnectionHandlerPimpl*)task_id->pimpl;
nexpaq 0:6c56fb4bc5f0 49 if(pimpl) {
nexpaq 0:6c56fb4bc5f0 50 eventOS_scheduler_set_active_tasklet(pimpl->connection_tasklet_handler());
nexpaq 0:6c56fb4bc5f0 51 }
nexpaq 0:6c56fb4bc5f0 52 switch (event->event_type) {
nexpaq 0:6c56fb4bc5f0 53 case M2MConnectionHandlerPimpl::ESocketIdle:
nexpaq 0:6c56fb4bc5f0 54 tr_debug("Connection Tasklet Generated");
nexpaq 0:6c56fb4bc5f0 55 break;
nexpaq 0:6c56fb4bc5f0 56 case M2MConnectionHandlerPimpl::ESocketReadytoRead:
nexpaq 0:6c56fb4bc5f0 57 tr_debug("connection_tasklet_event_handler - ESocketReadytoRead");
nexpaq 0:6c56fb4bc5f0 58 if(pimpl) {
nexpaq 0:6c56fb4bc5f0 59 if(pimpl->is_handshake_ongoing()) {
nexpaq 0:6c56fb4bc5f0 60 pimpl->receive_handshake_handler();
nexpaq 0:6c56fb4bc5f0 61 } else {
nexpaq 0:6c56fb4bc5f0 62 pimpl->receive_handler();
nexpaq 0:6c56fb4bc5f0 63 }
nexpaq 0:6c56fb4bc5f0 64 }
nexpaq 0:6c56fb4bc5f0 65 break;
nexpaq 0:6c56fb4bc5f0 66 case M2MConnectionHandlerPimpl::ESocketDnsHandler:
nexpaq 0:6c56fb4bc5f0 67 tr_debug("connection_tasklet_event_handler - ESocketDnsHandler");
nexpaq 0:6c56fb4bc5f0 68 if(pimpl) {
nexpaq 0:6c56fb4bc5f0 69 pimpl->dns_handler();
nexpaq 0:6c56fb4bc5f0 70 }
nexpaq 0:6c56fb4bc5f0 71 break;
nexpaq 0:6c56fb4bc5f0 72 case M2MConnectionHandlerPimpl::ESocketSend:
nexpaq 0:6c56fb4bc5f0 73 tr_debug("connection_tasklet_event_handler - ESocketSend");
nexpaq 0:6c56fb4bc5f0 74 if(pimpl) {
nexpaq 0:6c56fb4bc5f0 75 pimpl->send_socket_data((uint8_t*)task_id->data_ptr,(uint16_t)event->event_data);
nexpaq 0:6c56fb4bc5f0 76 if (task_id->data_ptr) {
nexpaq 0:6c56fb4bc5f0 77 free(task_id->data_ptr);
nexpaq 0:6c56fb4bc5f0 78 }
nexpaq 0:6c56fb4bc5f0 79 }
nexpaq 0:6c56fb4bc5f0 80 break;
nexpaq 0:6c56fb4bc5f0 81 default:
nexpaq 0:6c56fb4bc5f0 82 break;
nexpaq 0:6c56fb4bc5f0 83 }
nexpaq 0:6c56fb4bc5f0 84 if (task_id) {
nexpaq 0:6c56fb4bc5f0 85 memory_pool.free(task_id);
nexpaq 0:6c56fb4bc5f0 86 }
nexpaq 0:6c56fb4bc5f0 87 }
nexpaq 0:6c56fb4bc5f0 88
nexpaq 0:6c56fb4bc5f0 89 M2MConnectionHandlerPimpl::M2MConnectionHandlerPimpl(M2MConnectionHandler* base, M2MConnectionObserver &observer,
nexpaq 0:6c56fb4bc5f0 90 M2MConnectionSecurity* sec,
nexpaq 0:6c56fb4bc5f0 91 M2MInterface::BindingMode mode,
nexpaq 0:6c56fb4bc5f0 92 M2MInterface::NetworkStack stack)
nexpaq 0:6c56fb4bc5f0 93 :_base(base),
nexpaq 0:6c56fb4bc5f0 94 _observer(observer),
nexpaq 0:6c56fb4bc5f0 95 _security_impl(sec),
nexpaq 0:6c56fb4bc5f0 96 _use_secure_connection(false),
nexpaq 0:6c56fb4bc5f0 97 _binding_mode(mode),
nexpaq 0:6c56fb4bc5f0 98 _network_stack(stack),
nexpaq 0:6c56fb4bc5f0 99 _socket(0),
nexpaq 0:6c56fb4bc5f0 100 _is_handshaking(false),
nexpaq 0:6c56fb4bc5f0 101 _listening(true),
nexpaq 0:6c56fb4bc5f0 102 _server_type(M2MConnectionObserver::LWM2MServer),
nexpaq 0:6c56fb4bc5f0 103 _server_port(0),
nexpaq 0:6c56fb4bc5f0 104 _listen_port(0),
nexpaq 0:6c56fb4bc5f0 105 _running(false),
nexpaq 0:6c56fb4bc5f0 106 _net_iface(0),
nexpaq 0:6c56fb4bc5f0 107 _socket_address(0)
nexpaq 0:6c56fb4bc5f0 108 {
nexpaq 0:6c56fb4bc5f0 109 memset(&_address_buffer, 0, sizeof _address_buffer);
nexpaq 0:6c56fb4bc5f0 110 memset(&_address, 0, sizeof _address);
nexpaq 0:6c56fb4bc5f0 111 _address._address = _address_buffer;
nexpaq 0:6c56fb4bc5f0 112
nexpaq 0:6c56fb4bc5f0 113 if (_network_stack != M2MInterface::LwIP_IPv4) {
nexpaq 0:6c56fb4bc5f0 114 tr_error("ConnectionHandler: Unsupported network stack, only IPv4 is currently supported");
nexpaq 0:6c56fb4bc5f0 115 }
nexpaq 0:6c56fb4bc5f0 116 _running = true;
nexpaq 0:6c56fb4bc5f0 117 tr_debug("M2MConnectionHandlerPimpl::M2MConnectionHandlerPimpl() - Initializing thread");
nexpaq 0:6c56fb4bc5f0 118 eventOS_scheduler_mutex_wait();
nexpaq 0:6c56fb4bc5f0 119 if (M2MConnectionHandlerPimpl::_tasklet_id == -1) {
nexpaq 0:6c56fb4bc5f0 120 M2MConnectionHandlerPimpl::_tasklet_id = eventOS_event_handler_create(&connection_tasklet_event_handler, ESocketIdle);
nexpaq 0:6c56fb4bc5f0 121 }
nexpaq 0:6c56fb4bc5f0 122 eventOS_scheduler_mutex_release();
nexpaq 0:6c56fb4bc5f0 123 }
nexpaq 0:6c56fb4bc5f0 124
nexpaq 0:6c56fb4bc5f0 125 M2MConnectionHandlerPimpl::~M2MConnectionHandlerPimpl()
nexpaq 0:6c56fb4bc5f0 126 {
nexpaq 0:6c56fb4bc5f0 127 tr_debug("M2MConnectionHandlerPimpl::~M2MConnectionHandlerPimpl()");
nexpaq 0:6c56fb4bc5f0 128 if(_socket_address) {
nexpaq 0:6c56fb4bc5f0 129 delete _socket_address;
nexpaq 0:6c56fb4bc5f0 130 _socket_address = NULL;
nexpaq 0:6c56fb4bc5f0 131 }
nexpaq 0:6c56fb4bc5f0 132 if (_socket) {
nexpaq 0:6c56fb4bc5f0 133 delete _socket;
nexpaq 0:6c56fb4bc5f0 134 _socket = 0;
nexpaq 0:6c56fb4bc5f0 135 }
nexpaq 0:6c56fb4bc5f0 136 _net_iface = 0;
nexpaq 0:6c56fb4bc5f0 137 delete _security_impl;
nexpaq 0:6c56fb4bc5f0 138 tr_debug("M2MConnectionHandlerPimpl::~M2MConnectionHandlerPimpl() - OUT");
nexpaq 0:6c56fb4bc5f0 139 }
nexpaq 0:6c56fb4bc5f0 140
nexpaq 0:6c56fb4bc5f0 141 bool M2MConnectionHandlerPimpl::bind_connection(const uint16_t listen_port)
nexpaq 0:6c56fb4bc5f0 142 {
nexpaq 0:6c56fb4bc5f0 143 _listen_port = listen_port;
nexpaq 0:6c56fb4bc5f0 144 return true;
nexpaq 0:6c56fb4bc5f0 145 }
nexpaq 0:6c56fb4bc5f0 146
nexpaq 0:6c56fb4bc5f0 147 bool M2MConnectionHandlerPimpl::resolve_server_address(const String& server_address,
nexpaq 0:6c56fb4bc5f0 148 const uint16_t server_port,
nexpaq 0:6c56fb4bc5f0 149 M2MConnectionObserver::ServerType server_type,
nexpaq 0:6c56fb4bc5f0 150 const M2MSecurity* security)
nexpaq 0:6c56fb4bc5f0 151 {
nexpaq 0:6c56fb4bc5f0 152 tr_debug("M2MConnectionHandlerPimpl::resolve_server_address()");
nexpaq 0:6c56fb4bc5f0 153 if (!_net_iface) {
nexpaq 0:6c56fb4bc5f0 154 return false;
nexpaq 0:6c56fb4bc5f0 155 }
nexpaq 0:6c56fb4bc5f0 156 _security = security;
nexpaq 0:6c56fb4bc5f0 157 _server_port = server_port;
nexpaq 0:6c56fb4bc5f0 158 _server_type = server_type;
nexpaq 0:6c56fb4bc5f0 159 _server_address = server_address;
nexpaq 0:6c56fb4bc5f0 160 TaskIdentifier* task = memory_pool.alloc();
nexpaq 0:6c56fb4bc5f0 161 if (!task) {
nexpaq 0:6c56fb4bc5f0 162 return false;
nexpaq 0:6c56fb4bc5f0 163 }
nexpaq 0:6c56fb4bc5f0 164 task->pimpl = this;
nexpaq 0:6c56fb4bc5f0 165
nexpaq 0:6c56fb4bc5f0 166 arm_event_s event;
nexpaq 0:6c56fb4bc5f0 167 event.receiver = M2MConnectionHandlerPimpl::_tasklet_id;
nexpaq 0:6c56fb4bc5f0 168 event.sender = 0;
nexpaq 0:6c56fb4bc5f0 169 event.event_type = ESocketDnsHandler;
nexpaq 0:6c56fb4bc5f0 170 event.data_ptr = task;
nexpaq 0:6c56fb4bc5f0 171 event.priority = ARM_LIB_HIGH_PRIORITY_EVENT;
nexpaq 0:6c56fb4bc5f0 172 return eventOS_event_send(&event) == 0 ? true : false;
nexpaq 0:6c56fb4bc5f0 173 }
nexpaq 0:6c56fb4bc5f0 174
nexpaq 0:6c56fb4bc5f0 175 void M2MConnectionHandlerPimpl::dns_handler()
nexpaq 0:6c56fb4bc5f0 176 {
nexpaq 0:6c56fb4bc5f0 177 tr_debug("M2MConnectionHandlerPimpl::dns_handler()");
nexpaq 0:6c56fb4bc5f0 178 if(_socket_address) {
nexpaq 0:6c56fb4bc5f0 179 delete _socket_address;
nexpaq 0:6c56fb4bc5f0 180 _socket_address = NULL;
nexpaq 0:6c56fb4bc5f0 181 }
nexpaq 0:6c56fb4bc5f0 182 _socket_address = new SocketAddress(_net_iface,_server_address.c_str(), _server_port);
nexpaq 0:6c56fb4bc5f0 183 if(*_socket_address) {
nexpaq 0:6c56fb4bc5f0 184 _address._address = (void*)_socket_address->get_ip_address();
nexpaq 0:6c56fb4bc5f0 185 tr_debug("IP Address %s",_socket_address->get_ip_address());
nexpaq 0:6c56fb4bc5f0 186 tr_debug("Port %d",_socket_address->get_port());
nexpaq 0:6c56fb4bc5f0 187 _address._length = strlen((char*)_address._address);
nexpaq 0:6c56fb4bc5f0 188 _address._port = _socket_address->get_port();
nexpaq 0:6c56fb4bc5f0 189 _address._stack = _network_stack;
nexpaq 0:6c56fb4bc5f0 190 } else {
nexpaq 0:6c56fb4bc5f0 191 _observer.socket_error(M2MConnectionHandler::DNS_RESOLVING_ERROR, true);
nexpaq 0:6c56fb4bc5f0 192 close_socket();
nexpaq 0:6c56fb4bc5f0 193 return;
nexpaq 0:6c56fb4bc5f0 194 }
nexpaq 0:6c56fb4bc5f0 195
nexpaq 0:6c56fb4bc5f0 196 close_socket();
nexpaq 0:6c56fb4bc5f0 197 init_socket();
nexpaq 0:6c56fb4bc5f0 198
nexpaq 0:6c56fb4bc5f0 199 if(is_tcp_connection()) {
nexpaq 0:6c56fb4bc5f0 200 tr_debug("M2MConnectionHandlerPimpl::resolve_server_address - Using TCP");
nexpaq 0:6c56fb4bc5f0 201 if (((TCPSocket*)_socket)->connect(*_socket_address) < 0) {
nexpaq 0:6c56fb4bc5f0 202 _observer.socket_error(M2MConnectionHandler::SOCKET_ABORT);
nexpaq 0:6c56fb4bc5f0 203 return;
nexpaq 0:6c56fb4bc5f0 204 }
nexpaq 0:6c56fb4bc5f0 205 }
nexpaq 0:6c56fb4bc5f0 206
nexpaq 0:6c56fb4bc5f0 207 _running = true;
nexpaq 0:6c56fb4bc5f0 208
nexpaq 0:6c56fb4bc5f0 209 if (_security) {
nexpaq 0:6c56fb4bc5f0 210 if (_security->resource_value_int(M2MSecurity::SecurityMode) == M2MSecurity::Certificate ||
nexpaq 0:6c56fb4bc5f0 211 _security->resource_value_int(M2MSecurity::SecurityMode) == M2MSecurity::Psk) {
nexpaq 0:6c56fb4bc5f0 212
nexpaq 0:6c56fb4bc5f0 213 if( _security_impl != NULL ){
nexpaq 0:6c56fb4bc5f0 214 _security_impl->reset();
nexpaq 0:6c56fb4bc5f0 215 if (_security_impl->init(_security) == 0) {
nexpaq 0:6c56fb4bc5f0 216 _is_handshaking = true;
nexpaq 0:6c56fb4bc5f0 217 tr_debug("M2MConnectionHandlerPimpl::resolve_server_address - connect DTLS");
nexpaq 0:6c56fb4bc5f0 218 if(_security_impl->start_connecting_non_blocking(_base) < 0 ){
nexpaq 0:6c56fb4bc5f0 219 tr_debug("M2MConnectionHandlerPimpl::dns_handler - handshake failed");
nexpaq 0:6c56fb4bc5f0 220 _is_handshaking = false;
nexpaq 0:6c56fb4bc5f0 221 _observer.socket_error(M2MConnectionHandler::SSL_CONNECTION_ERROR);
nexpaq 0:6c56fb4bc5f0 222 close_socket();
nexpaq 0:6c56fb4bc5f0 223 return;
nexpaq 0:6c56fb4bc5f0 224 }
nexpaq 0:6c56fb4bc5f0 225 } else {
nexpaq 0:6c56fb4bc5f0 226 tr_error("M2MConnectionHandlerPimpl::resolve_server_address - init failed");
nexpaq 0:6c56fb4bc5f0 227 _observer.socket_error(M2MConnectionHandler::SSL_CONNECTION_ERROR, false);
nexpaq 0:6c56fb4bc5f0 228 close_socket();
nexpaq 0:6c56fb4bc5f0 229 return;
nexpaq 0:6c56fb4bc5f0 230 }
nexpaq 0:6c56fb4bc5f0 231 } else {
nexpaq 0:6c56fb4bc5f0 232 tr_error("M2MConnectionHandlerPimpl::dns_handler - sec is null");
nexpaq 0:6c56fb4bc5f0 233 _observer.socket_error(M2MConnectionHandler::SSL_CONNECTION_ERROR, false);
nexpaq 0:6c56fb4bc5f0 234 close_socket();
nexpaq 0:6c56fb4bc5f0 235 return;
nexpaq 0:6c56fb4bc5f0 236 }
nexpaq 0:6c56fb4bc5f0 237 }
nexpaq 0:6c56fb4bc5f0 238 }
nexpaq 0:6c56fb4bc5f0 239 if(!_is_handshaking) {
nexpaq 0:6c56fb4bc5f0 240 enable_keepalive();
nexpaq 0:6c56fb4bc5f0 241 _observer.address_ready(_address,
nexpaq 0:6c56fb4bc5f0 242 _server_type,
nexpaq 0:6c56fb4bc5f0 243 _address._port);
nexpaq 0:6c56fb4bc5f0 244 }
nexpaq 0:6c56fb4bc5f0 245 }
nexpaq 0:6c56fb4bc5f0 246
nexpaq 0:6c56fb4bc5f0 247 void M2MConnectionHandlerPimpl::send_handler()
nexpaq 0:6c56fb4bc5f0 248 {
nexpaq 0:6c56fb4bc5f0 249 tr_debug("M2MConnectionHandlerPimpl::send_handler()");
nexpaq 0:6c56fb4bc5f0 250 _observer.data_sent();
nexpaq 0:6c56fb4bc5f0 251 }
nexpaq 0:6c56fb4bc5f0 252
nexpaq 0:6c56fb4bc5f0 253 bool M2MConnectionHandlerPimpl::send_data(uint8_t *data,
nexpaq 0:6c56fb4bc5f0 254 uint16_t data_len,
nexpaq 0:6c56fb4bc5f0 255 sn_nsdl_addr_s *address)
nexpaq 0:6c56fb4bc5f0 256 {
nexpaq 0:6c56fb4bc5f0 257 tr_debug("M2MConnectionHandlerPimpl::send_data()");
nexpaq 0:6c56fb4bc5f0 258 if (address == NULL || data == NULL) {
nexpaq 0:6c56fb4bc5f0 259 return false;
nexpaq 0:6c56fb4bc5f0 260 }
nexpaq 0:6c56fb4bc5f0 261
nexpaq 0:6c56fb4bc5f0 262 uint8_t *buffer = (uint8_t*)malloc(data_len);
nexpaq 0:6c56fb4bc5f0 263 if(!buffer) {
nexpaq 0:6c56fb4bc5f0 264 return false;
nexpaq 0:6c56fb4bc5f0 265 }
nexpaq 0:6c56fb4bc5f0 266
nexpaq 0:6c56fb4bc5f0 267 TaskIdentifier* task = memory_pool.alloc();
nexpaq 0:6c56fb4bc5f0 268 if (!task) {
nexpaq 0:6c56fb4bc5f0 269 free(buffer);
nexpaq 0:6c56fb4bc5f0 270 return false;
nexpaq 0:6c56fb4bc5f0 271 }
nexpaq 0:6c56fb4bc5f0 272 task->pimpl = this;
nexpaq 0:6c56fb4bc5f0 273 memcpy(buffer, data, data_len);
nexpaq 0:6c56fb4bc5f0 274 task->data_ptr = buffer;
nexpaq 0:6c56fb4bc5f0 275 arm_event_s event;
nexpaq 0:6c56fb4bc5f0 276 event.receiver = M2MConnectionHandlerPimpl::_tasklet_id;
nexpaq 0:6c56fb4bc5f0 277 event.sender = 0;
nexpaq 0:6c56fb4bc5f0 278 event.event_type = ESocketSend;
nexpaq 0:6c56fb4bc5f0 279 event.data_ptr = task;
nexpaq 0:6c56fb4bc5f0 280 event.event_data = data_len;
nexpaq 0:6c56fb4bc5f0 281 event.priority = ARM_LIB_HIGH_PRIORITY_EVENT;
nexpaq 0:6c56fb4bc5f0 282
nexpaq 0:6c56fb4bc5f0 283 return eventOS_event_send(&event) == 0 ? true : false;
nexpaq 0:6c56fb4bc5f0 284 }
nexpaq 0:6c56fb4bc5f0 285
nexpaq 0:6c56fb4bc5f0 286 void M2MConnectionHandlerPimpl::send_socket_data(uint8_t *data,
nexpaq 0:6c56fb4bc5f0 287 uint16_t data_len)
nexpaq 0:6c56fb4bc5f0 288 {
nexpaq 0:6c56fb4bc5f0 289 bool success = false;
nexpaq 0:6c56fb4bc5f0 290 if( _use_secure_connection ){
nexpaq 0:6c56fb4bc5f0 291 if( _security_impl->send_message(data, data_len) > 0){
nexpaq 0:6c56fb4bc5f0 292 success = true;
nexpaq 0:6c56fb4bc5f0 293 }
nexpaq 0:6c56fb4bc5f0 294 } else {
nexpaq 0:6c56fb4bc5f0 295 int32_t ret = -1;
nexpaq 0:6c56fb4bc5f0 296 if(is_tcp_connection()){
nexpaq 0:6c56fb4bc5f0 297 //We need to "shim" the length in front
nexpaq 0:6c56fb4bc5f0 298 uint16_t d_len = data_len+4;
nexpaq 0:6c56fb4bc5f0 299 uint8_t* d = (uint8_t*)malloc(data_len+4);
nexpaq 0:6c56fb4bc5f0 300
nexpaq 0:6c56fb4bc5f0 301 d[0] = (data_len >> 24 )& 0xff;
nexpaq 0:6c56fb4bc5f0 302 d[1] = (data_len >> 16 )& 0xff;
nexpaq 0:6c56fb4bc5f0 303 d[2] = (data_len >> 8 )& 0xff;
nexpaq 0:6c56fb4bc5f0 304 d[3] = data_len & 0xff;
nexpaq 0:6c56fb4bc5f0 305 memmove(d+4, data, data_len);
nexpaq 0:6c56fb4bc5f0 306 ret = ((TCPSocket*)_socket)->send(d,d_len);
nexpaq 0:6c56fb4bc5f0 307 free(d);
nexpaq 0:6c56fb4bc5f0 308 }else {
nexpaq 0:6c56fb4bc5f0 309 ret = ((UDPSocket*)_socket)->sendto(*_socket_address,data, data_len);
nexpaq 0:6c56fb4bc5f0 310 }
nexpaq 0:6c56fb4bc5f0 311 if (ret > 0) {
nexpaq 0:6c56fb4bc5f0 312 success = true;
nexpaq 0:6c56fb4bc5f0 313 }
nexpaq 0:6c56fb4bc5f0 314 }
nexpaq 0:6c56fb4bc5f0 315
nexpaq 0:6c56fb4bc5f0 316 if (!success) {
nexpaq 0:6c56fb4bc5f0 317 _observer.socket_error(M2MConnectionHandler::SOCKET_SEND_ERROR, true);
nexpaq 0:6c56fb4bc5f0 318 close_socket();
nexpaq 0:6c56fb4bc5f0 319 }
nexpaq 0:6c56fb4bc5f0 320 }
nexpaq 0:6c56fb4bc5f0 321
nexpaq 0:6c56fb4bc5f0 322 int8_t M2MConnectionHandlerPimpl::connection_tasklet_handler()
nexpaq 0:6c56fb4bc5f0 323 {
nexpaq 0:6c56fb4bc5f0 324 return M2MConnectionHandlerPimpl::_tasklet_id;
nexpaq 0:6c56fb4bc5f0 325 }
nexpaq 0:6c56fb4bc5f0 326
nexpaq 0:6c56fb4bc5f0 327 void M2MConnectionHandlerPimpl::socket_event()
nexpaq 0:6c56fb4bc5f0 328 {
nexpaq 0:6c56fb4bc5f0 329 TaskIdentifier* task = memory_pool.alloc();
nexpaq 0:6c56fb4bc5f0 330 if (!task) {
nexpaq 0:6c56fb4bc5f0 331 _observer.socket_error(M2MConnectionHandler::SOCKET_READ_ERROR, true);
nexpaq 0:6c56fb4bc5f0 332 return;
nexpaq 0:6c56fb4bc5f0 333 }
nexpaq 0:6c56fb4bc5f0 334 task->pimpl = this;
nexpaq 0:6c56fb4bc5f0 335
nexpaq 0:6c56fb4bc5f0 336 arm_event_s event;
nexpaq 0:6c56fb4bc5f0 337 event.receiver = M2MConnectionHandlerPimpl::_tasklet_id;
nexpaq 0:6c56fb4bc5f0 338 event.sender = 0;
nexpaq 0:6c56fb4bc5f0 339 event.event_type = ESocketReadytoRead;
nexpaq 0:6c56fb4bc5f0 340 event.data_ptr = task;
nexpaq 0:6c56fb4bc5f0 341 event.priority = ARM_LIB_HIGH_PRIORITY_EVENT;
nexpaq 0:6c56fb4bc5f0 342 int8_t error = eventOS_event_send(&event);
nexpaq 0:6c56fb4bc5f0 343 if(error != 0) {
nexpaq 0:6c56fb4bc5f0 344 _observer.socket_error(M2MConnectionHandler::SOCKET_READ_ERROR, true);
nexpaq 0:6c56fb4bc5f0 345 }
nexpaq 0:6c56fb4bc5f0 346 }
nexpaq 0:6c56fb4bc5f0 347
nexpaq 0:6c56fb4bc5f0 348 bool M2MConnectionHandlerPimpl::start_listening_for_data()
nexpaq 0:6c56fb4bc5f0 349 {
nexpaq 0:6c56fb4bc5f0 350 tr_debug("M2MConnectionHandlerPimpl::start_listening_for_data()");
nexpaq 0:6c56fb4bc5f0 351 // Boolean return required for other platforms,
nexpaq 0:6c56fb4bc5f0 352 // not needed in mbed OS Socket.
nexpaq 0:6c56fb4bc5f0 353 _listening = true;
nexpaq 0:6c56fb4bc5f0 354 _running = true;
nexpaq 0:6c56fb4bc5f0 355 return _listening;
nexpaq 0:6c56fb4bc5f0 356 }
nexpaq 0:6c56fb4bc5f0 357
nexpaq 0:6c56fb4bc5f0 358 void M2MConnectionHandlerPimpl::stop_listening()
nexpaq 0:6c56fb4bc5f0 359 {
nexpaq 0:6c56fb4bc5f0 360 tr_debug("M2MConnectionHandlerPimpl::stop_listening()");
nexpaq 0:6c56fb4bc5f0 361 _listening = false;
nexpaq 0:6c56fb4bc5f0 362 if(_security_impl) {
nexpaq 0:6c56fb4bc5f0 363 _security_impl->reset();
nexpaq 0:6c56fb4bc5f0 364 }
nexpaq 0:6c56fb4bc5f0 365 }
nexpaq 0:6c56fb4bc5f0 366
nexpaq 0:6c56fb4bc5f0 367 int M2MConnectionHandlerPimpl::send_to_socket(const unsigned char *buf, size_t len)
nexpaq 0:6c56fb4bc5f0 368 {
nexpaq 0:6c56fb4bc5f0 369 tr_debug("M2MConnectionHandlerPimpl::send_to_socket len - %d", len);
nexpaq 0:6c56fb4bc5f0 370 int size = -1;
nexpaq 0:6c56fb4bc5f0 371 if(is_tcp_connection()) {
nexpaq 0:6c56fb4bc5f0 372 size = ((TCPSocket*)_socket)->send(buf,len);
nexpaq 0:6c56fb4bc5f0 373 } else {
nexpaq 0:6c56fb4bc5f0 374 size = ((UDPSocket*)_socket)->sendto(*_socket_address,buf,len);
nexpaq 0:6c56fb4bc5f0 375 }
nexpaq 0:6c56fb4bc5f0 376 tr_debug("M2MConnectionHandlerPimpl::send_to_socket size - %d", size);
nexpaq 0:6c56fb4bc5f0 377 if(NSAPI_ERROR_WOULD_BLOCK == size){
nexpaq 0:6c56fb4bc5f0 378 if(_is_handshaking) {
nexpaq 0:6c56fb4bc5f0 379 return M2MConnectionHandler::CONNECTION_ERROR_WANTS_WRITE;
nexpaq 0:6c56fb4bc5f0 380 } else {
nexpaq 0:6c56fb4bc5f0 381 return len;
nexpaq 0:6c56fb4bc5f0 382 }
nexpaq 0:6c56fb4bc5f0 383 }else if(size < 0){
nexpaq 0:6c56fb4bc5f0 384 return -1;
nexpaq 0:6c56fb4bc5f0 385 }else{
nexpaq 0:6c56fb4bc5f0 386 if(!_is_handshaking) {
nexpaq 0:6c56fb4bc5f0 387 _observer.data_sent();
nexpaq 0:6c56fb4bc5f0 388 }
nexpaq 0:6c56fb4bc5f0 389 return size;
nexpaq 0:6c56fb4bc5f0 390 }
nexpaq 0:6c56fb4bc5f0 391 }
nexpaq 0:6c56fb4bc5f0 392
nexpaq 0:6c56fb4bc5f0 393 int M2MConnectionHandlerPimpl::receive_from_socket(unsigned char *buf, size_t len)
nexpaq 0:6c56fb4bc5f0 394 {
nexpaq 0:6c56fb4bc5f0 395 tr_debug("M2MConnectionHandlerPimpl::receive_from_socket");
nexpaq 0:6c56fb4bc5f0 396 int recv = -1;
nexpaq 0:6c56fb4bc5f0 397 if(is_tcp_connection()) {
nexpaq 0:6c56fb4bc5f0 398 recv = ((TCPSocket*)_socket)->recv(buf, len);
nexpaq 0:6c56fb4bc5f0 399 } else {
nexpaq 0:6c56fb4bc5f0 400 recv = ((UDPSocket*)_socket)->recvfrom(NULL,buf, len);
nexpaq 0:6c56fb4bc5f0 401 }
nexpaq 0:6c56fb4bc5f0 402 tr_debug("M2MConnectionHandlerPimpl::receive_from_socket recv size %d", recv);
nexpaq 0:6c56fb4bc5f0 403 if(NSAPI_ERROR_WOULD_BLOCK == recv){
nexpaq 0:6c56fb4bc5f0 404 return M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ;
nexpaq 0:6c56fb4bc5f0 405 }else if(recv < 0){
nexpaq 0:6c56fb4bc5f0 406 return -1;
nexpaq 0:6c56fb4bc5f0 407 }else{
nexpaq 0:6c56fb4bc5f0 408 return recv;
nexpaq 0:6c56fb4bc5f0 409 }
nexpaq 0:6c56fb4bc5f0 410 }
nexpaq 0:6c56fb4bc5f0 411
nexpaq 0:6c56fb4bc5f0 412 void M2MConnectionHandlerPimpl::handle_connection_error(int error)
nexpaq 0:6c56fb4bc5f0 413 {
nexpaq 0:6c56fb4bc5f0 414 tr_debug("M2MConnectionHandlerPimpl::handle_connection_error");
nexpaq 0:6c56fb4bc5f0 415 _observer.socket_error(error);
nexpaq 0:6c56fb4bc5f0 416 }
nexpaq 0:6c56fb4bc5f0 417
nexpaq 0:6c56fb4bc5f0 418 void M2MConnectionHandlerPimpl::set_platform_network_handler(void *handler)
nexpaq 0:6c56fb4bc5f0 419 {
nexpaq 0:6c56fb4bc5f0 420 tr_debug("M2MConnectionHandlerPimpl::set_platform_network_handler");
nexpaq 0:6c56fb4bc5f0 421 _net_iface = (NetworkInterface*)handler;
nexpaq 0:6c56fb4bc5f0 422 }
nexpaq 0:6c56fb4bc5f0 423
nexpaq 0:6c56fb4bc5f0 424 void M2MConnectionHandlerPimpl::receive_handshake_handler()
nexpaq 0:6c56fb4bc5f0 425 {
nexpaq 0:6c56fb4bc5f0 426 tr_debug("M2MConnectionHandlerPimpl::receive_handshake_handler()");
nexpaq 0:6c56fb4bc5f0 427 if( _is_handshaking ){
nexpaq 0:6c56fb4bc5f0 428 int ret = _security_impl->continue_connecting();
nexpaq 0:6c56fb4bc5f0 429 tr_debug("M2MConnectionHandlerPimpl::receive_handshake_handler() - ret %d", ret);
nexpaq 0:6c56fb4bc5f0 430 if( ret == M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ ){ //We wait for next readable event
nexpaq 0:6c56fb4bc5f0 431 tr_debug("M2MConnectionHandlerPimpl::receive_handshake_handler() - We wait for next readable event");
nexpaq 0:6c56fb4bc5f0 432 return;
nexpaq 0:6c56fb4bc5f0 433 } else if( ret == 0 ){
nexpaq 0:6c56fb4bc5f0 434 _is_handshaking = false;
nexpaq 0:6c56fb4bc5f0 435 _use_secure_connection = true;
nexpaq 0:6c56fb4bc5f0 436 enable_keepalive();
nexpaq 0:6c56fb4bc5f0 437 _observer.address_ready(_address,
nexpaq 0:6c56fb4bc5f0 438 _server_type,
nexpaq 0:6c56fb4bc5f0 439 _server_port);
nexpaq 0:6c56fb4bc5f0 440 }else if( ret < 0 ){
nexpaq 0:6c56fb4bc5f0 441 _is_handshaking = false;
nexpaq 0:6c56fb4bc5f0 442 _observer.socket_error(M2MConnectionHandler::SSL_CONNECTION_ERROR, true);
nexpaq 0:6c56fb4bc5f0 443 close_socket();
nexpaq 0:6c56fb4bc5f0 444 }
nexpaq 0:6c56fb4bc5f0 445 }
nexpaq 0:6c56fb4bc5f0 446 }
nexpaq 0:6c56fb4bc5f0 447
nexpaq 0:6c56fb4bc5f0 448 bool M2MConnectionHandlerPimpl::is_handshake_ongoing()
nexpaq 0:6c56fb4bc5f0 449 {
nexpaq 0:6c56fb4bc5f0 450 return _is_handshaking;
nexpaq 0:6c56fb4bc5f0 451 }
nexpaq 0:6c56fb4bc5f0 452
nexpaq 0:6c56fb4bc5f0 453 void M2MConnectionHandlerPimpl::receive_handler()
nexpaq 0:6c56fb4bc5f0 454 {
nexpaq 0:6c56fb4bc5f0 455 tr_debug("M2MConnectionHandlerPimpl::receive_handler()");
nexpaq 0:6c56fb4bc5f0 456 memset(_recv_buffer, 0, 1024);
nexpaq 0:6c56fb4bc5f0 457 size_t receive_length = sizeof(_recv_buffer);
nexpaq 0:6c56fb4bc5f0 458
nexpaq 0:6c56fb4bc5f0 459 if(_listening) {
nexpaq 0:6c56fb4bc5f0 460 if( _use_secure_connection ){
nexpaq 0:6c56fb4bc5f0 461 int rcv_size = _security_impl->read(_recv_buffer, receive_length);
nexpaq 0:6c56fb4bc5f0 462
nexpaq 0:6c56fb4bc5f0 463 if(rcv_size >= 0){
nexpaq 0:6c56fb4bc5f0 464 _observer.data_available((uint8_t*)_recv_buffer,
nexpaq 0:6c56fb4bc5f0 465 rcv_size, _address);
nexpaq 0:6c56fb4bc5f0 466 } else if (M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ != rcv_size) {
nexpaq 0:6c56fb4bc5f0 467 _observer.socket_error(M2MConnectionHandler::SOCKET_READ_ERROR, true);
nexpaq 0:6c56fb4bc5f0 468 close_socket();
nexpaq 0:6c56fb4bc5f0 469 return;
nexpaq 0:6c56fb4bc5f0 470 }
nexpaq 0:6c56fb4bc5f0 471 }else{
nexpaq 0:6c56fb4bc5f0 472 int recv = -1;
nexpaq 0:6c56fb4bc5f0 473 if(is_tcp_connection()){
nexpaq 0:6c56fb4bc5f0 474 recv = ((TCPSocket*)_socket)->recv(_recv_buffer, receive_length);
nexpaq 0:6c56fb4bc5f0 475
nexpaq 0:6c56fb4bc5f0 476 }else{
nexpaq 0:6c56fb4bc5f0 477 recv = ((UDPSocket*)_socket)->recvfrom(NULL,_recv_buffer, receive_length);
nexpaq 0:6c56fb4bc5f0 478 }
nexpaq 0:6c56fb4bc5f0 479 if (recv > 0) {
nexpaq 0:6c56fb4bc5f0 480 // Send data for processing.
nexpaq 0:6c56fb4bc5f0 481 if(is_tcp_connection()){
nexpaq 0:6c56fb4bc5f0 482 //We need to "shim" out the length from the front
nexpaq 0:6c56fb4bc5f0 483 if( receive_length > 4 ){
nexpaq 0:6c56fb4bc5f0 484 uint64_t len = (_recv_buffer[0] << 24 & 0xFF000000) + (_recv_buffer[1] << 16 & 0xFF0000);
nexpaq 0:6c56fb4bc5f0 485 len += (_recv_buffer[2] << 8 & 0xFF00) + (_recv_buffer[3] & 0xFF);
nexpaq 0:6c56fb4bc5f0 486 if(len > 0) {
nexpaq 0:6c56fb4bc5f0 487 uint8_t* buf = (uint8_t*)malloc(len);
nexpaq 0:6c56fb4bc5f0 488 if(buf) {
nexpaq 0:6c56fb4bc5f0 489 memmove(buf, _recv_buffer+4, len);
nexpaq 0:6c56fb4bc5f0 490 // Observer for TCP plain mode
nexpaq 0:6c56fb4bc5f0 491 _observer.data_available(buf,len,_address);
nexpaq 0:6c56fb4bc5f0 492 free(buf);
nexpaq 0:6c56fb4bc5f0 493 }
nexpaq 0:6c56fb4bc5f0 494 }
nexpaq 0:6c56fb4bc5f0 495 }else{
nexpaq 0:6c56fb4bc5f0 496 _observer.socket_error(M2MConnectionHandler::SOCKET_READ_ERROR, true);
nexpaq 0:6c56fb4bc5f0 497 close_socket();
nexpaq 0:6c56fb4bc5f0 498 }
nexpaq 0:6c56fb4bc5f0 499 } else { // Observer for UDP plain mode
nexpaq 0:6c56fb4bc5f0 500 tr_debug("M2MConnectionHandlerPimpl::receive_handler - data received %d", recv);
nexpaq 0:6c56fb4bc5f0 501 _observer.data_available((uint8_t*)_recv_buffer,
nexpaq 0:6c56fb4bc5f0 502 recv, _address);
nexpaq 0:6c56fb4bc5f0 503 }
nexpaq 0:6c56fb4bc5f0 504 } else if(NSAPI_ERROR_WOULD_BLOCK != recv) {
nexpaq 0:6c56fb4bc5f0 505 // Socket error in receiving
nexpaq 0:6c56fb4bc5f0 506 _observer.socket_error(M2MConnectionHandler::SOCKET_READ_ERROR, true);
nexpaq 0:6c56fb4bc5f0 507 close_socket();
nexpaq 0:6c56fb4bc5f0 508 }
nexpaq 0:6c56fb4bc5f0 509 }
nexpaq 0:6c56fb4bc5f0 510 }
nexpaq 0:6c56fb4bc5f0 511 }
nexpaq 0:6c56fb4bc5f0 512
nexpaq 0:6c56fb4bc5f0 513 void M2MConnectionHandlerPimpl::claim_mutex()
nexpaq 0:6c56fb4bc5f0 514 {
nexpaq 0:6c56fb4bc5f0 515 eventOS_scheduler_mutex_wait();
nexpaq 0:6c56fb4bc5f0 516 }
nexpaq 0:6c56fb4bc5f0 517
nexpaq 0:6c56fb4bc5f0 518 void M2MConnectionHandlerPimpl::release_mutex()
nexpaq 0:6c56fb4bc5f0 519 {
nexpaq 0:6c56fb4bc5f0 520 eventOS_scheduler_mutex_release();
nexpaq 0:6c56fb4bc5f0 521 }
nexpaq 0:6c56fb4bc5f0 522
nexpaq 0:6c56fb4bc5f0 523 void M2MConnectionHandlerPimpl::init_socket()
nexpaq 0:6c56fb4bc5f0 524 {
nexpaq 0:6c56fb4bc5f0 525 tr_debug("M2MConnectionHandlerPimpl::init_socket - IN");
nexpaq 0:6c56fb4bc5f0 526 _is_handshaking = false;
nexpaq 0:6c56fb4bc5f0 527 _running = true;
nexpaq 0:6c56fb4bc5f0 528
nexpaq 0:6c56fb4bc5f0 529 if(is_tcp_connection()) {
nexpaq 0:6c56fb4bc5f0 530 tr_debug("M2MConnectionHandlerPimpl::init_socket - Using TCP");
nexpaq 0:6c56fb4bc5f0 531 _socket = new TCPSocket(_net_iface);
nexpaq 0:6c56fb4bc5f0 532 if(_socket) {
nexpaq 0:6c56fb4bc5f0 533 _socket->attach(this, &M2MConnectionHandlerPimpl::socket_event);
nexpaq 0:6c56fb4bc5f0 534 } else {
nexpaq 0:6c56fb4bc5f0 535 _observer.socket_error(M2MConnectionHandler::SOCKET_ABORT);
nexpaq 0:6c56fb4bc5f0 536 return;
nexpaq 0:6c56fb4bc5f0 537 }
nexpaq 0:6c56fb4bc5f0 538 } else {
nexpaq 0:6c56fb4bc5f0 539 tr_debug("M2MConnectionHandlerPimpl::init_socket - Using UDP - port %d", _listen_port);
nexpaq 0:6c56fb4bc5f0 540 _socket = new UDPSocket(_net_iface);
nexpaq 0:6c56fb4bc5f0 541 if(_socket) {
nexpaq 0:6c56fb4bc5f0 542 _socket->bind(_listen_port);
nexpaq 0:6c56fb4bc5f0 543 _socket->attach(this, &M2MConnectionHandlerPimpl::socket_event);
nexpaq 0:6c56fb4bc5f0 544 } else {
nexpaq 0:6c56fb4bc5f0 545 _observer.socket_error(M2MConnectionHandler::SOCKET_ABORT);
nexpaq 0:6c56fb4bc5f0 546 return;
nexpaq 0:6c56fb4bc5f0 547 }
nexpaq 0:6c56fb4bc5f0 548 }
nexpaq 0:6c56fb4bc5f0 549 _socket->set_blocking(false);
nexpaq 0:6c56fb4bc5f0 550 tr_debug("M2MConnectionHandlerPimpl::init_socket - OUT");
nexpaq 0:6c56fb4bc5f0 551 }
nexpaq 0:6c56fb4bc5f0 552
nexpaq 0:6c56fb4bc5f0 553 bool M2MConnectionHandlerPimpl::is_tcp_connection()
nexpaq 0:6c56fb4bc5f0 554 {
nexpaq 0:6c56fb4bc5f0 555 return _binding_mode == M2MInterface::TCP ||
nexpaq 0:6c56fb4bc5f0 556 _binding_mode == M2MInterface::TCP_QUEUE ? true : false;
nexpaq 0:6c56fb4bc5f0 557 }
nexpaq 0:6c56fb4bc5f0 558
nexpaq 0:6c56fb4bc5f0 559 void M2MConnectionHandlerPimpl::close_socket()
nexpaq 0:6c56fb4bc5f0 560 {
nexpaq 0:6c56fb4bc5f0 561 tr_debug("M2MConnectionHandlerPimpl::close_socket() - IN");
nexpaq 0:6c56fb4bc5f0 562 if(_socket) {
nexpaq 0:6c56fb4bc5f0 563 _running = false;
nexpaq 0:6c56fb4bc5f0 564 _socket->close();
nexpaq 0:6c56fb4bc5f0 565 delete _socket;
nexpaq 0:6c56fb4bc5f0 566 _socket = NULL;
nexpaq 0:6c56fb4bc5f0 567 }
nexpaq 0:6c56fb4bc5f0 568 tr_debug("M2MConnectionHandlerPimpl::close_socket() - OUT");
nexpaq 0:6c56fb4bc5f0 569 }
nexpaq 0:6c56fb4bc5f0 570
nexpaq 0:6c56fb4bc5f0 571 void M2MConnectionHandlerPimpl::enable_keepalive()
nexpaq 0:6c56fb4bc5f0 572 {
nexpaq 0:6c56fb4bc5f0 573 #if MBED_CLIENT_TCP_KEEPALIVE_TIME
nexpaq 0:6c56fb4bc5f0 574 if(is_tcp_connection()) {
nexpaq 0:6c56fb4bc5f0 575 int keepalive = MBED_CLIENT_TCP_KEEPALIVE_TIME;
nexpaq 0:6c56fb4bc5f0 576 int enable = 1;
nexpaq 0:6c56fb4bc5f0 577 tr_debug("M2MConnectionHandlerPimpl::resolve_hostname - keepalive %d s\n", keepalive);
nexpaq 0:6c56fb4bc5f0 578 if(_socket->setsockopt(1,NSAPI_KEEPALIVE,&enable,sizeof(enable)) != 0) {
nexpaq 0:6c56fb4bc5f0 579 tr_error("M2MConnectionHandlerPimpl::enable_keepalive - setsockopt fail to Set Keepalive\n");
nexpaq 0:6c56fb4bc5f0 580 }
nexpaq 0:6c56fb4bc5f0 581 if(_socket->setsockopt(1,NSAPI_KEEPINTVL,&keepalive,sizeof(keepalive)) != 0) {
nexpaq 0:6c56fb4bc5f0 582 tr_error("M2MConnectionHandlerPimpl::enable_keepalive - setsockopt fail to Set Keepalive TimeInterval\n");
nexpaq 0:6c56fb4bc5f0 583 }
nexpaq 0:6c56fb4bc5f0 584 if(_socket->setsockopt(1,NSAPI_KEEPIDLE,&keepalive,sizeof(keepalive)) != 0) {
nexpaq 0:6c56fb4bc5f0 585 tr_error("M2MConnectionHandlerPimpl::enable_keepalive - setsockopt fail to Set Keepalive Time\n");
nexpaq 0:6c56fb4bc5f0 586 }
nexpaq 0:6c56fb4bc5f0 587 }
nexpaq 0:6c56fb4bc5f0 588 #endif
nexpaq 0:6c56fb4bc5f0 589 }