iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / docs / guides / ProgrammersGuide.txt
1 /*!\r
2 \r
3 \r
4 @page OCGuides Programmer's Guide\r
5 \r
6 \r
7 This document covers the architecture and basic operations of the Iotivity Resource API stack, including sample coverage of protocol, flows, APIs and some use cases. It is intended to provide context for the developers using IoTivity APIs and provide a high level architectural overview.\r
8 \r
9 @section Stack_Blocks Stack Blocks\r
10 \r
11 The Resource API stack consists of several thin layers of software. In unconstrained environments such as Android*, iOS*, or Microsoft* Windows*, the stack provides APIs in C and C++ which allow developers to talk to both constrained and unconstrained devices via IP networks, with potential support for additional network protocols and wireless technologies. In the first release, the key technologies for connectivity include UDP/IP and the Constrained Application Protocol (CoAP).
12 \r
13 @image html stack_diagram.png\r
14 \r
15 @section Terminology Terminology\r
16 \r
17 <b>Device</b>\r
18 A constrained device that has the Thin Block stack installed which enabled one or more services for other Thin Block or Unified Block devices to consume.\r
19 \r
20 <b>Resource</b>\r
21 A resource is a component in a server that can be viewed and controlled by another Thin Block or Unified Block device. There are different resource types, for example a temperature sensor, a light controller etc.\r
22 \r
23 Resources can be arranged in a hierarchal manner to form a tree of resources. This generic method of structure enables one to model many different topologies of resources.\r
24 \r
25 @li Example: A light controller could be a resource.\r
26 @li Example: A light array could be a set of resources organized in a flat (non-hierarchical) manner.\r
27 @li Example: A garage door opener could be a resource; it could host two resources - light and lock.\r
28 \r
29 A more detailed description of resources and management of resources along with code snippets is provided later in this document.\r
30 \r
31 <b>Operations</b>\r
32 Operations are actions that a Thin Block or Unified Block can perform on attributes associated with a particular resource. Resource attributes can have different operations on it based on the nature of the resource type. Fundamentally, these are GET and PUT operations. Additionally, attributes can also be declared to be observable to enable remote devices to subscribe to changes.\r
33 \r
34 @li Example: One of the child resources on the garage door opener is the light control; it has a GET operation that allows a device to get the current light state (on / off).\r
35 \r
36 @section Functionally Functionally\r
37 \r
38 The initial release of IoTivity includes functionally for:\r
39  @li @ref Guide_Register_Resource "Resource registration"\r
40  @li @ref Guide_Find_Resource "Resource discovery"\r
41  @li Device discovery with filtering\r
42  @li Property attributes (@ref Guide_GET "get"/ @ref Guide_PUT "set"/ @ref Guide_Observe "observe")\r
43  @li Resource tree (resources having sub-resources)\r
44  @li Presence notification service defined as a virtual resource (not detailed in this document)\r
45 \r
46 @section External_References External References \r
47 \r
48 The following references may provide guidance to this document.\r
49  @note In some places, the IoTivity design may differ from the CoRE specifications. In these cases, please consider the CoRE specifications as informative but not definitive on the Iotivity design and architecture.\r
50 \r
51  @li The Constrained Application Protocol (CoAP) - https://datatracker.ietf.org/doc/rfc7252\r
52  @li Constrained RESTful Environments (CoRE) Link Format - https://datatracker.ietf.org/doc/rfc6690\r
53  @li Observing Resources in CoAP - https://datatracker.ietf.org/doc/draft-ietf-core-observe\r
54  @li CoRE Interfaces (expired draft) - https://datatracker.ietf.org/doc/draft-ietf-core-interfaces\r
55 \r
56 @section Protocol Protocol Message Format(s)\r
57 \r
58 The OIC protocol (abbreviated to OC in code) is a REST-like interface similar to HTTP and CoAP. However, it is   a one level up abstraction of the those protocols to allow the addition of additional transports including Bluetooth Classic, Bluetooth Smart (BLE), Zigbee or others in the future. To that end, every attempt has been made to keep CoAP and HTTP specific aspects from being expressed directly in the OIC protocol. The following sections  describe how specific transports are used to support the OIC protocol and abstractions.\r
59 \r
60 @subsection Protocol_CoAP Constrained Application Protocol (CoAP)\r
61 \r
62 Constrained Application Protocol is one of the IoTivity supported transports. It is designed to be used in very simple devices and is particularly targeted for small, low power devices like sensors, switches, etc. The protocol is modeled after HTTP and provides easy translation between HTTP and CoAP. It is UDP-based (instead of TCP), providing support for multicast.\r
63 \r
64 CoAP is now a standard (RFC7252) as defined by the Internet Engineering Task Force (IETF) Constrained RESTful environments (CoRE) Working Group. Additional RFCs and drafts cover higher order behaviors.\r
65 \r
66 <b>Message format</b>\r
67 The following table contains a brief overview of the contents of a CoAP packet. Use it as a cheat sheet for the following discussion. For details on the Constrain Resource Protocol, see http://datatracker.ietf.org/doc/rfc7252/?include_text=1.\r
68 \r
69 <table cellspacing="0" cellpadding="0" border=1>\r
70 \r
71 <tr>\r
72 <th valign="top" ><p>Field</p></th>\r
73 <th valign="top" ><p>Value</p></th>\r
74 <th valign="top" ><p>Short</p>\r
75 \r
76 <p>Hand</p></th>\r
77 <th valign="top" ><p>Notes</p></th>\r
78 </tr>\r
79 \r
80 <tr>\r
81 <th valign="top" ><p>Address</p></th>\r
82 <th valign="top" ><p>&lt;Device IPD&gt;:&lt;port&gt;</p>\r
83 \r
84 <p>224.0.1.187:5683</p></th>\r
85 <th valign="top" ><p><br />\r
86 </p></th>\r
87 <th valign="top" ><p>Device IP address and port multicast group IP address and port</p></th>\r
88 </tr>\r
89 \r
90 <tr>\r
91 <th valign="top" ><p>Version</p></th>\r
92 <th valign="top" ><p>Version 1 (01b)</p></th>\r
93 <th valign="top" ><p><br />\r
94 </p></th>\r
95 <th valign="top" ><p>Constant</p></th>\r
96 </tr>\r
97 \r
98 <tr>\r
99 <th valign="top" ><p>Type</p></th>\r
100 <th valign="top" ><p>Confirmable (00b)</p>\r
101 \r
102 <p>Non-confirmable (01b)</p>\r
103 \r
104 <p>Acknowledgement (10b)</p>\r
105 \r
106 <p>Reset (11b)</p></th>\r
107 <th valign="top" ><p>CON</p>\r
108 \r
109 <p>NON</p>\r
110 \r
111 <p>ACK</p>\r
112 \r
113 <p>RST</p></th>\r
114 <th valign="top" ><p><br />\r
115 </p></th>\r
116 </tr>\r
117 \r
118 <tr>\r
119 <th valign="top" ><p>Token</p>\r
120 \r
121 <p>Length</p></th>\r
122 <th valign="top" ><p>Xxxxb</p></th>\r
123 <th valign="top" ><p><br />\r
124 </p></th>\r
125 <th valign="top" ><p>Length of the token. Valid values are between 0 and 8.</p></th>\r
126 </tr>\r
127 \r
128 <tr>\r
129 <th valign="top" ><p>Code</p></th>\r
130 <th valign="top" ><p>Request (0.xx)</p>\r
131 \r
132 <p>Success (2.xx)</p>\r
133 \r
134 <p>Client error (4.xx)</p>\r
135 \r
136 <p>Server error (5.xx)</p></th>\r
137 <th valign="top" ><p><br />\r
138 </p></th>\r
139 <th valign="top" ><p>Common requests and responses:</p>\r
140 \r
141 <p>GET (0.01)</p>\r
142 \r
143 <p>CREATED (2.01)</p>\r
144 \r
145 <p>CHANGED (2.04)</p>\r
146 \r
147 <p>CONTENT (2.05)</p></th>\r
148 </tr>\r
149 \r
150 <tr>\r
151 <th valign="top" ><p>Message</p>\r
152 \r
153 <p>ID</p></th>\r
154 <th valign="top" ><p>0xXXXX</p></th>\r
155 <th valign="top" ><p>MID</p></th>\r
156 <th valign="top" ><p>Generated by sender</p>\r
157 \r
158 <p>Allows receiver to de-duplicate requests</p></th>\r
159 </tr>\r
160 \r
161 <tr>\r
162 <th valign="top" ><p>Token</p></th>\r
163 <th valign="top" ><p><br />\r
164 </p></th>\r
165 <th valign="top" ><p>TOK</p></th>\r
166 <th valign="top" ><p>Generated by client to match REQ to RESP</p></th>\r
167 </tr>\r
168 \r
169 <tr>\r
170 <th valign="top" ><p>Options</p></th>\r
171 <th valign="top" ><p><br />\r
172 </p></th>\r
173 <th valign="top" ><p>*</p></th>\r
174 <th valign="top" ><p>Contains the URI path and query, request and response headers</p></th>\r
175 </tr>\r
176 \r
177 <tr>\r
178 <th valign="top" ><p>Payload</p></th>\r
179 <th valign="top" ><p><br />\r
180 </p></th>\r
181 <th valign="top" ><p><br />\r
182 </p></th>\r
183 <th valign="top" ><p><br />\r
184 </p></th>\r
185 </tr>\r
186 \r
187 </table>\r
188 \r
189 \r
190 <b>Short-hand notation</b>\r
191 \r
192 The following two tables provide examples of request and response packets with explanations on the meaning of the short-hand notation used through the description of the queries and replies.\r
193 \r
194 @note Acknowledgements can come back separate from content. For the purposes of understanding the semantics of the query and responses, we will assume that all responses come back immediately. In production, requests can be acknowledged and the contents sent back at a later time. In addition, retry logic, de-duplication, congestion control and other features of the CoAP protocol libraries are neglected here.\r
195 \r
196 <b>Request example</b>\r
197 \r
198 In this example, the request is to the CoRE "core" resource in the well-known namespace. It provides a simple example of a multicast request to a compound URI with a query section.\r
199 \r
200 \r
201 <table cellspacing="0" cellpadding="0" border=1>\r
202 \r
203 <tr>\r
204 <th valign="top" ><p>Fields</p></th>\r
205 <th valign="top" ><p>Sample Values</p></th>\r
206 <th valign="top" ><p>Explanation</p></th>\r
207 </tr>\r
208 \r
209 <tr>\r
210 <th valign="top" ><p>Address</p></th>\r
211 <th valign="top" ><p>224.0.1.187:5683</p></th>\r
212 <th valign="top" ><p>Multicast packet address</p></th>\r
213 </tr>\r
214 \r
215 <tr>\r
216 <th valign="top" ><p>Header</p></th>\r
217 <th valign="top" ><p>NON, GET, MID=0x7D40</p></th>\r
218 <th valign="top" ><p>Non-confirmable</p>\r
219 \r
220 <p>GET (code=0.01)</p>\r
221 \r
222 <p>Message ID = 0x7D40</p></th>\r
223 </tr>\r
224 \r
225 <tr>\r
226 <th valign="top" ><p>Token</p></th>\r
227 <th valign="top" ><p>0x75, 0x55</p></th>\r
228 <th valign="top" ><p>Token Length = s</p>\r
229 \r
230 <p>Token = 0x75, 0x55</p></th>\r
231 </tr>\r
232 \r
233 <tr>\r
234 <th valign="top" ><p>URI-Path</p></th>\r
235 <th valign="top" ><p>oc</p></th>\r
236 <th rowspan="4" valign="top" ><p>"/oc/core?rt=sensor&if=core.ll"</p></th>\r
237 </tr>\r
238 \r
239 <tr>\r
240 <th valign="top" ><p>URI-Path</p></th>\r
241 <th valign="top" ><p>core</p></th>\r
242 </tr>\r
243 \r
244 <tr>\r
245 <th valign="top" ><p>URI-Query</p></th>\r
246 <th valign="top" ><p>rt=sensor</p></th>\r
247 <th valign="middle" ><p><br />\r
248 </p></th>\r
249 </tr>\r
250 \r
251 <tr>\r
252 <th valign="top" ><p>URI-Query</p></th>\r
253 <th valign="top" ><p>if=core.ll</p></th>\r
254 <th valign="middle" ><p><br />\r
255 </p></th>\r
256 </tr>\r
257 \r
258 </table>\r
259 \r
260 <b>Acknowledged response example</b>\r
261 \r
262 In this example, the response is returned.\r
263 @note The payload in this example is for demonstration of the packet format and not a valid discovery response.\r
264 \r
265 <table cellspacing="0" cellpadding="0" border=1>\r
266 \r
267 <tr>\r
268 <th valign="top" ><p>Fields</p></th>\r
269 <th valign="top" ><p>Sample Values</p></th>\r
270 <th valign="top" ><p>Explanation</p></th>\r
271 </tr>\r
272 \r
273 <tr>\r
274 <th valign="top" ><p>Address</p></th>\r
275 <th valign="top" ><p>192.168.0.0:5683</p></th>\r
276 <th valign="top" ><p>Unicast packet</p></th>\r
277 </tr>\r
278 \r
279 <tr>\r
280 <th valign="top" ><p>Header</p></th>\r
281 <th valign="top" ><p>ACK, CONTENT, MID=0x7D40</p></th>\r
282 <th valign="top" ><p>Non-confirmable</p>\r
283 \r
284 <p>Content (code=2.05)</p>\r
285 \r
286 <p>Message ID = 0x7D40</p></th>\r
287 </tr>\r
288 \r
289 <tr>\r
290 <th valign="top" ><p>Token</p></th>\r
291 <th valign="top" ><p>0x75, 0x55</p></th>\r
292 <th valign="top" ><p>Token Length = 2</p>\r
293 \r
294 <p>Token = 0x75, 0x55</p></th>\r
295 </tr>\r
296 \r
297 <tr>\r
298 <th valign="top" ><p>Payload</p></th>\r
299 <th valign="top" ><p>"Sample Payload"</p></th>\r
300 <th valign="top" ><p>Raw content</p></th>\r
301 </tr>\r
302 \r
303 </table>\r
304 \r
305 \r
306 \r
307 \r
308 */\r