Skip to content

Commit 2ba7c35

Browse files
committed
Otap: better handle otap exchange with neighbors
When a node exchanges a scatchpad to one of its neighbors, the dual mcu is not able to serve UART requests. And dual mcu app for now doesn't discard old requests from host. So better handle this situation to avoid detecting an uart issue when it is just an OTAP exchange. Also remove some redundant traces (especially printing received buffers).
1 parent 80bfc9a commit 2ba7c35

File tree

5 files changed

+47
-31
lines changed

5 files changed

+47
-31
lines changed

lib/platform/linux/platform.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ static void * poll_for_indication(void * unused)
214214
}
215215

216216
#if MAX_DURATION_POLL_FAIL_S != 0
217-
if (get_ind_res >= 0)
217+
if (get_ind_res >= 0 || get_ind_res == WPC_INT_SYNC_ERROR)
218218
{
219-
// Poll request executed fine, reset fail counter
219+
// Poll request executed fine or at least com is working with sink, reset fail counter
220220
m_last_successful_poll_ts = get_timestamp_s();
221221
}
222222
else

lib/platform/linux/serial.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "serial_termios2.h"
1717

1818
#define LOG_MODULE_NAME "SERIAL"
19-
#define MAX_LOG_LEVEL DEBUG_LOG_LEVEL
19+
#define MAX_LOG_LEVEL INFO_LOG_LEVEL
2020
#include "logger.h"
2121

2222
static int fd = -1;
@@ -143,7 +143,7 @@ int Serial_close()
143143

144144
if (close(fd) < 0)
145145
{
146-
LOGD("Error %d closing serial link: %s\n", errno, strerror (errno));
146+
LOGW("Error %d closing serial link: %s\n", errno, strerror (errno));
147147
return -1;
148148
}
149149

lib/wpc/include/wpc_internal.h

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88

99
#include "wpc_types.h"
1010

