/** * qmi_recv_new_server() - handler of NEW_SERVER control message * @qmi: qmi handle * @service: service id of the new server * @instance: instance id of the new server * @node: node of the new server * @port: port of the new server * * Calls the new_server callback to inform the client about a newly registered * server matching the currently registered service lookup.
*/ staticvoid qmi_recv_new_server(struct qmi_handle *qmi, unsignedint service, unsignedint instance, unsignedint node, unsignedint port)
{ struct qmi_ops *ops = &qmi->ops; struct qmi_service *svc; int ret;
if (!ops->new_server) return;
/* Ignore EOF marker */ if (!node && !port) return;
svc = kzalloc(sizeof(*svc), GFP_KERNEL); if (!svc) return;
ret = ops->new_server(qmi, svc); if (ret < 0)
kfree(svc); else
list_add(&svc->list_node, &qmi->lookup_results);
}
/** * qmi_recv_del_server() - handler of DEL_SERVER control message * @qmi: qmi handle * @node: node of the dying server, a value of -1 matches all nodes * @port: port of the dying server, a value of -1 matches all ports * * Calls the del_server callback for each previously seen server, allowing the * client to react to the disappearing server.
*/ staticvoid qmi_recv_del_server(struct qmi_handle *qmi, unsignedint node, unsignedint port)
{ struct qmi_ops *ops = &qmi->ops; struct qmi_service *svc; struct qmi_service *tmp;
/** * qmi_recv_bye() - handler of BYE control message * @qmi: qmi handle * @node: id of the dying node * * Signals the client that all previously registered services on this node are * now gone and then calls the bye callback to allow the client further * cleaning up resources associated with this remote.
*/ staticvoid qmi_recv_bye(struct qmi_handle *qmi, unsignedint node)
{ struct qmi_ops *ops = &qmi->ops;
qmi_recv_del_server(qmi, node, -1);
if (ops->bye)
ops->bye(qmi, node);
}
/** * qmi_recv_del_client() - handler of DEL_CLIENT control message * @qmi: qmi handle * @node: node of the dying client * @port: port of the dying client * * Signals the client about a dying client, by calling the del_client callback.
*/ staticvoid qmi_recv_del_client(struct qmi_handle *qmi, unsignedint node, unsignedint port)
{ struct qmi_ops *ops = &qmi->ops;
if (ops->del_client)
ops->del_client(qmi, node, port);
}
mutex_lock(&qmi->sock_lock); if (qmi->sock) {
ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt)); if (ret < 0)
pr_err("failed to send lookup registration: %d\n", ret);
}
mutex_unlock(&qmi->sock_lock);
}
/** * qmi_add_lookup() - register a new lookup with the name service * @qmi: qmi handle * @service: service id of the request * @version: version number of the request * @instance: instance id of the request * * Registering a lookup query with the name server will cause the name server * to send NEW_SERVER and DEL_SERVER control messages to this socket as * matching services are registered. * * Return: 0 on success, negative errno on failure.
*/ int qmi_add_lookup(struct qmi_handle *qmi, unsignedint service, unsignedint version, unsignedint instance)
{ struct qmi_service *svc;
svc = kzalloc(sizeof(*svc), GFP_KERNEL); if (!svc) return -ENOMEM;
mutex_lock(&qmi->sock_lock); if (qmi->sock) {
ret = kernel_sendmsg(qmi->sock, &msg, &iv, 1, sizeof(pkt)); if (ret < 0)
pr_err("send service registration failed: %d\n", ret);
}
mutex_unlock(&qmi->sock_lock);
}
/** * qmi_add_server() - register a service with the name service * @qmi: qmi handle * @service: type of the service * @instance: instance of the service * @version: version of the service * * Register a new service with the name service. This allows clients to find * and start sending messages to the client associated with @qmi. * * Return: 0 on success, negative errno on failure.
*/ int qmi_add_server(struct qmi_handle *qmi, unsignedint service, unsignedint version, unsignedint instance)
{ struct qmi_service *svc;
svc = kzalloc(sizeof(*svc), GFP_KERNEL); if (!svc) return -ENOMEM;
/** * qmi_txn_init() - allocate transaction id within the given QMI handle * @qmi: QMI handle * @txn: transaction context * @ei: description of how to decode a matching response (optional) * @c_struct: pointer to the object to decode the response into (optional) * * This allocates a transaction id within the QMI handle. If @ei and @c_struct * are specified any responses to this transaction will be decoded as described * by @ei into @c_struct. * * A client calling qmi_txn_init() must call either qmi_txn_wait() or * qmi_txn_cancel() to free up the allocated resources. * * Return: Transaction id on success, negative errno on failure.
*/ int qmi_txn_init(struct qmi_handle *qmi, struct qmi_txn *txn, conststruct qmi_elem_info *ei, void *c_struct)
{ int ret;
mutex_lock(&qmi->txn_lock);
ret = idr_alloc_cyclic(&qmi->txns, txn, 0, U16_MAX, GFP_KERNEL); if (ret < 0)
pr_err("failed to allocate transaction id\n");
txn->id = ret;
mutex_unlock(&qmi->txn_lock);
return ret;
}
EXPORT_SYMBOL_GPL(qmi_txn_init);
/** * qmi_txn_wait() - wait for a response on a transaction * @txn: transaction handle * @timeout: timeout, in jiffies * * If the transaction is decoded by the means of @ei and @c_struct the return * value will be the returned value of qmi_decode_message(), otherwise it's up * to the specified message handler to fill out the result. * * Return: the transaction response on success, negative errno on failure.
*/ int qmi_txn_wait(struct qmi_txn *txn, unsignedlong timeout)
{ struct qmi_handle *qmi = txn->qmi; int ret;
ret = wait_for_completion_timeout(&txn->completion, timeout);
/** * qmi_invoke_handler() - find and invoke a handler for a message * @qmi: qmi handle * @sq: sockaddr of the sender * @txn: transaction object for the message * @buf: buffer containing the message * @len: length of @buf * * Find handler and invoke handler for the incoming message.
*/ staticvoid qmi_invoke_handler(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, struct qmi_txn *txn, constvoid *buf, size_t len)
{ conststruct qmi_msg_handler *handler; conststruct qmi_header *hdr = buf; void *dest; int ret;
if (!qmi->handlers) return;
for (handler = qmi->handlers; handler->fn; handler++) { if (handler->type == hdr->type &&
handler->msg_id == le16_to_cpu(hdr->msg_id)) break;
}
if (!handler->fn) return;
dest = kzalloc(handler->decoded_size, GFP_KERNEL); if (!dest) return;
ret = qmi_decode_message(buf, len, handler->ei, dest); if (ret < 0)
pr_err("failed to decode incoming message\n"); else
handler->fn(qmi, sq, txn, dest);
kfree(dest);
}
/** * qmi_handle_net_reset() - invoked to handle ENETRESET on a QMI handle * @qmi: the QMI context * * As a result of registering a name service with the QRTR all open sockets are * flagged with ENETRESET and this function will be called. The typical case is * the initial boot, where this signals that the local node id has been * configured and as such any bound sockets needs to be rebound. So close the * socket, inform the client and re-initialize the socket. * * For clients it's generally sufficient to react to the del_server callbacks, * but server code is expected to treat the net_reset callback as a "bye" from * all nodes. * * Finally the QMI handle will send out registration requests for any lookups * and services.
*/ staticvoid qmi_handle_net_reset(struct qmi_handle *qmi)
{ struct sockaddr_qrtr sq; struct qmi_service *svc; struct socket *sock;
sock = qmi_sock_create(qmi, &sq); if (IS_ERR(sock)) return;
if (len < sizeof(*hdr)) {
pr_err("ignoring short QMI packet\n"); return;
}
hdr = buf;
/* If this is a response, find the matching transaction handle */ if (hdr->type == QMI_RESPONSE) {
mutex_lock(&qmi->txn_lock);
txn = idr_find(&qmi->txns, le16_to_cpu(hdr->txn_id));
mutex_unlock(&txn->lock);
} else { /* Create a txn based on the txn_id of the incoming message */
memset(&tmp_txn, 0, sizeof(tmp_txn));
tmp_txn.id = le16_to_cpu(hdr->txn_id);
/** * qmi_handle_init() - initialize a QMI client handle * @qmi: QMI handle to initialize * @recv_buf_size: maximum size of incoming message * @ops: reference to callbacks for QRTR notifications * @handlers: NULL-terminated list of QMI message handlers * * This initializes the QMI client handle to allow sending and receiving QMI * messages. As messages are received the appropriate handler will be invoked. * * Return: 0 on success, negative errno on failure.
*/ int qmi_handle_init(struct qmi_handle *qmi, size_t recv_buf_size, conststruct qmi_ops *ops, conststruct qmi_msg_handler *handlers)
{ int ret;
qmi->handlers = handlers; if (ops)
qmi->ops = *ops;
/* Make room for the header */
recv_buf_size += sizeof(struct qmi_header); /* Must also be sufficient to hold a control packet */ if (recv_buf_size < sizeof(struct qrtr_ctrl_pkt))
recv_buf_size = sizeof(struct qrtr_ctrl_pkt);
/* Free registered service information */
list_for_each_entry_safe(svc, tmp, &qmi->services, list_node) {
list_del(&svc->list_node);
kfree(svc);
}
}
EXPORT_SYMBOL_GPL(qmi_handle_release);
/** * qmi_send_message() - send a QMI message * @qmi: QMI client handle * @sq: destination sockaddr * @txn: transaction object to use for the message * @type: type of message to send * @msg_id: message id * @len: max length of the QMI message * @ei: QMI message description * @c_struct: object to be encoded * * This function encodes @c_struct using @ei into a message of type @type, * with @msg_id and @txn into a buffer of maximum size @len, and sends this to * @sq. * * Return: 0 on success, negative errno on failure.
*/ static ssize_t qmi_send_message(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, struct qmi_txn *txn, int type, int msg_id, size_t len, conststruct qmi_elem_info *ei, constvoid *c_struct)
{ struct msghdr msghdr = {}; struct kvec iv; void *msg; int ret;
msg = qmi_encode_message(type,
msg_id, &len,
txn->id, ei,
c_struct); if (IS_ERR(msg)) return PTR_ERR(msg);
iv.iov_base = msg;
iv.iov_len = len;
if (sq) {
msghdr.msg_name = sq;
msghdr.msg_namelen = sizeof(*sq);
}
mutex_lock(&qmi->sock_lock); if (qmi->sock) {
ret = kernel_sendmsg(qmi->sock, &msghdr, &iv, 1, len); if (ret < 0)
pr_err("failed to send QMI message\n");
} else {
ret = -EPIPE;
}
mutex_unlock(&qmi->sock_lock);
kfree(msg);
return ret < 0 ? ret : 0;
}
/** * qmi_send_request() - send a request QMI message * @qmi: QMI client handle * @sq: destination sockaddr * @txn: transaction object to use for the message * @msg_id: message id * @len: max length of the QMI message * @ei: QMI message description * @c_struct: object to be encoded * * Return: 0 on success, negative errno on failure.
*/
ssize_t qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, struct qmi_txn *txn, int msg_id, size_t len, conststruct qmi_elem_info *ei, constvoid *c_struct)
{ return qmi_send_message(qmi, sq, txn, QMI_REQUEST, msg_id, len, ei,
c_struct);
}
EXPORT_SYMBOL_GPL(qmi_send_request);
/** * qmi_send_response() - send a response QMI message * @qmi: QMI client handle * @sq: destination sockaddr * @txn: transaction object to use for the message * @msg_id: message id * @len: max length of the QMI message * @ei: QMI message description * @c_struct: object to be encoded * * Return: 0 on success, negative errno on failure.
*/
ssize_t qmi_send_response(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, struct qmi_txn *txn, int msg_id, size_t len, conststruct qmi_elem_info *ei, constvoid *c_struct)
{ return qmi_send_message(qmi, sq, txn, QMI_RESPONSE, msg_id, len, ei,
c_struct);
}
EXPORT_SYMBOL_GPL(qmi_send_response);
/** * qmi_send_indication() - send an indication QMI message * @qmi: QMI client handle * @sq: destination sockaddr * @msg_id: message id * @len: max length of the QMI message * @ei: QMI message description * @c_struct: object to be encoded * * Return: 0 on success, negative errno on failure.
*/
ssize_t qmi_send_indication(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, int msg_id, size_t len, conststruct qmi_elem_info *ei, constvoid *c_struct)
{ struct qmi_txn txn;
ssize_t rval; int ret;
ret = qmi_txn_init(qmi, &txn, NULL, NULL); if (ret < 0) return ret;
rval = qmi_send_message(qmi, sq, &txn, QMI_INDICATION, msg_id, len, ei,
c_struct);
/* We don't care about future messages on this txn */
qmi_txn_cancel(&txn);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.