16
16
#include <homekit/characteristics.h>
17
17
#include <string.h>
18
18
#include "lwip/api.h"
19
+ #include <udplogger.h>
19
20
20
- //use nc -kulnw0 34567 to collect this output
21
- #define LOG (message , ...) sprintf(string+strlen(string),message, ##__VA_ARGS__)
22
- char string [1450 ]= {0 }; //in the end I do not know to prevent overflow, so I use the max size of 1 UDP packet
23
- void log_send (void * pvParameters ){
24
- struct netconn * conn ;
25
- int i = 0 ,len ;
26
-
27
- while (sdk_wifi_station_get_connect_status () != STATION_GOT_IP ) vTaskDelay (20 ); //Check if we have an IP every 200ms
28
- // Create UDP connection
29
- conn = netconn_new (NETCONN_UDP );
30
- if (netconn_bind ( conn , IP_ADDR_ANY , 33333 ) != ERR_OK ) netconn_delete (conn );
31
- if (netconn_connect (conn , IP_ADDR_BROADCAST , 34567 ) != ERR_OK ) netconn_delete (conn );
32
-
33
- while (1 ){
34
- len = strlen (string );
35
- if ((!i && len ) || len > 1000 ) {
36
- struct netbuf * buf = netbuf_new ();
37
- void * data = netbuf_alloc (buf ,len );
38
- memcpy (data ,string ,len );
39
- string [0 ]= 0 ; //there is a risk of new LOG to add to string after we measured len
40
- if (netconn_send (conn , buf ) == ERR_OK ) netbuf_delete (buf );
41
- i = 10 ;
42
- }
43
- if (!i ) i = 10 ; //sends output every 100ms if not more than 1000 bytes
44
- i -- ;
45
- vTaskDelay (1 ); //with len>1000 and delay=10ms, we might handle 800kbps throughput
46
- }
47
- }
48
- //==================== END OF UDP LOG COLLECTOR ===================
21
+ #ifndef VERSION
22
+ #error You must set VERSION=x.y.z to match github version tag x.y.z
23
+ #endif
49
24
50
25
//hardcoded CRC16_MODBUS
51
26
char _open []= {0x03 ,0x01 ,0xb9 ,0x24 }; //n=4
@@ -74,7 +49,7 @@ char _setdir1[]={0x02,0x03,0x01,0x01,0x13,0xe7}; //n=6
74
49
#define _setdir1_n 6
75
50
char _setpos []= {0x03 ,0x04 ,0x00 ,0x00 ,0x00 }; //n=5 still needs value and CRC filled in
76
51
#define _setpos_n 5
77
- #define SEND (message ) do {LOG (#message "\n"); \
52
+ #define SEND (message ) do {UDPLUO (#message "\n"); \
78
53
taskENTER_CRITICAL(); \
79
54
memcpy(order.chars, _ ## message, _ ## message ## _n); \
80
55
order.len=_ ## message ## _n; \
@@ -159,23 +134,23 @@ homekit_value_t calibrate_get() {
159
134
}
160
135
void calibrate_set (homekit_value_t value ) {
161
136
if (value .format != homekit_format_bool ) {
162
- LOG ("Invalid calibrated-value format: %d\n" , value .format );
137
+ UDPLUO ("Invalid calibrated-value format: %d\n" , value .format );
163
138
return ;
164
139
}
165
140
calibrated = value .bool_value ;
166
- LOG ("Calibrate: %d\n" , calibrated );
141
+ UDPLUO ("Calibrate: %d\n" , calibrated );
167
142
if ( calibrated ) xTaskCreate (calibrate_task , "calibrated" , 256 , NULL , 1 , NULL );
168
143
else {SEND (uncal ); SEND (reqpos );}
169
144
}
170
145
171
146
172
147
void reverse_set (homekit_value_t value ) {
173
148
if (value .format != homekit_format_bool ) {
174
- LOG ("Invalid reversed-value format: %d\n" , value .format );
149
+ UDPLUO ("Invalid reversed-value format: %d\n" , value .format );
175
150
return ;
176
151
}
177
152
reversed = value .bool_value ;
178
- LOG ("Reverse: %d\n" , reversed );
153
+ UDPLUO ("Reverse: %d\n" , reversed );
179
154
if (reversed ) SEND (setdir1 ); else SEND (setdir0 );
180
155
SEND (reqdir );
181
156
SEND (reqcal );
@@ -201,20 +176,20 @@ homekit_value_t hold_get() {
201
176
}
202
177
void hold_set (homekit_value_t value ) {
203
178
if (value .format != homekit_format_bool ) {
204
- LOG ("Invalid hold-value format: %d\n" , value .format );
179
+ UDPLUO ("Invalid hold-value format: %d\n" , value .format );
205
180
return ;
206
181
}
207
- LOG ("H:%3d\n" ,value .bool_value );
182
+ UDPLUO ("H:%3d\n" ,value .bool_value );
208
183
hold = value .bool_value ;
209
184
}
210
185
211
186
212
187
void target_set (homekit_value_t value ) {
213
188
if (value .format != homekit_format_uint8 ) {
214
- LOG ("Invalid target-value format: %d\n" , value .format );
189
+ UDPLUO ("Invalid target-value format: %d\n" , value .format );
215
190
return ;
216
191
}
217
- LOG ("T:%3d\n" ,value .int_value );
192
+ UDPLUO ("T:%3d\n" ,value .int_value );
218
193
219
194
uint crc = 0xFFFF ;
220
195
int i ,j ;
@@ -244,7 +219,7 @@ void target_set(homekit_value_t value) {
244
219
// }
245
220
246
221
void identify (homekit_value_t _value ) {
247
- LOG ("Identify\n" );
222
+ UDPLUO ("Identify\n" );
248
223
// xTaskCreate(identify_task, "identify", 256, NULL, 2, NULL);
249
224
}
250
225
@@ -255,15 +230,15 @@ void sender_task(void *pvParameters){
255
230
int i ,attempt ;
256
231
struct _order ordr ;
257
232
258
- if ( senderQueue == 0 ) {LOG ("NO SEND QUEUE!\n" );vTaskDelete (NULL );}
233
+ if ( senderQueue == 0 ) {UDPLUO ("NO SEND QUEUE!\n" );vTaskDelete (NULL );}
259
234
ulTaskNotifyTake ( pdTRUE , 0 );
260
235
while (1 ) {
261
236
if ( xQueueReceive ( senderQueue , (void * )& ordr , (TickType_t ) portMAX_DELAY ) ) {
262
237
attempt = 3 ;
263
238
do {uart_putc (1 ,0x55 );uart_putc (1 ,0xfe );uart_putc (1 ,0xfe );
264
239
for (i = 0 ;i < ordr .len ;i ++ ) uart_putc (1 ,ordr .chars [i ]);
265
240
uart_flush_txfifo (1 );
266
- for (i = 0 ;i < ordr .len ;i ++ ) LOG ("%02x" ,ordr .chars [i ]); LOG (" sent\n" );
241
+ for (i = 0 ;i < ordr .len ;i ++ ) UDPLUO ("%02x" ,ordr .chars [i ]); UDPLUO (" sent\n" );
267
242
if (ulTaskNotifyTake ( pdTRUE , CONFIRM_TIMEOUT )== pdTRUE ) break ; //semafore to signal a response received
268
243
} while (-- attempt );
269
244
}
@@ -287,7 +262,7 @@ void report_task(void *pvParameters){
287
262
bool obstructed ;
288
263
struct _report rep ;
289
264
290
- if ( reportQueue == 0 ) {LOG ("NO REPORT QUEUE!\n" );vTaskDelete (NULL );}
265
+ if ( reportQueue == 0 ) {UDPLUO ("NO REPORT QUEUE!\n" );vTaskDelete (NULL );}
291
266
while (1 ) {
292
267
if ( xQueueReceive ( reportQueue , (void * )& rep , (TickType_t ) timer ) ) {
293
268
obstructed = false; timer = 100 ;
@@ -306,8 +281,8 @@ void report_task(void *pvParameters){
306
281
307
282
if (rep .position == 0xff ) aware = 0 ; else aware = 1 ;
308
283
309
- LOG ("pos=%02x,dir=%02x,sta=%02x,cal=%02x," ,rep .position ,rep .direction ,rep .status ,rep .calibr );
310
- LOG ("state=%d, obstructed=%d\n" ,state .value .int_value ,obstruction .value .bool_value );
284
+ UDPLUO ("pos=%02x,dir=%02x,sta=%02x,cal=%02x," ,rep .position ,rep .direction ,rep .status ,rep .calibr );
285
+ UDPLUO ("state=%d, obstructed=%d\n" ,state .value .int_value ,obstruction .value .bool_value );
311
286
}
312
287
SEND (reqcal ); SEND (reqpos ); SEND (reqsta );
313
288
}
@@ -319,9 +294,9 @@ int idx=0;
319
294
320
295
void parse (int positions ) {
321
296
int i = 0 ;
322
- if (positions < 4 ) LOG ("%02x%02x\n" ,buff [0 ],buff [1 ]);
297
+ if (positions < 4 ) UDPLUO ("%02x%02x\n" ,buff [0 ],buff [1 ]);
323
298
else {
324
- for (i = 3 ;i < positions - 2 ;i ++ ) LOG ("%02x" ,buff [i ]); LOG ("\n" );
299
+ for (i = 3 ;i < positions - 2 ;i ++ ) UDPLUO ("%02x" ,buff [i ]); UDPLUO ("\n" );
325
300
326
301
if (buff [3 ]== 0x03 && buff [4 ]== 0x01 ) xTaskNotifyGive ( SendTask ); // open confirmation
327
302
if (buff [3 ]== 0x03 && buff [4 ]== 0x02 ) xTaskNotifyGive ( SendTask ); //close confirmation
@@ -406,11 +381,11 @@ void uart_parse_input(void *pvParameters) {
406
381
//int i;
407
382
for (;;) {
408
383
buff [idx ++ ]= uart_getc (0 );
409
- //for (i=1;i<idx;i++) LOG (" ");
410
- //LOG ("v%d\n",idx);
384
+ //for (i=1;i<idx;i++) UDPLUO (" ");
385
+ //UDPLUO ("v%d\n",idx);
411
386
while (idx ){
412
- //for (i=0;i<16;i++) LOG ("%02x.",buff[i]);
413
- //LOG (" %d\n",idx);
387
+ //for (i=0;i<16;i++) UDPLUO ("%02x.",buff[i]);
388
+ //UDPLUO (" %d\n",idx);
414
389
if (!(buff [0 ]== 0x88 || buff [0 ]== 0x55 )) { shift_buff (1 ); continue ;}
415
390
if (buff [0 ]== 0x88 && buff [1 ]== 0xf8 ) {parse (2 ); shift_buff (2 ); continue ;}
416
391
if (buff [0 ]== 0x88 && idx >=2 ) { shift_buff (1 ); continue ;}
@@ -436,7 +411,7 @@ void uart_parse_input(void *pvParameters) {
436
411
if (!crc16 (16 )) parse (16 );
437
412
shift_buff (16 ); continue ;
438
413
} //BUG FIX these shifts for failures should be 1 only
439
- if (idx == 16 ) LOG ("something went wrong: idx=16!\n" );
414
+ if (idx == 16 ) UDPLUO ("something went wrong: idx=16!\n" );
440
415
break ;
441
416
}
442
417
}
@@ -503,8 +478,8 @@ homekit_server_config_t config = {
503
478
};
504
479
505
480
void user_init (void ) {
506
- xTaskCreate ( log_send , "logsend" , 256 , NULL , 4 , NULL ); //is prio4 a good idea??
507
- LOG ( "Aqara Curtain Motor\n" );
481
+ udplog_init ( 2 );
482
+ UDPLUS ( "\n\n\nAqara Curtain Motor " VERSION " \n" );
508
483
509
484
motor_init ();
510
485
0 commit comments