Skip to content

Commit d5c3052

Browse files
authored
Add pubsub samples to SDK (#82)
Move C++ pub-sub samples from CRT to SDK.
1 parent 7fb2723 commit d5c3052

File tree

6 files changed

+938
-4
lines changed

6 files changed

+938
-4
lines changed

CMakeLists.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cmake_minimum_required(VERSION 3.1)
22

33
option(BUILD_DEPS "Builds aws common runtime dependencies as part of build, only do this if you don't want to control your dependency chain." OFF)
4+
option(BUILD_SAMPLES "Build samples as part of the build" ON)
45

56
if (DEFINED CMAKE_PREFIX_PATH)
67
file(TO_CMAKE_PATH "${CMAKE_PREFIX_PATH}" CMAKE_PREFIX_PATH)
@@ -115,8 +116,11 @@ find_package(aws-crt-cpp REQUIRED)
115116
add_subdirectory(jobs)
116117
add_subdirectory(shadow)
117118
add_subdirectory(discovery)
118-
add_subdirectory(samples/jobs/describe_job_execution)
119-
add_subdirectory(samples/shadow/shadow_sync)
120-
add_subdirectory(samples/greengrass/basic_discovery)
121-
122119

120+
if (BUILD_SAMPLES)
121+
add_subdirectory(samples/mqtt/basic_pub_sub)
122+
add_subdirectory(samples/mqtt/raw_pub_sub)
123+
add_subdirectory(samples/jobs/describe_job_execution)
124+
add_subdirectory(samples/shadow/shadow_sync)
125+
add_subdirectory(samples/greengrass/basic_discovery)
126+
endif()

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,54 @@ cmake --build . --target install
4646
```
4747
# Samples
4848

49+
## Basic MQTT Pub-Sub
50+
51+
This sample uses the
52+
[Message Broker](https://docs.aws.amazon.com/iot/latest/developerguide/iot-message-broker.html)
53+
for AWS IoT to send and receive messages through an MQTT connection.
54+
On startup, the device connects to the server and subscribes to a topic.
55+
56+
The terminal prompts the user for input. Type something and press enter to publish a message to the topic.
57+
Since the sample is subscribed to the same topic, it will also receive the message back from the server.
58+
Type `quit` and press enter to end the sample.
59+
60+
Source: `samples/mqtt/basic_pub_sub`
61+
62+
Your Thing's
63+
[Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html)
64+
must provide privileges for this sample to connect, subscribe, publish,
65+
and receive.
66+
67+
```json
68+
{
69+
"Effect": "Allow",
70+
"Action": [
71+
"iot:Receive",
72+
"iot:Publish"
73+
],
74+
"Resource": [
75+
"arn:aws:iot:<your-region>:<your-id>:topic/a/b"
76+
],
77+
},
78+
{
79+
"Effect": "Allow",
80+
"Action": [
81+
"iot:Subscribe"
82+
],
83+
"Resource": [
84+
"arn:aws:iot:<your-region>:<your-id>:topicfilter/a/b"
85+
]
86+
}
87+
```
88+
89+
## Raw MQTT Pub-Sub
90+
This sample is similar to the Basic Pub-Sub, but the connection setup is more manual.
91+
This is a starting point for using custom
92+
[Configurable Endpoints](https://docs.aws.amazon.com/iot/latest/developerguide/iot-custom-endpoints-configurable.html).
93+
94+
source: `samples/mqtt/raw_pub_sub`
95+
96+
4997
## Shadow
5098

5199
This sample uses the AWS IoT
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
project(basic-pub-sub CXX)
2+
3+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/lib/cmake")
4+
5+
file(GLOB PUB_SUB_SRC
6+
"*.cpp"
7+
)
8+
9+
set(PUB_SUB_PROJECT_NAME basic-pub-sub)
10+
add_executable(${PUB_SUB_PROJECT_NAME} ${PUB_SUB_SRC})
11+
set_target_properties(${PUB_SUB_PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)
12+
13+
set(CMAKE_C_FLAGS_DEBUGOPT "")
14+
15+
#set warnings
16+
if (MSVC)
17+
target_compile_options(${PUB_SUB_PROJECT_NAME} PRIVATE /W4 /WX /wd4068)
18+
else ()
19+
target_compile_options(${PUB_SUB_PROJECT_NAME} PRIVATE -Wall -Wno-long-long -pedantic -Werror)
20+
endif ()
21+
22+
if (CMAKE_BUILD_TYPE STREQUAL "" OR CMAKE_BUILD_TYPE MATCHES Debug)
23+
target_compile_definitions(${PUB_SUB_PROJECT_NAME} PRIVATE "-DDEBUG_BUILD")
24+
endif ()
25+
26+
target_include_directories(${PUB_SUB_PROJECT_NAME} PUBLIC
27+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
28+
$<INSTALL_INTERFACE:include>)
29+
30+
target_link_libraries(${PUB_SUB_PROJECT_NAME} AWS::aws-crt-cpp)

0 commit comments

Comments
 (0)