Skip to content

Commit 0c29940

Browse files
Merge pull request aws#1 from awslabs/mqtt_bindings
Initial work on mqtt bindings for cpp, a sample app, and a simple tes…
2 parents e135f60 + 977d107 commit 0c29940

23 files changed

+2249
-0
lines changed

CMakeLists.txt

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
project(aws-crt-cpp CXX)
3+
4+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/lib/cmake")
5+
6+
if (NOT CMAKE_CXX_STANDARD)
7+
set(CMAKE_CXX_STANDARD 11)
8+
endif()
9+
10+
file(GLOB AWS_CRT_HEADERS
11+
"include/aws/crt/*.h"
12+
)
13+
14+
file(GLOB AWS_CRT_IO_HEADERS
15+
"include/aws/crt/io/*.h"
16+
)
17+
18+
file(GLOB AWS_CRT_MQTT_HEADERS
19+
"include/aws/crt/mqtt/*.h"
20+
)
21+
22+
file(GLOB AWS_CRT_CPP_HEADERS
23+
${AWS_CRT_HEADERS}
24+
${AWS_CRT_IO_HEADERS}
25+
${AWS_CRT_MQTT_HEADERS}
26+
)
27+
28+
file(GLOB AWS_CRT_SRC
29+
"source/*.cpp"
30+
)
31+
32+
file (GLOB AWS_CRT_IO_SRC
33+
"source/io/*.cpp"
34+
)
35+
36+
file (GLOB AWS_CRT_MQTT_SRC
37+
"source/mqtt/*.cpp"
38+
)
39+
40+
file(GLOB AWS_CRT_CPP_SRC
41+
${AWS_CRT_SRC}
42+
${AWS_CRT_IO_SRC}
43+
${AWS_CRT_MQTT_SRC}
44+
)
45+
46+
if (WIN32)
47+
if (MSVC)
48+
source_group("Header Files\\aws\\crt" FILES ${AWS_CRT_HEADERS})
49+
source_group("Header Files\\aws\\crt\\io" FILES ${AWS_CRT_IO_HEADERS})
50+
source_group("Header Files\\aws\\crt\\mqtt" FILES ${AWS_CRT_MQTT_HEADERS})
51+
source_group("Source Files" FILES ${AWS_CRT_SRC})
52+
source_group("Source Files\\io" FILES ${AWS_CRT_IO_SRC})
53+
source_group("Source Files\\mqtt" FILES ${AWS_CRT_MQTT_SRC})
54+
endif ()
55+
endif()
56+
57+
add_library(${CMAKE_PROJECT_NAME} ${AWS_CRT_CPP_SRC})
58+
59+
if (BUILD_SHARED_LIBS AND WIN32)
60+
target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC -DUSE_IMPORT_EXPORT)
61+
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE -DAWS_CRT_CPP_EXPORTS)
62+
endif()
63+
64+
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)
65+
66+
set(CMAKE_C_FLAGS_DEBUGOPT "")
67+
68+
#set warnings
69+
if (MSVC)
70+
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE /W4 /WX)
71+
else ()
72+
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -Wall -Wno-long-long -pedantic -Werror)
73+
endif ()
74+
75+
if (CMAKE_BUILD_TYPE STREQUAL "" OR CMAKE_BUILD_TYPE MATCHES Debug)
76+
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE "-DDEBUG_BUILD")
77+
endif ()
78+
79+
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
80+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
81+
$<INSTALL_INTERFACE:include>)
82+
83+
find_package(aws-c-mqtt REQUIRED)
84+
target_link_libraries(${CMAKE_PROJECT_NAME} AWS::aws-c-mqtt)
85+
86+
install(FILES ${AWS_CRT_HEADERS} DESTINATION "include/aws/crt")
87+
install(FILES ${AWS_CRT_IO_HEADERS} DESTINATION "include/aws/crt/io")
88+
install(FILES ${AWS_CRT_MQTT_HEADERS} DESTINATION "include/aws/crt/mqtt")
89+
90+
install(
91+
TARGETS ${CMAKE_PROJECT_NAME}
92+
EXPORT ${CMAKE_PROJECT_NAME}-targets
93+
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
94+
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
95+
)
96+
97+
install(EXPORT "${CMAKE_PROJECT_NAME}-targets"
98+
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_PROJECT_NAME}/cmake/"
99+
NAMESPACE AWS::)
100+
101+
configure_file("cmake/${CMAKE_PROJECT_NAME}-config.cmake"
102+
"${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config.cmake"
103+
@ONLY)
104+
105+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config.cmake"
106+
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_PROJECT_NAME}/cmake/")
107+
108+
enable_testing()
109+
add_subdirectory(tests)
110+
add_subdirectory(samples/mqtt_pub_sub)

