Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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(D7); 00031 DigitalOut BG96_PWRKEY(D9); 00032 00033 BG96_RESET = 1; 00034 BG96_PWRKEY = 1; 00035 wait_ms(200); 00036 00037 BG96_RESET = 0; 00038 BG96_PWRKEY = 0; 00039 wait_ms(300); 00040 00041 BG96_RESET = 1; 00042 wait_ms(5000); 00043 } 00044 00045 NetworkInterface *iface; 00046 00047 // Echo server hostname 00048 const char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME; 00049 00050 // Echo server port (same for TCP and UDP) 00051 const int port = MBED_CONF_APP_ECHO_SERVER_PORT; 00052 00053 static rtos::Mutex trace_mutex; 00054 00055 #if MBED_CONF_MBED_TRACE_ENABLE 00056 static void trace_wait() 00057 { 00058 trace_mutex.lock(); 00059 } 00060 00061 static void trace_release() 00062 { 00063 trace_mutex.unlock(); 00064 } 00065 00066 static char time_st[50]; 00067 00068 static char* trace_time(size_t ss) 00069 { 00070 snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count()); 00071 return time_st; 00072 } 00073 00074 static void trace_open() 00075 { 00076 mbed_trace_init(); 00077 mbed_trace_prefix_function_set( &trace_time ); 00078 00079 mbed_trace_mutex_wait_function_set(trace_wait); 00080 mbed_trace_mutex_release_function_set(trace_release); 00081 00082 mbed_cellular_trace::mutex_wait_function_set(trace_wait); 00083 mbed_cellular_trace::mutex_release_function_set(trace_release); 00084 } 00085 00086 static void trace_close() 00087 { 00088 mbed_cellular_trace::mutex_wait_function_set(NULL); 00089 mbed_cellular_trace::mutex_release_function_set(NULL); 00090 00091 mbed_trace_free(); 00092 } 00093 #endif // #if MBED_CONF_MBED_TRACE_ENABLE 00094 00095 Thread dot_thread(osPriorityNormal, 512); 00096 00097 void print_function(const char *format, ...) 00098 { 00099 trace_mutex.lock(); 00100 va_list arglist; 00101 va_start( arglist, format ); 00102 vprintf(format, arglist); 00103 va_end( arglist ); 00104 trace_mutex.unlock(); 00105 } 00106 00107 void dot_event() 00108 { 00109 while (true) { 00110 ThisThread::sleep_for(4000); 00111 if (iface && iface->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) { 00112 break; 00113 } else { 00114 trace_mutex.lock(); 00115 printf("."); 00116 fflush(stdout); 00117 trace_mutex.unlock(); 00118 } 00119 } 00120 } 00121 00122 /** 00123 * Connects to the Cellular Network 00124 */ 00125 nsapi_error_t do_connect() 00126 { 00127 nsapi_error_t retcode = NSAPI_ERROR_OK; 00128 uint8_t retry_counter = 0; 00129 00130 while (iface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) { 00131 retcode = iface->connect(); 00132 if (retcode == NSAPI_ERROR_AUTH_FAILURE) { 00133 print_function("\n\nAuthentication Failure. Exiting application\n"); 00134 } else if (retcode == NSAPI_ERROR_OK) { 00135 print_function("\n\nConnection Established.\n"); 00136 } else if (retry_counter > RETRY_COUNT) { 00137 print_function("\n\nFatal connection failure: %d\n", retcode); 00138 } else { 00139 print_function("\n\nCouldn't connect: %d, will retry\n", retcode); 00140 retry_counter++; 00141 continue; 00142 } 00143 break; 00144 } 00145 return retcode; 00146 } 00147 00148 /** 00149 * Opens a UDP or a TCP socket with the given echo server and performs an echo 00150 * transaction retrieving current. 00151 */ 00152 nsapi_error_t test_send_recv() 00153 { 00154 nsapi_size_or_error_t retcode; 00155 #if MBED_CONF_APP_SOCK_TYPE == TCP 00156 TCPSocket sock; 00157 #else 00158 UDPSocket sock; 00159 #endif 00160 00161 retcode = sock.open(iface); 00162 if (retcode != NSAPI_ERROR_OK) { 00163 #if MBED_CONF_APP_SOCK_TYPE == TCP 00164 print_function("TCPSocket.open() fails, code: %d\n", retcode); 00165 #else 00166 print_function("UDPSocket.open() fails, code: %d\n", retcode); 00167 #endif 00168 return -1; 00169 } 00170 00171 SocketAddress sock_addr; 00172 retcode = iface->gethostbyname(host_name, &sock_addr); 00173 if (retcode != NSAPI_ERROR_OK) { 00174 print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode); 00175 return -1; 00176 } 00177 00178 sock_addr.set_port(port); 00179 00180 sock.set_timeout(15000); 00181 int n = 0; 00182 const char *echo_string = "TEST"; 00183 char recv_buf[4]; 00184 #if MBED_CONF_APP_SOCK_TYPE == TCP 00185 retcode = sock.connect(sock_addr); 00186 if (retcode < 0) { 00187 print_function("TCPSocket.connect() fails, code: %d\n", retcode); 00188 return -1; 00189 } else { 00190 print_function("TCP: connected with %s server\n", host_name); 00191 } 00192 retcode = sock.send((void*) echo_string, sizeof(echo_string)); 00193 if (retcode < 0) { 00194 print_function("TCPSocket.send() fails, code: %d\n", retcode); 00195 return -1; 00196 } else { 00197 print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name); 00198 } 00199 00200 n = sock.recv((void*) recv_buf, sizeof(recv_buf)); 00201 #else 00202 00203 retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string)); 00204 if (retcode < 0) { 00205 print_function("UDPSocket.sendto() fails, code: %d\n", retcode); 00206 return -1; 00207 } else { 00208 print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name); 00209 } 00210 00211 n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf)); 00212 #endif 00213 00214 sock.close(); 00215 00216 if (n > 0) { 00217 print_function("Received from echo server %d Bytes\n", n); 00218 return 0; 00219 } 00220 00221 return -1; 00222 } 00223 00224 int main() 00225 { 00226 print_function("\n\nmbed-os-example-cellular\n"); 00227 print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__); 00228 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN 00229 print_function("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL")); 00230 #endif 00231 00232 print_function("Establishing connection\n"); 00233 00234 BG96_Modem_PowerON(); 00235 print_function("M2Mnet(BG96) Power ON\n"); 00236 00237 #if MBED_CONF_MBED_TRACE_ENABLE 00238 trace_open(); 00239 #else 00240 dot_thread.start(dot_event); 00241 #endif // #if MBED_CONF_MBED_TRACE_ENABLE 00242 00243 // sim pin, apn, credentials and possible plmn are taken atuomtically from json when using get_default_instance() 00244 iface = NetworkInterface::get_default_instance(); 00245 MBED_ASSERT(iface); 00246 00247 nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION; 00248 00249 /* Attempt to connect to a cellular network */ 00250 if (do_connect() == NSAPI_ERROR_OK) { 00251 retcode = test_send_recv(); 00252 } 00253 00254 if (iface->disconnect() != NSAPI_ERROR_OK) { 00255 print_function("\n\n disconnect failed.\n\n"); 00256 } 00257 00258 if (retcode == NSAPI_ERROR_OK) { 00259 print_function("\n\nSuccess. Exiting \n\n"); 00260 } else { 00261 print_function("\n\nFailure. Exiting \n\n"); 00262 } 00263 00264 #if MBED_CONF_MBED_TRACE_ENABLE 00265 trace_close(); 00266 #else 00267 dot_thread.terminate(); 00268 #endif // #if MBED_CONF_MBED_TRACE_ENABLE 00269 00270 return 0; 00271 } 00272 // EOF
Generated on Wed Jul 20 2022 14:45:59 by
1.7.2