Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set LastKnownGoodTime using UTC via python-generated header #28668

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions build/chip/write_build_time_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python
# Copyright (c) 2023 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.

import optparse
cecille marked this conversation as resolved.
Show resolved Hide resolved
import os
from datetime import datetime, timezone


def utc_time_in_matter_epoch_s():
""" Returns the time in matter epoch in s. """
# Matter epoch is 0 hours, 0 minutes, 0 seconds on Jan 1, 2000 UTC
utc_matter = datetime.now(tz=timezone.utc) - datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc)
return int(utc_matter.total_seconds())


class Options:
def __init__(self, output, header_guard, define_name, define_val):
self.output = output
self.header_guard = header_guard
self.define_name = define_name
self.define_val = define_val


def GetOptions():
parser = optparse.OptionParser()
parser.add_option('--output', help="Output header name (inside gen dir)")
parser.add_option('--gen-dir',
help="Path to root of generated file directory tree.")
cmdline_options, _ = parser.parse_args()

# Compute header guard by replacing some chars with _ and upper-casing.
header_guard = cmdline_options.output.upper()
cecille marked this conversation as resolved.
Show resolved Hide resolved
header_guard = \
header_guard.replace('/', '_').replace('\\', '_').replace('.', '_')
header_guard += '_'

# The actual output file is inside the gen dir.
output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)

define_name = 'CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S'
build_time = utc_time_in_matter_epoch_s()

return Options(output=output,
header_guard=header_guard,
define_name=define_name,
define_val=str(build_time))


def WriteHeader(options):
with open(options.output, "w") as output_file:
output_file.write("// Generated by write_build_time_header.py\n")

output_file.write(f'\n#ifndef {options.header_guard}\n')
output_file.write(f'#define {options.header_guard}\n\n')

output_file.write(f'#define {options.define_name} {options.define_val}\n')

output_file.write(f'\n#endif // {options.header_guard}\n')


options = GetOptions()
WriteHeader(options)
24 changes: 23 additions & 1 deletion src/credentials/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,32 @@ import("//build_overrides/chip.gni")
import("//build_overrides/nlassert.gni")
import("${chip_root}/src/crypto/crypto.gni")
import("${chip_root}/src/platform/device.gni")

declare_args() {
chip_build_example_creds = true
}

action("gen_build_time_header") {
script = "${chip_root}/build/chip/write_build_time_header.py"

header_file = "FirmwareBuildTime.h"
include_dir = "${root_gen_dir}/include"
outputs = [ "${include_dir}/${header_file}" ]

args = [
"--output",
header_file,
"--gen-dir",
rebase_path(include_dir, root_build_dir),
]

visibility = [ ":build_time_header" ]
}

source_set("build_time_header") {
sources = get_target_outputs(":gen_build_time_header")
deps = [ ":gen_build_time_header" ]
}

static_library("credentials") {
output_name = "libCredentials"

Expand Down Expand Up @@ -87,6 +108,7 @@ static_library("credentials") {
cflags = [ "-Wconversion" ]

public_deps = [
":build_time_header",
"${chip_root}/src/crypto",
"${chip_root}/src/lib/asn1",
"${chip_root}/src/lib/core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#ifndef GENERIC_CONFIGURATION_MANAGER_IMPL_CPP
#define GENERIC_CONFIGURATION_MANAGER_IMPL_CPP

#include <FirmwareBuildTime.h>
#include <ble/CHIPBleServiceData.h>
#include <crypto/CHIPCryptoPAL.h>
#include <crypto/RandUtils.h>
Expand Down Expand Up @@ -290,6 +291,12 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetFirmwareBuildChipEpo
chipEpochTime = sFirmwareBuildChipEpochTime.Value();
return CHIP_NO_ERROR;
}
#ifdef CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S
{
chipEpochTime = chip::System::Clock::Seconds32(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S);
return CHIP_NO_ERROR;
}
#endif
// Else, attempt to read the hard-coded values.
VerifyOrReturnError(!BUILD_DATE_IS_BAD(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_DATE), CHIP_ERROR_INTERNAL);
VerifyOrReturnError(!BUILD_TIME_IS_BAD(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME), CHIP_ERROR_INTERNAL);
Expand Down
1 change: 1 addition & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ if (chip_device_platform != "none") {
":platform_base",
"${chip_root}/src/app:app_config",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/credentials:build_time_header",
"${chip_root}/src/crypto",
"${chip_root}/src/lib/support",
]
Expand Down