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 mbedConnectorInterfaceV3 by
Utils.cpp
00001 /** 00002 * @file Utils.cpp 00003 * @brief mbed CoAP Endpoint misc utils collection 00004 * @author Doug Anson 00005 * @version 1.0 00006 * @see 00007 * 00008 * Copyright (c) 2014 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); 00011 * you may not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, 00018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 */ 00022 00023 // mbed Endpoint includes 00024 #include "mbed-connector-interface/ConnectorEndpoint.h" 00025 #include "mbed-connector-interface/OptionsBuilder.h" 00026 #include "mbed-connector-interface/mbedEndpointNetwork.h" 00027 #include "mbed-connector-interface/DeviceManager.h" 00028 #include "mbed-connector-interface/ObjectInstanceManager.h" 00029 00030 // External references (defined in main.cpp) 00031 Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder); 00032 extern Logger logger; 00033 00034 // Our Endpoint 00035 Connector::Endpoint *__endpoint = NULL; 00036 00037 // Our Endpoint configured Options 00038 Connector::OptionsBuilder config; 00039 Connector::Options *options = NULL; 00040 00041 // initialize the Connector::Endpoint instance 00042 void *utils_init_endpoint(bool canActAsRouterNode) { 00043 // alloc Endpoint 00044 logger.log("Endpoint: allocating endpoint instance..."); 00045 Connector::Endpoint *ep = new Connector::Endpoint(&logger,options); 00046 if (ep != NULL) { 00047 // link to config object 00048 config.setEndpoint((void *)ep); 00049 00050 // not sure if we need this anymore... 00051 ep->asRouterNode(canActAsRouterNode); 00052 00053 // Add ObjectInstanceManager 00054 ObjectInstanceManager *oim = new ObjectInstanceManager(&logger,(void *)ep); 00055 ep->setObjectInstanceManager(oim); 00056 } 00057 return (void *)ep; 00058 } 00059 00060 // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint... 00061 void utils_configure_endpoint(void *p) 00062 { 00063 // our Endpoint 00064 Connector::Endpoint *ep = (Connector::Endpoint *)p; 00065 00066 // default configuration - see mbedConnectorInterface.h for definitions... 00067 logger.log("Endpoint: setting defaults..."); 00068 config.setEndpointNodename(NODE_NAME); 00069 config.setEndpointType(DEFAULT_ENDPOINT_TYPE); 00070 config.setLifetime(REG_LIFETIME_SEC); 00071 00072 // WiFi defaults 00073 config.setWiFiSSID((char *)WIFI_DEFAULT_SSID); // default: changeme 00074 config.setWiFiAuthType(WIFI_NONE); // default: none 00075 config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY); // default: changeme 00076 00077 #ifdef ENABLE_MBED_CLOUD_SUPPORT 00078 // Default CoAP Connection Type (mbed Cloud R1.2+) 00079 config.setCoAPConnectionType(COAP_TCP); // default CoAP Connection Type - TCP 00080 #else 00081 // Default CoAP Connection Type (Connector/mDS) 00082 config.setCoAPConnectionType(COAP_UDP); // default CoAP Connection Type - UDP 00083 #endif 00084 00085 // Set the default IP Address Type 00086 #if MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD 00087 config.setIPAddressType(IP_ADDRESS_TYPE_IPV6); // IPv6 is the default for mesh 00088 #else 00089 config.setIPAddressType(IP_ADDRESS_TYPE_IPV4); // IPv4 is the default for all but mesh 00090 #endif 00091 00092 // Establish default CoAP observation behavior 00093 config.setImmedateObservationEnabled(true); 00094 00095 // Establish default CoAP GET-based observation control behavior 00096 config.setEnableGETObservationControl(false); 00097 00098 // Device Manager installation 00099 DeviceManager *device_manager = (DeviceManager *)ep->getDeviceManager(); 00100 if (device_manager != NULL) { 00101 logger.log("Endpoint: installing and setting up device manager and its resources..."); 00102 device_manager->install((void *)ep,(void *)&config); 00103 } 00104 else { 00105 logger.log("Endpoint: no device manager installed..."); 00106 } 00107 00108 // main.cpp can override or change any of the above defaults... 00109 logger.log("Endpoint: gathering configuration overrides..."); 00110 options = configure_endpoint(config); 00111 00112 // set our options 00113 ep->setOptions(options); 00114 00115 // DONE 00116 logger.log("Endpoint: endpoint configuration completed."); 00117 } 00118 00119 // build out the endpoint and its resources 00120 void utils_build_endpoint(void *p) 00121 { 00122 if (p != NULL) { 00123 // Build the Endpoint 00124 logger.log("Endpoint: building endpoint and its resources..."); 00125 Connector::Endpoint *ep = (Connector::Endpoint *)p; 00126 ep->buildEndpoint(); 00127 } 00128 } 00129 00130 // parse out the CoAP port number from the connection URL 00131 uint16_t extract_port_from_url(char *url,uint16_t default_port) 00132 { 00133 uint16_t port = default_port; 00134 00135 if (url != NULL && strlen(url) > 0) { 00136 char buffer[MAX_CONN_URL_LENGTH+1]; 00137 char uri[MAX_CONN_URL_LENGTH+1]; 00138 char path[MAX_CONN_URL_LENGTH+1]; 00139 00140 // initialize the buffer 00141 memset(buffer,0,MAX_CONN_URL_LENGTH+1); 00142 memset(uri,0,MAX_CONN_URL_LENGTH+1); 00143 memset(path,0,MAX_CONN_URL_LENGTH+1); 00144 int length = strlen(url); 00145 00146 // truncate if needed 00147 if (length >MAX_CONN_URL_LENGTH) length = MAX_CONN_URL_LENGTH; 00148 00149 // make a copy... 00150 memcpy(buffer,url,length); 00151 00152 // remove the forward slashes and colons 00153 for(int i=0;i<length;++i) { 00154 if (buffer[i] == ':') buffer[i] = ' '; 00155 if (buffer[i] == '/') buffer[i] = ' '; 00156 if (buffer[i] == '[') buffer[i] = ' '; 00157 if (buffer[i] == ']') buffer[i] = ' '; 00158 } 00159 00160 // parse 00161 sscanf(buffer,"%s%s%d",uri,path,(int *)&port); 00162 00163 // IPv6 kludge until we parse it properly... 00164 if (strcmp(uri,"coaps") == 0 && strcmp(path,"2607") == 0) { 00165 // IPv6 in use - so nail to Connector's IPv6 address 00166 strcpy(uri,"coap"); 00167 strcpy(path,"2607:f0d0:2601:52::20"); 00168 port = default_port; 00169 00170 // DEBUG 00171 logger.log("Endpoint: Connector IPV6 uri: %s path: %s port: %d",uri,path,port); 00172 } 00173 } 00174 00175 // DEBUG 00176 logger.log("Endpoint: Connector URL: %s CoAP port: %u",url,port); 00177 00178 // return the port 00179 return port; 00180 }
Generated on Tue Jul 12 2022 18:16:52 by
1.7.2
