Raspberry Pi as a Wireless Router

73
Raspberry Pi 體驗嵌入式系統開發 - 自己動手做無線路由器 台灣樹莓派 <[email protected]> Oct 28, 2013/Raspberry Pi #1

description

 

Transcript of Raspberry Pi as a Wireless Router

Page 1: Raspberry Pi as a Wireless Router

用 Raspberry Pi 體驗嵌入式系統開發

- 自己動手做無線路由器

台灣樹莓派 <[email protected]>Oct 28, 2013/Raspberry Pi #1

Page 2: Raspberry Pi as a Wireless Router

● Raspberry Pi 好好玩 - 應用篇

● Raspberry Pi 好好玩 - GPIO 篇

相關議程

Page 3: Raspberry Pi as a Wireless Router

● 信用卡大小般的電腦

Raspberry Pi 是什麼 ?

http://www.flickr.com/photos/fotero/7697063016/

Page 4: Raspberry Pi as a Wireless Router

● SoC Broadcom BCM2835● CPU: ARM11 family, 700 MHz● GPU: Broadcom VideoCore IV, OpenGL ES 2.0,

1080p30 H.264/MPEG-4 AVC decoder● Memory: 512 Mb SDRAM

● Video outputs: Composite, HDMI● Audio outputs: 3.5 mm jack, HDMI● Onboard storage: SD, MMC, SDIO card slot● Onboard 10/100 Ethernet RJ45

Raspberry Pi 硬體規格 (Model B)

Page 5: Raspberry Pi as a Wireless Router

週邊 + OS

http://www.raspberrypi-tutorials.co.uk/starting-up-your-raspberry-pi-device/set-raspberry-pi/

Page 6: Raspberry Pi as a Wireless Router

Raspberry Pi 怎麼玩 ?

Page 7: Raspberry Pi as a Wireless Router
Page 8: Raspberry Pi as a Wireless Router

Raspberry Pi 還可以怎麼玩 ?

Page 9: Raspberry Pi as a Wireless Router
Page 10: Raspberry Pi as a Wireless Router
Page 11: Raspberry Pi as a Wireless Router

看了很多 還是不知道怎麼開始

Page 12: Raspberry Pi as a Wireless Router

Raspberry Pi 的軟硬整合之道

Page 13: Raspberry Pi as a Wireless Router

用 Raspberry Pi 體驗嵌入式系統開發

- 自己動手做無線路由器

Page 14: Raspberry Pi as a Wireless Router

● 為特定應用而設計的專用計算機系統 ● 通常執行的是帶有特定要求的預先定義的任務

- wikipedia

● 常見作法 : 特製硬體 + 嵌入式 Linux+Driver+ 客製化應用程式

嵌入式系統

Page 15: Raspberry Pi as a Wireless Router

● 硬體設計 & 驗證

● 軟體開發 & 系統整合

● 測試

嵌入式系統開發流程

Page 16: Raspberry Pi as a Wireless Router

● 無線 (wireless) + 路由器 (router)

無線路由器

Page 17: Raspberry Pi as a Wireless Router

使用情境

http://www.techiestate.com/how-to-set-up-a-wireless-router/

Page 18: Raspberry Pi as a Wireless Router

使用情境

http://www.techiestate.com/how-to-set-up-a-wireless-router/

實做目標

Page 19: Raspberry Pi as a Wireless Router

● 取得對外網路● 有線 / 無線?

● 如何發射訊號?● 網路的分配?● 使用者介面?● … 未知的問題

情境 = 問題

Page 20: Raspberry Pi as a Wireless Router

使用情境

http://www.techiestate.com/how-to-set-up-a-wireless-router/

1. 取得對外網路

2. 有線 / 無線介接

3. 發射無線訊號

5. 使用者介面

4. 網路的分配

Page 21: Raspberry Pi as a Wireless Router

使用情境

http://www.techiestate.com/how-to-set-up-a-wireless-router/

1. 取得對外網路

2. 有線 / 無線介接

3. 發射無線訊號

