1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) 2025, Richard Acayan. All rights reserved.
*/
#include <gio/gio.h>
#include <glib.h>
#include <libqmi-glib.h>
#include <libqrtr.h>
#include <stdio.h>
#include <string.h>
#include "qvd-message.h"
enum {
PROP_REQ = 1,
PROP_SOCK,
PROP_NODE,
PROP_PORT,
N_PROPS,
};
struct _QVDMessage {
GObject parent_instance;
gboolean txn_valid;
unsigned int txn;
GSocket *sock;
struct qrtr_packet *req;
guint32 node;
guint32 port;
};
G_DEFINE_TYPE(QVDMessage, qvd_message, G_TYPE_OBJECT)
static GParamSpec *props[N_PROPS] = { NULL, };
static void qvd_message_dispose(GObject *obj);
void qvd_message_get_property(GObject *obj,
guint prop,
GValue *val,
GParamSpec *spec)
{
QVDMessage *ctx = QVD_MESSAGE(obj);
switch (prop) {
case PROP_REQ:
g_value_set_pointer(val, ctx->req);
break;
case PROP_SOCK:
g_value_set_object(val, G_OBJECT(ctx->sock));
break;
case PROP_NODE:
g_value_set_uint(val, ctx->node);
break;
case PROP_PORT:
g_value_set_uint(val, ctx->port);
break;
default:
break;
}
}
void qvd_message_set_property(GObject *obj,
guint prop,
const GValue *val,
GParamSpec *spec)
{
QVDMessage *ctx = QVD_MESSAGE(obj);
switch (prop) {
case PROP_REQ:
ctx->req = g_value_get_pointer(val);
break;
case PROP_SOCK:
ctx->sock = G_SOCKET(g_value_get_object(val));
break;
case PROP_NODE:
ctx->node = g_value_get_uint(val);
break;
case PROP_PORT:
ctx->port = g_value_get_uint(val);
break;
default:
break;
}
}
static void qvd_message_class_init(QVDMessageClass *c)
{
GObjectClass *obj_class = G_OBJECT_CLASS(c);
obj_class->get_property = qvd_message_get_property;
obj_class->set_property = qvd_message_set_property;
obj_class->dispose = qvd_message_dispose;
props[PROP_REQ] = g_param_spec_pointer("request", "Request",
"QMI request from the modem",
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
props[PROP_SOCK] = g_param_spec_object("socket", "Socket",
"Socket for request and response",
G_TYPE_SOCKET,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
props[PROP_NODE] = g_param_spec_uint("peer-node", "Peer Node",
"QRTR Node of client",
0, UINT32_MAX, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
props[PROP_PORT] = g_param_spec_uint("peer-port", "Peer Port",
"QRTR Port of client",
0, UINT32_MAX, 0,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
g_object_class_install_properties(obj_class, N_PROPS, props);
}
static void qvd_message_init(QVDMessage *ctx)
{
}
static void qvd_message_dispose(GObject *obj)
{
QVDMessage *ctx = QVD_MESSAGE(obj);
g_clear_object(&ctx->sock);
g_free(ctx->req->data);
g_free(ctx->req);
}
QVDMessage *qvd_message_new(GSocket *sock,
const struct qrtr_packet *req,
guint32 node, guint32 port)
{
return QVD_MESSAGE(g_object_new(QVD_TYPE_MESSAGE,
"socket", sock,
"request", req,
"peer-node", node,
"peer-port", port,
NULL));
}
guint qvd_message_get_msg_id(QVDMessage *ctx)
{
unsigned int msg_id;
qmi_decode_header(ctx->req, &msg_id);
return msg_id;
}
void qvd_message_decode(QVDMessage *ctx,
void *c_struct,
struct qmi_elem_info *ei,
GError **out_err)
{
GError *err;
guint msg_id;
int ret;
msg_id = qvd_message_get_msg_id(ctx);
ret = qmi_decode_message(c_struct, &ctx->txn, ctx->req, QMI_REQUEST, msg_id, ei);
if (ret < 0) {
err = g_error_new(QMI_CORE_ERROR, QMI_CORE_ERROR_INVALID_MESSAGE,
"failed to decode request: %s",
strerror(-ret));
g_propagate_error(out_err, err);
return;
}
ctx->txn_valid = true;
}
void qvd_message_respond(QVDMessage *ctx,
const void *c_struct,
struct qmi_elem_info *ei,
GError **out_err)
{
GError *err;
struct qrtr_packet outpkt;
guint msg_id;
ssize_t ret;
int fd;
if (!ctx->txn_valid) {
err = g_error_new(QMI_CORE_ERROR, QMI_CORE_ERROR_WRONG_STATE,
"request must be decoded before issuing a response");
g_propagate_error(out_err, err);
return;
}
fd = g_socket_get_fd(ctx->sock);
msg_id = qvd_message_get_msg_id(ctx);
outpkt.data_len = 65536;
outpkt.data = g_malloc(outpkt.data_len);
ret = qmi_encode_message(&outpkt, QMI_RESPONSE, msg_id, ctx->txn, c_struct, ei);
if (ret < 0) {
err = g_error_new(QMI_CORE_ERROR, QMI_CORE_ERROR_INVALID_MESSAGE,
"failed to encode response: %s",
strerror(-ret));
g_propagate_error(out_err, err);
goto err;
}
ret = qrtr_sendto(fd, ctx->node, ctx->port, outpkt.data, outpkt.data_len);
if (ret < 0) {
err = g_error_new(QMI_CORE_ERROR, QMI_CORE_ERROR_INVALID_MESSAGE,
"failed to send response: %s",
strerror(-ret));
g_propagate_error(out_err, err);
}
err:
g_free(outpkt.data);
}
|