Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more flexibility into configuration. #11

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 75 additions & 67 deletions src/relay_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <libconfig.h>
#include "libconfig.h"
#include "awa/static.h"
#include "log.h"

Expand Down Expand Up @@ -73,16 +73,17 @@

//! @endcond

static AwaResult RelayStateResourceHandler(client,
operation,
objectID,
objectInstanceID,
resourceID,
resourceInstanceID,
dataPointer,
dataSize,
changed);
static AwaResult RelayStateResourceHandler(AwaStaticClient * client,
AwaOperation operation,
AwaObjectID objectID,
AwaObjectInstanceID objectInstanceID,
AwaResourceID resourceID,
AwaResourceInstanceID resourceInstanceID,
void ** dataPointer,
size_t * dataSize,
bool * changed);

void ChangeRelayState(bool state);
/***************************************************************************************************
* Typedef
**************************************************************************************************/
Expand All @@ -97,8 +98,8 @@ typedef struct
AwaResourceInstanceID instanceID; /**< resource instance ID */
AwaResourceType type; /**< type of resource e.g. bool, string, integer etc. */
const char *name; /**< resource name */
bool isMandatory; /**< whethe this is mandatory resource or not. */
AwaResourceOperations operation; /**< Operation types that can be performed on that reosurce. */
bool isMandatory; /**< is it mandatory resource or not. */
AwaResourceOperations operation; /**< Operation types that can be performed on that resource. */
AwaStaticClientHandler handler;
/*@}*/
} Resource;
Expand Down Expand Up @@ -133,11 +134,13 @@ static volatile int g_keepRunning = 1;
/** Keeps current relay state. */
bool g_relayState = false;
/** Keeps certificate */
char *g_cert = NULL;
/** Keeps bootstrap server url */
uint8_t *g_cert = NULL;
/** Keeps bootstrap server URL */
const char *g_bootstrapServerUrl = NULL;
/** Keeps path to certificate file */
char *g_certFilePath = NULL;
/** security mode for connection **/
AwaSecurityMode g_securityMode = AwaSecurityMode_NoSec;

config_t cfg;

Expand Down Expand Up @@ -169,9 +172,9 @@ static Object objects[] =

/**
* Reads current value of specified digital output and stores it on value.
* @param *value stores gpio value
* @param value stores GPIO value
* @param pin number to read
* Returns 0 upon succesfull read, -1 otherwise.
* Returns 0 upon successful read, -1 otherwise.
*/
static int ReadGPIO(bool * value, int pin)
{
Expand Down Expand Up @@ -237,12 +240,14 @@ static AwaResult RelayStateResourceHandler(AwaStaticClient * client,
*changed = true;
return AwaResult_SuccessChanged;

default:
return AwaResult_InternalError;
}
}

/**
* @brief Prints relay_gateway_appd usage.
* @param *program holds application name.
* @param program holds application name.
*/
static void PrintUsage(const char *program)
{
Expand All @@ -251,18 +256,18 @@ static void PrintUsage(const char *program)
" -v : Debug level from 1 to 5\n"
" fatal(1), error(2), warning(3), info(4), debug(5) and max(>5)\n"
" default is info.\n"
" -c : path to configuration"
" -h : Print help and exit.\n\n",
program);
}

