Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / samples / client / cloud_connector.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 #include "cloud_connector.h"
21
22 #include <stdarg.h>
23 #include <stdlib.h>
24
25 #include "oic_string.h"
26 #include "oic_malloc.h"
27
28 #include "ocpayload.h"
29
30 #include "rdpayload.h"
31
32 #define OC_RD_PUBLISH_TTL 86400
33 #define DEFAULT_CONTEXT_VALUE 0x99
34
35 #define DEFAULT_COAP_TCP_HOST "coap+tcp://"
36 #define DEFAULT_COAP_TCP_PORT 5683
37
38 #define DEFAULT_COAP_TCP_SECURE_HOST "coaps+tcp://"
39 #define DEFAULT_COAP_TCP_SECURE_PORT 5864
40
41 #define DEFAULT_AUTH_REGISTER_LOGIN "/oic/auth/?reqtype=register"
42 #define DEFAULT_AUTH_LOGIN "/oic/auth/?reqtype=login"
43 #define DEFAULT_AUTH_LOGOUT "/oic/auth/?reqtype=logout"
44
45 static OCStackResult createStringLL(uint8_t numElements, OCResourceHandle handle,
46                                     const char *(*getValue)(OCResourceHandle handle, uint8_t i), OCStringLL **stringLL)
47 {
48     for (uint8_t i = 0; i < numElements; ++i)
49     {
50         const char *value = getValue(handle, i);
51         if (!*stringLL)
52         {
53             *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
54             if (!*stringLL)
55             {
56                 return OC_STACK_NO_MEMORY;
57             }
58             (*stringLL)->value = OICStrdup(value);
59             if (!(*stringLL)->value)
60             {
61                 return OC_STACK_NO_MEMORY;
62             }
63         }
64         else
65         {
66             OCStringLL *cur = *stringLL;
67             while (cur->next)
68             {
69                 cur = cur->next;
70             }
71             cur->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
72             if (!cur->next)
73             {
74                 return OC_STACK_NO_MEMORY;
75             }
76             cur->next->value = OICStrdup(value);
77             if (!cur->next->value)
78             {
79                 return OC_STACK_NO_MEMORY;
80             }
81         }
82     }
83     return OC_STACK_OK;
84 }
85
86 OCStackResult OCCloudRegisterLogin(const char *host, const char *auth_provider,
87                                    const char *auth_code, OCClientResponseHandler response)
88 {
89     char    targetUri[MAX_URI_LENGTH * 2] = { 0, };
90     snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, DEFAULT_AUTH_REGISTER_LOGIN);
91
92     OCCallbackData cbData;
93     memset(&cbData, 0, sizeof(OCCallbackData));
94     cbData.cb = response;
95     cbData.cd = NULL;
96     cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
97
98     OCRepPayload *registerPayload = OCRepPayloadCreate();
99     if (!registerPayload)
100     {
101         goto no_memory;
102     }
103
104     OCRepPayloadSetPropString(registerPayload, "authprovider", auth_provider);
105     OCRepPayloadSetPropString(registerPayload, "authcode", auth_code);
106
107     return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)registerPayload,
108                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
109
110 no_memory:
111     OCRepPayloadDestroy(registerPayload);
112     return OC_STACK_NO_MEMORY;
113 }
114
115 OCStackResult OCCloudLoginout(const char *host, const char *query, const char *auth_session,
116                               OCClientResponseHandler response)
117 {
118     char    targetUri[MAX_URI_LENGTH * 2] = { 0, };
119     snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
120
121     OCCallbackData cbData;
122     memset(&cbData, 0, sizeof(OCCallbackData));
123     cbData.cb = response;
124     cbData.cd = NULL;
125     cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
126
127     OCRepPayload *loginoutPayload = OCRepPayloadCreate();
128     if (!loginoutPayload)
129     {
130         goto no_memory;
131     }
132
133     OCRepPayloadSetPropString(loginoutPayload, "session", auth_session);
134
135     return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)loginoutPayload,
136                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
137
138 no_memory:
139     OCRepPayloadDestroy(loginoutPayload);
140     return OC_STACK_NO_MEMORY;
141 }
142
143
144 OCStackResult OCCloudLogin(const char *host, const char *auth_session,
145                            OCClientResponseHandler response)
146 {
147     return OCCloudLoginout(host, DEFAULT_AUTH_LOGIN, auth_session, response);
148 }
149
150 OCStackResult OCCloudLogout(const char *host, const char *auth_session,
151                             OCClientResponseHandler response)
152 {
153     return OCCloudLoginout(host, DEFAULT_AUTH_LOGOUT, auth_session, response);
154 }
155
156 OCStackResult OCCloudPublish(const char *host, const char *query,
157                              OCClientResponseHandler response, int numArg, ...)
158 {
159     char    targetUri[MAX_URI_LENGTH * 2] = { 0, };
160     snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
161
162     // Gather all resources locally and do publish
163     OCCallbackData cbData;
164     memset(&cbData, 0, sizeof(OCCallbackData));
165     cbData.cb = response;
166     cbData.cd = NULL;
167     cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
168
169     OCTagsPayload *tagsPayload = NULL;
170     OCLinksPayload *linksPayload = NULL;
171     OCStringLL *rt = NULL;
172     OCStringLL *itf = NULL;
173     OCStringLL *mt = NULL;
174
175     OCRDPayload *rdPayload = OCRDPayloadCreate();
176     if (!rdPayload)
177     {
178         goto no_memory;
179     }
180
181     const unsigned char *id = (unsigned char *)OCGetServerInstanceIDString();
182     tagsPayload = OCCopyTagsResources(NULL, id,
183                                       NULL, OC_DISCOVERABLE, 0, 0, NULL, NULL, OC_RD_PUBLISH_TTL);
184     if (!tagsPayload)
185     {
186         goto no_memory;
187     }
188
189     va_list arguments;
190     va_start(arguments, numArg);
191
192     for (int j = 0; j < numArg; j++)
193     {
194         OCResourceHandle handle = va_arg(arguments, OCResourceHandle);
195         if (handle)
196         {
197             rt = itf = mt = NULL;
198             const char *uri = OCGetResourceUri(handle);
199             uint8_t numElement;
200             if (OC_STACK_OK == OCGetNumberOfResourceTypes(handle, &numElement))
201             {
202                 OCStackResult res = createStringLL(numElement, handle, OCGetResourceTypeName, &rt);
203                 if (res != OC_STACK_OK || !rt)
204                 {
205                     goto no_memory;
206                 }
207             }
208
209             if (OC_STACK_OK == OCGetNumberOfResourceInterfaces(handle, &numElement))
210             {
211                 OCStackResult res = createStringLL(numElement, handle, OCGetResourceInterfaceName, &itf);
212                 if (res != OC_STACK_OK || !itf)
213                 {
214                     goto no_memory;
215                 }
216             }
217
218             mt = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
219             if (!mt)
220             {
221                 goto no_memory;
222             }
223             mt->value = OICStrdup("application/cbor");
224             if (!mt->value)
225             {
226                 goto no_memory;
227             }
228
229             if (!linksPayload)
230             {
231                 linksPayload = OCCopyLinksResources(uri, rt, itf, NULL, 0, NULL,
232                                                     NULL, j, mt);;
233                 if (!linksPayload)
234                 {
235                     goto no_memory;
236                 }
237             }
238             else
239             {
240                 OCLinksPayload *temp = linksPayload;
241                 while (temp->next)
242                 {
243                     temp = temp->next;
244                 }
245                 temp->next = OCCopyLinksResources(uri, rt, itf, NULL, 0, NULL,
246                                                   NULL, j, mt);
247                 if (!temp->next)
248                 {
249                     goto no_memory;
250                 }
251             }
252             OCFreeOCStringLL(rt);
253             OCFreeOCStringLL(itf);
254             OCFreeOCStringLL(mt);
255         }
256     }
257     va_end(arguments);
258
259     rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
260     if (!rdPayload->rdPublish)
261     {
262         goto no_memory;
263     }
264
265     return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
266                         CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
267
268 no_memory:
269     va_end(arguments);
270     if (rt)
271     {
272         OCFreeOCStringLL(rt);
273     }
274     if (itf)
275     {
276         OCFreeOCStringLL(itf);
277     }
278     if (mt)
279     {
280         OCFreeOCStringLL(mt);
281     }
282     if (tagsPayload)
283     {
284         OCFreeTagsResource(tagsPayload);
285     }
286     if (linksPayload)
287     {
288         OCFreeLinksResource(linksPayload);
289     }
290     OCRDPayloadDestroy(rdPayload);
291     return OC_STACK_NO_MEMORY;
292 }