<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "os/os.h"
#include "console/console.h"
#include "nimble/hci_common.h"
#include "nimble/hci_transport.h"
#include "host/host_hci.h"
#include "host_dbg_priv.h"
#include "ble_hs_priv.h"
#ifdef PHONY_TRANSPORT
#include "host/ble_hs_test.h"
#endif

/**
 * This buffer holds one of the following:
 * 1. The current outgoing HCI command.
 * 2. The current incoming HCI acknowledgement (command complete or command
 *    status event).
 */
uint8_t host_hci_cmd_buf[HCI_EVT_BUF_SIZE];

static int
host_hci_cmd_transport(uint8_t *cmdbuf)
{
#ifdef PHONY_TRANSPORT
    ble_hs_test_hci_txed(cmdbuf);
    return 0;
#else
    int rc;

    rc = ble_hci_transport_host_cmd_send(cmdbuf);
    switch (rc) {
    case 0:
        return 0;

    case BLE_ERR_MEM_CAPACITY:
        return BLE_HS_ENOMEM_EVT;

    default:
        return BLE_HS_EUNKNOWN;
    }
#endif
}

void
host_hci_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len, void *buf)
{
    uint16_t opcode;
    uint8_t *u8ptr;

    u8ptr = buf;

    opcode = (ogf << 10) | ocf;
    htole16(u8ptr, opcode);
    u8ptr[2] = len;
}

int
host_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len, const void *cmddata)
{
    int rc;

    htole16(host_hci_cmd_buf, ogf << 10 | ocf);
    host_hci_cmd_buf[2] = len;
    if (len != 0) {
        memcpy(host_hci_cmd_buf + BLE_HCI_CMD_HDR_LEN, cmddata, len);
    }

    rc = host_hci_cmd_transport(host_hci_cmd_buf);
    BLE_HS_LOG(DEBUG, "host_hci_cmd_send: ogf=0x%02x ocf=0x%02x len=%d "
                      "rc=%d\n", ogf, ocf, len, rc);
    ble_hs_misc_log_flat_buf(host_hci_cmd_buf, len + BLE_HCI_CMD_HDR_LEN);
    BLE_HS_LOG(DEBUG, "\n");

    if (rc == 0) {
        STATS_INC(ble_hs_stats, hci_cmd);
    }

    return rc;
}

int
host_hci_cmd_send_buf(void *buf)
{
    uint16_t opcode;
    uint8_t *u8ptr;
    uint8_t len;<commit_msg>BLE Host - HCI commands were logged incorrectly.<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "os/os.h"
#include "console/console.h"
#include "nimble/hci_common.h"
#include "nimble/hci_transport.h"
#include "host/host_hci.h"
#include "host_dbg_priv.h"
#include "ble_hs_priv.h"
#ifdef PHONY_TRANSPORT
#include "host/ble_hs_test.h"
#endif

/**
 * This buffer holds one of the following:
 * 1. The current outgoing HCI command.
 * 2. The current incoming HCI acknowledgement (command complete or command
 *    status event).
 */
uint8_t host_hci_cmd_buf[HCI_EVT_BUF_SIZE];

static int
host_hci_cmd_transport(uint8_t *cmdbuf)
{
#ifdef PHONY_TRANSPORT
    ble_hs_test_hci_txed(cmdbuf);
    return 0;
#else
    int rc;

    rc = ble_hci_transport_host_cmd_send(cmdbuf);
    switch (rc) {
    case 0:
        return 0;

    case BLE_ERR_MEM_CAPACITY:
        return BLE_HS_ENOMEM_EVT;

    default:
        return BLE_HS_EUNKNOWN;
    }
#endif
}

void
host_hci_write_hdr(uint8_t ogf, uint8_t ocf, uint8_t len, void *buf)
{
    uint16_t opcode;
    uint8_t *u8ptr;

    u8ptr = buf;

    opcode = (ogf << 10) | ocf;
    htole16(u8ptr, opcode);
    u8ptr[2] = len;
}

int
host_hci_cmd_send(uint8_t ogf, uint8_t ocf, uint8_t len, const void *cmddata)
{
    int rc;

    htole16(host_hci_cmd_buf, ogf << 10 | ocf);
    host_hci_cmd_buf[2] = len;
    if (len != 0) {
        memcpy(host_hci_cmd_buf + BLE_HCI_CMD_HDR_LEN, cmddata, len);
    }

    BLE_HS_LOG(DEBUG, "host_hci_cmd_send: ogf=0x%02x ocf=0x%02x len=%d\n",
               ogf, ocf, len);
    ble_hs_misc_log_flat_buf(host_hci_cmd_buf, len + BLE_HCI_CMD_HDR_LEN);
    BLE_HS_LOG(DEBUG, "\n");
    rc = host_hci_cmd_transport(host_hci_cmd_buf);

    if (rc == 0) {
        STATS_INC(ble_hs_stats, hci_cmd);
    } else {
        BLE_HS_LOG(DEBUG, "host_hci_cmd_send failure; rc=%d\n", rc);
    }

    return rc;
}

int
host_hci_cmd_send_buf(void *buf)
{
    uint16_t opcode;
    uint8_t *u8ptr;
    uint8_t len;
{'fraction_non_alphanumeric': 0.10026857654431513, 'fraction_numerical': 0.016830796777081467, 'mean_word_length': 3.5975308641975308, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2861447', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2100, 'n_words': 565}