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

implement gettimeofday() #2162

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ target_compile_options(${EXECUTABLE_NAME} PUBLIC

set_target_properties(${EXECUTABLE_NAME} PROPERTIES
SUFFIX ".out"
LINK_FLAGS "-mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_FILE_NAME}.map"
LINK_FLAGS "-Wl,--wrap=gettimeofday -mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_FILE_NAME}.map"
)

add_custom_command(TARGET ${EXECUTABLE_NAME}
Expand Down Expand Up @@ -954,7 +954,7 @@ target_compile_options(${EXECUTABLE_MCUBOOT_NAME} PUBLIC

set_target_properties(${EXECUTABLE_MCUBOOT_NAME} PROPERTIES
SUFFIX ".out"
LINK_FLAGS "-mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT_MCUBOOT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_MCUBOOT_FILE_NAME}.map"
LINK_FLAGS "-Wl,--wrap=gettimeofday -mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT_MCUBOOT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_MCUBOOT_FILE_NAME}.map"
)

add_custom_command(TARGET ${EXECUTABLE_MCUBOOT_NAME}
Expand Down Expand Up @@ -997,7 +997,7 @@ target_compile_options(${EXECUTABLE_RECOVERY_NAME} PUBLIC

set_target_properties(${EXECUTABLE_RECOVERY_NAME} PROPERTIES
SUFFIX ".out"
LINK_FLAGS "-mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_RECOVERY_FILE_NAME}.map"
LINK_FLAGS "-Wl,--wrap=gettimeofday -mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_RECOVERY_FILE_NAME}.map"
)

add_custom_command(TARGET ${EXECUTABLE_RECOVERY_NAME}
Expand Down Expand Up @@ -1029,7 +1029,7 @@ target_compile_options(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} PUBLIC

set_target_properties(${EXECUTABLE_RECOVERY_MCUBOOT_NAME} PROPERTIES
SUFFIX ".out"
LINK_FLAGS "-mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT_MCUBOOT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_GRAPHICS_FILE_NAME}.map"
LINK_FLAGS "-Wl,--wrap=gettimeofday -mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT_MCUBOOT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_GRAPHICS_FILE_NAME}.map"
)

add_custom_command(TARGET ${EXECUTABLE_RECOVERY_MCUBOOT_NAME}
Expand Down Expand Up @@ -1072,7 +1072,7 @@ add_dependencies(${EXECUTABLE_RECOVERYLOADER_NAME} ${EXECUTABLE_RECOVERY_MCUBOOT

set_target_properties(${EXECUTABLE_RECOVERYLOADER_NAME} PROPERTIES
SUFFIX ".out"
LINK_FLAGS "-mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_RECOVERYLOADER_FILE_NAME}.map"
LINK_FLAGS "-Wl,--wrap=gettimeofday -mthumb -mabi=aapcs -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_RECOVERYLOADER_FILE_NAME}.map"
)

add_custom_command(TARGET ${EXECUTABLE_RECOVERYLOADER_NAME}
Expand Down Expand Up @@ -1107,7 +1107,7 @@ add_dependencies(${EXECUTABLE_MCUBOOT_RECOVERYLOADER_NAME} ${EXECUTABLE_RECOVERY

set_target_properties(${EXECUTABLE_MCUBOOT_RECOVERYLOADER_NAME} PROPERTIES
SUFFIX ".out"
LINK_FLAGS "-mthumb -mabi=aapcs -std=gnu++98 -std=c99 -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT_MCUBOOT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.map"
LINK_FLAGS "-Wl,--wrap=gettimeofday -mthumb -mabi=aapcs -std=gnu++98 -std=c99 -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT_MCUBOOT} -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,--gc-sections -Wl,--print-memory-usage --specs=nano.specs -lc -lnosys -lm -Wl,-Map=${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.map"
)

add_custom_command(TARGET ${EXECUTABLE_MCUBOOT_RECOVERYLOADER_NAME}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ble/NimbleController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
alertNotificationClient {systemTask, notificationManager},
currentTimeService {dateTimeController},
musicService {*this},
weatherService {dateTimeController},
weatherService {},
batteryInformationService {batteryController},
immediateAlertService {systemTask, notificationManager},
heartRateService {*this, heartRateController},
Expand Down
7 changes: 4 additions & 3 deletions src/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <algorithm>
#include <array>
#include <cstring>
#include <chrono>
#include <nrf_log.h>

using namespace Pinetime::Controllers;
Expand Down Expand Up @@ -80,7 +81,7 @@ int WeatherCallback(uint16_t /*connHandle*/, uint16_t /*attrHandle*/, struct ble
return static_cast<Pinetime::Controllers::SimpleWeatherService*>(arg)->OnCommand(ctxt);
}

SimpleWeatherService::SimpleWeatherService(DateTime& dateTimeController) : dateTimeController(dateTimeController) {
SimpleWeatherService::SimpleWeatherService() {
}

void SimpleWeatherService::Init() {
Expand Down Expand Up @@ -127,7 +128,7 @@ int SimpleWeatherService::OnCommand(struct ble_gatt_access_ctxt* ctxt) {

std::optional<SimpleWeatherService::CurrentWeather> SimpleWeatherService::Current() const {
if (currentWeather) {
auto currentTime = dateTimeController.CurrentDateTime().time_since_epoch();
auto currentTime = std::chrono::system_clock::now().time_since_epoch();
auto weatherTpSecond = std::chrono::seconds {currentWeather->timestamp};
auto weatherTp = std::chrono::duration_cast<std::chrono::seconds>(weatherTpSecond);
auto delta = currentTime - weatherTp;
Expand All @@ -141,7 +142,7 @@ std::optional<SimpleWeatherService::CurrentWeather> SimpleWeatherService::Curren

std::optional<SimpleWeatherService::Forecast> SimpleWeatherService::GetForecast() const {
if (forecast) {
auto currentTime = dateTimeController.CurrentDateTime().time_since_epoch();
auto currentTime = std::chrono::system_clock::now().time_since_epoch();
auto weatherTpSecond = std::chrono::seconds {forecast->timestamp};
auto weatherTp = std::chrono::duration_cast<std::chrono::seconds>(weatherTpSecond);
auto delta = currentTime - weatherTp;
Expand Down
6 changes: 1 addition & 5 deletions src/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@
#undef max
#undef min

#include "components/datetime/DateTimeController.h"

int WeatherCallback(uint16_t connHandle, uint16_t attrHandle, struct ble_gatt_access_ctxt* ctxt, void* arg);

namespace Pinetime {
namespace Controllers {

class SimpleWeatherService {
public:
explicit SimpleWeatherService(DateTime& dateTimeController);
explicit SimpleWeatherService();

void Init();

Expand Down Expand Up @@ -165,8 +163,6 @@ namespace Pinetime {

uint16_t eventHandle {};

Pinetime::Controllers::DateTime& dateTimeController;

std::optional<CurrentWeather> currentWeather;
std::optional<Forecast> forecast;
};
Expand Down
17 changes: 17 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include "touchhandler/TouchHandler.h"
#include "buttonhandler/ButtonHandler.h"

#include <sys/time.h>

#if NRF_LOG_ENABLED
#include "logging/NrfLogger.h"
Pinetime::Logging::NrfLogger logger;
Expand Down Expand Up @@ -109,6 +111,21 @@ Pinetime::Controllers::TouchHandler touchHandler;
Pinetime::Controllers::ButtonHandler buttonHandler;
Pinetime::Controllers::BrightnessController brightnessController {};

extern "C" {
int __wrap_gettimeofday(struct timeval* tv, struct timezone* tz) {
using namespace std::chrono;
auto currentDayTime(round<microseconds>(dateTimeController.CurrentDateTime()));
auto usAfterEpoch(currentDayTime.time_since_epoch().count());
tv->tv_sec = usAfterEpoch / 1000000;
tv->tv_usec = usAfterEpoch % 1000000;
tz->tz_minuteswest = dateTimeController.TzOffset() + dateTimeController.DstOffset();
tz->tz_minuteswest *= -60;
tz->tz_dsttime = -1;
errno = 0;
return 0;
}
}

Pinetime::Applications::DisplayApp displayApp(lcd,
touchPanel,
batteryController,
Expand Down
Loading