Skip to content

Commit

Permalink
[platform] add platform api for mDNS (project-chip#3241)
Browse files Browse the repository at this point in the history
* [platform] add platform api for mdns

* add timeout for tests

* use pragma once

* use nlTest

* nit

* splict text key and val

* modify interface for better cross-platform compatibility

* fix address

* fix code reviews

* fix race

* move to happy tests

* update base build image

* fix build

* add feature flags for mdns

* enable mdns in cirque
  • Loading branch information
gjc13 authored Oct 26, 2020
1 parent 2e27d8f commit 5de2929
Show file tree
Hide file tree
Showing 23 changed files with 1,280 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/android.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build-android:0.4.11
image: connectedhomeip/chip-build-android:0.4.12
volumes:
- "/tmp/log_output:/tmp/test_logs"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/bloat_check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build:0.4.11
image: connectedhomeip/chip-build:0.4.12

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build:0.4.11
image: connectedhomeip/chip-build:0.4.12
volumes:
- "/tmp/log_output:/tmp/test_logs"
options:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/examples-gn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build-esp32:0.4.11
image: connectedhomeip/chip-build-esp32:0.4.12
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"
- "/tmp/output_binaries:/tmp/output_binaries"
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build-nrf-platform:0.4.11
image: connectedhomeip/chip-build-nrf-platform:0.4.12
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"
- "/tmp/output_binaries:/tmp/output_binaries"
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build:0.4.11
image: connectedhomeip/chip-build:0.4.12
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"
- "/tmp/output_binaries:/tmp/output_binaries"
Expand Down Expand Up @@ -164,7 +164,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build-efr32:0.4.11
image: connectedhomeip/chip-build-efr32:0.4.12
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"
- "/tmp/output_binaries:/tmp/output_binaries"
Expand Down Expand Up @@ -222,7 +222,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build-nrf-platform:0.4.11
image: connectedhomeip/chip-build-nrf-platform:0.4.12
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"
- "/tmp/output_binaries:/tmp/output_binaries"
Expand Down Expand Up @@ -300,7 +300,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build:0.4.11
image: connectedhomeip/chip-build:0.4.12
volumes:
- "/tmp/bloat_reports:/tmp/bloat_reports"
- "/tmp/output_binaries:/tmp/output_binaries"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/qemu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build-esp32-qemu:0.4.11
image: connectedhomeip/chip-build-esp32-qemu:0.4.12
volumes:
- "/tmp/log_output:/tmp/test_logs"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit_integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: connectedhomeip/chip-build:0.4.11
image: connectedhomeip/chip-build:0.4.12
volumes:
- "/tmp/log_output:/tmp/test_logs"
- "/tmp/happy_test_logs:/tmp/happy_test_logs"
Expand Down
2 changes: 1 addition & 1 deletion integrations/docker/images/chip-build-cirque/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.11
0.4.12
7 changes: 6 additions & 1 deletion integrations/docker/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ VERSION=${DOCKER_RUN_VERSION:-$(cat "$here/version")} ||
DOCKER_RUN_IMAGE
DOCKER_RUN_VERSION"

# full image name
FULL_IMAGE_NAME="$ORG/$IMAGE${VERSION:+:${VERSION}}"

# where
RUN_DIR=${DOCKER_RUN_DIR:-$(pwd)}

Expand Down Expand Up @@ -101,4 +104,6 @@ for arg in "$@"; do
esac
done

docker run "${runargs[@]}" --rm --mount "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" -w "$RUN_DIR" -v "$RUN_DIR:$RUN_DIR" "$ORG/$IMAGE${VERSION:+:${VERSION}}" "$@"
docker pull "$FULL_IMAGE_NAME" || "$here"/build.sh

docker run "${runargs[@]}" --rm --mount "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" -w "$RUN_DIR" -v "$RUN_DIR:$RUN_DIR" "$FULL_IMAGE_NAME" "$@"
7 changes: 5 additions & 2 deletions scripts/tests/happy_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ function happytest_install_packages() {
echo "install_packages should be invoked under root"
return 1
fi
echo "Install packages: bridge-utils iproute2 net-tools python3-lockfile python3-pip python3-psutil python3-setuptools strace"
apt-get update && apt-get install -y bridge-utils \
echo "Install packages: avahi-daemon bridge-utils iproute2 net-tools python3-lockfile python3-pip python3-psutil python3-setuptools strace"
apt-get update && apt-get install -y avahi-daemon \
bridge-utils \
iproute2 \
net-tools \
python3-lockfile \
Expand All @@ -47,6 +48,8 @@ function happytest_install_packages() {
function happytest_bootstrap() {
echo "Bootstrapping Happy Test"
set -e
service dbus start
service avahi-daemon start
# Bootstrap Happy
cd "$REPO_DIR/third_party/happy/repo"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

// Include the non-inline definitions for the GenericPlatformManagerImpl<> template,
// from which the GenericPlatformManagerImpl_POSIX<> template inherits.
#include <platform/Linux/MdnsImpl.h>
#include <platform/internal/GenericPlatformManagerImpl.cpp>

#include <system/SystemLayer.h>
Expand Down Expand Up @@ -138,6 +139,9 @@ void GenericPlatformManagerImpl_POSIX<ImplClass>::SysUpdate()
InetLayer.PrepareSelect(mMaxFd, &mReadSet, &mWriteSet, &mErrorSet, mNextTimeout);
}
#endif // !(CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK)
#if CHIP_ENABLE_MDNS
chip::Protocols::Mdns::UpdateMdnsDataset(mReadSet, mWriteSet, mErrorSet, mMaxFd, mNextTimeout);
#endif
}

template <class ImplClass>
Expand Down Expand Up @@ -173,6 +177,9 @@ void GenericPlatformManagerImpl_POSIX<ImplClass>::SysProcess()
#endif // !(CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK)

ProcessDeviceEvents();
#if CHIP_ENABLE_MDNS
chip::Protocols::Mdns::ProcessMdns(mReadSet, mWriteSet, mErrorSet);
#endif
}

template <class ImplClass>
Expand Down
3 changes: 3 additions & 0 deletions src/lib/core/CHIPError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ bool FormatCHIPError(char * buf, uint16_t bufSize, int32_t err)
case CHIP_ERROR_UNSUPPORTED_WIRELESS_OPERATING_LOCATION:
desc = "Unsupported wireless operating location";
break;
case CHIP_ERROR_MDNS_COLLISSION:
desc = "mDNS collission";
break;
}
#endif // !CHIP_CONFIG_SHORT_ERROR_STR

