BOROSOFTWAREAll work →
scantool/diag_l0.h 147 lines
1#ifndef _DIAG_L0_H_
2#define _DIAG_L0_H_
3
4/*
5 * freediag - Vehicle Diagnostic Utility
6 * (c) fenugrec 2014-2015
7 * GPLv3
8 */
9
10#if defined(__cplusplus)
11extern "C" {
12#endif
13
14#include <stdbool.h>
15#include <stddef.h>
16#include <stdint.h>
17
18/*
19 * L0 device structure
20 * This is the structure to interface between the L1 code
21 * and the interface-manufacturer dependent code (in diag_l0_<if>.c)
22 * A "diag_l0_device" is a unique association between an l0 driver (diag_l0_dumb for instance)
23 * and a hardware resource (serial port, file, etc.)
24 */
25struct diag_l0_device {
26 void *l0_int; /** Handle for internal L0 data */
27 const struct diag_l0 *dl0; /** The L0 driver's diag_l0 */
28
29 bool opened; /** L0 status */
30};
31
32
33// diag_l0 : every diag_l0_???.c "driver" fills in one of these to describe itself.
34struct diag_l0 {
35 const char *longname; /* Useful textual name, unused at the moment */
36 const char *shortname; /* Short, unique text name for user interface */
37
38 int l1proto_mask; /** supported L1protocols, defined in diag_l1.h */
39
40 /** set up global/default state of driver, if applicable
41 *
42 * @return 0 if ok
43 *
44 * Note: the implementation must not do any dynamic mem operation (*alloc etc) or open handles.
45 * That way we won't need to add an _end function.
46 */
47 int (*init)(void);
48
49
50 /*** Private funcs, do not call directly ! ***/
51 /* These are called from the "public funcs" listed below. */
52
53 int (*_new)(struct diag_l0_device *);
54 struct cfgi *(*_getcfg)(struct diag_l0_device *);
55 void (*_del)(struct diag_l0_device *);
56 int (*_open)(struct diag_l0_device *, int l1_proto);
57 void (*_close)(struct diag_l0_device *);
58 uint32_t (*_getflags)(struct diag_l0_device *);
59
60 int (*_recv)(struct diag_l0_device *, void *data, size_t len, unsigned int timeout);
61 int (*_send)(struct diag_l0_device *, const void *data, size_t len);
62
63 int (*_ioctl)(struct diag_l0_device *, unsigned cmd, void *data);
64};
65
66
67
68/***** Public funcs *****/
69
70/** Alloc new dl0d and call L0's "_new";
71 * (no open, default params, etc)
72 * @return 0 if ok */
73struct diag_l0_device *diag_l0_new(const char *shortname);
74
75
76/** Get linked-list of config items.
77 * @return NULL if no items exist */
78struct cfgi *diag_l0_getcfg(struct diag_l0_device *);
79
80
81/** Delete L0 driver instance.
82 *
83 * Caller MUST have closed it first with diag_l0_close !
84 * Opposite of diag_l0_new()
85 */
86void diag_l0_del(struct diag_l0_device *);
87
88
89/** Open an L0 device with a given L1 proto
90 *
91 * @return 0 if ok
92 *
93 * L0 device must have been created with diag_l0_new() first
94 */
95int diag_l0_open(struct diag_l0_device *, int l1protocol);
96
97
98/** Close diag_l0_device
99 *
100 * Does not free the struct itself, to allow reuse.
101 */
102void diag_l0_close(struct diag_l0_device *);
103
104
105/** Get L0 device flags
106 * @return bitmask of flags defined in diag_l1.h
107 */
108uint32_t diag_l0_getflags(struct diag_l0_device *);
109
110
111/** Receive bytes.
112 * @param timeout: in ms
113 * @return # of bytes read
114 */
115int diag_l0_recv(struct diag_l0_device *, void *data, size_t len, unsigned int timeout);
116
117
118/** Send bytes.
119 * @return 0 on success
120 */
121int diag_l0_send(struct diag_l0_device *, const void *data, size_t len);
122
123/** Send IOCTL to L0
124 * @param command : IOCTL #, defined in diag.h
125 * @param data optional, input/output
126 * @return 0 if OK, diag error num (<0) on error
127 */
128int diag_l0_ioctl(struct diag_l0_device *, unsigned cmd, void *data);
129
130
131/*** globals ***/
132
133extern int diag_l0_debug; // debug flags
134
135
136/*
137 * l0dev_list : static-allocated list of supported L0 devices, since it can
138 * be entirely determined at compile-time.
139 * The last item is a NULL ptr to ease iterating.
140 */
141extern const struct diag_l0 *l0dev_list[]; /* defined in diag_config.c */
142
143#if defined(__cplusplus)
144}
145#endif
146#endif // _DIAG_L0_H_
147