use TCP to connect to mbed connector
Fork of mbedConnectorInterfaceWithDM by
source/Utils.cpp@10:3f79b5e67c22, 2016-03-04 (annotated)
- Committer:
- ansond
- Date:
- Fri Mar 04 19:16:57 2016 +0000
- Revision:
- 10:3f79b5e67c22
- Parent:
- 8:f950fb1b78c0
- Child:
- 11:3d4434825cd7
updated to allow for specification of UDP or TCP under CoAP and IPv4 or IPv6 addressing types
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 | 1:16f0fb5b8d97 | 27 | |
ansond | 10:3f79b5e67c22 | 28 | // Maximum CoAP URL length |
ansond | 10:3f79b5e67c22 | 29 | #define MAX_CONN_URL_LENGTH 128 |
ansond | 10:3f79b5e67c22 | 30 | |
ansond | 1:16f0fb5b8d97 | 31 | // External references (defined in main.cpp) |
ansond | 1:16f0fb5b8d97 | 32 | Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder); |
ansond | 1:16f0fb5b8d97 | 33 | extern Logger logger; |
ansond | 1:16f0fb5b8d97 | 34 | |
ansond | 8:f950fb1b78c0 | 35 | // Our Endpoint |
ansond | 8:f950fb1b78c0 | 36 | Connector::Endpoint *__endpoint = NULL; |
ansond | 8:f950fb1b78c0 | 37 | |
ansond | 1:16f0fb5b8d97 | 38 | // Our Endpoint configured Options |
ansond | 1:16f0fb5b8d97 | 39 | Connector::OptionsBuilder config; |
ansond | 1:16f0fb5b8d97 | 40 | Connector::Options *options = NULL; |
ansond | 1:16f0fb5b8d97 | 41 | |
ansond | 8:f950fb1b78c0 | 42 | // initialize the Connector::Endpoint instance |
ansond | 8:f950fb1b78c0 | 43 | void *utils_init_endpoint(bool canActAsRouterNode) { |
ansond | 8:f950fb1b78c0 | 44 | // alloc Endpoint |
ansond | 8:f950fb1b78c0 | 45 | logger.log("Endpoint: allocating endpoint instance..."); |
ansond | 8:f950fb1b78c0 | 46 | Connector::Endpoint *ep = new Connector::Endpoint(&logger,options); |
ansond | 8:f950fb1b78c0 | 47 | if (ep != NULL) { |
ansond | 8:f950fb1b78c0 | 48 | ep->asRouterNode(canActAsRouterNode); |
ansond | 8:f950fb1b78c0 | 49 | } |
ansond | 8:f950fb1b78c0 | 50 | return (void *)ep; |
ansond | 8:f950fb1b78c0 | 51 | } |
ansond | 1:16f0fb5b8d97 | 52 | |
ansond | 1:16f0fb5b8d97 | 53 | // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint... |
ansond | 1:16f0fb5b8d97 | 54 | void utils_configure_endpoint(void *p) |
ansond | 1:16f0fb5b8d97 | 55 | { |
ansond | 1:16f0fb5b8d97 | 56 | // our Endpoint |
ansond | 1:16f0fb5b8d97 | 57 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 1:16f0fb5b8d97 | 58 | |
ansond | 1:16f0fb5b8d97 | 59 | // NSP/NSDL default configuration - see mbedConnectorInterface.h for definitions... |
ansond | 1:16f0fb5b8d97 | 60 | logger.log("Endpoint: setting defaults..."); |
ansond | 1:16f0fb5b8d97 | 61 | config.setEndpointNodename(NODE_NAME); |
ansond | 1:16f0fb5b8d97 | 62 | config.setEndpointType(NSP_ENDPOINT_TYPE); |
ansond | 1:16f0fb5b8d97 | 63 | config.setRegUpdatePeriod(REG_UPDATE_PERIOD_MS); |
ansond | 1:16f0fb5b8d97 | 64 | config.setLifetime(REG_LIFETIME_SEC); |
ansond | 0:1f1f55e73248 | 65 | |
ansond | 1:16f0fb5b8d97 | 66 | // WiFi defaults |
ansond | 1:16f0fb5b8d97 | 67 | config.setWiFiSSID((char *)WIFI_DEFAULT_SSID); // default: changeme |
ansond | 1:16f0fb5b8d97 | 68 | config.setWiFiAuthType(WIFI_WPA_PERSONAL); // default: WPA Personal |
ansond | 1:16f0fb5b8d97 | 69 | config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY); // default: changeme |
ansond | 8:f950fb1b78c0 | 70 | |
ansond | 8:f950fb1b78c0 | 71 | // 802.15.4 defaults |
ansond | 8:f950fb1b78c0 | 72 | unsigned char psk[16]; |
ansond | 8:f950fb1b78c0 | 73 | unsigned char psk_identity[2]; |
ansond | 8:f950fb1b78c0 | 74 | memset(psk,0,16); |
ansond | 8:f950fb1b78c0 | 75 | memset(psk_identity,0,2); |
ansond | 8:f950fb1b78c0 | 76 | config.setPreSharedKey(psk); |
ansond | 8:f950fb1b78c0 | 77 | config.setPreSharedKeyIdentity(psk_identity); |
ansond | 1:16f0fb5b8d97 | 78 | |
ansond | 1:16f0fb5b8d97 | 79 | // Establish default CoAP observation behavior |
ansond | 1:16f0fb5b8d97 | 80 | config.setImmedateObservationEnabled(true); |
ansond | 1:16f0fb5b8d97 | 81 | |
ansond | 1:16f0fb5b8d97 | 82 | // Establish default CoAP GET-based observation control behavior |
ansond | 1:16f0fb5b8d97 | 83 | config.setEnableGETObservationControl(false); |
ansond | 1:16f0fb5b8d97 | 84 | |
ansond | 1:16f0fb5b8d97 | 85 | // main.cpp can override or change any of the above defaults... |
ansond | 1:16f0fb5b8d97 | 86 | logger.log("Endpoint: gathering configuration overrides..."); |
ansond | 1:16f0fb5b8d97 | 87 | options = configure_endpoint(config); |
ansond | 8:f950fb1b78c0 | 88 | ep->setOptions(options); |
ansond | 8:f950fb1b78c0 | 89 | |
ansond | 1:16f0fb5b8d97 | 90 | // DONE |
ansond | 1:16f0fb5b8d97 | 91 | logger.log("Endpoint: endpoint configuration completed."); |
ansond | 1:16f0fb5b8d97 | 92 | } |
ansond | 1:16f0fb5b8d97 | 93 | |
ansond | 8:f950fb1b78c0 | 94 | // build out the endpoint and its resources |
ansond | 8:f950fb1b78c0 | 95 | void utils_build_endpoint(void *p) |
ansond | 1:16f0fb5b8d97 | 96 | { |
ansond | 1:16f0fb5b8d97 | 97 | if (p != NULL) { |
ansond | 8:f950fb1b78c0 | 98 | // Build the Endpoint |
ansond | 8:f950fb1b78c0 | 99 | logger.log("Endpoint: building endpoint and its resources..."); |
ansond | 1:16f0fb5b8d97 | 100 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 8:f950fb1b78c0 | 101 | ep->build_endpoint(); |
ansond | 1:16f0fb5b8d97 | 102 | } |
ansond | 10:3f79b5e67c22 | 103 | } |
ansond | 10:3f79b5e67c22 | 104 | |
ansond | 10:3f79b5e67c22 | 105 | // parse out the CoAP port number from the connection URL |
ansond | 10:3f79b5e67c22 | 106 | uint16_t extract_port_from_url(char *url,uint16_t default_port) |
ansond | 10:3f79b5e67c22 | 107 | { |
ansond | 10:3f79b5e67c22 | 108 | uint16_t port = default_port; |
ansond | 10:3f79b5e67c22 | 109 | |
ansond | 10:3f79b5e67c22 | 110 | if (url != NULL && strlen(url) > 0) { |
ansond | 10:3f79b5e67c22 | 111 | char buffer[MAX_CONN_URL_LENGTH+1]; |
ansond | 10:3f79b5e67c22 | 112 | char uri[MAX_CONN_URL_LENGTH+1]; |
ansond | 10:3f79b5e67c22 | 113 | char path[MAX_CONN_URL_LENGTH+1]; |
ansond | 10:3f79b5e67c22 | 114 | |
ansond | 10:3f79b5e67c22 | 115 | // initialize the buffer |
ansond | 10:3f79b5e67c22 | 116 | memset(buffer,0,MAX_CONN_URL_LENGTH+1); |
ansond | 10:3f79b5e67c22 | 117 | memset(uri,0,MAX_CONN_URL_LENGTH+1); |
ansond | 10:3f79b5e67c22 | 118 | memset(path,0,MAX_CONN_URL_LENGTH+1); |
ansond | 10:3f79b5e67c22 | 119 | int length = strlen(url); |
ansond | 10:3f79b5e67c22 | 120 | |
ansond | 10:3f79b5e67c22 | 121 | // truncate if needed |
ansond | 10:3f79b5e67c22 | 122 | if (length >MAX_CONN_URL_LENGTH) length = MAX_CONN_URL_LENGTH; |
ansond | 10:3f79b5e67c22 | 123 | |
ansond | 10:3f79b5e67c22 | 124 | // make a copy... |
ansond | 10:3f79b5e67c22 | 125 | memcpy(buffer,url,length); |
ansond | 10:3f79b5e67c22 | 126 | |
ansond | 10:3f79b5e67c22 | 127 | // remove the forward slashes and colons |
ansond | 10:3f79b5e67c22 | 128 | for(int i=0;i<length;++i) { |
ansond | 10:3f79b5e67c22 | 129 | if (buffer[i] == ':') buffer[i] = ' '; |
ansond | 10:3f79b5e67c22 | 130 | if (buffer[i] == '/') buffer[i] = ' '; |
ansond | 10:3f79b5e67c22 | 131 | } |
ansond | 10:3f79b5e67c22 | 132 | |
ansond | 10:3f79b5e67c22 | 133 | // parse |
ansond | 10:3f79b5e67c22 | 134 | sscanf(buffer,"%s %s %d",uri,path,(int *)&port); |
ansond | 10:3f79b5e67c22 | 135 | } |
ansond | 10:3f79b5e67c22 | 136 | |
ansond | 10:3f79b5e67c22 | 137 | // DEBUG |
ansond | 10:3f79b5e67c22 | 138 | logger.log("Endpoint: CoAP port: %u",port); |
ansond | 10:3f79b5e67c22 | 139 | |
ansond | 10:3f79b5e67c22 | 140 | // return the port |
ansond | 10:3f79b5e67c22 | 141 | return port; |
ansond | 8:f950fb1b78c0 | 142 | } |