Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerInterface.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 "NSConsumerInterface.h"
22
23 #include <stdlib.h>
24 #include <stdbool.h>
25
26 #include "NSCommon.h"
27 #include "NSConsumerCommon.h"
28 #include "NSConstants.h"
29 #include "NSConsumerQueueScheduler.h"
30 #include "oic_malloc.h"
31
32 // Public APIs
33 NSResult NSStartConsumer(
34         NSProviderDiscoveredCallback discoverCb,
35         NSNotificationReceivedCallback  postCb,
36         NSSyncCallback syncCb)
37 {
38     bool isStartedConsumer = NSIsStartedConsumer();
39     NS_VERTIFY_NOT_NULL(isStartedConsumer == false ? (void *) 1 : NULL, NS_OK);
40
41     NS_VERTIFY_NOT_NULL(discoverCb, NS_ERROR);
42     NS_VERTIFY_NOT_NULL(postCb, NS_ERROR);
43     NS_VERTIFY_NOT_NULL(syncCb, NS_ERROR);
44
45     NSSetDiscoverProviderCb(discoverCb);
46     NSSetNotificationPostedCb(postCb);
47     NSSetNotificationSyncCb(syncCb);
48     NSSetIsStartedConsumer(true);
49
50     NSResult ret = NSConsumerMessageHandlerInit();
51     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
52             NS_ERROR, NSStopConsumer());
53
54     return NS_OK;
55 }
56
57 NSResult NSStopConsumer()
58 {
59     NSSetDiscoverProviderCb(NULL);
60     NSSetNotificationPostedCb(NULL);
61     NSSetNotificationSyncCb(NULL);
62     NSSetIsStartedConsumer(false);
63
64     NSConsumerMessageHandlerExit();
65
66     return NS_OK;
67 }
68
69 NSResult NSSubscribe(NSProvider * provider)
70 {
71     NSTask * subscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider);
72     NS_VERTIFY_NOT_NULL(subscribeTask, NS_ERROR);
73
74     return NSConsumerPushEvent(subscribeTask);
75 }
76
77 NSResult NSUnsubscribe(NSProvider * provider)
78 {
79     NSTask * unsubscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) provider);
80     NS_VERTIFY_NOT_NULL(unsubscribeTask, NS_ERROR);
81
82     return NSConsumerPushEvent(unsubscribeTask);
83 }
84
85 NSResult NSRescanProvider()
86 {
87     NSTask * discoverTask = NSMakeTask(TASK_CONSUMER_REQ_DISCOVER, NULL);
88     NS_VERTIFY_NOT_NULL(discoverTask, NS_ERROR);
89
90     return NSConsumerPushEvent(discoverTask);
91 }
92
93 NSResult NSConsumerReadCheck(NSMessage * obj)
94 {
95     NSTask * readTask = NSMakeTask(TASK_SEND_READ, (void *) obj);
96     NS_VERTIFY_NOT_NULL(readTask, NS_ERROR);
97
98     return NSConsumerPushEvent(readTask);
99 }
100
101 NSResult NSConsumerDismissCheck(NSMessage * obj)
102 {
103     NSTask * dismissTask = NSMakeTask(TASK_SEND_DISMISS, (void *) obj);
104     NS_VERTIFY_NOT_NULL(dismissTask, NS_ERROR);
105
106     return NSConsumerPushEvent(dismissTask);
107 }
108
109 NSResult NSDropNSObject(NSMessage * obj)
110 {
111     NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
112
113     if (obj->messageId)
114     {
115         obj->messageId = 0;
116     }
117
118     if (obj->title)
119     {
120         OICFree(obj->title);
121         obj->title = NULL;
122     }
123
124     if (obj->contentText)
125     {
126         OICFree(obj->contentText);
127         obj->contentText = NULL;
128     }
129
130     if (obj->sourceName)
131     {
132         OICFree(obj->sourceName);
133         obj->sourceName = NULL;
134     }
135
136     OICFree(obj);
137
138     return NS_OK;
139 }