11+
typedef enum {
12+
WPC_INT_GEN_ERROR = -1, //< Generic error code
13+
WPC_INT_TIMEOUT_ERROR = -2, //< Timeout error
14+
WPC_INT_SYNC_ERROR = -3, //< Synchronization error
15+
WPC_INT_WRONG_PARAM_ERROR = -4, //< Wrong parameter
16+
WPC_INT_WRONG_CRC = -5, //< Wrong crc
17+
WPC_INT_WRONG_BUFFER_SIZE = -6, //< Wrong provided buffer size
18+
} WPC_Int_error_code_e;
19+
1120
/**
1221
* \brief Callback to be called when an indication is received
1322
* \param frame

lib/wpc/slip.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//#define PRINT_BUFFERS
1313
#include "logger.h"
1414

15+
#include "wpc_internal.h"
1516
#include "slip.h"
1617
#include "util.h"
1718

@@ -138,13 +139,13 @@ static uint32_t slip_encode_buffer(uint8_t * buffer,
138139
if (current == END_SLIP_OCTET || current == ESC_SLIP_OCTET) {
139140
//Check for buffer_escaped overflow
140141
if (write >= len_escaped)
141-
return -1;
142+
return WPC_INT_WRONG_BUFFER_SIZE;
142143
buffer_escaped[write++] = ESC_SLIP_OCTET;
143144
current = (current == END_SLIP_OCTET ? END_SUBS_OCTET : ESC_SUBS_OCTET);
144145
}
145146
//Check for buffer_escaped overflow
146147
if (write >= len_escaped)
147-
return -1;
148+
return WPC_INT_WRONG_BUFFER_SIZE;
148149

149150
buffer_escaped[write++] = current;
150151
}
@@ -167,7 +168,7 @@ int Slip_decode(uint8_t * buffer,
167168
{
168169
LOG_PRINT_BUFFER(buffer, len);
169170
LOGE("Wrong crc 0x%04x (computed) vs 0x%04x (received)\n", crc, crc_from_frame);
170-
return -1;
171+
return WPC_INT_WRONG_CRC;
171172
}
172173

173174
return decoded_len - 2;
@@ -191,7 +192,7 @@ int Slip_encode(uint8_t * buffer_in,
191192
if (total_size == -1)
192193
{
193194
LOGE("Provided buffer in encode is too small\n");
194-
return -1;
195+
return WPC_INT_WRONG_BUFFER_SIZE;
195196
}
196197

197198
//Add the escaped crc to the end of the buffer in LE
@@ -204,7 +205,7 @@ int Slip_encode(uint8_t * buffer_in,
204205
if (temp_size == -1)
205206
{
206207
LOGE("Provided buffer in encode is too small\n");
207-
return -1;
208+
return WPC_INT_WRONG_BUFFER_SIZE;
208209
}
209210

210211
total_size += temp_size;
@@ -240,7 +241,7 @@ int Slip_send_buffer(uint8_t * buffer,
240241
if (written_size != size + 4)
241242
{
242243
LOGE("Not able to write all the encoded packet %d vs %d\n", written_size, size + 4);
243-
return -1;
244+
return WPC_INT_GEN_ERROR;
244245
}
245246

246247
return 0;
@@ -269,7 +270,7 @@ int Slip_get_buffer(uint8_t * buffer,
269270
if (res == 0)
270271
{
271272
LOGW("Timeout to receive frame (size=%d)\n", size);
272-
return -1;
273+
return WPC_INT_TIMEOUT_ERROR;
273274
}
274275
else if (res < 0 || res > 1)
275276
{
@@ -321,7 +322,7 @@ int Slip_get_buffer(uint8_t * buffer,
321322
if (size > sizeof(receiving_buffer))
322323
{
323324
LOGE("Receiving too much bytes from serial line %d vs %d\n", size, sizeof(receiving_buffer));
324-
return -1;
325+
return WPC_INT_GEN_ERROR;
325326
}
326327
}
327328
else
@@ -346,7 +347,7 @@ int Slip_init(write_f write,
346347
read_f read)
347348
{
348349
if (!write || !read)
349-
return -1;
350+
return WPC_INT_WRONG_PARAM_ERROR;
350351

351352
write_function = write;
352353
read_function = read;

lib/wpc/wpc_internal.c

+24-18
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
// the stack for a request
3333
#define TIMEOUT_CONFIRM_MS 500
3434

35-
// Attempt to receive confirm. In some seldom use cases it can happen
36-
// that a previous confirm request was still in the buffer. Just try several
37-
// times to get the right confirm
38-
#define MAX_CONFIRM_ATTEMPT 5
35+
// Attempt to receive confirm from a request.
36+
// In some cases like OTAP it can happen that Dual MCU app doesn't answer
37+
// for a long time (can be up to 1mins when exchanging an OTAP with a neighbor).
38+
// So all the poll requests done during this period can be received in a raw
39+
// So this mechanism allows to flush previous poll requests. Try several
40+
// times to get the right confirm.
41+
#define MAX_CONFIRM_ATTEMPT 50
3942

4043
// Struct that describes a received frame with its timestamp
4144
typedef struct
@@ -106,7 +109,7 @@ static int send_request_locked(wpc_frame_t * request,
106109
if (Slip_send_buffer((uint8_t *) request, request->payload_length + 3) < 0)
107110
{
108111
LOGE("Cannot send request\n");
109-
return -1;
112+
return WPC_INT_GEN_ERROR;
110113
}
111114

112115
while (attempt < MAX_CONFIRM_ATTEMPT)
@@ -116,7 +119,8 @@ static int send_request_locked(wpc_frame_t * request,
116119
if (confirm_size < 0)
117120
{
118121
LOGE("Didn't receive answer to the request 0x%02x\n", request->primitive_id);
119-
return -1;
122+
// Return confirm_size to propagate the error code
123+
return confirm_size;
120124
}
121125

122126
// Check the confirm
@@ -127,7 +131,7 @@ static int send_request_locked(wpc_frame_t * request,
127131
LOGW("Waiting confirm for primitive_id 0x%02x but received 0x%02x\n",
128132
request->primitive_id + SAP_CONFIRM_OFFSET,
129133
rec_confirm->primitive_id);
130-
LOG_PRINT_BUFFER(buffer, FRAME_SIZE((wpc_frame_t *) buffer));
134+
//LOG_PRINT_BUFFER(buffer, FRAME_SIZE((wpc_frame_t *) buffer));
131135
attempt++;
132136
continue;
133137
}
@@ -136,7 +140,7 @@ static int send_request_locked(wpc_frame_t * request,
136140
LOGW("Waiting confirm for frame_id 0x%02x but received 0x%02x\n",
137141
request->frame_id,
138142
rec_confirm->frame_id);
139-
LOG_PRINT_BUFFER(buffer, FRAME_SIZE((wpc_frame_t *) buffer));
143+
//LOG_PRINT_BUFFER(buffer, FRAME_SIZE((wpc_frame_t *) buffer));
140144
attempt++;
141145
continue;
142146
}
@@ -146,7 +150,7 @@ static int send_request_locked(wpc_frame_t * request,
146150
if (attempt == MAX_CONFIRM_ATTEMPT)
147151
{
148152
LOGE("Synchronization lost\n");
149-
return -1;
153+
return WPC_INT_SYNC_ERROR;
150154
}
151155

152156
// Copy the confirm
@@ -209,7 +213,7 @@ static int handle_indication(bool last_one,
209213
if (res <= 0)
210214
{
211215
LOGE("Timeout waiting for indication last_one=%d\n", last_one);
212-
return -1;
216+
return WPC_INT_TIMEOUT_ERROR;
213217
}
214218

215219
// Get timestamp just after reception
@@ -246,25 +250,27 @@ static int get_indication_locked(unsigned int max_ind,
246250
wpc_frame_t request;
247251
wpc_frame_t confirm;
248252
int remaining_ind = 1;
253+
int ret;
249254

250255
if (max_ind == 0)
251256
{
252257
// Poll request cannot be done if no indication
253258
// can be handled
254259
LOGE("Wrong number of indication\n");
255-
return -1;
260+
return WPC_INT_WRONG_PARAM_ERROR;
256261
}
257262

258263
request.primitive_id = MSAP_INDICATION_POLL_REQUEST;
259264
request.payload_length = 0;
260265

261266
LOGD("Start a poll request\n");
262-
if (send_request_locked(&request,
263-
&confirm,
264-
TIMEOUT_CONFIRM_MS) < 0)
267+
ret = send_request_locked(&request,
268+
&confirm,
269+
TIMEOUT_CONFIRM_MS);
270+
if (ret < 0)
265271
{
266272
LOGE("Unable to poll for request\n");
267-
return -1;
273+
return ret;
268274
}
269275

270276
if (confirm.payload.sap_generic_confirm_payload.result == 0)
@@ -281,7 +287,7 @@ static int get_indication_locked(unsigned int max_ind,
281287
if (remaining_ind < 0)
282288
{
283289
LOGE("Cannot get an indication\n");
284-
return -1;
290+
return remaining_ind;
285291
}
286292
}
287293

@@ -320,15 +326,15 @@ int WPC_Int_initialize(char * port_name, unsigned long bitrate)
320326
{
321327
// Open the serial connection
322328
if (Serial_open(port_name, bitrate) < 0)
323-
return -1;
329+
return WPC_INT_GEN_ERROR;
324330

325331
// Initialize the slip module
326332
Slip_init(&Serial_write, &Serial_read);
327333

328334
if (!Platform_init())
329335
{
330336
Serial_close();
331-
return -1;
337+
return WPC_INT_GEN_ERROR;
332338
}
333339

334340
dsap_init();

0 commit comments

Comments
 (0)