blob: 543dfb3fe9a997722969f442f205ec8b700ac0b5 [file] [log] [blame]
Amit Pundir33109f72020-02-07 22:26:08 +05301/*
2 * Copyright (c) 2018, Linaro Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31#include <errno.h>
32#include <libqrtr.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "servreg_loc.h"
39
40struct pd_map {
41 const char *service;
42 const char *domain;
43 int instance;
44};
45
46static const struct pd_map pd_maps[] = {
47 { "kernel/elf_loader", "msm/modem/wlan_pd", 1 },
48 {}
49};
50
51static void handle_get_domain_list(int sock, const struct qrtr_packet *pkt)
52{
53 struct servreg_loc_get_domain_list_resp resp = {};
54 struct servreg_loc_get_domain_list_req req = {};
55 struct servreg_loc_domain_list_entry *entry;
56 DEFINE_QRTR_PACKET(resp_buf, 256);
57 const struct pd_map *pd_map = pd_maps;
58 unsigned int txn;
59 ssize_t len;
60 int ret;
61
62 ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
63 SERVREG_LOC_GET_DOMAIN_LIST,
64 servreg_loc_get_domain_list_req_ei);
65 if (ret < 0) {
66 resp.result.result = QMI_RESULT_FAILURE;
67 resp.result.error = QMI_ERR_MALFORMED_MSG;
68 goto respond;
69 }
70
71 req.name[sizeof(req.name)-1] = '\0';
72
73 resp.result.result = QMI_RESULT_SUCCESS;
74 resp.db_revision_valid = 1;
75 resp.db_revision = 1;
76
77 while (pd_map->service) {
78 if (!strcmp(pd_map->service, req.name)) {
79 entry = &resp.domain_list[resp.domain_list_len++];
80
81 strcpy(entry->name, pd_map->domain);
82 entry->name_len = strlen(pd_map->domain);
83 entry->instance_id = pd_map->instance;
84 }
85
86 pd_map++;
87 }
88
89 if (resp.domain_list_len)
90 resp.domain_list_valid = 1;
91
92 resp.total_domains_valid = 1;
93 resp.total_domains = resp.domain_list_len;
94
95respond:
96 len = qmi_encode_message(&resp_buf,
97 QMI_RESPONSE, SERVREG_LOC_GET_DOMAIN_LIST,
98 txn, &resp,
99 servreg_loc_get_domain_list_resp_ei);
100 if (len < 0) {
101 fprintf(stderr,
102 "[PD-MAPPER] failed to encode get_domain_list response: %s\n",
103 strerror(-len));
104 return;
105 }
106
107 ret = qrtr_sendto(sock, pkt->node, pkt->port,
108 resp_buf.data, resp_buf.data_len);
109 if (ret < 0) {
110 fprintf(stderr,
111 "[PD-MAPPER] failed to send get_domain_list response: %s\n",
112 strerror(-ret));
113 }
114}
115
John Stultz68b3e612020-02-26 23:41:07 +0000116int main(int argc __unused, char **argv __unused)
Amit Pundir33109f72020-02-07 22:26:08 +0530117{
118 struct sockaddr_qrtr sq;
119 struct qrtr_packet pkt;
120 unsigned int msg_id;
121 socklen_t sl;
122 char buf[4096];
123 int ret;
124 int fd;
125
126 fd = qrtr_open(0);
127 if (fd < 0) {
128 fprintf(stderr, "failed to open qrtr socket\n");
129 exit(1);
130 }
131
132 ret = qrtr_publish(fd, SERVREG_QMI_SERVICE,
133 SERVREG_QMI_VERSION, SERVREG_QMI_INSTANCE);
134 if (ret < 0) {
135 fprintf(stderr, "failed to publish service registry service\n");
136 exit(1);
137 }
138
139 for (;;) {
140 ret = qrtr_poll(fd, -1);
141 if (ret < 0) {
142 if (errno == EINTR) {
143 continue;
144 } else {
145 fprintf(stderr, "qrtr_poll failed\n");
146 break;
147 }
148 }
149
150 sl = sizeof(sq);
151 ret = recvfrom(fd, buf, sizeof(buf), 0, (void *)&sq, &sl);
152 if (ret < 0) {
153 ret = -errno;
154 if (ret != -ENETRESET)
155 fprintf(stderr, "[PD-MAPPER] recvfrom failed: %d\n", ret);
156 return ret;
157 }
158
159 ret = qrtr_decode(&pkt, buf, ret, &sq);
160 if (ret < 0) {
161 fprintf(stderr, "[PD-MAPPER] unable to decode qrtr packet\n");
162 return ret;
163 }
164
165 switch (pkt.type) {
166 case QRTR_TYPE_DATA:
167 ret = qmi_decode_header(&pkt, &msg_id);
168 if (ret < 0)
169 continue;
170
171 switch (msg_id) {
172 case SERVREG_LOC_GET_DOMAIN_LIST:
173 handle_get_domain_list(fd, &pkt);
174 break;
175 case SERVREG_LOC_PFR:
176 printf("[PD-MAPPER] pfr\n");
177 break;
178 };
179 break;
180 };
181 }
182
183 close(fd);
184
185 return 0;
186}