WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ]

Added by Fil Bergamo 3 months ago

Edit:
It seems like someone could be mislead by the way my original post was written, so I want to make things clear:
This thread is about making WiFi work USING AN EXTERNAL DONGLE. The internal Broadcom chip is not yet usable in freedom.

Hi folks,

I just wanted to share my enthusiasm for my recent successful experience in making WiFi work on Replicant, using this external dongle.
As I've already written [[here]] , I spent a couple of days working on the command line, and now I have a usable WiFi client on my Replicant Phone!

IT..
COULD..
WORK!!!! (muahuahahaha!)

Thanks to Tibi (Tehnoetic) for releasing all the necessary patches to Replicant and the fundamental scripts for wpa_supplicant!

I'm now planning to develop a graphical front-end to make it usable by everyone.

I'm providing every detail I can to those who want to learn more, so if you're curious, just ask!

P.S. please read [[this]] and help if you can!

[EDIT]
I am now proud to provide some useful free-licensed scripts, to make the WiFi experience a bit easier:

the basic instructions are the same provided here
I'm going to update Tehnoetic's wiki too, as soon as I'm able.

The HUGE difference I discovered is that IT IS NOT NECESSARY TO FORMAT THE /data PARTITION in order to recover from a system crash.
Instead, it is sufficient to chmod the /data/misc/wifi folder and all its contents to 777.
Maybe some less privilege is sufficient, but I haven't tried yet. I only tried 777, and it works. If someone finds out that less privilege is needed, please let us know, as "least privilege" is always the best philosophy!

Other improvements I was able to achieve are:
1) the ability to scan for available networks, and connect to the one of interest by simply choosing a number from the list, and providing the relative WPA password.
2) a better management of the connection, with both connect and disconnect scritps.
3) an automated way to write the configuration file for wpa_supplicant.

So, without further introduction, here are the instructions:
(It is assumed that you have tehnoetic's unofficial version of Replicant running on the target device which must be an i9100)

- Back up all of your data! Back up all of your data! Back up all of your data!
All the scripts and relative instructions are provided "AS IS" without ANY warranty.
I tested all of the following instructions, and they SHOULD cause no harm, but I don't take any responsibility for any data loss or any damage to your device.

- Download and copy the needed drivers/firmwares for ath9k (refer to: https://wiki.tehnoetic.com/doku.php?id=tehnoetic_wifi_replicant)
- Download the scripts attached to this post.
- Copy all the scripts to /data/misc/wifi:

adb push startwpa.sh /data/misc/wifi
adb push rstr.sh /data/misc/wifi
adb push check_modules.sh /data/misc/wifi
adb push connect.sh /data/misc/wifi
adb push disconnect.sh /data/misc/wifi
adb push select_network.sh /data/misc/wifi
adb push write_config.sh /data/misc/wifi

- To prepare the environment run the rstr.sh script on the target device, in terminal emulator, running as root.
(the "rstr" script can be used both at system startup to set up the environment, and after a crash, to restore it)
# bash /data/misc/wifi/rstr.sh

- To scan for available WiFi networks, run the "select_network.sh" script
# bash /data/misc/wifi/select_network.sh

- The script scans for networks, and lists the available ones:
Scanning for networks... OK
Retrieving network list... 

Please input the number of the network you want to connect to:
     # |  SSID
    ---|------------------
    1) | JustAnotherWiFi_AP
    2) | SuperFastNetwork
    3) | ARandomISPname1234

- (EXAMPLE) Enter 1 to connect to JustAnotherWiFi_AP, the script will then ask for a PSK password for the connection:
Enter a password for JustAnotherWiFi_AP

- Input the password and press enter. The script will automatically set up "wpa_supplicant.conf" for the desired connection. You will then be asked if you want to connect:
Network connection has been set up for JustAnotherWiFi_AP!
Do you want to connect to JustAnotherWiFi_AP now? [y/n] 

- Input "y" to connect.. Everything should work...

- BEFORE UNPLUGGING THE DONGLE: make sure you stop wpa_supplicant and dhcpcd, to avoid possible crashes!

bash /data/misc/wifi/disconnect.sh

