Skip to content

Commit

Permalink
New network/device API, next phase of introduction. (#554)
Browse files Browse the repository at this point in the history
New network/device API, next phase of introduction.

Lots of changes in this commit unfortunately.  The meat of it is:

- what was u_device_internal.c/u_device_internal.h is now:
  - src/u_device.c: contains the implementation of the public device API functions,
  - src/u_device_shared.c/.h: contains the functions that the device API shares with
    other ubxlib modules but are NOT part of the public device API,
  - src/u_device_private_xxx.c/.h: contains the device-specific parts of the device
    open/close calls for each xxx device type, not shared with anyone/anything,
  - src/u_device_shared_xxx.h: defines a structure, for each device type, of the data
    which is known to the device API but needs to be shared with the network
    API; this data is hooked into the pContext pointer of the device instance
    so that network can get at it,
  - src/u_device_private_xxx_stub.c: stubs to permit compilation without a given
    device type; note that this doesn't work _FULLY_ (see
    u-blox#36) but until we have actually
    implemented the new "stubbing" mechanism we should maintain it, since
    a customer is known to be using it,
- made naming consistent (only one spelling of "Cfg", variables that are
  of thingType_t have "Type" on the end of the variable name), intended
  to make it easier for a user to know what to write for a variable
  name or a type without having to think about it,
- as suggested, moved to the new network structure names for the
  new code, so uDeviceNetworkCfgxxx_t -> uNetworkCfgxxx_t.
- implemented the new bits of the device API for all device types,
- moved the initialisation stuff from being in the uNetwork API to the
  uDevice API, i.e. uNetworkInit()/Deinit() -> uDeviceInit()/Deinit(),
- implemented the remaining new bits of the network API,
- implemented the new dynamic behaviours, specifically:
  - taking down a network no longer performs the power off, that is done
    only by closing the device,
  - deinitialising the device/network API does NOT do a clean-up
    (need to decide whether we think this is a good idea or not 'cos
    it could be made to do so),
- "up" function parameter renamed to "upNotDown"; a bit more to grab
  hold of for my eyes, I couldn't find it easily when reading the code,
- all the old APIs still work.

But I'm afraid I've not tested _any_ of the new stuff yet :-(.
  • Loading branch information
RobMeades authored May 17, 2022
1 parent b69d111 commit 3c84122
Show file tree
Hide file tree
Showing 54 changed files with 2,258 additions and 679 deletions.
2 changes: 1 addition & 1 deletion ble/src/u_ble_cfg_intmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

#include "u_error_common.h"

#include "u_device_internal.h"
#include "u_device_shared.h"

#include "u_cfg_sw.h"
#include "u_port_gatt.h"
Expand Down
2 changes: 1 addition & 1 deletion ble/src/u_ble_sps_intmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

#include "u_error_common.h"

#include "u_device_internal.h"
#include "u_device_shared.h"

#include "u_cfg_sw.h"
#include "u_port.h"
Expand Down
2 changes: 1 addition & 1 deletion ble/test/u_ble_test_private.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

#include "u_error_common.h"

#include "u_device_internal.h"
#include "u_device_shared.h"

#include "u_port.h"
#include "u_port_debug.h"
Expand Down
2 changes: 1 addition & 1 deletion cell/src/u_cell.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "u_port_gpio.h"

#include "u_at_client.h"
#include "u_device_internal.h"
#include "u_device_shared.h"

#include "u_cell_module_type.h"
#include "u_cell.h" // Order is
Expand Down
70 changes: 41 additions & 29 deletions common/device/api/u_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @brief This is a high-level API for initializing a u-blox device
* (chip or module). These functions are generally used in conjunction
* with those in the network API, see u_network.h for further information.
* These functions are thread-safe.
*/

#ifdef __cplusplus
Expand Down Expand Up @@ -117,7 +118,7 @@ typedef struct {
against it might and with the clause"; if this
field is populated then the version field of
this structure must be set to 1 or higher". */
} uDeviceConfigUart_t;
} uDeviceCfgUart_t;

/** I2C transport configuration.
*/
Expand All @@ -137,7 +138,7 @@ typedef struct {
against it might end with the clause "; if this
field is populated then the version field of
this structure must be set to 1 or higher". */
} uDeviceConfigI2c_t;
} uDeviceCfgI2c_t;

