1 #ifndef _QRTR_LIB_H_
2 #define _QRTR_LIB_H_
3 
4 #include <linux/qrtr.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #ifndef offsetof
15 #define offsetof(type, md) ((size_t)&((type *)0)->md)
16 #endif
17 
18 #ifndef container_of
19 #define container_of(ptr, type, member) \
20   ((type *)((char *)(ptr) - offsetof(type, member)))
21 #endif
22 
23 #ifndef AF_QIPCRTR
24 #define AF_QIPCRTR 42
25 #endif
26 
27 struct sockaddr_qrtr;
28 
29 struct qrtr_packet {
30 	int type;
31 
32 	unsigned int node;
33 	unsigned int port;
34 
35 	unsigned int service;
36 	unsigned int instance;
37 	unsigned int version;
38 
39 	void *data;
40 	size_t data_len;
41 };
42 
43 #define DEFINE_QRTR_PACKET(pkt, size) \
44 	char pkt ## _buf[size]; \
45 	struct qrtr_packet pkt = { .data = pkt ##_buf, \
46 				   .data_len = sizeof(pkt ##_buf), }
47 
48 #define QMI_REQUEST     0
49 #define QMI_RESPONSE    2
50 #define QMI_INDICATION  4
51 
52 #define QMI_COMMON_TLV_TYPE 0
53 
54 enum qmi_elem_type {
55 	QMI_EOTI,
56 	QMI_OPT_FLAG,
57 	QMI_DATA_LEN,
58 	QMI_UNSIGNED_1_BYTE,
59 	QMI_UNSIGNED_2_BYTE,
60 	QMI_UNSIGNED_4_BYTE,
61 	QMI_UNSIGNED_8_BYTE,
62 	QMI_SIGNED_1_BYTE_ENUM,
63 	QMI_SIGNED_2_BYTE_ENUM,
64 	QMI_SIGNED_4_BYTE_ENUM,
65 	QMI_STRUCT,
66 	QMI_STRING,
67 };
68 
69 enum qmi_array_type {
70 	NO_ARRAY,
71 	STATIC_ARRAY,
72 	VAR_LEN_ARRAY,
73 };
74 
75 /**
76  * struct qmi_elem_info - describes how to encode a single QMI element
77  * @data_type:  Data type of this element.
78  * @elem_len:   Array length of this element, if an array.
79  * @elem_size:  Size of a single instance of this data type.
80  * @array_type: Array type of this element.
81  * @tlv_type:   QMI message specific type to identify which element
82  *              is present in an incoming message.
83  * @offset:     Specifies the offset of the first instance of this
84  *              element in the data structure.
85  * @ei_array:   Null-terminated array of @qmi_elem_info to describe nested
86  *              structures.
87  */
88 struct qmi_elem_info {
89 	enum qmi_elem_type data_type;
90 	uint32_t elem_len;
91 	uint32_t elem_size;
92 	enum qmi_array_type array_type;
93 	uint8_t tlv_type;
94 	size_t offset;
95 	struct qmi_elem_info *ei_array;
96 };
97 
98 #define QMI_RESULT_SUCCESS_V01                  0
99 #define QMI_RESULT_FAILURE_V01                  1
100 
101 #define QMI_ERR_NONE_V01                        0
102 #define QMI_ERR_MALFORMED_MSG_V01               1
103 #define QMI_ERR_NO_MEMORY_V01                   2
104 #define QMI_ERR_INTERNAL_V01                    3
105 #define QMI_ERR_CLIENT_IDS_EXHAUSTED_V01        5
106 #define QMI_ERR_INVALID_ID_V01                  41
107 #define QMI_ERR_ENCODING_V01                    58
108 #define QMI_ERR_INCOMPATIBLE_STATE_V01          90
109 #define QMI_ERR_NOT_SUPPORTED_V01               94
110 
111 /**
112  * qmi_response_type_v01 - common response header (decoded)
113  * @result:     result of the transaction
114  * @error:      error value, when @result is QMI_RESULT_FAILURE_V01
115  */
116 struct qmi_response_type_v01 {
117 	uint16_t result;
118 	uint16_t error;
119 };
120 
121 extern struct qmi_elem_info qmi_response_type_v01_ei[];
122 
123 int qrtr_open(int rport);
124 void qrtr_close(int sock);
125 
126 int qrtr_sendto(int sock, uint32_t node, uint32_t port, const void *data, unsigned int sz);
127 int qrtr_recvfrom(int sock, void *buf, unsigned int bsz, uint32_t *node, uint32_t *port);
128 int qrtr_recv(int sock, void *buf, unsigned int bsz);
129 
130 int qrtr_new_server(int sock, uint32_t service, uint16_t version, uint16_t instance);
131 int qrtr_remove_server(int sock, uint32_t service, uint16_t version, uint16_t instance);
132 
133 int qrtr_publish(int sock, uint32_t service, uint16_t version, uint16_t instance);
134 int qrtr_bye(int sock, uint32_t service, uint16_t version, uint16_t instance);
135 
136 int qrtr_new_lookup(int sock, uint32_t service, uint16_t version, uint16_t instance);
137 int qrtr_remove_lookup(int sock, uint32_t service, uint16_t version, uint16_t instance);
138 
139 int qrtr_poll(int sock, unsigned int ms);
140 
141 int qrtr_decode(struct qrtr_packet *dest, void *buf, size_t len,
142 		const struct sockaddr_qrtr *sq);
143 
144 int qmi_decode_header(const struct qrtr_packet *pkt, unsigned int *msg_id);
145 int qmi_decode_message(void *c_struct, unsigned int *txn,
146 		       const struct qrtr_packet *pkt,
147 		       int type, int id, struct qmi_elem_info *ei);
148 ssize_t qmi_encode_message(struct qrtr_packet *pkt, int type, int msg_id,
149 			   int txn_id, const void *c_struct,
150 			   struct qmi_elem_info *ei);
151 
152 /* Initial kernel header didn't expose these */
153 #ifndef QRTR_NODE_BCAST
154 
155 #define QRTR_NODE_BCAST 0xffffffffu
156 #define QRTR_PORT_CTRL  0xfffffffeu
157 
158 enum qrtr_pkt_type {
159         QRTR_TYPE_DATA          = 1,
160         QRTR_TYPE_HELLO         = 2,
161         QRTR_TYPE_BYE           = 3,
162         QRTR_TYPE_NEW_SERVER    = 4,
163         QRTR_TYPE_DEL_SERVER    = 5,
164         QRTR_TYPE_DEL_CLIENT    = 6,
165         QRTR_TYPE_RESUME_TX     = 7,
166         QRTR_TYPE_EXIT          = 8,
167         QRTR_TYPE_PING          = 9,
168         QRTR_TYPE_NEW_LOOKUP    = 10,
169         QRTR_TYPE_DEL_LOOKUP    = 11,
170 };
171 
172 struct qrtr_ctrl_pkt {
173         __le32 cmd;
174 
175         union {
176                 struct {
177                         __le32 service;
178                         __le32 instance;
179                         __le32 node;
180                         __le32 port;
181                 } server;
182 
183                 struct {
184                         __le32 node;
185                         __le32 port;
186                 } client;
187         };
188 } __attribute__((packed));
189 
190 #endif
191 
192 #ifdef __cplusplus
193 }  /* extern "C" */
194 #endif
195 
196 #endif
197