![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mbed os example for Wislink RAK2011
main.cpp@6:5678c0b6f74e, 2018-05-10 (annotated)
- Committer:
- mbed_official
- Date:
- Thu May 10 05:17:02 2018 +0100
- Revision:
- 6:5678c0b6f74e
- Parent:
- 2:0f644d6045cf
- Child:
- 11:23ea0907186e
Merge pull request #63 from ARMmbed/adding_trace_support_and_bug_fixes
Added trace support.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-cellular
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 | 0:4611f6cf2413 | 20 | #include "OnboardCellularInterface.h" |
mbed_official | 6:5678c0b6f74e | 21 | #include "CellularLog.h" |
mbed_official | 0:4611f6cf2413 | 22 | |
mbed_official | 0:4611f6cf2413 | 23 | #define UDP 0 |
mbed_official | 0:4611f6cf2413 | 24 | #define TCP 1 |
mbed_official | 0:4611f6cf2413 | 25 | |
mbed_official | 0:4611f6cf2413 | 26 | // SIM pin code goes here |
mbed_official | 0:4611f6cf2413 | 27 | #ifndef MBED_CONF_APP_SIM_PIN_CODE |
mbed_official | 0:4611f6cf2413 | 28 | # define MBED_CONF_APP_SIM_PIN_CODE "1234" |
mbed_official | 0:4611f6cf2413 | 29 | #endif |
mbed_official | 0:4611f6cf2413 | 30 | |
mbed_official | 0:4611f6cf2413 | 31 | #ifndef MBED_CONF_APP_APN |
mbed_official | 0:4611f6cf2413 | 32 | # define MBED_CONF_APP_APN "internet" |
mbed_official | 0:4611f6cf2413 | 33 | #endif |
mbed_official | 0:4611f6cf2413 | 34 | #ifndef MBED_CONF_APP_USERNAME |
mbed_official | 0:4611f6cf2413 | 35 | # define MBED_CONF_APP_USERNAME NULL |
mbed_official | 0:4611f6cf2413 | 36 | #endif |
mbed_official | 0:4611f6cf2413 | 37 | #ifndef MBED_CONF_APP_PASSWORD |
mbed_official | 0:4611f6cf2413 | 38 | # define MBED_CONF_APP_PASSWORD NULL |
mbed_official | 0:4611f6cf2413 | 39 | #endif |
mbed_official | 0:4611f6cf2413 | 40 | |
mbed_official | 0:4611f6cf2413 | 41 | // Number of retries / |
mbed_official | 0:4611f6cf2413 | 42 | #define RETRY_COUNT 3 |
mbed_official | 0:4611f6cf2413 | 43 | |
mbed_official | 0:4611f6cf2413 | 44 | |
mbed_official | 0:4611f6cf2413 | 45 | |
mbed_official | 0:4611f6cf2413 | 46 | // CellularInterface object |
mbed_official | 0:4611f6cf2413 | 47 | OnboardCellularInterface iface; |
mbed_official | 0:4611f6cf2413 | 48 | |
mbed_official | 0:4611f6cf2413 | 49 | // Echo server hostname |
mbed_official | 2:0f644d6045cf | 50 | const char *host_name = "echo.mbedcloudtesting.com"; |
mbed_official | 0:4611f6cf2413 | 51 | |
mbed_official | 0:4611f6cf2413 | 52 | // Echo server port (same for TCP and UDP) |
mbed_official | 0:4611f6cf2413 | 53 | const int port = 7; |
mbed_official | 0:4611f6cf2413 | 54 | |
mbed_official | 6:5678c0b6f74e | 55 | static rtos::Mutex trace_mutex; |
mbed_official | 6:5678c0b6f74e | 56 | |
mbed_official | 6:5678c0b6f74e | 57 | #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 58 | static void trace_wait() |
mbed_official | 6:5678c0b6f74e | 59 | { |
mbed_official | 6:5678c0b6f74e | 60 | trace_mutex.lock(); |
mbed_official | 6:5678c0b6f74e | 61 | } |
mbed_official | 6:5678c0b6f74e | 62 | |
mbed_official | 6:5678c0b6f74e | 63 | static void trace_release() |
mbed_official | 6:5678c0b6f74e | 64 | { |
mbed_official | 6:5678c0b6f74e | 65 | trace_mutex.unlock(); |
mbed_official | 6:5678c0b6f74e | 66 | } |
mbed_official | 6:5678c0b6f74e | 67 | |
mbed_official | 6:5678c0b6f74e | 68 | static char time_st[50]; |
mbed_official | 6:5678c0b6f74e | 69 | |
mbed_official | 6:5678c0b6f74e | 70 | static char* trace_time(size_t ss) |
mbed_official | 6:5678c0b6f74e | 71 | { |
mbed_official | 6:5678c0b6f74e | 72 | snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count()); |
mbed_official | 6:5678c0b6f74e | 73 | return time_st; |
mbed_official | 6:5678c0b6f74e | 74 | } |
mbed_official | 6:5678c0b6f74e | 75 | |
mbed_official | 6:5678c0b6f74e | 76 | static void trace_open() |
mbed_official | 6:5678c0b6f74e | 77 | { |
mbed_official | 6:5678c0b6f74e | 78 | mbed_trace_init(); |
mbed_official | 6:5678c0b6f74e | 79 | mbed_trace_prefix_function_set( &trace_time ); |
mbed_official | 6:5678c0b6f74e | 80 | |
mbed_official | 6:5678c0b6f74e | 81 | mbed_trace_mutex_wait_function_set(trace_wait); |
mbed_official | 6:5678c0b6f74e | 82 | mbed_trace_mutex_release_function_set(trace_release); |
mbed_official | 6:5678c0b6f74e | 83 | } |
mbed_official | 6:5678c0b6f74e | 84 | |
mbed_official | 6:5678c0b6f74e | 85 | static void trace_close() |
mbed_official | 6:5678c0b6f74e | 86 | { |
mbed_official | 6:5678c0b6f74e | 87 | mbed_trace_free(); |
mbed_official | 6:5678c0b6f74e | 88 | } |
mbed_official | 6:5678c0b6f74e | 89 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 90 | |
mbed_official | 2:0f644d6045cf | 91 | Thread dot_thread(osPriorityNormal, 512); |
mbed_official | 0:4611f6cf2413 | 92 | |
mbed_official | 6:5678c0b6f74e | 93 | void print_function(const char *format, ...) |
mbed_official | 0:4611f6cf2413 | 94 | { |
mbed_official | 6:5678c0b6f74e | 95 | trace_mutex.lock(); |
mbed_official | 6:5678c0b6f74e | 96 | va_list arglist; |
mbed_official | 6:5678c0b6f74e | 97 | va_start( arglist, format ); |
mbed_official | 6:5678c0b6f74e | 98 | vprintf(format, arglist); |
mbed_official | 6:5678c0b6f74e | 99 | va_end( arglist ); |
mbed_official | 6:5678c0b6f74e | 100 | trace_mutex.unlock(); |
mbed_official | 0:4611f6cf2413 | 101 | } |
mbed_official | 0:4611f6cf2413 | 102 | |
mbed_official | 0:4611f6cf2413 | 103 | void dot_event() |
mbed_official | 0:4611f6cf2413 | 104 | { |
mbed_official | 0:4611f6cf2413 | 105 | while (true) { |
mbed_official | 6:5678c0b6f74e | 106 | Thread::wait(4000); |
mbed_official | 0:4611f6cf2413 | 107 | if (!iface.is_connected()) { |
mbed_official | 6:5678c0b6f74e | 108 | trace_mutex.lock(); |
mbed_official | 6:5678c0b6f74e | 109 | printf("."); |
mbed_official | 6:5678c0b6f74e | 110 | fflush(stdout); |
mbed_official | 6:5678c0b6f74e | 111 | trace_mutex.unlock(); |
mbed_official | 0:4611f6cf2413 | 112 | } else { |
mbed_official | 0:4611f6cf2413 | 113 | break; |
mbed_official | 0:4611f6cf2413 | 114 | } |
mbed_official | 0:4611f6cf2413 | 115 | } |
mbed_official | 0:4611f6cf2413 | 116 | } |
mbed_official | 0:4611f6cf2413 | 117 | |
mbed_official | 0:4611f6cf2413 | 118 | /** |
mbed_official | 0:4611f6cf2413 | 119 | * Connects to the Cellular Network |
mbed_official | 0:4611f6cf2413 | 120 | */ |
mbed_official | 0:4611f6cf2413 | 121 | nsapi_error_t do_connect() |
mbed_official | 0:4611f6cf2413 | 122 | { |
mbed_official | 6:5678c0b6f74e | 123 | nsapi_error_t retcode = NSAPI_ERROR_OK; |
mbed_official | 0:4611f6cf2413 | 124 | uint8_t retry_counter = 0; |
mbed_official | 0:4611f6cf2413 | 125 | |
mbed_official | 0:4611f6cf2413 | 126 | while (!iface.is_connected()) { |
mbed_official | 0:4611f6cf2413 | 127 | retcode = iface.connect(); |
mbed_official | 0:4611f6cf2413 | 128 | if (retcode == NSAPI_ERROR_AUTH_FAILURE) { |
mbed_official | 0:4611f6cf2413 | 129 | print_function("\n\nAuthentication Failure. Exiting application\n"); |
mbed_official | 6:5678c0b6f74e | 130 | } else if (retcode == NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 131 | print_function("\n\nConnection Established.\n"); |
mbed_official | 6:5678c0b6f74e | 132 | } else if (retry_counter > RETRY_COUNT) { |
mbed_official | 6:5678c0b6f74e | 133 | print_function("\n\nFatal connection failure: %d\n", retcode); |
mbed_official | 6:5678c0b6f74e | 134 | } else { |
mbed_official | 6:5678c0b6f74e | 135 | print_function("\n\nCouldn't connect: %d, will retry\n", retcode); |
mbed_official | 0:4611f6cf2413 | 136 | retry_counter++; |
mbed_official | 0:4611f6cf2413 | 137 | continue; |
mbed_official | 0:4611f6cf2413 | 138 | } |
mbed_official | 0:4611f6cf2413 | 139 | break; |
mbed_official | 0:4611f6cf2413 | 140 | } |
mbed_official | 6:5678c0b6f74e | 141 | return retcode; |
mbed_official | 0:4611f6cf2413 | 142 | } |
mbed_official | 0:4611f6cf2413 | 143 | |
mbed_official | 0:4611f6cf2413 | 144 | /** |
mbed_official | 0:4611f6cf2413 | 145 | * Opens a UDP or a TCP socket with the given echo server and performs an echo |
mbed_official | 0:4611f6cf2413 | 146 | * transaction retrieving current. |
mbed_official | 0:4611f6cf2413 | 147 | */ |
mbed_official | 0:4611f6cf2413 | 148 | nsapi_error_t test_send_recv() |
mbed_official | 0:4611f6cf2413 | 149 | { |
mbed_official | 0:4611f6cf2413 | 150 | nsapi_size_or_error_t retcode; |
mbed_official | 0:4611f6cf2413 | 151 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
mbed_official | 0:4611f6cf2413 | 152 | TCPSocket sock; |
mbed_official | 0:4611f6cf2413 | 153 | #else |
mbed_official | 0:4611f6cf2413 | 154 | UDPSocket sock; |
mbed_official | 0:4611f6cf2413 | 155 | #endif |
mbed_official | 0:4611f6cf2413 | 156 | |
mbed_official | 0:4611f6cf2413 | 157 | retcode = sock.open(&iface); |
mbed_official | 0:4611f6cf2413 | 158 | if (retcode != NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 159 | print_function("UDPSocket.open() fails, code: %d\n", retcode); |
mbed_official | 0:4611f6cf2413 | 160 | return -1; |
mbed_official | 0:4611f6cf2413 | 161 | } |
mbed_official | 0:4611f6cf2413 | 162 | |
mbed_official | 0:4611f6cf2413 | 163 | SocketAddress sock_addr; |
mbed_official | 0:4611f6cf2413 | 164 | retcode = iface.gethostbyname(host_name, &sock_addr); |
mbed_official | 0:4611f6cf2413 | 165 | if (retcode != NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 166 | print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode); |
mbed_official | 0:4611f6cf2413 | 167 | return -1; |
mbed_official | 0:4611f6cf2413 | 168 | } |
mbed_official | 0:4611f6cf2413 | 169 | |
mbed_official | 0:4611f6cf2413 | 170 | sock_addr.set_port(port); |
mbed_official | 0:4611f6cf2413 | 171 | |
mbed_official | 0:4611f6cf2413 | 172 | sock.set_timeout(15000); |
mbed_official | 0:4611f6cf2413 | 173 | int n = 0; |
mbed_official | 0:4611f6cf2413 | 174 | const char *echo_string = "TEST"; |
mbed_official | 0:4611f6cf2413 | 175 | char recv_buf[4]; |
mbed_official | 0:4611f6cf2413 | 176 | #if MBED_CONF_APP_SOCK_TYPE == TCP |
mbed_official | 0:4611f6cf2413 | 177 | retcode = sock.connect(sock_addr); |
mbed_official | 0:4611f6cf2413 | 178 | if (retcode < 0) { |
mbed_official | 6:5678c0b6f74e | 179 | print_function("TCPSocket.connect() fails, code: %d\n", retcode); |
mbed_official | 0:4611f6cf2413 | 180 | return -1; |
mbed_official | 0:4611f6cf2413 | 181 | } else { |
mbed_official | 6:5678c0b6f74e | 182 | print_function("TCP: connected with %s server\n", host_name); |
mbed_official | 0:4611f6cf2413 | 183 | } |
mbed_official | 0:4611f6cf2413 | 184 | retcode = sock.send((void*) echo_string, sizeof(echo_string)); |
mbed_official | 0:4611f6cf2413 | 185 | if (retcode < 0) { |
mbed_official | 6:5678c0b6f74e | 186 | print_function("TCPSocket.send() fails, code: %d\n", retcode); |
mbed_official | 0:4611f6cf2413 | 187 | return -1; |
mbed_official | 0:4611f6cf2413 | 188 | } else { |
mbed_official | 6:5678c0b6f74e | 189 | print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name); |
mbed_official | 0:4611f6cf2413 | 190 | } |
mbed_official | 0:4611f6cf2413 | 191 | |
mbed_official | 0:4611f6cf2413 | 192 | n = sock.recv((void*) recv_buf, sizeof(recv_buf)); |
mbed_official | 0:4611f6cf2413 | 193 | #else |
mbed_official | 0:4611f6cf2413 | 194 | |
mbed_official | 0:4611f6cf2413 | 195 | retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string)); |
mbed_official | 0:4611f6cf2413 | 196 | if (retcode < 0) { |
mbed_official | 6:5678c0b6f74e | 197 | print_function("UDPSocket.sendto() fails, code: %d\n", retcode); |
mbed_official | 0:4611f6cf2413 | 198 | return -1; |
mbed_official | 0:4611f6cf2413 | 199 | } else { |
mbed_official | 6:5678c0b6f74e | 200 | print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name); |
mbed_official | 0:4611f6cf2413 | 201 | } |
mbed_official | 0:4611f6cf2413 | 202 | |
mbed_official | 0:4611f6cf2413 | 203 | n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf)); |
mbed_official | 0:4611f6cf2413 | 204 | #endif |
mbed_official | 0:4611f6cf2413 | 205 | |
mbed_official | 0:4611f6cf2413 | 206 | sock.close(); |
mbed_official | 0:4611f6cf2413 | 207 | |
mbed_official | 0:4611f6cf2413 | 208 | if (n > 0) { |
mbed_official | 6:5678c0b6f74e | 209 | print_function("Received from echo server %d Bytes\n", n); |
mbed_official | 0:4611f6cf2413 | 210 | return 0; |
mbed_official | 0:4611f6cf2413 | 211 | } |
mbed_official | 0:4611f6cf2413 | 212 | |
mbed_official | 0:4611f6cf2413 | 213 | return -1; |
mbed_official | 0:4611f6cf2413 | 214 | } |
mbed_official | 0:4611f6cf2413 | 215 | |
mbed_official | 0:4611f6cf2413 | 216 | int main() |
mbed_official | 0:4611f6cf2413 | 217 | { |
mbed_official | 6:5678c0b6f74e | 218 | print_function("\n\nmbed-os-example-cellular\n"); |
mbed_official | 6:5678c0b6f74e | 219 | print_function("Establishing connection\n"); |
mbed_official | 6:5678c0b6f74e | 220 | #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 221 | trace_open(); |
mbed_official | 6:5678c0b6f74e | 222 | #else |
mbed_official | 6:5678c0b6f74e | 223 | dot_thread.start(dot_event); |
mbed_official | 6:5678c0b6f74e | 224 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 0:4611f6cf2413 | 225 | /* Set Pin code for SIM card */ |
mbed_official | 0:4611f6cf2413 | 226 | iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE); |
mbed_official | 0:4611f6cf2413 | 227 | |
mbed_official | 6:5678c0b6f74e | 228 | /* Set network credentials here, e.g., APN */ |
mbed_official | 0:4611f6cf2413 | 229 | iface.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD); |
mbed_official | 0:4611f6cf2413 | 230 | |
mbed_official | 6:5678c0b6f74e | 231 | /* Set the modem debug on/off */ |
mbed_official | 6:5678c0b6f74e | 232 | iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE); |
mbed_official | 6:5678c0b6f74e | 233 | |
mbed_official | 6:5678c0b6f74e | 234 | nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION; |
mbed_official | 0:4611f6cf2413 | 235 | |
mbed_official | 0:4611f6cf2413 | 236 | /* Attempt to connect to a cellular network */ |
mbed_official | 0:4611f6cf2413 | 237 | if (do_connect() == NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 238 | retcode = test_send_recv(); |
mbed_official | 0:4611f6cf2413 | 239 | } |
mbed_official | 0:4611f6cf2413 | 240 | |
mbed_official | 6:5678c0b6f74e | 241 | if (retcode == NSAPI_ERROR_OK) { |
mbed_official | 6:5678c0b6f74e | 242 | print_function("\n\nSuccess. Exiting \n\n"); |
mbed_official | 6:5678c0b6f74e | 243 | } else { |
mbed_official | 6:5678c0b6f74e | 244 | print_function("\n\nFailure. Exiting \n\n"); |
mbed_official | 6:5678c0b6f74e | 245 | } |
mbed_official | 6:5678c0b6f74e | 246 | #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 247 | trace_close(); |
mbed_official | 6:5678c0b6f74e | 248 | #endif // #if MBED_CONF_MBED_TRACE_ENABLE |
mbed_official | 6:5678c0b6f74e | 249 | |
mbed_official | 6:5678c0b6f74e | 250 | return 0; |
mbed_official | 0:4611f6cf2413 | 251 | } |
mbed_official | 0:4611f6cf2413 | 252 | // EOF |