bluez/tools/mesh/agent.c
Tedd Ho-Jeong An 0a259dd05b tools: Add SPDX License Identifier
This patch adds SPDX License Identifier and removes the license text.

-------------------------------------
       License            COUNT
-------------------------------------
 GPL-2.0-or-later     :     97
 LGPL-2.1-or-later    :     38
 GPL-2.0-only         :      2

License: GPL-2.0-or-later
   tools/l2cap-tester.c
   tools/hcisecfilter.c
   tools/ciptool.c
   tools/btsnoop.c
   tools/check-selftest.c
   tools/btpclientctl.c
   tools/hci-tester.c
   tools/hcitool.c
   tools/btiotest.c
   tools/oobtest.c
   tools/btinfo.c
   tools/hwdb.c
   tools/hciattach_bcm43xx.c
   tools/mgmt-tester.c
   tools/hex2hcd.c
   tools/hciattach_st.c
   tools/smp-tester.c
   tools/bluetooth-player.c
   tools/hciattach_tialt.c
   tools/gap-tester.c
   tools/bluemoon.c
   tools/bneptest.c
   tools/gatt-service.c
   tools/rctest.c
   tools/rfcomm-tester.c
   tools/hcieventmask.c
   tools/hciattach_ti.c
   tools/seq2bseq.c
   tools/scotest.c
   tools/bcmfw.c
   tools/hciconfig.c
   tools/btattach.c
   tools/l2ping.c
   tools/obexctl.c
   tools/l2test.c
   tools/hciattach_intel.c
   tools/hciattach.h
   tools/create-image.c
   tools/bnep-tester.c
   tools/userchan-tester.c
   tools/rfcomm.c
   tools/btmon-logger.c
   tools/hcidump.c
   tools/rtlfw.c
   tools/hciattach_qualcomm.c
   tools/btproxy.c
   tools/nokfw.c
   tools/hciattach_ath3k.c
   tools/3dsp.c
   tools/bdaddr.c
   tools/sco-tester.c
   tools/hciattach.c
   tools/amptest.c
   tools/btgatt-server.c
   tools/btgatt-client.c
   tools/cltest.c
   tools/ibeacon.c
   tools/mcaptest.c
   tools/hid2hci.c
   tools/btmgmt.c
   tools/advtest.c
   tools/eddystone.c
   tools/avtest.c
   tools/mpris-proxy.c
   tools/avinfo.c
   tools/sdptool.c
   tools/btconfig.c
   tools/update_compids.sh
   tools/parser/parser.h
   tools/parser/obex.c
   tools/parser/amp.c
   tools/parser/sdp.c
   tools/parser/tcpip.c
   tools/parser/sap.c
   tools/parser/cmtp.c
   tools/parser/avctp.c
   tools/parser/lmp.c
   tools/parser/ppp.c
   tools/parser/rfcomm.h
   tools/parser/hci.c
   tools/parser/sdp.h
   tools/parser/parser.c
   tools/parser/rfcomm.c
   tools/parser/avdtp.c
   tools/parser/avrcp.c
   tools/parser/ericsson.c
   tools/parser/hcrp.c
   tools/parser/bpa.c
   tools/parser/hidp.c
   tools/parser/bnep.c
   tools/parser/capi.c
   tools/parser/att.c
   tools/parser/l2cap.c
   tools/parser/smp.c
   tools/parser/csr.c
   tools/parser/l2cap.h
   tools/parse_companies.pl

License: LGPL-2.1-or-later
   tools/test-runner.c
   tools/btpclient.c
   tools/meshctl.c
   tools/mesh-cfgclient.c
   tools/mesh/model.h
   tools/mesh/util.h
   tools/mesh/config-model.h
   tools/mesh/cfgcli.h
   tools/mesh/mesh-db.c
   tools/mesh/mesh-db.h
   tools/mesh/keys.c
   tools/mesh/util.c
   tools/mesh/agent.h
   tools/mesh/remote.c
   tools/mesh/keys.h
   tools/mesh/agent.c
   tools/mesh/cfgcli.c
   tools/mesh/remote.h
   tools/mesh-gatt/prov.c
   tools/mesh-gatt/util.h
   tools/mesh-gatt/prov.h
   tools/mesh-gatt/net.c
   tools/mesh-gatt/util.c
   tools/mesh-gatt/prov-db.h
   tools/mesh-gatt/crypto.c
   tools/mesh-gatt/crypto.h
   tools/mesh-gatt/gatt.c
   tools/mesh-gatt/config-server.c
   tools/mesh-gatt/keys.h
   tools/mesh-gatt/onoff-model.c
   tools/mesh-gatt/net.h
   tools/mesh-gatt/gatt.h
   tools/mesh-gatt/node.c
   tools/mesh-gatt/config-client.c
   tools/mesh-gatt/mesh-net.h
   tools/mesh-gatt/node.h
   tools/mesh-gatt/onoff-model.h
   tools/mesh-gatt/prov-db.c