4. 網路的分配

5. 使用者介面

+ 很多未知的問題

Page 22: Raspberry Pi as a Wireless Router

一個我們熟悉的裝置

背後是許多知識 & 功能的集合

Page 23: Raspberry Pi as a Wireless Router

● 建構嵌入式系統開發環境● 設定無線 / 有線封包轉送

● 設定 DHCP/Wireless Access Point● 移植小型的網頁伺服器● 撰寫 CGI 程式

● 以 Python 控制硬體

學習目標

Page 24: Raspberry Pi as a Wireless Router

體驗 1 - 建構嵌入式系統開發環境

Page 25: Raspberry Pi as a Wireless Router

● x86, ARM, MIPS, PowerPC● 效能 ? 省電 ? 價格 ?

● 一個 ARM 架構

● 有 ARMv1, ARMv2..., ARMv8● ARM v6: Raspberry Pi Broadcom BCM2835

指令集架構

真八核 PS2 Power Macintoshes80386

Page 26: Raspberry Pi as a Wireless Router

● 同樣的程式碼在不同的平台 ( 架構 ) 上執行

● 問題?● addressing space● little endian/big endian● alignment● …

● 編譯器 ( 們 ) 會幫我們解決一切?

Porting( 移植 )

Page 27: Raspberry Pi as a Wireless Router

● 一堆工具 + library● Compiler● Linker● Assembler● Debugger● Libraries● ...

Toolchain

http://www.aboutdebian.com/compile.htm

Page 28: Raspberry Pi as a Wireless Router

● 在完善的開發平台下開發其他平台的軟體

● Ex: 在 x86 平台下開發 ARM 的軟體

● gcc foo.c -o foo● arm-linux-gnueabi-gcc foo.c -o foo

Cross Compile 交叉式編譯

Page 29: Raspberry Pi as a Wireless Router

● ./configure --build --host --target● --build: the architecture of the build machine● --host: the architecture that you want the file to run on● --target: the machine that GCC will produce code for

● 在 build 下編好 , 在 host 執行 target 版本

Ex: ./configure

--build=i686-pc-linux-gnu --host=arm-linux--target=mips-linux

利用 autotools

Page 30: Raspberry Pi as a Wireless Router

● 下載 Raspberry Pi 的 toolchain● https://github.com/raspberrypi/tools

● 三個選擇

● arm-bcm2708-linux-gnueabi● arm-bcm2708hardfp-linux-gnueabi ● gcc-linaro-arm-linux-gnueabihf-raspbian( 推薦 )

● 加到環境變數

● echo 'export PATH=\$PATH:/path/to/toolchain' >> ~/.bashrc

建構 cross-compiling 環境

Page 31: Raspberry Pi as a Wireless Router

體驗 2 - 開發嵌入式系統所需功能

Page 32: Raspberry Pi as a Wireless Router

● 假設● 有一個東西可以介接有線和無線

● 有一個東西可以發射無線網路的訊號

● 有一個東西可以分配網路

● 做一個控制的使用者介面

想像所需要的功能

Page 33: Raspberry Pi as a Wireless Router

● 假設● 有一個東西可以介接有線和無線

– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號

● 有一個東西可以分配網路

● 做一個控制的使用者介面

想像所需要的功能

Page 34: Raspberry Pi as a Wireless Router

iptables

Page 35: Raspberry Pi as a Wireless Router

● netfilter( 封包過濾機制 )● 根據封包的表頭採取動作

● iptables( 用戶空間程式 )● 封包過濾軟體

Linux 封包過濾概念

Page 36: Raspberry Pi as a Wireless Router

封包過濾規則動作與分析流程

http://linux.vbird.org/linux_server/0250simple_firewall.php

Page 37: Raspberry Pi as a Wireless Router

iptables

http://linux.vbird.org/linux_server/0250simple_firewall.php

Page 38: Raspberry Pi as a Wireless Router

● NAT = Network Address Translation● 來源與目的之間的 IP 和 port 轉換● POSTROUTING

