Skip to content

Commit

Permalink
aws-iot-device-sdk-cpp-v2: add fleet provisoning example
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-roos committed Jul 9, 2024
1 parent 2f527cd commit 2084fe0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# nooelint: oelint.file.underscores
require aws-iot-device-sdk-cpp-v2-samples.inc

S = "${WORKDIR}/git/samples/fleet_provisioning/fleet_provisioning"

SRC_URI:append = " \
file://run-ptest \
"

RDEPENDS:${PN}-ptest += "python3 aws-cli"

do_install() {
install -d ${D}${bindir}
install ${B}/fleet-provisioning ${D}${bindir}
}

inherit ptest pkgconfig

do_install_ptest() {
install -d ${D}${PTEST_PATH}/tests

install -m 0755 ${S}/../../../utils/parse_cert_set_result.py ${D}${PTEST_PATH}/

install -m 0755 ${WORKDIR}/run-ptest ${D}${PTEST_PATH}/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh
set -euxo pipefail
echo "nameserver 8.8.8.8" > /etc/resolv.conf

DATE=$(date "+%F_%H-%M-%S")
ROLE="fprole_$DATE"
TEMPLATE="fptemplate_$DATE"
AWS_ACCOUNT_NUMBER=$(aws sts get-caller-identity --query "Account" --output text)
TEMPLATE_JSON='{"Parameters":{"DeviceLocation":{"Type":"String"},"AWS::IoT::Certificate::Id":{"Type":"String"},"SerialNumber":{"Type":"String"}},"Mappings":{"LocationTable":{"Seattle":{"LocationUrl":"https://example.aws"}}},"Resources":{"thing":{"Type":"AWS::IoT::Thing","Properties":{"ThingName":{"Fn::Join":["",["ThingPrefix_",{"Ref":"SerialNumber"}]]},"AttributePayload":{"version":"v1","serialNumber":"serialNumber"}},"OverrideSettings":{"AttributePayload":"MERGE","ThingTypeName":"REPLACE","ThingGroups":"DO_NOTHING"}},"certificate":{"Type":"AWS::IoT::Certificate","Properties":{"CertificateId":{"Ref":"AWS::IoT::Certificate::Id"},"Status":"Active"},"OverrideSettings":{"Status":"REPLACE"}},"policy":{"Type":"AWS::IoT::Policy","Properties":{"PolicyDocument":{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["iot:Connect","iot:Subscribe","iot:Publish","iot:Receive"],"Resource":"*"}]}}}},"DeviceConfiguration":{"FallbackUrl":"https://www.example.com/test-site","LocationUrl":{"Fn::FindInMap":["LocationTable",{"Ref":"DeviceLocation"},"LocationUrl"]}}}'
IOT_DATA_ENDPOINT=`aws --output text iot describe-endpoint --endpoint-type iot:Data-ATS`

aws iam create-role \
--role-name $ROLE \
--assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Action":"sts:AssumeRole","Effect":"Allow","Principal":{"Service":"iot.amazonaws.com"}}]}'

aws iam attach-role-policy \
--role-name $ROLE \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSIoTThingsRegistration

# wait for role to be populated
sleep 10

aws iot create-provisioning-template \
--template-name $TEMPLATE \
--provisioning-role-arn arn:aws:iam::$AWS_ACCOUNT_NUMBER:role/$ROLE \
--template-body $TEMPLATE_JSON \
--enabled

aws iot create-provisioning-claim \
--template-name $TEMPLATE \
| python3 ./parse_cert_set_result.py \
--path ./tmp \
--filename provision

fleet-provisioning --endpoint $IOT_DATA_ENDPOINT --cert ./tmp/provision.cert.pem --key ./tmp/provision.private.key --template_name $TEMPLATE --template_parameters '{"SerialNumber":"'$DATE'","DeviceLocation":"Seattle"}'
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ FILES_SOLIBSDEV = ""
# enable PACKAGECONFIG = "static" to build static instead of shared libs
PACKAGECONFIG[static] = "-DBUILD_SHARED_LIBS=OFF,-DBUILD_SHARED_LIBS=ON,,"

# this does not work because: https://github.com/aws/aws-iot-device-sdk-cpp-v2/issues/393
# PACKAGECONFIG ??= "\
# ${@bb.utils.contains('PTEST_ENABLED', '1', 'with-tests', '', d)} \
# "
# PACKAGECONFIG[with-tests] = "-DBUILD_TESTING=ON,-DBUILD_TESTING=OFF,"
PACKAGECONFIG ??= "\
${@bb.utils.contains('PTEST_ENABLED', '1', 'with-tests', '', d)} \
"
PACKAGECONFIG[with-tests] = "-DBUILD_TESTING=ON,-DBUILD_TESTING=OFF,"

FILES:${PN}-dev += "${libdir}/*/cmake"

RDEPENDS:${PN}-ptest:prepend = "\
RDEPENDS:${PN}-ptest += "\
aws-iot-device-sdk-cpp-v2-samples-mqtt5-pubsub \
"
"

BBCLASSEXTEND = "native nativesdk"

0 comments on commit 2084fe0

Please sign in to comment.