Installing Prolog working environment for Ubuntu

In this blog, hopefully I will kickstart you to learn prolog using swi-prolog.

First, let’s add the repository we will use to install the stable version of swi-prolog:

sudo apt-add-repository ppa:swi-prolog/stable

Update your repository:

sudo apt-get update

And the last step of the installation, installing the swi-prolog:

sudo apt-get install swi-prolog

Let’s test our prolog working environment,

Prolog needs a knowledge base to understand the rules,

Create a knowledge-base:

touch knowledgebasetest

Enter some rules:

 
animal(cat).
animal(bird).
animal(human).
insect(ant).

average(X,Y):- S is (X + Y) / 2, write(S).
sum(X,Y):- S is X + Y, write(S).
sub(X,Y):- S is X - Y, write(S).
multiply(X,Y):- S is X * Y, write(S).
divide(X,Y):- S is X / Y, write(S).

After we save our knowledge base, run the swipl with it to load it to our program:

swipl knowledgebasetest

In sumamry:
” :- “, here is an “if”,
” , ” here is an “and”,
” . ” here is a “;” (semi-colon)

In addition, you can just capitalize the first letter of a word to make it a variable.

After we load the program we will have a screen giving the following:

Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.3)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- 

In here, let’s send a query to our program test the rules.

 ?- animal(cat).

We can think it as asking if a cat is an animal.

Rest is same so I will pass to functions.

 ?- divide(600,15).
40 true.

 

C# – Dynamic methods with lambda expressions

 

 

Lambda expression is a function that is not bound with an identifier

The logic behind this is like the one shown below.

(parameters) => expression

I know it looks weird so I will try to make it look less weirdo.

using System;

namespace lambdaexpressions
{
    public class Program
    {
        
        public static void Main(string[] args)
        {
            
			Action greetings = () => { Console.WriteLine("Hi, Sir! Welcome."); };
 			greetings();

  			Action saymyname = (name) => { Console.WriteLine("Hi "+name+"! Have a nice day!" ); };
			saymyname("John Doe");
			saymyname("Jane Doe");
			saymyname("Mehmet Burak");
  			
            
        }
        
    }
}

Here, Lambda expressions let us use dynamic functions.

Output:

Hi, Sir! Welcome.
Hi John Doe! Have a nice day!
Hi Jane Doe! Have a nice day!
Hi Mehmet Burak! Have a nice day!
using System;

namespace lambdaexpressions
{
    public class Program
    {
		static void nameandsurname(Action callback,string surname){
		
			Console.Write("Hi, ");
			callback();
			Console.WriteLine(surname + "! How are ye?");
			
		}
		
        public static void Main(string[] args)
        {
            string gender = "male";
	    nameandsurname( () => { if(gender =="male"){ Console.Write("John "); }else if(gender =="female"){	Console.Write("Jane ");	}},"Doe");
        }
    }
}

In here, we used System.Action to redirect the parameters to our function.
Output:

Hi, John Doe! How are ye?

C# – Delegate

What the heck is a delegate?

Delegate:

– Function pointers
– Can be changed run-time
– Comes from System.Delegate

using System;

delegate void myDelegate(string name);

namespace delegatedis
{
    public class Program
    {
        static void women(string name){
            Console.WriteLine("Hi Miss "+name+"! Have a nice day!");
        }

        static void attackhelicopter(string name){
            Console.WriteLine("Hi Apache " +name+"! gl hf!");
        }
        
        public static void Main(string[] args)
        {
            myDelegate mydel;
            string name = "Jane Doe";
            mydel = women; // We can use mydel like it is a pointer.
            mydel(name);
            string name = "AH-64";
            mydel = attackhelicopter; 
            mydel(name);
            
        }
    }
}

Out:

Hi Miss Jane Doe! Have a nice day!
Hi Apache AH-64! gl hf!

Installing Lisp working environment slime with Emacs in Ubuntu

No matter how hard it looks emacs is not that hard to use, at least for basic purposes.

 

Let’s install emacs first:

sudo apt-get install emacs -y

Install Steel bank common lisp the Common Lisp compiler:

sudo apt-get install sbcl

Install the slime(Superior Lisp Interaction for Emacs) for Emacs:

sudo apt-get install slime

Now, we can open the emacs

emacs

Press “Alt – X” to jump to the command line and type “slime” and open up our working environment.

For example, let’s code a function that returns the double of given integers.

We will have a screen like the one below;

; SLIME 2015-08-24
CL-USER>

We can start coding here:

(defun double (x) (* x 2))

After we complete the function we can call it and give an input.

(double 6)

