source: S-port/trunk/Core/Src/cmd_net.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 4.6 KB
Line 
1/****************************************************************************************
2*
3* cmd_net.c
4*
5* Ñåòü è MAC
6****************************************************************************************/
7
8#include <string.h>
9#include "commands.h"
10#include "main.h"
11#include "lwip.h"
12#include "plc.h"
13#include "snmp_msg.h"
14#include "log_and_debug.h"
15
16extern char community[16], trap_community[16];
17extern struct eth_addr MACAddr;
18extern network_settings table_network[MAX_IP];
19extern plc_data_typeDef PLC_Data[AMOUNT_BLOCK_PLC];
20extern struct netif gnetif;
21//extern char community[16];
22
23void cmd_getNet(json_tokens_data_t *tokens_data, json_response_t *json_response)
24{
25 char mac[17] = {0};
26 sprintf(mac, "%02X:%02X:%02X:%02X:%02X:%02X", table_network[0].netif.hwaddr[0], table_network[0].netif.hwaddr[1], table_network[0].netif.hwaddr[2], table_network[0].netif.hwaddr[3], table_network[0].netif.hwaddr[4], table_network[0].netif.hwaddr[5]);
27 json_write_obj_string("status", "ok");
28 json_write_obj_object("net");
29 json_write_obj_string("MAC", mac);
30 json_write_obj_string("ip", table_network[0].ipv4_addr);
31 json_write_obj_string("mask", table_network[0].ipv4_nm);
32 json_write_obj_string("gw", table_network[0].ipv4_gw);
33 json_write_end();
34}
35
36void cmd_setNet(json_tokens_data_t *tokens_data, json_response_t *json_response)
37{
38 if(json_get_value_object(tokens_data, "net", NULL, json_type_object, json_root)== json_success){ // ïðîâåðÿåì åñòü ëè îáúåêò
39 json_get_value_object(tokens_data, "ip", &table_network[0].ipv4_addr, json_type_string, json_obj);
40 json_get_value_object(tokens_data, "mask", &table_network[0].ipv4_nm, json_type_string, json_obj);
41 json_get_value_object(tokens_data, "gw", &table_network[0].ipv4_gw, json_type_string, json_obj);
42 LWIP_NetApply(&table_network[0]); // ïåðåâîäèì LWIP íà íîâûé àäðåñ
43 __logWrite("changing IP address to %s", table_network[0].ipv4_addr);
44 json_write_obj_string("status", "ok");
45 }
46 else {
47 json_write_obj_string("status", "error");
48 json_write_obj_string("error", "invalid parameter");
49 }
50}
51
52void cmd_getSnmp(json_tokens_data_t *tokens_data, json_response_t *json_response)
53{
54 uint8_t snmp_version = 0xFF;
55
56 if(snmp_v1_enabled()) snmp_version = SNMP_VERSION_1; // ýòî âåðñèÿ 1
57 else if(snmp_v2c_enabled()) snmp_version = SNMP_VERSION_2c; // ýòî âåðñèÿ 2c
58 //else if(snmp_v3_enabled()) snmp_version = SNMP_VERSION_3; // ýòî âåðñèÿ 3
59
60 json_write_obj_string("status", "ok");
61 json_write_obj_int("version", snmp_version);
62 json_write_obj_string("community", (char*)snmp_get_community());
63 json_write_obj_object("trap");
64 json_write_obj_bool("enable", snmp_get_traps_enable(0));
65 json_write_obj_string("community", (char*)snmp_get_community_trap());
66 json_write_obj_string("ip", (char*)snmp_get_ip_dst_trap(0));
67 json_write_end();
68
69}
70
71void cmd_setSnmp(json_tokens_data_t *tokens_data, json_response_t *json_response)
72{
73 _Bool ok = 0;
74 char trap_ip[16] = {0};
75 uint8_t trap_en = 0, snmp_version = 0xFF;
76 ip_addr_t ip;
77
78 snmp_v1_enable(DISABLE);
79 snmp_v2c_enable(DISABLE);
80 //snmp_v3_enable(DISABLE);
81
82 if(json_get_value_object(tokens_data, "version", &snmp_version, json_type_integer, json_root)== json_success){
83
84 if(snmp_version == SNMP_VERSION_1)snmp_v1_enable(ENABLE);
85 else if(snmp_version == SNMP_VERSION_2c)snmp_v2c_enable(ENABLE);
86 //else if(snmp_version == SNMP_VERSION_3)snmp_v3_enable(ENABLE);
87
88 if(json_get_value_object(tokens_data, "community", &community, json_type_string, json_root)== json_success) {
89 if(strncmp((char*)snmp_get_community(), community, strlen(community)) != 0) snmp_set_community(community);
90 ok = 1;
91 }
92
93 if(json_get_value_object(tokens_data, "trap", NULL, json_type_object, json_root)== json_success) {
94 json_get_value_object(tokens_data, "enable", &trap_en, json_type_boolean, json_obj);
95 json_get_value_object(tokens_data, "ip", &trap_ip, json_type_string, json_obj);
96 json_get_value_object(tokens_data, "community", &trap_community, json_type_string, json_obj);
97
98 if(strncmp((char*)snmp_get_community_trap(), trap_community, strlen(trap_community)) != 0) snmp_set_community_trap(trap_community);// èçìåíèëîñü
99
100 if(trap_en == 1){
101 ip4addr_aton(trap_ip, &ip);
102 snmp_trap_dst_ip_set(0, &ip);
103 snmp_trap_dst_enable(0, ENABLE);
104 }
105 else snmp_trap_dst_enable(0, DISABLE);
106 ok = 1;
107 }
108 }
109
110 if(ok == 1) json_write_obj_string("status", "ok");
111 else {
112 json_write_obj_string("status", "error");
113 json_write_obj_string("error", "invalid parameter");
114 }
115}
Note: See TracBrowser for help on using the repository browser.