Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 26 additions & 48 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,36 @@
# See the License for the specific language governing permissions and
# limitations under the License.

all:

# Batch Build Tool for IoTC Client Examples
#
# These Examples are built against a pre-built IoTC Client library.
#
# The pre-built IoTC Client library paths are in IOTC_CLIENT_INC_PATH
# and IOTC_CLIENT_LIB_PATH in rules.mk. Their default values reflect the
# original relative location of examples to IoTC Client. If this
# relative location is altered the varables shall be adjusted either
# directly in rules.mk or in commandline like
# 'make IOTC_CLIENT_INC_PATH=new/path/include IOTC_CLIENT_LIB_PATH=new/path/obj/osx'.
#
# Examples can be batch-built with 'make' in this directory or one-by-one
# 'make' in each example directory.
#
# Targets:
#
# all: builds all examples
# clean: cleans all examples
#
# [EXAMPLE_NAME]: builds only the specified example
# 'make mqtt_logic_example'
#
# Result binaries are under [EXAMPLE_NAME]/bin
#
# If you wish to create your own application based on a IoTC example source and
# makefile structure, then the following steps are suggested:
# - create a new subdirectory under examples: my_new_example
# - copy mqtt_logic_example/Makefile to my_new_example/Makefile and initialize
# variable IOTC_EXAMPLE_NAME with 'my_new_example'
# - put your code into my_new_exapmle/src/my_new_example.c
# - add the new example to the examples list: IOTC_EXAMPLES_ALL += my_new_example
# - execute 'make my_new_example'
#
# It should be 'easy' to alter these makefiles to cross-compile. This requires the
# following modification in rules.mk:
# - available cross-compiled IoTC Client library
# - changing the compiler and linker variables: CC and AR
# - adjusting linked libraries

MD=@
iot_core_mqtt_client: iot_core_mqtt_client/iot_core_mqtt_client
freertos_linux_gcp_iot: freertos_linux/Linux_gcc_gcp_iot/Linux_gcc_gcp_iot
zephyr_native_posix: zephyr_native_posix/build/zephyr/zephyr.exe
.PHONY: iot_core_mqtt_client freertos_linux zephyr_native_posix

IOTC_EXAMPLES_ALL := iot_core_mqtt_client
iot_core_mqtt_client/iot_core_mqtt_client:
$(MAKE) -C .. clean_all
$(MAKE) -C ..
$(MAKE) -C ./iot_core_mqtt_client

all: $(IOTC_EXAMPLES_ALL)
freertos_linux/Linux_gcc_gcp_iot/Linux_gcc_gcp_iot:
$(MAKE) -C .. clean_all
$(MAKE) -C .. PRESET=FREERTOS_POSIX_REL
$(MAKE) -C ./freertos_linux/Linux_gcc_gcp_iot

.PHONY : $(IOTC_EXAMPLES_ALL)
zephyr_native_posix/build/zephyr/zephyr.exe:
$(MAKE) -C .. clean_all
$(MAKE) -C .. PRESET=ZEPHYR
$(MAKE) -C ./zephyr_native_posix/build

$(IOTC_EXAMPLES_ALL) :
$(MD) $(MAKE) -C $(CURDIR)/$@ all
all: iot_core_mqtt_client/iot_core_mqtt_client \
freertos_linux/Linux_gcc_gcp_iot/Linux_gcc_gcp_iot \
zephyr_native_posix/build/zephyr/zephyr.exe

.PHONY : clean
clean:
@for M in $(IOTC_EXAMPLES_ALL); do \
$(MAKE) -C $(CURDIR)/$$M clean; \
done;
$(MAKE) -C ./iot_core_mqtt_client clean
$(MAKE) -C ./freertos_linux/Linux_gcc_gcp_iot clean
$(MAKE) -C ./zephyr_native_posix/build clean
# The above clean command doesn't remove the zephyr.exe binary.
rm -rf ./zephyr_native_posix/build/zephyr/zephyr.exe
66 changes: 0 additions & 66 deletions examples/common/rules.mk

This file was deleted.

156 changes: 73 additions & 83 deletions examples/common/src/commandline.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,41 @@
*/

#include "commandline.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "iotc.h"

#ifndef IOTC_CROSS_TARGET
#include <getopt.h>
#define IOTC_EXAMPLE_DEFAULT_QOS IOTC_MQTT_QOS_AT_LEAST_ONCE
#endif

#include "iotc_mqtt.h"

#ifndef IOTC_CROSS_TARGET
const iotc_mqtt_qos_t kDefaultMqttQos = IOTC_MQTT_QOS_AT_LEAST_ONCE;
#else
#define IOTC_EXAMPLE_DEFAULT_QOS IOTC_MQTT_QOS_AT_MOST_ONCE
const iotc_mqtt_qos_t kDefaultMqttQos = IOTC_MQTT_QOS_AT_MOST_ONCE;
#endif /* IOTC_CROSS_TARGET */

void iotc_usage(const char* options, unsigned options_length);
void iotc_print_usage(const char* binary_name);

iotc_mqtt_qos_t iotc_example_qos = IOTC_EXAMPLE_DEFAULT_QOS;
const char kDefaultPrivateKeyFilename[] = "ec_private.pem";

#define DEFAULT_PRIVATE_KEY_FIILENAME "ec_private.pem"
iotc_core_parameters_t iotc_core_parameters;

