1/27/2017

Distributed Databases - Bond Energy Algorithm

這個演算法算是在去唸書期間,以第一學期來說,算是最麻煩的,即使公式都給了。這門課是講關於分散式系統資料庫,對我來說跟天書沒什麼兩樣,因為那時連資料庫碰都沒碰過,一點概念都沒有。而且現在才紀錄下來好像太晚了點.....都忘了差不多了XDD

總而言之,這門課的第一作業就是叫我們實現Bond Energy Algorithm。在講它之前,有必要先講一些分散式資料庫的簡單觀念說一下。

分散式資料庫是一群資料庫系統(或是所謂的節點)的集合,它們都是透過網路來連結,而且彼此互相之間是有關聯的。它被應用在非常廣的範圍,像是Google 就是用分散式系統管理資料庫,不然一個資料點掛了就全部陣亡。舉例來說,小明在台北市查找一本書。實際上這連線是不止會傳回台北所在的資料,也會向其它縣市的資料庫進行查尋(如果沒有或是全部查找)。

在分散式資料庫系統中,我們通常須將存放資料的表格進行分割,並放在多個資料庫節點上。而這分割又被分為Horizontal / Vertical fragmentation。而BEA就是在設計Vertical Fragmentation Algorithm時,基於Attribute affinity values,找出一組Attirbutes的關聯的意義。

BEA的演算法公式如下 :




1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Bond Energy algorithm
        input: AA (Attribute Affinity matrix)
        output: CA (Clustered Affinity matrix)
     */

     //copy old AA matrix to new AA in order to avoid index mistake
     AAnew = new double[MATRIX_SIZE+1][MATRIX_SIZE+1];
     CA = new double[MATRIX_SIZE+2][MATRIX_SIZE+2];
     tempColumn = new double[MATRIX_SIZE+1];

     for(int i = 1; i < MATRIX_SIZE+1; i++) {
       for(int j = 1; j < MATRIX_SIZE+1; j++) {
         AAnew[i][j] = AA[i-1][j-1];
        // System.out.println("AAnew["+i+"]["+j+"] = "+AAnew[i][j]);
       }
     }

     //fill columns and rows
     for(int i = 0; i < MATRIX_SIZE+1; i++) {
       CA[0][i] = i;
      for(int j = 0; j < MATRIX_SIZE+1; j++) {
         CA[j][0] = j;
       }
     }

     for(int i = 0; i < MATRIX_SIZE+1; i++) {
       AAnew[0][i] = i;
       for(int j = 0; j < MATRIX_SIZE+1; j++) {
         AAnew[j][0] = j;
       }
     }


     // Copy the first and second columns from AA
     for(int i = 1; i < MATRIX_SIZE+1; i++) {
       CA[i][1] = AAnew[i][1];
       CA[i][2] = AAnew[i][2];
     }

     index = 3; // start from third index
     RightMostIndex = 2; //current rightmost index of AA.
     while(index <= MATRIX_SIZE) {
       double contrib = 0;
       double maxContrib = 0;
       //System.out.println("------ index = "+index);

       for(int i = 1; i < index; i++) {
          contrib = cont(CA[0][i-1], index, CA[0][i], AAnew);
          //if found the highest contribution, record it
          if(contrib > maxContrib) {
            maxContrib = contrib;
            record(CA[0][i-1], index, CA[0][i]);
          }
        }
        contrib = cont(CA[0][index-1], index, index+1, AAnew);

        if(contrib > maxContrib) {
          maxContrib = contrib;
          record(CA[0][index-1], index, index+1);
        }
       /* place the highest contribution */
       CAplacement();
       index++;
     }
     CArowplace();
     CAprint();
   }

   /* This function is to order rows according the placement of columns */
   public static void CArowplace() {
     //double[][] temp;
     CAfinal =  new double[MATRIX_SIZE+2][MATRIX_SIZE+2];

     for(int i = 0; i <= MATRIX_SIZE; i++) {
       double row = CA[0][i];
       CAfinal[0][i] = row;

       for(int j = 0; j <= MATRIX_SIZE; j++) {
         //temp[i][j] = CA[(int)row][j];
         //System.out.println("row ="+(int)row);
         CAfinal[i][j] = CA[(int)row][j];
       }
     }
   }


