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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// This sample provides the way to create cloud sample
24 #include "ocpayload.h"
25 #include "rd_client.h"
26 #include "cloud_connector.h"
28 static bool isCloudLoggedin = false;
30 OCStackResult OCCloudSignup(const char *host, const char *deviceId,
31 const char *authprovider,
32 const char *authcode, OCClientResponseHandler response)
34 char targetUri[MAX_URI_LENGTH * 2] = { 0, };
35 snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, DEFAULT_AUTH_SIGNUP);
37 OCCallbackData cbData;
38 memset(&cbData, 0, sizeof(OCCallbackData));
41 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
43 printf("Host: %s\n", targetUri);
44 printf("Dev id: %s\n", deviceId);
45 printf("Auth Provider: %s\n", authprovider);
46 printf("Auth Code: %s\n", authcode);
48 OCRepPayload *registerPayload = OCRepPayloadCreate();
50 OCRepPayloadSetPropString(registerPayload, "di", deviceId);
51 OCRepPayloadSetPropString(registerPayload, "authprovider", authprovider);
52 OCRepPayloadSetPropString(registerPayload, "authcode", authcode);
54 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)registerPayload,
55 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
58 OCStackResult OCCloudSession(const char *host, const char *query, const char *uId,
60 const char *accesstoken,
61 bool isLogin, OCClientResponseHandler response)
63 char targetUri[MAX_URI_LENGTH * 2] = { 0, };
64 snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
66 OCCallbackData cbData;
67 memset(&cbData, 0, sizeof(OCCallbackData));
70 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
72 OCRepPayload *loginoutPayload = OCRepPayloadCreate();
76 OCRepPayloadSetPropString(loginoutPayload, "uid", uId);
81 OCRepPayloadSetPropString(loginoutPayload, "di", deviceId);
84 if (accesstoken != NULL)
86 OCRepPayloadSetPropString(loginoutPayload, "accesstoken", accesstoken);
88 OCRepPayloadSetPropBool(loginoutPayload, "login", isLogin);
90 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)loginoutPayload,
91 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
94 //Client should call refresh before expiresin or when receive 4.01 during sign-in
95 OCStackResult OCCloudRefresh(const char *host, const char *query, const char *uId,
96 const char *deviceId, const char *refreshtoken, OCClientResponseHandler response)
98 char targetUri[MAX_URI_LENGTH * 2] = { 0, };
99 snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
101 OCCallbackData cbData;
102 memset(&cbData, 0, sizeof(OCCallbackData));
103 cbData.cb = response;
105 cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
107 OCRepPayload *refreshPayload = OCRepPayloadCreate();
109 OCRepPayloadSetPropString(refreshPayload, "uid", uId);
110 OCRepPayloadSetPropString(refreshPayload, "di", deviceId);
111 OCRepPayloadSetPropString(refreshPayload, "granttype", "refresh_token");
112 OCRepPayloadSetPropString(refreshPayload, "refreshtoken", refreshtoken);
114 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)refreshPayload,
115 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
118 OCStackResult OCCloudLogin(const char *host, const char *uId, const char *deviceId,
119 const char *accesstoken, OCClientResponseHandler response)
121 return OCCloudSession(host, DEFAULT_AUTH_SESSION, uId, deviceId, accesstoken, true, response);
124 OCStackResult OCCloudLogout(const char *host, OCClientResponseHandler response)
126 return OCCloudSession(host, DEFAULT_AUTH_SESSION, NULL, NULL, NULL, false, response);
129 void printRepresentation(OCRepPayloadValue *value)
133 printf("Key: %s / ", value->name);
136 case OCREP_PROP_NULL:
137 printf("Value: None\n");
140 printf("Value: %d\n", value->i);
142 case OCREP_PROP_DOUBLE:
143 printf("Value: %f\n", value->d);
145 case OCREP_PROP_BOOL:
146 printf("Value: %d\n", value->b);
148 case OCREP_PROP_STRING:
149 printf("Value: %s\n", value->str);
151 case OCREP_PROP_BYTE_STRING:
152 printf("Value: Byte String\n");
154 case OCREP_PROP_OBJECT:
155 printf("Value: Object\n");
157 case OCREP_PROP_ARRAY:
158 printf("Value: Array\n");
167 // callback for sign-up and sign-in
168 OCStackApplicationResult CloudSignupCallback(void *ctx,
169 OCDoHandle handle, OCClientResponse *clientResponse)
171 if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
173 printf("Invalid Cloud Login/out callback received\n");
176 printf("Signup response received: %d\n", clientResponse->result);
178 if (clientResponse->payload != NULL &&
179 clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
181 printf("PAYLOAD_TYPE_REPRESENTATION received\n");
182 printf("Sign-in using retrieved accesstoken when disconnected or reboot\n");
184 OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
186 printf("Get payload values\n");
187 printRepresentation(val);
189 return OC_STACK_KEEP_TRANSACTION;
192 OCStackApplicationResult CloudLoginoutCallback(void *ctx,
193 OCDoHandle handle, OCClientResponse *clientResponse)
195 if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
197 printf("Invalid Cloud Login/out callback received\n");
200 printf("Login/out response received: %d\n", clientResponse->result);
202 if (clientResponse->payload != NULL &&
203 clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
205 printf("PAYLOAD_TYPE_REPRESENTATION received\n");
207 OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
209 printf("Get payload values\n");
210 printRepresentation(val);
212 if(clientResponse->result < 5)
214 isCloudLoggedin = true;
218 return OC_STACK_KEEP_TRANSACTION;
221 bool IsCloudLoggedin()
223 return isCloudLoggedin;