Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-client by
source/m2minterfaceimpl.cpp@4:ae5178938864, 2016-04-02 (annotated)
- Committer:
- Yogesh Pande
- Date:
- Sat Apr 02 00:31:13 2016 +0300
- Revision:
- 4:ae5178938864
- Parent:
- 1:79b6cc67d8b4
- Child:
- 5:e36098b177a4
Latest updated sources from Github and some trace group refactor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Christopher Haster |
1:79b6cc67d8b4 | 1 | /* |
Christopher Haster |
1:79b6cc67d8b4 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
Christopher Haster |
1:79b6cc67d8b4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
Christopher Haster |
1:79b6cc67d8b4 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
Christopher Haster |
1:79b6cc67d8b4 | 5 | * not use this file except in compliance with the License. |
Christopher Haster |
1:79b6cc67d8b4 | 6 | * You may obtain a copy of the License at |
Christopher Haster |
1:79b6cc67d8b4 | 7 | * |
Christopher Haster |
1:79b6cc67d8b4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Haster |
1:79b6cc67d8b4 | 9 | * |
Christopher Haster |
1:79b6cc67d8b4 | 10 | * Unless required by applicable law or agreed to in writing, software |
Christopher Haster |
1:79b6cc67d8b4 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
Christopher Haster |
1:79b6cc67d8b4 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Christopher Haster |
1:79b6cc67d8b4 | 13 | * See the License for the specific language governing permissions and |
Christopher Haster |
1:79b6cc67d8b4 | 14 | * limitations under the License. |
Christopher Haster |
1:79b6cc67d8b4 | 15 | */ |
Christopher Haster |
1:79b6cc67d8b4 | 16 | #include <assert.h> |
Christopher Haster |
1:79b6cc67d8b4 | 17 | #include "include/m2minterfaceimpl.h" |
Christopher Haster |
1:79b6cc67d8b4 | 18 | #include "include/eventdata.h" |
Christopher Haster |
1:79b6cc67d8b4 | 19 | #include "mbed-client/m2minterfaceobserver.h" |
Christopher Haster |
1:79b6cc67d8b4 | 20 | #include "mbed-client/m2mconnectionhandler.h" |
Christopher Haster |
1:79b6cc67d8b4 | 21 | #include "mbed-client/m2mconnectionsecurity.h" |
Christopher Haster |
1:79b6cc67d8b4 | 22 | #include "include/m2mnsdlinterface.h" |
Christopher Haster |
1:79b6cc67d8b4 | 23 | #include "mbed-client/m2msecurity.h" |
Christopher Haster |
1:79b6cc67d8b4 | 24 | #include "mbed-client/m2mconstants.h" |
Christopher Haster |
1:79b6cc67d8b4 | 25 | #include "mbed-client/m2mtimer.h" |
Yogesh Pande |
4:ae5178938864 | 26 | #include "mbed-trace/mbed_trace.h" |
Yogesh Pande |
4:ae5178938864 | 27 | |
Yogesh Pande |
4:ae5178938864 | 28 | #define TRACE_GROUP "mClt" |
Christopher Haster |
1:79b6cc67d8b4 | 29 | |
Christopher Haster |
1:79b6cc67d8b4 | 30 | M2MInterfaceImpl::M2MInterfaceImpl(M2MInterfaceObserver& observer, |
Christopher Haster |
1:79b6cc67d8b4 | 31 | const String &ep_name, |
Christopher Haster |
1:79b6cc67d8b4 | 32 | const String &ep_type, |
Christopher Haster |
1:79b6cc67d8b4 | 33 | const int32_t l_time, |
Christopher Haster |
1:79b6cc67d8b4 | 34 | const uint16_t listen_port, |
Christopher Haster |
1:79b6cc67d8b4 | 35 | const String &dmn, |
Christopher Haster |
1:79b6cc67d8b4 | 36 | M2MInterface::BindingMode mode, |
Christopher Haster |
1:79b6cc67d8b4 | 37 | M2MInterface::NetworkStack stack, |
Christopher Haster |
1:79b6cc67d8b4 | 38 | const String &con_addr) |
Christopher Haster |
1:79b6cc67d8b4 | 39 | : _observer(observer), |
Christopher Haster |
1:79b6cc67d8b4 | 40 | _nsdl_interface(new M2MNsdlInterface(*this)), |
Christopher Haster |
1:79b6cc67d8b4 | 41 | _current_state(0), |
Christopher Haster |
1:79b6cc67d8b4 | 42 | _max_states( STATE_MAX_STATES ), |
Christopher Haster |
1:79b6cc67d8b4 | 43 | _event_generated(false), |
Christopher Haster |
1:79b6cc67d8b4 | 44 | _event_data(NULL), |
Christopher Haster |
1:79b6cc67d8b4 | 45 | _endpoint_name(ep_name), |
Christopher Haster |
1:79b6cc67d8b4 | 46 | _endpoint_type(ep_type), |
Christopher Haster |
1:79b6cc67d8b4 | 47 | _domain( dmn), |
Christopher Haster |
1:79b6cc67d8b4 | 48 | _life_time(l_time), |
Christopher Haster |
1:79b6cc67d8b4 | 49 | _binding_mode(mode), |
Christopher Haster |
1:79b6cc67d8b4 | 50 | _context_address(con_addr), |
Christopher Haster |
1:79b6cc67d8b4 | 51 | _listen_port(listen_port), |
Christopher Haster |
1:79b6cc67d8b4 | 52 | _register_server(NULL), |
Christopher Haster |
1:79b6cc67d8b4 | 53 | _event_ignored(false), |
Christopher Haster |
1:79b6cc67d8b4 | 54 | _register_ongoing(false), |
Christopher Haster |
1:79b6cc67d8b4 | 55 | _update_register_ongoing(false), |
Christopher Haster |
1:79b6cc67d8b4 | 56 | _queue_sleep_timer(new M2MTimer(*this)), |
Christopher Haster |
1:79b6cc67d8b4 | 57 | _callback_handler(NULL) |
Christopher Haster |
1:79b6cc67d8b4 | 58 | { |
Christopher Haster |
1:79b6cc67d8b4 | 59 | M2MConnectionSecurity::SecurityMode sec_mode = M2MConnectionSecurity::DTLS; |
Christopher Haster |
1:79b6cc67d8b4 | 60 | //Hack for now |
Christopher Haster |
1:79b6cc67d8b4 | 61 | if( _binding_mode == M2MInterface::TCP ){ |
Christopher Haster |
1:79b6cc67d8b4 | 62 | _binding_mode = M2MInterface::UDP; |
Christopher Haster |
1:79b6cc67d8b4 | 63 | sec_mode = M2MConnectionSecurity::TLS; |
Christopher Haster |
1:79b6cc67d8b4 | 64 | }else if( _binding_mode == M2MInterface::TCP_QUEUE ){ |
Christopher Haster |
1:79b6cc67d8b4 | 65 | _binding_mode = M2MInterface::UDP_QUEUE; |
Christopher Haster |
1:79b6cc67d8b4 | 66 | sec_mode = M2MConnectionSecurity::TLS; |
Christopher Haster |
1:79b6cc67d8b4 | 67 | } |
Christopher Haster |
1:79b6cc67d8b4 | 68 | tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -IN"); |
Christopher Haster |
1:79b6cc67d8b4 | 69 | _nsdl_interface->create_endpoint(_endpoint_name, |
Christopher Haster |
1:79b6cc67d8b4 | 70 | _endpoint_type, |
Christopher Haster |
1:79b6cc67d8b4 | 71 | _life_time, |
Christopher Haster |
1:79b6cc67d8b4 | 72 | _domain, |
Christopher Haster |
1:79b6cc67d8b4 | 73 | (uint8_t)_binding_mode, |
Christopher Haster |
1:79b6cc67d8b4 | 74 | _context_address); |
Christopher Haster |
1:79b6cc67d8b4 | 75 | |
Christopher Haster |
1:79b6cc67d8b4 | 76 | //Here we must use TCP still |
Christopher Haster |
1:79b6cc67d8b4 | 77 | _connection_handler = new M2MConnectionHandler(*this, new M2MConnectionSecurity(sec_mode), mode, stack); |
Christopher Haster |
1:79b6cc67d8b4 | 78 | |
Christopher Haster |
1:79b6cc67d8b4 | 79 | _connection_handler->bind_connection(_listen_port); |
Christopher Haster |
1:79b6cc67d8b4 | 80 | tr_debug("M2MInterfaceImpl::M2MInterfaceImpl() -OUT"); |
Christopher Haster |
1:79b6cc67d8b4 | 81 | } |
Christopher Haster |
1:79b6cc67d8b4 | 82 | |
Christopher Haster |
1:79b6cc67d8b4 | 83 | |
Christopher Haster |
1:79b6cc67d8b4 | 84 | M2MInterfaceImpl::~M2MInterfaceImpl() |
Christopher Haster |
1:79b6cc67d8b4 | 85 | { |
Christopher Haster |
1:79b6cc67d8b4 | 86 | tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - IN"); |
Christopher Haster |
1:79b6cc67d8b4 | 87 | delete _queue_sleep_timer; |
Christopher Haster |
1:79b6cc67d8b4 | 88 | delete _nsdl_interface; |
Christopher Haster |
1:79b6cc67d8b4 | 89 | _connection_handler->stop_listening(); |
Christopher Haster |
1:79b6cc67d8b4 | 90 | delete _connection_handler; |
Christopher Haster |
1:79b6cc67d8b4 | 91 | tr_debug("M2MInterfaceImpl::~M2MInterfaceImpl() - OUT"); |
Christopher Haster |
1:79b6cc67d8b4 | 92 | } |
Christopher Haster |
1:79b6cc67d8b4 | 93 | |
Yogesh Pande |
4:ae5178938864 | 94 | |
Christopher Haster |
1:79b6cc67d8b4 | 95 | void M2MInterfaceImpl::bootstrap(M2MSecurity *security) |
Christopher Haster |
1:79b6cc67d8b4 | 96 | { |
Yogesh Pande |
4:ae5178938864 | 97 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 98 | tr_debug("M2MInterfaceImpl::bootstrap(M2MSecurity *security) - IN"); |
Christopher Haster |
1:79b6cc67d8b4 | 99 | // Transition to a new state based upon |
Christopher Haster |
1:79b6cc67d8b4 | 100 | // the current state of the state machine |
Christopher Haster |
1:79b6cc67d8b4 | 101 | M2MSecurityData* data = new M2MSecurityData(); |
Christopher Haster |
1:79b6cc67d8b4 | 102 | data->_object = security; |
Christopher Haster |
1:79b6cc67d8b4 | 103 | BEGIN_TRANSITION_MAP // - Current State - |
Christopher Haster |
1:79b6cc67d8b4 | 104 | TRANSITION_MAP_ENTRY (STATE_BOOTSTRAP) // state_idle |
Christopher Haster |
1:79b6cc67d8b4 | 105 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap |
Christopher Haster |
1:79b6cc67d8b4 | 106 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 107 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 108 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrapped |
Christopher Haster |
1:79b6cc67d8b4 | 109 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register |
Christopher Haster |
1:79b6cc67d8b4 | 110 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 111 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 112 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_registered |
Christopher Haster |
1:79b6cc67d8b4 | 113 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration |
Christopher Haster |
1:79b6cc67d8b4 | 114 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister |
Christopher Haster |
1:79b6cc67d8b4 | 115 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered |
Christopher Haster |
1:79b6cc67d8b4 | 116 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 117 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent |
Christopher Haster |
1:79b6cc67d8b4 | 118 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received |
Christopher Haster |
1:79b6cc67d8b4 | 119 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 120 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed |
Christopher Haster |
1:79b6cc67d8b4 | 121 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_waiting |
Christopher Haster |
1:79b6cc67d8b4 | 122 | END_TRANSITION_MAP(data) |
Christopher Haster |
1:79b6cc67d8b4 | 123 | if(_event_ignored) { |
Christopher Haster |
1:79b6cc67d8b4 | 124 | _event_ignored = false; |
Christopher Haster |
1:79b6cc67d8b4 | 125 | _observer.error(M2MInterface::NotAllowed); |
Christopher Haster |
1:79b6cc67d8b4 | 126 | } |
Christopher Haster |
1:79b6cc67d8b4 | 127 | tr_debug("M2MInterfaceImpl::bootstrap(M2MSecurity *security) - OUT"); |
Yogesh Pande |
4:ae5178938864 | 128 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 129 | } |
Christopher Haster |
1:79b6cc67d8b4 | 130 | |
Christopher Haster |
1:79b6cc67d8b4 | 131 | void M2MInterfaceImpl::cancel_bootstrap() |
Christopher Haster |
1:79b6cc67d8b4 | 132 | { |
Yogesh Pande |
4:ae5178938864 | 133 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 134 | //TODO: Do we need this ? |
Yogesh Pande |
4:ae5178938864 | 135 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 136 | } |
Christopher Haster |
1:79b6cc67d8b4 | 137 | |
Christopher Haster |
1:79b6cc67d8b4 | 138 | void M2MInterfaceImpl::register_object(M2MSecurity *security, const M2MObjectList &object_list) |
Christopher Haster |
1:79b6cc67d8b4 | 139 | { |
Christopher Haster |
1:79b6cc67d8b4 | 140 | tr_debug("M2MInterfaceImpl::register_object(M2MSecurity *security,const M2MObjectList &object_list) - IN"); |
Christopher Haster |
1:79b6cc67d8b4 | 141 | // Transition to a new state based upon |
Christopher Haster |
1:79b6cc67d8b4 | 142 | // the current state of the state machine |
Christopher Haster |
1:79b6cc67d8b4 | 143 | //TODO: manage register object in a list. |
Christopher Haster |
1:79b6cc67d8b4 | 144 | if(!_register_ongoing) { |
Christopher Haster |
1:79b6cc67d8b4 | 145 | _register_ongoing = true; |
Christopher Haster |
1:79b6cc67d8b4 | 146 | _register_server = security; |
Christopher Haster |
1:79b6cc67d8b4 | 147 | M2MRegisterData *data = new M2MRegisterData(); |
Christopher Haster |
1:79b6cc67d8b4 | 148 | data->_object = security; |
Christopher Haster |
1:79b6cc67d8b4 | 149 | data->_object_list = object_list; |
Christopher Haster |
1:79b6cc67d8b4 | 150 | BEGIN_TRANSITION_MAP // - Current State - |
Christopher Haster |
1:79b6cc67d8b4 | 151 | TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_idle |
Christopher Haster |
1:79b6cc67d8b4 | 152 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap |
Christopher Haster |
1:79b6cc67d8b4 | 153 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 154 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 155 | TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_bootstrapped |
Christopher Haster |
1:79b6cc67d8b4 | 156 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register |
Christopher Haster |
1:79b6cc67d8b4 | 157 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 158 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 159 | TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_registered |
Christopher Haster |
1:79b6cc67d8b4 | 160 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration |
Christopher Haster |
1:79b6cc67d8b4 | 161 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister |
Christopher Haster |
1:79b6cc67d8b4 | 162 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered |
Christopher Haster |
1:79b6cc67d8b4 | 163 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 164 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent |
Christopher Haster |
1:79b6cc67d8b4 | 165 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received |
Christopher Haster |
1:79b6cc67d8b4 | 166 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 167 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed |
Christopher Haster |
1:79b6cc67d8b4 | 168 | TRANSITION_MAP_ENTRY (STATE_REGISTER) // state_waiting |
Christopher Haster |
1:79b6cc67d8b4 | 169 | END_TRANSITION_MAP(data) |
Christopher Haster |
1:79b6cc67d8b4 | 170 | if(_event_ignored) { |
Christopher Haster |
1:79b6cc67d8b4 | 171 | _event_ignored = false; |
Christopher Haster |
1:79b6cc67d8b4 | 172 | _observer.error(M2MInterface::NotAllowed); |
Christopher Haster |
1:79b6cc67d8b4 | 173 | } |
Christopher Haster |
1:79b6cc67d8b4 | 174 | } else { |
Christopher Haster |
1:79b6cc67d8b4 | 175 | tr_debug("M2MInterfaceImpl::register_object(M2MSecurity *security,const M2MObjectList &object_list) - NOT ALLOWED"); |
Christopher Haster |
1:79b6cc67d8b4 | 176 | _observer.error(M2MInterface::NotAllowed); |
Christopher Haster |
1:79b6cc67d8b4 | 177 | } |
Christopher Haster |
1:79b6cc67d8b4 | 178 | tr_debug("M2MInterfaceImpl::register_object(M2MSecurity *security,const M2MObjectList &object_list) - OUT"); |
Christopher Haster |
1:79b6cc67d8b4 | 179 | } |
Christopher Haster |
1:79b6cc67d8b4 | 180 | |
Christopher Haster |
1:79b6cc67d8b4 | 181 | void M2MInterfaceImpl::update_registration(M2MSecurity *security_object, const uint32_t lifetime) |
Christopher Haster |
1:79b6cc67d8b4 | 182 | { |
Christopher Haster |
1:79b6cc67d8b4 | 183 | tr_debug("M2MInterfaceImpl::update_registration(M2MSecurity *security,const uint32_t lifetime) - IN"); |
Christopher Haster |
1:79b6cc67d8b4 | 184 | // Transition to a new state based upon |
Christopher Haster |
1:79b6cc67d8b4 | 185 | // the current state of the state machine |
Christopher Haster |
1:79b6cc67d8b4 | 186 | if(lifetime != 0 && (lifetime < MINIMUM_REGISTRATION_TIME)) { |
Christopher Haster |
1:79b6cc67d8b4 | 187 | _observer.error(M2MInterface::InvalidParameters); |
Christopher Haster |
1:79b6cc67d8b4 | 188 | } else if(!_update_register_ongoing){ |
Christopher Haster |
1:79b6cc67d8b4 | 189 | _update_register_ongoing = true; |
Christopher Haster |
1:79b6cc67d8b4 | 190 | M2MUpdateRegisterData *data = new M2MUpdateRegisterData(); |
Christopher Haster |
1:79b6cc67d8b4 | 191 | data->_object = security_object; |
Christopher Haster |
1:79b6cc67d8b4 | 192 | data->_lifetime = lifetime; |
Christopher Haster |
1:79b6cc67d8b4 | 193 | BEGIN_TRANSITION_MAP // - Current State - |
Christopher Haster |
1:79b6cc67d8b4 | 194 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_idle |
Christopher Haster |
1:79b6cc67d8b4 | 195 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap |
Christopher Haster |
1:79b6cc67d8b4 | 196 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 197 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 198 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrapped |
Christopher Haster |
1:79b6cc67d8b4 | 199 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register |
Christopher Haster |
1:79b6cc67d8b4 | 200 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 201 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 202 | TRANSITION_MAP_ENTRY (STATE_UPDATE_REGISTRATION) // state_registered |
Christopher Haster |
1:79b6cc67d8b4 | 203 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_update_registration |
Christopher Haster |
1:79b6cc67d8b4 | 204 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister |
Christopher Haster |
1:79b6cc67d8b4 | 205 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered |
Christopher Haster |
1:79b6cc67d8b4 | 206 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 207 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent |
Christopher Haster |
1:79b6cc67d8b4 | 208 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received |
Christopher Haster |
1:79b6cc67d8b4 | 209 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 210 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed |
Christopher Haster |
1:79b6cc67d8b4 | 211 | TRANSITION_MAP_ENTRY (STATE_UPDATE_REGISTRATION) // state_waiting |
Christopher Haster |
1:79b6cc67d8b4 | 212 | END_TRANSITION_MAP(data) |
Christopher Haster |
1:79b6cc67d8b4 | 213 | if(_event_ignored) { |
Christopher Haster |
1:79b6cc67d8b4 | 214 | _event_ignored = false; |
Christopher Haster |
1:79b6cc67d8b4 | 215 | _observer.error(M2MInterface::NotAllowed); |
Christopher Haster |
1:79b6cc67d8b4 | 216 | } |
Christopher Haster |
1:79b6cc67d8b4 | 217 | } else { |
Christopher Haster |
1:79b6cc67d8b4 | 218 | tr_debug("M2MInterfaceImpl::update_registration(M2MSecurity *security,const M2MObjectList &object_list) - NOT ALLOWED"); |
Christopher Haster |
1:79b6cc67d8b4 | 219 | _observer.error(M2MInterface::NotAllowed); |
Christopher Haster |
1:79b6cc67d8b4 | 220 | } |
Christopher Haster |
1:79b6cc67d8b4 | 221 | tr_debug("M2MInterfaceImpl::update_registration(M2MSecurity *security,const uint32_t lifetime) - OUT"); |
Christopher Haster |
1:79b6cc67d8b4 | 222 | } |
Christopher Haster |
1:79b6cc67d8b4 | 223 | |
Christopher Haster |
1:79b6cc67d8b4 | 224 | void M2MInterfaceImpl::unregister_object(M2MSecurity* /*security*/) |
Christopher Haster |
1:79b6cc67d8b4 | 225 | { |
Christopher Haster |
1:79b6cc67d8b4 | 226 | tr_debug("M2MInterfaceImpl::unregister_object(M2MSecurity *security) - IN"); |
Christopher Haster |
1:79b6cc67d8b4 | 227 | // Transition to a new state based upon |
Christopher Haster |
1:79b6cc67d8b4 | 228 | // the current state of the state machine |
Christopher Haster |
1:79b6cc67d8b4 | 229 | BEGIN_TRANSITION_MAP // - Current State - |
Christopher Haster |
1:79b6cc67d8b4 | 230 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_idle |
Christopher Haster |
1:79b6cc67d8b4 | 231 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap |
Christopher Haster |
1:79b6cc67d8b4 | 232 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state__bootstrap_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 233 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrap_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 234 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_bootstrapped |
Christopher Haster |
1:79b6cc67d8b4 | 235 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register |
Christopher Haster |
1:79b6cc67d8b4 | 236 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_address_resolved |
Christopher Haster |
1:79b6cc67d8b4 | 237 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_register_resource_created |
Christopher Haster |
1:79b6cc67d8b4 | 238 | TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_registered |
Christopher Haster |
1:79b6cc67d8b4 | 239 | TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_update_registration |
Christopher Haster |
1:79b6cc67d8b4 | 240 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregister |
Christopher Haster |
1:79b6cc67d8b4 | 241 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_unregistered |
Christopher Haster |
1:79b6cc67d8b4 | 242 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_sending_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 243 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_sent |
Christopher Haster |
1:79b6cc67d8b4 | 244 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_received |
Christopher Haster |
1:79b6cc67d8b4 | 245 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_processing_coap_data |
Christopher Haster |
1:79b6cc67d8b4 | 246 | TRANSITION_MAP_ENTRY (EVENT_IGNORED) // state_coap_data_processed |
Christopher Haster |
1:79b6cc67d8b4 | 247 | TRANSITION_MAP_ENTRY (STATE_UNREGISTER) // state_waiting |
Christopher Haster |
1:79b6cc67d8b4 | 248 | END_TRANSITION_MAP(NULL) |
Christopher Haster |
1:79b6cc67d8b4 | 249 | if(_event_ignored) { |
Christopher Haster |
1:79b6cc67d8b4 | 250 | _event_ignored = false; |
Christopher Haster |
1:79b6cc67d8b4 | 251 | _observer.error(M2MInterface::NotAllowed); |
Christopher Haster |
1:79b6cc67d8b4 | 252 | } |
Christopher Haster |
1:79b6cc67d8b4 | 253 | tr_debug("M2MInterfaceImpl::unregister_object(M2MSecurity *security) - OUT"); |
Christopher Haster |
1:79b6cc67d8b4 | 254 | } |
Christopher Haster |
1:79b6cc67d8b4 | 255 | |
Christopher Haster |
1:79b6cc67d8b4 | 256 | void M2MInterfaceImpl::set_queue_sleep_handler(callback_handler handler) |
Christopher Haster |
1:79b6cc67d8b4 | 257 | { |
Christopher Haster |
1:79b6cc67d8b4 | 258 | tr_debug("M2MInterfaceImpl::set_queue_sleep_handler()"); |
Christopher Haster |
1:79b6cc67d8b4 | 259 | _callback_handler = handler; |
Christopher Haster |
1:79b6cc67d8b4 | 260 | } |
Christopher Haster |
1:79b6cc67d8b4 | 261 | |
Christopher Haster |
1:79b6cc67d8b4 | 262 | void M2MInterfaceImpl::coap_message_ready(uint8_t *data_ptr, |
Christopher Haster |
1:79b6cc67d8b4 | 263 | uint16_t data_len, |
Christopher Haster |
1:79b6cc67d8b4 | 264 | sn_nsdl_addr_s *address_ptr) |
Christopher Haster |
1:79b6cc67d8b4 | 265 | { |
Christopher Haster |
1:79b6cc67d8b4 | 266 | tr_debug("M2MInterfaceImpl::coap_message_ready(uint8_t *data_ptr,uint16_t data_len,sn_nsdl_addr_s *address_ptr)"); |
Christopher Haster |
1:79b6cc67d8b4 | 267 | internal_event(STATE_SENDING_COAP_DATA); |
Christopher Haster |
1:79b6cc67d8b4 | 268 | if(!_connection_handler->send_data(data_ptr,data_len,address_ptr)) { |
Christopher Haster |
1:79b6cc67d8b4 | 269 | internal_event( STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 270 | tr_error("M2MInterfaceImpl::coap_message_ready() - M2MInterface::NetworkError"); |
Christopher Haster |
1:79b6cc67d8b4 | 271 | _observer.error(M2MInterface::NetworkError); |
Christopher Haster |
1:79b6cc67d8b4 | 272 | } |
Christopher Haster |
1:79b6cc67d8b4 | 273 | } |
Christopher Haster |
1:79b6cc67d8b4 | 274 | |
Christopher Haster |
1:79b6cc67d8b4 | 275 | void M2MInterfaceImpl::client_registered(M2MServer *server_object) |
Christopher Haster |
1:79b6cc67d8b4 | 276 | { |
Christopher Haster |
1:79b6cc67d8b4 | 277 | tr_debug("M2MInterfaceImpl::client_registered(M2MServer *server_object)"); |
Christopher Haster |
1:79b6cc67d8b4 | 278 | internal_event(STATE_REGISTERED); |
Christopher Haster |
1:79b6cc67d8b4 | 279 | //Inform client is registered. |
Christopher Haster |
1:79b6cc67d8b4 | 280 | //TODO: manage register object in a list. |
Christopher Haster |
1:79b6cc67d8b4 | 281 | _observer.object_registered(_register_server,*server_object); |
Christopher Haster |
1:79b6cc67d8b4 | 282 | } |
Christopher Haster |
1:79b6cc67d8b4 | 283 | |
Christopher Haster |
1:79b6cc67d8b4 | 284 | void M2MInterfaceImpl::registration_updated(const M2MServer &server_object) |
Christopher Haster |
1:79b6cc67d8b4 | 285 | { |
Christopher Haster |
1:79b6cc67d8b4 | 286 | tr_debug("M2MInterfaceImpl::registration_updated(const M2MServer &server_object)"); |
Christopher Haster |
1:79b6cc67d8b4 | 287 | internal_event(STATE_REGISTERED); |
Christopher Haster |
1:79b6cc67d8b4 | 288 | _observer.registration_updated(_register_server,server_object); |
Christopher Haster |
1:79b6cc67d8b4 | 289 | } |
Christopher Haster |
1:79b6cc67d8b4 | 290 | |
Christopher Haster |
1:79b6cc67d8b4 | 291 | |
Christopher Haster |
1:79b6cc67d8b4 | 292 | void M2MInterfaceImpl::registration_error(uint8_t error_code) |
Christopher Haster |
1:79b6cc67d8b4 | 293 | { |
Yogesh Pande |
4:ae5178938864 | 294 | tr_debug("M2MInterfaceImpl::registration_error code [%d]", error_code); |
Christopher Haster |
1:79b6cc67d8b4 | 295 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 296 | _observer.error((M2MInterface::Error)error_code); |
Christopher Haster |
1:79b6cc67d8b4 | 297 | } |
Christopher Haster |
1:79b6cc67d8b4 | 298 | |
Christopher Haster |
1:79b6cc67d8b4 | 299 | void M2MInterfaceImpl::client_unregistered() |
Christopher Haster |
1:79b6cc67d8b4 | 300 | { |
Christopher Haster |
1:79b6cc67d8b4 | 301 | tr_debug("M2MInterfaceImpl::client_unregistered()"); |
Christopher Haster |
1:79b6cc67d8b4 | 302 | internal_event(STATE_UNREGISTERED); |
Christopher Haster |
1:79b6cc67d8b4 | 303 | //TODO: manage register object in a list. |
Christopher Haster |
1:79b6cc67d8b4 | 304 | _observer.object_unregistered(_register_server); |
Christopher Haster |
1:79b6cc67d8b4 | 305 | } |
Christopher Haster |
1:79b6cc67d8b4 | 306 | |
Yogesh Pande |
4:ae5178938864 | 307 | |
Christopher Haster |
1:79b6cc67d8b4 | 308 | void M2MInterfaceImpl::bootstrap_done(M2MSecurity *security_object) |
Christopher Haster |
1:79b6cc67d8b4 | 309 | { |
Yogesh Pande |
4:ae5178938864 | 310 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 311 | tr_debug("M2MInterfaceImpl::bootstrap_done(M2MSecurity *security_object)"); |
Christopher Haster |
1:79b6cc67d8b4 | 312 | internal_event(STATE_BOOTSTRAPPED); |
Christopher Haster |
1:79b6cc67d8b4 | 313 | _observer.bootstrap_done(security_object); |
Yogesh Pande |
4:ae5178938864 | 314 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 315 | } |
Christopher Haster |
1:79b6cc67d8b4 | 316 | |
Christopher Haster |
1:79b6cc67d8b4 | 317 | void M2MInterfaceImpl::bootstrap_error() |
Christopher Haster |
1:79b6cc67d8b4 | 318 | { |
Yogesh Pande |
4:ae5178938864 | 319 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 320 | tr_debug("M2MInterfaceImpl::bootstrap_error()"); |
Christopher Haster |
1:79b6cc67d8b4 | 321 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 322 | _observer.error(M2MInterface::BootstrapFailed); |
Yogesh Pande |
4:ae5178938864 | 323 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 324 | } |
Christopher Haster |
1:79b6cc67d8b4 | 325 | |
Christopher Haster |
1:79b6cc67d8b4 | 326 | void M2MInterfaceImpl::coap_data_processed() |
Christopher Haster |
1:79b6cc67d8b4 | 327 | { |
Christopher Haster |
1:79b6cc67d8b4 | 328 | tr_debug("M2MInterfaceImpl::coap_data_processed()"); |
Christopher Haster |
1:79b6cc67d8b4 | 329 | internal_event(STATE_COAP_DATA_PROCESSED); |
Christopher Haster |
1:79b6cc67d8b4 | 330 | } |
Christopher Haster |
1:79b6cc67d8b4 | 331 | |
Christopher Haster |
1:79b6cc67d8b4 | 332 | void M2MInterfaceImpl::value_updated(M2MBase *base) |
Christopher Haster |
1:79b6cc67d8b4 | 333 | { |
Christopher Haster |
1:79b6cc67d8b4 | 334 | tr_debug("M2MInterfaceImpl::value_updated(M2MBase *base)"); |
Christopher Haster |
1:79b6cc67d8b4 | 335 | if(base) { |
Christopher Haster |
1:79b6cc67d8b4 | 336 | M2MBase::BaseType type = base->base_type(); |
Christopher Haster |
1:79b6cc67d8b4 | 337 | _observer.value_updated(base, type); |
Christopher Haster |
1:79b6cc67d8b4 | 338 | } |
Christopher Haster |
1:79b6cc67d8b4 | 339 | } |
Christopher Haster |
1:79b6cc67d8b4 | 340 | |
Christopher Haster |
1:79b6cc67d8b4 | 341 | void M2MInterfaceImpl::data_available(uint8_t* data, |
Christopher Haster |
1:79b6cc67d8b4 | 342 | uint16_t data_size, |
Christopher Haster |
1:79b6cc67d8b4 | 343 | const M2MConnectionObserver::SocketAddress &address) |
Christopher Haster |
1:79b6cc67d8b4 | 344 | { |
Christopher Haster |
1:79b6cc67d8b4 | 345 | tr_debug("M2MInterfaceImpl::data_available(uint8_t* data,uint16_t data_size,const M2MConnectionObserver::SocketAddress &address)"); |
Christopher Haster |
1:79b6cc67d8b4 | 346 | ReceivedData *event = new ReceivedData(); |
Christopher Haster |
1:79b6cc67d8b4 | 347 | event->_data = data; |
Christopher Haster |
1:79b6cc67d8b4 | 348 | event->_size = data_size; |
Christopher Haster |
1:79b6cc67d8b4 | 349 | event->_address = &address; |
Christopher Haster |
1:79b6cc67d8b4 | 350 | internal_event(STATE_COAP_DATA_RECEIVED, event); |
Christopher Haster |
1:79b6cc67d8b4 | 351 | } |
Christopher Haster |
1:79b6cc67d8b4 | 352 | |
Yogesh Pande |
4:ae5178938864 | 353 | void M2MInterfaceImpl::socket_error(uint8_t error_code) |
Christopher Haster |
1:79b6cc67d8b4 | 354 | { |
Yogesh Pande |
4:ae5178938864 | 355 | tr_debug("M2MInterfaceImpl::socket_error code [%d]", error_code); |
Christopher Haster |
1:79b6cc67d8b4 | 356 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 357 | M2MInterface::Error error = M2MInterface::NetworkError; |
Christopher Haster |
1:79b6cc67d8b4 | 358 | _observer.error(error); |
Christopher Haster |
1:79b6cc67d8b4 | 359 | } |
Christopher Haster |
1:79b6cc67d8b4 | 360 | |
Christopher Haster |
1:79b6cc67d8b4 | 361 | void M2MInterfaceImpl::address_ready(const M2MConnectionObserver::SocketAddress &address, |
Christopher Haster |
1:79b6cc67d8b4 | 362 | M2MConnectionObserver::ServerType server_type, |
Christopher Haster |
1:79b6cc67d8b4 | 363 | const uint16_t server_port) |
Christopher Haster |
1:79b6cc67d8b4 | 364 | { |
Christopher Haster |
1:79b6cc67d8b4 | 365 | tr_debug("M2MInterfaceImpl::address_ready(const M2MConnectionObserver::SocketAddress ,M2MConnectionObserver::ServerType,const uint16_t)"); |
Christopher Haster |
1:79b6cc67d8b4 | 366 | ResolvedAddressData *data = new ResolvedAddressData(); |
Christopher Haster |
1:79b6cc67d8b4 | 367 | data->_address = &address; |
Christopher Haster |
1:79b6cc67d8b4 | 368 | data->_port = server_port; |
Christopher Haster |
1:79b6cc67d8b4 | 369 | if( M2MConnectionObserver::Bootstrap == server_type) { |
Christopher Haster |
1:79b6cc67d8b4 | 370 | tr_debug("M2MInterfaceImpl::address_ready() Server Type Bootstrap"); |
Christopher Haster |
1:79b6cc67d8b4 | 371 | internal_event(STATE_BOOTSTRAP_ADDRESS_RESOLVED, data); |
Christopher Haster |
1:79b6cc67d8b4 | 372 | } else { |
Christopher Haster |
1:79b6cc67d8b4 | 373 | tr_debug("M2MInterfaceImpl::address_ready() Server Type LWM2M"); |
Christopher Haster |
1:79b6cc67d8b4 | 374 | internal_event(STATE_REGISTER_ADDRESS_RESOLVED, data); |
Christopher Haster |
1:79b6cc67d8b4 | 375 | } |
Christopher Haster |
1:79b6cc67d8b4 | 376 | } |
Christopher Haster |
1:79b6cc67d8b4 | 377 | |
Christopher Haster |
1:79b6cc67d8b4 | 378 | void M2MInterfaceImpl::data_sent() |
Christopher Haster |
1:79b6cc67d8b4 | 379 | { |
Christopher Haster |
1:79b6cc67d8b4 | 380 | tr_debug("M2MInterfaceImpl::data_sent()"); |
Christopher Haster |
1:79b6cc67d8b4 | 381 | if(_binding_mode == M2MInterface::UDP_QUEUE || |
Christopher Haster |
1:79b6cc67d8b4 | 382 | _binding_mode == M2MInterface::TCP_QUEUE || |
Christopher Haster |
1:79b6cc67d8b4 | 383 | _binding_mode == M2MInterface::SMS_QUEUE || |
Christopher Haster |
1:79b6cc67d8b4 | 384 | _binding_mode == M2MInterface::UDP_SMS_QUEUE) { |
Christopher Haster |
1:79b6cc67d8b4 | 385 | if(_callback_handler) { |
Christopher Haster |
1:79b6cc67d8b4 | 386 | _queue_sleep_timer->stop_timer(); |
Christopher Haster |
1:79b6cc67d8b4 | 387 | _queue_sleep_timer->start_timer(RETRY_COUNT*RETRY_INTERVAL*1000, M2MTimerObserver::QueueSleep); |
Christopher Haster |
1:79b6cc67d8b4 | 388 | } |
Christopher Haster |
1:79b6cc67d8b4 | 389 | } |
Christopher Haster |
1:79b6cc67d8b4 | 390 | internal_event(STATE_COAP_DATA_SENT); |
Christopher Haster |
1:79b6cc67d8b4 | 391 | } |
Christopher Haster |
1:79b6cc67d8b4 | 392 | |
Christopher Haster |
1:79b6cc67d8b4 | 393 | void M2MInterfaceImpl::timer_expired(M2MTimerObserver::Type type) |
Christopher Haster |
1:79b6cc67d8b4 | 394 | { |
Christopher Haster |
1:79b6cc67d8b4 | 395 | tr_debug("M2MInterfaceImpl::timer_expired()"); |
Christopher Haster |
1:79b6cc67d8b4 | 396 | if(M2MTimerObserver::QueueSleep == type) { |
Christopher Haster |
1:79b6cc67d8b4 | 397 | if(_callback_handler) { |
Christopher Haster |
1:79b6cc67d8b4 | 398 | _callback_handler(); |
Christopher Haster |
1:79b6cc67d8b4 | 399 | } |
Christopher Haster |
1:79b6cc67d8b4 | 400 | _queue_sleep_timer->stop_timer(); |
Christopher Haster |
1:79b6cc67d8b4 | 401 | } |
Christopher Haster |
1:79b6cc67d8b4 | 402 | } |
Christopher Haster |
1:79b6cc67d8b4 | 403 | |
Christopher Haster |
1:79b6cc67d8b4 | 404 | // state machine sits here. |
Christopher Haster |
1:79b6cc67d8b4 | 405 | void M2MInterfaceImpl::state_idle(EventData* /*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 406 | { |
Christopher Haster |
1:79b6cc67d8b4 | 407 | // Handle Idle state here |
Christopher Haster |
1:79b6cc67d8b4 | 408 | // Cleanup all resources, if necessary |
Christopher Haster |
1:79b6cc67d8b4 | 409 | _connection_handler->stop_listening(); |
Christopher Haster |
1:79b6cc67d8b4 | 410 | _nsdl_interface->stop_timers(); |
Christopher Haster |
1:79b6cc67d8b4 | 411 | _register_ongoing = false; |
Christopher Haster |
1:79b6cc67d8b4 | 412 | _update_register_ongoing = false; |
Christopher Haster |
1:79b6cc67d8b4 | 413 | tr_debug("M2MInterfaceImpl::state_idle"); |
Christopher Haster |
1:79b6cc67d8b4 | 414 | } |
Christopher Haster |
1:79b6cc67d8b4 | 415 | |
Yogesh Pande |
4:ae5178938864 | 416 | |
Christopher Haster |
1:79b6cc67d8b4 | 417 | void M2MInterfaceImpl::state_bootstrap( EventData *data) |
Christopher Haster |
1:79b6cc67d8b4 | 418 | { |
Yogesh Pande |
4:ae5178938864 | 419 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 420 | tr_debug("M2MInterfaceImpl::state_bootstrap"); |
Christopher Haster |
1:79b6cc67d8b4 | 421 | // Start with bootstrapping preparation |
Christopher Haster |
1:79b6cc67d8b4 | 422 | bool success = false; |
Christopher Haster |
1:79b6cc67d8b4 | 423 | if(data) { |
Christopher Haster |
1:79b6cc67d8b4 | 424 | M2MSecurityData *event = (M2MSecurityData *)data; |
Christopher Haster |
1:79b6cc67d8b4 | 425 | M2MSecurity *security = event->_object; |
Christopher Haster |
1:79b6cc67d8b4 | 426 | if(security) { |
Christopher Haster |
1:79b6cc67d8b4 | 427 | if(M2MSecurity::Bootstrap == security->server_type()) { |
Christopher Haster |
1:79b6cc67d8b4 | 428 | tr_debug("M2MInterfaceImpl::state_bootstrap - server_type : M2MSecurity::Bootstrap"); |
Christopher Haster |
1:79b6cc67d8b4 | 429 | String server_address = security->resource_value_string(M2MSecurity::M2MServerUri); |
Christopher Haster |
1:79b6cc67d8b4 | 430 | tr_debug("M2MInterfaceImpl::state_bootstrap - server_address %s", server_address.c_str()); |
Christopher Haster |
1:79b6cc67d8b4 | 431 | String ip_address; |
Yogesh Pande |
4:ae5178938864 | 432 | uint16_t port = 0; |
Yogesh Pande |
4:ae5178938864 | 433 | String coap; |
Christopher Haster |
1:79b6cc67d8b4 | 434 | if(server_address.compare(0,COAP.size(),COAP) == 0) { |
Yogesh Pande |
4:ae5178938864 | 435 | coap = COAP; |
Yogesh Pande |
4:ae5178938864 | 436 | } |
Yogesh Pande |
4:ae5178938864 | 437 | else if(server_address.compare(0,COAPS.size(),COAPS) == 0) { |
Yogesh Pande |
4:ae5178938864 | 438 | security->resource_value_int(M2MSecurity::SecurityMode) != M2MSecurity::NoSecurity ? coap = COAPS: coap = ""; |
Yogesh Pande |
4:ae5178938864 | 439 | } |
Yogesh Pande |
4:ae5178938864 | 440 | if(!coap.empty()) { |
Yogesh Pande |
4:ae5178938864 | 441 | server_address = server_address.substr(coap.size(), |
Yogesh Pande |
4:ae5178938864 | 442 | server_address.size()-coap.size()); |
Yogesh Pande |
4:ae5178938864 | 443 | int colonFound = server_address.find_last_of(':'); //10 |
Christopher Haster |
1:79b6cc67d8b4 | 444 | if(colonFound != -1) { |
Yogesh Pande |
4:ae5178938864 | 445 | ip_address = server_address.substr(0,colonFound); |
Yogesh Pande |
4:ae5178938864 | 446 | port = atoi(server_address.substr(colonFound+1, |
Christopher Haster |
1:79b6cc67d8b4 | 447 | server_address.size()-ip_address.size()).c_str()); |
Yogesh Pande |
4:ae5178938864 | 448 | colonFound = ip_address.find_last_of(']'); |
Yogesh Pande |
4:ae5178938864 | 449 | if(ip_address.compare(0,1,"[") == 0) { |
Yogesh Pande |
4:ae5178938864 | 450 | if(colonFound == -1) { |
Yogesh Pande |
4:ae5178938864 | 451 | ip_address.clear(); |
Yogesh Pande |
4:ae5178938864 | 452 | } else { |
Yogesh Pande |
4:ae5178938864 | 453 | ip_address = ip_address.substr(1,colonFound-1); |
Yogesh Pande |
4:ae5178938864 | 454 | } |
Yogesh Pande |
4:ae5178938864 | 455 | } else if(colonFound != -1) { |
Yogesh Pande |
4:ae5178938864 | 456 | ip_address.clear(); |
Yogesh Pande |
4:ae5178938864 | 457 | } |
Christopher Haster |
1:79b6cc67d8b4 | 458 | } |
Yogesh Pande |
4:ae5178938864 | 459 | tr_debug("M2MInterfaceImpl::state_bootstrap - IP address %s , Port %d", ip_address.c_str(), port); |
Yogesh Pande |
4:ae5178938864 | 460 | // If bind and resolving server address succeed then proceed else |
Yogesh Pande |
4:ae5178938864 | 461 | // return error to the application and go to Idle state. |
Yogesh Pande |
4:ae5178938864 | 462 | if(ip_address.empty()) { |
Yogesh Pande |
4:ae5178938864 | 463 | tr_error("M2MInterfaceImpl::state_bootstrap - set error as M2MInterface::InvalidParameters"); |
Yogesh Pande |
4:ae5178938864 | 464 | success = false; |
Yogesh Pande |
4:ae5178938864 | 465 | } else if(_connection_handler->resolve_server_address(ip_address, |
Yogesh Pande |
4:ae5178938864 | 466 | port, |
Yogesh Pande |
4:ae5178938864 | 467 | M2MConnectionObserver::Bootstrap, |
Yogesh Pande |
4:ae5178938864 | 468 | security)) { |
Yogesh Pande |
4:ae5178938864 | 469 | tr_debug("M2MInterfaceImpl::state_bootstrap - resolve_server_address - success"); |
Yogesh Pande |
4:ae5178938864 | 470 | success = true; |
Yogesh Pande |
4:ae5178938864 | 471 | } |
Christopher Haster |
1:79b6cc67d8b4 | 472 | } |
Christopher Haster |
1:79b6cc67d8b4 | 473 | } |
Christopher Haster |
1:79b6cc67d8b4 | 474 | } |
Christopher Haster |
1:79b6cc67d8b4 | 475 | } |
Christopher Haster |
1:79b6cc67d8b4 | 476 | if(!success) { |
Christopher Haster |
1:79b6cc67d8b4 | 477 | tr_error("M2MInterfaceImpl::state_bootstrap - M2MInterface::InvalidParameters"); |
Christopher Haster |
1:79b6cc67d8b4 | 478 | _observer.error(M2MInterface::InvalidParameters); |
Christopher Haster |
1:79b6cc67d8b4 | 479 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 480 | } |
Yogesh Pande |
4:ae5178938864 | 481 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 482 | } |
Christopher Haster |
1:79b6cc67d8b4 | 483 | |
Christopher Haster |
1:79b6cc67d8b4 | 484 | void M2MInterfaceImpl::state_bootstrap_address_resolved( EventData *data) |
Christopher Haster |
1:79b6cc67d8b4 | 485 | { |
Yogesh Pande |
4:ae5178938864 | 486 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 487 | tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved"); |
Christopher Haster |
1:79b6cc67d8b4 | 488 | ResolvedAddressData *event = (ResolvedAddressData *)data; |
Christopher Haster |
1:79b6cc67d8b4 | 489 | sn_nsdl_addr_s address; |
Christopher Haster |
1:79b6cc67d8b4 | 490 | |
Christopher Haster |
1:79b6cc67d8b4 | 491 | M2MInterface::NetworkStack stack = event->_address->_stack; |
Christopher Haster |
1:79b6cc67d8b4 | 492 | |
Christopher Haster |
1:79b6cc67d8b4 | 493 | if(M2MInterface::LwIP_IPv4 == stack) { |
Christopher Haster |
1:79b6cc67d8b4 | 494 | tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : IPv4 address"); |
Christopher Haster |
1:79b6cc67d8b4 | 495 | address.type = SN_NSDL_ADDRESS_TYPE_IPV4; |
Christopher Haster |
1:79b6cc67d8b4 | 496 | address.addr_len = 4; |
Christopher Haster |
1:79b6cc67d8b4 | 497 | } else if((M2MInterface::LwIP_IPv6 == stack) || |
Christopher Haster |
1:79b6cc67d8b4 | 498 | (M2MInterface::Nanostack_IPv6 == stack)) { |
Christopher Haster |
1:79b6cc67d8b4 | 499 | tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : IPv6 address"); |
Christopher Haster |
1:79b6cc67d8b4 | 500 | address.type = SN_NSDL_ADDRESS_TYPE_IPV6; |
Christopher Haster |
1:79b6cc67d8b4 | 501 | address.addr_len = 16; |
Christopher Haster |
1:79b6cc67d8b4 | 502 | } |
Christopher Haster |
1:79b6cc67d8b4 | 503 | address.port = event->_port; |
Christopher Haster |
1:79b6cc67d8b4 | 504 | address.addr_ptr = (uint8_t*)event->_address->_address; |
Christopher Haster |
1:79b6cc67d8b4 | 505 | if(_nsdl_interface->create_bootstrap_resource(&address)) { |
Christopher Haster |
1:79b6cc67d8b4 | 506 | tr_debug("M2MInterfaceImpl::state_bootstrap_address_resolved : create_bootstrap_resource - success"); |
Christopher Haster |
1:79b6cc67d8b4 | 507 | internal_event(STATE_BOOTSTRAP_RESOURCE_CREATED); |
Yogesh Pande |
4:ae5178938864 | 508 | _connection_handler->start_listening_for_data(); |
Christopher Haster |
1:79b6cc67d8b4 | 509 | } else{ |
Christopher Haster |
1:79b6cc67d8b4 | 510 | // If resource creation fails then inform error to application |
Christopher Haster |
1:79b6cc67d8b4 | 511 | tr_error("M2MInterfaceImpl::state_bootstrap_address_resolved : M2MInterface::InvalidParameters"); |
Christopher Haster |
1:79b6cc67d8b4 | 512 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 513 | _observer.error(M2MInterface::InvalidParameters); |
Christopher Haster |
1:79b6cc67d8b4 | 514 | } |
Yogesh Pande |
4:ae5178938864 | 515 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 516 | } |
Christopher Haster |
1:79b6cc67d8b4 | 517 | |
Christopher Haster |
1:79b6cc67d8b4 | 518 | void M2MInterfaceImpl::state_bootstrap_resource_created( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 519 | { |
Yogesh Pande |
4:ae5178938864 | 520 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 521 | tr_debug("M2MInterfaceImpl::state_bootstrap_resource_created"); |
Yogesh Pande |
4:ae5178938864 | 522 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 523 | } |
Christopher Haster |
1:79b6cc67d8b4 | 524 | |
Christopher Haster |
1:79b6cc67d8b4 | 525 | void M2MInterfaceImpl::state_bootstrapped( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 526 | { |
Yogesh Pande |
4:ae5178938864 | 527 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 528 | tr_debug("M2MInterfaceImpl::state_bootstrapped"); |
Yogesh Pande |
4:ae5178938864 | 529 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 530 | } |
Christopher Haster |
1:79b6cc67d8b4 | 531 | |
Christopher Haster |
1:79b6cc67d8b4 | 532 | void M2MInterfaceImpl::state_register( EventData *data) |
Christopher Haster |
1:79b6cc67d8b4 | 533 | { |
Christopher Haster |
1:79b6cc67d8b4 | 534 | tr_debug("M2MInterfaceImpl::state_register"); |
Christopher Haster |
1:79b6cc67d8b4 | 535 | // Start with registration preparation |
Christopher Haster |
1:79b6cc67d8b4 | 536 | bool success = false; |
Christopher Haster |
1:79b6cc67d8b4 | 537 | M2MInterface::Error error = M2MInterface::InvalidParameters; |
Christopher Haster |
1:79b6cc67d8b4 | 538 | if(data) { |
Christopher Haster |
1:79b6cc67d8b4 | 539 | M2MRegisterData *event = (M2MRegisterData *)data; |
Christopher Haster |
1:79b6cc67d8b4 | 540 | M2MSecurity *security = event->_object; |
Christopher Haster |
1:79b6cc67d8b4 | 541 | if(security) { |
Christopher Haster |
1:79b6cc67d8b4 | 542 | if(M2MSecurity::M2MServer == security->server_type()) { |
Christopher Haster |
1:79b6cc67d8b4 | 543 | tr_debug("M2MInterfaceImpl::state_register - server_type : M2MSecurity::M2MServer"); |
Christopher Haster |
1:79b6cc67d8b4 | 544 | if(_nsdl_interface->create_nsdl_list_structure(event->_object_list)) { |
Christopher Haster |
1:79b6cc67d8b4 | 545 | tr_debug("M2MInterfaceImpl::state_register - create_nsdl_list_structure - success"); |
Christopher Haster |
1:79b6cc67d8b4 | 546 | // If the nsdl resource structure is created successfully |
Christopher Haster |
1:79b6cc67d8b4 | 547 | String server_address = security->resource_value_string(M2MSecurity::M2MServerUri); |
Christopher Haster |
1:79b6cc67d8b4 | 548 | tr_debug("M2MInterfaceImpl::state_register - server_address %s", server_address.c_str()); |
Christopher Haster |
1:79b6cc67d8b4 | 549 | String ip_address; |
Yogesh Pande |
4:ae5178938864 | 550 | uint16_t port = 0; |
Yogesh Pande |
4:ae5178938864 | 551 | String coap; |
Christopher Haster |
1:79b6cc67d8b4 | 552 | if(server_address.compare(0,COAP.size(),COAP) == 0) { |
Yogesh Pande |
4:ae5178938864 | 553 | coap = COAP; |
Yogesh Pande |
4:ae5178938864 | 554 | } |
Yogesh Pande |
4:ae5178938864 | 555 | else if(server_address.compare(0,COAPS.size(),COAPS) == 0) { |
Yogesh Pande |
4:ae5178938864 | 556 | security->resource_value_int(M2MSecurity::SecurityMode) != M2MSecurity::NoSecurity ? coap = COAPS: coap = ""; |
Yogesh Pande |
4:ae5178938864 | 557 | } |
Yogesh Pande |
4:ae5178938864 | 558 | if(!coap.empty()) { |
Yogesh Pande |
4:ae5178938864 | 559 | server_address = server_address.substr(coap.size(), |
Yogesh Pande |
4:ae5178938864 | 560 | server_address.size()-coap.size()); |
Christopher Haster |
1:79b6cc67d8b4 | 561 | int colonFound = server_address.find_last_of(':'); //10 |
Christopher Haster |
1:79b6cc67d8b4 | 562 | if(colonFound != -1) { |
Yogesh Pande |
4:ae5178938864 | 563 | ip_address = server_address.substr(0,colonFound); |
Yogesh Pande |
4:ae5178938864 | 564 | port = atoi(server_address.substr(colonFound+1, |
Christopher Haster |
1:79b6cc67d8b4 | 565 | server_address.size()-ip_address.size()).c_str()); |
Yogesh Pande |
4:ae5178938864 | 566 | colonFound = ip_address.find_last_of(']'); |
Yogesh Pande |
4:ae5178938864 | 567 | if(ip_address.compare(0,1,"[") == 0) { |
Yogesh Pande |
4:ae5178938864 | 568 | if(colonFound == -1) { |
Yogesh Pande |
4:ae5178938864 | 569 | ip_address.clear(); |
Yogesh Pande |
4:ae5178938864 | 570 | } else { |
Yogesh Pande |
4:ae5178938864 | 571 | ip_address = ip_address.substr(1,colonFound-1); |
Yogesh Pande |
4:ae5178938864 | 572 | } |
Yogesh Pande |
4:ae5178938864 | 573 | } else if(colonFound != -1) { |
Yogesh Pande |
4:ae5178938864 | 574 | ip_address.clear(); |
Christopher Haster |
1:79b6cc67d8b4 | 575 | } |
Christopher Haster |
1:79b6cc67d8b4 | 576 | } |
Yogesh Pande |
4:ae5178938864 | 577 | tr_debug("M2MInterfaceImpl::state_register - IP address %s , Port %d", ip_address.c_str(), port); |
Yogesh Pande |
4:ae5178938864 | 578 | // If bind and resolving server address succeed then proceed else |
Yogesh Pande |
4:ae5178938864 | 579 | // return error to the application and go to Idle state. |
Yogesh Pande |
4:ae5178938864 | 580 | if(ip_address.empty()) { |
Yogesh Pande |
4:ae5178938864 | 581 | tr_error("M2MInterfaceImpl::state_register - set error as M2MInterface::InvalidParameters"); |
Yogesh Pande |
4:ae5178938864 | 582 | error = M2MInterface::InvalidParameters; |
Yogesh Pande |
4:ae5178938864 | 583 | }else if(_connection_handler->resolve_server_address(ip_address, |
Yogesh Pande |
4:ae5178938864 | 584 | port, |
Yogesh Pande |
4:ae5178938864 | 585 | M2MConnectionObserver::LWM2MServer, |
Yogesh Pande |
4:ae5178938864 | 586 | security)) { |
Yogesh Pande |
4:ae5178938864 | 587 | tr_debug("M2MInterfaceImpl::state_register - resolve_server_address - success"); |
Yogesh Pande |
4:ae5178938864 | 588 | success = true; |
Yogesh Pande |
4:ae5178938864 | 589 | } else { |
Yogesh Pande |
4:ae5178938864 | 590 | tr_error("M2MInterfaceImpl::state_register - set error as M2MInterface::NetworkError"); |
Yogesh Pande |
4:ae5178938864 | 591 | error = M2MInterface::NetworkError; |
Yogesh Pande |
4:ae5178938864 | 592 | } |
Christopher Haster |
1:79b6cc67d8b4 | 593 | } |
Christopher Haster |
1:79b6cc67d8b4 | 594 | } |
Christopher Haster |
1:79b6cc67d8b4 | 595 | } |
Christopher Haster |
1:79b6cc67d8b4 | 596 | } |
Christopher Haster |
1:79b6cc67d8b4 | 597 | } |
Christopher Haster |
1:79b6cc67d8b4 | 598 | if(!success) { |
Christopher Haster |
1:79b6cc67d8b4 | 599 | tr_error("M2MInterfaceImpl::state_register - Error Occured %d", (int)error); |
Christopher Haster |
1:79b6cc67d8b4 | 600 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 601 | _observer.error(error); |
Christopher Haster |
1:79b6cc67d8b4 | 602 | } |
Christopher Haster |
1:79b6cc67d8b4 | 603 | } |
Christopher Haster |
1:79b6cc67d8b4 | 604 | |
Christopher Haster |
1:79b6cc67d8b4 | 605 | void M2MInterfaceImpl::state_register_address_resolved( EventData *data) |
Christopher Haster |
1:79b6cc67d8b4 | 606 | { |
Christopher Haster |
1:79b6cc67d8b4 | 607 | tr_debug("M2MInterfaceImpl::state_register_address_resolved"); |
Christopher Haster |
1:79b6cc67d8b4 | 608 | if(data) { |
Christopher Haster |
1:79b6cc67d8b4 | 609 | ResolvedAddressData *event = (ResolvedAddressData *)data; |
Christopher Haster |
1:79b6cc67d8b4 | 610 | |
Christopher Haster |
1:79b6cc67d8b4 | 611 | sn_nsdl_addr_type_e address_type = SN_NSDL_ADDRESS_TYPE_IPV6; |
Christopher Haster |
1:79b6cc67d8b4 | 612 | |
Christopher Haster |
1:79b6cc67d8b4 | 613 | M2MInterface::NetworkStack stack = event->_address->_stack; |
Christopher Haster |
1:79b6cc67d8b4 | 614 | |
Christopher Haster |
1:79b6cc67d8b4 | 615 | if(M2MInterface::LwIP_IPv4 == stack) { |
Christopher Haster |
1:79b6cc67d8b4 | 616 | tr_debug("M2MInterfaceImpl::state_register_address_resolved : IPv4 address"); |
Christopher Haster |
1:79b6cc67d8b4 | 617 | address_type = SN_NSDL_ADDRESS_TYPE_IPV4; |
Christopher Haster |
1:79b6cc67d8b4 | 618 | } else if((M2MInterface::LwIP_IPv6 == stack) || |
Christopher Haster |
1:79b6cc67d8b4 | 619 | (M2MInterface::Nanostack_IPv6 == stack)) { |
Christopher Haster |
1:79b6cc67d8b4 | 620 | tr_debug("M2MInterfaceImpl::state_register_address_resolved : IPv6 address"); |
Christopher Haster |
1:79b6cc67d8b4 | 621 | address_type = SN_NSDL_ADDRESS_TYPE_IPV6; |
Christopher Haster |
1:79b6cc67d8b4 | 622 | } |
Yogesh Pande |
4:ae5178938864 | 623 | if(_nsdl_interface->send_register_message((uint8_t*)event->_address->_address,event->_port, address_type)) { |
Yogesh Pande |
4:ae5178938864 | 624 | internal_event(STATE_REGISTER_RESOURCE_CREATED); |
Yogesh Pande |
4:ae5178938864 | 625 | _connection_handler->start_listening_for_data(); |
Yogesh Pande |
4:ae5178938864 | 626 | } else { |
Christopher Haster |
1:79b6cc67d8b4 | 627 | // If resource creation fails then inform error to application |
Christopher Haster |
1:79b6cc67d8b4 | 628 | tr_error("M2MInterfaceImpl::state_register_address_resolved : M2MInterface::InvalidParameters"); |
Christopher Haster |
1:79b6cc67d8b4 | 629 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 630 | _observer.error(M2MInterface::InvalidParameters); |
Christopher Haster |
1:79b6cc67d8b4 | 631 | } |
Christopher Haster |
1:79b6cc67d8b4 | 632 | } |
Christopher Haster |
1:79b6cc67d8b4 | 633 | } |
Christopher Haster |
1:79b6cc67d8b4 | 634 | |
Christopher Haster |
1:79b6cc67d8b4 | 635 | void M2MInterfaceImpl::state_register_resource_created( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 636 | { |
Christopher Haster |
1:79b6cc67d8b4 | 637 | tr_debug("M2MInterfaceImpl::state_register_resource_created"); |
Christopher Haster |
1:79b6cc67d8b4 | 638 | } |
Christopher Haster |
1:79b6cc67d8b4 | 639 | |
Christopher Haster |
1:79b6cc67d8b4 | 640 | void M2MInterfaceImpl::state_registered( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 641 | { |
Christopher Haster |
1:79b6cc67d8b4 | 642 | tr_debug("M2MInterfaceImpl::state_registered"); |
Christopher Haster |
1:79b6cc67d8b4 | 643 | _register_ongoing = false; |
Christopher Haster |
1:79b6cc67d8b4 | 644 | _update_register_ongoing = false; |
Christopher Haster |
1:79b6cc67d8b4 | 645 | } |
Christopher Haster |
1:79b6cc67d8b4 | 646 | |
Christopher Haster |
1:79b6cc67d8b4 | 647 | void M2MInterfaceImpl::state_update_registration( EventData *data) |
Christopher Haster |
1:79b6cc67d8b4 | 648 | { |
Christopher Haster |
1:79b6cc67d8b4 | 649 | tr_debug("M2MInterfaceImpl::state_update_registration"); |
Christopher Haster |
1:79b6cc67d8b4 | 650 | // Start with registration preparation |
Christopher Haster |
1:79b6cc67d8b4 | 651 | bool success = false; |
Christopher Haster |
1:79b6cc67d8b4 | 652 | if(data) { |
Christopher Haster |
1:79b6cc67d8b4 | 653 | M2MUpdateRegisterData *event = (M2MUpdateRegisterData *)data; |
Christopher Haster |
1:79b6cc67d8b4 | 654 | success = _nsdl_interface->send_update_registration(event->_lifetime); |
Christopher Haster |
1:79b6cc67d8b4 | 655 | |
Christopher Haster |
1:79b6cc67d8b4 | 656 | } |
Christopher Haster |
1:79b6cc67d8b4 | 657 | if(!success) { |
Christopher Haster |
1:79b6cc67d8b4 | 658 | tr_error("M2MInterfaceImpl::state_register_address_resolved : M2MInterface::InvalidParameters"); |
Christopher Haster |
1:79b6cc67d8b4 | 659 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 660 | _observer.error(M2MInterface::InvalidParameters); |
Christopher Haster |
1:79b6cc67d8b4 | 661 | } |
Christopher Haster |
1:79b6cc67d8b4 | 662 | } |
Christopher Haster |
1:79b6cc67d8b4 | 663 | |
Christopher Haster |
1:79b6cc67d8b4 | 664 | void M2MInterfaceImpl::state_unregister( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 665 | { |
Christopher Haster |
1:79b6cc67d8b4 | 666 | tr_debug("M2MInterfaceImpl::state_unregister"); |
Christopher Haster |
1:79b6cc67d8b4 | 667 | internal_event(STATE_SENDING_COAP_DATA); |
Christopher Haster |
1:79b6cc67d8b4 | 668 | if(!_nsdl_interface->send_unregister_message()) { |
Christopher Haster |
1:79b6cc67d8b4 | 669 | tr_error("M2MInterfaceImpl::state_unregister : M2MInterface::NotRegistered"); |
Christopher Haster |
1:79b6cc67d8b4 | 670 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 671 | _observer.error(M2MInterface::NotRegistered); |
Christopher Haster |
1:79b6cc67d8b4 | 672 | } |
Christopher Haster |
1:79b6cc67d8b4 | 673 | } |
Christopher Haster |
1:79b6cc67d8b4 | 674 | |
Christopher Haster |
1:79b6cc67d8b4 | 675 | void M2MInterfaceImpl::state_unregistered( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 676 | { |
Christopher Haster |
1:79b6cc67d8b4 | 677 | tr_debug("M2MInterfaceImpl::state_unregistered"); |
Christopher Haster |
1:79b6cc67d8b4 | 678 | internal_event(STATE_IDLE); |
Christopher Haster |
1:79b6cc67d8b4 | 679 | } |
Christopher Haster |
1:79b6cc67d8b4 | 680 | |
Christopher Haster |
1:79b6cc67d8b4 | 681 | void M2MInterfaceImpl::state_sending_coap_data( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 682 | { |
Christopher Haster |
1:79b6cc67d8b4 | 683 | tr_debug("M2MInterfaceImpl::state_sending_coap_data"); |
Christopher Haster |
1:79b6cc67d8b4 | 684 | internal_event(STATE_WAITING); |
Christopher Haster |
1:79b6cc67d8b4 | 685 | } |
Christopher Haster |
1:79b6cc67d8b4 | 686 | |
Christopher Haster |
1:79b6cc67d8b4 | 687 | void M2MInterfaceImpl::state_coap_data_sent( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 688 | { |
Christopher Haster |
1:79b6cc67d8b4 | 689 | tr_debug("M2MInterfaceImpl::state_coap_data_sent"); |
Christopher Haster |
1:79b6cc67d8b4 | 690 | internal_event(STATE_WAITING); |
Christopher Haster |
1:79b6cc67d8b4 | 691 | } |
Christopher Haster |
1:79b6cc67d8b4 | 692 | |
Christopher Haster |
1:79b6cc67d8b4 | 693 | void M2MInterfaceImpl::state_coap_data_received( EventData *data) |
Christopher Haster |
1:79b6cc67d8b4 | 694 | { |
Christopher Haster |
1:79b6cc67d8b4 | 695 | tr_debug("M2MInterfaceImpl::state_coap_data_received"); |
Christopher Haster |
1:79b6cc67d8b4 | 696 | if(data) { |
Christopher Haster |
1:79b6cc67d8b4 | 697 | ReceivedData *event = (ReceivedData*)data; |
Christopher Haster |
1:79b6cc67d8b4 | 698 | sn_nsdl_addr_s address; |
Christopher Haster |
1:79b6cc67d8b4 | 699 | |
Christopher Haster |
1:79b6cc67d8b4 | 700 | M2MInterface::NetworkStack stack = event->_address->_stack; |
Christopher Haster |
1:79b6cc67d8b4 | 701 | |
Christopher Haster |
1:79b6cc67d8b4 | 702 | if(M2MInterface::LwIP_IPv4 == stack) { |
Christopher Haster |
1:79b6cc67d8b4 | 703 | tr_debug("M2MInterfaceImpl::state_coap_data_received : IPv4 address"); |
Christopher Haster |
1:79b6cc67d8b4 | 704 | address.type = SN_NSDL_ADDRESS_TYPE_IPV4; |
Christopher Haster |
1:79b6cc67d8b4 | 705 | address.addr_len = 4; |
Christopher Haster |
1:79b6cc67d8b4 | 706 | } else if((M2MInterface::LwIP_IPv6 == stack) || |
Christopher Haster |
1:79b6cc67d8b4 | 707 | (M2MInterface::Nanostack_IPv6 == stack)) { |
Christopher Haster |
1:79b6cc67d8b4 | 708 | tr_debug("M2MInterfaceImpl::state_coap_data_received : IPv6 address"); |
Christopher Haster |
1:79b6cc67d8b4 | 709 | address.type = SN_NSDL_ADDRESS_TYPE_IPV6; |
Christopher Haster |
1:79b6cc67d8b4 | 710 | address.addr_len = 16; |
Christopher Haster |
1:79b6cc67d8b4 | 711 | } |
Christopher Haster |
1:79b6cc67d8b4 | 712 | address.port = event->_address->_port; |
Christopher Haster |
1:79b6cc67d8b4 | 713 | address.addr_ptr = (uint8_t*)event->_address->_address; |
Christopher Haster |
1:79b6cc67d8b4 | 714 | |
Christopher Haster |
1:79b6cc67d8b4 | 715 | // Process received data |
Christopher Haster |
1:79b6cc67d8b4 | 716 | internal_event(STATE_PROCESSING_COAP_DATA); |
Christopher Haster |
1:79b6cc67d8b4 | 717 | if(!_nsdl_interface->process_received_data(event->_data, |
Christopher Haster |
1:79b6cc67d8b4 | 718 | event->_size, |
Christopher Haster |
1:79b6cc67d8b4 | 719 | &address)) { |
Christopher Haster |
1:79b6cc67d8b4 | 720 | tr_error("M2MInterfaceImpl::state_coap_data_received : M2MInterface::ResponseParseFailed"); |
Christopher Haster |
1:79b6cc67d8b4 | 721 | _observer.error(M2MInterface::ResponseParseFailed); |
Christopher Haster |
1:79b6cc67d8b4 | 722 | } |
Christopher Haster |
1:79b6cc67d8b4 | 723 | } |
Christopher Haster |
1:79b6cc67d8b4 | 724 | } |
Christopher Haster |
1:79b6cc67d8b4 | 725 | |
Christopher Haster |
1:79b6cc67d8b4 | 726 | void M2MInterfaceImpl::state_processing_coap_data( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 727 | { |
Christopher Haster |
1:79b6cc67d8b4 | 728 | tr_debug("M2MInterfaceImpl::state_processing_coap_data"); |
Christopher Haster |
1:79b6cc67d8b4 | 729 | internal_event(STATE_WAITING); |
Christopher Haster |
1:79b6cc67d8b4 | 730 | } |
Christopher Haster |
1:79b6cc67d8b4 | 731 | |
Christopher Haster |
1:79b6cc67d8b4 | 732 | void M2MInterfaceImpl::state_coap_data_processed( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 733 | { |
Christopher Haster |
1:79b6cc67d8b4 | 734 | tr_debug("M2MInterfaceImpl::state_coap_data_processed"); |
Christopher Haster |
1:79b6cc67d8b4 | 735 | internal_event(STATE_WAITING); |
Christopher Haster |
1:79b6cc67d8b4 | 736 | } |
Christopher Haster |
1:79b6cc67d8b4 | 737 | |
Christopher Haster |
1:79b6cc67d8b4 | 738 | void M2MInterfaceImpl::state_waiting( EventData */*data*/) |
Christopher Haster |
1:79b6cc67d8b4 | 739 | { |
Christopher Haster |
1:79b6cc67d8b4 | 740 | tr_debug("M2MInterfaceImpl::state_waiting"); |
Christopher Haster |
1:79b6cc67d8b4 | 741 | } |
Christopher Haster |
1:79b6cc67d8b4 | 742 | |
Christopher Haster |
1:79b6cc67d8b4 | 743 | // generates an external event. called once per external event |
Christopher Haster |
1:79b6cc67d8b4 | 744 | // to start the state machine executing |
Christopher Haster |
1:79b6cc67d8b4 | 745 | void M2MInterfaceImpl::external_event(uint8_t new_state, |
Christopher Haster |
1:79b6cc67d8b4 | 746 | EventData* p_data) |
Christopher Haster |
1:79b6cc67d8b4 | 747 | { |
Christopher Haster |
1:79b6cc67d8b4 | 748 | tr_debug("M2MInterfaceImpl::external_event : new state %d", new_state); |
Christopher Haster |
1:79b6cc67d8b4 | 749 | // if we are supposed to ignore this event |
Christopher Haster |
1:79b6cc67d8b4 | 750 | if (new_state == EVENT_IGNORED) { |
Christopher Haster |
1:79b6cc67d8b4 | 751 | tr_debug("M2MInterfaceImpl::external_event : new state is EVENT_IGNORED"); |
Christopher Haster |
1:79b6cc67d8b4 | 752 | // just delete the event data, if any |
Christopher Haster |
1:79b6cc67d8b4 | 753 | if (p_data) { |
Christopher Haster |
1:79b6cc67d8b4 | 754 | delete p_data; |
Christopher Haster |
1:79b6cc67d8b4 | 755 | p_data = NULL; |
Christopher Haster |
1:79b6cc67d8b4 | 756 | } |
Christopher Haster |
1:79b6cc67d8b4 | 757 | _event_ignored = true; |
Christopher Haster |
1:79b6cc67d8b4 | 758 | } |
Christopher Haster |
1:79b6cc67d8b4 | 759 | else { |
Christopher Haster |
1:79b6cc67d8b4 | 760 | tr_debug("M2MInterfaceImpl::external_event : handle new state"); |
Christopher Haster |
1:79b6cc67d8b4 | 761 | // generate the event and execute the state engine |
Christopher Haster |
1:79b6cc67d8b4 | 762 | internal_event(new_state, p_data); |
Christopher Haster |
1:79b6cc67d8b4 | 763 | } |
Christopher Haster |
1:79b6cc67d8b4 | 764 | } |
Christopher Haster |
1:79b6cc67d8b4 | 765 | |
Christopher Haster |
1:79b6cc67d8b4 | 766 | // generates an internal event. called from within a state |
Christopher Haster |
1:79b6cc67d8b4 | 767 | // function to transition to a new state |
Christopher Haster |
1:79b6cc67d8b4 | 768 | void M2MInterfaceImpl::internal_event(uint8_t new_state, |
Christopher Haster |
1:79b6cc67d8b4 | 769 | EventData* p_data) |
Christopher Haster |
1:79b6cc67d8b4 | 770 | { |
Christopher Haster |
1:79b6cc67d8b4 | 771 | tr_debug("M2MInterfaceImpl::internal_event : new state %d", new_state); |
Christopher Haster |
1:79b6cc67d8b4 | 772 | _event_data = p_data; |
Christopher Haster |
1:79b6cc67d8b4 | 773 | _event_generated = true; |
Christopher Haster |
1:79b6cc67d8b4 | 774 | _current_state = new_state; |
Christopher Haster |
1:79b6cc67d8b4 | 775 | state_engine(); |
Christopher Haster |
1:79b6cc67d8b4 | 776 | } |
Christopher Haster |
1:79b6cc67d8b4 | 777 | |
Christopher Haster |
1:79b6cc67d8b4 | 778 | // the state engine executes the state machine states |
Christopher Haster |
1:79b6cc67d8b4 | 779 | void M2MInterfaceImpl::state_engine (void ) |
Christopher Haster |
1:79b6cc67d8b4 | 780 | { |
Christopher Haster |
1:79b6cc67d8b4 | 781 | tr_debug("M2MInterfaceImpl::state_engine"); |
Christopher Haster |
1:79b6cc67d8b4 | 782 | EventData* p_data_temp = NULL; |
Christopher Haster |
1:79b6cc67d8b4 | 783 | |
Christopher Haster |
1:79b6cc67d8b4 | 784 | // while events are being generated keep executing states |
Christopher Haster |
1:79b6cc67d8b4 | 785 | while (_event_generated) { |
Christopher Haster |
1:79b6cc67d8b4 | 786 | p_data_temp = _event_data; // copy of event data pointer |
Christopher Haster |
1:79b6cc67d8b4 | 787 | _event_data = NULL; // event data used up, reset ptr |
Christopher Haster |
1:79b6cc67d8b4 | 788 | _event_generated = false; // event used up, reset flag |
Christopher Haster |
1:79b6cc67d8b4 | 789 | |
Christopher Haster |
1:79b6cc67d8b4 | 790 | assert(_current_state < _max_states); |
Christopher Haster |
1:79b6cc67d8b4 | 791 | |
Christopher Haster |
1:79b6cc67d8b4 | 792 | state_function( _current_state, p_data_temp ); |
Christopher Haster |
1:79b6cc67d8b4 | 793 | |
Christopher Haster |
1:79b6cc67d8b4 | 794 | // if event data was used, then delete it |
Christopher Haster |
1:79b6cc67d8b4 | 795 | if (p_data_temp) { |
Christopher Haster |
1:79b6cc67d8b4 | 796 | delete p_data_temp; |
Christopher Haster |
1:79b6cc67d8b4 | 797 | p_data_temp = NULL; |
Christopher Haster |
1:79b6cc67d8b4 | 798 | } |
Christopher Haster |
1:79b6cc67d8b4 | 799 | } |
Christopher Haster |
1:79b6cc67d8b4 | 800 | } |
Christopher Haster |
1:79b6cc67d8b4 | 801 | |
Christopher Haster |
1:79b6cc67d8b4 | 802 | void M2MInterfaceImpl::state_function( uint8_t current_state, EventData* data ) |
Christopher Haster |
1:79b6cc67d8b4 | 803 | { |
Christopher Haster |
1:79b6cc67d8b4 | 804 | switch( current_state ) { |
Christopher Haster |
1:79b6cc67d8b4 | 805 | case STATE_IDLE: |
Christopher Haster |
1:79b6cc67d8b4 | 806 | M2MInterfaceImpl::state_idle(data); |
Christopher Haster |
1:79b6cc67d8b4 | 807 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 808 | case STATE_BOOTSTRAP: |
Yogesh Pande |
4:ae5178938864 | 809 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 810 | M2MInterfaceImpl::state_bootstrap(data); |
Yogesh Pande |
4:ae5178938864 | 811 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 812 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 813 | case STATE_BOOTSTRAP_ADDRESS_RESOLVED: |
Yogesh Pande |
4:ae5178938864 | 814 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 815 | M2MInterfaceImpl::state_bootstrap_address_resolved(data); |
Yogesh Pande |
4:ae5178938864 | 816 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 817 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 818 | case STATE_BOOTSTRAP_RESOURCE_CREATED: |
Yogesh Pande |
4:ae5178938864 | 819 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 820 | M2MInterfaceImpl::state_bootstrap_resource_created(data); |
Yogesh Pande |
4:ae5178938864 | 821 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 822 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 823 | case STATE_BOOTSTRAPPED: |
Yogesh Pande |
4:ae5178938864 | 824 | #ifndef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 825 | M2MInterfaceImpl::state_bootstrapped(data); |
Yogesh Pande |
4:ae5178938864 | 826 | #endif //YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE |
Christopher Haster |
1:79b6cc67d8b4 | 827 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 828 | case STATE_REGISTER: |
Christopher Haster |
1:79b6cc67d8b4 | 829 | M2MInterfaceImpl::state_register(data); |
Christopher Haster |
1:79b6cc67d8b4 | 830 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 831 | case STATE_REGISTER_ADDRESS_RESOLVED: |
Christopher Haster |
1:79b6cc67d8b4 | 832 | M2MInterfaceImpl::state_register_address_resolved(data); |
Christopher Haster |
1:79b6cc67d8b4 | 833 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 834 | case STATE_REGISTER_RESOURCE_CREATED: |
Christopher Haster |
1:79b6cc67d8b4 | 835 | M2MInterfaceImpl::state_register_resource_created(data); |
Christopher Haster |
1:79b6cc67d8b4 | 836 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 837 | case STATE_REGISTERED: |
Christopher Haster |
1:79b6cc67d8b4 | 838 | M2MInterfaceImpl::state_registered(data); |
Christopher Haster |
1:79b6cc67d8b4 | 839 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 840 | case STATE_UPDATE_REGISTRATION: |
Christopher Haster |
1:79b6cc67d8b4 | 841 | M2MInterfaceImpl::state_update_registration(data); |
Christopher Haster |
1:79b6cc67d8b4 | 842 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 843 | case STATE_UNREGISTER: |
Christopher Haster |
1:79b6cc67d8b4 | 844 | M2MInterfaceImpl::state_unregister(data); |
Christopher Haster |
1:79b6cc67d8b4 | 845 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 846 | case STATE_UNREGISTERED: |
Christopher Haster |
1:79b6cc67d8b4 | 847 | M2MInterfaceImpl::state_unregistered(data); |
Christopher Haster |
1:79b6cc67d8b4 | 848 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 849 | case STATE_SENDING_COAP_DATA: |
Christopher Haster |
1:79b6cc67d8b4 | 850 | M2MInterfaceImpl::state_sending_coap_data(data); |
Christopher Haster |
1:79b6cc67d8b4 | 851 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 852 | case STATE_COAP_DATA_SENT: |
Christopher Haster |
1:79b6cc67d8b4 | 853 | M2MInterfaceImpl::state_coap_data_sent(data); |
Christopher Haster |
1:79b6cc67d8b4 | 854 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 855 | case STATE_COAP_DATA_RECEIVED: |
Christopher Haster |
1:79b6cc67d8b4 | 856 | M2MInterfaceImpl::state_coap_data_received(data); |
Christopher Haster |
1:79b6cc67d8b4 | 857 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 858 | case STATE_PROCESSING_COAP_DATA: |
Christopher Haster |
1:79b6cc67d8b4 | 859 | M2MInterfaceImpl::state_processing_coap_data(data); |
Christopher Haster |
1:79b6cc67d8b4 | 860 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 861 | case STATE_COAP_DATA_PROCESSED: |
Christopher Haster |
1:79b6cc67d8b4 | 862 | M2MInterfaceImpl::state_coap_data_processed(data); |
Christopher Haster |
1:79b6cc67d8b4 | 863 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 864 | case STATE_WAITING: |
Christopher Haster |
1:79b6cc67d8b4 | 865 | M2MInterfaceImpl::state_waiting(data); |
Christopher Haster |
1:79b6cc67d8b4 | 866 | break; |
Christopher Haster |
1:79b6cc67d8b4 | 867 | } |
Christopher Haster |
1:79b6cc67d8b4 | 868 | } |