看起來雖然滿複雜,但實現這公式是滿簡單的。麻煩的是要算出AA matrix。當時老師不打算用課本上的演算法,改用一個叫做 Cosine-based similarity 的算法。所以AA matrix 的算法變成如下 :




1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Start - Attribute Affinity algorithm */
     AA = new double[MATRIX_SIZE][MATRIX_SIZE];
     DecimalFormat df = new DecimalFormat("0.0000");

     for(int i = 0; i < att.size(); i++) {
       for(int j = 0; j < att.size(); j++) {
         double Aik = 0;
         double Ajk = 0;
         double AffNumerator = 0;
         double AffDenominator = 0;
         double temp = 0;
         double temp1 = 0;
         double TempSqrt = 0;
         double Temp1Sqrt = 0;
         for(int k = 0; k < query.size(); k++) {
           double SumAcc = 0;
           /* sum all accMatrix then multiply use(qk,Ai)*/
           for(int site = 0; site < 3; site++) {
             SumAcc = SumAcc + accMatrix[k][site];
           }
           Aik = beQueried[k][i] * SumAcc;
           Ajk = beQueried[k][j] * SumAcc;

           AffNumerator = AffNumerator + (Aik*Ajk);
           temp = temp + Math.pow(Aik,2);
           temp1 = temp1 + Math.pow(Ajk,2);
         }
         TempSqrt = Math.sqrt(temp);
         Temp1Sqrt = Math.sqrt(temp1);
         AffDenominator = TempSqrt * Temp1Sqrt;
         //System.out.println("Affn = "+AffNumerator);
         //System.out.println("Affd = "+AffDenominator);
         AA[i][j] = Double.parseDouble(df.format(AffNumerator / AffDenominator));
         System.out.println("AA["+i+"]["+j+"] = "+AA[i][j]);
       }
     }


這個程式都放在Github,需要的人請自取

整個過程都有註解,所以應該還滿好理解,只是執行速度太慢就是了...而且當時寫這作業沒想其它的,看起來真有點亂...

1/17/2017

I'm back

On 17th of November, I finally completed my degree of Master in Australia. This three-year journey took me a lot of efforts on assignments and projects, but really benefited from those activities during the study. To be honest, I'd never thought that one day I would have been able to graduate from foreign schools and even worked on there. I've truly appreciated my parents who give me such an opportunity to study abroad, and also thankful my girlfriend company with me in the long distance.  Of course, there are many people who care about me that I really appreciate as well.

Although I'm still studying IELTS test in order to apply for PR visa after the graduation ( under 0.5 in part of listening...), updating what I've learnt from the university on this blog is my next step! I hope that by writing the blog, I could be able to organise my thoughts and review the things I learnt.

By the way, if anyone would like to look for a partner for practising IELTS speaking or something else, you can just simply send an email to me :) 

10/16/2012

Can not connection to Softap for security

Kernel : 2.6.37

WiFi module : WG7310 ( Inside WL127x )

在Android Jelly Bean 底下,當打開hostspot時,發現只能連接OPEN方式,任何加密都無法被連接上。

起初以為是自己從ICS移稙到JB時,Frameworks出了問題,結果不是。

底下是錯誤的log :

