Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / tests / TestThreadStackMgr.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
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  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /* TIZEN_CHIP_MODIFY */
19
20 #include <assert.h>
21 #include <dbus/dbus.h>
22 #include <memory>
23
24 #include <support/CHIPMem.h>
25 #include <support/UnitTestRegistration.h>
26
27 #include "platform/internal/CHIPDeviceLayerInternal.h"
28
29 #include "platform/PlatformManager.h"
30 #include "platform/ThreadStackManager.h"
31
32 #if CHIP_DEVICE_LAYER_TARGET == LINUX || CHIP_DEVICE_LAYER_TARGET == TIZEN
33 #include <thread>
34
35 struct DBusConnectionDeleter
36 {
37     void operator()(DBusConnection * aConnection) { dbus_connection_unref(aConnection); }
38 };
39
40 using UniqueDBusConnection = std::unique_ptr<DBusConnection, DBusConnectionDeleter>;
41 #endif
42
43 void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
44 {
45     (void) arg;
46     if (event->Type == chip::DeviceLayer::DeviceEventType::kThreadConnectivityChange)
47     {
48         if (event->ThreadConnectivityChange.Result == chip::DeviceLayer::ConnectivityChange::kConnectivity_Established)
49         {
50             exit(0);
51         }
52     }
53 }
54
55 int TestThreadStackManager()
56 {
57     chip::DeviceLayer::ThreadStackManagerImpl impl;
58     chip::DeviceLayer::Internal::DeviceNetworkInfo info;
59     uint16_t masterKey[16] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
60
61     strncpy(info.ThreadNetworkName, "CHIP-TEST", sizeof(info.ThreadNetworkName));
62     info.ThreadChannel                    = UINT8_MAX;
63     info.ThreadPANId                      = 0x3455;
64     info.FieldPresent.ThreadExtendedPANId = false;
65     info.FieldPresent.ThreadMeshPrefix    = false;
66     info.FieldPresent.ThreadPSKc          = false;
67     memcpy(&info.ThreadMasterKey, &masterKey, sizeof(masterKey));
68
69     chip::Platform::MemoryInit();
70     chip::DeviceLayer::PlatformMgrImpl().InitChipStack();
71     chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0);
72
73     impl.InitThreadStack();
74     impl.StartThreadTask();
75     impl._SetThreadProvision(info);
76     impl._SetThreadEnabled(true);
77
78     printf("Start Thread task done\n");
79
80     chip::DeviceLayer::PlatformMgrImpl().RunEventLoop();
81
82     return -1;
83 }
84
85 CHIP_REGISTER_TEST_SUITE(TestThreadStackManager);