Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / lighting-app / linux / Options.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 #include "Options.h"
20
21 #include <platform/CHIPDeviceLayer.h>
22
23 #include <core/CHIPError.h>
24 #include <support/CHIPArgParser.hpp>
25
26 using namespace chip;
27 using namespace chip::ArgParser;
28
29 namespace {
30 LinuxDeviceOptions gDeviceOptions;
31
32 // Follow the code style of command line arguments in case we need to add more options in the future.
33 enum
34 {
35     kDeviceOption_BleDevice = 0x1000,
36     kDeviceOption_WiFi      = 0x1001,
37     kDeviceOption_Thread    = 0x1002,
38 };
39
40 OptionDef sDeviceOptionDefs[] = { { "ble-device", kArgumentRequired, kDeviceOption_BleDevice },
41 #if CHIP_DEVICE_CONFIG_ENABLE_WPA
42                                   { "wifi", kNoArgument, kDeviceOption_WiFi },
43 #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
44 #if CHIP_ENABLE_OPENTHREAD
45                                   { "thread", kNoArgument, kDeviceOption_Thread },
46 #endif // CHIP_ENABLE_OPENTHREAD
47                                   {} };
48
49 const char * sDeviceOptionHelp = "  --ble-device <number>\n"
50                                  "       The device number for CHIPoBLE, without 'hci' prefix, can be found by hciconfig.\n"
51 #if CHIP_DEVICE_CONFIG_ENABLE_WPA
52                                  "\n"
53                                  "  --wifi\n"
54                                  "       Enable WiFi management via wpa_supplicant.\n"
55 #endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
56 #if CHIP_ENABLE_OPENTHREAD
57                                  "\n"
58                                  "  --thread\n"
59                                  "       Enable Thread management via ot-agent.\n"
60 #endif // CHIP_ENABLE_OPENTHREAD
61                                  "\n";
62
63 bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
64 {
65     bool retval = true;
66
67     switch (aIdentifier)
68     {
69
70     case kDeviceOption_BleDevice:
71         if (!ParseInt(aValue, LinuxDeviceOptions::GetInstance().mBleDevice))
72         {
73             PrintArgError("%s: invalid value specified for ble device number: %s\n", aProgram, aValue);
74             retval = false;
75         }
76         break;
77
78     case kDeviceOption_WiFi:
79         LinuxDeviceOptions::GetInstance().mWiFi = true;
80         break;
81
82     case kDeviceOption_Thread:
83         LinuxDeviceOptions::GetInstance().mThread = true;
84         break;
85
86     default:
87         PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
88         retval = false;
89         break;
90     }
91
92     return (retval);
93 }
94
95 OptionSet sDeviceOptions = { HandleOption, sDeviceOptionDefs, "GENERAL OPTIONS", sDeviceOptionHelp };
96
97 OptionSet * sLinuxDeviceOptionSets[] = { &sDeviceOptions, nullptr };
98 } // namespace
99
100 CHIP_ERROR ParseArguments(int argc, char * argv[])
101 {
102     if (!ParseArgs(argv[0], argc, argv, sLinuxDeviceOptionSets))
103     {
104         return CHIP_ERROR_INVALID_ARGUMENT;
105     }
106     return CHIP_NO_ERROR;
107 }
108
109 LinuxDeviceOptions & LinuxDeviceOptions::GetInstance()
110 {
111     return gDeviceOptions;
112 }