|
| 1 | +/* Copyright 2019 Google LLC |
| 2 | + * |
| 3 | + * This is part of the Google Cloud IoT Edge Embedded C Client, |
| 4 | + * it is licensed under the BSD 3-Clause license; you may not use this file |
| 5 | + * except in compliance with the License. |
| 6 | + * |
| 7 | + * You may obtain a copy of the License at: |
| 8 | + * https://opensource.org/licenses/BSD-3-Clause |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <cmdline.h> |
| 18 | +#include <string.h> |
| 19 | +#include <zephyr.h> |
| 20 | + |
| 21 | +#include <commandline.h> |
| 22 | +#include <example_utils.h> |
| 23 | +#include <iotc.h> |
| 24 | + |
| 25 | +char ec_private_key_pem[PRIVATE_KEY_BUFFER_SIZE] = {0}; |
| 26 | + |
| 27 | +void main(void) { |
| 28 | + printk("Example for Zephyr port.\n"); |
| 29 | + |
| 30 | + /* commandline sample: |
| 31 | + zephyr.exe -testargs -p <GCP IoT Core Project ID> -d projects/<GCP IoT Core |
| 32 | + Project ID>/locations/<Region>/registries/<GCP IoT Core Registry |
| 33 | + ID>/devices/<GCP IoT Core Device ID> -t /devices/<GCP IoT Core |
| 34 | + DeviceID>/state |
| 35 | + */ |
| 36 | + |
| 37 | + int argc = 0; |
| 38 | + char** argv = NULL; |
| 39 | + |
| 40 | + native_get_cmd_line_args(&argc, &argv); |
| 41 | + |
| 42 | + /* Zephyr passes the "-testargs" internal command line argument too. This code |
| 43 | + * skips it to be compatible to the native command line argument handlin. */ |
| 44 | + if (1 < argc && 0 == strcmp(argv[1], "-testargs")) { |
| 45 | + --argc; |
| 46 | + ++argv; |
| 47 | + } |
| 48 | + |
| 49 | + /* parsing GCP IoT related command line arguments */ |
| 50 | + if (0 != iotc_example_handle_command_line_args(argc, argv)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (0 != load_ec_private_key_pem_from_posix_fs(ec_private_key_pem, |
| 55 | + PRIVATE_KEY_BUFFER_SIZE)) { |
| 56 | + printk("\nApplication exiting due to private key load error.\n\n"); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + printk("Starting GCP IoT Embedded C Client...\n"); |
| 61 | + |
| 62 | + iotc_initialize(); |
| 63 | + |
| 64 | + iotc_context_handle_t context_handle = iotc_create_context(); |
| 65 | + |
| 66 | + const uint16_t connection_timeout = 10; |
| 67 | + const uint16_t keepalive_timeout = 10; |
| 68 | + |
| 69 | + iotc_crypto_private_key_data_t key_data; |
| 70 | + key_data.private_key_signature_algorithm = |
| 71 | + IOTC_JWT_PRIVATE_KEY_SIGNATURE_ALGORITHM_ES256; |
| 72 | + key_data.private_key_union_type = IOTC_CRYPTO_KEY_UNION_TYPE_PEM; |
| 73 | + key_data.private_key_union.key_pem.key = ec_private_key_pem; |
| 74 | + |
| 75 | + iotc_connect(context_handle, iotc_project_id, iotc_device_path, &key_data, |
| 76 | + /*{jwt_expiration_period_sec=*/3600, connection_timeout, |
| 77 | + keepalive_timeout, on_connection_state_changed); |
| 78 | + |
| 79 | + iotc_events_process_blocking(); |
| 80 | + |
| 81 | + /* Cleanup the default context, releasing its memory */ |
| 82 | + iotc_delete_context(context_handle); |
| 83 | + |
| 84 | + /* Cleanup internal allocations that were created by iotc_initialize. */ |
| 85 | + iotc_shutdown(); |
| 86 | +} |
0 commit comments