Skip to content

Commit

Permalink
Merge branch 'iot' into mqtt_test_app
Browse files Browse the repository at this point in the history
  • Loading branch information
sbSteveK committed Oct 1, 2024
2 parents c0e4aa6 + 3b05289 commit d6a8a13
Show file tree
Hide file tree
Showing 26 changed files with 214 additions and 135 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: GitHub Action for SwiftLint
uses: norio-nomura/action-swiftlint@3.2.1

Expand All @@ -38,7 +38,7 @@ jobs:
run: |
aws s3 cp --debug s3://aws-crt-test-stuff/ci/${{ env.BUILDER_VERSION }}/linux-container-ci.sh ./linux-container-ci.sh && chmod a+x ./linux-container-ci.sh
./linux-container-ci.sh ${{ env.BUILDER_VERSION }} aws-crt-swift-5-${{ matrix.image }} build -p ${{ env.PACKAGE_NAME }}
osx:
macos:
runs-on: ${{ matrix.runner }}
env:
DEVELOPER_DIR: /Applications/Xcode.app
Expand All @@ -49,11 +49,11 @@ jobs:
matrix:
# This matrix runs tests on Mac, on oldest & newest supported Xcodes
runner:
- macos-12
- macos-13
- macos-12 # x64
- macos-13 # x64
- macos-14
- macos-13-xlarge
- macos-14-xlarge
- macos-14-large #x64
steps:
- name: Build ${{ env.PACKAGE_NAME }} + consumers
run: |
Expand All @@ -72,11 +72,11 @@ jobs:
matrix:
# This matrix runs tests on iOS, tvOS & watchOS, on oldest & newest supported Xcodes
runner:
- macos-12
- macos-13
- macos-12 # x64
- macos-13 # x64
- macos-14
- macos-13-xlarge
- macos-14-xlarge
- macos-14-large #x64
target:
[{ os: ios, destination: 'iOS Simulator,OS=16.1,name=iPhone 14'},
{ os: ios, destination: 'iOS Simulator,OS=17.2,name=iPhone 15'},
Expand All @@ -96,7 +96,7 @@ jobs:
- runner: macos-13
xcode: Xcode_15.2
# Don't run new macOS with old Xcode
- runner: macos-14-xlarge
- runner: macos-14-large
xcode: Xcode_14.1
- runner: macos-14
xcode: Xcode_14.1
Expand Down Expand Up @@ -125,7 +125,7 @@ jobs:
runs-on: ubuntu-22.04 # latest
steps:
- name: Checkout Source
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0
Expand Down
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ disabled_rules:
- unused_capture_list
- blanket_disable_command
- identifier_name
- trailing_comma

opt_in_rules:
- vertical_parameter_alignment_on_call
Expand Down
16 changes: 9 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import PackageDescription

let excludesFromAll = ["tests", "cmake", "CONTRIBUTING.md",
"LICENSE", "format-check.sh", "NOTICE", "builder.json",
"LICENSE", "format-check.py", "NOTICE", "builder.json",
"CMakeLists.txt", "README.md"]
var packageTargets: [Target] = []

Expand All @@ -15,7 +15,11 @@ var package = Package(name: "aws-crt-swift",
)

let cSettings: [CSetting] = [
.define("DEBUG_BUILD", .when(configuration: .debug))
.define("DEBUG_BUILD", .when(configuration: .debug)),
// Disable Intel VTune tracing API here since aws-crt-swift doesn't use CMake
.define("INTEL_NO_ITTNOTIFY_API"),
// Don't use APIs forbidden by App Stores (e.g. non-public system APIs)
.define("AWS_APPSTORE_SAFE"),
]

//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -81,10 +85,8 @@ awsCCalPlatformExcludes.append("source/unix")
/// s2n-tls
//////////////////////////////////////////////////////////////////////
#if os(Linux)
// add pq-crypto back after adding in platform and chipset detection
let s2nExcludes = ["bin", "codebuild", "coverage", "docker-images",
"docs", "lib", "pq-crypto/kyber_r3",
"pq-crypto/README.md", "pq-crypto/Makefile", "pq-crypto/s2n_pq_asm.mk",
"docs", "lib",
"libcrypto-build", "scram",
"s2n.mk", "Makefile", "stuffer/Makefile", "crypto/Makefile",
"tls/Makefile", "utils/Makefile", "error/Makefile", "tls/extensions/Makefile",
Expand All @@ -108,7 +110,7 @@ packageTargets.append(.target(
/// aws-c-io
//////////////////////////////////////////////////////////////////////
var ioDependencies: [Target.Dependency] = ["AwsCCommon", "AwsCCal"]
var awsCIoPlatformExcludes = ["docs", "CODE_OF_CONDUCT.md", "codebuild", "PKCS11.md", "THIRD-PARTY-LICENSES.txt",
var awsCIoPlatformExcludes = ["docs", "CODE_OF_CONDUCT.md", "codebuild", "PKCS11.md",
"source/pkcs11/v2.40"] + excludesFromAll
var cSettingsIO = cSettings

Expand Down Expand Up @@ -136,11 +138,11 @@ awsCIoPlatformExcludes.append("source/s2n")
/// aws-c-checksums
//////////////////////////////////////////////////////////////////////
var awsCChecksumsExcludes = [
"bin",
"CMakeLists.txt",
"LICENSE",
"builder.json",
"README.md",
"format-check.sh",
"cmake",
"tests"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0.
// Copyright Amazon.com, Inc. or its affiliates.
// All Rights Reserved. SPDX-License-Identifier: Apache-2.0.

import AwsCAuth
import AwsCIo
Expand Down Expand Up @@ -572,6 +572,13 @@ private func onGetCredentials(credentials: OpaquePointer?,
continuationCore.continuation.resume(returning: Credentials(rawValue: credentials!))
}

// We need to share this pointer to C in a task block but Swift compiler complains
// that Pointer does not conform to Sendable. Wrap the pointer in a @unchecked Sendable block
// for Swift compiler to stop complaining.
struct SendablePointer: @unchecked Sendable {
let pointer: UnsafeMutableRawPointer
}

private func getCredentialsDelegateFn(_ delegatePtr: UnsafeMutableRawPointer!,
_ callbackFn: (@convention(c) (
OpaquePointer?,
Expand All @@ -581,14 +588,15 @@ private func getCredentialsDelegateFn(_ delegatePtr: UnsafeMutableRawPointer!,
let delegate = Unmanaged<Box<CredentialsProviding>>
.fromOpaque(delegatePtr)
.takeUnretainedValue().contents
let userData = SendablePointer(pointer: userData)
Task {
do {
let credentials = try await delegate.getCredentials()
callbackFn(credentials.rawValue, AWS_OP_SUCCESS, userData)
callbackFn(credentials.rawValue, AWS_OP_SUCCESS, userData.pointer)
} catch CommonRunTimeError.crtError(let crtError) {
callbackFn(nil, crtError.code, userData)
callbackFn(nil, crtError.code, userData.pointer)
} catch {
callbackFn(nil, Int32(AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE.rawValue), userData)
callbackFn(nil, Int32(AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE.rawValue), userData.pointer)
}
}
return AWS_OP_SUCCESS
Expand Down
7 changes: 5 additions & 2 deletions Source/AwsCommonRuntimeKit/crt/CommonRuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public struct CRTError: Equatable {
}
var message = String(cString: aws_error_str(self.code))
if let context {
message += ": " + context
if message.last != "." {
message += "."
}
message += " " + context
}
self.message = message
self.message = message
self.name = String(cString: aws_error_name(self.code))
}

Expand Down
64 changes: 41 additions & 23 deletions Source/AwsCommonRuntimeKit/crt/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,56 @@
// SPDX-License-Identifier: Apache-2.0.

import AwsCCommon
var logger: aws_logger?
import Foundation

public enum LogTarget {
case standardOutput
case standardError
case filePath(String)
}

public struct Logger {
public static func initialize(pipe: UnsafeMutablePointer<FILE>?, level: LogLevel) {
// Clean up the logger if it was previously initialized
if var logger = logger {
aws_logger_clean_up(&logger)
aws_logger_set(nil)
private static var logger: aws_logger?
private static let lock = NSLock()

/// Initializes the CRT logger based on the specified log target and log level. The CRT logger must be only initialized once in your application. Initializing the logger multiple times is not supported.
/// - Parameters:
/// - target: The logging target, which can be standard output, standard error, or a custom file path.
/// - level: The logging level, represented by the `LogLevel` enum.
/// - Throws: CommonRunTimeError.crtException
public static func initialize(target: LogTarget, level: LogLevel) throws {
lock.lock()
defer { lock.unlock() }

// Check if the logger is already initialized
guard logger == nil else {
throw CommonRunTimeError.crtError(CRTError(
code: AWS_ERROR_UNSUPPORTED_OPERATION.rawValue,
context: "Initializing the CRT Logger multiple times is not supported."))
}

// Initialize the logger
logger = aws_logger()
var options = aws_logger_standard_options()
options.level = level.rawValue
options.file = pipe
aws_logger_init_standard(&logger!, allocator.rawValue, &options)
aws_logger_set(&logger!)
}

public static func initilize(filePath: String, level: LogLevel) {
// Clean up the logger if it was previously initialized
if var logger = logger {
aws_logger_clean_up(&logger)
aws_logger_set(nil)
}
logger = aws_logger()

filePath.withCString { cFilePath in
var options = aws_logger_standard_options()
options.level = level.rawValue
options.filename = cFilePath
// Set options based on the logging target
switch target {
case .standardOutput:
options.file = stdout
aws_logger_init_standard(&logger!, allocator.rawValue, &options)
aws_logger_set(&logger!)
case .standardError:
options.file = stderr
aws_logger_init_standard(&logger!, allocator.rawValue, &options)
case .filePath(let filePath):
filePath.withCString { cFilePath in
options.filename = cFilePath
aws_logger_init_standard(&logger!, allocator.rawValue, &options)
}
}

// Initialize and set the logger
aws_logger_set(&logger!)
}
}

Expand Down
15 changes: 15 additions & 0 deletions Source/AwsCommonRuntimeKit/crt/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,18 @@ func withOptionalArrayRawPointer<T, Result>(
return try body(ptr)
}
}
extension Array where Element == String {
func withByteCursorArray<R>(_ body: (UnsafePointer<aws_byte_cursor>, Int) -> R) -> R {
let len = self.count
let cStrings = self.map { strdup($0) }
let cursors = cStrings.map { aws_byte_cursor_from_c_str($0) }

defer {
cStrings.forEach { free($0) }
}

return cursors.withUnsafeBufferPointer { cursorsPtr in
return body(cursorsPtr.baseAddress!, len)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public class EndpointsRequestContext {
}
}

/// Add a string array endpoint parameter to the request context
/// - Parameters:
/// - name: The name of the parameter
/// - value: The value of the parameter
public func add(name: String, value: [String]?) throws {
guard let value = value else {
return
}
if (name.withByteCursor { nameCursor in
value.withByteCursorArray { ptr, len in
aws_endpoints_request_context_add_string_array(allocator.rawValue, rawValue, nameCursor, ptr, len)
}
}) != AWS_OP_SUCCESS {
throw CommonRunTimeError.crtError(.makeFromLastError())
}
}

deinit {
aws_endpoints_request_context_release(rawValue)
}
Expand Down
Loading

0 comments on commit d6a8a13

Please sign in to comment.