use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Tue Jun 14 04:01:34 2016 +0000
Revision:
27:b8aaf7dc7023
Parent:
21:0bbe9057e7b1
Child:
33:1d0b855df5a5
Initial merge and update of device and firmware objects

Who changed what in which revision?

UserRevisionLine numberNew 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 10:3f79b5e67c22 30 // Maximum CoAP URL length
ansond 10:3f79b5e67c22 31 #define MAX_CONN_URL_LENGTH 128
ansond 10:3f79b5e67c22 32
ansond 1:16f0fb5b8d97 33 // External references (defined in main.cpp)
ansond 1:16f0fb5b8d97 34 Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder);
ansond 1:16f0fb5b8d97 35 extern Logger logger;
ansond 1:16f0fb5b8d97 36
ansond 8:f950fb1b78c0 37 // Our Endpoint
ansond 8:f950fb1b78c0 38 Connector::Endpoint *__endpoint = NULL;
ansond 8:f950fb1b78c0 39
ansond 1:16f0fb5b8d97 40 // Our Endpoint configured Options
ansond 1:16f0fb5b8d97 41 Connector::OptionsBuilder config;
ansond 1:16f0fb5b8d97 42 Connector::Options *options = NULL;
ansond 1:16f0fb5b8d97 43
ansond 8:f950fb1b78c0 44 // initialize the Connector::Endpoint instance
ansond 8:f950fb1b78c0 45 void *utils_init_endpoint(bool canActAsRouterNode) {
ansond 8:f950fb1b78c0 46 // alloc Endpoint
ansond 8:f950fb1b78c0 47 logger.log("Endpoint: allocating endpoint instance...");
ansond 8:f950fb1b78c0 48 Connector::Endpoint *ep = new Connector::Endpoint(&logger,options);
ansond 8:f950fb1b78c0 49 if (ep != NULL) {
ansond 27:b8aaf7dc7023 50 // link to config object
ansond 21:0bbe9057e7b1 51 config.setEndpoint((void *)ep);
ansond 27:b8aaf7dc7023 52
ansond 27:b8aaf7dc7023 53 // not sure if we need this anymore...
ansond 8:f950fb1b78c0 54 ep->asRouterNode(canActAsRouterNode);
ansond 27:b8aaf7dc7023 55
ansond 27:b8aaf7dc7023 56 // Add ObjectInstanceManager
ansond 27:b8aaf7dc7023 57 ObjectInstanceManager *oim = new ObjectInstanceManager(&logger,(void *)ep);
ansond 27:b8aaf7dc7023 58 ep->setObjectInstanceManager(oim);
ansond 8:f950fb1b78c0 59 }
ansond 8:f950fb1b78c0 60 return (void *)ep;
ansond 8:f950fb1b78c0 61 }
ansond 1:16f0fb5b8d97 62
ansond 1:16f0fb5b8d97 63 // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint...
ansond 1:16f0fb5b8d97 64 void utils_configure_endpoint(void *p)
ansond 1:16f0fb5b8d97 65 {
ansond 1:16f0fb5b8d97 66 // our Endpoint
ansond 1:16f0fb5b8d97 67 Connector::Endpoint *ep = (Connector::Endpoint *)p;
ansond 1:16f0fb5b8d97 68
ansond 1:16f0fb5b8d97 69 // NSP/NSDL default configuration - see mbedConnectorInterface.h for definitions...
ansond 1:16f0fb5b8d97 70 logger.log("Endpoint: setting defaults...");
ansond 1:16f0fb5b8d97 71 config.setEndpointNodename(NODE_NAME);
ansond 1:16f0fb5b8d97 72 config.setEndpointType(NSP_ENDPOINT_TYPE);
ansond 1:16f0fb5b8d97 73 config.setRegUpdatePeriod(REG_UPDATE_PERIOD_MS);
ansond 1:16f0fb5b8d97 74 config.setLifetime(REG_LIFETIME_SEC);
ansond 0:1f1f55e73248 75
ansond 1:16f0fb5b8d97 76 // WiFi defaults
ansond 1:16f0fb5b8d97 77 config.setWiFiSSID((char *)WIFI_DEFAULT_SSID); // default: changeme
ansond 12:d0e61bac8c27 78 config.setWiFiAuthType(WIFI_NONE); // default: none
ansond 11:3d4434825cd7 79 config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY); // default: changeme
ansond 11:3d4434825cd7 80
ansond 11:3d4434825cd7 81 // Default CoAP Connection Type
ansond 11:3d4434825cd7 82 config.setCoAPConnectionType(COAP_UDP); // default CoAP Connection Type
ansond 11:3d4434825cd7 83
ansond 11:3d4434825cd7 84 // Set the default IP Address Type
ansond 11:3d4434825cd7 85 config.setIPAddressType(IP_ADDRESS_TYPE_IPV4); // IPv4 is the default
ansond 8:f950fb1b78c0 86
ansond 8:f950fb1b78c0 87 // 802.15.4 defaults
ansond 8:f950fb1b78c0 88 unsigned char psk[16];
ansond 8:f950fb1b78c0 89 unsigned char psk_identity[2];
ansond 8:f950fb1b78c0 90 memset(psk,0,16);
ansond 8:f950fb1b78c0 91 memset(psk_identity,0,2);
ansond 8:f950fb1b78c0 92 config.setPreSharedKey(psk);
ansond 8:f950fb1b78c0 93 config.setPreSharedKeyIdentity(psk_identity);
ansond 1:16f0fb5b8d97 94
ansond 1:16f0fb5b8d97 95 // Establish default CoAP observation behavior
ansond 1:16f0fb5b8d97 96 config.setImmedateObservationEnabled(true);
ansond 1:16f0fb5b8d97 97
ansond 1:16f0fb5b8d97 98 // Establish default CoAP GET-based observation control behavior
ansond 1:16f0fb5b8d97 99 config.setEnableGETObservationControl(false);
ansond 1:16f0fb5b8d97 100
ansond 13:9edad7677211 101 // Device Manager installation
ansond 13:9edad7677211 102 DeviceManager *device_manager = (DeviceManager *)ep->getDeviceManager();
ansond 13:9edad7677211 103 if (device_manager != NULL) {
ansond 13:9edad7677211 104 logger.log("Endpoint: installing and setting up device manager and its resources...");
ansond 13:9edad7677211 105 device_manager->install((void *)ep,(void *)&config);
ansond 13:9edad7677211 106 }
ansond 13:9edad7677211 107 else {
ansond 13:9edad7677211 108 logger.log("Endpoint: no device manager installed...");
ansond 13:9edad7677211 109 }
ansond 13:9edad7677211 110
ansond 1:16f0fb5b8d97 111 // main.cpp can override or change any of the above defaults...
ansond 1:16f0fb5b8d97 112 logger.log("Endpoint: gathering configuration overrides...");
ansond 1:16f0fb5b8d97 113 options = configure_endpoint(config);
ansond 21:0bbe9057e7b1 114
ansond 21:0bbe9057e7b1 115 // set our options
ansond 8:f950fb1b78c0 116 ep->setOptions(options);
ansond 8:f950fb1b78c0 117
ansond 1:16f0fb5b8d97 118 // DONE
ansond 1:16f0fb5b8d97 119 logger.log("Endpoint: endpoint configuration completed.");
ansond 1:16f0fb5b8d97 120 }
ansond 1:16f0fb5b8d97 121
ansond 8:f950fb1b78c0 122 // build out the endpoint and its resources
ansond 8:f950fb1b78c0 123 void utils_build_endpoint(void *p)
ansond 1:16f0fb5b8d97 124 {
ansond 1:16f0fb5b8d97 125 if (p != NULL) {
ansond 8:f950fb1b78c0 126 // Build the Endpoint
ansond 8:f950fb1b78c0 127 logger.log("Endpoint: building endpoint and its resources...");
ansond 1:16f0fb5b8d97 128 Connector::Endpoint *ep = (Connector::Endpoint *)p;
ansond 8:f950fb1b78c0 129 ep->build_endpoint();
ansond 1:16f0fb5b8d97 130 }
ansond 10:3f79b5e67c22 131 }
ansond 10:3f79b5e67c22 132
ansond 10:3f79b5e67c22 133 // parse out the CoAP port number from the connection URL
ansond 10:3f79b5e67c22 134 uint16_t extract_port_from_url(char *url,uint16_t default_port)
ansond 10:3f79b5e67c22 135 {
ansond 10:3f79b5e67c22 136 uint16_t port = default_port;
ansond 10:3f79b5e67c22 137
ansond 10:3f79b5e67c22 138 if (url != NULL && strlen(url) > 0) {
ansond 10:3f79b5e67c22 139 char buffer[MAX_CONN_URL_LENGTH+1];
ansond 10:3f79b5e67c22 140 char uri[MAX_CONN_URL_LENGTH+1];
ansond 10:3f79b5e67c22 141 char path[MAX_CONN_URL_LENGTH+1];
ansond 10:3f79b5e67c22 142
ansond 10:3f79b5e67c22 143 // initialize the buffer
ansond 10:3f79b5e67c22 144 memset(buffer,0,MAX_CONN_URL_LENGTH+1);
ansond 10:3f79b5e67c22 145 memset(uri,0,MAX_CONN_URL_LENGTH+1);
ansond 10:3f79b5e67c22 146 memset(path,0,MAX_CONN_URL_LENGTH+1);
ansond 10:3f79b5e67c22 147 int length = strlen(url);
ansond 10:3f79b5e67c22 148
ansond 10:3f79b5e67c22 149 // truncate if needed
ansond 10:3f79b5e67c22 150 if (length >MAX_CONN_URL_LENGTH) length = MAX_CONN_URL_LENGTH;
ansond 10:3f79b5e67c22 151
ansond 10:3f79b5e67c22 152 // make a copy...
ansond 10:3f79b5e67c22 153 memcpy(buffer,url,length);
ansond 10:3f79b5e67c22 154
ansond 10:3f79b5e67c22 155 // remove the forward slashes and colons
ansond 10:3f79b5e67c22 156 for(int i=0;i<length;++i) {
ansond 10:3f79b5e67c22 157 if (buffer[i] == ':') buffer[i] = ' ';
ansond 10:3f79b5e67c22 158 if (buffer[i] == '/') buffer[i] = ' ';
ansond 10:3f79b5e67c22 159 }
ansond 10:3f79b5e67c22 160
ansond 10:3f79b5e67c22 161 // parse
ansond 10:3f79b5e67c22 162 sscanf(buffer,"%s %s %d",uri,path,(int *)&port);
ansond 10:3f79b5e67c22 163 }
ansond 10:3f79b5e67c22 164
ansond 10:3f79b5e67c22 165 // DEBUG
ansond 10:3f79b5e67c22 166 logger.log("Endpoint: CoAP port: %u",port);
ansond 10:3f79b5e67c22 167
ansond 10:3f79b5e67c22 168 // return the port
ansond 10:3f79b5e67c22 169 return port;
ansond 8:f950fb1b78c0 170 }