Skip to content

Commit

Permalink
samples: google_iot_device: Remove "command line" and replace with co…
Browse files Browse the repository at this point in the history
…nfig.c (zephyrproject-rtos#8)

* samples: google_iot_device: Remove "command line" and replace with config.c

There is no "command line" in Zephyr RTOS. "Command line" which was
used previously is an artifact of a special-purpose, testing target
"native_posix", not portable to any other Zephyr target.

So, instead provide config.c file, where a user can define all needed
application parameters (which previously were passed on "command
line"). As such a file contain credentials, it should be never put
under version control. This commit includes a template file,
config.c.in, which can be copied by a user to config.c, and required
values filled in.

Signed-off-by: Paul Sokolovsky <[email protected]>

* samples: google_iot_device: README: Update for moving from cmdline to config.c

Signed-off-by: Paul Sokolovsky <[email protected]>
  • Loading branch information
pfalcon authored and Arpad Tigyi committed Sep 5, 2019
1 parent ea88c71 commit 2a99fa1
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 318 deletions.
2 changes: 1 addition & 1 deletion samples/net/google_iot_device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ add_compile_definitions(IOTC_DISABLE_CERTVERIFY)

target_sources(app PRIVATE
src/main.c
src/commandline.c
src/config.c
src/example_utils.c
src/bsp/iotc_bsp_tls_mbedtls.c
src/bsp/iotc_bsp_crypto_mbedtls.c
Expand Down
12 changes: 3 additions & 9 deletions samples/net/google_iot_device/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ in a Zephyr github repo clone in github/atigyi account
- `west update`
- `export ZEPHYR_TOOLCHAIN_VARIANT=zephyr`
- `cd zephyr/samples/net/google_iot_device`
- Copy template file `config.c.in` to `config.c`, and fill in Google Cloud
connection parameters (read comments there).
- `west build -b native_posix -- -G'Unix Makefiles'`
- provide an ECDSA private key in PEM format in the CWD in a file
named `ec_private.pem`. This private key is associated with the
Goolge IoT Device the sample app will impersonate.
- `./build/zephyr/zephyr.exe -testargs -p [GOOGLE_CLOUD_PROJECT_ID] -d
[GOOGLE_CLOUD_DEVICE_PATH] -t [GOOGLE_CLOUD_PUBLIS_TOPIC]`
- example command: `./build/zephyr/zephyr.exe -testargs -p
macr-o-matic-3100t -d
projects/macr-o-matic-3100t/locations/europe-west1/registries/garage-door-meters/devices/sz11-front-reel
-t /devices/sz11-front-reel/state`
- Run the application: `./build/zephyr/zephyr.exe`

See `Google Cloud MQTT Documentation
<https://cloud.google.com/iot/docs/how-tos/mqtt-bridge>`_.
Expand Down
161 changes: 0 additions & 161 deletions samples/net/google_iot_device/src/commandline.c

This file was deleted.

23 changes: 0 additions & 23 deletions samples/net/google_iot_device/src/commandline.h

This file was deleted.

24 changes: 24 additions & 0 deletions samples/net/google_iot_device/src/config.c.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright 2019 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "config.h"

/* CHANGE THIS */
const char* iotc_project_id = "<GCP IoT Core Project ID>";
/* CHANGE THIS */
const char* iotc_device_path = "projects/<GCP IoT Core Project ID>/locations/<Region>/registries/<GCP IoT Core Registry ID>/devices/<GCP IoT Core Device ID>";
/* CHANGE THIS */
const char* iotc_publish_topic = "/devices/<GCP IoT Core Device ID>/state";
/* CHANGE THIS*/
const char* iotc_private_key_pem =
"-----BEGIN EC PRIVATE KEY-----\n"
"use\n"
"your\n"
"key\n"
"-----END EC PRIVATE KEY-----\n";

const char* iotc_publish_message = "Hello from Zephyr!";

iotc_mqtt_qos_t iotc_example_qos = IOTC_MQTT_QOS_AT_LEAST_ONCE;
18 changes: 18 additions & 0 deletions samples/net/google_iot_device/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright 2019 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/

/*
* Declarations for application config options.
*/

#include <iotc_mqtt.h>

extern iotc_mqtt_qos_t iotc_example_qos;

extern const char* iotc_project_id;
extern const char* iotc_device_path;
extern const char* iotc_private_key_pem;
extern const char* iotc_publish_topic;
extern const char* iotc_publish_message;
86 changes: 1 addition & 85 deletions samples/net/google_iot_device/src/example_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <iotc_jwt.h>
#include <stdio.h>

#include "commandline.h"
#include "config.h"
#include "example_utils.h"

#define IOTC_UNUSED(x) (void)(x)
Expand All @@ -17,90 +17,6 @@ extern iotc_crypto_key_data_t iotc_connect_private_key_data;
static iotc_timed_task_handle_t delayed_publish_task =
IOTC_INVALID_TIMED_TASK_HANDLE;

int iotc_example_handle_command_line_args(int argc, char* argv[]) {
char options[] = "h:p:d:t:m:f:";
int missingparameter = 0;
int retval = 0;

/* log the executable name and library version */
printf("\n%s\n%s\n", argv[0], iotc_cilent_version_str);

/* Parse the argv array for ONLY the options specified in the options string
*/
retval = iotc_parse(argc, argv, options, sizeof(options));

if (-1 == retval) {
/* iotc_parse has returned an error, and has already logged the error
to the console. Therefore just silently exit here. */
return -1;
}

/* Check to see that the required parameters were all present on the command
* line */
if (NULL == iotc_project_id) {
missingparameter = 1;
printf("-p --project_id is required\n");
}

if (NULL == iotc_device_path) {
missingparameter = 1;
printf("-d --device_path is required\n");
}

if (NULL == iotc_publish_topic) {
missingparameter = 1;
printf("-t --publish_topic is required\n");
}

if (1 == missingparameter) {
/* Error has already been logged, above. Silently exit here */
printf("\n");
return -1;
}

return 0;
}

int load_ec_private_key_pem_from_posix_fs(char* buf_ec_private_key_pem,
size_t buf_len) {
FILE* fp = fopen(iotc_private_key_filename, "rb");
if (fp == NULL) {
printf("ERROR!\n");
printf(
"\tMissing Private Key required for JWT signing.\n"
"\tPlease copy and paste your device's EC private key into\n"
"\ta file with the following path based on this executable's\n"
"\tcurrent working dir:\n\t\t\'%s\'\n\n"
"\tAlternatively use the --help command line parameter to learn\n"
"\thow to set a path to your file using command line arguments\n",
iotc_private_key_filename);
return -1;
}

fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
rewind(fp);

if ((size_t)file_size > buf_len) {
printf(
"private key file size of %lu bytes is larger that certificate buffer "
"size of %lu bytes\n",
file_size, (long)buf_len);
fclose(fp);
return -1;
}

long bytes_read = fread(buf_ec_private_key_pem, 1, file_size, fp);
fclose(fp);

if (bytes_read != file_size) {
printf("could not fully read private key file\n");
return -1;
}

return 0;
}

void on_connection_state_changed(iotc_context_handle_t in_context_handle,
void* data, iotc_state_t state) {
iotc_connection_data_t* conn_data = (iotc_connection_data_t*)data;
Expand Down
15 changes: 4 additions & 11 deletions samples/net/google_iot_device/src/example_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@

#include <iotc.h>

/* Parses and manages commandline arguments. Project id, device path and publish
* topic are require to be passed as command line arguments. Prints
* notification if either of these is not found.
*/
int iotc_example_handle_command_line_args(int argc, char* argv[]);

/* Attempts to load the client's identifying private key from disk so that the
/* Attempts to load the client's identifying private key so that the
byte data may be passed to the 'iotc_connect function'. Please note that the
IoTC API and Board Support Package have various means to use private keys.
This example assumes the use of one that must be provided to a TLS
implementation in a buffer, but secure chips with slot-based key stores can
also be used. Please see the Crypto BSP for more information. */
int load_ec_private_key_pem_from_posix_fs(char* buf_ec_private_key_pem,
size_t buf_len);
int load_ec_private_key_pem(char* buf_ec_private_key_pem, size_t buf_len);

/* A callback function that will be invoked whenever the connection state
has changed.
Expand Down Expand Up @@ -61,8 +54,8 @@ int load_ec_private_key_pem_from_posix_fs(char* buf_ec_private_key_pem,
void on_connection_state_changed(iotc_context_handle_t in_context_handle,
void* data, iotc_state_t state);

/* A function that publishes to the topic that was specified in the command
line parameters of the application. This is invoked directly upon connect
/* A function that publishes to the topic that was specified in the config
parameters of the application. This is invoked directly upon connect
in the 'on_connection_state_changed' function, but also by the IoTC Client's
event system on a 5 second interval. */
void publish_function(iotc_context_handle_t context_handle,
Expand Down
Loading

0 comments on commit 2a99fa1

Please sign in to comment.