obtmain.c 47.3 KB
Newer Older
1
/*
2
// Copyright (c) 2017-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include "oc_api.h"
18
#include "oc_core_res.h"
19 20
#include "oc_obt.h"
#include "port/oc_clock.h"
George Nash's avatar
George Nash committed
21 22 23
#if defined(_WIN32)
#include <windows.h>
#elif defined(__linux__)
24
#include <pthread.h>
George Nash's avatar
George Nash committed
25 26 27
#else
#error "Unsupported OS"
#endif
28 29 30
#include <signal.h>
#include <stdio.h>

31
#define MAX_NUM_DEVICES (50)
32 33
#define MAX_NUM_RESOURCES (100)
#define MAX_NUM_RT (50)
34 35 36 37 38 39

/* Structure in app to track currently discovered owned/unowned devices */
typedef struct device_handle_t
{
  struct device_handle_t *next;
  oc_uuid_t uuid;
40
  char device_name[64];
41 42 43 44 45 46 47 48
} device_handle_t;
/* Pool of device handles */
OC_MEMB(device_handles, device_handle_t, MAX_OWNED_DEVICES);
/* List of known owned devices */
OC_LIST(owned_devices);
/* List of known un-owned devices */
OC_LIST(unowned_devices);

49
#if defined(_WIN32)
George Nash's avatar
George Nash committed
50 51 52 53 54 55 56 57 58 59
static HANDLE event_thread;
static CRITICAL_SECTION app_sync_lock;
static CONDITION_VARIABLE cv;
static CRITICAL_SECTION cs;

/* OS specific definition for lock/unlock */
#define otb_mutex_lock(m) EnterCriticalSection(&m)
#define otb_mutex_unlock(m) LeaveCriticalSection(&m)

#elif defined(__linux__)
60 61 62 63
static pthread_t event_thread;
static pthread_mutex_t app_sync_lock;
static pthread_mutex_t mutex;
static pthread_cond_t cv;
George Nash's avatar
George Nash committed
64 65 66 67 68

/* OS specific definition for lock/unlock */
#define otb_mutex_lock(m) pthread_mutex_lock(&m)
#define otb_mutex_unlock(m) pthread_mutex_unlock(&m)

69
static struct timespec ts;
George Nash's avatar
George Nash committed
70
#endif
71 72 73 74 75
static int quit;

static void
display_menu(void)
{
76
  PRINT("\n\n################################################\nOCF 2.x "
77 78 79 80
        "Onboarding Tool\n################################################\n");
  PRINT("[0] Display this menu\n");
  PRINT("-----------------------------------------------\n");
  PRINT("[1] Discover un-owned devices\n");
81 82 83 84 85
  PRINT("[2] Discover un-owned devices in the realm-local IPv6 scope\n");
  PRINT("[3] Discover un-owned devices in the site-local IPv6 scope\n");
  PRINT("[4] Discover owned devices\n");
  PRINT("[5] Discover owned devices in the realm-local IPv6 scope\n");
  PRINT("[6] Discover owned devices in the site-local IPv6 scope\n");
86
  PRINT("[7] Discover all resources on the device\n");
87
  PRINT("-----------------------------------------------\n");
88 89 90
  PRINT("[8] Just-Works Ownership Transfer Method\n");
  PRINT("[9] Request Random PIN from device for OTM\n");
  PRINT("[10] Random PIN Ownership Transfer Method\n");
91
#ifdef OC_PKI
92
  PRINT("[11] Manufacturer Certificate based Ownership Transfer Method\n");
93
#endif /* OC_PKI */
94
  PRINT("-----------------------------------------------\n");
95 96 97 98 99 100 101
  PRINT("[12] Provision pair-wise credentials\n");
  PRINT("[13] Provision ACE2\n");
  PRINT("[14] Provision auth-crypt RW access to NCRs\n");
  PRINT("[15] RETRIEVE /oic/sec/cred\n");
  PRINT("[16] DELETE cred by credid\n");
  PRINT("[17] RETRIEVE /oic/sec/acl2\n");
  PRINT("[18] DELETE ace by aceid\n");
102 103
  PRINT("[19] RETRIEVE own creds\n");
  PRINT("[20] DELETE own cred by credid\n");
104
#ifdef OC_PKI
105 106 107
  PRINT("[21] Provision role RW access to NCRs\n");
  PRINT("[22] Provision identity certificate\n");
  PRINT("[23] Provision role certificate\n");
108
#endif /* OC_PKI */
109
  PRINT("-----------------------------------------------\n");
110 111 112
#ifdef OC_PKI
  PRINT("[96] Install new manufacturer trust anchor\n");
#endif /* OC_PKI */
113 114 115 116
  PRINT("[97] RESET device\n");
  PRINT("[98] RESET OBT\n");
  PRINT("-----------------------------------------------\n");
  PRINT("[99] Exit\n");
117 118 119 120 121 122
  PRINT("################################################\n");
  PRINT("\nSelect option: \n");
}

