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
*************************************************************************************************/
/*************************************************************************************************
* API's
*************************************************************************************************/
void commands_init(void)
{
gos_result_t result;
coap_settings_t* settings;
{
UNUSED_PARAMETER(result);
}
}
/*************************************************************************************************
* 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);
return GOS_CMD_EXECUTE_AOK;
}
GOS_DEFINE_COMMAND(coap, put)
{
if(strlen(argv[0]) >= MAX_PATH_LENGTH)
{
GOS_LOG("Failed (maximum path length is %u)", MAX_PATH_LENGTH-1);
return GOS_CMD_BAD_ARGS;
}
else if(strlen(argv[1]) >= MAX_PAYLOAD_LENGTH)
{
GOS_LOG("Failed (maximum payload is %u)", MAX_PAYLOAD_LENGTH-1);
return GOS_CMD_BAD_ARGS;
}
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);
return GOS_CMD_EXECUTE_AOK;
}
GOS_DEFINE_COMMAND(coap, post)
{
if(strlen(argv[0]) >= MAX_PATH_LENGTH)
{
GOS_LOG("Failed (maximum path length is %u)", MAX_PATH_LENGTH-1);
return GOS_CMD_BAD_ARGS;
}
else if(strlen(argv[1]) >= MAX_PAYLOAD_LENGTH)
{
GOS_LOG("Failed (maximum string length is %u)", MAX_PAYLOAD_LENGTH-1);
return GOS_CMD_BAD_ARGS;
}
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);
return GOS_CMD_EXECUTE_AOK;
}
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);
return GOS_CMD_BAD_ARGS;
}
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);
return GOS_CMD_EXECUTE_AOK;
}
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);
return GOS_CMD_BAD_ARGS;
}
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);
return GOS_CMD_EXECUTE_AOK;
}
GOS_DEFINE_COMMAND(coap, cancel)
{
if (observing == false) { //nothing to cancel
return GOS_CMD_EXECUTE_AOK;
}
gos_event_issue(coap_app_cancel, NULL, 0);
return GOS_CMD_EXECUTE_AOK;
}
/*************************************************************************************************
* Getters
*************************************************************************************************/
{
coap_settings_t* settings;
GOS_NVM_GET(COAP, DEMO, settings);
return GOS_CMD_SUCCESS;
}
/*************************************************************************************************/
{
coap_settings_t* settings;
GOS_NVM_GET(COAP, DEMO, settings);
return GOS_CMD_SUCCESS;
}
/*************************************************************************************************/
{
coap_settings_t* settings;
GOS_NVM_GET(COAP, DEMO, settings);
return GOS_CMD_SUCCESS;
}
/*************************************************************************************************
* Setters
*************************************************************************************************/
{
coap_settings_t* settings;
if(strlen(argv[1]) > sizeof(settings->host))
{
return GOS_CMD_BAD_ARGS;
}
else
{
GOS_NVM_GET(COAP, DEMO, settings);
strcpy((char*)settings->host, argv[1]);
}
}
/*************************************************************************************************/
{
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;
return GOS_CMD_SUCCESS;
}
/*************************************************************************************************/
{
coap_settings_t* settings;
if(strlen(argv[1]) > sizeof(settings->path))
{
return GOS_CMD_BAD_ARGS;
}
else
{
GOS_NVM_GET(COAP, DEMO, settings);
strcpy((char*)settings->path, argv[1]);
return GOS_CMD_SET_OK;
}
}