-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nanostack: translate errors from sendmsg #10410
Nanostack: translate errors from sendmsg #10410
Conversation
@@ -648,6 +648,18 @@ nsapi_size_or_error_t Nanostack::do_sendto(void *handle, const ns_address_t *add | |||
* */ | |||
if (retcode == NS_EWOULDBLOCK) { | |||
ret = NSAPI_ERROR_WOULD_BLOCK; | |||
} else if (retcode == -1) { | |||
ret = NSAPI_ERROR_PARAMETER; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we refer to Nanostack's socket API:
/**
...
* Error returns:
*
* \return -1 Invalid socket ID.
* \return -2 Socket memory allocation fail.
* \return -3 TCP state not established or address scope not defined .
* \return -4 Unknown interface.
* \return -5 Socket not connected
* \return -6 Packet too short (ICMP raw socket error).
*/
int16_t socket_sendmsg(int8_t socket, const ns_msghdr_t *msg, int flags);
then should -1 be NSAPI_ERROR_NO_SOCKET
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how -1 is returned:
if (!msg) {
return -1;
}
Or here (I think this is the one they meant by "Invalid socket ID."):
if (!socket_ptr || !socket_ptr->inet_pcb) {
tr_warn("Socket Id %i", sid);
ret_val = -1;
goto fail;
}
Or here:
if (!socket_message_validate_iov(msg, &payload_length)) {
return -1;
}
Looks to me like -1 is returned if the input parameters are wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Then its clearly a parameter error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, -1 is generally "parameter error" in Nanostack, for the few socket functions that have multiple error returns. But the socket ID isn't distinguished from any other faulty parameter.
I would have thought there would be scope for NSAPI_ERROR_NO_SOCKET
being generated in the wrapper, at least.
@michalpasztamobica, thank you for your changes. |
@@ -648,6 +648,18 @@ nsapi_size_or_error_t Nanostack::do_sendto(void *handle, const ns_address_t *add | |||
* */ | |||
if (retcode == NS_EWOULDBLOCK) { | |||
ret = NSAPI_ERROR_WOULD_BLOCK; | |||
} else if (retcode == -1) { | |||
ret = NSAPI_ERROR_PARAMETER; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, -1 is generally "parameter error" in Nanostack, for the few socket functions that have multiple error returns. But the socket ID isn't distinguished from any other faulty parameter.
I would have thought there would be scope for NSAPI_ERROR_NO_SOCKET
being generated in the wrapper, at least.
@@ -648,6 +648,18 @@ nsapi_size_or_error_t Nanostack::do_sendto(void *handle, const ns_address_t *add | |||
* */ | |||
if (retcode == NS_EWOULDBLOCK) { | |||
ret = NSAPI_ERROR_WOULD_BLOCK; | |||
} else if (retcode == -1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like this is getting to the point it should be a switch statement for the error cases, although I guess you still need an if for the < 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's why I stayed with it if-else statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a tad wary that the compiler will end up doing all the comparisons in the order specified. Compilers are generally good at spotting how to break a switch up into ranges and figuring out when to do a branch table, and when to do binary chop, but I'm not sure how good they are at optimising an if-else chain on 1 variable.
At least make the non-error case the first, if going down this route, for speed.
It might be time to use a non-conventional if-else-switch
control construct:
if (retcode >= 0) {
ret = retcode;
} else switch (retcode) {
case -1:
ret = NSAPI_ERROR_PARAMETER;
break;
...
default:
tr_error();
ret = NSAPI_ERROR_DEVICE_ERROR;
break;
}
(Would look neater if Mbed OS style didn't insist on the extra switch indent).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
69083d4
to
eb74e45
Compare
tr_error("socket_sendmsg: error=%d", retcode); | ||
ret = NSAPI_ERROR_DEVICE_ERROR; | ||
} else { | ||
if (retcode >=0 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray space that the formatter will likely complain about
Translate errors to NSAPI_ERROR_*, instead of always returning NSAPI_ERROR_DEVICE_ERROR
eb74e45
to
d1d0118
Compare
Test run: FAILEDSummary: 1 of 11 test jobs failed Failed test jobs:
|
Failures are in components-storage-blockdevice-component_sd-tests-* , rather irrelevant for this PR. |
Test restarted |
ci started |
Test run: SUCCESSSummary: 11 of 11 test jobs passed |
Marking this for 5.13 as it looks like an interface change to me. @michalpasztamobica could you please also add a release notes section to the header explaining the impact to the user. |
@adbridge , I would say this is more of a fix - after all the function call looks the same and the return type does not change either. We only fix the code to return appropriate error values. I will leave it up to you to decide - the Release Note is added. |
@michalpasztamobica My concern would be that if a user of the interface is just expecting to receive the one previous error code but now could get multiple different ones, their application could fail ? |
@adbridge , I misunderstood "interface change" to be a PR that will actually change the API (like add a new function, a parameter or change return type). But giving it a second thought - you are right: anything that can become an annoyance to the users better be communicated clearly in the release notes. |
Application that expects certain kind of errors, sound a bit unstable in a first place. Being portable, application should be able to fail gracefully in any error, and work normally between all connectivities we offer. To me this change brings clarify to Nanostack's error codes, but Anna's concern is valid, and we should therefore release this only in 5.13. |
Description
Translate errors to NSAPI_ERROR_*, instead of always returning NSAPI_ERROR_DEVICE_ERROR.
Originally, I only wanted to add the handling of -2 (out of memory) error, to fix the numerous SENDTO_REPEAT failures I can see in Nanostack, but while I was at it I though it is wise to translate the other errors as well, instead of just returning DEVICE_ERROR and loosing the information returned from sendmsg call.
Pull request type
Reviewers
@SeppoTakalo
@kjbracey-arm
@KariHaapalehto
@VeijoPesonen
Release Notes
Nanostack send calls return a more accurate information in case of errors.