– SNAT/MASQUERADE

NAT Table

Page 39: Raspberry Pi as a Wireless Router

● iptables -A INPUT -i wlan0 -j ACCEPT● iptables -t nat -A POSTROUTING -s

192.168.77.0/24 -o eth0 -j MASQUERADE● sysctl net.ipv4.ip_forward=1

封包轉送規則

Page 40: Raspberry Pi as a Wireless Router

● 假設● 有一個東西可以介接有線和無線

– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號

– AP mode● 有一個東西可以分配網路

● 做一個控制的使用者介面

想像所需要的功能

Page 41: Raspberry Pi as a Wireless Router

● Station (STA) infrastructure mode● AccessPoint (AP) infrastructure mode● Monitor (MON) mode● Ad-Hoc (IBSS) mode● Wireless Distribution System (WDS) mode● Mesh

無線網卡工作模式

http://wireless.kernel.org/en/users/Documentation/modes

Page 42: Raspberry Pi as a Wireless Router

● AP(Master) mode● Ralink rt2400 / rt2500● Broadcom 43xx ● Realtek RTL8180● Texas Instruments ACX100/ACX111● Intel PRO/Wireless (ipwXXXX) series● ZyDAS ZD1211● ...

選擇可以做 AP 的無線網卡

https://help.ubuntu.com/community/WifiDocs/MasterMode

Page 43: Raspberry Pi as a Wireless Router

● User space daemon for access point● Support IEEE 802.1X/WPA/WPA2/EAP

● 硬體的支援 + 軟體的實做

hostapd

http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html

Page 44: Raspberry Pi as a Wireless Router

● 下載 hostapd(RTL8188CUS)● http://bit.ly/t0HWH1

● 設定編譯參數 configure● CC=arm-linux-gnueabihf-gcc ./configure --host=arm-

linux --prefix=/path/to/install● 編譯

● make && make install

Cross Compile hostapd

Page 45: Raspberry Pi as a Wireless Router

● 假設● 有一個東西可以介接有線和無線

– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號

– AP mode● 有一個東西可以分配網路

– dhcp daemon● 做一個控制的使用者介面

想像所需要的功能

Page 46: Raspberry Pi as a Wireless Router

● 分派 IP, 給予相關資訊

● 管理租約

● isc-dhcp-server

DHCP Daemon

http://linux.vbird.org/linux_server/0340dhcp.php

Page 47: Raspberry Pi as a Wireless Router

● 下載 dhcpd● wget

ftp://ftp.isc.org/isc/dhcp/4.1-ESV-R8/dhcp-4.1-ESV-R8.tar.gz

● 設定編譯參數 configure● CC=arm-linux-gnueabihf-gcc ./configure --host=arm-linux

--prefix=/path/to/install

● 編譯

● make && make install

Cross Compile dhcpd

Page 48: Raspberry Pi as a Wireless Router

● 假設● 有一個東西可以介接有線和無線

– iptables & ip forwarding● 有一個東西可以發射無線網路的訊號

– AP mode● 有一個東西可以分配網路

– dhcp daemon● 做一個控制的使用者介面

想像所需要的功能

Page 49: Raspberry Pi as a Wireless Router

體驗 3 – 在適合的環境下開發 UI

Page 50: Raspberry Pi as a Wireless Router

App 在嵌入式系統是如何顯示的 ?

Framebuffer

Hardware

Application

GUI-ToolKit

Page 51: Raspberry Pi as a Wireless Router

Native App vs. Browser Based App

/dev/fb driver

SoC

Page 52: Raspberry Pi as a Wireless Router

Native App

/dev/fb driver

SoC

1. 根據 toolkit 寫 ap2. 執行速度快3. 可以和系統直接以 IPC溝通

Page 53: Raspberry Pi as a Wireless Router

Browser Based App

/dev/fb driver

SoC

1. 可支援 HTML5 和 CSS32. 開發速度快3. 需要以 plugin方式擴充

Page 54: Raspberry Pi as a Wireless Router