#define SCANF(...)                                                             \
  do {                                                                         \
123
    if (scanf(__VA_ARGS__) <= 0) {                                             \
124
      PRINT("ERROR Invalid input\n");                                          \
125
      fflush(stdin);                                                           \
126 127 128 129 130 131 132
    }                                                                          \
  } while (0)

static int
app_init(void)
{
  int ret = oc_init_platform("OCF", NULL, NULL);
133
  ret |= oc_add_device("/oic/d", "oic.d.dots", "OBT", "ocf.2.1.1",
134 135 136
                       "ocf.res.1.0.0,ocf.sh.1.0.0", NULL, NULL);
  oc_device_bind_resource_type(0, "oic.d.ams");
  oc_device_bind_resource_type(0, "oic.d.cms");
137 138 139 140 141 142 143 144 145 146 147 148
  return ret;
}

static void
issue_requests(void)
{
  oc_obt_init();
}

static void
signal_event_loop(void)
{
George Nash's avatar
George Nash committed
149 150 151 152
#if defined(_WIN32)
  WakeConditionVariable(&cv);
#elif defined(__linux__)
  otb_mutex_lock(mutex);
153
  pthread_cond_signal(&cv);
George Nash's avatar
George Nash committed
154 155
  otb_mutex_unlock(mutex);
#endif
156 157 158 159 160 161 162 163 164 165
}

static void
handle_signal(int signal)
{
  (void)signal;
  quit = 1;
  signal_event_loop();
}

George Nash's avatar
George Nash committed
166 167 168 169 170 171
#if defined(_WIN32)
DWORD WINAPI
ocf_event_thread(LPVOID lpParam)
{
  oc_clock_time_t next_event;
  while (quit != 1) {
172 173 174
    otb_mutex_lock(app_sync_lock);
    next_event = oc_main_poll();
    otb_mutex_unlock(app_sync_lock);
George Nash's avatar
George Nash committed
175

176 177 178 179 180 181 182
    if (next_event == 0) {
      SleepConditionVariableCS(&cv, &cs, INFINITE);
    } else {
      oc_clock_time_t now = oc_clock_time();
      if (now < next_event) {
        SleepConditionVariableCS(
          &cv, &cs, (DWORD)((next_event - now) * 1000 / OC_CLOCK_SECOND));
George Nash's avatar
George Nash committed
183
      }
184
    }
George Nash's avatar
George Nash committed
185 186 187
  }

  oc_main_shutdown();
188
  oc_obt_shutdown();
George Nash's avatar
George Nash committed
189 190 191
  return TRUE;
}
#elif defined(__linux__)
192 193 194 195 196 197
static void *
ocf_event_thread(void *data)
{
  (void)data;
  oc_clock_time_t next_event;
  while (quit != 1) {
George Nash's avatar
George Nash committed
198
    otb_mutex_lock(app_sync_lock);
199
    next_event = oc_main_poll();
George Nash's avatar
George Nash committed
200
    otb_mutex_unlock(app_sync_lock);
201

George Nash's avatar
George Nash committed
202
    otb_mutex_lock(mutex);
203 204 205 206 207 208 209
    if (next_event == 0) {
      pthread_cond_wait(&cv, &mutex);
    } else {
      ts.tv_sec = (next_event / OC_CLOCK_SECOND);
      ts.tv_nsec = (next_event % OC_CLOCK_SECOND) * 1.e09 / OC_CLOCK_SECOND;
      pthread_cond_timedwait(&cv, &mutex, &ts);
    }
George Nash's avatar
George Nash committed
210
    otb_mutex_unlock(mutex);
211 212
  }
  oc_main_shutdown();
213
  oc_obt_shutdown();
214 215
  return NULL;
}
George Nash's avatar
George Nash committed
216
#endif
217

218
/* App utility functions */
219
static device_handle_t *
220 221 222 223 224
is_device_in_list(oc_uuid_t *uuid, oc_list_t list)
{
  device_handle_t *device = (device_handle_t *)oc_list_head(list);
  while (device != NULL) {
    if (memcmp(device->uuid.id, uuid->id, 16) == 0) {
225
      return device;
226 227 228
    }
    device = device->next;
  }
229
  return NULL;
230 231 232
}

static bool
233
add_device_to_list(oc_uuid_t *uuid, const char *device_name, oc_list_t list)
234
{
235
  device_handle_t *device = is_device_in_list(uuid, list);
236 237

  if (!device) {
238 239 240 241 242 243
    device = oc_memb_alloc(&device_handles);
    if (!device) {
      return false;
    }
    memcpy(device->uuid.id, uuid->id, 16);
    oc_list_add(list, device);
244 245
  }

246
  if (device_name) {
247 248 249 250
    size_t len = strlen(device_name);
    len = (len > 63) ? 63 : len;
    strncpy(device->device_name, device_name, len);
    device->device_name[len] = '\0';
251 252 253
  } else {
    device->device_name[0] = '\0';
  }
254 255
  return true;
}
256 257 258 259 260 261 262 263 264 265

