on Android, Coding

validate ip address using regex

The code snippet below will check if the passed string is a valid IP address or not .
An Internet Protocol address (IP address) is a numerical label assigned to each device participating in a computer network that uses the Internet Protocol for communication. Read more about IP address here. This snippet will validate IP version 4 addresses.

/**
 * validate IP address
 * @param String IP address to verify
 * @return Boolean result
 */
public static Boolean isIP(String input) {
    if (input!= null && !input.isEmpty()) {
        //Regex
        String regex = "^((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]\\d)|\\d)(\\.((25[0-5])|(2[0-4]\\d)|(1\\d\\d)|([1-9]\\d)|\\d)){3}$";
        //check if input matches the regex
        if (text.matches(regex)) {
            // valid ip
            return true;
        } else {
            // invalid ip
            return false;
        }
    }
    // invalid input
    return false;
}  

 

SIMCOM SIM800H change in hardware. S2-105HG / S2-10651 , S2-1065N / S2-1065Q

SIMCOM has updated the hardware of SIM800H modules with hardware versions (S2-105HG / S2-10651) due to the fact that the power amplifier chip that these modules used (RF7176) is discontinued from production line. The Firmware also had to be changed due to modifications necessary for operation of the new power amplifier chips.

All the new modules ( with hardware versions(S2-1065N / S2-1065Q) ) will be having the new power amplifier chips namely RF7198. According to SIMCOM this change in hardware wont affect the properties and characteristics of the module.

Please note that:

  • firmware for the new hardware version of module differs from the old version of the firmware. Therefore, a new hardware version module can not be flashed with the firmware for the old hardware version module.
  • firmware image files for the old and new hardware versions will be with different names. Firmware for the old version is designated “1308B06SIM800H32_BT” while the firmware for the new version is designated “1309B06SIM800H32_BT”.

sim800H

Firmware files for SIM800H

All the firmware files can be found at the firmware collection page of this blog. If you want to contribute the files you have you can do so by uploading your files to user uploads section or by emailing me the files you have.

 

 

 

isOnline() check if android device has internet connectivity

What do you want?

  • If you just want to check for a connection to any network – not caring if internet is available – then most of the answers here (including the accepted), implementing isConnectedOrConnecting() will work well.
  • If you want to know if you have an internet connection (as the question title indicates) please read on

Ping for the main name servers

public boolean isOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {
       Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    } catch (IOException e)          { e.printStackTrace(); } 
      catch (InterruptedException e) { e.printStackTrace(); }
    return false;
}

That’s it! Yes that short, yes it is fast, no it does not need to run in background, no you don’t need root privileges.

Possible Questions

  • Is this really fast enough?Yes, very fast!
  • Is there really no reliable way to check if internet is available, other than testing something on the internet?Not as far as I know, but let me know, and I will edit my answer.
  • Couldn’t I just ping my own page, which I want to request anyways?Sure! You could even check both, if you want to differentiate between “internet connection available” and your own servers beeing reachable
  • What if the DNS is down?Google DNS (e.g. 8.8.8.8) is the largest public DNS service in the world. As of 2013 it serves 130 billion requests a day. Let ‘s just say, your app not responding would probably not be the talk of the day.
  • Which permissions are required?
    <uses-permission android:name="android.permission.INTERNET" />

    Just internet access – what surprise ^^ (BTW have you ever thought about, how some of the methods suggested here could even have a remote glue about the availability of internet, without this permission?)

 

USB OTG as an Extra USB Host in Allwinner A10, A20 etc

USB OTG port on Allwinner SoC based boards is a very useful tool. It can be used to flash an image to the flash memory or connect to a Host PC and making the board to act as a device.

Although there are many scenarios where USB OTG is useful, at times it is more useful to have an extra USB HOST on the board. All the settings of hardware configurations of allwinner SoCs are stored in a file named ` script.bin` that is compiled from a text file called fex file.We can learn more about the fex file from Sunxi Wiki in the Fex Guide.

A FEX file defines various aspects of how the SoC works. It configures the GPIO pins and sets up DRAM, Display, etc parameters.

Each line consists of a key = value pair combination under a [sectionheader]. All three, [sectionheader], key and value are case-sensitive. For comments a semi-colon (;) is used and everything following a semi-colon is ignored. The chip does not parse a textual version of a fex file, it gets cleaned and compiled by a fex-compiler. A reverse engineerd open source version exists in the sunxi-tools repository. Also a de-compiler which takes a binary script.bin and creates a textual script.fex. Usually, script.bin can be found on the nanda boot partition on A10 devices.

Changing script.bin file without removing the microSD card

The tools for script.bin changing are located in /opt/sunxi-tools directory:

  # cd /opt/sunxi-tools
  # ./chscr.sh

This will convert script.bin file from sdcard to script.fex file and the file will be opened using nano editor. Now you can change the board modules and parameters, save the changes (“CTRL”+”X”; confirm with “Y”) and exit (“CTRL”+”X” again) from nano editor.

  # ./wrscr.sh

