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.
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2minterfacefactory.cpp@0:8f0bb79ddd48, 2021-05-04 (annotated)
- Committer:
- leothedragon
- Date:
- Tue May 04 08:55:12 2021 +0000
- Revision:
- 0:8f0bb79ddd48
nmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:8f0bb79ddd48 | 1 | /* |
leothedragon | 0:8f0bb79ddd48 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
leothedragon | 0:8f0bb79ddd48 | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:8f0bb79ddd48 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:8f0bb79ddd48 | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:8f0bb79ddd48 | 6 | * You may obtain a copy of the License at |
leothedragon | 0:8f0bb79ddd48 | 7 | * |
leothedragon | 0:8f0bb79ddd48 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:8f0bb79ddd48 | 9 | * |
leothedragon | 0:8f0bb79ddd48 | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:8f0bb79ddd48 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:8f0bb79ddd48 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:8f0bb79ddd48 | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:8f0bb79ddd48 | 14 | * limitations under the License. |
leothedragon | 0:8f0bb79ddd48 | 15 | */ |
leothedragon | 0:8f0bb79ddd48 | 16 | |
leothedragon | 0:8f0bb79ddd48 | 17 | // Note: this macro is needed on armcc to get the the PRI*32 macros |
leothedragon | 0:8f0bb79ddd48 | 18 | // from inttypes.h in a C++ code. |
leothedragon | 0:8f0bb79ddd48 | 19 | #ifndef __STDC_FORMAT_MACROS |
leothedragon | 0:8f0bb79ddd48 | 20 | #define __STDC_FORMAT_MACROS |
leothedragon | 0:8f0bb79ddd48 | 21 | #endif |
leothedragon | 0:8f0bb79ddd48 | 22 | |
leothedragon | 0:8f0bb79ddd48 | 23 | #include "mbed-client/m2minterfacefactory.h" |
leothedragon | 0:8f0bb79ddd48 | 24 | #include "mbed-client/m2mserver.h" |
leothedragon | 0:8f0bb79ddd48 | 25 | #include "mbed-client/m2mdevice.h" |
leothedragon | 0:8f0bb79ddd48 | 26 | #include "mbed-client/m2mfirmware.h" |
leothedragon | 0:8f0bb79ddd48 | 27 | #include "mbed-client/m2mobject.h" |
leothedragon | 0:8f0bb79ddd48 | 28 | #include "mbed-client/m2mendpoint.h" |
leothedragon | 0:8f0bb79ddd48 | 29 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:8f0bb79ddd48 | 30 | #include "mbed-client/m2mconfig.h" |
leothedragon | 0:8f0bb79ddd48 | 31 | #include "include/m2minterfaceimpl.h" |
leothedragon | 0:8f0bb79ddd48 | 32 | #include "mbed-trace/mbed_trace.h" |
leothedragon | 0:8f0bb79ddd48 | 33 | |
leothedragon | 0:8f0bb79ddd48 | 34 | #include <inttypes.h> |
leothedragon | 0:8f0bb79ddd48 | 35 | |
leothedragon | 0:8f0bb79ddd48 | 36 | #define TRACE_GROUP "mClt" |
leothedragon | 0:8f0bb79ddd48 | 37 | |
leothedragon | 0:8f0bb79ddd48 | 38 | M2MInterface* M2MInterfaceFactory::create_interface(M2MInterfaceObserver &observer, |
leothedragon | 0:8f0bb79ddd48 | 39 | const String &endpoint_name, |
leothedragon | 0:8f0bb79ddd48 | 40 | const String &endpoint_type, |
leothedragon | 0:8f0bb79ddd48 | 41 | const int32_t life_time, |
leothedragon | 0:8f0bb79ddd48 | 42 | const uint16_t listen_port, |
leothedragon | 0:8f0bb79ddd48 | 43 | const String &domain, |
leothedragon | 0:8f0bb79ddd48 | 44 | M2MInterface::BindingMode mode, |
leothedragon | 0:8f0bb79ddd48 | 45 | M2MInterface::NetworkStack stack, |
leothedragon | 0:8f0bb79ddd48 | 46 | const String &context_address) |
leothedragon | 0:8f0bb79ddd48 | 47 | { |
leothedragon | 0:8f0bb79ddd48 | 48 | tr_debug("M2MInterfaceFactory::create_interface - IN"); |
leothedragon | 0:8f0bb79ddd48 | 49 | tr_info("M2MInterfaceFactory::create_interface - parameters endpoint name : %s",endpoint_name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 50 | tr_info("M2MInterfaceFactory::create_interface - parameters endpoint type : %s",endpoint_type.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 51 | tr_info("M2MInterfaceFactory::create_interface - parameters life time(in secs): %" PRId32,life_time); |
leothedragon | 0:8f0bb79ddd48 | 52 | tr_info("M2MInterfaceFactory::create_interface - parameters Listen Port : %d",listen_port); |
leothedragon | 0:8f0bb79ddd48 | 53 | tr_info("M2MInterfaceFactory::create_interface - parameters Binding Mode : %d",(int)mode); |
leothedragon | 0:8f0bb79ddd48 | 54 | tr_info("M2MInterfaceFactory::create_interface - parameters NetworkStack : %d",(int)stack); |
leothedragon | 0:8f0bb79ddd48 | 55 | M2MInterfaceImpl *interface = NULL; |
leothedragon | 0:8f0bb79ddd48 | 56 | |
leothedragon | 0:8f0bb79ddd48 | 57 | |
leothedragon | 0:8f0bb79ddd48 | 58 | bool endpoint_type_valid = true; |
leothedragon | 0:8f0bb79ddd48 | 59 | if(!endpoint_type.empty()) { |
leothedragon | 0:8f0bb79ddd48 | 60 | if(endpoint_type.size() > MAX_ALLOWED_STRING_LENGTH){ |
leothedragon | 0:8f0bb79ddd48 | 61 | endpoint_type_valid = false; |
leothedragon | 0:8f0bb79ddd48 | 62 | } |
leothedragon | 0:8f0bb79ddd48 | 63 | } |
leothedragon | 0:8f0bb79ddd48 | 64 | |
leothedragon | 0:8f0bb79ddd48 | 65 | bool domain_valid = true; |
leothedragon | 0:8f0bb79ddd48 | 66 | if(!domain.empty()) { |
leothedragon | 0:8f0bb79ddd48 | 67 | if(domain.size() > MAX_ALLOWED_STRING_LENGTH){ |
leothedragon | 0:8f0bb79ddd48 | 68 | domain_valid = false; |
leothedragon | 0:8f0bb79ddd48 | 69 | } |
leothedragon | 0:8f0bb79ddd48 | 70 | } |
leothedragon | 0:8f0bb79ddd48 | 71 | |
leothedragon | 0:8f0bb79ddd48 | 72 | if(((life_time == -1) || (life_time >= MINIMUM_REGISTRATION_TIME)) && |
leothedragon | 0:8f0bb79ddd48 | 73 | !endpoint_name.empty() && (endpoint_name.size() <= MAX_ALLOWED_STRING_LENGTH) && |
leothedragon | 0:8f0bb79ddd48 | 74 | endpoint_type_valid && domain_valid) { |
leothedragon | 0:8f0bb79ddd48 | 75 | tr_debug("M2MInterfaceFactory::create_interface - Creating M2MInterfaceImpl"); |
leothedragon | 0:8f0bb79ddd48 | 76 | interface = new M2MInterfaceImpl(observer, endpoint_name, |
leothedragon | 0:8f0bb79ddd48 | 77 | endpoint_type, life_time, |
leothedragon | 0:8f0bb79ddd48 | 78 | listen_port, domain, mode, |
leothedragon | 0:8f0bb79ddd48 | 79 | stack, context_address); |
leothedragon | 0:8f0bb79ddd48 | 80 | |
leothedragon | 0:8f0bb79ddd48 | 81 | } |
leothedragon | 0:8f0bb79ddd48 | 82 | tr_debug("M2MInterfaceFactory::create_interface - OUT"); |
leothedragon | 0:8f0bb79ddd48 | 83 | return interface; |
leothedragon | 0:8f0bb79ddd48 | 84 | } |
leothedragon | 0:8f0bb79ddd48 | 85 | |
leothedragon | 0:8f0bb79ddd48 | 86 | M2MSecurity* M2MInterfaceFactory::create_security(M2MSecurity::ServerType server_type) |
leothedragon | 0:8f0bb79ddd48 | 87 | { |
leothedragon | 0:8f0bb79ddd48 | 88 | tr_debug("M2MInterfaceFactory::create_security"); |
leothedragon | 0:8f0bb79ddd48 | 89 | M2MSecurity *security = M2MSecurity::get_instance(); |
leothedragon | 0:8f0bb79ddd48 | 90 | return security; |
leothedragon | 0:8f0bb79ddd48 | 91 | } |
leothedragon | 0:8f0bb79ddd48 | 92 | |
leothedragon | 0:8f0bb79ddd48 | 93 | M2MServer* M2MInterfaceFactory::create_server() |
leothedragon | 0:8f0bb79ddd48 | 94 | { |
leothedragon | 0:8f0bb79ddd48 | 95 | tr_debug("M2MInterfaceFactory::create_server"); |
leothedragon | 0:8f0bb79ddd48 | 96 | M2MServer *server = new M2MServer(); |
leothedragon | 0:8f0bb79ddd48 | 97 | return server; |
leothedragon | 0:8f0bb79ddd48 | 98 | } |
leothedragon | 0:8f0bb79ddd48 | 99 | |
leothedragon | 0:8f0bb79ddd48 | 100 | M2MDevice* M2MInterfaceFactory::create_device() |
leothedragon | 0:8f0bb79ddd48 | 101 | { |
leothedragon | 0:8f0bb79ddd48 | 102 | tr_debug("M2MInterfaceFactory::create_device"); |
leothedragon | 0:8f0bb79ddd48 | 103 | M2MDevice* device = M2MDevice::get_instance(); |
leothedragon | 0:8f0bb79ddd48 | 104 | return device; |
leothedragon | 0:8f0bb79ddd48 | 105 | } |
leothedragon | 0:8f0bb79ddd48 | 106 | |
leothedragon | 0:8f0bb79ddd48 | 107 | M2MFirmware* M2MInterfaceFactory::create_firmware() |
leothedragon | 0:8f0bb79ddd48 | 108 | { |
leothedragon | 0:8f0bb79ddd48 | 109 | tr_debug("M2MInterfaceFactory::create_firmware"); |
leothedragon | 0:8f0bb79ddd48 | 110 | M2MFirmware* firmware = M2MFirmware::get_instance(); |
leothedragon | 0:8f0bb79ddd48 | 111 | return firmware; |
leothedragon | 0:8f0bb79ddd48 | 112 | } |
leothedragon | 0:8f0bb79ddd48 | 113 | |
leothedragon | 0:8f0bb79ddd48 | 114 | M2MObject* M2MInterfaceFactory::create_object(const String &name) |
leothedragon | 0:8f0bb79ddd48 | 115 | { |
leothedragon | 0:8f0bb79ddd48 | 116 | tr_debug("M2MInterfaceFactory::create_object : Name : %s", name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 117 | if(name.size() > MAX_ALLOWED_STRING_LENGTH || name.empty()){ |
leothedragon | 0:8f0bb79ddd48 | 118 | return NULL; |
leothedragon | 0:8f0bb79ddd48 | 119 | } |
leothedragon | 0:8f0bb79ddd48 | 120 | |
leothedragon | 0:8f0bb79ddd48 | 121 | M2MObject *object = NULL; |
leothedragon | 0:8f0bb79ddd48 | 122 | char *name_copy = M2MBase::stringdup(name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 123 | if (name_copy) { |
leothedragon | 0:8f0bb79ddd48 | 124 | object = new M2MObject(name, name_copy); |
leothedragon | 0:8f0bb79ddd48 | 125 | } |
leothedragon | 0:8f0bb79ddd48 | 126 | return object; |
leothedragon | 0:8f0bb79ddd48 | 127 | } |
leothedragon | 0:8f0bb79ddd48 | 128 | |
leothedragon | 0:8f0bb79ddd48 | 129 | #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION |
leothedragon | 0:8f0bb79ddd48 | 130 | M2MEndpoint* M2MInterfaceFactory::create_endpoint(const String &name) |
leothedragon | 0:8f0bb79ddd48 | 131 | { |
leothedragon | 0:8f0bb79ddd48 | 132 | tr_debug("M2MInterfaceFactory::create_endpoint : Name : %s", name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 133 | if(name.size() > MAX_ALLOWED_STRING_LENGTH || name.empty()){ |
leothedragon | 0:8f0bb79ddd48 | 134 | return NULL; |
leothedragon | 0:8f0bb79ddd48 | 135 | } |
leothedragon | 0:8f0bb79ddd48 | 136 | |
leothedragon | 0:8f0bb79ddd48 | 137 | M2MEndpoint *object = NULL; |
leothedragon | 0:8f0bb79ddd48 | 138 | char *path = (char*)malloc(2 + name.size() + 1); |
leothedragon | 0:8f0bb79ddd48 | 139 | if (path) { |
leothedragon | 0:8f0bb79ddd48 | 140 | // Prepend path with directory prefix "d/" so that all endpoints will be under common path |
leothedragon | 0:8f0bb79ddd48 | 141 | path[0] = 'd'; |
leothedragon | 0:8f0bb79ddd48 | 142 | path[1] = '/'; |
leothedragon | 0:8f0bb79ddd48 | 143 | memcpy(&path[2], name.c_str(), name.size()); |
leothedragon | 0:8f0bb79ddd48 | 144 | path[name.size() + 2] = '\0'; |
leothedragon | 0:8f0bb79ddd48 | 145 | object = new M2MEndpoint(name, path); |
leothedragon | 0:8f0bb79ddd48 | 146 | } |
leothedragon | 0:8f0bb79ddd48 | 147 | return object; |
leothedragon | 0:8f0bb79ddd48 | 148 | } |
leothedragon | 0:8f0bb79ddd48 | 149 | #endif |