void
empty_device_list(oc_list_t list)
{
  device_handle_t *device = (device_handle_t *)oc_list_pop(list);
  while (device != NULL) {
    oc_memb_free(&device_handles, device);
    device = (device_handle_t *)oc_list_pop(list);
  }
}
266 267 268
/* End of app utility functions */

/* App invocations of oc_obt APIs */
269 270 271 272
static void
get_device(oc_client_response_t *data)
{
  oc_rep_t *rep = data->payload;
273 274 275 276 277 278 279 280 281
  char *di = NULL, *n = NULL;
  size_t di_len = 0, n_len = 0;

  if (oc_rep_get_string(rep, "di", &di, &di_len)) {
    oc_uuid_t uuid;
    oc_str_to_uuid(di, &uuid);
    if (!oc_rep_get_string(rep, "n", &n, &n_len)) {
      n = NULL;
      n_len = 0;
282
    }
283 284

    add_device_to_list(&uuid, n, data->user_data);
285 286 287
  }
}

288
static void
289
unowned_device_cb(oc_uuid_t *uuid, oc_endpoint_t *eps, void *data)
290 291
{
  (void)data;
292 293
  char di[37];
  oc_uuid_to_str(uuid, di, 37);
294
  oc_endpoint_t *ep = eps;
295 296 297 298 299 300

  PRINT("\nDiscovered unowned device: %s at:\n", di);
  while (eps != NULL) {
    PRINTipaddr(*eps);
    PRINT("\n");
    eps = eps->next;
301
  }
302

303
  oc_do_get("/oic/d", ep, NULL, &get_device, HIGH_QOS, unowned_devices);
304 305 306
}

static void
307
owned_device_cb(oc_uuid_t *uuid, oc_endpoint_t *eps, void *data)
308 309
{
  (void)data;
310 311
  char di[37];
  oc_uuid_to_str(uuid, di, 37);
312
  oc_endpoint_t *ep = eps;
313 314 315 316 317 318

  PRINT("\nDiscovered owned device: %s at:\n", di);
  while (eps != NULL) {
    PRINTipaddr(*eps);
    PRINT("\n");
    eps = eps->next;
319
  }
320

321
  oc_do_get("/oic/d", ep, NULL, &get_device, HIGH_QOS, owned_devices);
322 323 324
}

static void
325
discover_owned_devices(uint8_t scope)
326
{
George Nash's avatar
George Nash committed
327
  otb_mutex_lock(app_sync_lock);
328 329 330 331 332 333 334
  if (scope == 0x02) {
    oc_obt_discover_owned_devices(owned_device_cb, NULL);
  } else if (scope == 0x03) {
    oc_obt_discover_owned_devices_realm_local_ipv6(owned_device_cb, NULL);
  } else if (scope == 0x05) {
    oc_obt_discover_owned_devices_site_local_ipv6(owned_device_cb, NULL);
  }
George Nash's avatar
George Nash committed
335
  otb_mutex_unlock(app_sync_lock);
336
  signal_event_loop();
337 338 339
}

static void
340
discover_unowned_devices(uint8_t scope)
341
{
George Nash's avatar
George Nash committed
342
  otb_mutex_lock(app_sync_lock);
343 344 345 346 347 348 349
  if (scope == 0x02) {
    oc_obt_discover_unowned_devices(unowned_device_cb, NULL);
  } else if (scope == 0x03) {
    oc_obt_discover_unowned_devices_realm_local_ipv6(unowned_device_cb, NULL);
  } else if (scope == 0x05) {
    oc_obt_discover_unowned_devices_site_local_ipv6(unowned_device_cb, NULL);
  }
George Nash's avatar
George Nash committed
350
  otb_mutex_unlock(app_sync_lock);
351 352 353
  signal_event_loop();
}

354 355 356
static void
otm_rdp_cb(oc_uuid_t *uuid, int status, void *data)
{
357 358
  device_handle_t *device = (device_handle_t *)data;
  memcpy(device->uuid.id, uuid->id, 16);
359 360 361 362 363
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

  if (status >= 0) {
    PRINT("\nSuccessfully performed OTM on device %s\n", di);
364
    oc_list_add(owned_devices, device);
365 366
  } else {
    PRINT("\nERROR performing ownership transfer on device %s\n", di);
367
    oc_memb_free(&device_handles, device);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  }
}

