|
@@ -3,8 +3,10 @@
|
|
|
#include <stdlib.h>
|
|
|
#include "protocol.h"
|
|
|
#include "malloc.h"
|
|
|
+#include <stdio.h>
|
|
|
#include "gateway_message.h"
|
|
|
|
|
|
+
|
|
|
GATEWAY_PARAMS gateway_config_params = {0};
|
|
|
|
|
|
GATEWAY_PARAMS *get_gateway_config_params()
|
|
@@ -14,120 +16,74 @@ GATEWAY_PARAMS *get_gateway_config_params()
|
|
|
|
|
|
void parseStringField(const char *data, const char *field, char *value);
|
|
|
int parseIntField(const char *data, const char *field);
|
|
|
-void addNode(char *nodestring);
|
|
|
-void addDevice(char *string, NODE_PARAMS *node);
|
|
|
+//void addNode(char *nodestring);
|
|
|
+void addDevice(char *string);
|
|
|
int extract_substring(const char *input_string, const char *start_token, const char *end_token, char *result);
|
|
|
void addSensorListParams(char *paramString, DEVICE_PARAMS *device);
|
|
|
void addCommandListParams(char *paramString, DEVICE_PARAMS *device);
|
|
|
/**
|
|
|
* @brief 解析输入字符串网关结构体信息,将数据保存
|
|
|
- * @param char *nodestring 输入字符串的数据
|
|
|
+ * @param char *gatewayString 输入字符串的数据
|
|
|
* @retval 无
|
|
|
*/
|
|
|
-void addGatewayParams(char *gatewayString)
|
|
|
+ void addGatewayParams(char *gatewayString)
|
|
|
{
|
|
|
- parseStringField(gatewayString, "\"commandTopic\":\"", (char *)&gateway_config_params.commandTopic);
|
|
|
- parseStringField(gatewayString, "\"messageTopic\":\"", (char *)&gateway_config_params.messageTopic);
|
|
|
- parseStringField(gatewayString, "\"deviceId\":\"", (char *)&gateway_config_params.deviceId);
|
|
|
- parseStringField(gatewayString, "\"host\":\"", (char *)&gateway_config_params.host);
|
|
|
- gateway_config_params.port = parseIntField(gatewayString, "\"port\":");
|
|
|
- gateway_config_params.node_params = NULL;
|
|
|
- gateway_config_params.pollTime = parseIntField(gatewayString, "\"pollTime\":");
|
|
|
- char *nodeString = strstr(gatewayString,"nodeList"); //移位置到节点数据开始处
|
|
|
+ //gateway_config_params.device_params = NULL;
|
|
|
+ gateway_config_params.port = parseIntField(gatewayString, "\"port\":");
|
|
|
+ gateway_config_params.stopBit = parseIntField(gatewayString, "\"stopBit\":");
|
|
|
+ gateway_config_params.dataBits = parseIntField(gatewayString, "\"dataBit\":");
|
|
|
+ gateway_config_params.baudrate = parseIntField(gatewayString, "\"baudrate\":");
|
|
|
+ gateway_config_params.checkBit = parseIntField(gatewayString, "\"checkBit\":");
|
|
|
+ gateway_config_params.inboundTime= parseIntField(gatewayString, "\"inboundTime\":");
|
|
|
+
|
|
|
+ parseStringField(gatewayString, "\"host\":\"", (char *)&gateway_config_params.host);
|
|
|
+ parseStringField(gatewayString, "\"deviceId\":\"", (char *)&gateway_config_params.deviceId);
|
|
|
+ parseStringField(gatewayString, "\"commandTopic\":\"", (char *)&gateway_config_params.commandTopic);
|
|
|
+ parseStringField(gatewayString, "\"messageTopic\":\"", (char *)&gateway_config_params.messageTopic);
|
|
|
+ parseStringField(gatewayString, "\"username\":\"", (char *)&gateway_config_params.username);
|
|
|
+ parseStringField(gatewayString, "\"password\":\"", (char *)&gateway_config_params.passwd);
|
|
|
+
|
|
|
+ char *deviceString = strstr(gatewayString,"deviceList"); //移位置到节点数据开始处
|
|
|
|
|
|
- while (1)
|
|
|
- {
|
|
|
- addNode(nodeString);
|
|
|
- nodeString=nodeString;
|
|
|
- nodeString=strstr(nodeString,"}");
|
|
|
- nodeString[0]='A';
|
|
|
- nodeString++;
|
|
|
- if(nodeString[0]==']')//数组结束标志、
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @brief 解析输入的字符串节点信息,并将此节点信息挂载在网关结构体的节点下面
|
|
|
- * @param char *nodestring
|
|
|
- * @retval 无
|
|
|
- 注:node内包含的设备名称device_1在别的节点下也可能包含,所以在解析节点时要先将数据截取出来让匹配不会越界到别的节点
|
|
|
- */
|
|
|
-
|
|
|
-void addNode(char *nodestring)
|
|
|
-{
|
|
|
- // 新增一个节点的设备并解析最外层的节点属性
|
|
|
- char *deviceString = (char *)nodestring; // 用于移位解析device设备信息
|
|
|
- NODE_PARAMS *newNode = malloc(sizeof(NODE_PARAMS));
|
|
|
- newNode->device_params = NULL; // 先将其设备挂载为NULL
|
|
|
- newNode->nextNode = NULL; // 下一个设备的挂载为NULL
|
|
|
- char nodeIdString[5];
|
|
|
- parseStringField(nodestring, "\"nodeId\":\"", (char *)&nodeIdString);
|
|
|
- for (int j = 0; j < 2; j++)
|
|
|
- {
|
|
|
- uint8_t byte;
|
|
|
- sscanf((const char *)&nodeIdString[j * 2], "%2hhx", &byte);
|
|
|
- newNode->node_address[j]=byte;
|
|
|
- }
|
|
|
- newNode->RFFrequency = parseIntField(nodestring, "\"RFFrequency\":");
|
|
|
- newNode->SignalBw = parseIntField(nodestring, "\"SignalBw\":");
|
|
|
- newNode->SpreadingFactor = parseIntField(nodestring, "\"SpreadingFactor\":");
|
|
|
- newNode->ErrorCoding = parseIntField(nodestring, "\"ErrorCoding\":");
|
|
|
|
|
|
- deviceString=strstr(nodestring,"deviceList");//移位置到设备数组起始处
|
|
|
- while (1)
|
|
|
+ while (1)
|
|
|
{
|
|
|
if (deviceString)
|
|
|
{
|
|
|
- addDevice(deviceString, newNode); // 往此节点下挂载设备信息
|
|
|
+ addDevice(deviceString); // 往此地址下挂载设备信息
|
|
|
deviceString=deviceString;//重新获取设备字符串的位置
|
|
|
deviceString = strstr(deviceString, "}");
|
|
|
deviceString[0]='A';
|
|
|
deviceString++;
|
|
|
if(deviceString[0]==']')
|
|
|
{
|
|
|
- break;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
- /* code */
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 添加节点到网关链表中
|
|
|
- if (gateway_config_params.node_params == NULL)
|
|
|
- {
|
|
|
- gateway_config_params.node_params = newNode;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- NODE_PARAMS *current = gateway_config_params.node_params;
|
|
|
- while (current->nextNode != NULL)
|
|
|
- {
|
|
|
- current = current->nextNode;
|
|
|
- }
|
|
|
- current->nextNode = newNode;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* @brief 解析输入字段的字符串,解析出属于该节点下的设备信息
|
|
|
* @param uint8_t *string输入的字符串数据,NODE_PARAMS *node节点信息
|
|
|
* @retval 无
|
|
|
*/
|
|
|
-void addDevice(char *deviceString, NODE_PARAMS *node)
|
|
|
+
|
|
|
+void addDevice(char *deviceString)
|
|
|
{
|
|
|
char *paramString = (char *)deviceString; // 属性指针
|
|
|
- // 创建新设备页
|
|
|
- DEVICE_PARAMS *newDevicePage = (DEVICE_PARAMS *)malloc(sizeof(DEVICE_PARAMS));
|
|
|
+ DEVICE_PARAMS *newDevicePage = (DEVICE_PARAMS *)malloc(sizeof(DEVICE_PARAMS)); // 创建新设备页
|
|
|
newDevicePage->nextDevice = NULL;
|
|
|
- parseStringField(deviceString, "\"deviceId\":\"", (char *)&newDevicePage->deviceID);
|
|
|
newDevicePage->protocol = parseIntField(deviceString, "\"protocol\":");
|
|
|
+ newDevicePage->MDBdataType = parseIntField(deviceString, "\"dataType\":");
|
|
|
+ newDevicePage->MDBbigLittleFormat = parseIntField(deviceString, "\"bigLittleFormat\":");
|
|
|
+
|
|
|
+ parseStringField(deviceString, "\"deviceId\":\"", (char *)&newDevicePage->deviceID);
|
|
|
+
|
|
|
newDevicePage->params = (PARAMS_PROTOCOL_COMMAND *)malloc(sizeof(PARAMS_PROTOCOL_COMMAND));
|
|
|
|
|
|
paramString = strstr(paramString, "\"sensorData\":["); // 找到该节点的轮询上发属性
|
|
@@ -141,7 +97,7 @@ void addDevice(char *deviceString, NODE_PARAMS *node)
|
|
|
if (paramString[0] == ']')
|
|
|
{
|
|
|
paramString = (char *)deviceString; // 复原指针位置
|
|
|
- break; // 找到了结束符,跳出循环
|
|
|
+ break; // 找到了结束符,跳出循环
|
|
|
}
|
|
|
}
|
|
|
if(newDevicePage->protocol==MODBUS) //如果为modbus的话解析写指令
|
|
@@ -160,8 +116,8 @@ void addDevice(char *deviceString, NODE_PARAMS *node)
|
|
|
paramString++;
|
|
|
if (paramString[0] == ']')
|
|
|
{
|
|
|
- paramString = (char *)deviceString;
|
|
|
- break;
|
|
|
+ paramString = (char *)deviceString;// 复原指针位置
|
|
|
+ break; // 找到了结束符,跳出循环
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -169,13 +125,13 @@ void addDevice(char *deviceString, NODE_PARAMS *node)
|
|
|
// 解析下发的mqtt存储信息下发控制指令
|
|
|
|
|
|
// 添加设备页到链表末尾
|
|
|
- if (node->device_params == NULL)
|
|
|
+ if (gateway_config_params.device_params == NULL)
|
|
|
{
|
|
|
- node->device_params = newDevicePage;
|
|
|
+ gateway_config_params.device_params = newDevicePage;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- DEVICE_PARAMS *current = node->device_params;
|
|
|
+ DEVICE_PARAMS *current = gateway_config_params.device_params;
|
|
|
while (current->nextDevice != NULL)
|
|
|
{
|
|
|
current = current->nextDevice;
|
|
@@ -197,7 +153,7 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
|
|
|
case DLT645_97:
|
|
|
case DLT645_07:
|
|
|
{
|
|
|
- NODE_READ_DLT645_COMMAND *read_dlt645_command = malloc(sizeof(NODE_READ_DLT645_COMMAND));
|
|
|
+ GATEWAY_READ_DLT645_COMMAND *read_dlt645_command = malloc(sizeof(GATEWAY_READ_DLT645_COMMAND));
|
|
|
read_dlt645_command->Identification = parseIntField(paramString, "\"identifier645\":");
|
|
|
parseStringField(paramString, "\"identifier\":\"", (char *)&read_dlt645_command->keyword);
|
|
|
char *string = mymalloc(SRAMIN, 13);
|
|
@@ -209,13 +165,13 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
|
|
|
read_dlt645_command->deviceID645[j]=byte;
|
|
|
}
|
|
|
myfree(SRAMIN, string);
|
|
|
- if (device->params->node_read_dlt645_command == NULL)
|
|
|
+ if (device->params->gateway_read_dlt645_command == NULL)
|
|
|
{
|
|
|
- device->params->node_read_dlt645_command = read_dlt645_command;
|
|
|
+ device->params->gateway_read_dlt645_command = read_dlt645_command;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- NODE_READ_DLT645_COMMAND *current = device->params->node_read_dlt645_command;
|
|
|
+ GATEWAY_READ_DLT645_COMMAND *current = device->params->gateway_read_dlt645_command;
|
|
|
while (current->nextParams != NULL)
|
|
|
{
|
|
|
current = current->nextParams;
|
|
@@ -223,23 +179,25 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
|
|
|
current->nextParams = read_dlt645_command;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
break;
|
|
|
case MODBUS:
|
|
|
{
|
|
|
- NODE_READ_MODBUS_COMMAND *read_modbus_command = malloc(sizeof(NODE_READ_MODBUS_COMMAND));
|
|
|
+ GATEWAY_READ_MODBUS_COMMAND *read_modbus_command = malloc(sizeof(GATEWAY_READ_MODBUS_COMMAND));
|
|
|
parseStringField(paramString,"\"identifier\":\"",(char *)&read_modbus_command->keyword);
|
|
|
read_modbus_command->decimalPoint = parseIntField(paramString, "\"precise\":");
|
|
|
read_modbus_command->functionCode = parseIntField(paramString, "\"rFunctionCode\":");
|
|
|
read_modbus_command->slaveAddress = parseIntField(paramString, "\"slaveAddress\":");
|
|
|
read_modbus_command->registerAddress = parseIntField(paramString, "\"registerAddress\":");
|
|
|
read_modbus_command->registerByteNum = parseIntField(paramString, "\"registerByteNum\":");
|
|
|
- if (device->params->node_read_modbus_command == NULL)
|
|
|
+
|
|
|
+ if (device->params->gateway_read_modbus_command== NULL)
|
|
|
{
|
|
|
- device->params->node_read_modbus_command = read_modbus_command;
|
|
|
+ device->params->gateway_read_modbus_command = read_modbus_command;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- NODE_READ_MODBUS_COMMAND *current = device->params->node_read_modbus_command;
|
|
|
+ GATEWAY_READ_MODBUS_COMMAND *current = device->params->gateway_read_modbus_command;
|
|
|
while (current->nextParams != NULL)
|
|
|
{
|
|
|
current = current->nextParams;
|
|
@@ -260,19 +218,20 @@ void addSensorListParams(char *paramString, DEVICE_PARAMS *device)
|
|
|
void addCommandListParams(char *paramString, DEVICE_PARAMS *device)
|
|
|
{
|
|
|
|
|
|
- NODE_WRITE_MODBUS_COMMAND *write_modbus_command=malloc(sizeof(NODE_WRITE_MODBUS_COMMAND));
|
|
|
+ GATEWAY_WRITE_MODBUS_COMMAND *write_modbus_command=malloc(sizeof(GATEWAY_WRITE_MODBUS_COMMAND));
|
|
|
parseStringField(paramString,"\"identifier\":\"",(char *)&write_modbus_command->keyword);
|
|
|
|
|
|
write_modbus_command->functionCode=parseIntField(paramString, "\"wFunctionCode\":");
|
|
|
write_modbus_command->slaveAddress=parseIntField(paramString, "\"slaveAddress\":");
|
|
|
write_modbus_command->registerAddress = parseIntField(paramString, "\"registerAddress\":");
|
|
|
- if(device->params->node_write_modbus_command == NULL)
|
|
|
+ write_modbus_command->registerByteNum = parseIntField(paramString, "\"registerByteNum\":");
|
|
|
+ if(device->params->gateway_write_modbus_command == NULL)
|
|
|
{
|
|
|
- device->params->node_write_modbus_command=write_modbus_command;
|
|
|
+ device->params->gateway_write_modbus_command=write_modbus_command;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- NODE_WRITE_MODBUS_COMMAND *current=device->params->node_write_modbus_command;
|
|
|
+ GATEWAY_WRITE_MODBUS_COMMAND *current=device->params->gateway_write_modbus_command;
|
|
|
while (current->nextParams != NULL)
|
|
|
{
|
|
|
current = current->nextParams;
|