feat add control_framework, read json file OK

This commit is contained in:
Liu_Weichao
2022-11-24 10:46:54 +08:00
parent 2a8a11849d
commit bc47ae644f
13 changed files with 144 additions and 112 deletions

View File

@@ -74,6 +74,9 @@ static int ControlAnalyzeRecipe(ControlProtocolType control_protocol, const char
uint16_t recipe_file_length = 0;
char *recipe_file_buf;
/*wait for SD-card mount done*/
PrivTaskDelay(5000);
//Step1 : read recipe file data from SD card or other store device
recipe_file_fd = PrivOpen(recipe_name, O_RDONLY);
if (recipe_file_fd < 0) {
@@ -110,7 +113,6 @@ static int ControlAnalyzeRecipe(ControlProtocolType control_protocol, const char
//Step2 : CJSON analyze
#ifdef LIB_USING_CJSON
cJSON *recipe_file_json = cJSON_Parse(recipe_file_buf);
PrivFree(recipe_file_buf);
if (NULL == recipe_file_json) {
printf("Parse recipe_file_buf failed!\n");
return -1;
@@ -120,14 +122,22 @@ static int ControlAnalyzeRecipe(ControlProtocolType control_protocol, const char
memset(control_protocol->recipe, 0, sizeof(struct ControlRecipe));
/*Get basic information from recipe file*/
if (RecipeBasicInformation(control_protocol->recipe, control_protocol->protocol_type, recipe_file_json) < 0) {
if (RecipeBasicInformation(control_protocol->recipe, recipe_file_json) < 0) {
return -1;
}
control_protocol->protocol_type = control_protocol->recipe->protocol_type;
printf("%s %d control_protocol %p recipe %p\n", __func__, __LINE__, control_protocol, control_protocol->recipe);
/*Get the variable need to read from recipe file*/
RecipeReadVariableItem(control_protocol->recipe, control_protocol->protocol_type, recipe_file_json);
RecipeReadVariableItem(control_protocol->recipe, recipe_file_json);
control_protocol->done = control_protocol->recipe->done;
cJSON_Delete(recipe_file_json);
PrivFree(recipe_file_buf);
printf("Read and parse recipe file done!\n");
#endif
@@ -247,6 +257,8 @@ int ControlFrameworkInit(void)
goto _out;
}
printf("%s malloc control_protocol %p\n", __func__, control_protocol);
//Control Protocol Struct Init
ret = ControlProtocolInit(control_protocol);
if (ret < 0) {
@@ -255,6 +267,8 @@ int ControlFrameworkInit(void)
goto _out;
}
printf("%s malloc CONTROL_RECIPE_FILE %s\n", __func__, CONTROL_RECIPE_FILE);
//Read Recipe File, Get Control Protocol Configure Param
ret = ControlAnalyzeRecipe(control_protocol, CONTROL_RECIPE_FILE);
if (ret < 0) {
@@ -265,6 +279,8 @@ int ControlFrameworkInit(void)
control_protocol->protocol_status = CONTROL_REGISTERED;
printf("%s recipe %p\n", __func__, control_protocol->recipe);
ret = ControlPeripheralInit(control_protocol->recipe);
if (ret < 0) {
printf("%s failed!\n", __func__);
@@ -272,6 +288,8 @@ int ControlFrameworkInit(void)
goto _out;
}
printf("%s ControlPeripheralInit done\n", __func__);
_out:
return ret;
}

View File

@@ -30,7 +30,6 @@ extern "C" {
struct ControlProtocol;
typedef struct ControlProtocol *ControlProtocolType;
typedef struct ControlData *ControlDataType;
struct ControlDone
{
@@ -76,7 +75,6 @@ struct ControlProtocol
sem_t sem;
pthread_mutex_t lock;
//struct DoublelistNode link;
};
/*Control Framework Protocol Init*/
@@ -85,12 +83,6 @@ int ControlFrameworkInit(void);
/*Control Framework Find certain Protocol*/
ControlProtocolType ControlProtocolFind(void);
// /*Control Framework Protocol Register*/
// int ControlProtocolRegister(struct ControlProtocol *control_protocol);
// /*Control Framework Protocol Unregister*/
// int ControlProtocolUnregister(struct ControlProtocol *control_protocol);
/*Control Framework Protocol Open*/
int ControlProtocolOpen(struct ControlProtocol *control_protocol);

View File

@@ -72,7 +72,7 @@ static int ControlProtocolInitDesc(struct ControlRecipe *p_recipe, struct Contro
for( i = 0; sub_protocol_desc[i].fn != NULL; i++ ) {
if (p_recipe->protocol_type == sub_protocol_desc[i].protocol_type) {
ret = sub_protocol_desc[i].fn(p_recipe);
printf("control protocol initialize %d %s\n", sub_protocol_desc[i].protocol_type, ret == 0 ? "success" : "failed");
printf("%s initialize %d %s\n", __func__, sub_protocol_desc[i].protocol_type, ret == 0 ? "success" : "failed");
break;
}
}
@@ -87,16 +87,16 @@ static int ControlProtocolInitDesc(struct ControlRecipe *p_recipe, struct Contro
static void FormatDataHeader(struct ControlRecipe *p_recipe)
{
uint16_t plc_read_data_length = CONTROL_DATA_HEAD_LENGTH + p_recipe->total_data_length;//Head length is CONTROL_DATA_HEAD_LENGTH
uint8_t *data = p_recipe->protocol_data->data;
uint8_t *data = p_recipe->protocol_data.data;
data[0] = CONTROL_DATA_HEAD_1;
data[1] = CONTROL_DATA_HEAD_2;
data[2] = (p_recipe->device_id) >> 8;
data[3] = p_recipe->device_id;
data[4] = (plc_read_data_length) >> 8;
data[5] = plc_read_data_length;
data[6] = (p_recipe->read_item_count) >> 8;
data[7] = p_recipe->read_item_count;
data[2] = (uint8_t)(p_recipe->device_id >> 8);
data[3] = (uint8_t)p_recipe->device_id;
data[4] = (uint8_t)(plc_read_data_length >> 8);
data[5] = (uint8_t)plc_read_data_length;
data[6] = (uint8_t)(p_recipe->read_item_count >> 8);
data[7] = (uint8_t)p_recipe->read_item_count;
}
/**
@@ -236,8 +236,12 @@ int ControlConnectSocket(BasicSocketPlc *p_plc)
return -4;
}
printf("%s %d ip %u.%u.%u.%u port %d\n", __func__, __LINE__,
p_plc->ip[0], p_plc->ip[1], p_plc->ip[2], p_plc->ip[3],
p_plc->port);
if (connect(plc_socket, (struct sockaddr*)&plc_addr_in, sizeof(struct sockaddr)) == -1) {
printf("Connect plc socket failed!\n");
printf("Connect plc socket failed!errno %d\n", errno);
closesocket(plc_socket);
return -5;
} else {
@@ -359,17 +363,12 @@ int ControlPeripheralInit(struct ControlRecipe *p_recipe)
/**
* @description: Control Framework Get Recipe Basic Information
* @param p_recipe - Control recipe pointer
* @param protocol_type - protocol type
* @param p_recipe_file_json - recipe_file_json pointer
* @return success : 0 error : -1
*/
int RecipeBasicInformation(struct ControlRecipe *p_recipe, int protocol_type, cJSON *p_recipe_file_json)
int RecipeBasicInformation(struct ControlRecipe *p_recipe, cJSON *p_recipe_file_json)
{
if (protocol_type != (ProtocolType)(cJSON_GetObjectItem(p_recipe_file_json, "protocol_type")->valueint)) {
printf("protocol type not match!\n");
return -1;
}
p_recipe->protocol_type = protocol_type;
p_recipe->protocol_type = (ProtocolType)(cJSON_GetObjectItem(p_recipe_file_json, "protocol_type")->valueint);
p_recipe->device_id = cJSON_GetObjectItem(p_recipe_file_json, "device_id")->valueint;
strncpy(p_recipe->device_name, cJSON_GetObjectItem(p_recipe_file_json, "device_name")->valuestring, 20);
@@ -394,17 +393,16 @@ int RecipeBasicInformation(struct ControlRecipe *p_recipe, int protocol_type, cJ
printf("\n************************************************************\n");
}
extern int FinsProtocolFormatCmd(struct ControlRecipe *p_recipe, ProtocolFormatInfo *protocol_format_info);
/**
* @description: Control Framework Read Variable Item Function
* @param p_recipe - Control recipe pointer
* @param protocol_type - protocol type
* @param p_recipe_file_json - recipe_file_json pointer
* @return
*/
void RecipeReadVariableItem(struct ControlRecipe *p_recipe, int protocol_type, cJSON *p_recipe_file_json)
void RecipeReadVariableItem(struct ControlRecipe *p_recipe, cJSON *p_recipe_file_json)
{
int ret = 0;
int i, ret = 0;
ProtocolFormatInfo protocol_format_info;
memset(&protocol_format_info, 0, sizeof(ProtocolFormatInfo));
@@ -416,10 +414,13 @@ void RecipeReadVariableItem(struct ControlRecipe *p_recipe, int protocol_type, c
p_recipe->total_data_length = GetRecipeTotalDataLength(read_item_list_json);
/*Malloc Read Data Pointer, Reference "CONTROL FRAMEWORK READ DATA FORMAT"*/
p_recipe->protocol_data = PrivMalloc(sizeof(struct ProtocolData));
p_recipe->protocol_data->data = PrivMalloc(CONTROL_DATA_HEAD_LENGTH + p_recipe->total_data_length);
p_recipe->protocol_data->data_length = CONTROL_DATA_HEAD_LENGTH + p_recipe->total_data_length;
memset(p_recipe->protocol_data->data, 0, p_recipe->protocol_data->data_length);
p_recipe->protocol_data.data = PrivMalloc(CONTROL_DATA_HEAD_LENGTH + p_recipe->total_data_length);
p_recipe->protocol_data.data_length = CONTROL_DATA_HEAD_LENGTH + p_recipe->total_data_length;
memset(p_recipe->protocol_data.data, 0, p_recipe->protocol_data.data_length);
protocol_format_info.p_read_item_data = p_recipe->protocol_data.data + CONTROL_DATA_HEAD_LENGTH;
printf("%s %d recipe %p\n", __func__, __LINE__, p_recipe);
/*Init The Control Protocol*/
ControlProtocolInitDesc(p_recipe, protocol_init);
@@ -427,18 +428,26 @@ void RecipeReadVariableItem(struct ControlRecipe *p_recipe, int protocol_type, c
/*Format Data Header, Reference "CONTROL FRAMEWORK READ DATA FORMAT"*/
FormatDataHeader(p_recipe);
for (uint16_t read_item_index = 0; read_item_index < p_recipe->read_item_count; read_item_index ++) {
cJSON *read_single_item_json = cJSON_GetArrayItem(read_item_list_json, read_item_index);
uint16_t read_item_count = p_recipe->read_item_count;
printf("%s %d protocol_format_info %p read_item_count %p\n", __func__, __LINE__, &protocol_format_info, &(p_recipe->read_item_count));
for (i = 0; i < read_item_count; i ++) {
printf("%s %d read_item_index %d read_item_count %d\n", __func__, __LINE__, i, read_item_count);
cJSON *read_single_item_json = cJSON_GetArrayItem(read_item_list_json, i);
protocol_format_info.read_single_item_json = read_single_item_json;
protocol_format_info.read_item_index = read_item_index;
protocol_format_info.read_item_index = i;
/*Format Protocol Cmd By Analyze Variable Item One By One*/
ret = p_recipe->ControlProtocolFormatCmd(p_recipe, &protocol_format_info);
if (ret < 0) {
printf("%s read %d item failed!\n", __func__, read_item_index);
printf("%s read %d item failed!\n", __func__, i);
continue;
}
printf("%s %d ret %d recipe %p count %p [%d] [%d]\n", __func__, __LINE__, ret, p_recipe, &(p_recipe->read_item_count), p_recipe->read_item_count, read_item_count);
}
}
}

View File

@@ -76,7 +76,9 @@ typedef struct
typedef struct
{
cJSON *read_single_item_json;
uint8_t *p_read_item_data;
uint16_t read_item_index;//Variable item index(1 ++)
uint8_t last_item_size;
}ProtocolFormatInfo;
struct ProtocolData
@@ -104,7 +106,7 @@ struct SocketConfig
struct ControlRecipe
{
char device_name[20];
uint8_t device_name[20];
uint16_t device_id;
uint16_t read_period;
uint16_t read_item_count;
@@ -114,11 +116,12 @@ struct ControlRecipe
ProtocolType protocol_type;
void *read_item;
struct ControlDone *done;
struct SerialConfig serial_config;
struct SocketConfig socket_config;
struct ProtocolData *protocol_data;
struct ProtocolData protocol_data;
int (*ControlProtocolFormatCmd)(struct ControlRecipe *p_recipe, ProtocolFormatInfo *protocol_format_info);
};
@@ -127,10 +130,10 @@ struct ControlRecipe
uint8_t GetValueTypeMemorySize(UniformValueType uniform_value_type);
/*Get basic information from recipe file*/
int RecipeBasicInformation(struct ControlRecipe *p_recipe, int protocol_type, cJSON *p_recipe_file_json);
int RecipeBasicInformation(struct ControlRecipe *p_recipe, cJSON *p_recipe_file_json);
/*Get the variable need to read from recipe file*/
void RecipeReadVariableItem(struct ControlRecipe *p_recipe, int protocol_type, cJSON *p_recipe_file_json);
void RecipeReadVariableItem(struct ControlRecipe *p_recipe, cJSON *p_recipe_file_json);
/*Control Framework Peripheral Device Init*/
int ControlPeripheralInit(struct ControlRecipe *p_recipe);

View File

@@ -29,6 +29,10 @@
*/
void SocketInit(char *ip, char *mask, char *gw)
{
printf("%s ip %d.%d.%d.%d mask %d.%d.%d.%d gw %d.%d.%d.%d\n", __func__,
ip[0], ip[1], ip[2], ip[3],
mask[0], mask[1], mask[2], mask[3],
gw[0], gw[1], gw[2], gw[3]);
#ifdef BSP_USING_LWIP
lwip_config_tcp(0, ip, mask, gw);
#endif