static void
otm_rdp(void)
{
  if (oc_list_length(unowned_devices) == 0) {
    PRINT("\nPlease Re-discover Unowned devices\n");
    return;
  }

  device_handle_t *device = (device_handle_t *)oc_list_head(unowned_devices);
  device_handle_t *devices[MAX_NUM_DEVICES];
  int i = 0, c;

  PRINT("\nUnowned Devices:\n");
  while (device != NULL) {
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
387
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    devices[i] = device;
    i++;
    device = device->next;
  }
  PRINT("\n\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  unsigned char pin[24];
  PRINT("\nEnter Random PIN: ");
  SCANF("%10s", pin);

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_perform_random_pin_otm(
405
    &devices[c]->uuid, pin, strlen((const char *)pin), otm_rdp_cb, devices[c]);
406 407
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to perform Random PIN OTM\n");
408 409 410 411
    /* Having issued an OTM request, remove this item from the unowned device
     * list
     */
    oc_list_remove(unowned_devices, devices[c]);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  } else {
    PRINT("\nERROR issuing request to perform Random PIN OTM\n");
  }

  otb_mutex_unlock(app_sync_lock);
}

static void
random_pin_cb(oc_uuid_t *uuid, int status, void *data)
{
  (void)data;
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

  if (status >= 0) {
    PRINT("\nSuccessfully requested device %s to generate a Random PIN\n", di);
  } else {
    PRINT("\nERROR requesting device %s to generate a Random PIN\n", di);
  }
}

static void
request_random_pin(void)
{
  if (oc_list_length(unowned_devices) == 0) {
    PRINT("\nPlease Re-discover Unowned devices\n");
    return;
  }

  device_handle_t *device = (device_handle_t *)oc_list_head(unowned_devices);
  device_handle_t *devices[MAX_NUM_DEVICES];
  int i = 0, c;

  PRINT("\nUnowned Devices:\n");
  while (device != NULL) {
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
449
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    devices[i] = device;
    i++;
    device = device->next;
  }
  PRINT("\n\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  otb_mutex_lock(app_sync_lock);

  int ret = oc_obt_request_random_pin(&devices[c]->uuid, random_pin_cb, NULL);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to generate a random PIN\n");
  } else {
    PRINT("\nERROR issuing request to generate random PIN\n");
  }

  otb_mutex_unlock(app_sync_lock);
}

473
#ifdef OC_PKI
474
static void
475
otm_cert_cb(oc_uuid_t *uuid, int status, void *data)
476
{
477 478
  device_handle_t *device = (device_handle_t *)data;
  memcpy(device->uuid.id, uuid->id, 16);
479 480 481
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

482
  if (status >= 0) {
483
    PRINT("\nSuccessfully performed OTM on device %s\n", di);
484
    oc_list_add(owned_devices, device);
485 486
  } else {
    PRINT("\nERROR performing ownership transfer on device %s\n", di);
487
    oc_memb_free(&device_handles, device);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
  }
}

static void
otm_cert(void)
{
  if (oc_list_length(unowned_devices) == 0) {
    PRINT("\nPlease Re-discover Unowned devices\n");
    return;
  }

  device_handle_t *device = (device_handle_t *)oc_list_head(unowned_devices);
  device_handle_t *devices[MAX_NUM_DEVICES];
  int i = 0, c;

  PRINT("\nUnowned Devices:\n");
  while (device != NULL) {
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    devices[i] = device;
    i++;
    device = device->next;
  }
  PRINT("\n\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  otb_mutex_lock(app_sync_lock);

521
  int ret = oc_obt_perform_cert_otm(&devices[c]->uuid, otm_cert_cb, devices[c]);
522 523
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to perform ownership transfer\n");
524 525 526 527
    /* Having issued an OTM request, remove this item from the unowned device
     * list
     */
    oc_list_remove(unowned_devices, devices[c]);
528 529 530 531 532 533 534 535 536 537 538
  } else {
    PRINT("\nERROR issuing request to perform ownership transfer\n");
  }

  otb_mutex_unlock(app_sync_lock);
}
#endif /* OC_PKI */

static void
otm_just_works_cb(oc_uuid_t *uuid, int status, void *data)
{
539 540
  device_handle_t *device = (device_handle_t *)data;
  memcpy(device->uuid.id, uuid->id, 16);
541 542 543 544 545
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

  if (status >= 0) {
    PRINT("\nSuccessfully performed OTM on device with UUID %s\n", di);
546
    oc_list_add(owned_devices, device);
547
  } else {
548
    oc_memb_free(&device_handles, device);
549
    PRINT("\nERROR performing ownership transfer on device %s\n", di);
550 551 552 553
  }
}

