Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect wifiandheadunit #50

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion aa_wireless_dongle/package/aawg/src/proxyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ void AAWProxy::forward(ProxyDirection direction, std::atomic<bool>& should_exit)
void AAWProxy::handleClient(int server_sock) {
struct sockaddr client_address;
socklen_t client_addresslen = sizeof(client_address);
int rc;
struct timeval timeout;

while (true) {
fd_set rfds;
timeout.tv_sec = 30;
timeout.tv_usec = 0;

FD_ZERO(&rfds);
FD_SET(server_sock, &rfds);

rc = select(server_sock+1,&rfds,NULL,NULL,&timeout);
if (rc == 0) {
Logger::instance()->info("Server socket timeout 30s\n");
close(server_sock);
close(server_sock+1);
return;
}
if (rc > 0) {
break;
}
}

if ((m_tcp_fd = accept(server_sock, &client_address, &client_addresslen)) < 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to set send and recieve timeouts to the client connection socket so that it closes when the phone goes away after connection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this break happens when there is data to read before timeout occurs, going out the while to the accept function

close(server_sock);
Logger::instance()->info("accept failed: %s\n", strerror(errno));
Expand All @@ -138,13 +161,17 @@ void AAWProxy::handleClient(int server_sock) {
usb_tcp.join();
tcp_usb.join();

should_exit = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this help in any way? We've already joined both the threads by now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was no sure about this variable to control the threads in this moment is necessary to inform or no, you better knows the behavior


close(m_usb_fd);
m_usb_fd = -1;

close(m_tcp_fd);
m_tcp_fd = -1;

Logger::instance()->info("Forwarding stopped\n");
Logger::instance()->info("Restart communication\n");
return;
}

std::optional<std::thread> AAWProxy::startServer(int32_t port) {
Expand All @@ -160,7 +187,16 @@ std::optional<std::thread> AAWProxy::startServer(int32_t port) {
Logger::instance()->info("setsockopt failed: %s\n", strerror(errno));
return std::nullopt;
}


struct timeval tv = {
.tv_sec = 30
};

if (setsockopt(server_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: If we're using select before accept, how does setting this help or make a difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept is a unlimited time blocking function as it read information while select can manage a timeout because select only detect that the is information but not read it

Logger::instance()->info("setsockopt failed timeout set: %s\n", strerror(errno));
return std::nullopt;
}

struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
Expand Down
3 changes: 3 additions & 0 deletions aa_wireless_dongle/package/aawg/src/usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ void UsbManager::disableGadget() {
disableGadget(accessoryGadgetName);

Logger::instance()->info("USB Manager: Disabled all USB gadgets\n");

enableGadget(defaultGadgetName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? We generally want to keep usb disabled until we have the phone connection ready to work around headunit timeouts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it depends of the head unit makes the timeout, in my case, in hyundai, i need to have the usb connect (and disabled and enable) or it will never try again if you enabled after "large" time disabled. In this particular headunit works better without the "Connect to USB after phone connection is available" e511b7c commit ; but maybe it this not general behavior buy I don't have other cars to test.

In my own personalization I managed/test this commit controlled by a enviroment variable with this code:

    if (const char* env_p = std::getenv("HEADUNIT_FIRST")) {
        UsbManager::instance().enableDefaultAndWaitForAccessroy();
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe its easier if file is present in /boot partition, as this can be edited from the pc prior to install the sdcard...

touch /boot/HEADUNIT_FIRST 

Copy link
Contributor Author

@Ioniq3 Ioniq3 Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's another way.
So thanks you for your ideas on personalization, I get some of them and add to /etc/init.d/S93aawgd the following lines in my branch personalization:

		xx=$(ls /var/lib/bluetooth/ | awk -F':' '{print $4$5$6 }' | tr [[:upper:]] [[:lower:]] | tr -d '\n')
                export ADAPTER_ALIAS=AndroidAuto_$xx
                export AAWG_WIFI_SSID=AndroidAuto_$xx
                export AAWG_WIFI_PASSWORD=1234567890
		export HEADUNIT_FIRST=1

before start-stop-daemon -S -b -q -m -p "$PIDFILE" -x "/usr/bin/$DAEMON"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have an env file in /boot or somewhere else and source it if that works. We should ideally have a nicer ini config file, but for now env variables might be easier to get started with.

Logger::instance()->info("USB Manager: Start default gadget after restart\n");
}

void UsbManager::enableDefaultAndWaitForAccessroy() {
Expand Down
Loading