/**
* @bried Reds certificate from given file path
* @param *filePath path to file containing certificate
* @param **certificate will hold read certificate
* @return true on successfull read, false otherwise
* @brief Reds certificate from given file path
* @param filePath path to file containing certificate
* @param certificate will hold read certificate
* @return true on successful read, false otherwise
*/
bool ReadCertificate(const char *filePath, char **certificate) {
char *fileContents;
bool ReadCertificate(const char *filePath, uint8_t **certificate) {
size_t inputFileSize;
FILE *inputFile = fopen(filePath, "rb");
if (inputFile == NULL)
Expand All @@ -281,20 +286,17 @@ bool ReadCertificate(const char *filePath, char **certificate) {
fread(*certificate, sizeof(char), inputFileSize, inputFile);
if (fclose(inputFile) == EOF)
{
LOG(LOG_WARN, "Couldn't close cerificate file.");
LOG(LOG_WARN, "Couldn't close certificate file.");
}
return true;
}


/**
* @brief Reads config file and save properties into global variables
* @param *filePath path to config file
* @return true on succesfull readm false otherwise
* @param filePath path to config file
* @return true on successful read false otherwise
*/
bool ReadConfigFile(const char *filePath) {


config_init(&cfg);
if(! config_read_file(&cfg, filePath))
{
Expand All @@ -306,10 +308,18 @@ bool ReadConfigFile(const char *filePath) {
LOG(LOG_ERR, "Config file does not contain BOOTSTRAP_URL property.");
return false;
}
if(!config_lookup_string(&cfg, "CERT_FILE_PATH", &g_certFilePath))
else
{
LOG(LOG_ERR, "Config file does not contain CERT_FILE_PATH property.");
return false;
LOG(LOG_INFO, "Boostrap: %s", g_bootstrapServerUrl);
}
if(!config_lookup_string(&cfg, "CERT_FILE_PATH", (const char **)&g_certFilePath))
{
LOG(LOG_WARN, "Config file does not contain CERT_FILE_PATH property, client will run in non secure mode.");
g_certFilePath = NULL;
}
else
{
g_securityMode = AwaSecurityMode_Certificate;
}

return true;
Expand All @@ -322,17 +332,16 @@ bool ReadConfigFile(const char *filePath) {
static int ParseCommandArgs(int argc, char *argv[], const char **fptr)
{
int opt, tmp;
bool isConfigFileSpecified = false;
opterr = 0;
char *configFilePath = NULL;

while (1)
{
opt = getopt(argc, argv, "l:v:c:");
if (opt == -1)
{
opt = getopt(argc, argv, "l:v:c:h");
if (opt == -1)
{
break;
}
}
switch (opt)
{
case 'l':
Expand All @@ -358,7 +367,7 @@ static int ParseCommandArgs(int argc, char *argv[], const char **fptr)
return 0;

case 'c':
configFilePath = malloc(strlen(optarg));
configFilePath = malloc(strlen(optarg) + 1);
sprintf(configFilePath, "%s", optarg);
break;

Expand All @@ -368,7 +377,7 @@ static int ParseCommandArgs(int argc, char *argv[], const char **fptr)
}
}
if (configFilePath == NULL) {
configFilePath = malloc(strlen(DEFAULT_PATH_CONFIG_FILE));
configFilePath = malloc(strlen(DEFAULT_PATH_CONFIG_FILE) + 1);
sprintf(configFilePath, "%s", DEFAULT_PATH_CONFIG_FILE);
}

Expand All @@ -386,13 +395,13 @@ static int ParseCommandArgs(int argc, char *argv[], const char **fptr)

/**
* @brief Handles Ctrl+C signal. Helps exit app gracefully.
* @param signal id of signal to be handled
*/
static void CtrlCHandler(int signal) {
LOG(LOG_INFO, "Exit triggered...");
g_keepRunning = 0;
}


/**
* @brief Turn on or off relay on click board depending on specified state.
* @param state to be set on relay
Expand All @@ -408,10 +417,9 @@ void ChangeRelayState(bool state)
LOG(LOG_INFO, "Changed relay state on Ci40 board to %d", state);
}


/**
* @brief Add all resource definitions that belong to object.
* @param *object whose resources are to be defined.
* @param object whose resources are to be defined.
* @return pointer to flow object definition.
*/
static bool AddResourceDefinitions(AwaStaticClient *client, Object *object)
Expand Down Expand Up @@ -452,8 +460,8 @@ static bool AddResourceDefinitions(AwaStaticClient *client, Object *object)


/**
* @brief Define all objects and its resources with client deamon.
* @param *session holds client session.
* @brief Define all objects and its resources with client daemon.
* @param client instance of client used for defining objects.
* @return true if all objects are successfully defined, false otherwise.
*/
static bool DefineClientObjects(AwaStaticClient *client)
Expand All @@ -463,7 +471,7 @@ static bool DefineClientObjects(AwaStaticClient *client)

if (client == NULL)
{
LOG(LOG_ERR, "Null parameter passsed to %s()", __func__);
LOG(LOG_ERR, "Null parameter passed to %s()", __func__);
return false;
}

Expand All @@ -483,15 +491,15 @@ static bool DefineClientObjects(AwaStaticClient *client)

/**
* @brief Create object instances on client daemon.
* @param *session holds client session.
* @return true if objects are successfully defined on client, false otherwise.
* @param client instance of client used for object creation.
* @return true if objects are successfully created on client, false otherwise.
*/
static bool CreateObjectInstances(AwaStaticClient *client)
{
bool success = true;
if (client == NULL)
{
LOG(LOG_ERR, "Null parameter passsed to %s()", __func__);
LOG(LOG_ERR, "Null parameter passed to %s()", __func__);
return false;
}

Expand All @@ -511,16 +519,13 @@ static bool CreateObjectInstances(AwaStaticClient *client)
return success;
}



/*
* This function sets resource operation handlers.
* @param *session holds client session
* @param client instance of client used to define operation handlers.
* @return true when all handlers are set properly, false otherwise.
*/
static bool SetResourceOperationHandlers(AwaStaticClient *client)
{
char path[URL_PATH_SIZE] = {0};
int i,j;
bool success = true;

Expand All @@ -547,7 +552,7 @@ static bool SetResourceOperationHandlers(AwaStaticClient *client)
return success;
}

static AwaStaticClient *PrepareStaticCLient()
static AwaStaticClient *PrepareStaticClient()
{
AwaStaticClient * awaClient = AwaStaticClient_New();

Expand All @@ -562,20 +567,20 @@ static AwaStaticClient *PrepareStaticCLient()
AwaStaticClient_SetCoAPListenAddressPort(awaClient, "0.0.0.0", CLIENT_COAP_PORT);
AwaStaticClient_SetBootstrapServerURI(awaClient, g_bootstrapServerUrl);
AwaStaticClient_Init(awaClient);
AwaStaticClient_SetCertificate(awaClient, g_cert, strlen(g_cert), AwaSecurityMode_Certificate);

if (g_securityMode == AwaSecurityMode_Certificate) {
AwaStaticClient_SetCertificate(awaClient, g_cert, strlen((const char *)g_cert), AwaCertificateFormat_PEM);
}
return awaClient;
}


/**
* @brief Relay gateway application observes the IPSO resource for relay
* on client daemon and changes relay state when notification is
* received.
*/
int main(int argc, char **argv)
{
int i=0, ret;
int ret;
FILE *configFile;
const char *fptr = NULL;

Expand All @@ -601,9 +606,7 @@ int main(int argc, char **argv)
}

signal(SIGINT, CtrlCHandler);

LOG(LOG_INFO, "Relay Gateway Application ...");

LOG(LOG_INFO, "------------------------\n");

int tmp = 0;
Expand All @@ -615,21 +618,27 @@ int main(int argc, char **argv)
g_keepRunning = false;
}

LOG(LOG_INFO, "Looking for certificate file under : %s", g_certFilePath);
while (!ReadCertificate(g_certFilePath, &g_cert))
if (g_securityMode == AwaSecurityMode_Certificate)
{
sleep(2);
if (g_keepRunning == false)
LOG(LOG_INFO, "Looking for certificate file under : %s", g_certFilePath);
while (!ReadCertificate(g_certFilePath, &g_cert))
{
break;
sleep(2);
if (g_keepRunning == false)
{
break;
}
}
}

AwaStaticClient * staticClient = NULL;
if (g_keepRunning)
{
LOG(LOG_INFO, "Certificate found. ");
staticClient = PrepareStaticCLient();
if (g_securityMode == AwaSecurityMode_Certificate)
{
LOG(LOG_INFO, "Certificate found. ");
}
staticClient = PrepareStaticClient();
}

if (g_keepRunning && staticClient == NULL)
Expand Down Expand Up @@ -661,7 +670,6 @@ int main(int argc, char **argv)
LOG(LOG_INFO, "Observing IPSO object on path /3201/0/5550");
}


while (g_keepRunning) {
AwaStaticClient_Process(staticClient);
sleep(1);
Expand Down