mirror of
https://github.com/emqx/neuron.git
synced 2026-10-27 04:19:50 +08:00
Add tty and schema interface implementation
This commit is contained in:
@@ -11,6 +11,8 @@ set(src
|
||||
command/node.c
|
||||
command/ping.c
|
||||
command/plugin.c
|
||||
command/tty.c
|
||||
command/schema.c
|
||||
command/command.c)
|
||||
|
||||
add_library(plugin-mqtt SHARED ${src})
|
||||
|
||||
@@ -297,8 +297,22 @@ static int write_response(mqtt_response_t *response, neu_plugin_t *plugin,
|
||||
}
|
||||
|
||||
// Manipulate TTY
|
||||
static int tty_response_get(char **output_str, neu_plugin_t *plugin,
|
||||
neu_json_mqtt_t *mqtt, char *json_str)
|
||||
{
|
||||
UNUSED(json_str);
|
||||
*output_str = command_tty_get(plugin, mqtt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Manipulate plugin schema
|
||||
static int schema_response_get(char **output_str, neu_plugin_t *plugin,
|
||||
neu_json_mqtt_t *mqtt, char *json_str)
|
||||
{
|
||||
UNUSED(json_str);
|
||||
*output_str = command_schema_get(plugin, mqtt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Manipulate node setting
|
||||
static int setting_response_set(char **output_str, neu_plugin_t *plugin,
|
||||
@@ -470,11 +484,17 @@ void command_response_handle(mqtt_response_t *response)
|
||||
|
||||
case TOPIC_TYPE_TTYS: {
|
||||
// TTY GET
|
||||
if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) {
|
||||
rc = tty_response_get(&ret_str, plugin, mqtt, json_str);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case TOPIC_TYPE_SCHEMA: {
|
||||
// Schema GET
|
||||
if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) {
|
||||
rc = schema_response_get(&ret_str, plugin, mqtt, json_str);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ extern "C" {
|
||||
#include "ping.h"
|
||||
#include "plugin.h"
|
||||
#include "read_write.h"
|
||||
#include "schema.h"
|
||||
#include "tty.h"
|
||||
|
||||
enum topic_type {
|
||||
TOPIC_TYPE_PING = 0,
|
||||
|
||||
@@ -33,6 +33,7 @@ extern "C" {
|
||||
#include "json/neu_json_plugin.h"
|
||||
#include "json/neu_json_rw.h"
|
||||
#include "json/neu_json_tag.h"
|
||||
#include "json/neu_json_tty.h"
|
||||
|
||||
#define UNUSED(x) (void) (x)
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
|
||||
#include <errno.h>
|
||||
#include <jansson.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "schema.h"
|
||||
|
||||
static char *schema_inject(char *buf, neu_json_mqtt_t *mqtt)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t * schema = json_loads(buf, 0, &error);
|
||||
|
||||
if (NULL == schema) {
|
||||
log_debug("json error, column:%d, line:%d, pos:%d, %s, %s",
|
||||
error.column, error.line, error.position, error.source,
|
||||
error.text);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
json_t *root = json_object();
|
||||
json_object_set(root, "schema", schema);
|
||||
json_object_set(root, "uuid", json_string(mqtt->uuid));
|
||||
json_object_set(root, "command", json_string(mqtt->command));
|
||||
char *ret_str = json_dumps(root, 4096);
|
||||
return ret_str;
|
||||
}
|
||||
|
||||
char *command_schema_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt)
|
||||
{
|
||||
UNUSED(plugin);
|
||||
char buf[4096] = { 0 };
|
||||
FILE *fp = fopen("./plugin_param_schema.json", "r");
|
||||
|
||||
if (fp == NULL) {
|
||||
log_info("open ./plugin_param_schema.json error: %d", errno);
|
||||
|
||||
char * result = NULL;
|
||||
neu_json_error_resp_t error = { 0 };
|
||||
error.error = NEU_ERR_FAILURE;
|
||||
neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt,
|
||||
neu_json_encode_mqtt_resp, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
fread(buf, 1, sizeof(buf), fp);
|
||||
fclose(fp);
|
||||
|
||||
char *ret_str = schema_inject(buf, mqtt);
|
||||
return ret_str;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
|
||||
#ifndef NEURON_PLUGIN_MQTT_COMMAND_SCHEMA
|
||||
#define NEURON_PLUGIN_MQTT_COMMAND_SCHEMA
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <neuron.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
char *command_schema_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "tty.h"
|
||||
|
||||
// Copy from normal_handle.c
|
||||
|
||||
#define DEV_TTY_LENTH 261
|
||||
|
||||
static int get_tty_file_list(char ***tty_file)
|
||||
{
|
||||
DIR * dir = NULL;
|
||||
struct dirent *ptr = NULL;
|
||||
int n_file = 0;
|
||||
|
||||
*tty_file = NULL;
|
||||
dir = opendir("/dev");
|
||||
if (dir == NULL) {
|
||||
log_error("Open dir error: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
ptr = readdir(dir);
|
||||
while (ptr != NULL) {
|
||||
if (ptr->d_type == 2) {
|
||||
char dev_tty[DEV_TTY_LENTH] = { 0 };
|
||||
if (strstr(ptr->d_name, "tty") != NULL) {
|
||||
n_file += 1;
|
||||
*tty_file = realloc(*tty_file, sizeof(char *) * n_file);
|
||||
snprintf(dev_tty, sizeof(dev_tty), "/dev/%s", ptr->d_name);
|
||||
(*tty_file)[n_file - 1] = strdup(dev_tty);
|
||||
}
|
||||
}
|
||||
ptr = readdir(dir);
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
return n_file;
|
||||
}
|
||||
|
||||
char *command_tty_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt)
|
||||
{
|
||||
log_info("Get tty uuid:%s", mqtt->uuid);
|
||||
|
||||
UNUSED(plugin);
|
||||
neu_json_get_tty_resp_t ttys_res = { 0 };
|
||||
char * result = NULL;
|
||||
ttys_res.n_tty = get_tty_file_list(&ttys_res.ttys);
|
||||
if (ttys_res.n_tty == -1) {
|
||||
neu_json_error_resp_t error = { 0 };
|
||||
error.error = NEU_ERR_FAILURE;
|
||||
neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt,
|
||||
neu_json_encode_mqtt_resp, &result);
|
||||
} else {
|
||||
neu_json_encode_with_mqtt(&ttys_res, neu_json_encode_get_tty_resp, mqtt,
|
||||
neu_json_encode_mqtt_resp, &result);
|
||||
|
||||
for (int i = 0; i < ttys_res.n_tty; i++) {
|
||||
free(ttys_res.ttys[i]);
|
||||
}
|
||||
|
||||
free(result);
|
||||
free(ttys_res.ttys);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
|
||||
#ifndef NEURON_PLUGIN_MQTT_COMMAND_TTY
|
||||
#define NEURON_PLUGIN_MQTT_COMMAND_TTY
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <neuron.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
char *command_tty_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user