Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderResource.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 "NSProviderResource.h"
22
23 const char* NSType = "oic.r.notification";
24 const char* NSMessageType = "oic.r.notification.message";
25 const char* NSSyncType = "oic.r.notification.sync";
26
27 const char* NSInterface = "oic.if.baseline";
28 const char* NSMessgeInterface = "oic.if.baseline.message";
29 const char* NSSyncInterface = "oic.if.baseline.sync";
30
31 const char* NSUri = "/notification";
32 const char* NSMessageUri = "/notification/message";
33 const char* NSSyncUri = "/notification/sync";
34
35 /* Structure to represent notification resources */
36 typedef struct
37 {
38     OCResourceHandle handle;
39     int accepter;
40     char* message_uri;
41     char* sync_uri;
42 } NSNotificationResource;
43
44 typedef struct
45 {
46     OCResourceHandle handle;
47     char* id;
48     char* title;
49     char* body;
50 } NSMessageResource;
51
52 typedef struct
53 {
54     OCResourceHandle handle;
55     char* id;
56     char* state;
57     NSDevice device;
58 } NSSyncResource;
59
60 NSNotificationResource NotificationResource;
61 NSMessageResource NotificationMessageResource;
62 NSSyncResource NotificationSyncResource;
63
64 NSResult NSCreateResource(char *uri)
65 {
66     if (!uri)
67     {
68         OIC_LOG(ERROR, RESOURCE_TAG, "Resource URI cannot be NULL");
69         return NS_ERROR;
70     }
71
72     if (strcmp(uri, NSUri) == 0)
73     {
74
75         NotificationResource.accepter = 0;
76         NotificationResource.message_uri = NSMessageUri;
77         NotificationResource.sync_uri = NSSyncUri;
78         NotificationResource.handle = NULL;
79
80         if (OCCreateResource(&NotificationResource.handle, NSType, NSInterface, NSUri,
81                 NSEntityHandlerNotificationCb, NULL, OC_DISCOVERABLE) != OC_STACK_OK)
82         {
83             OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Resource"));
84             return NS_ERROR;
85         }
86
87         printf("NotificationResource.handle = %u\n", NotificationResource.handle);
88
89
90     }
91     else if (strcmp(uri, NSMessageUri) == 0)
92     {
93
94         NotificationMessageResource.id = NULL;
95         NotificationMessageResource.title = NULL;
96         NotificationMessageResource.body = NULL;
97         NotificationMessageResource.handle = NULL;
98
99         if (OCCreateResource(&NotificationMessageResource.handle, NSMessageType, NSInterface,
100                 NSMessageUri, NSEntityHandlerMessageCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
101         {
102             OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Message Resource"));
103             return NS_ERROR;
104         }
105
106         printf("NotificationMessageResource.handle = %u\n", NotificationMessageResource.handle);
107
108
109     }
110     else if (strcmp(uri, NSSyncUri) == 0)
111     {
112         NotificationSyncResource.id = NULL;
113         NotificationSyncResource.state = NULL;
114         NotificationSyncResource.device;
115         NotificationSyncResource.handle = NULL;
116
117         if (OCCreateResource(&(NotificationSyncResource.handle), NSSyncType, NSInterface, NSSyncUri,
118                 NSEntityHandlerSyncCb, NULL, OC_OBSERVABLE) != OC_STACK_OK)
119         {
120             OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to Create Notification Sync Resource"));
121             return NS_ERROR;
122         }
123
124         printf("NotificationSyncResource.handle = %u\n", NotificationSyncResource.handle);
125
126     }
127     else
128     {
129         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to create resource with invalid URI"));
130         return NS_ERROR;
131     }
132
133     return NS_OK;
134 }
135
136 NSResult NSRegisterResource()
137 {
138     OIC_LOG(INFO, RESOURCE_TAG, "NSRegisterResource");
139
140     if (NSCreateResource(NSSyncUri) != NS_OK)
141     {
142         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Sync Resource"));
143         return NS_ERROR;
144     }
145
146     if (NSCreateResource(NSMessageUri) != NS_OK)
147     {
148         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Message Resource"));
149         return NS_ERROR;
150     }
151
152     if (NSCreateResource(NSUri) != NS_OK)
153     {
154         OIC_LOG(ERROR, RESOURCE_TAG, PCF("Fail to register Notification Resource"));
155         return NS_ERROR;
156     }
157
158     return NS_OK;
159 }
160
161 NSResult NSPutNotificationResource(int accepter, OCResourceHandle * handle)
162 {
163     NotificationResource.accepter = accepter;
164     NotificationResource.message_uri = NSMessageUri;
165     NotificationResource.sync_uri = NSSyncUri;
166
167     *handle = NotificationResource.handle;
168
169     return NS_OK;
170 }
171
172 NSResult NSPutMessageResource(NSMessage *msg, OCResourceHandle * handle)
173 {
174     OIC_LOG(INFO, RESOURCE_TAG, "Put notification message to Resource");
175
176     if(msg != NULL)
177     {
178         printf("msg is not null\n");
179         NotificationMessageResource.id = strdup(msg->mId);
180         NotificationMessageResource.title = strdup(msg->mTitle);
181         NotificationMessageResource.body = strdup(msg->mContentText);
182     }
183     else
184     {
185         printf("msg is null\n");
186     }
187
188     *handle = NotificationMessageResource.handle;
189
190     return NS_OK;
191 }
192
193 NSResult NSPutSyncResource(NSSync *sync, OCResourceHandle * handle)
194 {
195     OIC_LOG(INFO, RESOURCE_TAG, "Put notification sync to Resource");
196
197     *handle = NotificationSyncResource.handle;
198
199     return NS_OK;
200 }
201
202 const char* NSGetNotificationUri()
203 {
204     return NSUri;
205 }
206
207 const char* NSGetNotificationMessageUri()
208 {
209     return NSMessageUri;
210 }
211
212 const char* NSGetNotificationSyncUri()
213 {
214     return NSSyncUri;
215 }
216
217 NSResult NSCopyString(char** targetString, const char* sourceString)
218 {
219     if (sourceString)
220     {
221         *targetString = (char *) malloc(strlen(sourceString) + 1);
222
223         if (*targetString)
224         {
225             strncpy(*targetString, sourceString, (strlen(sourceString) + 1));
226             return NS_SUCCESS;
227         }
228     }
229
230     return NS_FAIL;
231 }