Modified Error handle of 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 OCStackApplicationResult NSConsumerPresenceListener(
36         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
37 {
38     (void) ctx;
39     (void) handle;
40
41     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
42     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
43
44     NS_LOG_V(DEBUG, "Presence income : %s:%d",
45             clientResponse->devAddr.addr, clientResponse->devAddr.port);
46     NS_LOG_V(DEBUG, "Presence result : %d",
47             clientResponse->result);
48     NS_LOG_V(DEBUG, "Presence sequenceNum : %d",
49             clientResponse->sequenceNumber);
50
51     if (!NSIsStartedConsumer())
52     {
53         return OC_STACK_DELETE_TRANSACTION;
54     }
55
56     OCPresencePayload * payload = (OCPresencePayload *)clientResponse->payload;
57     if (payload->trigger == OC_PRESENCE_TRIGGER_DELETE ||
58             clientResponse->result == OC_STACK_PRESENCE_STOPPED)
59     {
60         // TODO find request and cancel
61         NS_LOG(DEBUG, "stopped presence or resource is deleted.");
62         //OCCancel(handle, NS_QOS, NULL, 0);
63     }
64
65     else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
66     {
67         NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
68             NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
69     }
70
71     return OC_STACK_KEEP_TRANSACTION;
72 }
73
74 OCStackApplicationResult NSProviderDiscoverListener(
75         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
76 {
77     (void) ctx;
78     (void) handle;
79
80     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
81     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
82
83     NS_LOG_V(DEBUG, "Discover income : %s:%d",
84             clientResponse->devAddr.addr, clientResponse->devAddr.port);
85     NS_LOG_V(DEBUG, "Discover result : %d",
86             clientResponse->result);
87     NS_LOG_V(DEBUG, "Discover sequenceNum : %d",
88             clientResponse->sequenceNumber);
89
90     if (!NSIsStartedConsumer())
91     {
92         return OC_STACK_DELETE_TRANSACTION;
93     }
94
95     if (!clientResponse->payload)
96     {
97         return OC_STACK_KEEP_TRANSACTION;
98     }
99
100     OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
101     while (resource)
102     {
103         if (!strcmp(resource->uri, NS_RESOURCE_URI))
104         {
105             NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
106                     NS_RESOURCE_URI, NULL, NSIntrospectProvider, NULL);
107         }
108         resource = resource->next;
109     }
110
111     return OC_STACK_KEEP_TRANSACTION;
112 }
113
114 OCStackApplicationResult NSIntrospectProvider(
115         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
116 {
117     (void) ctx;
118     (void) handle;
119
120     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
121     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
122
123     int64_t accepter = 0;
124     char * messageUri = NULL;
125     char * syncUri = NULL;
126
127     NS_LOG_V(DEBUG, "GET response income : %s:%d",
128             clientResponse->devAddr.addr, clientResponse->devAddr.port);
129     NS_LOG_V(DEBUG, "GET response result : %d",
130             clientResponse->result);
131     NS_LOG_V(DEBUG, "GET response sequenceNum : %d",
132             clientResponse->sequenceNumber);
133     NS_LOG_V(DEBUG, "GET response resource uri : %s",
134             clientResponse->resourceUri);
135
136     if (!NSIsStartedConsumer())
137     {
138         return OC_STACK_DELETE_TRANSACTION;
139     }
140
141     NS_VERTIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
142
143     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
144     while (payload)
145     {
146         NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
147         payload = payload->next;
148     }
149
150     NS_LOG(DEBUG, "getting information of accepter");
151     bool getResult = OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
152                                             NS_PAYLOAD_KEY_ACCEPTER, & accepter);
153     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
154
155     NS_LOG(DEBUG, "getting message URI");
156     getResult = OCRepPayloadGetPropString(
157             (OCRepPayload *)clientResponse->payload,
158             "MESSAGE_URI", & messageUri);
159     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
160
161     NS_LOG(DEBUG, "getting sync URI");
162     getResult = OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
163                 NS_ATTRIBUTE_SYNC, & syncUri);
164     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL,
165             OC_STACK_KEEP_TRANSACTION, OICFree(messageUri));
166
167     NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
168     NS_VERTIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
169
170     // TODO set id
171     newProvider->mId = NULL;
172     newProvider->messageUri = messageUri;
173     newProvider->syncUri = syncUri;
174
175     newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
176     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider->mUserData,
177                    OC_STACK_KEEP_TRANSACTION, OICFree(newProvider));
178     memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
179
180     if (accepter == NS_ACCEPTER_CONSUMER)
181     {
182         NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
183
184         NSDiscoveredProvider(newProvider);
185     }
186     else
187     {
188         NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
189
190         NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider);
191         NS_VERTIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
192
193         NSConsumerPushEvent(task);
194     }
195
196     return OC_STACK_KEEP_TRANSACTION;
197 }
198
199 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
200 {
201     NS_VERTIFY_NOT_NULL_V(task);
202
203     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
204     if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
205     {
206         NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY,
207                 NULL, NSProviderDiscoverListener, NULL);
208     }
209     else
210     {
211         NS_LOG(ERROR, "Unknown type message");
212     }
213 }
214