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