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"
45 #include "cainterface.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 5*/
70 #define OC_OFFSET_SEQUENCE_NUMBER (4)
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 /** resourceUrl will be filled in occoap using the path options in received request PDU.*/
84 char resourceUrl[MAX_URI_LENGTH];
86 /** resource query send by client.*/
87 char query[MAX_QUERY_LENGTH];
89 /** reqJSON is retrieved from the payload of the received request PDU.*/
90 uint8_t payload[MAX_REQUEST_LENGTH];
92 /** qos is indicating if the request is CON or NON.*/
93 OCQualityOfService qos;
95 /** Number of the received vendor specific header options.*/
96 uint8_t numRcvdVendorSpecificHeaderOptions;
98 /** Array of received vendor specific header option .*/
99 OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
101 /** Remote end-point address **/
104 /** Token for the observe request.*/
105 CAToken_t requestToken;
110 /** The ID of CoAP PDU.*/
113 /** For delayed Response.*/
114 uint8_t delayedResNeeded;
116 /** For More packet.*/
117 uint8_t reqMorePacket;
119 /** The number of requested packet.*/
120 uint32_t reqPacketNum;
122 /** The size of requested packet.*/
123 uint16_t reqPacketSize;
125 /** The number of responded packet.*/
126 uint32_t resPacketNum;
128 /** Responded packet size.*/
129 uint16_t resPacketSize;
131 /** The total size of requested packet.*/
133 } OCServerProtocolRequest;
136 * This structure will be created in occoap and passed up the stack on the client side.
140 /** handle is retrieved by comparing the token-handle pair in the PDU.*/
143 /** This is how long this response is valid for (in seconds).*/
146 /** This is the Uri of the resource. (ex. "coap://192.168.1.1/a/led").*/
149 /** This is the relative Uri of the resource. (ex. "/a/led").*/
152 /** This is the received payload.*/
155 /** This is the token received OTA.*/
158 /** this structure will be passed to client.*/
159 OCClientResponse * clientResponse;
163 * This typedef is to represent our Server Instance identification.
165 typedef uint8_t ServerID[16];
167 //-----------------------------------------------------------------------------
168 // Internal function prototypes
169 //-----------------------------------------------------------------------------
173 * Handler function for sending a response from multiple resources, such as a collection.
174 * Aggregates responses from multiple resource until all responses are received then sends the
175 * concatenated response
177 * TODO: Need to add a timeout in case a (remote?) resource does not respond
179 * @param token Token to search for.
180 * @param tokenLength Length of token.
181 * @param status Feedback status.
186 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
190 * Handler function to execute stack requests
192 * @param protocolRequest Pointer to the protocol requests from server.
197 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
199 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
200 const CAResponseResult_t responseResult, const CAMessageType_t type,
201 const uint8_t numOptions, const CAHeaderOption_t *options,
202 CAToken_t token, uint8_t tokenLength);
207 * Notify Presence subscribers that a resource has been modified.
209 * @param resourceType Handle to the resourceType linked list of resource that was modified.
210 * @param trigger The simplified reason this API was invoked.
212 * @return ::OC_STACK_OK on success, some other value upon failure.
214 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
215 OCPresenceTrigger trigger);
218 * Send Stop Notification to Presence subscribers.
220 * @return ::OC_STACK_OK on success, some other value upon failure.
222 OCStackResult SendStopNotification();
223 #endif // WITH_PRESENCE
226 * Function to parse the IPv4 address.
228 * @param ipAddrStr Pointer to a string of IPv4 address.
229 * @param ipAddr pointer to IPv4 adress.
230 * @param port Port number.
232 * @return true on success, false upon failure.
234 bool ParseIPv4Address(char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
237 * Bind a resource interface to a resource.
239 * @param resource Target resource.
240 * @param resourceInterfaceName Resource interface.
242 * @return ::OC_STACK_OK on success, some other value upon failure.
244 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
245 const char *resourceInterfaceName);
247 * Bind a resource type to a resource.
249 * @param resource Target resource.
250 * @param resourceTypeName Name of resource type.
251 * @return ::OC_STACK_OK on success, some other value upon failure.
253 OCStackResult BindResourceTypeToResource(OCResource* resource,
254 const char *resourceTypeName);
258 * Converts a CAResult_t type to a OCStackResult type.
260 * @param caResult CAResult_t value to convert.
261 * @return OCStackResult that was converted from the input CAResult_t value.
263 OCStackResult CAResultToOCResult(CAResult_t caResult);
266 * Get a byte representation of the server instance ID.
267 * The memory is managed internal to this function, so freeing it externally will
268 * result in a runtime error.
270 * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
271 * This is done automatically during the OCInit process,
272 * so ensure that this call is done after that.
274 * @return A uint8_t representation the server instance ID.
276 const uint8_t* OCGetServerInstanceID(void);
279 * Get a string representation the server instance ID.
280 * The memory is managed internal to this function, so freeing externally will result
281 * in a runtime error.
282 * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
283 * This is done automatically during the OCInit process,
284 * so ensure that this call is done after that.
286 * @return A string representation the server instance ID.
288 const char* OCGetServerInstanceIDString(void);
291 * Map OCQualityOfService to CAMessageType.
293 * @param qos Input qos.
295 * @return CA message type for a given qos.
297 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
301 * Enable/disable a resource property.
303 * @param inputProperty Pointer to resource property.
304 * @param resourceProperties Property to be enabled/disabled.
305 * @param enable 0:disable, 1:enable.
307 * @return OCStackResult that was converted from the input CAResult_t value.
309 //TODO: should the following function be public?
310 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
311 OCResourceProperty resourceProperties, uint8_t enable);
314 const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
316 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
318 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
320 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
324 #endif // __cplusplus
326 #endif /* OCSTACKINTERNAL_H_ */