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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
| #define SUM_BUFFER_SIZE 30000 #define TEMP_BUFFER_SIZE 512 #define MAX_DATA_PER_PACKET 505
typedef enum { STATE_IDLE, STATE_TYPE, STATE_PRINT, STATE_AUDIO } State_t;
uint8_t temp_buffer[TEMP_BUFFER_SIZE]; uint32_t temp_pos = 0; uint32_t data_sum_size = 0; uint8_t is_get_sum_size = 0; uint8_t data_type = 0; uint8_t text_buffer[SUM_BUFFER_SIZE]; uint32_t write_pos = 0; uint32_t read_pos = 0; uint32_t total_written = 0; State_t state = STATE_IDLE;
static int calculate_crc16(const uint8_t *data, uint16_t length) { uint16_t crc = 0x0000; for (uint16_t i = 0; i < length; i++) { crc ^= (uint16_t)data[i] << 8; for (uint8_t j = 0; j < 8; j++) { if (crc & 0x8000) crc = (crc << 1) ^ 0x8005; else crc <<= 1; } } return crc; }
static int validate_format(uint8_t *buffer, uint32_t pos) { if (pos < 7) return 1; uint16_t data_length = (buffer[2] << 8) | buffer[3]; if (data_length > TEMP_BUFFER_SIZE - 7) { LOG(LOG_LVL_ERROR, "Data length too big: %u\n", data_length); return -1; } uint16_t expected_pos = 4 + data_length + 2 + 1; if (pos < expected_pos) return 1; if (buffer[0] != 0xAA || buffer[expected_pos - 1] != 0xFF || pos != expected_pos) { LOG(LOG_LVL_ERROR, "Data format error: head=0x%02X, tail=0x%02X, pos=%u, expected=%u\n", buffer[0], buffer[pos - 1], pos, expected_pos); return -1; } return 2; }
static void send_ACK(int sockfd, uint8_t response_type) { uint8_t response[3] = {0xAA, response_type, 0xFF}; int attempts = 5; int sent; while (attempts-- > 0) { sent = lwip_send(sockfd, response, sizeof(response), 0); if (sent < 0) { LOG(LOG_LVL_ERROR, "send_ACK error, errno=%d, attempts left=%d\n", errno, attempts); if (errno == EAGAIN || errno == ETIMEDOUT) { OS_MsDelay(50); continue; } break; } else if (sent != sizeof(response)) { LOG(LOG_LVL_ERROR, "Partial ACK send: expected 3, sent %d\n", sent); break; } else { switch(response_type){ case 0xAA: LOG(LOG_LVL_INFO, "Sent %s\n", "ACK_0xAA");break; case 0xEE: LOG(LOG_LVL_INFO, "Sent %s\n", "NACK_0xEE");break; case 0x02: LOG(LOG_LVL_INFO, "Sent %s\n", "ACK_0x02");break; case 0x01: LOG(LOG_LVL_INFO, "Sent %s\n", "ACK_0x01");break; } return; } } LOG(LOG_LVL_ERROR, "Failed to send ACK after retries\n"); }
static int receive_sum_size(int sockfd, uint32_t *sum_size) { uint8_t size_buffer[4]; int size_pos = 0; char buffer[4]; int len = lwip_recv(sockfd, buffer, sizeof(buffer), 0); if (len < 0) { return -1; } else if (len > 0) { LOG(LOG_LVL_INFO, "receive_sum_size len: %d\n", len); int i = 0; while (i < len && size_pos < 4) { size_buffer[size_pos] = buffer[i]; size_pos++; i++; } if (size_pos == 4) { *sum_size = ((uint32_t)size_buffer[0] << 24) | ((uint32_t)size_buffer[1] << 16) | ((uint32_t)size_buffer[2] << 8) | (uint32_t)size_buffer[3]; LOG(LOG_LVL_INFO, "Received total size: %u bytes\n", *sum_size); send_ACK(sockfd, 0xAA); return 1; } } return 0; }
static int get_free_space(uint32_t write_pos, uint32_t read_pos) { if ((write_pos + 1) % SUM_BUFFER_SIZE == read_pos) { return 0; } if (write_pos >= read_pos) { return SUM_BUFFER_SIZE - (write_pos - read_pos) - 1; } else { return read_pos - write_pos - 1; } }
static uint8_t write_circular_buffer(uint8_t *buffer, uint32_t *write_pos, uint32_t *read_pos, uint8_t *data, uint16_t data_length) { uint32_t free_space = get_free_space(*write_pos, *read_pos); if (free_space <= data_length) { LOG(LOG_LVL_ERROR, "Not enough space: available=%u, required=%u\n", free_space, data_length); return -1; } uint32_t space_to_end = SUM_BUFFER_SIZE - *write_pos; if (space_to_end >= data_length) { memcpy(&buffer[*write_pos], data, data_length); } else { memcpy(&buffer[*write_pos], data, space_to_end); memcpy(&buffer[0], data + space_to_end, data_length - space_to_end); } *write_pos = (*write_pos + data_length) % SUM_BUFFER_SIZE; return 0; }
static int receive_data(int sockfd, uint8_t *text_buffer, uint32_t *write_pos, uint32_t *read_pos) { static uint8_t buffer[1024]; static uint8_t temp_buffer[1024]; static uint32_t temp_pos = 0; static int has_partial = 0;
if (has_partial && temp_pos > 0) { int validation_result = validate_format(temp_buffer, temp_pos); if (validation_result == 2) { uint16_t data_length = (temp_buffer[2] << 8) | temp_buffer[3]; uint8_t data_type = temp_buffer[1]; uint8_t *data = &temp_buffer[4]; uint16_t recv_crc = (temp_buffer[4 + data_length] << 8) | temp_buffer[5 + data_length]; uint16_t calculated_crc = calculate_crc16(data, data_length);
if (calculated_crc != recv_crc) { LOG(LOG_LVL_ERROR, "CRC16 mismatch: received=0x%04X, calculated=0x%04X\n", recv_crc, calculated_crc); send_ACK(sockfd, 0xEE); } else { if (data_length == 0 && (data_type == 0x11 || data_type == 0x12)) { LOG(LOG_LVL_INFO, "recv type : 0x%02X\n", data_type); send_ACK(sockfd, 0xAA); state = (data_type == 0x11) ? STATE_PRINT : STATE_AUDIO; } else if (write_circular_buffer(text_buffer, write_pos, read_pos, data, data_length) != 0) { LOG(LOG_LVL_ERROR, "Write to buffer failed\n"); send_ACK(sockfd, 0xEE); } else { total_written += data_length; send_ACK(sockfd, 0xAA); LOG(LOG_LVL_INFO, "CRC match, total_written=%u\n", total_written); } } temp_pos = 0; has_partial = 0; } else if (validation_result == -1) { send_ACK(sockfd, 0xEE); temp_pos = 0; has_partial = 0; return 0; } }
int len = lwip_recv(sockfd, buffer, sizeof(buffer), 0); if (len < 0) { if (errno == EAGAIN || errno == ETIMEDOUT) { OS_MsDelay(100); return 0; } LOG(LOG_LVL_ERROR, "receive_data len < 0: %d\n", errno); return -1; } else if (len == 0) { LOG(LOG_LVL_INFO, "Server closed connection\n"); return -1; }
int i = 0; while (i < len) { if (temp_pos >= sizeof(temp_buffer)) { send_ACK(sockfd, 0xEE); temp_pos = 0; has_partial = 0; LOG(LOG_LVL_INFO, "temp_pos >= sizeof(temp_buffer)...\n"); return 0; }
temp_buffer[temp_pos] = buffer[i]; temp_pos++;
int validation_result = validate_format(temp_buffer, temp_pos); if (validation_result == -1) { send_ACK(sockfd, 0xEE); temp_pos = 0; has_partial = 0; return 0; } else if (validation_result == 2) { uint16_t data_length = (temp_buffer[2] << 8) | temp_buffer[3]; uint8_t data_type = temp_buffer[1]; uint8_t *data = &temp_buffer[4]; uint16_t recv_crc = (temp_buffer[4 + data_length] << 8) | temp_buffer[5 + data_length]; uint16_t calculated_crc = calculate_crc16(data, data_length);
if (calculated_crc != recv_crc) { LOG(LOG_LVL_ERROR, "CRC16 mismatch: received=0x%04X, calculated=0x%04X\n", recv_crc, calculated_crc); send_ACK(sockfd, 0xEE); } else { if (data_length == 0 && (data_type == 0x11 || data_type == 0x12)) { LOG(LOG_LVL_INFO, "recv type : 0x%02X\n", data_type); send_ACK(sockfd, 0xAA); state = (data_type == 0x11) ? STATE_PRINT : STATE_AUDIO; } else if (write_circular_buffer(text_buffer, write_pos, read_pos, data, data_length) != 0) { LOG(LOG_LVL_ERROR, "Write to buffer failed\n"); send_ACK(sockfd, 0xEE); } else { total_written += data_length; send_ACK(sockfd, 0xAA); LOG(LOG_LVL_INFO, "CRC match, total_written=%u\n", total_written); } } temp_pos = 0; has_partial = 0;
if (i + 1 < len) { if (buffer[i + 1] != 0xAA) { LOG(LOG_LVL_INFO, "Discarding invalid data after packet: %02X\n", buffer[i + 1]); return 0; } } } i++; }
if (temp_pos > 0 && temp_buffer[temp_pos - 1] == 0xAA && validate_format(temp_buffer, temp_pos) != 2) { has_partial = 1; LOG(LOG_LVL_INFO, "Partial packet detected, waiting for more data\n"); }
return 0; }
static void send_data(int sockfd, uint8_t type, uint16_t length, uint8_t *data) { uint8_t send_buffer[7 + MAX_DATA_PER_PACKET];
send_ACK(sockfd, 0x01);
send_buffer[0] = 0xAA; send_buffer[1] = type; send_buffer[2] = 0x00; send_buffer[3] = 0x00; uint16_t crc = calculate_crc16(&send_buffer[4], 0); send_buffer[4] = (crc >> 8) & 0xFF; send_buffer[5] = crc & 0xFF; send_buffer[6] = 0xFF; int sent = lwip_send(sockfd, send_buffer, 7, 0); if (sent != 7) { LOG(LOG_LVL_ERROR, "Send type guide failed: expected 7, sent %d\n", sent); return; } LOG(LOG_LVL_INFO, "Sent type guide: 0x%02X\n", type);
uint16_t remaining_length = length; uint8_t *data_ptr = data;
while (remaining_length > 0) { uint16_t chunk_length = (remaining_length > MAX_DATA_PER_PACKET) ? MAX_DATA_PER_PACKET : remaining_length; uint16_t total_packet_length = 7 + chunk_length;
send_buffer[0] = 0xAA; send_buffer[1] = type; send_buffer[2] = (chunk_length >> 8) & 0xFF; send_buffer[3] = chunk_length & 0xFF; memcpy(&send_buffer[4], data_ptr, chunk_length); crc = calculate_crc16(data_ptr, chunk_length); send_buffer[4 + chunk_length] = (crc >> 8) & 0xFF; send_buffer[5 + chunk_length] = crc & 0xFF; send_buffer[6 + chunk_length] = 0xFF;
sent = lwip_send(sockfd, send_buffer, total_packet_length, 0); if (sent != total_packet_length) { LOG(LOG_LVL_ERROR, "Send data failed: expected %d, sent %d\n", total_packet_length, sent); break; } LOG(LOG_LVL_INFO, "Sent %d bytes (data length: %u)\n", sent, chunk_length);
remaining_length -= chunk_length; data_ptr += chunk_length; } }
static void socket_task_entry(void *params) { LN_UNUSED(params); int sockfd = -1; struct sockaddr_in server_addr; int timeout_ms = 5000;
while (1) { if (!netdev_got_ip()) { LOG(LOG_LVL_INFO, "No IP, waiting for WiFi...\n"); OS_MsDelay(1000); continue; }
tcpip_ip_info_t ip_info; netdev_get_ip_info(NETIF_IDX_STA, &ip_info); LOG(LOG_LVL_INFO, "IP: %s, Mask: %s, Gateway: %s\n", ip4addr_ntoa(&ip_info.ip), ip4addr_ntoa(&ip_info.netmask), ip4addr_ntoa(&ip_info.gw));
int8_t rssi; wifi_sta_get_rssi(&rssi); LOG(LOG_LVL_INFO, "WiFi RSSI: %d dBm\n", rssi);
while (netdev_got_ip()) { if (sockfd < 0) { sockfd = lwip_socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { LOG(LOG_LVL_ERROR, "Socket creation failed: %d\n", errno); OS_MsDelay(2000); continue; } LOG(LOG_LVL_INFO, "Socket create ID=%d\n", sockfd);
int flags = 1; if (lwip_ioctl(sockfd, FIONBIO, &flags) < 0) { LOG(LOG_LVL_ERROR, "socket ioctl error: %d\n", errno); lwip_close(sockfd); sockfd = -1; OS_MsDelay(2000); continue; }
memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = lwip_htons(8080); server_addr.sin_addr.s_addr = inet_addr("192.168.106.181"); LOG(LOG_LVL_INFO, "Connecting to 192.168.106.181:8080...\n");
int connect_result = lwip_connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (connect_result < 0 && errno != EINPROGRESS) { LOG(LOG_LVL_ERROR, "lwip_connect failed: %d\n", errno); lwip_close(sockfd); sockfd = -1; OS_MsDelay(2000); continue; }
fd_set writefds, exceptfds; struct timeval tv; tv.tv_sec = 10; tv.tv_usec = 0; FD_ZERO(&writefds); FD_ZERO(&exceptfds); FD_SET(sockfd, &writefds); FD_SET(sockfd, &exceptfds); int select_result = lwip_select(sockfd + 1, NULL, &writefds, &exceptfds, &tv); if (select_result <= 0 || FD_ISSET(sockfd, &exceptfds)) { LOG(LOG_LVL_ERROR, "Connect timed out or failed: %d\n", errno); lwip_close(sockfd); sockfd = -1; OS_MsDelay(2000); continue; }
int so_error = 0; socklen_t len = sizeof(so_error); if (lwip_getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len) < 0 || so_error != 0) { LOG(LOG_LVL_ERROR, "Connect failed with error: %d\n", so_error); lwip_close(sockfd); sockfd = -1; OS_MsDelay(2000); continue; }
LOG(LOG_LVL_INFO, "Connected to 192.168.106.181:8080\n"); tv.tv_sec = timeout_ms / 1000; tv.tv_usec = (timeout_ms % 1000) * 1000; if (lwip_setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { LOG(LOG_LVL_ERROR, "setsockopt SO_RCVTIMEO failed: %d\n", errno); } }
while (sockfd >= 0 && netdev_got_ip()) { uint8_t response[3]; int len;
switch (state) { case STATE_IDLE: len = lwip_recv(sockfd, response, 3, 0); if (len == 3 && response[0] == 0xAA && response[1] == 0x01 && response[2] == 0xFF) { LOG(LOG_LVL_INFO, "Received start signal\n"); send_ACK(sockfd, 0xAA); state = STATE_TYPE; } break;
case STATE_TYPE: receive_data(sockfd, text_buffer, &write_pos, &read_pos); break;
case STATE_PRINT: if (!is_get_sum_size) { if (receive_sum_size(sockfd, &data_sum_size) == 1) { is_get_sum_size = 1; } } else { int result = receive_data(sockfd, text_buffer, &write_pos, &read_pos); if (result == -1) { lwip_close(sockfd); sockfd = -1; break; }
if (result == 0) { len = lwip_recv(sockfd, response, 3, 0); if (len == 3 && response[0] == 0xAA && response[1] == 0x02 && response[2] == 0xFF) { LOG(LOG_LVL_INFO, "Received end signal\n"); send_ACK(sockfd, 0xAA); state = STATE_IDLE; is_get_sum_size = 0; total_written = 0; write_pos = 0; read_pos = 0; break; } }
if (total_written >= data_sum_size) { uint32_t available_data = (write_pos >= read_pos) ? (write_pos - read_pos) : (SUM_BUFFER_SIZE - read_pos + write_pos); if (available_data > 0) { send_data(sockfd, data_type, available_data, &text_buffer[read_pos]); read_pos = (read_pos + available_data) % SUM_BUFFER_SIZE; LOG(LOG_LVL_INFO, "Final send: Sent %u bytes, new read_pos=%u\n", available_data, read_pos); } } } break;
case STATE_AUDIO: int result = receive_data(sockfd, text_buffer, &write_pos, &read_pos); if (result == -1) { lwip_close(sockfd); sockfd = -1; break; }
if (result == 0) { len = lwip_recv(sockfd, response, 3, 0); if (len == 3 && response[0] == 0xAA && response[1] == 0x02 && response[2] == 0xFF) { LOG(LOG_LVL_INFO, "Received end signal\n"); send_ACK(sockfd, 0xAA); uint32_t available_data = (write_pos >= read_pos) ? (write_pos - read_pos) : (SUM_BUFFER_SIZE - read_pos + write_pos); if (available_data > 0) { send_data(sockfd, data_type, available_data, &text_buffer[read_pos]); read_pos = (read_pos + available_data) % SUM_BUFFER_SIZE; LOG(LOG_LVL_INFO, "Final send: Sent %u bytes, new read_pos=%u\n", available_data, read_pos); } state = STATE_IDLE; total_written = 0; write_pos = 0; read_pos = 0; break; } }
uint32_t available_data = (write_pos >= read_pos) ? (write_pos - read_pos) : (SUM_BUFFER_SIZE - read_pos + write_pos); if (available_data >= SUM_BUFFER_SIZE / 2) { send_data(sockfd, data_type, available_data, &text_buffer[read_pos]); read_pos = (read_pos + available_data) % SUM_BUFFER_SIZE; LOG(LOG_LVL_INFO, "Sent %u bytes, new read_pos=%u\n", available_data, read_pos); } break; } }
if (sockfd < 0) { LOG(LOG_LVL_ERROR, "Socket disconnect, retrying...\n"); OS_MsDelay(2000); } } LOG(LOG_LVL_ERROR, "WiFi disconnected, waiting...\n"); OS_MsDelay(2000); } }
|