Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerCommon.h
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics 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 #ifndef _NS_CONSUMER_CONSTANTS_H_
22 #define _NS_CONSUMER_CONSTANTS_H_
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif // __cplusplus
27
28 #include <stdio.h>
29 #include <stdbool.h>
30
31 #include "NSConsumerInterface.h"
32 #include "NSStructs.h"
33 #include "ocstack.h"
34
35 #define NS_QOS OC_LOW_QOS
36 #define NS_RESOURCE_TYPE "oic.r.notification"
37 #define NS_RESOURCE_URI "/notification"
38 #define NS_INTERFACE_BASELINE "oic.if.baseline"
39 #define NS_INTERFACE_NOTIFICATION "oic.if.notification"
40 #define NS_RESOURCE_QUERY "/oic/res"
41
42 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
43 #define NS_DEVICE_ID_LENGTH 37
44
45 #define NS_VERIFY_NOT_NULL_V(obj) \
46     { \
47         if ((obj) == NULL) \
48         { \
49             NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \
50             return; \
51         } \
52     }
53
54 #define NS_VERIFY_NOT_NULL(obj, retVal) \
55     { \
56         if ((obj) == NULL) \
57         { \
58             NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \
59             return (retVal); \
60         } \
61     }
62
63 #define NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(obj, func) \
64     { \
65         if ((obj) == NULL) \
66         { \
67             NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \
68             NS_LOG(ERROR, "execute deletion"); \
69             (func); \
70             return; \
71         } \
72     }
73
74 #define NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, retVal, func) \
75     { \
76         if ((obj) == NULL) \
77         { \
78             NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \
79             NS_LOG(ERROR, "execute deletion"); \
80             (func); \
81             return (retVal); \
82         } \
83     }
84
85 #define NS_VERIFY_STACK_OK_V(obj) \
86     { \
87         OCStackResult _ret = (obj); \
88         if ( _ret != OC_STACK_OK) \
89         { \
90             NS_LOG_V(ERROR, "%s : %s is not OC_STACK_OK : %d", __func__, #obj, _ret); \
91             return; \
92         } \
93     }
94
95 #define NS_VERIFY_STACK_OK(obj, retVal) \
96     { \
97         OCStackResult _ret = (obj); \
98         if ( _ret != OC_STACK_OK) \
99         { \
100             NS_LOG_V(ERROR, "%s : %s is not OC_STACK_OK : %d", __func__, #obj, _ret); \
101             return (retVal); \
102         } \
103     }
104
105 #define NS_VERIFY_STACK_OK_WITH_POST_CLEANING(obj, retVal, func) \
106     { \
107         OCStackResult _ret = (obj); \
108         if ( _ret != OC_STACK_OK) \
109         { \
110             NS_LOG_V(ERROR, "%s : %s is not OC_STACK_OK : %d", __func__, #obj, _ret); \
111             (func); \
112             return (retVal); \
113         } \
114     }
115
116 #define NSOICFree(obj) \
117     { \
118         if ((obj)) \
119         { \
120             OICFree((obj)); \
121             (obj) = NULL; \
122             NS_LOG_V(DEBUG, "%s : %s Removed", __func__, #obj); \
123         } \
124     }
125
126 typedef enum
127 {
128     NS_DISCOVER_DEFAULT, // will work for adapter_ip.
129     NS_DISCOVER_UDP,
130     NS_DISCOVER_TCP,
131     NS_DISCOVER_CLOUD
132 } NSConsumerDiscoverType;
133
134 typedef struct NSProviderConnectionInfo
135 {
136     OCDevAddr * addr;
137
138     OCDoHandle messageHandle;
139     OCDoHandle syncHandle;
140
141     bool isCloudConnection;
142
143     struct NSProviderConnectionInfo * next;
144
145 } NSProviderConnectionInfo;
146
147 typedef struct
148 {
149     char providerId[NS_DEVICE_ID_LENGTH];
150
151     char * messageUri;
152     char * syncUri;
153
154     NSAccessPolicy accessPolicy;
155
156     NSProviderConnectionInfo * connection;
157
158 } NSProvider_internal;
159
160 typedef struct
161 {
162     uint64_t messageId;
163     char providerId[NS_DEVICE_ID_LENGTH];
164     NSSyncType state;
165
166     NSProviderConnectionInfo * connection;
167
168 } NSSyncInfo_internal;
169
170 typedef struct
171 {
172     // Mandatory
173     uint64_t messageId;
174     char providerId[NS_DEVICE_ID_LENGTH];
175
176     //optional
177     NSMessageType type;
178     char * dateTime;
179     uint64_t ttl;
180     char * title;
181     char * contentText;
182     char * sourceName;
183     NSMediaContents mediaContents;
184
185     OCDevAddr * i_addr;
186     NSSyncType i_messageTypes;
187 } NSMessage_consumer;
188
189 bool NSIsStartedConsumer();
190 void NSSetIsStartedConsumer(bool setValue);
191
192 void NSSetDiscoverProviderCb(NSProviderDiscoveredCallback cb);
193 void NSDiscoveredProvider(NSProvider * provider);
194
195 void NSSetSubscriptionAcceptedCb(NSSubscriptionAcceptedCallback cb);
196 void NSSubscriptionAccepted(NSProvider * provider);
197
198 void NSSetMessagePostedCb(NSMessageReceivedCallback  cb);
199 void NSMessagePost(NSMessage * obj);
200
201 void NSSetNotificationSyncCb(NSSyncInfoReceivedCallback cb);
202 void NSNotificationSync(NSSyncInfo * sync);
203
204 char ** NSGetConsumerId();
205 void NSSetConsumerId(char * cId);
206
207 char * NSMakeRequestUriWithConsumerId(const char * uri);
208
209 NSTask * NSMakeTask(NSTaskType, void *);
210
211 NSResult NSConsumerPushEvent(NSTask *);
212
213 NSMessage_consumer * NSCopyMessage(NSMessage_consumer *);
214 void NSRemoveMessage(NSMessage_consumer *);
215
216 NSProviderConnectionInfo * NSCreateProviderConnections(OCDevAddr * inAddr);
217 NSProviderConnectionInfo * NSCopyProviderConnections(NSProviderConnectionInfo * conn);
218 void NSRemoveConnections(NSProviderConnectionInfo * connections);
219
220 NSProvider_internal * NSCopyProvider(NSProvider_internal *);
221 void NSRemoveProvider(NSProvider_internal *);
222
223 OCStackResult NSInvokeRequest(OCDoHandle * handle,
224         OCMethod method, const OCDevAddr * addr,
225         const char * queryUrl, OCPayload * payload,
226         void * callbackFunc, void * callbackData, OCConnectivityType type);
227
228 #ifdef __cplusplus
229 }
230 #endif // __cplusplus
231
232 #endif // _NS_CONSUMER_CONSTANTS_H_