D/hostapd ( 2058): random: Got 5/20 bytes from /dev/random
I/hostapd ( 2058): random: Only 5/20 bytes of strong random data available from /dev/random
I/hostapd ( 2058): random: Not enough entropy pool available for secure operations
I/hostapd ( 2058): WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects
D/hostapd ( 2058): GMK - hexdump(len=32): [REMOVED]
D/hostapd ( 2058): Key Counter - hexdump(len=32): [REMOVED]
D/hostapd ( 2058): WPA: Delay group state machine start until Beacon frames have been configured
D/hostapd ( 2058): WPS: Building WPS IE for (Re)Association Response
D/hostapd ( 2058): WPS:  * Version (hardcoded 0x10)
D/hostapd ( 2058): WPS:  * Response Type (3)
D/hostapd ( 2058): WPS:  * Version2 (0x20)
D/hostapd ( 2058): nl80211: Set beacon (beacon_set=0)
D/hostapd ( 2058): WPA: Start group state machine to set initial keys
D/hostapd ( 2058): WPA: group state machine entering state GTK_INIT (VLAN-ID 0)
D/hostapd ( 2058): GTK - hexdump(len=16): [REMOVED]
D/hostapd ( 2058): WPA: group state machine entering state SETKEYSDONE (VLAN-ID 0)
D/hostapd ( 2058): wpa_driver_nl80211_set_key: ifindex=6 alg=3 addr=0x4006f0f1 key_idx=1 set_tx=1 seq_len=0 key_len=16
D/hostapd ( 2058):    broadcast key
D/hostapd ( 2058): nl80211: set_key failed; err=-2 No such file or directory)
D/hostapd ( 2058): wpa_driver_nl80211_set_operstate: operstate 0->1 (UP)

一直以為是TI的wpa_supplicant出了問題....結果竟然也不是 !

原來是kernel的某個config沒被打開,造成hostapd去產生一個隨機的dummy key時錯誤,wpa_supplicant去set key時自然會fail...

不清楚是不是所有的secure key都會通過 "/dev/random"  去對kernel做儲存,但就目前SoftAP而言,wpa_supplicant都是透過這種方式去儲存和產生所有的key number.  (STA就不知道了....)

在kernel config 把 "CONFIG_CRYPTO_ANSI_CPRNG" 打開就可以解決該問題了  !

8/22/2012

NCLP R5-SP1 WLAN Dirver for modules of Jorjin the inside TI WL12xx chip


#################################################################
#   README
#
#   Introduction Wlan Driver For Jorjin SiP Modules
#
#   NLCP Version        : R5_SP1
#   Operation system    : Linux / Android
#   Date                : July,24 2012
#   Mantainer           : Dicky Chiang <dickychiang@jorjin.com.tw>
#
#   Jorjin Technologies Inc - http://www.jorjin.com.tw/
#
#################################################################

NLCP(Native Linux Communication Package) is open source,which to integrated for TI.

The wlan driver will to support WG72xx/WG73xx/WG75xx of the Jorjin SiP modules.

Wirelees solutions of the between TI and Jorjin:
* WG72xx - Inside WL125x chip
* WG73xx - Inside WL127x chip
* WG75xx - Inside WL128x chip

Download site:

# git clone git://github.com/dickychiang/compat-wireless-r5-sp1.git

====================================================================================================
1. Build Instructions

Setting your environment:

# export YOUR_PATH=`pwd`
# export DRIVER_PATH=${YOUR_PATH}/
# export ARCH=arm
# export CROSS_COMPILE=
# export PATH=$PATH:
# export DESTDIR=
# export KLIB=
# export KLIB_BUILD=$KLIB

Building:

# make modules     # Only modules
# make install     # Build modules & install to your filesystem

====================================================================================================
2. Firmware

Choose for your module type then to copy into the filesystem.

For Android:
# cp ${DRIVER_PATH}/jorjin/firmware/WG73xx_fw/*.bin \
     /etc/firmware/ti-connectivity/
For Linux:
# cp ${DRIVER_PATH}/jorjin/firmware/WG73xx_fw/*.bin \
     /lib/firmware/ti-connectivity/

====================================================================================================

3. Copy driver module

