mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
NOTE:
This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!
source/Utils.cpp@8:f950fb1b78c0, 2016-02-24 (annotated)
- Committer:
- ansond
- Date:
- Wed Feb 24 05:52:00 2016 +0000
- Revision:
- 8:f950fb1b78c0
- Parent:
- 7:e94f0bd92545
- Child:
- 10:3f79b5e67c22
massive update and re-org
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 | 1:16f0fb5b8d97 | 28 | // External references (defined in main.cpp) |
ansond | 1:16f0fb5b8d97 | 29 | Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder); |
ansond | 1:16f0fb5b8d97 | 30 | extern Logger logger; |
ansond | 1:16f0fb5b8d97 | 31 | |
ansond | 8:f950fb1b78c0 | 32 | // Our Endpoint |
ansond | 8:f950fb1b78c0 | 33 | Connector::Endpoint *__endpoint = NULL; |
ansond | 8:f950fb1b78c0 | 34 | |
ansond | 1:16f0fb5b8d97 | 35 | // Our Endpoint configured Options |
ansond | 1:16f0fb5b8d97 | 36 | Connector::OptionsBuilder config; |
ansond | 1:16f0fb5b8d97 | 37 | Connector::Options *options = NULL; |
ansond | 1:16f0fb5b8d97 | 38 | |
ansond | 8:f950fb1b78c0 | 39 | // initialize the Connector::Endpoint instance |
ansond | 8:f950fb1b78c0 | 40 | void *utils_init_endpoint(bool canActAsRouterNode) { |
ansond | 8:f950fb1b78c0 | 41 | // alloc Endpoint |
ansond | 8:f950fb1b78c0 | 42 | logger.log("Endpoint: allocating endpoint instance..."); |
ansond | 8:f950fb1b78c0 | 43 | Connector::Endpoint *ep = new Connector::Endpoint(&logger,options); |
ansond | 8:f950fb1b78c0 | 44 | if (ep != NULL) { |
ansond | 8:f950fb1b78c0 | 45 | ep->asRouterNode(canActAsRouterNode); |
ansond | 8:f950fb1b78c0 | 46 | } |
ansond | 8:f950fb1b78c0 | 47 | return (void *)ep; |
ansond | 8:f950fb1b78c0 | 48 | } |
ansond | 1:16f0fb5b8d97 | 49 | |
ansond | 1:16f0fb5b8d97 | 50 | // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint... |
ansond | 1:16f0fb5b8d97 | 51 | void utils_configure_endpoint(void *p) |
ansond | 1:16f0fb5b8d97 | 52 | { |
ansond | 1:16f0fb5b8d97 | 53 | // our Endpoint |
ansond | 1:16f0fb5b8d97 | 54 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 1:16f0fb5b8d97 | 55 | |
ansond | 1:16f0fb5b8d97 | 56 | // NSP/NSDL default configuration - see mbedConnectorInterface.h for definitions... |
ansond | 1:16f0fb5b8d97 | 57 | logger.log("Endpoint: setting defaults..."); |
ansond | 1:16f0fb5b8d97 | 58 | config.setEndpointNodename(NODE_NAME); |
ansond | 1:16f0fb5b8d97 | 59 | config.setEndpointType(NSP_ENDPOINT_TYPE); |
ansond | 1:16f0fb5b8d97 | 60 | config.setRegUpdatePeriod(REG_UPDATE_PERIOD_MS); |
ansond | 1:16f0fb5b8d97 | 61 | config.setLifetime(REG_LIFETIME_SEC); |
ansond | 0:1f1f55e73248 | 62 | |
ansond | 1:16f0fb5b8d97 | 63 | // WiFi defaults |
ansond | 1:16f0fb5b8d97 | 64 | config.setWiFiSSID((char *)WIFI_DEFAULT_SSID); // default: changeme |
ansond | 1:16f0fb5b8d97 | 65 | config.setWiFiAuthType(WIFI_WPA_PERSONAL); // default: WPA Personal |
ansond | 1:16f0fb5b8d97 | 66 | config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY); // default: changeme |
ansond | 8:f950fb1b78c0 | 67 | |
ansond | 8:f950fb1b78c0 | 68 | // 802.15.4 defaults |
ansond | 8:f950fb1b78c0 | 69 | unsigned char psk[16]; |
ansond | 8:f950fb1b78c0 | 70 | unsigned char psk_identity[2]; |
ansond | 8:f950fb1b78c0 | 71 | memset(psk,0,16); |
ansond | 8:f950fb1b78c0 | 72 | memset(psk_identity,0,2); |
ansond | 8:f950fb1b78c0 | 73 | config.setPreSharedKey(psk); |
ansond | 8:f950fb1b78c0 | 74 | config.setPreSharedKeyIdentity(psk_identity); |
ansond | 1:16f0fb5b8d97 | 75 | |
ansond | 1:16f0fb5b8d97 | 76 | // Establish default CoAP observation behavior |
ansond | 1:16f0fb5b8d97 | 77 | config.setImmedateObservationEnabled(true); |
ansond | 1:16f0fb5b8d97 | 78 | |
ansond | 1:16f0fb5b8d97 | 79 | // Establish default CoAP GET-based observation control behavior |
ansond | 1:16f0fb5b8d97 | 80 | config.setEnableGETObservationControl(false); |
ansond | 1:16f0fb5b8d97 | 81 | |
ansond | 1:16f0fb5b8d97 | 82 | // main.cpp can override or change any of the above defaults... |
ansond | 1:16f0fb5b8d97 | 83 | logger.log("Endpoint: gathering configuration overrides..."); |
ansond | 1:16f0fb5b8d97 | 84 | options = configure_endpoint(config); |
ansond | 8:f950fb1b78c0 | 85 | ep->setOptions(options); |
ansond | 8:f950fb1b78c0 | 86 | |
ansond | 1:16f0fb5b8d97 | 87 | // DONE |
ansond | 1:16f0fb5b8d97 | 88 | logger.log("Endpoint: endpoint configuration completed."); |
ansond | 1:16f0fb5b8d97 | 89 | } |
ansond | 1:16f0fb5b8d97 | 90 | |
ansond | 8:f950fb1b78c0 | 91 | // build out the endpoint and its resources |
ansond | 8:f950fb1b78c0 | 92 | void utils_build_endpoint(void *p) |
ansond | 1:16f0fb5b8d97 | 93 | { |
ansond | 1:16f0fb5b8d97 | 94 | if (p != NULL) { |
ansond | 8:f950fb1b78c0 | 95 | // Build the Endpoint |
ansond | 8:f950fb1b78c0 | 96 | logger.log("Endpoint: building endpoint and its resources..."); |
ansond | 1:16f0fb5b8d97 | 97 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 8:f950fb1b78c0 | 98 | ep->build_endpoint(); |
ansond | 1:16f0fb5b8d97 | 99 | } |
ansond | 8:f950fb1b78c0 | 100 | } |