顯示具有 Bluetooth 標籤的文章。 顯示所有文章
顯示具有 Bluetooth 標籤的文章。 顯示所有文章

2014年6月29日 星期日

[Linux] 如何使用BlueZ,取得ibeacon資訊

Bluetooth 基本架構,使用BlueZ介紹、ibeacon Packet Format 分析 和 , 簡單介紹Bluetooth的基本理論,以及ibeacon的Packet Format,接下來則介紹要如何使用BlueZ來取得ibeacon的Packet。若需要自行製作ibeacon,可參考Reference[1]。

1. 安裝BlueZ至Linux
  • Download Page 
  • Compiler and  Install
    • configure
    • ./configure --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var --enable-experimental --with-systemdsystemunitdir=/lib/systemd/system --with-systemduserunitdir=/usr/lib/systemd
    • compiler
    • make ; make install
2. 接上Bluetooth Device,確認Bluetooth Device是否已經Running
#hciconfig -a ; hciconfig hci0 up
//Note : 如果想知道HCI Command,則可以下
#hciconfig -a | hcidump
圖一: 尋找device
3. 都準備就緒了,就可以收詢ibeacon
#hcitool -i hci0 lescan --duplicates | hcidump -i hci0

圖二:  HCI Event
4. 分析封包
圖三藍色框框是HCI Event等相關訊息,紅色框框(PDU Payload)則是ibeacon device提供的重要資訊。(圖四提供Payload欄位定義)。

圖三: ibeacon raw data

圖四: iBeacon packet format


Reference :
[1]  How To Make a Raspberry Pi Turn on a Lamp with an iBeacon

[Bluetooth] HCI Packet Format 分析

HCI 共四種Packet Type(參考圖一),HCI Command和Event Packet只有單向功能,Command Packet只能從Host到Control,而Event只能從Control到Host,其餘兩型態軍是雙向。

圖一: HCI packet indicators

  •  HCI Command Packet 由一個OpCode、一個Parameter Total Length以及數個Parameter 所組成,最大封包是255 bytes。OpCode再分成OpCode Group Filed(OGF)和OpCode Command File(OCF)兩欄位,OGF是長度為6 bit,OCF則是剩餘的10bit的組成。其中,0x3F(00111111b)是OGF保留給廠商來執行Debug使用(vendor-specific debug commands),有些產商也會拿來當Firmware Upgrade使用。

圖二: HCI Command Packet
圖片來源 : Bluetooth 4.0 Spec


  • HCI Event Packet 由一個EventCode、一個Parameter Total Length以及數個Parameter 所組成,最大封包是255 bytes。 EventCode 範圍從0x00-0xFF,其中,0xFF(11111111b)是OGF保留給廠商來執行Debug使用(vendor-specific debug events)。

    圖三: HCI Event Packet
    圖片來源 : Bluetooth 4.0 Spec

Reference:
[1] bluetooth org
[2] BlueZ
[3] Bluetooth HCI Data format

[Bluetooth] Bluetooth 基本架構,使用BlueZ介紹

