Skip to content

Commit

Permalink
Merge branch 'context' of https://github.com/alonbl/ubxlib
Browse files Browse the repository at this point in the history
  • Loading branch information
RobMeades committed Oct 14, 2023
2 parents 9ab3569 + 67cf7c4 commit c012cbc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions common/device/api/u_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,24 @@ int32_t uDeviceOpen(const uDeviceCfg_t *pDeviceCfg,
*/
int32_t uDeviceClose(uDeviceHandle_t devHandle, bool powerOff);

/** Attach user context to device.
*
* Note: This is NOT thread-safe and should NOT be called when any
* other uDevice API function might be called. Best call it just
* after calling uDeviceOpen() and before calling anything else.
*
*
* @param devHandle handle to a previously opened device.
* @param pUserContext a user context to set.
*/
void uDeviceSetUserContext(uDeviceHandle_t devHandle, void *pUserContext);

/** Get device attached user context.
*
* @return User context that was set using uDeviceSetUserContext().
*/
void *pUDeviceGetUserContext(uDeviceHandle_t devHandle);

#ifdef __cplusplus
}
#endif
Expand Down
20 changes: 20 additions & 0 deletions common/device/src/u_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,24 @@ int32_t uDeviceClose(uDeviceHandle_t devHandle, bool powerOff)
return errorCode;
}

void uDeviceSetUserContext(uDeviceHandle_t devHandle, void *pUserContext)
{
if (devHandle != NULL) {
U_DEVICE_INSTANCE(devHandle)->pUserContext = pUserContext;
}
}

/** Get device attached user context.
*
* @return User context that was set using @ref uDeviceSetUserContext.
*/
void *pUDeviceGetUserContext(uDeviceHandle_t devHandle)
{
if (devHandle == NULL) {
return NULL;
} else {
return U_DEVICE_INSTANCE(devHandle)->pUserContext;
}
}

// End of file
1 change: 1 addition & 0 deletions common/device/src/u_device_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ typedef struct {
uDeviceType_t deviceType; /**< type of device. */
int32_t moduleType; /**< module identification (when applicable). */
void *pContext; /**< private instance data for the device. */
void *pUserContext; /**< user context attached to device. */
uDeviceNetworkData_t networkData[U_DEVICE_NETWORKS_MAX_NUM]; /**< network cfg and private data. */
// Note: In the future structs of function pointers for socket, MQTT etc.
// implementations may be added here.
Expand Down

0 comments on commit c012cbc

Please sign in to comment.