Merge branch 'master' into connectivity-abstraction
[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     // the REST method retrieved from received request PDU
69     OCMethod method;
70     // resourceUrl will be filled in occoap using the path options in received request PDU
71     unsigned char resourceUrl[MAX_URI_LENGTH];
72     // resource query send by client
73     unsigned char query[MAX_QUERY_LENGTH];
74     // reqJSON is retrieved from the payload of the received request PDU
75     unsigned char reqJSONPayload[MAX_REQUEST_LENGTH];
76     // qos is indicating if the request is CON or NON
77     OCQualityOfService qos;
78     // Observe option field
79     uint32_t observationOption;
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     // qos is indicating if the request is CON or NON
105     OCQualityOfService qos;
106     // Observe option field
107     uint32_t observationOption;
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 TTL;
138     // this structure will be passed to client
139     OCClientResponse * clientResponse;
140 } OCResponse;
141
142 // following typedef is to represent our Server Instance identification.
143 typedef uint32_t ServerID;
144
145 //-----------------------------------------------------------------------------
146 // Internal function prototypes
147 //-----------------------------------------------------------------------------
148 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status);
149 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
150 void HandleStackResponses(OCResponse * response);
151 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
152 #ifdef WITH_PRESENCE
153 OCStackResult SendPresenceNotification(OCResourceType *resourceType);
154 #endif
155
156 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
157                                             const char *resourceInterfaceName);
158
159 OCStackResult BindResourceTypeToResource(OCResource* resource,
160                                             const char *resourceTypeName);
161 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName);
162
163 // returns the internal representation of the server instance ID.
164 // Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
165 // This is done automatically during the OCInit process (via the call to OCInitCoAP),
166 // so ensure that this call is done after that.
167 const ServerID OCGetServerInstanceID(void);
168
169 // returns a string representation  the server instance ID.
170 // The memory is managed internal to this function, so freeing externally will result
171 // in a compiler error
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 char* OCGetServerInstanceIDString(void);
176 #ifdef WITH_PRESENCE
177 //TODO: should the following function be public?
178 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
179         OCResourceProperty resourceProperties, uint8_t enable);
180 #endif
181
182 #ifdef __cplusplus
183 }
184 #endif // __cplusplus
185
186 #endif /* OCSTACKINTERNAL_H_ */