It will return 12 as you know.

Good luck, have fun!

 

 

Installing DHCP Server to Raspberry Pi 3

In this blog, I am going to show you how to set up a DHCP on Raspberry Pi 3 and assign IP addresses to the devices in our network.

Let’s make sure our Pi is up-to-date:

sudo apt-get update && sudo apt-get upgrade

We will use isc-dhcp-server:

sudo apt-get install isc-dhcp-server -y

It is OK to get errors as we did not set our DHCP Server.

To make our setting we will edit “dhcpd.conf”

sudo vi /etc/dhcp/dhcpd.conf

Set it up like the network we want, I want my network to:

  • use 10.0.0.0/24 IP Addresses,
  • route it over 10.0.0.1,
  • have 255.255.255.0 netmask,
  • have a range of 10.0.0.10-10.0.0.30 IP Addresses.
subnet 10.0.0.0 netmask 255.255.255.0 {
 range 10.0.0.10 10.0.0.30;
 option routers 10.0.0.1;
}

Save it:

sudo dhcpd -cf /etc/dhcp/dhcpd.conf

Set the interface’s IP Address with ifconfig and restart the service:

sudo ifconfig eth0 10.0.0.1
sudo service isc-dhcp-server restart

After we connect it to a hub or a switch with devices connected to it, we will see the devices getting the IP Addresses.

Restart the service if it doesn’t.

Trying to hack the CX-32W drone to use it in my object tracking drone project

I am not going to touch the OpenCV part in this blog. Just the hacking attempt I guess. Our drone can be controlled by a remote controller and a smartphone app. The app is communicating over Wifi so you have to connect it to the drone’s wifi hotspot in order to control it and I want to configure the application to develop my own SDK so I will start with capturing the packets between my smartphone and the drone to see what’s going on and after that decompile the application to see if I can figure it out.

The drone is “CX-32W”, a “Cheerson Hobby” brand multicopter. It has 4 CW/CCW DC Motors, 7.4V 450 mAh Li-Po Battery, FPV Cam, a transmitter and a “Lewei50 single-board computer”. This mini computer has an operating system and modules in it.

Link!

I used nmap to scan the open ports. And I noticed ftp port is open. And tried to mess with it.The username was “root” and password was just blank. But unfortunately we don’t have the write permission just only read. Tried some combinations for the telnet, it didn’t work.

root@localhost:~# nmap -p 1-65535 192.168.0.1
Starting Nmap 7.40 ( https://nmap.org ) at 2017-03-30 19:52 +03
Nmap scan report for 192.168.0.1
Host is up (0.053s latency).
Not shown: 65528 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
23/tcp   open  telnet
6789/tcp open  ibm-db2-admin
6790/tcp open  hnmp
7060/tcp open  unknown
8060/tcp open  aero
9060/tcp open  CardWeb-IO
MAC Address: 28:F3:66:CB:1D:78 (Shenzhen Bilian electronic)
Nmap done: 1 IP address (1 host up) scanned in 64.19 seconds

Captured the packets between the drone and my smart phone. Had some over 50000 UDP, 7060 TCP,8060 TCP ports. 192.168.0.1

I started looking at 50000 UDP first. The interface may look confusing. So i will try to explain it. Blue and “<—-” ones are the packets that sent by my application to drone. I figured out it’s “idle” state and my drone has to send them to keep connected.

We can imitate the communication with our computer by sending the same packets to the drone over wifi to it’s 50000 UDP Port.

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

UDP_IP = '192.168.0.1'
UDP_PORT = 50000
msg = bytes.fromhex('CC 80 80 80 80 00 00 33')
sock.sendto(msg, (UDP_IP, UDP_PORT))
sock.sendto(msg, (UDP_IP, UDP_PORT))
sock.sendto(msg, (UDP_IP, UDP_PORT))
sock.sendto(msg, (UDP_IP, UDP_PORT))
msg = bytes.fromhex('CC 82 7F 80 80 00 FD 33')
while True:
    sock.sendto(msg, (UDP_IP, UDP_PORT))
sock.close

The 8060 Port sending “lewei_cmd” and drone responds to it.

And the 7060, we have captured 6.3 MBs of packet over this port. I checked the app and the drone’s files and found out this is a H.264 I-Frame type data. I-Frame is like sends only the changed pixels to save data. Idk how this affects the readability of packets.

A packet of a single frame:

Checked the activity_main.xml to see if I can find out how to read the packets and explored a new world.

<com.lewei.multiple.lw93.MySurfaceView android:id=”@id/mySurfaceView” android:layout_width=”-1″ android:layout_height=”-1″ />