application layer protocols for the iot - intranet...
TRANSCRIPT
Politecnico di MilanoAdvanced Network Technologies Laboratory
Application Layer Protocols for the IoT
1
Politecnico di MilanoAdvanced Network Technologies Laboratory
COnstrained ApplicationProtocol(COAP)
2
o GOAL:toenableweb-basedservicesinconstrainedwirelessnetworksn 8bitmicro-controllersn limitedmemoryn low-powernetworks
o Problem:WEBsolutionarehardlyapplicable
o Solution:re-designweb-basedservicesforconstrainednetworks->COAP
Background
3
o ResourcesintheWebare:n managedbyserversn identifiedbyURIsn accessedsynchronouslyby
clientsthroughrequest/responseparadigms
o Inaword,RepresentationalStateTransfer(REST)
HowDoestheWebWork?
4
o Embedded web transfer protocol (coap://)o Asynchronous transaction model
o UDP binding with reliability and multicast supporto GET, POST, PUT, DELETE methodso URI support
o 4 byte headero Subset of MIME types and HTTP response codeso Built-in discovery o Optional observation and block transfer
CoAP At a Glance
7
o Transport: n (mainly) UDP binding
o Message Exchange between Endpointsn Messages with 4 bytes header (shared by request and
responses) containing a message ID (16 bits)
n Reliable exchange through Confirmable Messages which must be acknowledged (through ACK or Reset Messages). Simple Stop-and-Wait retransmission with exponential back-off.
n Unreliable exchange through Non-Confirmable Messagen Duplicate detection for both confirmable and non-confirmable
messages (through message ID)
COAPMessagingBasics
8
o REST Request/Response piggybacked on CoAP Messages
o Method, Response Code and Options (URI, content-type etc.)
COAP Message Semantics
10
0 1 2 30 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|Ver| T | TKL | Code | Message ID |+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+| Token (if any, TKL bytes) ...+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+| Options (if any) ...+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|1 1 1 1 1 1 1 1| Payload (if any) ...+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Ver - Version (1)
T – Message Type (Confirmable, Non-Confirmable, Acknowledgement, Reset)
TKL- Token Length, if any, the number of Token bytes after this header
Code - Request Method (1-10) or Response Code (40-255)
Message ID – 16-bit identifier for matching responses
Token – Optional response matching token
Message Header (4 bytes)
14
0 1 2 3 4 5 6 7+---------------+---------------+| | || Option Delta | Option Length | 1 byte| | |+---------------+---------------+\ \/ Option Delta / 0-2 bytes\ (extended) \+-------------------------------+\ \/ Option Length / 0-2 bytes\ (extended) \+-------------------------------+\ \/ /\ \/ Option Value / 0 or more bytes\ \/ /\ \+-------------------------------+
Option Delta - Difference between this option type and the previousLength - Length of the option value
Value - The value of Length bytes immediately follows Length
Option Format
15
+-----+---+---+---+---+----------------+--------+--------+----------+| No. | C | U | N | R | Name | Format | Length | Default |+-----+---+---+---+---+----------------+--------+--------+----------+| 1 | x | | | x | If-Match | opaque | 0-8 | (none) || 3 | x | x | - | | Uri-Host | string | 1-255 | (see || | | | | | | | | below) || 4 | | | | x | ETag | opaque | 1-8 | (none) || 5 | x | | | | If-None-Match | empty | 0 | (none) || 7 | x | x | - | | Uri-Port | uint | 0-2 | (see || | | | | | | | | below) || 8 | | | | x | Location-Path | string | 0-255 | (none) || 11 | x | x | - | x | Uri-Path | string | 0-255 | (none) || 12 | | | | | Content-Format | uint | 0-2 | (none) || 14 | | x | - | | Max-Age | uint | 0-4 | 60 || 15 | x | x | - | x | Uri-Query | string | 0-255 | (none) || 16 | | | | | Accept | uint | 0-2 | (none) || 20 | | | | x | Location-Query | string | 0-255 | (none) || 35 | x | x | - | | Proxy-Uri | string | 1-1034 | (none) || 39 | x | x | - | | Proxy-Scheme | string | 1-255 | (none) |+-----+---+---+---+---+----------------+--------+--------+----------+
C=Critical, U=Unsafe, N=NoCacheKey, R=Repeatable
Base Specification Options
16
o StopandWaitapproacho Repeatarequestafteratime-outincase
ACK(orRST)isnotcomingback
Dealing with Packet Loss
17
o Initialtime-outsetto:n Rand[ACK_TIMEOUT,ACK_TIMEOUT*
ACK_RANDOM_FACTOR]([2s,3s])o Whentime-outexpiresandthetransmissioncounteris
lessthanMAX_RETRANSMIT(4)n retransmitn Increasetransmissioncountern doublethetime-outvalue
o Theprocedureisrepeateduntiln AACKisreceivedn ARSTmessageisreceivedn thetransmissioncounterexceedsMAX_RETRANSMITn thetotalattemptdurationexceedsMAX_TRANSMIT_WAIT
(93s)
Back-OffDetails
18
o PROBLEM:n RESTparadigmisoften“PULL”type,thatis,
dataisobtainedbyissuinganexplicitrequestn Information/datainWSNisoften
periodic/triggered(e.g.,getmeatemperaturesampleevery2secondsorgetmeawarningiftemperaturegoesbelow5°C)
o SOLUTION:useObservationonCOAPresources
COAPObservation
20
o PROBLEM:avoidsegmentationinthelowerlayers(IPv6)
o SOLUTION:COAPBlockTransferModen bringsupfragmentationattheapplication
layer
COAPBlockTransfer
22
o Block2 Optionaddedtomessagesn nr=incrementalblocknumberwithinoriginaldatan m=moreblocksflagn sz=blocksize
Block transfer
23
o Resource Discoveryn GOAL: Discovering the links hosted by
CoAP (or HTTP) serversGET /.well-known/core?optional_query_string
n Returns a link-header style formato URL, relation, type, interface, content-type etc.
Discovery & Semantics
24
</dev/bat>;obs;if="";rt="ipso:dev-bat";ct="0",</dev/mdl>;if="";rt="ipso:dev-mdl";ct="0",</dev/mfg>;if="";rt="ipso:dev-mfg";ct="0”,</pwr/0/rel>;obs;if="";rt="ipso:pwr-rel";ct="0",</pwr/0/w>;obs;if="";rt="ipso:pwr-w";ct="0",</sen/temp>;obs;if="";rt="ucum:Cel";ct="0"
CoRE Resource Discovery
25
o Open source implementations:n Java CoAP Library Californium n C CoAP Library Erbiumn libCoAP C Library n jCoAP Java Libraryn OpenCoAP C Libraryn TinyOS and Contiki include CoAP support
o Firefox has a CoAP plugin called Coppero Wireshark has CoAP plugin
Getting Started with CoAP
26
Politecnico di MilanoAdvanced Network Technologies Laboratory
TheMessageQueuingTelemetryTransport(MQTT)
Inventedin1999(IBMproprietarystandard)Releasedin2010OfficialOASISstandardsince2014CurrentversionMQTT3.1.1 http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
27
MQTTisaClientServerpublish/subscribemessaging transportprotocol.
o Morefeatures:n Simpletoimplement(especiallyatthesensor
side)n QoS Supportn Lightweightandbandwidthefficientn Dataagnosticn Sessionawareness
MQTTinshort
28
o Publish/Subscribeparadigmn Clientsdon’tknoweachothern One-to-Manyparadigmn Everyclientpublishes&subscribesn PUSHinformationparadigmcomparedtoPULL’s
oneinCOAP
CommunicationPattern
29
Publish: “30° C” Publish: “30° C”
Publish: “30° C”
Subscribe: “?°C”
Subscribe: “?°C”MQTT
Senosr nodeMobile device
Laptop
o Publish/Subscribeparadigmn Clientsdon’tknoweachothern One-to-Manyparadigmn Everyclientpublishes&subscribesn PUSHinformationparadigmcomparedtoPULL’s
oneinCOAP
MQTTComponents
30
Publish: “30° C” Publish: “30° C”
Publish: “30° C”
Subscribe: “?°C”
Subscribe: “?°C”
Senosr nodeMQTT Client Mobile device
MQTT Client
LaptopMQTT Client
MQTTBroker
deib/antlab/room5/temperature
o Wildcardsallowedonlywhensubscribing
deib/antlab/ + /temperature
Plussigncanbeusedinmultiplelevels
MQTTTopics
31
Topic level Topic separator
Single level Wildcard
o EachMQTTclientopensoneconnectiontotheMQTTBroker
o Pushcapabilitieso Workseventhrough
firewalls
MQTTConnections
33
MQTT
TCP
IP
o CONNECTmessagefields:n clientId “clientMatteo”n cleanSession truen username (opt) “matteo”n password (opt) “1234”n lastWillTopic (opt) “matteo/temp”n lastWillQoS (opt) 1n lastWillMessage (opt) “something wrong”n keepAlive 30
OpenConnections
34
MQTTBroker
MQTTClient
CONNECT
CONNACK
o CONNACKmessagefields:n sessionPresent truen returnCode 0-4
0: everything ok1: unacceptable version2: id rejected3: server unavailable4: bad username and pwd5: unauthorized
OpenConnections
35
MQTTBroker
MQTTClient
CONNECT
CONNACK
o PUBLISHmessagefieldsn packeId 2n topicName “matteo/temp”n QoS 1n retainFlag falsen Payload “temperature:30”n dupFlag false
Publishing
36
MQTTBroker
MQTTClient
PUBLISH
MQTTClient
MQTTClient
MQTTClient
PUBLISH
o Bestefforttransfer(samereliabilityprovidedbytheunderlyingtrasportprotocol)
QoS 0:“atmostonce”
37
MQTTBroker
MQTTClient
PUBLISH QoS=0
o TheMQTTclientstoresthemessageandkeepsretransmittingituntilitisacknowledgedbytheMQTTbroker(messagecanbereceivedmultipletimes)
o PUBACKmessagefields:n packetId 2
QoS 1:“atleastonce”
38
MQTTBroker
MQTTClient
PUBLISH QoS=1
PUBACK
o PUBREC,PUBREL,PUBCOMPmessagefields:n PacketId 2
o PUBLISHreception@MQTTbroker:processthepacketaccordingly, sendPUBRECmessageback,storelocallypacketId toavoidduplicateprocessing
o PUBRECreception@MQTTclient:discardtheinitialpacketandsendPUBRELo PUBRELreception@MQTTclient:clearanycurrent stateandsendPUBCOMP
QoS 2:“exactlyonce”
39
MQTTBroker
MQTTClient
PUBLISH QoS=1
PUBREC
PUBREL
PUBCOMP
o SUBSCRIBEmessagefields:n packetId 2n QoS1 0n Topic1 “matteo/temp/1”n QoS2 1n Topic2 “kitchen/temp/2”n …....... ........
Subscribing
40
MQTTBroker
MQTTClient
SUBSCRIBE
SUBACK
o SUBACKmessage fields:n packetId 2n returnCode 1 2n returnCode 2 0
one returnCodefor each topic inthe subscription
one QoS/topic couplefor each subscription
o UNSUBSCRIBEmessagefields:n packetId 2n Topic1 “matteo/temp/1”n Topic2 “kitchen/temp/2”n …....... ........
Unsubscribing
41
MQTTBroker
MQTTClient
UNSUBSCRIBE
UNSUBACK
o UNSUBACKmessage fields:n packetId 2
one returnCodefor each topic inthe subscription
o Indefaultoperationmodewhentheclientdisconnects,alltheclient-relatedstatusatthebrokerisflushed(listofsubscription,QoS pendingmessages,etc.)
o Inpersistentsessionsbothclientandbrokermaintainasession:n Broker:
o Existenceofasession,eveniftherearenosubscriptionso Allsubscriptionso AllmessagesinQoS 1or2flow,whicharenotconfirmedbythecliento AllnewQoS 1or2messages,whichtheclientmissedwhileitwasofflneo AllreceivedQoS 2messages,whicharenotyetconfirmedtotheclient
n Cliento AllmessagesinaQoS 1or2flow,whicharenotconfirmedbythebrokero AllreceivedQoS 2messages,whicharenotyetconfirmedtothebroker
o Thatmeanseveniftheclientisofflinealltheabovewillbestoredbythebrokerandareavailablerightaftertheclientreconnects.
PersistentSessions
42
o Problem:publishingandsubscribingareasynchronousprocesses
o Aclientsubscribingtoatopicpatternmaynotgetanymessageonthattopicuntilsomeotherclientpublishesonit
o Retaind messagesarePUBLISHmessageswiththeretainedFlag settoone
o Thebrokerstoreslocallytheretainedmessageandsendittoanyotherclientwhichsubscribestoatopicpatternmatchingthatoftheretainedmessage
RetainedMessges
43
o TheLastWillandTestament(LWT)notifiesotherclientsaboutanharddisconnec byaspecificclient
o Eachclientcanspecifyitslastwillmessagewhenconnectingtoabroker
o Thebrokerwillstorethemessageuntilitdetectsclientharddisconnection
o Thebrokersendsthemessagetoallsubscribedclientsonthespecifictopic
o ThestoredLWTmessagewillbediscardedifaclientdisconnectsgracefullybysendingaDISCONNECTmessage.
LastWillMessage
44
o CONNECTmessagefields:n clientId “clientMatteo”n cleanSession truen username (opt) “matteo”n password (opt) “1234”n lastWillTopic (opt) “matteo/temp”n lastWillQoS (opt) 1n lastWillMessage (opt) “something wrong”n keepAlive 30
LWTSetUp
45
MQTTBroker
MQTTClient
CONNECT
CONNACK
o AnI/Oerrorornetworkfailureisdetectedbytheserver.
o TheclientfailstocommunicatewithintheKeepAlivetime.
o TheclientclosesthenetworkconnectionwithoutsendingaDISCONNECTpacketfirst.
o Theserverclosesthenetworkconnectionbecauseofaprotocolerror.
LWTMessageissentwhen..
46
o Itisresponsability oftheclienttokeeptheMQTTconnectionactive
o Uponexpirationofthekeepalive,ifnootherinteractionhashappenedwithbroker,theclient”pings”thebrokerwhich“pingsitback”
o PINGREQandPINGRESPmessageshavenullpayload
Keepalive
47
MQTTBroker
MQTTClient
PINGREQ
PINGRESP
o Maindifferencesn Extendedarchitecture
withGatewaysandForwarders
n Newgatewaydiscoveryprocedures(andmessages)
n Somemessagesaremore“compressed”
n Extendedkeepaliveprocedurestosupportsleepingclients
MQTT-SN
48