Rename header file to hpp.
[apps/agl-service-can-low-level.git] / src / can-decoder.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 decoder_t::decoder_t()
19  : decoded_value{false, openxc_DynamicField_Type_STRING, false, "", false, 0, false, false}
20 {
21 }
22
23 float decoder_t::parseSignalBitfield(const CanSignal& signal, const CanMessage& message)
24 {
25          return bitfield_parse_float(message->data, CAN_MESSAGE_SIZE,
26                         signal->bitPosition, signal->bitSize, signal->factor,
27                         signal->offset);
28 }
29
30 openxc_DynamicField decoder_t::noopDecoder(const CanSignal& signal,
31         const CanSignal& signals, float value, bool* send)
32 {
33         decoded_value = { .has_type = true,
34                                         .type = openxc_DynamicField_Type_NUM,
35                                         .has_numeric_value = true,
36                                         .numeric_value = value };
37     return decoded_value;
38 }
39
40 openxc_DynamicField decoder_t::booleanDecoder(const CanSignal& signal,
41         const CanSignal& signals, float value, bool* send)
42 {
43         decoded_value = { .has_type = true,
44                                         .type = openxc_DynamicField_Type_BOOL,
45                                         .has_boolean_value = true,
46                                         .numeric_value = value == 0.0 ? false : true };
47         return decoded_value;
48 }
49
50 openxc_DynamicField decoder_t::ignoreDecoder(const CanSignal& signal,
51         const CanSignal& signals, float value, bool* send)
52 {
53     if(send)
54       *send = false;
55     
56     openxc_DynamicField decoded_value = {0};
57     return decoded_value;
58 }
59
60 openxc_DynamicField decoder_t::stateDecoder(const CanSignal& signal,
61         const CanSignal& signals, float value, bool* send)
62 {
63     openxc_DynamicField decoded_value = {0};
64     decoded_value.has_type = true;
65     decoded_value.type = openxc_DynamicField_Type_STRING;
66     decoded_value.has_string_value = true;
67
68     const CanSignalState* signalState = lookupSignalState(value, signal);
69     if(signalState != NULL) {
70         ::strcpy(decoded_value.string_value, signalState->name);
71     } else {
72         *send = false;
73     }
74     return decoded_value;
75 }
76
77 openxc_DynamicField decoder_t::decodeSignal(const CanSignal& signal,
78         float value, const CanSignal& signals, bool* send)
79 {
80     SignalDecoder decoder = signal->decoder == NULL ?
81                                             noopDecoder : signal->decoder;
82     openxc_DynamicField decoded_value = decoder(signal, signals,
83             value, send);
84     return decoded_value;
85 }
86
87 openxc_DynamicField decoder_t::decodeSignal(const CanSignal& signal,
88         const CanMessage& message, const CanSignal& signals, bool* send) {
89     float value = parseSignalBitfield(signal, message);
90     return decodeSignal(signal, value, signals, send);
91 }