static void
554
otm_just_works(void)
555
{
556 557
  if (oc_list_length(unowned_devices) == 0) {
    PRINT("\nPlease Re-discover Unowned devices\n");
558 559
    return;
  }
560 561 562

  device_handle_t *device = (device_handle_t *)oc_list_head(unowned_devices);
  device_handle_t *devices[MAX_NUM_DEVICES];
563
  int i = 0, c;
564

565 566
  PRINT("\nUnowned Devices:\n");
  while (device != NULL) {
Jaehong Jo's avatar
Jaehong Jo committed
567 568
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
569
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
570
    devices[i] = device;
571 572 573 574 575
    i++;
    device = device->next;
  }
  PRINT("\n\nSelect device: ");
  SCANF("%d", &c);
576
  if (c < 0 || c >= i) {
577 578 579
    PRINT("ERROR: Invalid selection\n");
    return;
  }
580

George Nash's avatar
George Nash committed
581
  otb_mutex_lock(app_sync_lock);
582

583 584
  int ret = oc_obt_perform_just_works_otm(&devices[c]->uuid, otm_just_works_cb,
                                          devices[c]);
585 586
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to perform ownership transfer\n");
587 588 589 590
    /* Having issued an OTM request, remove this item from the unowned device
     * list
     */
    oc_list_remove(unowned_devices, devices[c]);
591 592 593
  } else {
    PRINT("\nERROR issuing request to perform ownership transfer\n");
  }
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  otb_mutex_unlock(app_sync_lock);
}

static void
retrieve_acl2_rsrc_cb(oc_sec_acl_t *acl, void *data)
{
  (void)data;
  if (acl) {
    PRINT("\n/oic/sec/acl2:\n");
    oc_sec_ace_t *ac = oc_list_head(acl->subjects);
    PRINT("\n################################################\n");
    while (ac) {
      PRINT("aceid: %d\n", ac->aceid);
      if (ac->subject_type == OC_SUBJECT_UUID) {
        char uuid[37];
        oc_uuid_to_str(&ac->subject.uuid, uuid, 37);
        PRINT("subject: %s\n", uuid);
      } else if (ac->subject_type == OC_SUBJECT_ROLE) {
        PRINT("Roleid_role: %s\n", oc_string(ac->subject.role.role));
        if (oc_string_len(ac->subject.role.authority) > 0) {
          PRINT("Roleid_authority: %s\n",
                oc_string(ac->subject.role.authority));
        }
      } else if (ac->subject_type == OC_SUBJECT_CONN) {
        PRINT("connection type: ");
        if (ac->subject.conn == OC_CONN_AUTH_CRYPT) {
          PRINT("auth-crypt\n");
        } else {
          PRINT("anon-clear\n");
        }
      }
      PRINT("Permissions: ");
      if (ac->permission & OC_PERM_CREATE) {
        PRINT(" C ");
      }
      if (ac->permission & OC_PERM_RETRIEVE) {
        PRINT(" R ");
      }
      if (ac->permission & OC_PERM_UPDATE) {
        PRINT(" U ");
      }
      if (ac->permission & OC_PERM_DELETE) {
        PRINT(" D ");
      }
      if (ac->permission & OC_PERM_NOTIFY) {
        PRINT(" N ");
      }
      PRINT("\n");
      PRINT("Resources: ");
      oc_ace_res_t *res = oc_list_head(ac->resources);
      while (res) {
        if (oc_string_len(res->href) > 0) {
          PRINT(" %s ", oc_string(res->href));
        } else if (res->wildcard != 0) {
          switch (res->wildcard) {
          case OC_ACE_WC_ALL:
            PRINT(" * ");
            break;
          case OC_ACE_WC_ALL_SECURED:
            PRINT(" + ");
            break;
          case OC_ACE_WC_ALL_PUBLIC:
            PRINT(" - ");
            break;
          default:
            break;
          }
        }
        res = res->next;
      }
      ac = ac->next;
      PRINT("\n-----\n");
    }
    PRINT("\n################################################\n");

    /* Freeing the ACL structure */
    oc_obt_free_acl(acl);
  } else {
    PRINT("\nERROR RETRIEving /oic/sec/acl2\n");
  }
}

static void
retrieve_acl2_rsrc(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }
684

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, c;

  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }
  PRINT("\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_retrieve_acl(&devices[c]->uuid, retrieve_acl2_rsrc_cb, NULL);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to RETRIEVE /oic/sec/acl2\n");
  } else {
    PRINT("\nERROR issuing request to RETRIEVE /oic/sec/acl2\n");
  }
  otb_mutex_unlock(app_sync_lock);
}

static void
716
display_cred_rsrc(oc_sec_creds_t *creds)
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
{
  if (creds) {
    PRINT("\n/oic/sec/cred:\n");
    oc_sec_cred_t *cr = oc_list_head(creds->creds);
    PRINT("\n################################################\n");
    while (cr) {
      char uuid[37];
      oc_uuid_to_str(&cr->subjectuuid, uuid, 37);
      PRINT("credid: %d\n", cr->credid);
      PRINT("subjectuuid: %s\n", uuid);
      PRINT("credtype: %s\n", oc_cred_credtype_string(cr->credtype));
#ifdef OC_PKI
      PRINT("credusage: %s\n", oc_cred_read_credusage(cr->credusage));
      if (oc_string_len(cr->publicdata.data) > 0) {
        PRINT("publicdata_encoding: %s\n",
              oc_cred_read_encoding(cr->publicdata.encoding));
      }
#endif /* OC_PKI */
      PRINT("privatedata_encoding: %s\n",
            oc_cred_read_encoding(cr->privatedata.encoding));
      if (oc_string_len(cr->role.role) > 0) {
        PRINT("roleid_role: %s\n", oc_string(cr->role.role));
      }
      if (oc_string_len(cr->role.authority) > 0) {
        PRINT("roleid_authority: %s\n", oc_string(cr->role.authority));
      }
      PRINT("\n-----\n");
      cr = cr->next;
    }
    PRINT("\n################################################\n");
747 748
  }
}
749

