Jim Flynn / Mbed OS aws-iot-device-sdk-mbed-c
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers easy-connect.cpp Source File

easy-connect.cpp

00001 
00002 /*
00003  * FILE: easy-connect.cpp
00004  *
00005  * Copyright (c) 2015 - 2017 ARM Limited. All rights reserved.
00006  * SPDX-License-Identifier: Apache-2.0
00007  * Licensed under the Apache License, Version 2.0 (the License); you may
00008  * not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  * http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00015  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 #include "mbed.h"
00021 #include "easy-connect.h"
00022 
00023 /*
00024  * Instantiate the configured network interface
00025  */
00026 
00027 #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00028 #include "EthernetInterface.h"
00029 EthernetInterface eth;
00030 
00031 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A
00032 #include "WNC14A2AInterface.h"
00033 
00034 #if MBED_CONF_APP_WNC_DEBUG == true
00035 #include "WNCDebug.h"
00036 WNCDebug dbgout(stderr);
00037 WNC14A2AInterface wnc(&dbgout);
00038 #else
00039 WNC14A2AInterface wnc;
00040 #endif
00041 
00042 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_BG96
00043 #include "BG96Interface.h"
00044 BG96Interface bg96;
00045 
00046 #else
00047 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
00048 #endif // MBED_CONF_APP_NETWORK_INTERFACE
00049 
00050 /* \brief print_MAC - print_MAC  - helper function to print out MAC address
00051  * in: network_interface - pointer to network i/f
00052  *     bool log-messages   print out logs or not
00053  * MAC address is printed, if it can be acquired & log_messages is true.
00054  *
00055  */
00056 void print_MAC(NetworkInterface* network_interface, bool log_messages) {
00057     const char *mac_addr = network_interface->get_mac_address();
00058     if( !log_messages )
00059         return;
00060     if (mac_addr == NULL) 
00061         printf("[EasyConnect] ERROR - No MAC address\n");
00062     else
00063         printf("[EasyConnect] MAC address %s\n", mac_addr);
00064 }
00065 
00066 
00067 /* \brief easy_connect     easy_connect() function to connect the pre-defined network bearer,
00068  *                         config done via mbed_app.json (see README.md for details).
00069  *
00070  * IN: bool  log_messages  print out diagnostics or not.
00071  */
00072 NetworkInterface* easy_connect(bool log_messages) {
00073     NetworkInterface* network_interface = NULL;
00074     int connect_success = -1;
00075 
00076 #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00077     if (log_messages) {
00078         printf("[EasyConnect] Using Ethernet\n");
00079     }
00080     network_interface = ð
00081     connect_success = eth.connect();
00082 
00083 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A
00084     if (log_messages) {
00085         printf("[EasyConnect] Using WNC14A2A\n");
00086     }
00087     #if MBED_CONF_APP_WNC_DEBUG == true
00088     printf("[EasyConnect] With WNC14A2A debug output set to 0x%02X\n",MBED_CONF_APP_WNC_DEBUG_SETTING);
00089     wnc.doDebug(MBED_CONF_APP_WNC_DEBUG_SETTING);
00090     #endif
00091     network_interface = &wnc;
00092     connect_success = wnc.connect();
00093 
00094 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_BG96
00095     if (log_messages) {
00096         printf("[EasyConnect] Using BG96\n");
00097     }
00098     #if MBED_CONF_APP_BG96_DEBUG == true
00099     printf("[EasyConnect] With BG96 debug output set to 0x%02X\n",MBED_CONF_APP_BG96_DEBUG_SETTING);
00100     bg96.doDebug(MBED_CONF_APP_BG96_DEBUG_SETTING);
00101     #endif
00102     network_interface = &bg96;
00103     connect_success = bg96.connect();
00104 #endif
00105 
00106     if(connect_success == 0) {
00107         if (log_messages) {
00108             printf("[EasyConnect] Connected to Network successfully\n");
00109             print_MAC(network_interface, log_messages);
00110             }
00111     } else {
00112         if (log_messages) {
00113             print_MAC(network_interface, log_messages);
00114             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00115         }
00116         return NULL;
00117     }
00118 
00119     const char *ip_addr  = network_interface->get_ip_address();
00120     if (ip_addr == NULL) {
00121         if (log_messages) {
00122             printf("[EasyConnect] ERROR - No IP address\n");
00123         }
00124         return NULL;
00125     }
00126 
00127     if (log_messages) {
00128         printf("[EasyConnect] IP address %s\n", ip_addr);
00129     }
00130     return network_interface;
00131 }
00132 
00133 /* \brief easy_get_netif - easy_connect function to get pointer to network interface
00134  *                        without connecting to it.
00135  *
00136  * IN: bool  log_messages  print out diagnostics or not.
00137  */
00138 NetworkInterface* easy_get_netif(bool log_messages) {
00139 #if MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00140     if (log_messages) {
00141         printf("[EasyConnect] Ethernet\n");
00142     }
00143     return ð
00144 
00145 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_WNC14A2A
00146     if (log_messages) {
00147         printf("[EasyConnect] WNC14A2A\n");
00148     }
00149     return  &wnc;
00150 #elif MBED_CONF_APP_NETWORK_INTERFACE == CELLULAR_BG96
00151     if (log_messages) {
00152         printf("[EasyConnect] BG96\n");
00153     }
00154     return  &bg96;
00155 #endif
00156 }
00157