Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / service / notification / examples / linux / 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
21 /// This sample provides the way to create cloud sample
22
23 #include "ocstack.h"
24 #include "ocpayload.h"
25 #include "rd_client.h"
26 #include "cloud_connector.h"
27
28 static bool isCloudLoggedin = false;
29
30 OCStackResult OCCloudSignup(const char *host, const char *deviceId,
31         const char *authprovider,
32         const char *authcode, OCClientResponseHandler response)
33 {
34         char    targetUri[MAX_URI_LENGTH * 2] = { 0, };
35         snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, DEFAULT_AUTH_SIGNUP);
36
37         OCCallbackData cbData;
38         memset(&cbData, 0, sizeof(OCCallbackData));
39         cbData.cb = response;
40         cbData.cd = NULL;
41         cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
42
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);
47
48         OCRepPayload *registerPayload = OCRepPayloadCreate();
49
50         OCRepPayloadSetPropString(registerPayload, "di", deviceId);
51         OCRepPayloadSetPropString(registerPayload, "authprovider", authprovider);
52         OCRepPayloadSetPropString(registerPayload, "authcode", authcode);
53
54         return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)registerPayload,
55                 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
56 }
57
58 OCStackResult OCCloudSession(const char *host, const char *query, const char *uId,
59         const char *deviceId,
60         const char *accesstoken,
61         bool isLogin, OCClientResponseHandler response)
62 {
63         char    targetUri[MAX_URI_LENGTH * 2] = { 0, };
64         snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
65
66         OCCallbackData cbData;
67         memset(&cbData, 0, sizeof(OCCallbackData));
68         cbData.cb = response;
69         cbData.cd = NULL;
70         cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
71
72         OCRepPayload *loginoutPayload = OCRepPayloadCreate();
73
74         if (uId != NULL)
75         {
76                 OCRepPayloadSetPropString(loginoutPayload, "uid", uId);
77         }
78
79         if (deviceId != NULL)
80         {
81                 OCRepPayloadSetPropString(loginoutPayload, "di", deviceId);
82         }
83
84         if (accesstoken != NULL)
85         {
86                 OCRepPayloadSetPropString(loginoutPayload, "accesstoken", accesstoken);
87         }
88         OCRepPayloadSetPropBool(loginoutPayload, "login", isLogin);
89
90         return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)loginoutPayload,
91                 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
92 }
93
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)
97 {
98         char    targetUri[MAX_URI_LENGTH * 2] = { 0, };
99         snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query);
100
101         OCCallbackData cbData;
102         memset(&cbData, 0, sizeof(OCCallbackData));
103         cbData.cb = response;
104         cbData.cd = NULL;
105         cbData.context = (void *)DEFAULT_CONTEXT_VALUE;
106
107         OCRepPayload *refreshPayload = OCRepPayloadCreate();
108
109         OCRepPayloadSetPropString(refreshPayload, "uid", uId);
110         OCRepPayloadSetPropString(refreshPayload, "di", deviceId);
111         OCRepPayloadSetPropString(refreshPayload, "granttype", "refresh_token");
112         OCRepPayloadSetPropString(refreshPayload, "refreshtoken", refreshtoken);
113
114         return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)refreshPayload,
115                 CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0);
116 }
117
118 OCStackResult OCCloudLogin(const char *host, const char *uId, const char *deviceId,
119         const char *accesstoken, OCClientResponseHandler response)
120 {
121         return OCCloudSession(host, DEFAULT_AUTH_SESSION, uId, deviceId, accesstoken, true, response);
122 }
123
124 OCStackResult OCCloudLogout(const char *host, OCClientResponseHandler response)
125 {
126         return OCCloudSession(host, DEFAULT_AUTH_SESSION, NULL, NULL, NULL, false, response);
127 }
128
129 void printRepresentation(OCRepPayloadValue *value)
130 {
131         while (value)
132         {
133                 printf("Key: %s / ", value->name);
134                 switch (value->type)
135                 {
136                 case OCREP_PROP_NULL:
137                         printf("Value: None\n");
138                         break;
139                 case OCREP_PROP_INT:
140                         printf("Value: %d\n", value->i);
141                         break;
142                 case OCREP_PROP_DOUBLE:
143                         printf("Value: %f\n", value->d);
144                         break;
145                 case OCREP_PROP_BOOL:
146                         printf("Value: %d\n", value->b);
147                         break;
148                 case OCREP_PROP_STRING:
149                         printf("Value: %s\n", value->str);
150                         break;
151                 case OCREP_PROP_BYTE_STRING:
152                         printf("Value: Byte String\n");
153                         break;
154                 case OCREP_PROP_OBJECT:
155                         printf("Value: Object\n");
156                         break;
157                 case OCREP_PROP_ARRAY:
158                         printf("Value: Array\n");
159                         break;
160                 default:
161                         break;
162                 }
163                 value = value->next;
164         }
165 }
166
167 // callback for sign-up and sign-in
168 OCStackApplicationResult CloudSignupCallback(void *ctx,
169         OCDoHandle handle, OCClientResponse *clientResponse)
170 {
171     if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
172     {
173         printf("Invalid Cloud Login/out callback received\n");
174     }
175
176     printf("Signup response received: %d\n", clientResponse->result);
177
178     if (clientResponse->payload != NULL &&
179             clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
180     {
181         printf("PAYLOAD_TYPE_REPRESENTATION received\n");
182         printf("Sign-in using retrieved accesstoken when disconnected or reboot\n");
183
184         OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
185
186         printf("Get payload values\n");
187         printRepresentation(val);
188     }
189     return OC_STACK_KEEP_TRANSACTION;
190 }
191
192 OCStackApplicationResult CloudLoginoutCallback(void *ctx,
193         OCDoHandle handle, OCClientResponse *clientResponse)
194 {
195     if (ctx != (void *)DEFAULT_CONTEXT_VALUE)
196     {
197         printf("Invalid Cloud Login/out callback received\n");
198     }
199
200     printf("Login/out response received: %d\n", clientResponse->result);
201
202     if (clientResponse->payload != NULL &&
203             clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION)
204     {
205         printf("PAYLOAD_TYPE_REPRESENTATION received\n");
206
207         OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values;
208
209         printf("Get payload values\n");
210         printRepresentation(val);
211
212         if(clientResponse->result < 5)
213         {
214             isCloudLoggedin = true;
215         }
216     }
217
218     return OC_STACK_KEEP_TRANSACTION;
219 }
220
221 bool IsCloudLoggedin()
222 {
223     return isCloudLoggedin;
224 }
225