# cp ${DRIVER_PATH}/compat/compat.ko                           $(DESTDIR)/system/lib/modules
# cp ${DRIVER_PATH}/drivers/net/wireless/wl12xx/wl12xx.ko      $(DESTDIR)/system/lib/modules
# cp ${DRIVER_PATH}/net/mac80211/mac80211.ko                   $(DESTDIR)/system/lib/modules
# cp ${DRIVER_PATH}/net/wireless/cfg80211.ko                   $(DESTDIR)/system/lib/modules
# cp ${DRIVER_PATH}/drivers/net/wireless/wl12xx/wl12xx_sdio.ko $(DESTDIR)/system/lib/modules
# cp ${DRIVER_PATH}/drivers/net/wireless/wl12xx/wl12xx_spi.ko  $(DESTDIR)/system/lib/modules

====================================================================================================
4. Insert module

Go to you filesystem then according to as below for insert module.

# insmod compat.ko
# insmod cfg80211.ko
# insmod mac80211.ko

Choose for your module type.
# insmod wl1251.ko
# insmod wl1251_sdio.ko

====================================================================================================
************************************
* Related WLAN tools & application *
************************************

1. Install SoftAP & wpa_supplicant_8 with R5-SP1 WLAN driver
# git clone git://github.com/dickychiang/hostap.git
# cd hostap
# git checkout R5_SP1_Maintenance_Release
# cd ../ ; cp -rf hostap/* $MYDROID/external/wpa_supplicant_8

2. Install ti-utils (calibration tools)
# git clone git://github.com/dickychiang/ti-utils.git
# cd ti-utils
# git checkout R5_SP1_Maintenance_Release
# cd ../ ; cp -rf ti-utils $MYDROID/external/ti-utils

3. Install IW (for Android version)
# git clone git://github.com/dickychiang/iw.git
# cd iw
# git checkout ol_R5.00.21
# cd ../ ; cp -rf iw $MYDROID/external/iw

4. Install crda(Central Regulatory Domain Agent)
# git clone git://github.com/dickychiang/crda.git
# cp -rf crda $MYDROID/external/crda

====================================================================================================
************************************
* How to Calibration *
************************************
The calibrator and other useful utilities for TI wireless solution,
based on wl12xx driver.

Calibration is a process in which specific radio configuration parameters are
generated and saved into the NVS file, later to be used by the wl12xx driver
upon initialization.
These configuration parameters are specific to the chip on the specific design
and therefore are sent back to the driver to store in non-volatile memory for
later use. Upon initialization, the wl12xx driver will load an NVS file where
it expects to read those parameters and send them to the chip.

The NVS file contains 2 main parts - one stores the calibration parameters and
the other one stores initialization information required for the wl12xx driver.


1. Install ti-utils (calibration tools)
# git clone git://github.com/dickychiang/ti-utils.git
# cd ti-utils
# git checkout R5_SP1_Maintenance_Release
# make
Don't forget to set your environment before make.

# cp ./calibrator /bin/

2. Copy ini files into the filesystem
For Andriod:
# cp ${DRIVER_PATH}/jorjin/ini_files/ \
${TAGET_OUT}/etc/firmware/ti-connectivity/TQS_S_2.6.ini 
For Native Linux:
# cp ${DRIVER_PATH}/jorjin/ini_files/ \
${ROOT_FS}/lib/firmware/ti-connectivity/TQS_S_2.6.ini 

3. Execute calibrator on your board
# rmmod wl12xx_sdio.ko
# calibrator plt autocalibrate     
         Device name. Probably wlan0
  Full path to wl12xx_sdio.ko kernel module                                                                             
         Full path to Radio param ini file
         Full path of nvs file. Must be the real path as wl12xx will load it
For example: 
# calibrator plt autocalibrate wlan0 \
/system/lib/modules/wl12xx_sdio.ko \
/etc/firmware/ti-connectivity/TQS_S_2.6.ini \
/etc/firmware/ti-connectivity/wl1271-nvs.bin 00:01:02:03:04:05

Appendix:

How to change MAC address in calibrated NVS

# calibrator set nvs_mac  []

If the MAC address missing, the random valid value will be added