750 751 752 753 754 755
static void
retrieve_cred_rsrc_cb(oc_sec_creds_t *creds, void *data)
{
  (void)data;
  if (creds) {
    display_cred_rsrc(creds);
756 757 758 759 760 761 762
    /* Freeing the creds structure */
    oc_obt_free_creds(creds);
  } else {
    PRINT("\nERROR RETRIEving /oic/sec/cred\n");
  }
}

763 764 765 766 767 768 769 770 771 772 773 774
static void
retrieve_own_creds(void)
{
  otb_mutex_lock(app_sync_lock);
  /* The creds returned by oc_obt_retrieve_own_creds() point to
     internal data structures that store the security context of the OBT.
     DO NOT free them.
  */
  display_cred_rsrc(oc_obt_retrieve_own_creds());
  otb_mutex_unlock(app_sync_lock);
}

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
static void
retrieve_cred_rsrc(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, c;

  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }
  PRINT("\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  otb_mutex_lock(app_sync_lock);
  int ret =
    oc_obt_retrieve_creds(&devices[c]->uuid, retrieve_cred_rsrc_cb, NULL);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to RETRIEVE /oic/sec/cred\n");
  } else {
    PRINT("\nERROR issuing request to RETRIEVE /oic/sec/cred\n");
  }
  otb_mutex_unlock(app_sync_lock);
}

static void
delete_ace_by_aceid_cb(int status, void *data)
{
  (void)data;
  if (status >= 0) {
    PRINT("\nSuccessfully DELETEd ace\n");
  } else {
    PRINT("\nERROR DELETing ace\n");
  }
}

static void
delete_ace_by_aceid(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, c;

  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }
  PRINT("\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  PRINT("\nEnter aceid: ");
  int aceid;
  SCANF("%d", &aceid);

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_delete_ace_by_aceid(&devices[c]->uuid, aceid,
                                       delete_ace_by_aceid_cb, NULL);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to DELETE /oic/sec/acl2\n");
  } else {
    PRINT("\nERROR issuing request to DELETE /oic/sec/acl2\n");
  }
  otb_mutex_unlock(app_sync_lock);
}

static void
delete_cred_by_credid_cb(int status, void *data)
{
  (void)data;
  if (status >= 0) {
    PRINT("\nSuccessfully DELETEd cred\n");
  } else {
    PRINT("\nERROR DELETing cred\n");
  }
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
static void
delete_own_cred_by_credid(void)
{
  PRINT("\nEnter credid: ");
  int credid;
  SCANF("%d", &credid);

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_delete_own_cred_by_credid(credid);
  if (ret >= 0) {
    PRINT("\nSuccessfully DELETED cred\n");
  } else {
    PRINT("\nERROR DELETing cred\n");
  }
  otb_mutex_unlock(app_sync_lock);
}

896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
static void
delete_cred_by_credid(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, c;

  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }
  PRINT("\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  PRINT("\nEnter credid: ");
  int credid;
  SCANF("%d", &credid);

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_delete_cred_by_credid(&devices[c]->uuid, credid,
                                         delete_cred_by_credid_cb, NULL);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to DELETE /oic/sec/cred\n");
  } else {
    PRINT("\nERROR issuing request to DELETE /oic/sec/cred\n");
  }
George Nash's avatar
George Nash committed
936
  otb_mutex_unlock(app_sync_lock);
937 938 939
}

static void
940
reset_device_cb(oc_uuid_t *uuid, int status, void *data)
941 942
{
  (void)data;
943 944 945
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

946
  oc_memb_free(&device_handles, data);
947

948
  if (status >= 0) {
949
    PRINT("\nSuccessfully performed hard RESET to device %s\n", di);
950
  } else {
951
    PRINT("\nERROR performing hard RESET to device %s\n", di);
952 953 954 955 956 957
  }
}

static void
reset_device(void)
{
958
  if (oc_list_length(owned_devices) == 0) {
959 960 961
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }
962 963 964

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
965
  int i = 0, c;
966

967 968 969
  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
Jaehong Jo's avatar
Jaehong Jo committed
970 971
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
972
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
973 974 975 976 977
    i++;
    device = device->next;
  }
  PRINT("\nSelect device: ");
  SCANF("%d", &c);
978
  if (c < 0 || c >= i) {
979 980 981
    PRINT("ERROR: Invalid selection\n");
    return;
  }
982

George Nash's avatar
George Nash committed
983
  otb_mutex_lock(app_sync_lock);
