Added Initial version of notification.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerDiscovery.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 "NSConsumerDiscovery.h"
22
23 #include <string.h>
24 #include "NSCommon.h"
25 #include "NSConsumerCommon.h"
26 #include "ocpayload.h"
27 #include "oic_malloc.h"
28
29 #define NS_PAYLOAD_KEY_ACCEPTER "ACCEPTER"
30 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
31 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
32 #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
33
34 OCDoHandle * getPresenceHandle()
35 {
36     static OCDoHandle * g_PresenceHandle = NULL;
37
38     return g_PresenceHandle;
39 }
40
41 OCStackApplicationResult NSConsumerPresenceListener(
42         OCDoHandle handle, OCClientResponse * clientResponse)
43 {
44     (void) handle;
45     NS_CONSUMER_LOG_V(DEBUG, "Presence income : %s:%d",
46             clientResponse->devAddr.addr, clientResponse->devAddr.port);
47     NS_CONSUMER_LOG_V(DEBUG, "Presence result : %d",
48             clientResponse->result);
49     NS_CONSUMER_LOG_V(DEBUG, "Presence sequenceNum : %d",
50             clientResponse->sequenceNumber);
51
52     if (!NSIsStartedConsumer())
53     {
54         return OC_STACK_DELETE_TRANSACTION;
55     }
56
57     OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
58     if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
59             clientResponse->result == OC_STACK_PRESENCE_STOPPED)
60     {
61         // TODO find request and cancel
62         NS_CONSUMER_LOG(DEBUG, "stopped presence or resource is deleted.");
63         //OCCancel(handle, NS_QOS, NULL, 0);
64     }
65
66     else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
67     {
68     NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, clientResponse->addr,
69             NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener);
70     }
71
72     return OC_STACK_KEEP_TRANSACTION;
73 }
74
75 OCStackApplicationResult NSProviderDiscoverListener(
76         OCDoHandle handle, OCClientResponse * clientResponse)
77 {
78     (void) handle;
79     NS_CONSUMER_LOG_V(DEBUG, "Discover income : %s:%d",
80             clientResponse->devAddr.addr, clientResponse->devAddr.port);
81     NS_CONSUMER_LOG_V(DEBUG, "Discover result : %d",
82             clientResponse->result);
83     NS_CONSUMER_LOG_V(DEBUG, "Discover sequenceNum : %d",
84             clientResponse->sequenceNumber);
85
86     if (!NSIsStartedConsumer())
87     {
88         return OC_STACK_DELETE_TRANSACTION;
89     }
90
91     if (!clientResponse->payload)
92     {
93         return OC_STACK_KEEP_TRANSACTION;
94     }
95
96     OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
97     while (resource)
98     {
99         if (!strcmp(resource->uri, NS_RESOURCE_URI))
100         {
101             NSRequestToResourceIntrospection(
102                     NULL, OC_REST_GET, clientResponse->addr,
103                     NS_RESOURCE_URI, NULL, NSGetProviderInformation);
104         }
105         resource = resource->next;
106     }
107
108     return OC_STACK_KEEP_TRANSACTION;
109 }
110
111 OCStackApplicationResult NSGetProviderInformation(
112         OCDoHandle handle, OCClientResponse * clientResponse)
113 {
114     (void) handle;
115     int64_t accepter = 0;
116
117     NS_CONSUMER_LOG_V(DEBUG, "GET response income : %s:%d",
118             clientResponse->devAddr.addr, clientResponse->devAddr.port);
119     NS_CONSUMER_LOG_V(DEBUG, "GET response result : %d",
120             clientResponse->result);
121     NS_CONSUMER_LOG_V(DEBUG, "GET response sequenceNum : %d",
122             clientResponse->sequenceNumber);
123     NS_CONSUMER_LOG_V(DEBUG, "GET response resource uri : %s",
124             clientResponse->resourceUri);
125
126     if (!NSIsStartedConsumer())
127     {
128         return OC_STACK_DELETE_TRANSACTION;
129     }
130
131     if (!clientResponse->payload)
132     {
133         NS_CONSUMER_LOG(ERROR, "payload is null");
134         return OC_STACK_KEEP_TRANSACTION;
135     }
136
137     if (!OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
138             NS_PAYLOAD_KEY_ACCEPTER, & accepter))
139     {
140         NS_CONSUMER_LOG(ERROR, "can not seach for accepter");
141         return OC_STACK_KEEP_TRANSACTION;
142     }
143
144     NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
145     if (!newProvider)
146     {
147         NS_CONSUMER_LOG(DEBUG, "NSProvider allocation fail");
148         return OC_STACK_KEEP_TRANSACTION;
149     }
150
151     // TODO set id
152     newProvider->mId = NULL;
153     newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
154     if (!newProvider)
155     {
156         NS_CONSUMER_LOG(DEBUG, "OCDevAddr allocation fail");
157         OICFree(newProvider);
158         return OC_STACK_KEEP_TRANSACTION;
159     }
160     memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
161
162     {
163         OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
164         while (payload)
165         {
166             NS_CONSUMER_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
167             payload = payload->next;
168         }
169     }
170
171     if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
172             "MESSAGE_URI", & newProvider->messageUri))
173     {
174         OICFree(newProvider->mUserData);
175         OICFree(newProvider);
176         NS_CONSUMER_LOG(ERROR, "can not seach for message uri");
177         return OC_STACK_KEEP_TRANSACTION;
178     }
179
180     if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
181             NS_ATTRIBUTE_SYNC, & newProvider->syncUri))
182     {
183         OICFree(newProvider->messageUri);
184         OICFree(newProvider->mUserData);
185         OICFree(newProvider);
186         NS_CONSUMER_LOG(ERROR, "can not seach for sync uri");
187         return OC_STACK_KEEP_TRANSACTION;
188     }
189
190     if (accepter == NS_ACCEPTER_CONSUMER)
191     {
192         NS_CONSUMER_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
193
194         NSDiscoveredProvider(newProvider);
195     }
196     else
197     {
198         NS_CONSUMER_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
199
200         NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider);
201         if (!task)
202         {
203             NS_CONSUMER_LOG(DEBUG, "NSTask allocation fail");
204             return OC_STACK_KEEP_TRANSACTION;
205         }
206
207         NSConsumerPushEvent(task);
208     }
209
210     return OC_STACK_KEEP_TRANSACTION;
211 }
212
213 void NSConsumerDiscoveryHandleMsg(NSTask * task)
214 {
215     if (!task)
216     {
217         NS_CONSUMER_LOG(ERROR, "task is null");
218         return;
219     }
220
221     NS_CONSUMER_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
222     if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
223     {
224         NSRequestToResourceIntrospection(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY,
225                 NULL, NSProviderDiscoverListener);
226     }
227     else
228     {
229         NS_CONSUMER_LOG(ERROR, "Unknown type message");
230     }
231 }
232