Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerCommunication.c
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 #include "NSConstants.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConsumerCommunication.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
26 #include "ocpayload.h"
27
28 #define NS_SYNC_URI "/notification/sync"
29
30 unsigned long NS_MESSAGE_ACCEPTANCE = 1;
31
32 NSMessage_consumer * NSCreateMessage_internal(uint64_t msgId, const char * providerId);
33 NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state);
34
35 NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse);
36 NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse);
37
38 OCRepPayload * NSGetofSyncInfoPayload(NSMessage_consumer * message, int type);
39 OCStackResult NSSendSyncInfoUsingMessage(NSMessage_consumer * message, int type);
40
41 // TODO it seem to not to be this file
42 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
43
44 NSResult NSConsumerSubscribeProvider(NSProvider * provider)
45 {
46     NSProvider_internal * provider_internal = (NSProvider_internal *) provider;
47     NS_VERIFY_NOT_NULL(provider_internal, NS_ERROR);
48
49     NS_LOG(DEBUG, "get subscribe message query");
50     char * query = NSMakeRequestUriWithConsumerId(provider_internal->messageUri);
51     NS_VERIFY_NOT_NULL(query, NS_ERROR);
52
53     NS_LOG(DEBUG, "subscribe message");
54     NS_LOG_V(DEBUG, "subscribe query : %s", query);
55     OCStackResult ret = NSInvokeRequest(&(provider_internal->i_messageHandle),
56                           OC_REST_OBSERVE, provider_internal->i_addr,
57                           query, NULL, NSConsumerMessageListener, NULL);
58     NS_VERIFY_STACK_OK(ret, NS_ERROR);
59     NSOICFree(query);
60
61     NS_LOG(DEBUG, "get subscribe sync query");
62     query = NSMakeRequestUriWithConsumerId(provider_internal->syncUri);
63     NS_VERIFY_NOT_NULL(query, NS_ERROR);
64
65     NS_LOG(DEBUG, "subscribe sync");
66     NS_LOG_V(DEBUG, "subscribe query : %s", query);
67     ret = NSInvokeRequest(&(provider_internal->i_syncHandle),
68                           OC_REST_OBSERVE, provider_internal->i_addr,
69                           query, NULL, NSConsumerSyncInfoListener, NULL);
70     NS_VERIFY_STACK_OK(ret, NS_ERROR);
71     NSOICFree(query);
72
73     return NS_OK;
74 }
75
76 OCStackApplicationResult NSConsumerCheckPostResult(
77         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
78 {
79     (void) ctx;
80     (void) handle;
81
82     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
83     NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
84
85     return OC_STACK_KEEP_TRANSACTION;
86 }
87
88 void NSRemoveSyncInfoObj(NSSyncInfo * sync)
89 {
90     NSOICFree(sync);
91 }
92
93 OCStackApplicationResult NSConsumerSyncInfoListener(
94         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
95 {
96     (void) ctx;
97     (void) handle;
98
99     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
100     NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
101
102     NS_LOG(DEBUG, "get NSSyncInfo");
103     NSSyncInfo * newSync = NSGetSyncInfoc(clientResponse);
104     NS_VERIFY_NOT_NULL(newSync, OC_STACK_KEEP_TRANSACTION);
105
106     NSTaskType taskType = TASK_RECV_SYNCINFO;
107
108     NS_LOG(DEBUG, "build NSTask");
109     NSTask * task = NSMakeTask(taskType, (void *) newSync);
110     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task,
111                OC_STACK_KEEP_TRANSACTION, NSRemoveSyncInfoObj(newSync));
112
113     NSConsumerPushEvent(task);
114
115     return OC_STACK_KEEP_TRANSACTION;
116 }
117
118 OCStackApplicationResult NSConsumerMessageListener(
119         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
120 {
121     (void) ctx;
122     (void) handle;
123
124     NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
125     NS_VERIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
126
127     NS_LOG(DEBUG, "build NSMessage");
128     NSMessage_consumer * newNoti = NSGetMessage(clientResponse);
129     NS_VERIFY_NOT_NULL(newNoti, OC_STACK_KEEP_TRANSACTION);
130
131     NSTaskType type = TASK_CONSUMER_RECV_MESSAGE;
132
133     if (newNoti->messageId == NS_MESSAGE_ACCEPTANCE)
134     {
135         NS_LOG(DEBUG, "Receive Subscribe confirm");
136         type = TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED;
137     }
138     else
139     {
140         NS_LOG(DEBUG, "Receive new message");
141     }
142
143     NS_LOG(DEBUG, "build NSTask");
144     NSTask * task = NSMakeTask(type, (void *) newNoti);
145     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(newNoti));
146
147     NSConsumerPushEvent(task);
148
149     return OC_STACK_KEEP_TRANSACTION;
150 }
151
152 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
153 {
154     NSMessage_consumer * cachedNoti = NSGetMessage(clientResponse);
155     NS_LOG(DEBUG, "build NSMessage");
156     NS_VERIFY_NOT_NULL(cachedNoti, NS_ERROR);
157
158     NS_LOG(DEBUG, "build NSTask");
159     NSTask * task = NSMakeTask(type, (void *) cachedNoti);
160     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(cachedNoti));
161
162     NSConsumerPushEvent(task);
163
164     return NS_OK;
165 }
166
167 void NSGetMessagePostClean(char * pId, OCDevAddr * addr)
168 {
169     NSOICFree(pId);
170     NSOICFree(addr);
171 }
172
173 NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse)
174 {
175     NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
176     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
177
178     NS_LOG(DEBUG, "get msg id");
179     uint64_t id = NULL;
180     bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
181     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
182
183     NS_LOG(DEBUG, "get provider id");
184     char * pId = NULL;
185     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
186     NS_LOG_V (DEBUG, "provider id: %s", pId);
187     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
188
189     NS_LOG(DEBUG, "get provider address");
190     OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
191     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL, NSGetMessagePostClean(pId, addr));
192     memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
193
194     NS_LOG(DEBUG, "create NSMessage");
195     NSMessage_consumer * retMsg = NSCreateMessage_internal(id, pId);
196     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retMsg, NULL, NSGetMessagePostClean(pId, addr));
197     NSOICFree(pId);
198
199     retMsg->i_addr = addr;
200     retMsg->i_messageTypes = NS_SYNC_UNREAD;
201
202     NS_LOG(DEBUG, "get msg optional field");
203     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retMsg->title);
204     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retMsg->contentText);
205     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retMsg->sourceName);
206
207     OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TYPE, (int64_t *)&retMsg->type);
208     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retMsg->dateTime);
209     OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retMsg->ttl);
210
211     NS_LOG_V(DEBUG, "Msg Address : %s", retMsg->i_addr->addr);
212     NS_LOG_V(DEBUG, "Msg ID      : %llu", retMsg->messageId);
213     NS_LOG_V(DEBUG, "Msg Title   : %s", retMsg->title);
214     NS_LOG_V(DEBUG, "Msg Content : %s", retMsg->contentText);
215     NS_LOG_V(DEBUG, "Msg Source  : %s", retMsg->sourceName);
216     NS_LOG_V(DEBUG, "Msg Type    : %d", retMsg->type);
217     NS_LOG_V(DEBUG, "Msg Date    : %s", retMsg->dateTime);
218     NS_LOG_V(DEBUG, "Msg ttl     : %llu", retMsg->ttl);
219
220     return retMsg;
221 }
222
223 NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
224 {
225     NS_VERIFY_NOT_NULL(clientResponse->payload, NULL);
226
227     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
228
229     NS_LOG(DEBUG, "get msg id");
230     uint64_t id = NULL;
231     bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
232     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
233
234     NS_LOG(DEBUG, "get provider id");
235     char * pId = NULL;
236     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
237     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
238
239     NS_LOG(DEBUG, "get state");
240     int64_t state = 0;
241     getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state);
242     NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
243
244     NS_LOG(DEBUG, "create NSSyncInfo");
245     NSSyncInfo * retSync = NSCreateSyncInfo_consumer(id, pId, (NSSyncType)state);
246     NS_VERIFY_NOT_NULL(retSync, NULL);
247
248     NS_LOG_V(DEBUG, "Sync ID : %llu", retSync->messageId);
249     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
250     NS_LOG_V(DEBUG, "Sync Provider ID : %s", retSync->providerId);
251
252     return retSync;
253 }
254
255 NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * providerId)
256 {
257     NSMessage_consumer * retMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
258     NS_VERIFY_NOT_NULL(retMsg, NULL);
259
260     retMsg->messageId = id;
261     OICStrcpy(retMsg->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
262     retMsg->title = NULL;
263     retMsg->contentText = NULL;
264     retMsg->sourceName = NULL;
265     retMsg->type = NS_MESSAGE_INFO;
266     retMsg->dateTime = NULL;
267     retMsg->ttl = 0;
268     retMsg->i_addr = NULL;
269
270     return retMsg;
271 }
272
273 NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state)
274 {
275     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
276     NS_VERIFY_NOT_NULL(retSync, NULL);
277
278     retSync->messageId = msgId;
279     retSync->state = state;
280     OICStrcpy(retSync->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
281
282     return retSync;
283 }
284
285 OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr)
286 {
287     OCRepPayload * payload = OCRepPayloadCreate();
288     NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR);
289
290     OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)syncInfo->messageId);
291     OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, syncInfo->state);
292     OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, syncInfo->providerId);
293
294     return NSInvokeRequest(NULL, OC_REST_POST, addr,
295                            NS_SYNC_URI, (OCPayload*)payload,
296                            NSConsumerCheckPostResult, NULL);
297 }
298
299 void NSConsumerCommunicationTaskProcessing(NSTask * task)
300 {
301     NS_VERIFY_NOT_NULL_V(task);
302
303     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
304     if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE)
305     {
306         NS_LOG(DEBUG, "Request Subscribe");
307         NSResult ret = NSConsumerSubscribeProvider((NSProvider *)task->taskData);
308         NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
309     }
310     else if (task->taskType == TASK_SEND_SYNCINFO)
311     {
312         // TODO find target OCDevAddr using provider Id.
313         NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)task->taskData;
314         OCDevAddr * addr = syncInfo->i_addr;
315         OCStackResult ret = NSSendSyncInfo((NSSyncInfo *)(task->taskData), addr);
316         NS_VERIFY_STACK_OK_V(ret);
317
318         NSOICFree(syncInfo->i_addr);
319         NSOICFree(syncInfo);
320     }
321     else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
322     {
323         NSProvider_internal * provider = (NSProvider_internal *)task->taskData;
324
325         OCCancel(provider->i_messageHandle, NS_QOS, NULL, 0);
326         OCCancel(provider->i_syncHandle, NS_QOS, NULL, 0);
327     }
328     else
329     {
330         NS_LOG(ERROR, "Unknown type message");
331     }
332 }