984 985
  int ret =
    oc_obt_device_hard_reset(&devices[c]->uuid, reset_device_cb, devices[c]);
986 987
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to perform hard RESET\n");
988
    oc_list_remove(owned_devices, devices[c]);
989 990 991
  } else {
    PRINT("\nERROR issuing request to perform hard RESET\n");
  }
George Nash's avatar
George Nash committed
992
  otb_mutex_unlock(app_sync_lock);
993 994
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
#ifdef OC_PKI
static void
provision_id_cert_cb(int status, void *data)
{
  (void)data;
  if (status >= 0) {
    PRINT("\nSuccessfully provisioned identity certificate\n");
  } else {
    PRINT("\nERROR provisioning identity certificate\n");
  }
}

static void
provision_id_cert(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, c;

  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }
  PRINT("\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_provision_identity_certificate(&devices[c]->uuid,
                                                  provision_id_cert_cb, NULL);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to provision identity certificate\n");
  } else {
    PRINT("\nERROR issuing request to provision identity certificate\n");
  }
  otb_mutex_unlock(app_sync_lock);
}

static void
provision_role_cert_cb(int status, void *data)
{
  (void)data;
  if (status >= 0) {
    PRINT("\nSuccessfully provisioned role certificate\n");
  } else {
    PRINT("\nERROR provisioning role certificate\n");
  }
}

static void
provision_role_cert(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, c;

  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }
  PRINT("\nSelect device: ");
  SCANF("%d", &c);
  if (c < 0 || c >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  oc_role_t *roles = NULL;
  do {
    char role[64];
    PRINT("\nEnter role: ");
    SCANF("%63s", role);
    PRINT("\nAuthority? [0-No, 1-Yes]: ");
    SCANF("%d", &i);
    if (i == 1) {
      char authority[64];
      PRINT("\nEnter Authority: ");
      SCANF("%63s", authority);
      roles = oc_obt_add_roleid(roles, role, authority);
    } else {
      roles = oc_obt_add_roleid(roles, role, NULL);
    }
    PRINT("\nMore Roles? [0-No, 1-Yes]: ");
    SCANF("%d", &i);
  } while (i == 1);

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_provision_role_certificate(roles, &devices[c]->uuid,
                                              provision_role_cert_cb, NULL);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to provision role certificate\n");
  } else {
    PRINT("\nERROR issuing request to provision role certificate\n");
  }
  otb_mutex_unlock(app_sync_lock);
}

static void
provision_role_wildcard_ace_cb(oc_uuid_t *uuid, int status, void *data)
{
  (void)data;
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

  if (status >= 0) {
    PRINT("\nSuccessfully provisioned rold * ACE to device %s\n", di);
  } else {
    PRINT("\nERROR provisioning ACE to device %s\n", di);
  }
}

