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 "NSConsumerMessageHandler.h"
29 #include "oic_malloc.h"
30
31 // Public APIs
32 NSResult NSStartConsumer(
33         NSProviderDiscoveredCallback discoverCb,
34         NSNotificationReceivedCallback  postCb,
35         NSSyncCallback syncCb)
36 {
37     if (NSIsStartedConsumer())
38     {
39         printf("is already started consumer service?\n");
40         return NS_OK;
41     }
42
43     NSSetDiscoverProviderCb(discoverCb);
44     NSSetNotificationPostedCb(postCb);
45     NSSetNotificationSyncCb(syncCb);
46     NSSetIsStartedConsumer(true);
47
48     if (NS_OK != NSConsumerMessageHandlerInit())
49     {
50         return NS_ERROR;
51     }
52
53     return NS_OK;
54 }
55
56 NSResult NSStopConsumer()
57 {
58     NSSetDiscoverProviderCb(NULL);
59     NSSetNotificationPostedCb(NULL);
60     NSSetNotificationSyncCb(NULL);
61     NSSetIsStartedConsumer(false);
62
63     return NS_OK;
64 }
65
66 NSResult NSSubscribeProvider(NSProvider * provider)
67 {
68     NSTask * subscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider);
69     if (!subscribeTask)
70     {
71         NS_CONSUMER_LOG(ERROR, "NSTask allocation fail");
72         return NS_ERROR;
73     }
74
75     return NSConsumerPushEvent(subscribeTask);
76 }
77
78 NSResult NSUnsubscribeProvider(NSProvider * provider)
79 {
80     NSTask * unsubscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) provider);
81     if (!unsubscribeTask)
82     {
83         NS_CONSUMER_LOG(ERROR, "NSTask allocation fail");
84         return NS_ERROR;
85     }
86
87     return NSConsumerPushEvent(unsubscribeTask);
88 }
89
90 NSResult NSRescanProvider()
91 {
92     NSTask * discoverTask = NSMakeTask(TASK_CONSUMER_REQ_DISCOVER, NULL);
93     if (!discoverTask)
94     {
95         NS_CONSUMER_LOG(ERROR, "NSTask allocation fail");
96         return NS_ERROR;
97     }
98
99     return NSConsumerPushEvent(discoverTask);
100 }
101
102 NSResult NSRead(NSMessage * obj)
103 {
104     NSTask * readTask = NSMakeTask(TASK_SEND_READ, (void *) obj);
105     if (!readTask)
106     {
107         NS_CONSUMER_LOG(ERROR, "NSTask allocation fail");
108         return NS_ERROR;
109     }
110
111     return NSConsumerPushEvent(readTask);
112 }
113
114 NSResult NSDismiss(NSMessage * obj)
115 {
116     NSTask * dismissTask = NSMakeTask(TASK_SEND_DISMISS, (void *) obj);
117     if (!dismissTask)
118     {
119         NS_CONSUMER_LOG(ERROR, "NSTask allocation fail");
120         return NS_ERROR;
121     }
122
123     return NSConsumerPushEvent(dismissTask);
124 }
125
126 NSResult NSDropNSObject(NSMessage * obj)
127 {
128     if (!obj)
129     {
130         return NS_ERROR;
131     }
132
133     if (obj->mId)
134     {
135         OICFree(obj->mId);
136         obj->mId = NULL;
137     }
138
139     if (obj->mTitle)
140     {
141         OICFree(obj->mTitle);
142         obj->mTitle = NULL;
143     }
144
145     if (obj->mContentText)
146     {
147         OICFree(obj->mContentText);
148         obj->mContentText = NULL;
149     }
150
151     OICFree(obj);
152
153     return NS_OK;
154 }