| 583 | } |
| 584 | |
| 585 | std::string Galea::find_device () |
| 586 | { |
| 587 | #ifdef _WIN32 |
| 588 | std::string ssdp_ip_address = "192.168.137.255"; |
| 589 | #else |
| 590 | std::string ssdp_ip_address = "239.255.255.250"; |
| 591 | #endif |
| 592 | |
| 593 | safe_logger (spdlog::level::trace, "trying to autodiscover device via SSDP"); |
| 594 | safe_logger (spdlog::level::trace, "timeout for search is {}", params.timeout); |
| 595 | std::string ip_address = ""; |
| 596 | SocketClientUDP udp_client (ssdp_ip_address.c_str (), |
| 597 | 1900); // ssdp ip and port |
| 598 | |
| 599 | int res = udp_client.connect (); |
| 600 | if (res == (int)SocketClientUDPReturnCodes::STATUS_OK) |
| 601 | { |
| 602 | std::string msearch = ("M-SEARCH * HTTP/1.1\r\nHost: " + ssdp_ip_address + |
| 603 | ":1900\r\nMAN: ssdp:discover\r\n" |
| 604 | "ST: urn:schemas-upnp-org:device:Basic:1\r\n" |
| 605 | "MX: 3\r\n" |
| 606 | "\r\n" |
| 607 | "\r\n"); |
| 608 | |
| 609 | safe_logger (spdlog::level::trace, "Use search request {}", msearch.c_str ()); |
| 610 | |
| 611 | res = (int)udp_client.send (msearch.c_str (), (int)msearch.size ()); |
| 612 | if (res == msearch.size ()) |
| 613 | { |
| 614 | unsigned char b[250]; |
| 615 | auto start_time = std::chrono::high_resolution_clock::now (); |
| 616 | int run_time = 0; |
| 617 | while (run_time < params.timeout) |
| 618 | { |
| 619 | res = udp_client.recv (b, 250); |
| 620 | if (res > 1) |
| 621 | { |
| 622 | std::string response ((const char *)b); |
| 623 | safe_logger (spdlog::level::trace, "Search response: {}", b); |
| 624 | std::regex rgx_ip ("LOCATION: http://([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)"); |
| 625 | std::smatch matches; |
| 626 | if (std::regex_search (response, matches, rgx_ip) == true) |
| 627 | { |
| 628 | if (matches.size () == 2) |
| 629 | { |
| 630 | std::regex rgx_sn ( |
| 631 | "USN: uuid:" + params.serial_number + "::upnp:rootdevice"); |
| 632 | if ((params.serial_number.empty ()) || |
| 633 | (std::regex_search (response, rgx_sn))) |
| 634 | { |
| 635 | ip_address = matches.str (1); |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | auto end_time = std::chrono::high_resolution_clock::now (); |
| 642 | run_time = |