直接上问题代码
typedef struct Node_DEV_INFO_ST
{
long long DevId; //所属环境主机ID
char NodeId[32]; //自己分配的传感器设备ID
char DeviceId[32]; //IOT平台返回的传感器设备ID,复用机器人deviceid
char DevName[32]; //端设备名称
char DevDesc[256]; //端设备描述
char mfgInfo[128]; //端设备厂商信息
char nodeModel[32]; //端设备模型
char monitorObj[64]; //监测对象(安装位置)
char nodeType[32]; //设备型号
char nodeCategory[32]; //通讯类别( 0:无线,1:有线)
char expirydate[4]; //使用年限
char modelId[32]; //端设备模型编号
char status[16]; //端设备状态
int controlFlag; //端设备控制标记
int sampleFreq; //数据上报频率
DevModbusInfo tModbusInfo;
long long lLastReportTime;//最新一次上报时间
bool bState; //传感器状态 0在线 1离线
int sampleCount; //上报计数器
list_t RelateCfgList; //关联配置链表
list_t list;
}NodeDevInfoSt;
//获取机器人的nodeId
void SS
P_RobotNodeIdeGet(char *nodeId)
{
if(NULL == nodeId)
{
return;
}
EnvDevInfoSt *pEnvDev = NULL;
NodeDevInfoSt *ptNodeDev = NULL;
pthread_rwlock_rdlock(&stDeviceMgr.DNClock);
list_for_each_entry(pEnvDev, &stDeviceMgr.EnvHostList, list)
{
list_for_each_entry(ptNodeDev, &pEnvDev->DevNodeCurList, list)
{
if(0 == strncmp(ptNodeDev->nodeModel, "TROB", sizeof(ptNodeDev->nodeModel)))
{
strncpy(nodeId, ptNodeDev->NodeId, sizeof(ptNodeDev->NodeId));
}
break;
}
}
pthread_rwlock_unlock(&stDeviceMgr.DNClock);
}
/***************************************************************************
function: ESP_RobotTaskGet
input:
output:
Description:iot下发机器人任务
****************************************************************************/
int ESP_RobotTaskSet(NodeDevInfoSt *pSensorNode, JsonInfoSt *pJsonInfo)
{
char sendBuf[1024*16 + ROBOT_MIN_PACKET_LEN] = {0};
char szSql[1024*16] = {0};
int rc = 0;
char *pcErrMsg = NULL;
sqlite3 *db = NULL;
char msg[10] = {0};
char code[8] = {0};
char Message[8] = {0};
char time[24] = {0};
char numStr[16] = {0};
char nodeId[20] = {0};
int rown = 0;
int coln = 0;
char **result;
robotTaskDetailSt tRobotTaskSt = {0};
//将任务写进数据库,不确认可否存下deviceList(最大16k)4000 * 5
//若机器人可以存储任务,则网关机侧不需要存本地
//因网关机需要在页面展示任务以及运行情况,所以需要存本地数据库
//新问题:如何保证平台,网关,机器人三者任务相同
rc = sqlite3_open(DATA_BASE_PATH, &db);
if(rc)
{
printf("opnen database failed.\n");
return SSP_ERR;
}
//若任务ID存在则更新该任务,不存在则新增此任务
if(SSP_ERR == pJsonInfotoRobotTaskSt(pJsonInfo, &tRobotTaskSt))
{
ESP_LOG(LOG_ERROR, "pJsonInfotoRobotTaskSt ERROR\n");
return SSP_ERR;
}
memset(szSql, 0, sizeof(szSql));
snprintf(szSql, sizeof(szSql), DB_SQL_ROBOT_TASK_SEARCH, tRobotTaskSt.TaskCode);
if(sqlite3_get_table(db, szSql, &result, &rown, &coln, &pcErrMsg) != SQLITE_OK)
{
rc = 1;
ESP_LOG(LOG_ERROR, "can not Get robot task info from database\n");
}
else
{
memset(szSql, 0, sizeof(szSql));
if(0 == rown)
{
snprintf(szSql, sizeof(szSql), DB_SQL_ROBOT_TASK_INSERT, \
tRobotTaskSt.Type, tRobotTaskSt.TaskCode, tRobotTaskSt.TaskName, \
tRobotTaskSt.Priority, tRobotTaskSt.DeviceLevel, pJsonInfo->deviceList, \
tRobotTaskSt.FixedStartTime, tRobotTaskSt.CycleMonth, tRobotTaskSt.CycleWeek, \
tRobotTaskSt.CycleExecuteTime, tRobotTaskSt.CycleStartTime, tRobotTaskSt.CycleEndTime, \
tRobotTaskSt.IntervalNumber, tRobotTaskSt.IntervalType, tRobotTaskSt.IntervalExecuteTime, \
tRobotTaskSt.IntervalStartTime, tRobotTaskSt.IntervalEndTime, tRobotTaskSt.InvalidStartTime, \
tRobotTaskSt.InvalidEndTime, tRobotTaskSt.IsEnable, tRobotTaskSt.Creator, \
tRobotTaskSt.CreateTime, 4);
}
else
{
snprintf(szSql, sizeof(szSql), DB_SQL_ROBOT_TASK_ATTR_UPDATE, \
tRobotTaskSt.Type, tRobotTaskSt.TaskName, \
tRobotTaskSt.Priority, tRobotTaskSt.DeviceLevel, pJsonInfo->deviceList, \
tRobotTaskSt.FixedStartTime, tRobotTaskSt.CycleMonth, tRobotTaskSt.CycleWeek, \
tRobotTaskSt.CycleExecuteTime, tRobotTaskSt.CycleStartTime, tRobotTaskSt.CycleEndTime, \
tRobotTaskSt.IntervalNumber, tRobotTaskSt.IntervalType, tRobotTaskSt.IntervalExecuteTime, \
tRobotTaskSt.IntervalStartTime, tRobotTaskSt.IntervalEndTime, tRobotTaskSt.InvalidStartTime, \
tRobotTaskSt.InvalidEndTime, tRobotTaskSt.IsEnable, tRobotTaskSt.Creator, \
tRobotTaskSt.CreateTime, 4, tRobotTaskSt.TaskCode);
}
rc = sqlite3_exec(db, szSql, 0, 0, &pcErrMsg);
}
ESP_LOG(LOG_ERROR, "tRobotTaskSt.TaskCode:%s\n", tRobotTaskSt.TaskCode);
if(NULL != pcErrMsg)
{
ESP_LOG(LOG_ERROR, "%s\n", pcErrMsg);
sqlite3_free(pcErrMsg);
}
sqlite3_free_table(result);
sqlite3_close(db);
SSP_GetLocalCurTime(time);
SSP_RobotNodeIdeGet(nodeId);
ESP_LOG(LOG_ERROR, "tRobotTaskSt.TaskCode:%s\n", tRobotTaskSt.TaskCode);
}
以上代码中,第一个ESP_LOG(LOG_ERROR, “tRobotTaskSt.TaskCode:%s\n”, tRobotTaskSt.TaskCode);打印tRobotTaskSt.TaskCode中是有值的。但第二个打印中tRobotTaskSt.TaskCode值丢失。
[20210906-13:00:50][ALL][OdyMqttClient.c][ESP_RobotTaskSet][2427]tRobotTaskSt.TaskCode:e4797b58775b4901b083054d1d088dcc
[20210906-13:00:50][ALL][OdyMqttClient.c][ESP_RobotTaskSet]
[2438]tRobotTaskSt.TaskCode:
经反复定位,原因为局部变量nodeId的长度不足导致。SSP_RobotNodeIdeGet接口中需要返回的nodeId为32字节,但局部变量定义时长度定义成20字节,导致后续局部变量在内存中被覆盖。修改 char nodeId[20] = {0};为 char nodeId[32] = {0};即可。问题解决。