forked from xuos/xiuos
modify XiUOS DIR : (1.add plc_demo in APP_Framework/control_app; 2.add industrial_network、industrial_fieldbus and industrial_wlan; 3.add XiZi_AIoT and modify XiZi as XiZi_IIoT.)
This commit is contained in:
@@ -1,21 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
/**
|
||||
* @file control.c
|
||||
* @brief control framework code
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022-09-27
|
||||
*/
|
||||
|
||||
#include <control.h>
|
||||
|
||||
void control_init(void)
|
||||
{
|
||||
|
||||
//to do
|
||||
}
|
||||
|
||||
void control_read_file()
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
24
APP_Framework/Framework/control/shared/control.h
Normal file
24
APP_Framework/Framework/control/shared/control.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2022 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file control.h
|
||||
* @brief control framework code
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022-09-27
|
||||
*/
|
||||
|
||||
#include <control_file.h>
|
||||
#include <cJSON.h>
|
||||
|
||||
|
||||
@@ -13,194 +13,9 @@
|
||||
/**
|
||||
* @file control_file.c
|
||||
* @brief control relative file operation
|
||||
* @version 1.0
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022-03-17
|
||||
* @date 2022-09-37
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "cJSON.h"
|
||||
#include "transform.h"
|
||||
#include "control_file.h"
|
||||
|
||||
//json file parameter for PLC socket communication as below:
|
||||
//{
|
||||
// "ip": "192.168.250.6",
|
||||
// "port": 102,
|
||||
// "cmd": [3, 0, 0, 22, 17, 224, 0, 0, 2, 200, 0, 193, 2, 2, 1, 194, 2, 2, 1, 192, 1, 10],
|
||||
// "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" \
|
||||
//}"
|
||||
|
||||
#define TEST_PLC_JSON_TXT \
|
||||
"{ \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" \
|
||||
"}"
|
||||
|
||||
|
||||
CtlPlcSockParamType ctl_file_param;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void CtlFileClose(FILE *fd)
|
||||
{
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
void CtlFileRead(FILE *fd, int size, char *buf)
|
||||
{
|
||||
fseek(fd, 0, SEEK_SET);
|
||||
fread(buf, size, 1, fd);
|
||||
ctl_print("read file %d: %.100s\n", size, buf);
|
||||
}
|
||||
|
||||
void CtlFileWrite(FILE *fd, int size, char *buf)
|
||||
{
|
||||
size_t write_size = 0;
|
||||
write_size = fwrite(buf, strlen(buf) + 1, 1, fd);
|
||||
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);
|
||||
if(fd == NULL)
|
||||
return;
|
||||
char *file_buf = TEST_PLC_JSON_TXT;
|
||||
CtlFileWrite(fd, strlen(file_buf), file_buf);
|
||||
CtlFileClose(fd);
|
||||
}
|
||||
|
||||
#ifdef LIB_USING_CJSON
|
||||
|
||||
void CtlParseJsonArray(cJSON *dat, int *cmd_len, char *cmd)
|
||||
{
|
||||
int len, i;
|
||||
if(cJSON_IsArray(dat))
|
||||
{
|
||||
len = cJSON_GetArraySize(dat);
|
||||
ctl_print("json cmd %d\n", len);
|
||||
|
||||
for(i = 0; i < len; i++)
|
||||
{
|
||||
cJSON *cmd_val = cJSON_GetArrayItem(dat, i);
|
||||
if(NULL == cmd_val)
|
||||
continue;
|
||||
ctl_print("0x%x ", cmd_val->valueint);
|
||||
cmd[i] = cmd_val->valueint;
|
||||
}
|
||||
*cmd_len = len;
|
||||
ctl_print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void CtlParseJsonData(char *buf)
|
||||
{
|
||||
cJSON *file_dat = NULL;
|
||||
cJSON *ip_dat = NULL;
|
||||
cJSON *port_dat = NULL;
|
||||
cJSON *tcp_dat = NULL;
|
||||
cJSON *cmd_dat = NULL;
|
||||
char cmd_title[10] = {"cmd"};
|
||||
CtlPlcSockParamType *file_param = &ctl_file_param;
|
||||
|
||||
file_dat = cJSON_Parse(buf);
|
||||
if(file_dat == NULL)
|
||||
{
|
||||
ctl_error("ctrl parse failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
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],
|
||||
&file_param->ip[1],
|
||||
&file_param->ip[2],
|
||||
&file_param->ip[3]);
|
||||
|
||||
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;
|
||||
|
||||
for(int i = 0; i < CTL_CMD_NUM; i++)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
cJSON_Delete(file_dat);
|
||||
}
|
||||
|
||||
void CtlParseFileTest(void)
|
||||
{
|
||||
//for PLC socket parameter file
|
||||
FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME);
|
||||
if(fd == NULL)
|
||||
{
|
||||
ctl_error("ctl get file %s failed\n", PLC_SOCK_FILE_NAME);
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -12,48 +12,15 @@
|
||||
|
||||
/**
|
||||
* @file control_file.h
|
||||
* @brief control relative API
|
||||
* @version 1.0
|
||||
* @brief control file function relative API
|
||||
* @version 3.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2022-03-17
|
||||
* @date 2022-09-27
|
||||
*/
|
||||
|
||||
#ifndef __CONTROL_FILE_H_
|
||||
#define __CONTROL_FILE_H_
|
||||
#ifndef CONTROL_FILE_H
|
||||
#define CONTROL_FILE_H
|
||||
|
||||
#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_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);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user