During my tests, I have found that a rough 90% of the crashes must be due to a lack of current to the external dongle.
I fact, crashes tend to occur when the device battery is low (below 40%) or when doing CPU-intensive operations (e.g. playing 720p videos while being connected).
I'm going to try and use a "Y" cable together with an additional power supply ("power bank"), and see if the situation gets better.
Apart from this, regular web browsing (no heavy graphics or scripts) should work like a charm.

If you encounter any problem, ask here or on the mailing list. I'll be glad to help you if I'm able to, as soon as my schedule allows me to.
Any improvements and corrections are very, very, very welcome, so please, if you find a bug, or have any suggestion, don't hesitate to answer this post or write on the mailing list.

My idea was, and still is, to write an application to provide a GUI around these scripts.
But, as of now, I still haven't been able to overcome the numerous troubles I encountered in trying to get Replicant's SDK work in Eclipse.
That's to say: if someone wants to help, we can join efforts and do The Right Thing to make Replicant even better then now!

I hope many of you will find my work useful and enjoyable!

write_config.sh - writes a config file for wpa_supplicant using provided parameters (SSID and password) (1.1 kB)

select_network.sh - Scans for available connections and lets you choose one to connect to. (2.8 kB)

check_modules.sh - checks if the needed drivers are loaded (2.5 kB)

connect.sh - connects to the network using current wpa_supplicant.conf file (1.1 kB)

disconnect.sh - stops dhcpcd and wpa_supplicant (180 Bytes)

rstr.sh - sets up or restores the working environment (1.2 kB)

startwpa.sh - starts wpa_supplicant without configuration, in order to make it work wit wpa_cli (82 Bytes)


Replies (17)

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo 3 months ago

UPDATE

I've improved the WiFi scripts a little bit:
- rearranged the whole system to have a "global" variables setup, so that it's easier to manage and change the paths for debugging or for any needed modification.
- added a trivial system to store "known" connections on a file, so that you don't need to re-enter the password each time you connect.

I also managed to place some shortcuts on the home screen of the device, to ease the launching of the scripts (read on for a little tutorial).

To set up the whole environment:

- download the RepWiFi.tar.bz2 package attached to this post.
- extract the content and navigate inside the extracted "v1.3" folder
- from within the "v1.3" folder run:

bash setup.sh

- this will start adb as root, and push all the scripts on the device, in the /data/misc/wifi folder.
- to launch the scripts from the command line, refer to my previous post here above.
- to ease your life a little bit and use some shortcuts, read on:

To create the shortcuts:

The "terminal emulator" app, installed by default in Replicant 4.2 comes with a widget that allows us to place a shortcut on the home screen, pointing to a command to be executed in terminal.
This comes in handy for our wifi scripts, so that we can then search networks, connect and disconnect by just tapping a shortcut.
1) open the "app drawer" and scroll to the widget section, to find the "Term shortcut" widget.
2) long press on "term shortcut" and release the shortcut where you like, on the home screen.
3) a configuration window will appear:

4.1) leave the "command" textbox blank!
4.2) in the "Arguments" textbox write the command to run the script you want like this:

su -c "bash path/to/the/script"

5) call your shortcut whatever you want by placing a name in the "shortcut label" textbox, and click "ok".
6) you can now launch the script by simply clicking on the shortcut you just created.
7) make sure you close the terminal window after running the scritp! I'm not sure why, but if you leave the terminal window open, the next command you launch via a shortcut will behave strangely (sometimes won't even run until you close the previous window).

Repeat the above points 1-5 for each script you need, using the following command lines:
su -c "bash /data/misc/wifi/rstr.sh" for the "restore" script,
su -c "bash /data/misc/wifi/select_network.sh" for the network selection script
su -c "bash /data/misc/wifi/connect.sh" for the "reconnect" script
su -c "bash /data/misc/wifi/disconnect.sh" for the "disconnect" script

I hope the instructions above are clear enough for everyone.
In case they aren't, just ask and I'll be glad to help!

Enjoy Libre WiFi connectivity!

Fil

RepWiFi.tar.bz2 (4.8 kB)

screenshot.png (143 kB)

screenshot.png (53.1 kB)

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Kurtis Hanna about 1 month ago

This is great! Thank you so much for all the work you've put into this!

