forked from vedderb/bldc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
conf_custom.c
148 lines (118 loc) · 3.59 KB
/
conf_custom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright 2022 Benjamin Vedder benjamin@vedder.se
This file is part of the VESC firmware.
The VESC firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "conf_custom.h"
#include "datatypes.h"
#include "packet.h"
#include "mempools.h"
#include "buffer.h"
#include "utils_sys.h"
#include <string.h>
// Function pointers
static int (*m_get_cfg)(uint8_t *data, bool is_default) = 0;
static bool (*m_set_cfg)(uint8_t *data) = 0;
static int (*m_get_cfg_xml)(uint8_t **data) = 0;
void conf_custom_add_config(
int (*get_cfg)(uint8_t *data, bool is_default),
bool (*set_cfg)(uint8_t *data),
int (*get_cfg_xml)(uint8_t **data)) {
if (utils_is_func_valid(get_cfg) &&
utils_is_func_valid(set_cfg) &&
utils_is_func_valid(get_cfg_xml)) {
m_get_cfg = get_cfg;
m_set_cfg = set_cfg;
m_get_cfg_xml = get_cfg_xml;
}
}
void conf_custom_clear_configs(void) {
m_get_cfg = 0;
m_set_cfg = 0;
m_get_cfg_xml = 0;
}
int conf_custom_cfg_num(void) {
int res = 0;
if (m_get_cfg_xml) {
uint8_t *xml_data = 0;
m_get_cfg_xml(&xml_data);
if (utils_is_func_valid(xml_data)) {
res = 1;
}
}
return res;
}
int conf_custom_get_cfg_xml(int conf_ind, uint8_t **data) {
if (conf_ind != 0 || m_get_cfg_xml == 0) {
return 0;
}
return m_get_cfg_xml(data);
}
void conf_custom_process_cmd(unsigned char *data, unsigned int len,
void(*reply_func)(unsigned char *data, unsigned int len)) {
COMM_PACKET_ID packet_id;
packet_id = data[0];
data++;
len--;
switch (packet_id) {
case COMM_GET_CUSTOM_CONFIG:
case COMM_GET_CUSTOM_CONFIG_DEFAULT: {
int conf_ind = data[0];
if (m_get_cfg && conf_ind == 0) {
uint8_t *send_buffer = mempools_get_packet_buffer();
int32_t ind = 0;
send_buffer[ind++] = packet_id;
send_buffer[ind++] = conf_ind;
int32_t len_cfg = m_get_cfg(send_buffer + ind, packet_id == COMM_GET_CUSTOM_CONFIG_DEFAULT);
ind += len_cfg;
reply_func(send_buffer, ind);
mempools_free_packet_buffer(send_buffer);
}
} break;
case COMM_SET_CUSTOM_CONFIG: {
int conf_ind = data[0];
if (m_set_cfg && conf_ind == 0) {
m_set_cfg(data + 1);
int32_t ind = 0;
uint8_t send_buffer[50];
send_buffer[ind++] = packet_id;
reply_func(send_buffer, ind);
}
} break;
case COMM_GET_CUSTOM_CONFIG_XML: {
int32_t ind = 0;
int conf_ind = data[ind++];
if (conf_ind != 0 || m_get_cfg_xml == 0) {
break;
}
int32_t len_conf = buffer_get_int32(data, &ind);
int32_t ofs_conf = buffer_get_int32(data, &ind);
uint8_t *xml_data = 0;
int xml_len = m_get_cfg_xml(&xml_data);
if ((len_conf + ofs_conf) > xml_len || len_conf > (PACKET_MAX_PL_LEN - 10)) {
break;
}
uint8_t *send_buffer = mempools_get_packet_buffer();
ind = 0;
send_buffer[ind++] = packet_id;
send_buffer[ind++] = conf_ind;
buffer_append_int32(send_buffer, xml_len, &ind);
buffer_append_int32(send_buffer, ofs_conf, &ind);
memcpy(send_buffer + ind, xml_data + ofs_conf, len_conf);
ind += len_conf;
reply_func(send_buffer, ind);
mempools_free_packet_buffer(send_buffer);
} break;
default:
break;
}
}