1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 * This file contains the Internal include file used by lower layers of the OC stack
31 #ifndef OCSTACKINTERNAL_H_
32 #define OCSTACKINTERNAL_H_
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 #include "ocstackconfig.h"
40 #include "occlientcb.h"
44 #include "cainterface.h"
45 #include "experimental/securevirtualresourcetypes.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 /** Default device entity Handler.*/
57 extern OCDeviceEntityHandler defaultDeviceHandler;
59 /** Default Callback parameter.*/
60 extern void* defaultDeviceHandlerCallbackParameter;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 /** The coap scheme */
67 #define OC_COAP_SCHEME "coap://"
69 /** the first outgoing sequence number will be 1*/
70 #define OC_OFFSET_SEQUENCE_NUMBER (0)
73 * This structure will be created in occoap and passed up the stack on the server side.
77 /** Observe option field.*/
78 uint32_t observationOption;
80 /** The REST method retrieved from received request PDU.*/
83 /** the provided payload format. */
84 OCPayloadFormat payloadFormat;
86 /** the requested payload format. */
87 OCPayloadFormat acceptFormat;
89 /** the requested payload version. */
90 uint16_t acceptVersion;
92 /** resourceUrl will be filled in occoap using the path options in received request PDU.*/
93 char resourceUrl[MAX_URI_LENGTH];
95 /** resource query send by client.*/
96 char query[MAX_QUERY_LENGTH];
98 /** reqJSON is retrieved from the payload of the received request PDU.*/
101 /** qos is indicating if the request is CON or NON.*/
102 OCQualityOfService qos;
104 /** Number of the received vendor specific header options.*/
105 uint8_t numRcvdVendorSpecificHeaderOptions;
107 /** Array of received vendor specific header option .*/
108 OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
110 /** Remote end-point address **/
113 /** Token for the observe request.*/
114 CAToken_t requestToken;
119 /** The ID of CoAP PDU.*/
122 /** For delayed Response.*/
123 uint8_t delayedResNeeded;
125 /** For More packet.*/
126 uint8_t reqMorePacket;
128 /** The number of requested packet.*/
129 uint32_t reqPacketNum;
131 /** The size of requested packet.*/
132 uint16_t reqPacketSize;
134 /** The number of responded packet.*/
135 uint32_t resPacketNum;
137 /** Responded packet size.*/
138 uint16_t resPacketSize;
140 /** The total size of requested packet.*/
142 } OCServerProtocolRequest;
145 * This typedef is to represent our Server Instance identification.
147 typedef uint8_t ServerID[16];
149 //-----------------------------------------------------------------------------
150 // Internal function prototypes
151 //-----------------------------------------------------------------------------
155 * Handler function for sending a response from multiple resources, such as a collection.
156 * Aggregates responses from multiple resource until all responses are received then sends the
157 * concatenated response
159 * TODO: Need to add a timeout in case a (remote?) resource does not respond
161 * @param token Token to search for.
162 * @param tokenLength Length of token.
163 * @param status Feedback status.
168 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
172 * Handler function to execute stack requests
174 * @param protocolRequest Pointer to the protocol requests from server.
179 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
181 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
182 const CAResponseResult_t responseResult, const CAMessageType_t type,
183 const uint8_t numOptions, const CAHeaderOption_t *options,
184 CAToken_t token, uint8_t tokenLength, const char *resourceUri,
185 CADataType_t dataType);
190 * Notify Presence subscribers that a resource has been modified.
192 * @param resourceType Handle to the resourceType linked list of resource that was modified.
193 * @param trigger The simplified reason this API was invoked.
195 * @return ::OC_STACK_OK on success, some other value upon failure.
197 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
198 OCPresenceTrigger trigger);
201 * Send Stop Notification to Presence subscribers.
203 * @return ::OC_STACK_OK on success, some other value upon failure.
205 OCStackResult SendStopNotification();
206 #endif // WITH_PRESENCE
209 * Bind a resource interface to a resource.
211 * @param resource Target resource.
212 * @param resourceInterfaceName Resource interface.
214 * @return ::OC_STACK_OK on success, some other value upon failure.
216 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
217 const char *resourceInterfaceName);
219 * Bind a resource type to a resource.
221 * @param resource Target resource.
222 * @param resourceTypeName Name of resource type.
223 * @return ::OC_STACK_OK on success, some other value upon failure.
225 OCStackResult BindResourceTypeToResource(OCResource *resource,
226 const char *resourceTypeName);
228 * Bind a Transport Protocol Suites type to a resource.
230 * @param resource Target resource.
231 * @param resourceTpsTypes Name of transport protocol suites type.
232 * @return ::OC_STACK_OK on success, some other value upon failure.
234 OCStackResult BindTpsTypeToResource(OCResource *resource,
235 OCTpsSchemeFlags resourceTpsTypes);
238 * Convert OCStackResult to CAResponseResult_t.
240 * @param ocCode OCStackResult code.
241 * @param method OCMethod method the return code replies to.
242 * @return ::CA_CONTENT on OK, some other value upon failure.
244 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
247 * Converts a CAResult_t type to a OCStackResult type.
249 * @param caResult CAResult_t value to convert.
250 * @return OCStackResult that was converted from the input CAResult_t value.
252 OCStackResult CAResultToOCResult(CAResult_t caResult);
255 * Converts a OCStackResult type to a bool type.
257 * @param ocResult OCStackResult value to convert.
258 * @return true on success, false upon failure.
260 bool OCResultToSuccess(OCStackResult ocResult);
263 * Map OCQualityOfService to CAMessageType.
265 * @param qos Input qos.
267 * @return CA message type for a given qos.
269 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
273 * Enable/disable a resource property.
275 * @param inputProperty Pointer to resource property.
276 * @param resourceProperties Property to be enabled/disabled.
277 * @param enable 0:disable, 1:enable.
279 * @return OCStackResult that was converted from the input CAResult_t value.
281 //TODO: should the following function be public?
282 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
283 OCResourceProperty resourceProperties, uint8_t enable);
286 const char *OC_CALL convertTriggerEnumToString(OCPresenceTrigger trigger);
288 OCPresenceTrigger OC_CALL convertTriggerStringToEnum(const char * triggerStr);
290 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
292 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
295 * Get the CoAP ticks after the specified number of milli-seconds.
297 * @param milliSeconds Milli-seconds.
300 uint32_t GetTicks(uint32_t milliSeconds);
303 * Extract interface and resource type from the query.
305 * @param query is the request received from the client
306 * @param filterOne will include result if the interface is included in the query.
307 * @param filterTwo will include result if the resource type is included in the query.
309 * @return ::OC_STACK_OK on success, some other value upon failure
311 OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo);
313 #if defined(RD_CLIENT) || defined(RD_SERVER)
315 * This function binds an resource unique ins value to the resource. This can be only called
316 * when the stack has received a response from resource-directory.
318 * @param requestUri URI of the resource.
319 * @param response Response from queries to remote servers.
321 * @return ::OC_STACK_OK on success, some other value upon failure.
323 OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
324 const OCClientResponse *response);
328 * Delete all of the dynamically allocated elements that were created for the resource attributes.
330 * @param rsrcAttributes Specified resource attribute.
332 void OCDeleteResourceAttributes(OCAttribute *rsrcAttributes);
336 * Add resource payload with endpoint payload to discovery payload.
338 * @param payload Pointer to discovery payload.
339 * @param res Pointer to OCresource structure.
340 * @param securePort Secure port number.
341 * @param networkInfo List of CAEndpoint_t.
342 * @param infoSize Size of CAEndpoint_t list.
343 * @param devAddr Pointer to OCDevAddr structure.
345 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
346 uint16_t securePort, void *networkInfo, size_t infoSize,
347 const OCDevAddr *devAddr);
350 * Add resource payload with endpoint payload to discovery payload.
352 * @param payload Pointer to discovery payload.
353 * @param res Pointer to OCresource structure.
354 * @param securePort Secure port number.
355 * @param networkInfo List of CAEndpoint_t.
356 * @param infoSize Size of CAEndpoint_t list.
357 * @param devAddr Pointer to OCDevAddr structure.
358 * @param tcpPort TCP port number.
360 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
361 uint16_t securePort, void *networkInfo, size_t infoSize,
362 const OCDevAddr *devAddr, uint16_t tcpPort);
364 /* This method will retrieve the tcp port */
365 OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port, bool secured);
369 * This function creates list of OCEndpointPayload structure,
370 * which matches with the resource's endpointType from list of
373 * @param[in] resource the resource
374 * @param[in] devAddr devAddr Structure pointing to the address.
375 * @param[in] networkInfo array of CAEndpoint_t
376 * @param[in] infoSize size of array
377 * @param[out] listHead pointer to HeadNode pointer
378 * @param[out] epSize size of array(set NULL not to use it)
379 * @param[out] selfEp endpoint that matches devAddr for use in anchor(set NULL not to use it)
381 * @return if success return pointer else NULL
383 OCEndpointPayload* CreateEndpointPayloadList(const OCResource *resource,
384 const OCDevAddr *devAddr, CAEndpoint_t *networkInfo,
385 size_t infoSize, OCEndpointPayload **listHead, size_t* epSize, OCEndpointPayload** selfEp);
388 * This function returns to destroy endpoint payload
391 void OC_CALL OCEndpointPayloadDestroy(OCEndpointPayload* payload);
393 // Check on Accept Version option.
394 bool OCRequestIsOCFContentFormat(OCEntityHandlerRequest *ehRequest, bool* isOCFContentFormat);
398 #endif // __cplusplus
400 #endif /* OCSTACKINTERNAL_H_ */