Switch from GPLv2 to GPLv2+
[fw/altos] / ao-tools / lib / cc-telemetry.c
index 99da2680c1cdca4fe3be0a9143a7e4f1781b7a67..42315128c13745986461665ff6d31fea8714a34b 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -60,3 +61,32 @@ cc_telemetry_parse(const char *input_line, union ao_telemetry_all *telemetry)
        memcpy(telemetry, hex+1, 34);
        return TRUE;
 }
+
+uint8_t
+cc_telemetry_cksum(const union ao_telemetry_all *telemetry)
+{
+       const uint8_t   *x = (const uint8_t *) telemetry;
+       int i;
+       uint8_t sum = 0x5a;
+       for (i = 0; i < 34; i++)
+               sum += x[i];
+       return sum;
+}
+
+void
+cc_telemetry_unparse(const union ao_telemetry_all *telemetry, char output_line[CC_TELEMETRY_BUFSIZE])
+{
+       uint8_t hex[36];
+       int i;
+       int p;
+
+       hex[0] = 34;
+       memcpy(hex+1, telemetry, 34);
+       hex[35] = cc_telemetry_cksum(telemetry);
+       strcpy(output_line, "TELEM ");
+       p = strlen(output_line);
+       for (i = 0; i < 36; i++) {
+               sprintf(output_line + p, "%02x", hex[i]);
+               p += 2;
+       }
+}