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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 #include "cathreadpool.h"
28 #include "ocpayload.h"
29 #include "payload_logging.h"
30 #include "ocprovisioningmanager.h"
33 #include "cloudAuth.h"
34 #include "cloudWrapper.h"
36 #include "OCCloudProvisioning.h"
39 #include <unistd.h> //for unlink
42 #define TAG "cloudClient"
45 #define DEFAULT_HOST "10.113.68.85"//"127.0.0.1"
46 #define DEFAULT_PORT OC_MULTICAST_PORT
47 #define DEFAULT_DEVICEID "6A757374-776F-726B-4465-765575696430"
48 #define DEFAULT_USERID "6A757374-776F-726B-4465-765575696430"
49 #define DEFAULT_AUTH_PROVIDER "github"
50 #define DEFAULT_DB_FILE "./cloud.dat"
51 #define DEFAULT_RESPONSE_WAIT_TIME (10 * 1000000) //10s
53 #define GITHUB_AUTH_LINK "https://github.com/login?return_to=%2Flogin%2Foauth%2Fauthorize%3Fclient_id%3Dea9c18f540323b0213d0%26redirect_uri%3Dhttp%253A%252F%252Fwww.example.com%252Foauth_callback%252F"
55 static bool fExit = false;
57 static OCDevAddr endPoint;
58 static char token[1024];
59 static char authProvider[1024];
60 static char *fname = DEFAULT_DB_FILE;
61 static uint64_t timeout;
62 static uint16_t g_credId = 0;
67 OCCloudProvisioning g_cloudProv(ip, 0);
88 ACL_ID_GET_BY_DEVICE = 31,
91 ACL_INDIVIDUAL_GET_INFO = 40,
92 ACL_INDIVIDUAL_UPDATE_ACE = 41,
93 ACL_INDIVIDUAL_DELETE = 42,
95 ACL_GROUP_CREATE = 50,
98 ACL_GROUP_OBSERVE= 53,
99 ACL_GROUP_DELETE = 54,
101 ACL_GROUP_SHARE_DEVICE = 60,
102 ACL_GROUP_DELETE_DEVICE = 61,
103 ACL_GROUP_GET_INFO = 62,
105 ACL_POLICY_CHECK_REQUEST = 70,
107 ACL_GROUP_INVITE_USER = 80,
108 ACL_GROUP_GET_INVITE = 81,
109 ACL_GROUP_DELETE_INVITE = 82,
110 ACL_GROUP_CANCEL_INVITE = 83,
117 printf("************************************************************\n");
118 printf("****************** Cloud Client Requests *******************\n");
119 printf("************************************************************\n");
120 printf("** AUTHORIZATION\n");
121 printf("** %d - Sign Up request\n", SIGN_UP);
122 printf("** %d - Sign In request\n", SIGN_IN);
123 printf("** %d - Sign Out request\n", SIGN_OUT);
125 printf("** SETTINGS \n");
126 printf("** %d - Change default host\n", HOST);
127 printf("** %d - Change default port\n", PORT);
128 printf("** %d - Change default database filename\n", DB_FILE);
129 printf("** %d - Change default auth provider\n", AUTH_PROVIDER);
130 printf("** %d - Change TLS cipher suite to RSA\n", USE_RSA);
131 printf("** %d - Save Trust Cert. Chain into Cred of SVR\n", SAVE_TRUST_CERT);
132 printf("** %d - Change Protocol type (CoAP/CoAPs)\n", USE_SECURE_CONN);
134 printf("** CERTIFICATE REQUEST\n");
135 printf("** %d - Certificate Request\n", CSR_SIGN);
138 printf("** %d - CRL GET Request\n", CRL_GET);
139 printf("** %d - CRL POST Request\n", CRL_POST);
141 printf("** ACL MANAGER\n");
142 printf("** %d - ACL id create Request\n", ACL_ID_CREATE);
143 printf("** %d - ACL id retrieve by device Request\n", ACL_ID_GET_BY_DEVICE);
144 printf("** %d - ACL id delete Request\n", ACL_ID_DELETE);
146 printf("** ACL INDIVIDUAL\n");
147 printf("** %d - ACL individual get info Request\n", ACL_INDIVIDUAL_GET_INFO);
148 printf("** %d - ACL individual update ACE Request\n", ACL_INDIVIDUAL_UPDATE_ACE);
149 printf("** %d - ACL individual delete Request\n", ACL_INDIVIDUAL_DELETE);
151 printf("** ACL GROUP MANAGER\n");
152 printf("** %d - ACL Create Group Request\n", ACL_GROUP_CREATE);
153 printf("** %d - ACL Find Group Request\n", ACL_GROUP_FIND);
154 printf("** %d - ACL Join to invited Group Request\n", ACL_GROUP_JOIN);
155 printf("** %d - ACL Observe Group Request\n", ACL_GROUP_OBSERVE);
156 printf("** %d - ACL Delete Group Request\n", ACL_GROUP_DELETE);
158 printf("** ACL INDIVIDUAL GROUP\n");
159 printf("** %d - ACL Share device into Group Request\n", ACL_GROUP_SHARE_DEVICE);
160 printf("** %d - ACL Delete device from Group Request\n", ACL_GROUP_DELETE_DEVICE);
161 printf("** %d - ACL Get Group Info Request\n", ACL_GROUP_GET_INFO);
163 printf("** ACL POLICY ENFORCEMENT\n");
164 printf("** %d - ACL Check Permissions Request\n", ACL_POLICY_CHECK_REQUEST);
166 printf("** ACL MEMBER INVITATION\n");
167 printf("** %d - ACL Invite user to group Request\n", ACL_GROUP_INVITE_USER);
168 printf("** %d - ACL Retrieve invitation Request\n", ACL_GROUP_GET_INVITE);
169 printf("** %d - ACL Delete invitation Request\n", ACL_GROUP_DELETE_INVITE);
170 printf("** %d - ACL Cancel invitation Request\n", ACL_GROUP_CANCEL_INVITE);
173 printf("** %d - Exit cloud client\n\n", EXIT);
174 printf("************************************************************\n");
176 printf(">> Enter Menu number:\n");
180 * This is default callback to all requests
181 * Used to sync with main menu
183 * @param[in] ctx context
184 * @param[in] result result
185 * @param[in] data data
187 void handleCB(void* ctx, OCStackResult result, void* data)
193 ca_mutex_lock(mutex);
194 ca_cond_signal(cond);
195 ca_mutex_unlock(mutex);
198 void handleCB1(OCStackResult result, void *data)
203 ca_mutex_lock(mutex);
204 ca_cond_signal(cond);
205 ca_mutex_unlock(mutex);
208 void handleCB2(OCStackResult result, std::string data)
213 ca_mutex_lock(mutex);
214 ca_cond_signal(cond);
215 ca_mutex_unlock(mutex);
218 static int saveTrustCert(void)
220 OCStackResult res = OC_STACK_ERROR;
221 OIC_LOG(INFO, TAG, "Save Trust Cert. Chain into Cred of SVR");
223 ByteArray trustCertChainArray = {0, 0};
225 FILE *fp = fopen("rootca.crt", "rb+");
230 if (fseeko(fp, 0, SEEK_END) == 0 && (fsize = ftello(fp)))
232 trustCertChainArray.data = (uint8_t*)OICCalloc(1, fsize+1);
233 trustCertChainArray.len = fsize+1;
234 if (NULL == trustCertChainArray.data)
236 OIC_LOG(ERROR,TAG,"OICCalloc");
241 fsize = fread(trustCertChainArray.data, 1, fsize, fp);
245 OIC_LOG_BUFFER(DEBUG, TAG, trustCertChainArray.data, trustCertChainArray.len);
247 res = OCSaveTrustCertChain(trustCertChainArray.data, trustCertChainArray.len, OIC_ENCODING_PEM,&g_credId);
249 if(OC_STACK_OK != res)
251 OIC_LOG(ERROR, TAG, "OCSaveTrustCertChainBin API error");
254 OIC_LOG_V(INFO, TAG, "CredId of Saved Trust Cert. Chain into Cred of SVR : %d.\n", g_credId);
259 static void userRequests(void *data)
263 memset(token, 0, sizeof(token));
264 memset(authProvider, 0, sizeof(authProvider));
265 strncpy(authProvider, DEFAULT_AUTH_PROVIDER, sizeof(authProvider));
266 memset(&endPoint, 0, sizeof(endPoint));
267 strncpy(endPoint.addr, DEFAULT_HOST, sizeof(endPoint.addr));
268 endPoint.port = DEFAULT_PORT;
270 mutex = ca_mutex_new();
271 cond = ca_cond_new();
273 while (false == fExit)
275 OCStackResult res = OC_STACK_ERROR;
276 timeout = DEFAULT_RESPONSE_WAIT_TIME;
278 printf("-----------------------------------------------------------\n");
279 printf("Connecting to: %s:%d\n", endPoint.addr, endPoint.port);
280 printf("via auth provider: %s\n", authProvider);
281 printf("srv file: %s\n", fname);
282 printf("CoAP prefix: %s\n", DEFAULT_PREFIX);
283 printf("-----------------------------------------------------------\n");
288 scanf("%d", &request);
293 if (0 == strncmp(authProvider, DEFAULT_AUTH_PROVIDER, sizeof(authProvider)))
295 printf("Paste to browser %s and get auth code\n", GITHUB_AUTH_LINK);
297 readString(token, sizeof(token), "auth token", "check link above");
298 res = CloudSignUp(&endPoint, authProvider, token, handleCloudSignUpResponse);
301 res = CloudSignIn(&endPoint, handleCloudSignInResponse);
304 res = CloudSignOut(&endPoint, handleCloudSignOutResponse);
308 readString(endPoint.addr, sizeof(endPoint.addr), "host ip address", DEFAULT_HOST);
309 std::string str(endPoint.addr);
310 g_cloudProv.setIpAddr(str);
316 snprintf(example, sizeof(example), "%d", DEFAULT_PORT);
318 readInteger(&tmp, "port number", example);
320 g_cloudProv.setPort((uint16_t)tmp);
324 res = OCWrapperGetCRL( g_cloudProv, handleCB1);
327 res = OCWrapperPostCRL( g_cloudProv, handleCB1);
329 case ACL_GROUP_CREATE:
330 res = OCWrapperAclCreateGroup(&endPoint, handleCB);
333 res = OCWrapperAclFindMyGroup(&endPoint, handleCB);
335 case ACL_GROUP_DELETE:
336 res = OCWrapperAclDeleteGroup(&endPoint, handleCB);
339 res = OCWrapperAclJoinToInvitedGroup(&endPoint, handleCB);
341 case ACL_GROUP_OBSERVE:
342 res = OCWrapperAclObserveGroup(&endPoint, handleCB);
344 case ACL_GROUP_SHARE_DEVICE:
345 res = OCWrapperAclShareDeviceIntoGroup(&endPoint, handleCB);
347 case ACL_GROUP_DELETE_DEVICE:
348 res = OCWrapperAclDeleteDeviceFromGroup(&endPoint, handleCB);
350 case ACL_GROUP_GET_INFO:
351 res = OCWrapperAclGroupGetInfo(&endPoint, handleCB);
353 case ACL_GROUP_INVITE_USER:
354 res = OCWrapperAclInviteUser(&endPoint, handleCB);
356 case ACL_GROUP_GET_INVITE:
357 res = OCWrapperAclGetInvitation(&endPoint, handleCB);
359 case ACL_GROUP_DELETE_INVITE:
360 res = OCWrapperAclDeleteInvitation(&endPoint, handleCB);
362 case ACL_GROUP_CANCEL_INVITE:
363 res = OCWrapperAclCancelInvitation(&endPoint, handleCB);
365 case ACL_POLICY_CHECK_REQUEST:
366 res = OCWrapperAclPolicyCheck(&endPoint, handleCB);
368 case ACL_ID_GET_BY_DEVICE:
369 res = OCWrapperAclIdGetByDevice( g_cloudProv, handleCB2);
372 res = OCWrapperAclIdCreate(&endPoint, handleCB);
375 res = OCWrapperAclIdDelete(&endPoint, handleCB);
377 case ACL_INDIVIDUAL_GET_INFO:
378 res = OCWrapperAclIndividualGetInfo( g_cloudProv, handleCB1);
380 case ACL_INDIVIDUAL_UPDATE_ACE:
381 res = OCWrapperAclIndividualUpdateAce(&endPoint, handleCB);
383 case ACL_INDIVIDUAL_DELETE:
384 res = OCWrapperAclIndividualDelete(&endPoint, handleCB);
387 res = OCWrapperCertificateIssueRequest( g_cloudProv, handleCB1);
390 // CASelectCipherSuite(0x35, CA_ADAPTER_TCP);
392 case SAVE_TRUST_CERT:
395 case USE_SECURE_CONN:
398 readInteger(&tmp, "CoAP protocol type", "0 - non-secure, 1 - secure");
399 setCoapPrefix(0 == tmp ? false : true);
403 ca_mutex_free(mutex);
408 printf(">> Entered Wrong Menu Number. Please Enter Again\n\n");
412 //if requests were sent then wait response
413 if (res == OC_STACK_OK)
415 ca_mutex_lock(mutex);
416 ca_cond_wait_for(cond, mutex, timeout);
417 ca_mutex_unlock(mutex);
424 FILE* server_fopen(const char *path, const char *mode)
427 return fopen(fname, mode);
431 * Check file accessibility
433 * @param[in] name file path
434 * @return true if check was successful
436 static bool checkConfig(const char *name)
438 FILE* file = fopen(name, "rb");
448 static void printUsage(char *name)
450 printf("Wrong arguments count!\n");
451 printf("Usage : %s <database_filename>\n", name);
452 printf("Examples : 1) %s 2) %s cloud.dat\n", name, name);
455 //==============================================================
456 int main(int argc, char *argv[])
465 if (!checkConfig(fname))
467 OIC_LOG_V(ERROR, TAG, "Can't open database file %s, exit!", fname);
476 //Initialize Persistent Storage for SVR database
477 OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
479 OCRegisterPersistentStorageHandler(&ps);
481 OCMode stackMode = OC_CLIENT_SERVER;
482 if (OCInit(NULL, 0, stackMode) != OC_STACK_OK)
484 OIC_LOG(ERROR, TAG, "OCStack init error, exit\n");
488 ca_thread_pool_t g_threadPoolHandle = NULL;
489 CAResult_t res = ca_thread_pool_init(1, &g_threadPoolHandle);
490 if (CA_STATUS_OK != res)
492 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
496 res = ca_thread_pool_add_task(g_threadPoolHandle, userRequests, NULL);
497 if (CA_STATUS_OK != res)
499 OIC_LOG(ERROR, TAG, "thread pool add task error.");
503 while(false == fExit)
505 if (OCProcess() != OC_STACK_OK)
507 OIC_LOG(ERROR, TAG,"OCProcess process error, exit\n");
512 if (OCStop() != OC_STACK_OK)
514 OIC_LOG(ERROR, TAG, "OCStop process error\n");
518 if (g_threadPoolHandle)
520 ca_thread_pool_free(g_threadPoolHandle);