CodeZoo co., ltd
/
mbed-os-example-cellular-Type1SC
Cellular example for CodeZoo Type1SC Shield
Embed:
(wiki syntax)
Show/hide line numbers
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 "CellularNonIPSocket.h" 00019 #include "UDPSocket.h" 00020 #include "TCPSocket.h" 00021 #include "cellular_demo_tracing.h" 00022 00023 /* configuration choices in mbed_app.json */ 00024 #define UDP 0 00025 #define TCP 1 00026 #define NONIP 2 00027 00028 #if MBED_CONF_APP_SOCK_TYPE == TCP 00029 static constexpr char SOCKET_TYPE[] = "TCP"; 00030 #elif MBED_CONF_APP_SOCK_TYPE == UDP 00031 static constexpr char SOCKET_TYPE[] = "UDP"; 00032 #elif MBED_CONF_APP_SOCK_TYPE == NONIP 00033 static constexpr char SOCKET_TYPE[] = "CellularNonIP"; 00034 #endif 00035 static const char ECHO_HOSTNAME[] = MBED_CONF_APP_ECHO_SERVER_HOSTNAME; 00036 00037 00038 class CellularDemo { 00039 static constexpr uint8_t RETRY_COUNT = 3; 00040 00041 public: 00042 CellularDemo(NetworkInterface &network) 00043 : _net(network) 00044 { } 00045 00046 ~CellularDemo() { } 00047 00048 /** Run the cellular demo. */ 00049 void run() 00050 { 00051 /* sim pin, apn, credentials and possible plmn are taken automatically from json 00052 * when using NetworkInterface::set_default_parameters() */ 00053 _net.set_default_parameters(); 00054 00055 nsapi_size_or_error_t ret = NSAPI_ERROR_NO_CONNECTION; 00056 00057 if (connect_cellular()) { 00058 /* ping echo server */ 00059 if (!test_send_and_receive()) { 00060 printf("Sending and received data failed.\n"); 00061 } 00062 00063 ret = _net.disconnect(); 00064 00065 if (ret != NSAPI_ERROR_OK) { 00066 printf("Disconnect failed (error: %d).\n", ret); 00067 } 00068 } 00069 00070 if (ret == NSAPI_ERROR_OK) { 00071 printf("Success. Exiting\n"); 00072 } else { 00073 printf("Failure. Exiting\n"); 00074 } 00075 } 00076 00077 private: 00078 /** 00079 * For UDP or TCP it opens a socket with the given echo server and performs an echo transaction. 00080 * For Cellular Non-IP it opens a socket for which the data delivery path is decided 00081 * by network's control plane CIoT optimisation setup, for the given APN. 00082 */ 00083 bool test_send_and_receive() 00084 { 00085 nsapi_size_or_error_t ret; 00086 00087 ret = _socket.open(&_net); 00088 00089 if (ret != NSAPI_ERROR_OK) { 00090 printf("%sSocket.open() fails, code: %d\n", SOCKET_TYPE, ret); 00091 return false; 00092 } 00093 00094 _socket.set_timeout(15000); 00095 00096 if (!resolve_hostname()) { 00097 return false; 00098 } 00099 00100 if (!connect_socket()) { 00101 return false; 00102 } 00103 00104 ret = send_test_data(); 00105 00106 if (ret < 0) { 00107 printf("%sSocket.send() fails, code: %d\n", SOCKET_TYPE, ret); 00108 return false; 00109 } else { 00110 printf("%s: Sent %d Bytes to %s\n", SOCKET_TYPE, ret, ECHO_HOSTNAME); 00111 } 00112 00113 ret = receive_test_data(); 00114 00115 if (ret < 0) { 00116 printf("%sSocket.recv() fails, code: %d\n", SOCKET_TYPE, ret); 00117 return false; 00118 } else { 00119 printf("Received from echo server %d Bytes\n", ret); 00120 } 00121 00122 ret = _socket.close(); 00123 00124 if (ret != NSAPI_ERROR_OK) { 00125 printf("%sSocket.close() fails, code: %d\n", SOCKET_TYPE, ret); 00126 return false; 00127 } 00128 00129 return true; 00130 } 00131 00132 /** Connects to the Cellular Network */ 00133 bool connect_cellular() 00134 { 00135 printf("Establishing connection\n"); 00136 00137 /* check if we're already connected */ 00138 if (_net.get_connection_status() == NSAPI_STATUS_GLOBAL_UP) { 00139 return true; 00140 } 00141 00142 nsapi_error_t ret; 00143 00144 for (uint8_t retry = 0; retry <= RETRY_COUNT; retry++) { 00145 ret = _net.connect(); 00146 00147 if (ret == NSAPI_ERROR_OK) { 00148 printf("Connection Established.\n"); 00149 return true; 00150 } else if (ret == NSAPI_ERROR_AUTH_FAILURE) { 00151 printf("Authentication Failure.\n"); 00152 return false; 00153 } else { 00154 printf("Couldn't connect: %d, will retry\n", ret); 00155 } 00156 } 00157 00158 printf("Fatal connection failure: %d\n", ret); 00159 00160 return false; 00161 } 00162 00163 /** Connects to the Cellular Network */ 00164 bool resolve_hostname() 00165 { 00166 #if MBED_CONF_APP_SOCK_TYPE != NONIP 00167 nsapi_error_t ret = _net.gethostbyname(ECHO_HOSTNAME, &_socket_address); 00168 00169 if (ret != NSAPI_ERROR_OK) { 00170 printf("Couldn't resolve remote host: %s, code: %d\n", ECHO_HOSTNAME, ret); 00171 return false; 00172 } 00173 00174 _socket_address.set_port(MBED_CONF_APP_ECHO_SERVER_PORT); 00175 #endif 00176 return true; 00177 } 00178 00179 bool connect_socket() 00180 { 00181 #if MBED_CONF_APP_SOCK_TYPE == TCP 00182 nsapi_error_t ret = _socket.connect(_socket_address); 00183 if (ret < 0) { 00184 printf("TCPSocket.connect() fails, code: %d\n", ret); 00185 return false; 00186 } else { 00187 printf("TCP: connected with %s server\n", ECHO_HOSTNAME); 00188 } 00189 #endif 00190 return true; 00191 } 00192 00193 nsapi_error_t send_test_data() 00194 { 00195 const char *echo_string = "Hello CodeZoo!!!"; 00196 #if MBED_CONF_APP_SOCK_TYPE == UDP 00197 return _socket.sendto(_socket_address, (void*)echo_string, strlen(echo_string)); 00198 #else 00199 return _socket.send((void*)echo_string, strlen(echo_string)); 00200 #endif 00201 } 00202 00203 nsapi_error_t receive_test_data() 00204 { 00205 int len=0; 00206 char receive_buffer[18]={0}; 00207 #if MBED_CONF_APP_SOCK_TYPE == UDP 00208 len = _socket.recvfrom(&_socket_address, (void*)receive_buffer, sizeof(receive_buffer)); 00209 printf("recvfrom : %s \n",receive_buffer); 00210 #else 00211 len = _socket.recv((void*)receive_buffer, sizeof(receive_buffer)); 00212 printf("recv : %s \n",receive_buffer); 00213 #endif 00214 return len; 00215 } 00216 00217 private: 00218 NetworkInterface &_net; 00219 00220 #if MBED_CONF_APP_SOCK_TYPE == TCP 00221 TCPSocket _socket; 00222 SocketAddress _socket_address; 00223 #elif MBED_CONF_APP_SOCK_TYPE == UDP 00224 UDPSocket _socket; 00225 SocketAddress _socket_address; 00226 #elif MBED_CONF_APP_SOCK_TYPE == NONIP 00227 CellularNonIPSocket _socket; 00228 #endif 00229 }; 00230 00231 int main() { 00232 printf("\nmbed-os-example-cellular\n"); 00233 00234 trace_open(); 00235 00236 #if MBED_CONF_APP_SOCK_TYPE == NONIP 00237 NetworkInterface *net = CellularContext::get_default_nonip_instance(); 00238 #else 00239 NetworkInterface *net = CellularContext::get_default_instance(); 00240 #endif 00241 00242 if (net) { 00243 CellularDemo example(*net); 00244 example.run(); 00245 } else { 00246 printf("Failed to get_default_instance()\n"); 00247 } 00248 00249 trace_close(); 00250 00251 return 0; 00252 }
Generated on Wed Jul 13 2022 10:07:45 by 1.7.2