mbed Connector Endpoint interface. This interface permits a mbed endpoint to easily setup MDS resources and emit those resources to an MDS server.

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Committer:
ansond
Date:
Sun Sep 06 03:16:02 2015 +0000
Revision:
61:143beb6d8800
Parent:
49:8ac08e5deefb
fixes to observation configuration/toggle switch issues.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 1:cabdd0350707 1 /**
ansond 1:cabdd0350707 2 * @file Utils.cpp
ansond 1:cabdd0350707 3 * @brief mbed CoAP Endpoint misc utils collection
ansond 1:cabdd0350707 4 * @author Doug Anson
ansond 1:cabdd0350707 5 * @version 1.0
sam_grove 2:853f9ecc12df 6 * @see
ansond 1:cabdd0350707 7 *
ansond 1:cabdd0350707 8 * Copyright (c) 2014
ansond 1:cabdd0350707 9 *
ansond 1:cabdd0350707 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 1:cabdd0350707 11 * you may not use this file except in compliance with the License.
ansond 1:cabdd0350707 12 * You may obtain a copy of the License at
ansond 1:cabdd0350707 13 *
ansond 1:cabdd0350707 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 1:cabdd0350707 15 *
ansond 1:cabdd0350707 16 * Unless required by applicable law or agreed to in writing, software
ansond 1:cabdd0350707 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 1:cabdd0350707 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 1:cabdd0350707 19 * See the License for the specific language governing permissions and
ansond 1:cabdd0350707 20 * limitations under the License.
ansond 1:cabdd0350707 21 */
sam_grove 2:853f9ecc12df 22
ansond 1:cabdd0350707 23 // mbed Endpoint includes
ansond 8:b518d1c01df1 24 #include "ConnectorEndpoint.h"
ansond 1:cabdd0350707 25 #include "OptionsBuilder.h"
ansond 1:cabdd0350707 26
ansond 1:cabdd0350707 27 // External references (defined in main.cpp)
ansond 1:cabdd0350707 28 Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder);
ansond 1:cabdd0350707 29 extern Logger logger;
ansond 1:cabdd0350707 30
ansond 16:383ad1356963 31 // Our Endpoint
ansond 16:383ad1356963 32 Connector::Endpoint *endpoint = NULL;
ansond 16:383ad1356963 33
ansond 16:383ad1356963 34 // Our Endpoint configured Options
ansond 16:383ad1356963 35 Connector::OptionsBuilder config;
ansond 16:383ad1356963 36 Connector::Options *options = NULL;
ansond 16:383ad1356963 37
ansond 3:11b2f4e58378 38 // ************************* NSDL Linkage - MDS CONFIGURATION (defaulted) *********************************
ansond 3:11b2f4e58378 39
ansond 4:84159d67d32d 40 uint8_t NSP_address_bytes[NSP_IP_ADDRESS_LENGTH] = NSP_IP_ADDRESS; // which MDS instance we want to bind to...
ansond 4:84159d67d32d 41 uint8_t endpoint_name[NODE_NAME_LENGTH] = NODE_NAME; // our NODE name
ansond 6:516a88842a2b 42 uint8_t domain_name[NSP_DOMAIN_LENGTH] = NSP_DOMAIN; // our MDS domain name
ansond 11:c1b2b4b6c341 43 int nsp_port = NSP_COAP_UDP_PORT; // our MDS UDP port
ansond 4:84159d67d32d 44 uint8_t ep_type[NSP_ENDPOINT_TYPE_LENGTH] = NSP_ENDPOINT_TYPE; // our NODE type
ansond 4:84159d67d32d 45 uint8_t lifetime_ptr[NSP_LIFE_TIME_LENGTH] = { NSP_LIFE_TIME }; // NSDL lifetime
ansond 4:84159d67d32d 46 uint8_t app_MAC_address[NODE_MAC_ADDRESS_LENGTH] = NODE_MAC_ADDRESS; // Node MAC address
ansond 4:84159d67d32d 47 uint32_t channel_list = NODE_CHANNEL_LIST; // Node RF Channel list
ansond 3:11b2f4e58378 48
ansond 16:383ad1356963 49 uint8_t wifi_ssid[WIFI_SSID_LENGTH]; // WiFi SSID
ansond 16:383ad1356963 50 WiFiAuthTypes wifi_auth_type; // WiFi Auth Type
ansond 16:383ad1356963 51 uint8_t wifi_auth_key[WIFI_AUTH_KEY_LENGTH]; // WiFi Auth Key
ansond 16:383ad1356963 52
ansond 19:e2cbaeeea509 53 uint8_t mesh_network_id[MESH_NETWORK_ID_LENGTH] = MESH_DEF_NETWORK_ID; // 802.15.4 Network ID (6LowPAN)
ansond 19:e2cbaeeea509 54 uint8_t rf_channel = MESH_DEF_RF_CHANNEL; // 802.15.4 RF Channel (6LowPAN)
ansond 19:e2cbaeeea509 55
ansond 3:11b2f4e58378 56 // ************************* NSDL Linkage - MDS CONFIGURATION (defaulted) *********************************
ansond 3:11b2f4e58378 57
ansond 3:11b2f4e58378 58
ansond 1:cabdd0350707 59 // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint...
ansond 16:383ad1356963 60 void utils_configure_endpoint()
ansond 3:11b2f4e58378 61 {
ansond 3:11b2f4e58378 62 // NSP/NSDL default configuration - see mbedConnectorInterface.h for definitions...
ansond 16:383ad1356963 63 logger.log("utils_configure_endpoint: setting defaults...");
ansond 3:11b2f4e58378 64 config.setNSPAddress(NSP_address_bytes);
ansond 3:11b2f4e58378 65 config.setNSPPortNumber(NSP_COAP_UDP_PORT);
ansond 3:11b2f4e58378 66 config.setDomain(NSP_DOMAIN);
ansond 14:5cfaeee144bc 67 config.setEndpointNodename(NODE_NAME);
ansond 3:11b2f4e58378 68 config.setEndpointType(NSP_ENDPOINT_TYPE);
ansond 3:11b2f4e58378 69 config.setLifetime(NSP_LIFE_TIME);
ansond 3:11b2f4e58378 70
ansond 3:11b2f4e58378 71 // Node default configuration - see mbedConnectorInterface.h for definitions...
ansond 3:11b2f4e58378 72 config.setRadioChannelList(NODE_CHANNEL_LIST);
ansond 3:11b2f4e58378 73 config.setReadUpdatePeriod(NSP_RD_UPDATE_PERIOD);
ansond 3:11b2f4e58378 74 config.setEndpointNodename(NODE_NAME);
ansond 5:a929d65eb385 75 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 3:11b2f4e58378 76
ansond 16:383ad1356963 77 // WiFi defaults
ansond 16:383ad1356963 78 config.setWiFiSSID(WIFI_DEFAULT_SSID); // default: changeme
ansond 16:383ad1356963 79 config.setWiFiAuthType(WIFI_WPA_PERSONAL); // default: WPA Personal
ansond 16:383ad1356963 80 config.setWiFiAuthKey(WIFI_DEFAULT_AUTH_KEY); // default: changeme
ansond 16:383ad1356963 81
ansond 19:e2cbaeeea509 82 // 802.15.4 defaults (6LowPAN)
ansond 19:e2cbaeeea509 83 config.setNetworkID((char *)mesh_network_id);
ansond 19:e2cbaeeea509 84 config.setRadioChannel((int)mesh_network_id);
ansond 19:e2cbaeeea509 85
ansond 48:4b9ee3e32f93 86 // Establish default CoAP observation behavior
ansond 48:4b9ee3e32f93 87 config.setImmedateObservationEnabled(false); // default: false (per CoAP spec)
ansond 48:4b9ee3e32f93 88
ansond 48:4b9ee3e32f93 89 // Establish default CoAP GET-based observation control behavior
ansond 49:8ac08e5deefb 90 config.setEnableGETObservationControl(true); // default: true (CoAP spec)
ansond 48:4b9ee3e32f93 91
ansond 3:11b2f4e58378 92 // main.cpp can override or change any of the above defaults...
ansond 16:383ad1356963 93 logger.log("utils_configure_endpoint: gathering configuration overrides...");
ansond 16:383ad1356963 94 options = configure_endpoint(config);
ansond 4:84159d67d32d 95
ansond 3:11b2f4e58378 96 // with options, lets set the underlying NSDL globals...
ansond 16:383ad1356963 97 logger.log("utils_configure_endpoint: finalizing configuration...");
ansond 4:84159d67d32d 98 memcpy(NSP_address_bytes,options->getNSPAddress(),NSP_IP_ADDRESS_LENGTH);
ansond 29:2f33650ca800 99 memset(endpoint_name,0,NODE_NAME_LENGTH);
ansond 7:09c5d9ae56cb 100 memcpy(endpoint_name,options->getEndpointNodename().c_str(),options->getEndpointNodename().size());
ansond 29:2f33650ca800 101 memset(domain_name,0,NSP_DOMAIN_LENGTH);
ansond 7:09c5d9ae56cb 102 memcpy(domain_name,options->getDomain().c_str(),options->getDomain().size());
ansond 11:c1b2b4b6c341 103 nsp_port = options->getNSPPortNumber();
ansond 29:2f33650ca800 104 memset(ep_type,0,NSP_ENDPOINT_TYPE_LENGTH);
ansond 7:09c5d9ae56cb 105 memcpy(ep_type,options->getEndpointType().c_str(),options->getEndpointType().size());
ansond 4:84159d67d32d 106 memcpy(lifetime_ptr,options->getLifetime(),NSP_LIFE_TIME_LENGTH);
ansond 5:a929d65eb385 107 memcpy(app_MAC_address,options->getMACAddress(),NODE_MAC_ADDRESS_LENGTH); // TODO: arm_ns_tasklet_create() should call Endpoint::plumbNetwork()... currently its called before this MAC address can be (re)set
ansond 3:11b2f4e58378 108 channel_list = options->getRadioChannelList();
ansond 16:383ad1356963 109
ansond 16:383ad1356963 110 // WiFi Configration
ansond 16:383ad1356963 111 memcpy(wifi_ssid,options->getWiFiSSID().c_str(),options->getWiFiSSID().size());
ansond 16:383ad1356963 112 wifi_auth_type = options->getWiFiAuthType();
ansond 16:383ad1356963 113 memcpy(wifi_auth_key,options->getWiFiAuthKey().c_str(),options->getWiFiAuthKey().size());
ansond 19:e2cbaeeea509 114
ansond 19:e2cbaeeea509 115 // 802.15.4 Configuration
ansond 19:e2cbaeeea509 116 memcpy(mesh_network_id,options->getNetworkID().c_str(),options->getNetworkID().size());
ansond 19:e2cbaeeea509 117 rf_channel = options->getRadioChannel();
ansond 16:383ad1356963 118
ansond 16:383ad1356963 119 // DONE
ansond 16:383ad1356963 120 logger.log("utils_configure_endpoint: endpoint configuration completed.");
ansond 16:383ad1356963 121 }
ansond 16:383ad1356963 122
ansond 16:383ad1356963 123 // initialize and register the endpoint and its resources
ansond 16:383ad1356963 124 void utils_init_and_register_endpoint(void)
ansond 16:383ad1356963 125 {
ansond 16:383ad1356963 126 // initialize NSDL
ansond 16:383ad1356963 127 logger.log("net_stubs_post_plumb_network: initializing NSP..\r\n.");
ansond 16:383ad1356963 128 nsdl_init();
ansond 16:383ad1356963 129 nsdl_set_nsp_address();
sam_grove 2:853f9ecc12df 130
ansond 3:11b2f4e58378 131 // alloc Endpoint
ansond 16:383ad1356963 132 logger.log("utils_init_and_register_endpoint: allocating endpoint instance...");
ansond 16:383ad1356963 133 if (endpoint == NULL) endpoint = new Connector::Endpoint(&logger,options);
sam_grove 2:853f9ecc12df 134
ansond 3:11b2f4e58378 135 // initialize Endpoint resources
ansond 16:383ad1356963 136 logger.log("utils_init_and_register_endpoint: registering endpoint and its resources...");
ansond 16:383ad1356963 137 endpoint->register_endpoint();
sam_grove 2:853f9ecc12df 138 }