Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp20327pxx; Wed, 28 Oct 2020 16:49:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywS3v6fE5YAoRPhIoIgVginymZb3yO/Vpxf14K0FLa8/JZZelPKb+EyQAtoIk+cXjfRS5J X-Received: by 2002:a50:d88c:: with SMTP id p12mr1342036edj.352.1603928980005; Wed, 28 Oct 2020 16:49:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603928979; cv=none; d=google.com; s=arc-20160816; b=XwLvIxFKBXP2CSLFZjEfQPoxvqtabzd5JF3gaLtqAe6pYFJogSz2+JrEdVWIEEuUe9 H0aPYo1zYZca7+rjB9rrZdQJJ201L3zvSsEaD5daMoX1vjwptIyjrObUa++VbpyxXtdZ fFm33mJH2hO6hTfanyqK0gTh9rUGMbRZontmqZlUJ3USfU2RhIUkUsoK8H0dpYqHHH0r zBMhJIGlcAZSFLaIvZnisTFIHpACRWhITV03FKzRJXsajetFMApK4hz97WU9Qvg5RUoy naESUOOsxIb4/qKzLoh5AtygHtMGA0WQ9iNDG+EkkU36yfDsY6I9Z0GeRVlfYbKmVDfN sArQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :sender:dkim-signature; bh=5ukqu3eD+sBtgJjiHf4OJ2+rkm1jUG9yhNQ5PdOd7KQ=; b=GrJc7yEvHen0741X+tDJDmjgUW9kJDYJAGG8LI3reCz6AUX62I6XjTQq2ztZz6uexB tTKynvYURB9gu618FGAQUQgUkx91RR8ciFdMThvkhIFhZ1R9Ujs6uEYC1YYN8rURO0tm fh/JvBF1UANeQvWzNxm0wrM4QOGVyLHEDV5WOO6SKQyISugMMuleodPVN0H7hSbr6ft1 mfYMVMeQcrD8ilhWDfBQ+RYi67LRD1Udw2RUqrs8JXyzbkYQ9JKXF5bMlomvTurDtap4 nwJeycQOAYBgeJ9w1huhL2REXKxovmSPoRNHr7sYkHKXUIe5uCMP6roEqDdCS1wj0C5G i7wA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=lp53cvzl; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m8si797482edj.357.2020.10.28.16.49.16; Wed, 28 Oct 2020 16:49:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=lp53cvzl; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727534AbgJ1XsW (ORCPT + 99 others); Wed, 28 Oct 2020 19:48:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725768AbgJ1XsW (ORCPT ); Wed, 28 Oct 2020 19:48:22 -0400 Received: from mail-oo1-xc49.google.com (mail-oo1-xc49.google.com [IPv6:2607:f8b0:4864:20::c49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDED3C0613CF for ; Wed, 28 Oct 2020 16:48:21 -0700 (PDT) Received: by mail-oo1-xc49.google.com with SMTP id t9so394792oon.9 for ; Wed, 28 Oct 2020 16:48:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=5ukqu3eD+sBtgJjiHf4OJ2+rkm1jUG9yhNQ5PdOd7KQ=; b=lp53cvzlZvlprwJM36+J6rFotTTnggS1qfnDDp5ybCRY1SOJ3wZS6stG79iW1iNT4c xdwwYwlseHmE875z6yteCh7+TEkr83lmW99laWrcUCZL8mYnC64V3oiD2xd3yj9cy8k5 PnHL7BcmzmfTMe7EPXjYNkeE+9ERaSCNdBcrw//aAA95Bjf6QEfdbzomilp2o7mGRp8B ylGf88Syduo53m21tuNOhhDDBH3FgPOh33YXIC/DMCjY0cVFNjoz9DBikhiKZD5VO4zg BNT+Ow0CTA0M035/RGqw1KjIbJt9/LYXfjgbEjcae4TI5taz5T8alfFgmzsTaeQAIaai ItOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=5ukqu3eD+sBtgJjiHf4OJ2+rkm1jUG9yhNQ5PdOd7KQ=; b=GQxvcPBQfU4Jj4WZbArWjjBaFDunortXroJwTgujhlhb96m9UetNAthU1pRARw4UGh r75jGjouiESwrBjWCV+Zb4AUzDT2Gda/lsv3C8tVSnljCRmOGt0Z2eLvILmXc4AlTqlX HuTAeyESSY6JTFS6mB6nUxg+O2gLh9vrErnUII7NDUPRaGaW05Fw49utApJO4VwMXWt9 ZQv78pTAlPI9jYY0l5Pbo+x+ZCd4qo0qJJVg84m6inGL5UOxurqUf22IzCiABQ1RD8Qg 7AJVF0bSz+s9mECHWljiWNEpF2wdgdNMxE0b4zuEDTMttwZ0KPxpX5MwLLtfo/Xooxfg oiFA== X-Gm-Message-State: AOAM532cL+Hl9WyG2BjI856r4yZ8q4kEWJE0YjgSOvQ+BvBoY1qJ2clH bpjbxABnJTWuLGqzef9EhqU61lBcOxz1lQDJ1H/ftDh8aOX+b13AVotLiEeKMyDJ/yKbE/fPfP4 6mOPO78sHtDSOsZVVLTYXrsmrobpd3NTAxRT66Yo9tqj8TmiVuFW3nn0e0NUYdt4V5Rvh8tl/GW TS9o8Cvng4Iy8= Sender: "howardchung via sendgmr" X-Received: from howardchung-p920.tpe.corp.google.com ([2401:fa00:1:10:f693:9fff:fef4:4e45]) (user=howardchung job=sendgmr) by 2002:a25:d441:: with SMTP id m62mr10016672ybf.422.1603885978195; Wed, 28 Oct 2020 04:52:58 -0700 (PDT) Date: Wed, 28 Oct 2020 19:52:46 +0800 Message-Id: <20201028195132.BlueZ.v5.1.I2fa28644a012f69c4665977e5816bcf8a51963f5@changeid> Mime-Version: 1.0 X-Mailer: git-send-email 2.29.0.rc2.309.g374f81d7ae-goog Subject: [BlueZ PATCH v5 1/2] mgmt: Add support of mgmt TLV API From: Howard Chung To: linux-bluetooth@vger.kernel.org Cc: apusaka@chromium.org, mmandlik@chromium.org, alainm@chromium.org, luiz.dentz@gmail.com, mcchou@chromium.org, Howard Chung Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This adds API to send multiple TLVs to kernel, it is useful for mgmt set system config command. --- Changes in v5: - Implement mgmt_tlv API in src/shared/mgmt.c Changes in v4: - fix type of buf_len Changes in v3: - use iovec to fill buffer Changes in v2: - fix multiple line dereference issue src/shared/mgmt.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++ src/shared/mgmt.h | 9 +++++ 2 files changed, 103 insertions(+) diff --git a/src/shared/mgmt.c b/src/shared/mgmt.c index b327b4088c6f..9ea9974f5535 100644 --- a/src/shared/mgmt.c +++ b/src/shared/mgmt.c @@ -68,6 +68,11 @@ struct mgmt_notify { void *user_data; }; +struct mgmt_tlv_list { + struct queue *tlv_queue; + uint16_t size; +}; + static void destroy_request(void *data) { struct mgmt_request *request = data; @@ -558,6 +563,95 @@ static struct mgmt_request *create_request(uint16_t opcode, uint16_t index, return request; } +struct mgmt_tlv_list *mgmt_tlv_list_new(void) +{ + struct mgmt_tlv_list *tlv_list = new0(struct mgmt_tlv_list, 1); + + tlv_list->tlv_queue = queue_new(); + tlv_list->size = 0; + + return tlv_list; +} + +static struct mgmt_tlv *mgmt_tlv_new(uint16_t type, uint8_t length, + void *value) +{ + struct mgmt_tlv *entry = malloc(sizeof(*entry) + length); + + if (!entry) + return NULL; + + entry->type = htobs(type); + entry->length = length; + memcpy(entry->value, value, length); + + return entry; +} + +static void mgmt_tlv_free(struct mgmt_tlv *entry) +{ + free(entry); +} + +void mgmt_tlv_list_free(struct mgmt_tlv_list *tlv_list) +{ + queue_destroy(tlv_list->tlv_queue, NULL); + free(tlv_list); +} + +bool mgmt_tlv_add(struct mgmt_tlv_list *tlv_list, uint16_t type, uint8_t length, + void *value) +{ + struct mgmt_tlv *entry = mgmt_tlv_new(type, length, value); + + if (!entry) + return false; + + if (!queue_push_tail(tlv_list->tlv_queue, entry)) { + mgmt_tlv_free(entry); + return false; + } + + tlv_list->size += sizeof(*entry) + entry->length; + return true; +} + +static void mgmt_tlv_to_buf(void *data, void *user_data) +{ + struct mgmt_tlv *entry = data; + uint8_t **buf_ptr = user_data; + size_t entry_size = sizeof(*entry) + entry->length; + + memcpy(*buf_ptr, entry, entry_size); + *buf_ptr += entry_size; +} + +unsigned int mgmt_send_tlv(struct mgmt *mgmt, uint16_t opcode, uint16_t index, + struct mgmt_tlv_list *tlv_list, + mgmt_request_func_t callback, + void *user_data, mgmt_destroy_func_t destroy) +{ + uint8_t *buf, *buf_ptr; + unsigned int ret; + + if (!tlv_list) + return 0; + + buf = malloc(tlv_list->size); + + if (!buf) + return 0; + + buf_ptr = buf; + + queue_foreach(tlv_list->tlv_queue, mgmt_tlv_to_buf, &buf_ptr); + + ret = mgmt_send(mgmt, opcode, index, tlv_list->size, (void *)buf, + callback, user_data, destroy); + free(buf); + return ret; +} + unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index, uint16_t length, const void *param, mgmt_request_func_t callback, diff --git a/src/shared/mgmt.h b/src/shared/mgmt.h index 6608faa7ed0f..74b8befd87bd 100644 --- a/src/shared/mgmt.h +++ b/src/shared/mgmt.h @@ -16,6 +16,7 @@ typedef void (*mgmt_destroy_func_t)(void *user_data); struct mgmt; +struct mgmt_tlv_list; struct mgmt *mgmt_new(int fd); struct mgmt *mgmt_new_default(void); @@ -33,6 +34,14 @@ bool mgmt_set_close_on_unref(struct mgmt *mgmt, bool do_close); typedef void (*mgmt_request_func_t)(uint8_t status, uint16_t length, const void *param, void *user_data); +struct mgmt_tlv_list *mgmt_tlv_list_new(void); +void mgmt_tlv_list_free(struct mgmt_tlv_list *tlv_list); +bool mgmt_tlv_add(struct mgmt_tlv_list *tlv_list, uint16_t type, uint8_t length, + void *value); +unsigned int mgmt_send_tlv(struct mgmt *mgmt, uint16_t opcode, uint16_t index, + struct mgmt_tlv_list *tlv_list, + mgmt_request_func_t callback, + void *user_data, mgmt_destroy_func_t destroy); unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index, uint16_t length, const void *param, mgmt_request_func_t callback, -- 2.29.0.rc2.309.g374f81d7ae-goog