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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
| 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", "ACK_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 get_free_space(uint32_t write_pos, uint32_t read_pos, uint32_t buffer_size) { if ((write_pos + 1) % buffer_size == read_pos) { return 0; } if (write_pos >= read_pos) { return buffer_size - (write_pos - read_pos) - 1; } else { return read_pos - write_pos - 1; } }
static int read_circular_buffer(void *buffer, uint32_t *read_pos, uint32_t *write_pos, uint32_t buffer_size, void *data, uint16_t data_length, size_t element_size) { uint32_t available = (*write_pos >= *read_pos) ? (*write_pos - *read_pos) : (buffer_size - *read_pos + *write_pos); if (available < data_length) { return -1; } uint32_t space_to_end = buffer_size - *read_pos; if (space_to_end >= data_length) { memcpy(data, (uint8_t *)buffer + (*read_pos * element_size), data_length * element_size); } else { memcpy(data, (uint8_t *)buffer + (*read_pos * element_size), space_to_end * element_size); memcpy((uint8_t *)data + (space_to_end * element_size), buffer, (data_length - space_to_end) * element_size); } *read_pos = (*read_pos + data_length) % buffer_size; return 0; }
static int write_circular_buffer(void *buffer, uint32_t *write_pos, uint32_t *read_pos, uint32_t buffer_size, void *data, uint16_t data_length, size_t element_size) { uint32_t free_space = get_free_space(*write_pos, *read_pos, buffer_size); uint32_t wait_start = OS_GetTicks();
while (free_space < data_length) { LOG(LOG_LVL_INFO, "Buffer full, data_length=%u, free_space=%u\n", data_length, free_space); vTaskDelay(pdMS_TO_TICKS(10)); free_space = get_free_space(*write_pos, *read_pos, buffer_size); LOG(LOG_LVL_INFO, "write_circular_buffer write=%u, read=%u\n", *write_pos, *read_pos); if (OS_GetTicks() - wait_start > pdMS_TO_TICKS(5000)) { LOG(LOG_LVL_ERROR, "Timeout waiting for buffer space after 5s\n"); return -1; } } uint32_t space_to_end = buffer_size - *write_pos; if (space_to_end >= data_length) { memcpy((uint8_t *)buffer + (*write_pos * element_size), data, data_length * element_size); } else { memcpy((uint8_t *)buffer + (*write_pos * element_size), data, space_to_end * element_size); memcpy(buffer, (uint8_t *)data + (space_to_end * element_size), (data_length - space_to_end) * element_size); } *write_pos = (*write_pos + data_length) % buffer_size; return 0; }
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) { if (errno == EAGAIN || errno == EWOULDBLOCK) { return 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 check_end_signal(uint8_t *buffer, uint32_t *pos, int sockfd) { if (*pos < 3) { return 0; }
for (uint32_t i = 0; i <= *pos - 3; i++) { if (buffer[i] == 0xAA && buffer[i + 1] == 0x02 && buffer[i + 2] == 0xFF) { LOG(LOG_LVL_INFO, "recv end pos=%u\n", i); uint32_t remaining = *pos - (i + 3);
if (remaining >= 3 && buffer[i + 3] == 0xAA && buffer[i + 4] == 0x01 && buffer[i + 5] == 0xFF) { LOG(LOG_LVL_INFO, "Start signal follows end signal, remaining=%u\n", remaining); memmove(buffer, &buffer[i + 3], remaining); *pos = remaining; send_ACK(sockfd, 0xAA); return 1; }
if (remaining > 0) { LOG(LOG_LVL_INFO, "Moving %u bytes after end signal\n", remaining); memmove(buffer, &buffer[i + 3], remaining); *pos = remaining; } else { *pos = 0; }
send_ACK(sockfd, 0xAA); return -3; } } return 0; }
static int process_packet(uint8_t *buffer, uint32_t pos, int sockfd, uint8_t *data_type, uint32_t *total_written) { uint16_t data_length = (buffer[2] << 8) | buffer[3]; *data_type = buffer[1]; uint8_t *data = &buffer[4]; uint16_t recv_crc = (buffer[4 + data_length] << 8) | buffer[5 + data_length]; uint16_t calculated_crc = calculate_crc16(data, data_length);
if (calculated_crc != recv_crc) { LOG(LOG_LVL_ERROR, "CRC16 error: received=0x%04X, calculated=0x%04X\n", recv_crc, calculated_crc); send_ACK(sockfd, 0xEE); return -1; } 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); return 0; }
taskENTER_CRITICAL(); if (*data_type == 0x12) { if (data_length % 2 != 0) { LOG(LOG_LVL_ERROR, "Audio data length not two: %u\n", data_length); send_ACK(sockfd, 0xEE); } else { uint16_t sample_count = data_length / 2; int16_t samples[sample_count]; for (uint16_t i = 0; i < sample_count; i++) { samples[i] = (int16_t)((data[i * 2] << 8) | data[i * 2 + 1]); } int write_result = write_circular_buffer(PCMBuffer_rx, (uint32_t *)&rx_write_pos, (uint32_t *)&rx_read_pos, RING_BUFFER_SIZE, (uint8_t *)samples, sample_count, sizeof(int16_t)); if (write_result == 0) { *total_written += sample_count; send_ACK(sockfd, 0xAA); LOG(LOG_LVL_INFO, "CRC match, total_written=%u (0x12)\n", *total_written); xSemaphoreGive(rx_data_ready_sem); } else if (write_result == -1) { xSemaphoreGive(rx_data_ready_sem); send_ACK(sockfd, 0xEE); LOG(LOG_LVL_INFO, "Buffer full, signalled consumer for audio data\n"); } } } else if (*data_type == 0x11) { int write_result = write_circular_buffer(print_buffer, (uint32_t *)&print_write_pos, (uint32_t *)&print_read_pos, SUM_BUFFER_SIZE, data, data_length, sizeof(uint8_t)); if (write_result == 0) { *total_written += data_length; send_ACK(sockfd, 0xAA); LOG(LOG_LVL_INFO, "CRC match, total_written=%u (bytes)\n", *total_written); xSemaphoreGive(print_data_ready_sem); } else if (write_result == -1) { xSemaphoreGive(print_data_ready_sem); send_ACK(sockfd, 0xEE); LOG(LOG_LVL_INFO, "Buffer full, signalled consumer for print data\n"); } } taskEXIT_CRITICAL(); return 0; }
static int receive_data(int sockfd, int *has_partial, uint8_t *temp_buffer, uint32_t *temp_pos, uint8_t *data_type, uint32_t *total_written) { int len; static uint8_t buffer[4096];
if (*has_partial && *temp_pos > 0) { int end_result = check_end_signal(temp_buffer, temp_pos, sockfd); if (end_result == -3) { *has_partial = 0; return -3; } else if (end_result == 1) { *has_partial = 1; return 1; }
int validation_result = validate_format(temp_buffer, *temp_pos); if (validation_result == 2) { process_packet(temp_buffer, *temp_pos, sockfd, data_type, total_written); *temp_pos = 0; *has_partial = 0; } else if (validation_result == -1) { send_ACK(sockfd, 0xEE); *temp_pos = 0; *has_partial = 0; } }
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, "recv_data len < 0: %d\n", errno); return -1; } else if (len == 0) { LOG(LOG_LVL_INFO, "Server closed connection\n"); return -2; }
int i = 0; while (i < len) { if (*temp_pos >= TEMP_BUFFER_SIZE) { send_ACK(sockfd, 0xEE); *temp_pos = 0; *has_partial = 0; LOG(LOG_LVL_INFO, "temp_pos >= sizeof(temp_buffer)...\n"); break; }
temp_buffer[*temp_pos] = buffer[i]; (*temp_pos)++;
int end_result = check_end_signal(temp_buffer, temp_pos, sockfd); if (end_result == -3) { *has_partial = 0; return -3; } else if (end_result == 1) { *has_partial = 1; return 1; }
int validation_result = validate_format(temp_buffer, *temp_pos); if (validation_result == -1) { send_ACK(sockfd, 0xEE); *temp_pos = 0; *has_partial = 0; continue; } else if (validation_result == 2) { process_packet(temp_buffer, *temp_pos, sockfd, data_type, total_written); *temp_pos = 0; *has_partial = 0; if (i + 1 < len && buffer[i + 1] != 0xAA) { LOG(LOG_LVL_INFO, "Discarding invalid data after packet: %02X\n", buffer[i + 1]); break; } } 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, "package not wanzheng\n"); }
return 0; }
static void socket_task_entry(void *params) { LN_UNUSED(params); struct sockaddr_in server_addr; int timeout_ms = 5000; int sockfd = -1; int has_partial = 0; uint8_t temp_buffer[TEMP_BUFFER_SIZE] = {0}; uint32_t temp_pos = 0; uint8_t data_type = 0; uint32_t total_written = 0; State_t state = STATE_IDLE; uint32_t data_sum_size = 0; uint8_t is_get_sum_size = 0; static uint32_t last_data_time = 0;
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 create error: %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.2.27"); LOG(LOG_LVL_INFO, "Connecting to 192.168.2.27: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.2.27: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); }
state = STATE_IDLE; has_partial = 0; temp_pos = 0; data_type = 0; total_written = 0; data_sum_size = 0; is_get_sum_size = 0; last_data_time = 0; }
while (sockfd >= 0 && netdev_got_ip()) { switch (state) { case STATE_IDLE: { uint8_t response[3]; int len = lwip_recv(sockfd, response, sizeof(response), 0); if (len < 0) { if (errno == EAGAIN || errno == ETIMEDOUT) { OS_MsDelay(100); break; } LOG(LOG_LVL_ERROR, "STATE_IDLE len < 0: %d\n", errno); lwip_close(sockfd); sockfd = -1; break; } else if (len == 0) { LOG(LOG_LVL_INFO, "Server closed connection\n"); lwip_close(sockfd); sockfd = -1; break; }
if (len == 3 && response[0] == 0xAA && response[1] == 0x01 && response[2] == 0xFF) { LOG(LOG_LVL_INFO, "recv start...\n"); send_ACK(sockfd, 0xAA); state = STATE_TYPE; } else { LOG(LOG_LVL_ERROR, "recv start error: %02X %02X %02X\n", response[0], response[1], response[2]); send_ACK(sockfd, 0xEE); } break; }
case STATE_TYPE: { int ret = receive_data(sockfd, &has_partial, temp_buffer, &temp_pos, &data_type, &total_written); if (ret == -1 || ret == -2) { LOG(LOG_LVL_INFO, "STATE_TYPE(sockfd)...\n"); lwip_close(sockfd); sockfd = -1; break; } else if (ret == -3) { LOG(LOG_LVL_ERROR, "Unexpected end signal in STATE_TYPE\n"); state = STATE_IDLE; break; }
if (data_type == 0x11 || data_type == 0x12) { state = (data_type == 0x11) ? STATE_PRINT : STATE_AUDIO; data_type = 0; } break; }
case STATE_PRINT: { if (!is_get_sum_size) { int ret = receive_sum_size(sockfd, &data_sum_size); if (ret == -1) { LOG(LOG_LVL_ERROR, "receive_sum_size error\n"); lwip_close(sockfd); sockfd = -1; break; } if (ret == 1) { is_get_sum_size = 1; total_written = 0; last_data_time = OS_GetTicks(); LOG(LOG_LVL_INFO, "STATE_PRINT sum size: %u bytes\n", data_sum_size); } } else { int ret = receive_data(sockfd, &has_partial, temp_buffer, &temp_pos, &data_type, &total_written); if (ret == -1) { LOG(LOG_LVL_INFO, "STATE_PRINT(sockfd)...\n"); lwip_close(sockfd); sockfd = -1; break; } else if (ret == -2) { LOG(LOG_LVL_INFO, "Server closed connection\n"); if (total_written < data_sum_size) { LOG(LOG_LVL_ERROR, "Incomplete print data: received %u of %u bytes\n", total_written, data_sum_size); } lwip_close(sockfd); sockfd = -1; is_get_sum_size = 0; total_written = 0; print_write_pos = 0; print_read_pos = 0; state = STATE_IDLE; break; } else if (ret == -3) { if (total_written < data_sum_size) { LOG(LOG_LVL_ERROR, "print data not wz: received %u of %u bytes\n", total_written, data_sum_size); } is_get_sum_size = 0; total_written = 0; print_write_pos = 0; print_read_pos = 0; state = STATE_IDLE; break; }else if (ret == 1) { LOG(LOG_LVL_INFO, "New session started after end signal\n"); total_written = 0; rx_write_pos = 0; rx_read_pos = 0; state = STATE_TYPE; break; } last_data_time = OS_GetTicks(); if (OS_GetTicks() - last_data_time > pdMS_TO_TICKS(5000)) { LOG(LOG_LVL_ERROR, "Timeout waiting for print data\n"); lwip_close(sockfd); sockfd = -1; is_get_sum_size = 0; total_written = 0; print_write_pos = 0; print_read_pos = 0; state = STATE_IDLE; break; } LOG(LOG_LVL_INFO, "Received %u of %u bytes\n", total_written, data_sum_size); } break; }
case STATE_AUDIO: { int ret = receive_data(sockfd, &has_partial, temp_buffer, &temp_pos, &data_type, &total_written); if (ret == -1) { LOG(LOG_LVL_INFO, "lwip_close(sockfd)...\n"); lwip_close(sockfd); sockfd = -1; break; } else if (ret == -2) { LOG(LOG_LVL_INFO, "Server closed connection\n"); lwip_close(sockfd); sockfd = -1; total_written = 0; rx_write_pos = 0; rx_read_pos = 0; state = STATE_IDLE; break; } else if (ret == -3) { total_written = 0; rx_write_pos = 0; rx_read_pos = 0; state = STATE_IDLE; break; }else if (ret == 1) { LOG(LOG_LVL_INFO, "New session started after end signal\n"); total_written = 0; rx_write_pos = 0; rx_read_pos = 0; state = STATE_TYPE; break; } last_data_time = OS_GetTicks(); if (OS_GetTicks() - last_data_time > pdMS_TO_TICKS(5000)) { LOG(LOG_LVL_ERROR, "Timeout waiting for audio data\n"); lwip_close(sockfd); sockfd = -1; total_written = 0; rx_write_pos = 0; rx_read_pos = 0; state = STATE_IDLE; break; }
if (xSemaphoreTake(tx_data_ready_sem, pdMS_TO_TICKS(100)) == pdTRUE) { uint16_t data_length = 0; int16_t data[MAX_DATA_PER_PACKET / 2]; taskENTER_CRITICAL(); uint32_t available = (tx_write_pos >= tx_read_pos) ? (tx_write_pos - tx_read_pos) : (RING_BUFFER_SIZE - tx_read_pos + tx_write_pos); if (available >= SEND_THRESHOLD / 2) { data_length = (available > MAX_DATA_PER_PACKET / 2) ? (MAX_DATA_PER_PACKET / 2) : available; read_circular_buffer(PCMBuffer_tx, (uint32_t *)&tx_read_pos, (uint32_t *)&tx_write_pos, RING_BUFFER_SIZE, data, data_length, sizeof(int16_t)); } taskEXIT_CRITICAL();
if (data_length > 0) { uint8_t send_buffer[7 + MAX_DATA_PER_PACKET]; send_buffer[0] = 0xAA; send_buffer[1] = 0x12; send_buffer[2] = (data_length * 2 >> 8) & 0xFF; send_buffer[3] = (data_length * 2) & 0xFF; memcpy(&send_buffer[4], data, data_length * 2); uint16_t crc = calculate_crc16((uint8_t *)data, data_length * 2); send_buffer[4 + data_length * 2] = (crc >> 8) & 0xFF; send_buffer[5 + data_length * 2] = crc & 0xFF; send_buffer[6 + data_length * 2] = 0xFF;
int sent = lwip_send(sockfd, send_buffer, 7 + data_length * 2, 0); if (sent != 7 + data_length * 2) { LOG(LOG_LVL_ERROR, "Send data failed: expected %d, sent %d\n", 7 + data_length * 2, sent); lwip_close(sockfd); sockfd = -1; break; } else { LOG(LOG_LVL_INFO, "Sent %d bytes (data length: %u)\n", sent, data_length * 2); } } } 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); } }
void audio_task(void *param) { pwm_init(40000, 0, PWM_CHA_0, GPIO_B, GPIO_PIN_5); pwm_init(40000, 0, PWM_CHA_1, GPIO_B, GPIO_PIN_6); pwm_start(PWM_CHA_0); pwm_start(PWM_CHA_1);
bool speaker_on = false; int16_t samples[192]; const uint32_t samples_per_batch = 192;
while (1) { LOG(LOG_LVL_INFO, "Audio task sem, count=%lu\n", uxSemaphoreGetCount(rx_data_ready_sem)); if (xSemaphoreTake(rx_data_ready_sem, pdMS_TO_TICKS(10)) == pdTRUE) { taskENTER_CRITICAL(); uint32_t available = (rx_write_pos >= rx_read_pos) ? (rx_write_pos - rx_read_pos) : (RING_BUFFER_SIZE - rx_read_pos + rx_write_pos); LOG(LOG_LVL_INFO, "Audio task: available=%u, write=%u, read=%u\n", available, rx_write_pos, rx_read_pos); if (available >= samples_per_batch) { if (!speaker_on) { pwm_start(PWM_CHA_0); pwm_start(PWM_CHA_1); speaker_on = true; } if (read_circular_buffer(PCMBuffer_rx, (uint32_t *)&rx_read_pos, (uint32_t *)&rx_write_pos, RING_BUFFER_SIZE, samples, samples_per_batch, sizeof(int16_t)) == 0) { LOG(LOG_LVL_INFO, "Audio task read %u samples, new read=%u\n", samples_per_batch, rx_read_pos); taskEXIT_CRITICAL(); for (uint32_t i = 0; i < samples_per_batch; i++) { uint16_t pwm_value = (samples[i] + 32768) >> 4; float duty = (pwm_value / 4095.0f) * 100.0f; pwm_set_duty(PWM_CHA_0, duty); vTaskDelay(pdMS_TO_TICKS(1000 / 16000)); } } else { LOG(LOG_LVL_ERROR, "Read from PCMBuffer_rx failed\n"); taskEXIT_CRITICAL(); } } else { taskEXIT_CRITICAL(); } } else { LOG(LOG_LVL_INFO, "Audio task timeout, no data ready\n"); if (speaker_on) { pwm_stop(PWM_CHA_0); pwm_stop(PWM_CHA_1); speaker_on = false; LOG(LOG_LVL_INFO, "Speaker turned off due to no data\n"); } } vTaskDelay(pdMS_TO_TICKS(10)); } }
void print_task(void *param) { while (1) { if (xSemaphoreTake(print_data_ready_sem, portMAX_DELAY) == pdTRUE) { uint8_t data[SUM_BUFFER_SIZE]; taskENTER_CRITICAL(); uint32_t available = (print_write_pos >= print_read_pos) ? (print_write_pos - print_read_pos) : (SUM_BUFFER_SIZE - print_read_pos + print_write_pos); if (available > 0) { read_circular_buffer(print_buffer, (uint32_t *)&print_read_pos, (uint32_t *)&print_write_pos, SUM_BUFFER_SIZE, data, available, sizeof(uint8_t)); LOG(LOG_LVL_INFO, "Print task processed %u bytes\n", available); xSemaphoreGive(print_data_ready_sem); } taskEXIT_CRITICAL(); } } }
void usr_app_task_entry(void *params){ rx_data_ready_sem = xSemaphoreCreateCounting(65535, 0);; tx_data_ready_sem = xSemaphoreCreateCounting(35535, 0);; print_data_ready_sem = xSemaphoreCreateCounting(35535, 0);; if (rx_data_ready_sem == NULL || tx_data_ready_sem == NULL || print_data_ready_sem == NULL) { LOG(LOG_LVL_ERROR, "Semaphore creation failed!\n"); }
if (OS_OK != OS_ThreadCreate(&g_socket_thread, "SocketTask", socket_task_entry, NULL, OS_PRIORITY_BELOW_NORMAL, USR_APP_TASK_STACK_SIZE)) { LOG(LOG_LVL_ERROR, "SocketTask failed!\n"); LN_ASSERT(1); } if (OS_OK != OS_ThreadCreate(&g_audio_thread, "AudioTask", audio_task, NULL, OS_PRIORITY_BELOW_NORMAL, USR_APP_TASK_STACK_SIZE)) { LOG(LOG_LVL_ERROR, "AudioTask failed!\n"); LN_ASSERT(1); }
}
|