optimize the codes with lwip and opcua

This commit is contained in:
wlyu
2022-03-21 16:47:32 +08:00
parent a52854efb6
commit f74d1dafd7
25 changed files with 452 additions and 2257 deletions

View File

@@ -36,6 +36,7 @@
"{ \r\n"\
" \"ip\": \"192.168.250.6\", \r\n"\
" \"port\": 102, \r\n"\
" \"tcp\": 1, \r\n"\
" \"cmd\": [3, 0, 0, 22, 17, 224, 0, 0, 2, 200, 0, 193, 2, 2, 1, 194, 2, 2, 1, 192, 1, 10], \r\n"\
" \"cmd1\": [3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 0, 13, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 0, 240], \r\n" \
" \"cmd2\": [3, 0, 0, 31, 2, 240, 128, 50, 1, 0, 0, 51, 1, 0, 14, 0, 0, 4, 1, 18, 10, 16, 2, 0, 210, 0, 52, 132, 0, 0, 0]\r\n" \
@@ -48,12 +49,21 @@ FILE *CtlFileInit(char *file)
{
FILE *fd = NULL;
#ifdef MOUNT_SDCARD
// SD card mount flag 1: OK
if(sd_mount_flag == 0)
{
ctl_error("SD card mount failed\n");
return NULL;
}
fd = fopen(file, "a+");
if(fd == NULL)
{
ctl_error("open file %s failed\n", file);
}
#endif
return fd;
}
@@ -76,6 +86,22 @@ void CtlFileWrite(FILE *fd, int size, char *buf)
ctl_print("write size %d: %s\n", size, buf);
}
int CtlFileReadWithFilename(char *file, int size, char *buf)
{
FILE *fd;
fd = fopen(file, "r");
if(fd == NULL)
{
ctl_error("open file %s failed\n", file);
return EEMPTY;
}
fseek(fd, 0, SEEK_SET);
fread(buf, size, 1, fd);
ctl_print("read file %d: %.100s\n", size, buf);
return EOK;
}
void CtlCreateFileTest(void)
{
FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME);
@@ -117,10 +143,9 @@ void CtlParseJsonData(char *buf)
cJSON *file_dat = NULL;
cJSON *ip_dat = NULL;
cJSON *port_dat = NULL;
cJSON *tcp_dat = NULL;
cJSON *cmd_dat = NULL;
int cmd_num = 0;
int cmd_index = 1;
char cmd_str[10] = {0};
char cmd_title[10] = {"cmd"};
CtlPlcSockParamType *file_param = &ctl_file_param;
file_dat = cJSON_Parse(buf);
@@ -132,6 +157,7 @@ void CtlParseJsonData(char *buf)
ip_dat = cJSON_GetObjectItem(file_dat, "ip");
port_dat = cJSON_GetObjectItem(file_dat, "port");
tcp_dat = cJSON_GetObjectItem(file_dat, "tcp");
ctl_print(" ip : %s\n", ip_dat->valuestring);
sscanf(ip_dat->valuestring, "%d.%d.%d.%d", &file_param->ip[0],
@@ -141,14 +167,17 @@ void CtlParseJsonData(char *buf)
ctl_print(" port: %s %d\n", ip_dat->string, port_dat->valueint);
file_param->port = port_dat->valueint;
file_param->tcp = tcp_dat->valueint;
file_param->cmd_num = 0;
strcpy(cmd_str, "cmd");
while(cmd_dat = cJSON_GetObjectItem(file_dat, cmd_str))
for(int i = 0; i < CTL_CMD_NUM; i++)
{
CtlParseJsonArray(cmd_dat, &file_param->cmd_len[cmd_index - 1], file_param->cmd[cmd_index - 1]);
snprintf(cmd_str, sizeof(cmd_str), "cmd%d", cmd_index++);
cmd_dat = cJSON_GetObjectItem(file_dat, cmd_title);
if(!cmd_dat)
break;
CtlParseJsonArray(cmd_dat, &file_param->cmd_len[i], file_param->cmd[i]);
snprintf(cmd_title, sizeof(cmd_title), "cmd%d", ++file_param->cmd_num);
}
file_param->cmd_num = cmd_index - 1;
cJSON_Delete(file_dat);
}
@@ -156,14 +185,25 @@ void CtlParseJsonData(char *buf)
void CtlParseFileTest(void)
{
//for PLC socket parameter file
char file_buf[CTL_FILE_SIZE] = {0};
FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME);
if(fd == NULL)
{
ctl_error("ctl get file %s failed\n", PLC_SOCK_FILE_NAME);
return;
memset(file_buf, 0, CTL_FILE_SIZE);
CtlFileRead(fd, CTL_FILE_SIZE, file_buf);
}
char *file_buf = malloc(CTL_FILE_LEN);
if(file_buf == NULL)
{
ctl_error("ctl malloc failed\n");
return;
}
memset(file_buf, 0, CTL_FILE_LEN);
CtlFileRead(fd, CTL_FILE_LEN, file_buf);
CtlFileClose(fd);
CtlParseJsonData(file_buf);
free(file_buf);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0),

View File

@@ -21,31 +21,36 @@
#ifndef __CONTROL_FILE_H_
#define __CONTROL_FILE_H_
#define CTL_FILE_SIZE 1000
#define CTL_CMD_NUM 10 // max command number
#define CTL_CMD_LEN 100
#define CTL_IP_LEN 32
#define CTL_FILE_LEN 1000 // control file size
#define CTL_CMD_NUM 10 // support command number
#define CTL_CMD_LEN 100 // control command length
#define CTL_IP_LEN 32 // IP address length
#define CTL_FILE_NAME_LEN 100 // file name length
#define PLC_SOCK_FILE_NAME "/plc/socket_param.json"
#define ctl_print KPrintf
#define ctl_print //KPrintf
#define ctl_error KPrintf
// for running plc socket
typedef struct CtlPlcSockParamStruct
{
char ip[CTL_IP_LEN];
int port;
int tcp; // 1: TCP 0: UDP
int cmd_num; //command number
int cmd_len[CTL_CMD_NUM]; // command length
char cmd[CTL_CMD_NUM][CTL_CMD_LEN];
}CtlPlcSockParamType;
extern CtlPlcSockParamType ctl_file_param;
extern int sd_mount_flag;
FILE *CtlFileInit(char *file);
void CtlFileClose(FILE *fd);
void CtlFileRead(FILE *fd, int size, char *buf);
void CtlFileWrite(FILE *fd, int size, char *buf);
int CtlFileReadWithFilename(char *file_name, int size, char *buf);
#ifdef LIB_USING_CJSON
void CtlParseJsonData(char *buf);