xue liu / Mbed OS mbed-os-example-cellular-Wislink
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2017 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "common_functions.h"
00019 #include "UDPSocket.h"
00020 #include "CellularLog.h"
00021 
00022 #define UDP 0
00023 #define TCP 1
00024 
00025 // Number of retries /
00026 #define RETRY_COUNT 3
00027 
00028 void BG96_Modem_PowerON(void)
00029 {
00030     DigitalOut BG96_RESET(D5);
00031     DigitalOut BG96_PWRKEY(D6);
00032  
00033     BG96_RESET = 1;
00034     wait_ms(200);
00035  
00036     BG96_RESET = 0;
00037     BG96_PWRKEY = 1;
00038     wait_ms(300);
00039  
00040     BG96_PWRKEY = 0;   
00041     wait_ms(5000);
00042 }
00043 
00044 NetworkInterface *iface;
00045 
00046 // Echo server hostname
00047 const char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME;
00048 
00049 // Echo server port (same for TCP and UDP)
00050 const int port = MBED_CONF_APP_ECHO_SERVER_PORT;
00051 
00052 static rtos::Mutex trace_mutex;
00053 
00054 #if MBED_CONF_MBED_TRACE_ENABLE
00055 static void trace_wait()
00056 {
00057     trace_mutex.lock();
00058 }
00059 
00060 static void trace_release()
00061 {
00062     trace_mutex.unlock();
00063 }
00064 
00065 static char time_st[50];
00066 
00067 static char* trace_time(size_t ss)
00068 {
00069     snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count());
00070     return time_st;
00071 }
00072 
00073 static void trace_open()
00074 {
00075     mbed_trace_init();
00076     mbed_trace_prefix_function_set( &trace_time );
00077 
00078     mbed_trace_mutex_wait_function_set(trace_wait);
00079     mbed_trace_mutex_release_function_set(trace_release);
00080 
00081     mbed_cellular_trace::mutex_wait_function_set(trace_wait);
00082     mbed_cellular_trace::mutex_release_function_set(trace_release);
00083 }
00084 
00085 static void trace_close()
00086 {
00087     mbed_cellular_trace::mutex_wait_function_set(NULL);
00088     mbed_cellular_trace::mutex_release_function_set(NULL);
00089 
00090     mbed_trace_free();
00091 }
00092 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
00093 
00094 Thread dot_thread(osPriorityNormal, 512);
00095 
00096 void print_function(const char *format, ...)
00097 {
00098     trace_mutex.lock();
00099     va_list arglist;
00100     va_start( arglist, format );
00101     vprintf(format, arglist);
00102     va_end( arglist );
00103     trace_mutex.unlock();
00104 }
00105 
00106 void dot_event()
00107 {
00108     while (true) {
00109         ThisThread::sleep_for(4000);
00110         if (iface && iface->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) {
00111             break;
00112         } else {
00113             trace_mutex.lock();
00114             printf(".");
00115             fflush(stdout);
00116             trace_mutex.unlock();
00117         }
00118     }
00119 }
00120 
00121 /**
00122  * Connects to the Cellular Network
00123  */
00124 nsapi_error_t do_connect()
00125 {
00126     nsapi_error_t retcode = NSAPI_ERROR_OK;
00127     uint8_t retry_counter = 0;
00128 
00129     while (iface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
00130         retcode = iface->connect();
00131         if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
00132             print_function("\n\nAuthentication Failure. Exiting application\n");
00133         } else if (retcode == NSAPI_ERROR_OK) {
00134             print_function("\n\nConnection Established.\n");
00135         } else if (retry_counter > RETRY_COUNT) {
00136             print_function("\n\nFatal connection failure: %d\n", retcode);
00137         } else {
00138             print_function("\n\nCouldn't connect: %d, will retry\n", retcode);
00139             retry_counter++;
00140             continue;
00141         }
00142         break;
00143     }
00144     return retcode;
00145 }
00146 
00147 /**
00148  * Opens a UDP or a TCP socket with the given echo server and performs an echo
00149  * transaction retrieving current.
00150  */
00151 nsapi_error_t test_send_recv()
00152 {
00153     nsapi_size_or_error_t retcode;
00154 #if MBED_CONF_APP_SOCK_TYPE == TCP
00155     TCPSocket sock;
00156 #else
00157     UDPSocket sock;
00158 #endif
00159 
00160     retcode = sock.open(iface);
00161     if (retcode != NSAPI_ERROR_OK) {
00162 #if MBED_CONF_APP_SOCK_TYPE == TCP
00163         print_function("TCPSocket.open() fails, code: %d\n", retcode);
00164 #else
00165         print_function("UDPSocket.open() fails, code: %d\n", retcode);
00166 #endif
00167         return -1;
00168     }
00169 
00170     SocketAddress sock_addr;
00171     retcode = iface->gethostbyname(host_name, &sock_addr);
00172     if (retcode != NSAPI_ERROR_OK) {
00173         print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
00174         return -1;
00175     }
00176 
00177     sock_addr.set_port(port);
00178 
00179     sock.set_timeout(15000);
00180     int n = 0;
00181     const char *echo_string = "TEST";
00182     char recv_buf[4];
00183 #if MBED_CONF_APP_SOCK_TYPE == TCP
00184     retcode = sock.connect(sock_addr);
00185     if (retcode < 0) {
00186         print_function("TCPSocket.connect() fails, code: %d\n", retcode);
00187         return -1;
00188     } else {
00189         print_function("TCP: connected with %s server\n", host_name);
00190     }
00191     retcode = sock.send((void*) echo_string, sizeof(echo_string));
00192     if (retcode < 0) {
00193         print_function("TCPSocket.send() fails, code: %d\n", retcode);
00194         return -1;
00195     } else {
00196         print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
00197     }
00198 
00199     n = sock.recv((void*) recv_buf, sizeof(recv_buf));
00200 #else
00201 
00202     retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
00203     if (retcode < 0) {
00204         print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
00205         return -1;
00206     } else {
00207         print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name);
00208     }
00209 
00210     n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
00211 #endif
00212 
00213     sock.close();
00214 
00215     if (n > 0) {
00216         print_function("Received from echo server %d Bytes\n", n);
00217         return 0;
00218     }
00219 
00220     return -1;
00221 }
00222 
00223 int main()
00224 {
00225     print_function("\n\nmbed-os-example-cellular\n");
00226     print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__);
00227 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN
00228     print_function("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL"));
00229 #endif
00230 
00231     print_function("Establishing connection\n");
00232 
00233     BG96_Modem_PowerON();
00234     print_function("M2Mnet(BG96) Power ON\n");
00235         
00236 #if MBED_CONF_MBED_TRACE_ENABLE
00237     trace_open();
00238 #else
00239     dot_thread.start(dot_event);
00240 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
00241 
00242     // sim pin, apn, credentials and possible plmn are taken atuomtically from json when using get_default_instance()
00243     iface = NetworkInterface::get_default_instance();
00244     MBED_ASSERT(iface);
00245 
00246     nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
00247 
00248     /* Attempt to connect to a cellular network */
00249     if (do_connect() == NSAPI_ERROR_OK) {
00250         retcode = test_send_recv();
00251     }
00252 
00253     if (iface->disconnect() != NSAPI_ERROR_OK) {
00254         print_function("\n\n disconnect failed.\n\n");
00255     }
00256 
00257     if (retcode == NSAPI_ERROR_OK) {
00258         print_function("\n\nSuccess. Exiting \n\n");
00259     } else {
00260         print_function("\n\nFailure. Exiting \n\n");
00261     }
00262 
00263 #if MBED_CONF_MBED_TRACE_ENABLE
00264     trace_close();
00265 #else
00266     dot_thread.terminate();
00267 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
00268 
00269     return 0;
00270 }
00271 // EOF