Do you happen to have an i9300 to test this on, by chance?

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo about 1 month ago

Do you happen to have an i9300 to test this on, by chance?

Not yet. But I'm planning to get one and try the scripts on it.
The scripts themselves should work just fine on ANY device having wpa_supplicant and wpa_cli installed, as they are basically only a set of commands to those two programs.
They should even work on any GNU/linux system by just changing the working directory to something more sensible (e.g. /home/user/wifi )
And from what I know, the S3 should run the same driver/firmwares for atheros AR9271, so there should be no difference in making the whole thing work.
Still I'm not completely sure, so I suggest you ask Tibi from Tehnoetic if his unofficial version of Replicant (which is needed in order to have support for free wifi) runs the same on both devices.

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Mzee Mzee about 1 month ago

Wow, this is awesome news! When can we expect this to be included in the official distribution of Replicant? How can the community help to make this a reality as fast as possible?

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo about 1 month ago

When can we expect this to be included in the official distribution of Replicant? How can the community help to make this a reality as fast as possible?

I think Tibi(tct) is in touch with PaulK to organize the inclusion of wifi support in the next release of Replicant..
I really don't know much more about it.
I suggest you contact Tibi (http://redmine.replicant.us/users/4785) or Paul (http://redmine.replicant.us/projects/replicant/wiki#Contact) to have better information.

Anyway, WiFi on Replicant IS ALREADY a reality. You can have it working, installing Tehnoetic's unofficial zip (refer to https://wiki.tehnoetic.com/doku.php?id=tehnoetic_wifi_replicant), and following my tutorial in this very thread!

Happy Hacking!

Fil

Minimal script for Replicant 6.0 - Added by Wolfgang Wiedmeyer 28 days ago

All the necessary patches are now included in the kernel for the Galaxy S2 and Galaxy S3 and it should work with Replicant 6.0. The firmware is also part of Replicant 6.0 images, so no need to manually copy it to the device.

I attached a minimal script that allows to connect to a WiFi access point. You need to edit the name and password of your access point at the beginning of the script. Then you can push it to the device:

adb push path/to/wifi_setup.sh /data/misc/wifi/

Attach the wifi dongle and run the script as root in the /data/misc/wifi directory using the terminal emulator app (bash wifi_setup.sh). You may need to enable the terminal app in the developer settings. These are all the needed steps to get WiFi working.

It should be pretty straightforward to change Fil's scripts accordingly and include the commands from my script that are specific to Replicant 6.0. So if someone wants to help with this, this would be a great place to start!

Security-wise, I'm pretty satisfied with the script. The configuration steps don't require to change any permissions and nothing needs to be made world-readable. It also looks like wpa_supplicant correctly drops its privileges and runs as the wifi user.

wifi_setup.sh - minimal script for Replicant 6.0 (1.9 kB)

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo 27 days ago

Wow!

Thank you so much for everything you've done in this regard!
As I now own an I9300, I'm planning to test Replicant 6.0 with WiFi as soon as possible!

I'm also planning to work on the scripts today, to try and merge your work with mine.
I'll post my results as soon as I've finished.

Thank you again ;)

Fil

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo 24 days ago

I have started playing around with your script..
It works, with some modifications, in Replicant 4.2 (basically stripping away the ndc commands, and the -I option from wpa_supplicant)

I have now tried to run it in Replicant 6.0, but I haven't been able to get it to work yet..
It seems like interface wlan0 disappears somewhere during the execution of the script.

ip link correctly lists wlan0 before running the scripts (and after unplugging and plugging the dongle back in).
But while the script runs, some command complains about "wlan0" being inexistent. In fact, the interface disappears from ip link too, after running the script.
I didn't have much time to do a deeper testing. I'm going for it tomorrow.

As a side note:
/data/misc/wifi isn't accessible in the regular shell.
su is still needed to run the script.

More details tomorrow..

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Wolfgang Wiedmeyer 24 days ago

Fil Bergamo wrote:

I have started playing around with your script..
It works, with some modifications, in Replicant 4.2 (basically stripping away the ndc commands, and the -I option from wpa_supplicant)

Great!

