Doug Anson / mbedConnectorInterfaceV3
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Utils.cpp Source File

Utils.cpp

Go to the documentation of this file.
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 
00029 // Maximum CoAP URL length
00030 #define MAX_CONN_URL_LENGTH     128
00031 
00032 // External references (defined in main.cpp)
00033 Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder);
00034 extern Logger logger;
00035 
00036 // Our Endpoint
00037 Connector::Endpoint *__endpoint = NULL;
00038 
00039 // Our Endpoint configured Options
00040 Connector::OptionsBuilder config;
00041 Connector::Options *options = NULL;
00042 
00043 // initialize the Connector::Endpoint instance
00044 void *utils_init_endpoint(bool canActAsRouterNode) {
00045     // alloc Endpoint
00046     logger.log("Endpoint: allocating endpoint instance...");
00047     Connector::Endpoint *ep = new Connector::Endpoint(&logger,options);
00048     if (ep != NULL) {
00049         config.setEndpoint((void *)ep);
00050         ep->asRouterNode(canActAsRouterNode);
00051     }
00052     return (void *)ep;
00053 }
00054 
00055 // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint...
00056 void utils_configure_endpoint(void *p)
00057 {   
00058     // our Endpoint 
00059     Connector::Endpoint *ep = (Connector::Endpoint *)p;
00060     
00061     // NSP/NSDL default configuration - see mbedConnectorInterface.h for definitions... 
00062     logger.log("Endpoint: setting defaults...");
00063     config.setEndpointNodename(NODE_NAME);
00064     config.setEndpointType(NSP_ENDPOINT_TYPE);
00065     config.setRegUpdatePeriod(REG_UPDATE_PERIOD_MS);
00066     config.setLifetime(REG_LIFETIME_SEC);
00067 
00068     // WiFi defaults
00069     config.setWiFiSSID((char *)WIFI_DEFAULT_SSID);                      // default: changeme
00070     config.setWiFiAuthType(WIFI_NONE);                                  // default: none
00071     config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY);               // default: changeme
00072     
00073     // Default CoAP Connection Type
00074     config.setCoAPConnectionType(COAP_UDP);                             // default CoAP Connection Type
00075     
00076     // Set the default IP Address Type
00077     config.setIPAddressType(IP_ADDRESS_TYPE_IPV4);                      // IPv4 is the default
00078     
00079     // 802.15.4 defaults
00080     unsigned char psk[16];
00081     unsigned char psk_identity[2];
00082     memset(psk,0,16);
00083     memset(psk_identity,0,2);
00084     config.setPreSharedKey(psk);
00085     config.setPreSharedKeyIdentity(psk_identity);
00086                  
00087     // Establish default CoAP observation behavior
00088     config.setImmedateObservationEnabled(true);    
00089 
00090     // Establish default CoAP GET-based observation control behavior
00091     config.setEnableGETObservationControl(false);    
00092 
00093     // Device Manager installation
00094     DeviceManager *device_manager = (DeviceManager *)ep->getDeviceManager();
00095     if (device_manager != NULL) {
00096         logger.log("Endpoint: installing and setting up device manager and its resources...");
00097         device_manager->install((void *)ep,(void *)&config);
00098     }
00099     else {
00100         logger.log("Endpoint: no device manager installed...");
00101     }
00102     
00103     // main.cpp can override or change any of the above defaults...
00104     logger.log("Endpoint: gathering configuration overrides...");
00105     options = configure_endpoint(config);
00106     
00107     // set our options
00108     ep->setOptions(options);
00109     
00110     // DONE
00111     logger.log("Endpoint: endpoint configuration completed.");
00112 }
00113 
00114 // build out  the endpoint and its resources
00115 void utils_build_endpoint(void *p)
00116 {
00117     if (p != NULL) {
00118         // Build the Endpoint
00119         logger.log("Endpoint: building endpoint and its resources...");
00120         Connector::Endpoint *ep = (Connector::Endpoint *)p;
00121         ep->build_endpoint();
00122     }
00123 }
00124 
00125 // parse out the CoAP port number from the connection URL
00126 uint16_t extract_port_from_url(char *url,uint16_t default_port) 
00127 {
00128     uint16_t port = default_port;
00129     
00130     if (url != NULL && strlen(url) > 0) {
00131         char buffer[MAX_CONN_URL_LENGTH+1];
00132         char uri[MAX_CONN_URL_LENGTH+1];
00133         char path[MAX_CONN_URL_LENGTH+1];
00134         
00135         // initialize the buffer
00136         memset(buffer,0,MAX_CONN_URL_LENGTH+1);
00137         memset(uri,0,MAX_CONN_URL_LENGTH+1);
00138         memset(path,0,MAX_CONN_URL_LENGTH+1);
00139         int length = strlen(url); 
00140         
00141         // truncate if needed
00142         if (length >MAX_CONN_URL_LENGTH) length = MAX_CONN_URL_LENGTH;
00143         
00144         // make a copy...
00145         memcpy(buffer,url,length);
00146         
00147         // remove the forward slashes and colons
00148         for(int i=0;i<length;++i) {
00149             if (buffer[i] == ':') buffer[i] = ' ';
00150             if (buffer[i] == '/') buffer[i] = ' ';
00151         }
00152         
00153         // parse
00154         sscanf(buffer,"%s %s %d",uri,path,(int *)&port);
00155     }
00156     
00157     // DEBUG
00158     logger.log("Endpoint: CoAP port: %u",port);
00159     
00160     // return the port
00161     return port; 
00162 }