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