SUNGTAE KIM
/
mbed-os-example-cellular-BG96
Set specific IP Address/Port
main.cpp@45:2d57fafd8d73, 2019-09-06 (annotated)
- Committer:
- stkim92
- Date:
- Fri Sep 06 07:44:03 2019 +0000
- Revision:
- 45:2d57fafd8d73
- Parent:
- 43:91f11760b50f
Set Server IP/Port
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:4611f6cf2413 | 1 | /* |
mbed_official | 0:4611f6cf2413 | 2 | * Copyright (c) 2017 ARM Limited. All rights reserved. |
mbed_official | 0:4611f6cf2413 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 0:4611f6cf2413 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbed_official | 0:4611f6cf2413 | 5 | * not use this file except in compliance with the License. |
mbed_official | 0:4611f6cf2413 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:4611f6cf2413 | 7 | * |
mbed_official | 0:4611f6cf2413 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:4611f6cf2413 | 9 | * |
mbed_official | 0:4611f6cf2413 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:4611f6cf2413 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbed_official | 0:4611f6cf2413 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:4611f6cf2413 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 0:4611f6cf2413 | 14 | * limitations under the License. |
mbed_official | 0:4611f6cf2413 | 15 | */ |
mbed_official | 0:4611f6cf2413 | 16 | |
mbed_official | 0:4611f6cf2413 | 17 | #include "mbed.h" |
mbed_official | 0:4611f6cf2413 | 18 | #include "common_functions.h" |
mbed_official | 0:4611f6cf2413 | 19 | #include "UDPSocket.h" |
mbed_official | 6:5678c0b6f74e | 20 | #include "CellularLog.h" |
mbed_official | 0:4611f6cf2413 | 21 | |
mbed_official | 0:4611f6cf2413 | 22 | #define UDP 0 |
mbed_official | 0:4611f6cf2413 | 23 | #define TCP 1 |
mbed_official | 0:4611f6cf2413 | 24 | |
mbed_official | 0:4611f6cf2413 | 25 | // Number of retries / |
mbed_official | 0:4611f6cf2413 | 26 | #define RETRY_COUNT 3 |
mbed_official | 0:4611f6cf2413 | 27 | |
Daniel_Lee | 43:91f11760b50f | 28 | void BG96_Modem_PowerON(void) |
Daniel_Lee | 43:91f11760b50f | 29 | { |
Daniel_Lee | 43:91f11760b50f | 30 | DigitalOut BG96_RESET(D7); |
Daniel_Lee | 43:91f11760b50f | 31 | DigitalOut BG96_PWRKEY(D9); |
Daniel_Lee | 43:91f11760b50f | 32 | |
Daniel_Lee | 43:91f11760b50f | 33 | BG96_RESET = 1; |
Daniel_Lee | 43:91f11760b50f | 34 | BG96_PWRKEY = 1; |
Daniel_Lee | 43:91f11760b50f | 35 | wait_ms(200); |
Daniel_Lee | 43:91f11760b50f | 36 | |
Daniel_Lee | 43:91f11760b50f | 37 | BG96_RESET = 0; |
Daniel_Lee | 43:91f11760b50f | 38 | BG96_PWRKEY = 0; |
Daniel_Lee | 43:91f11760b50f | 39 | wait_ms(300); |
Daniel_Lee | 43:91f11760b50f | 40 | |
Daniel_Lee | 43:91f11760b50f | 41 | BG96_RESET = 1; |
Daniel_Lee | 43:91f11760b50f | 42 | wait_ms(5000); |
Daniel_Lee | 43:91f11760b50f | 43 | } |
Daniel_Lee | 43:91f11760b50f | 44 | |
mbed_official | 28:232da3ce8a88 | 45 | NetworkInterface *iface; |
mbed_official | 0:4611f6cf2413 | 46 | |
mbed_official | 0:4611f6cf2413 | 47 | // Echo server hostname |
stkim92 | 45:2d57fafd8d73 | 48 | //nst char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME; |
stkim92 | 45:2d57fafd8d73 | 49 | const char *server_ip_address = "222.98.xxx.xxx"; |
mbed_official | 0:4611f6cf2413 | 50 | |
mbed_official | 0:4611f6cf2413 | 51 | // Echo server port (same for TCP and UDP) |
stkim92 | 45:2d57fafd8d73 | 52 | //const int port = MBED_CONF_APP_ECHO_SERVER_PORT; |
stkim92 | 45:2d57fafd8d73 | 53 | const int port = 7878; |
stkim92 | 45:2d57fafd8d73 | 54 | |
mbed_official | 0:4611f6cf2413 | 55 | |
mbed_official | 6:5678c0b6f74e | 56 | static rtos::Mutex trace_mutex; |
mbed_official | 6:5678c0b6f74e | 57 | |
mbed_official | 6:5678c0b6f74e | 58 | #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 59 | static void trace_wait() |
mbed_official | 6:5678c0b6f74e | 60 | { |
mbed_official | 6:5678c0b6f74e | 61 | trace_mutex.lock(); |
mbed_official | 6:5678c0b6f74e | 62 | } |
mbed_official | 6:5678c0b6f74e | 63 | |
mbed_official | 6:5678c0b6f74e | 64 | static void trace_release() |
mbed_official | 6:5678c0b6f74e | 65 | { |
mbed_official | 6:5678c0b6f74e | 66 | trace_mutex.unlock(); |
mbed_official | 6:5678c0b6f74e | 67 | } |
mbed_official | 6:5678c0b6f74e | 68 | |
mbed_official | 6:5678c0b6f74e | 69 | static char time_st[50]; |
mbed_official | 6:5678c0b6f74e | 70 | |
mbed_official | 6:5678c0b6f74e | 71 | static char* trace_time(size_t ss) |
mbed_official | 6:5678c0b6f74e | 72 | { |
mbed_official | 6:5678c0b6f74e | 73 | snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count()); |
mbed_official | 6:5678c0b6f74e | 74 | return time_st; |
mbed_official | 6:5678c0b6f74e | 75 | } |
mbed_official | 6:5678c0b6f74e | 76 | |
mbed_official | 6:5678c0b6f74e | 77 | static void trace_open() |
mbed_official | 6:5678c0b6f74e | 78 | { |
mbed_official | 6:5678c0b6f74e | 79 | mbed_trace_init(); |
mbed_official | 6:5678c0b6f74e | 80 | mbed_trace_prefix_function_set( &trace_time ); |
mbed_official | 6:5678c0b6f74e | 81 | |
mbed_official | 6:5678c0b6f74e | 82 | mbed_trace_mutex_wait_function_set(trace_wait); |
mbed_official | 6:5678c0b6f74e | 83 | mbed_trace_mutex_release_function_set(trace_release); |
mbed_official | 11:23ea0907186e | 84 | |
mbed_official | 11:23ea0907186e | 85 | mbed_cellular_trace::mutex_wait_function_set(trace_wait); |
mbed_official | 11:23ea0907186e | 86 | mbed_cellular_trace::mutex_release_function_set(trace_release); |
mbed_official | 6:5678c0b6f74e | 87 | } |
mbed_official | 6:5678c0b6f74e | 88 | |
mbed_official | 6:5678c0b6f74e | 89 | static void trace_close() |
mbed_official | 6:5678c0b6f74e | 90 | { |
mbed_official | 11:23ea0907186e | 91 | mbed_cellular_trace::mutex_wait_function_set(NULL); |
mbed_official | 11:23ea0907186e | 92 | mbed_cellular_trace::mutex_release_function_set(NULL); |
mbed_official | 11:23ea0907186e | 93 | |
mbed_official | 6:5678c0b6f74e | 94 | mbed_trace_free(); |
mbed_official | 6:5678c0b6f74e | 95 | } |
mbed_official | 6:5678c0b6f74e | 96 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 97 | |
mbed_official | 2:0f644d6045cf | 98 | Thread dot_thread(osPriorityNormal, 512); |
mbed_official | 0:4611f6cf2413 | 99 | |
mbed_official | 6:5678c0b6f74e | 100 | void print_function(const char *format, ...) |
mbed_official | 0:4611f6cf2413 | 101 | { |
mbed_official | 6:5678c0b6f74e | 102 | trace_mutex.lock(); |
mbed_official | 6:5678c0b6f74e | 103 | va_list arglist; |
mbed_official | 6:5678c0b6f74e | 104 | va_start( arglist, format ); |
mbed_official | 6:5678c0b6f74e | 105 | vprintf(format, arglist); |
mbed_official | 6:5678c0b6f74e | 106 | va_end( arglist ); |
mbed_official | 6:5678c0b6f74e | 107 | trace_mutex.unlock(); |
mbed_official | 0:4611f6cf2413 | 108 | } |
mbed_official | 0:4611f6cf2413 | 109 | |
mbed_official | 0:4611f6cf2413 | 110 | void dot_event() |
mbed_official | 0:4611f6cf2413 | 111 | { |
mbed_official | 0:4611f6cf2413 | 112 | while (true) { |
mbed_official | 26:348eec457e58 | 113 | ThisThread::sleep_for(4000); |
mbed_official | 28:232da3ce8a88 | 114 | if (iface && iface->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) { |
mbed_official | 13:4bea5334b419 | 115 | break; |
mbed_official | 13:4bea5334b419 | 116 | } else { |
mbed_official | 6:5678c0b6f74e | 117 | trace_mutex.lock(); |
mbed_official | 6:5678c0b6f74e | 118 | printf("."); |
mbed_official | 6:5678c0b6f74e | 119 | fflush(stdout); |
mbed_official | 6:5678c0b6f74e | 120 | trace_mutex.unlock(); |
mbed_official | 0:4611f6cf2413 | 121 | } |
mbed_official | 0:4611f6cf2413 | 122 | } |
mbed_official | 0:4611f6cf2413 | 123 | } |
mbed_official | 0:4611f6cf2413 | 124 | |
mbed_official | 0:4611f6cf2413 | 125 | /** |
mbed_official | 0:4611f6cf2413 | 126 | * Connects to the Cellular Network |
mbed_official | 0:4611f6cf2413 | 127 | */ |
mbed_official | 0:4611f6cf2413 | 128 | nsapi_error_t do_connect() |
mbed_official | 0:4611f6cf2413 | 129 | { |
mbed_official | 6:5678c0b6f74e | 130 | nsapi_error_t retcode = NSAPI_ERROR_OK; |
mbed_official | 0:4611f6cf2413 | 131 | uint8_t retry_counter = 0; |
mbed_official | 0:4611f6cf2413 | 132 | |
mbed_official | 28:232da3ce8a88 | 133 | while (iface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) { |
mbed_official | 11:23ea0907186e | 134 | retcode = iface->connect(); |
mbed_official | 0:4611f6cf2413 | 135 | if (retcode == NSAPI_ERROR_AUTH_FAILURE) { |
mbed_official | 0:4611f6cf2413 | 136 | print_function("\n\nAuthentication Failure. Exiting application\n"); |
mbed_official | 6:5678c0b6f74e | 137 | } else if (retcode == NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 138 | print_function("\n\nConnection Established.\n"); |
mbed_official | 6:5678c0b6f74e | 139 | } else if (retry_counter > RETRY_COUNT) { |
mbed_official | 6:5678c0b6f74e | 140 | print_function("\n\nFatal connection failure: %d\n", retcode); |
mbed_official | 6:5678c0b6f74e | 141 | } else { |
mbed_official | 6:5678c0b6f74e | 142 | print_function("\n\nCouldn't connect: %d, will retry\n", retcode); |
mbed_official | 0:4611f6cf2413 | 143 | retry_counter++; |
mbed_official | 0:4611f6cf2413 | 144 | continue; |
mbed_official | 0:4611f6cf2413 | 145 | } |
mbed_official | 0:4611f6cf2413 | 146 | break; |
mbed_official | 0:4611f6cf2413 | 147 | } |
mbed_official | 6:5678c0b6f74e | 148 | return retcode; |
mbed_official | 0:4611f6cf2413 | 149 | } |
mbed_official | 0:4611f6cf2413 | 150 | |
mbed_official | 0:4611f6cf2413 | 151 | /** |
mbed_official | 0:4611f6cf2413 | 152 | * Opens a UDP or a TCP socket with the given echo server and performs an echo |
mbed_official | 0:4611f6cf2413 | 153 | * transaction retrieving current. |
mbed_official | 0:4611f6cf2413 | 154 | */ |
mbed_official | 0:4611f6cf2413 | 155 | nsapi_error_t test_send_recv() |
mbed_official | 0:4611f6cf2413 | 156 | { |
mbed_official | 0:4611f6cf2413 | 157 | nsapi_size_or_error_t retcode; |
mbed_official | 0:4611f6cf2413 | 158 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
mbed_official | 0:4611f6cf2413 | 159 | TCPSocket sock; |
mbed_official | 0:4611f6cf2413 | 160 | #else |
mbed_official | 0:4611f6cf2413 | 161 | UDPSocket sock; |
mbed_official | 0:4611f6cf2413 | 162 | #endif |
mbed_official | 0:4611f6cf2413 | 163 | |
mbed_official | 11:23ea0907186e | 164 | retcode = sock.open(iface); |
mbed_official | 0:4611f6cf2413 | 165 | if (retcode != NSAPI_ERROR_OK) { |
mbed_official | 21:e356e039f917 | 166 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
mbed_official | 21:e356e039f917 | 167 | print_function("TCPSocket.open() fails, code: %d\n", retcode); |
mbed_official | 21:e356e039f917 | 168 | #else |
mbed_official | 6:5678c0b6f74e | 169 | print_function("UDPSocket.open() fails, code: %d\n", retcode); |
mbed_official | 21:e356e039f917 | 170 | #endif |
mbed_official | 0:4611f6cf2413 | 171 | return -1; |
mbed_official | 0:4611f6cf2413 | 172 | } |
mbed_official | 0:4611f6cf2413 | 173 | |
mbed_official | 0:4611f6cf2413 | 174 | SocketAddress sock_addr; |
stkim92 | 45:2d57fafd8d73 | 175 | //retcode = iface->gethostbyname(host_name, &sock_addr); |
stkim92 | 45:2d57fafd8d73 | 176 | //if (retcode != NSAPI_ERROR_OK) { |
stkim92 | 45:2d57fafd8d73 | 177 | // print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode); |
stkim92 | 45:2d57fafd8d73 | 178 | // return -1; |
stkim92 | 45:2d57fafd8d73 | 179 | //} |
stkim92 | 45:2d57fafd8d73 | 180 | |
stkim92 | 45:2d57fafd8d73 | 181 | sock_addr.set_ip_address(server_ip_address); |
mbed_official | 0:4611f6cf2413 | 182 | sock_addr.set_port(port); |
mbed_official | 0:4611f6cf2413 | 183 | |
mbed_official | 0:4611f6cf2413 | 184 | sock.set_timeout(15000); |
mbed_official | 0:4611f6cf2413 | 185 | int n = 0; |
stkim92 | 45:2d57fafd8d73 | 186 | const char *msg_string = "TEST"; |
mbed_official | 0:4611f6cf2413 | 187 | char recv_buf[4]; |
mbed_official | 0:4611f6cf2413 | 188 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
mbed_official | 0:4611f6cf2413 | 189 | retcode = sock.connect(sock_addr); |
mbed_official | 0:4611f6cf2413 | 190 | if (retcode < 0) { |
mbed_official | 6:5678c0b6f74e | 191 | print_function("TCPSocket.connect() fails, code: %d\n", retcode); |
mbed_official | 0:4611f6cf2413 | 192 | return -1; |
mbed_official | 0:4611f6cf2413 | 193 | } else { |
stkim92 | 45:2d57fafd8d73 | 194 | //int_function("TCP: connected with %s server\n", host_name); |
stkim92 | 45:2d57fafd8d73 | 195 | //int_function("TCP: connected with %s server\n", sock_addr.get_ip_address()); |
mbed_official | 0:4611f6cf2413 | 196 | } |
stkim92 | 45:2d57fafd8d73 | 197 | retcode = sock.send((void*) msg_string, sizeof(msg_string)); |
mbed_official | 0:4611f6cf2413 | 198 | if (retcode < 0) { |
mbed_official | 6:5678c0b6f74e | 199 | print_function("TCPSocket.send() fails, code: %d\n", retcode); |
mbed_official | 0:4611f6cf2413 | 200 | return -1; |
mbed_official | 0:4611f6cf2413 | 201 | } else { |
stkim92 | 45:2d57fafd8d73 | 202 | //print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name); |
stkim92 | 45:2d57fafd8d73 | 203 | print_function("TCP: Sent %d Bytes to %s\n", retcode, sock_addr.get_ip_address()); |
mbed_official | 0:4611f6cf2413 | 204 | } |
mbed_official | 0:4611f6cf2413 | 205 | |
mbed_official | 0:4611f6cf2413 | 206 | n = sock.recv((void*) recv_buf, sizeof(recv_buf)); |
mbed_official | 0:4611f6cf2413 | 207 | #else |
mbed_official | 0:4611f6cf2413 | 208 | |
stkim92 | 45:2d57fafd8d73 | 209 | retcode = sock.sendto(sock_addr, (void*) msg_string, sizeof(msg_string)); |
mbed_official | 0:4611f6cf2413 | 210 | if (retcode < 0) { |
mbed_official | 6:5678c0b6f74e | 211 | print_function("UDPSocket.sendto() fails, code: %d\n", retcode); |
mbed_official | 0:4611f6cf2413 | 212 | return -1; |
mbed_official | 0:4611f6cf2413 | 213 | } else { |
mbed_official | 6:5678c0b6f74e | 214 | print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name); |
mbed_official | 0:4611f6cf2413 | 215 | } |
mbed_official | 0:4611f6cf2413 | 216 | |
mbed_official | 0:4611f6cf2413 | 217 | n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf)); |
mbed_official | 0:4611f6cf2413 | 218 | #endif |
mbed_official | 0:4611f6cf2413 | 219 | |
mbed_official | 0:4611f6cf2413 | 220 | sock.close(); |
mbed_official | 0:4611f6cf2413 | 221 | |
mbed_official | 0:4611f6cf2413 | 222 | if (n > 0) { |
stkim92 | 45:2d57fafd8d73 | 223 | print_function("Received from server %d Bytes\n", n); |
mbed_official | 0:4611f6cf2413 | 224 | return 0; |
mbed_official | 0:4611f6cf2413 | 225 | } |
mbed_official | 0:4611f6cf2413 | 226 | |
mbed_official | 0:4611f6cf2413 | 227 | return -1; |
mbed_official | 0:4611f6cf2413 | 228 | } |
mbed_official | 0:4611f6cf2413 | 229 | |
mbed_official | 0:4611f6cf2413 | 230 | int main() |
mbed_official | 0:4611f6cf2413 | 231 | { |
mbed_official | 6:5678c0b6f74e | 232 | print_function("\n\nmbed-os-example-cellular\n"); |
mbed_official | 34:6f85d44597ac | 233 | print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__); |
mbed_official | 34:6f85d44597ac | 234 | #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN |
mbed_official | 36:6294a71c9e9e | 235 | print_function("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL")); |
mbed_official | 34:6f85d44597ac | 236 | #endif |
mbed_official | 34:6f85d44597ac | 237 | |
mbed_official | 6:5678c0b6f74e | 238 | print_function("Establishing connection\n"); |
Daniel_Lee | 43:91f11760b50f | 239 | |
Daniel_Lee | 43:91f11760b50f | 240 | BG96_Modem_PowerON(); |
Daniel_Lee | 43:91f11760b50f | 241 | print_function("M2Mnet(BG96) Power ON\n"); |
Daniel_Lee | 43:91f11760b50f | 242 | |
mbed_official | 6:5678c0b6f74e | 243 | #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 244 | trace_open(); |
mbed_official | 6:5678c0b6f74e | 245 | #else |
mbed_official | 6:5678c0b6f74e | 246 | dot_thread.start(dot_event); |
mbed_official | 6:5678c0b6f74e | 247 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 27:97054be1a741 | 248 | |
mbed_official | 27:97054be1a741 | 249 | // sim pin, apn, credentials and possible plmn are taken atuomtically from json when using get_default_instance() |
mbed_official | 28:232da3ce8a88 | 250 | iface = NetworkInterface::get_default_instance(); |
mbed_official | 11:23ea0907186e | 251 | MBED_ASSERT(iface); |
mbed_official | 11:23ea0907186e | 252 | |
mbed_official | 6:5678c0b6f74e | 253 | nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION; |
mbed_official | 0:4611f6cf2413 | 254 | |
mbed_official | 0:4611f6cf2413 | 255 | /* Attempt to connect to a cellular network */ |
mbed_official | 0:4611f6cf2413 | 256 | if (do_connect() == NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 257 | retcode = test_send_recv(); |
mbed_official | 0:4611f6cf2413 | 258 | } |
mbed_official | 0:4611f6cf2413 | 259 | |
mbed_official | 26:348eec457e58 | 260 | if (iface->disconnect() != NSAPI_ERROR_OK) { |
mbed_official | 26:348eec457e58 | 261 | print_function("\n\n disconnect failed.\n\n"); |
mbed_official | 26:348eec457e58 | 262 | } |
mbed_official | 26:348eec457e58 | 263 | |
mbed_official | 6:5678c0b6f74e | 264 | if (retcode == NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 265 | print_function("\n\nSuccess. Exiting \n\n"); |
mbed_official | 6:5678c0b6f74e | 266 | } else { |
mbed_official | 6:5678c0b6f74e | 267 | print_function("\n\nFailure. Exiting \n\n"); |
mbed_official | 6:5678c0b6f74e | 268 | } |
mbed_official | 26:348eec457e58 | 269 | |
mbed_official | 6:5678c0b6f74e | 270 | #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 271 | trace_close(); |
mbed_official | 26:348eec457e58 | 272 | #else |
mbed_official | 26:348eec457e58 | 273 | dot_thread.terminate(); |
mbed_official | 6:5678c0b6f74e | 274 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 275 | |
mbed_official | 6:5678c0b6f74e | 276 | return 0; |
mbed_official | 0:4611f6cf2413 | 277 | } |
mbed_official | 0:4611f6cf2413 | 278 | // EOF |