Skip to content

Commit

Permalink
Update YCommand interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dariosb committed Jun 23, 2020
1 parent 103f033 commit 36ea47e
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 184 deletions.
7 changes: 5 additions & 2 deletions src/YCommand/inc/YCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

/* ----------------------------- Include files ----------------------------- */
#include "rkh.h"
#include "YCommandParser.h"
#include "Config.h"

/* ---------------------- External C language linkage ---------------------- */
#ifdef __cplusplus
Expand All @@ -44,6 +42,11 @@ typedef enum

#define YCOMMAND_SECURITY_KEY_DFT "123"

#define YCOMMAND_ID_LEN 2
#define YCOMMAND_SECURITY_LEN 3
#define YCOMMAND_DATA_LEN 32
#define YCOMMAND_INDEX_LEN 11

/* ------------------------------- Data types ------------------------------ */
typedef struct
{
Expand Down
44 changes: 44 additions & 0 deletions src/YCommand/inc/YCommandExec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* \file YCommandExec.h
* \brief Yipies Command Parser.
*/

/* -------------------------- Development history -------------------------- */
/*
* 2020.04.17 DaBa v1.0.00 Initial version
*/

/* -------------------------------- Authors -------------------------------- */
/*
* DaBa Dario Balina [email protected]
*/

/* --------------------------------- Notes --------------------------------- */
/* --------------------------------- Module -------------------------------- */
#ifndef __YCOMMANDEXEC_H__
#define __YCOMMANDEXEC_H__

/* ----------------------------- Include files ----------------------------- */
#include "YCommandParser.h"

/* ---------------------- External C language linkage ---------------------- */
#ifdef __cplusplus
extern "C" {
#endif

/* --------------------------------- Macros -------------------------------- */
/* -------------------------------- Constants ------------------------------ */
/* ------------------------------- Data types ------------------------------ */
/* -------------------------- External variables --------------------------- */
/* -------------------------- Function prototypes -------------------------- */
YCmdRes YCommand_exec(YCmdParserData *pcmd);

/* -------------------- External C language linkage end -------------------- */
#ifdef __cplusplus
}
#endif

/* ------------------------------ Module end ------------------------------- */
#endif

/* ------------------------------ End of file ------------------------------ */
45 changes: 45 additions & 0 deletions src/YCommand/inc/YCommandFormat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* \file YCommandFormat.h
* \brief Yipies Command Format.
*/

/* -------------------------- Development history -------------------------- */
/*
* 2020.04.17 DaBa v1.0.00 Initial version
*/

/* -------------------------------- Authors -------------------------------- */
/*
* DaBa Dario Balina [email protected]
*/

/* --------------------------------- Notes --------------------------------- */
/* --------------------------------- Module -------------------------------- */
#ifndef __YCOMMANDFORMAT_H__
#define __YCOMMANDFORMAT_H__

/* ----------------------------- Include files ----------------------------- */
#include "YCommand.h"
#include "YCommandParser.h"

/* ---------------------- External C language linkage ---------------------- */
#ifdef __cplusplus
extern "C" {
#endif

/* --------------------------------- Macros -------------------------------- */
/* -------------------------------- Constants ------------------------------ */
/* ------------------------------- Data types ------------------------------ */
/* -------------------------- External variables --------------------------- */
/* -------------------------- Function prototypes -------------------------- */
YCmdRes YCommandFormat_format(YCmdParserData *pCmd, char *p);

/* -------------------- External C language linkage end -------------------- */
#ifdef __cplusplus
}
#endif

/* ------------------------------ Module end ------------------------------- */
#endif

/* ------------------------------ End of file ------------------------------ */
62 changes: 50 additions & 12 deletions src/YCommand/inc/YCommandParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/* -------------------------- Development history -------------------------- */
/*
* 2020.04.17 Daba v1.0.00 Initial version
* 2020.04.17 DaBa v1.0.00 Initial version
*/

/* -------------------------------- Authors -------------------------------- */
Expand All @@ -21,6 +21,9 @@
/* ----------------------------- Include files ----------------------------- */
#include "rkh.h"
#include "ssp.h"
#include "YCommand.h"
#include "Config.h"


/* ---------------------- External C language linkage ---------------------- */
#ifdef __cplusplus
Expand All @@ -39,12 +42,29 @@ typedef enum

}YCmdPResult;

#define YCOMMAND_INDEX_LEN 11
#define YCOMMAND_ID_LEN 2
#define YCOMMAND_SECURITY_LEN 3
#define YCOMMAND_DATA_LEN 32

/* ------------------------------- Data types ------------------------------ */
typedef enum
{
YCmdServerIp,
YCmdServerPort,
YCmdConnectionTime,
YCmdGpsTime,
YCmdAccLimit,
YCmdBreakLimit,
YCmdStatus,
YCmdSetOut1,
YCmdSetOut2,
YCmdSetOut3,
YCmdSetOut4,
YCmdSetOut5,
YCmdSetOut6,
YCmdReset,
YCmdSampleTime,
YCmdDataFormat,

YCmdNum,
} YCmd_t;

typedef struct
{
SSP parser;
Expand All @@ -55,14 +75,32 @@ typedef struct
YCmdPResult result;
}YCommandParser;

typedef union
{
char serverIp[IP_LENGTH+1];
char serverPort[PORT_LENGTH+1];

rui16_t _rui16;
rui16_t connTime;
rui16_t updateGPSTime;

rui8_t _rui8;
rui8_t sampleTime;
rui8_t accLimit;
rui8_t brLimit;
rui8_t status;
rui8_t outValue;
} CmdData;

typedef struct
{
YCommand *p;
YCmd_t id;
CmdData data;
}YCmdParserData;

