Skip to content

Commit 8ea7a42

Browse files
author
Teppo Järvelin
committed
Added trace support.
Fixed forever loop if connection fails. Fixed to set modem debug to correct place after settings the credentials so that is actually has an effect to modem debug.
1 parent 9fb7117 commit 8ea7a42

File tree

3 files changed

+100
-32
lines changed

3 files changed

+100
-32
lines changed

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ See the file `mbed_app.json` in the root directory of your application. This fil
4545
"help": "The password string to use for this APN, set to 0 if none",
4646
"value": 0
4747
}
48-
```
48+
```
4949

5050
### Selecting socket type (TCP or UDP)
5151

5252

53-
You can choose which socket type the application should use; however, please note that TCP is a more reliable tranmission protocol. For example:
53+
You can choose which socket type the application should use; however, please note that TCP is a more reliable transmission protocol. For example:
5454

5555

5656
```json
@@ -70,6 +70,25 @@ If you like details and wish to know about all the AT interactions between the m
7070
},
7171
```
7272

73+
### Turning on the tracing and trace level
74+
75+
If you like to add more traces or follow the current ones you can turn traces on by changing `mbed-trace.enable` in mbed_app.json
76+
77+
```"target_overrides": {
78+
"*": {
79+
"target.features_add": ["LWIP", "COMMON_PAL"],
80+
"mbed-trace.enable": false,
81+
```
82+
83+
Changing the trace level is done also in mbed_add.json by changing value in `trace-level`
84+
85+
```"trace-level": {
86+
"help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG",
87+
"macro_name": "MBED_TRACE_MAX_LEVEL",
88+
"value": "TRACE_LEVEL_INFO"
89+
}
90+
```
91+
7392
### Board support
7493

