Merge "[CA-Integration] Use 5298 port for MC query since CA uses 5298 as MC port...
[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 CA_INT
40     #include "cacommon.h"
41     #include "cainterface.h"
42 #endif
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif // __cplusplus
47
48
49 //-----------------------------------------------------------------------------
50 // Global variables
51 //-----------------------------------------------------------------------------
52 extern OCDeviceEntityHandler defaultDeviceHandler;
53
54 //-----------------------------------------------------------------------------
55 // Defines
56 //-----------------------------------------------------------------------------
57 #define OC_COAP_SCHEME "coap://"
58 #define OC_OFFSET_SEQUENCE_NUMBER (4) // the first outgoing sequence number will be 5
59
60 #ifdef CA_INT
61 // TODO-CA: This has been defined in CA layer as well, but is not exposed externally.
62 // Need to expose it from CA and remove the definition below.
63 #define CA_MAX_TOKEN_LEN   (8)
64 #endif //CA_INT
65
66 typedef struct {
67     // Observe option field
68     uint32_t option;
69     // IP address & port of client registered for observe
70     OCDevAddr *subAddr;
71     // CoAP token for the observe request
72     OCCoAPToken *token;
73     // The result of the observe request
74     OCStackResult result;
75 } OCObserveReq;
76
77 // following structure will be created in occoap and passed up the stack on the server side
78 typedef struct {
79     // Observe option field
80     uint32_t observationOption;
81     // the REST method retrieved from received request PDU
82     OCMethod method;
83     // resourceUrl will be filled in occoap using the path options in received request PDU
84     unsigned char resourceUrl[MAX_URI_LENGTH];
85     // resource query send by client
86     unsigned char query[MAX_QUERY_LENGTH];
87     // reqJSON is retrieved from the payload of the received request PDU
88     unsigned char reqJSONPayload[MAX_REQUEST_LENGTH];
89     // qos is indicating if the request is CON or NON
90     OCQualityOfService qos;
91     // An array of the received vendor specific header options
92     uint8_t numRcvdVendorSpecificHeaderOptions;
93     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
94 #ifdef CA_INT
95     /** Remote Endpoint address **/
96     CAAddress_t addressInfo;
97     /** Connectivity of the endpoint**/
98     CAConnectivityType_t connectivityType;
99     char token[CA_MAX_TOKEN_LEN+1];
100 #endif
101     //////////////////////////////////////////////////////////
102     // TODO: Consider moving these member to CoAP
103     // IP address & port of client registered for observe
104     OCDevAddr requesterAddr;
105     // CoAP token for the observe request
106     OCCoAPToken requestToken;
107     // The ID of CoAP pdu
108     uint16_t coapID;
109     uint8_t delayedResNeeded;
110     uint8_t secured;
111     //////////////////////////////////////////////////////////
112     uint8_t reqMorePacket;
113     uint32_t reqPacketNum;
114     uint16_t reqPacketSize;
115     uint32_t resPacketNum;
116     uint16_t resPacketSize;
117     size_t reqTotalSize;
118 } OCServerProtocolRequest;
119
120 typedef struct
121 {
122     // Observe option field
123     uint32_t observationOption;
124     // qos is indicating if the request is CON or NON
125     OCQualityOfService qos;
126     // Allow the entity handler to pass a result with the response
127     OCStackResult result;
128     // IP address & port of client registered for observe
129     OCDevAddr *requesterAddr;
130     // CoAP token for the observe request
131     OCCoAPToken *requestToken;
132     // The ID of CoAP pdu
133     uint16_t coapID;
134     // Flag indicating that response is to be delayed before sending
135     uint8_t delayedResNeeded;
136     uint8_t secured;
137     uint8_t slowFlag;
138     uint8_t notificationFlag;
139     // this is the pointer to server payload data to be transferred
140     unsigned char *payload;
141     // size of server payload data.  Don't rely on null terminated data for size
142     uint16_t payloadSize;
143     // An array of the vendor specific header options the entity handler wishes to use in response
144     uint8_t numSendVendorSpecificHeaderOptions;
145     OCHeaderOption *sendVendorSpecificHeaderOptions;
146     // URI of new resource that entity handler might create
147     unsigned char * resourceUri;
148 } OCServerProtocolResponse;
149
150 // following structure will be created in occoap and passed up the stack on the client side
151 typedef struct {
152     // handle is retrieved by comparing the token-handle pair in the PDU.
153     ClientCB * cbNode;
154     // This is how long this response is valid for (in seconds).
155     uint32_t maxAge;
156     // This is the Uri of the resource. (ex. "coap://192.168.1.1/a/led")
157     unsigned char * fullUri;
158     // This is the relative Uri of the resource. (ex. "/a/led")
159     unsigned char * rcvdUri;
160     // This is the received payload.
161     unsigned char * bufRes;
162     // This is the token received OTA.
163     OCCoAPToken * rcvdToken;
164     // this structure will be passed to client
165     OCClientResponse * clientResponse;
166 } OCResponse;
167
168 // following typedef is to represent our Server Instance identification.
169 typedef uint32_t ServerID;
170
171 //-----------------------------------------------------------------------------
172 // Internal function prototypes
173 //-----------------------------------------------------------------------------
174 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status);
175 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
176 OCStackResult HandleStackResponses(OCResponse * response);
177 #ifdef WITH_PRESENCE
178 OCStackResult SendPresenceNotification(OCResourceType *resourceType);
179 #endif // WITH_PRESENCE
180 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
181
182 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
183                                             const char *resourceInterfaceName);
184
185 OCStackResult BindResourceTypeToResource(OCResource* resource,
186                                             const char *resourceTypeName);
187 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName);
188
189 // returns the internal representation of the server instance ID.
190 // Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
191 // This is done automatically during the OCInit process (via the call to OCInitCoAP),
192 // so ensure that this call is done after that.
193 const ServerID OCGetServerInstanceID(void);
194
195 // returns a string representation  the server instance ID.
196 // The memory is managed internal to this function, so freeing externally will result
197 // in a compiler error
198 // Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
199 // This is done automatically during the OCInit process (via the call to OCInitCoAP),
200 // so ensure that this call is done after that.
201 const char* OCGetServerInstanceIDString(void);
202 #ifdef WITH_PRESENCE
203 //TODO: should the following function be public?
204 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
205         OCResourceProperty resourceProperties, uint8_t enable);
206 #endif
207
208 #ifdef __cplusplus
209 }
210 #endif // __cplusplus
211
212 #endif /* OCSTACKINTERNAL_H_ */