ff5a8e6d8e10480779937e42960047e361979635
[apps/low-level-can-service.git] / src / can_decode_message.cpp
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Romain Forlot" <romain.forlot@iot.bzh>
4  * Author "Loic Collignon" <loic.collignon@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <linux/can.h>
20 #include <linux/can/raw.h>
21 #include <queue>
22
23 #include <afb/afb-binding.h>
24
25 #include "can-utils.h"
26 #include "can-decoder.h"
27 #include "openxc.pb.h"
28
29 void can_decode_message(CanBus_c *can_bus)
30 {
31         CanMessage_c *can_message;
32         int i;
33         std:vector <CanSignal> *signals;
34         CanSignal sig;
35         openxc_VehicleMessage vehicle_message;
36         openxc_SimpleMessage s_message;
37         openxc_DynamicField key, ret;
38         Decoder_c decoder();
39
40         vehicle_message = {.has_type = true,
41                                           .type = openxc_VehicleMessage_Type::openxc_VehicleMessage_Type_SIMPLE,
42                                           .has_simple_message = true };
43
44         while(true)
45         {
46                 if(can_message = can_bus->next_can_message())
47                 {
48                         /* First we have to found which CanSignal is */
49                         key = { .has_type = true,
50                                         .type = openxc_DynamicField_Type::openxc_DynamicField_Type_NUM,
51                                         .has_numeric_value = true,
52                                         .numeric_value = (double)can_message.get_id() };
53                         signals = GetSignals(key);
54
55                         /* Decoding the message ! Don't kill the messenger ! */
56                         if(signals.size() > 0)
57                         {
58                                 for(i=0; i< signals.size(); i++)
59                                 {
60                                         sig = signals.back();
61                                         ret = decoder.decodeSignal(&sig, can_message, SIGNALS, SIGNALS.size(), true);
62
63                                         s_message = {.has_name = true,
64                                                                                 .name = sig->genericName,
65                                                                                 .has_value = true,
66                                                                                 .value = ret
67                                                                         };
68                                         vehicle_message.simple_message = s_message;
69                                         vehicle_message_q.push(vehicle_message);
70                                         
71                                         signals.pop_back();
72                                 }
73                         }
74                 }
75         }
76 }