32
32
// the stack for a request
33
33
#define TIMEOUT_CONFIRM_MS 500
34
34
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
39
42
40
43
// Struct that describes a received frame with its timestamp
41
44
typedef struct
@@ -106,7 +109,7 @@ static int send_request_locked(wpc_frame_t * request,
106
109
if (Slip_send_buffer ((uint8_t * ) request , request -> payload_length + 3 ) < 0 )
107
110
{
108
111
LOGE ("Cannot send request\n" );
109
- return -1 ;
112
+ return WPC_INT_GEN_ERROR ;
110
113
}
111
114
112
115
while (attempt < MAX_CONFIRM_ATTEMPT )
@@ -116,7 +119,8 @@ static int send_request_locked(wpc_frame_t * request,
116
119
if (confirm_size < 0 )
117
120
{
118
121
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 ;
120
124
}
121
125
122
126
// Check the confirm
@@ -127,7 +131,7 @@ static int send_request_locked(wpc_frame_t * request,
127
131
LOGW ("Waiting confirm for primitive_id 0x%02x but received 0x%02x\n" ,
128
132
request -> primitive_id + SAP_CONFIRM_OFFSET ,
129
133
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));
131
135
attempt ++ ;
132
136
continue ;
133
137
}
@@ -136,7 +140,7 @@ static int send_request_locked(wpc_frame_t * request,
136
140
LOGW ("Waiting confirm for frame_id 0x%02x but received 0x%02x\n" ,
137
141
request -> frame_id ,
138
142
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));
140
144
attempt ++ ;
141
145
continue ;
142
146
}
@@ -146,7 +150,7 @@ static int send_request_locked(wpc_frame_t * request,
146
150
if (attempt == MAX_CONFIRM_ATTEMPT )
147
151
{
148
152
LOGE ("Synchronization lost\n" );
149
- return -1 ;
153
+ return WPC_INT_SYNC_ERROR ;
150
154
}
151
155
152
156
// Copy the confirm
@@ -209,7 +213,7 @@ static int handle_indication(bool last_one,
209
213
if (res <= 0 )
210
214
{
211
215
LOGE ("Timeout waiting for indication last_one=%d\n" , last_one );
212
- return -1 ;
216
+ return WPC_INT_TIMEOUT_ERROR ;
213
217
}
214
218
215
219
// Get timestamp just after reception
@@ -246,25 +250,27 @@ static int get_indication_locked(unsigned int max_ind,
246
250
wpc_frame_t request ;
247
251
wpc_frame_t confirm ;
248
252
int remaining_ind = 1 ;
253
+ int ret ;
249
254
250
255
if (max_ind == 0 )
251
256
{
252
257
// Poll request cannot be done if no indication
253
258
// can be handled
254
259
LOGE ("Wrong number of indication\n" );
255
- return -1 ;
260
+ return WPC_INT_WRONG_PARAM_ERROR ;
256
261
}
257
262
258
263
request .primitive_id = MSAP_INDICATION_POLL_REQUEST ;
259
264
request .payload_length = 0 ;
260
265
261
266
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 )
265
271
{
266
272
LOGE ("Unable to poll for request\n" );
267
- return -1 ;
273
+ return ret ;
268
274
}
269
275
270
276
if (confirm .payload .sap_generic_confirm_payload .result == 0 )
@@ -281,7 +287,7 @@ static int get_indication_locked(unsigned int max_ind,
281
287
if (remaining_ind < 0 )
282
288
{
283
289
LOGE ("Cannot get an indication\n" );
284
- return -1 ;
290
+ return remaining_ind ;
285
291
}
286
292
}
287
293
@@ -320,15 +326,15 @@ int WPC_Int_initialize(char * port_name, unsigned long bitrate)
320
326
{
321
327
// Open the serial connection
322
328
if (Serial_open (port_name , bitrate ) < 0 )
323
- return -1 ;
329
+ return WPC_INT_GEN_ERROR ;
324
330
325
331
// Initialize the slip module
326
332
Slip_init (& Serial_write , & Serial_read );
327
333
328
334
if (!Platform_init ())
329
335
{
330
336
Serial_close ();
331
- return -1 ;
337
+ return WPC_INT_GEN_ERROR ;
332
338
}
333
339
334
340
dsap_init ();
0 commit comments