Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Registering a IOT Device in C++

Registering a IOT Device in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++javaasp-netcomhosting
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13881496
    wrote on last edited by
    #1

    The C++ API doesn't seem to provide a similar registration interface for device as is provided by the Java. Before I forge ahead and write on myself I want to verify I am not overlooking anything. The Java API that (I believe) accomplishes registration of a device is:

    /** Create a device that is authenticated using ES256. */
    public static void createDeviceWithEs256(String deviceId, String publicKeyFilePath,
    String projectId, String cloudRegion, String registryName)
    throws GeneralSecurityException, IOException {
    GoogleCredential credential =
    GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(
    GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
    .setApplicationName(APP_NAME).build();

    final String registryPath = String.format("projects/%s/locations/%s/registries/%s",
    projectId, cloudRegion, registryName);

    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    final String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("ES256_PEM");

    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);

    System.out.println("Creating device with id: " + deviceId);
    Device device = new Device();
    device.setId(deviceId);
    device.setCredentials(Arrays.asList(devCredential));

    Device createdDevice =
    service
    .projects()
    .locations()
    .registries()
    .devices()
    .create(registryPath, device)
    .execute();

    System.out.println("Created device: " + createdDevice.toPrettyString());
    }

    [^] Any comments much appreciated. Tom

    L 1 Reply Last reply
    0
    • U User 13881496

      The C++ API doesn't seem to provide a similar registration interface for device as is provided by the Java. Before I forge ahead and write on myself I want to verify I am not overlooking anything. The Java API that (I believe) accomplishes registration of a device is:

      /** Create a device that is authenticated using ES256. */
      public static void createDeviceWithEs256(String deviceId, String publicKeyFilePath,
      String projectId, String cloudRegion, String registryName)
      throws GeneralSecurityException, IOException {
      GoogleCredential credential =
      GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
      JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
      HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
      final CloudIot service = new CloudIot.Builder(
      GoogleNetHttpTransport.newTrustedTransport(),jsonFactory, init)
      .setApplicationName(APP_NAME).build();

      final String registryPath = String.format("projects/%s/locations/%s/registries/%s",
      projectId, cloudRegion, registryName);

      PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
      final String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
      publicKeyCredential.setKey(key);
      publicKeyCredential.setFormat("ES256_PEM");

      DeviceCredential devCredential = new DeviceCredential();
      devCredential.setPublicKey(publicKeyCredential);

      System.out.println("Creating device with id: " + deviceId);
      Device device = new Device();
      device.setId(deviceId);
      device.setCredentials(Arrays.asList(devCredential));

      Device createdDevice =
      service
      .projects()
      .locations()
      .registries()
      .devices()
      .create(registryPath, device)
      .execute();

      System.out.println("Created device: " + createdDevice.toPrettyString());
      }

      [^] Any comments much appreciated. Tom

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That looks more like a set of Google API calls, than native Java.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups