Moved all presence functionality into the stack layer from coap layer.
[platform/upstream/iotivity.git] / resource / csdk / stack / include / internal / ocstackinternal.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 //
21
22 //-----------------------------------------------------------------------------
23 // Internal include file used by lower layers of the OC stack
24 //-----------------------------------------------------------------------------
25 #ifndef OCSTACKINTERNAL_H_
26 #define OCSTACKINTERNAL_H_
27
28 //-----------------------------------------------------------------------------
29 // Includes
30 //-----------------------------------------------------------------------------
31 #include <stdbool.h>
32 #include "ocstack.h"
33 #include "ocstackconfig.h"
34 #include "occoaptoken.h"
35 #include "occlientcb.h"
36 #include <logger.h>
37 #include <ocrandom.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif // __cplusplus
42
43
44 //-----------------------------------------------------------------------------
45 // Global variables
46 //-----------------------------------------------------------------------------
47 extern OCDeviceEntityHandler defaultDeviceHandler;
48
49 //-----------------------------------------------------------------------------
50 // Defines
51 //-----------------------------------------------------------------------------
52 #define OC_COAP_SCHEME "coap://"
53 #define OC_OFFSET_SEQUENCE_NUMBER (4) // the first outgoing sequence number will be 5
54
55 typedef struct {
56     // Observe option field
57     uint32_t option;
58     // IP address & port of client registered for observe
59     OCDevAddr *subAddr;
60     // CoAP token for the observe request
61     OCCoAPToken *token;
62     // The result of the observe request
63     OCStackResult result;
64 } OCObserveReq;
65
66 // following structure will be created in occoap and passed up the stack on the server side
67 typedef struct {
68     // Observe option field
69     uint32_t observationOption;
70     // the REST method retrieved from received request PDU
71     OCMethod method;
72     // resourceUrl will be filled in occoap using the path options in received request PDU
73     unsigned char resourceUrl[MAX_URI_LENGTH];
74     // resource query send by client
75     unsigned char query[MAX_QUERY_LENGTH];
76     // reqJSON is retrieved from the payload of the received request PDU
77     unsigned char reqJSONPayload[MAX_REQUEST_LENGTH];
78     // qos is indicating if the request is CON or NON
79     OCQualityOfService qos;
80     // An array of the received vendor specific header options
81     uint8_t numRcvdVendorSpecificHeaderOptions;
82     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
83     //////////////////////////////////////////////////////////
84     // TODO: Consider moving these member to CoAP
85     // IP address & port of client registered for observe
86     OCDevAddr requesterAddr;
87     // CoAP token for the observe request
88     OCCoAPToken requestToken;
89     // The ID of CoAP pdu
90     uint16_t coapID;
91     uint8_t delayedResNeeded;
92     uint8_t secured;
93     //////////////////////////////////////////////////////////
94     uint8_t reqMorePacket;
95     uint32_t reqPacketNum;
96     uint16_t reqPacketSize;
97     uint32_t resPacketNum;
98     uint16_t resPacketSize;
99     uint32_t reqTotalSize;
100 } OCServerProtocolRequest;
101
102 typedef struct
103 {
104     // Observe option field
105     uint32_t observationOption;
106     // qos is indicating if the request is CON or NON
107     OCQualityOfService qos;
108     // Allow the entity handler to pass a result with the response
109     OCStackResult result;
110     // IP address & port of client registered for observe
111     OCDevAddr *requesterAddr;
112     // CoAP token for the observe request
113     OCCoAPToken *requestToken;
114     // The ID of CoAP pdu
115     uint16_t coapID;
116     // Flag indicating that response is to be delayed before sending
117     uint8_t delayedResNeeded;
118     uint8_t secured;
119     uint8_t slowFlag;
120     uint8_t notificationFlag;
121     // this is the pointer to server payload data to be transferred
122     unsigned char *payload;
123     // size of server payload data.  Don't rely on null terminated data for size
124     uint16_t payloadSize;
125     // An array of the vendor specific header options the entity handler wishes to use in response
126     uint8_t numSendVendorSpecificHeaderOptions;
127     OCHeaderOption *sendVendorSpecificHeaderOptions;
128     // URI of new resource that entity handler might create
129     unsigned char * resourceUri;
130 } OCServerProtocolResponse;
131
132 // following structure will be created in occoap and passed up the stack on the client side
133 typedef struct {
134     // handle is retrieved by comparing the token-handle pair in the PDU.
135     ClientCB * cbNode;
136     // This is how long this response is valid for (in seconds).
137     uint32_t maxAge;
138     // This is the Uri of the resource. (ex. "coap://192.168.1.1/a/led")
139     unsigned char * fullUri;
140     // This is the relative Uri of the resource. (ex. "/a/led")
141     unsigned char * rcvdUri;
142     // This is the received payload.
143     unsigned char * bufRes;
144     // This is the token received OTA.
145     OCCoAPToken * rcvdToken;
146     // this structure will be passed to client
147     OCClientResponse * clientResponse;
148 } OCResponse;
149
150 // following typedef is to represent our Server Instance identification.
151 typedef uint32_t ServerID;
152
153 //-----------------------------------------------------------------------------
154 // Internal function prototypes
155 //-----------------------------------------------------------------------------
156 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status);
157 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
158 OCStackResult HandleStackResponses(OCResponse * response);
159 #ifdef WITH_PRESENCE
160 OCStackResult SendPresenceNotification(OCResourceType *resourceType);
161 #endif // WITH_PRESENCE
162 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
163
164 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
165                                             const char *resourceInterfaceName);
166
167 OCStackResult BindResourceTypeToResource(OCResource* resource,
168                                             const char *resourceTypeName);
169 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName);
170
171 // returns the internal representation of the server instance ID.
172 // Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
173 // This is done automatically during the OCInit process (via the call to OCInitCoAP),
174 // so ensure that this call is done after that.
175 const ServerID OCGetServerInstanceID(void);
176
177 // returns a string representation  the server instance ID.
178 // The memory is managed internal to this function, so freeing externally will result
179 // in a compiler error
180 // Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
181 // This is done automatically during the OCInit process (via the call to OCInitCoAP),
182 // so ensure that this call is done after that.
183 const char* OCGetServerInstanceIDString(void);
184 #ifdef WITH_PRESENCE
185 //TODO: should the following function be public?
186 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
187         OCResourceProperty resourceProperties, uint8_t enable);
188 #endif
189
190 #ifdef __cplusplus
191 }
192 #endif // __cplusplus
193
194 #endif /* OCSTACKINTERNAL_H_ */