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