this will convert script.fex to script.bin and the script.bin file will be written to sdcard.

  reboot

Reboot the board and the new settings would be enabled.

Changing script.bin file by removing the microSD card

The biggest part of the board configuration might be edited, changed or improved in a file called script.bin

The script.bin file can usually be found in the main directory of a microSD card prepared with official Debian image. The folder containing the script can be inspected under both Windows, Linux or Mac.

You can’t directly edit binary file so you would need to convert it to text format (it is called fex in this case), then edit the parameters via a text editor and finally switch it back to binary format.

IMPORTANT! ADJUSTING SCRIPT.BIN WITH IMPROPER VALUES MIGHT BREAK YOUR DEBIAN IMAGE AND IT IS ALWAYS RECOMMENDED TO KEEP A BACK-UP OF YOUR DEFAULT SCRIPT.BIN

To convert back and forth the script.bin you might use different tools. You can find Windows tools here: SUNXI TOOLS FOR WINDOWS . For Linux convertors please check the sunxi tools here: SUNXI TOOLS

Editing Fex File for USB OTG

Now open the Fex file and locate the USB section.

;——————————————————————————- 
;[usbc0]:  The configuration of controller 0 
;usb_used: Enable bit. If USB is enabled or not. 1: enable; 0: disable. 
;usb_port_type: Type of USB port. 0:device only;1:host only;2:OTG 
;usb_detect_type: Detect type. 0: Not detect; 1: detect vbus/id; 2: Detect id/dpdm
;——————————————————————————- 
;——————————————————————————- 

;——————————————————————————-
[usbc0]
usb_used = 1 
usb_port_type = 0 
usb_detect_type = 0 
usb_id_gpio = port:PH04<0><1><default><default> 
usb_det_vbus_gpio = “axp_ctrl” 
usb_drv_vbus_gpio = port:PB09<1><0><default><0> 
;usb_restrict_gpio = port:PH00<1><0><default><0> 
usb_host_init_state = 0 
usb_restric_flag = 0 
usb_restric_voltage = 3550000 
usb_restric_capacity= 5

We just need to change usb_port_type from 0 to 1 to make it a USB host.

Converting fex to script.bin:

The following commands are executed at sunxi-tools directory:

$./fex2bin script.fex >script.bin
$sudo cp script.bin /boot/script.bin
$sudo umount /boot
Now restart the board and you can use USB OTG as USB host.
 

Eagle footprint library for Allwinner A10 SoC.

The Allwinner A10 is a family of single-core SoC devices designed by Allwinner Technology from Zhuhai, China. Currently the family consists of the A10, A13, A10s and A12. The SoCs incorporate the ARM Cortex-A8 as their main processor and the Mali 400 as the GPU.

The Allwinner A10 is known for its ability to boot Linux distributions such as Debian, Ubuntu, Fedora, and other ARM architecture-capable distributions from an SD card, in addition to the Android OS usually installed on the flash memory of the device.

Here I am sharing the footprint for Allwinner A10 Soc chip taken from Olimex design.

Capture

 

 

Raspberry pi Zero launched , SBC priced at $5

This is awesome. The only thing I miss is the LAN interface but at $5 its still amazing. It has Broadcom BCM2835 processor and that also 40% faster than the pi 1 with 512 MB of Ram.

Today, I’m pleased to be able to announce the immediate availability of Raspberry Pi Zero, made in Wales and priced at just $5. Zero is a full-fledged member of the Raspberry Pi family, featuring:

  • A Broadcom BCM2835 application processor
    • 1GHz ARM11 core (40% faster than Raspberry Pi 1)
  • 512MB of LPDDR2 SDRAM
  • A micro-SD card slot
  • A mini-HDMI socket for 1080p60 video output
  • Micro-USB sockets for data and power
  • An unpopulated 40-pin GPIO header
    • Identical pinout to Model A+/B+/2B
  • An unpopulated composite video header
  • Our smallest ever form factor, at 65mm x 30mm x 5mm

Raspberry Pi Zero runs Raspbian and all your favourite applications, including Scratch, Minecraft and Sonic Pi.

RaspberryPi BLOG

 

Android: How to get MAC address of Ethernet port (not WiFi)

There are some android devices that have an ethernet (LAN) port for example Android STB, Android powered door-phone monitors, etc. The regular way of getting MAC address is as follows

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();

Not to forget adding the following permission in the AndroidManifest.xml 

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

The above method will give us the MAC address of WiFi. Assuming your Ethernet interface is eth0, to get the MAC address of Ethernet port we have to try opening and reading the file /sys/class/net/eth0/address. This can be Implemented as below.

public static String loadFileAsString(String filePath) throws java.io.IOException{
    StringBuffer data = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    char[] buf = new char[1024];
    int numRead=0;
    while((numRead=reader.read(buf)) != -1){
        String readData = String.valueOf(buf, 0, numRead);
        data.append(readData);
    }
    reader.close();
    return data.toString();
}

public String getMacAddress(){
    try {
        return loadFileAsString("/sys/class/net/eth0/address")
                .toUpperCase().substring(0, 17);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}