Skip to content
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

xbee: add timeout for AT command response (fixes #4731) #4734

Merged
merged 1 commit into from
Apr 19, 2016

Conversation

Yonezawa-T2
Copy link
Contributor

Adds timeout for receiving AT command response.

Tested on a native platform with a XBee connected via USB adapter.
It works fine for normal case without producing debug output "xbee: response timeout". When the following lines in _rx_cb is commented out to simulate broken UART, it produces debug output "xbee: response timeout" and proceeds.

dev->resp_buf[dev->resp_count++] = (uint8_t)c;
if (dev->resp_count == dev->resp_limit) {
    /* here we ignore the checksum to prevent deadlocks */
    mutex_unlock(&(dev->resp_lock));
    dev->int_state = XBEE_INT_STATE_IDLE;
}

@Yonezawa-T2 Yonezawa-T2 mentioned this pull request Feb 4, 2016
@Yonezawa-T2
Copy link
Contributor Author

The test is done in a branch with #4445 merged.

@OlegHahm OlegHahm added this to the Release 2016.03 milestone Feb 4, 2016
@OlegHahm OlegHahm added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Area: drivers Area: Device drivers labels Feb 4, 2016
@OlegHahm
Copy link
Member

OlegHahm commented Feb 4, 2016

I can test next week, if needed.

@OlegHahm
Copy link
Member

OlegHahm commented Feb 4, 2016

#4443 is also required, right?

@Yonezawa-T2
Copy link
Contributor Author

@OlegHahm Thank you for testing. Yes, you need #4443 for native platform.

My experiment branch is here: https://github.com/Yonezawa-T2/RIOT/tree/xbee_border_router. This contains #4443, #4445, #4448 and some other minor patches.

Steps to test:

  • connect a XBee module to Mac
  • go to examples/gnrc_xbee_border_router
  • edit xbee_params.h for baud rate
  • make
  • sudo ./bin/native/gnrc_xbee_border_router.elf tap0 -c /dev/tty.usbserial-XXXXXXXX where /dev/tty.usbserial-XXXXXXXX is the TTY device file for the XBee deivce.

The XBee driver initializes the XBee hardware on boot. On successful startup, it outputs debug outputs like following:

xbee: AT_CMD: +++
xbee: AT_CMD: ATMM2
xbee: AT_CMD: ATAP1
xbee: AT_CMD: ATAC
xbee: AT_CMD: ATCN
xbee: AT_CMD: SH
xbee: AT_CMD: SL
xbee: AT_CMD: MYZ8
xbee: AT_CMD: CH
xbee: AT_CMD: ID
xbee: Initialization successful

If we comment out some codes in _rx_cb, we will get the following outputs:

xbee: AT_CMD: +++
xbee: AT_CMD: ATMM2
xbee: AT_CMD: ATAP1
xbee: AT_CMD: ATAC
xbee: AT_CMD: ATCN
xbee: AT_CMD: SH
xbee: response timeout
xbee: AT_CMD: MY
xbee: response timeout
xbee: AT_CMD: CH
xbee: response timeout
xbee: AT_CMD: ID
xbee: response timeout
xbee: Initialization successful

As xbee.c don't check status of AT commands on startup, it shows "Initialization successful" despite of timeout.

@Yonezawa-T2
Copy link
Contributor Author

Added status checking on startup: #4749.

@kYc0o
Copy link
Contributor

kYc0o commented Mar 28, 2016

ping @Yonezawa-T2

@Yonezawa-T2
Copy link
Contributor Author

I'm waiting for review.

@kYc0o kYc0o added Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Mar 29, 2016
@OlegHahm
Copy link
Member

Let's try to review and test this tonight.

@haukepetersen
Copy link
Contributor

I think we might simplify this PR to some extend (and save memory with this): The paradigm we use in device drivers concerning broken connections is, that we check the correct connection to a device once during initialization and assume a working connection from this point on. For the xbee device, I think we should simply send a single AT comman AT once in the beginning and watch (with timeout) for the OK reply. If this times out, we abort the initialization returning an error code. If this is successful, we assume a correct connection to the device and there is no need to keep the timer struct in the device descriptor.

@Yonezawa-T2
Copy link
Contributor Author

Moved the timer from xbee_t to the stack. The thread blocks until a response arrives or the timer expired, so we can allocate the timer on the stack. I think checking timeout only on the startup would not yield simplicity anymore.

@haukepetersen
Copy link
Contributor

But it would enable the init function to return an error code. Alternatively we should add a return value to the _api_at_cmd function that we can check during init. This way init does not return a success value when it actually failed...

@Yonezawa-T2
Copy link
Contributor Author

_api_at_cmd already returns error via resp->status and xbee_init returns error when the timer timed out (#4749).

@haukepetersen
Copy link
Contributor

I don't see it. Say the call _at_cmd(dev, "ATMM2\r"); fails, the init sequence will just continue with the rest of the initialization sequence, until one of the non-AT calls later on might fail. This is not particular clean?!

@Yonezawa-T2
Copy link
Contributor Author

xbee_init calls _get_addr_long, which calls _api_at_cmd. So connection error is certainly reported as an error from xbee_init (see #4749).

To check response for _at_cmd, we have to modify _rx_cb, that impose complexity.

@haukepetersen
Copy link
Contributor

now I see why I was so confused - I forgot that _at_cmd and _api_at_cmd were two different functions - long time since I wrote that code, sorry for all the nit-picking...

Just tested this PR -> there is still a bug: you need to call mutex_unlock(&(dev->tx_lock)); at the end of the _api_at_cmd function, otherwise it stays locked and hangs the second time this function is called...

@Yonezawa-T2
Copy link
Contributor Author

tx_lock is already addressed in #4445.

@haukepetersen haukepetersen added the State: waiting for other PR State: The PR requires another PR to be merged first label Apr 1, 2016
@haukepetersen
Copy link
Contributor

this seems not be be my day, overlooked that this depnends on #4445

@kYc0o
Copy link
Contributor

kYc0o commented Apr 18, 2016

@Yonezawa-T2 can you squash this too? code seems ok, I can test it ASAP, se let's Murdock check for errors.

@Yonezawa-T2
Copy link
Contributor Author

Squashed.

@Yonezawa-T2 Yonezawa-T2 removed the State: waiting for other PR State: The PR requires another PR to be merged first label Apr 19, 2016
@kYc0o kYc0o added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed Community: Hack'n'ACK candidate This PR is a candidate for review and discussion during one of RIOT's monthly Hack'n'ACK parties CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Apr 19, 2016
@kYc0o
Copy link
Contributor

kYc0o commented Apr 19, 2016

will need rebase since #4445 has been merged...

@kYc0o
Copy link
Contributor

kYc0o commented Apr 19, 2016

ACK and try to merge it...

@kYc0o kYc0o merged commit 724276e into RIOT-OS:master Apr 19, 2016
@miri64
Copy link
Member

miri64 commented Apr 19, 2016

Needs backport.

@miri64 miri64 added the Process: needs backport Integration Process: The PR is required to be backported to a release or feature branch label Apr 19, 2016
@miri64 miri64 removed the Process: needs backport Integration Process: The PR is required to be backported to a release or feature branch label Apr 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: drivers Area: Device drivers CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants