1c993d7fe815e7af6b001b1ff5d385a91e23fb16
[apps/low-level-can-service.git] / ll-can-binding.h
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 /*
19  * Interface between the daemon and the binding
20  */
21 static const struct afb_binding_interface *interface;
22
23 /*
24  * the type of position expected
25  *
26  * here, this type is the selection of protocol
27  */
28 enum type {
29         type_OBDII,
30         type_CAN,
31         type_DEFAULT = type_CAN,
32         type_INVALID = -1
33 };
34
35 #define type_size sizeof(enum type)-2
36
37 /*
38  * names of the types
39  */
40 static const char * const type_NAMES[type_size] = {
41         "OBDII",
42         "CAN"
43 };
44
45 /* CAN variable initialization */
46 struct canfd_frame canfd_frame;
47
48 struct can_handler {
49         int socket;
50         char *device;
51         bool is_fdmode_on;
52         struct sockaddr_can txAddress;
53 };
54
55 /*
56  * each generated event
57  */
58 typedef struct _event event;
59 struct _event {
60         event *next;                    /* link for the next event */
61         const char *name;               /* name of the event */
62         struct afb_event afb_event;     /* the event for the binder */
63         openxc_CanMessage can_message;  /* value for the can_message */
64 };
65
66 /*
67  * each can event, will browse by the id
68  */
69 typedef struct _can_event can_event;
70 struct _can_event {
71         can_event *next;        /* Link to the next other can message */
72         event *events;          /* events for the can message */
73         uint32_t id;            /* id of the event for unsubscribe */
74         enum type type;         /* the type of data expected */
75 };
76
77 can_event *can_events_list;
78
79 /* TODO : Add help comments :p */
80 static int connect_to_event_loop();
81
82 // Initialize default can_handler values
83 static struct can_handler can_handler = {
84         .socket = -1,
85         .device = "vcan0",
86         .is_fdmode_on = false,
87 };
88
89 static void send_event();
90
91 static int retry( int(*func)());
92
93 static void parse_can_frame(openxc_CanMessage *can_message, struct canfd_frame *canfd_frame, int maxdlen);