====================================================================================================
**********************
* Building for Linux *
**********************

1. Install libnl-2.0

First to download it:

# git clone git://github.com/dickychiang/libnl-2.0.git

And export environment or can reference to script 'm.sh'

# export ROOTFS_PATH=
# export PATH=:$PATH
# export CROSS_COMPILE=
# ./configure --prefix=${ROOTFS}  CC=${CROSS_COMPILE}gcc --host=arm-linux LD=${CROSS_COMPILE}ld
# make
# make install

====================================================================================================
2. Install IW tool for wireless configuration

Download source code :

# git clone git://github.com/dickychiang/iw-linux.git

Export environment or reference script 'm.sh' for compile.

# export PKG_CONFIG_PATH=${ROOTFS_PATH}/lib/pkgconfig:$PKG_CONFIG_PATH
# export PKG_CONFIG_LIBDIR=${ROOTFS_PATH}/lib/:$PKG_CONFIG_LIBDIR
# export NFSROOT=${ROOTFS_PATH}
# make PREFIX=${NFSROOT} CC=${CROSS_COMPILE}gcc
# make PREFIX=${NFSROOT} CC=${CROSS_COMPILE}gcc install
====================================================================================================
3. How to use iw tool

Usually is commonly commands the following.

# iw wlan0 scan                     // Scan for Wifi network
# iw wlan0 connect            // Connect to specifc Access point (by its SSID)
# iw wlan0 disconnect               // Disconnect from the Access point that the Station is connected too   
# iw wlan0 link                     // Verify Link Status

For example as below:

# iw wlan0 scan
BSS 00:13:46:dd:e2:e9 (on wlan0)
TSF: 362449530606 usec (4d, 04:40:49)
freq: 2452
beacon interval: 100
capability: ESS Privacy ShortPreamble (0x0031)
    signal: -50.00 dBm
    last seen: 270 ms ago
    SSID: jjap
    Supported rates: 1.0* 2.0* 5.5* 11.0* 22.0 
    DS Parameter set: channel 9
    ERP: 
    Extended supported rates: 6.0 9.0 12.0 18.0 24.0 36.0 48.0 54.

# iw wlan0 connect jjap keys 0:0397430202

# iw wlan0 link
Connected to 00:13:46:dd:e2:e9 (on wlan10)
SSID: jjap
freq: 2452
RX: 9400 bytes (96 packets)
TX: 0 bytes (0 packets)
    signal: -48 dBm
    tx bitrate: 1.0 MBit/s

    bss flags:      short-preamble
    dtim period:    3
    beacon int:     100

2/17/2012

Wlan driver of Jorjin SiP modules

if your use wlan modules for Jorjin, can reference my integrated of wlan driver in github.

The wlan driver will be supported WG7210, WG73x, WG75 modules,it running on the Linux 2.6.30 upon and Android GB/ICS.

So if your are interested, let us go ahead !

#################################################################
#

#   NLCP Version        : R4_12
#   Operation system    : Linux / Android
#   Date                       : Jan 2012
#   Mantainer               : Dicky Chiang
#
#################################################################

NLCP(Native Linux Communication Package) is open source with integrated for TI.

The wlan driver will to support WG72, WG73x, WG75x series of Jorjin SiP modules.

Dirver download site:

$ git clone git://github.com/dickychiang/compat-wireless-r4-12.git

========================================================================
1. How to build the driver

You can refer the script file "m.sh" for setting environment. For example:

# Export parameter
$ export ARCH=arm
$ export CROSS_COMPILE=
$ export KERNEL_PATH=
$ export KLIB=${KERNEL_PATH}
$ export KLIB_BUILD=${KERNEL_PATH}

# Compile it
$ make modules

========================================================================
2. Install .ko files

If you are compiled is completed, need to copy file ".ko" to your filesystem. Or reference the script file "cp.sh".

For example:

$ export ROOTFS=
$ cp compat/compat.ko ${ROOTFS}/lib/modules/
$ cp net/mac80211/mac80211.ko ${ROOTFS}/lib/modules
$ cp net/wireless/cfg80211.ko ${ROOTFS}/lib/modules

* WG72x
$ cp drivers/net/wireless/wl1251/*.ko ${ROOTFS}/lib/modules

* WG73x/WG75x
$ cp drivers/net/wireless/wl12xx/*.ko ${ROOTFS}/lib/modules

========================================================================
3. Firmware

Select your type of Jorjin SiP modules then to copy in filesystem.

For example:

$ cp jorjin/wg73x_fw/*.bin ${ROOTFS}/lib/firmware/ti-connectivity

========================================================================
4. Insert module

If boot in system, according to as below for insert module.

$ insmod compat.ko
$ insmod cfg80211.ko
$ insmod mac80211.ko

For your type.
$ insmod wl1251.ko
$ insmod wl1251_sdio.ko

********************************************
* Wireless Configuration (For Linux system)*
********************************************
----------------------------------------------------------------------------------------------------
IW commands are Linux based commands that are used as configuration utility for wireless devices.
IW commands also provide connection to WLAN devices however it is not supported advance security modes (supports Non secured networks and WEP only) therefore we normaly use that option for debug mode only while working without WPA supplicant.

IW commands is use 'nl80211' interface, so we need to install 'libnl' first then to support it.
----------------------------------------------------------------------------------------------------

1. Install libnl-2.0

First to download it:

$ git clone git://github.com/dickychiang/libnl-2.0.git

And export environment or can reference to script 'm.sh'

$ export ROOTFS_PATH=
$ export PATH=:$PATH
$ export CROSS_COMPILE=
$ ./configure --prefix=${ROOTFS}  CC=${CROSS_COMPILE}gcc --host=arm-linux LD=${CROSS_COMPILE}ld
$ make
$ make install
========================================================================
2. Install IW tool for wireless configuration

Download source code :

$ git clone git://github.com/dickychiang/iw-linux.git

Export environment or reference script 'm.sh' for compile.

$ export PKG_CONFIG_PATH=${ROOTFS_PATH}/lib/pkgconfig:$PKG_CONFIG_PATH
$ export PKG_CONFIG_LIBDIR=${ROOTFS_PATH}/lib/:$PKG_CONFIG_LIBDIR
$ export NFSROOT=${ROOTFS_PATH}
$ make PREFIX=${NFSROOT} CC=${CROSS_COMPILE}gcc
$ make PREFIX=${NFSROOT} CC=${CROSS_COMPILE}gcc install
========================================================================
3. How to using the iw tool

Usually, the following is commonly commands.

$ iw wlan0 scan                     // Scan for Wifi network
$ iw wlan0 connect           // Connect to specifc Access point (by its SSID)
$ iw wlan0 disconnect               // Disconnect from the Access point that the Station is connected too  
$ iw wlan0 link                     // Verify Link Status

For example as following:

$ iw wlan0 scan
BSS 00:13:46:dd:e2:e9 (on wlan0)
TSF: 362449530606 usec (4d, 04:40:49)
freq: 2452
beacon interval: 100
capability: ESS Privacy ShortPreamble (0x0031)
    signal: -50.00 dBm
    last seen: 270 ms ago
    SSID: jjap
    Supported rates: 1.0* 2.0* 5.5* 11.0* 22.0
    DS Parameter set: channel 9
    ERP:
    Extended supported rates: 6.0 9.0 12.0 18.0 24.0 36.0 48.0 54.

$ iw wlan0 connect jjap keys 0:0397430202

$ iw wlan0 link
Connected to 00:13:46:dd:e2:e9 (on wlan10)
SSID: jjap
freq: 2452
RX: 9400 bytes (96 packets)
TX: 0 bytes (0 packets)
    signal: -48 dBm
    tx bitrate: 1.0 MBit/s

    bss flags:      short-preamble
    dtim period:    3
    beacon int:     100