BlueZ是由官方Linux Bluetooth protocol stack,提供開發者可透過Command Line方式或使用blueZ library與Bluetooth device溝通。BlueZ與Linux網路程式類似,分成 :
  • Kernel Layer
    • Bluetooth kernel subsystem core
    • L2CAP and SCO audio kernel layers
    • RFCOMM, BNEP, CMTP and HIDP kernel implementations
    • HCI UART, USB, PCMCIA and virtual device drivers
  • User Space Layer
    • General Bluetooth and SDP libraries and daemons
    • Configuration and testing utilities
    • Protocol decoding and analysis tools
    若在執行Bluetooth Command Line發生問題時,那就先檢查Linux Kernel Option是否都已經正確開啟設定。

    Kernel Option
    -*- Networking support --->
        -*- Bluetooth Subsystem support --->
           (...將需求部分開啟....)


    圖一: Bluetooth Core System
    圖片來源: Bluetooth 4.0 Spec
    Bluetooth(BT) 是由一個 Host(BR/EDR、LE),加上一個Primary Controller(BR、EDR)及零個以上的Secondary Controllers(AMP)所組成。Host與Control之間的溝通,透過Host Control Interface來達成,對造網路OSI Model或DOD Model(如圖二),就是Transport Layer,類似網路程式設計裡的Socket Interface。

    圖二: Bluetooth Stack

    如上述,BlueZ實作 Linux Kernel Layer and User Layer,而他們之間對應關係如下 :
    • bluetooh.ko,為blueZ的核心,其餘的所有module,都需要透過這個Service運作。
    • hci_uart.ko, transporting HCI Packet over UART Packet
    • hci_usb.ko, transporting HCI Packet over USB Packet
    • l2cap.ko,L2CAP是負責多工處理的功能,將不同Application Service的Packet,有效率的傳送至下層處理,也可做QoS的功能。
    • rfcomm.ko,若想使用termianl傳遞Bluetooth Packet,則須先模擬一個Serial Port,這些由rfcomm來完成。
    • sco.ko,負責處理聲音傳遞。
    圖三: Figure 16.2 Bluetooth protocol layers mapped to BlueZ kernel modules
    圖片來源: Chapter 16. Linux Without Wires



    Reference :
    [1] bluetooth org
    [2] BlueZ
    [3] Chapter 16. Linux Without Wires
    [4] Study BT concept

    2014年6月26日 星期四

    [Bluetooth] ibeacon Packet Format 分析

    ibeacon是Apple於2013年底所發表的室內定位技術。Apple利用Bluetooth4.0 LE(2010 Release) advertising 特性,定期廣播訊ibeacon packet,使得Client端取得訊息強度之參數(RSSI,Received Signal Strength Indication),再透過訊號強度與距離換算函數計算出真實距離,最後搭配定位演算做到室內定位之功能。

    AIR INTERFACE PACKETS是一個只能運作在LE Device下的特殊封包,也是在Link layer中,唯一可以運作在advertising channel和data channel的Packet。Apple利用此特性,把ibeacon資訊夾帶在這個封包裡面。

    AIR INTERFACE PACKETS 定義如下:

    圖一 : AIR INTERFACE PACKET FORMAT
    圖片來源 : Bluetooth 4.0 Spec
    • Preamble
      • 所有的Link Layer pakcet都有這個Byte,可以被使用在執行
        • Frequency synchronization
        • symbol timing estimation
        • Automatic Gain Control (AGC) training
      • Advertising Channel Preamble => 10101010b
      • Data Channel Preamble,有兩種可能
        • LSB of Access Address is 1 => 01010101b
        • LSB of Access Address is 0 => 10101010b
    •  Access Address
      • All advertising channel packets => 10001110100010011011111011010110b  (0x8E89BED6)
      • Data channel packets
    • PDU ( Protocol Data Unit )
      • 儲存下一層Protocol的Data

    • CRC
      • 檢查碼

    iBeacon packet format定義如下:
    圖三 : iBeacon packet format

    以下 Raw Data是從網路下取得,藉此來解釋封包。必須要注意Bluetooth的傳遞為little endian,故要做轉換。

    Raw Data
    D6 BE 89 8E 40 24 65 FA D9 72 39 D0 02 01 06 1A
    FF 4C 00 02 15 74 27 8B DA B6 44 45 20 8F 0C 72
    0E AF 05 99 35 00 00 00 00 C5 04 21 B9
    • Access Addres(4bytes) = 0x8E89BED6 (Advertising Channel)
    • Advertising PDU Header (2 bytes) 
    圖四 : Advertising channel PDU Header
    圖片來源 : Bluetooth 4.0 Spec
    註: RFU = RESERVED FOR FUTURE USE
      •  PDU Type(4bits) = 0000b
      圖五 : Advertising channel PDU Header's PDU Type field enconding
      圖片來源 : Bluetooth 4.0 Spec
        • ADV_IND: connectable undirected advertising event
        • ADV_DIRECT_IND: connectable directed advertising event
        • ADV_NONCONN_IND: non-connectable undirected advertising event
        • ADV_SCAN_IND: scannable undirected advertising event
      • RFU (2bits)   = 00b
      • TxAdd(1bits) = 1b
      • RxAdd(1bits) = 0b
      • Length(6bits) =  0x24 = 36bytes
    •  MAC Address (6bytes) = D0:39:72:D9:FA:65
    •  Advertising Data
      • AD1 Length(1byte) = 0x02
      • AD1 Type(1byte)  = 0x01 
      • AD1 Flags(1byte) = 0x06
    圖六 : Flags
    圖片來源 : Bluetooth 4.0 Spec
      • AD2 Length(1byte) = 0x1A = 25 bytes
      • AD2 Type(1byte)  = 0xFF
    圖七 : Manufacturer Specific Data
    圖片來源 : Bluetooth 4.0 Spec
    •  Manufacturer Specific Data
      • Manufacturer type(2bytes) = 0x004C
    圖八 : Manufacturer Specific Data
    資料來源 :what-is-the-ibeacon-bluetooth-profile
      • ibeacon AD Indicator(2bytes) = 02
      • Data Length (1byte) = 0x15 = 21 bytes
      • UUID (16bytes) =74 27 8B DA B6 44 45 20 8F 0C 720E AF 05 99 35
      • Major Number (2bytes) = 00 00
      • Minor Number (2bytes) = 00 00
      • TX Power (1byte) = 0xC5 = -59db
      圖三 : TX Power Level
      圖片來源 : Bluetooth 4.0 Spec
    • CRC(3bytes) = 04 21 B9
      Reference :
      [1] bluetooth org
      [2] how-do-ibeacons-work
      [3] what-is-the-ibeacon-bluetooth-profile
      [4] Core Version 4.0 - Bluetooth

      2014年6月22日 星期日

      [Bluetooth] Bluetooth Link Layer

      1. Link Layer 像是網路七層裡的Data Link層,主要是負責接收Phyiscal Layer送上來了封包進行分析處理,在一需求回應至上層(如圖一)。
      2. 圖一: Bluetooth System Architecture
      3. Link Layer 的運作可以被表示成一個狀態機,並且在任何時間下,只允許運作於某一狀態。狀態共分成五種 (如圖二):
      4. 圖二 : State Diagram of the Link layer state machine
        圖片來源 : Bluetooth 4.0 Spec

        • Standby State
          • does not transmit or receive any packets
          • The Standby State can be entered from any other state.
        • Advertising State
          • the state will be transmitting advertising channel packets
          • possibly listening to and responding to responses triggered by these advertising channel packets.
          • the Advertising State can be entered from the Standby State
        • Scanning State
          • will be listening for advertising channel packets from devices that are advertising
          • The Scanning State can be entered from the Standby State.
        • Initiating State
          •  will be listening for advertising channel packets from a specific device(s) and responding to these packets to initiate a connection with another device.
          •  The Initiating State can be entered from the Standby State.
        • Connection State
          • can be entered either from the Initiating State or the Advertising State.
          • two roles are defined
            • Master Role
            • Slave Role
          • When entered from the Initiating State, the Connection State shall be in the
            Master Role. 
          • When entered from the Advertising State, the Connection State
            shall be in the Slave Role.
         
      5.  在Link layer只有兩種Packets
        • Advertising Packet
          • 共三組Advertising Channel(圖三)
        • Data Packet 
          • 共三十七組Data Channel(圖三)

        圖三 : Channel Index
        圖片來源 : Bluetooth 4.0 Spec
      Reference:
      [1]  Core Version 4.0 - Bluetooth
      [2] How Bluetooth LE works? - Link layer