cloud/coap_demo/commands.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include "gos.h"
#include "common.h"
#include "nvm_settings.h"
/*************************************************************************************************
* Getters and Setters List
*************************************************************************************************/
GOS_CMD_CREATE_GETTER_SETTER(coap, host, "coap.host", SC3('c', 'o', 'h', 'o'), 1, 1);
GOS_CMD_CREATE_GETTER_SETTER(coap, port, "coap.port", SC3('c', 'o', 'p', 'o'), 1, 1);
GOS_CMD_CREATE_GETTER_SETTER(coap, path, "coap.path", SC3('c', 'o', 'p', 'a'), 1, 1);
GOS_CMD_CREATE_COMMAND(coap, get, "coap_get", "get", 0, 1, true);
GOS_CMD_CREATE_COMMAND(coap, discover, "coap_discover", "discover", 0, 1, true);
GOS_CMD_CREATE_COMMAND(coap, put, "coap_put", "put", 0, 1, true);
GOS_CMD_CREATE_COMMAND(coap, post, "coap_post", "post", 0, 1, true);
GOS_CMD_CREATE_COMMAND(coap, delete, "coap_delete", "delete", 0, 1, true);
GOS_CMD_CREATE_COMMAND(coap, observe, "coap_observe", "observe", 0, 1, true);
GOS_CMD_CREATE_COMMAND(coap, cancel, "coap_cancel", "cancel", 0, 1, true);
/*************************************************************************************************
* API's
*************************************************************************************************/
void commands_init(void)
{
gos_result_t result;
coap_settings_t* settings;
if (GOS_FAILED(result, GOS_NVM_GET(COAP, DEMO, settings)))
{
GOS_LOG("Failed to loaded default settings");
}
}
/*************************************************************************************************
* Commands
*************************************************************************************************/
{
if(argv[0] == NULL)
{
custom_path[0] = 0;
}
else if(strlen(argv[0]) >= MAX_PATH_LENGTH)
{
GOS_LOG("Failed (maximum path length is %u)", MAX_PATH_LENGTH-1);
}
else
{
// ToDo: can not pass argv as argument at the moment, as argv is deallocated
// before event fires. Event will be replaced with direct function call once
// zx_ APIs replaced with gos_ (as zx_ can not be called from custom commands)
strncpy(custom_path, argv[0], MAX_PATH_LENGTH-1);
custom_path[MAX_PATH_LENGTH-1] = 0;
}
gos_event_issue(coap_app_get, NULL, 0);
}
GOS_DEFINE_COMMAND(coap, discover)
{
gos_event_issue(coap_app_discover, NULL, 0);
}
{
if(strlen(argv[0]) >= MAX_PATH_LENGTH)
{
GOS_LOG("Failed (maximum path length is %u)", MAX_PATH_LENGTH-1);
}
else if(strlen(argv[1]) >= MAX_PAYLOAD_LENGTH)
{
GOS_LOG("Failed (maximum payload is %u)", MAX_PAYLOAD_LENGTH-1);
}
else
{
// ToDo: can not pass argv as argument at the moment, as argv is deallocated
// before event fires. Event will be replaced with direct function call once
// zx_ APIs replaced with gos_ (as zx_ can not be called from custom commands)
strncpy(custom_path, argv[0], MAX_PATH_LENGTH-1);
custom_path[MAX_PATH_LENGTH-1] = 0;
strncpy(payload, argv[1], MAX_PAYLOAD_LENGTH-1);
payload[MAX_PAYLOAD_LENGTH-1] = 0;
}
gos_event_issue(coap_app_put, NULL, 0);
}
GOS_DEFINE_COMMAND(coap, post)
{
if(strlen(argv[0]) >= MAX_PATH_LENGTH)
{
GOS_LOG("Failed (maximum path length is %u)", MAX_PATH_LENGTH-1);
}
else if(strlen(argv[1]) >= MAX_PAYLOAD_LENGTH)
{
GOS_LOG("Failed (maximum string length is %u)", MAX_PAYLOAD_LENGTH-1);
}
else
{
// ToDo: can not pass argv as argument at the moment, as argv is deallocated
// before event fires. Event will be replaced with direct function call once
// zx_ APIs replaced with gos_ (as zx_ can not be called from custom commands)
strncpy(custom_path, argv[0], MAX_PATH_LENGTH-1);
custom_path[MAX_PATH_LENGTH-1] = 0;
strncpy(payload, argv[1], MAX_PAYLOAD_LENGTH-1);
payload[MAX_PAYLOAD_LENGTH-1] = 0;
}
gos_event_issue(coap_app_post, NULL, 0);
}
GOS_DEFINE_COMMAND(coap, delete)
{
if(argv[0] == NULL)
{
custom_path[0] = 0;
}
else if(strlen(argv[0]) >= MAX_PATH_LENGTH)
{
GOS_LOG("Failed (maximum path length is %u)", MAX_PATH_LENGTH-1);
}
else
{
// ToDo: can not pass argv as argument at the moment, as argv is deallocated
// before event fires. Event will be replaced with direct function call once
// zx_ APIs replaced with gos_ (as zx_ can not be called from custom commands)
strncpy(custom_path, argv[0], MAX_PATH_LENGTH-1);
custom_path[MAX_PATH_LENGTH-1] = 0;
}
gos_event_issue(coap_app_delete, NULL, 0);
}
GOS_DEFINE_COMMAND(coap, observe)
{
if (observing == true) {
GOS_LOG("Already observing a resource, execute coap_cancel first");
}
if(argv[0] == NULL)
{
custom_path[0] = 0;
}
else if(strlen(argv[0]) >= MAX_PATH_LENGTH)
{
GOS_LOG("Failed (maximum path length is %u)", MAX_PATH_LENGTH-1);
}
else
{
// ToDo: can not pass argv as argument at the moment, as argv is deallocated
// before event fires. Event will be replaced with direct function call once
// zx_ APIs replaced with gos_ (as zx_ can not be called from custom commands)
strncpy(custom_path, argv[0], MAX_PATH_LENGTH-1);
custom_path[MAX_PATH_LENGTH-1] = 0;
}
gos_event_issue(coap_app_observe, NULL, 0);
}
GOS_DEFINE_COMMAND(coap, cancel)
{
if (observing == false) { //nothing to cancel
}
gos_event_issue(coap_app_cancel, NULL, 0);
}
/*************************************************************************************************
* Getters
*************************************************************************************************/
gos_cmd_result_t coap_get_host_command(int argc, char ** argv)
{
coap_settings_t* settings;
GOS_NVM_GET(COAP, DEMO, settings);
gos_cmd_format_response(GOS_CMD_SUCCESS, "%s", settings->host);
}
/*************************************************************************************************/
gos_cmd_result_t coap_get_port_command(int argc, char ** argv)
{
coap_settings_t* settings;
GOS_NVM_GET(COAP, DEMO, settings);
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", settings->port);
}
/*************************************************************************************************/
gos_cmd_result_t coap_get_path_command(int argc, char ** argv)
{
coap_settings_t* settings;
GOS_NVM_GET(COAP, DEMO, settings);
gos_cmd_format_response(GOS_CMD_SUCCESS, "%s", settings->path);
}
/*************************************************************************************************
* Setters
*************************************************************************************************/
gos_cmd_result_t coap_set_host_command(int argc, char ** argv)
{
coap_settings_t* settings;
if(strlen(argv[1]) > sizeof(settings->host))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(settings->host));
}
else
{
GOS_NVM_GET(COAP, DEMO, settings);
strcpy((char*)settings->host, argv[1]);
}
}
/*************************************************************************************************/
gos_cmd_result_t coap_set_port_command(int argc, char ** argv)
{
coap_settings_t* settings;
GOS_NVM_GET(COAP, DEMO, settings);
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, settings->port, argv[1], 0, 65535);
coap_uri.port = settings->port;
}
/*************************************************************************************************/
gos_cmd_result_t coap_set_path_command(int argc, char ** argv)
{
coap_settings_t* settings;
if(strlen(argv[1]) > sizeof(settings->path))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(settings->path));
}
else
{
GOS_NVM_GET(COAP, DEMO, settings);
strcpy((char*)settings->path, argv[1]);
}
}