From 79e808eb8be3095b4f94efcffba97515ba1870bd Mon Sep 17 00:00:00 2001 From: Rob Meades Date: Tue, 23 Apr 2024 13:54:49 +0100 Subject: [PATCH] MAJOR CHANGE: uTimeout API. (#1143) MAJOR CHANGE: uTimeout API. IN CASE OF COMPILATION FAILURE: if you have copied an existing ubxlib .c file to create your own file, you may find it fails to compile after merging this commit: this is because, in our internal .c files, we do not include ubxlib.h, we bring in only the required headers, and so your .c file will be missing u_timeout.h. To fix this you should #include "u_timeout.h" nearish the top of the inclusions in your .c file, above where you would include things like u_at_client.h, below where you would include u_error_common.h (see the modified .c files in this commit for the pattern). A new common API, uTimeout, is added and all timeouts are routed through it. This API performs time comparisons with unsigned 32-bit arithmetic to ensure that timeouts expire correctly if they happen to cross the 32-bit wrap point, which will occur every 50 days for a millisecond tick; otherwise there were instances where a timeout could potentially get "stuck" for 25 days (the unsigned 32-bit wrap length for a millisecond tick). The implementation of the API includes the ability to speed up the apparent wrap-rate of the underlying tick; this is used on test instance 23 (which includes cellular, GNSS and short-range module types) to give it a good thrashing. Our thanks to warasilapm for raising this issue and _my_ thanks to mazgch for providing the solution. --- DEVELOPMENT_PRINCIPLES.md | 14 +- ble/src/gen2/u_ble_cfg_extmod.c | 2 + ble/src/gen2/u_ble_gap_extmod.c | 1 + ble/src/gen2/u_ble_gatt_extmod.c | 1 + ble/src/gen2/u_ble_sps_extmod.c | 1 + ble/src/u_ble_cfg_extmod.c | 2 + ble/src/u_ble_gap_extmod.c | 1 + ble/src/u_ble_gatt_extmod.c | 1 + ble/src/u_ble_nus.c | 1 + ble/src/u_ble_sps_extmod.c | 1 + ble/src/u_ble_sps_intmod.c | 23 +-- ble/test/u_ble_bond_test.c | 2 + cell/src/u_cell.c | 5 + cell/src/u_cell_cfg.c | 2 + cell/src/u_cell_file.c | 1 + cell/src/u_cell_fota.c | 2 + cell/src/u_cell_geofence.c | 2 + cell/src/u_cell_gpio.c | 2 + cell/src/u_cell_http.c | 2 + cell/src/u_cell_info.c | 2 + cell/src/u_cell_loc.c | 8 +- cell/src/u_cell_mno_db.c | 2 + cell/src/u_cell_mqtt.c | 73 ++++---- cell/src/u_cell_mux.c | 20 ++- cell/src/u_cell_mux_private.c | 2 + cell/src/u_cell_net.c | 80 ++++----- cell/src/u_cell_ppp.c | 8 +- cell/src/u_cell_private.c | 20 ++- cell/src/u_cell_private.h | 12 +- cell/src/u_cell_pwr.c | 25 +-- cell/src/u_cell_sec.c | 2 + cell/src/u_cell_sec_tls.c | 2 + cell/src/u_cell_sim.c | 2 + cell/src/u_cell_sock.c | 14 +- cell/src/u_cell_time.c | 16 +- cell/test/u_cell_cfg_test.c | 11 +- cell/test/u_cell_file_test.c | 2 + cell/test/u_cell_fota_test.c | 2 + cell/test/u_cell_geofence_test.c | 34 ++-- cell/test/u_cell_gpio_test.c | 2 + cell/test/u_cell_http_test.c | 27 +-- cell/test/u_cell_info_test.c | 15 +- cell/test/u_cell_loc_test.c | 37 ++-- cell/test/u_cell_mqtt_test.c | 15 +- cell/test/u_cell_mux_private_test.c | 2 + cell/test/u_cell_mux_test.c | 34 ++-- cell/test/u_cell_net_test.c | 65 ++++--- cell/test/u_cell_ppp_test.c | 17 +- cell/test/u_cell_pwr_test.c | 48 ++--- cell/test/u_cell_sec_tls_test.c | 2 + cell/test/u_cell_sock_test.c | 15 +- cell/test/u_cell_test_preamble.c | 2 + cell/test/u_cell_test_private.c | 2 + cell/test/u_cell_time_test.c | 64 ++++--- common/at_client/src/u_at_client.c | 62 +++---- common/at_client/test/u_at_client_test.c | 15 +- common/at_client/test/u_at_client_test_data.c | 18 +- common/device/src/u_device_private_cell.c | 9 +- common/device/src/u_device_shared_cell.h | 2 +- common/geofence/src/u_geofence.c | 25 ++- common/geofence/src/u_geofence_shared.h | 2 +- common/geofence/test/u_geofence_test.c | 10 +- common/http_client/src/u_http_client.c | 8 +- common/http_client/test/u_http_client_test.c | 18 +- .../src/u_location_private_cloud_locate.c | 6 +- common/location/src/u_location_stub_wifi.c | 1 + common/location/test/u_location_test.c | 48 +++-- .../test/u_location_test_shared_cfg.c | 2 + common/mqtt_client/test/u_mqtt_client_test.c | 117 ++++++------ common/network/src/u_network_private_cell.c | 9 +- common/network/src/u_network_private_wifi.c | 14 +- common/network/test/u_network_test.c | 16 +- common/security/src/u_security_credential.c | 2 + common/security/test/u_security_test.c | 11 +- common/security/test/u_security_tls_test.c | 42 ++--- common/short_range/src/gen2/u_short_range.c | 2 + .../short_range/src/gen2/u_short_range_cfg.c | 2 + .../src/gen2/u_short_range_private.c | 2 + common/short_range/src/u_short_range.c | 4 +- common/short_range/src/u_short_range_cfg.c | 2 + .../src/u_short_range_edm_stream.c | 7 +- .../short_range/src/u_short_range_private.c | 2 + .../short_range/src/u_short_range_private.h | 2 +- common/short_range/test/u_short_range_test.c | 2 + .../test/u_short_range_test_private.c | 2 + common/sock/src/u_sock.c | 12 +- common/sock/test/u_sock_test.c | 114 ++++++------ common/timeout/api/u_timeout.h | 148 +++++++++++++++ common/timeout/src/u_timeout.c | 129 +++++++++++++ common/timeout/test/u_timeout_test.c | 170 ++++++++++++++++++ example/mqtt_client/mqtt_main.c | 6 +- gnss/src/lib_mga/u_lib_mga.c | 67 ++++--- gnss/src/lib_mga/u_lib_mga.h | 6 +- gnss/src/u_gnss.c | 2 + gnss/src/u_gnss_dec_ubx_nav_pvt.c | 1 + gnss/src/u_gnss_geofence.c | 2 + gnss/src/u_gnss_mga.c | 17 +- gnss/src/u_gnss_pos.c | 23 ++- gnss/src/u_gnss_private.c | 33 ++-- gnss/src/u_gnss_stub_cell.c | 2 + gnss/src/u_gnss_util.c | 10 +- gnss/test/u_gnss_geofence_test.c | 40 +++-- gnss/test/u_gnss_info_test.c | 18 +- gnss/test/u_gnss_mga_test.c | 10 +- gnss/test/u_gnss_msg_test.c | 10 +- gnss/test/u_gnss_pos_test.c | 72 ++++---- gnss/test/u_gnss_test_private.c | 2 + port/api/u_port.h | 21 ++- port/platform/arduino/include.txt | 1 + port/platform/arduino/include_test.txt | 1 + port/platform/arduino/source.txt | 1 + .../cell_ucpu/r5/app/test/ucpu_uart_test.c | 6 +- port/platform/cell_ucpu/r5/src/u_port_uart.c | 5 +- port/platform/common/automation/DATABASE.md | 2 +- .../common/mutex_debug/u_mutex_debug.c | 8 +- port/platform/esp-idf/src/u_port_uart.c | 5 +- .../platform/esp-idf/test/u_espidf_ppp_test.c | 20 ++- port/platform/linux/src/u_port.c | 3 +- port/platform/linux/src/u_port_ppp.c | 16 +- port/platform/linux/test/u_linux_ppp_test.c | 20 ++- port/platform/nrf5sdk/src/u_port_uart.c | 6 +- port/platform/platformio/example/position.c | 4 +- port/platform/platformio/inc_src.txt | 1 + port/platform/stm32cube/src/u_port_i2c.c | 11 +- port/platform/stm32cube/src/u_port_private.c | 2 +- port/platform/stm32cube/src/u_port_uart.c | 16 +- port/platform/windows/src/u_port.c | 2 +- port/platform/zephyr/src/u_port_ppp.c | 16 +- port/platform/zephyr/src/u_port_uart.c | 6 +- port/platform/zephyr/test/u_zephyr_ppp_test.c | 20 ++- port/test/u_port_test.c | 44 ++--- port/ubxlib.cmake | 1 + port/ubxlib.mk | 1 + ubxlib.h | 1 + wifi/src/gen2/u_wifi.c | 1 + wifi/src/gen2/u_wifi_cfg.c | 2 + wifi/src/gen2/u_wifi_http.c | 2 + wifi/src/gen2/u_wifi_loc.c | 2 + wifi/src/gen2/u_wifi_mqtt.c | 2 + wifi/src/gen2/u_wifi_sock.c | 7 +- wifi/src/u_wifi.c | 1 + wifi/src/u_wifi_cfg.c | 2 + wifi/src/u_wifi_geofence.c | 2 + wifi/src/u_wifi_http.c | 2 + wifi/src/u_wifi_loc.c | 9 +- wifi/src/u_wifi_mqtt.c | 2 + wifi/src/u_wifi_sock.c | 8 +- wifi/test/u_wifi_captive_portal_test.c | 12 +- wifi/test/u_wifi_geofence_test.c | 28 +-- wifi/test/u_wifi_loc_test.c | 29 +-- 150 files changed, 1624 insertions(+), 835 deletions(-) create mode 100644 common/timeout/api/u_timeout.h create mode 100644 common/timeout/src/u_timeout.c create mode 100644 common/timeout/test/u_timeout_test.c diff --git a/DEVELOPMENT_PRINCIPLES.md b/DEVELOPMENT_PRINCIPLES.md index 7d293bfe..58b2065a 100644 --- a/DEVELOPMENT_PRINCIPLES.md +++ b/DEVELOPMENT_PRINCIPLES.md @@ -67,8 +67,16 @@ Since the commit messages are our only change documentation, and since Github is - pull the PR into a branch of `ubxlib_priv` so that you can throw it at the test system to prove that it is all good, - make sure that `ubxlib` `master` is up to date with `ubxlib_priv` `master` (i.e. push `ubxlib_priv` `master` to `ubxlib` `master`, which should always be possible, see above), - do a rebase-merge of the customer PR into `ubxlib` `master` (i.e. directly, not going via `ubxlib_priv`), - - pull `ubxlib` `master` back into `ubxlib_priv` `master` (i.e. with the latest `ubxlib_priv` `master` on your machine, pull `ubxlib` `master` and then push that to `ubxlib_priv` `master`). - + - pull `ubxlib` `master` back into `ubxlib_priv` `master` (i.e. with the latest `ubxlib_priv` `master` checked-out on your machine, pull `ubxlib` `master` and then push that to `ubxlib_priv` `master`). + The only exception to the above is when there has been active work on the `ubxlib_priv` `development` branch and that is ready to be brought into `master`: this should be brought into `ubxlib_priv` `master` through a normal (i.e. non-rebase, non-squash) merge since it will likely be a _very_ large commit of disparate things that will not be describable when in one big blob. -As an aside, if `master` moves on underneath a branch **THAT YOU ALONE** are working on, please do a `rebase` of that development branch onto `master`, rather then merging the changes from `master` onto your branch, (i.e. checkout `master` locally, pull the latest `master` and then `rebase` your branch onto `master`); the reason for this is that, otherwise, the merge process can be confused and end up thinking that you intend to remove things that have just been added in the `master` branch. If you share the branch with someone else, i.e. you are not working on it alone, then take care because rebasing obviously changes history; it may still be the right thing to do, 'cos the ground has indeed moved underneath you, history _has_ changed, but make sure that anyone else who is working on the branch with you is aware of what you have done when you push the branch back to the repo. \ No newline at end of file +As an aside, if `master` moves on underneath a branch **THAT YOU ALONE** are working on, please do a `rebase` of that development branch onto `master`, rather then merging the changes from `master` onto your branch, (i.e. checkout `master` locally, pull the latest `master` and then `rebase` your branch onto `master`); the reason for this is that, otherwise, the merge process can be confused and end up thinking that you intend to remove things that have just been added in the `master` branch. If you share the branch with someone else, i.e. you are not working on it alone, then take care because rebasing obviously changes history; it may still be the right thing to do, 'cos the ground has indeed moved underneath you, history _has_ changed, but make sure that anyone else who is working on the branch with you is aware of what you have done when you push the branch back to the repo. + +# Beware The Wrap +Embedded systems usually have no better than a 32 bit millisecond tick count, i.e. a 32 bit counter that will wrap at 2^32 - 1, or 4,294,967,295, or about 50 days (25 days if treated as signed); the return value of the port layer `uPortGetTickTimeMs()` is a 32-bit integer for this reason. + +The systems that `ubxlib` is built into will need to be up for longer than 50 days, so the `ubxlib` code must behave well around such a wrap and, specifically, not unintentionally become stuck for 50 days if the tick counter happens to wrap while the code is waiting on it. For any timeouts or delays, **always** use the [uTimeout](/common/timeout/api/u_timeout.h) API, which defines an "anonymous" `uTimeoutStart_t` structure that can be populated with a call to `uTimeoutStart()` and then checked with the `uTimeoutExpiredMs()` or the `uTimeoutExpiredSeconds()` functions; these are designed to ensure that nothing will get stuck. + +# Be Explicit About Units +Where a number represents a quantity it will have a unit: seconds, milliseconds, nanoseconds, Volts, milliVolts, decibels, decibels relative to one milliWatt (dBm), words (as opposed to bytes), kilo-sheep, etc. You may recall the tale of the [Mars Climate Orbiter](https://en.wikipedia.org/wiki/Mars_Climate_Orbiter), a $327 million spacecraft that was lost because the NASA navigation software expected measurements in Newton-seconds while their contractor was providing measurements in pound-force seconds, a factor of 4.5 different; where a number represents a quantity, **be explicit** about the unit by including it in the variable/parameter name. For instance, presented with a variable/parameter named `timeout`, you could get things wrong by three orders of magnitude or more when applying that parameter, without realising it; naming it something like `timeoutMs` or `timeoutSeconds` will make the intended usage clear. \ No newline at end of file diff --git a/ble/src/gen2/u_ble_cfg_extmod.c b/ble/src/gen2/u_ble_cfg_extmod.c index 38529260..8c9098e3 100644 --- a/ble/src/gen2/u_ble_cfg_extmod.c +++ b/ble/src/gen2/u_ble_cfg_extmod.c @@ -40,6 +40,8 @@ #include "u_cfg_sw.h" #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range_module_type.h" diff --git a/ble/src/gen2/u_ble_gap_extmod.c b/ble/src/gen2/u_ble_gap_extmod.c index 14b32043..92dd34df 100644 --- a/ble/src/gen2/u_ble_gap_extmod.c +++ b/ble/src/gen2/u_ble_gap_extmod.c @@ -37,6 +37,7 @@ #include "stdlib.h" // strol(), atoi(), strol(), strtof() #include "string.h" // memset(), strncpy(), strtok_r(), strtol() #include "u_error_common.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_ble.h" #include "u_ble_cfg.h" diff --git a/ble/src/gen2/u_ble_gatt_extmod.c b/ble/src/gen2/u_ble_gatt_extmod.c index 6a84a878..cacfed6a 100644 --- a/ble/src/gen2/u_ble_gatt_extmod.c +++ b/ble/src/gen2/u_ble_gatt_extmod.c @@ -37,6 +37,7 @@ #include "stdlib.h" // strol(), atoi(), strol(), strtof() #include "string.h" // memset(), strncpy(), strtok_r(), strtol() #include "u_error_common.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_ble.h" #include "u_ble_cfg.h" diff --git a/ble/src/gen2/u_ble_sps_extmod.c b/ble/src/gen2/u_ble_sps_extmod.c index 09754e70..54565d34 100644 --- a/ble/src/gen2/u_ble_sps_extmod.c +++ b/ble/src/gen2/u_ble_sps_extmod.c @@ -45,6 +45,7 @@ #include "u_port_event_queue.h" #include "u_cfg_os_platform_specific.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_ble_sps.h" #include "u_ble_private.h" diff --git a/ble/src/u_ble_cfg_extmod.c b/ble/src/u_ble_cfg_extmod.c index 482e4dbd..b9db9a0e 100644 --- a/ble/src/u_ble_cfg_extmod.c +++ b/ble/src/u_ble_cfg_extmod.c @@ -40,6 +40,8 @@ #include "u_cfg_sw.h" #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range_module_type.h" diff --git a/ble/src/u_ble_gap_extmod.c b/ble/src/u_ble_gap_extmod.c index 323a70f2..ef98d24d 100644 --- a/ble/src/u_ble_gap_extmod.c +++ b/ble/src/u_ble_gap_extmod.c @@ -37,6 +37,7 @@ #include "stdlib.h" // strol(), atoi(), strol(), strtof() #include "string.h" // memset(), strncpy(), strtok_r(), strtol() #include "u_error_common.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_ble.h" #include "u_ble_cfg.h" diff --git a/ble/src/u_ble_gatt_extmod.c b/ble/src/u_ble_gatt_extmod.c index d8f90138..a7dcef00 100644 --- a/ble/src/u_ble_gatt_extmod.c +++ b/ble/src/u_ble_gatt_extmod.c @@ -37,6 +37,7 @@ #include "stdlib.h" // strol(), atoi(), strol(), strtof() #include "string.h" // memset(), strncpy(), strtok_r(), strtol() #include "u_error_common.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_ble.h" #include "u_ble_cfg.h" diff --git a/ble/src/u_ble_nus.c b/ble/src/u_ble_nus.c index f8817b92..dd62c88e 100644 --- a/ble/src/u_ble_nus.c +++ b/ble/src/u_ble_nus.c @@ -37,6 +37,7 @@ #include "stdlib.h" // strol(), atoi(), strol(), strtof() #include "string.h" // memset(), strncpy(), strtok_r(), strtol() #include "u_error_common.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_ble.h" #include "u_ble_cfg.h" diff --git a/ble/src/u_ble_sps_extmod.c b/ble/src/u_ble_sps_extmod.c index a2085085..a7803d7c 100644 --- a/ble/src/u_ble_sps_extmod.c +++ b/ble/src/u_ble_sps_extmod.c @@ -45,6 +45,7 @@ #include "u_port_event_queue.h" #include "u_cfg_os_platform_specific.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_ble_sps.h" #include "u_ble_private.h" diff --git a/ble/src/u_ble_sps_intmod.c b/ble/src/u_ble_sps_intmod.c index 324ccf93..5ab05bee 100644 --- a/ble/src/u_ble_sps_intmod.c +++ b/ble/src/u_ble_sps_intmod.c @@ -39,6 +39,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_device_shared.h" #include "u_cfg_sw.h" @@ -1232,14 +1234,12 @@ int32_t uBleSpsSend(uDeviceHandle_t devHandle, int32_t channel, const char *pDat return (int32_t)U_ERROR_COMMON_INVALID_PARAMETER; } - int64_t startTime = uPortGetTickTimeMs(); int32_t errorCode = (int32_t)U_ERROR_COMMON_SUCCESS; spsConnection_t *pSpsConn = pGetSpsConn(spsConnHandle); if (pSpsConn->spsState == SPS_STATE_CONNECTED) { - uint32_t timeout = pSpsConn->dataSendTimeoutMs; - int64_t time = startTime; - - while ((bytesLeftToSend > 0) && (time - startTime < timeout)) { + uint32_t timeoutMs = pSpsConn->dataSendTimeoutMs; + uTimeoutStart_t timeoutStart = uTimeoutStart(); + while ((bytesLeftToSend > 0) && !uTimeoutExpiredMs(timeoutStart, timeoutMs)) { int32_t bytesToSendNow = bytesLeftToSend; int32_t maxDataLength = pSpsConn->mtu - U_BLE_PDU_HEADER_SIZE; @@ -1253,13 +1253,11 @@ int32_t uBleSpsSend(uDeviceHandle_t devHandle, int32_t channel, const char *pDat // again later if we are out of credits. (void)uPortSemaphoreTryTake(pSpsConn->txCreditsSemaphore, 0); if (pSpsConn->txCredits == 0) { - int32_t timeoutLeft = (int32_t)timeout - (int32_t)(time - startTime); - if (timeoutLeft < 0) { - timeoutLeft = 0; - } + uint32_t elapsedMs = uTimeoutElapsedMs(timeoutStart); + uint32_t timeoutLeftMs = (elapsedMs >= timeoutMs) ? 0 : timeoutMs - elapsedMs; // We are out of credits, wait for more - if (uPortSemaphoreTryTake(pSpsConn->txCreditsSemaphore, timeoutLeft) != 0) { - uPortLog("U_BLE_SPS: SPS Timed out waiting for new TX credits!\n"); + if (uPortSemaphoreTryTake(pSpsConn->txCreditsSemaphore, timeoutLeftMs) != 0) { + uPortLog("U_BLE_SPS: SPS timed out waiting for new TX credits!\n"); break; } } @@ -1277,9 +1275,6 @@ int32_t uBleSpsSend(uDeviceHandle_t devHandle, int32_t channel, const char *pDat errorCode = (int32_t)U_ERROR_COMMON_UNKNOWN; break; } - if (bytesLeftToSend > 0) { - time = uPortGetTickTimeMs(); - } } } diff --git a/ble/test/u_ble_bond_test.c b/ble/test/u_ble_bond_test.c index 024ebf67..07bf43bf 100644 --- a/ble/test/u_ble_bond_test.c +++ b/ble/test/u_ble_bond_test.c @@ -64,6 +64,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range_pbuf.h" #include "u_short_range.h" diff --git a/cell/src/u_cell.c b/cell/src/u_cell.c index cc61ad9a..7ac89ce4 100644 --- a/cell/src/u_cell.c +++ b/cell/src/u_cell.c @@ -37,11 +37,14 @@ #include "u_error_common.h" +#include "u_port.h" #include "u_port_debug.h" #include "u_port_os.h" #include "u_port_heap.h" #include "u_port_gpio.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_ringbuffer.h" @@ -297,6 +300,8 @@ int32_t uCellAdd(uCellModuleType_t moduleType, pInstance->pinPwrOn = pinPwrOn; pInstance->pinVInt = pinVInt; pInstance->pinDtrPowerSaving = -1; + pInstance->lastCfunFlipTime = uTimeoutStart(); + pInstance->lastDtrPinToggleTime = uTimeoutStart(); for (size_t x = 0; x < sizeof(pInstance->networkStatus) / sizeof(pInstance->networkStatus[0]); x++) { diff --git a/cell/src/u_cell_cfg.c b/cell/src/u_cell_cfg.c index 5ee6d383..bf061dd8 100644 --- a/cell/src/u_cell_cfg.c +++ b/cell/src/u_cell_cfg.c @@ -46,6 +46,8 @@ #include "u_port_os.h" #include "u_port_heap.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/src/u_cell_file.c b/cell/src/u_cell_file.c index 1ca9b75b..8ecd02cc 100644 --- a/cell/src/u_cell_file.c +++ b/cell/src/u_cell_file.c @@ -39,6 +39,7 @@ #include "u_error_common.h" #include "u_port.h" #include "u_port_os.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_cell_module_type.h" #include "u_cell_net.h" diff --git a/cell/src/u_cell_fota.c b/cell/src/u_cell_fota.c index f4b41b3c..d6a21c08 100644 --- a/cell/src/u_cell_fota.c +++ b/cell/src/u_cell_fota.c @@ -41,6 +41,8 @@ #include "u_port_os.h" #include "u_port_heap.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/src/u_cell_geofence.c b/cell/src/u_cell_geofence.c index 69f9ee0c..7c108449 100644 --- a/cell/src/u_cell_geofence.c +++ b/cell/src/u_cell_geofence.c @@ -37,6 +37,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_linked_list.h" diff --git a/cell/src/u_cell_gpio.c b/cell/src/u_cell_gpio.c index 940639b3..635f248d 100644 --- a/cell/src/u_cell_gpio.c +++ b/cell/src/u_cell_gpio.c @@ -42,6 +42,8 @@ #include "u_port_os.h" #include "u_port_heap.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/src/u_cell_http.c b/cell/src/u_cell_http.c index ccbf950d..a03321c0 100644 --- a/cell/src/u_cell_http.c +++ b/cell/src/u_cell_http.c @@ -51,6 +51,8 @@ #include "u_port_debug.h" #include "u_port_event_queue.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock.h" diff --git a/cell/src/u_cell_info.c b/cell/src/u_cell_info.c index cc6d3fa8..a073b3ac 100644 --- a/cell/src/u_cell_info.c +++ b/cell/src/u_cell_info.c @@ -49,6 +49,8 @@ #include "u_port_os.h" #include "u_port_uart.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" diff --git a/cell/src/u_cell_loc.c b/cell/src/u_cell_loc.c index a5fdc252..7b989665 100644 --- a/cell/src/u_cell_loc.c +++ b/cell/src/u_cell_loc.c @@ -51,6 +51,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_time.h" #include "u_at_client.h" @@ -1748,7 +1750,7 @@ int32_t uCellLocGet(uDeviceHandle_t cellHandle, uCellPrivateLocContext_t *pContext; uCellLocFixDataStorage_t *pFixDataStorage; volatile uCellLocFixDataStorageBlock_t fixDataStorageBlock = {0}; - int64_t startTime; + uTimeoutStart_t timeoutStart; fixDataStorageBlock.errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; @@ -1795,10 +1797,10 @@ int32_t uCellLocGet(uDeviceHandle_t cellHandle, uPortLog("U_CELL_LOC: waiting for the answer...\n"); // Wait for the callback called by the URC to set // errorCode inside our block to success - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((fixDataStorageBlock.errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) && (((pKeepGoingCallback == NULL) && - (uPortGetTickTimeMs() - startTime) / 1000 < U_CELL_LOC_TIMEOUT_SECONDS) || + !uTimeoutExpiredSeconds(timeoutStart, U_CELL_LOC_TIMEOUT_SECONDS)) || ((pKeepGoingCallback != NULL) && pKeepGoingCallback(cellHandle)))) { // Relax a little uPortTaskBlock(1000); diff --git a/cell/src/u_cell_mno_db.c b/cell/src/u_cell_mno_db.c index e7c32024..7cce4096 100644 --- a/cell/src/u_cell_mno_db.c +++ b/cell/src/u_cell_mno_db.c @@ -34,6 +34,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_port.h" diff --git a/cell/src/u_cell_mqtt.c b/cell/src/u_cell_mqtt.c index d2128fa6..4db347e2 100644 --- a/cell/src/u_cell_mqtt.c +++ b/cell/src/u_cell_mqtt.c @@ -51,6 +51,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_hex_bin_convert.h" #include "u_at_client.h" @@ -1083,7 +1085,7 @@ static int32_t doSaraR4OldSyntaxUmqttQuery(const uCellPrivateInstance_t *pInstan uAtClientHandle_t atHandle = pInstance->atHandle; char buffer[13]; // Enough room for "AT+UMQTT=x?" int32_t status; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // The old SARA-R4 MQTT AT interface syntax gets very // peculiar here. @@ -1110,9 +1112,10 @@ static int32_t doSaraR4OldSyntaxUmqttQuery(const uCellPrivateInstance_t *pInstan // Wait for the URC to capture the answer // This is just a local thing so set a short timeout // and don't bother with keepGoingCallback - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((!checkUrcStatusField(pUrcStatus, number)) && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_MQTT_LOCAL_URC_TIMEOUT_MS)) { + !uTimeoutExpiredMs(timeoutStart, + U_CELL_MQTT_LOCAL_URC_TIMEOUT_MS)) { uPortTaskBlock(250); } if (checkUrcStatusField(pUrcStatus, number)) { @@ -1344,7 +1347,7 @@ static int32_t connect(const uCellPrivateInstance_t *pInstance, bool mqttSn; volatile uCellMqttUrcStatus_t *pUrcStatus; uAtClientHandle_t atHandle; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t status = 1; size_t tryCount = 0; @@ -1358,8 +1361,8 @@ static int32_t connect(const uCellPrivateInstance_t *pInstance, // take a little while to find out that the connection // has actually been made and hence we wait here for // it to be ready to connect - while (uPortGetTickTimeMs() - pInstance->connectedAtMs < - U_CELL_MQTT_CONNECT_DELAY_MILLISECONDS) { + while (!uTimeoutExpiredMs(pInstance->connectedAt, + U_CELL_MQTT_CONNECT_DELAY_MILLISECONDS)) { uPortTaskBlock(100); } } @@ -1410,17 +1413,18 @@ static int32_t connect(const uCellPrivateInstance_t *pInstance, " second(s)...\n", U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS); errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((pUrcStatus->flagsBitmap & (1 << U_CELL_MQTT_URC_FLAG_CONNECT_UPDATED)) == 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000) ) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); } if ((int32_t) onNotOff == pContext->connected) { - uPortLog("U_CELL_MQTT: %s after %d second(s).\n", + uPortLog("U_CELL_MQTT: %s after %u second(s).\n", onNotOff ? "connected" : "disconnected", - (uPortGetTickTimeMs() - startTimeMs) / 1000); + uTimeoutElapsedSeconds(timeoutStart)); errorCode = (int32_t) U_ERROR_COMMON_SUCCESS; } else { printErrorCodes(pInstance); @@ -1547,7 +1551,7 @@ static int32_t publish(const uCellPrivateInstance_t *pInstance, int32_t status = 1; bool isAscii; bool messageWritten = false; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t promptTimeoutSeconds = U_CELL_MQTT_PROMPT_TIMEOUT_NORMAL_SECONDS; size_t tryCount = 0; @@ -1710,9 +1714,10 @@ static int32_t publish(const uCellPrivateInstance_t *pInstance, // Wait for a URC to say that the publish // has succeeded errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((pUrcStatus->flagsBitmap & (1 << U_CELL_MQTT_URC_FLAG_PUBLISH_UPDATED)) == 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); @@ -1769,7 +1774,7 @@ static int32_t subscribe(const uCellPrivateInstance_t *pInstance, volatile uCellMqttUrcStatus_t *pUrcStatus; uAtClientHandle_t atHandle; int32_t status = 1; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t tryCount = 0; pContext = (volatile uCellMqttContext_t *) pInstance->pMqttContext; @@ -1827,9 +1832,10 @@ static int32_t subscribe(const uCellPrivateInstance_t *pInstance, // On all platforms need to wait for a URC to // say that the subscribe has succeeded errorCodeOrQos = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((pUrcStatus->flagsBitmap & (1 << U_CELL_MQTT_URC_FLAG_SUBSCRIBE_UPDATED)) == 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); @@ -1865,7 +1871,7 @@ static int32_t unsubscribe(const uCellPrivateInstance_t *pInstance, volatile uCellMqttUrcStatus_t *pUrcStatus; uAtClientHandle_t atHandle; int32_t status = 1; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t tryCount = 0; pContext = (volatile uCellMqttContext_t *) pInstance->pMqttContext; @@ -1915,9 +1921,10 @@ static int32_t unsubscribe(const uCellPrivateInstance_t *pInstance, // If this is the new syntax we need to wait // for a URC to say that the unsubscribe has succeeded errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((pUrcStatus->flagsBitmap & (1 << U_CELL_MQTT_URC_FLAG_UNSUBSCRIBE_UPDATED)) == 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); @@ -1954,7 +1961,7 @@ static int32_t readMessage(const uCellPrivateInstance_t *pInstance, uAtClientHandle_t atHandle; size_t messageSizeBytes = 0; int32_t status; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; uCellMqttQos_t qos; int32_t topicNameType = -1; int32_t topicNameBytesRead = -1; @@ -2004,9 +2011,10 @@ static int32_t readMessage(const uCellPrivateInstance_t *pInstance, if ((uAtClientUnlock(atHandle) == 0) && (status == 1)) { // Wait for a URC containing the message errorCode = (int32_t) U_ERROR_COMMON_EMPTY; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (!pUrcMessage->messageRead && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); @@ -3373,7 +3381,7 @@ int32_t uCellMqttSnRegisterNormalTopic(uDeviceHandle_t cellHandle, volatile uCellMqttContext_t *pContext; volatile uCellMqttUrcStatus_t *pUrcStatus; uAtClientHandle_t atHandle; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t tryCount = 0; U_CELL_MQTT_ENTRY_FUNCTION(cellHandle, &pInstance, &errorCode, true); @@ -3404,9 +3412,10 @@ int32_t uCellMqttSnRegisterNormalTopic(uDeviceHandle_t cellHandle, if (uAtClientUnlock(atHandle) == 0) { // Wait for a URC to get the ID errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((pUrcStatus->flagsBitmap & (1 << U_CELL_MQTT_URC_FLAG_REGISTER_UPDATED)) == 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); @@ -3641,7 +3650,7 @@ int32_t uCellMqttSnSetWillMessaage(uDeviceHandle_t cellHandle, volatile uCellMqttContext_t *pContext; volatile uCellMqttUrcStatus_t *pUrcStatus; uAtClientHandle_t atHandle; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t tryCount = 0; U_CELL_MQTT_ENTRY_FUNCTION(cellHandle, &pInstance, &errorCode, true); @@ -3673,9 +3682,10 @@ int32_t uCellMqttSnSetWillMessaage(uDeviceHandle_t cellHandle, if (uAtClientUnlock(atHandle) == 0) { // Wait for a URC to indicate success errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((pUrcStatus->flagsBitmap & (1 << U_CELL_MQTT_URC_FLAG_WILL_MESSAGE_UPDATED)) == 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); @@ -3709,7 +3719,7 @@ int32_t uCellMqttSnSetWillParameters(uDeviceHandle_t cellHandle, volatile uCellMqttContext_t *pContext; volatile uCellMqttUrcStatus_t *pUrcStatus; uAtClientHandle_t atHandle; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t tryCount = 0; U_CELL_MQTT_ENTRY_FUNCTION(cellHandle, &pInstance, &errorCode, true); @@ -3748,9 +3758,10 @@ int32_t uCellMqttSnSetWillParameters(uDeviceHandle_t cellHandle, if (uAtClientUnlock(atHandle) == 0) { // Wait for a URC to indicate success errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((pUrcStatus->flagsBitmap & (1 << U_CELL_MQTT_URC_FLAG_WILL_PARAMETERS_UPDATED)) == 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(timeoutStart, + U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback())) { uPortTaskBlock(1000); diff --git a/cell/src/u_cell_mux.c b/cell/src/u_cell_mux.c index 40cc5140..5a1d14b8 100644 --- a/cell/src/u_cell_mux.c +++ b/cell/src/u_cell_mux.c @@ -73,6 +73,8 @@ #include "u_interface.h" #include "u_ringbuffer.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" @@ -240,7 +242,7 @@ static int32_t sendEvent(uCellMuxPrivateContext_t *pContext, uCellMuxPrivateEventCallback_t *pEventCallback; uCellMuxEventTrampoline_t trampolineData; uint32_t eventCallbackFilter; - int64_t startTime = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); bool irqSupported; if ((pContext != NULL) && (pChannelContext != NULL) && !pChannelContext->markedForDeletion) { @@ -260,7 +262,7 @@ static int32_t sendEvent(uCellMuxPrivateContext_t *pContext, uPortTaskBlock(U_CFG_OS_YIELD_MS); irqSupported = (errorCode != (int32_t) U_ERROR_COMMON_NOT_IMPLEMENTED) && (errorCode != (int32_t) U_ERROR_COMMON_NOT_SUPPORTED); - } while (irqSupported && (uPortGetTickTimeMs() - startTime < delayMs)); + } while (irqSupported && !uTimeoutExpiredMs(timeoutStart, delayMs)); if (!irqSupported) { // If IRQ is not supported, just gotta do the normal send @@ -371,7 +373,7 @@ static int32_t serialWriteInnards(struct uDeviceSerial_t *pDeviceSerial, size_t sizeWritten = 0; int32_t thisLengthWritten; size_t lengthWritten; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; bool activityPinIsSet = false; // Encode the CMUX frame in chunks of the maximum information @@ -386,9 +388,9 @@ static int32_t serialWriteInnards(struct uDeviceSerial_t *pDeviceSerial, activityPinIsSet = true; uCellPrivateSetPinDtr(pInstance, true); } - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((sizeWritten < sizeBytes) && (sizeOrErrorCode >= 0) && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_MUX_WRITE_TIMEOUT_MS)) { + !uTimeoutExpiredMs(timeoutStart, U_CELL_MUX_WRITE_TIMEOUT_MS)) { // Encode a chunk as UIH thisChunkSize = sizeBytes - sizeWritten; if (thisChunkSize > U_CELL_MUX_PRIVATE_INFORMATION_LENGTH_MAX_BYTES) { @@ -401,7 +403,7 @@ static int32_t serialWriteInnards(struct uDeviceSerial_t *pDeviceSerial, if (sizeOrErrorCode >= 0) { lengthWritten = 0; while ((sizeOrErrorCode >= 0) && (lengthWritten < (size_t) sizeOrErrorCode) && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_MUX_WRITE_TIMEOUT_MS)) { + !uTimeoutExpiredMs(timeoutStart, U_CELL_MUX_WRITE_TIMEOUT_MS)) { if (!pChannelContext->traffic.txIsFlowControlledOff) { // Send the data thisLengthWritten = uPortUartWrite(pChannelContext->pContext->underlyingStreamHandle, @@ -497,7 +499,7 @@ static int32_t sendCommandCheckResponse(uDeviceSerial_t *pDeviceSerial, char buffer[U_CELL_MUX_PRIVATE_FRAME_OVERHEAD_MAX_BYTES + sizeof(pFrameSend->information)]; char *pTmp; int32_t length; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // Flush out any existing information field data while (serialReadInnards(pTraffic, buffer, sizeof(buffer)) > 0) {} @@ -521,9 +523,9 @@ static int32_t sendCommandCheckResponse(uDeviceSerial_t *pDeviceSerial, if (timeoutMs > 0) { errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; // Wait for a response - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((pTraffic->wantedResponseFrameType != U_CELL_MUX_PRIVATE_FRAME_TYPE_NONE) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs)) { + !uTimeoutExpiredMs(timeoutStart, timeoutMs)) { uPortTaskBlock(10); } if (pTraffic->wantedResponseFrameType == U_CELL_MUX_PRIVATE_FRAME_TYPE_NONE) { diff --git a/cell/src/u_cell_mux_private.c b/cell/src/u_cell_mux_private.c index 99adaded..9f7459ef 100644 --- a/cell/src/u_cell_mux_private.c +++ b/cell/src/u_cell_mux_private.c @@ -49,6 +49,8 @@ #include "u_ringbuffer.h" #include "u_interface.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_serial.h" diff --git a/cell/src/u_cell_net.c b/cell/src/u_cell_net.c index 4eb222a2..90804747 100644 --- a/cell/src/u_cell_net.c +++ b/cell/src/u_cell_net.c @@ -51,6 +51,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock.h" @@ -752,9 +754,8 @@ static bool keepGoingLocalCb(const uCellPrivateInstance_t *pInstance) if (pInstance->pKeepGoingCallback != NULL) { keepGoing = pInstance->pKeepGoingCallback(pInstance->cellHandle); } else { - if ((pInstance->startTimeMs > 0) && - (uPortGetTickTimeMs() - pInstance->startTimeMs > - (U_CELL_NET_CONNECT_TIMEOUT_SECONDS * 1000))) { + if (uTimeoutExpiredSeconds(pInstance->timeoutStart, + U_CELL_NET_CONNECT_TIMEOUT_SECONDS)) { keepGoing = false; } } @@ -774,8 +775,8 @@ static int32_t radioOff(uCellPrivateInstance_t *pInstance) pInstance->profileState = U_CELL_PRIVATE_PROFILE_STATE_SHOULD_BE_DOWN; for (size_t x = 3; (x > 0) && (errorCode < 0); x--) { // Wait for flip time to expire - while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs < - (U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) { + while (!uTimeoutExpiredSeconds(pInstance->lastCfunFlipTime, + U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS)) { uPortTaskBlock(1000); } uAtClientLock(atHandle); @@ -804,7 +805,7 @@ static int32_t radioOff(uCellPrivateInstance_t *pInstance) } if (errorCode == 0) { - pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs(); + pInstance->lastCfunFlipTime = uTimeoutStart(); } return errorCode; @@ -1246,8 +1247,8 @@ static int32_t registerNetwork(uCellPrivateInstance_t *pInstance, // Come out of airplane mode and try to register // Wait for flip time to expire first though - while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs < - (U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) { + while (!uTimeoutExpiredSeconds(pInstance->lastCfunFlipTime, + U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS)) { uPortTaskBlock(1000); } // Reset the current registration status @@ -1260,7 +1261,7 @@ static int32_t registerNetwork(uCellPrivateInstance_t *pInstance, uAtClientCommandStopReadResponse(atHandle); errorCode = uAtClientUnlock(atHandle); if ((errorCode == 0) && (pMccMnc != NULL)) { - pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs(); + pInstance->lastCfunFlipTime = uTimeoutStart(); // A network was given, so automatic // mode is not enough. In manual mode // the AT command does not return until @@ -1737,7 +1738,7 @@ static int32_t activateContextUpsd(const uCellPrivateInstance_t *pInstance, int32_t errorCode; uAtClientHandle_t atHandle = pInstance->atHandle; uAtClientDeviceError_t deviceError; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; bool activated = false; // SARA-U2 pattern: everything is done through AT+UPSD @@ -1813,7 +1814,7 @@ static int32_t activateContextUpsd(const uCellPrivateInstance_t *pInstance, uAtClientLock(atHandle); // Set timeout to 1 second and we can spin around // the loop - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); uAtClientTimeoutSet(atHandle, 1000); uAtClientCommandStart(atHandle, "AT+UPSDA="); uAtClientWriteInt(atHandle, profileId); @@ -1823,8 +1824,8 @@ static int32_t activateContextUpsd(const uCellPrivateInstance_t *pInstance, deviceError.type = U_AT_CLIENT_DEVICE_ERROR_TYPE_NO_ERROR; while (!activated && keepGoingLocalCb(pInstance) && (deviceError.type == U_AT_CLIENT_DEVICE_ERROR_TYPE_NO_ERROR) && - (uPortGetTickTimeMs() - startTimeMs < - (U_CELL_NET_UPSD_CONTEXT_ACTIVATION_TIME_SECONDS * 1000))) { + !uTimeoutExpiredSeconds(timeoutStart, + U_CELL_NET_UPSD_CONTEXT_ACTIVATION_TIME_SECONDS)) { uAtClientClearError(atHandle); uAtClientResponseStart(atHandle, NULL); activated = (uAtClientErrorGet(atHandle) == 0); @@ -2429,7 +2430,7 @@ int32_t uCellNetConnect(uDeviceHandle_t cellHandle, pApnConfig = pApnGetConfig(buffer); } pInstance->pKeepGoingCallback = pKeepGoingCallback; - pInstance->startTimeMs = uPortGetTickTimeMs(); + pInstance->timeoutStart = uTimeoutStart(); // Now try to connect, potentially multiple times do { if (pApnConfig != NULL) { @@ -2601,22 +2602,19 @@ int32_t uCellNetConnect(uDeviceHandle_t cellHandle, memcpy(pInstance->mccMnc, pMccMnc, sizeof(pInstance->mccMnc)); } pInstance->profileState = U_CELL_PRIVATE_PROFILE_STATE_SHOULD_BE_UP; - pInstance->connectedAtMs = uPortGetTickTimeMs(); - uPortLog("U_CELL_NET: connected after %d second(s).\n", - (int32_t) ((uPortGetTickTimeMs() - - pInstance->startTimeMs) / 1000)); + pInstance->connectedAt = uTimeoutStart(); + uPortLog("U_CELL_NET: connected after %u second(s).\n", + uTimeoutElapsedSeconds(pInstance->timeoutStart)); } else { // Switch radio off after failure radioOff(pInstance); uPortLog("U_CELL_NET: connection attempt stopped after" - " %d second(s).\n", - (int32_t) ((uPortGetTickTimeMs() - - pInstance->startTimeMs) / 1000)); + " %u second(s).\n", + uTimeoutElapsedSeconds(pInstance->timeoutStart)); } // Take away the callback again pInstance->pKeepGoingCallback = NULL; - pInstance->startTimeMs = 0; } } else { uPortLog("U_CELL_NET: already connected.\n"); @@ -2654,7 +2652,7 @@ int32_t uCellNetRegister(uDeviceHandle_t cellHandle, errorCode = prepareConnect(pInstance); if (errorCode == 0) { pInstance->pKeepGoingCallback = pKeepGoingCallback; - pInstance->startTimeMs = uPortGetTickTimeMs(); + pInstance->timeoutStart = uTimeoutStart(); if (pMccMnc == NULL) { // If no MCC/MNC is given, make sure we are in // automatic network selection mode @@ -2692,21 +2690,18 @@ int32_t uCellNetRegister(uDeviceHandle_t cellHandle, if (pMccMnc != NULL) { memcpy(pInstance->mccMnc, pMccMnc, sizeof(pInstance->mccMnc)); } - uPortLog("U_CELL_NET: registered after %d second(s).\n", - (int32_t) ((uPortGetTickTimeMs() - - pInstance->startTimeMs) / 1000)); + uPortLog("U_CELL_NET: registered after %u second(s).\n", + uTimeoutElapsedSeconds(pInstance->timeoutStart)); } else { // Switch radio off after failure radioOff(pInstance); uPortLog("U_CELL_NET: registration attempt stopped after" - " %d second(s).\n", - (int32_t) ((uPortGetTickTimeMs() - - pInstance->startTimeMs) / 1000)); + " %u second(s).\n", + uTimeoutElapsedSeconds(pInstance->timeoutStart)); } // Take away the callback again pInstance->pKeepGoingCallback = NULL; - pInstance->startTimeMs = 0; } } @@ -2758,7 +2753,7 @@ int32_t uCellNetActivate(uDeviceHandle_t cellHandle, if (errorCode != 0) { // No, get to work pInstance->pKeepGoingCallback = pKeepGoingCallback; - pInstance->startTimeMs = uPortGetTickTimeMs(); + pInstance->timeoutStart = uTimeoutStart(); if ((pApn == NULL) && (uCellPrivateGetImsi(pInstance, imsi) == 0)) { // Set up the APN look-up since none is specified @@ -2867,12 +2862,11 @@ int32_t uCellNetActivate(uDeviceHandle_t cellHandle, // Take away the callback again pInstance->pKeepGoingCallback = NULL; - pInstance->startTimeMs = 0; } if (errorCode == 0) { pInstance->profileState = U_CELL_PRIVATE_PROFILE_STATE_SHOULD_BE_UP; - pInstance->connectedAtMs = uPortGetTickTimeMs(); + pInstance->connectedAt = uTimeoutStart(); if (pApn != NULL) { uPortLog("U_CELL_NET: activated on APN \"%s\".\n", pApn); } else { @@ -3018,7 +3012,7 @@ int32_t uCellNetScanGetFirst(uDeviceHandle_t cellHandle, char *pBuffer; int32_t bytesRead; int32_t mode; - int64_t innerStartTimeMs; + uTimeoutStart_t innerTimeoutStart; uAtClientDeviceError_t deviceError; bool gotAnswer = false; char *pSaved; @@ -3056,7 +3050,7 @@ int32_t uCellNetScanGetFirst(uDeviceHandle_t cellHandle, // (,,,[,]) // it will be longer than that hence we set // a threshold for readBytes of > 12 characters. - pInstance->startTimeMs = uPortGetTickTimeMs(); + pInstance->timeoutStart = uTimeoutStart(); for (size_t x = U_CELL_NET_SCAN_RETRIES + 1; (x > 0) && (errorCodeOrNumber <= 0) && ((pKeepGoingCallback == NULL) || (pKeepGoingCallback(cellHandle))); @@ -3075,10 +3069,10 @@ int32_t uCellNetScanGetFirst(uDeviceHandle_t cellHandle, // Sit in a loop waiting for a response // of some form to arrive bytesRead = -1; - innerStartTimeMs = uPortGetTickTimeMs(); + innerTimeoutStart = uTimeoutStart(); while ((bytesRead <= 0) && - (uPortGetTickTimeMs() - innerStartTimeMs < - (U_CELL_NET_SCAN_TIME_SECONDS * 1000)) && + !uTimeoutExpiredSeconds(innerTimeoutStart, + U_CELL_NET_SCAN_TIME_SECONDS) && ((pKeepGoingCallback == NULL) || (pKeepGoingCallback(cellHandle)))) { uAtClientResponseStart(atHandle, "+COPS:"); // We use uAtClientReadBytes() here because the @@ -3213,7 +3207,7 @@ int32_t uCellNetDeepScan(uDeviceHandle_t cellHandle, bool keepGoing = true; int32_t number; int32_t cFunMode; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; if (gUCellPrivateMutex != NULL) { @@ -3232,10 +3226,11 @@ int32_t uCellNetDeepScan(uDeviceHandle_t cellHandle, // to do a network search, as it might be if // we've just come out of airplane mode, // it may return "Temporary Failure" - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); for (size_t x = U_CELL_NET_DEEP_SCAN_RETRIES + 1; (x > 0) && (errorCodeOrNumber < 0) && keepGoing && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_NET_DEEP_SCAN_TIME_SECONDS * 1000); + !uTimeoutExpiredSeconds(timeoutStart, + U_CELL_NET_DEEP_SCAN_TIME_SECONDS); x--) { number = 0; errorCodeOrNumber = (int32_t) U_ERROR_COMMON_TIMEOUT; @@ -3252,7 +3247,8 @@ int32_t uCellNetDeepScan(uDeviceHandle_t cellHandle, // want to be able to stop the command part way through, // hence the AT handling code below is more complex than usual. while ((errorCodeOrNumber == (int32_t) U_ERROR_COMMON_TIMEOUT) && keepGoing && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_NET_DEEP_SCAN_TIME_SECONDS * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, + U_CELL_NET_DEEP_SCAN_TIME_SECONDS)) { if (uAtClientResponseStart(atHandle, NULL) == 0) { // See if we have a line errorCodeOrNumber = parseDeepScanLine(atHandle, &cell); diff --git a/cell/src/u_cell_ppp.c b/cell/src/u_cell_ppp.c index bde2e615..1127ee09 100644 --- a/cell/src/u_cell_ppp.c +++ b/cell/src/u_cell_ppp.c @@ -53,6 +53,8 @@ #include "u_interface.h" #include "u_ringbuffer.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" @@ -229,7 +231,7 @@ static int32_t sendExpect(uCellPppContext_t *pContext, uDeviceHandle_t cellHandle = pContext->cellHandle; char buffer[64]; int32_t timeoutMs = timeoutSeconds * 1000; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t x = 0; int32_t y = 0; size_t startMatchOffset; @@ -246,9 +248,9 @@ static int32_t sendExpect(uCellPppContext_t *pContext, if (pResponse != NULL) { // Wait for a response to come back errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs) && + !uTimeoutExpiredMs(timeoutStart, timeoutMs) && ((pKeepGoingCallback == NULL) || (pKeepGoingCallback(cellHandle)))) { x = pDeviceSerial->read(pDeviceSerial, buffer + y, sizeof(buffer) - y); if (x > 0) { diff --git a/cell/src/u_cell_private.c b/cell/src/u_cell_private.c index 6de437e4..1a9b3596 100644 --- a/cell/src/u_cell_private.c +++ b/cell/src/u_cell_private.c @@ -45,6 +45,8 @@ #include "u_port_event_queue.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock.h" @@ -896,15 +898,15 @@ int32_t uCellPrivateCFunOne(uCellPrivateInstance_t *pInstance) // Set powered-up mode if it wasn't already if (errorCodeOrMode != 1) { // Wait for flip time to expire - while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs < - (U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) { + while (!uTimeoutExpiredSeconds(pInstance->lastCfunFlipTime, + U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS)) { uPortTaskBlock(1000); } uAtClientLock(atHandle); uAtClientCommandStart(atHandle, "AT+CFUN=1"); uAtClientCommandStopReadResponse(atHandle); if (uAtClientUnlock(atHandle) == 0) { - pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs(); + pInstance->lastCfunFlipTime = uTimeoutStart(); // And don't do anything for a second, // as the module might not be quite ready yet uPortTaskBlock(1000); @@ -921,8 +923,8 @@ void uCellPrivateCFunMode(uCellPrivateInstance_t *pInstance, uAtClientHandle_t atHandle = pInstance->atHandle; // Wait for flip time to expire - while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs < - (U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) { + while (!uTimeoutExpiredSeconds(pInstance->lastCfunFlipTime, + U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS)) { uPortTaskBlock(1000); } uAtClientLock(atHandle); @@ -937,7 +939,7 @@ void uCellPrivateCFunMode(uCellPrivateInstance_t *pInstance, uAtClientWriteInt(atHandle, mode); uAtClientCommandStopReadResponse(atHandle); if (uAtClientUnlock(atHandle) == 0) { - pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs(); + pInstance->lastCfunFlipTime = uTimeoutStart(); } } @@ -1530,12 +1532,12 @@ void uCellPrivateSetPinDtr(uCellPrivateInstance_t *pInstance, bool doNotPowerSav if (!doNotPowerSave) { targetState = !targetState; } - while (uPortGetTickTimeMs() - pInstance->lastDtrPinToggleTimeMs < - U_CELL_PWR_UART_POWER_SAVING_DTR_HYSTERESIS_MS) { + while (!uTimeoutExpiredMs(pInstance->lastDtrPinToggleTime, + U_CELL_PWR_UART_POWER_SAVING_DTR_HYSTERESIS_MS)) { uPortTaskBlock(U_CELL_PRIVATE_DTR_PIN_HYSTERESIS_INTERVAL_MS); } if (uPortGpioSet(pInstance->pinDtrPowerSaving, targetState) == 0) { - pInstance->lastDtrPinToggleTimeMs = uPortGetTickTimeMs(); + pInstance->lastDtrPinToggleTime = uTimeoutStart(); uPortTaskBlock(U_CELL_PWR_UART_POWER_SAVING_DTR_READY_MS); } } diff --git a/cell/src/u_cell_private.h b/cell/src/u_cell_private.h index 67a3dff0..ba4d0597 100644 --- a/cell/src/u_cell_private.h +++ b/cell/src/u_cell_private.h @@ -470,20 +470,20 @@ typedef struct uCellPrivateInstance_t { been requested (set to zeroes for automatic mode). */ - int64_t lastCfunFlipTimeMs; /**< The last time a flip of state from + uTimeoutStart_t lastCfunFlipTime; /**< The last time a flip of state from "off" (AT+CFUN=0/4) to "on" (AT+CFUN=1) or back was performed. */ - int32_t lastDtrPinToggleTimeMs; /**< The last time DTR was toggled for power-saving. */ + uTimeoutStart_t lastDtrPinToggleTime; /**< The last time DTR was toggled for power-saving. */ uCellNetStatus_t networkStatus[U_CELL_PRIVATE_NET_REG_TYPE_MAX_NUM]; /**< Registation status for each type, separating CREG, CGREG and CEREG. */ uCellNetRat_t rat[U_CELL_PRIVATE_NET_REG_TYPE_MAX_NUM]; /**< The active RAT for each registration type. */ int32_t lastEmmRejectCause; /**< Used by uCellNetGetLastEmmRejectCause() only. */ uCellPrivateRadioParameters_t radioParameters; /**< The radio parameters. */ - int32_t startTimeMs; /**< Used while connecting and scanning. */ - int32_t connectedAtMs; /**< When a connection was last established, - can be used for offsetting from that time; - does NOT mean that we are currently connected. */ + uTimeoutStart_t timeoutStart; /**< Used while connecting and scanning. */ + uTimeoutStart_t connectedAt; /**< When a connection was last established, + can be used for offsetting from that time; + does NOT mean that we are currently connected. */ bool rebootIsRequired; /**< Set to true if a reboot of the module is required, e.g. as a result of a configuration change. */ diff --git a/cell/src/u_cell_pwr.c b/cell/src/u_cell_pwr.c index c8931835..f45c4440 100644 --- a/cell/src/u_cell_pwr.c +++ b/cell/src/u_cell_pwr.c @@ -48,6 +48,8 @@ #include "u_ringbuffer.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -1135,8 +1137,8 @@ static int32_t moduleConfigure(uCellPrivateInstance_t *pInstance, if (andRadioOff) { // Switch the radio off until commanded to connect // Wait for flip time to expire - while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs < - (U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) { + while (!uTimeoutExpiredSeconds(pInstance->lastCfunFlipTime, + U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS)) { uPortTaskBlock(1000); } uAtClientLock(atHandle); @@ -1147,7 +1149,7 @@ static int32_t moduleConfigure(uCellPrivateInstance_t *pInstance, pInstance->pModule->radioOffCfun); uAtClientCommandStopReadResponse(atHandle); if (uAtClientUnlock(atHandle) == 0) { - pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs(); + pInstance->lastCfunFlipTime = uTimeoutStart(); errorCode = (int32_t) U_ERROR_COMMON_SUCCESS; } } else { @@ -1164,10 +1166,11 @@ static void waitForPowerOff(uCellPrivateInstance_t *pInstance, { uAtClientHandle_t atHandle = pInstance->atHandle; bool moduleIsOff = false; - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); while (!moduleIsOff && - (uPortGetTickTimeMs() - startTimeMs < pInstance->pModule->powerDownWaitSeconds * 1000) && + !uTimeoutExpiredSeconds(timeoutStart, + pInstance->pModule->powerDownWaitSeconds) && ((pKeepGoingCallback == NULL) || pKeepGoingCallback(pInstance->cellHandle))) { if (pInstance->pinVInt >= 0) { // If we have a VInt pin then wait until that @@ -2250,8 +2253,8 @@ int32_t uCellPwrReboot(uDeviceHandle_t cellHandle, if (pInstance != NULL) { uPortLog("U_CELL_PWR: rebooting.\n"); // Wait for flip time to expire - while (uPortGetTickTimeMs() - pInstance->lastCfunFlipTimeMs < - (U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS * 1000)) { + while (!uTimeoutExpiredSeconds(pInstance->lastCfunFlipTime, + U_CELL_PRIVATE_AT_CFUN_FLIP_DELAY_SECONDS)) { uPortTaskBlock(1000); } // Sleep is no longer available @@ -2347,7 +2350,7 @@ int32_t uCellPwrResetHard(uDeviceHandle_t cellHandle, int32_t pinReset) uCellPrivateInstance_t *pInstance; int32_t platformError; uPortGpioConfig_t gpioConfig; - int64_t startTime; + uTimeoutStart_t timeoutStart; int32_t resetHoldMilliseconds; int32_t pinResetToggleToState = (pinReset & U_CELL_PIN_INVERTED) ? !U_CELL_RESET_PIN_TOGGLE_TO_STATE : U_CELL_RESET_PIN_TOGGLE_TO_STATE; @@ -2403,8 +2406,8 @@ int32_t uCellPwrResetHard(uDeviceHandle_t cellHandle, int32_t pinReset) if (platformError == 0) { // We have rebooted pInstance->rebootIsRequired = false; - startTime = uPortGetTickTimeMs(); - while (uPortGetTickTimeMs() - startTime < resetHoldMilliseconds) { + timeoutStart = uTimeoutStart(); + while (!uTimeoutExpiredMs(timeoutStart, resetHoldMilliseconds)) { uPortTaskBlock(100); } // Set the pin back to the "non RESET" state @@ -2415,7 +2418,7 @@ int32_t uCellPwrResetHard(uDeviceHandle_t cellHandle, int32_t pinReset) // Wait for the module to boot uPortTaskBlock(pInstance->pModule->rebootCommandWaitSeconds * 1000); // Wait for the module to return to life and configure it - pInstance->lastCfunFlipTimeMs = uPortGetTickTimeMs(); + pInstance->lastCfunFlipTime = uTimeoutStart(); errorCode = uCellPwrPrivateIsAlive(pInstance, U_CELL_PWR_IS_ALIVE_ATTEMPTS_POWER_ON); if (errorCode == 0) { diff --git a/cell/src/u_cell_sec.c b/cell/src/u_cell_sec.c index 6aef65c1..4611c697 100644 --- a/cell/src/u_cell_sec.c +++ b/cell/src/u_cell_sec.c @@ -39,6 +39,8 @@ #include "u_port_os.h" #include "u_port_heap.h" +#include "u_timeout.h" + #include "u_hex_bin_convert.h" #include "u_at_client.h" diff --git a/cell/src/u_cell_sec_tls.c b/cell/src/u_cell_sec_tls.c index 026f8274..f156feae 100644 --- a/cell/src/u_cell_sec_tls.c +++ b/cell/src/u_cell_sec_tls.c @@ -41,6 +41,8 @@ #include "u_port_os.h" #include "u_port_heap.h" +#include "u_timeout.h" + #include "u_hex_bin_convert.h" #include "u_at_client.h" diff --git a/cell/src/u_cell_sim.c b/cell/src/u_cell_sim.c index 32c1c733..968095a5 100644 --- a/cell/src/u_cell_sim.c +++ b/cell/src/u_cell_sim.c @@ -42,6 +42,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_hex_bin_convert.h" diff --git a/cell/src/u_cell_sock.c b/cell/src/u_cell_sock.c index 3c6de8a5..8fbed4d9 100644 --- a/cell/src/u_cell_sock.c +++ b/cell/src/u_cell_sock.c @@ -43,6 +43,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_hex_bin_convert.h" @@ -1970,7 +1972,7 @@ int32_t uCellSockGetHostByName(uDeviceHandle_t cellHandle, int32_t bytesRead = 0; char buffer[U_SOCK_ADDRESS_STRING_MAX_LENGTH_BYTES]; uSockAddress_t address; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t tries = 0; memset(&address, 0, sizeof(address)); @@ -1991,17 +1993,17 @@ int32_t uCellSockGetHostByName(uDeviceHandle_t cellHandle, // instead of "+UDNSRN" (i.e. it adds an extra "U") // so, if parsing for the correct response fails and // we're on LENA-R8 then allow one retry. - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (((atError < 0) || (bytesRead <= 0)) && - ((uPortGetTickTimeMs() - startTimeMs < - U_CELL_SOCK_DNS_SHOULD_RETRY_MS) || + (!uTimeoutExpiredMs(timeoutStart, + U_CELL_SOCK_DNS_SHOULD_RETRY_MS) || ((pInstance->pModule->moduleType == U_CELL_MODULE_TYPE_LENA_R8) && (tries < 2)))) { if (pInstance->pModule->moduleType == U_CELL_MODULE_TYPE_SARA_R422) { // SARA-R422 can get upset if UDNSRN is sent very quickly // after a connection is made so we add a short delay here - while (uPortGetTickTimeMs() - pInstance->connectedAtMs < - U_CELL_SOCK_SARA_R422_DNS_DELAY_MILLISECONDS) { + while (!uTimeoutExpiredMs(pInstance->connectedAt, + U_CELL_SOCK_SARA_R422_DNS_DELAY_MILLISECONDS)) { uPortTaskBlock(100); } } diff --git a/cell/src/u_cell_time.c b/cell/src/u_cell_time.c index e9e6465e..aafb0429 100644 --- a/cell/src/u_cell_time.c +++ b/cell/src/u_cell_time.c @@ -49,6 +49,8 @@ #include "u_port_uart.h" #include "u_port_ppp.h" +#include "u_timeout.h" + #include "u_time.h" #include "u_at_client.h" @@ -681,7 +683,7 @@ int32_t uCellTimeSyncCellEnable(uDeviceHandle_t cellHandle, uCellTimeCellSyncPrivateContext_t *pContext; uAtClientHandle_t atHandle; char buffer[7]; // Enough room for MCC/MNC plus a null terminator - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; if (gUCellPrivateMutex != NULL) { @@ -733,10 +735,11 @@ int32_t uCellTimeSyncCellEnable(uDeviceHandle_t cellHandle, errorCode = uAtClientUnlock(atHandle); if (errorCode == 0) { // Wait for the URC for the outcome - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; while ((pContext->errorCode == INT_MIN) && - (uPortGetTickTimeMs() - startTimeMs < (U_CELL_TIME_SYNC_TIME_SECONDS * 1000))) { + !uTimeoutExpiredSeconds(timeoutStart, + U_CELL_TIME_SYNC_TIME_SECONDS)) { uPortTaskBlock(1000); } if (pContext->errorCode != INT_MIN) { @@ -770,7 +773,7 @@ int32_t uCellTimeSyncCellDisable(uDeviceHandle_t cellHandle) uCellPrivateInstance_t *pInstance; uCellTimeCellSyncPrivateContext_t *pContext; uAtClientHandle_t atHandle; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; if (gUCellPrivateMutex != NULL) { @@ -795,10 +798,11 @@ int32_t uCellTimeSyncCellDisable(uDeviceHandle_t cellHandle) errorCode = uAtClientUnlock(atHandle); if (errorCode == 0) { // Have to wait for the URC for the outcome - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; while ((pContext->errorCode != (int32_t) U_ERROR_COMMON_CANCELLED) && - (uPortGetTickTimeMs() - startTimeMs < (U_CELL_TIME_SYNC_TIME_SECONDS * 1000))) { + !uTimeoutExpiredSeconds(timeoutStart, + U_CELL_TIME_SYNC_TIME_SECONDS)) { uPortTaskBlock(1000); } if (pContext->errorCode != INT_MIN) { diff --git a/cell/test/u_cell_cfg_test.c b/cell/test/u_cell_cfg_test.c index 030b4723..86bc74d8 100644 --- a/cell/test/u_cell_cfg_test.c +++ b/cell/test/u_cell_cfg_test.c @@ -56,6 +56,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -136,7 +138,7 @@ /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** The GNSS profile bit map. */ @@ -157,7 +159,8 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -756,8 +759,8 @@ U_PORT_TEST_FUNCTION("[cellCfg]", "cellCfgGetSetMnoProfile") if (U_CELL_PRIVATE_HAS(pModule, U_CELL_PRIVATE_FEATURE_MNO_PROFILE)) { U_TEST_PRINT_LINE("trying to set MNO profile while connected..."); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, NULL, keepGoingCallback) == 0); U_PORT_TEST_ASSERT(uCellNetIsRegistered(cellHandle)); diff --git a/cell/test/u_cell_file_test.c b/cell/test/u_cell_file_test.c index 1de9dd56..311b5711 100644 --- a/cell/test/u_cell_file_test.c +++ b/cell/test/u_cell_file_test.c @@ -56,6 +56,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/test/u_cell_fota_test.c b/cell/test/u_cell_fota_test.c index 284b45d8..a7f83dbb 100644 --- a/cell/test/u_cell_fota_test.c +++ b/cell/test/u_cell_fota_test.c @@ -52,6 +52,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/test/u_cell_geofence_test.c b/cell/test/u_cell_geofence_test.c index fe344da7..35374ac1 100644 --- a/cell/test/u_cell_geofence_test.c +++ b/cell/test/u_cell_geofence_test.c @@ -51,6 +51,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_location.h" @@ -144,7 +146,7 @@ static uGeofence_t *gpFenceB = NULL; /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Variable to track the errors in the callback. */ @@ -176,7 +178,8 @@ static bool keepGoingCallback(uDeviceHandle_t cellHandle) bool keepGoing = true; U_PORT_TEST_ASSERT(cellHandle == gHandles.cellHandle); - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -267,7 +270,6 @@ U_PORT_TEST_FUNCTION("[cellGeofence]", "cellGeofenceLive") #if defined(U_CFG_APP_CELL_LOC_AUTHENTICATION_TOKEN) && defined(U_CFG_TEST_CELL_GEOFENCE) uDeviceHandle_t cellHandle; int32_t resourceCount; - int32_t startTime; int32_t x; size_t badStatusCount; @@ -313,7 +315,8 @@ U_PORT_TEST_FUNCTION("[cellGeofence]", "cellGeofenceLive") U_PORT_TEST_ASSERT(x == 0); // Make sure we are connected to a network - gStopTimeMs = uPortGetTickTimeMs() + (U_CELL_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; x = uCellNetConnect(cellHandle, NULL, # ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -367,8 +370,8 @@ U_PORT_TEST_FUNCTION("[cellGeofence]", "cellGeofenceLive") // Get position, blocking version U_TEST_PRINT_LINE("cell locate, blocking version."); - startTime = uPortGetTickTimeMs(); - gStopTimeMs = startTime + U_CELL_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; x = uCellLocGet(cellHandle, NULL, NULL, NULL, NULL, NULL, NULL, NULL, keepGoingCallback); U_TEST_PRINT_LINE("result was %d, gErrorCode was %d.", x, gErrorCode); @@ -376,8 +379,8 @@ U_PORT_TEST_FUNCTION("[cellGeofence]", "cellGeofenceLive") gpPositionStateString[gPositionStateA], gpPositionStateString[gPositionStateB]); if (x == 0) { - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTime) / 1000); + U_TEST_PRINT_LINE("location establishment took %u second(s).", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); } U_PORT_TEST_ASSERT(x == 0); @@ -394,14 +397,15 @@ U_PORT_TEST_FUNCTION("[cellGeofence]", "cellGeofenceLive") for (int32_t y = 3; (y > 0) && (gErrorCode == 0); y--) { gPositionStateA = U_GEOFENCE_POSITION_STATE_NONE; gPositionStateB = U_GEOFENCE_POSITION_STATE_NONE; - gStopTimeMs = startTime + U_CELL_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; - startTime = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellLocGetStart(cellHandle, posCallback) == 0); - U_TEST_PRINT_LINE("waiting up to %d second(s) for results from asynchonous API...", - U_CELL_GEOFENCE_TEST_TIMEOUT_SECONDS); + U_TEST_PRINT_LINE("waiting up to %u second(s) for results from asynchonous API...", + gTimeoutStop.durationMs / 1000); badStatusCount = 0; while ((gErrorCode >= 0) && (gErrorCode < 2) && - (uPortGetTickTimeMs() < gStopTimeMs) && + (!uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) && (badStatusCount < U_CELL_GEOFENCE_TEST_BAD_STATUS_LIMIT)) { x = uCellLocGetStatus(cellHandle); U_PORT_TEST_ASSERT((x >= U_LOCATION_STATUS_UNKNOWN) && @@ -417,8 +421,8 @@ U_PORT_TEST_FUNCTION("[cellGeofence]", "cellGeofenceLive") uPortTaskBlock(1000); } if (gErrorCode == 2) { - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTime) / 1000); + U_TEST_PRINT_LINE("location establishment took %u second(s).", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); U_TEST_PRINT_LINE("result was %d, gErrorCode was %d.", x, gErrorCode); U_TEST_PRINT_LINE("%s fence A, %s fence B.", gpPositionStateString[gPositionStateA], diff --git a/cell/test/u_cell_gpio_test.c b/cell/test/u_cell_gpio_test.c index 7dd77354..d618f63a 100644 --- a/cell/test/u_cell_gpio_test.c +++ b/cell/test/u_cell_gpio_test.c @@ -54,6 +54,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/test/u_cell_http_test.c b/cell/test/u_cell_http_test.c index c2a73bfc..4d980eaa 100644 --- a/cell/test/u_cell_http_test.c +++ b/cell/test/u_cell_http_test.c @@ -62,6 +62,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -131,7 +133,7 @@ typedef struct { /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Handles. */ @@ -183,7 +185,8 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -286,18 +289,18 @@ static void callback(uDeviceHandle_t cellHandle, int32_t httpHandle, } // Check an HTTP response, return true if it is good, else false. -static bool waitCheckHttpResponse(int32_t timeoutSeconds, +static bool waitCheckHttpResponse(uint32_t timeoutSeconds, volatile uCellHttpTestCallback_t *pCallbackData, uDeviceHandle_t cellHandle, int32_t httpHandle, uCellHttpRequest_t requestType, const char *pFileNameResponse) { bool isOk = false; - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); - U_TEST_PRINT_LINE("waiting up to %d second(s) for response to request type %s...", + U_TEST_PRINT_LINE("waiting up to %u second(s) for response to request type %s...", timeoutSeconds, pHttpRequestTypeStr(requestType)); - while ((uPortGetTickTimeMs() - startTimeMs < (timeoutSeconds * 1000)) && + while (!uTimeoutExpiredSeconds(timeoutStart, timeoutSeconds) && !pCallbackData->called) { uPortTaskBlock(100); } @@ -305,8 +308,8 @@ static bool waitCheckHttpResponse(int32_t timeoutSeconds, if (pCallbackData->called) { isOk = true; // The callback was called, check everything - U_TEST_PRINT_LINE("response received after %d millisecond(s).", - uPortGetTickTimeMs() - startTimeMs); + U_TEST_PRINT_LINE("response received after %u millisecond(s).", + uTimeoutElapsedMs(timeoutStart)); if (pCallbackData->cellHandle != cellHandle) { U_TEST_PRINT_LINE("expected cell handle 0x%08x, got 0x%08x.", cellHandle, pCallbackData->cellHandle); @@ -344,8 +347,8 @@ static bool waitCheckHttpResponse(int32_t timeoutSeconds, isOk = false; } } else { - U_TEST_PRINT_LINE("callback not called after %d second(s).", - (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("callback not called after %u second(s).", + uTimeoutElapsedSeconds(timeoutStart)); } // Reset for next time @@ -408,8 +411,8 @@ U_PORT_TEST_FUNCTION("[cellHttp]", "cellHttp") // Make a cellular connection, since we will need to do a // DNS look-up on the HTTP server domain name - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; y = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), diff --git a/cell/test/u_cell_info_test.c b/cell/test/u_cell_info_test.c index 51af6ecc..a87a70da 100644 --- a/cell/test/u_cell_info_test.c +++ b/cell/test/u_cell_info_test.c @@ -54,6 +54,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -102,7 +104,7 @@ /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Handles. */ @@ -119,7 +121,8 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -340,8 +343,8 @@ U_PORT_TEST_FUNCTION("[cellInfo]", "cellInfoRadioParameters") } U_TEST_PRINT_LINE("checking values after registration..."); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, NULL, keepGoingCallback) == 0); U_PORT_TEST_ASSERT(uCellNetIsRegistered(cellHandle)); @@ -432,8 +435,8 @@ U_PORT_TEST_FUNCTION("[cellInfo]", "cellInfoTime") cellHandle = gHandles.cellHandle; U_TEST_PRINT_LINE("registering to check the time..."); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, NULL, keepGoingCallback) == 0); U_TEST_PRINT_LINE("fetching the UTC time..."); diff --git a/cell/test/u_cell_loc_test.c b/cell/test/u_cell_loc_test.c index be0975ca..af1c8757 100644 --- a/cell/test/u_cell_loc_test.c +++ b/cell/test/u_cell_loc_test.c @@ -57,6 +57,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #ifdef U_CFG_TEST_GNSS_MODULE_TYPE @@ -196,7 +198,7 @@ static const char *gpAssistNowDataType[] = {"U_GNSS_MGA_DATA_TYPE_EPHEMERIS", /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Cell handle as seen by posCallback(). */ @@ -248,7 +250,8 @@ static bool keepGoingCallback(uDeviceHandle_t param) (void) param; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -653,7 +656,6 @@ U_PORT_TEST_FUNCTION("[cellLoc]", "cellLocLoc") #if defined(U_CFG_APP_CELL_LOC_AUTHENTICATION_TOKEN) && defined(U_CFG_TEST_CELL_LOCATE) uDeviceHandle_t cellHandle; int32_t resourceCount; - int64_t startTime; int32_t latitudeX1e7 = INT_MIN; int32_t longitudeX1e7 = INT_MIN; int32_t altitudeMillimetres = INT_MIN; @@ -709,7 +711,8 @@ U_PORT_TEST_FUNCTION("[cellLoc]", "cellLocLoc") U_PORT_TEST_ASSERT(x == 0); // Make sure we are connected to a network - gStopTimeMs = uPortGetTickTimeMs() + (U_CELL_LOC_TEST_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_LOC_TEST_TIMEOUT_SECONDS * 1000; x = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -731,8 +734,8 @@ U_PORT_TEST_FUNCTION("[cellLoc]", "cellLocLoc") // Get position, blocking version U_TEST_PRINT_LINE("location establishment, blocking version."); - startTime = uPortGetTickTimeMs(); - gStopTimeMs = startTime + U_CELL_LOC_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_LOC_TEST_TIMEOUT_SECONDS * 1000; x = uCellLocGet(cellHandle, &latitudeX1e7, &longitudeX1e7, &altitudeMillimetres, &radiusMillimetres, &speedMillimetresPerSecond, &svs, @@ -741,8 +744,8 @@ U_PORT_TEST_FUNCTION("[cellLoc]", "cellLocLoc") // If we are running on a cellular test network we won't get position but // we should always get time if (x == 0) { - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTime) / 1000); + U_TEST_PRINT_LINE("location establishment took %u second(s).", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); if ((radiusMillimetres > 0) && (radiusMillimetres <= U_CELL_LOC_TEST_MAX_RADIUS_MILLIMETRES)) { prefix[0] = latLongToBits(latitudeX1e7, &(whole[0]), &(fraction[0])); @@ -774,13 +777,15 @@ U_PORT_TEST_FUNCTION("[cellLoc]", "cellLocLoc") // location again quickly after returning an answer for (int32_t y = 3; (y > 0) && (gErrorCode != 0); y--) { gErrorCode = 0xFFFFFFFF; - gStopTimeMs = startTime + U_CELL_LOC_TEST_TIMEOUT_SECONDS * 1000; - startTime = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_LOC_TEST_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellLocGetStart(cellHandle, posCallback) == 0); - U_TEST_PRINT_LINE("waiting up to %d second(s) for results from asynchonous API...", - U_CELL_LOC_TEST_TIMEOUT_SECONDS); + U_TEST_PRINT_LINE("waiting up to %u second(s) for results from asynchonous API...", + gTimeoutStop.durationMs / 1000); badStatusCount = 0; - while ((gErrorCode == 0xFFFFFFFF) && (uPortGetTickTimeMs() < gStopTimeMs) && + while ((gErrorCode == 0xFFFFFFFF) && + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs) && (badStatusCount < U_CELL_LOC_TEST_BAD_STATUS_LIMIT)) { x = uCellLocGetStatus(cellHandle); U_PORT_TEST_ASSERT((x >= U_LOCATION_STATUS_UNKNOWN) && @@ -799,16 +804,14 @@ U_PORT_TEST_FUNCTION("[cellLoc]", "cellLocLoc") // If we are running on a cellular test network we won't get position but // we should always get time if (gErrorCode == 0) { - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTime) / 1000); + U_TEST_PRINT_LINE("location establishment took %u second(s).", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); U_PORT_TEST_ASSERT(gCellHandle == cellHandle); if ((radiusMillimetres > 0) && (radiusMillimetres <= U_CELL_LOC_TEST_MAX_RADIUS_MILLIMETRES)) { x = uCellLocGetStatus(cellHandle); U_PORT_TEST_ASSERT((x >= U_LOCATION_STATUS_UNKNOWN) && (x < U_LOCATION_STATUS_MAX_NUM)); - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTime) / 1000); U_PORT_TEST_ASSERT(gLatitudeX1e7 > INT_MIN); U_PORT_TEST_ASSERT(gLongitudeX1e7 > INT_MIN); U_PORT_TEST_ASSERT(gAltitudeMillimetres > INT_MIN); diff --git a/cell/test/u_cell_mqtt_test.c b/cell/test/u_cell_mqtt_test.c index fe6927ba..205b98ce 100644 --- a/cell/test/u_cell_mqtt_test.c +++ b/cell/test/u_cell_mqtt_test.c @@ -57,6 +57,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -128,7 +130,7 @@ /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Generic handles. */ @@ -162,7 +164,8 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -219,8 +222,8 @@ U_PORT_TEST_FUNCTION("[cellMqtt]", "cellMqtt") // Make a cellular connection, since we will need to do a // DNS look-up on the MQTT broker domain name - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; x = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -534,8 +537,8 @@ U_PORT_TEST_FUNCTION("[cellMqtt]", "cellMqttSn") // Make a cellular connection, since we will need to do a // DNS look-up on the MQTT-SN broker domain name - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; x = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), diff --git a/cell/test/u_cell_mux_private_test.c b/cell/test/u_cell_mux_private_test.c index cc11a762..4fe9626d 100644 --- a/cell/test/u_cell_mux_private_test.c +++ b/cell/test/u_cell_mux_private_test.c @@ -46,6 +46,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_ringbuffer.h" diff --git a/cell/test/u_cell_mux_test.c b/cell/test/u_cell_mux_test.c index ddec85bd..c5ac54e4 100644 --- a/cell/test/u_cell_mux_test.c +++ b/cell/test/u_cell_mux_test.c @@ -64,6 +64,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock.h" @@ -168,7 +170,7 @@ typedef struct { /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Handles. */ @@ -226,7 +228,8 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -248,8 +251,8 @@ static void printBuffer(const char *pBuffer, size_t length) // Make a cellular connection static int32_t connect(uDeviceHandle_t cellHandle) { - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; return uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -372,18 +375,18 @@ static void httpCallback(uDeviceHandle_t cellHandle, int32_t httpHandle, } // Check an HTTP response, return true if it is good, else false. -static bool httpWaitCheckResponse(int32_t timeoutSeconds, +static bool httpWaitCheckResponse(uint32_t timeoutSeconds, volatile uCellMuxHttpTestCallback_t *pCallbackData, uDeviceHandle_t cellHandle, int32_t httpHandle, uCellHttpRequest_t requestType, const char *pFileNameResponse) { bool isOk = false; - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); - U_TEST_PRINT_LINE("waiting up to %d second(s) for response to HTTP request...", + U_TEST_PRINT_LINE("waiting up to %u second(s) for response to HTTP request...", timeoutSeconds); - while ((uPortGetTickTimeMs() - startTimeMs < (timeoutSeconds * 1000)) && + while (!uTimeoutExpiredSeconds(timeoutStart, timeoutSeconds) && !pCallbackData->called) { uPortTaskBlock(100); } @@ -391,8 +394,8 @@ static bool httpWaitCheckResponse(int32_t timeoutSeconds, if (pCallbackData->called) { isOk = true; // The callback was called, check everything - U_TEST_PRINT_LINE("response received after %d millisecond(s).", - uPortGetTickTimeMs() - startTimeMs); + U_TEST_PRINT_LINE("response received after %u millisecond(s).", + uTimeoutElapsedMs(timeoutStart)); if (pCallbackData->cellHandle != cellHandle) { U_TEST_PRINT_LINE("expected cell handle 0x%08x, got 0x%08x.", cellHandle, pCallbackData->cellHandle); @@ -428,8 +431,8 @@ static bool httpWaitCheckResponse(int32_t timeoutSeconds, isOk = false; } } else { - U_TEST_PRINT_LINE("callback not called after %d second(s).", - (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("callback not called after %u second(s).", + uTimeoutElapsedSeconds(timeoutStart)); } // Reset for next time @@ -724,7 +727,7 @@ U_PORT_TEST_FUNCTION("[cellMux]", "cellMuxMqtt") char topic[U_CELL_INFO_IMEI_SIZE + 1] = {0}; char *pMessageIn; char *pTopicStrIn; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t messageSize = sizeof(gMqttSendData) - 1; uCellMqttQos_t qos; bool uartSleepWasEnabled; @@ -803,7 +806,7 @@ U_PORT_TEST_FUNCTION("[cellMux]", "cellMuxMqtt") U_CELL_MQTT_QOS_AT_MOST_ONCE) == 0); U_TEST_PRINT_LINE("publishing \"%s\" to topic \"%s\"...", gMqttSendData, topic); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); gMqttMessagesAvailable = 0; U_PORT_TEST_ASSERT(uCellMqttPublish(cellHandle, topic, gMqttSendData, sizeof(gMqttSendData) - 1, @@ -813,7 +816,8 @@ U_PORT_TEST_FUNCTION("[cellMux]", "cellMuxMqtt") U_TEST_PRINT_LINE("waiting %d second(s) for message to be sent back...", U_CELL_MUX_TEST_MQTT_RESPONSE_TIMEOUT_MS); while ((gMqttMessagesAvailable == 0) && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_MUX_TEST_MQTT_RESPONSE_TIMEOUT_MS)) { + !uTimeoutExpiredMs(timeoutStart, + U_CELL_MUX_TEST_MQTT_RESPONSE_TIMEOUT_MS)) { uPortTaskBlock(1000); } diff --git a/cell/test/u_cell_net_test.c b/cell/test/u_cell_net_test.c index 7141f835..7a73588b 100644 --- a/cell/test/u_cell_net_test.c +++ b/cell/test/u_cell_net_test.c @@ -58,6 +58,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -91,7 +93,7 @@ /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Handles. */ @@ -127,7 +129,8 @@ static bool keepGoingCallback(uDeviceHandle_t cellHandle) gCallbackErrorCode = 1; } - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -281,7 +284,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetConnectDisconnectPlus") // Connect with a very short time-out to show that aborts work U_TEST_PRINT_LINE("testing abort of connection attempt due to timeout."); - gStopTimeMs = uPortGetTickTimeMs() + 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = 1000; x = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -302,8 +306,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetConnectDisconnectPlus") U_PORT_TEST_ASSERT(x < 0); // Now connect with a sensible timeout - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; x = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -424,7 +428,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetConnectDisconnectPlus") // LENA-R8 which does not support reading the current APN // and hence can't tell if we're on the right one or not, // hence the timeout is larger than the 5 seconds it used to be - gStopTimeMs = uPortGetTickTimeMs() + 60000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = 60000; U_TEST_PRINT_LINE("connecting again with same APN..."); x = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN @@ -459,7 +464,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetConnectDisconnectPlus") // Don't try using an invalid APN with SARA-U201 as it // upsets it too much U_TEST_PRINT_LINE("connecting with different (invalid) APN..."); - gStopTimeMs = uPortGetTickTimeMs() + 10000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = 10000; x = uCellNetConnect(cellHandle, NULL, "flibble", # ifdef U_CELL_TEST_CFG_USERNAME U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_USERNAME), @@ -554,8 +560,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") // user request, so give it several goes for (size_t x = 5; (x > 0) && (y <= 0); x--) { U_TEST_PRINT_LINE("scanning for networks..."); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; memset(buffer, 0, sizeof(buffer)); memset(mccMnc, 0, sizeof(mccMnc)); for (int32_t z = uCellNetScanGetFirst(cellHandle, buffer, @@ -590,13 +596,14 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") // since that is where the results can be used // Register with a very short time-out to show that aborts work - gStopTimeMs = uPortGetTickTimeMs() + 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = 1000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, NULL, keepGoingCallback) < 0); // Now register with a sensible timeout U_TEST_PRINT_LINE("registering..."); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, NULL, keepGoingCallback) == 0); // Check that we're registered @@ -617,13 +624,14 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") // Register again: should come back with no error pretty much straight away U_TEST_PRINT_LINE("registering while already registered..."); - gStopTimeMs = uPortGetTickTimeMs() + 10000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = 10000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, NULL, keepGoingCallback) == 0); // Now activate a PDP context U_TEST_PRINT_LINE("activating context..."); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONTEXT_ACTIVATION_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONTEXT_ACTIVATION_TIMEOUT_SECONDS * 1000; y = uCellNetActivate(cellHandle, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -663,8 +671,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") U_CELL_PRIVATE_MODULE_IS_SARA_R4(pModule->moduleType)) { // If we were originally on LTE, or if this is a SARA-R4 // we will now be deregistered, so register again - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, NULL, keepGoingCallback) == 0); } else { // Get the IP address again, should be gone in the non-LTE/R4 case @@ -673,8 +681,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") // Check that we can activate the PDP context again U_TEST_PRINT_LINE("activating context..."); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONTEXT_ACTIVATION_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONTEXT_ACTIVATION_TIMEOUT_SECONDS * 1000; y = uCellNetActivate(cellHandle, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -708,7 +716,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") // uCellNetActivate() performs to see if the current context // is fine will fail and so we will detach and reattach here, // which takes longer - gStopTimeMs = uPortGetTickTimeMs() + 60000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = 60000; y = uCellNetActivate(cellHandle, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -743,8 +752,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") // Don't do this for SARA-U201 as it upsets it rather a lot U_TEST_PRINT_LINE("activating context with different (invalid) APN..."); rat = uCellNetGetActiveRat(cellHandle); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONTEXT_ACTIVATION_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONTEXT_ACTIVATION_TIMEOUT_SECONDS * 1000; y = uCellNetActivate(cellHandle, "flibble", # ifdef U_CELL_TEST_CFG_USERNAME U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_USERNAME), @@ -778,8 +787,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") y = -1; for (size_t x = 2; (x > 0) && (y < 0); x--) { U_TEST_PRINT_LINE("connecting manually to network %s...", mccMnc); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; y = uCellNetConnect(cellHandle, mccMnc, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -837,8 +846,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") // Now register with manual network selection U_TEST_PRINT_LINE("registering manually on network %s...", mccMnc); - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellNetRegister(cellHandle, mccMnc, keepGoingCallback) == 0); @@ -853,8 +862,8 @@ U_PORT_TEST_FUNCTION("[cellNet]", "cellNetScanRegActDeact") (status == U_CELL_NET_STATUS_REGISTERED_NO_CSFB_ROAMING)); // Now activate a PDP context - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; y = uCellNetActivate(cellHandle, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), diff --git a/cell/test/u_cell_ppp_test.c b/cell/test/u_cell_ppp_test.c index 62f4b5f5..b0a8c081 100644 --- a/cell/test/u_cell_ppp_test.c +++ b/cell/test/u_cell_ppp_test.c @@ -57,6 +57,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #ifdef U_CFG_TEST_GNSS_MODULE_TYPE @@ -118,7 +120,7 @@ /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Handle. */ @@ -140,7 +142,8 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -279,7 +282,8 @@ U_PORT_TEST_FUNCTION("[cellPpp]", "cellPppBasic") if (U_CELL_PRIVATE_HAS(pModule, U_CELL_PRIVATE_FEATURE_PPP)) { U_TEST_PRINT_LINE("testing PPP, first with no connection."); // First check before having connected: should return error - gStopTimeMs = uPortGetTickTimeMs() + (U_CELL_PPP_TEST_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_PPP_TEST_TIMEOUT_SECONDS * 1000; x = uCellPppOpen(cellHandle, NULL, NULL, gBuffer, sizeof(gBuffer), keepGoingCallback); U_TEST_PRINT_LINE("uCellPppOpen() returned %d.", x); @@ -290,8 +294,8 @@ U_PORT_TEST_FUNCTION("[cellPpp]", "cellPppBasic") U_TEST_PRINT_LINE("now with a connection."); // Now connect - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; x = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -311,7 +315,8 @@ U_PORT_TEST_FUNCTION("[cellPpp]", "cellPppBasic") keepGoingCallback); U_PORT_TEST_ASSERT (x == 0); - gStopTimeMs = uPortGetTickTimeMs() + (U_CELL_PPP_TEST_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_PPP_TEST_TIMEOUT_SECONDS * 1000; x = uCellPppOpen(cellHandle, NULL, NULL, gBuffer, sizeof(gBuffer), keepGoingCallback); U_TEST_PRINT_LINE("uCellPppOpen() returned %d.", x); diff --git a/cell/test/u_cell_pwr_test.c b/cell/test/u_cell_pwr_test.c index 1dee21ff..cd39fb4d 100644 --- a/cell/test/u_cell_pwr_test.c +++ b/cell/test/u_cell_pwr_test.c @@ -54,6 +54,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock.h" @@ -192,7 +194,7 @@ static uCellTestPrivate_t gHandles = U_CELL_TEST_PRIVATE_DEFAULTS; !defined(U_CFG_CELL_DISABLE_UART_POWER_SAVING) /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** A variable to track errors in the callbacks. */ @@ -241,7 +243,8 @@ static bool keepGoingCallback(uDeviceHandle_t cellHandle) gCallbackErrorCode = 1; } - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -261,7 +264,8 @@ static void testPowerAliveVInt(uCellTestPrivate_t *pHandles, bool trulyHardPowerOff = false; const uCellPrivateModule_t *pModule; # if U_CFG_APP_PIN_CELL_VINT < 0 - int64_t timeMs; + uTimeoutStart_t timeoutStart; + uint32_t y; # endif # if U_CFG_APP_PIN_CELL_ENABLE_POWER >= 0 @@ -350,23 +354,23 @@ static void testPowerAliveVInt(uCellTestPrivate_t *pHandles, // called here as we've no control over how long the // module takes to power off. pKeepGoingCallback = keepGoingCallback; - gStopTimeMs = uPortGetTickTimeMs() + - (((int64_t) pModule->powerDownWaitSeconds) * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = pModule->powerDownWaitSeconds * 1000; } # if U_CFG_APP_PIN_CELL_VINT < 0 - timeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); # endif U_TEST_PRINT_LINE("powering off..."); uCellPwrOff(cellHandle, pKeepGoingCallback); U_TEST_PRINT_LINE("power off completed."); # if U_CFG_APP_PIN_CELL_VINT < 0 - timeMs = uPortGetTickTimeMs() - timeMs; - if (timeMs < pModule->powerDownWaitSeconds * 1000) { - timeMs = (pModule->powerDownWaitSeconds * 1000) - timeMs; - U_TEST_PRINT_LINE("waiting another %d second(s) to be sure of a " + y = uTimeoutElapsedMs(timeoutStart); + if (y < (uint32_t) pModule->powerDownWaitSeconds * 1000) { + y = (pModule->powerDownWaitSeconds * 1000) - y; + U_TEST_PRINT_LINE("waiting another %u second(s) to be sure of a " "clean power off as there's no VInt pin to tell us...", - (int32_t) ((timeMs / 1000) + 1)); - uPortTaskBlock(timeMs); + (y / 1000) + 1); + uPortTaskBlock(y); } # endif } @@ -398,19 +402,19 @@ static void testPowerAliveVInt(uCellTestPrivate_t *pHandles, pModule->minAwakeTimeSeconds); uPortTaskBlock(pModule->minAwakeTimeSeconds * 1000); # if U_CFG_APP_PIN_CELL_VINT < 0 - timeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); # endif U_TEST_PRINT_LINE("hard powering off..."); uCellPwrOffHard(cellHandle, trulyHardPowerOff, NULL); U_TEST_PRINT_LINE("hard power off completed."); # if U_CFG_APP_PIN_CELL_VINT < 0 - timeMs = uPortGetTickTimeMs() - timeMs; - if (!trulyHardPowerOff && (timeMs < pModule->powerDownWaitSeconds * 1000)) { - timeMs = (pModule->powerDownWaitSeconds * 1000) - timeMs; - U_TEST_PRINT_LINE("waiting another %d second(s) to be sure of" - " a clean power off as there's no VInt pin to" - " tell us...", (int32_t) ((timeMs / 1000) + 1)); - uPortTaskBlock(timeMs); + y = uTimeoutElapsedMs(timeoutStart); + if (!trulyHardPowerOff && (y < (uint32_t) pModule->powerDownWaitSeconds * 1000)) { + y = (pModule->powerDownWaitSeconds * 1000) - y; + U_TEST_PRINT_LINE("waiting another %u second(s) to be sure of a " + "clean power off as there's no VInt pin to tell us...", + (y / 1000) + 1); + uPortTaskBlock(y); } # endif } @@ -461,8 +465,8 @@ static void wakeCallback(uDeviceHandle_t cellHandle, void *pParam) // Connect to a cellular network. static int32_t connectNetwork(uDeviceHandle_t cellHandle) { - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; return uCellNetConnect(cellHandle, NULL, # ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), diff --git a/cell/test/u_cell_sec_tls_test.c b/cell/test/u_cell_sec_tls_test.c index 90154942..f3b0a3c9 100644 --- a/cell/test/u_cell_sec_tls_test.c +++ b/cell/test/u_cell_sec_tls_test.c @@ -52,6 +52,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/test/u_cell_sock_test.c b/cell/test/u_cell_sock_test.c index 67708a0d..cde99e80 100644 --- a/cell/test/u_cell_sock_test.c +++ b/cell/test/u_cell_sock_test.c @@ -56,6 +56,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock.h" @@ -114,7 +116,7 @@ typedef struct { /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Generic handles. */ @@ -460,7 +462,8 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -589,8 +592,8 @@ U_PORT_TEST_FUNCTION("[cellSock]", "cellSockBasic") // for pModule from now on // Connect to the network - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; y = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -961,8 +964,8 @@ U_PORT_TEST_FUNCTION("[cellSock]", "cellSockOptionSetGet") cellHandle = gHandles.cellHandle; // Connect to the network - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; y = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), diff --git a/cell/test/u_cell_test_preamble.c b/cell/test/u_cell_test_preamble.c index 6e8168a0..37fad744 100644 --- a/cell/test/u_cell_test_preamble.c +++ b/cell/test/u_cell_test_preamble.c @@ -42,6 +42,8 @@ #include "u_port_debug.h" #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/test/u_cell_test_private.c b/cell/test/u_cell_test_private.c index 6ba8add7..d3670aa3 100644 --- a/cell/test/u_cell_test_private.c +++ b/cell/test/u_cell_test_private.c @@ -43,6 +43,8 @@ #include "u_port_os.h" // Required by u_cell_private.h #include "u_port_uart.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/cell/test/u_cell_time_test.c b/cell/test/u_cell_time_test.c index 99f42472..00a39743 100644 --- a/cell/test/u_cell_time_test.c +++ b/cell/test/u_cell_time_test.c @@ -55,6 +55,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -128,7 +130,7 @@ typedef struct uCellTimeTestCellInfoList_t { /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs = 0; +static uTimeoutStop_t gTimeoutStop; /** Handles. */ @@ -169,7 +171,10 @@ static bool keepGoingCallback(uDeviceHandle_t unused) (void) unused; - if (uPortGetTickTimeMs() > gStopTimeMs) { + // Zero duration means abort in this case + if ((gTimeoutStop.durationMs == 0) || + uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -273,7 +278,8 @@ static bool cellInfoCallback(uDeviceHandle_t cellHandle, } } - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -344,7 +350,6 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") const uCellPrivateModule_t *pModule; int32_t resourceCount; int32_t y; - int32_t startTimeMs; uCellTimeTestCellInfoList_t *pTmp; bool gnssIsInsideCell; #ifndef U_CELL_CFG_SARA_R5_00B @@ -372,8 +377,8 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") // Make a cellular connection so that we can test that sync works // despite that - gStopTimeMs = uPortGetTickTimeMs() + - (U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TEST_CFG_CONNECT_TIMEOUT_SECONDS * 1000; y = uCellNetConnect(cellHandle, NULL, #ifdef U_CELL_TEST_CFG_APN U_PORT_STRINGIFY_QUOTED(U_CELL_TEST_CFG_APN), @@ -409,7 +414,8 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") gEventCallback = INT_MIN; memset(&gEvent, 0xFF, sizeof(gEvent)); gEvent.synchronised = false; - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000; y = 0; // Give this a few goes as sync can fail randomly for (size_t x = 0; (y == 0) && !gEvent.synchronised && (x < U_CELL_TIME_TEST_RETRIES + 1); x++) { @@ -418,7 +424,8 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") if (U_CELL_PRIVATE_MODULE_IS_SARA_R5(pModule->moduleType)) { U_PORT_TEST_ASSERT(y == 0); while (!gEvent.synchronised && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(100); } U_TEST_PRINT_LINE("gEventCallback is %d.", gEventCallback); @@ -446,7 +453,8 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") gEventCallback = INT_MIN; memset(&gEvent, 0xFF, sizeof(gEvent)); gEvent.synchronised = false; - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000; y = 0; // Give this a few goes as sync can fail randomly for (size_t x = 0; (y == 0) && !gEvent.synchronised && (x < U_CELL_TIME_TEST_RETRIES + 1); x++) { @@ -455,7 +463,8 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") if (U_CELL_PRIVATE_MODULE_IS_SARA_R5(pModule->moduleType)) { U_PORT_TEST_ASSERT(y == 0); while (!gEvent.synchronised && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(100); } U_TEST_PRINT_LINE("gEventCallback is %d.", gEventCallback); @@ -489,21 +498,25 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") gEventCallback = INT_MIN; memset(&gEvent, 0xFF, sizeof(gEvent)); gEvent.synchronised = false; - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000; U_PORT_TEST_ASSERT(uCellTimeEnable(cellHandle, U_CELL_TIME_MODE_ONE_SHOT, !gnssIsInsideCell, 0, eventCallback, &gEventCallback) == 0); while (!gEvent.synchronised && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(100); } U_TEST_PRINT_LINE("gEventCallback is %d.", gEventCallback); if (gEvent.synchronised) { U_PORT_TEST_ASSERT(gEventCallback == 0); printAndCheckEvent(&gEvent, !gnssIsInsideCell); - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000; while ((gTimeCallback == INT_MIN) && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(100); } U_TEST_PRINT_LINE("gTimeCallback is %d.", gTimeCallback); @@ -538,7 +551,8 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") gEventCallback = INT_MIN; memset(&gEvent, 0xFF, sizeof(gEvent)); gEvent.synchronised = false; - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000; y = 0; // Give this a few goes as sync can fail randomly for (size_t x = 0; (y == 0) && !gEvent.synchronised && (x < U_CELL_TIME_TEST_RETRIES + 1); x++) { @@ -546,16 +560,19 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") !uCellLocGnssInsideCell(cellHandle), 0, eventCallback, &gEventCallback) == 0); while (!gEvent.synchronised && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(100); } U_TEST_PRINT_LINE("gEventCallback is %d.", gEventCallback); if (gEvent.synchronised) { U_PORT_TEST_ASSERT(gEventCallback == 0); printAndCheckEvent(&gEvent, !gnssIsInsideCell); - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000; while ((gTimeCallback == INT_MIN) && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(100); } U_TEST_PRINT_LINE("gTimeCallback is %d.", gTimeCallback); @@ -586,7 +603,7 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") // ...and again with a callback, but abort immediately U_TEST_PRINT_LINE("adding a callback but aborting the deep scan."); gCellInfoCallback = INT_MIN; - gStopTimeMs = 0; + gTimeoutStop.durationMs = 0; y = uCellNetDeepScan(cellHandle, cellInfoCallback, &gpCellInfoList); U_TEST_PRINT_LINE("aborted uCellNetDeepScan() returned %d.", y); U_PORT_TEST_ASSERT(y < 0); @@ -600,7 +617,8 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") // Do this a few times as the module can sometimes find nothing gCellInfoCallback = INT_MIN; for (size_t x = 0; ((gCellInfoCallback == INT_MIN) || (gpCellInfoList == NULL)) && (x < 3); x++) { - gStopTimeMs = uPortGetTickTimeMs() + (U_CELL_TIME_TEST_DEEP_SCAN_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_DEEP_SCAN_TIMEOUT_SECONDS * 1000; y = uCellNetDeepScan(cellHandle, cellInfoCallback, &gpCellInfoList); U_TEST_PRINT_LINE("%d cell(s) found on try %d.", y, x + 1); if (y > 0) { @@ -654,13 +672,15 @@ U_PORT_TEST_FUNCTION("[cellTime]", "cellTimeBasic") gEventCallback = INT_MIN; memset(&gEvent, 0xFF, sizeof(gEvent)); gEvent.synchronised = false; - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000; // SARA-R520M10 does not support U_CELL_TIME_MODE_EXT_INT_TIMESTAMP so we can // test this cell by U_CELL_TIME_MODE_ONE_SHOT which is supported by all modules. U_PORT_TEST_ASSERT(uCellTimeEnable(cellHandle, U_CELL_TIME_MODE_ONE_SHOT, true, 0, eventCallback, &gEventCallback) == 0); while (!gEvent.synchronised && - (uPortGetTickTimeMs() - startTimeMs < U_CELL_TIME_TEST_GUARD_TIME_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(100); } U_TEST_PRINT_LINE("gEventCallback is %d.", gEventCallback); diff --git a/common/at_client/src/u_at_client.c b/common/at_client/src/u_at_client.c index 371f3ecc..4db6e2f9 100644 --- a/common/at_client/src/u_at_client.c +++ b/common/at_client/src/u_at_client.c @@ -60,6 +60,8 @@ #include "u_device_serial.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range_pbuf.h" #include "u_short_range_module_type.h" @@ -405,7 +407,7 @@ typedef struct { int32_t pin; int32_t readyMs; bool highIsOn; - int32_t lastToggleTime; + uTimeoutStart_t lastToggleTime; int32_t hysteresisMs; } uAtClientActivityPin_t; @@ -443,9 +445,9 @@ typedef struct uAtClientInstance_t { uAtClientTag_t stopTag; /** The stop tag for the current scope. */ uAtClientUrc_t *pUrcList; /** Linked-list anchor for URC handlers. */ uAtClientUrc_t *pUrcRead; /** Pointer used when reading the URC handlers. */ - int32_t lastResponseStopMs; /** The time the last response ended in milliseconds. */ + uTimeoutStart_t lastResponseStop; /** The time the last response ended in milliseconds. */ int32_t lockTimeMs; /** The time when the stream was locked. */ - int32_t lastTxTimeMs; /** The time when the last transmit activity was carried out, set to -1 initially. */ + uTimeoutStart_t lastTxTime; /** The time when the last transmit activity was carried out. */ size_t urcMaxStringLength; /** The longest URC string to monitor for. */ size_t maxRespLength; /** The max length of OK, (CME) (CMS) ERROR and URCs. */ bool delimiterRequired; /** Is a delimiter to be inserted before the next parameter or not. */ @@ -1268,9 +1270,7 @@ static int32_t pollTimeRemaining(int32_t atTimeoutMs, timeRemainingMs = 0; } - // No need to worry about overflow here, we're never awake - // for long enough - return (int32_t) timeRemainingMs; + return timeRemainingMs; } // Zero the buffer. @@ -2263,8 +2263,9 @@ static size_t write(uAtClientInstance_t *pClient, while (((pData < pDataEnd) || andFlush) && (pClient->error == U_ERROR_COMMON_SUCCESS)) { lengthToWrite = length - (pData - pDataStart); - if ((pClient->pWakeUp != NULL) && (pClient->lastTxTimeMs >= 0) && - (uPortGetTickTimeMs() - pClient->lastTxTimeMs > pClient->pWakeUp->inactivityTimeoutMs) && + if ((pClient->pWakeUp != NULL) && + uTimeoutExpiredMs(pClient->lastTxTime, + pClient->pWakeUp->inactivityTimeoutMs) && (uPortMutexTryLock(pClient->pWakeUp->inWakeUpHandlerMutex, 0) == 0)) { // We have a wake-up handler, the inactivity timeout // has expired and we've managed to lock the wake-up @@ -2372,7 +2373,7 @@ static size_t write(uAtClientInstance_t *pClient, if (thisLengthWritten > 0) { pDataToWrite += thisLengthWritten; lengthToWrite -= thisLengthWritten; - pClient->lastTxTimeMs = uPortGetTickTimeMs(); + pClient->lastTxTime = uTimeoutStart(); } else { setError(pClient, U_ERROR_COMMON_DEVICE_ERROR); } @@ -2447,13 +2448,13 @@ static uPortMutexHandle_t tryLock(uAtClientInstance_t *pClient) pClient->lockTimeMs = uPortGetTickTimeMs(); if (pClient->pActivityPin != NULL) { // If an activity pin is set then switch it on - while (uPortGetTickTimeMs() - pClient->pActivityPin->lastToggleTime < - pClient->pActivityPin->hysteresisMs) { + while (!uTimeoutExpiredMs(pClient->pActivityPin->lastToggleTime, + pClient->pActivityPin->hysteresisMs)) { uPortTaskBlock(U_AT_CLIENT_ACTIVITY_PIN_HYSTERESIS_INTERVAL_MS); } if (uPortGpioSet(pClient->pActivityPin->pin, (int32_t) pClient->pActivityPin->highIsOn) == 0) { - pClient->pActivityPin->lastToggleTime = uPortGetTickTimeMs(); + pClient->pActivityPin->lastToggleTime = uTimeoutStart(); uPortTaskBlock(pClient->pActivityPin->readyMs); } } @@ -2490,13 +2491,13 @@ static void unlockNoDataCheck(uAtClientInstance_t *pClient, if (pClient->pActivityPin != NULL) { // If an activity pin is set then switch it off - while (uPortGetTickTimeMs() - pClient->pActivityPin->lastToggleTime < - pClient->pActivityPin->hysteresisMs) { + while (!uTimeoutExpiredMs(pClient->pActivityPin->lastToggleTime, + pClient->pActivityPin->hysteresisMs)) { uPortTaskBlock(U_AT_CLIENT_ACTIVITY_PIN_HYSTERESIS_INTERVAL_MS); } if (uPortGpioSet(pClient->pActivityPin->pin, (int32_t) !pClient->pActivityPin->highIsOn) == 0) { - pClient->pActivityPin->lastToggleTime = uPortGetTickTimeMs(); + pClient->pActivityPin->lastToggleTime = uTimeoutStart(); } } } @@ -2773,6 +2774,7 @@ static uAtClientHandle_t clientAdd(const uAtClientStreamHandle_t *pStream, bool receiveBufferIsMalloced = false; uDeviceSerial_t *pDeviceSerial; int32_t errorCode = -1; + uTimeoutStart_t timeoutStart; U_PORT_MUTEX_LOCK(gMutex); @@ -2804,6 +2806,7 @@ static uAtClientHandle_t clientAdd(const uAtClientStreamHandle_t *pStream, (uPortMutexCreate(&(pClient->urcPermittedMutex)) == 0)) { // Set all the non-zero initial values before we set // the event handlers which might call us + timeoutStart = uTimeoutStart(); pClient->newSendNextTime = true; pClient->stream = *pStream; pClient->atTimeoutMs = U_AT_CLIENT_DEFAULT_TIMEOUT_MS; @@ -2816,9 +2819,10 @@ static uAtClientHandle_t clientAdd(const uAtClientStreamHandle_t *pStream, clearError(pClient); // This will also set stopTag setScope(pClient, U_AT_CLIENT_SCOPE_NONE); - pClient->lastTxTimeMs = -1; + pClient->lastTxTime = timeoutStart; pClient->urcMaxStringLength = U_AT_CLIENT_INITIAL_URC_LENGTH; pClient->maxRespLength = U_AT_CLIENT_MAX_LENGTH_INFORMATION_RESPONSE_PREFIX; + pClient->lastResponseStop = timeoutStart; // Set up the buffer and its protection markers pClient->pReceiveBuffer->dataBufferSize = receiveBufferSize - U_AT_CLIENT_BUFFER_OVERHEAD_BYTES; @@ -3259,14 +3263,14 @@ void uAtClientLock(uAtClientHandle_t atHandle) streamMutex = streamLock(pClient); mutexStackPush(&(pClient->lockedStreamMutexStack), streamMutex); if (pClient->pActivityPin != NULL) { - while (uPortGetTickTimeMs() - pClient->pActivityPin->lastToggleTime < - pClient->pActivityPin->hysteresisMs) { + while (!uTimeoutExpiredMs(pClient->pActivityPin->lastToggleTime, + pClient->pActivityPin->hysteresisMs)) { uPortTaskBlock(U_AT_CLIENT_ACTIVITY_PIN_HYSTERESIS_INTERVAL_MS); } // If an activity pin is set then switch it on if (uPortGpioSet(pClient->pActivityPin->pin, (int32_t) pClient->pActivityPin->highIsOn) == 0) { - pClient->pActivityPin->lastToggleTime = uPortGetTickTimeMs(); + pClient->pActivityPin->lastToggleTime = uTimeoutStart(); uPortTaskBlock(pClient->pActivityPin->readyMs); } } @@ -3387,10 +3391,10 @@ void uAtClientCommandStart(uAtClientHandle_t atHandle, U_AT_CLIENT_LOCK_CLIENT_MUTEX(pClient); if (pClient->error == U_ERROR_COMMON_SUCCESS) { - // Wait for delay period if required, constructed this way - // to be safe if uPortGetTickTimeMs() wraps + // Wait for delay period if required if (pClient->delayMs > 0) { - while (uPortGetTickTimeMs() - pClient->lastResponseStopMs < pClient->delayMs) { + while (!uTimeoutExpiredMs(pClient->lastResponseStop, + pClient->delayMs)) { uPortTaskBlock(10); } } @@ -3789,7 +3793,7 @@ void uAtClientResponseStop(uAtClientHandle_t atHandle) setScope(pClient, U_AT_CLIENT_SCOPE_NONE); } - pClient->lastResponseStopMs = uPortGetTickTimeMs(); + pClient->lastResponseStop = uTimeoutStart(); U_AT_CLIENT_UNLOCK_CLIENT_MUTEX(pClient); } @@ -3906,7 +3910,7 @@ int32_t uAtClientWaitCharacter(uAtClientHandle_t atHandle, uErrorCode_t errorCode = U_ERROR_COMMON_INVALID_PARAMETER; uAtClientInstance_t *pClient = (uAtClientInstance_t *) atHandle; uAtClientReceiveBuffer_t *pReceiveBuffer = pClient->pReceiveBuffer; - int32_t stopTimeMs; + uTimeoutStart_t timeoutStart; bool urcFound; // IMPORTANT: this can't lock pClient->mutex as it @@ -3924,11 +3928,7 @@ int32_t uAtClientWaitCharacter(uAtClientHandle_t atHandle, // gets to zero (in which case we won't call bufferFill()) // and hence, for safety, we run our own AT timeout guard // on the loop as well - stopTimeMs = uPortGetTickTimeMs() + pClient->atTimeoutMs; - if (stopTimeMs < 0) { - // Protect against wrapping - stopTimeMs = pClient->atTimeoutMs; - } + timeoutStart = uTimeoutStart(); while ((errorCode != U_ERROR_COMMON_SUCCESS) && (pClient->error == U_ERROR_COMMON_SUCCESS)) { // Continue to look for URCs, you never @@ -3970,7 +3970,7 @@ int32_t uAtClientWaitCharacter(uAtClientHandle_t atHandle, pClient->numConsecutiveAtTimeouts = 0; } } else { - if (uPortGetTickTimeMs() > stopTimeMs) { + if (uTimeoutExpiredMs(timeoutStart, pClient->atTimeoutMs)) { // If we're stuck, set an error setError(pClient, U_ERROR_COMMON_DEVICE_ERROR); consecutiveTimeout(pClient); @@ -4571,7 +4571,7 @@ int32_t uAtClientSetActivityPin(uAtClientHandle_t atHandle, pClient->pActivityPin->pin = pin; pClient->pActivityPin->readyMs = readyMs; pClient->pActivityPin->highIsOn = highIsOn; - pClient->pActivityPin->lastToggleTime = uPortGetTickTimeMs(); + pClient->pActivityPin->lastToggleTime = uTimeoutStart(); pClient->pActivityPin->hysteresisMs = hysteresisMs; errorCode = (int32_t) U_ERROR_COMMON_SUCCESS; } diff --git a/common/at_client/test/u_at_client_test.c b/common/at_client/test/u_at_client_test.c index ac68b80f..781545d8 100644 --- a/common/at_client/test/u_at_client_test.c +++ b/common/at_client/test/u_at_client_test.c @@ -57,6 +57,7 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_at_client_test.h" #include "u_at_client_test_data.h" @@ -284,13 +285,13 @@ static bool atTimeoutIsObeyed(uAtClientHandle_t atClientHandle, int32_t timeoutMs) { bool success = false; - int64_t startTime; - int32_t duration; + uTimeoutStart_t timeoutStart; + int32_t durationMs; int32_t consecutiveTimeouts; int32_t x; int32_t y; - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); uAtClientLock(atClientHandle); // Send nothing consecutiveTimeouts = gConsecutiveTimeout; @@ -309,11 +310,11 @@ static bool atTimeoutIsObeyed(uAtClientHandle_t atClientHandle, uPortTaskBlock(10); if ((x < 0) && (y < 0) && (gConsecutiveTimeout == consecutiveTimeouts + 1)) { - duration = (int32_t) (uPortGetTickTimeMs() - startTime); - if ((duration < timeoutMs) || - (duration > timeoutMs + U_AT_CLIENT_TEST_AT_TIMEOUT_TOLERANCE_MS)) { + durationMs = uTimeoutElapsedMs(timeoutStart); + if ((durationMs < timeoutMs) || + (durationMs > timeoutMs + U_AT_CLIENT_TEST_AT_TIMEOUT_TOLERANCE_MS)) { U_TEST_PRINT_LINE("AT timeout was not obeyed (%d ms as opposed" - " to %d ms).", (int) duration, timeoutMs); + " to %d ms).", (int) durationMs, timeoutMs); } else { success = true; } diff --git a/common/at_client/test/u_at_client_test_data.c b/common/at_client/test/u_at_client_test_data.c index 14db3352..2cbe2f9e 100644 --- a/common/at_client/test/u_at_client_test_data.c +++ b/common/at_client/test/u_at_client_test_data.c @@ -42,6 +42,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_port_clib_platform_specific.h" /* Integer stdio, must be included before the other port files if any print or scan function is used. */ @@ -869,15 +871,15 @@ static int32_t handleReadOnError(uAtClientHandle_t atClientHandle, { int32_t lastError; uint64_t uint64; - int64_t startTime; - int32_t duration; + uTimeoutStart_t timeoutStart; + int32_t durationMs; const uAtClientTestEchoError_t *pError; #if !U_CFG_ENABLE_LOGGING (void) index; #endif - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); pError = (const uAtClientTestEchoError_t *) pParameter; U_TEST_PRINT_LINE_X("checking that parameter reads return error when" @@ -940,20 +942,20 @@ static int32_t handleReadOnError(uAtClientHandle_t atClientHandle, } // The errors should be returned within the guard times - duration = (int32_t) (uPortGetTickTimeMs() - startTime); + durationMs = uTimeoutElapsedMs(timeoutStart); if (lastError == 0) { - if (duration < pError->timeMinMs) { + if (durationMs < pError->timeMinMs) { U_TEST_PRINT_LINE_X("reads took %d ms when a minimum of %d ms was" - " expected.", index + 1, duration, + " expected.", index + 1, durationMs, pError->timeMinMs); lastError = 6; } } if (lastError == 0) { - if (duration > pError->timeMaxMs) { + if (durationMs > pError->timeMaxMs) { U_TEST_PRINT_LINE_X("reads took %d ms when a maximum of %d ms" - " was expected.", index + 1, duration, + " was expected.", index + 1, durationMs, pError->timeMaxMs); lastError = 7; } diff --git a/common/device/src/u_device_private_cell.c b/common/device/src/u_device_private_cell.c index 7a7e0658..ce330b83 100644 --- a/common/device/src/u_device_private_cell.c +++ b/common/device/src/u_device_private_cell.c @@ -37,6 +37,8 @@ #include "u_device.h" #include "u_device_shared.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" @@ -79,7 +81,8 @@ static bool keepGoingCallback(uDeviceHandle_t devHandle) if (uDeviceGetInstance(devHandle, &pInstance) == 0) { pContext = (uDeviceCellContext_t *) pInstance->pContext; if ((pContext == NULL) || - (uPortGetTickTimeMs() < pContext->stopTimeMs)) { + !uTimeoutExpiredMs(pContext->timeoutStop.timeoutStart, + pContext->timeoutStop.durationMs)) { keepGoing = true; } } @@ -169,8 +172,8 @@ static int32_t addDevice(const uDeviceCfgUart_t *pCfgUart, pDeviceHandle); if (errorCode == 0) { // Set the timeout - pContext->stopTimeMs = uPortGetTickTimeMs() + - (U_DEVICE_PRIVATE_CELL_POWER_ON_GUARD_TIME_SECONDS * 1000); + pContext->timeoutStop.timeoutStart = uTimeoutStart(); + pContext->timeoutStop.durationMs = U_DEVICE_PRIVATE_CELL_POWER_ON_GUARD_TIME_SECONDS * 1000; // Remember the PWR_ON pin 'cos we need it during power down pContext->pinPwrOn = pCfgCell->pinPwrOn; // Hook our context data off the device handle diff --git a/common/device/src/u_device_shared_cell.h b/common/device/src/u_device_shared_cell.h index 16a2d00e..3a68db1f 100644 --- a/common/device/src/u_device_shared_cell.h +++ b/common/device/src/u_device_shared_cell.h @@ -46,7 +46,7 @@ extern "C" { typedef struct { int32_t uart; uAtClientHandle_t at; - int64_t stopTimeMs; + uTimeoutStop_t timeoutStop; int32_t pinPwrOn; uDeviceSerial_t *pPppDeviceSerial; } uDeviceCellContext_t; diff --git a/common/geofence/src/u_geofence.c b/common/geofence/src/u_geofence.c index 45614e8a..668ac864 100644 --- a/common/geofence/src/u_geofence.c +++ b/common/geofence/src/u_geofence.c @@ -146,6 +146,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_linked_list.h" @@ -1258,21 +1260,18 @@ static uGeofencePositionState_t testSquareExtent(const uGeofenceSquare_t *pSquar static uGeofencePositionState_t testSpeed(const uGeofenceDynamic_t *pPreviousDistance) { uGeofencePositionState_t positionState = U_GEOFENCE_POSITION_STATE_NONE; - int32_t timeNowMs; + uint32_t timeDifferenceMs; int64_t distanceTravelledMillimetres; if ((pPreviousDistance->lastStatus.distanceMillimetres != LLONG_MIN) && (pPreviousDistance->maxHorizontalSpeedMillimetresPerSecond >= 0)) { // Work out how far we can have travelled in the time - timeNowMs = uPortGetTickTimeMs(); - // Guard against wrap - if (timeNowMs > pPreviousDistance->lastStatus.timeMs) { - // Divide by 1000 below to get per second - distanceTravelledMillimetres = ((int64_t) (timeNowMs - pPreviousDistance->lastStatus.timeMs)) * - pPreviousDistance->maxHorizontalSpeedMillimetresPerSecond / 1000; - if (distanceTravelledMillimetres < pPreviousDistance->lastStatus.distanceMillimetres) { - positionState = U_GEOFENCE_POSITION_STATE_OUTSIDE; - } + timeDifferenceMs = uTimeoutElapsedMs(pPreviousDistance->lastStatus.timeoutStart); + // Divide by 1000 below to get per second + distanceTravelledMillimetres = ((int64_t) timeDifferenceMs) * + pPreviousDistance->maxHorizontalSpeedMillimetresPerSecond / 1000; + if (distanceTravelledMillimetres < pPreviousDistance->lastStatus.distanceMillimetres) { + positionState = U_GEOFENCE_POSITION_STATE_OUTSIDE; } } @@ -1655,7 +1654,7 @@ bool testPosition(const uGeofence_t *pFence, } else { if (distanceMinMetres == distanceMinMetres) { // NAN test pDynamic->lastStatus.distanceMillimetres = (int64_t) (distanceMinMetres * 1000); - pDynamic->lastStatus.timeMs = uPortGetTickTimeMs(); + pDynamic->lastStatus.timeoutStart = uTimeoutStart(); } } } @@ -1697,7 +1696,7 @@ int32_t uGeofenceContextEnsure(uGeofenceContext_t **ppFenceContext) if (*ppFenceContext != NULL) { memset(*ppFenceContext, 0, sizeof(**ppFenceContext)); (*ppFenceContext)->dynamic.lastStatus.distanceMillimetres = LLONG_MIN; - (*ppFenceContext)->dynamic.lastStatus.timeMs = uPortGetTickTimeMs(); + (*ppFenceContext)->dynamic.lastStatus.timeoutStart = uTimeoutStart(); (*ppFenceContext)->dynamic.maxHorizontalSpeedMillimetresPerSecond = -1; errorCode = (int32_t) U_ERROR_COMMON_SUCCESS; } @@ -1903,7 +1902,7 @@ uGeofencePositionState_t uGeofenceContextTest(uDeviceHandle_t devHandle, if ((dynamic.lastStatus.distanceMillimetres != LLONG_MIN) && (dynamic.lastStatus.distanceMillimetres < dynamicsMinDistance.lastStatus.distanceMillimetres)) { dynamicsMinDistance.lastStatus.distanceMillimetres = dynamic.lastStatus.distanceMillimetres; - dynamicsMinDistance.lastStatus.distanceMillimetres = uPortGetTickTimeMs(); + dynamicsMinDistance.lastStatus.timeoutStart = dynamic.lastStatus.timeoutStart; } if ((pFenceContext->pCallback != NULL) && (devHandle != NULL)) { pFenceContext->pCallback(devHandle, pFence, pFence->pNameStr, diff --git a/common/geofence/src/u_geofence_shared.h b/common/geofence/src/u_geofence_shared.h index cdb2c971..abcd0ab5 100644 --- a/common/geofence/src/u_geofence_shared.h +++ b/common/geofence/src/u_geofence_shared.h @@ -42,7 +42,7 @@ extern "C" { */ typedef struct { int64_t distanceMillimetres; /**< use LLONG_MIN to mean "not known". */ - int32_t timeMs; /**< populated from uPortGetTickTimeMs(). */ + uTimeoutStart_t timeoutStart; /**< populated from uTimeoutStart(). */ } uGeofenceDynamicStatus_t; /** Structure to hold the maximum speed that a device will travel diff --git a/common/geofence/test/u_geofence_test.c b/common/geofence/test/u_geofence_test.c index e2c6edda..7a4283b4 100644 --- a/common/geofence/test/u_geofence_test.c +++ b/common/geofence/test/u_geofence_test.c @@ -51,6 +51,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_linked_list.h" #include "u_port_clib_platform_specific.h" /* must be included before the other @@ -642,7 +644,7 @@ U_PORT_TEST_FUNCTION("[geofence]", "geofenceBasic") uGeofencePositionState_t positionState; bool newPolygon; char prefixBuffer[32]; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t numEdges; size_t numShapes; size_t numFailedCalculations; @@ -777,7 +779,7 @@ U_PORT_TEST_FUNCTION("[geofence]", "geofenceBasic") // Test the point(s) against the fence in all permutations of parameters, // do it twice, once with prints and then without to get an accurate timing for (size_t t = 0; t < 2; t++) { - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); // We take all of the points and test one parameter combination, then // take all of the points and repeat for the next parameter combination, // etc., rather than doing all of the parameter combinations for one @@ -856,10 +858,10 @@ U_PORT_TEST_FUNCTION("[geofence]", "geofenceBasic") } if (t > 0) { uPortLog(U_TEST_PREFIX_A "testing %d shape(s) (%d edge(s)) against %d point(s)," - " %d times each (print time excluded), averaged %d us per point", + " %d times each (print time excluded), averaged %u ms per point", (char) (x + 0x41), numShapes, numEdges, pTestData->numPoints, sizeof(gTestParameters) / sizeof(gTestParameters[0]), - (((uPortGetTickTimeMs() - startTimeMs) * 1000) / + (uTimeoutElapsedMs(timeoutStart) / (pTestData->numPoints * sizeof(gTestParameters) / sizeof(gTestParameters[0])))); if (numFailedCalculations > 0) { uPortLog(" AND %d CALCULATION(S) FAILED.\n", numFailedCalculations); diff --git a/common/http_client/src/u_http_client.c b/common/http_client/src/u_http_client.c index 3e2d6255..927b716e 100644 --- a/common/http_client/src/u_http_client.c +++ b/common/http_client/src/u_http_client.c @@ -67,6 +67,8 @@ #include "u_assert.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" @@ -675,12 +677,12 @@ static void exitFunctionRequest(uHttpClientContext_t *pContext, int32_t errorCod static int32_t block(volatile uHttpClientContext_t *pContext) { int32_t errorCode = (int32_t) U_ERROR_COMMON_SUCCESS; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t statusCodeOrError = 0; if (pContext->pResponseCallback == NULL) { // We're blocking - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; // Wait for the underlying layer to give a response // or pKeepGoingCallback=false or a timeout of twice the @@ -689,7 +691,7 @@ static int32_t block(volatile uHttpClientContext_t *pContext) // HTTP status code while ((statusCodeOrError == 0) && ((pContext->pKeepGoingCallback == NULL) || pContext->pKeepGoingCallback()) && - (uPortGetTickTimeMs() - startTimeMs < (pContext->timeoutSeconds * 2) * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, pContext->timeoutSeconds * 2)) { if (uPortSemaphoreTryTake((uPortSemaphoreHandle_t) pContext->semaphoreHandle, 100) == 0) { statusCodeOrError = pContext->statusCodeOrError; } diff --git a/common/http_client/test/u_http_client_test.c b/common/http_client/test/u_http_client_test.c index 02853945..53d0bfd9 100644 --- a/common/http_client/test/u_http_client_test.c +++ b/common/http_client/test/u_http_client_test.c @@ -63,6 +63,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" #include "u_network_test_shared_cfg.h" #include "u_http_client_test_shared_cfg.h" @@ -400,7 +402,7 @@ static int32_t checkResponse(uHttpClientTestOperation_t operation, bool checkBinary, bool rtsFlowControlEnabled) { int32_t outcome = (int32_t) U_ERROR_COMMON_SUCCESS; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t x; const char *pTmp; int32_t y; @@ -429,21 +431,21 @@ static int32_t checkResponse(uHttpClientTestOperation_t operation, // For the non-blocking case, should have an initial // error code of zero if (errorOrStatusCode == 0) { - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); // Wait for twice as long as the timeout as a guard U_TEST_PRINT_LINE("waiting for asynchronous response for up to" " %d second(s)...", (pConnection->timeoutSeconds * 2) + U_HTTP_CLIENT_TEST_RESPONSE_TIMEOUT_EXTRA_SECONDS); while (!pCallbackData->called && - (uPortGetTickTimeMs() - startTimeMs < ((pConnection->timeoutSeconds * 2) + - U_HTTP_CLIENT_TEST_RESPONSE_TIMEOUT_EXTRA_SECONDS) * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, (pConnection->timeoutSeconds * 2) + + U_HTTP_CLIENT_TEST_RESPONSE_TIMEOUT_EXTRA_SECONDS)) { uPortTaskBlock(100); } if (pCallbackData->called) { responseSize = pCallbackData->responseSize; - U_TEST_PRINT_LINE("response received in %d ms.\n", - uPortGetTickTimeMs() - startTimeMs); + U_TEST_PRINT_LINE("response received in %u ms.\n", + uTimeoutElapsedMs(timeoutStart)); if (pCallbackData->statusCodeOrError != expectedStatusCode) { U_TEST_PRINT_LINE("expected status code %d, got %d.\n", expectedStatusCode, pCallbackData->statusCodeOrError); @@ -456,8 +458,8 @@ static int32_t checkResponse(uHttpClientTestOperation_t operation, } } } else { - U_TEST_PRINT_LINE("callback not called after %d second(s).\n", - (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("callback not called after %u second(s).\n", + uTimeoutElapsedSeconds(timeoutStart)); outcome = (int32_t) U_ERROR_COMMON_TIMEOUT; } } else { diff --git a/common/location/src/u_location_private_cloud_locate.c b/common/location/src/u_location_private_cloud_locate.c index 8953763a..ea79e7f3 100644 --- a/common/location/src/u_location_private_cloud_locate.c +++ b/common/location/src/u_location_private_cloud_locate.c @@ -47,6 +47,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_time.h" #include "u_ubx_protocol.h" @@ -363,7 +365,7 @@ int32_t uLocationPrivateCloudLocate(uDeviceHandle_t devHandle, char topicBuffer[U_LOCATION_PRIVATE_CLOUD_LOCATE_SUBSCRIBE_TOPIC_LENGTH_BYTES]; char *pTopicBufferRead; char *pMessageRead; - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); bool subscribed = false; size_t z; @@ -447,7 +449,7 @@ int32_t uLocationPrivateCloudLocate(uDeviceHandle_t devHandle, " location from server...\n"); while ((errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) && (((pKeepGoingCallback == NULL) && - (uPortGetTickTimeMs() - startTimeMs) / 1000 < U_LOCATION_TIMEOUT_SECONDS) || + !uTimeoutExpiredSeconds(timeoutStart, U_LOCATION_TIMEOUT_SECONDS)) || ((pKeepGoingCallback != NULL) && pKeepGoingCallback(devHandle)))) { if (uMqttClientGetUnread(pMqttClientContext) > 0) { z = U_LOCATION_PRIVATE_CLOUD_LOCATE_READ_MESSAGE_LENGTH_BYTES; diff --git a/common/location/src/u_location_stub_wifi.c b/common/location/src/u_location_stub_wifi.c index ae09f81d..d2b49335 100644 --- a/common/location/src/u_location_stub_wifi.c +++ b/common/location/src/u_location_stub_wifi.c @@ -38,6 +38,7 @@ #include "u_compiler.h" // U_WEAK #include "u_error_common.h" +#include "u_timeout.h" #include "u_port_os.h" #include "u_location.h" #include "u_short_range_module_type.h" diff --git a/common/location/test/u_location_test.c b/common/location/test/u_location_test.c index 6d58b6a8..e58a5b96 100644 --- a/common/location/test/u_location_test.c +++ b/common/location/test/u_location_test.c @@ -63,6 +63,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" #include "u_network_test_shared_cfg.h" @@ -117,7 +119,7 @@ /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs = 0; +static uTimeoutStop_t gTimeoutStop; /** Keep track of the current network handle so that the * keepGoingCallback() can check it. @@ -177,7 +179,8 @@ static bool keepGoingCallback(uDeviceHandle_t devHandle) bool keepGoing = true; U_PORT_TEST_ASSERT((gDevHandle == NULL) || (devHandle == gDevHandle)); - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -286,16 +289,17 @@ static bool httpPostCheck(uLocationType_t locationType, volatile int32_t *pHttpStatusCode) { bool success = true; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; if ((pHttpContext != NULL) && ((locationType == U_LOCATION_TYPE_CLOUD_GOOGLE) || (locationType == U_LOCATION_TYPE_CLOUD_SKYHOOK) || (locationType == U_LOCATION_TYPE_CLOUD_HERE))) { success = false; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((*pHttpStatusCode != 200) && - (uPortGetTickTimeMs() - startTimeMs < U_LOCATION_TEST_HTTP_TIMEOUT_SECONDS * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, + U_LOCATION_TEST_HTTP_TIMEOUT_SECONDS)) { uPortTaskBlock(100); } if (*pHttpStatusCode != 200) { @@ -328,7 +332,6 @@ static void testBlocking(uDeviceHandle_t devHandle, const uLocationTestCfg_t *pLocationCfg) { uLocation_t location; - int32_t startTimeMs = 0; int32_t timeoutMs = U_LOCATION_TEST_CFG_TIMEOUT_SECONDS * 1000; int32_t y; const uLocationAssist_t *pLocationAssist = NULL; @@ -356,8 +359,8 @@ static void testBlocking(uDeviceHandle_t devHandle, // WiFi can sometimes fail y = -1; for (int32_t x = 0; (x < 3) && (y != 0); x++) { - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + timeoutMs; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = timeoutMs; y = uLocationGet(devHandle, locationType, pLocationAssist, pAuthenticationTokenStr, @@ -380,8 +383,8 @@ static void testBlocking(uDeviceHandle_t devHandle, " position (HTTP status code %d).", y); } } - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("location establishment took %u second(s).", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); // If we are running on a test cellular network we won't get position but // we should always get time if ((location.radiusMillimetres > 0) && @@ -453,7 +456,7 @@ static void testOneShot(uDeviceHandle_t devHandle, uLocationType_t locationType, const uLocationTestCfg_t *pLocationCfg) { - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t y; int32_t timeoutMs = U_LOCATION_TEST_CFG_TIMEOUT_SECONDS * 1000; const uLocationAssist_t *pLocationAssist = NULL; @@ -467,7 +470,7 @@ static void testOneShot(uDeviceHandle_t devHandle, pAuthenticationTokenStr = pLocationCfg->pAuthenticationTokenStr; pLocationAssist = pLocationCfg->pLocationAssist; } - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); uLocationTestResetLocation(&gLocation); if (pLocationCfg != NULL) { @@ -489,7 +492,7 @@ static void testOneShot(uDeviceHandle_t devHandle, " one-shot API...", timeoutMs); while ((gErrorCode == INT_MIN) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs)) { + !uTimeoutExpiredMs(timeoutStart, timeoutMs)) { // Location establishment status is only supported for cell locate y = uLocationGetStatus(devHandle); if (locationType == U_LOCATION_TYPE_CLOUD_CELL_LOCATE) { @@ -501,8 +504,8 @@ static void testOneShot(uDeviceHandle_t devHandle, } if (gErrorCode == 0) { - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("location establishment took %u second(s).", + uTimeoutElapsedSeconds(timeoutStart)); // If we are running on a cellular test network we might not // get position but we should always get time U_PORT_TEST_ASSERT(gDevHandle == devHandle); @@ -585,7 +588,7 @@ static void testContinuous(uDeviceHandle_t devHandle, uLocationType_t locationType, const uLocationTestCfg_t *pLocationCfg) { - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t timeoutMs = U_LOCATION_TEST_CFG_TIMEOUT_SECONDS * 1000; int32_t y; const uLocationAssist_t *pLocationAssist = NULL; @@ -599,7 +602,7 @@ static void testContinuous(uDeviceHandle_t devHandle, pAuthenticationTokenStr = pLocationCfg->pAuthenticationTokenStr; pLocationAssist = pLocationCfg->pLocationAssist; } - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); uLocationTestResetLocation(&gLocation); if (pLocationCfg != NULL) { @@ -621,7 +624,7 @@ static void testContinuous(uDeviceHandle_t devHandle, timeoutMs * U_LOCATION_TEST_CFG_CONTINUOUS_COUNT, U_LOCATION_TEST_CFG_CONTINUOUS_COUNT); while ((gCount < U_LOCATION_TEST_CFG_CONTINUOUS_COUNT) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs)) { + !uTimeoutExpiredMs(timeoutStart, timeoutMs)) { // Location establishment status is only supported for cell locate y = uLocationGetStatus(devHandle); if (locationType == U_LOCATION_TYPE_CLOUD_CELL_LOCATE) { @@ -632,14 +635,9 @@ static void testContinuous(uDeviceHandle_t devHandle, uPortTaskBlock(1000); } - // There has been something off going on with this test on Windows, - // so print a little extra diagnostic information here so that we - // can watch it - U_TEST_PRINT_LINE("startTimeMs is %d, ticks now is %d, gCount is %d.", - startTimeMs, uPortGetTickTimeMs(), gCount); if (gCount >= U_LOCATION_TEST_CFG_CONTINUOUS_COUNT) { - U_TEST_PRINT_LINE("took %d second(s) to get location %d time(s).", - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000, + U_TEST_PRINT_LINE("took %u second(s) to get location %d time(s).", + uTimeoutElapsedSeconds(timeoutStart), gCount); // If we are running on a cellular test network we might not // get position but we should always get time diff --git a/common/location/test/u_location_test_shared_cfg.c b/common/location/test/u_location_test_shared_cfg.c index 1320536d..cd7f1f87 100644 --- a/common/location/test/u_location_test_shared_cfg.c +++ b/common/location/test/u_location_test_shared_cfg.c @@ -43,6 +43,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_network.h" #include "u_network_test_shared_cfg.h" diff --git a/common/mqtt_client/test/u_mqtt_client_test.c b/common/mqtt_client/test/u_mqtt_client_test.c index 57860627..f23f2a91 100644 --- a/common/mqtt_client/test/u_mqtt_client_test.c +++ b/common/mqtt_client/test/u_mqtt_client_test.c @@ -63,6 +63,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" #include "u_network_test_shared_cfg.h" @@ -147,7 +149,7 @@ /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** The test MQTT context. */ @@ -183,7 +185,8 @@ static bool keepGoingCallback(void) { bool keepGoing = true; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -279,7 +282,6 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClient") int32_t y; int32_t z; size_t s; - int32_t startTimeMs; char *pTopicOut; char *pTopicIn; char *pMessageOut; @@ -378,14 +380,13 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClient") // Connect it U_TEST_PRINT_LINE_MQTT("connecting to \"%s\"...", connection.pBrokerNameStr); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; y = uMqttClientConnect(gpMqttContextA, &connection); z = uMqttClientOpenResetLastError(); if (y == 0) { - U_TEST_PRINT_LINE_MQTT("connect successful after %d ms.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE_MQTT("connect successful after %u ms.", + uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); U_PORT_TEST_ASSERT(z == 0); // Note: can't check the return value here as it is // utterly module specific, only really checking that it @@ -400,17 +401,16 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClient") &gNumUnread) == 0); U_TEST_PRINT_LINE_MQTT("subscribing to topic \"%s\"...", pTopicOut); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; y = uMqttClientSubscribe(gpMqttContextA, pTopicOut, U_MQTT_QOS_EXACTLY_ONCE); if (y >= 0) { - U_TEST_PRINT_LINE_MQTT("subscribe successful after %d ms, QoS %d.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs), y); + U_TEST_PRINT_LINE_MQTT("subscribe successful after %u ms, QoS %d.", + uTimeoutElapsedMs(gTimeoutStop.timeoutStart), y); } else { - U_TEST_PRINT_LINE_MQTT("subscribe returned error %d after %d ms," + U_TEST_PRINT_LINE_MQTT("subscribe returned error %d after %u ms," " module error %d.", y, - (int32_t) (uPortGetTickTimeMs() - startTimeMs), + uTimeoutElapsedMs(gTimeoutStop.timeoutStart), uMqttClientGetLastErrorCode(gpMqttContextA)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); @@ -436,9 +436,8 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClient") U_TEST_PRINT_LINE_MQTT("publishing %d byte(s) to topic \"%s\"...", U_MQTT_CLIENT_TEST_PUBLISH_MAX_LENGTH_BYTES, pTopicOut); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; // Fill in the outgoing message buffer with all possible things s = 0; y = U_MQTT_CLIENT_TEST_PUBLISH_MAX_LENGTH_BYTES; @@ -455,32 +454,33 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClient") U_MQTT_CLIENT_TEST_PUBLISH_MAX_LENGTH_BYTES, U_MQTT_QOS_EXACTLY_ONCE, false); if (y == 0) { - U_TEST_PRINT_LINE_MQTT("publish successful after %d ms.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE_MQTT("publish successful after %u ms.", + uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); // We've just sent a message U_PORT_TEST_ASSERT(uMqttClientGetTotalMessagesSent(gpMqttContextA) > 0); } else { - U_TEST_PRINT_LINE_MQTT("publish returned error %d after %d ms, module" + U_TEST_PRINT_LINE_MQTT("publish returned error %d after %u ms, module" " error %d.", y, - (int32_t) (uPortGetTickTimeMs() - startTimeMs), + uTimeoutElapsedMs(gTimeoutStop.timeoutStart), uMqttClientGetLastErrorCode(gpMqttContextA)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); } U_TEST_PRINT_LINE_MQTT("waiting for an unread message indication..."); - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; while ((gNumUnread == 0) && - (uPortGetTickTimeMs() < startTimeMs + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000))) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(1000); } if (gNumUnread > 0) { U_TEST_PRINT_LINE_MQTT("%d message(s) unread.", gNumUnread); } else { - U_TEST_PRINT_LINE_MQTT("no messages unread after %d ms.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE_MQTT("no messages unread after %u ms.", + uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); } @@ -546,8 +546,8 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClient") // Cancel the subscribe U_TEST_PRINT_LINE_MQTT("unsubscribing from topic \"%s\"...", pTopicOut); - gStopTimeMs = uPortGetTickTimeMs() + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; U_PORT_TEST_ASSERT(uMqttClientUnsubscribe(gpMqttContextA, pTopicOut) == 0); // Remove the callback @@ -567,9 +567,9 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClient") } } else { if (noTls) { - U_TEST_PRINT_LINE_MQTT("connection failed after %d ms," + U_TEST_PRINT_LINE_MQTT("connection failed after %u ms," " with error %d, module error %d.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs), z, + uTimeoutElapsedMs(gTimeoutStop.timeoutStart), z, uMqttClientGetLastErrorCode(gpMqttContextA)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); @@ -631,7 +631,6 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClientSn") int32_t y; int32_t z; size_t s; - int32_t startTimeMs; char *pTopicNameOutMqtt; uMqttSnTopicName_t topicNameOut; uMqttSnTopicName_t topicNameIn; @@ -717,14 +716,13 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClientSn") // Connect it U_TEST_PRINT_LINE_MQTTSN("connecting to \"%s\"...", connection.pBrokerNameStr); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; y = uMqttClientConnect(gpMqttContextA, &connection); z = uMqttClientOpenResetLastError(); if (y == 0) { - U_TEST_PRINT_LINE_MQTTSN("connect successful after %d ms.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE_MQTTSN("connect successful after %u ms.", + uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); U_PORT_TEST_ASSERT(z == 0); // Note: can't check the return value here as it is // utterly module specific, only really checking that it @@ -739,24 +737,23 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClientSn") &gNumUnread) == 0); U_TEST_PRINT_LINE_MQTTSN("subscribing to MQTT topic \"%s\"...", pTopicNameOutMqtt); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; memset(&topicNameOut, 0xFF, sizeof(topicNameOut)); y = uMqttClientSnSubscribeNormalTopic(gpMqttContextA, pTopicNameOutMqtt, U_MQTT_QOS_EXACTLY_ONCE, &topicNameOut); if (y >= 0) { - U_TEST_PRINT_LINE_MQTTSN("subscribe successful after %d ms, topic ID \"%d\"," - " QoS %d.", (int32_t) (uPortGetTickTimeMs() - startTimeMs), + U_TEST_PRINT_LINE_MQTTSN("subscribe successful after %u ms, topic ID \"%d\"," + " QoS %d.", uTimeoutElapsedMs(gTimeoutStop.timeoutStart), topicNameOut.name.id, y); U_PORT_TEST_ASSERT(uMqttClientSnGetTopicNameType(&topicNameOut) == U_MQTT_SN_TOPIC_NAME_TYPE_ID_NORMAL); U_PORT_TEST_ASSERT(uMqttClientSnGetTopicId(&topicNameOut) >= 0); U_PORT_TEST_ASSERT(uMqttClientSnGetTopicNameShort(&topicNameOut, topicNameShortStr) < 0); } else { - U_TEST_PRINT_LINE_MQTTSN("subscribe returned error %d after %d ms," + U_TEST_PRINT_LINE_MQTTSN("subscribe returned error %d after %u ms," " module error %d.", y, - (int32_t) (uPortGetTickTimeMs() - startTimeMs), + uTimeoutElapsedMs(gTimeoutStop.timeoutStart), uMqttClientGetLastErrorCode(gpMqttContextA)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); @@ -780,9 +777,8 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClientSn") U_TEST_PRINT_LINE_MQTTSN("publishing %d byte(s) to topic \"%d\"...", U_MQTT_CLIENT_TEST_PUBLISH_MAX_LENGTH_BYTES, topicNameOut.name.id); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; // Fill in the outgoing message buffer with all possible things s = 0; y = U_MQTT_CLIENT_TEST_PUBLISH_MAX_LENGTH_BYTES; @@ -799,32 +795,33 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClientSn") pMessageOut, U_MQTT_CLIENT_TEST_PUBLISH_MAX_LENGTH_BYTES, U_MQTT_QOS_EXACTLY_ONCE, false); if (y == 0) { - U_TEST_PRINT_LINE_MQTTSN("publish successful after %d ms.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE_MQTTSN("publish successful after %u ms.", + uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); // We've just sent a message U_PORT_TEST_ASSERT(uMqttClientGetTotalMessagesSent(gpMqttContextA) > 0); } else { - U_TEST_PRINT_LINE_MQTTSN("publish returned error %d after %d ms, module" + U_TEST_PRINT_LINE_MQTTSN("publish returned error %d after %u ms, module" " error %d.", y, - (int32_t) (uPortGetTickTimeMs() - startTimeMs), + uTimeoutElapsedMs(gTimeoutStop.timeoutStart), uMqttClientGetLastErrorCode(gpMqttContextA)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); } U_TEST_PRINT_LINE_MQTTSN("waiting for an unread message indication..."); - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; while ((gNumUnread == 0) && - (uPortGetTickTimeMs() - startTimeMs < - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000))) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(1000); } if (gNumUnread > 0) { U_TEST_PRINT_LINE_MQTTSN("%d message(s) unread.", gNumUnread); } else { - U_TEST_PRINT_LINE_MQTTSN("no messages unread after %d ms.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE_MQTTSN("no messages unread after %u ms.", + uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); } @@ -886,8 +883,8 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClientSn") // Cancel the subscribe U_TEST_PRINT_LINE_MQTTSN("unsubscribing from topic \"%d\"...", topicNameOut.name.id); - gStopTimeMs = uPortGetTickTimeMs() + - (U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_MQTT_CLIENT_RESPONSE_WAIT_SECONDS * 1000; U_PORT_TEST_ASSERT(uMqttClientSnUnsubscribeNormalTopic(gpMqttContextA, pTopicNameOutMqtt) == 0); // Remove the callback @@ -949,9 +946,9 @@ U_PORT_TEST_FUNCTION("[mqttClient]", "mqttClientSn") U_PORT_TEST_ASSERT(gDisconnectCallbackCalled); } } else { - U_TEST_PRINT_LINE_MQTTSN("connection failed after %d ms," + U_TEST_PRINT_LINE_MQTTSN("connection failed after %u ms," " with error %d, module error %d.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs), z, + uTimeoutElapsedMs(gTimeoutStop.timeoutStart), z, uMqttClientGetLastErrorCode(gpMqttContextA)); //lint -e(506, 774) Suppress constant value Boolean U_PORT_TEST_ASSERT(false); diff --git a/common/network/src/u_network_private_cell.c b/common/network/src/u_network_private_cell.c index e5e88539..d51c479c 100644 --- a/common/network/src/u_network_private_cell.c +++ b/common/network/src/u_network_private_cell.c @@ -40,6 +40,8 @@ #include "u_port.h" #include "u_port_uart.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device.h" @@ -85,7 +87,8 @@ static bool keepGoingCallback(uDeviceHandle_t devHandle) if (uDeviceGetInstance(devHandle, &pDevInstance) == 0) { pContext = (uDeviceCellContext_t *) pDevInstance->pContext; if ((pContext == NULL) || - (uPortGetTickTimeMs() < pContext->stopTimeMs)) { + !uTimeoutExpiredMs(pContext->timeoutStop.timeoutStart, + pContext->timeoutStop.durationMs)) { keepGoing = true; } } @@ -175,8 +178,8 @@ int32_t uNetworkPrivateChangeStateCell(uDeviceHandle_t devHandle, if (timeoutSeconds <= 0) { timeoutSeconds = U_CELL_NET_CONNECT_TIMEOUT_SECONDS; } - pContext->stopTimeMs = uPortGetTickTimeMs() + - (((int64_t) timeoutSeconds) * 1000); + pContext->timeoutStop.timeoutStart = uTimeoutStart(); + pContext->timeoutStop.durationMs = timeoutSeconds * 1000; } if (upNotDown) { // Set the authentication mode diff --git a/common/network/src/u_network_private_wifi.c b/common/network/src/u_network_private_wifi.c index 57a46b83..cea55001 100644 --- a/common/network/src/u_network_private_wifi.c +++ b/common/network/src/u_network_private_wifi.c @@ -40,6 +40,8 @@ #include "u_cfg_os_platform_specific.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" @@ -249,8 +251,8 @@ static inline void statusQueueClear(const uPortQueueHandle_t queueHandle) static inline int32_t statusQueueWaitForWifiDisabled(const uPortQueueHandle_t queueHandle, int32_t timeoutSec) { - int32_t startTime = (int32_t)uPortGetTickTimeMs(); - while ((int32_t)uPortGetTickTimeMs() - startTime < timeoutSec * 1000) { + uTimeoutStart_t timeoutStart = uTimeoutStart(); + while (!uTimeoutExpiredSeconds(timeoutStart, timeoutSec)) { uStatusMessage_t msg; int32_t errorCode = uPortQueueTryReceive(queueHandle, 1000, &msg); if ((errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) && @@ -265,8 +267,8 @@ static inline int32_t statusQueueWaitForWifiDisabled(const uPortQueueHandle_t qu static inline int32_t statusQueueWaitForWifiConnected(const uPortQueueHandle_t queueHandle, int32_t timeoutSec) { - int32_t startTime = (int32_t)uPortGetTickTimeMs(); - while ((int32_t)uPortGetTickTimeMs() - startTime < timeoutSec * 1000) { + uTimeoutStart_t timeoutStart = uTimeoutStart(); + while (!uTimeoutExpiredSeconds(timeoutStart, timeoutSec)) { uStatusMessage_t msg; int32_t errorCode = uPortQueueTryReceive(queueHandle, 1000, &msg); if ((errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) && @@ -283,8 +285,8 @@ static inline int32_t statusQueueWaitForNetworkUp(const uPortQueueHandle_t queue static const uint32_t desiredNetStatusMask = U_WIFI_STATUS_MASK_IPV4_UP | U_WIFI_STATUS_MASK_IPV6_UP; uint32_t lastNetStatusMask = 0; - int32_t startTime = (int32_t)uPortGetTickTimeMs(); - while ((int32_t)uPortGetTickTimeMs() - startTime < timeoutSec * 1000) { + uTimeoutStart_t timeoutStart = uTimeoutStart(); + while (!uTimeoutExpiredSeconds(timeoutStart, timeoutSec)) { uStatusMessage_t msg; int32_t errorCode = uPortQueueTryReceive(queueHandle, 1000, &msg); if (errorCode == (int32_t) U_ERROR_COMMON_SUCCESS) { diff --git a/common/network/test/u_network_test.c b/common/network/test/u_network_test.c index b8d18441..2ba9bfc9 100644 --- a/common/network/test/u_network_test.c +++ b/common/network/test/u_network_test.c @@ -56,6 +56,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #ifdef U_CFG_TEST_CELL_MODULE_TYPE # include "u_cell_module_type.h" # include "u_cell_test_cfg.h" // For the cellular test macros @@ -184,7 +186,7 @@ static uPortSemaphoreHandle_t gBleConnectionSem = NULL; /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Keep track of the current network handle so that the * keepGoingCallback() can check it. @@ -345,7 +347,8 @@ static bool keepGoingCallback(uDeviceHandle_t devHandle) bool keepGoing = true; U_PORT_TEST_ASSERT(devHandle == gDevHandle); - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -752,7 +755,6 @@ U_PORT_TEST_FUNCTION("[network]", "networkLoc") const uLocationTestCfg_t *pLocationCfg; int32_t y; uLocation_t location; - int64_t startTime; int32_t resourceCount; // In case a previous test failed @@ -799,8 +801,8 @@ U_PORT_TEST_FUNCTION("[network]", "networkLoc") // Just take the first one, we don't care which as this // is a network test not a location test pLocationCfg = gpULocationTestCfg[pTmp->networkType]->pCfgData[0]; - startTime = uPortGetTickTimeMs(); - gStopTimeMs = startTime + U_LOCATION_TEST_CFG_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_LOCATION_TEST_CFG_TIMEOUT_SECONDS * 1000; uLocationTestResetLocation(&location); U_TEST_PRINT_LINE("getting location using %s.", gpULocationTestTypeStr[pLocationCfg->locationType]); @@ -810,8 +812,8 @@ U_PORT_TEST_FUNCTION("[network]", "networkLoc") pLocationCfg->pAuthenticationTokenStr, &location, keepGoingCallback); if (y == 0) { - U_TEST_PRINT_LINE("location establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTime) / 1000); + U_TEST_PRINT_LINE("location establishment took %u second(s).", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); } // If we are running on a local cellular network we won't get position but // we should always get time diff --git a/common/security/src/u_security_credential.c b/common/security/src/u_security_credential.c index 75d7c2c9..d898a285 100644 --- a/common/security/src/u_security_credential.c +++ b/common/security/src/u_security_credential.c @@ -50,6 +50,8 @@ #include "u_port_os.h" #include "u_port_heap.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" diff --git a/common/security/test/u_security_test.c b/common/security/test/u_security_test.c index d2e84aa1..93efe8e6 100644 --- a/common/security/test/u_security_test.c +++ b/common/security/test/u_security_test.c @@ -55,6 +55,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #ifdef U_CFG_TEST_CELL_MODULE_TYPE #include "u_cell_module_type.h" #include "u_cell_test_cfg.h" // For the cellular test macros @@ -94,7 +96,7 @@ #ifdef U_CFG_SECURITY_DEVICE_PROFILE_UID /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; #endif /* ---------------------------------------------------------------- @@ -107,7 +109,8 @@ static bool keepGoingCallback() { bool keepGoing = true; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -221,8 +224,8 @@ U_PORT_TEST_FUNCTION("[security]", "securitySeal") " number \"%s\"...", U_PORT_STRINGIFY_QUOTED(U_CFG_SECURITY_DEVICE_PROFILE_UID), serialNumber); - gStopTimeMs = uPortGetTickTimeMs() + - (U_SECURITY_TEST_SEAL_TIMEOUT_SECONDS * 1000); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_SECURITY_TEST_SEAL_TIMEOUT_SECONDS * 1000; if (uSecuritySealSet(devHandle, U_PORT_STRINGIFY_QUOTED(U_CFG_SECURITY_DEVICE_PROFILE_UID), serialNumber, keepGoingCallback) == 0) { diff --git a/common/security/test/u_security_tls_test.c b/common/security/test/u_security_tls_test.c index 1617be15..e60b017e 100644 --- a/common/security/test/u_security_tls_test.c +++ b/common/security/test/u_security_tls_test.c @@ -43,6 +43,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_port.h" #include "u_port_os.h" #include "u_port_heap.h" @@ -163,18 +165,18 @@ static size_t send(uSockDescriptor_t descriptor, { int32_t x; size_t sentSizeBytes = 0; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; U_TEST_PRINT_LINE("sending %d byte(s) of data...", sizeBytes); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((sentSizeBytes < sizeBytes) && - ((uPortGetTickTimeMs() - startTimeMs) < 10000)) { + !uTimeoutExpiredSeconds(timeoutStart, 10)) { x = uSockWrite(descriptor, (const void *) pData, sizeBytes - sentSizeBytes); if (x > 0) { sentSizeBytes += x; U_TEST_PRINT_LINE("sent %d byte(s) of data @%d ms.", - sentSizeBytes, (int32_t) uPortGetTickTimeMs()); + sentSizeBytes, uPortGetTickTimeMs()); } } @@ -213,7 +215,7 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsSock") uDeviceHandle_t devHandle = NULL; uSockDescriptor_t descriptor = -1; uSockAddress_t remoteAddress; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t sizeBytes; size_t offset; int32_t y; @@ -362,7 +364,7 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsSock") U_PORT_TEST_ASSERT(send(descriptor, gData, sizeof(gData) - 1) == sizeof(gData) - 1); U_TEST_PRINT_LINE("%d byte(s) sent via TCP @%d ms, now receiving...", - sizeof(gData) - 1, (int32_t) uPortGetTickTimeMs()); + sizeof(gData) - 1, uPortGetTickTimeMs()); // ...and capture them all again afterwards uPortFree(gpDataReceived); // In case the previous test failed @@ -371,12 +373,12 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsSock") //lint -e(668) Suppress possible use of NULL pointer // for gpDataReceived memset(gpDataReceived, 0, sizeof(gData) - 1); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); offset = 0; //lint -e{441} Suppress loop variable not found in // condition: we're using time instead for (y = 0; (offset < sizeof(gData) - 1) && - (uPortGetTickTimeMs() - startTimeMs < 20000); y++) { + !uTimeoutExpiredSeconds(timeoutStart, 20); y++) { sizeBytes = uSockRead(descriptor, gpDataReceived + offset, (sizeof(gData) - 1) - offset); @@ -387,12 +389,12 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsSock") } sizeBytes = offset; if (sizeBytes < sizeof(gData) - 1) { - U_TEST_PRINT_LINE("only %d byte(s) received after %d ms.", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE("only %d byte(s) received after %u ms.", sizeBytes, + uTimeoutElapsedMs(timeoutStart)); } else { - U_TEST_PRINT_LINE("all %d byte(s) received back after %d ms, checking" + U_TEST_PRINT_LINE("all %d byte(s) received back after %u ms, checking" " if they were as expected...", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + uTimeoutElapsedMs(timeoutStart)); } // Check that we reassembled everything correctly @@ -451,7 +453,7 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsUdpSock") uDeviceHandle_t devHandle = NULL; uSockDescriptor_t descriptor = -1; uSockAddress_t remoteAddress; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t sizeBytes; size_t offset; int32_t y; @@ -600,7 +602,7 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsUdpSock") U_PORT_TEST_ASSERT(send(descriptor, gData, sizeof(gData) - 1) == sizeof(gData) - 1); U_TEST_PRINT_LINE("%d byte(s) sent via UDP @%d ms, now receiving...", - sizeof(gData) - 1, (int32_t) uPortGetTickTimeMs()); + sizeof(gData) - 1, uPortGetTickTimeMs()); // ...and capture them all again afterwards uPortFree(gpDataReceived); // In case the previous test failed @@ -609,12 +611,12 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsUdpSock") //lint -e(668) Suppress possible use of NULL pointer // for gpDataReceived memset(gpDataReceived, 0, sizeof(gData) - 1); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); offset = 0; //lint -e{441} Suppress loop variable not found in // condition: we're using time instead for (y = 0; (offset < sizeof(gData) - 1) && - (uPortGetTickTimeMs() - startTimeMs < 20000); y++) { + !uTimeoutExpiredSeconds(timeoutStart, 20); y++) { sizeBytes = uSockRead(descriptor, gpDataReceived + offset, (sizeof(gData) - 1) - offset); @@ -625,12 +627,12 @@ U_PORT_TEST_FUNCTION("[securityTls]", "securityTlsUdpSock") } sizeBytes = offset; if (sizeBytes < sizeof(gData) - 1) { - U_TEST_PRINT_LINE("only %d byte(s) received after %d ms.", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE("only %d byte(s) received after %u ms.", sizeBytes, + uTimeoutElapsedMs(timeoutStart)); } else { - U_TEST_PRINT_LINE("all %d byte(s) received back after %d ms, checking" + U_TEST_PRINT_LINE("all %d byte(s) received back after %u ms, checking" " if they were as expected...", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + uTimeoutElapsedMs(timeoutStart)); } // Check that we reassembled everything correctly diff --git a/common/short_range/src/gen2/u_short_range.c b/common/short_range/src/gen2/u_short_range.c index eb368c09..294b2a41 100644 --- a/common/short_range/src/gen2/u_short_range.c +++ b/common/short_range/src/gen2/u_short_range.c @@ -49,6 +49,8 @@ #include "u_port_uart.h" #include "u_compiler.h" +#include "u_timeout.h" + #include "u_cx_at_client.h" #include "u_cx_general.h" #include "u_cx_system.h" diff --git a/common/short_range/src/gen2/u_short_range_cfg.c b/common/short_range/src/gen2/u_short_range_cfg.c index 4ba4fca6..f6bdb363 100644 --- a/common/short_range/src/gen2/u_short_range_cfg.c +++ b/common/short_range/src/gen2/u_short_range_cfg.c @@ -43,6 +43,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range.h" diff --git a/common/short_range/src/gen2/u_short_range_private.c b/common/short_range/src/gen2/u_short_range_private.c index 466ad299..a140e575 100644 --- a/common/short_range/src/gen2/u_short_range_private.c +++ b/common/short_range/src/gen2/u_short_range_private.c @@ -34,6 +34,8 @@ #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" diff --git a/common/short_range/src/u_short_range.c b/common/short_range/src/u_short_range.c index 1b9b6dc9..8545778a 100644 --- a/common/short_range/src/u_short_range.c +++ b/common/short_range/src/u_short_range.c @@ -47,6 +47,8 @@ #include "u_port_gpio.h" #include "u_port_uart.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_linked_list.h" @@ -237,7 +239,7 @@ static int32_t uShortRangeAdd(uShortRangeModuleType_t moduleType, pInstance->devHandle = pDevInstance; pInstance->atHandle = atHandle; pInstance->mode = U_SHORT_RANGE_MODE_EDM; - pInstance->startTimeMs = 500; + pInstance->timeoutStart = uTimeoutStart(); pInstance->urcConHandlerSet = false; pInstance->sockNextLocalPort = -1; pInstance->uartHandle = uartHandle; diff --git a/common/short_range/src/u_short_range_cfg.c b/common/short_range/src/u_short_range_cfg.c index b9f4f0d5..bf7fbfba 100644 --- a/common/short_range/src/u_short_range_cfg.c +++ b/common/short_range/src/u_short_range_cfg.c @@ -43,6 +43,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range.h" diff --git a/common/short_range/src/u_short_range_edm_stream.c b/common/short_range/src/u_short_range_edm_stream.c index 3465b26e..09bb38ef 100644 --- a/common/short_range/src/u_short_range_edm_stream.c +++ b/common/short_range/src/u_short_range_edm_stream.c @@ -39,6 +39,7 @@ #include "u_port_event_queue.h" #include "u_port_uart.h" #include "u_port_debug.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_short_range_pbuf.h" #include "u_short_range_module_type.h" @@ -1396,8 +1397,7 @@ int32_t uShortRangeEdmStreamWrite(int32_t handle, int32_t channel, char head[U_SHORT_RANGE_EDM_DATA_HEAD_SIZE]; char tail[U_SHORT_RANGE_EDM_TAIL_SIZE]; sizeOrErrorCode = 0; - int64_t startTime = uPortGetTickTimeMs(); - int64_t endTime; + uTimeoutStart_t timeoutStart = uTimeoutStart(); do { send = ((int32_t)sizeBytes - sizeOrErrorCode); @@ -1429,9 +1429,8 @@ int32_t uShortRangeEdmStreamWrite(int32_t handle, int32_t channel, } else { sizeOrErrorCode += send; } - endTime = uPortGetTickTimeMs(); } while (((int32_t)sizeBytes > sizeOrErrorCode) && - (endTime - startTime < timeoutMs)); + !uTimeoutExpiredMs(timeoutStart, timeoutMs)); } } U_PORT_MUTEX_UNLOCK(gMutex); diff --git a/common/short_range/src/u_short_range_private.c b/common/short_range/src/u_short_range_private.c index c7b7a75c..36dd9642 100644 --- a/common/short_range/src/u_short_range_private.c +++ b/common/short_range/src/u_short_range_private.c @@ -34,6 +34,8 @@ #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_device_shared.h" diff --git a/common/short_range/src/u_short_range_private.h b/common/short_range/src/u_short_range_private.h index e5a0f26a..ac7d0517 100644 --- a/common/short_range/src/u_short_range_private.h +++ b/common/short_range/src/u_short_range_private.h @@ -149,7 +149,7 @@ typedef struct uShortRangePrivateInstance_t { uAtClientHandle_t atHandle; /**< the AT client handle to use. */ int32_t streamHandle; /**< handle to the underlaying stream. */ uAtClientStream_t streamType; /**< stream type. */ - int32_t startTimeMs; /**< used while restarting. */ + uTimeoutStart_t timeoutStart; /**< used while restarting. */ int32_t ticksLastRestart; bool urcConHandlerSet; int32_t sockNextLocalPort; diff --git a/common/short_range/test/u_short_range_test.c b/common/short_range/test/u_short_range_test.c index e0e36ecb..5116346a 100644 --- a/common/short_range/test/u_short_range_test.c +++ b/common/short_range/test/u_short_range_test.c @@ -55,6 +55,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #ifdef U_CFG_TEST_SHORT_RANGE_MODULE_TYPE #include "u_ble_sps.h" #include "u_short_range_private.h" diff --git a/common/short_range/test/u_short_range_test_private.c b/common/short_range/test/u_short_range_test_private.c index 66016352..b100f2f2 100644 --- a/common/short_range/test/u_short_range_test_private.c +++ b/common/short_range/test/u_short_range_test_private.c @@ -42,6 +42,8 @@ #include "u_port_debug.h" #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" //lint -efile(766, u_ble_sps.h) diff --git a/common/sock/src/u_sock.c b/common/sock/src/u_sock.c index 09e1d831..5a306ce1 100644 --- a/common/sock/src/u_sock.c +++ b/common/sock/src/u_sock.c @@ -324,6 +324,8 @@ #include "u_device_shared.h" +#include "u_timeout.h" + #include "u_port_clib_platform_specific.h" /* struct timeval in some cases and integer stdio, must be included before the other port files if @@ -1291,7 +1293,7 @@ static int32_t receive(const uSockContainer_t *pContainer, uDeviceHandle_t devHandle = pContainer->socket.devHandle; int32_t sockHandle = pContainer->socket.sockHandle; int32_t negErrnoOrSize = -U_SOCK_ENOSYS; - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); int32_t devType = uDeviceGetDeviceType(devHandle); // Run around the loop until a packet of data turns up @@ -1333,8 +1335,8 @@ static int32_t receive(const uSockContainer_t *pContainer, } } while ((negErrnoOrSize < 0) && (pContainer->socket.blocking) && - (uPortGetTickTimeMs() - startTimeMs < - pContainer->socket.receiveTimeoutMs)); + !uTimeoutExpiredMs(timeoutStart, + pContainer->socket.receiveTimeoutMs)); return negErrnoOrSize; } @@ -1724,7 +1726,7 @@ int32_t uSockOptionSet(uSockDescriptor_t descriptor, (((int64_t) ((const struct timeval *) pOptionValue)->tv_sec) * 1000); uPortLog("U_SOCK: timeout for socket descriptor" " %d set to %d ms.\n", descriptor, - (int32_t) pContainer->socket.receiveTimeoutMs); + pContainer->socket.receiveTimeoutMs); } else { uPortLog("U_SOCK: socket option %d:0x%04x" " could not be set to value ", @@ -1827,7 +1829,7 @@ int32_t uSockOptionGet(uSockDescriptor_t descriptor, *pOptionValueLength = sizeof(struct timeval); uPortLog("U_SOCK: timeout for socket descriptor" " %d is %d ms.\n", descriptor, - (int32_t) pContainer->socket.receiveTimeoutMs); + pContainer->socket.receiveTimeoutMs); } } else { errnoLocal = U_SOCK_ENONE; diff --git a/common/sock/test/u_sock_test.c b/common/sock/test/u_sock_test.c index 106b843f..ab3c1f4f 100644 --- a/common/sock/test/u_sock_test.c +++ b/common/sock/test/u_sock_test.c @@ -66,6 +66,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" // In order to provide a comms #include "u_network_test_shared_cfg.h" // path for the socket @@ -574,7 +576,7 @@ static int32_t doUdpEchoBasic(uSockDescriptor_t descriptor, int32_t sentSizeBytes; int32_t receivedSizeBytes = 0; #if U_CFG_ENABLE_LOGGING - int64_t timeNowMs; + uTimeoutStart_t timeoutStart; #endif uPortFree(gpDataReceived2); @@ -598,7 +600,7 @@ static int32_t doUdpEchoBasic(uSockDescriptor_t descriptor, } if (sentSizeBytes == (int32_t)sendSizeBytes) { #if U_CFG_ENABLE_LOGGING - timeNowMs = (int32_t) uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); #endif //lint -e(668) Suppress possible use of NULL pointer // for gpDataReceived2 (it is checked above) @@ -614,8 +616,8 @@ static int32_t doUdpEchoBasic(uSockDescriptor_t descriptor, printAddress(&senderAddress, true); uPortLog(".\n"); } else { - U_TEST_PRINT_LINE("received no UDP data back after %d ms.", - (int32_t) (uPortGetTickTimeMs() - timeNowMs)); + U_TEST_PRINT_LINE("received no UDP data back after %u ms.", + uTimeoutElapsedMs(timeoutStart)); // Reset errno 'cos we're going to retry and subsequent things might be upset by it errno = 0; } @@ -679,7 +681,7 @@ static void rxAsyncEventTask(void *pParameter, size_t parameterLength) } if (sizeBytes > 0) { U_TEST_PRINT_LINE("received %d byte(s) of data @%d ms.", - sizeBytes, (int32_t) uPortGetTickTimeMs()); + sizeBytes, uPortGetTickTimeMs()); pTestConfig->bytesReceived += sizeBytes; pTestConfig->packetsReceived++; } @@ -692,12 +694,12 @@ static size_t sendTcp(uSockDescriptor_t descriptor, { int32_t x; size_t sentSizeBytes = 0; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; U_TEST_PRINT_LINE("sending %d byte(s) of TCP data...", sizeBytes); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((sentSizeBytes < sizeBytes) && - ((uPortGetTickTimeMs() - startTimeMs) < 10000)) { + !uTimeoutExpiredSeconds(timeoutStart, 10)) { x = uSockWrite(descriptor, (const void *) pData, sizeBytes - sentSizeBytes); if (x > 0) { @@ -708,7 +710,7 @@ static size_t sendTcp(uSockDescriptor_t descriptor, sentSizeBytes += x; pData += x; U_TEST_PRINT_LINE("sent %d byte(s) of TCP data @%d ms.", - sentSizeBytes, (int32_t) uPortGetTickTimeMs()); + sentSizeBytes, uPortGetTickTimeMs()); } } @@ -1093,7 +1095,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockBasicTcp") size_t sizeBytes; size_t offset; int32_t y; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t resourceCount; // Call clean up to release OS resources that may @@ -1188,7 +1190,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockBasicTcp") } sizeBytes = offset; U_TEST_PRINT_LINE("%d byte(s) sent via TCP @%d ms, now receiving...", - sizeBytes, (int32_t) uPortGetTickTimeMs()); + sizeBytes, uPortGetTickTimeMs()); // Check if the uSockTotalBytesSent() matches value of sizeBytes U_PORT_TEST_ASSERT(uSockGetTotalBytesSent(descriptor) == (int32_t)sizeBytes); @@ -1203,12 +1205,12 @@ U_PORT_TEST_FUNCTION("[sock]", "sockBasicTcp") memset(gpDataReceived1, U_SOCK_TEST_FILL_CHARACTER, (sizeof(gSendData) - 1) + (U_SOCK_TEST_GUARD_LENGTH_SIZE_BYTES * 2)); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); offset = 0; //lint -e{441} Suppress loop variable not found in // condition: we're using time instead for (y = 0; (offset < sizeof(gSendData) - 1) && - (uPortGetTickTimeMs() - startTimeMs < 20000); y++) { + !uTimeoutExpiredSeconds(timeoutStart, 20); y++) { sizeBytes = uSockRead(descriptor, gpDataReceived1 + offset + U_SOCK_TEST_GUARD_LENGTH_SIZE_BYTES, @@ -1220,12 +1222,12 @@ U_PORT_TEST_FUNCTION("[sock]", "sockBasicTcp") } sizeBytes = offset; if (sizeBytes < sizeof(gSendData) - 1) { - U_TEST_PRINT_LINE("only %d byte(s) received after %d ms.", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE("only %d byte(s) received after %u ms.", sizeBytes, + uTimeoutElapsedMs(timeoutStart)); } else { - U_TEST_PRINT_LINE("all %d byte(s) received back after %d ms," + U_TEST_PRINT_LINE("all %d byte(s) received back after %u ms," " checking if they were as expected...", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + uTimeoutElapsedMs(timeoutStart)); } // Check that we reassembled everything correctly @@ -1431,7 +1433,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockOptionsSetGet") size_t *pLength; struct timeval timeout; char *pData[1]; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t timeoutMs; int32_t elapsedMs; int32_t resourceCount; @@ -1489,14 +1491,14 @@ U_PORT_TEST_FUNCTION("[sock]", "sockOptionsSetGet") pLength) == 0); timeoutMs = ((int32_t) timeout.tv_sec) * 1000 + timeout.tv_usec / 1000; U_PORT_TEST_ASSERT(timeoutMs == U_SOCK_RECEIVE_TIMEOUT_DEFAULT_MS); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockReceiveFrom(descriptor, NULL, pData, sizeof(pData)) < 0); - elapsedMs = uPortGetTickTimeMs() - startTimeMs; + elapsedMs = uTimeoutElapsedMs(timeoutStart); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; U_TEST_PRINT_LINE("uSockReceiveFrom() of nothing took %d" - " millisecond(s)...", (int32_t) elapsedMs); + " millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs > timeoutMs - U_SOCK_TEST_TIME_MARGIN_MINUS_MS); U_PORT_TEST_ASSERT(elapsedMs < timeoutMs + @@ -1505,22 +1507,21 @@ U_PORT_TEST_FUNCTION("[sock]", "sockOptionsSetGet") timeout.tv_usec = 500000; timeoutMs = (((int32_t) timeout.tv_sec) * 1000) + (timeout.tv_usec / 1000); - U_TEST_PRINT_LINE("setting timeout to %d millisecond(s)...", - (int32_t) timeoutMs); + U_TEST_PRINT_LINE("setting timeout to %d millisecond(s)...", timeoutMs); U_PORT_TEST_ASSERT(uSockOptionSet(descriptor, U_SOCK_OPT_LEVEL_SOCK, U_SOCK_OPT_RCVTIMEO, (void *) &timeout, sizeof(timeout)) == 0); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockReceiveFrom(descriptor, NULL, pData, sizeof(pData)) < 0); - elapsedMs = uPortGetTickTimeMs() - startTimeMs; + elapsedMs = uTimeoutElapsedMs(timeoutStart); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; U_TEST_PRINT_LINE("uSockReceiveFrom() of nothing took %d" - " millisecond(s)...", (int32_t) elapsedMs); + " millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs > timeoutMs - U_SOCK_TEST_TIME_MARGIN_MINUS_MS); U_PORT_TEST_ASSERT(elapsedMs < timeoutMs + @@ -1558,7 +1559,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockLocalPort") uSockAddress_t remoteAddress; uSockDescriptor_t descriptor; bool closedCallbackCalled; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t sizeBytes; size_t offset; int32_t y; @@ -1643,7 +1644,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockLocalPort") } sizeBytes = offset; U_TEST_PRINT_LINE("%d byte(s) sent via TCP @%d ms, now receiving...", - sizeBytes, (int32_t) uPortGetTickTimeMs()); + sizeBytes, uPortGetTickTimeMs()); uPortFree(gpDataReceived1); gpDataReceived1 = (char *) pUPortMalloc((sizeof(gSendData) - 1) + (U_SOCK_TEST_GUARD_LENGTH_SIZE_BYTES * 2)); @@ -1653,12 +1654,12 @@ U_PORT_TEST_FUNCTION("[sock]", "sockLocalPort") memset(gpDataReceived1, U_SOCK_TEST_FILL_CHARACTER, (sizeof(gSendData) - 1) + (U_SOCK_TEST_GUARD_LENGTH_SIZE_BYTES * 2)); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); offset = 0; //lint -e{441} Suppress loop variable not found in // condition: we're using time instead for (y = 0; (offset < sizeof(gSendData) - 1) && - (uPortGetTickTimeMs() - startTimeMs < 20000); y++) { + !uTimeoutExpiredSeconds(timeoutStart, 20); y++) { sizeBytes = uSockRead(descriptor, gpDataReceived1 + offset + U_SOCK_TEST_GUARD_LENGTH_SIZE_BYTES, @@ -1670,12 +1671,12 @@ U_PORT_TEST_FUNCTION("[sock]", "sockLocalPort") } sizeBytes = offset; if (sizeBytes < sizeof(gSendData) - 1) { - U_TEST_PRINT_LINE("only %d byte(s) received after %d ms.", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + U_TEST_PRINT_LINE("only %d byte(s) received after %u ms.", sizeBytes, + uTimeoutElapsedMs(timeoutStart)); } else { - U_TEST_PRINT_LINE("all %d byte(s) received back after %d ms," + U_TEST_PRINT_LINE("all %d byte(s) received back after %u ms," " checking if they were as expected...", sizeBytes, - (int32_t) (uPortGetTickTimeMs() - startTimeMs)); + uTimeoutElapsedMs(timeoutStart)); } // Check that we reassembled everything correctly @@ -1738,7 +1739,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockNonBlocking") bool isBlocking; struct timeval timeout; char *pData[1]; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t timeoutMs; int32_t elapsedMs; int32_t resourceCount; @@ -1813,26 +1814,25 @@ U_PORT_TEST_FUNCTION("[sock]", "sockNonBlocking") U_SOCK_OPT_RCVTIMEO, (void *) &timeout, sizeof(timeout)) == 0); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockReceiveFrom(descriptor, NULL, pData, sizeof(pData)) < 0); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; - elapsedMs = uPortGetTickTimeMs() - startTimeMs; + elapsedMs = uTimeoutElapsedMs(timeoutStart); U_TEST_PRINT_LINE("uSockReceiveFrom() of nothing took %d" - " millisecond(s)...", (int32_t) elapsedMs); + " millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs > timeoutMs - U_SOCK_TEST_TIME_MARGIN_MINUS_MS); U_PORT_TEST_ASSERT(elapsedMs < timeoutMs + U_SOCK_TEST_TIME_MARGIN_PLUS_MS); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockRead(descriptor, pData, sizeof(pData)) < 0); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; - elapsedMs = uPortGetTickTimeMs() - startTimeMs; - U_TEST_PRINT_LINE("uSockRead() of nothing took %d millisecond(s)...", - (int32_t) elapsedMs); + elapsedMs = uTimeoutElapsedMs(timeoutStart); + U_TEST_PRINT_LINE("uSockRead() of nothing took %d millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs > timeoutMs - U_SOCK_TEST_TIME_MARGIN_MINUS_MS); U_PORT_TEST_ASSERT(elapsedMs < timeoutMs + @@ -1850,24 +1850,24 @@ U_PORT_TEST_FUNCTION("[sock]", "sockNonBlocking") U_PORT_TEST_ASSERT(!uSockBlockingGet(descriptor)); U_TEST_PRINT_LINE("check that it has worked for receive..."); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockReceiveFrom(descriptor, NULL, pData, sizeof(pData)) < 0); - elapsedMs = uPortGetTickTimeMs() - startTimeMs; + elapsedMs = uTimeoutElapsedMs(timeoutStart); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; U_TEST_PRINT_LINE("uSockReceiveFrom() of nothing with blocking off" - " took %d millisecond(s)...", (int32_t) elapsedMs); + " took %d millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs < U_SOCK_TEST_NON_BLOCKING_TIME_MS + U_SOCK_TEST_TIME_MARGIN_PLUS_MS); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockRead(descriptor, pData, sizeof(pData)) < 0); - elapsedMs = uPortGetTickTimeMs() - startTimeMs; + elapsedMs = uTimeoutElapsedMs(timeoutStart); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; U_TEST_PRINT_LINE("uSockRead() of nothing with blocking off" - " took %d millisecond(s)...", (int32_t) elapsedMs); + " took %d millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs < U_SOCK_TEST_NON_BLOCKING_TIME_MS + U_SOCK_TEST_TIME_MARGIN_PLUS_MS); @@ -1876,26 +1876,26 @@ U_PORT_TEST_FUNCTION("[sock]", "sockNonBlocking") U_PORT_TEST_ASSERT(uSockBlockingGet(descriptor)); U_TEST_PRINT_LINE("check that we're blocking again..."); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockReceiveFrom(descriptor, NULL, pData, sizeof(pData)) < 0); - elapsedMs = uPortGetTickTimeMs() - startTimeMs; + elapsedMs = uTimeoutElapsedMs(timeoutStart); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; U_TEST_PRINT_LINE("uSockReceiveFrom() of nothing with blocking on" - " took %d millisecond(s)...", (int32_t) elapsedMs); + " took %d millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs > timeoutMs - U_SOCK_TEST_TIME_MARGIN_MINUS_MS); U_PORT_TEST_ASSERT(elapsedMs < timeoutMs + U_SOCK_TEST_TIME_MARGIN_PLUS_MS); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_PORT_TEST_ASSERT(uSockRead(descriptor, pData, sizeof(pData)) < 0); - elapsedMs = uPortGetTickTimeMs() - startTimeMs; + elapsedMs = uTimeoutElapsedMs(timeoutStart); U_PORT_TEST_ASSERT(errno == U_SOCK_EWOULDBLOCK); errno = 0; U_TEST_PRINT_LINE("uSockRead() of nothing with blocking on took" - " %d millisecond(s)...", (int32_t) elapsedMs); + " %d millisecond(s)...", elapsedMs); U_PORT_TEST_ASSERT(elapsedMs > timeoutMs - U_SOCK_TEST_TIME_MARGIN_MINUS_MS); U_PORT_TEST_ASSERT(elapsedMs < timeoutMs + @@ -1949,7 +1949,7 @@ U_PORT_TEST_FUNCTION("[sock]", "sockUdpEchoNonPingPong") // if U_CFG_ENABLE_LOGGING is 0) int32_t y; int32_t z; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t resourceCount; // Call clean up to release OS resources that may @@ -2047,12 +2047,12 @@ U_PORT_TEST_FUNCTION("[sock]", "sockUdpEchoNonPingPong") // for gpDataReceived1 (it is checked above) memset(gpDataReceived1, U_SOCK_TEST_FILL_CHARACTER, (sizeof(gSendData) - 1) + (U_SOCK_TEST_GUARD_LENGTH_SIZE_BYTES * 2)); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); offset = 0; //lint -e{441} Suppress loop variable not found in // condition: we're using time instead for (y = 0; (offset < sizeof(gSendData) - 1) && - (uPortGetTickTimeMs() - startTimeMs < 15000); y++) { + !uTimeoutExpiredSeconds(timeoutStart, 15); y++) { z = uSockReceiveFrom(descriptor, NULL, gpDataReceived1 + offset + U_SOCK_TEST_GUARD_LENGTH_SIZE_BYTES, diff --git a/common/timeout/api/u_timeout.h b/common/timeout/api/u_timeout.h new file mode 100644 index 00000000..527c95e1 --- /dev/null +++ b/common/timeout/api/u_timeout.h @@ -0,0 +1,148 @@ +/* + * Copyright 2019-2024 u-blox + * + * 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. + */ + +#ifndef _U_TIMEOUT_H_ +#define _U_TIMEOUT_H_ + +/* Only header files representing a direct and unavoidable + * dependency between the API of this module and the API + * of another module should be included here; otherwise + * please keep #includes to your .c files. */ + +/** \addtogroup __timeout __Timeout handling + * @{ + */ + +/** @file + * @brief Functions to handle time-outs in a wrap-safe manner. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/* ---------------------------------------------------------------- + * COMPILE-TIME MACROS + * -------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- + * TYPES + * -------------------------------------------------------------- */ + +/** "Anonymous" structure to hold the start time, used in time-out + * calculations. The contents of this structure MUST NEVER BE + * REFERENCED except by the code here. + */ +typedef struct { + uint32_t timeMs; +} uTimeoutStart_t; + +/** It is sometimes necessary to carry around a start time and + * a duration in order to effect a "stop time". This structure + * may be used for convenience. + */ +typedef struct { + uTimeoutStart_t timeoutStart; + uint32_t durationMs; /** you might use a duration of 0 to mean + "not set", but you MUST THEN CHECK + this YOURSELF before passing the contents + of this structure into uTimeoutExpiredMs() + or uTimeoutExpiredSeconds(), otherwise + the time-out will expire IMMEDIATELY. */ +} uTimeoutStop_t; + +/* ---------------------------------------------------------------- + * FUNCTIONS + * -------------------------------------------------------------- */ + +/** Initialise a time-out with the current time; the value returned + * by this function may be passed to the uTimeoutExpiredMs(), + * uTimeoutExpiredSeconds(), uTimeoutElapsedMs() or + * uTimeoutElapsedSeconds() functions, which must be used for + * wrap-safe time handling. + * + * The underlying source of the tick is uPortGetTickTimeMs() and the + * same restrictions apply. + * + * @return the current time in a form that can be used for time-out + * checks. + */ +uTimeoutStart_t uTimeoutStart(); + +/** Perform a time-out check in a way that will behave predictably + * across a tick-counter wrap. See also uTimeoutExpiredSeconds(). + * + * Where you might have been going to write: + * + * ``` + * int32_t startTimeMs = uPortGetTickTimeMs(); + * if (uPortGetTickTimeMs() - startTimeMs > timeoutMs) { + * // Do something because the time-out has expired + * } + * ``` + * + * ...then write this instead: + * + * ``` + * uTimeoutStart_t timeoutStart = uTimeoutStart(); + * if (uTimeoutExpiredMs(timeoutStart, timeoutMs)) { + * // Do something because the time-out has expired + * } + * ``` + * + * @param startTime the start time, populated using uTimeoutStart(). + * @param durationMs the duration of the time-out in milliseconds. + * @return true if the given duration has passed + * since the start time, else false. + */ +bool uTimeoutExpiredMs(uTimeoutStart_t startTime, uint32_t durationMs); + +/** As uTimeoutExpiredMs() but for values in seconds. + * + * @param startTime the start time, populated using uTimeoutStart(). + * @param durationSeconds the duration of the time-out in seconds. + * @return true if the given duration has passed + * since the start time, else false. + */ +bool uTimeoutExpiredSeconds(uTimeoutStart_t startTime, + uint32_t durationSeconds); + +/** Return how much time has passed since the start of a time-out. + * See also uTimeoutElapsedSeconds(). + * + * @param startTime the start time, populated using uTimeoutStart(). + * @return the amount of time that has elapsed since + * startTime in milliseconds. + */ +uint32_t uTimeoutElapsedMs(uTimeoutStart_t startTime); + +/** As uTimeoutElapsedMs() but returning a value in seconds. + * + * @param startTime the start time, populated using uTimeoutStart(). + * @return the amount of time that has elapsed since + * startTime in seconds. + */ +uint32_t uTimeoutElapsedSeconds(uTimeoutStart_t startTime); + +#ifdef __cplusplus +} +#endif + +/** @}*/ + +#endif // _U_TIMEOUT_H_ + +// End of file diff --git a/common/timeout/src/u_timeout.c b/common/timeout/src/u_timeout.c new file mode 100644 index 00000000..64587d31 --- /dev/null +++ b/common/timeout/src/u_timeout.c @@ -0,0 +1,129 @@ +/* + * Copyright 2019-2024 u-blox + * + * 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. + */ + +/** @file + * @brief Timeout functions. + */ + +#ifdef U_CFG_OVERRIDE +# include "u_cfg_override.h" // For a customer's configuration override +#endif + +/* ---------------------------------------------------------------- + * INCLUDE FILES + * -------------------------------------------------------------- */ + +#include "stddef.h" // NULL, size_t etc. +#include "stdint.h" // int32_t etc. +#include "stdbool.h" + +#include "u_timeout.h" + +#include "u_port.h" + +/* ---------------------------------------------------------------- + * COMPILE-TIME MACROS + * -------------------------------------------------------------- */ + +#ifndef U_CFG_TEST_TIMEOUT_SPEED_UP +/** Used during testing only, this allows the code here to perceive + * the underlying tick as running faster in powers of 2, meaning + * that it sees more tick timer wraps, without externally altering + * the behaviour of the code (though obviously reducing the maximum + * duration of any timeout). + * + * For instance, if you set #U_CFG_TEST_TIMEOUT_SPEED_UP to 14 + * then time will be 16,384 times faster and so, with a millisecond + * tick, the wrap will be ever 262 seconds, just over 4 minutes, + * which is longer than the duration of any timers used during + * testing and shorter than a run of all tests, so would serve to + * bring the 32-bit tick-wrap into play. + */ +# define U_CFG_TEST_TIMEOUT_SPEED_UP 0 +#endif + +/* ---------------------------------------------------------------- + * TYPES + * -------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- + * VARIABLES + * -------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- + * STATIC FUNCTIONS + * -------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- + * PUBLIC FUNCTIONS + * -------------------------------------------------------------- */ + +// Initialise a time-out with the current time. +uTimeoutStart_t uTimeoutStart() +{ + uTimeoutStart_t timeoutStart; + uint32_t nowTimeMs = (uint32_t) uPortGetTickTimeMs(); +#if (U_CFG_TEST_TIMEOUT_SPEED_UP > 0) + nowTimeMs <<= U_CFG_TEST_TIMEOUT_SPEED_UP; +#endif + timeoutStart.timeMs = nowTimeMs; + return timeoutStart; +} + +// Perform a time-out check in a wrap-safe way. +bool uTimeoutExpiredMs(uTimeoutStart_t startTime, uint32_t durationMs) +{ + uint32_t nowTimeMs = (uint32_t) uPortGetTickTimeMs(); +#if (U_CFG_TEST_TIMEOUT_SPEED_UP > 0) + nowTimeMs <<= U_CFG_TEST_TIMEOUT_SPEED_UP; + durationMs <<= U_CFG_TEST_TIMEOUT_SPEED_UP; +#endif + // Move the time-frame from the unaligned free running ticks + // to 0 by subtracting the start time + uint32_t elapsedTimeMs = nowTimeMs - startTime.timeMs; + // This will evaluate to false during the next durationMs after + // uTimeoutStart() was called + return elapsedTimeMs > durationMs; +} + +// As uTimeoutExpiredMs() but for values in seconds. +bool uTimeoutExpiredSeconds(uTimeoutStart_t startTime, + uint32_t durationSeconds) +{ + return uTimeoutExpiredMs(startTime, durationSeconds * 1000); +} + +// Return how time has passed in milliseconds. +uint32_t uTimeoutElapsedMs(uTimeoutStart_t startTime) +{ + uint32_t nowTimeMs = (uint32_t) uPortGetTickTimeMs(); +#if (U_CFG_TEST_TIMEOUT_SPEED_UP > 0) + nowTimeMs <<= U_CFG_TEST_TIMEOUT_SPEED_UP; +#endif + uint32_t elapsedTimeMs = nowTimeMs - startTime.timeMs; +#if (U_CFG_TEST_TIMEOUT_SPEED_UP > 0) + elapsedTimeMs >>= U_CFG_TEST_TIMEOUT_SPEED_UP; +#endif + return elapsedTimeMs; +} + +// As uTimeoutElapsedMs() but returning a value in seconds. +uint32_t uTimeoutElapsedSeconds(uTimeoutStart_t startTime) +{ + return uTimeoutElapsedMs(startTime) / 1000; +} + +// End of file diff --git a/common/timeout/test/u_timeout_test.c b/common/timeout/test/u_timeout_test.c new file mode 100644 index 00000000..28e90e36 --- /dev/null +++ b/common/timeout/test/u_timeout_test.c @@ -0,0 +1,170 @@ +/* + * Copyright 2019-2024 u-blox + * + * 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. + */ + +/* Only #includes of u_* and the C standard library are allowed here, + * no platform stuff and no OS stuff. Anything required from + * the platform/OS must be brought in through u_port* to maintain + * portability. + */ + +/** @file + * @brief Tests for the timeout API, should run without any HW. + * + * IMPORTANT: this test only makes sense with #U_CFG_TEST_TIMEOUT_SPEED_UP + * set to 18 or more. To run this test in an acceptable time-frame (around + * a minute) please set #U_CFG_TEST_TIMEOUT_SPEED_UP to 18 when compiling + * ubxlib. The aim here is to have the tick timer wrap during testing + * and no tests to get "stuck" as a result. With a 1 ms tick, a 32-bit + * counter would wrap in 2^32 - 1 (4,294,967,295) seconds, so a + * speed-up of 18 means a wrap every 16 seconds. + */ + +#ifdef U_CFG_OVERRIDE +# include "u_cfg_override.h" // For a customer's configuration override +#endif + +#include "limits.h" +#include "stddef.h" // NULL, size_t etc. +#include "stdint.h" // int32_t etc. +#include "stdbool.h" + +#include "u_cfg_sw.h" +#include "u_cfg_app_platform_specific.h" +#include "u_cfg_test_platform_specific.h" + +#include "u_error_common.h" + +#include "u_port.h" +#include "u_port_debug.h" +#include "u_port_os.h" + +#include "u_test_util_resource_check.h" + +#include "u_timeout.h" + +/* ---------------------------------------------------------------- + * COMPILE-TIME MACROS + * -------------------------------------------------------------- */ + +/** The string to put at the start of all prints from this test. + */ +#define U_TEST_PREFIX "U_TIMEOUT_TEST: " + +/** Print a whole line, with terminator, prefixed for this test file. + */ +#define U_TEST_PRINT_LINE(format, ...) uPortLog(U_TEST_PREFIX format "\n", ##__VA_ARGS__) + +#ifndef U_TIMEOUT_NUMBER_OF_WRAPS +/** How many times we would like to go around the + * clock during testing. + */ +# define U_TIMEOUT_NUMBER_OF_WRAPS 2 +#endif + +#ifndef U_TIMEOUT_TEST_ITERATIONS +/** How many iterations we would like in total. + */ +# define U_TIMEOUT_TEST_ITERATIONS (U_TIMEOUT_NUMBER_OF_WRAPS * 5) +#endif + +#ifndef U_TIMEOUT_DURATION_MS +/** How long each timeout should be. + */ +# define U_TIMEOUT_DURATION_MS ((UINT32_MAX * U_TIMEOUT_NUMBER_OF_WRAPS) / U_TIMEOUT_TEST_ITERATIONS) +#endif + +/* ---------------------------------------------------------------- + * TYPES + * -------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- + * VARIABLES + * -------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- + * STATIC FUNCTIONS + * -------------------------------------------------------------- */ + +/* ---------------------------------------------------------------- + * PUBLIC FUNCTIONS: TESTS + * -------------------------------------------------------------- */ + +#if (U_CFG_TEST_TIMEOUT_SPEED_UP >= 18) +// A basic test that the timeout functions do not get stuck at a wrap. +U_PORT_TEST_FUNCTION("[timeout]", "timeoutWrap") +{ + int32_t resourceCount; + uTimeoutStart_t timeoutStart; + int32_t startTickMs; + int32_t stopTickMs; + uint32_t acceleratedElapsedTimeMs; + + // Whatever called us likely initialised the + // port so deinitialise it here to obtain the + // correct initial heap size + uPortDeinit(); + resourceCount = uTestUtilGetDynamicResourceCount(); + + uPortInit(); + + U_TEST_PRINT_LINE("testing timeout API at 2^%d real time," + " so one day passes in %d milliseconds" + " and a 32-bit counter will wrap every" + " %d seconds; there will be %d iterations" + " each of length %d days.", + U_CFG_TEST_TIMEOUT_SPEED_UP, + 86400000 >> U_CFG_TEST_TIMEOUT_SPEED_UP, + ((UINT32_MAX >> U_CFG_TEST_TIMEOUT_SPEED_UP) + 1) / 1000, + U_TIMEOUT_TEST_ITERATIONS, + (U_TIMEOUT_DURATION_MS << U_CFG_TEST_TIMEOUT_SPEED_UP) / 86400000); + + for (uint32_t x = 0; x < U_TIMEOUT_TEST_ITERATIONS; x++) { + U_TEST_PRINT_LINE("timeout %d: %d day(s)...", x, + (U_TIMEOUT_DURATION_MS << U_CFG_TEST_TIMEOUT_SPEED_UP) / 86400000); + timeoutStart = uTimeoutStart(); + startTickMs = uPortGetTickTimeMs(); + U_PORT_TEST_ASSERT(uTimeoutElapsedMs(timeoutStart) << U_CFG_TEST_TIMEOUT_SPEED_UP < + U_TIMEOUT_DURATION_MS); + while (!uTimeoutExpiredMs(timeoutStart, U_TIMEOUT_DURATION_MS)) { + uPortTaskBlock(10); + } + U_PORT_TEST_ASSERT(uTimeoutElapsedMs(timeoutStart) << U_CFG_TEST_TIMEOUT_SPEED_UP >= + U_TIMEOUT_DURATION_MS); + stopTickMs = uPortGetTickTimeMs(); + acceleratedElapsedTimeMs = (uint32_t) (stopTickMs - startTickMs) << U_CFG_TEST_TIMEOUT_SPEED_UP; + U_TEST_PRINT_LINE("...took %u day(s) to elapse%s.", + acceleratedElapsedTimeMs / 86400000, + ((uint32_t) (stopTickMs) << U_CFG_TEST_TIMEOUT_SPEED_UP) < + ((uint32_t) (startTickMs) << U_CFG_TEST_TIMEOUT_SPEED_UP) ? " and the underlying tick wrapped" : + ""); + // If a timer has taken longer than one half of a loop around + // uPortGetTickTimeMs(), when scaled by U_CFG_TEST_TIMEOUT_SPEED_UP + // then it has got stuck + U_PORT_TEST_ASSERT(acceleratedElapsedTimeMs < UINT32_MAX / 2); + } + + uPortDeinit(); + + // Check for resource leaks + uTestUtilResourceCheck(U_TEST_PREFIX, NULL, true); + resourceCount = uTestUtilGetDynamicResourceCount() - resourceCount; + U_TEST_PRINT_LINE("we have leaked %d resources(s).", resourceCount); + U_PORT_TEST_ASSERT(resourceCount <= 0); +} + +#endif + +// End of file \ No newline at end of file diff --git a/example/mqtt_client/mqtt_main.c b/example/mqtt_client/mqtt_main.c index 7f515814..e6778cc0 100644 --- a/example/mqtt_client/mqtt_main.c +++ b/example/mqtt_client/mqtt_main.c @@ -253,7 +253,7 @@ U_PORT_TEST_FUNCTION("[example]", "exampleMqttClient") char buffer[64]; size_t bufferSize; volatile bool messagesAvailable = false; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t returnCode; // Initialise the APIs we will need @@ -340,7 +340,7 @@ U_PORT_TEST_FUNCTION("[example]", "exampleMqttClient") // MQTT broker uPortLog("Publishing \"%s\" to topic \"%s\"...\n", message, topic); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); // If you were using MQTT-SN, you would call // uMqttClientSnPublish() instead and pass it // the MQTT-SN topic name returned by @@ -353,7 +353,7 @@ U_PORT_TEST_FUNCTION("[example]", "exampleMqttClient") // Wait for us to be notified that our new // message is available on the broker while (!messagesAvailable && - (uPortGetTickTimeMs() - startTimeMs < 10000)) { + !uTimeoutExpiredSeconds(timeoutStart, 10)) { uPortTaskBlock(1000); } diff --git a/gnss/src/lib_mga/u_lib_mga.c b/gnss/src/lib_mga/u_lib_mga.c index f66895f9..763e9675 100644 --- a/gnss/src/lib_mga/u_lib_mga.c +++ b/gnss/src/lib_mga/u_lib_mga.c @@ -234,7 +234,8 @@ static UBX_U2 s_flashSequence = 0; // Specific support for legacy aiding static bool s_bLegacyAiding = false; static LEGACY_AIDING_STATE s_aidState = LEGACY_AIDING_IDLE; -static time_t s_aidingTimeout = 0; +// MODIFIED: use timeout API +static uTimeoutStop_t s_aidingTimeout = {0}; static UBX_U2 s_alpfileId = 0; static UBX_U1 *s_pAidingData = NULL; @@ -435,7 +436,8 @@ MGA_API_RESULT mgaSessionStart(void) U_ASSERT(s_flashSequence == 0); U_ASSERT(s_aidState == LEGACY_AIDING_IDLE); - U_ASSERT(s_aidingTimeout == 0); + // MODIFIED: use timeout API + U_ASSERT(s_aidingTimeout.durationMs == 0); s_sessionState = MGA_ACTIVE_PROCESSING_DATA; } @@ -1035,7 +1037,7 @@ MGA_API_RESULT mgaSessionSendOfflineToFlash(const UBX_U1* pMgaData, UBX_I4 iSize s_pMgaFlashBlockList[i].state = MGA_MSG_WAITING_TO_SEND; s_pMgaFlashBlockList[i].sequenceNumber = (UBX_U2)i; s_pMgaFlashBlockList[i].retryCount = 0; - s_pMgaFlashBlockList[i].timeOut = 0; + s_pMgaFlashBlockList[i].timeOut.durationMs = 0; s_pMgaFlashBlockList[i].mgaFailedReason = MGA_FAILED_REASON_CODE_NOT_SET; if ((i == (s_mgaFlashBlockCount - 1)) && (lastBlockSize > 0)) @@ -1108,7 +1110,7 @@ MGA_API_RESULT mgaSessionSendLegacyOfflineToFlash(const UBX_U1* pAidingData, UBX s_pMgaFlashBlockList[i].state = MGA_MSG_WAITING_TO_SEND; s_pMgaFlashBlockList[i].sequenceNumber = (UBX_U2)i; s_pMgaFlashBlockList[i].retryCount = 0; - s_pMgaFlashBlockList[i].timeOut = 0; + s_pMgaFlashBlockList[i].timeOut.durationMs = 0; s_pMgaFlashBlockList[i].mgaFailedReason = MGA_FAILED_REASON_CODE_NOT_SET; if ((i == (s_mgaFlashBlockCount - 1)) && (lastBlockSize > 0)) @@ -1180,15 +1182,14 @@ MGA_API_RESULT mgaCheckForTimeOuts(void) MgaMsgInfo* pMsgInfo = s_pMgaMsgList; UBX_U4 i; - size_t rob = 0; for (i = 0; i < s_mgaBlockCount; i++) { if (pMsgInfo->state == MGA_MSG_WAITING_FOR_ACK) { - rob++; - int32_t now = uPortGetTickTimeMs(); - - if (now > pMsgInfo->timeOut) + // MODIFIED: use timeout API + if ((pMsgInfo->timeOut.durationMs > 0) && + uTimeoutExpiredMs(pMsgInfo->timeOut.timeoutStart, + pMsgInfo->timeOut.durationMs)) { if (pMsgInfo->retryCount < s_pFlowConfig->msgRetryCount) { @@ -1233,8 +1234,10 @@ MGA_API_RESULT mgaCheckForTimeOuts(void) if ((s_pLastFlashBlockSent->state == MGA_MSG_WAITING_FOR_ACK) || (s_pLastFlashBlockSent->state == MGA_MSG_WAITING_FOR_ACK_SECOND_CHANCE)) { - int32_t now = uPortGetTickTimeMs(); - if (now > s_pLastFlashBlockSent->timeOut) + // MODIFIED: use timeout API + if ((s_pLastFlashBlockSent->timeOut.durationMs > 0) && + uTimeoutExpiredMs(s_pLastFlashBlockSent->timeOut.timeoutStart, + s_pLastFlashBlockSent->timeOut.durationMs)) { // Timed out if (s_pLastFlashBlockSent->state == MGA_MSG_WAITING_FOR_ACK_SECOND_CHANCE) @@ -2238,12 +2241,14 @@ static void handleLegacyAidingTimeout(void) // do not lock here - locks must already be in place U_ASSERT(s_bLegacyAiding); - int32_t now = uPortGetTickTimeMs(); switch (s_aidState) { case LEGACY_AIDING_STARTING: - if (now > s_aidingTimeout) + // MODIFIED: use timeout API + if ((s_aidingTimeout.durationMs > 0) && + uTimeoutExpiredMs(s_aidingTimeout.timeoutStart, + s_aidingTimeout.durationMs)) { U_ASSERT(s_pEvtInterface); if (s_pEvtInterface->evtProgress) @@ -2262,7 +2267,10 @@ static void handleLegacyAidingTimeout(void) U_ASSERT((s_pLastFlashBlockSent->state == MGA_MSG_WAITING_FOR_ACK) || (s_pLastFlashBlockSent->state == MGA_MSG_WAITING_FOR_ACK_SECOND_CHANCE)); - if (now > s_pLastFlashBlockSent->timeOut) + // MODIFIED: use timeout API + if ((s_pLastFlashBlockSent->timeOut.durationMs > 0) && + uTimeoutExpiredMs(s_pLastFlashBlockSent->timeOut.timeoutStart, + s_pLastFlashBlockSent->timeOut.durationMs)) { if (s_pLastFlashBlockSent->state == MGA_MSG_WAITING_FOR_ACK) { @@ -2288,7 +2296,10 @@ static void handleLegacyAidingTimeout(void) break; case LEGACY_AIDING_STOPPING: - if (now > s_aidingTimeout) + // MODIFIED: use timeout API + if ((s_aidingTimeout.durationMs > 0) && + uTimeoutExpiredMs(s_aidingTimeout.timeoutStart, + s_aidingTimeout.durationMs)) { // Give up waiting for 'stop' ack if (s_pEvtInterface->evtProgress) @@ -2419,7 +2430,9 @@ static void sendMgaFlashBlock(bool next) s_pEvtInterface->evtWriteDevice(s_pCallbackContext, flashDataMsg, (UBX_I4)flashMsgTotalSize); s_pLastFlashBlockSent->state = MGA_MSG_WAITING_FOR_ACK; - s_pLastFlashBlockSent->timeOut = s_pFlowConfig->msgTimeOut + uPortGetTickTimeMs(); + // MODIFIED: use timeout API + s_pLastFlashBlockSent->timeOut.timeoutStart = uTimeoutStart(); + s_pLastFlashBlockSent->timeOut.durationMs = s_pFlowConfig->msgTimeOut; if (s_pEvtInterface->evtProgress) { @@ -2518,7 +2531,9 @@ static void sendFlashMainSeqBlock(void) s_pEvtInterface->evtWriteDevice(s_pCallbackContext, aidingFlashDataMsg, (UBX_I4)flashMsgTotalSize); s_pLastFlashBlockSent->state = MGA_MSG_WAITING_FOR_ACK; - s_pLastFlashBlockSent->timeOut = s_pFlowConfig->msgTimeOut + uPortGetTickTimeMs(); + // MODIFIED: use timeout API + s_pLastFlashBlockSent->timeOut.timeoutStart = uTimeoutStart(); + s_pLastFlashBlockSent->timeOut.durationMs = s_pFlowConfig->msgTimeOut; if (s_pEvtInterface->evtProgress) { @@ -2565,7 +2580,9 @@ static void sendAidingFlashStop(void) U_ASSERT(validChecksum(&aidingFlashStopMsg[2], sizeof(aidingFlashStopMsg) - 4)); s_pEvtInterface->evtWriteDevice(s_pCallbackContext, aidingFlashStopMsg, sizeof(aidingFlashStopMsg)); - s_aidingTimeout = (s_pFlowConfig->msgTimeOut / 1000) + (uPortGetTickTimeMs() / 1000); + // MODIFIED: use timeout API + s_aidingTimeout.timeoutStart = uTimeoutStart(); + s_aidingTimeout.durationMs = s_pFlowConfig->msgTimeOut; } static void addMgaIniTime(const UBX_U1* pMgaData, UBX_I4* iSize, UBX_U1** pMgaDataOut, const MgaTimeAdjust* pTime) @@ -2788,7 +2805,9 @@ static UBX_I4 sendNextMgaMessage(void) msgSize = s_pLastMsgSent->msgSize; s_pEvtInterface->evtWriteDevice(s_pCallbackContext, s_pLastMsgSent->pMsg, msgSize); s_pLastMsgSent->state = MGA_MSG_WAITING_FOR_ACK; - s_pLastMsgSent->timeOut = s_pFlowConfig->msgTimeOut + uPortGetTickTimeMs(); + // MODIFIED: use timeout API + s_pLastMsgSent->timeOut.timeoutStart = uTimeoutStart(); + s_pLastMsgSent->timeOut.durationMs = s_pFlowConfig->msgTimeOut; if (s_pEvtInterface->evtProgress) { @@ -2809,7 +2828,9 @@ static void resendMessage(MgaMsgInfo* pResendMsg) U_ASSERT(s_pEvtInterface->evtWriteDevice); s_pEvtInterface->evtWriteDevice(s_pCallbackContext, pResendMsg->pMsg, pResendMsg->msgSize); pResendMsg->state = MGA_MSG_WAITING_FOR_ACK; - pResendMsg->timeOut = s_pFlowConfig->msgTimeOut + uPortGetTickTimeMs(); + // MODIFIED: use timeout API + pResendMsg->timeOut.timeoutStart = uTimeoutStart(); + pResendMsg->timeOut.durationMs = s_pFlowConfig->msgTimeOut; if (s_pEvtInterface->evtProgress) { @@ -3014,7 +3035,8 @@ static MgaMsgInfo* buildMsgList(const UBX_U1* pMgaData, unsigned int uNumEntries pCurrentBlock->pMsg = pMgaData; pCurrentBlock->msgSize = msgSize; pCurrentBlock->state = MGA_MSG_WAITING_TO_SEND; - pCurrentBlock->timeOut = 0; // set when transfer takes place + // set when transfer takes place + pCurrentBlock->timeOut.durationMs = 0; pCurrentBlock->retryCount = 0; pCurrentBlock->sequenceNumber = (UBX_U2)i; pCurrentBlock->mgaFailedReason = MGA_FAILED_REASON_CODE_NOT_SET; @@ -3078,7 +3100,8 @@ static void sessionStop(MGA_PROGRESS_EVENT_TYPE evtType, const void* pEventInfo, // Tidy up any specific legacy aiding flash transfer settings s_bLegacyAiding = false; s_aidState = LEGACY_AIDING_IDLE; - s_aidingTimeout = 0; + // MODIFIED: use timeout API + s_aidingTimeout.durationMs = 0; // Tidy up any legacy aiding server settings s_pAidingData = NULL; diff --git a/gnss/src/lib_mga/u_lib_mga.h b/gnss/src/lib_mga/u_lib_mga.h index 8e0329fe..98054885 100644 --- a/gnss/src/lib_mga/u_lib_mga.h +++ b/gnss/src/lib_mga/u_lib_mga.h @@ -29,6 +29,8 @@ #include "u_lib_mga_common_types.h" // MODIFED: quotes instead of <> for consistency with ubxlib #include "time.h" +// MODIFIED: inclusion of ubxlib timeout API +#include "u_timeout.h" /////////////////////////////////////////////////////////////////////////////// #ifdef __cplusplus @@ -233,6 +235,8 @@ extern "C" { // MODIFIED: the members of this structure have been re-ordered to put the smallest at the end, // most likely to lead to efficient structure packing + // ALSO timeOut has been changed to uTimeoutStop_t, see /common/timeout/api, and, + // in the durationMs field of the structure, zero is used to mean "no timeout set" //! Message information structure. /*! For each UBX message that needs to be transferred to the receiver, a message info structure instance is maintained by the libMga. These are used to manage the transfer of UBX messages to the receiver. @@ -241,7 +245,7 @@ extern "C" { */ typedef struct { - time_t timeOut; //!< The time in the future when the UBX message is considered to have been lost and not made it to the receiver. + uTimeoutStop_t timeOut; //!< The time in the future when the UBX message is considered to have been lost and not made it to the receiver. const UBX_U1* pMsg; //!< Pointer to the start of the UBX message. struct //!< Fields related to MGA message transfers { diff --git a/gnss/src/u_gnss.c b/gnss/src/u_gnss.c index 92b4e408..2a8d591b 100644 --- a/gnss/src/u_gnss.c +++ b/gnss/src/u_gnss.c @@ -39,6 +39,8 @@ #include "u_ringbuffer.h" #include "u_linked_list.h" +#include "u_timeout.h" + #include "u_device_shared.h" #include "u_at_client.h" diff --git a/gnss/src/u_gnss_dec_ubx_nav_pvt.c b/gnss/src/u_gnss_dec_ubx_nav_pvt.c index d500aabf..f9352086 100644 --- a/gnss/src/u_gnss_dec_ubx_nav_pvt.c +++ b/gnss/src/u_gnss_dec_ubx_nav_pvt.c @@ -31,6 +31,7 @@ #include "stddef.h" // NULL, size_t etc. #include "stdint.h" // int32_t etc. +#include "stdbool.h" #include "time.h" // mktime() #include "u_error_common.h" diff --git a/gnss/src/u_gnss_geofence.c b/gnss/src/u_gnss_geofence.c index 8b87ba69..6aa2b2a5 100644 --- a/gnss/src/u_gnss_geofence.c +++ b/gnss/src/u_gnss_geofence.c @@ -37,6 +37,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_linked_list.h" diff --git a/gnss/src/u_gnss_mga.c b/gnss/src/u_gnss_mga.c index df04d1b1..b8c434ae 100644 --- a/gnss/src/u_gnss_mga.c +++ b/gnss/src/u_gnss_mga.c @@ -44,6 +44,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_ubx_protocol.h" @@ -389,7 +391,7 @@ static int32_t ubxMgaSendWaitAck(uGnssPrivateInstance_t *pInstance, size_t messageBodyLengthBytes) { int32_t errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // The UBX-MGA-ACK message ID uGnssPrivateMessageId_t ackMessageId = {.type = U_GNSS_PROTOCOL_UBX, .id.ubx = 0x1360 @@ -413,7 +415,7 @@ static int32_t ubxMgaSendWaitAck(uGnssPrivateInstance_t *pInstance, if (x == messageBodyLengthBytes + U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES) { errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; // Wait for the UBX-MGA-ACK-DATA0 response for our message ID - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); do { x = uGnssPrivateReceiveStreamMessage(pInstance, &ackMessageId, pInstance->ringBufferReadHandlePrivate, @@ -430,7 +432,9 @@ static int32_t ubxMgaSendWaitAck(uGnssPrivateInstance_t *pInstance, } } } - } while ((ackState == 0) && (uPortGetTickTimeMs() - startTimeMs < pInstance->timeoutMs)); + } while ((ackState == 0) && + !uTimeoutExpiredMs(timeoutStart, + pInstance->timeoutMs)); if (ackState == 2) { errorCode = (int32_t) U_ERROR_COMMON_SUCCESS; } else if (ackState == 1) { @@ -1056,7 +1060,7 @@ int32_t uGnssMgaGetDatabase(uDeviceHandle_t gnssHandle, uGnssPrivateInstance_t *pInstance; int32_t protocolsOut = 0; int32_t readHandle; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // The UBX-MGA message class/ID (to capture -DBD and -ACK) uGnssPrivateMessageId_t messageId = {.type = U_GNSS_PROTOCOL_UBX, .id.ubx = 0x1300 + U_GNSS_UBX_MESSAGE_ID_ALL @@ -1098,9 +1102,10 @@ int32_t uGnssMgaGetDatabase(uDeviceHandle_t gnssHandle, if (uGnssPrivateSendOnlyStreamUbxMessage(pInstance, 0x13, 0x80, NULL, 0) == U_UBX_PROTOCOL_OVERHEAD_LENGTH_BYTES) { errorCodeOrLength = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (context.keepGoing && (context.errorCodeOrLength >= 0) && - (uPortGetTickTimeMs() - startTimeMs < U_GNSS_MGA_DATABASE_READ_TIMEOUT_MS)) { + !uTimeoutExpiredMs(timeoutStart, + U_GNSS_MGA_DATABASE_READ_TIMEOUT_MS)) { uPortTaskBlock(250); } if (!context.keepGoing) { diff --git a/gnss/src/u_gnss_pos.c b/gnss/src/u_gnss_pos.c index cbfae95c..a6a2025c 100644 --- a/gnss/src/u_gnss_pos.c +++ b/gnss/src/u_gnss_pos.c @@ -44,6 +44,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_time.h" #include "u_at_client.h" @@ -290,7 +292,7 @@ static void posGetTask(void *pParameter) { int32_t errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; uGnssPosGetTaskParameters_t taskParameters; - int64_t startTime; + uTimeoutStart_t timeoutStart; int32_t latitudeX1e7 = INT_MIN; int32_t longitudeX1e7 = INT_MIN; int32_t altitudeMillimetres = INT_MIN; @@ -307,12 +309,13 @@ static void posGetTask(void *pParameter) // Lock the mutex to indicate that we're running U_PORT_MUTEX_LOCK(taskParameters.pInstance->posMutex); - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); taskParameters.pInstance->posTaskFlags |= U_GNSS_POS_TASK_FLAG_HAS_RUN; while ((taskParameters.pInstance->posTaskFlags & U_GNSS_POS_TASK_FLAG_KEEP_GOING) && (errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) && - ((uPortGetTickTimeMs() - startTime) / 1000 < U_GNSS_POS_TIMEOUT_SECONDS)) { + !uTimeoutExpiredSeconds(timeoutStart, + U_GNSS_POS_TIMEOUT_SECONDS)) { // Call posGet() to do the work errorCode = posGet(taskParameters.pInstance, &latitudeX1e7, @@ -450,7 +453,7 @@ int32_t uGnssPosGet(uDeviceHandle_t gnssHandle, #ifdef U_CFG_SARA_R5_M8_WORKAROUND uint8_t message[4]; // Room for the body of a UBX-CFG-ANT message #endif - int64_t startTime; + uTimeoutStart_t timeoutStart; if (gUGnssPrivateMutex != NULL) { @@ -476,11 +479,12 @@ int32_t uGnssPosGet(uDeviceHandle_t gnssHandle, (const char *) message, 4); } #endif - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); errorCode = (int32_t) U_ERROR_COMMON_TIMEOUT; while ((errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) && (((pKeepGoingCallback == NULL) && - (uPortGetTickTimeMs() - startTime) / 1000 < U_GNSS_POS_TIMEOUT_SECONDS) || + !uTimeoutExpiredSeconds(timeoutStart, + U_GNSS_POS_TIMEOUT_SECONDS)) || ((pKeepGoingCallback != NULL) && pKeepGoingCallback(gnssHandle)))) { // Call posGet() to do the work errorCode = posGet(pInstance, @@ -896,7 +900,7 @@ int32_t uGnssPosGetRrlp(uDeviceHandle_t gnssHandle, char *pBuffer, int32_t errorCodeOrLength = (int32_t) U_ERROR_COMMON_NOT_INITIALISED; uGnssPrivateInstance_t *pInstance; int32_t messageClass; - int64_t startTime; + uTimeoutStart_t timeoutStart; int32_t svs; int32_t numBytes; int32_t z; @@ -939,11 +943,12 @@ int32_t uGnssPosGetRrlp(uDeviceHandle_t gnssHandle, char *pBuffer, } #endif messageClass = gRrlpModeToUbxRxmMessageClass[pInstance->rrlpMode]; - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); errorCodeOrLength = (int32_t) U_ERROR_COMMON_TIMEOUT; while ((errorCodeOrLength == (int32_t) U_ERROR_COMMON_TIMEOUT) && (((pKeepGoingCallback == NULL) && - (uPortGetTickTimeMs() - startTime) / 1000 < U_GNSS_POS_TIMEOUT_SECONDS) || + !uTimeoutExpiredSeconds(timeoutStart, + U_GNSS_POS_TIMEOUT_SECONDS)) || ((pKeepGoingCallback != NULL) && pKeepGoingCallback(gnssHandle)))) { numBytes = uGnssPrivateSendReceiveUbxMessage(pInstance, diff --git a/gnss/src/u_gnss_private.c b/gnss/src/u_gnss_private.c index 1afc28ca..101d9429 100644 --- a/gnss/src/u_gnss_private.c +++ b/gnss/src/u_gnss_private.c @@ -49,6 +49,8 @@ #include "u_port_spi.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_hex_bin_convert.h" #include "u_at_client.h" @@ -338,13 +340,13 @@ static int32_t streamGetFromRingBuffer(uGnssPrivateInstance_t *pInstance, size_t totalSize = 0; int32_t x; size_t leftToRead = size; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; if (pInstance != NULL) { - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); errorCodeOrLength = (int32_t) U_ERROR_COMMON_TIMEOUT; while ((leftToRead > 0) && - (uPortGetTickTimeMs() - startTimeMs < maxTimeMs)) { + !uTimeoutExpiredMs(timeoutStart, maxTimeMs)) { if (andRemove) { receiveSize = (int32_t) uRingBufferReadHandle(&(pInstance->ringBuffer), readHandle, @@ -2155,7 +2157,7 @@ int32_t uGnssPrivateStreamFillRingBuffer(uGnssPrivateInstance_t *pInstance, int32_t timeoutMs, int32_t maxTimeMs) { int32_t errorCodeOrLength = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t privateStreamTypeOrError; int32_t receiveSize; int32_t totalReceiveSize = 0; @@ -2176,7 +2178,7 @@ int32_t uGnssPrivateStreamFillRingBuffer(uGnssPrivateInstance_t *pInstance, privateStreamTypeOrError = uGnssPrivateGetStreamType(pInstance->transportType); if (privateStreamTypeOrError >= 0) { errorCodeOrLength = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); // This is constructed as a do()/while() so that // it always has one go even with a zero timeout do { @@ -2255,8 +2257,11 @@ int32_t uGnssPrivateStreamFillRingBuffer(uGnssPrivateInstance_t *pInstance, (timeoutMs > 0) && (ringBufferAvailableSize > 0) && // The first condition below is the "not yet received anything case", guarded by timeoutMs // the second condition below is when we're receiving stuff, guarded by maxTimeMs - (((totalReceiveSize == 0) && (uPortGetTickTimeMs() - startTimeMs < timeoutMs)) || - ((receiveSize > 0) && ((maxTimeMs == 0) || (uPortGetTickTimeMs() - startTimeMs < maxTimeMs))))); + (((totalReceiveSize == 0) && + !uTimeoutExpiredMs(timeoutStart, timeoutMs)) || + ((receiveSize > 0) && ((maxTimeMs == 0) || + !uTimeoutExpiredMs(timeoutStart, + maxTimeMs))))); } } @@ -2423,14 +2428,14 @@ int32_t uGnssPrivateReceiveStreamMessage(uGnssPrivateInstance_t *pInstance, int32_t receiveSize; int32_t ringBufferSize; size_t discardSize = 0; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t x = timeoutMs > 0 ? U_GNSS_RING_BUFFER_MIN_FILL_TIME_MS : 0; int32_t y; if ((pInstance != NULL) && (pPrivateMessageId != NULL) && (ppBuffer != NULL) && ((*ppBuffer == NULL) || (size > 0))) { errorCodeOrLength = (int32_t) U_ERROR_COMMON_TIMEOUT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); // Lock our read pointer while we look for stuff uRingBufferLockReadHandle(&(pInstance->ringBuffer), readHandle); // This is constructed as a do()/while() so that it always has one go @@ -2468,7 +2473,7 @@ int32_t uGnssPrivateReceiveStreamMessage(uGnssPrivateInstance_t *pInstance, if (*ppBuffer != NULL) { // Now read the message data into the buffer, // which will move our read pointer on - y = timeoutMs - (uPortGetTickTimeMs() - startTimeMs); + y = timeoutMs - uTimeoutElapsedMs(timeoutStart); if (y < U_GNSS_RING_BUFFER_MIN_FILL_TIME_MS) { // Make sure we give ourselves time to read the messsage out y = U_GNSS_RING_BUFFER_MIN_FILL_TIME_MS; @@ -2497,7 +2502,7 @@ int32_t uGnssPrivateReceiveStreamMessage(uGnssPrivateInstance_t *pInstance, } while ((((errorCodeOrLength < 0) && (errorCodeOrLength != (int32_t) U_GNSS_ERROR_NACK) && (errorCodeOrLength != (int32_t) U_ERROR_COMMON_NO_MEMORY)) || (discardSize > 0)) && (timeoutMs > 0) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs) && + !uTimeoutExpiredMs(timeoutStart, timeoutMs) && ((pKeepGoingCallback == NULL) || pKeepGoingCallback(pInstance->gnssHandle))); // Read pointer can be unlocked now @@ -2609,7 +2614,7 @@ int32_t uGnssPrivateSendUbxMessage(uGnssPrivateInstance_t *pInstance, int32_t errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER; uGnssPrivateUbxReceiveMessage_t response = {0}; // Keep Valgrind happy int32_t timeoutMs; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; char ackBody[2] = {0}; char *pBody = &(ackBody[0]); @@ -2622,7 +2627,7 @@ int32_t uGnssPrivateSendUbxMessage(uGnssPrivateInstance_t *pInstance, response.bodySize = sizeof(ackBody); timeoutMs = pInstance->timeoutMs; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); errorCode = sendReceiveUbxMessage(pInstance, messageClass, messageId, pMessageBody, messageBodyLengthBytes, &response); @@ -2638,7 +2643,7 @@ int32_t uGnssPrivateSendUbxMessage(uGnssPrivateInstance_t *pInstance, (ackBody[1] != (char) messageId) && !((response.id == 0x01) || (response.id == 0x00)) && (uGnssPrivateGetStreamType(pInstance->transportType) >= 0) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs)) { + !uTimeoutExpiredMs(timeoutStart, timeoutMs)) { response.cls = 0x05; response.id = -1; errorCode = receiveUbxMessageStream(pInstance, &response, diff --git a/gnss/src/u_gnss_stub_cell.c b/gnss/src/u_gnss_stub_cell.c index 48da1ce1..3bdd97ea 100644 --- a/gnss/src/u_gnss_stub_cell.c +++ b/gnss/src/u_gnss_stub_cell.c @@ -41,6 +41,8 @@ #include "u_port_os.h" #include "u_port.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_cell_module_type.h" diff --git a/gnss/src/u_gnss_util.c b/gnss/src/u_gnss_util.c index ba1ea281..4a0690e5 100644 --- a/gnss/src/u_gnss_util.c +++ b/gnss/src/u_gnss_util.c @@ -46,6 +46,8 @@ #include "u_port_i2c.h" #include "u_port_spi.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_ubx_protocol.h" @@ -96,7 +98,7 @@ int32_t uGnssUtilUbxTransparentSendReceive(uDeviceHandle_t gnssHandle, uGnssPrivateInstance_t *pInstance; int32_t privateStreamTypeOrError; uDeviceSerial_t *pDeviceSerial = NULL; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t x = 0; int32_t bytesRead = 0; char *pBuffer; @@ -173,11 +175,11 @@ int32_t uGnssUtilUbxTransparentSendReceive(uDeviceHandle_t gnssHandle, errorCodeOrResponseLength = (int32_t) U_ERROR_COMMON_SUCCESS; if (pResponse != NULL) { errorCodeOrResponseLength = (int32_t) U_GNSS_ERROR_TRANSPORT; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); // Wait for something to start coming back while ((bytesRead < (int32_t) maxResponseLengthBytes) && ((x = uGnssPrivateStreamGetReceiveSize(pInstance)) <= 0) && - (uPortGetTickTimeMs() - startTimeMs < pInstance->timeoutMs)) { + !uTimeoutExpiredMs(timeoutStart, pInstance->timeoutMs)) { // Relax a little uPortTaskBlock(U_GNSS_UTIL_TRANSPARENT_RECEIVE_DELAY_MS); } @@ -185,7 +187,7 @@ int32_t uGnssUtilUbxTransparentSendReceive(uDeviceHandle_t gnssHandle, // Got something; continue receiving until nothing arrives or we time out while ((bytesRead < (int32_t) maxResponseLengthBytes) && ((x = uGnssPrivateStreamGetReceiveSize(pInstance)) > 0) && - (uPortGetTickTimeMs() - startTimeMs < pInstance->timeoutMs)) { + !uTimeoutExpiredMs(timeoutStart, pInstance->timeoutMs)) { if (x > 0) { if (x > ((int32_t) maxResponseLengthBytes) - bytesRead) { x = maxResponseLengthBytes - bytesRead; diff --git a/gnss/test/u_gnss_geofence_test.c b/gnss/test/u_gnss_geofence_test.c index 394987d3..c07b1160 100644 --- a/gnss/test/u_gnss_geofence_test.c +++ b/gnss/test/u_gnss_geofence_test.c @@ -50,6 +50,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" // Required by u_gnss_private.h #include "u_linked_list.h" @@ -169,7 +171,7 @@ static uGnssTestPrivate_t gHandles = U_GNSS_TEST_PRIVATE_DEFAULTS; /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Variable to track the parameters received by a callback that * vary with the position being tested. @@ -202,7 +204,8 @@ static bool keepGoingCallback(uDeviceHandle_t gnssHandle) bool keepGoing = true; U_PORT_TEST_ASSERT(gnssHandle == gHandles.gnssHandle); - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -810,7 +813,6 @@ U_PORT_TEST_FUNCTION("[gnssGeofence]", "gnssGeofenceLive") uGnssTransportType_t transportTypes[U_GNSS_TRANSPORT_MAX_NUM]; int32_t y; uGnssGeofenceTestCallbackParams_t callbackParams; - int32_t startTimeMs; // In case fence A was left hanging uGnssGeofenceRemove(NULL, NULL); @@ -875,14 +877,14 @@ U_PORT_TEST_FUNCTION("[gnssGeofence]", "gnssGeofenceLive") U_GEOFENCE_POSITION_STATE_INSIDE, U_GEOFENCE_POSITION_STATE_OUTSIDE, LLONG_MIN, LLONG_MIN, INT_MIN, INT_MIN, INT_MIN); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS * 1000; y = uGnssPosGet(gnssDevHandle, NULL, NULL, NULL, NULL, NULL, NULL, NULL, keepGoingCallback); U_TEST_PRINT_LINE("calling uGnssPosGet() returned %d.", y); U_PORT_TEST_ASSERT(y == 0); - U_TEST_PRINT_LINE("position establishment took %d second(s).", - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("position establishment took %u second(s).", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); U_PORT_TEST_ASSERT(checkCallbackResult(&callbackParams, &gCallbackParameters)); // Repeat for the asynchronous position API @@ -891,14 +893,16 @@ U_PORT_TEST_FUNCTION("[gnssGeofence]", "gnssGeofenceLive") U_GEOFENCE_POSITION_STATE_INSIDE, U_GEOFENCE_POSITION_STATE_OUTSIDE, LLONG_MIN, LLONG_MIN, INT_MIN, INT_MIN, INT_MIN); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS * 1000; y = uGnssPosGetStart(gnssDevHandle, posCallback); U_TEST_PRINT_LINE("calling uGnssPosGetStart() returned %d.", y); U_PORT_TEST_ASSERT(y == 0); - U_TEST_PRINT_LINE("waiting up to %d second(s) for results from asynchronous API...", - U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS); - while ((gCallbackParameters.called < 2) && (uPortGetTickTimeMs() < gStopTimeMs)) { + U_TEST_PRINT_LINE("waiting up to %u second(s) for results from asynchronous API...", + gTimeoutStop.durationMs / 1000); + while ((gCallbackParameters.called < 2) && + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(1000); } U_PORT_TEST_ASSERT(checkCallbackResult(&callbackParams, &gCallbackParameters)); @@ -910,14 +914,16 @@ U_PORT_TEST_FUNCTION("[gnssGeofence]", "gnssGeofenceLive") U_GEOFENCE_POSITION_STATE_INSIDE, U_GEOFENCE_POSITION_STATE_OUTSIDE, LLONG_MIN, LLONG_MIN, INT_MIN, INT_MIN, INT_MIN); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS * 1000; y = uGnssPosGetStreamedStart(gnssDevHandle, 1000, posCallback);; U_TEST_PRINT_LINE("calling uGnssPosGetStreamedStart() returned %d.", y); U_PORT_TEST_ASSERT(y == 0); - U_TEST_PRINT_LINE("waiting up to %d second(s) for results from streamed API...", - U_GNSS_GEOFENCE_TEST_POS_TIMEOUT_SECONDS); - while ((gCallbackParameters.called < 2) && (uPortGetTickTimeMs() < gStopTimeMs)) { + U_TEST_PRINT_LINE("waiting up to %u second(s) for results from streamed API...", + gTimeoutStop.durationMs / 1000); + while ((gCallbackParameters.called < 2) && + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(1000); } // Stop the stream before potentially asserting diff --git a/gnss/test/u_gnss_info_test.c b/gnss/test/u_gnss_info_test.c index 42996dec..ac8e58f8 100644 --- a/gnss/test/u_gnss_info_test.c +++ b/gnss/test/u_gnss_info_test.c @@ -47,6 +47,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" // Required by u_gnss_private.h #include "u_port.h" @@ -254,7 +256,7 @@ U_PORT_TEST_FUNCTION("[gnssInfo]", "gnssInfoTime") uDeviceHandle_t gnssHandle; int32_t resourceCount; int64_t y; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; size_t iterations; uGnssTransportType_t transportTypes[U_GNSS_TRANSPORT_MAX_NUM]; @@ -293,19 +295,19 @@ U_PORT_TEST_FUNCTION("[gnssInfo]", "gnssInfoTime") // has not yet found time U_TEST_PRINT_LINE("waiting up to %d second(s) to establish UTC time...", U_GNSS_TIME_TEST_TIMEOUT_SECONDS); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((y < 0) && - (uPortGetTickTimeMs() - startTimeMs < (U_GNSS_TIME_TEST_TIMEOUT_SECONDS * 1000))) { + !uTimeoutExpiredSeconds(timeoutStart, + U_GNSS_TIME_TEST_TIMEOUT_SECONDS)) { y = uGnssInfoGetTimeUtc(gnssHandle); } if (y > 0) { - U_TEST_PRINT_LINE("UTC time according to GNSS is %d (took %d second(s)" + U_TEST_PRINT_LINE("UTC time according to GNSS is %d (took %u second(s)" " to establish).", (int32_t) y, - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + uTimeoutElapsedSeconds(timeoutStart)); } else { - U_TEST_PRINT_LINE("could not get UTC time from GNSS after %d second(s) (%d).", - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000, - (int32_t) y); + U_TEST_PRINT_LINE("could not get UTC time from GNSS after %u second(s) (%d).", + uTimeoutElapsedSeconds(timeoutStart), (int32_t) y); } U_PORT_TEST_ASSERT(y > U_GNSS_TEST_MIN_UTC_TIME); diff --git a/gnss/test/u_gnss_mga_test.c b/gnss/test/u_gnss_mga_test.c index 25dc9a0e..60de6edb 100644 --- a/gnss/test/u_gnss_mga_test.c +++ b/gnss/test/u_gnss_mga_test.c @@ -71,6 +71,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" #include "u_network_test_shared_cfg.h" @@ -549,7 +551,7 @@ U_PORT_TEST_FUNCTION("[gnssMga]", "gnssMgaBasic") int32_t callbackParameter; int32_t z; uGnssCommunicationStats_t communicationStats; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; const char *pProtocolName; #endif @@ -667,7 +669,7 @@ U_PORT_TEST_FUNCTION("[gnssMga]", "gnssMgaBasic") # ifndef U_GNSS_MGA_TEST_DISABLE_DATABASE callbackParameter = 0; gDatabaseCalledCount = 0; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); if ((transportTypes[w] != U_GNSS_TRANSPORT_AT) && (intermediateHandle == NULL)) { U_TEST_PRINT_LINE("reading database from GNSS device."); gDatabaseHasQzss = false; @@ -675,8 +677,8 @@ U_PORT_TEST_FUNCTION("[gnssMga]", "gnssMgaBasic") U_TEST_PRINT_LINE("uGnssMgaGetDatabase() returned %d.", z); if (callbackParameter >= 0) { U_TEST_PRINT_LINE("database callback was called %d times, with a total" - " of %d byte(s) in %d milliseconds.", gDatabaseCalledCount, - callbackParameter, uPortGetTickTimeMs() - startTimeMs); + " of %d byte(s) in %u milliseconds.", gDatabaseCalledCount, + callbackParameter, uTimeoutElapsedMs(timeoutStart)); U_PORT_TEST_ASSERT(z == callbackParameter); } else { U_TEST_PRINT_LINE("database callback returned error %d.", callbackParameter); diff --git a/gnss/test/u_gnss_msg_test.c b/gnss/test/u_gnss_msg_test.c index e3012517..2a561841 100644 --- a/gnss/test/u_gnss_msg_test.c +++ b/gnss/test/u_gnss_msg_test.c @@ -48,6 +48,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" // Required by u_gnss_private.h #include "u_port_clib_platform_specific.h" /* Integer stdio, must be included @@ -167,7 +169,7 @@ typedef struct { /** Used for keepGoingCallback() timeout. */ -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Handles. */ @@ -198,7 +200,8 @@ static bool keepGoingCallback(uDeviceHandle_t gnssHandle) gCallbackErrorCode = 1; } - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -414,7 +417,8 @@ U_PORT_TEST_FUNCTION("[gnssMsg]", "gnssMsgReceiveBlocking") U_TEST_PRINT_LINE("receiving response without message filter and with auto-buffer."); messageId.type = U_GNSS_PROTOCOL_UBX; messageId.id.ubx = U_GNSS_UBX_MESSAGE(U_GNSS_UBX_MESSAGE_CLASS_ALL, U_GNSS_UBX_MESSAGE_ID_ALL); - gStopTimeMs = uPortGetTickTimeMs() + U_GNSS_MSG_TEST_MESSAGE_RECEIVE_TIMEOUT_MS; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_MSG_TEST_MESSAGE_RECEIVE_TIMEOUT_MS; pBuffer3 = NULL; gCallbackErrorCode = 0; x = uGnssMsgReceive(gnssHandle, &messageId, &pBuffer3, 0, diff --git a/gnss/test/u_gnss_pos_test.c b/gnss/test/u_gnss_pos_test.c index 2cd3e2ec..b860d931 100644 --- a/gnss/test/u_gnss_pos_test.c +++ b/gnss/test/u_gnss_pos_test.c @@ -47,6 +47,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" // Required by u_gnss_private.h #include "u_port.h" @@ -198,7 +200,7 @@ /** Used for keepGoingCallback() timeout. */ -static int64_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; /** Handles. */ @@ -282,7 +284,8 @@ static bool keepGoingCallback(uDeviceHandle_t gnssHandle) bool keepGoing = true; U_PORT_TEST_ASSERT(gnssHandle == gHandles.gnssHandle); - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -365,7 +368,6 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosPos") char prefix[2]; int32_t whole[2]; int32_t fraction[2]; - int64_t startTimeMs; int32_t resourceCount; size_t iterations; uGnssTransportType_t transportTypes[U_GNSS_TRANSPORT_MAX_NUM]; @@ -406,8 +408,8 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosPos") speedMillimetresPerSecond = INT_MIN; svs = 0; timeUtc = LONG_MIN; - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; y = uGnssPosGet(gnssHandle, &latitudeX1e7, &longitudeX1e7, &altitudeMillimetres, @@ -418,8 +420,8 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosPos") U_TEST_PRINT_LINE_X("calling uGnssPosGet() returned %d.", z + 1, y); U_PORT_TEST_ASSERT(y == 0); - U_TEST_PRINT_LINE_X("position establishment took %d second(s).", z + 1, - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE_X("position establishment took %u second(s).", z + 1, + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); prefix[0] = latLongToBits(latitudeX1e7, &(whole[0]), &(fraction[0])); prefix[1] = latLongToBits(longitudeX1e7, &(whole[1]), &(fraction[1])); U_TEST_PRINT_LINE_X("location %c%d.%07d/%c%d.%07d (radius %d metre(s)), %d metre(s) high," @@ -452,8 +454,8 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosPos") timeUtc = LONG_MIN; gErrorCode = 0xFFFFFFFF; gGoodPosCount = 0; - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; if (z == 0) { // Check that calling stop first causes no problem uGnssPosGetStop(gnssHandle); @@ -467,12 +469,13 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosPos") U_TEST_PRINT_LINE_X("calling uGnssPosGetStart() twice in rapid succession returned %d.", z + 1, y); gErrorCode = 0xFFFFFFFF; gGoodPosCount = 0; - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; } - U_TEST_PRINT_LINE_X("waiting up to %d second(s) for results from asynchronous API...", z + 1, - U_GNSS_POS_TEST_TIMEOUT_SECONDS); - while ((gErrorCode == 0xFFFFFFFF) && (uPortGetTickTimeMs() < gStopTimeMs)) { + U_TEST_PRINT_LINE_X("waiting up to %u second(s) for results from asynchronous API...", z + 1, + gTimeoutStop.durationMs / 1000); + while ((gErrorCode == 0xFFFFFFFF) && !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(1000); } @@ -483,8 +486,8 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosPos") U_TEST_PRINT_LINE_X("asynchonous API received error code %d.", z + 1, gErrorCode); U_PORT_TEST_ASSERT(gErrorCode == 0); U_PORT_TEST_ASSERT(gGoodPosCount == 1); - U_TEST_PRINT_LINE_X("position establishment took %d second(s).", z + 1, - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE_X("position establishment took %u second(s).", z + 1, + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); prefix[0] = latLongToBits(gLatitudeX1e7, &(whole[0]), &(fraction[0])); prefix[1] = latLongToBits(gLongitudeX1e7, &(whole[1]), &(fraction[1])); @@ -535,7 +538,6 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosRrlp") uDeviceHandle_t gnssHandle; int32_t y; char *pBuffer; - int64_t startTimeMs; int32_t resourceCount; size_t iterations; uGnssTransportType_t transportTypes[U_GNSS_TRANSPORT_MAX_NUM]; @@ -581,8 +583,8 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosRrlp") U_PORT_TEST_ASSERT(y >= 6); U_PORT_TEST_ASSERT(y <= U_GNSS_POS_RRLP_SIZE_BYTES); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; U_TEST_PRINT_LINE("asking for RRLP information with thresholds..."); y = uGnssPosGetRrlp(gnssHandle, pBuffer, U_GNSS_POS_RRLP_SIZE_BYTES, U_GNSS_POS_TEST_RRLP_SVS_THRESHOLD, @@ -590,8 +592,8 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosRrlp") U_GNSS_POS_TEST_RRLP_MULTIPATH_INDEX_LIMIT, U_GNSS_POS_TEST_RRLP_PSEUDORANGE_RMS_ERROR_INDEX_LIMIT, keepGoingCallback); - U_TEST_PRINT_LINE("RRLP took %d second(s) to arrive.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("RRLP took %u second(s) to arrive.", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); U_TEST_PRINT_LINE("%d byte(s) of RRLP information was returned.", y); // Must contain at least 6 bytes for the header U_PORT_TEST_ASSERT(y >= 6); @@ -632,15 +634,15 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosRrlp") if (y == 0) { // Do an RRLP get of the 12C compact mode with whacky thresholds, since // they should be ignored - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; U_TEST_PRINT_LINE("asking for compact RRLP information 12C..."); // length should be the UBX protocol overhead plus 12 bytes of data y = uGnssPosGetRrlp(gnssHandle, pBuffer, 8 + 12, INT_MAX, INT_MAX, INT_MAX, INT_MAX, keepGoingCallback); - U_TEST_PRINT_LINE("RRLP took %d second(s) to arrive.", - (int32_t) (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("RRLP took %u second(s) to arrive.", + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); U_TEST_PRINT_LINE("%d byte(s) of RRLP information was returned.", y); // Must contain at least 6 bytes for the header U_PORT_TEST_ASSERT(y >= 6); @@ -679,14 +681,13 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosStreamed") char prefix[2]; int32_t whole[2]; int32_t fraction[2]; - int32_t startTimeMs; - int32_t posTimeMs = -1; int32_t resourceCount; size_t iterations; uGnssTransportType_t transportTypes[U_GNSS_TRANSPORT_MAX_NUM]; int32_t a = -1; int32_t b = -1; uGnssTimeSystem_t t = U_GNSS_TIME_SYSTEM_NONE; + uTimeoutStart_t timeoutStart; // In case a previous test failed uGnssTestPrivateCleanup(&gHandles); @@ -767,19 +768,20 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosStreamed") uGnssPosGetStreamedStop(gnssHandle); } gErrorCode = 0xFFFFFFFF; - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_GNSS_POS_TEST_TIMEOUT_SECONDS * 1000; y = uGnssPosGetStreamedStart(gnssHandle, U_GNSS_POS_TEST_STREAMED_RATE_MS, posCallback); U_TEST_PRINT_LINE_X("uGnssPosGetStreamedStart() returned %d.", z + 1, y); U_PORT_TEST_ASSERT(y == 0); - U_TEST_PRINT_LINE_X("waiting up to %d second(s) for first valid result from streamed API...", - z + 1, U_GNSS_POS_TEST_TIMEOUT_SECONDS); - while ((gErrorCode != 0) && (uPortGetTickTimeMs() < gStopTimeMs)) { + U_TEST_PRINT_LINE_X("waiting up to %u second(s) for first valid result from streamed API...", + z + 1, gTimeoutStop.durationMs / 1000); + while ((gErrorCode != 0) && !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(1000); } if (gErrorCode == 0) { - posTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); U_TEST_PRINT_LINE_X("waiting %d second(s) for rate change to take effect...", z + 1, U_GNSS_POS_TEST_STREAMED_WAIT_SECONDS); uPortTaskBlock(1000 * U_GNSS_POS_TEST_STREAMED_WAIT_SECONDS); @@ -795,8 +797,8 @@ U_PORT_TEST_FUNCTION("[gnssPos]", "gnssPosStreamed") U_TEST_PRINT_LINE_X("streamed position callback received error code %d.", z + 1, gErrorCode); U_PORT_TEST_ASSERT(gErrorCode == 0); if (gGoodPosCount > 0) { - U_TEST_PRINT_LINE_X("position establishment took %d second(s).", z + 1, - (posTimeMs - startTimeMs) / 1000); + U_TEST_PRINT_LINE_X("position establishment took %u second(s).", z + 1, + uTimeoutElapsedSeconds(timeoutStart)); U_TEST_PRINT_LINE_X("the streamed position callback was called with a good position %d time(s)" " in %d second(s), average every %d millisecond(s) (expected every" " %d milliseconds).", z + 1, diff --git a/gnss/test/u_gnss_test_private.c b/gnss/test/u_gnss_test_private.c index f7c551b3..b61c5a3e 100644 --- a/gnss/test/u_gnss_test_private.c +++ b/gnss/test/u_gnss_test_private.c @@ -48,6 +48,8 @@ #include "u_port_i2c.h" #include "u_port_spi.h" +#include "u_timeout.h" + #include "u_at_client.h" // Required by u_gnss_private.h #include "u_cell_module_type.h" diff --git a/port/api/u_port.h b/port/api/u_port.h index 748d6d02..2d38597f 100644 --- a/port/api/u_port.h +++ b/port/api/u_port.h @@ -22,6 +22,8 @@ * of another module should be included here; otherwise * please keep #includes to your .c files. */ +#include "u_compiler.h" // U_INLINE + /** \addtogroup __port __Port * @{ */ @@ -102,9 +104,11 @@ extern "C" { * and then calls pEntryPoint, i.e. the application, in an RTOS task. * This is used as a standard way to start the system for all of the * u-blox examples and all of the u-blox tests. + * * You may have your own mechanism for initialisating the HW and * starting an RTOS task, in which case you need not use this * function. + * * This function only returns if there is an error; * code execution ends up in pEntryPoint, which should * never return. @@ -143,11 +147,18 @@ int32_t uPortInit(); void uPortDeinit(); /** Get the current OS tick converted to a time in milliseconds. - * This is guaranteed to be unaffected by any time setting activity. - * It is NOT maintained while the processor is in deep sleep, i.e. - * with clocks stopped; port initialisation should be called on - * return from deep sleep and that will restart this time from - * zero once more. + * + * IMPORTANT: the value returned by this function should NOT + * be used for checking time-outs or measuring delays; please + * instead use uTimeoutStart(), the return value of which may be + * passed to uTimeoutExpiredMs() or uTimeoutExpiredSeconds(), + * time-out checking functions that know how to handle tick wraps. + * + * The return value of this function is guaranteed to be + * unaffected by any time setting activity. It is NOT maintained + * while the processor is in deep sleep, i.e. with clocks stopped; + * port initialisation should be called on return from deep sleep + * and that will restart this time from zero once more. * * @return the current OS tick converted to milliseconds. */ diff --git a/port/platform/arduino/include.txt b/port/platform/arduino/include.txt index ea247424..a8e944f3 100644 --- a/port/platform/arduino/include.txt +++ b/port/platform/arduino/include.txt @@ -11,6 +11,7 @@ cfg common/error/api common/assert/api +common/timeout/api common/type/api common/device/api common/device/src diff --git a/port/platform/arduino/include_test.txt b/port/platform/arduino/include_test.txt index 589266cb..a2af7f14 100644 --- a/port/platform/arduino/include_test.txt +++ b/port/platform/arduino/include_test.txt @@ -7,6 +7,7 @@ ble/test cell/test gnss/test wifi/test +common/timeout/test common/sock/test common/device/test common/network/test diff --git a/port/platform/arduino/source.txt b/port/platform/arduino/source.txt index 7856a85b..d9b74d72 100644 --- a/port/platform/arduino/source.txt +++ b/port/platform/arduino/source.txt @@ -80,6 +80,7 @@ common/network/src/u_network_private_wifi.c common/network/src/u_network_private_wifi_stub.c common/network/src/u_network_private_gnss.c common/network/src/u_network_private_gnss_stub.c +common/timeout/src/u_timeout.c common/sock/src/u_sock.c common/sock/src/u_sock_stub_cell.c common/sock/src/u_sock_stub_wifi.c diff --git a/port/platform/cell_ucpu/r5/app/test/ucpu_uart_test.c b/port/platform/cell_ucpu/r5/app/test/ucpu_uart_test.c index 50487b6c..5f215031 100644 --- a/port/platform/cell_ucpu/r5/app/test/ucpu_uart_test.c +++ b/port/platform/cell_ucpu/r5/app/test/ucpu_uart_test.c @@ -213,7 +213,7 @@ static void mqttThread(void *thread_input) char pubMessage[MEG_SIZE]; char readBuffer[MEG_SIZE]; size_t readBufferSize; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; uint32_t count = 0; uint32_t result = 0; volatile bool messagesAvailable = false; @@ -265,7 +265,7 @@ static void mqttThread(void *thread_input) if (isConnectedToServer) { uPortLog("MQTT itteration count = %d\n", ++count); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); memset(pubMessage, 0, sizeof(pubMessage)); snprintf(pubMessage, sizeof(pubMessage), "%s%d", message, count); @@ -282,7 +282,7 @@ static void mqttThread(void *thread_input) // Wait for us to be notified that our new // message is available on the broker while (!messagesAvailable && - (uPortGetTickTimeMs() - startTimeMs < 20000)) { + !uTimeoutExpiredSeconds(timeoutStart, 20)) { uPortTaskBlock(1000); } diff --git a/port/platform/cell_ucpu/r5/src/u_port_uart.c b/port/platform/cell_ucpu/r5/src/u_port_uart.c index edbed9f7..7aca5ccc 100644 --- a/port/platform/cell_ucpu/r5/src/u_port_uart.c +++ b/port/platform/cell_ucpu/r5/src/u_port_uart.c @@ -25,6 +25,7 @@ #include "u_cfg_sw.h" #include "u_error_common.h" #include "u_cfg_os_platform_specific.h" // For U_CFG_OS_YIELD_MS +#include "u_timeout.h" #include "u_port_debug.h" #include "u_port.h" @@ -813,7 +814,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, int32_t delayMs) { int32_t errorCode = U_ERROR_COMMON_NOT_INITIALISED; - int64_t startTime = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); if ((gModemUartMutex != NULL) && (!gModemUartContext.markedForDeletion)) { @@ -831,7 +832,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, NULL, 0); uPortTaskBlock(U_CFG_OS_YIELD_MS); } while ((errorCode != 0) && - (uPortGetTickTimeMs() - startTime < delayMs)); + !uTimeoutExpiredMs(timeoutStart, delayMs)); } } diff --git a/port/platform/common/automation/DATABASE.md b/port/platform/common/automation/DATABASE.md index f2ea6948..1b788826 100644 --- a/port/platform/common/automation/DATABASE.md +++ b/port/platform/common/automation/DATABASE.md @@ -63,7 +63,7 @@ The table below defines the instances of test hardware available on the `ubxlib` | 21 | WHRE board (NINA-W1), Cat M1 | ESP32 | | ESP-IDF | | SARA_R410M_02B | cell mqtt_client | | U_CFG_APP_FILTER=cellMqtt.mqttClient.exampleMqtt.cellMuxMqtt.cellCfgGreeting.cellInfo.cellCfgTime.cellSim. U_CFG_TEST_PIN_A=-1 U_CFG_TEST_PIN_B=-1 U_CFG_TEST_PIN_C=-1 U_CFG_TEST_UART_A=-1 U_DEBUG_UTILS_DUMP_THREADS | | 22.0 | NINA-W1 + EVK, Cat M1 | ESP32 | esp32:esp32:nina_w10 | Arduino | ESP-IDF | SARA_R422 M8 | port device network sock security cell mqtt_client http_client gnss location || U_CFG_PPP_ENABLE U_CFG_TEST_CELL_PWR_DISABLE U_GNSS_TEST_DISABLE_ACTIVE_ANTENNA_DISABLE U_CFG_MONITOR_DTR_RTS_OFF U_CFG_1V8_SIM_WORKAROUND U_CFG_APP_PIN_CELL_ENABLE_POWER=-1 U_CFG_APP_PIN_CELL_VINT=-1 U_CFG_APP_PIN_CELL_PWR_ON=5 U_CFG_APP_PIN_CELL_TXD=14 U_CFG_TEST_PIN_A=-1 U_CFG_TEST_PIN_B=-1 U_CFG_TEST_PIN_C=-1 U_CFG_TEST_UART_A=-1 U_DEBUG_UTILS_DUMP_THREADS | | 22.1 | EVK, Cat M1 | ESP32 | | ESP-IDF | | SARA_R422 | port | | U_CFG_APP_FILTER=espidf. U_CFG_PPP_ENABLE U_CFG_TEST_CELL_PWR_DISABLE U_CFG_MONITOR_DTR_RTS_OFF U_CFG_1V8_SIM_WORKAROUND U_CFG_APP_PIN_CELL_ENABLE_POWER=-1 U_CFG_APP_PIN_CELL_VINT=-1 U_CFG_APP_PIN_CELL_PWR_ON=5 U_CFG_APP_PIN_CELL_TXD=14 U_CFG_TEST_PIN_A=-1 U_CFG_TEST_PIN_B=-1 U_CFG_TEST_PIN_C=-1 U_CFG_TEST_UART_A=-1 U_DEBUG_UTILS_DUMP_THREADS | -| 23 | Windows + EVK, Cat M1, uConnect | WIN32 | | WINDOWS | MSVC | SARA_R5 M8 NINA_W15 | port device network sock ble wifi cell short_range security mqtt_client http_client ubx_protocol gnss spartn location geofence |cell short_range gnss geodesic| U_CFG_GEOFENCE U_AT_CLIENT_PRINT_WITH_TIMESTAMP U_CFG_HEAP_MONITOR U_ASSERT_HOOK_FUNCTION_TEST_RETURN U_CFG_TEST_DISABLE_GREETING_CALLBACK U_CFG_MUTEX_DEBUG U_NETWORK_GNSS_CFG_CELL_USE_AT_ONLY U_GNSS_MSG_TEST_MESSAGE_RECEIVE_NON_BLOCKING_PRINT U_CFG_TEST_NET_STATUS_CELL=RF_SWITCH_A U_CFG_TEST_NET_STATUS_SHORT_RANGE=PWR_SWITCH_A U_BLE_TEST_CFG_REMOTE_SPS_CENTRAL=6009C390E4DAp U_CFG_TEST_UART_A=100 U_CFG_APP_SHORT_RANGE_UART=101 U_CFG_APP_SHORT_RANGE_UART2=104 U_CFG_APP_CELL_UART=102 U_CFG_QUEUE_DEBUG | +| 23 | Windows + EVK, Cat M1, uConnect | WIN32 | | WINDOWS | MSVC | SARA_R5 M8 NINA_W15 | port device network sock ble wifi cell short_range security mqtt_client http_client ubx_protocol gnss spartn location geofence |cell short_range gnss geodesic| U_CFG_TEST_TIMEOUT_SPEED_UP=14 U_CFG_GEOFENCE U_AT_CLIENT_PRINT_WITH_TIMESTAMP U_CFG_HEAP_MONITOR U_ASSERT_HOOK_FUNCTION_TEST_RETURN U_CFG_TEST_DISABLE_GREETING_CALLBACK U_CFG_MUTEX_DEBUG U_NETWORK_GNSS_CFG_CELL_USE_AT_ONLY U_GNSS_MSG_TEST_MESSAGE_RECEIVE_NON_BLOCKING_PRINT U_CFG_TEST_NET_STATUS_CELL=RF_SWITCH_A U_CFG_TEST_NET_STATUS_SHORT_RANGE=PWR_SWITCH_A U_BLE_TEST_CFG_REMOTE_SPS_CENTRAL=6009C390E4DAp U_CFG_TEST_UART_A=100 U_CFG_APP_SHORT_RANGE_UART=101 U_CFG_APP_SHORT_RANGE_UART2=104 U_CFG_APP_CELL_UART=102 U_CFG_QUEUE_DEBUG | | 24.0 | Linux/Posix under Zephyr | LINUX32 | native_posix | Zephyr | | | port gnss geofence | gnss geodesic | U_CFG_APP_FILTER=port.atClient.gnssFenceStandalone. U_CFG_GEOFENCE U_CFG_GNSS_FENCE_USE_GEODESIC U_CFG_HEAP_MONITOR U_ASSERT_HOOK_FUNCTION_TEST_RETURN U_CFG_MUTEX_DEBUG U_CFG_TEST_UART_A=0 U_CFG_TEST_UART_B=1 | | 24.1 | Linux + EVK, live network | LINUX64 | | Linux | | LARA_R6 | port | | U_CFG_APP_FILTER=port.examplePppLinuxSockets. U_CFG_PPP_ENABLE U_CFG_HEAP_MONITOR U_CFG_APP_CELL_UART=0 U_CFG_APP_CELL_UART_PPP=1 U_CFG_APP_UART_PREFIX=/dev/ublox_lara_r6_usb_at_ U_CELL_TEST_CFG_APN=iot.1nce.net U_CELL_CFG_APN_DEFAULT=iot.1nce.net U_CELL_TEST_NO_INVALID_APN U_CELL_TEST_CFG_BANDMASK1=0x0000000000080084ULL U_CELL_NET_TEST_RAT=U_CELL_NET_RAT_LTE U_CELL_TEST_CFG_MNO_PROFILE=90 U_CFG_MUTEX_DEBUG | | 25 | HPG Solution board (NINA-W1), live network | ESP32 | | ESP-IDF | | LARA_R6 M9 | port device network sock cell security mqtt_client gnss location geofence || U_CFG_GEOFENCE U_CFG_TEST_GNSS_POWER_SAVING_NOT_SUPPORTED U_CFG_TEST_DISABLE_MUX U_GNSS_MGA_TEST_ASSIST_NOW_AUTONOMOUS_NOT_SUPPORTED U_NETWORK_GNSS_CFG_CELL_USE_AT_ONLY U_HTTP_CLIENT_DISABLE_TEST U_CELL_CFG_TEST_USE_FIXED_TIME_SECONDS U_CFG_TEST_CELL_GEOFENCE U_CFG_MONITOR_DTR_RTS_OFF U_CELL_TEST_NO_INVALID_APN U_CELL_TEST_CFG_BANDMASK1=0x0000000000080084ULL U_CELL_NET_TEST_RAT=U_CELL_NET_RAT_LTE U_CELL_TEST_CFG_MNO_PROFILE=90 U_CFG_APP_PIN_CELL_ENABLE_POWER=-1 U_CFG_APP_PIN_CELL_PWR_ON=0x800c U_CFG_APP_PIN_CELL_RESET=13 U_CELL_RESET_PIN_DRIVE_MODE=U_PORT_GPIO_DRIVE_MODE_NORMAL U_CFG_APP_PIN_CELL_VINT=0x8025 U_CFG_APP_PIN_CELL_DTR=15 U_CFG_APP_PIN_CELL_TXD=25 U_CFG_APP_PIN_CELL_RXD=26 U_CFG_APP_PIN_CELL_RTS=27 U_CFG_APP_PIN_CELL_CTS=36 U_CFG_APP_GNSS_I2C=0 U_GNSS_TEST_I2C_ADDRESS_EXTRA=0x43 U_CFG_APP_CELL_PIN_GNSS_POWER=-1 U_CFG_APP_CELL_PIN_GNSS_DATA_READY=-1 U_CFG_TEST_PIN_A=-1 U_CFG_TEST_PIN_B=-1 U_CFG_TEST_PIN_C=-1 U_CFG_TEST_UART_A=-1 U_DEBUG_UTILS_DUMP_THREADS | diff --git a/port/platform/common/mutex_debug/u_mutex_debug.c b/port/platform/common/mutex_debug/u_mutex_debug.c index d5d63acd..e01d0b51 100644 --- a/port/platform/common/mutex_debug/u_mutex_debug.c +++ b/port/platform/common/mutex_debug/u_mutex_debug.c @@ -32,6 +32,7 @@ #include "u_cfg_sw.h" #include "u_cfg_os_platform_specific.h" #include "u_error_common.h" +#include "u_timeout.h" #include "u_assert.h" #include "u_port.h" #include "u_port_debug.h" @@ -378,7 +379,7 @@ static void watchdogTask(void *pParam) { uMutexInfo_t *pMutexInfo; uMutexFunctionInfo_t *pWaiting; - int64_t calledMs = 0; + uTimeoutStart_t timeoutStart = uTimeoutStart(); bool callCallback = false; (void) pParam; @@ -417,7 +418,8 @@ static void watchdogTask(void *pParam) pMutexInfo = pMutexInfo->pNext; // Don't call the callback too often though - if (uPortGetTickTimeMs() - calledMs < (U_MUTEX_DEBUG_WATCHDOG_MAX_BARK_SECONDS * 1000)) { + if (!uTimeoutExpiredSeconds(timeoutStart, + U_MUTEX_DEBUG_WATCHDOG_MAX_BARK_SECONDS)) { callCallback = false; } } @@ -427,7 +429,7 @@ static void watchdogTask(void *pParam) if (callCallback) { // Call the callback outside the locks so that it can have them gpWatchdogCallback(gpWatchdogCallbackParam); - calledMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); } // Sleep until the next go diff --git a/port/platform/esp-idf/src/u_port_uart.c b/port/platform/esp-idf/src/u_port_uart.c index 6da41983..5914ef83 100644 --- a/port/platform/esp-idf/src/u_port_uart.c +++ b/port/platform/esp-idf/src/u_port_uart.c @@ -32,6 +32,7 @@ #include "u_compiler.h" // U_ATOMIC_XXX() macros #include "u_error_common.h" +#include "u_timeout.h" #include "u_port_debug.h" #include "u_port.h" #include "u_port_os.h" @@ -646,7 +647,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, { int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED; uart_event_t event; - int64_t startTime = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); if (gMutex != NULL) { @@ -672,7 +673,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, (void *) &event); uPortTaskBlock(U_CFG_OS_YIELD_MS); } while ((errorCode != 0) && - (uPortGetTickTimeMs() - startTime < delayMs)); + !uTimeoutExpiredMs(timeoutStart, delayMs)); } U_PORT_MUTEX_UNLOCK(gMutex); diff --git a/port/platform/esp-idf/test/u_espidf_ppp_test.c b/port/platform/esp-idf/test/u_espidf_ppp_test.c index ff1535c8..7a52f578 100644 --- a/port/platform/esp-idf/test/u_espidf_ppp_test.c +++ b/port/platform/esp-idf/test/u_espidf_ppp_test.c @@ -73,6 +73,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" // In order to provide a comms #include "u_network_test_shared_cfg.h" // path for the socket @@ -253,7 +255,7 @@ static void rxTask(void *pParameter) pTestConfig->bytesReceived, 0); if (sizeBytes > 0) { U_TEST_PRINT_LINE("received %d byte(s) of data @%d ms.", - sizeBytes, (int32_t) uPortGetTickTimeMs()); + sizeBytes, uPortGetTickTimeMs()); pTestConfig->bytesReceived += sizeBytes; pTestConfig->packetsReceived++; } else { @@ -284,18 +286,18 @@ static size_t sendTcp(int32_t sock, const char *pData, size_t sizeBytes) { int32_t x; size_t sentSizeBytes = 0; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; U_TEST_PRINT_LINE("sending %d byte(s) of TCP data...", sizeBytes); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((sentSizeBytes < sizeBytes) && - ((uPortGetTickTimeMs() - startTimeMs) < 10000)) { + !uTimeoutExpiredSeconds(timeoutStart, 10)) { x = send(sock, (const void *) pData, sizeBytes - sentSizeBytes, 0); if (x > 0) { sentSizeBytes += x; pData += x; U_TEST_PRINT_LINE("sent %d byte(s) of TCP data @%d ms.", - sentSizeBytes, (int32_t) uPortGetTickTimeMs()); + sentSizeBytes, uPortGetTickTimeMs()); } } @@ -389,7 +391,7 @@ U_PORT_TEST_FUNCTION("[espidfSock]", "espidfSockTcp") int32_t x; size_t sizeBytes = 0; size_t offset; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // Whatever called us likely initialised the // port so deinitialise it here to obtain the @@ -457,7 +459,7 @@ U_PORT_TEST_FUNCTION("[espidfSock]", "espidfSockTcp") // Throw random sized segments up... offset = 0; x = 0; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (offset < gTestConfig.bytesToSend) { sizeBytes = (rand() % U_SOCK_TEST_MAX_TCP_READ_WRITE_SIZE) + 1; sizeBytes = fix(sizeBytes, U_SOCK_TEST_MAX_TCP_READ_WRITE_SIZE); @@ -484,10 +486,10 @@ U_PORT_TEST_FUNCTION("[espidfSock]", "espidfSockTcp") U_TEST_PRINT_LINE("TCP async receive task got %d segment(s)" " totalling %d byte(s) and the send/receive" - " process took %d milliseconds.", + " process took %u milliseconds.", gTestConfig.packetsReceived, gTestConfig.bytesReceived, - uPortGetTickTimeMs() - startTimeMs); + uTimeoutElapsedMs(timeoutStart)); // Check that we reassembled everything correctly U_PORT_TEST_ASSERT(checkAgainstSentData(gSendData, diff --git a/port/platform/linux/src/u_port.c b/port/platform/linux/src/u_port.c index c66cf3d8..d3872419 100644 --- a/port/platform/linux/src/u_port.c +++ b/port/platform/linux/src/u_port.c @@ -117,8 +117,7 @@ int32_t uPortGetTickTimeMs() struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == 0) { - // Ensure that the calculation wraps correctly - ms = ((((int64_t) ts.tv_sec) * 1000) + (((int64_t) ts.tv_nsec) / 1000000)) % INT_MAX; + ms = (((int64_t) ts.tv_sec) * 1000) + (((int64_t) ts.tv_nsec) / 1000000); } return ms; diff --git a/port/platform/linux/src/u_port_ppp.c b/port/platform/linux/src/u_port_ppp.c index 8a22a3bd..e7d4ca77 100644 --- a/port/platform/linux/src/u_port_ppp.c +++ b/port/platform/linux/src/u_port_ppp.c @@ -44,6 +44,8 @@ #include "u_cfg_sw.h" #include "u_error_common.h" +#include "u_timeout.h" + #include "u_linked_list.h" #include "u_sock.h" @@ -328,7 +330,7 @@ static void terminateLink(uPortPppInterface_t *pPppInterface) int32_t sent; const char *pData; size_t retryCount = 0; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; bool pppdConnected = (pPppInterface->connectedSocket >= 0); // First, suspend normal data transfer between the entities @@ -378,9 +380,10 @@ static void terminateLink(uPortPppInterface_t *pPppInterface) // Wait for the response from pppd on the MCU side, and // from the cellular side (via the waitingForModuleDisconnect flag) - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((pPppInterface->waitingForModuleDisconnect || pppdConnected) && - (uPortGetTickTimeMs() - startTimeMs < U_PORT_PPP_CONNECT_TIMEOUT_SECONDS * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, + U_PORT_PPP_CONNECT_TIMEOUT_SECONDS)) { // Wait for data to arrive on the connected socket if (pppdConnected && socketSelect(pPppInterface->connectedSocket, U_CFG_OS_YIELD_MS)) { @@ -839,7 +842,7 @@ int32_t uPortPppConnect(void *pDevHandle, { int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED; uPortPppInterface_t *pPppInterface; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // There is no way for this code to provide the authentication // parameters back to pppd, the user has to set them when @@ -878,9 +881,10 @@ int32_t uPortPppConnect(void *pDevHandle, // people at a PPP kinda problem errorCode = (int32_t) U_ERROR_COMMON_PROTOCOL_ERROR; // Wait for the IP connection to succeed - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((!pPppInterface->ipConnected) && - (uPortGetTickTimeMs() - startTimeMs < U_PORT_PPP_CONNECT_TIMEOUT_SECONDS * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, + U_PORT_PPP_CONNECT_TIMEOUT_SECONDS)) { uPortTaskBlock(250); } if (pPppInterface->ipConnected) { diff --git a/port/platform/linux/test/u_linux_ppp_test.c b/port/platform/linux/test/u_linux_ppp_test.c index c86a42a3..a880eed1 100644 --- a/port/platform/linux/test/u_linux_ppp_test.c +++ b/port/platform/linux/test/u_linux_ppp_test.c @@ -71,6 +71,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" // In order to provide a comms #include "u_network_test_shared_cfg.h" // path for the socket @@ -251,7 +253,7 @@ static void rxTask(void *pParameter) pTestConfig->bytesReceived, 0); if (sizeBytes > 0) { U_TEST_PRINT_LINE("received %d byte(s) of data @%d ms.", - sizeBytes, (int32_t) uPortGetTickTimeMs()); + sizeBytes, uPortGetTickTimeMs()); pTestConfig->bytesReceived += sizeBytes; pTestConfig->packetsReceived++; } else { @@ -282,18 +284,18 @@ static size_t sendTcp(int32_t sock, const char *pData, size_t sizeBytes) { int32_t x; size_t sentSizeBytes = 0; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; U_TEST_PRINT_LINE("sending %d byte(s) of TCP data...", sizeBytes); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((sentSizeBytes < sizeBytes) && - ((uPortGetTickTimeMs() - startTimeMs) < 10000)) { + !uTimeoutExpiredSeconds(timeoutStart, 10)) { x = send(sock, (const void *) pData, sizeBytes - sentSizeBytes, 0); if (x > 0) { sentSizeBytes += x; pData += x; U_TEST_PRINT_LINE("sent %d byte(s) of TCP data @%d ms.", - sentSizeBytes, (int32_t) uPortGetTickTimeMs()); + sentSizeBytes, uPortGetTickTimeMs()); } } @@ -396,7 +398,7 @@ U_PORT_TEST_FUNCTION("[testLinuxSock]", "testLinuxSockTcp") int32_t x; size_t sizeBytes = 0; size_t offset; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; struct ifreq interface = {0}; // Whatever called us likely initialised the @@ -471,7 +473,7 @@ U_PORT_TEST_FUNCTION("[testLinuxSock]", "testLinuxSockTcp") // Throw random sized segments up... offset = 0; x = 0; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (offset < gTestConfig.bytesToSend) { sizeBytes = (rand() % U_SOCK_TEST_MAX_TCP_READ_WRITE_SIZE) + 1; sizeBytes = fix(sizeBytes, U_SOCK_TEST_MAX_TCP_READ_WRITE_SIZE); @@ -498,10 +500,10 @@ U_PORT_TEST_FUNCTION("[testLinuxSock]", "testLinuxSockTcp") U_TEST_PRINT_LINE("TCP async receive task got %d segment(s)" " totalling %d byte(s) and the send/receive" - " process took %d milliseconds.", + " process took %u milliseconds.", gTestConfig.packetsReceived, gTestConfig.bytesReceived, - uPortGetTickTimeMs() - startTimeMs); + uTimeoutElapsedMs(timeoutStart)); // Check that we reassembled everything correctly U_PORT_TEST_ASSERT(checkAgainstSentData(gSendData, diff --git a/port/platform/nrf5sdk/src/u_port_uart.c b/port/platform/nrf5sdk/src/u_port_uart.c index b96d45f1..6201411d 100644 --- a/port/platform/nrf5sdk/src/u_port_uart.c +++ b/port/platform/nrf5sdk/src/u_port_uart.c @@ -47,6 +47,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_port_clib_platform_specific.h" /* Integer stdio, must be included before the other port files if any print or scan function is used. */ @@ -968,7 +970,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, { uErrorCode_t errorCode = U_ERROR_COMMON_NOT_INITIALISED; uPortUartEvent_t event; - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); if (gMutex != NULL) { @@ -988,7 +990,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, &event, sizeof(event)); uPortTaskBlock(U_CFG_OS_YIELD_MS); } while ((errorCode != 0) && - (uPortGetTickTimeMs() - startTimeMs < delayMs)); + !uTimeoutExpiredMs(timeoutStart, delayMs)); } U_PORT_MUTEX_UNLOCK(gMutex); diff --git a/port/platform/platformio/example/position.c b/port/platform/platformio/example/position.c index 33f64921..63427668 100644 --- a/port/platform/platformio/example/position.c +++ b/port/platform/platformio/example/position.c @@ -80,13 +80,13 @@ void main() printf("Waiting for position."); uLocation_t location; int tries = 0; - int64_t startTime = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); do { printf("."); errorCode = uLocationGet(deviceHandle, U_LOCATION_TYPE_GNSS, NULL, NULL, &location, NULL); } while (errorCode == U_ERROR_COMMON_TIMEOUT && tries++ < 4); - printf("\nWaited: %lld s\n", (uPortGetTickTimeMs() - startTime) / 1000); + printf("\nWaited: %u s\n", uTimeoutElapsedSeconds(timeoutStart)); if (errorCode == 0) { printf("Position: https://maps.google.com/?q=%d.%07d,%d.%07d\n", location.latitudeX1e7 / 10000000, location.latitudeX1e7 % 10000000, diff --git a/port/platform/platformio/inc_src.txt b/port/platform/platformio/inc_src.txt index 2386ea31..0da5041f 100644 --- a/port/platform/platformio/inc_src.txt +++ b/port/platform/platformio/inc_src.txt @@ -24,6 +24,7 @@ common/at_client common/error common/assert +common/timeout common/location common/mqtt_client common/http_client diff --git a/port/platform/stm32cube/src/u_port_i2c.c b/port/platform/stm32cube/src/u_port_i2c.c index baef69e0..58158fe7 100644 --- a/port/platform/stm32cube/src/u_port_i2c.c +++ b/port/platform/stm32cube/src/u_port_i2c.c @@ -26,6 +26,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_port.h" #include "u_port_os.h" #include "u_port_gpio.h" // For unblocking @@ -239,11 +241,11 @@ static int32_t configureHw(I2C_TypeDef *pReg, int32_t clockHertz) static bool waitFlagOk(I2C_TypeDef *pReg, uint32_t flag, FlagStatus status, int32_t timeoutMs) { - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); bool wait; while ((wait = (U_PORT_HAL_I2C_GET_FLAG(pReg, flag) != status)) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs)) { + !uTimeoutExpiredMs(timeoutStart, timeoutMs)) { } return !wait; @@ -256,12 +258,13 @@ static bool waitFlagOk(I2C_TypeDef *pReg, uint32_t flag, static bool waitTransmitOk(I2C_TypeDef *pReg, uint32_t flag, int32_t timeoutMs) { - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); bool wait; bool ackFailed = false; while ((wait = (U_PORT_HAL_I2C_GET_FLAG(pReg, flag) == RESET)) && - (uPortGetTickTimeMs() - startTimeMs < timeoutMs) && !ackFailed) { + !uTimeoutExpiredMs(timeoutStart, timeoutMs) && + !ackFailed) { if (U_PORT_HAL_I2C_GET_FLAG(pReg, I2C_FLAG_AF) == SET) { // If there's been an acknowledgement failure, // give up in an organised way diff --git a/port/platform/stm32cube/src/u_port_private.c b/port/platform/stm32cube/src/u_port_private.c index 28377d5f..081a2dc0 100644 --- a/port/platform/stm32cube/src/u_port_private.c +++ b/port/platform/stm32cube/src/u_port_private.c @@ -106,7 +106,7 @@ typedef struct uPortPrivateTimer_t { * -------------------------------------------------------------- */ // Counter to keep track of RTOS ticks: NOT static -// so that the stm32f4xx_it.c can update it. +// so that the u_exception_handler.c can update it. int32_t gTickTimerRtosCount; // Get the GPIOx address for a given GPIO port. diff --git a/port/platform/stm32cube/src/u_port_uart.c b/port/platform/stm32cube/src/u_port_uart.c index 861aa607..eab0b11b 100644 --- a/port/platform/stm32cube/src/u_port_uart.c +++ b/port/platform/stm32cube/src/u_port_uart.c @@ -34,6 +34,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_port_clib_platform_specific.h" /* Integer stdio, must be included before the other port files if any print or scan function is used. */ @@ -1313,7 +1315,7 @@ int32_t uPortUartWrite(int32_t handle, uPortUartData_t *pUartData; USART_TypeDef *pReg; bool txOk = true; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; if (gMutex != NULL) { @@ -1325,7 +1327,7 @@ int32_t uPortUartWrite(int32_t handle, pReg = gUartCfg[pUartData->uart].pReg; // Do the blocking send sizeOrErrorCode = (int32_t) sizeBytes; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((sizeBytes > 0) && (txOk)) { LL_USART_TransmitData8(pReg, *pDataPtr); // Hint when debugging: if your code stops dead here @@ -1337,7 +1339,8 @@ int32_t uPortUartWrite(int32_t handle, // was wrong and it's not connected to the right // thing. while (!(txOk = LL_USART_IsActiveFlag_TXE(pReg)) && - (uPortGetTickTimeMs() - startTimeMs < U_PORT_UART_WRITE_TIMEOUT_MS)) {} + !uTimeoutExpiredMs(timeoutStart, + U_PORT_UART_WRITE_TIMEOUT_MS)) {} if (txOk) { pDataPtr++; sizeBytes--; @@ -1346,7 +1349,8 @@ int32_t uPortUartWrite(int32_t handle, // Wait for transmission to complete so that we don't // write over stuff the next time while (!LL_USART_IsActiveFlag_TC(pReg) && - (uPortGetTickTimeMs() - startTimeMs < U_PORT_UART_WRITE_TIMEOUT_MS)) {} + !uTimeoutExpiredMs(timeoutStart, + U_PORT_UART_WRITE_TIMEOUT_MS)) {} sizeOrErrorCode -= (int32_t) sizeBytes; } @@ -1521,7 +1525,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, uErrorCode_t errorCode = U_ERROR_COMMON_NOT_INITIALISED; uPortUartData_t *pUartData; uPortUartEvent_t event; - int64_t startTime = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); if (gMutex != NULL) { @@ -1541,7 +1545,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, &event, sizeof(event)); uPortTaskBlock(U_CFG_OS_YIELD_MS); } while ((errorCode != 0) && - (uPortGetTickTimeMs() - startTime < delayMs)); + !uTimeoutExpiredMs(timeoutStart, delayMs)); } U_PORT_MUTEX_UNLOCK(gMutex); diff --git a/port/platform/windows/src/u_port.c b/port/platform/windows/src/u_port.c index 81b81533..bfed98a4 100644 --- a/port/platform/windows/src/u_port.c +++ b/port/platform/windows/src/u_port.c @@ -153,7 +153,7 @@ void uPortDeinit() // Get the current tick in milliseconds. int32_t uPortGetTickTimeMs() { - return GetTickCount() % INT_MAX; + return GetTickCount(); } // Get the minimum amount of heap free, ever, in bytes. diff --git a/port/platform/zephyr/src/u_port_ppp.c b/port/platform/zephyr/src/u_port_ppp.c index e22aff31..f5f6136e 100644 --- a/port/platform/zephyr/src/u_port_ppp.c +++ b/port/platform/zephyr/src/u_port_ppp.c @@ -49,6 +49,8 @@ #include "u_cfg_sw.h" #include "u_error_common.h" +#include "u_timeout.h" + #include "u_sock.h" // uSockStringToAddress() #include "u_port.h" @@ -647,7 +649,7 @@ static void netIfEventCallback(struct net_mgmt_event_callback *pCb, // Detach the Zephyr PPP interface. static void pppDetach(uPortPppInterface_t *pPppInterface) { - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; if ((pPppInterface != NULL) && (pPppInterface->pNetIf != NULL)) { // There is a bug in Zephyr versions before 3.6.0 which means @@ -666,9 +668,10 @@ static void pppDetach(uPortPppInterface_t *pPppInterface) // Wait for netIfEventCallback to be called-back // with the event NET_EVENT_IF_DOWN; it // will set gpPppInterface->ipConnected - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((gpPppInterface->ipConnected) && - (uPortGetTickTimeMs() - startTimeMs < U_PORT_PPP_DISCONNECT_TIMEOUT_SECONDS * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, + U_PORT_PPP_DISCONNECT_TIMEOUT_SECONDS)) { uPortTaskBlock(250); } pPppInterface->ipConnected = false; @@ -834,7 +837,7 @@ int32_t uPortPppConnect(void *pDevHandle, { int32_t errorCode = (int32_t) U_ERROR_COMMON_NOT_INITIALISED; struct net_if *pNetIf; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // Note: Zephyr does not (as of version 3.5 at least) support // entering a user name and password, and probably doesn't @@ -888,9 +891,10 @@ int32_t uPortPppConnect(void *pDevHandle, // Wait for netIfEventCallback to be called back // with the event NET_EVENT_IPV4_ADDR_ADD; it // will set gpPppInterface->ipConnected - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((!gpPppInterface->ipConnected) && - (uPortGetTickTimeMs() - startTimeMs < U_PORT_PPP_CONNECT_TIMEOUT_SECONDS * 1000)) { + !uTimeoutExpiredSeconds(timeoutStart, + U_PORT_PPP_CONNECT_TIMEOUT_SECONDS)) { uPortTaskBlock(250); } if (gpPppInterface->ipConnected) { diff --git a/port/platform/zephyr/src/u_port_uart.c b/port/platform/zephyr/src/u_port_uart.c index 2f6a1123..a120b6f8 100644 --- a/port/platform/zephyr/src/u_port_uart.c +++ b/port/platform/zephyr/src/u_port_uart.c @@ -54,6 +54,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_linked_list.h" #include "u_port_debug.h" @@ -839,7 +841,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, uErrorCode_t errorCode = U_ERROR_COMMON_NOT_INITIALISED; uPortUartData_t *pUartData; uPortUartEvent_t event; - int64_t startTime = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); if (gMutex != NULL) { @@ -858,7 +860,7 @@ int32_t uPortUartEventTrySend(int32_t handle, uint32_t eventBitMap, &event, sizeof(event)); uPortTaskBlock(U_CFG_OS_YIELD_MS); } while ((errorCode != 0) && - (uPortGetTickTimeMs() - startTime < delayMs)); + !uTimeoutExpiredMs(timeoutStart, delayMs)); } U_PORT_MUTEX_UNLOCK(gMutex); diff --git a/port/platform/zephyr/test/u_zephyr_ppp_test.c b/port/platform/zephyr/test/u_zephyr_ppp_test.c index 30861e5e..05b24d77 100644 --- a/port/platform/zephyr/test/u_zephyr_ppp_test.c +++ b/port/platform/zephyr/test/u_zephyr_ppp_test.c @@ -59,6 +59,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_network.h" // In order to provide a comms path #include "u_network_test_shared_cfg.h" @@ -254,7 +256,7 @@ static void rxTask(void *pParameter) pTestConfig->bytesReceived, 0); if (sizeBytes > 0) { U_TEST_PRINT_LINE("received %d byte(s) of data @%d ms.", - sizeBytes, (int32_t) uPortGetTickTimeMs()); + sizeBytes, uPortGetTickTimeMs()); pTestConfig->bytesReceived += sizeBytes; pTestConfig->packetsReceived++; } else { @@ -285,18 +287,18 @@ static size_t sendTcp(int32_t sock, const char *pData, size_t sizeBytes) { int32_t x; size_t sentSizeBytes = 0; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; U_TEST_PRINT_LINE("sending %d byte(s) of TCP data...", sizeBytes); - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((sentSizeBytes < sizeBytes) && - ((uPortGetTickTimeMs() - startTimeMs) < 10000)) { + !uTimeoutExpiredSeconds(timeoutStart, 10)) { x = zsock_send(sock, (const void *) pData, sizeBytes - sentSizeBytes, 0); if (x > 0) { sentSizeBytes += x; pData += x; U_TEST_PRINT_LINE("sent %d byte(s) of TCP data @%d ms.", - sentSizeBytes, (int32_t) uPortGetTickTimeMs()); + sentSizeBytes, uPortGetTickTimeMs()); } } @@ -390,7 +392,7 @@ U_PORT_TEST_FUNCTION("[zephyrSock]", "zephyrSockTcp") int32_t x; size_t sizeBytes = 0; size_t offset; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; // Whatever called us likely initialised the // port so deinitialise it here to obtain the @@ -460,7 +462,7 @@ U_PORT_TEST_FUNCTION("[zephyrSock]", "zephyrSockTcp") // Throw random sized segments up... offset = 0; x = 0; - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while (offset < gTestConfig.bytesToSend) { sizeBytes = (rand() % U_SOCK_TEST_MAX_TCP_READ_WRITE_SIZE) + 1; sizeBytes = fix(sizeBytes, U_SOCK_TEST_MAX_TCP_READ_WRITE_SIZE); @@ -487,10 +489,10 @@ U_PORT_TEST_FUNCTION("[zephyrSock]", "zephyrSockTcp") U_TEST_PRINT_LINE("TCP async receive task got %d segment(s)" " totalling %d byte(s) and the send/receive" - " process took %d milliseconds.", + " process took %u milliseconds.", gTestConfig.packetsReceived, gTestConfig.bytesReceived, - uPortGetTickTimeMs() - startTimeMs); + uTimeoutElapsedMs(timeoutStart)); // Check that we reassembled everything correctly U_PORT_TEST_ASSERT(checkAgainstSentData(gSendData, diff --git a/port/test/u_port_test.c b/port/test/u_port_test.c index fece7bb7..a440e82f 100644 --- a/port/test/u_port_test.c +++ b/port/test/u_port_test.c @@ -38,6 +38,7 @@ #include "string.h" // strlen() and strcmp() #include "stdio.h" // snprintf() #include "time.h" // time_t and struct tm + #include "u_compiler.h" #include "u_cfg_sw.h" @@ -70,6 +71,8 @@ #include "u_port_event_queue.h" #include "u_error_common.h" +#include "u_timeout.h" + #include "u_assert.h" #include "u_test_util_resource_check.h" @@ -1511,7 +1514,7 @@ U_PORT_TEST_FUNCTION("[port]", "portOs") U_PORT_TEST_ASSERT(errorCode == 0); U_PORT_TEST_ASSERT(gTaskHandle != NULL); - U_TEST_PRINT_LINE("time now %d ms.", (int32_t) uPortGetTickTimeMs()); + U_TEST_PRINT_LINE("time now %d ms.", uPortGetTickTimeMs()); uPortTaskBlock(200); U_TEST_PRINT_LINE("unlocking mutex, allowing task to execute."); U_PORT_TEST_ASSERT(uPortMutexUnlock(gMutexHandle) == 0);; @@ -1602,7 +1605,7 @@ U_PORT_TEST_FUNCTION("[port]", "portOs") timeNowMs = uPortGetTickTimeMs() - startTimeMs; U_TEST_PRINT_LINE("according to uPortGetTickTimeMs()" - " the test took %d ms.", (int32_t) timeNowMs); + " the test took %d ms.", timeNowMs); #ifdef U_PORT_TEST_CHECK_TIME_TAKEN U_PORT_TEST_ASSERT((timeNowMs > 0) && (timeNowMs < U_PORT_TEST_OS_GUARD_DURATION_MS)); @@ -1792,7 +1795,7 @@ U_PORT_TEST_FUNCTION("[port]", "portOsSemaphore") timeNowMs = uPortGetTickTimeMs() - startTimeTestMs; U_TEST_PRINT_LINE("according to uPortGetTickTimeMs() the test took %d ms.", - (int32_t) timeNowMs); + timeNowMs); #ifdef U_PORT_TEST_CHECK_TIME_TAKEN U_PORT_TEST_ASSERT((timeNowMs > 0) && (timeNowMs < U_PORT_TEST_OS_GUARD_DURATION_MS)); @@ -1842,8 +1845,7 @@ U_PORT_TEST_FUNCTION("[port]", "portOsExtended") uPortTaskBlock(U_PORT_TEST_OS_BLOCK_TIME_MS); timeDelta = uPortGetTickTimeMs() - timeNowMs; U_TEST_PRINT_LINE("uPortTaskBlock(%d) blocked for %d ms.", - U_PORT_TEST_OS_BLOCK_TIME_MS, - (int32_t) (timeDelta)); + U_PORT_TEST_OS_BLOCK_TIME_MS, timeDelta); U_PORT_TEST_ASSERT((timeDelta >= U_PORT_TEST_OS_BLOCK_TIME_MS - U_PORT_TEST_OS_BLOCK_TIME_TOLERANCE_MS) && (timeDelta <= U_PORT_TEST_OS_BLOCK_TIME_MS + @@ -1872,8 +1874,7 @@ U_PORT_TEST_FUNCTION("[port]", "portOsExtended") uPortTaskBlock(U_PORT_TEST_OS_BLOCK_TIME_MS); timeDelta = uPortGetTickTimeMs() - timeNowMs; U_TEST_PRINT_LINE("uPortTaskBlock(%d) blocked for %d ms.", - U_PORT_TEST_OS_BLOCK_TIME_MS, - (int32_t) (timeDelta)); + U_PORT_TEST_OS_BLOCK_TIME_MS, timeDelta); U_PORT_TEST_ASSERT((timeDelta >= U_PORT_TEST_OS_BLOCK_TIME_MS - U_PORT_TEST_OS_BLOCK_TIME_TOLERANCE_MS) && (timeDelta <= U_PORT_TEST_OS_BLOCK_TIME_MS + @@ -1887,8 +1888,7 @@ U_PORT_TEST_FUNCTION("[port]", "portOsExtended") uPortTaskBlock(U_PORT_TEST_OS_BLOCK_TIME_MS); timeDelta = uPortGetTickTimeMs() - timeNowMs; U_TEST_PRINT_LINE("uPortTaskBlock(%d) blocked for %d ms.", - U_PORT_TEST_OS_BLOCK_TIME_MS, - (int32_t) (timeDelta)); + U_PORT_TEST_OS_BLOCK_TIME_MS, timeDelta); U_PORT_TEST_ASSERT((timeDelta >= U_PORT_TEST_OS_BLOCK_TIME_MS - U_PORT_TEST_OS_BLOCK_TIME_TOLERANCE_MS) && (timeDelta <= U_PORT_TEST_OS_BLOCK_TIME_MS + @@ -3043,7 +3043,7 @@ U_PORT_TEST_FUNCTION("[port]", "portTimers") { int32_t resourceCount; int32_t y; - int64_t startTime; + uTimeoutStart_t timeoutStart; // Whatever called us likely initialised the // port so deinitialise it here to obtain the @@ -3102,9 +3102,9 @@ U_PORT_TEST_FUNCTION("[port]", "portTimers") // one-shot timer expires // Note: this test deliberately allows for slop in the actual timer // values however their relative values should still be correct - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((gTimerParameterValue[2] == 0) && - (uPortGetTickTimeMs() - startTime < 10000)) { + !uTimeoutExpiredSeconds(timeoutStart, 10)) { uPortTaskBlock(100); } U_PORT_TEST_ASSERT((gTimerParameterValue[2] == 1) && (gTimerParameterValue[3] == 3)); @@ -3120,9 +3120,9 @@ U_PORT_TEST_FUNCTION("[port]", "portTimers") U_PORT_TEST_ASSERT(uPortTimerStart(gTimerHandle[3]) == 0); U_PORT_TEST_ASSERT(uPortTimerStart(gTimerHandle[3]) == 0); // Wait for the periodic timer to expire one more time - startTime = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((gTimerParameterValue[3] < 4) && - (uPortGetTickTimeMs() - startTime < 5000)) { + !uTimeoutExpiredSeconds(timeoutStart, 5)) { uPortTaskBlock(100); } U_PORT_TEST_ASSERT(gTimerParameterValue[3] == 4); @@ -3174,7 +3174,7 @@ U_PORT_TEST_FUNCTION("[port]", "portCriticalSection") int32_t errorCode; int32_t resourceCount; uint32_t y; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; int32_t errorFlag = 0x00; // Whatever called us likely initialised the @@ -3210,8 +3210,8 @@ U_PORT_TEST_FUNCTION("[port]", "portCriticalSection") U_PORT_TEST_ASSERT(gVariable > 0); // Start the critical section - startTimeMs = uPortGetTickTimeMs(); - (void)startTimeMs; // Suppress value not being read (it is for Windows) + timeoutStart = uTimeoutStart(); + (void) timeoutStart; // Suppress value not being read (it is for Windows) errorCode = uPortEnterCritical(); // Note: don't assert inside here as we don't want to leave this test // with the critical section active, instead just set errorFlag to indicate @@ -3231,8 +3231,8 @@ U_PORT_TEST_FUNCTION("[port]", "portCriticalSection") // long time to _prove_ that the critical section has worked //lint -e{441, 550} Suppress loop variable not used in 2nd part of for() for (size_t x = 0; (gVariable == y) && - (uPortGetTickTimeMs() - startTimeMs < - U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS); x++) { + !uTimeoutExpiredMs(timeoutStart, + U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS); x++) { uPortTaskBlock(100); } #endif @@ -3248,11 +3248,11 @@ U_PORT_TEST_FUNCTION("[port]", "portCriticalSection") U_PORT_TEST_ASSERT(errorFlag == 0); // gVariable should start changing again - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); //lint -e{441, 550} Suppress loop variable not used in 2nd part of for() for (size_t x = 0; (gVariable == y) && - (uPortGetTickTimeMs() - startTimeMs < - U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS); x++) { + !uTimeoutExpiredMs(timeoutStart, + U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS); x++) { uPortTaskBlock(10); } U_PORT_TEST_ASSERT(gVariable != y); diff --git a/port/ubxlib.cmake b/port/ubxlib.cmake index 5d555039..04aecd9e 100644 --- a/port/ubxlib.cmake +++ b/port/ubxlib.cmake @@ -99,6 +99,7 @@ endfunction() u_add_module_dir(base ${UBXLIB_BASE}/common/at_client) u_add_module_dir(base ${UBXLIB_BASE}/common/error) u_add_module_dir(base ${UBXLIB_BASE}/common/assert) +u_add_module_dir(base ${UBXLIB_BASE}/common/timeout) u_add_module_dir(base ${UBXLIB_BASE}/common/location) u_add_module_dir(base ${UBXLIB_BASE}/common/mqtt_client) u_add_module_dir(base ${UBXLIB_BASE}/common/http_client) diff --git a/port/ubxlib.mk b/port/ubxlib.mk index 32c7fea4..ac0471d9 100644 --- a/port/ubxlib.mk +++ b/port/ubxlib.mk @@ -10,6 +10,7 @@ UBXLIB_MODULE_DIRS = \ ${UBXLIB_BASE}/common/at_client \ ${UBXLIB_BASE}/common/error \ ${UBXLIB_BASE}/common/assert \ + ${UBXLIB_BASE}/common/timeout \ ${UBXLIB_BASE}/common/location \ ${UBXLIB_BASE}/common/mqtt_client \ ${UBXLIB_BASE}/common/http_client \ diff --git a/ubxlib.h b/ubxlib.h index b99d7c16..f63af0c8 100644 --- a/ubxlib.h +++ b/ubxlib.h @@ -72,6 +72,7 @@ #include // Other common APIs +#include #include #include #include diff --git a/wifi/src/gen2/u_wifi.c b/wifi/src/gen2/u_wifi.c index 4c0ec7dc..47b4e7cf 100644 --- a/wifi/src/gen2/u_wifi.c +++ b/wifi/src/gen2/u_wifi.c @@ -42,6 +42,7 @@ #include "u_port_debug.h" #include "u_cfg_os_platform_specific.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_short_range_module_type.h" #include "u_short_range.h" diff --git a/wifi/src/gen2/u_wifi_cfg.c b/wifi/src/gen2/u_wifi_cfg.c index 09866bb8..bf21357b 100644 --- a/wifi/src/gen2/u_wifi_cfg.c +++ b/wifi/src/gen2/u_wifi_cfg.c @@ -37,6 +37,8 @@ #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range_module_type.h" diff --git a/wifi/src/gen2/u_wifi_http.c b/wifi/src/gen2/u_wifi_http.c index 3bff4b72..a64ab61d 100644 --- a/wifi/src/gen2/u_wifi_http.c +++ b/wifi/src/gen2/u_wifi_http.c @@ -40,6 +40,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_assert.h" #include "u_port_clib_platform_specific.h" /* Integer stdio, must be included diff --git a/wifi/src/gen2/u_wifi_loc.c b/wifi/src/gen2/u_wifi_loc.c index 7f2e900c..deb3d526 100644 --- a/wifi/src/gen2/u_wifi_loc.c +++ b/wifi/src/gen2/u_wifi_loc.c @@ -48,6 +48,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_location.h" diff --git a/wifi/src/gen2/u_wifi_mqtt.c b/wifi/src/gen2/u_wifi_mqtt.c index 8957f06c..b24f87a1 100644 --- a/wifi/src/gen2/u_wifi_mqtt.c +++ b/wifi/src/gen2/u_wifi_mqtt.c @@ -50,6 +50,8 @@ #include "u_port_debug.h" #include "u_port_event_queue.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_mqtt_common.h" diff --git a/wifi/src/gen2/u_wifi_sock.c b/wifi/src/gen2/u_wifi_sock.c index 11ecd095..78cf2d10 100644 --- a/wifi/src/gen2/u_wifi_sock.c +++ b/wifi/src/gen2/u_wifi_sock.c @@ -37,6 +37,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_assert.h" #include "u_port.h" @@ -538,8 +540,9 @@ int32_t uWifiSockReceiveFrom(uDeviceHandle_t devHandle, *pRemoteAddress = pUWiFiSocket->remoteAddress; } } - int32_t startTimeMs = uPortGetTickTimeMs(); - while (((uPortGetTickTimeMs() - startTimeMs) < 5000) && (dataSizeBytes > 0) && + uTimeoutStart_t timeoutStart = uTimeoutStart(); + while (!uTimeoutExpiredSeconds(timeoutStart, 5) && + (dataSizeBytes > 0) && ((res = uWifiSockRead(devHandle, sockHandle, pData, dataSizeBytes)) >= 0)) { tot += res; dataSizeBytes -= res; diff --git a/wifi/src/u_wifi.c b/wifi/src/u_wifi.c index 81fa6184..abc46014 100644 --- a/wifi/src/u_wifi.c +++ b/wifi/src/u_wifi.c @@ -42,6 +42,7 @@ #include "u_port_debug.h" #include "u_cfg_os_platform_specific.h" +#include "u_timeout.h" #include "u_at_client.h" #include "u_short_range_module_type.h" #include "u_short_range.h" diff --git a/wifi/src/u_wifi_cfg.c b/wifi/src/u_wifi_cfg.c index 530ac3e2..20fe680b 100644 --- a/wifi/src/u_wifi_cfg.c +++ b/wifi/src/u_wifi_cfg.c @@ -37,6 +37,8 @@ #include "u_port_os.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_short_range_module_type.h" diff --git a/wifi/src/u_wifi_geofence.c b/wifi/src/u_wifi_geofence.c index 442bc741..aa202b66 100644 --- a/wifi/src/u_wifi_geofence.c +++ b/wifi/src/u_wifi_geofence.c @@ -35,6 +35,8 @@ #include "u_error_common.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_linked_list.h" diff --git a/wifi/src/u_wifi_http.c b/wifi/src/u_wifi_http.c index 51784a6e..7ad8a882 100644 --- a/wifi/src/u_wifi_http.c +++ b/wifi/src/u_wifi_http.c @@ -51,6 +51,8 @@ #include "u_port_debug.h" #include "u_port_event_queue.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock.h" diff --git a/wifi/src/u_wifi_loc.c b/wifi/src/u_wifi_loc.c index d114d6ab..bf2a9518 100644 --- a/wifi/src/u_wifi_loc.c +++ b/wifi/src/u_wifi_loc.c @@ -48,6 +48,8 @@ #include "u_port_heap.h" #include "u_port_debug.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_location.h" @@ -611,7 +613,7 @@ int32_t uWifiLocGet(uDeviceHandle_t wifiHandle, uShortRangePrivateInstance_t *pInstance; volatile uWifiLocContext_t *pContext; uAtClientHandle_t atHandle; - int32_t startTimeMs; + uTimeoutStart_t timeoutStart; uLocation_t location; if (gUShortRangePrivateMutex != NULL) { @@ -647,10 +649,11 @@ int32_t uWifiLocGet(uDeviceHandle_t wifiHandle, uWifiPrivateUudhttpUrc, pInstance); if (errorCode == 0) { - startTimeMs = uPortGetTickTimeMs(); + timeoutStart = uTimeoutStart(); while ((pContext->errorCode == (int32_t) U_ERROR_COMMON_TIMEOUT) && (((pKeepGoingCallback == NULL) && - ((uPortGetTickTimeMs() - startTimeMs) < U_WIFI_LOC_ANSWER_TIMEOUT_SECONDS * 1000)) || + !uTimeoutExpiredSeconds(timeoutStart, + U_WIFI_LOC_ANSWER_TIMEOUT_SECONDS)) || ((pKeepGoingCallback != NULL) && pKeepGoingCallback(wifiHandle)))) { uPortTaskBlock(250); } diff --git a/wifi/src/u_wifi_mqtt.c b/wifi/src/u_wifi_mqtt.c index 33182114..de86a17e 100644 --- a/wifi/src/u_wifi_mqtt.c +++ b/wifi/src/u_wifi_mqtt.c @@ -50,6 +50,8 @@ #include "u_port_debug.h" #include "u_port_event_queue.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_mqtt_common.h" diff --git a/wifi/src/u_wifi_sock.c b/wifi/src/u_wifi_sock.c index fee6f916..a8b6b99f 100644 --- a/wifi/src/u_wifi_sock.c +++ b/wifi/src/u_wifi_sock.c @@ -45,6 +45,8 @@ #include "u_port_debug.h" #include "u_cfg_os_platform_specific.h" +#include "u_timeout.h" + #include "u_at_client.h" #include "u_sock_errno.h" @@ -1756,7 +1758,7 @@ int32_t uWifiSockAccept(uDeviceHandle_t devHandle, return -U_SOCK_EBADFD; } uWifiSockSocket_t *pServerSock = &(gSockets[sockHandle]); - int32_t startTimeMs = uPortGetTickTimeMs(); + uTimeoutStart_t timeoutStart = uTimeoutStart(); while (true) { uShortRangeLock(); uWifiSockSocket_t *pClientSock = pFindClientSocketByPort(devHandle, pServerSock->localPort); @@ -1765,8 +1767,8 @@ int32_t uWifiSockAccept(uDeviceHandle_t devHandle, *pRemoteAddress = pClientSock->remoteAddress; return pClientSock->sockHandle; } else if (gUWifiSocketAcceptTimeoutS >= 0) { - if ((uPortGetTickTimeMs() - startTimeMs) / 1000 > - gUWifiSocketAcceptTimeoutS) { + if (uTimeoutExpiredSeconds(timeoutStart, + gUWifiSocketAcceptTimeoutS)) { return U_ERROR_COMMON_TIMEOUT; } } diff --git a/wifi/test/u_wifi_captive_portal_test.c b/wifi/test/u_wifi_captive_portal_test.c index c6dc87cb..1e7634fe 100644 --- a/wifi/test/u_wifi_captive_portal_test.c +++ b/wifi/test/u_wifi_captive_portal_test.c @@ -62,6 +62,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_sock.h" #include "u_at_client.h" @@ -128,7 +130,7 @@ static uDeviceCfg_t gDeviceCfg = { } }; -static int32_t gStartTimeMs = -1; +static uTimeoutStop_t gTimeoutStop; /* ---------------------------------------------------------------- * STATIC FUNCTIONS @@ -140,8 +142,9 @@ static bool keepGoingCallback(uDeviceHandle_t devHandle) U_PORT_TEST_ASSERT(devHandle == gDeviceHandle); - if ((gStartTimeMs >= 0) && - (uPortGetTickTimeMs() - gStartTimeMs > U_WIFI_CAPTIVE_PORTAL_TEST_TIMEOUT_SECONDS * 1000)) { + if ((gTimeoutStop.durationMs > 0) && + uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -181,7 +184,8 @@ U_PORT_TEST_FUNCTION("[wifiCaptivePortal]", "wifiCaptivePortal") uNetworkInterfaceDown(gDeviceHandle, U_NETWORK_TYPE_WIFI); // Now do the actual test - gStartTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_WIFI_CAPTIVE_PORTAL_TEST_TIMEOUT_SECONDS * 1000; int32_t returnCode = uWifiCaptivePortal(gDeviceHandle, "UBXLIB_TEST_PORTAL", NULL, keepGoingCallback); U_TEST_PRINT_LINE("uWifiCaptivePortal() returned %d.", returnCode); diff --git a/wifi/test/u_wifi_geofence_test.c b/wifi/test/u_wifi_geofence_test.c index c64d060c..1e1abbd8 100644 --- a/wifi/test/u_wifi_geofence_test.c +++ b/wifi/test/u_wifi_geofence_test.c @@ -62,6 +62,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_location.h" #include "u_linked_list.h" @@ -142,7 +144,7 @@ static uShortRangeUartConfig_t gUart = { .uartPort = U_CFG_APP_SHORT_RANGE_UART, static uWifiTestPrivate_t gHandles = { -1, -1, NULL, NULL }; -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; static int32_t gErrorCode; static uGeofence_t *gpFenceA = NULL; @@ -164,7 +166,8 @@ static bool keepGoingCallback(uDeviceHandle_t param) (void) param; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -236,7 +239,6 @@ U_PORT_TEST_FUNCTION("[wifiGeofence]", "wifiGeofenceBasic") { int32_t resourceCount; uLocation_t location; - int32_t startTimeMs; int32_t x; resourceCount = uTestUtilGetDynamicResourceCount(); @@ -282,16 +284,16 @@ U_PORT_TEST_FUNCTION("[wifiGeofence]", "wifiGeofenceBasic") U_PORT_TEST_ASSERT(uWifiGeofenceApply(gHandles.devHandle, gpFenceB) == 0); U_TEST_PRINT_LINE("testing geofence with blocking Wifi location."); - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_WIFI_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_WIFI_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; // Choose Google to do this with as it seems generally the most reliable x = uWifiLocGet(gHandles.devHandle, U_LOCATION_TYPE_CLOUD_GOOGLE, U_PORT_STRINGIFY_QUOTED(U_CFG_APP_GOOGLE_MAPS_API_KEY), U_WIFI_GEOFENCE_TEST_AP_FILTER, U_WIFI_GEOFENCE_TEST_RSSI_FILTER_DBM, &location, keepGoingCallback); - U_TEST_PRINT_LINE("uWifiLocGet() returned %d in %d ms.", - x, uPortGetTickTimeMs() - startTimeMs); + U_TEST_PRINT_LINE("uWifiLocGet() returned %d in %u ms.", + x, uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); U_TEST_PRINT_LINE("%s fence A, %s fence B.", gpPositionStateString[gPositionStateA], gpPositionStateString[gPositionStateB]); @@ -305,7 +307,8 @@ U_PORT_TEST_FUNCTION("[wifiGeofence]", "wifiGeofenceBasic") gErrorCode = 0; gPositionStateA = U_GEOFENCE_POSITION_STATE_NONE; gPositionStateB = U_GEOFENCE_POSITION_STATE_NONE; - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_WIFI_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000; x = uWifiLocGetStart(gHandles.devHandle, U_LOCATION_TYPE_CLOUD_GOOGLE, U_PORT_STRINGIFY_QUOTED(U_CFG_APP_GOOGLE_MAPS_API_KEY), U_WIFI_GEOFENCE_TEST_AP_FILTER, @@ -313,9 +316,10 @@ U_PORT_TEST_FUNCTION("[wifiGeofence]", "wifiGeofenceBasic") posCallback); U_TEST_PRINT_LINE("uWifiLocGetStart() returned %d.", x); U_PORT_TEST_ASSERT(x == 0); - U_TEST_PRINT_LINE("waiting %d second(s) for result...", U_WIFI_GEOFENCE_TEST_TIMEOUT_SECONDS); + U_TEST_PRINT_LINE("waiting %u second(s) for result...", gTimeoutStop.durationMs / 1000); while ((gErrorCode >= 0) && (gErrorCode < 2) && - ((uPortGetTickTimeMs() - startTimeMs) < U_WIFI_GEOFENCE_TEST_TIMEOUT_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(250); } // On really fast systems (e.g. Linux machines) it is possible @@ -323,8 +327,8 @@ U_PORT_TEST_FUNCTION("[wifiGeofence]", "wifiGeofenceBasic") // give it a moment to do so uPortTaskBlock(250); uWifiLocGetStop(gHandles.devHandle); - U_TEST_PRINT_LINE("gErrorCode was %d after %d second(s).", gErrorCode, - (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("gErrorCode was %d after %u second(s).", gErrorCode, + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); U_TEST_PRINT_LINE("%s fence A, %s fence B.", gpPositionStateString[gPositionStateA], gpPositionStateString[gPositionStateB]); diff --git a/wifi/test/u_wifi_loc_test.c b/wifi/test/u_wifi_loc_test.c index d467f394..2082b8f5 100644 --- a/wifi/test/u_wifi_loc_test.c +++ b/wifi/test/u_wifi_loc_test.c @@ -60,6 +60,8 @@ #include "u_test_util_resource_check.h" +#include "u_timeout.h" + #include "u_location.h" #include "u_at_client.h" @@ -151,7 +153,7 @@ static uWifiLocTestLocType_t gLocType[] = { static size_t gIteration; // Stop time, global so that keepGoingCallback() can find it. -static int32_t gStopTimeMs; +static uTimeoutStop_t gTimeoutStop; // Global used for callback() to indicate what it received. static int32_t gCallback; @@ -167,7 +169,8 @@ static bool keepGoingCallback(uDeviceHandle_t param) (void) param; - if (uPortGetTickTimeMs() > gStopTimeMs) { + if (uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { keepGoing = false; } @@ -292,7 +295,6 @@ U_PORT_TEST_FUNCTION("[wifiLoc]", "wifiLocBasic") { int32_t z; int32_t resourceCount; - int32_t startTimeMs = 0; uLocation_t location; resourceCount = uTestUtilGetDynamicResourceCount(); @@ -306,16 +308,17 @@ U_PORT_TEST_FUNCTION("[wifiLoc]", "wifiLocBasic") U_TEST_PRINT_LINE("testing blocking Wifi location with %s.", gLocType[gIteration].pName); // It is possible for these cloud services to fail, so give them a few goes for (size_t y = 0; (y < U_WIFI_LOC_TEST_TRIES) && (z != 0); y++) { - startTimeMs = uPortGetTickTimeMs(); - gStopTimeMs = startTimeMs + U_WIFI_LOC_TEST_TIMEOUT_SECONDS * 1000; + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_WIFI_LOC_TEST_TIMEOUT_SECONDS * 1000; locationSetDefaults(&location); z = uWifiLocGet(gHandles.devHandle, gLocType[gIteration].type, gLocType[gIteration].pApiKey, U_WIFI_LOC_TEST_AP_FILTER, U_WIFI_LOC_TEST_RSSI_FILTER_DBM, &location, keepGoingCallback); - U_TEST_PRINT_LINE("uWifiLocGet() for %s returned %d in %d ms.", - gLocType[gIteration].pName, z, uPortGetTickTimeMs() - startTimeMs); + U_TEST_PRINT_LINE("uWifiLocGet() for %s returned %d in %u ms.", + gLocType[gIteration].pName, z, + uTimeoutElapsedMs(gTimeoutStop.timeoutStart)); } // Success or allow error code 206 on HERE since it often isn't able to establish position in our lab U_PORT_TEST_ASSERT((z == 0) || ((z == 206) && @@ -342,7 +345,8 @@ U_PORT_TEST_FUNCTION("[wifiLoc]", "wifiLocBasic") // It is possible for these cloud services to fail, so give them a few goes gCallback = INT_MIN; for (size_t y = 0; (y < U_WIFI_LOC_TEST_TRIES) && (gCallback != 0); y++) { - startTimeMs = uPortGetTickTimeMs(); + gTimeoutStop.timeoutStart = uTimeoutStart(); + gTimeoutStop.durationMs = U_WIFI_LOC_TEST_TIMEOUT_SECONDS * 1000; gCallback = INT_MIN; locationSetDefaults(&location); z = uWifiLocGetStart(gHandles.devHandle, gLocType[gIteration].type, @@ -352,9 +356,10 @@ U_PORT_TEST_FUNCTION("[wifiLoc]", "wifiLocBasic") callback); U_TEST_PRINT_LINE("uWifiLocGetStart() for %s returned %d.", gLocType[gIteration].pName, z); U_PORT_TEST_ASSERT(z == 0); - U_TEST_PRINT_LINE("waiting %d second(s) for result...", U_WIFI_LOC_TEST_TIMEOUT_SECONDS); + U_TEST_PRINT_LINE("waiting %u second(s) for result...", gTimeoutStop.durationMs / 1000); while ((gCallback == INT_MIN) && - ((uPortGetTickTimeMs() - startTimeMs) < U_WIFI_LOC_TEST_TIMEOUT_SECONDS * 1000)) { + !uTimeoutExpiredMs(gTimeoutStop.timeoutStart, + gTimeoutStop.durationMs)) { uPortTaskBlock(250); } if (gCallback != 0) { @@ -363,8 +368,8 @@ U_PORT_TEST_FUNCTION("[wifiLoc]", "wifiLocBasic") } } uWifiLocGetStop(gHandles.devHandle); - U_TEST_PRINT_LINE("gCallback was %d after %d second(s).", gCallback, - (uPortGetTickTimeMs() - startTimeMs) / 1000); + U_TEST_PRINT_LINE("gCallback was %d after %u second(s).", gCallback, + uTimeoutElapsedSeconds(gTimeoutStop.timeoutStart)); U_PORT_TEST_ASSERT(gCallback >= 0); if (gCallback != 0) { // Sometimes the cloud service (e.g. Here does this on occasion) is