const char* iotc_project_id;
const char* iotc_device_path;
const char* iotc_publish_topic;
const char* iotc_publish_message;
const char* iotc_private_key_filename;

int iotc_parse(int argc, char** argv, char* valid_options,
unsigned options_length) {
int iotc_parse_commandline_flags(int argc, char** argv) {
int c;
int iotc_help_flag = 0;
iotc_project_id = NULL;
iotc_device_path = NULL;
iotc_publish_topic = NULL;
iotc_private_key_filename = DEFAULT_PRIVATE_KEY_FIILENAME;
iotc_publish_message = "Hello From Your IoTC client!";
int has_error = 0;
int help_flag_present = 0;
static char valid_options[] = "hp:d:t:m:f:";

iotc_core_parameters.project_id = NULL;
iotc_core_parameters.device_path = NULL;
iotc_core_parameters.publish_topic = NULL;
iotc_core_parameters.private_key_filename = kDefaultPrivateKeyFilename;
iotc_core_parameters.publish_message = "Hello From Your GCP IoT client!";
iotc_core_parameters.example_qos = kDefaultMqttQos;

while (1) {
static struct option long_options[] = {
Expand All @@ -75,98 +77,86 @@ int iotc_parse(int argc, char** argv, char* valid_options,

switch (c) {
case 'p':
iotc_project_id = optarg;
iotc_core_parameters.project_id = optarg;
break;
case 'd':
iotc_device_path = optarg;
iotc_core_parameters.device_path = optarg;
break;
case 't':
iotc_publish_topic = optarg;
iotc_core_parameters.publish_topic = optarg;
break;
case 'm':
iotc_publish_message = optarg;
iotc_core_parameters.publish_message = optarg;
break;
case 'f':
iotc_private_key_filename = optarg;
iotc_core_parameters.private_key_filename = optarg;
break;
case 'h':
default:
iotc_help_flag = 1;
help_flag_present = 1;
break;
}
}

/* Print any unrecognized command line arguments. */
if (optind < argc) {
printf(
"The application could not recognize the following non-option "
"arguments: ");
printf("[ FAIL ] Invalid commandline flags: ");
while (optind < argc) {
printf("%s ", argv[optind++]);
}
putchar('\n');
}
putchar('\n');

if (1 == iotc_help_flag) /* Print the usage statement */
{
iotc_usage(valid_options, options_length);
return (
-1); /* Don't run the application if -h --help was on the commandline */
/* Print the usage statement */
if (1 == help_flag_present) {
iotc_print_usage(argv[0]);
return -1;
}

return (0);
}
/* Check that all required parameters were present. */
if (NULL == iotc_core_parameters.project_id) {
has_error = 1;
printf("[ FAIL ] -p --project_id is required\n");
}

void iotc_usage(const char* options, unsigned options_length) {
assert(NULL != options);
if (NULL == iotc_core_parameters.device_path) {
printf("[ FAIL ] -d --device_path is required\n");
}

/* For debugging printf( "options = %s %d\n", options, options_length ); */
if (NULL == iotc_core_parameters.publish_topic) {
has_error = 1;
printf("[ FAIL ] -t --publish_topic is required\n");
}

printf("Usage:\n");
while (0 < options_length) {
/* printf( "parsing option %c\n", *options ); */
switch (*options) {
case 'p':
printf(
"-p --project_id\n\tProvide the project_id your device is "
"registered in "
"Cloud IoT Core.\n");
break;
case 'd':
printf(
"-d --device_path\n\tProvide the full path of your device. For "
"example:\n"
"\t\tprojects/<project_id>/locations/<cloud_region>/registries/"
"<registry_id>/devices/<device_id>\n");
break;
case 't':
printf("-t --publish_topic\n\tThe topic on which to subscribe.\n");
break;
case 'm':
printf(
"-m --publish_message\n\tThe message to publish. A shell quoted "
"string of characters.\n");
break;
case 'f':
printf(
"-f --private_key_filename\n\tThe filename, including path from "
"cwd,\n");
printf(" \t of the device identifying private_key. Defaults to: %s\n",
DEFAULT_PRIVATE_KEY_FIILENAME);
break;
case 'h': /* Don't print anything for the help option since we're printing
usage */
break;
case ':': /* We'll skip the ':' character since it's not an option. */
break;
case '\0':
break;
default:
printf("WARNING: Option %c not recognized by usage()\n", *options);
}
++options;
--options_length;
if (1 == has_error) {
return -1;
}

return 0;
}

void iotc_print_usage(const char* binary_name) {
printf("Usage: %s <options> \n\n", binary_name);
printf("Supported options are:\n");
printf(
"\t -p --project_id (required)\n"
"\t\tThe project_id of your device, registered in Google Cloud IoT "
"Core.\n");
printf(
"\t-d --device_path (required)\n"
"\t\tThe path to your device. Example:\n"
"\t\tprojects/<project_id>/locations/<cloud_region>/registries/"
"<registry_id>/devices/<device_id>\n");
printf(
"\t-t --publish_topic (required)\n"
"\t\tThe topic to subscribe to.\n");
printf(
"\t-m --publish_message (required)\n"
"\t\tThe message to publish. A shell quoted list of characters.\n");
printf(
"\t-f --private_key_filename (optional) default: %s\n"
"\t\tRelative path to the private key file.\n",
kDefaultPrivateKeyFilename);
printf("\n");
}
Loading