]> sigrok.org Git - libsigrok.git/blobdiff - src/analog.c
analog.c: Return SR_ERR_ARG upon invalid arguments.
[libsigrok.git] / src / analog.c
index dfb38cec5a43749e5014f80b9b259f9c65640e95..e6efce2eb58e5132826487dd1002740f144d4da2 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
@@ -116,7 +117,7 @@ static struct unit_mq_string mq_strings[] = {
        ALL_ZERO
 };
 
-SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog,
+SR_PRIV int sr_analog_init(struct sr_datafeed_analog *analog,
                struct sr_analog_encoding *encoding,
                struct sr_analog_meaning *meaning,
                struct sr_analog_spec *spec,
@@ -150,13 +151,19 @@ SR_PRIV int sr_analog_init(struct sr_datafeed_analog2 *analog,
        return SR_OK;
 }
 
-SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
+SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog,
                float *outbuf)
 {
        float offset;
-       unsigned int b, i;
+       unsigned int b, i, count;
        gboolean bigendian;
 
+       if (!analog || !(analog->data) || !(analog->meaning)
+                       || !(analog->encoding) || !outbuf)
+               return SR_ERR_ARG;
+
+       count = analog->num_samples * g_slist_length(analog->meaning->channels);
+
 #ifdef WORDS_BIGENDIAN
        bigendian = TRUE;
 #else
@@ -170,19 +177,23 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
 
        if (analog->encoding->unitsize == sizeof(float)
                        && analog->encoding->is_bigendian == bigendian
-                       && (analog->encoding->scale.p == analog->encoding->scale.q)
+                       && analog->encoding->scale.p == 1
+                       && analog->encoding->scale.q == 1
                        && analog->encoding->offset.p / (float)analog->encoding->offset.q == 0) {
                /* The data is already in the right format. */
-               memcpy(outbuf, analog->data, analog->num_samples * sizeof(float));
+               memcpy(outbuf, analog->data, count * sizeof(float));
        } else {
-               for (i = 0; i < analog->num_samples; i += analog->encoding->unitsize) {
+               for (i = 0; i < count; i += analog->encoding->unitsize) {
                        for (b = 0; b < analog->encoding->unitsize; b++) {
                                if (analog->encoding->is_bigendian == bigendian)
-                                       outbuf[i + b] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
+                                       ((uint8_t *)outbuf)[i + b] =
+                                               ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
                                else
-                                       outbuf[i + (analog->encoding->unitsize - b)] = ((float *)analog->data)[i * analog->encoding->unitsize + b];
+                                       ((uint8_t *)outbuf)[i + (analog->encoding->unitsize - b)] =
+                                               ((uint8_t *)analog->data)[i * analog->encoding->unitsize + b];
                        }
-                       if (analog->encoding->scale.p != analog->encoding->scale.q)
+                       if (analog->encoding->scale.p != 1
+                                       || analog->encoding->scale.q != 1)
                                outbuf[i] = (outbuf[i] * analog->encoding->scale.p) / analog->encoding->scale.q;
                        offset = ((float)analog->encoding->offset.p / (float)analog->encoding->offset.q);
                        outbuf[i] += offset;
@@ -207,9 +218,12 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog,
  *
  * @since 0.4.0
  */
-SR_API int sr_analog_float_to_string(float value, int digits, char **result)
+SR_API int sr_analog_float_to_string(float value, unsigned int digits, char **result)
 {
-       int cnt, i;
+       unsigned int cnt, i;
+
+       if (!result)
+               return SR_ERR_ARG;
 
        /* This produces at least one too many digits */
        *result = g_strdup_printf("%.*f", digits, value);
@@ -238,11 +252,16 @@ SR_API int sr_analog_float_to_string(float value, int digits, char **result)
  *
  * @since 0.4.0
  */
-SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog2 *analog,
+SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog *analog,
                char **result)
 {
        int i;
-       GString *buf = g_string_new(NULL);
+       GString *buf;
+
+       if (!analog || !(analog->meaning) || !result)
+               return SR_ERR_ARG;
+
+       buf = g_string_new(NULL);
 
        for (i = 0; unit_strings[i].value; i++) {
                if (analog->meaning->unit == unit_strings[i].value) {
@@ -268,8 +287,11 @@ SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog2 *analog,
  * @param p Numerator
  * @param q Denominator
  */
-SR_API void sr_rational_set(struct sr_rational *r, uint64_t p, uint64_t q)
+SR_API void sr_rational_set(struct sr_rational *r, int64_t p, uint64_t q)
 {
+       if (!r)
+               return;
+
        r->p = p;
        r->q = q;
 }