I have now tried to run it in Replicant 6.0, but I haven't been able to get it to work yet..
It seems like interface wlan0 disappears somewhere during the execution of the script.

ip link correctly lists wlan0 before running the scripts (and after unplugging and plugging the dongle back in).
But while the script runs, some command complains about "wlan0" being inexistent. In fact, the interface disappears from ip link too, after running the script.
I didn't have much time to do a deeper testing. I'm going for it tomorrow.

In a previous version of the script, I did run "ifconfig "$iface" up" before launching wpa_supplicant. And I had these kind of issues before, but they disappeared after some changes and I removed the ifconfig command as it didn't seem to be necessary anymore. You could add the ifconfig initialization command. Maybe it solves the issue.

As a side note:
/data/misc/wifi isn't accessible in the regular shell.
su is still needed to run the script.

Yes, if we stick with the current setup and don't do the hard work of properly implementing a hardware abstraction layer for the usb dongle in the same way as for the internal wifi, it will likely always be necessary to run the scripts as root.

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo 22 days ago

As seen on... Replicant 6.0!

Hi everyone!
It's a pleasure to announce that, thanks to the great work done by Wolfgang, "RepWifi" is now available in Replicant 6.0 too! (Thank you, Wolfgang!)
I have upgraded the full set of connection management scripts and merged them with Wolfgang's one, in order to make them work in the up-coming next release.

Basic instructions are the same provided here

Attached to this post is the new package, containing both sets.

I have tested them in the default terminal bundled with R6.0, but I haven't been able to test them in the "Terminal Emulator" app, so I can't tell if the whole shortcut trick works in R6.0 too.. but I don't see why it shouldn't.

As always, comments, criticism and requests are very welcome!

Happy hacking

Fil

RepWiFi.tar.bz2 (17.4 kB)

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo 22 days ago

Wolfgang Wiedmeyer wrote:

In a previous version of the script, I did run "ifconfig "$iface" up" before launching wpa_supplicant. And I had these kind of issues before, but they disappeared after some changes and I removed the ifconfig command as it didn't seem to be necessary anymore. You could add the ifconfig initialization command. Maybe it solves the issue.

Indeed, that did the trick, thanks ;)

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Jacob Bahn 16 days ago

This is so great. Unfortunately I find it a bit confusing trying to figure out exactly what and how to do.

I know you are all busy, but I would really appreciate a simpler 'manual'. If provided I will try to install and provide feedback asap.

Thanks

/Jacob

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo 13 days ago

Jacob Bahn wrote:

I know you are all busy, but I would really appreciate a simpler 'manual'. If provided I will try to install and provide feedback asap.

Hi there.
I'm sorry if it is still a bit hard for users to have it working.
Unfortunately there is currently no way other then bare shell commands to connect to a WiFi network.
If you're unfamiliar with the shell things can get confusing.

I guess you already tired to follow the instructions provided in the first few posts on this thread.
I thought they were clear enough, but it seems they aren't.

In general, it should be sufficient to download the package from this post
Then, extract the correct folder for your version of replicant (either 4.2 or 6.0) on your host computer.
Then, connect your phone to the host computer via USB and enable usb debugging on the phone (Settings > Developer Options > USB debugging)
On the host computer, open a shell and "cd" into the directory where you have extracted the scripts (either "v1.3" or "v1.4").
From within this directory run:

adb root

Check your phone, if it asks for you to allow your computer to access ADB via USB. If it does, then grant the permission.
Then on the same shell, from within the "v1.3" directory (for Replicant 4.2) or the "v1.4" directory (for Replicant 6.0) run:
bash setup.sh

The "setup.sh" script should do everything for you, placing the needed scripts on your phone.
Now you can unplug the phone from the computer, plug in a usb OTG cable with the WiFi adapter connected to it.
From now on, you can follow this instructions to use WiFi connectivity.

If you need any other help, please ask here, reporting exactly for which specific steps you need help.
I'll be glad to help you!
Without specific requests I can do nothing more than point you back again to the original instructions.

Hope you succeed in using the scripts!

Happy Hacking!

Fil

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Timothy Magee 11 days ago

I have been following this discussion for several weeks now and eventually was overcome by the urge to purchase a Samsung Galaxy SIII i9300. I installed Replicant 6.0 and it works. I downloaded the scripts for WiFi and they worked . . . nearly.

