1 | #include "commands.h"
|
---|
2 | #include "sntp.h"
|
---|
3 | #include "string.h"
|
---|
4 | #include "main.h"
|
---|
5 | #include "fatfs.h"
|
---|
6 | #include "lwip.h"
|
---|
7 | #include "plc.h"
|
---|
8 | #include "temp.h"
|
---|
9 | #include "snmp_msg.h"
|
---|
10 |
|
---|
11 | void cmd_get_device(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
12 | void cmd_plc_enable_all_pool(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
13 | void cmd_plc_blockparams(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
14 | void cmd_plc_getblock(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
15 | void cmd_plc_setblock(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
16 | void cmd_plc_getBlockslist(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
17 | void cmd_plc_alarm(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
18 | void cmd_plc_rmblock(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
19 | void cmd_getNet(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
20 | void cmd_setNet(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
21 | void cmd_getSnmp(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
22 | void cmd_setSnmp(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
23 | void cmd_getRTC(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
24 | void cmd_setRTC(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
25 | void cmd_getVersion(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
26 | void cmd_setNTP(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
27 | void cmd_setRTCcorr(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
28 | void cmd_getRTCcorr(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
29 | void cmd_login(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
30 | void cmd_restart(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
31 | void cmd_changePass(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
32 | void cmd_saveConfig(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
33 | void cmd_getTemp(json_tokens_data_t *tokens_data, json_response_t *json_response);
|
---|
34 |
|
---|
35 |
|
---|
36 | static descriptor cmdTable[] = {
|
---|
37 | {"getDevice", cmd_get_device, 0},
|
---|
38 | {"enableAllPool", cmd_plc_enable_all_pool, PERM_CHANGE},
|
---|
39 | {"PLCblockParams", cmd_plc_blockparams, 0},
|
---|
40 | {"PLCgetBlockslist", cmd_plc_getBlockslist, 0},
|
---|
41 | {"PLCsetBlock", cmd_plc_setblock, PERM_CHANGE},
|
---|
42 | {"PLCgetBlock", cmd_plc_getblock, 0},
|
---|
43 | {"PLCrmBlock", cmd_plc_rmblock, PERM_CHANGE},
|
---|
44 | {"PLCalarm", cmd_plc_alarm, 0},
|
---|
45 | {"getTemp", cmd_getTemp, 0},
|
---|
46 | {"getNet", cmd_getNet, 0},
|
---|
47 | {"setNet", cmd_setNet, PERM_CHANGE},
|
---|
48 | {"getSnmp", cmd_getSnmp, 0},
|
---|
49 | {"setSnmp", cmd_setSnmp, PERM_CHANGE},
|
---|
50 | {"setRTCcorr", cmd_setRTCcorr, 0},
|
---|
51 | {"setNTP", cmd_setNTP, PERM_CHANGE},
|
---|
52 | {"getRTCcorr", cmd_getRTCcorr, 0},
|
---|
53 | {"getRTC", cmd_getRTC, 0},
|
---|
54 | {"setRTC", cmd_setRTC, 0},
|
---|
55 | {"getVersion", cmd_getVersion, 0},
|
---|
56 | {"login", cmd_login, 0},
|
---|
57 | {"restart", cmd_restart, 0},
|
---|
58 | {"changePass", cmd_changePass, 0},
|
---|
59 | {"saveConfig", cmd_saveConfig, PERM_SAVE},
|
---|
60 | {NULL, NULL, 0}
|
---|
61 | };
|
---|
62 |
|
---|
63 | void execute_cmd(json_tokens_data_t *tokens_data, json_response_t *json_response, cookie_token_data_typDef *cookieIn, char *cookieOut)
|
---|
64 | {
|
---|
65 | char cmd[32] = {0};
|
---|
66 | json_write_open(json_response->json_data, json_response->json_data_length, json_object_type, JSON_WRITE_COMPACT);
|
---|
67 | if(json_get_value_object(tokens_data, "cmd", cmd, json_type_string, json_root)== json_success){
|
---|
68 | json_write_obj_string("cmd", cmd);
|
---|
69 | if(memcmp(cmd, "login", 5) != 0){
|
---|
70 | if(checkLogin(cookieIn, cookieOut) == 0){
|
---|
71 | json_write_obj_string("status", "error");// îøèáêà àóòåíòèôèêàöèè
|
---|
72 | json_write_obj_string("error", "not logged in");
|
---|
73 | json_write_close();
|
---|
74 | return;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | for(descriptor *desc = cmdTable; desc->command; desc++){
|
---|
78 | if(memcmp(cmd, desc->command, strlen(desc->command)) == 0){
|
---|
79 | desc->handler(tokens_data, json_response); // åñëè íåò àâòîðèçàöèè, ïðîïóñòèòü òîëüêî êîìàíäó login
|
---|
80 | json_write_close();
|
---|
81 | return;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | json_write_obj_string("status", "error");
|
---|
86 | json_write_obj_string("error", "unknown cmd");
|
---|
87 | json_write_close();
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | extern uint32_t FW_Version[2];
|
---|
92 |
|
---|
93 | void cmd_getVersion(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
94 | {
|
---|
95 | json_write_obj_string("status", "ok");
|
---|
96 | json_write_obj_int("ver", FW_Version[1]);
|
---|
97 | json_write_obj_int("subVer", FW_Version[0]);
|
---|
98 | }
|
---|
99 |
|
---|
100 | extern struct board *curr_device;
|
---|
101 |
|
---|
102 | void cmd_get_device(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
103 | {
|
---|
104 | json_write_obj_string("status", "ok");
|
---|
105 | json_write_obj_int("type", curr_device->type);
|
---|
106 | json_write_obj_string("name", (char*)curr_device->Name);
|
---|
107 | }
|
---|
108 |
|
---|
109 | void cmd_restart(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
110 | {
|
---|
111 | taskENTER_CRITICAL();
|
---|
112 | HAL_Delay(500);
|
---|
113 | NVIC_SystemReset();
|
---|
114 | }
|
---|
115 |
|
---|
116 | extern sensor_temp_typeDef Sensor_Temperature;
|
---|
117 |
|
---|
118 | void cmd_getTemp(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
119 | {
|
---|
120 | char buff[9] = {0};
|
---|
121 | json_write_obj_string("status", "ok");
|
---|
122 | if(Sensor_Temperature.presence){
|
---|
123 | sprintf(buff, "%c%d.%d C", Sensor_Temperature.sign ? '-' : ' ', Sensor_Temperature.info, Sensor_Temperature.share);
|
---|
124 | json_write_obj_string("temp", buff);
|
---|
125 | }
|
---|
126 | else json_write_obj_string("temp", "missing");
|
---|
127 | }
|
---|
128 |
|
---|
129 | extern plc_data_typeDef PLC_Data[AMOUNT_BLOCK_PLC];
|
---|
130 | extern network_settings table_network[MAX_IP];
|
---|
131 | extern plc_common_typeDef plc_common;
|
---|
132 | extern xSemaphoreHandle MutexAccessFlash;
|
---|
133 |
|
---|
134 | void cmd_saveConfig(json_tokens_data_t *tokens_data, json_response_t *json_response)
|
---|
135 | {
|
---|
136 | FIL FileConfig;
|
---|
137 | xSemaphoreTake(MutexAccessFlash, portMAX_DELAY);
|
---|
138 | if(f_open(&FileConfig, "Tconfig.xml", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK){
|
---|
139 | f_printf(&FileConfig, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
---|
140 | f_printf(&FileConfig, "<config>\n");
|
---|
141 | f_printf(&FileConfig, "\t<net>\n");
|
---|
142 | f_printf(&FileConfig, "\t\t<ip>%s</ip>\n", table_network[0].ipv4_addr);
|
---|
143 | f_printf(&FileConfig, "\t\t<netmask>%s</netmask>\n", table_network[0].ipv4_nm);
|
---|
144 | f_printf(&FileConfig, "\t\t<gateway>%s</gateway>\n", table_network[0].ipv4_gw);
|
---|
145 | if(sntp_getserver(0)->addr != 0) f_printf(&FileConfig, "\t\t<ntp>%s</ntp>\n", ip4addr_ntoa(sntp_getserver(0)));
|
---|
146 | f_printf(&FileConfig, "\t</net>\n");
|
---|
147 | f_printf(&FileConfig, "\t<snmp version=\"2c\">\n"); // òóò ñäåëàòü çàïèñü íàñòîÿùåé âåðñèè
|
---|
148 | f_printf(&FileConfig, "\t\t<community>%s</community>\n", (char*)snmp_get_community());
|
---|
149 | f_printf(&FileConfig, "\t\t<trap-enable>%d</trap-enable>\n", snmp_get_traps_enable(0));
|
---|
150 | f_printf(&FileConfig, "\t\t\t<trap-hostname>%s</trap-hostname>\n", (char*)snmp_get_ip_dst_trap(0));
|
---|
151 | f_printf(&FileConfig, "\t\t<trap-community>%s</trap-community>\n", (char*)snmp_get_community_trap());
|
---|
152 | f_printf(&FileConfig, "\t</snmp>\n");
|
---|
153 | f_printf(&FileConfig, "\t<blocks num=\"%d\">\n", plc_common.num);
|
---|
154 | for(uint8_t i = 0; i < plc_common.num; i++){
|
---|
155 | f_printf(&FileConfig, "\t\t<block id=\"%d\" name=\"%s\">\n", i, PLC_Data[i].NameBlock);
|
---|
156 | f_printf(&FileConfig, "\t\t\t<poll>%d</poll>\n", PLC_Data[i].PollingTimeout);
|
---|
157 | f_printf(&FileConfig, "\t\t\t<filter>%d,%d</filter>\n", PLC_Data[i].Type_Filter, PLC_Data[i].FilterATT/*, PLC_Data[i].Power_PRD*/);
|
---|
158 | if(i > 0){
|
---|
159 | f_printf(&FileConfig, "\t\t\t<route>%d,%d,%d,%d,%d,%d,%d,%d,%d</route>\n", PLC_Data[i].Route[0], PLC_Data[i].Route[1], PLC_Data[i].Route[2],
|
---|
160 | 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]);
|
---|
161 | f_printf(&FileConfig, "\t\t\t<ip>%s</ip>\n", table_network[i].ipv4_addr);
|
---|
162 | f_printf(&FileConfig, "\t\t\t<netmask>%s</netmask>\n", table_network[i].ipv4_nm);
|
---|
163 | f_printf(&FileConfig, "\t\t\t<gateway>%s</gateway>\n", table_network[i].ipv4_gw);
|
---|
164 | }
|
---|
165 | f_printf(&FileConfig, "\t\t</block>\n");
|
---|
166 | }
|
---|
167 | f_printf(&FileConfig, "\t</blocks>\n");
|
---|
168 | f_printf(&FileConfig, "</config>\n");
|
---|
169 | f_close(&FileConfig);
|
---|
170 | json_write_obj_string("status", "ok");
|
---|
171 | }
|
---|
172 | else {
|
---|
173 | json_write_obj_string("status", "error");
|
---|
174 | json_write_obj_string("error", "save failed");
|
---|
175 | }
|
---|
176 | f_unlink("config.xml");
|
---|
177 | f_rename("Tconfig.xml", "config.xml");
|
---|
178 | xSemaphoreGive(MutexAccessFlash);
|
---|
179 | }
|
---|