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 (128)
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 uint8_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, 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 resourceAttributeRepresentation - attribute representation. list of attributes:type, with each pair
281 * separated by semicolons. Example: "state:oc.bt.b;power:oc.bt.i"
282 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
283 * @param allowedMethods - methods permitted on interface. Example: OC_REST_GET|OC_REST_PUT
284 * @param uri - URI of the resource. Example: "/a/led"
285 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
286 * @param resourceProperties - properties supported by resource. Example: OC_DISCOVERABLE|OC_OBSERVABLE
289 * OC_STACK_OK - no errors
290 * OC_STACK_ERROR - stack process error
292 OCStackResult OCCreateResource(OCResourceHandle *handle,
293 const char *resourceTypeName,
294 const char *resourceAttributeRepresentation,
295 const char *resourceInterfaceName,
296 uint8_t allowedMethods,
298 OCEntityHandler entityHandler,
299 uint8_t resourceProperties);
302 * Add a resource to a container resource.
304 * @param containerHandle - handle to the container resource
305 * @param addedResourceHandle - handle to resource to be added to the container resource
308 * OC_STACK_OK - no errors
309 * OC_STACK_ERROR - stack process error
311 OCStackResult OCBindContainedResourceToResource(OCResourceHandle containerHandle, OCResourceHandle addedResourceHandle);
314 * Bind a resourcetype to a resource.
316 * @param handle - handle to the container resource
317 * @param resourceTypeName - name of resource type. Example: "core.led"
318 * @param resourceAttributeRepresentation - attribute representation. list of attributes:type, with each pair
319 * separated by semicolons. Example: "state:oc.bt.b;power:oc.bt.i"
322 * OC_STACK_OK - no errors
323 * OC_STACK_ERROR - stack process error
325 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
326 const char *resourceTypeName,
327 const char *resourceAttributeRepresentation);
329 * Bind a resource interface to a resource.
331 * @param handle - handle to the container resource
332 * @param resourceInterfaceName - name of resource interface. Example: "core.rw"
333 * @param allowedMethods - methods permitted on interface. Example: OC_REST_GET|OC_REST_PUT
336 * OC_STACK_OK - no errors
337 * OC_STACK_ERROR - stack process error
339 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
340 const char *resourceInterfaceName,
341 uint8_t allowedMethods);
344 * Bind an entity handler to the resource.
346 * @param handle - handle to the resource that the contained resource is to be bound
347 * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
349 * OC_STACK_OK - no errors
350 * OC_STACK_ERROR - stack process error
352 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler);
355 * Get the number of resources that have been created in the stack.
357 * @param numResources - pointer to count variable
360 * OC_STACK_OK - no errors
361 * OC_STACK_ERROR - stack process error
364 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
367 * Get a resource handle by index.
369 * @param index - index of resource, 0 to Count - 1
372 * Resource handle - if found
373 * NULL - if not found
375 OCResourceHandle OCGetResourceHandle(uint8_t index);
378 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
381 * @param handle - handle of resource to be deleted
384 * OC_STACK_OK - no errors
385 * OC_STACK_ERROR - stack process error
387 OCStackResult OCDeleteResource(OCResourceHandle handle);
390 * Get the URI of the resource specified by handle.
392 * @param handle - handle of resource
394 * URI string - if resource found
395 * NULL - resource not found
397 const char *OCGetResourceUri(OCResourceHandle handle);
400 * Get the properties of the resource specified by handle.
401 * NOTE: that after a resource is created, the OC_ACTIVE property is set
402 * for the resource by the stack.
404 * @param handle - handle of resource
406 * property bitmap - if resource found
407 * NULL - resource not found
409 uint8_t OCGetResourceProperties(OCResourceHandle handle);
412 * Get the number of resource types of the resource.
414 * @param handle - handle of resource
415 * @param numResourceTypes - pointer to count variable
418 * OC_STACK_OK - no errors
419 * OC_STACK_ERROR - stack process error
421 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
424 * Get name of resource type of the resource.
426 * @param handle - handle of resource
427 * @param index - index of resource, 0 to Count - 1
430 * resource type name - if resource found
431 * NULL - resource not found
433 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
436 * Get attributes of resource type of the resource.
438 * @param handle - handle of resource
439 * @param index - index of resource, 0 to Count - 1
442 * resource type attributes - if resource found
443 * NULL - resource not found
445 const char *OCGetResourceAttributeRepresentation(OCResourceHandle handle, uint8_t index);
448 * Get the number of resource interfaces of the resource.
450 * @param handle - handle of resource
451 * @param numResources - pointer to count variable
454 * OC_STACK_OK - no errors
455 * OC_STACK_ERROR - stack process error
458 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle, uint8_t *numResourceInterfaces);
461 * Get name of resource interface of the resource.
463 * @param handle - handle of resource
464 * @param index - index of resource, 0 to Count - 1
467 * resource interface name - if resource found
468 * NULL - resource not found
470 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
473 * Get methods of resource interface of the resource.
475 * @param handle - handle of resource
476 * @param index - index of resource, 0 to Count - 1
479 * allowed methods - if resource found
480 * NULL - resource not found
482 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
485 * Get name of resource interface of the resource.
487 * @param containerHandle - handle of container resource
488 * @param index - index of contained resource, 0 to Count - 1
491 * handle to contained resource - if resource found
492 * NULL - resource not found
494 OCResourceHandle OCGetContainedResourceHandle(OCResourceHandle containerHandle, uint8_t index);
497 * Get the entity handler for a resource.
499 * @param handle - handle of resource
502 * entity handler - if resource found
503 * NULL - resource not found
505 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
508 * Notify observers that an observed value has changed.
510 * **NOTE: This API has NOT been finalized!!!**
512 * @param handle - handle of resource
515 * OC_STACK_OK - no errors
516 * OC_STACK_ERROR - stack not initialized
518 OCStackResult OCNotifyObservers(OCResourceHandle handle);
522 #endif // __cplusplus
524 #endif /* OCSTACK_H_ */