License: GPL-2.0-only
   tools/obex-server-tool.c
   tools/obex-client-tool.c
2020-09-29 14:37:23 -07:00

218 lines
4.2 KiB
C

// SPDX-License-Identifier: LGPL-2.1-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2017 Intel Corporation. All rights reserved.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <lib/bluetooth.h>
#include "src/shared/shell.h"
#include "tools/mesh/agent.h"
#define AGENT_PROMPT COLOR_BLUE "[mesh-agent]" COLOR_OFF "# "
struct input_request {
oob_type_t type;
uint16_t len;
agent_input_cb cb;
void *user_data;
};
static struct input_request pending_request = {NONE, 0, NULL, NULL};
bool agent_completion(void)
{
if (pending_request.type == NONE)
return false;
return true;
}
static void reset_input_request(void)
{
pending_request.type = NONE;
pending_request.len = 0;
pending_request.cb = NULL;
pending_request.user_data = NULL;
}
static bool str2hex(const char *str, uint16_t in_len, uint8_t *out,
uint16_t out_len)
{
uint16_t i;
if (in_len < out_len * 2)
return false;
for (i = 0; i < out_len; i++) {
if (sscanf(&str[i * 2], "%02hhx", &out[i]) != 1)
return false;
}
return true;
}
static void response_hexadecimal(const char *input, void *user_data)
{
uint8_t buf[MAX_HEXADECIMAL_OOB_LEN];
uint16_t len = pending_request.len;
if (!str2hex(input, strlen(input), buf, pending_request.len) ) {
bt_shell_printf("Incorrect input: expecting %d hex octets\n",
pending_request.len);
len = 0;
}
if (pending_request.cb)
pending_request.cb(HEXADECIMAL, buf, len,
pending_request.user_data);
reset_input_request();
}
static void response_decimal(const char *input, void *user_data)
{
uint8_t buf[DECIMAL_OOB_LEN];
uint16_t len = DECIMAL_OOB_LEN;
if (strlen(input) > pending_request.len)
len = 0;
bt_put_be32(atoi(input), buf);
if (pending_request.cb)
pending_request.cb(DECIMAL, buf, len,
pending_request.user_data);
reset_input_request();
}
static void response_ascii(const char *input, void *user_data)
{
if (pending_request.cb)
pending_request.cb(ASCII, (uint8_t *) input, strlen(input),
pending_request.user_data);
reset_input_request();
}
static bool request_hexadecimal(uint16_t len)
{
if (len > MAX_HEXADECIMAL_OOB_LEN)
return false;
bt_shell_printf("Request hexadecimal key (hex %d octets)\n", len);
bt_shell_prompt_input(AGENT_PROMPT, "Enter key (hex number):",
response_hexadecimal, NULL);
return true;
}
static uint32_t power_ten(uint8_t power)
{
uint32_t ret = 1;
while (power--)
ret *= 10;
return ret;
}
static bool request_decimal(const char *desc, uint16_t len)
{
if (!desc)
bt_shell_printf("Request decimal key (0 - %d)\n",
power_ten(len) - 1);
else
bt_shell_printf("%s (0 - %d)\n", desc, power_ten(len) - 1);
bt_shell_prompt_input(AGENT_PROMPT, "Enter decimal number:",
response_decimal, NULL);
return true;
}
static bool request_ascii(uint16_t len)
{
if (len > MAX_ASCII_OOB_LEN)
return false;
bt_shell_printf("Request ASCII key (max characters %d)\n", len);
bt_shell_prompt_input("mesh", "Enter key (ascii string):",
response_ascii, NULL);
return true;
}
bool agent_input_request(oob_type_t type, uint16_t max_len, const char *desc,
agent_input_cb cb, void *user_data)
{
bool result;
if (pending_request.type != NONE)
return false;
switch (type) {
case HEXADECIMAL:
result = request_hexadecimal(max_len);
break;
case DECIMAL:
result = request_decimal(desc, max_len);
break;
case ASCII:
result = request_ascii(max_len);
break;
case NONE:
case OUTPUT:
default:
return false;
};
if (result) {
pending_request.type = type;
pending_request.len = max_len;
pending_request.cb = cb;
pending_request.user_data = user_data;
return true;
}
return false;
}
static void response_output(const char *input, void *user_data)
{
reset_input_request();
}
bool agent_output_request(const char* str)
{
if (pending_request.type != NONE)
return false;
pending_request.type = OUTPUT;
bt_shell_prompt_input("mesh", str, response_output, NULL);
return true;
}
void agent_output_request_cancel(void)
{
if (pending_request.type != OUTPUT)
return;
pending_request.type = NONE;
bt_shell_release_prompt("");
}