Networking, USB, and ThreadsCSEE W4840Prof. Stephen A. EdwardsColumbia UniversityFall 2015
Ethernet (MAC) addresses48 bits ≈ 281 trillion (world population: 6.5 billion)Bits 48–24: Vendor codeBit 41: 0=ordinary, 1=group (broadcast) addressBi
An Ethernet Packet00d006269c00 Destination MAC address (router)00087423ccab Source MAC address (my desktop)0800 Type = IP packet45 IPv4, 5 word (20-by
IP Header31 28 27 24 23 16 15 13 12 0Version Words in Type of Service Total number of bytes= 4 Header (typically 0) in the IP packetIdentification Numb
IP Addresses32 bits ≈ 4 billion (world population: 6.5 billion)First n bits indicate network (n = 8, 16, 24)For example, columbia.eduowns 128.59.0.0 –
Sockets
Sockets// Create an Internet socket (SOCK_STREAM = TCP)int sockfd = socket(AF_INET, SOCK_STREAM, 0);// Connect to the server#define IPADDR(a,b,c,d) (h
USB: The Universal SerialBus
USB: Universal Serial Bus1.5 Mbps, 12 Mbps, 480 Mbps (USB 2.0), 5 Gbps (USB 3.0)Point-to-point, differential, twisted pair3–5m maximum cable length
USB Connectors
USB signalingNRZI: 0 = toggle, 1 = no changeBit stuffing: 0 automatically inserted after six consecutive 1sEach packet prefixed by a SYNC field: 3 0s fol
Ethernet and the InternetSocketsUSB: The Universal Serial Buslibusb 1.0POSIX Threads (pthreads)
USB PacketsAlways start with SYNCThen 4-bit type, 4-bit type complemented2 bits distinguish Token, Data, Handshake, and Special,other two bits select
USB Bus ProtocolPolled bus: host initiates all transfers.Most transactions involve three packets:I“Token” packet from host requesting dataIData packet
USB Data Flow TypesIControlFor configuration, etc.IBulk DataArbitrary data stream: burstyIInterrupt DataTimely, reliable delivery of data. Usually even
Layered Architecture
USB Physical Bus TopologySource: http://www.usblyzer.com/usb-topology.htm
lsusb outputFront: USB keyboardBack: IR receiverBack: Monitor w/ webcam, microphone (internal hub)Back: 7-port hub w/SD card readerBluetooth dongleSoC
lsusb -t outputFront: USB keyboardBack: IR receiverBack: Monitor w/ webcam, microphone (internal hub)Back: 7-port hub w/SD card readerBluetooth dongle
Devices, Configurations, Interfaces, and EndpointsDevices (Keyboards, Mice: physical object)Configurations (usually one)Interfaces (“logical device”: us
USB Addresses and EndpointsSource: http://www.beyondlogic.org/usbnutshell/usb3.shtml
USB Keyboard: lsusb (highlights)Bus 002 Device 007: ID 413c:2003 Dell Computer Corp. KeyboardDevice Descriptor:bDeviceClass 0 (Defined at Interface le
Ethernet and the Internet
USB SD Card Reader: lsusb (highlights)Bus 001 Device 006: ID 0bda:0119 Realtek Semiconductor Corp. Storage Device (SD card reader)Device Descriptor:bD
libusb 1.0
Libusb 1.0User-level C library for USB device access. lsusb built on it.www.libusb.org1.0 API supplants earlier libusb 0.1Nice tutorial: http://www.dr
Using libusb1. Initialize the library with libusb_init()2. Select your device from the list returned bylibusb_get_device_list(). Later, free the list
libusb: Finding a Keyboardlibusb_device**devs;struct libusb_device_descriptor desc;struct libusb_device_handle*keyboard = NULL;ssize_t num_devs, d; ui
libusb: Reading from a Keyboard#define USB_LCTRL (1 << 0)#define USB_LSHIFT (1 << 1)#define USB_LALT (1 << 2)#define USB_LGUI (1 <
USB HID Keyboard Protocol PacketPage 60 of http://www.usb.org/developers/devclass_docs/HID1_11.pdfByte Meaning0 Modifier keys1 Reserved2 Keycode 1...7
USB HID KeycodesPage 53: http://www.usb.org/developers/devclass_docs/Hut1_12v2.pdfCode Meaning0 No event...4 a or A5 b or B...29 z or Z30 1 or !...38
POSIX Threads (pthreads)
Creation and Termination#include <stdio.h>#include <pthread.h>void*mythread(void*ptr){printf("%s\n", (char*)ptr);return NULL;}in
EthernetStarted in about 1976 at Xerox PARCIEEE Standard 802.3Carrier-sense multiple access/carrier detect protocol:1. Listen to the cable2. If nobody
Mutexes#include <stdio.h>#include <pthread.h>pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;int counter = 0; /*Caution: shared variabl
Condition Variablespthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;int count; int valid = 0;void*wr
10Base-5 “Thicknet”Shared coax bus with “vampire tap” tranceiversYellow color suggested by the 802.3 standardFrom http://www.turkcenet.org/yerel_htm/1
10Base-2 “Thinnet”50-Ohm coax segments with BNC “T” connectorsCoax invariably blackFrom http://www.answers.com/topic/10base2
10Base-T and 100Base-TPut the shared medium in a hub: a star topology. Everybodyuses it now.Star topology Choice of colorsFrom http://www.asante.com/d
100Base-TX wiring (CAT 5)Pair of twisted pairs, one pair for each direction.Hub-to-computer cable is straight-through.Computer-to-computer cable is a
An Ethernet Frame7 bytes 1 6 6 2 46–1500 4Preamble SOF Dest. Src. Type Payload ChecksumSOF Start of FrameDest. Destination addressSrc. Source addressT
Comments to this Manuals