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
source/Utils.cpp@127:b4a661ff6fb9, 2017-09-26 (annotated)
- Committer:
- ansond
- Date:
- Tue Sep 26 16:01:31 2017 +0000
- Revision:
- 127:b4a661ff6fb9
- Parent:
- 108:df7102357164
minor re-ordering of FCC init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 1:16f0fb5b8d97 | 1 | /** |
ansond | 1:16f0fb5b8d97 | 2 | * @file Utils.cpp |
ansond | 1:16f0fb5b8d97 | 3 | * @brief mbed CoAP Endpoint misc utils collection |
ansond | 1:16f0fb5b8d97 | 4 | * @author Doug Anson |
ansond | 1:16f0fb5b8d97 | 5 | * @version 1.0 |
ansond | 1:16f0fb5b8d97 | 6 | * @see |
ansond | 1:16f0fb5b8d97 | 7 | * |
ansond | 1:16f0fb5b8d97 | 8 | * Copyright (c) 2014 |
ansond | 1:16f0fb5b8d97 | 9 | * |
ansond | 1:16f0fb5b8d97 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 1:16f0fb5b8d97 | 11 | * you may not use this file except in compliance with the License. |
ansond | 1:16f0fb5b8d97 | 12 | * You may obtain a copy of the License at |
ansond | 1:16f0fb5b8d97 | 13 | * |
ansond | 1:16f0fb5b8d97 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 1:16f0fb5b8d97 | 15 | * |
ansond | 1:16f0fb5b8d97 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 1:16f0fb5b8d97 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 1:16f0fb5b8d97 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 1:16f0fb5b8d97 | 19 | * See the License for the specific language governing permissions and |
ansond | 1:16f0fb5b8d97 | 20 | * limitations under the License. |
ansond | 1:16f0fb5b8d97 | 21 | */ |
ansond | 1:16f0fb5b8d97 | 22 | |
ansond | 1:16f0fb5b8d97 | 23 | // mbed Endpoint includes |
ansond | 1:16f0fb5b8d97 | 24 | #include "mbed-connector-interface/ConnectorEndpoint.h" |
ansond | 1:16f0fb5b8d97 | 25 | #include "mbed-connector-interface/OptionsBuilder.h" |
ansond | 6:d4d1105a1777 | 26 | #include "mbed-connector-interface/mbedEndpointNetwork.h" |
ansond | 13:9edad7677211 | 27 | #include "mbed-connector-interface/DeviceManager.h" |
ansond | 27:b8aaf7dc7023 | 28 | #include "mbed-connector-interface/ObjectInstanceManager.h" |
ansond | 1:16f0fb5b8d97 | 29 | |
ansond | 1:16f0fb5b8d97 | 30 | // External references (defined in main.cpp) |
ansond | 1:16f0fb5b8d97 | 31 | Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder); |
ansond | 1:16f0fb5b8d97 | 32 | extern Logger logger; |
ansond | 1:16f0fb5b8d97 | 33 | |
ansond | 8:f950fb1b78c0 | 34 | // Our Endpoint |
ansond | 8:f950fb1b78c0 | 35 | Connector::Endpoint *__endpoint = NULL; |
ansond | 8:f950fb1b78c0 | 36 | |
ansond | 1:16f0fb5b8d97 | 37 | // Our Endpoint configured Options |
ansond | 1:16f0fb5b8d97 | 38 | Connector::OptionsBuilder config; |
ansond | 1:16f0fb5b8d97 | 39 | Connector::Options *options = NULL; |
ansond | 1:16f0fb5b8d97 | 40 | |
ansond | 8:f950fb1b78c0 | 41 | // initialize the Connector::Endpoint instance |
ansond | 8:f950fb1b78c0 | 42 | void *utils_init_endpoint(bool canActAsRouterNode) { |
ansond | 8:f950fb1b78c0 | 43 | // alloc Endpoint |
ansond | 8:f950fb1b78c0 | 44 | logger.log("Endpoint: allocating endpoint instance..."); |
ansond | 8:f950fb1b78c0 | 45 | Connector::Endpoint *ep = new Connector::Endpoint(&logger,options); |
ansond | 8:f950fb1b78c0 | 46 | if (ep != NULL) { |
ansond | 27:b8aaf7dc7023 | 47 | // link to config object |
ansond | 21:0bbe9057e7b1 | 48 | config.setEndpoint((void *)ep); |
ansond | 27:b8aaf7dc7023 | 49 | |
ansond | 27:b8aaf7dc7023 | 50 | // not sure if we need this anymore... |
ansond | 8:f950fb1b78c0 | 51 | ep->asRouterNode(canActAsRouterNode); |
ansond | 27:b8aaf7dc7023 | 52 | |
ansond | 27:b8aaf7dc7023 | 53 | // Add ObjectInstanceManager |
ansond | 27:b8aaf7dc7023 | 54 | ObjectInstanceManager *oim = new ObjectInstanceManager(&logger,(void *)ep); |
ansond | 27:b8aaf7dc7023 | 55 | ep->setObjectInstanceManager(oim); |
ansond | 8:f950fb1b78c0 | 56 | } |
ansond | 8:f950fb1b78c0 | 57 | return (void *)ep; |
ansond | 8:f950fb1b78c0 | 58 | } |
ansond | 1:16f0fb5b8d97 | 59 | |
ansond | 1:16f0fb5b8d97 | 60 | // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint... |
ansond | 1:16f0fb5b8d97 | 61 | void utils_configure_endpoint(void *p) |
ansond | 1:16f0fb5b8d97 | 62 | { |
ansond | 1:16f0fb5b8d97 | 63 | // our Endpoint |
ansond | 1:16f0fb5b8d97 | 64 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 1:16f0fb5b8d97 | 65 | |
ansond | 54:dfee8691c83a | 66 | // default configuration - see mbedConnectorInterface.h for definitions... |
ansond | 1:16f0fb5b8d97 | 67 | logger.log("Endpoint: setting defaults..."); |
ansond | 1:16f0fb5b8d97 | 68 | config.setEndpointNodename(NODE_NAME); |
ansond | 54:dfee8691c83a | 69 | config.setEndpointType(DEFAULT_ENDPOINT_TYPE); |
ansond | 1:16f0fb5b8d97 | 70 | config.setLifetime(REG_LIFETIME_SEC); |
ansond | 0:1f1f55e73248 | 71 | |
ansond | 1:16f0fb5b8d97 | 72 | // WiFi defaults |
ansond | 1:16f0fb5b8d97 | 73 | config.setWiFiSSID((char *)WIFI_DEFAULT_SSID); // default: changeme |
ansond | 12:d0e61bac8c27 | 74 | config.setWiFiAuthType(WIFI_NONE); // default: none |
ansond | 11:3d4434825cd7 | 75 | config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY); // default: changeme |
ansond | 11:3d4434825cd7 | 76 | |
ansond | 108:df7102357164 | 77 | #ifdef ENABLE_MBED_CLOUD_SUPPORT |
ansond | 108:df7102357164 | 78 | // Default CoAP Connection Type (mbed Cloud R1.2+) |
ansond | 108:df7102357164 | 79 | config.setCoAPConnectionType(COAP_TCP); // default CoAP Connection Type - TCP |
ansond | 108:df7102357164 | 80 | #else |
ansond | 108:df7102357164 | 81 | // Default CoAP Connection Type (Connector/mDS) |
ansond | 108:df7102357164 | 82 | config.setCoAPConnectionType(COAP_UDP); // default CoAP Connection Type - UDP |
ansond | 108:df7102357164 | 83 | #endif |
ansond | 58:7a2df2ce591e | 84 | |
ansond | 58:7a2df2ce591e | 85 | // Set the default IP Address Type |
ansond | 58:7a2df2ce591e | 86 | #if MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
ansond | 58:7a2df2ce591e | 87 | config.setIPAddressType(IP_ADDRESS_TYPE_IPV6); // IPv6 is the default for mesh |
ansond | 58:7a2df2ce591e | 88 | #else |
ansond | 58:7a2df2ce591e | 89 | config.setIPAddressType(IP_ADDRESS_TYPE_IPV4); // IPv4 is the default for all but mesh |
ansond | 58:7a2df2ce591e | 90 | #endif |
ansond | 1:16f0fb5b8d97 | 91 | |
ansond | 1:16f0fb5b8d97 | 92 | // Establish default CoAP observation behavior |
ansond | 1:16f0fb5b8d97 | 93 | config.setImmedateObservationEnabled(true); |
ansond | 1:16f0fb5b8d97 | 94 | |
ansond | 1:16f0fb5b8d97 | 95 | // Establish default CoAP GET-based observation control behavior |
ansond | 1:16f0fb5b8d97 | 96 | config.setEnableGETObservationControl(false); |
ansond | 1:16f0fb5b8d97 | 97 | |
ansond | 13:9edad7677211 | 98 | // Device Manager installation |
ansond | 13:9edad7677211 | 99 | DeviceManager *device_manager = (DeviceManager *)ep->getDeviceManager(); |
ansond | 13:9edad7677211 | 100 | if (device_manager != NULL) { |
ansond | 13:9edad7677211 | 101 | logger.log("Endpoint: installing and setting up device manager and its resources..."); |
ansond | 13:9edad7677211 | 102 | device_manager->install((void *)ep,(void *)&config); |
ansond | 13:9edad7677211 | 103 | } |
ansond | 13:9edad7677211 | 104 | else { |
ansond | 13:9edad7677211 | 105 | logger.log("Endpoint: no device manager installed..."); |
ansond | 13:9edad7677211 | 106 | } |
ansond | 13:9edad7677211 | 107 | |
ansond | 1:16f0fb5b8d97 | 108 | // main.cpp can override or change any of the above defaults... |
ansond | 1:16f0fb5b8d97 | 109 | logger.log("Endpoint: gathering configuration overrides..."); |
ansond | 1:16f0fb5b8d97 | 110 | options = configure_endpoint(config); |
ansond | 21:0bbe9057e7b1 | 111 | |
ansond | 21:0bbe9057e7b1 | 112 | // set our options |
ansond | 8:f950fb1b78c0 | 113 | ep->setOptions(options); |
ansond | 8:f950fb1b78c0 | 114 | |
ansond | 1:16f0fb5b8d97 | 115 | // DONE |
ansond | 1:16f0fb5b8d97 | 116 | logger.log("Endpoint: endpoint configuration completed."); |
ansond | 1:16f0fb5b8d97 | 117 | } |
ansond | 1:16f0fb5b8d97 | 118 | |
ansond | 46:62da4ce20276 | 119 | // build out the endpoint and its resources |
ansond | 8:f950fb1b78c0 | 120 | void utils_build_endpoint(void *p) |
ansond | 1:16f0fb5b8d97 | 121 | { |
ansond | 1:16f0fb5b8d97 | 122 | if (p != NULL) { |
ansond | 8:f950fb1b78c0 | 123 | // Build the Endpoint |
ansond | 8:f950fb1b78c0 | 124 | logger.log("Endpoint: building endpoint and its resources..."); |
ansond | 1:16f0fb5b8d97 | 125 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 46:62da4ce20276 | 126 | ep->buildEndpoint(); |
ansond | 1:16f0fb5b8d97 | 127 | } |
ansond | 10:3f79b5e67c22 | 128 | } |
ansond | 10:3f79b5e67c22 | 129 | |
ansond | 10:3f79b5e67c22 | 130 | // parse out the CoAP port number from the connection URL |
ansond | 10:3f79b5e67c22 | 131 | uint16_t extract_port_from_url(char *url,uint16_t default_port) |
ansond | 10:3f79b5e67c22 | 132 | { |
ansond | 10:3f79b5e67c22 | 133 | uint16_t port = default_port; |
ansond | 10:3f79b5e67c22 | 134 | |
ansond | 10:3f79b5e67c22 | 135 | if (url != NULL && strlen(url) > 0) { |
ansond | 10:3f79b5e67c22 | 136 | char buffer[MAX_CONN_URL_LENGTH+1]; |
ansond | 10:3f79b5e67c22 | 137 | char uri[MAX_CONN_URL_LENGTH+1]; |
ansond | 10:3f79b5e67c22 | 138 | char path[MAX_CONN_URL_LENGTH+1]; |
ansond | 10:3f79b5e67c22 | 139 | |
ansond | 10:3f79b5e67c22 | 140 | // initialize the buffer |
ansond | 10:3f79b5e67c22 | 141 | memset(buffer,0,MAX_CONN_URL_LENGTH+1); |
ansond | 10:3f79b5e67c22 | 142 | memset(uri,0,MAX_CONN_URL_LENGTH+1); |
ansond | 10:3f79b5e67c22 | 143 | memset(path,0,MAX_CONN_URL_LENGTH+1); |
ansond | 10:3f79b5e67c22 | 144 | int length = strlen(url); |
ansond | 10:3f79b5e67c22 | 145 | |
ansond | 10:3f79b5e67c22 | 146 | // truncate if needed |
ansond | 10:3f79b5e67c22 | 147 | if (length >MAX_CONN_URL_LENGTH) length = MAX_CONN_URL_LENGTH; |
ansond | 10:3f79b5e67c22 | 148 | |
ansond | 10:3f79b5e67c22 | 149 | // make a copy... |
ansond | 10:3f79b5e67c22 | 150 | memcpy(buffer,url,length); |
ansond | 10:3f79b5e67c22 | 151 | |
ansond | 10:3f79b5e67c22 | 152 | // remove the forward slashes and colons |
ansond | 10:3f79b5e67c22 | 153 | for(int i=0;i<length;++i) { |
ansond | 10:3f79b5e67c22 | 154 | if (buffer[i] == ':') buffer[i] = ' '; |
ansond | 10:3f79b5e67c22 | 155 | if (buffer[i] == '/') buffer[i] = ' '; |
ansond | 53:d2f9f94b6000 | 156 | if (buffer[i] == '[') buffer[i] = ' '; |
ansond | 53:d2f9f94b6000 | 157 | if (buffer[i] == ']') buffer[i] = ' '; |
ansond | 10:3f79b5e67c22 | 158 | } |
ansond | 10:3f79b5e67c22 | 159 | |
ansond | 10:3f79b5e67c22 | 160 | // parse |
ansond | 53:d2f9f94b6000 | 161 | sscanf(buffer,"%s%s%d",uri,path,(int *)&port); |
ansond | 57:b017388f1aa3 | 162 | |
ansond | 57:b017388f1aa3 | 163 | // IPv6 kludge until we parse it properly... |
ansond | 57:b017388f1aa3 | 164 | if (strcmp(uri,"coaps") == 0 && strcmp(path,"2607") == 0) { |
ansond | 57:b017388f1aa3 | 165 | // IPv6 in use - so nail to Connector's IPv6 address |
ansond | 57:b017388f1aa3 | 166 | strcpy(uri,"coap"); |
ansond | 57:b017388f1aa3 | 167 | strcpy(path,"2607:f0d0:2601:52::20"); |
ansond | 57:b017388f1aa3 | 168 | port = default_port; |
ansond | 57:b017388f1aa3 | 169 | |
ansond | 57:b017388f1aa3 | 170 | // DEBUG |
ansond | 57:b017388f1aa3 | 171 | logger.log("Endpoint: Connector IPV6 uri: %s path: %s port: %d",uri,path,port); |
ansond | 57:b017388f1aa3 | 172 | } |
ansond | 10:3f79b5e67c22 | 173 | } |
ansond | 10:3f79b5e67c22 | 174 | |
ansond | 10:3f79b5e67c22 | 175 | // DEBUG |
ansond | 53:d2f9f94b6000 | 176 | logger.log("Endpoint: Connector URL: %s CoAP port: %u",url,port); |
ansond | 10:3f79b5e67c22 | 177 | |
ansond | 10:3f79b5e67c22 | 178 | // return the port |
ansond | 10:3f79b5e67c22 | 179 | return port; |
ansond | 8:f950fb1b78c0 | 180 | } |