7594
The [generic cellular modem driver](https://github.com/ARMmbed/mbed-os/tree/master/features/netsocket/cellular/generic_modem_driver) this application uses was written using only a standard AT command set. It uses PPP with an Mbed-supported external IP stack. These abilities make the driver essentially generic, or nonvendor specific. However, this particular driver is for onboard-modem types. In other words, the modem exists on the Mbed Enabled target as opposed to plug-in modules (shields). For more details, please see our [Mbed OS cellular documentation](https://os.mbed.com/docs/latest/reference/cellular-api.html).
@@ -94,7 +113,7 @@ You should see an output similar to this:
94113

95114
```
96115
mbed-os-example-cellular
97-
Establishing connection ......
116+
Establishing connection ......
98117
99118
Connection Established.
100119
TCP: connected with echo.mbedcloudtesting.com server

main.cpp

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "common_functions.h"
1919
#include "UDPSocket.h"
2020
#include "OnboardCellularInterface.h"
21+
#include "CellularLog.h"
2122

2223
#define UDP 0
2324
#define TCP 1
@@ -51,22 +52,58 @@ const char *host_name = "echo.mbedcloudtesting.com";
5152
// Echo server port (same for TCP and UDP)
5253
const int port = 7;
5354

54-
Mutex PrintMutex;
55+
static rtos::Mutex trace_mutex;
56+
57+
#if MBED_CONF_MBED_TRACE_ENABLE
58+
static void trace_wait()
59+
{
60+
trace_mutex.lock();
61+
}
62+
63+
static void trace_release()
64+
{
65+
trace_mutex.unlock();
66+
}
67+
68+
69+
static uint32_t cellular_starttime = us_ticker_read() / 1000L;
70+
static char time_st[50];
71+
72+
static char* trace_time(size_t ss)
73+
{
74+
snprintf(time_st, 49, "[%08lums]", ((us_ticker_read()-cellular_starttime) / 1000L));
75+
return time_st;
76+
}
77+
78+
static void trace_open()
79+
{
80+
mbed_trace_init();
81+
mbed_trace_prefix_function_set( &trace_time );
82+
83+
mbed_trace_mutex_wait_function_set(trace_wait);
84+
mbed_trace_mutex_release_function_set(trace_release);
85+
}
86+
87+
static void trace_close()
88+
{
89+
mbed_trace_free();
90+
}
91+
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
92+
5593
Thread dot_thread(osPriorityNormal, 512);
5694

5795
#define PRINT_TEXT_LENGTH 128
5896
char print_text[PRINT_TEXT_LENGTH];
5997
void print_function(const char *input_string)
6098
{
61-
PrintMutex.lock();
99+
trace_mutex.lock();
62100
printf("%s", input_string);
63101
fflush(NULL);
64-
PrintMutex.unlock();
102+
trace_mutex.unlock();
65103
}
66104

67105
void dot_event()
68106
{
69-
70107
while (true) {
71108
wait(4);
72109
if (!iface.is_connected()) {
@@ -75,41 +112,36 @@ void dot_event()
75112
break;
76113
}
77114
}
78-
79115
}
80116

81-
82117
/**
83118
* Connects to the Cellular Network
84119
*/
85120
nsapi_error_t do_connect()
86121
{
87-
nsapi_error_t retcode;
122+
nsapi_error_t retcode = NSAPI_ERROR_OK;
88123
uint8_t retry_counter = 0;
89124

90125
while (!iface.is_connected()) {
91-
92126
retcode = iface.connect();
93127
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
94128
print_function("\n\nAuthentication Failure. Exiting application\n");
95-
return retcode;
96-
} else if (retcode != NSAPI_ERROR_OK) {
129+
} else if (retcode == NSAPI_ERROR_OK) {
130+
print_function("\n\nConnection Established.\n");
131+
} else if (retry_counter > RETRY_COUNT) {
132+
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
133+
print_function(print_text);
134+
} else {
97135
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nCouldn't connect: %d, will retry\n", retcode);
98136
print_function(print_text);
99137
retry_counter++;
100138
continue;
101-
} else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
102-
snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
103-
print_function(print_text);
104-
return retcode;
105139
}
106-
107140
break;
108141
}
109142

110-
print_function("\n\nConnection Established.\n");
111-
112-
return NSAPI_ERROR_OK;
143+
dot_thread.terminate();
144+
return retcode;
113145
}
114146

115147
/**
@@ -196,28 +228,39 @@ nsapi_error_t test_send_recv()
196228

197229
int main()
198230
{
231+
print_function("\n\nmbed-os-example-cellular\n");
199232

200-
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
233+
#if MBED_CONF_MBED_TRACE_ENABLE
234+
trace_open();
235+
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
201236
/* Set Pin code for SIM card */
202237
iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE);
203238

204-
/* Set network credentials here, e.g., APN*/
239+
/* Set network credentials here, e.g., APN */
205240
iface.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
206241

207-
print_function("\n\nmbed-os-example-cellular\n");
242+
/* Set the modem debug on/off */
243+
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
244+
208245
print_function("Establishing connection ");
209246
dot_thread.start(dot_event);
210247

248+
nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
249+
211250
/* Attempt to connect to a cellular network */
212251
if (do_connect() == NSAPI_ERROR_OK) {
213-
nsapi_error_t retcode = test_send_recv();
214-
if (retcode == NSAPI_ERROR_OK) {
215-
print_function("\n\nSuccess. Exiting \n\n");
216-
return 0;
217-
}
252+
retcode = test_send_recv();
218253
}
219254

220-
print_function("\n\nFailure. Exiting \n\n");
221-
return -1;
255+
if (retcode == NSAPI_ERROR_OK) {
256+
print_function("\n\nSuccess. Exiting \n\n");
257+
} else {
258+
print_function("\n\nFailure. Exiting \n\n");
259+
}
260+
#if MBED_CONF_MBED_TRACE_ENABLE
261+
trace_close();
262+
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
263+
264+
return 0;
222265
}
223266
// EOF

mbed_app.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020
"password": {
2121
"help": "The password string to use for this APN, set to 0 if none",
2222
"value": 0
23+
},
24+
"trace-level": {
25+
"help": "Options are TRACE_LEVEL_ERROR,TRACE_LEVEL_WARN,TRACE_LEVEL_INFO,TRACE_LEVEL_DEBUG",
26+
"macro_name": "MBED_TRACE_MAX_LEVEL",
27+
"value": "TRACE_LEVEL_INFO"
2328
}
24-
},
29+
},
2530
"target_overrides": {
2631
"*": {
2732
"target.features_add": ["LWIP", "COMMON_PAL"],
33+
"mbed-trace.enable": false,
2834
"lwip.ipv4-enabled": true,
2935
"lwip.ethernet-enabled": false,
3036
"lwip.ppp-enabled": true,

0 commit comments

Comments
 (0)