Transcript socket

Message Control Multicast
Translator
葉俊克
系統架構
Translator
ZigBee
Node
Communication
Interface(RS232)
PC
Server-PC
ZigBee
ZigBee
Node
RF
Mapping
address
Ethernet
系統模組架構
MCMT
Server
PC
ZigBee
Node
RS232
UART
Address Mapping
Table
UART
ZigBee
ZigBee
Node
socket
UDP
Protocol Translator
IPv6 <-> ZigBee
IPv6
IEEE 802.3
UDP
IPv6
raw socket
libpcap
IEEE 802.3
3
Terminal
• Open terminal
– int open(const char *path, int oflag, option )
• Read/Write data from terminal RX/TX
– ssize_t read(int fildes, void *buf, size_t nbyte)
– ssize_t write(int fildes, const void *buf,
size_t nbyte)
• flush non-transmitted output data, non-read
input data, or both
– int tcflush(int fildes, int queue_selector)
Socket Setting
• Open socket (raw模式、raw協定)
– int socket(int domain, int type, int protocol)
– socket(PF_INET6, SOCK_RAW,IPPROTO_RAW )
• Set socket(不自動建立ip header)
– int setsockopt(int socket, int level,
int option_name, const void *option_value,
socklen_t option_len)
– setsockopt(sockfd, IPPROTO_RAW, IP_HDRINCL,
&optval, sizeof(int))
Socket
• Send data with a dst addr
– ssize_t sendto(int socket, const void *message,
size_t length, int flags, const struct sockaddr
*dest_addr, socklen_t dest_len)
• Receive data
– ssize_t recvfrom(int socket, void *restrict buffer,
size_t length, int flags, struct sockaddr
*restrict address, socklen_t *restrict address_len
• Udp check sum
– unsigned int Checksum(unsigned int cksum, void
*pBuffer, unsigned int size)
Pcap
• Find all NIC
– int pcap_findalldevs(pcap_if_t **alldevsp, char
*ebuf)
• Open a NIC to capture packet
– int pcap_open_live(char *dev, int snaplen, int
promisc, int ms, char *ebuf)
• Check the NIC can be used
– int pcap_datalink(pcap_t *p)
Pcap
• Compile the Filter Expressions
– int pcap_compile(pcap_t *p, struct
bpf_program *fp, char *str, int optimize,
bpf_u_int32 netmask)
• Associate a Filter to a Capture
– int pcap_setfilter(pcap_t *p, struct
bpf_program *fp)
• Capture Packets
– int pcap_loop(pcap_t *p, int cnt, pcap_handler
callback, u_char *user)
thread
• int pthread_create(pthread_t *pthread,
pthread_attr_t *attr, void *(*start_routine)
(void *), void *asg);
– 簡單說明
Tech. Doc.
• Socket
– Sock raw
– Udp checksum
• Pcap
– Winpcap
• UNIX® Network Programming Volume 1, Third Edition:
The Sockets Networking API
• Linux程式設計教學手冊(Beginning Linux Programming)
• 線上linux api查詢工具
• http://www.digipedia.pl/man/
• http://swoolley.org/man.cgi
• Libpcap
Install and Compile
– Install Libpcap LibPCAP-Devel
• yum install libpcap libpcap-devel
– Compile
• gcc expcap.c –l pcap
• Pthread
– Compile
• gcc exthread.c –lpthread
• Gcc –D_REENTRANT –I/usr/include/nptl ex.c –
L/usr/lin/nptl –lpthread (舊版方式,2003年以前。在
/usr/inlcude/phtrad.h檔可看日期)
API-ZigBee-Setting
• Set channel and PANID at tool/8wConfig.cfg
– -DDEFAULT_CHANLIST=0x00001000 // 12 - 0x0C
– -DZDAPP_CONFIG_PAN_ID=0xABCD
• Set Multicast/Groupcast at nwk_globals.c
– _NIB.nwkUseMultiCast = TRUE // Multicast
– _NIB.nwkUseMultiCast = FALSE // Groupcast
API-ZigBee-UART
• Set UART parameter at hal_uart.c
– uint8 HalUARTOpen(uint8 port, halUARTCfg_t
*config)
• UART read/write
– uint16 HalUARTRead(uint8 port, uint8 *buf, uint16
len)
– uint16 HalUARTWrite(uint8 port, uint8 *buf,
uint16 len)
API-ZigBee-Group
• Add group for an endpoint at aps_group.h
– ZStatus_t aps_AddGroup( uint8 endpoint,
aps_Group_t *group )
• Find a group with endpoint and groupID
– aps_Group_t *aps_FindGroup( uint8 endpoint,
uint16 groupID )
• Remove a group with endpoint and groupID
– uint8 aps_RemoveGroup( uint8 endpoint, uint16
groupID );
API-ZigBee
• Send RF Data at AF.h
– AF_DataRequest( afAddrType_t *dstAddr,
endPointDesc_t *srcEP, uint16 cID, uint16 len,
uint8 *buf, uint8 *transID, uint8 options, uint8
radius )
• Data message processor callback
– void
SampleApp_MessageMSGCB( afIncomingMSGPac
ket_t *pkt )
ZigBee Tech. doc.
• In Z-Stack Documents
– Z-Stack User's Guide - CC2530DB(read it first)
– Z-Stack Developer's Guide (read it before
programming)
– Z-Stack Sample Applications (description of
sample)
– Z-Stack API
– HAL Driver API (UART,LCD,LED,Switch)
ZigBee Tools
• Z-Stack - ZigBee Protocol Stack
– last: ZStack-CC2530-2.4.0-1.4.0(swrc126d.zip)
• SmartRF Protocol Packet Sniffer
– last: Packet Sniffer 2.15.1 (Rev. S)
• SmartRF Flash Programmer
– last: Flash Programmer 1.11.1 (Rev. M)
• IAR Embedded Workbench® for 8051
– Version: 7.60
– Last: 8.10
IAR Open Workspace
• “File”->”Open”->”Workspace”
• C:\Texas Instruments\ZStack-CC2530-2.4.01.4.0\Projects\zstack\Samples\SampleApp\CC2530DB
IAR Build Setting
• For generating a .hex file
IAR Build Setting
• “Project”->”Options…”
IAR Build Setting
Generating a .hex File
• Make: link to a .hex file
• Rebuild All: build a new .hex file
Download a File to Device
•
C:\Texas Instruments\ZStack-CC2530-2.4.0-1.4.0\Projects\zstack\
Samples\SampleApp\CC2530DB\CoordinatorEB\Exe\CoordinatorEB.hex
Demo
• Translators-PC:
2001:0e10:6840:21:21e:90ff:fe18:4cc // 10.21.10.103
• Translators-RF:
MAC: 00124B00010A2A83
• Node A:
MAC: 00124B00010A2A75
• Node B:
MAC: 00124B00010A2A5C
• Server:
0x00a1
2001:e10:6840:21:4687:fcff:fe41:6c0b // 10.10.21.59
• Prefix: 2001:e10:6840:409
• 設定路由表
– route –A inet6 add ZigBee_Prefix gw Translator_IP [dev
ethx]
• 增加IP
– ip -6 addr add ip dev ethx
• 啟動MCMT
– ./MCMT
• 啟動server
– ./Recv
• 啟動ZigBee Device
• 按五向鍵的”下”
– 發送加入maping table的封包
– 00 saddr MAC
Join Translator
Node A
Send data: 00(MT) + aa aa (short address) + 00 12 4b 00 01 0a 2a 75(MAC)
Node B
Send data: 00(MT) + bb bb (short address) + 00 12 4b 00 01 0a 2a 5c(MAC)
Translator
Communication
Interface(RS232)
PC
ZigBee
Address Mapping
Table
Src + Dst + Data
aa aa + 00 00 + 00(MT) + aa aa + 00 12 4b 00 01 0a 2a 5c
bb bb + 00 00 + 00(MT) + bb bb + 00 12 4b 00 01 0a 2a 5c
00 00(因為不需轉發,所以此欄在此功能不重要,但為了一致性仍保留)
Send data to server
Translator
Node A
Node B
Communication
Interface(RS232)
ZigBee
PC
Server-PC
Q&A
• 目前無法將End device的rxonwhenidle==ture
啟用。