Expand Down
9 changes: 9 additions & 0 deletions src/lib/core/CHIPError.h
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,15 @@ typedef CHIP_CONFIG_ERROR_TYPE CHIP_ERROR;
*/
#define CHIP_ERROR_UNSUPPORTED_WIRELESS_OPERATING_LOCATION _CHIP_ERROR(179)

/**
* @def CHIP_ERROR_MDNS_COLLISSION
*
* @brief
* The registered service name has collision on the LAN.
*
*/
#define CHIP_ERROR_MDNS_COLLISSION _CHIP_ERROR(180)

/**
* @}
*/
Expand Down
170 changes: 170 additions & 0 deletions src/lib/protocols/mdns/Mdns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file defines the platform API to publish and subscribe mDNS
* services.
*
* You can find the implementation in src/platform/<PLATFORM>/MdnsImpl.cpp.
*/

#pragma once

#include <stdint.h>

#include "core/CHIPError.h"
#include "inet/IPAddress.h"
#include "inet/InetInterface.h"
#include "lib/core/Optional.h"

namespace chip {
namespace Protocols {
namespace Mdns {

static constexpr uint8_t kMdnsNameMaxSize = 32;
static constexpr uint8_t kMdnsTypeMaxSize = 32;
static constexpr uint16_t kMdnsTextMaxSize = 64;

enum class MdnsServiceProtocol : uint8_t
{
kMdnsProtocolUdp = 0,
kMdnsProtocolTcp,
kMdnsProtocolUnknown = 255,
};

struct TextEntry
{
const char * mKey;
const uint8_t * mData;
size_t mDataSize;
};

struct MdnsService
{
char mName[kMdnsNameMaxSize + 1];
char mType[kMdnsTypeMaxSize + 1];
MdnsServiceProtocol mProtocol;
uint16_t mPort;
chip::Inet::InterfaceId interface;
TextEntry * mTextEntryies;
size_t mTextEntrySize;
Optional<chip::Inet::IPAddress> mAddress;
};

/**
* The callback function for mDNS resolve.
*
* The callback function SHALL NOT take the ownership of the result->mService.mTextEntries
* memory.
*
* @param[in] context The context passed to ChipMdnsBrowse or ChipMdnsResolve.
* @param[in] result The mdns resolve result, can be nullptr if error happens.
* @param[in] error The error code.
*
*/
using MdnsResolveCallback = void (*)(void * context, MdnsService * result, CHIP_ERROR error);

/**
* The callback function for mDNS browse.
*
* The callback function SHALL NOT take the ownership of the service->mTextEntries
* memory.
*
* @param[in] context The context passed to ChipMdnsBrowse or ChipMdnsResolve.
* @param[in] services The service list, can be nullptr.
* @param[in] serciesSize The size of the service list.
* @param[in] error The error code.
*
*/
using MdnsBrowseCallback = void (*)(void * context, MdnsService * services, size_t servicesSize, CHIP_ERROR error);

using MdnsAsnycReturnCallback = void (*)(void * context, CHIP_ERROR error);

/**
* This function intializes the mdns module
*
* @param[in] initCallback The callback for notifying the initialization result.
* @param[in] errorCallback The callback for notifying internal errors.
* @param[in] context The context passed to the callbacks.
*
* @retval CHIP_NO_ERROR The initialization succeeds.
* @retval Error code The initialization fails
*
*/
CHIP_ERROR ChipMdnsInit(MdnsAsnycReturnCallback initCallback, MdnsAsnycReturnCallback errorCallback, void * context);

/**
* This function publishes an service via mDNS.
*
* Calling the function again with the same service name, type, protocol,
* interface and port but different text will update the text published.
* This function will NOT take the ownership of service->mTextEntries memory.
*
* @param[in] service The service entry.
*
* @retval CHIP_NO_ERROR The publish succeeds.
* @retval CHIP_ERROR_INVALID_ARGUMENT The service is nullptr.
* @retval Error code The publish fails.
*
*/
CHIP_ERROR ChipMdnsPublishService(const MdnsService * service);

/**
* This function stops publishing service via mDNS.
*
* @retval CHIP_NO_ERROR The publish stops successfully.
* @retval Error code Stopping the publish fails.
*
*/
CHIP_ERROR ChipMdnsStopPublish();

/**
* This function browses the services published by mdns
*
* @param[in] type The service type.
* @param[in] protocol The service protocol.
* @param[in] interface The interface to send queries.
* @param[in] callback The callback for found services.
* @param[in] context The user context.
*
* @retval CHIP_NO_ERROR The browse succeeds.
* @retval CHIP_ERROR_INVALID_ARGUMENT The type or callback is nullptr.
* @retval Error code The browse fails.
*
*/
CHIP_ERROR ChipMdnsBrowse(const char * type, MdnsServiceProtocol protocol, chip::Inet::InterfaceId interface,
MdnsBrowseCallback callback, void * context);

/**
* This function resolves the services published by mdns
*
* @param[in] browseResult The service entry returned by @ref ChipMdnsBrowse
* @param[in] callback The callback for found services.
* @param[in] context The user context.
*
* @retval CHIP_NO_ERROR The resolve succeeds.
* @retval CHIP_ERROR_INVALID_ARGUMENT The name, type or callback is nullptr.
* @retval Error code The resolve fails.
*
*/
CHIP_ERROR ChipMdnsResolve(MdnsService * browseResult, chip::Inet::InterfaceId interface, MdnsResolveCallback callback,
void * context);

} // namespace Mdns
} // namespace Protocols
} // namespace chip
Loading

0 comments on commit 5de2929

Please sign in to comment.