Skip to content

Commit

Permalink
Set LastKnownGoodTime using UTC via python-generated header (#28668)
Browse files Browse the repository at this point in the history
* WIP: set LKGT using UTC via python

* appease the linter

* Changes from review

* Remove header guard cruft
  • Loading branch information
cecille authored and pull[bot] committed Nov 27, 2023
1 parent d6a1760 commit 1920965
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
62 changes: 62 additions & 0 deletions build/chip/write_build_time_header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/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 argparse
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, define_name, define_val):
self.output = output
self.define_name = define_name
self.define_val = define_val


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

# 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,
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('#pragma once\n\n')

output_file.write(f'#define {options.define_name} {options.define_val}\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 @@ -452,6 +452,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

0 comments on commit 1920965

Please sign in to comment.