/** Cellular device configuration.
*/
Expand All @@ -156,6 +157,7 @@ typedef struct {
int32_t pinVInt; /**< The input pin that is connected to the
VINT pin of the cellular module; use -1
if there is no such connection. */
const char *pPin; /**< The PIN of the SIM. */
/* This is the end of version 0 of this structure:
should any fields be added to this structure in
future they must be added AFTER this point and
Expand All @@ -166,7 +168,7 @@ typedef struct {
against it might end with the clause "; if this
field is populated then the version field
of this structure must be set to 1 or higher". */
} uDeviceConfigCell_t;
} uDeviceCfgCell_t;

/** GNSS device configuration.
*/
Expand Down Expand Up @@ -218,15 +220,15 @@ typedef struct {
against it might end with the clause "; if this
field is populated then the version field of
this structure must be set to 1 or higher". */
} uDeviceConfigGnss_t;
} uDeviceCfgGnss_t;

/** Short-range device configuration.
*/
typedef struct {
uDeviceVersion_t version; /**< Version of this structure; allow your
compiler to initialise this to zero
unless otherwise specified below. */
int32_t module; /**< The module type that is connected,
int32_t moduleType; /**< The module type that is connected,
see uShortRangeModuleType_t in
u_short_range_module_type.h. */
/* This is the end of version 0 of this structure:
Expand All @@ -239,7 +241,7 @@ typedef struct {
against it might end with the clause "; if this
field is populated then the version field of
this structure must be set to 1 or higher". */
} uDeviceConfigShortRange_t;
} uDeviceCfgShortRange_t;

/** The complete device configuration.
*/
Expand All @@ -249,14 +251,14 @@ typedef struct {
unless otherwise specified below. */
uDeviceType_t deviceType;
union {
uDeviceConfigCell_t cellCfg;
uDeviceConfigGnss_t gnssCfg;
uDeviceConfigShortRange_t shoCfg;
uDeviceCfgCell_t cfgCell;
uDeviceCfgGnss_t cfgGnss;
uDeviceCfgShortRange_t cfgSho;
} deviceCfg;
uDeviceTransportType_t transport;
uDeviceTransportType_t transportType;
union {
uDeviceConfigUart_t uartCfg;
uDeviceConfigI2c_t i2cCfg;
uDeviceCfgUart_t cfgUart;
uDeviceCfgI2c_t cfgI2c;
} transportCfg;
/* This is the end of version 0 of this structure:
should any fields be added to this structure in
Expand All @@ -268,34 +270,44 @@ typedef struct {
against it might end with the clause "; if this
field is populated then the version field of
this structure must be set to 1 or higher". */
} uDeviceConfig_t;
} uDeviceCfg_t;

/* ----------------------------------------------------------------
* FUNCTIONS
* -------------------------------------------------------------- */

/** Initialise the device API. If the device API has already
* been initialised this function returns success without doing
* anything.
*
* @return zero on success else negative error code.
*/
int32_t uDeviceInit();

/** Deinitialise the device API. Note that this performs no
* clean-up, it is up to the caller to ensure that all devices
* have been closed with a call to uDeviceClose().
*
* @return zero on success else negative error code.
*/
int32_t uDeviceDeinit();

/** Open a device instance.
*
* @param pDevCfg Device configuration
* @param[out] pUDeviceHandle A new device handle.
* @return 0 on success else a negative error code.
* @param pDeviceCfg device configuration, cannot be NULL.
* @param[out] pDeviceHandle a place to put the device handle;
* cannot be NULL.
* @return zero on success else a negative error code.
*/
int32_t uDeviceOpen(const uDeviceConfig_t *pDevCfg, uDeviceHandle_t *pUDeviceHandle);
int32_t uDeviceOpen(const uDeviceCfg_t *pDeviceCfg,
uDeviceHandle_t *pDeviceHandle);

/** Close an open device instance.
* TODO: QUESTION - what happens to the actual device? Is it affected
* by this operation? Previously the device was deliberately left
* unaffected, allowing the user to leave, for instance, a cellular module
* up and registered with the network so that they didn't have the time/
* power waste of doing all that on a subsequent device/network "up".
* I believe that is still the case; if so we should say that here,
* as I think it is important we are consistent about this across
* all device types.
/** Close an open device instance, powering it off.
*
* @param pUDeviceHandle Handle to a previously opened device.
* @return 0 on success else a negative error code.
* @param devHandle handle to a previously opened device.
* @return zero on success else a negative error code.
*/
int32_t uDeviceClose(uDeviceHandle_t pUDeviceHandle);
int32_t uDeviceClose(uDeviceHandle_t devHandle);

#ifdef __cplusplus
}
Expand Down
176 changes: 176 additions & 0 deletions common/device/src/u_device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright 2022 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 Functions for initializing a u-blox device (chip or module).
*/

