diff options
author | px4dev <px4@purgatory.org> | 2012-12-30 15:09:21 -0800 |
---|---|---|
committer | px4dev <px4@purgatory.org> | 2012-12-30 15:09:21 -0800 |
commit | dbb841b0dcad55e36d221fc83ac7bab283438a94 (patch) | |
tree | d317a1ac12b7345a065c221d3768abf909ce186a /apps/systemlib | |
parent | f2d4eb2887829efebef853c16223151e0a37f5bb (diff) | |
download | px4-firmware-dbb841b0dcad55e36d221fc83ac7bab283438a94.tar.gz px4-firmware-dbb841b0dcad55e36d221fc83ac7bab283438a94.tar.bz2 px4-firmware-dbb841b0dcad55e36d221fc83ac7bab283438a94.zip |
Rework the way we scan text for scaler definitions; something weird was going on with sscanf's handling of %n that wasn't obvious. This seems to work around the issue.
Diffstat (limited to 'apps/systemlib')
-rw-r--r-- | apps/systemlib/mixer/mixer_simple.cpp | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/apps/systemlib/mixer/mixer_simple.cpp b/apps/systemlib/mixer/mixer_simple.cpp index 048fba148..07dc5f37f 100644 --- a/apps/systemlib/mixer/mixer_simple.cpp +++ b/apps/systemlib/mixer/mixer_simple.cpp @@ -72,17 +72,25 @@ SimpleMixer::~SimpleMixer() } static const char * -skipspace(const char *p, unsigned &len) +findtag(const char *buf, unsigned &buflen, char tag) { - while (isspace(*p)) { - if (len == 0) - return nullptr; - - len--; - p++; + while (buflen >= 2) { + if ((buf[0] == tag) && (buf[1] == ':')) + return buf; + buf++; + buflen--; } + return nullptr; +} + +static void +skipline(const char *buf, unsigned &buflen) +{ + const char *p; - return p; + /* if we can find a CR or NL in the buffer, skip up to it */ + if ((p = (const char *)memchr(buf, '\r', buflen)) || (p = (const char *)memchr(buf, '\n', buflen))) + buflen -= (p - buf); } int @@ -90,20 +98,17 @@ SimpleMixer::parse_output_scaler(const char *buf, unsigned &buflen, mixer_scaler { int ret; int s[5]; - int used; - - buf = skipspace(buf, buflen); - - if (buflen < 16) + + buf = findtag(buf, buflen, 'O'); + if ((buf == nullptr) || (buflen < 12)) return -1; - if ((ret = sscanf(buf, "O: %d %d %d %d %d%n", - &s[0], &s[1], &s[2], &s[3], &s[4], &used)) != 5) { + if ((ret = sscanf(buf, "O: %d %d %d %d %d", + &s[0], &s[1], &s[2], &s[3], &s[4])) != 5) { debug("scaler parse failed on '%s' (got %d)", buf, ret); return -1; } - - buflen -= used; + skipline(buf, buflen); scaler.negative_scale = s[0] / 10000.0f; scaler.positive_scale = s[1] / 10000.0f; @@ -119,20 +124,17 @@ SimpleMixer::parse_control_scaler(const char *buf, unsigned &buflen, mixer_scale { unsigned u[2]; int s[5]; - int used; - buf = skipspace(buf, buflen); - - if (buflen < 16) + buf = findtag(buf, buflen, 'S'); + if ((buf == nullptr) || (buflen < 16)) return -1; - if (sscanf(buf, "S: %u %u %d %d %d %d %d%n", - &u[0], &u[1], &s[0], &s[1], &s[2], &s[3], &s[4], &used) != 7) { + if (sscanf(buf, "S: %u %u %d %d %d %d %d", + &u[0], &u[1], &s[0], &s[1], &s[2], &s[3], &s[4]) != 7) { debug("control parse failed on '%s'", buf); return -1; } - - buflen -= used; + skipline(buf, buflen); control_group = u[0]; control_index = u[1]; |