Solution for Android WiFi auto Disconnect from connected state (DHCP cause) "Go"

Source: Internet
Author: User
Tags strcmp usleep

This article was reproduced from: http://blog.csdn.net/DKBDKBDKB/article/details/38490201

The WiFi part of the code flow has been seen for some time, the first two days finally solved the work encountered a WiFi problem, the problem description and resolution process are as follows:

Hardware platform: iMx53

Software platform: Android2.3

Bug Description: 1, select hotspot, enter password, will show "Getting IP address ..." "And then becomes saved without a connection.

2, the system on the premise of WiFi connection, re-power, unable to automatically connect to the saved WiFi hotspot.

3, after the system WiFi connection, after a period of time automatically disconnected, the WiFi icon dimmed.

First of all, feel the WiFi connection automatically disconnect this situation, the Android upper layer should be more intelligent, the connected WiFi will be disconnected after the attempt to automatically reconnect the logic implementation, but from the log, connection is reset and reconnect but failed , no re-connect succeeded. So began to continue to analyze the cause of the re-connect failure, and finally found that the suspicion of DHCP is very large:

DHCP request error:timed out waiting for DHCPCD to start

This is the usual part of the log in the event of a problem: DHCP start fails. The code is implemented inside the/SYSTEM/CORE/LIBNETUTILS/DHCP_UTILS.C. The reason for start failure is that the Init.svc.dhcpcd_wlan0 prop has not changed to "running" in the specified time, so I made a re-open change in the Wait_for_property function, adding the following code:

[CPP]View PlainCopy
  1. if (strcmp (name, "init.svc.dhcpcd_wlan0") = = 0 && \
  2. strcmp (Desired_value, "running") = = 0) {
  3. if (maxnaps = = 7) {
  4. Property_set ("Ctl.start", "Dhcpcd_wlan0");
  5. LOGD ("Kevin 1st set Init.svc.dhcpcd_wlan0");
  6. } Else if (maxnaps = = 4) {
  7. Property_set ("Ctl.start", "Dhcpcd_wlan0");
  8. LOGD ("Kevin 2nd set Init.svc.dhcpcd_wlan0");
  9. }
  10. }

The problem has been greatly improved, and then there will be no start failure problem, but the attendant problem arises: After the system power up, the WiFi icon is connected, but click on the WiFi hotspot on the connection, there is no IP address in the pop-up window, and at this time the upper-level app does not surf the internet, Because clicking on the pop-up window is a series of attribute information obtained through the Get_property interface, the analysis is two possible: 1, the upper layer did not capture the property;2, the bottom is not the corresponding property set in. The first possibility is very low, so directly from the second case analysis, found that these properties are in Dhcp_start near the end of the time, through some of the DHCP script execution set in (/external/dhcpcd/dhcpcd-hooks script path), Setting the property "Dhcp.wlan0.result" will be set to "OK", at this time to ensure that the upper layer can obtain the corresponding property, in view of this, the analysis or DHCP start does not start normally, so the/system/core/ LIBNETUTILS/DHCP_UTILS.C implementation of its own new interface: Dhcp_reset (), specifically implemented as follows

[CPP]View PlainCopy
  1. Static const char dhcp_server[] ="Dhcpcd_wlan0";
  2. Static const char dhcp_prop[] ="Init.svc.dhcpcd_wlan0";
  3. Static const char dhcp_res[] ="Dhcp.wlan0.result";
  4. static int Dhcp_reset ()
  5. {
  6. int stop_wait = 5;
  7. int start_wait = 10;
  8. int result_wait = 20;
  9. char Kvalue[property_value_max] = {'} '};
  10. LOGD ("Kevin start Dhcp_reset");
  11. Property_set ("Ctl.stop", dhcp_server);
  12. While (stop_wait--> 0) {
  13. Usleep (500000);
  14. if (Property_get (Dhcp_prop, Kvalue, NULL)) {
  15. if (strcmp (Kvalue, "stopped") = = 0) {
  16. LOGD ("Kevin:property name is%s get_value \
  17. Is%s and Desired_value is stopped ", Dhcp_prop, Kvalue);
  18. Break ;
  19. }
  20. }
  21. }
  22. Property_set ("Ctl.start", dhcp_server);
  23. While (start_wait--> 0) {
  24. Usleep (500000);
  25. if (Property_get (Dhcp_prop, Kvalue, NULL)) {
  26. if (strcmp (Kvalue, "running") = = 0) {
  27. LOGD ("Kevin:property name is%s get_value \
  28. Is%s and Desired_value is running ", Dhcp_prop, Kvalue);
  29. Break ;
  30. }
  31. }
  32. }
  33. While (result_wait--> 0) {
  34. Usleep (500000);
  35. if (Property_get (Dhcp_res, Kvalue, NULL)) {
  36. if (strcmp (Kvalue, "OK") = = 0) {
  37. LOGD ("Kevin:property name is%s get_value \
  38. Is%s and desired_value are OK ", dhcp_res, Kvalue);
  39. return 0;
  40. }
  41. }
  42. }
  43. return-1; / * Failure * /
  44. }

In addition, the Wait_for_property () function needs to add the following code:

[CPP]View PlainCopy
  1. if (strcmp (name, "Dhcp.wlan0.result") = = 0 \
  2. && (maxnaps = = 12| | maxnaps = = 8)) {
  3. if (maxnaps = =)
  4. LOGD ("Kevin 1st Reset_dhcp");
  5. Else
  6. LOGD ("Kevin 2nd Reset_dhcp");
  7. if (dhcp_reset () = = 0)
  8. LOGD ("Kevin Dhcp_reset Success");
  9. Else
  10. LOGD ("Kevin Dhcp_reset failed");
  11. }

The implementation mechanism is in the process of waiting for the dhcp.wlan0.result to eventually have normal set, the opportunistic dhcp_reset, the basis is that after the normal start and set the time period of the property, call Dhcp_reset Restart the DHCP service, Note that you cannot blindly use "Ctl.start" to turn on DHCP at this time, because it may cause multiple DHCP processes to run concurrently in the system, so stop first and then start (this is my personal understanding).

At this point, DHCP caused a series of WiFi problems, announced the resolution!

If there are any questions, welcome to discuss, if there are errors, but also looking for expert guidance.

Solution for Android WiFi auto Disconnect from connected state (DHCP cause) "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.