add: relay endpoint
fix: discovery invalid read
This commit is contained in:
parent
1171ef22be
commit
a127a68e31
5 changed files with 61 additions and 2 deletions
|
@ -197,6 +197,12 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
|
|||
uint16_t discovered_command_port = mpack_node_u16(mpack_node_map_uint(root, DISCOVERY_MAPPING_COMMAND_PORT));
|
||||
uint8_t discovered_relay_count = mpack_node_u8(mpack_node_map_uint(root, DISCOVERY_MAPPING_RELAY_COUNT));
|
||||
const char *discovered_name = mpack_node_str(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
|
||||
size_t discovered_name_len = mpack_node_strlen(mpack_node_map_uint(root, DISCOVERY_MAPPING_NAME));
|
||||
|
||||
if(discovered_name_len > MAX_NAME_LENGTH)
|
||||
{
|
||||
discovered_name_len = MAX_NAME_LENGTH;
|
||||
}
|
||||
|
||||
bool found_discovered_in_list = 0;
|
||||
|
||||
|
@ -207,7 +213,7 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
|
|||
if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0)
|
||||
{
|
||||
known_controllers[i]->active = 1;
|
||||
strncpy(known_controllers[i]->name, discovered_name, MAX_NAME_LENGTH);
|
||||
strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
|
||||
known_controllers[i]->name[MAX_NAME_LENGTH] = '\0';
|
||||
known_controllers[i]->port = discovered_command_port;
|
||||
known_controllers[i]->relay_count = discovered_relay_count;
|
||||
|
|
40
endpoints/api_v1_relays.c
Normal file
40
endpoints/api_v1_relays.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <cJSON.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_relays.h>
|
||||
#include <logger.h>
|
||||
#include <models/junction_tag.h>
|
||||
#include <models/relay.h>
|
||||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
relay_t** all_relays = relay_get_all();
|
||||
|
||||
cJSON *json = cJSON_CreateArray();
|
||||
|
||||
for(int i = 0; all_relays[i] != NULL; ++i)
|
||||
{
|
||||
cJSON *json_relay = relay_to_json(all_relays[i]);
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
}
|
||||
|
||||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
relay_free_list(all_relays);
|
||||
}
|
9
include/endpoints/api_v1_relays.h
Normal file
9
include/endpoints/api_v1_relays.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef CORE_ENDPOINTS_API_V1_RELAYS_H
|
||||
#define CORE_ENDPOINTS_API_V1_RELAYS_H
|
||||
|
||||
#include <router.h>
|
||||
|
||||
void
|
||||
api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
|
||||
|
||||
#endif /* CORE_ENDPOINTS_API_V1_RELAYS_H */
|
|
@ -4,6 +4,7 @@
|
|||
#include <uuid/uuid.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <constants.h>
|
||||
#include <cJSON.h>
|
||||
#include <helpers.h>
|
||||
#include <models/relay.h>
|
||||
|
@ -12,7 +13,7 @@ typedef struct
|
|||
{
|
||||
int id;
|
||||
uuid_t uid;
|
||||
char name[128];
|
||||
char name[MAX_NAME_LENGTH + 1];
|
||||
char ip[17];
|
||||
int active;
|
||||
int port;
|
||||
|
|
3
router.c
3
router.c
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <endpoints/api_v1_controllers.h>
|
||||
#include <endpoints/api_v1_relays.h>
|
||||
|
||||
static const int HTTP_METHOD_GET = (1 << 0);
|
||||
static const int HTTP_METHOD_POST = (1 << 1);
|
||||
|
@ -61,6 +62,8 @@ router_init()
|
|||
|
||||
router_register_endpoint("/api/v1/controllers/discover/", HTTP_METHOD_POST, api_v1_controllers_discover_POST);
|
||||
router_register_endpoint("/api/v1/controllers/", HTTP_METHOD_GET, api_v1_controllers_GET);
|
||||
|
||||
router_register_endpoint("/api/v1/relays/", HTTP_METHOD_GET, api_v1_relays_GET);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Reference in a new issue