看看別人怎麼做

Page 55: Raspberry Pi as a Wireless Router

主要功能表

Page 56: Raspberry Pi as a Wireless Router

Browser based = web server + 程式

Page 57: Raspberry Pi as a Wireless Router

● 一個軟體● 回應從 80/8080 port進來的 HTTP 要求

● 可透過 CGI或module方式擴充

● 如 Apache, Nginx, Boa

網頁伺服器

http://www.resultantsys.com/index.php/general/what-is-a-web-application-server/

Page 58: Raspberry Pi as a Wireless Router

● 一個小型的網頁伺服器● 優點

● 小 , 25 支 C共 6899 行● 缺點

● 沒有 access control(authentication)● 無法 chroot● 不支援 SSL

● GPLv2 License

Boa Web Server (~2005)

Page 59: Raspberry Pi as a Wireless Router

● 下載 boa● wget http://www.boa.org/boa-0.94.13.tar.gz

● 設定編譯參數 configure● CC=arm-linux-gnueabihf-gcc ./configure --host=arm-

linux

● 編譯

● make && make install

Cross Compile Boa

Page 60: Raspberry Pi as a Wireless Router

有了 web server再找一個配合的動態語言

Page 61: Raspberry Pi as a Wireless Router
Page 62: Raspberry Pi as a Wireless Router

● Raspberry Pi內建

● Script語言● 強大的數學功能● 穩定的套件管理● 可結合多種web server

● 以 CGI方式執行 , 存取硬體資源

Python

Page 63: Raspberry Pi as a Wireless Router

● CGI = Common Gateway Interface● 一個讓程式可以在 web server被執行的標準

● C, Perl, Python, PHP...● 輸出 /輸入轉向

● Python CGI

CGI

Page 64: Raspberry Pi as a Wireless Router

體驗 4 - 存取硬體資源

Page 65: Raspberry Pi as a Wireless Router

● 控制硬體 (GPIO)● 讀寫 shell command● 用 AJAX 做畫面

以 Python 做控制

Page 66: Raspberry Pi as a Wireless Router

● A generic pin on an IC

General Purpose Input Output(GPIO)

http://raspberrypihobbyist.blogspot.tw/2012/09/so-many-inputs-so-few-gpio-pins.html

Page 67: Raspberry Pi as a Wireless Router

Raspberry Pi 的 GPIO

http://elinux.org/RPi_Low-level_peripherals

SPI / I2C / UART / PWM

Page 68: Raspberry Pi as a Wireless Router

線路圖

Page 69: Raspberry Pi as a Wireless Router

● Import module ● Define pin numbering● Setup up a channel● Input/Output● Cleanup

Python Code Flow

Page 70: Raspberry Pi as a Wireless Router

green = 26 , red = 24 # Define pin numbering

try:

import RPi.GPIO as GPIO # Import module

except RuntimeError:

print ("import RPi.GPIO error!!")

GPIO.setmode(GPIO.BOARD) # Setup up a channel

cmd = "sudo sysctl net.ipv4.ip_forward=1"

result = commands.getstatusoutput(cmd)

GPIO.output(green, GPIO.HIGH) # Input/Output

GPIO.output(red, GPIO.LOW) # # Input/Output

Python Code Example

Page 71: Raspberry Pi as a Wireless Router

DEMO

Page 72: Raspberry Pi as a Wireless Router

● 建構嵌入式系統開發環境● 開發嵌入式系統所需功能

● 設定 iptables 和封包轉送 ( 介接有線和無線 )● 交叉編譯 hostapd( 發射無線網路的訊號 )● 交叉編譯 isc-dhcp-server( 分配網路 )

● 在適合的環境下開發 UI● 交叉編譯 Boa( 使用者介面 )● 交叉編譯 Python( 動態語言 )

● 存取硬體資源

● 接線 , 用 Python存取硬體 (GPIO)

總結

Page 73: Raspberry Pi as a Wireless Router

Raspberry Pi Rocks the World

Thanks