Modified consumer internal function call flow.
[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 #include "oic_string.h"
30
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 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
36
37 OCStackApplicationResult NSConsumerPresenceListener(
38         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
39 {
40     (void) ctx;
41     (void) handle;
42
43     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
44     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
45
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         NS_LOG(DEBUG, "started presence or resource is created.");
70         NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
71             NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
72     }
73
74     return OC_STACK_KEEP_TRANSACTION;
75 }
76
77 OCStackApplicationResult NSProviderDiscoverListener(
78         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
79 {
80     (void) ctx;
81     (void) handle;
82
83     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
84     NS_VERTIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
85     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
86
87     NS_LOG_V(DEBUG, "Discover income : %s:%d",
88             clientResponse->devAddr.addr, clientResponse->devAddr.port);
89     NS_LOG_V(DEBUG, "Discover result : %d",
90             clientResponse->result);
91     NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
92             clientResponse->sequenceNumber);
93
94     if (!NSIsStartedConsumer())
95     {
96         return OC_STACK_DELETE_TRANSACTION;
97     }
98
99     OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
100     while (resource)
101     {
102         if (!strcmp(resource->uri, NS_RESOURCE_URI))
103         {
104             NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
105                     NS_RESOURCE_URI, NULL, NSIntrospectProvider, NULL);
106         }
107         resource = resource->next;
108     }
109
110     return OC_STACK_KEEP_TRANSACTION;
111 }
112
113 void NSRemoveProviderObj(NSProvider_internal * provider)
114 {
115     NSOICFree(provider->messageUri);
116     NSOICFree(provider->syncUri);
117
118     provider->messageHandle = NULL;
119     provider->syncHandle = NULL;
120     NSOICFree(provider->addr);
121
122     NSOICFree(provider);
123 }
124
125 OCStackApplicationResult NSIntrospectProvider(
126         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
127 {
128     (void) ctx;
129     (void) handle;
130
131     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
132     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
133
134     NS_LOG_V(DEBUG, "GET response income : %s:%d",
135             clientResponse->devAddr.addr, clientResponse->devAddr.port);
136     NS_LOG_V(DEBUG, "GET response result : %d",
137             clientResponse->result);
138     NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
139             clientResponse->sequenceNumber);
140     NS_LOG_V(DEBUG, "GET response resource uri : %s",
141             clientResponse->resourceUri);
142
143     if (!NSIsStartedConsumer())
144     {
145         return OC_STACK_DELETE_TRANSACTION;
146     }
147
148     NSProvider_internal * newProvider = NSGetProvider(clientResponse);
149     NS_VERTIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
150
151     NS_LOG(DEBUG, "build NSTask");
152     NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
153     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProviderObj(newProvider));
154
155     NSConsumerPushEvent(task);
156
157     return OC_STACK_KEEP_TRANSACTION;
158 }
159
160 void NSGetProviderPostClean(char * pId, char * mUri, char * sUri, OCDevAddr * addr)
161 {
162     NSOICFree(pId);
163     NSOICFree(mUri);
164     NSOICFree(sUri);
165     NSOICFree(addr);
166 }
167
168 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
169 {
170     NS_LOG(DEBUG, "create NSProvider");
171     NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
172
173     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
174     while (payload)
175     {
176         NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
177         payload = payload->next;
178     }
179
180     payload = (OCRepPayload *)clientResponse->payload;
181
182     char * providerId = NULL;
183     char * messageUri = NULL;
184     char * syncUri = NULL;
185     int64_t accepter = 0;
186     OCDevAddr * addr = NULL;
187
188     NS_LOG(DEBUG, "get information of accepter");
189     bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_POLICY, & accepter);
190     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
191
192     NS_LOG(DEBUG, "get provider ID");
193     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, & providerId);
194     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
195
196     NS_LOG(DEBUG, "get message URI");
197     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_MESSAGE, & messageUri);
198     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
199             NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
200
201     NS_LOG(DEBUG, "get sync URI");
202     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SYNC, & syncUri);
203     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
204             NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
205
206     NS_LOG(DEBUG, "get provider address");
207     addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
208     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL,
209            NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
210     memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
211
212     NSProvider_internal * newProvider
213         = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
214     NS_VERTIFY_NOT_NULL(newProvider, NULL);
215
216     OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
217     NSOICFree(providerId);
218     newProvider->messageUri = messageUri;
219     newProvider->syncUri = syncUri;
220     newProvider->accessPolicy = (NSAccessPolicy)accepter;
221     newProvider->addr = addr;
222     newProvider->messageHandle = NULL;
223     newProvider->syncHandle = NULL;
224
225     return newProvider;
226 }
227
228 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
229 {
230     NS_VERTIFY_NOT_NULL_V(task);
231
232     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
233     if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
234     {
235         NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY,
236                 NULL, NSProviderDiscoverListener, NULL);
237     }
238     else
239     {
240         NS_LOG(ERROR, "Unknown type message");
241     }
242 }