#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif

#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h" //

#include "u_error_common.h"

#include "u_device.h"
#include "u_device_shared.h"
#include "u_device_private_cell.h"
#include "u_device_private_gnss.h"
#include "u_device_private_short_range.h"

/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */

/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */

/* ----------------------------------------------------------------
* STATIC VARIABLES
* -------------------------------------------------------------- */

/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */

/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */

int32_t uDeviceInit()
{
int32_t errorCode = uDeviceMutexCreate();

if (errorCode == 0) {
errorCode = uDevicePrivateCellInit();
if (errorCode == (int32_t) U_ERROR_COMMON_NOT_IMPLEMENTED) {
errorCode = 0;
}
}
if (errorCode == 0) {
errorCode = uDevicePrivateGnssInit();
if (errorCode == (int32_t) U_ERROR_COMMON_NOT_IMPLEMENTED) {
errorCode = 0;
}
}
if (errorCode == 0) {
errorCode = uDevicePrivateShortRangeInit();
if (errorCode == (int32_t) U_ERROR_COMMON_NOT_IMPLEMENTED) {
errorCode = 0;
}
}

// Clean up on error
if (errorCode != 0) {
uDevicePrivateShortRangeDeinit();
uDevicePrivateCellDeinit();
uDevicePrivateGnssDeinit();
uDeviceMutexDestroy();
}

return errorCode;
}

int32_t uDeviceDeinit()
{
uDevicePrivateShortRangeDeinit();
uDevicePrivateGnssDeinit();
uDevicePrivateCellDeinit();
uDeviceMutexDestroy();
return (int32_t) U_ERROR_COMMON_SUCCESS;
}

int32_t uDeviceOpen(const uDeviceCfg_t *pDeviceCfg, uDeviceHandle_t *pDeviceHandle)
{
// Lock the API
int32_t errorCode = uDeviceLock();

if (errorCode == 0) {
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
if ((pDeviceCfg != NULL) && (pDeviceCfg->version == 0) && (pDeviceHandle != NULL)) {
switch (pDeviceCfg->deviceType) {
case U_DEVICE_TYPE_CELL:
errorCode = uDevicePrivateCellAdd(pDeviceCfg, pDeviceHandle);
if (errorCode == 0) {
U_DEVICE_INSTANCE(*pDeviceHandle)->moduleType = pDeviceCfg->deviceCfg.cfgCell.moduleType;
}
break;
case U_DEVICE_TYPE_GNSS:
errorCode = uDevicePrivateGnssAdd(pDeviceCfg, pDeviceHandle);
if (errorCode == 0) {
U_DEVICE_INSTANCE(*pDeviceHandle)->moduleType = pDeviceCfg->deviceCfg.cfgGnss.moduleType;
}
break;
case U_DEVICE_TYPE_SHORT_RANGE:
errorCode = uDevicePrivateShortRangeAdd(pDeviceCfg, pDeviceHandle);
if (errorCode == 0) {
U_DEVICE_INSTANCE(*pDeviceHandle)->moduleType = pDeviceCfg->deviceCfg.cfgSho.moduleType;
}
break;
case U_DEVICE_TYPE_SHORT_RANGE_OPEN_CPU:
errorCode = uDevicePrivateShortRangeOpenCpuAdd(pDeviceCfg, pDeviceHandle);
if (errorCode == 0) {
U_DEVICE_INSTANCE(*pDeviceHandle)->moduleType = pDeviceCfg->deviceCfg.cfgSho.moduleType;
}
break;
default:
break;
}
}

// ...and done
uDeviceUnlock();
}

return errorCode;
}

int32_t uDeviceClose(uDeviceHandle_t devHandle)
{
// Lock the API
int32_t errorCode = uDeviceLock();

if (errorCode == 0) {
errorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
switch (uDeviceGetDeviceType(devHandle)) {
case U_DEVICE_TYPE_CELL:
errorCode = uDevicePrivateCellRemove(devHandle);
break;
case U_DEVICE_TYPE_GNSS:
errorCode = uDevicePrivateGnssRemove(devHandle);
break;
case U_DEVICE_TYPE_SHORT_RANGE:
errorCode = uDevicePrivateShortRangeRemove(devHandle);
break;
case U_DEVICE_TYPE_SHORT_RANGE_OPEN_CPU:
errorCode = uDevicePrivateShortRangeOpenCpuRemove(devHandle);
break;
default:
break;
}

// ...and done
uDeviceUnlock();
}

return errorCode;
}

// End of file
Loading

0 comments on commit 3c84122

Please sign in to comment.