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@4:d9603064630c, 2016-02-21 (annotated)
- Committer:
- ansond
- Date:
- Sun Feb 21 16:21:47 2016 +0000
- Revision:
- 4:d9603064630c
- Parent:
- 1:16f0fb5b8d97
- Child:
- 6:d4d1105a1777
updated
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 | 1:16f0fb5b8d97 | 26 | |
ansond | 1:16f0fb5b8d97 | 27 | // External references (defined in main.cpp) |
ansond | 1:16f0fb5b8d97 | 28 | Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder); |
ansond | 1:16f0fb5b8d97 | 29 | extern Logger logger; |
ansond | 1:16f0fb5b8d97 | 30 | |
ansond | 1:16f0fb5b8d97 | 31 | // Our Endpoint configured Options |
ansond | 1:16f0fb5b8d97 | 32 | Connector::OptionsBuilder config; |
ansond | 1:16f0fb5b8d97 | 33 | Connector::Options *options = NULL; |
ansond | 1:16f0fb5b8d97 | 34 | |
ansond | 1:16f0fb5b8d97 | 35 | // Shutdown button (K64F only...) |
ansond | 1:16f0fb5b8d97 | 36 | #if defined(TARGET_K64F) |
ansond | 1:16f0fb5b8d97 | 37 | InterruptIn shutdown_button(SW3); |
ansond | 1:16f0fb5b8d97 | 38 | #endif |
ansond | 1:16f0fb5b8d97 | 39 | |
ansond | 1:16f0fb5b8d97 | 40 | // ************************* NSDL Linkage - MDS CONFIGURATION (defaulted) ********************************* |
ansond | 1:16f0fb5b8d97 | 41 | |
ansond | 1:16f0fb5b8d97 | 42 | uint8_t app_MAC_address[NODE_MAC_ADDRESS_LENGTH] = NODE_MAC_ADDRESS; // Node MAC address |
ansond | 1:16f0fb5b8d97 | 43 | uint32_t channel_list = NODE_CHANNEL_LIST; // Node RF Channel list |
ansond | 1:16f0fb5b8d97 | 44 | |
ansond | 1:16f0fb5b8d97 | 45 | uint8_t mesh_network_id[MESH_NETWORK_ID_LENGTH] = MESH_DEF_NETWORK_ID; // 802.15.4 Network ID (6LowPAN) |
ansond | 1:16f0fb5b8d97 | 46 | uint8_t rf_channel = MESH_DEF_RF_CHANNEL; // 802.15.4 RF Channel (6LowPAN) |
ansond | 1:16f0fb5b8d97 | 47 | |
ansond | 1:16f0fb5b8d97 | 48 | // ************************* NSDL Linkage - MDS CONFIGURATION (defaulted) ********************************* |
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 | 1:16f0fb5b8d97 | 67 | |
ansond | 1:16f0fb5b8d97 | 68 | // 802.15.4 defaults (6LowPAN) |
ansond | 1:16f0fb5b8d97 | 69 | config.setNetworkID((char *)mesh_network_id); |
ansond | 1:16f0fb5b8d97 | 70 | config.setRadioChannel((int)mesh_network_id); |
ansond | 1:16f0fb5b8d97 | 71 | config.setRadioChannelList(NODE_CHANNEL_LIST); |
ansond | 1:16f0fb5b8d97 | 72 | config.setMACAddress(app_MAC_address); // TODO: arm_ns_tasklet_create() should call Endpoint::plumbNetwork()... currently its called before this MAC address can be (re)set |
ansond | 1:16f0fb5b8d97 | 73 | |
ansond | 1:16f0fb5b8d97 | 74 | // Establish default CoAP observation behavior |
ansond | 1:16f0fb5b8d97 | 75 | config.setImmedateObservationEnabled(true); |
ansond | 1:16f0fb5b8d97 | 76 | |
ansond | 1:16f0fb5b8d97 | 77 | // Establish default CoAP GET-based observation control behavior |
ansond | 1:16f0fb5b8d97 | 78 | config.setEnableGETObservationControl(false); |
ansond | 1:16f0fb5b8d97 | 79 | |
ansond | 1:16f0fb5b8d97 | 80 | // main.cpp can override or change any of the above defaults... |
ansond | 1:16f0fb5b8d97 | 81 | logger.log("Endpoint: gathering configuration overrides..."); |
ansond | 1:16f0fb5b8d97 | 82 | options = configure_endpoint(config); |
ansond | 1:16f0fb5b8d97 | 83 | ep->setOptions(options); |
ansond | 1:16f0fb5b8d97 | 84 | |
ansond | 1:16f0fb5b8d97 | 85 | // DONE |
ansond | 1:16f0fb5b8d97 | 86 | logger.log("Endpoint: endpoint configuration completed."); |
ansond | 1:16f0fb5b8d97 | 87 | } |
ansond | 1:16f0fb5b8d97 | 88 | |
ansond | 1:16f0fb5b8d97 | 89 | // setup shutdown button (K64F only...) |
ansond | 1:16f0fb5b8d97 | 90 | #if defined(TARGET_K64F) |
ansond | 1:16f0fb5b8d97 | 91 | void utils_setup_deregistration_button(void *p) { |
ansond | 1:16f0fb5b8d97 | 92 | if (p != NULL) { |
ansond | 1:16f0fb5b8d97 | 93 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 1:16f0fb5b8d97 | 94 | logger.log("Endpoint: setting up shutdown button (SW2) for K64F..."); |
ansond | 1:16f0fb5b8d97 | 95 | shutdown_button.rise(ep,&Connector::Endpoint::closedown_endpoint); |
ansond | 1:16f0fb5b8d97 | 96 | } |
ansond | 1:16f0fb5b8d97 | 97 | } |
ansond | 1:16f0fb5b8d97 | 98 | #endif |
ansond | 1:16f0fb5b8d97 | 99 | |
ansond | 1:16f0fb5b8d97 | 100 | // initialize the Connector::Endpoint instance |
ansond | 1:16f0fb5b8d97 | 101 | void *utils_init_endpoint(bool canActAsRouterNode) { |
ansond | 1:16f0fb5b8d97 | 102 | // alloc Endpoint |
ansond | 1:16f0fb5b8d97 | 103 | logger.log("Endpoint: allocating endpoint instance..."); |
ansond | 1:16f0fb5b8d97 | 104 | Connector::Endpoint *ep = new Connector::Endpoint(&logger,options); |
ansond | 1:16f0fb5b8d97 | 105 | if (ep != NULL) { |
ansond | 1:16f0fb5b8d97 | 106 | ep->asRouterNode(canActAsRouterNode); |
ansond | 1:16f0fb5b8d97 | 107 | } |
ansond | 1:16f0fb5b8d97 | 108 | return (void *)ep; |
ansond | 1:16f0fb5b8d97 | 109 | } |
ansond | 1:16f0fb5b8d97 | 110 | |
ansond | 1:16f0fb5b8d97 | 111 | // register the endpoint and its resources |
ansond | 1:16f0fb5b8d97 | 112 | void utils_register_endpoint(void *p) |
ansond | 1:16f0fb5b8d97 | 113 | { |
ansond | 1:16f0fb5b8d97 | 114 | if (p != NULL) { |
ansond | 1:16f0fb5b8d97 | 115 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
ansond | 1:16f0fb5b8d97 | 116 | // initialize Endpoint resources |
ansond | 1:16f0fb5b8d97 | 117 | logger.log("Endpoint: registering endpoint and its resources..."); |
ansond | 1:16f0fb5b8d97 | 118 | ep->register_endpoint(); |
ansond | 1:16f0fb5b8d97 | 119 | |
ansond | 4:d9603064630c | 120 | // complete the registration |
ansond | 4:d9603064630c | 121 | logger.log("Completing Endpoint Registration"); |
ansond | 4:d9603064630c | 122 | ep->complete_endpoint_registration(ep->getServer(),ep->getObjectList()); |
ansond | 4:d9603064630c | 123 | |
ansond | 1:16f0fb5b8d97 | 124 | // setup the shutdown button (K64F only...) |
ansond | 1:16f0fb5b8d97 | 125 | #if defined(TARGET_K64F) |
ansond | 1:16f0fb5b8d97 | 126 | utils_setup_deregistration_button(p); |
ansond | 1:16f0fb5b8d97 | 127 | #endif |
ansond | 1:16f0fb5b8d97 | 128 | } |
ansond | 1:16f0fb5b8d97 | 129 | } |
ansond | 1:16f0fb5b8d97 | 130 |