Skip to content

Commit 2a99fa1

Browse files
pfalconArpad Tigyi
authored and
Arpad Tigyi
committed
samples: google_iot_device: Remove "command line" and replace with config.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]>
1 parent ea88c71 commit 2a99fa1

File tree

9 files changed

+60
-318
lines changed

9 files changed

+60
-318
lines changed

samples/net/google_iot_device/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ add_compile_definitions(IOTC_DISABLE_CERTVERIFY)
2222

2323
target_sources(app PRIVATE
2424
src/main.c
25-
src/commandline.c
25+
src/config.c
2626
src/example_utils.c
2727
src/bsp/iotc_bsp_tls_mbedtls.c
2828
src/bsp/iotc_bsp_crypto_mbedtls.c

samples/net/google_iot_device/README.rst

+3-9
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,10 @@ in a Zephyr github repo clone in github/atigyi account
9090
- `west update`
9191
- `export ZEPHYR_TOOLCHAIN_VARIANT=zephyr`
9292
- `cd zephyr/samples/net/google_iot_device`
93+
- Copy template file `config.c.in` to `config.c`, and fill in Google Cloud
94+
connection parameters (read comments there).
9395
- `west build -b native_posix -- -G'Unix Makefiles'`
94-
- provide an ECDSA private key in PEM format in the CWD in a file
95-
named `ec_private.pem`. This private key is associated with the
96-
Goolge IoT Device the sample app will impersonate.
97-
- `./build/zephyr/zephyr.exe -testargs -p [GOOGLE_CLOUD_PROJECT_ID] -d
98-
[GOOGLE_CLOUD_DEVICE_PATH] -t [GOOGLE_CLOUD_PUBLIS_TOPIC]`
99-
- example command: `./build/zephyr/zephyr.exe -testargs -p
100-
macr-o-matic-3100t -d
101-
projects/macr-o-matic-3100t/locations/europe-west1/registries/garage-door-meters/devices/sz11-front-reel
102-
-t /devices/sz11-front-reel/state`
96+
- Run the application: `./build/zephyr/zephyr.exe`
10397

10498
See `Google Cloud MQTT Documentation
10599
<https://cloud.google.com/iot/docs/how-tos/mqtt-bridge>`_.

samples/net/google_iot_device/src/commandline.c

-161
This file was deleted.

samples/net/google_iot_device/src/commandline.h

-23
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright 2019 Google LLC
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "config.h"
7+
8+
/* CHANGE THIS */
9+
const char* iotc_project_id = "<GCP IoT Core Project ID>";
10+
/* CHANGE THIS */
11+
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>";
12+
/* CHANGE THIS */
13+
const char* iotc_publish_topic = "/devices/<GCP IoT Core Device ID>/state";
14+
/* CHANGE THIS*/
15+
const char* iotc_private_key_pem =
16+
"-----BEGIN EC PRIVATE KEY-----\n"
17+
"use\n"
18+
"your\n"
19+
"key\n"
20+
"-----END EC PRIVATE KEY-----\n";
21+
22+
const char* iotc_publish_message = "Hello from Zephyr!";
23+
24+
iotc_mqtt_qos_t iotc_example_qos = IOTC_MQTT_QOS_AT_LEAST_ONCE;
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright 2019 Google LLC
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/*
7+
* Declarations for application config options.
8+
*/
9+
10+
#include <iotc_mqtt.h>
11+
12+
extern iotc_mqtt_qos_t iotc_example_qos;
13+
14+
extern const char* iotc_project_id;
15+
extern const char* iotc_device_path;
16+
extern const char* iotc_private_key_pem;
17+
extern const char* iotc_publish_topic;
18+
extern const char* iotc_publish_message;

samples/net/google_iot_device/src/example_utils.c

+1-85
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <iotc_jwt.h>
88
#include <stdio.h>
99

10-
#include "commandline.h"
10+
#include "config.h"
1111
#include "example_utils.h"
1212

1313
#define IOTC_UNUSED(x) (void)(x)
@@ -17,90 +17,6 @@ extern iotc_crypto_key_data_t iotc_connect_private_key_data;
1717
static iotc_timed_task_handle_t delayed_publish_task =
1818
IOTC_INVALID_TIMED_TASK_HANDLE;
1919

20-
int iotc_example_handle_command_line_args(int argc, char* argv[]) {
21-
char options[] = "h:p:d:t:m:f:";
22-
int missingparameter = 0;
23-
int retval = 0;
24-
25-
/* log the executable name and library version */
26-
printf("\n%s\n%s\n", argv[0], iotc_cilent_version_str);
27-
28-
/* Parse the argv array for ONLY the options specified in the options string
29-
*/
30-
retval = iotc_parse(argc, argv, options, sizeof(options));
31-
32-
if (-1 == retval) {
33-
/* iotc_parse has returned an error, and has already logged the error
34-
to the console. Therefore just silently exit here. */
35-
return -1;
36-
}
37-
38-
/* Check to see that the required parameters were all present on the command
39-
* line */
40-
if (NULL == iotc_project_id) {
41-
missingparameter = 1;
42-
printf("-p --project_id is required\n");
43-
}
44-
45-
if (NULL == iotc_device_path) {
46-
missingparameter = 1;
47-
printf("-d --device_path is required\n");
48-
}
49-
50-
if (NULL == iotc_publish_topic) {
51-
missingparameter = 1;
52-
printf("-t --publish_topic is required\n");
53-
}
54-
55-
if (1 == missingparameter) {
56-
/* Error has already been logged, above. Silently exit here */
57-
printf("\n");
58-
return -1;
59-
}
60-
61-
return 0;
62-
}
63-
64-
int load_ec_private_key_pem_from_posix_fs(char* buf_ec_private_key_pem,
65-
size_t buf_len) {
66-
FILE* fp = fopen(iotc_private_key_filename, "rb");
67-
if (fp == NULL) {
68-
printf("ERROR!\n");
69-
printf(
70-
"\tMissing Private Key required for JWT signing.\n"
71-
"\tPlease copy and paste your device's EC private key into\n"
72-
"\ta file with the following path based on this executable's\n"
73-
"\tcurrent working dir:\n\t\t\'%s\'\n\n"
74-
"\tAlternatively use the --help command line parameter to learn\n"
75-
"\thow to set a path to your file using command line arguments\n",
76-
iotc_private_key_filename);
77-
return -1;
78-
}
79-
80-
fseek(fp, 0, SEEK_END);
81-
long file_size = ftell(fp);
82-
rewind(fp);
83-
84-
if ((size_t)file_size > buf_len) {
85-
printf(
86-
"private key file size of %lu bytes is larger that certificate buffer "
87-
"size of %lu bytes\n",
88-
file_size, (long)buf_len);
89-
fclose(fp);
90-
return -1;
91-
}
92-
93-
long bytes_read = fread(buf_ec_private_key_pem, 1, file_size, fp);
94-
fclose(fp);
95-
96-
if (bytes_read != file_size) {
97-
printf("could not fully read private key file\n");
98-
return -1;
99-
}
100-
101-
return 0;
102-
}
103-
10420
void on_connection_state_changed(iotc_context_handle_t in_context_handle,
10521
void* data, iotc_state_t state) {
10622
iotc_connection_data_t* conn_data = (iotc_connection_data_t*)data;

samples/net/google_iot_device/src/example_utils.h

+4-11
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@
77

88
#include <iotc.h>
99

10-
/* Parses and manages commandline arguments. Project id, device path and publish
11-
* topic are require to be passed as command line arguments. Prints
12-
* notification if either of these is not found.
13-
*/
14-
int iotc_example_handle_command_line_args(int argc, char* argv[]);
15-
16-
/* Attempts to load the client's identifying private key from disk so that the
10+
/* Attempts to load the client's identifying private key so that the
1711
byte data may be passed to the 'iotc_connect function'. Please note that the
1812
IoTC API and Board Support Package have various means to use private keys.
1913
This example assumes the use of one that must be provided to a TLS
2014
implementation in a buffer, but secure chips with slot-based key stores can
2115
also be used. Please see the Crypto BSP for more information. */
22-
int load_ec_private_key_pem_from_posix_fs(char* buf_ec_private_key_pem,
23-
size_t buf_len);
16+
int load_ec_private_key_pem(char* buf_ec_private_key_pem, size_t buf_len);
2417

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

64-
/* A function that publishes to the topic that was specified in the command
65-
line parameters of the application. This is invoked directly upon connect
57+
/* A function that publishes to the topic that was specified in the config
58+
parameters of the application. This is invoked directly upon connect
6659
in the 'on_connection_state_changed' function, but also by the IoTC Client's
6760
event system on a 5 second interval. */
6861
void publish_function(iotc_context_handle_t context_handle,

0 commit comments

Comments
 (0)