Clarify when we are using int vs float and move decoders up a level.
authorChristopher Peplin <chris.peplin@rhubarbtech.com>
Fri, 14 Feb 2014 23:35:24 +0000 (18:35 -0500)
committerChristopher Peplin <chris.peplin@rhubarbtech.com>
Fri, 14 Feb 2014 23:35:48 +0000 (18:35 -0500)
Fixed #4 - the Decoder function type signature actually belonged one
level higher, in the application, since there's no capability of
actually using it in this library at the moment.

src/uds/uds.c
src/uds/uds.h
src/uds/uds_types.h
tests/test_core.c

index 9fec9a8..412f077 100644 (file)
@@ -258,17 +258,13 @@ DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
     return response;
 }
 
-float diagnostic_payload_to_float(const DiagnosticResponse* response) {
-    return bitfield_parse_float(response->payload,
-            response->payload_length, 0,
-            response->payload_length * CHAR_BIT, 1.0, 0);
+int diagnostic_payload_to_integer(const DiagnosticResponse* response) {
+    return get_bitfield(response->payload, response->payload_length, 0,
+            response->payload_length * CHAR_BIT);
 }
 
-/* Public:
- *
- * Functions pulled from http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01
- */
-float diagnostic_decode_obd2_pid(const DiagnosticResponse* response) {
+float diagnostic_decode_obd2_pid(const DiagnosticResponse* response,
+        int parsed_payload) {
     // handles on the single number values, not the bit encoded ones
     switch(response->pid) {
         case 0xa:
index a4761ca..cb13233 100644 (file)
@@ -83,7 +83,11 @@ DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
         const uint16_t arbitration_id, const uint8_t data[],
         const uint8_t size);
 
-float diagnostic_payload_to_float(const DiagnosticResponse* response);
+/* Public: Parse the entier payload of the reponse as a single integer.
+ *
+ * response - the received DiagnosticResponse.
+ */
+int diagnostic_payload_to_integer(const DiagnosticResponse* response);
 
 /* Public: Render a DiagnosticResponse as a string into the given buffer.
  *
@@ -97,7 +101,16 @@ float diagnostic_payload_to_float(const DiagnosticResponse* response);
 // void diagnostic_response_to_string(const DiagnosticResponse* response,
         // char* destination, size_t destination_length);
 
-float diagnostic_decode_obd2_pid(const DiagnosticResponse* response);
+/* Public: For many OBD-II PIDs with a numerical result, translate a diagnostic
+ * response payload into a meaningful number using the standard formulas.
+ *
+ * Functions pulled from http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01
+ *
+ * Returns the translated value or 0 if the PID is not in the OBD-II standard or
+ * does not use a numerical value (e.g. VIN).
+ */
+float diagnostic_decode_obd2_pid(const DiagnosticResponse* response,
+                int parsed_payload);
 
 #ifdef __cplusplus
 }
index 10d15d6..a71d53c 100644 (file)
@@ -133,15 +133,13 @@ typedef enum {
     OBD2_MODE_ENHANCED_DIAGNOSTIC_REQUEST = 0x22
 } DiagnosticMode;
 
-/* Public the signature for an optional function to be called when a diagnostic
+/* Public: The signature for an optional function to be called when a diagnostic
  * request is complete, and a response is received or there is a fatal error.
  *
  * response - the completed DiagnosticResponse.
  */
 typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
 
-typedef float (*DiagnosticResponseDecoder)(const DiagnosticResponse* response);
-
 /* Public: A handle for initiating and continuing a single diagnostic request.
  *
  * A diagnostic request requires one or more CAN messages to be sent, and one
index 815954b..30d19f4 100644 (file)
@@ -338,7 +338,7 @@ START_TEST (test_negative_response)
 }
 END_TEST
 
-START_TEST (test_payload_to_float)
+START_TEST (test_payload_to_integer)
 {
     uint16_t arb_id = OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST;
     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
@@ -348,7 +348,7 @@ START_TEST (test_payload_to_float)
     const uint8_t can_data[] = {0x4, 0x1 + 0x40, 0x2, 0x45, 0x12};
     DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8,
             can_data, sizeof(can_data));
-    ck_assert_int_eq(diagnostic_payload_to_float(&response), 0x4512);
+    ck_assert_int_eq(diagnostic_payload_to_integer(&response), 0x4512);
 }
 END_TEST
 
@@ -371,7 +371,7 @@ Suite* testSuite(void) {
     tcase_add_test(tc_core, test_missing_pid);
     tcase_add_test(tc_core, test_wrong_pid_then_right_completes);
     tcase_add_test(tc_core, test_negative_response);
-    tcase_add_test(tc_core, test_payload_to_float);
+    tcase_add_test(tc_core, test_payload_to_integer);
 
     // TODO these are future work:
     // TODO test request MIL