static void
provision_role_wildcard_ace(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, dev;

  PRINT("\nProvision role * ACE\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }

  if (i == 0) {
    PRINT("\nNo devices to provision.. Please Re-Discover Owned devices.\n");
    return;
  }

  PRINT("\n\nSelect device for provisioning: ");
  SCANF("%d", &dev);
  if (dev < 0 || dev >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  char role[64], authority[64];
  PRINT("\nEnter role: ");
  SCANF("%63s", role);
  int d;
  PRINT("\nAuthority? [0-No, 1-Yes]: ");
  SCANF("%d", &d);
  if (d == 1) {
    char authority[64];
    PRINT("\nEnter Authority: ");
    SCANF("%63s", authority);
  }

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_provision_role_wildcard_ace(
    &devices[dev]->uuid, role, (d == 1) ? authority : NULL,
    provision_role_wildcard_ace_cb, NULL);
  otb_mutex_unlock(app_sync_lock);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to provision role * ACE\n");
  } else {
    PRINT("\nERROR issuing request to provision role * ACE\n");
  }
}
#endif /* OC_PKI */

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
static void
provision_credentials_cb(int status, void *data)
{
  (void)data;
  if (status >= 0) {
    PRINT("\nSuccessfully provisioned pair-wise credentials\n");
  } else {
    PRINT("\nERROR provisioning pair-wise credentials\n");
  }
}

static void
provision_credentials(void)
{
1202
  if (oc_list_length(owned_devices) == 0) {
1203 1204 1205
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }
1206 1207 1208

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
1209
  int i = 0, c1, c2;
1210

1211 1212 1213
  PRINT("\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
Jaehong Jo's avatar
Jaehong Jo committed
1214 1215
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
1216
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
1217 1218 1219 1220 1221
    i++;
    device = device->next;
  }
  PRINT("\nSelect device 1: ");
  SCANF("%d", &c1);
1222
  if (c1 < 0 || c1 >= i) {
1223 1224 1225 1226 1227
    PRINT("ERROR: Invalid selection\n");
    return;
  }
  PRINT("Select device 2:");
  SCANF("%d", &c2);
1228
  if (c2 < 0 || c2 >= i || c2 == c1) {
1229 1230 1231
    PRINT("ERROR: Invalid selection\n");
    return;
  }
1232

George Nash's avatar
George Nash committed
1233
  otb_mutex_lock(app_sync_lock);
1234
  int ret = oc_obt_provision_pairwise_credentials(
1235
    &devices[c1]->uuid, &devices[c2]->uuid, provision_credentials_cb, NULL);
1236 1237 1238 1239 1240
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to provision credentials\n");
  } else {
    PRINT("\nERROR issuing request to provision credentials\n");
  }
George Nash's avatar
George Nash committed
1241
  otb_mutex_unlock(app_sync_lock);
1242 1243
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
static void
provision_authcrypt_wildcard_ace_cb(oc_uuid_t *uuid, int status, void *data)
{
  (void)data;
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

  if (status >= 0) {
    PRINT("\nSuccessfully provisioned auth-crypt * ACE to device %s\n", di);
  } else {
    PRINT("\nERROR provisioning ACE to device %s\n", di);
  }
}

static void
provision_authcrypt_wildcard_ace(void)
{
  if (oc_list_length(owned_devices) == 0) {
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
  int i = 0, dev;

  PRINT("\nProvision auth-crypt * ACE\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
    i++;
    device = device->next;
  }

  if (i == 0) {
    PRINT("\nNo devices to provision.. Please Re-Discover Owned devices.\n");
    return;
  }

  PRINT("\n\nSelect device for provisioning: ");
  SCANF("%d", &dev);
  if (dev < 0 || dev >= i) {
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  otb_mutex_lock(app_sync_lock);
  int ret = oc_obt_provision_auth_wildcard_ace(
    &devices[dev]->uuid, provision_authcrypt_wildcard_ace_cb, NULL);
  otb_mutex_unlock(app_sync_lock);
  if (ret >= 0) {
    PRINT("\nSuccessfully issued request to provision auth-crypt * ACE\n");
  } else {
    PRINT("\nERROR issuing request to provision auth-crypt * ACE\n");
  }
}

1303
static void
1304
provision_ace2_cb(oc_uuid_t *uuid, int status, void *data)
1305 1306
{
  (void)data;
1307 1308 1309
  char di[37];
  oc_uuid_to_str(uuid, di, 37);

1310
  if (status >= 0) {
1311
    PRINT("\nSuccessfully provisioned ACE to device %s\n", di);
1312
  } else {
1313
    PRINT("\nERROR provisioning ACE to device %s\n", di);
1314 1315 1316 1317 1318 1319
  }
}

static void
provision_ace2(void)
{
1320
  if (oc_list_length(owned_devices) == 0) {
1321 1322 1323 1324 1325 1326 1327
    PRINT("\n\nPlease Re-Discover Owned devices\n");
    return;
  }

  const char *conn_types[2] = { "anon-clear", "auth-crypt" };
  int num_resources = 0;

1328 1329
  device_handle_t *devices[MAX_NUM_DEVICES];
  device_handle_t *device = (device_handle_t *)oc_list_head(owned_devices);
1330
  int i = 0, dev, sub;
1331

1332 1333 1334
  PRINT("\nProvision ACL2\nMy Devices:\n");
  while (device != NULL) {
    devices[i] = device;
Jaehong Jo's avatar
Jaehong Jo committed
1335 1336
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
1337
    PRINT("[%d]: %s - %s\n", i, di, device->device_name);
1338 1339 1340 1341 1342
    i++;
    device = device->next;
  }

  if (i == 0) {
1343
    PRINT("\nNo devices to provision.. Please Re-Discover Owned devices.\n");
1344 1345 1346 1347 1348
    return;
  }

  PRINT("\n\nSelect device for provisioning: ");
  SCANF("%d", &dev);
1349
  if (dev < 0 || dev >= i) {
1350 1351 1352 1353 1354
    PRINT("ERROR: Invalid selection\n");
    return;
  }

  PRINT("\nSubjects:");
1355
  device = (device_handle_t *)oc_list_head(owned_devices);
1356 1357
  PRINT("\n[0]: %s\n", conn_types[0]);
  PRINT("[1]: %s\n", conn_types[1]);
1358
  PRINT("[2]: Role\n");
1359 1360
  i = 0;
  while (device != NULL) {
Jaehong Jo's avatar
Jaehong Jo committed
1361 1362
    char di[OC_UUID_LEN];
    oc_uuid_to_str(&device->uuid, di, OC_UUID_LEN);
1363
    PRINT("[%d]: %s - %s\n", i + 3, di, device->device_name);
1364 1365
    i++;
    device = device->next;
1366 1367 1368 1369 1370 1371

    if (!device) {
      oc_uuid_to_str(oc_core_get_device_id(0), di, OC_UUID_LEN);
      PRINT("[%d]: %s - (OBT)\n", i + 3, di);
      i++;
    }
Kishen Maloor's avatar