cmake/aws-crt-cpp-config.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include(CMakeFindDependencyMacro)
2+
3+
find_dependency(aws-c-mqtt)
4+
5+
include(${CMAKE_CURRENT_LIST_DIR}/@CMAKE_PROJECT_NAME@-targets.cmake)

include/aws/crt/Api.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
/*
3+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License").
6+
* You may not use this file except in compliance with the License.
7+
* A copy of the License is located at
8+
*
9+
* http://aws.amazon.com/apache2.0
10+
*
11+
* or in the "license" file accompanying this file. This file is distributed
12+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
* express or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
#include <aws/crt/Types.h>
17+
#include <aws/crt/io/Bootstrap.h>
18+
#include <aws/crt/io/EventLoopGroup.h>
19+
#include <aws/crt/io/TlsOptions.h>
20+
#include <aws/crt/mqtt/MqttClient.h>
21+
22+
namespace Aws
23+
{
24+
namespace Crt
25+
{
26+
class AWS_CRT_CPP_API ApiHandle
27+
{
28+
public:
29+
ApiHandle(Allocator* allocator) noexcept;
30+
ApiHandle() noexcept;
31+
~ApiHandle();
32+
ApiHandle(const ApiHandle&) = delete;
33+
ApiHandle(ApiHandle&&) = delete;
34+
ApiHandle& operator =(const ApiHandle&) = delete;
35+
ApiHandle& operator =(ApiHandle&&) = delete;
36+
};
37+
38+
AWS_CRT_CPP_API void LoadErrorStrings() noexcept;
39+
AWS_CRT_CPP_API const char* ErrorDebugString(int error) noexcept;
40+
}
41+
}

include/aws/crt/Exports.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
/*
4+
*Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
*icensed under the Apache License, Version 2.0 (the "License").
7+
*You may not use this file except in compliance with the License.
8+
*A copy of the License is located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed
13+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
#if defined(USE_WINDOWS_DLL_SEMANTICS) || defined(WIN32)
19+
# ifdef USE_IMPORT_EXPORT
20+
# ifdef AWS_CRT_CPP_EXPORTS
21+
# define AWS_CRT_CPP_API __declspec(dllexport)
22+
# else
23+
# define AWS_CRT_CPP_API __declspec(dllimport)
24+
# endif /* AWS_CRT_CPP_API */
25+
# else
26+
# define AWS_CRT_CPP_API
27+
# endif // USE_IMPORT_EXPORT
28+
29+
#else // defined (USE_WINDOWS_DLL_SEMANTICS) || defined (WIN32)
30+
# define AWS_CRT_CPP_API
31+
#endif // defined (USE_WINDOWS_DLL_SEMANTICS) || defined (WIN32)

