1 #include <err.h>
2 #include <errno.h>
3 #include <libgen.h>
4 #include <linux/qrtr.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/time.h>
11 #include <unistd.h>
12 
13 #include "libqrtr.h"
14 #include "logging.h"
15 #include "ns.h"
16 #include "util.h"
17 
18 #define DIAG_SERVICE 4097
19 
20 static const struct {
21 	unsigned int service;
22 	unsigned int ifilter;
23 	const char *name;
24 } common_names[] = {
25 	{ 0, 0, "Control service" },
26 	{ 1, 0, "Wireless Data Service" },
27 	{ 2, 0, "Device Management Service" },
28 	{ 3, 0, "Network Access Service" },
29 	{ 4, 0, "Quality Of Service service" },
30 	{ 5, 0, "Wireless Messaging Service" },
31 	{ 6, 0, "Position Determination Service" },
32 	{ 7, 0, "Authentication service" },
33 	{ 8, 0, "AT service" },
34 	{ 9, 0, "Voice service" },
35 	{ 10, 0, "Card Application Toolkit service (v2)" },
36 	{ 11, 0, "User Identity Module service" },
37 	{ 12, 0, "Phonebook Management service" },
38 	{ 13, 0, "QCHAT service" },
39 	{ 14, 0, "Remote file system service" },
40 	{ 15, 0, "Test service" },
41 	{ 16, 0, "Location service (~ PDS v2)" },
42 	{ 17, 0, "Specific absorption rate service" },
43 	{ 18, 0, "IMS settings service" },
44 	{ 19, 0, "Analog to digital converter driver service" },
45 	{ 20, 0, "Core sound driver service" },
46 	{ 21, 0, "Modem embedded file system service" },
47 	{ 22, 0, "Time service" },
48 	{ 23, 0, "Thermal sensors service" },
49 	{ 24, 0, "Thermal mitigation device service" },
50 	{ 25, 0, "Service access proxy service" },
51 	{ 26, 0, "Wireless data administrative service" },
52 	{ 27, 0, "TSYNC control service" },
53 	{ 28, 0, "Remote file system access service" },
54 	{ 29, 0, "Circuit switched videotelephony service" },
55 	{ 30, 0, "Qualcomm mobile access point service" },
56 	{ 31, 0, "IMS presence service" },
57 	{ 32, 0, "IMS videotelephony service" },
58 	{ 33, 0, "IMS application service" },
59 	{ 34, 0, "Coexistence service" },
60 	{ 36, 0, "Persistent device configuration service" },
61 	{ 38, 0, "Simultaneous transmit service" },
62 	{ 39, 0, "Bearer independent transport service" },
63 	{ 40, 0, "IMS RTP service" },
64 	{ 41, 0, "RF radiated performance enhancement service" },
65 	{ 42, 0, "Data system determination service" },
66 	{ 43, 0, "Subsystem control service" },
67 	{ 49, 0, "IPA control service" },
68 	{ 51, 0, "CoreSight remote tracing service" },
69 	{ 52, 0, "Dynamic Heap Memory Sharing" },
70 	{ 64, 0, "Service registry locator service" },
71 	{ 66, 0, "Service registry notification service" },
72 	{ 69, 0, "ATH10k WLAN firmware service" },
73 	{ 224, 0, "Card Application Toolkit service (v1)" },
74 	{ 225, 0, "Remote Management Service" },
75 	{ 226, 0, "Open Mobile Alliance device management service" },
76 	{ 312, 0, "QBT1000 Ultrasonic Fingerprint Sensor service" },
77 	{ 769, 0, "SLIMbus control service" },
78 	{ 771, 0, "Peripheral Access Control Manager service" },
79 	{ DIAG_SERVICE, 0, "DIAG service" },
80 };
81 
diag_instance_base_str(unsigned int instance_base)82 static const char *diag_instance_base_str(unsigned int instance_base)
83 {
84 	switch (instance_base) {
85 		case 0: return "MODEM";
86 		case 1: return "LPASS";
87 		case 2: return "WCNSS";
88 		case 3: return "SENSORS";
89 		case 4: return "CDSP";
90 		case 5: return "WDSP";
91 		default: return "<unk>";
92 	}
93 }
94 
diag_instance_str(unsigned int instance)95 static const char *diag_instance_str(unsigned int instance)
96 {
97 	switch (instance) {
98 		case 0: return "CNTL";
99 		case 1: return "CMD";
100 		case 2: return "DATA";
101 		case 3: return "DCI_CMD";
102 		case 4: return "DCI";
103 		default: return "<unk>";
104 	}
105 }
106 
get_diag_instance_info(char * str,size_t size,unsigned int instance)107 static int get_diag_instance_info(char *str, size_t size, unsigned int instance)
108 {
109 	return snprintf(str, size, "%s:%s",
110 			diag_instance_base_str(instance >> 6),
111 			diag_instance_str(instance & 0x3f));
112 }
113 
read_num_le(const char * str,int * rcp)114 static unsigned int read_num_le(const char *str, int *rcp)
115 {
116 	unsigned int ret;
117 	char *e;
118 
119 	if (*rcp)
120 		return 0;
121 
122 	errno = 0;
123 	ret = strtoul(str, &e, 0);
124 	*rcp = -(errno || *e);
125 
126 	return cpu_to_le32(ret);
127 }
128 
main(int argc,char ** argv)129 int main(int argc, char **argv)
130 {
131 	struct qrtr_ctrl_pkt pkt;
132 	struct sockaddr_qrtr sq;
133 	unsigned int instance;
134 	unsigned int service;
135 	unsigned int version;
136 	unsigned int node;
137 	unsigned int port;
138 	socklen_t sl = sizeof(sq);
139 	struct timeval tv;
140 	int sock;
141 	int len;
142 	int rc;
143 	const char *progname = basename(argv[0]);
144 
145 	qlog_setup(progname, false);
146 
147 	rc = 0;
148 	memset(&pkt, 0, sizeof(pkt));
149 
150 	switch (argc) {
151 	default:
152 		rc = -1;
153 		break;
154 	case 3: pkt.server.instance = read_num_le(argv[2], &rc);
155 	case 2: pkt.server.service = read_num_le(argv[1], &rc);
156 	case 1: break;
157 	}
158 	if (rc) {
159 		fprintf(stderr, "Usage: %s [<service> [<instance> [<filter>]]]\n", progname);
160 		exit(1);
161 	}
162 
163 	sock = socket(AF_QIPCRTR, SOCK_DGRAM, 0);
164 	if (sock < 0)
165 		PLOGE_AND_EXIT("sock(AF_QIPCRTR)");
166 
167 	rc = getsockname(sock, (void *)&sq, &sl);
168 	if (rc || sq.sq_family != AF_QIPCRTR || sl != sizeof(sq))
169 		PLOGE_AND_EXIT("getsockname()");
170 
171 	sq.sq_port = QRTR_PORT_CTRL;
172 
173 	tv.tv_sec = 1;
174 	tv.tv_usec = 0;
175 
176 	pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_LOOKUP);
177 
178 	rc = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
179 	if (rc)
180 		PLOGE_AND_EXIT("setsockopt(SO_RCVTIMEO)");
181 
182 	rc = sendto(sock, &pkt, sizeof(pkt), 0, (void *)&sq, sizeof(sq));
183 	if (rc < 0)
184 		PLOGE_AND_EXIT("sendto()");
185 
186 	printf("  Service Version Instance Node  Port\n");
187 
188 	while ((len = recv(sock, &pkt, sizeof(pkt), 0)) > 0) {
189 		unsigned int type = le32_to_cpu(pkt.cmd);
190 		const char *name = NULL;
191 		unsigned int i;
192 
193 		if (len < sizeof(pkt) || type != QRTR_TYPE_NEW_SERVER) {
194 			PLOGW("invalid/short packet");
195 			continue;
196 		}
197 
198 		if (!pkt.server.service && !pkt.server.instance &&
199 		    !pkt.server.node && !pkt.server.port)
200 			break;
201 
202 		service = le32_to_cpu(pkt.server.service);
203 		version = le32_to_cpu(pkt.server.instance) & 0xff;
204 		instance = le32_to_cpu(pkt.server.instance) >> 8;
205 		node = le32_to_cpu(pkt.server.node);
206 		port = le32_to_cpu(pkt.server.port);
207 
208 		for (i = 0; i < sizeof(common_names)/sizeof(common_names[0]); ++i) {
209 			if (service != common_names[i].service)
210 				continue;
211 			if (instance &&
212 			   (instance & common_names[i].ifilter) != common_names[i].ifilter)
213 				continue;
214 			name = common_names[i].name;
215 		}
216 		if (!name)
217 			name = "<unknown>";
218 
219 		if (service == DIAG_SERVICE) {
220 			char buf[24];
221 			instance = le32_to_cpu(pkt.server.instance);
222 			get_diag_instance_info(buf, sizeof(buf), instance);
223 			printf("%9d %7s %8d %4d %5d %s (%s)\n",
224 				service, "N/A", instance, node, port, name, buf);
225 		} else {
226 			printf("%9d %7d %8d %4d %5d %s\n",
227 				service, version, instance, node, port, name);
228 		}
229 	}
230 
231 	if (len < 0)
232 		PLOGE_AND_EXIT("recv()");
233 
234 	close(sock);
235 
236 	return 0;
237 }
238