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