Jim Flynn
/
aws-iot-device-sdk-mbed-c
Changes to enabled on-line compiler
platform/ezconnect/easy-connect.cpp@0:082731ede69f, 2018-05-30 (annotated)
- Committer:
- JMF
- Date:
- Wed May 30 20:59:51 2018 +0000
- Revision:
- 0:082731ede69f
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:082731ede69f | 1 | |
JMF | 0:082731ede69f | 2 | /* |
JMF | 0:082731ede69f | 3 | * FILE: easy-connect.cpp |
JMF | 0:082731ede69f | 4 | * |
JMF | 0:082731ede69f | 5 | * Copyright (c) 2015 - 2017 ARM Limited. All rights reserved. |
JMF | 0:082731ede69f | 6 | * SPDX-License-Identifier: Apache-2.0 |
JMF | 0:082731ede69f | 7 | * Licensed under the Apache License, Version 2.0 (the License); you may |
JMF | 0:082731ede69f | 8 | * not use this file except in compliance with the License. |
JMF | 0:082731ede69f | 9 | * You may obtain a copy of the License at |
JMF | 0:082731ede69f | 10 | * |
JMF | 0:082731ede69f | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:082731ede69f | 12 | * |
JMF | 0:082731ede69f | 13 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:082731ede69f | 14 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
JMF | 0:082731ede69f | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:082731ede69f | 16 | * See the License for the specific language governing permissions and |
JMF | 0:082731ede69f | 17 | * limitations under the License. |
JMF | 0:082731ede69f | 18 | */ |
JMF | 0:082731ede69f | 19 | |
JMF | 0:082731ede69f | 20 | #include "mbed.h" |
JMF | 0:082731ede69f | 21 | #include "easy-connect.h" |
JMF | 0:082731ede69f | 22 | |
JMF | 0:082731ede69f | 23 | /* |
JMF | 0:082731ede69f | 24 | * Instantiate the configured network interface |
JMF | 0:082731ede69f | 25 | */ |
JMF | 0:082731ede69f | 26 | |
JMF | 0:082731ede69f | 27 | #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
JMF | 0:082731ede69f | 28 | #include "EthernetInterface.h" |
JMF | 0:082731ede69f | 29 | EthernetInterface eth; |
JMF | 0:082731ede69f | 30 | |
JMF | 0:082731ede69f | 31 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A |
JMF | 0:082731ede69f | 32 | #include "WNC14A2AInterface.h" |
JMF | 0:082731ede69f | 33 | |
JMF | 0:082731ede69f | 34 | #if MBED_CONF_APP_WNC_DEBUG == true |
JMF | 0:082731ede69f | 35 | #include "WNCDebug.h" |
JMF | 0:082731ede69f | 36 | WNCDebug dbgout(stderr); |
JMF | 0:082731ede69f | 37 | WNC14A2AInterface wnc(&dbgout); |
JMF | 0:082731ede69f | 38 | #else |
JMF | 0:082731ede69f | 39 | WNC14A2AInterface wnc; |
JMF | 0:082731ede69f | 40 | #endif |
JMF | 0:082731ede69f | 41 | |
JMF | 0:082731ede69f | 42 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_BG96 |
JMF | 0:082731ede69f | 43 | #include "BG96Interface.h" |
JMF | 0:082731ede69f | 44 | BG96Interface bg96; |
JMF | 0:082731ede69f | 45 | |
JMF | 0:082731ede69f | 46 | #else |
JMF | 0:082731ede69f | 47 | #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)." |
JMF | 0:082731ede69f | 48 | #endif // MBED_CONF_APP_NETWORK_INTERFACE |
JMF | 0:082731ede69f | 49 | |
JMF | 0:082731ede69f | 50 | /* \brief print_MAC - print_MAC - helper function to print out MAC address |
JMF | 0:082731ede69f | 51 | * in: network_interface - pointer to network i/f |
JMF | 0:082731ede69f | 52 | * bool log-messages print out logs or not |
JMF | 0:082731ede69f | 53 | * MAC address is printed, if it can be acquired & log_messages is true. |
JMF | 0:082731ede69f | 54 | * |
JMF | 0:082731ede69f | 55 | */ |
JMF | 0:082731ede69f | 56 | void print_MAC(NetworkInterface* network_interface, bool log_messages) { |
JMF | 0:082731ede69f | 57 | const char *mac_addr = network_interface->get_mac_address(); |
JMF | 0:082731ede69f | 58 | if( !log_messages ) |
JMF | 0:082731ede69f | 59 | return; |
JMF | 0:082731ede69f | 60 | if (mac_addr == NULL) |
JMF | 0:082731ede69f | 61 | printf("[EasyConnect] ERROR - No MAC address\n"); |
JMF | 0:082731ede69f | 62 | else |
JMF | 0:082731ede69f | 63 | printf("[EasyConnect] MAC address %s\n", mac_addr); |
JMF | 0:082731ede69f | 64 | } |
JMF | 0:082731ede69f | 65 | |
JMF | 0:082731ede69f | 66 | |
JMF | 0:082731ede69f | 67 | /* \brief easy_connect easy_connect() function to connect the pre-defined network bearer, |
JMF | 0:082731ede69f | 68 | * config done via mbed_app.json (see README.md for details). |
JMF | 0:082731ede69f | 69 | * |
JMF | 0:082731ede69f | 70 | * IN: bool log_messages print out diagnostics or not. |
JMF | 0:082731ede69f | 71 | */ |
JMF | 0:082731ede69f | 72 | NetworkInterface* easy_connect(bool log_messages) { |
JMF | 0:082731ede69f | 73 | NetworkInterface* network_interface = NULL; |
JMF | 0:082731ede69f | 74 | int connect_success = -1; |
JMF | 0:082731ede69f | 75 | |
JMF | 0:082731ede69f | 76 | #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
JMF | 0:082731ede69f | 77 | if (log_messages) { |
JMF | 0:082731ede69f | 78 | printf("[EasyConnect] Using Ethernet\n"); |
JMF | 0:082731ede69f | 79 | } |
JMF | 0:082731ede69f | 80 | network_interface = ð |
JMF | 0:082731ede69f | 81 | connect_success = eth.connect(); |
JMF | 0:082731ede69f | 82 | |
JMF | 0:082731ede69f | 83 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A |
JMF | 0:082731ede69f | 84 | if (log_messages) { |
JMF | 0:082731ede69f | 85 | printf("[EasyConnect] Using WNC14A2A\n"); |
JMF | 0:082731ede69f | 86 | } |
JMF | 0:082731ede69f | 87 | #if MBED_CONF_APP_WNC_DEBUG == true |
JMF | 0:082731ede69f | 88 | printf("[EasyConnect] With WNC14A2A debug output set to 0x%02X\n",MBED_CONF_APP_WNC_DEBUG_SETTING); |
JMF | 0:082731ede69f | 89 | wnc.doDebug(MBED_CONF_APP_WNC_DEBUG_SETTING); |
JMF | 0:082731ede69f | 90 | #endif |
JMF | 0:082731ede69f | 91 | network_interface = &wnc; |
JMF | 0:082731ede69f | 92 | connect_success = wnc.connect(); |
JMF | 0:082731ede69f | 93 | |
JMF | 0:082731ede69f | 94 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_BG96 |
JMF | 0:082731ede69f | 95 | if (log_messages) { |
JMF | 0:082731ede69f | 96 | printf("[EasyConnect] Using BG96\n"); |
JMF | 0:082731ede69f | 97 | } |
JMF | 0:082731ede69f | 98 | #if MBED_CONF_APP_BG96_DEBUG == true |
JMF | 0:082731ede69f | 99 | printf("[EasyConnect] With BG96 debug output set to 0x%02X\n",MBED_CONF_APP_BG96_DEBUG_SETTING); |
JMF | 0:082731ede69f | 100 | bg96.doDebug(MBED_CONF_APP_BG96_DEBUG_SETTING); |
JMF | 0:082731ede69f | 101 | #endif |
JMF | 0:082731ede69f | 102 | network_interface = &bg96; |
JMF | 0:082731ede69f | 103 | connect_success = bg96.connect(); |
JMF | 0:082731ede69f | 104 | #endif |
JMF | 0:082731ede69f | 105 | |
JMF | 0:082731ede69f | 106 | if(connect_success == 0) { |
JMF | 0:082731ede69f | 107 | if (log_messages) { |
JMF | 0:082731ede69f | 108 | printf("[EasyConnect] Connected to Network successfully\n"); |
JMF | 0:082731ede69f | 109 | print_MAC(network_interface, log_messages); |
JMF | 0:082731ede69f | 110 | } |
JMF | 0:082731ede69f | 111 | } else { |
JMF | 0:082731ede69f | 112 | if (log_messages) { |
JMF | 0:082731ede69f | 113 | print_MAC(network_interface, log_messages); |
JMF | 0:082731ede69f | 114 | printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success); |
JMF | 0:082731ede69f | 115 | } |
JMF | 0:082731ede69f | 116 | return NULL; |
JMF | 0:082731ede69f | 117 | } |
JMF | 0:082731ede69f | 118 | |
JMF | 0:082731ede69f | 119 | const char *ip_addr = network_interface->get_ip_address(); |
JMF | 0:082731ede69f | 120 | if (ip_addr == NULL) { |
JMF | 0:082731ede69f | 121 | if (log_messages) { |
JMF | 0:082731ede69f | 122 | printf("[EasyConnect] ERROR - No IP address\n"); |
JMF | 0:082731ede69f | 123 | } |
JMF | 0:082731ede69f | 124 | return NULL; |
JMF | 0:082731ede69f | 125 | } |
JMF | 0:082731ede69f | 126 | |
JMF | 0:082731ede69f | 127 | if (log_messages) { |
JMF | 0:082731ede69f | 128 | printf("[EasyConnect] IP address %s\n", ip_addr); |
JMF | 0:082731ede69f | 129 | } |
JMF | 0:082731ede69f | 130 | return network_interface; |
JMF | 0:082731ede69f | 131 | } |
JMF | 0:082731ede69f | 132 | |
JMF | 0:082731ede69f | 133 | /* \brief easy_get_netif - easy_connect function to get pointer to network interface |
JMF | 0:082731ede69f | 134 | * without connecting to it. |
JMF | 0:082731ede69f | 135 | * |
JMF | 0:082731ede69f | 136 | * IN: bool log_messages print out diagnostics or not. |
JMF | 0:082731ede69f | 137 | */ |
JMF | 0:082731ede69f | 138 | NetworkInterface* easy_get_netif(bool log_messages) { |
JMF | 0:082731ede69f | 139 | #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET |
JMF | 0:082731ede69f | 140 | if (log_messages) { |
JMF | 0:082731ede69f | 141 | printf("[EasyConnect] Ethernet\n"); |
JMF | 0:082731ede69f | 142 | } |
JMF | 0:082731ede69f | 143 | return ð |
JMF | 0:082731ede69f | 144 | |
JMF | 0:082731ede69f | 145 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A |
JMF | 0:082731ede69f | 146 | if (log_messages) { |
JMF | 0:082731ede69f | 147 | printf("[EasyConnect] WNC14A2A\n"); |
JMF | 0:082731ede69f | 148 | } |
JMF | 0:082731ede69f | 149 | return &wnc; |
JMF | 0:082731ede69f | 150 | #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_BG96 |
JMF | 0:082731ede69f | 151 | if (log_messages) { |
JMF | 0:082731ede69f | 152 | printf("[EasyConnect] BG96\n"); |
JMF | 0:082731ede69f | 153 | } |
JMF | 0:082731ede69f | 154 | return &bg96; |
JMF | 0:082731ede69f | 155 | #endif |
JMF | 0:082731ede69f | 156 | } |
JMF | 0:082731ede69f | 157 |