Unfortunately, the scripts as written do not quite work on Galaxy S3 with Replicant 6.0. I have made some changes to them to make them work. As written, the scripts believed that the working directory was /data/misc/wifi. However, they were actually in /data/misc/wifi/scripts. I have fixed this and now setup.sh pushes the scripts /data/misc/wifi.

I also added support for unsecured networks.

Note: this is my firs tcontribution to the project and as such may contain BUGS. If you use these scripts they MIGHT BREAK YOUR PHONE. I cannot provide any warranty that these scripts will help you in any way.

On the other hand, they worked just fine for me and now allow me to connect to both WPA Personal and unsecured WiFi networks.

All my contributions are licensed under the GPLv3 or any later version.

With these scripts, I believe the instructions previously posted should actually work.

PS: For completeness I have included the old, unchanged, unmodified scripts for Replicant 4.2. I have not touched them, modified them or even looked at them. I just left them there and they do not have any new features I have added to the Replicant 6.0 scripts.

RepWiFi.tar.bz2 (20.4 kB)

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Fil Bergamo 10 days ago

Timothy Magee wrote:

Unfortunately, the scripts as written do not quite work on Galaxy S3 with Replicant 6.0. I have made some changes to them to make them work. As written, the scripts believed that the working directory was /data/misc/wifi. However, they were actually in /data/misc/wifi/scripts. I have fixed this and now setup.sh pushes the scripts /data/misc/wifi.

Thanks for correcting the issue!
I guess the incriminated line was the following (in the setup.sh script)

adb push ./scripts "$root_dir" 

I noticed you changed it to

adb push ./scripts/* "$root_dir" 

I think the previous one was working on my system (Trisquel 7), but I can't remember for sure.. anyway, I think your correction should work better..
Can you please confirm that the issue was in that line?
I'm no bash expert at all, so I'd like to learn if I was writing it the wrong way..

I also added support for unsecured networks.

That's really great too!
I spent my free time during the 2 days of FOSDEM trying to add that feature, but I haven't been able to work on that very much...

If I get it right, all you're doing is providing no password parameter to wpa_supplicant, if the network is unsecured...
Does it work for you? Because that was the first thing I tried, but it didn't work for me, unless with FOSDEM's open wifi network..
I was researching about explicitly telling wpa_supplicant that the network has no password with:

set_network 0 key_mgmt NONE

And I nearly had it working.. The connection to the AP was established, but then I couldn't get no gateway..
I haven't been able to try anymore, so now I'm curious to know if that could have been the right way to go..
..or not! :)

Can you please help me in this regard?

Thank you very much again for your contribution!

Fil

P.S. it would be fair if you added your copyright notice to the licensing header of the file(s) you edited, to get proper credit for your helpful contribution ;)

RE: WiFi works!!! A successful experience. [ SCRIPTS PROVIDED ] - Added by Timothy Magee 10 days ago

It sounds like we both don't know much about shell scripting. I guess we get to learn together!:-)

You asked if

-adb push ./scripts "$root_dir" 
+adb push ./scripts/* "$root_dir" 

fixed the first problem. Yes, it did. I still don't know if that is the 'correct' way to do it. I know that it works and that is what matters to me.

Also, I used,

set_network 0 key_mgmt NONE

to connect to insecure networks. So far it seems that it works for me. I'm not sure why you couldn't get a gateway on insecure networks. I'll look into it more on my phone. I do know that captive portals are a pain. For me, Browser would not let me visit captive portal pages until I typed in the IP address for the captive portal server. I had to find that IP address on another device running another operating system. Obviously not the best situation. Maybe I'll find a solution to that to.

I also changed some other aspects of the scripts so that passwords are now optional. For instance connect.sh now assumes that a network does not need a password if you don't give it one. The same way with add_network.sh.

Finally, I also posted the entire code to github. I know github is not the best hosting solution, but I have it and it works. If you use github and want commit access, just like send me your username or however its done and I'll add you.

https://github.com/TimMagee/ReplicantWiFi/

PS: I will update the copyright notice to reflect that we are joint authors on those files as soon as I get the chance.

(1-17/17)