/* -------------------------- External variables --------------------------- */
/* -------------------------- Function prototypes -------------------------- */
YCmdPResult YCommandParser_search(YCommandParser *me, char *p, ruint size);
YCmdPResult YCommandParser_securityCheck(YCommandParser *me, char *pkey);
ruint YCommandParser_getId(YCommandParser *me);
char * YCommandParser_getIndex(YCommandParser *me);
char * YCommandParser_getData(YCommandParser *me);

/* -------------------- External C language linkage end -------------------- */
#ifdef __cplusplus
}
Expand Down
138 changes: 9 additions & 129 deletions src/YCommand/src/YCommand.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,119 +19,22 @@
#include <stdlib.h>
#include "rkh.h"
#include "YCommand.h"
#include "YCommandParser.h"
#include "YCommandExec.h"
#include "IpValid.h"

/* ----------------------------- Local macros ------------------------------ */
/* ------------------------------- Constants ------------------------------- */
/* ---------------------------- Local data types --------------------------- */
typedef struct YCmdFormat
{
YCmd_t (*fmt)(YCommand *pCmd, char *pData);
ruint min;
ruint max;
}YCmdFormat;

/* ---------------------------- Global variables --------------------------- */
/* ---------------------------- Local variables ---------------------------- */
static YCommandParser yCmdParser;

/* ----------------------- Local function prototypes ----------------------- */
static YCmd_t fmt_none(YCommand *pCmd, char *pData);
static YCmd_t fmt_serverIp(YCommand *pCmd, char *pData);
static YCmd_t fmt_string(YCommand *pCmd, char *pData);
static YCmd_t fmt_rui8(YCommand *pCmd, char *pData);
static YCmd_t fmt_rui16(YCommand *pCmd, char *pData);

static const YCmdFormat fmtTable[YCmdNum] =
{
{ fmt_serverIp, 7, IP_LENGTH }, // YCmdServerIp
{ fmt_string, 1, PORT_LENGTH}, // YCmdServerPort
{ fmt_rui16, 1, 4 }, // YCmdConnectionTime
{ fmt_rui16, 1, 4 }, // YCmdGpsTime
{ fmt_rui8, 1, 2 }, // YCmdAccLimit
{ fmt_rui8, 1, 2 }, // YCmdBreakLimit
{ fmt_rui8, 1, 1 }, // YCmdStatus
{ fmt_rui8, 1, 1 }, // YCmdSetOut1
{ fmt_rui8, 1, 1 }, // YCmdSetOut2
{ fmt_rui8, 1, 1 }, // YCmdSetOut3
{ fmt_rui8, 1, 1 }, // YCmdSetOut4
{ fmt_rui8, 1, 1 }, // YCmdSetOut5
{ fmt_rui8, 1, 1 }, // YCmdSetOut6
{ fmt_none, 1, 1 }, // YCmdReset
{ fmt_rui8, 1, 1 }, // YCmdSampleTime
{ fmt_none, 1, 1 } // YCmdDataFormat
};


/* ---------------------------- Local functions ---------------------------- */
static
rInt
checkLen(char *p, ruint min, ruint max)
{
ruint l = strlen(p);

if(l < min || l > max)
return -1;
return 0;
}

static
YCmd_t
fmt_none(YCommand *pCmd, char *pData)
{
return pCmd->id;
}

static
YCmd_t
fmt_serverIp(YCommand *pCmd, char *pData)
{
if(IpValid_check(pData))
{
strcpy(pCmd->data.serverIp, pData);
}
else
{
pCmd->id = YCmdWrongFormat;
}

return pCmd->id;
}

static
YCmd_t
fmt_string(YCommand *pCmd, char *pData)
{
strcpy(pCmd->data.serverIp, pData);

return pCmd->id;
}

static
YCmd_t
fmt_rui16(YCommand *pCmd, char *pData)
{
pCmd->data._rui16 = (rui16_t)atoi(pData);

return pCmd->id;
}

static
YCmd_t
fmt_rui8(YCommand *pCmd, char *pData)
{
pCmd->data._rui8 = (rui8_t)atoi(pData);

return pCmd->id;
}

/* ---------------------------- Global functions --------------------------- */
YCmd_t
YCommand_parse(YCommand *pCmd, char *p, ruint size)
YCmdRes
YCommand_parseAndExec(YCommand *pCmd, char *p, ruint size)
{
YCmd_t cmd;
const YCmdFormat *pFmt;
char *pData;
YCmdParserData cmd;

if(pCmd == NULL || p == NULL || size == 0)
{
Expand All @@ -140,35 +43,12 @@ YCommand_parse(YCommand *pCmd, char *p, ruint size)

memset(pCmd, 0, sizeof(YCommand));

if(YCommandParser_search(&yCmdParser, p, size) < 0)
return YCmdUnknown;
cmd.p = pCmd;
memset(&(cmd.data), 0, sizeof(CmdData));

cmd = YCommandParser_getId(&yCmdParser);
if(cmd == YCmdUnknown)
{
return YCmdUnknown;
}
YCommand_parse(&cmd, p, size);

if(YCommandParser_securityCheck(&yCmdParser, YCOMMAND_SECURITY_KEY_DFT) < 0)
{
return YCmdInvalidKey;
}

pData = YCommandParser_getData(&yCmdParser);
pFmt = &fmtTable[cmd];

if(checkLen(pData, pFmt->min, pFmt->max) < 0)
return YCmdWrongLen;

pCmd->id = YCommandParser_getId(&yCmdParser);
cmd = (*pFmt->fmt)(pCmd, pData);

if(cmd >= 0)
{
strcpy(pCmd->index, YCommandParser_getIndex(&yCmdParser));
}

return cmd;
return YCmdOk;
}

/* ------------------------------ End of file ------------------------------ */
Loading

0 comments on commit 36ea47e

Please sign in to comment.