fe4b9fabf8
- 注册应答先发后写Flash,重复注册参数无变化时跳过写入;SX1276发送前等待上一帧TxDone,防止应答帧被下一帧截断 - SPI Flash操作增加互斥锁(ReadPara/WritePara/AddLog/SavLogNum),修复多线程并发写坏参数镜像 - WritePara自行擦除参数区全部扇区并加越界保护;移除通用写函数中的日志簿记副作用(双擦+LastLogEndAddr污染) - 开机参数加载增加CRC校验与通讯单元表语义清洗;修复忽略ReadPara返回值导致坏镜像放行并被回写洗白的问题 - RS485Send函数指针每次开机重设,不再从Flash恢复旧固件代码地址 - Boot升级完成置APP_BOOT_UPGRADE_FLAG,App启动时仅擦外部参数分区(扇区0~1,历史日志保留)并恢复标志 - 参数SavFlag绑定PARA_LAYOUT_VER布局版本戳,布局变更自动回退默认参数 - res gxjt改为仅擦参数分区,不再触发全片扫描 - 全部源码注释统一UTF-8(无BOM),修复历史GBK/UTF-8混合编码及乱码字符 - 软件版本 1.2 -> 1.3
374 lines
13 KiB
C
374 lines
13 KiB
C
#ifndef __PUBLIC_H
|
|
#define __PUBLIC_H
|
|
|
|
#include "bsp.h"
|
|
|
|
#define LORA_LOWPOWER //LORA低功耗模式支持
|
|
|
|
#define CRC16_BASE 0xA001
|
|
|
|
#define SENSOR_DATA_LEN_MAX 50
|
|
#define SENSOR_NUM_MAX 16
|
|
#define COMMUNIT_NUM_MAX 32
|
|
|
|
#define REGISTER_INTERVAL_MAX (2 * 60)
|
|
#define COMMUNIT_READ_SENSOR_INTERVAL_MAX (1 * 60)
|
|
|
|
#define LORA_ERR_RESET_DLY_MAX (10 * 60) //LORA通信异常复位延迟时间
|
|
#define CAT1_ERR_RESET_DLY_MAX (60 * 60) //CAT1通信异常复位延迟时间
|
|
|
|
typedef struct GateWay_t GateWayPara_t, *GateWayPara;
|
|
|
|
typedef void (*SendData)(uint8_t *sData, uint16_t sLen);
|
|
typedef int (*DataRevCallBack)(GateWayPara GateWay, uint8_t *rData, uint16_t rLen);
|
|
typedef int (*DataRevResCallBack)(GateWayPara GateWay, uint8_t *rData, uint16_t rLen, SendData Response);
|
|
typedef int (*SendDataOrg)(uint8_t *sData);
|
|
|
|
typedef enum {
|
|
CH_NULL = -1,
|
|
CH_DBG = 0,
|
|
CH_RS485_1,
|
|
CH_RS485_2,
|
|
CH_ETH,
|
|
CH_LORA,
|
|
CH_CAT1,
|
|
} Channel_m;
|
|
|
|
typedef enum {
|
|
LORA_COMM,
|
|
RS485CH1_COMM,
|
|
RS485CH2_COMM
|
|
} SensorComm_m;
|
|
|
|
#define CUCH_CH1 (1 << CH_RS485_1)
|
|
#define CUCH_CH2 (1 << CH_RS485_2)
|
|
#define CUCH_ETH (1 << CH_ETH)
|
|
#define CUCH_LORA (1 << CH_LORA)
|
|
#define CUCH_CAT1 (1 << CH_CAT1)
|
|
|
|
#define ChannelIsActive(cfg, ch) ((cfg).Much == (ch) || (cfg).Auch == (ch) || (cfg).Duch == (ch) || ((cfg).CuchMask & (1 << (ch))))
|
|
#define ChannelMQ(gw, ch) ((gw).ChMQ[(ch)])
|
|
|
|
const char *ChannelName(Channel_m ch);
|
|
|
|
typedef enum {
|
|
DEV_TYPE_BROADCAST,
|
|
DEV_TYPE_INC_COMMUNIT = 0x8001,
|
|
DEV_TYPE_EXT_COMMUNIT,
|
|
DEV_TYPE_ICE_SENSOR,
|
|
DEV_TYPE_CRACK_SENSOR,
|
|
DEV_TYPE_PRESSURE_SENSOR,
|
|
DEV_TYPE_RAIN_SENSOR,
|
|
}DevType_m;
|
|
|
|
|
|
typedef enum {
|
|
COMM_UNIT_CMD_REG = 0x00,
|
|
COMM_UNIT_CMD_CAIL = 0x01,
|
|
COMM_UNIT_CMD_READ = 0x02,
|
|
COMM_UNIT_CMD_SET_SENSOR_COLL_TIME = 0x05,
|
|
COMM_UNIT_CMD_TIME_SYNC = 0x06,
|
|
COMM_UNIT_CMD_CONT_LASER = 0x08,//连续激光控制
|
|
COMM_UNIT_CMD_GET_SIGNAL = 0x09, //0x09 获取Lora信号质量
|
|
COMM_UNIT_CMD_SET_FPO_TIME = 0x0A, //0x0A 全功率时间配置
|
|
COMM_UNIT_CMD_END,
|
|
}CommUnitCmd_m;
|
|
|
|
typedef enum {
|
|
NET_COMM_CMD_REG,
|
|
NET_COMM_CMD_CAIL,
|
|
NET_COMM_CMD_UPDATE,
|
|
NET_COMM_CMD_READ_GW_INFO,
|
|
//NET_COMM_CMD_READ_UNIT_INFO,
|
|
NET_COMM_CMD_GW_PARA_CONFIG,
|
|
NET_COMM_CMD_HIS_DATA,
|
|
NET_COMM_CMD_ADD_UNIT,
|
|
NET_COMM_CMD_LP_DEV_CONFIG,
|
|
NET_COMM_CMD_ALARM,
|
|
NET_COMM_CMD_CONT_LASER,//连续激光控制
|
|
NET_COMM_CMD_FPO_CONFIG = 0x0A,//0x0A 全功率时间配置
|
|
NET_COMM_CMD_END,
|
|
}NetCommCmd_m;
|
|
|
|
typedef enum {
|
|
SENSOR_TYPE_NULL, //0x0000 空类型
|
|
FORCE_6D, //0x0001 六维力
|
|
OSMOTIC_PRESSURE, //0x0002 渗透压
|
|
ELASTIC_WAVEGUIDE, //0x0003 弹性波导
|
|
DIELECTRIC_MASS, //0x0004 介电质
|
|
VIBRATE_SENSOR, //0x0005 振动传感器
|
|
CONT_DEFOR_3D, //0x0006 三向连续变形
|
|
COMM_UNIT, //0x0007 通讯单元
|
|
FORCE_3D, //0x0008 三维力
|
|
LASER_TRACING, //0x0009 激光追踪
|
|
WATER_LEVEL, //0x000A 水位
|
|
_0x000B, //0x000B 位移
|
|
_0x000C, //0x000C 流量
|
|
MULTI_PARAMETER_FUSION, //0x000D 多参融合
|
|
CRACK_DETECTION, //0x000E 裂缝
|
|
_0x000F, //0x000F 应变
|
|
ATTITUDE_MONITOR, //0x0010 姿态监测
|
|
LASER_DISPLACE, //0x0011 激光位移
|
|
REBAR_STRESS, //0x0012 钢筋应力
|
|
ANCHOR_ROD_STRESS, //0x0013 锚杆应力
|
|
SURFACE_STRESS, //0x0014 表面应力
|
|
PRESSURE, //0x0015 压力
|
|
MICROWAVE_DISPLACE, //0x0016 微波位移
|
|
DISPLACEMENT = 0x0020, //0x0020 位移变形传感器
|
|
EWS_7 = 0x0021, //0x0021 七要素气象站,含温度湿度气压风速风向降雨光照
|
|
}SensorType_m;
|
|
|
|
typedef struct {//主设备信息
|
|
uint8_t BatLevel; //电池电量
|
|
int8_t RSSI; //信号强度
|
|
int8_t Nsr; //信噪比
|
|
}__attribute__ ((packed))MasterDev_T,*MasterDevDp;
|
|
|
|
typedef struct {
|
|
int16_t AccX;
|
|
int16_t AccY;
|
|
int16_t AccZ;
|
|
int32_t Strain1;
|
|
int32_t Strain2;
|
|
int32_t Strain3;
|
|
int32_t Strain4;
|
|
int32_t Strain5;
|
|
int32_t Strain6;
|
|
}__attribute__((packed))Force6D_T;
|
|
|
|
typedef struct {
|
|
MasterDev_T MasterDev;
|
|
int16_t PitchAngle; //俯仰角 范围-900~900,精度0.1度
|
|
int16_t RollAngle; //横滚角 范围-900~900,精度0.1度
|
|
int16_t YawAngle; //航向角 范围0~3600,精度0.1度
|
|
}__attribute__((packed))LaserTracingType_T;
|
|
|
|
typedef struct {//0x0020 位移
|
|
MasterDev_T MasterDev;
|
|
int16_t PitchAngle; //俯仰角范围-900~900,精度0.1度
|
|
int16_t RollAngle; //横滚角范围-900~900,精度0.1度
|
|
int16_t YawAngle; //航向角范围0~3600,精度0.1度
|
|
float Temp; //温度
|
|
float Altitude; //高程变化值,单位cm
|
|
int16_t Distance; //1次测量相对基准值偏差,范围-300,精度0.1mm
|
|
}__attribute__ ((packed))Displacement_T,*DisplacementDp;
|
|
|
|
typedef struct {//0x0021 七要素气象站数据
|
|
uint8_t Time[7]; //"时间24小时制,Byte[0]*100+Byte[1]为年份,Byte[2]为月份,Byte[3]为日期,Byte[4]为时,Byte[5]为分,Byte[6]"
|
|
int16_t Temperature; //温度
|
|
uint16_t Humidity; //湿度
|
|
uint16_t Pressure; //气压
|
|
uint16_t WindSpeed; //平均风速
|
|
uint16_t WindDirection; //平均风向
|
|
uint16_t WindSpeed1m; //过去1分钟风速
|
|
uint16_t WindDirection1m; //过去1分钟风向
|
|
uint16_t WindSpeed10m; //过去10分钟风速
|
|
uint16_t WindDirection10m; //过去10分钟风向
|
|
uint16_t MinRainfall; //最小降雨量
|
|
uint16_t HourRainfall; //小时降雨量
|
|
uint16_t DayRainfall; //日降雨量
|
|
uint32_t Light; //光照强度
|
|
}__attribute__ ((packed))EWS_7_T,*EWS_7_Dp;
|
|
|
|
typedef struct {
|
|
int Distance;
|
|
}__attribute__((packed))LaserDistanceSensorType_t;
|
|
|
|
typedef struct {
|
|
uint8_t CommDevAddr[6]; //通讯设备地址
|
|
uint16_t CollectInterval; //采集时间间隔
|
|
uint16_t ReportInterval; //上报时间间隔
|
|
uint8_t ERInterval; //紧急上报时间间隔
|
|
uint8_t ERTime; //紧急上报时长
|
|
}__attribute__((packed))SvrDownLPDevConfigPara_t;
|
|
|
|
typedef struct {
|
|
uint8_t CommDevAddr[6]; //通讯设备地址
|
|
uint8_t FPOTimeStart; //全功率工作时间起始点(0~23)
|
|
uint8_t FPOTime; //全功率工作时间时长(0~24)
|
|
}__attribute__((packed))SvrDownFPOConfigPara_t;
|
|
|
|
typedef struct {
|
|
uint8_t AlarmType;
|
|
uint8_t AlarmState;
|
|
uint8_t AlarmPara;
|
|
}__attribute__((packed))GateWayAlarmType_t;
|
|
|
|
#define LW_DEV_COLLECT_INTERVAL_MAX 300//默认300秒
|
|
#define LW_DEV_REPORT_INTERVAL_MAX 20//20分钟
|
|
#define LW_DEV_ER_INTERVAL_MAX 2//2分钟
|
|
#define LW_DEV_ER_TIME_MAX 20//20分钟
|
|
#define LW_DEV_FPO_TIME_START_DEFAULT 0//默认0点开始
|
|
#define LW_DEV_FPO_TIME_DEFAULT 0//默认0小时
|
|
|
|
typedef struct {
|
|
uint16_t CollectInterval; //采集时间间隔
|
|
uint16_t ReportInterval; //上报时间间隔
|
|
uint8_t ERInterval; //紧急上报时间间隔
|
|
uint8_t ERTime; //紧急上报时长
|
|
uint32_t TimeStamp; //时间戳
|
|
}__attribute__((packed))LPDevConfigPara_t;
|
|
|
|
typedef struct {
|
|
bool RegFlag;
|
|
bool RevNewDataFlag;
|
|
bool CommStatus; //通讯状态1-在线0-离线
|
|
bool CailFlag; //校准标志,需要校准
|
|
bool TimeSyncFlag; //时间同步标志,需要同步时间
|
|
bool ContLaser; //连续激光控制标志,需要控制
|
|
bool LaserOnOff; //激光开关状态,开关状态
|
|
bool FPOTimeFlag; //全功率时间配置等待下发标志
|
|
bool FPOTimeConfirmed; //全功率时间配置已确认标志(传感器回应后置true)
|
|
uint32_t CommErrCnt;
|
|
uint8_t MasterMac[6];
|
|
uint8_t Mac[SENSOR_NUM_MAX][6];//主设备MAC,传感器MAC列表
|
|
uint8_t BatLevel;
|
|
int8_t RSSI; //信号强度
|
|
int8_t Nsr; //信噪比
|
|
uint16_t CUType;
|
|
uint8_t SensorN;
|
|
uint16_t SensorType[SENSOR_NUM_MAX];
|
|
//低功耗设备相关参数
|
|
bool ConfigFlag; //配置标志位1表示需要配置,传感器上报数据时检查此标志并下发配置参数
|
|
uint16_t CollectInterval; //采集时间间隔
|
|
uint16_t ReportInterval; //上报时间间隔
|
|
uint8_t ERInterval; //紧急上报时间间隔
|
|
uint8_t ERTime; //紧急上报时长
|
|
uint8_t FPOTimeStart; //全功率工作时间起始点(0~23)(待配置/已配置)
|
|
uint8_t FPOTime; //全功率工作时间时长(0~24)(待配置/已配置)
|
|
uint8_t CurFPOTimeStart; //传感器当前已确认生效的全功率起始点(0~23)
|
|
uint8_t CurFPOTime; //传感器当前已确认生效的全功率时长(0~24)
|
|
}__attribute__((packed))CommUnitPara_t, *CommUnitPara;
|
|
|
|
typedef struct {
|
|
bool ExclFlag;
|
|
uint8_t ExclMac[6];//排除的通讯单元MAC
|
|
}__attribute__((packed))ExclCommUnit_t, *ExclCommUnit;
|
|
|
|
typedef struct {
|
|
uint8_t Data[SENSOR_NUM_MAX][SENSOR_DATA_LEN_MAX];
|
|
}__attribute__((packed))CommUnitData_t, *CommUnitData;
|
|
|
|
typedef struct {
|
|
CommUnitPara_t Para;
|
|
}__attribute__((packed))CommUnit_t, *CommUnit;
|
|
|
|
typedef struct {
|
|
bool Enable; //通道使能
|
|
bool CommUnitEnable; //通讯单元使能,用于RS485通道挂载通讯单元时使用,主设备为RS485传感器时此标志无效
|
|
bool QuerySensorFlag; //查询传感器标志
|
|
bool UpgradeEnable;//升级使能标志
|
|
uint8_t Power;
|
|
uint32_t BaudRate;
|
|
SendData RS485Send;
|
|
CommUnit_t CUPara;
|
|
CommUnitData_t CUData;
|
|
}__attribute__((packed))RS485Para_t, *RS485Para;
|
|
|
|
typedef struct {
|
|
uint8_t Header;
|
|
uint8_t GWMac[6];
|
|
uint8_t DevMac[6];
|
|
uint8_t Cmd;
|
|
//uint16_t DevType;
|
|
uint16_t PayloadLen;
|
|
}__attribute__((packed))CommUnitFrameHeader_t, *CommUnitFrameHeader;
|
|
|
|
typedef struct {
|
|
uint8_t Header;
|
|
uint8_t GWMac[6];
|
|
//uint8_t SvrMac[6];
|
|
uint8_t Cmd;
|
|
uint8_t BatLevel;
|
|
uint32_t Timestamp;
|
|
uint8_t PacketNum;
|
|
uint8_t PacketIdx;
|
|
uint16_t PayloadLen;
|
|
}__attribute__((packed))NetCommFrameHeader_t, *NetCommFrameHeader;
|
|
|
|
typedef struct {
|
|
bool OnOff;
|
|
SendData LoraSend;
|
|
uint8_t ucChannel;
|
|
int8_t ucPower;
|
|
uint8_t SignalBw;
|
|
uint8_t SpreadFactor;
|
|
uint8_t ErrorCoding;
|
|
uint8_t RegPreamble;
|
|
uint32_t FreqCent;
|
|
}LoraPara_t, *pLoraPara;
|
|
|
|
typedef struct {
|
|
uint8_t DestAddr[4]; //目标服务器地址
|
|
uint16_t DestPort; //目标服务器端口
|
|
uint8_t LocalAddr[4]; //本地地址(备用)
|
|
uint16_t LocalPort; //本地端口(备用)
|
|
}LTENetPara_t, *pLTENetPara;
|
|
|
|
typedef struct {
|
|
uint8_t IpMode; //IP模式DHCP/静态
|
|
uint8_t IpAddr[4]; //本地IP地址
|
|
uint16_t IpPort; //本地端口
|
|
uint8_t WorkMode; //工作模式
|
|
uint8_t SubnetMask[4]; //子网掩码
|
|
uint8_t Gateway[4]; //网关
|
|
uint8_t DestIp[4]; //目标IP地址
|
|
uint16_t DestPort; //目标端口
|
|
}EthNetPara_t, *pEthNetPara;
|
|
|
|
typedef struct {
|
|
uint32_t SavFlag; //存储标志
|
|
Channel_m Much; //主上传通道
|
|
Channel_m Auch; //辅助上传通道
|
|
Channel_m Duch; //调试通道/升级通道
|
|
uint8_t CuchMask; //通讯单元通道掩码
|
|
uint8_t GwMac[6]; //网关mac
|
|
LTENetPara_t LTENet; //Cat1/4G网络参数配置
|
|
EthNetPara_t EthNet; //以太网网络参数配置
|
|
uint32_t CommUnitReadInterval; //通讯单元读取间隔,单位S
|
|
CommUnitPara_t CommUnitArray[COMMUNIT_NUM_MAX]; //通讯单元参数数组
|
|
ExclCommUnit_t ExclCommUnit[COMMUNIT_NUM_MAX]; //排除的通讯单元列表
|
|
RS485Para_t Rs485Ch1; //RS485通道1参数
|
|
RS485Para_t Rs485Ch2; //RS485通道2参数
|
|
LoraPara_t Lora; //Lora参数
|
|
bool OutageFlag; //断电标志
|
|
uint16_t crc16;
|
|
}__attribute__((packed))GWConfigPara_t, *GWConfigPara;
|
|
|
|
struct GateWay_t{
|
|
bool TimeSyncFlag; //时间同步标志
|
|
bool MuchRegFlag; //主通道注册标志
|
|
bool AuchRegFlag; //辅助通道注册标志
|
|
uint8_t SvrMac[6]; //服务器mac
|
|
GWConfigPara_t ConfigPara; //网关配置参数
|
|
CommUnitData_t CUDataArray[COMMUNIT_NUM_MAX];
|
|
uint8_t Battery; //电池电量
|
|
uint16_t UploadInterval; //上传间隔
|
|
uint32_t HistoryNum; //历史数据数量
|
|
DataRevCallBack MuchRevCallBack; //主通道接收回调
|
|
DataRevCallBack AuchRevCallBack; //辅助通道接收回调
|
|
DataRevResCallBack CommUnitRevCallBack; //通讯单元接收回调
|
|
rt_mq_t ChMQ[6]; //各通道消息队列,索引=Channel_m
|
|
rt_mq_t LoraRev_MQ; //Lora接收消息队列,用于缓存接收数据避免丢失
|
|
rt_mutex_t UartRevMutex;
|
|
rt_mutex_t EthTxMutex;
|
|
|
|
uint8_t BatteryReadDlyCnt; //电池读取延迟计数,开机后延时读取,4G模块上电需要时间稳定
|
|
};
|
|
|
|
int MuchRevCallBack(GateWayPara GateWay, uint8_t *rData, uint16_t rLen);
|
|
int AuchRevCallBack(GateWayPara GateWay, uint8_t *rData, uint16_t rLen);
|
|
uint16_t CRC_Modbus(uint16_t wBase, __IO uint8_t *para, uint16_t length);
|
|
uint8_t AsciiToHex(char *ASCData, uint8_t *HexData, uint8_t sLen);
|
|
void HexToAscii(uint8_t *HexData, char *ASCData, uint8_t sLen);
|
|
int NetCommOrgData(GateWayPara GateWay, uint8_t *MegData, uint8_t *OutData);
|
|
int CommUnitAnalyze(GateWayPara GateWay, uint8_t *rData, uint16_t rLen, SendData Response);
|
|
void DebugAnalyze(GateWayPara GateWay, uint8_t *rData, uint16_t rLen);
|
|
void CommUnitCmdSend(GateWayPara GateWay, uint8_t *CommUnitMac, CommUnitCmd_m Cmd, uint8_t *Payload, uint16_t PayloadLen, SendData Send);
|
|
void TrySendFullPowerCmd(GateWayPara GateWay, int CommUnitIdx);
|
|
int CheckCommUnitReg(GateWayPara GateWay, uint8_t *CommUnitMac);
|
|
int CheckCommUnitExcl(GateWayPara GateWay, uint8_t *CommUnitMac);
|
|
int CommUintOrgData(GateWayPara GateWay, CommUnitPara CUPara, CommUnitData CUData, uint8_t *sData);
|
|
void DebugDisplaySensorData(int SensorIdx, uint16_t SensorType, uint8_t *SensorData);
|
|
#endif
|