1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 //May want to refactor this in upcoming sprints. Don't want to expose to application layer that lower level stack is using CoAP.
37 #define OC_WELL_KNOWN_QUERY PCF("coap://224.0.1.187:5683/oc/core")
38 #define OC_EXPLICIT_DEVICE_DISCOVERY_URI PCF("coap://224.0.1.187:5683/oc/core?rt=core.led")
39 #define OC_MULTICAST_PREFIX PCF("coap://224.0.1.187:5683")
41 #define USE_RANDOM_PORT (0)
42 #define MAX_RESPONSE_LENGTH (256)
43 #define MAX_REQUEST_LENGTH (128)
44 #define MAX_URI_LENGTH (64)
45 #define MAX_QUERY_LENGTH (64)
46 #define MAX_CONTAINED_RESOURCES (5)
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
53 * OC Virtual resources supported by every OC device
56 OC_WELL_KNOWN_URI= 0, // "/oc/core"
57 OC_DEVICE_URI, // "/oc/core/d"
58 OC_RESOURCE_TYPES_URI, // "/oc/core/d/type"
59 OC_MAX_RESOURCES // Max items in the list
63 * Standard RESTful HTTP Methods
67 OC_REST_GET = (1 << 0), // Read
68 OC_REST_PUT = (1 << 1), // Write
69 OC_REST_POST = (1 << 2), // Update
70 OC_REST_DELETE = (1 << 3), // Delete
71 OC_REST_OBSERVE = (1 << 4), // Register observe request for most up date notifications ONLY.
72 OC_REST_OBSERVE_ALL = (1 << 5) // Register observe request for all notifications, including stale notifications.
76 * Host Mode of Operation
88 OC_NON_CONFIRMABLE = 0,
96 OC_ACTIVE = (1 << 0), // set == initialized; unset == deleted
97 OC_DISCOVERABLE = (1 << 1), // Discovery of this resource allowed
98 OC_OBSERVABLE = (1 << 2), // Observe allowed
99 OC_SLOW = (1 << 3) // Expect delay in processing requests. Send ACK to request and send response later
100 } OCResourceProperty;
103 * Declares Stack Results & Errors
107 OC_STACK_INVALID_URI,
108 OC_STACK_INVALID_QUERY,
110 OC_STACK_INVALID_PORT,
111 OC_STACK_INVALID_CALLBACK,
112 OC_STACK_INVALID_METHOD,
113 OC_STACK_INVALID_PARAM,
114 OC_STACK_INVALID_OBSERVE_PARAM,
118 OC_STACK_NO_RESOURCE, /* resource not found*/
119 OC_STACK_RESOURCE_ERROR, /*ex: not supported method or interface*/
120 OC_STACK_SLOW_RESOURCE,
121 OC_STACK_NO_OBSERVERS, /* resource has no registered observers */
126 * Handle to an @ref OCDoResource invocation.
128 typedef void * OCDoHandle;
131 * Handle to an OCResource object owned by the OCStack.
133 typedef void * OCResourceHandle;
136 * Incoming requests handled by the server. Requests are passed in as a parameter to the @ref OCEntityHandler callback API.
137 * @brief The @ref OCEntityHandler callback API must be implemented in the application in order to receive these requests.
140 // Associated resource
141 OCResourceHandle resource;
142 // resource query send by client
143 unsigned char * query;
144 // the REST method retrieved from received request PDU
146 // reqJSON is retrieved from the payload of the received request PDU
147 unsigned const char * reqJSONPayload;
148 // resJSON is allocated in the stack and will be used by entity handler to fill in its response
149 unsigned char * resJSONPayload;
150 // Length of maximum allowed response
151 uint16_t resJSONPayloadLen;
152 }OCEntityHandlerRequest;
155 * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API.
158 // the is the result of our stack, OCStackResult should contain coap/other error codes;
159 OCStackResult result;
160 // Address of remote server
162 // If associated with observe, this will represent the sequence of notifications from server.
163 uint32_t sequenceNumber;
164 // resJSONPayload is retrieved from the payload of the received request PDU
165 unsigned const char * resJSONPayload;
169 OC_INIT_FLAG = (1 << 0),
170 OC_REQUEST_FLAG = (1 << 1),
171 OC_OBSERVE_FLAG = (1 << 2)
172 } OCEntityHandlerFlag; //entity_handler_flag_t ;
174 // possible returned values from client application
176 OC_STACK_DELETE_TRANSACTION = 0,
177 OC_STACK_KEEP_TRANSACTION
178 } OCStackApplicationResult;
180 //-----------------------------------------------------------------------------
181 // Callback function definitions
182 //-----------------------------------------------------------------------------
185 * Client applications implement this callback to consume responses received from Servers.
187 typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle, OCClientResponse * clientResponse);
191 * This info is passed from application to OC Stack when initiating a request to Server
195 OCClientResponseHandler cb;
199 * Application server implementations must implement this callback to consume requests OTA.
200 * TODO: Need a clear explanation that the Entity handler callback needs to fill in the data inside the OCEntityHandlerRequest and then simply return from the callback.
202 typedef OCStackResult (*OCEntityHandler) (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest);
205 //-----------------------------------------------------------------------------
206 // Function prototypes
207 //-----------------------------------------------------------------------------
210 * Initialize the OC Stack. Must be called prior to starting the stack.
213 * IP Address of host device
215 * Port of host device
217 * Host device is client, server, or client-server
220 * OC_STACK_OK - no errors
221 * OC_STACK_ERROR - stack init error
223 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
226 * Stop the OC stack. Use for a controlled shutdown.
228 * OC_STACK_OK - no errors
229 * OC_STACK_ERROR - stack not initialized
231 OCStackResult OCStop();
234 * Called in main loop of OC client or server. Allows low-level processing of
238 * OC_STACK_OK - no errors
239 * OC_STACK_ERROR - stack process error
241 OCStackResult OCProcess();
244 * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
246 * @param handle - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
247 * @param method - @ref OCMethod to perform on the resource
248 * @param requiredUri - URI of the resource to interact with
249 * @param referenceUri - URI of the reference resource
250 * @param request - JSON encoded request
251 * @param qos - quality of service
252 * @param clientApplicationCB- asynchronous callback function that is invoked
253 * by the stack when discovery or resource interaction is complete
256 * OC_STACK_OK - no errors
257 * OC_STACK_INVALID_CALLBACK - invalid callback function pointer
258 * OC_STACK_INVALID_METHOD - invalid resource method
259 * OC_STACK_INVALID_URI - invalid required or reference URI
261 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri, const char *referenceUri,
262 const char *request, OCQualityOfService qos, OCCallbackData *cbData);
265 * Cancel a request associated with a specific @ref OCDoResource invocation.
267 * @param handle - Used to identify a specific OCDoResource invocation.
270 * OC_STACK_OK - No errors; Success
271 * OC_STACK_INVALID_PARAM - The handle provided is invalid.
273 OCStackResult OCCancel(OCDoHandle handle);
278 * @param handle - pointer to handle to newly created resource. Set by ocstack. Used to refer to resource
279 * @param resourceTypeName - name of resource type. Example: "core.led"
280 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
281 * @param uri - URI of the resource. Example: "/a/led"
282 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
283 * NULL for default entity handler
284 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
287 * OC_STACK_OK - no errors
288 * OC_STACK_ERROR - stack process error
290 OCStackResult OCCreateResource(OCResourceHandle *handle,
291 const char *resourceTypeName,
292 const char *resourceInterfaceName,
294 OCEntityHandler entityHandler,
295 uint8_t resourceProperties);
298 * Add a resource to a collection resource.
300 * @param collectionHandle - handle to the collection resource
301 * @param resourceHandle - handle to resource to be added to the collection resource
304 * OC_STACK_OK - no errors
305 * OC_STACK_ERROR - stack process error
306 * OC_STACK_INVALID_PARAM - invalid collectionhandle
308 OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
311 * Remove a resource from a collection resource.
313 * @param collectionHandle - handle to the collection resource
314 * @param resourceHandle - handle to resource to be removed from the collection resource
317 * OC_STACK_OK - no errors
318 * OC_STACK_ERROR - stack process error
319 * OC_STACK_INVALID_PARAM - invalid collectionhandle
321 OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
324 * Bind a resourcetype to a resource.
326 * @param handle - handle to the resource
327 * @param resourceTypeName - name of resource type. Example: "core.led"
330 * OC_STACK_OK - no errors
331 * OC_STACK_ERROR - stack process error
333 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
334 const char *resourceTypeName);
336 * Bind a resource interface to a resource.
338 * @param handle - handle to the resource
339 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
342 * OC_STACK_OK - no errors
343 * OC_STACK_ERROR - stack process error
345 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
346 const char *resourceInterfaceName);
349 * Bind an entity handler to the resource.
351 * @param handle - handle to the resource that the contained resource is to be bound
352 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
354 * OC_STACK_OK - no errors
355 * OC_STACK_ERROR - stack process error
357 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler);
360 * Get the number of resources that have been created in the stack.
362 * @param numResources - pointer to count variable
365 * OC_STACK_OK - no errors
366 * OC_STACK_ERROR - stack process error
369 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
372 * Get a resource handle by index.
374 * @param index - index of resource, 0 to Count - 1
377 * Resource handle - if found
378 * NULL - if not found
380 OCResourceHandle OCGetResourceHandle(uint8_t index);
383 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
386 * @param handle - handle of resource to be deleted
389 * OC_STACK_OK - no errors
390 * OC_STACK_ERROR - stack process error
392 OCStackResult OCDeleteResource(OCResourceHandle handle);
395 * Get the URI of the resource specified by handle.
397 * @param handle - handle of resource
399 * URI string - if resource found
400 * NULL - resource not found
402 const char *OCGetResourceUri(OCResourceHandle handle);
405 * Get the properties of the resource specified by handle.
406 * NOTE: that after a resource is created, the OC_ACTIVE property is set
407 * for the resource by the stack.
409 * @param handle - handle of resource
411 * property bitmap - if resource found
412 * NULL - resource not found
414 uint8_t OCGetResourceProperties(OCResourceHandle handle);
417 * Get the number of resource types of the resource.
419 * @param handle - handle of resource
420 * @param numResourceTypes - pointer to count variable
423 * OC_STACK_OK - no errors
424 * OC_STACK_ERROR - stack process error
426 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
429 * Get name of resource type of the resource.
431 * @param handle - handle of resource
432 * @param index - index of resource, 0 to Count - 1
435 * resource type name - if resource found
436 * NULL - resource not found
438 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
441 * Get the number of resource interfaces of the resource.
443 * @param handle - handle of resource
444 * @param numResources - pointer to count variable
447 * OC_STACK_OK - no errors
448 * OC_STACK_ERROR - stack process error
451 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle, uint8_t *numResourceInterfaces);
454 * Get name of resource interface of the resource.
456 * @param handle - handle of resource
457 * @param index - index of resource, 0 to Count - 1
460 * resource interface name - if resource found
461 * NULL - resource not found
463 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
466 * Get methods of resource interface of the resource.
468 * @param handle - handle of resource
469 * @param index - index of resource, 0 to Count - 1
472 * allowed methods - if resource found
473 * NULL - resource not found
475 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
478 * Get resource handle from the collection resource by index.
480 * @param collectionHandle - handle of collection resource
481 * @param index - index of contained resource, 0 to Count - 1
484 * handle to contained resource - if resource found
485 * NULL - resource not found
487 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle, uint8_t index);
490 * Get the entity handler for a resource.
492 * @param handle - handle of resource
495 * entity handler - if resource found
496 * NULL - resource not found
498 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
501 * Notify observers that an observed value has changed.
503 * **NOTE: This API has NOT been finalized!!!**
505 * @param handle - handle of resource
508 * OC_STACK_OK - no errors
509 * OC_STACK_ERROR - stack not initialized
511 OCStackResult OCNotifyObservers(OCResourceHandle handle);
515 #endif // __cplusplus
517 #endif /* OCSTACK_H_ */