#include "commands.h" #include "sntp.h" #include "string.h" #include "main.h" #include "fatfs.h" #include "lwip.h" #include "plc.h" #include "temp.h" #include "snmp_msg.h" void cmd_get_device(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_plc_enable_all_pool(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_plc_blockparams(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_plc_getblock(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_plc_setblock(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_plc_getBlockslist(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_plc_alarm(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_plc_rmblock(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_getNet(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_setNet(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_getSnmp(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_setSnmp(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_getRTC(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_setRTC(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_getVersion(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_setNTP(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_setRTCcorr(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_getRTCcorr(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_login(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_restart(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_changePass(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_saveConfig(json_tokens_data_t *tokens_data, json_response_t *json_response); void cmd_getTemp(json_tokens_data_t *tokens_data, json_response_t *json_response); static descriptor cmdTable[] = { {"getDevice", cmd_get_device, 0}, {"enableAllPool", cmd_plc_enable_all_pool, PERM_CHANGE}, {"PLCblockParams", cmd_plc_blockparams, 0}, {"PLCgetBlockslist", cmd_plc_getBlockslist, 0}, {"PLCsetBlock", cmd_plc_setblock, PERM_CHANGE}, {"PLCgetBlock", cmd_plc_getblock, 0}, {"PLCrmBlock", cmd_plc_rmblock, PERM_CHANGE}, {"PLCalarm", cmd_plc_alarm, 0}, {"getTemp", cmd_getTemp, 0}, {"getNet", cmd_getNet, 0}, {"setNet", cmd_setNet, PERM_CHANGE}, {"getSnmp", cmd_getSnmp, 0}, {"setSnmp", cmd_setSnmp, PERM_CHANGE}, {"setRTCcorr", cmd_setRTCcorr, 0}, {"setNTP", cmd_setNTP, PERM_CHANGE}, {"getRTCcorr", cmd_getRTCcorr, 0}, {"getRTC", cmd_getRTC, 0}, {"setRTC", cmd_setRTC, 0}, {"getVersion", cmd_getVersion, 0}, {"login", cmd_login, 0}, {"restart", cmd_restart, 0}, {"changePass", cmd_changePass, 0}, {"saveConfig", cmd_saveConfig, PERM_SAVE}, {NULL, NULL, 0} }; void execute_cmd(json_tokens_data_t *tokens_data, json_response_t *json_response, cookie_token_data_typDef *cookieIn, char *cookieOut) { char cmd[32] = {0}; json_write_open(json_response->json_data, json_response->json_data_length, json_object_type, JSON_WRITE_COMPACT); if(json_get_value_object(tokens_data, "cmd", cmd, json_type_string, json_root)== json_success){ json_write_obj_string("cmd", cmd); if(memcmp(cmd, "login", 5) != 0){ if(checkLogin(cookieIn, cookieOut) == 0){ json_write_obj_string("status", "error");// ошибка аутентификации json_write_obj_string("error", "not logged in"); json_write_close(); return; } } for(descriptor *desc = cmdTable; desc->command; desc++){ if(memcmp(cmd, desc->command, strlen(desc->command)) == 0){ desc->handler(tokens_data, json_response); // если нет авторизации, пропустить только команду login json_write_close(); return; } } } json_write_obj_string("status", "error"); json_write_obj_string("error", "unknown cmd"); json_write_close(); } extern uint32_t FW_Version[2]; void cmd_getVersion(json_tokens_data_t *tokens_data, json_response_t *json_response) { json_write_obj_string("status", "ok"); json_write_obj_int("ver", FW_Version[1]); json_write_obj_int("subVer", FW_Version[0]); } extern struct board *curr_device; void cmd_get_device(json_tokens_data_t *tokens_data, json_response_t *json_response) { json_write_obj_string("status", "ok"); json_write_obj_int("type", curr_device->type); json_write_obj_string("name", (char*)curr_device->Name); } void cmd_restart(json_tokens_data_t *tokens_data, json_response_t *json_response) { taskENTER_CRITICAL(); HAL_Delay(500); NVIC_SystemReset(); } extern sensor_temp_typeDef Sensor_Temperature; void cmd_getTemp(json_tokens_data_t *tokens_data, json_response_t *json_response) { char buff[9] = {0}; json_write_obj_string("status", "ok"); if(Sensor_Temperature.presence){ sprintf(buff, "%c%d.%d C", Sensor_Temperature.sign ? '-' : ' ', Sensor_Temperature.info, Sensor_Temperature.share); json_write_obj_string("temp", buff); } else json_write_obj_string("temp", "missing"); } extern plc_data_typeDef PLC_Data[AMOUNT_BLOCK_PLC]; extern network_settings table_network[MAX_IP]; extern plc_common_typeDef plc_common; extern xSemaphoreHandle MutexAccessFlash; void cmd_saveConfig(json_tokens_data_t *tokens_data, json_response_t *json_response) { FIL FileConfig; xSemaphoreTake(MutexAccessFlash, portMAX_DELAY); if(f_open(&FileConfig, "Tconfig.xml", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK){ f_printf(&FileConfig, "\n"); f_printf(&FileConfig, "\n"); f_printf(&FileConfig, "\t\n"); f_printf(&FileConfig, "\t\t%s\n", table_network[0].ipv4_addr); f_printf(&FileConfig, "\t\t%s\n", table_network[0].ipv4_nm); f_printf(&FileConfig, "\t\t%s\n", table_network[0].ipv4_gw); if(sntp_getserver(0)->addr != 0) f_printf(&FileConfig, "\t\t%s\n", ip4addr_ntoa(sntp_getserver(0))); f_printf(&FileConfig, "\t\n"); f_printf(&FileConfig, "\t\n"); // тут сделать запись настоящей версии f_printf(&FileConfig, "\t\t%s\n", (char*)snmp_get_community()); f_printf(&FileConfig, "\t\t%d\n", snmp_get_traps_enable(0)); f_printf(&FileConfig, "\t\t\t%s\n", (char*)snmp_get_ip_dst_trap(0)); f_printf(&FileConfig, "\t\t%s\n", (char*)snmp_get_community_trap()); f_printf(&FileConfig, "\t\n"); f_printf(&FileConfig, "\t\n", plc_common.num); for(uint8_t i = 0; i < plc_common.num; i++){ f_printf(&FileConfig, "\t\t\n", i, PLC_Data[i].NameBlock); f_printf(&FileConfig, "\t\t\t%d\n", PLC_Data[i].PollingTimeout); f_printf(&FileConfig, "\t\t\t%d,%d\n", PLC_Data[i].Type_Filter, PLC_Data[i].FilterATT/*, PLC_Data[i].Power_PRD*/); if(i > 0){ f_printf(&FileConfig, "\t\t\t%d,%d,%d,%d,%d,%d,%d,%d,%d\n", PLC_Data[i].Route[0], PLC_Data[i].Route[1], PLC_Data[i].Route[2], PLC_Data[i].Route[3], PLC_Data[i].Route[4], PLC_Data[i].Route[5], PLC_Data[i].Route[6], PLC_Data[i].Route[7], PLC_Data[i].Route[8]); f_printf(&FileConfig, "\t\t\t%s\n", table_network[i].ipv4_addr); f_printf(&FileConfig, "\t\t\t%s\n", table_network[i].ipv4_nm); f_printf(&FileConfig, "\t\t\t%s\n", table_network[i].ipv4_gw); } f_printf(&FileConfig, "\t\t\n"); } f_printf(&FileConfig, "\t\n"); f_printf(&FileConfig, "\n"); f_close(&FileConfig); json_write_obj_string("status", "ok"); } else { json_write_obj_string("status", "error"); json_write_obj_string("error", "save failed"); } f_unlink("config.xml"); f_rename("Tconfig.xml", "config.xml"); xSemaphoreGive(MutexAccessFlash); }