1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "cloud_connector.h"
25 #include "oic_string.h"
26 #include "oic_malloc.h"
28 #include "ocpayload.h"
30 #include "rdpayload.h"
32 #define OC_RD_PUBLISH_TTL 86400
33 #define DEFAULT_CONTEXT_VALUE 0x99
35 #define DEFAULT_COAP_TCP_HOST "coap+tcp://"
36 #define DEFAULT_COAP_TCP_PORT 5683
38 #define DEFAULT_COAP_TCP_SECURE_HOST "coaps+tcp://"
39 #define DEFAULT_COAP_TCP_SECURE_PORT 5864
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"
45 static OCStackResult createStringLL(uint8_t numElements, OCResourceHandle handle,
46 const char *(*getValue)(OCResourceHandle handle, uint8_t i), OCStringLL **stringLL)
48 for (uint8_t i = 0; i < numElements; ++i)
50 const char *value = getValue(handle, i);
53 *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
56 return OC_STACK_NO_MEMORY;
58 (*stringLL)->value = OICStrdup(value);
59 if (!(*stringLL)->value)
61 return OC_STACK_NO_MEMORY;
66 OCStringLL *cur = *stringLL;
71 cur->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
74 return OC_STACK_NO_MEMORY;
76 cur->next->value = OICStrdup(value);
77 if (!cur->next->value)
79 return OC_STACK_NO_MEMORY;
86 OCStackResult OCCloudRegisterLogin(const char *host, const char *auth_provider,
87 const char *auth_code, OCClientResponseHandler response)
89 char targetUri[MAX_URI_LENGTH * 2] = { 0, };
90 snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, DEFAULT_AUTH_REGISTER_LOGIN);
92 OCCallbackData cbData;
93 memset(&cbData, 0, sizeof(OCCallbackData));
96 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
98 OCRepPayload *registerPayload = OCRepPayloadCreate();
104 OCRepPayloadSetPropString(registerPayload, "authprovider", auth_provider);
105 OCRepPayloadSetPropString(registerPayload, "authcode", auth_code);
107 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)registerPayload,
108 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
111 OCRepPayloadDestroy(registerPayload);
112 return OC_STACK_NO_MEMORY;
115 OCStackResult OCCloudLoginout(const char *host, const char *query, const char *auth_session,
116 OCClientResponseHandler response)
118 char targetUri[MAX_URI_LENGTH * 2] = { 0, };
119 snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
121 OCCallbackData cbData;
122 memset(&cbData, 0, sizeof(OCCallbackData));
123 cbData.cb = response;
125 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
127 OCRepPayload *loginoutPayload = OCRepPayloadCreate();
128 if (!loginoutPayload)
133 OCRepPayloadSetPropString(loginoutPayload, "session", auth_session);
135 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)loginoutPayload,
136 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
139 OCRepPayloadDestroy(loginoutPayload);
140 return OC_STACK_NO_MEMORY;
144 OCStackResult OCCloudLogin(const char *host, const char *auth_session,
145 OCClientResponseHandler response)
147 return OCCloudLoginout(host, DEFAULT_AUTH_LOGIN, auth_session, response);
150 OCStackResult OCCloudLogout(const char *host, const char *auth_session,
151 OCClientResponseHandler response)
153 return OCCloudLoginout(host, DEFAULT_AUTH_LOGOUT, auth_session, response);
156 OCStackResult OCCloudPublish(const char *host, const char *query,
157 OCClientResponseHandler response, int numArg, ...)
159 char targetUri[MAX_URI_LENGTH * 2] = { 0, };
160 snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
162 // Gather all resources locally and do publish
163 OCCallbackData cbData;
164 memset(&cbData, 0, sizeof(OCCallbackData));
165 cbData.cb = response;
167 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
169 OCTagsPayload *tagsPayload = NULL;
170 OCLinksPayload *linksPayload = NULL;
171 OCStringLL *rt = NULL;
172 OCStringLL *itf = NULL;
173 OCStringLL *mt = NULL;
175 OCRDPayload *rdPayload = OCRDPayloadCreate();
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);
190 va_start(arguments, numArg);
192 for (int j = 0; j < numArg; j++)
194 OCResourceHandle handle = va_arg(arguments, OCResourceHandle);
197 rt = itf = mt = NULL;
198 const char *uri = OCGetResourceUri(handle);
200 if (OC_STACK_OK == OCGetNumberOfResourceTypes(handle, &numElement))
202 OCStackResult res = createStringLL(numElement, handle, OCGetResourceTypeName, &rt);
203 if (res != OC_STACK_OK || !rt)
209 if (OC_STACK_OK == OCGetNumberOfResourceInterfaces(handle, &numElement))
211 OCStackResult res = createStringLL(numElement, handle, OCGetResourceInterfaceName, &itf);
212 if (res != OC_STACK_OK || !itf)
218 mt = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
223 mt->value = OICStrdup("application/cbor");
231 linksPayload = OCCopyLinksResources(uri, rt, itf, NULL, 0, NULL,
240 OCLinksPayload *temp = linksPayload;
245 temp->next = OCCopyLinksResources(uri, rt, itf, NULL, 0, NULL,
252 OCFreeOCStringLL(rt);
253 OCFreeOCStringLL(itf);
254 OCFreeOCStringLL(mt);
259 rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload);
260 if (!rdPayload->rdPublish)
265 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
266 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
272 OCFreeOCStringLL(rt);
276 OCFreeOCStringLL(itf);
280 OCFreeOCStringLL(mt);
284 OCFreeTagsResource(tagsPayload);
288 OCFreeLinksResource(linksPayload);
290 OCRDPayloadDestroy(rdPayload);
291 return OC_STACK_NO_MEMORY;