2020-09-30 05:24:48 +08:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
2017-08-15 02:23:48 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* 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>
|
2017-12-08 18:52:49 +08:00
|
|
|
|
|
|
|
#include "src/shared/shell.h"
|
2018-06-22 14:32:00 +08:00
|
|
|
#include "tools/mesh/agent.h"
|
2017-08-15 02:23:48 +08:00
|
|
|
|
2019-11-09 09:33:53 +08:00
|
|
|
#define AGENT_PROMPT COLOR_BLUE "[mesh-agent]" COLOR_OFF "# "
|
|
|
|
|
2017-08-15 02:23:48 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:11:28 +08:00
|
|
|
static void reset_input_request(void)
|
|
|
|
{
|
|
|
|
pending_request.type = NONE;
|
|
|
|
pending_request.len = 0;
|
|
|
|
pending_request.cb = NULL;
|
|
|
|
pending_request.user_data = NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-02 02:57:20 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:11:28 +08:00
|
|
|
static void response_hexadecimal(const char *input, void *user_data)
|
2017-08-15 02:23:48 +08:00
|
|
|
{
|
|
|
|
uint8_t buf[MAX_HEXADECIMAL_OOB_LEN];
|
2020-03-12 06:59:40 +08:00
|
|
|
uint16_t len = pending_request.len;
|
2017-08-15 02:23:48 +08:00
|
|
|
|
|
|
|
if (!str2hex(input, strlen(input), buf, pending_request.len) ) {
|
2017-12-08 18:52:49 +08:00
|
|
|
bt_shell_printf("Incorrect input: expecting %d hex octets\n",
|
2017-08-15 02:23:48 +08:00
|
|
|
pending_request.len);
|
2020-03-12 06:59:40 +08:00
|
|
|
len = 0;
|
2017-08-15 02:23:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pending_request.cb)
|
2020-03-12 06:59:40 +08:00
|
|
|
pending_request.cb(HEXADECIMAL, buf, len,
|
2017-08-15 02:23:48 +08:00
|
|
|
pending_request.user_data);
|
2017-08-24 20:11:28 +08:00
|
|
|
|
|
|
|
reset_input_request();
|
2017-08-15 02:23:48 +08:00
|
|
|
}
|
|
|
|
|
2017-08-24 20:11:28 +08:00
|
|
|
static void response_decimal(const char *input, void *user_data)
|
2017-08-15 02:23:48 +08:00
|
|
|
{
|
|
|
|
uint8_t buf[DECIMAL_OOB_LEN];
|
2020-03-12 06:59:40 +08:00
|
|
|
uint16_t len = DECIMAL_OOB_LEN;
|
2017-08-15 02:23:48 +08:00
|
|
|
|
|
|
|
if (strlen(input) > pending_request.len)
|
2020-03-12 06:59:40 +08:00
|
|
|
len = 0;
|
2017-08-15 02:23:48 +08:00
|
|
|
|
|
|
|
bt_put_be32(atoi(input), buf);
|
|
|
|
|
|
|
|
if (pending_request.cb)
|
2020-03-12 06:59:40 +08:00
|
|
|
pending_request.cb(DECIMAL, buf, len,
|
2017-08-15 02:23:48 +08:00
|
|
|
pending_request.user_data);
|
|
|
|
|
2017-08-24 20:11:28 +08:00
|
|
|
reset_input_request();
|
2017-08-15 02:23:48 +08:00
|
|
|
}
|
|
|
|
|
2017-08-24 20:11:28 +08:00
|
|
|
static void response_ascii(const char *input, void *user_data)
|
2017-08-15 02:23:48 +08:00
|
|
|
{
|
|
|
|
if (pending_request.cb)
|
|
|
|
pending_request.cb(ASCII, (uint8_t *) input, strlen(input),
|
|
|
|
pending_request.user_data);
|
|
|
|
|
2017-08-24 20:11:28 +08:00
|
|
|
reset_input_request();
|
2017-08-15 02:23:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool request_hexadecimal(uint16_t len)
|
|
|
|
{
|
|
|
|
if (len > MAX_HEXADECIMAL_OOB_LEN)
|
|
|
|
return false;
|
|
|
|
|
2017-12-08 18:52:49 +08:00
|
|
|
bt_shell_printf("Request hexadecimal key (hex %d octets)\n", len);
|
2019-11-09 09:33:53 +08:00
|
|
|
bt_shell_prompt_input(AGENT_PROMPT, "Enter key (hex number):",
|
2019-11-02 02:57:20 +08:00
|
|
|
response_hexadecimal, NULL);
|
2017-08-15 02:23:48 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t power_ten(uint8_t power)
|
|
|
|
{
|
|
|
|
uint32_t ret = 1;
|
|
|
|
|
|
|
|
while (power--)
|
|
|
|
ret *= 10;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-09 09:33:53 +08:00
|
|
|
static bool request_decimal(const char *desc, uint16_t len)
|
2017-08-15 02:23:48 +08:00
|
|
|
{
|
2019-11-09 09:33:53 +08:00
|
|
|
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:",
|
2019-11-02 02:57:20 +08:00
|
|
|
response_decimal, NULL);
|
2017-08-15 02:23:48 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool request_ascii(uint16_t len)
|
|
|
|
{
|
2017-11-18 18:48:45 +08:00
|
|
|
if (len > MAX_ASCII_OOB_LEN)
|
2017-08-15 02:23:48 +08:00
|
|
|
return false;
|
|
|
|
|
2017-12-08 18:52:49 +08:00
|
|
|
bt_shell_printf("Request ASCII key (max characters %d)\n", len);
|
2019-11-02 02:57:20 +08:00
|
|
|
bt_shell_prompt_input("mesh", "Enter key (ascii string):",
|
|
|
|
response_ascii, NULL);
|
2017-08-15 02:23:48 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-09 09:33:53 +08:00
|
|
|
bool agent_input_request(oob_type_t type, uint16_t max_len, const char *desc,
|
|
|
|
agent_input_cb cb, void *user_data)
|
2017-08-15 02:23:48 +08:00
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
if (pending_request.type != NONE)
|
2019-11-02 02:57:20 +08:00
|
|
|
return false;
|
2017-08-15 02:23:48 +08:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case HEXADECIMAL:
|
|
|
|
result = request_hexadecimal(max_len);
|
|
|
|
break;
|
|
|
|
case DECIMAL:
|
2019-11-09 09:33:53 +08:00
|
|
|
result = request_decimal(desc, max_len);
|
2017-08-15 02:23:48 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-24 20:11:28 +08:00
|
|
|
static void response_output(const char *input, void *user_data)
|
|
|
|
{
|
|
|
|
reset_input_request();
|
|
|
|
}
|
|
|
|
|
2017-08-15 02:23:48 +08:00
|
|
|
bool agent_output_request(const char* str)
|
|
|
|
{
|
|
|
|
if (pending_request.type != NONE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pending_request.type = OUTPUT;
|
2017-12-08 18:52:49 +08:00
|
|
|
bt_shell_prompt_input("mesh", str, response_output, NULL);
|
2017-08-15 02:23:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void agent_output_request_cancel(void)
|
|
|
|
{
|
|
|
|
if (pending_request.type != OUTPUT)
|
|
|
|
return;
|
|
|
|
pending_request.type = NONE;
|
2017-12-08 18:52:49 +08:00
|
|
|
bt_shell_release_prompt("");
|
2017-08-15 02:23:48 +08:00
|
|
|
}
|