This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshempserver.c
executable file
·267 lines (205 loc) · 6.33 KB
/
shempserver.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* shempserver.c
*
* Created on: Jul 25, 2012
* Author: nabercro
*/
#include "shempserver.h"
//#define DEBUG_ENCODE
const uint8_t SERIAL_NUMBER[] = "111111"; // This is hardcoded serial number
#ifdef DEBUG_ENCODE
uint16_t test_buffer_encode[100] = {
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
};
uint16_t test_itor_encode = 0;
#endif
uint8_t output_buffer[OUTPUT_BUFFER_SIZE]; //These are not initialized to zero on Ti compilers
static uint16_t write_index;
static uint16_t read_index;
uint8_t reset_output_flag;
uint8_t hold_transmit_flag;
void init_transmits() {
int i;
for (i=0; i<OUTPUT_BUFFER_SIZE; i++) {
output_buffer[i] = 0;
}
//d//debug_push(d_init_transmits);
write_index = 0;
read_index = 0;
reset_output_flag = FALSE;
hold_transmit_flag = FALSE;
//d//debug_pop();
}
void reset_output_buffer() {
write_index = 0;
read_index = 0;
}
// Size Calculation
// Lxx 3 for length
// Tx 2 for type
// lx 2 for loc
// t+14 15 for time
// P+14 15 for period
// Cxx 3 for count
// Dxx... 1 + count*2 for data
// X 1 for end
#define HEADER_SIZE 42
uint8_t encode_data_for_transmit(node_ref args) {
uint8_t ret = FAILURE;
for(;;) {
if(!args) break;
sensor_ref s = (sensor_ref)node_get_val(args);
if(!s) break;
uint16_t cur_size = 0;
int16_t * array = sensor_get_data_array(s);
uint16_t temp_ptr = write_index;
cur_size = sensor_get_size(s)*sizeof(uint16_t) + HEADER_SIZE;
if(cur_size > OUTPUT_BUFFER_SIZE) return FAILURE; // Not ever going to fit
if(temp_ptr + cur_size > OUTPUT_BUFFER_SIZE - write_index) { //If the output_buffer has no space for msg then return failure.
// Cannot fit it
// temp_ptr = 0;
// read_index = 0;
// write_index = 0;
return FAILURE;
}
#ifdef DEBUG_ENCODE
test_buffer_encode[test_itor_encode] = s->end_time->milliseconds;
test_itor_encode++;
if (test_itor_encode == 99) {
return FAILURE;
}
#endif
output_buffer[temp_ptr++] = 'L';
write_2_bytes_to_string(&output_buffer[temp_ptr], cur_size);
temp_ptr += 2;
output_buffer[temp_ptr++] = 'T';
output_buffer[temp_ptr++] = sensor_get_type(s);
output_buffer[temp_ptr++] = 'l';
output_buffer[temp_ptr++] = sensor_get_loc(s);
output_buffer[temp_ptr++] = 't'; //time
write_time_to_string(&output_buffer[temp_ptr], sensor_get_end_time(s));
// write_count_to_string(&output_buffer[temp_ptr]);
temp_ptr += STRING_TO_TIME_LENGTH;
output_buffer[temp_ptr++] = 'P'; //period
write_time_to_string(&output_buffer[temp_ptr], sensor_get_period(s));
temp_ptr += STRING_TO_TIME_LENGTH;
output_buffer[temp_ptr++] = 'C';
write_2_bytes_to_string(&output_buffer[temp_ptr], sensor_get_size(s));
temp_ptr += 2;
output_buffer[temp_ptr++] = 'D';
//data
memcpy(&output_buffer[temp_ptr], array, sensor_get_size(s)*sizeof(uint16_t));
temp_ptr += sensor_get_size(s) * 2;
output_buffer[temp_ptr++] = 'X'; // end
write_index = temp_ptr;
ret = SUCCESS;
break;
}
return ret;
}
uint8_t encode_old_data_for_transmit(node_ref args) {
uint8_t ret = FAILURE;
for(;;) {
if(!args) break;
sensor_ref s = (sensor_ref)node_get_val(args);
if(!s) break;
uint16_t cur_size = 0;
int16_t * array = sensor_get_old_array(s);
uint16_t temp_ptr = write_index;
cur_size = sensor_get_size(s)*sizeof(uint16_t) + HEADER_SIZE;
if(cur_size > OUTPUT_BUFFER_SIZE) return FAILURE; // Not ever going to fit
if(temp_ptr + cur_size > OUTPUT_BUFFER_SIZE - write_index) { //If the output_buffer has no space for msg then return failure.
return FAILURE;
}
output_buffer[temp_ptr++] = 'L';
write_2_bytes_to_string(&output_buffer[temp_ptr], cur_size);
temp_ptr += 2;
output_buffer[temp_ptr++] = 'T';
output_buffer[temp_ptr++] = sensor_get_type(s);
output_buffer[temp_ptr++] = 'l';
output_buffer[temp_ptr++] = sensor_get_loc(s);
output_buffer[temp_ptr++] = 't'; //time
write_time_to_string(&output_buffer[temp_ptr], sensor_get_end_time(s));
temp_ptr += STRING_TO_TIME_LENGTH;
output_buffer[temp_ptr++] = 'P'; //period
write_time_to_string(&output_buffer[temp_ptr], sensor_get_period(s));
temp_ptr += STRING_TO_TIME_LENGTH;
output_buffer[temp_ptr++] = 'C';
write_2_bytes_to_string(&output_buffer[temp_ptr], sensor_get_size(s));
temp_ptr += 2;
output_buffer[temp_ptr++] = 'D';
//data
memcpy(&output_buffer[temp_ptr], array, sensor_get_size(s)*sizeof(uint16_t));
temp_ptr += sensor_get_size(s) * 2;
output_buffer[temp_ptr++] = 'X'; // end
write_index = temp_ptr;
ret = SUCCESS;
break;
}
return ret;
}
uint8_t have_data_to_transmit() {
if(reset_output_flag) return FALSE;
if (read_index != write_index) return TRUE;
return FALSE;
}
void hold_transmits() {
hold_transmit_flag = TRUE;
}
void continue_transmits() {
hold_transmit_flag = FALSE;
}
uint8_t transmit_data() {
if(!have_data_to_transmit()) {
return SUCCESS;
}
if(hold_transmit_flag) return SUCCESS;
reset_ack();
uint16_t end_ptr = write_index;
while (have_data_to_transmit()) {
uart_send_array(&output_buffer[read_index], end_ptr-read_index);
if(wait_for(&have_ack, 500)) {
read_index = end_ptr;
}
else {
return FAILURE;
}
end_ptr = write_index;
}
reset_output_buffer();
return SUCCESS;
}
#define HEADER_LENGTH 28 // 0x1C00
#define HEADER_LENGTH_PTR 1
#define HEADER_TIME_PTR 13
#define HEADER_SERIAL_PTR 6
#define HEADER_SERIAL_LENGTH 6
uint8_t transmit_header() {
uint8_t header[] = "L00THSxxxxxxt00000000000000X";
// Set up header
write_2_bytes_to_string(&header[HEADER_LENGTH_PTR], HEADER_LENGTH);
memcpy(&header[HEADER_SERIAL_PTR], SERIAL_NUMBER, HEADER_SERIAL_LENGTH);
reset_ack();
while(!have_ack()) {
write_time_to_string(&header[HEADER_TIME_PTR], global_time());
uart_send_array((uint8_t *)header, HEADER_LENGTH);
wait_for(&have_ack, 500);
}
return SUCCESS;
}
action_ref new_transmit_action(sensor_ref s) {
action_ref tx = new_action();
action_set_func(tx, &encode_data_for_transmit);
node_ref arg1 = new_node(s, 0);
action_set_args(tx, arg1);
return tx;
}