/src/openweave-core/src/test-apps/DeviceDescOptions.cpp
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2013-2017 Nest Labs, Inc. |
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 | | /** |
20 | | * @file |
21 | | * Implementation of DeviceDescOptions object, which handles parsing of command line options |
22 | | * that specify descriptive information about the simulated "device" used in test applications. |
23 | | * |
24 | | */ |
25 | | |
26 | | |
27 | | #include "ToolCommon.h" |
28 | | #include <Weave/Support/CodeUtils.h> |
29 | | #include <Weave/Profiles/WeaveProfiles.h> |
30 | | #include <Weave/Profiles/common/CommonProfile.h> |
31 | | #include <Weave/Profiles/vendor/nestlabs/device-description/NestProductIdentifiers.hpp> |
32 | | #include "DeviceDescOptions.h" |
33 | | |
34 | | using namespace nl::Weave::Profiles::DeviceDescription; |
35 | | |
36 | | DeviceDescOptions gDeviceDescOptions; |
37 | | |
38 | | DeviceDescOptions::DeviceDescOptions() |
39 | 10 | { |
40 | 10 | static OptionDef optionDefs[] = |
41 | 10 | { |
42 | 10 | { "serial-num", kArgumentRequired, kToolCommonOpt_DeviceSerialNum }, |
43 | 10 | { "vendor-id", kArgumentRequired, kToolCommonOpt_DeviceVendorId }, |
44 | 10 | { "product-id", kArgumentRequired, kToolCommonOpt_DeviceProductId }, |
45 | 10 | { "product-rev", kArgumentRequired, kToolCommonOpt_DeviceProductRevision }, |
46 | 10 | { "software-version", kArgumentRequired, kToolCommonOpt_DeviceSoftwareVersion }, |
47 | 10 | { } |
48 | 10 | }; |
49 | 10 | OptionDefs = optionDefs; |
50 | | |
51 | 10 | HelpGroupName = "DEVICE DESCRIPTION OPTIONS"; |
52 | | |
53 | 10 | OptionHelp = |
54 | 10 | " --serial-num <string>\n" |
55 | 10 | " Device serial number. Defaults to \"mock-device\".\n" |
56 | 10 | "\n" |
57 | 10 | " --vendor-id <int>\n" |
58 | 10 | " Device vendor id. Defaults to 0x235A (Nest Labs)\n" |
59 | 10 | "\n" |
60 | 10 | " --product-id <int>\n" |
61 | 10 | " Device product id. Defaults to 5 (Nest Protect).\n" |
62 | 10 | "\n" |
63 | 10 | " --product-rev <int>\n" |
64 | 10 | " Device product revision. Defaults to 1.\n" |
65 | 10 | "\n" |
66 | 10 | " --software-version <string>\n" |
67 | 10 | " Device software version string. Defaults to \"mock-device/1.0\".\n" |
68 | 10 | "\n"; |
69 | | |
70 | | // Setup Defaults. |
71 | 10 | BaseDeviceDesc.Clear(); |
72 | 10 | BaseDeviceDesc.VendorId = kWeaveVendor_NestLabs; |
73 | 10 | BaseDeviceDesc.ProductId = nl::Weave::Profiles::Vendor::Nestlabs::DeviceDescription::kNestWeaveProduct_Topaz; |
74 | 10 | BaseDeviceDesc.ProductRevision = 1; |
75 | 10 | BaseDeviceDesc.ManufacturingDate.Year = 2013; |
76 | 10 | BaseDeviceDesc.ManufacturingDate.Month = 1; |
77 | 10 | BaseDeviceDesc.ManufacturingDate.Day = 1; |
78 | 10 | memset(BaseDeviceDesc.Primary802154MACAddress, 0x11, sizeof(BaseDeviceDesc.Primary802154MACAddress)); |
79 | 10 | memset(BaseDeviceDesc.PrimaryWiFiMACAddress, 0x22, sizeof(BaseDeviceDesc.PrimaryWiFiMACAddress)); |
80 | 10 | strcpy(BaseDeviceDesc.RendezvousWiFiESSID, "MOCK-1111"); |
81 | 10 | strcpy(BaseDeviceDesc.SerialNumber, "mock-device"); |
82 | 10 | strcpy(BaseDeviceDesc.SoftwareVersion, "mock-device/1.0"); |
83 | 10 | BaseDeviceDesc.DeviceFeatures = |
84 | 10 | WeaveDeviceDescriptor::kFeature_HomeAlarmLinkCapable | |
85 | 10 | WeaveDeviceDescriptor::kFeature_LinePowered; |
86 | | // NOTE: For security reasons, pairing codes should only ever appear in device descriptors that are |
87 | | // encoded into QR codes. options.BaseDeviceDesc contains the device descriptor fields that get sent over the |
88 | | // network (e.g. in an IdentifyDevice exchange). Therefore the PairingCode field should never be |
89 | | // set here. |
90 | 10 | } |
91 | | |
92 | | void DeviceDescOptions::GetDeviceDesc(WeaveDeviceDescriptor& deviceDesc) |
93 | 0 | { |
94 | 0 | deviceDesc = BaseDeviceDesc; |
95 | 0 | deviceDesc.DeviceId = FabricState.LocalNodeId; |
96 | 0 | deviceDesc.FabricId = FabricState.FabricId; |
97 | 0 | memset(deviceDesc.PairingCode, 0, sizeof(deviceDesc.PairingCode)); |
98 | 0 | } |
99 | | |
100 | | bool DeviceDescOptions::HandleOption(const char *progName, OptionSet *optSet, int id, const char *name, const char *arg) |
101 | 0 | { |
102 | 0 | DeviceDescOptions& options = *static_cast<DeviceDescOptions *>(optSet); |
103 | |
|
104 | 0 | switch (id) |
105 | 0 | { |
106 | 0 | case kToolCommonOpt_DeviceSerialNum: |
107 | 0 | if (strlen(arg) > WeaveDeviceDescriptor::kMaxSerialNumberLength) |
108 | 0 | { |
109 | 0 | PrintArgError("%s: Invalid value specified for device serial number (value too long): %s\n", progName, arg); |
110 | 0 | return false; |
111 | 0 | } |
112 | 0 | strncpy(options.BaseDeviceDesc.SerialNumber, arg, sizeof(options.BaseDeviceDesc.SerialNumber)); |
113 | 0 | break; |
114 | 0 | case kToolCommonOpt_DeviceVendorId: |
115 | 0 | if (!ParseInt(arg, options.BaseDeviceDesc.VendorId) || options.BaseDeviceDesc.VendorId == 0 || options.BaseDeviceDesc.VendorId >= 0xFFF0) |
116 | 0 | { |
117 | 0 | PrintArgError("%s: Invalid value specified for device vendor ID: %s\n", progName, arg); |
118 | 0 | return false; |
119 | 0 | } |
120 | 0 | break; |
121 | 0 | case kToolCommonOpt_DeviceProductId: |
122 | 0 | if (!ParseInt(arg, options.BaseDeviceDesc.ProductId) || options.BaseDeviceDesc.ProductId == 0 || options.BaseDeviceDesc.ProductId == 0xFFFF) |
123 | 0 | { |
124 | 0 | PrintArgError("%s: Invalid value specified for device product ID: %s\n", progName, arg); |
125 | 0 | return false; |
126 | 0 | } |
127 | 0 | break; |
128 | 0 | case kToolCommonOpt_DeviceProductRevision: |
129 | 0 | if (!ParseInt(arg, options.BaseDeviceDesc.ProductRevision)) |
130 | 0 | { |
131 | 0 | PrintArgError("%s: Invalid value specified for device product revision: %s\n", progName, arg); |
132 | 0 | return false; |
133 | 0 | } |
134 | 0 | break; |
135 | 0 | case kToolCommonOpt_DeviceSoftwareVersion: |
136 | 0 | if (strlen(arg) > WeaveDeviceDescriptor::kMaxSoftwareVersionLength) |
137 | 0 | { |
138 | 0 | PrintArgError("%s: Invalid value specified for device software version (value too long): %s\n", progName, arg); |
139 | 0 | return false; |
140 | 0 | } |
141 | 0 | strncpy(options.BaseDeviceDesc.SoftwareVersion, arg, sizeof(options.BaseDeviceDesc.SoftwareVersion)); |
142 | 0 | break; |
143 | 0 | default: |
144 | 0 | PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", progName, name); |
145 | 0 | return false; |
146 | 0 | } |
147 | | |
148 | 0 | return true; |
149 | 0 | } |