include/aws/crt/StlAllocator.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
/*
3+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License").
6+
* You may not use this file except in compliance with the License.
7+
* A copy of the License is located at
8+
*
9+
* http://aws.amazon.com/apache2.0
10+
*
11+
* or in the "license" file accompanying this file. This file is distributed
12+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
* express or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
17+
#include <aws/crt/Types.h>
18+
19+
#include <memory>
20+
21+
namespace Aws
22+
{
23+
namespace Crt
24+
{
25+
extern Allocator* g_allocator;
26+
27+
template<typename T>
28+
class StlAllocator final : public std::allocator<T>
29+
{
30+
public:
31+
using Base = std::allocator<T>;
32+
33+
StlAllocator() noexcept : Base() {}
34+
StlAllocator(const StlAllocator<T>& a) noexcept : Base(a) {}
35+
36+
template<class U>
37+
StlAllocator(const StlAllocator<U>& a) noexcept : Base(a) {}
38+
39+
~StlAllocator() {}
40+
41+
using size_type = std::size_t;
42+
43+
template<typename U>
44+
struct rebind
45+
{
46+
typedef StlAllocator<U> other;
47+
};
48+
49+
typename Base::pointer allocate(size_type n, const void* hint = nullptr)
50+
{
51+
(void)hint;
52+
assert(g_allocator);
53+
return reinterpret_cast<typename Base::pointer>(aws_mem_acquire(g_allocator, n * sizeof(T)));
54+
}
55+
56+
void deallocate(typename Base::pointer p, size_type)
57+
{
58+
assert(g_allocator);
59+
aws_mem_release(g_allocator, p);
60+
}
61+
};
62+
}
63+
}

include/aws/crt/Types.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
/*
3+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License").
6+
* You may not use this file except in compliance with the License.
7+
* A copy of the License is located at
8+
*
9+
* http://aws.amazon.com/apache2.0
10+
*
11+
* or in the "license" file accompanying this file. This file is distributed
12+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
* express or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
#include <aws/crt/Exports.h>
17+
18+
#include <aws/common/common.h>
19+
#include <aws/mqtt/mqtt.h>
20+
#include <aws/io/socket.h>
21+
22+
struct aws_allocator;
23+
struct aws_byte_buf;
24+
struct aws_byte_cursor;
25+
struct aws_socket_options;
26+
27+
namespace Aws
28+
{
29+
namespace Crt
30+
{
31+
using Allocator = aws_allocator;
32+
using ByteBuf = aws_byte_buf;
33+
using ByteCursor = aws_byte_cursor;
34+
35+
AWS_CRT_CPP_API Allocator* DefaultAllocator() noexcept;
36+
AWS_CRT_CPP_API ByteBuf ByteBufFromCString(const char* str) noexcept;
37+
AWS_CRT_CPP_API ByteBuf ByteBufFromArray(const uint8_t *array, size_t len) noexcept;
38+
39+
namespace Io
40+
{
41+
using SocketOptions = aws_socket_options;
42+
}
43+
44+
namespace Mqtt
45+
{
46+
using QOS = aws_mqtt_qos;
47+
using ReturnCode = aws_mqtt_connect_return_code;
48+
}
49+
}
50+
}

include/aws/crt/io/Bootstrap.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
/*
3+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License").
6+
* You may not use this file except in compliance with the License.
7+
* A copy of the License is located at
8+
*
9+
* http://aws.amazon.com/apache2.0
10+
*
11+
* or in the "license" file accompanying this file. This file is distributed
12+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
* express or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
17+
#include <aws/crt/Exports.h>
18+
#include <aws/crt/Types.h>
19+
#include <aws/crt/io/EventLoopGroup.h>
20+
21+
#include <aws/io/channel_bootstrap.h>
22+
23+
namespace Aws
24+
{
25+
namespace Crt
26+
{
27+
namespace Io
28+
{
29+
class AWS_CRT_CPP_API ClientBootstrap final
30+
{
31+
public:
32+
ClientBootstrap(EventLoopGroup& elGroup,
33+
Allocator* allocator = DefaultAllocator()) noexcept;
34+
~ClientBootstrap();
35+
ClientBootstrap(const ClientBootstrap&) = delete;
36+
ClientBootstrap& operator=(const ClientBootstrap&) = delete;
37+
ClientBootstrap(ClientBootstrap&&) noexcept;
38+
ClientBootstrap& operator=(ClientBootstrap&&) noexcept;
39+
40+
operator bool() const noexcept;
41+
int LastError() const noexcept;
42+
43+
aws_client_bootstrap* GetUnderlyingHandle() noexcept;
44+
private:
45+
aws_client_bootstrap m_bootstrap;
46+
int m_lastError;
47+
};
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)