Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/** @file mqtt_os.h
 *
 * @brief MQTT Client depends on certain OS specific functionality. The needed
 *        methods are mapped here and should be implemented based on OS in use.
 *
 * @details Memory management, mutex, logging and wall clock are the needed
 *          functionality for MQTT module. The needed interfaces are defined
 *          in the OS. OS specific port of the interface shall be provided.
 *
 */

#ifndef MQTT_OS_H_
#define MQTT_OS_H_

#include <stddef.h>
#include <kernel.h>
#include <misc/mutex.h>

#include <net/net_core.h>

#include "mqtt_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

/**@brief Method to get trace logs from the module. */
#define MQTT_TRC(...) NET_DBG(__VA_ARGS__)

/**@brief Method to error logs from the module. */
#define MQTT_ERR(...) NET_ERR(__VA_ARGS__)

/**@brief Initialize the mutex for the module, if any.
 *
 * @details This method is called during module initialization @ref mqtt_init.
 */
static inline void mqtt_mutex_init(struct mqtt_client *client)
{
	sys_mutex_init(&client->internal.mutex);
}

/**@brief Acquire lock on the module specific mutex, if any.
 *
 * @details This is assumed to be a blocking method until the acquisition
 *          of the mutex succeeds.
 */
static inline void mqtt_mutex_lock(struct mqtt_client *client)
{
	int ret = sys_mutex_lock(&client->internal.mutex, K_FOREVER);

	__ASSERT(ret == 0, "sys_mutex_lock failed with %d", ret);
	(void)ret;
}

/**@brief Release the lock on the module specific mutex, if any.
 */
static inline void mqtt_mutex_unlock(struct mqtt_client *client)
{
	int ret = sys_mutex_unlock(&client->internal.mutex);

	__ASSERT(ret == 0, "sys_mutex_unlock failed with %d", ret);
	(void)ret;
}

/**@brief Method to get the sys tick or a wall clock in millisecond resolution.
 *
 * @retval Current wall clock or sys tick value in milliseconds.
 */
static inline u32_t mqtt_sys_tick_in_ms_get(void)
{
	return k_uptime_get_32();
}

/**@brief Method to get elapsed time in milliseconds since the last activity.
 *
 * @param[in] last_activity The value since elapsed time is requested.
 *
 * @retval Time elapsed since last_activity time.
 */
static inline u32_t mqtt_elapsed_time_in_ms_get(u32_t last_activity)
{
	s32_t diff = k_uptime_get_32() - last_activity;

	if (diff < 0) {
		return 0;
	}

	return diff;
}

#ifdef __cplusplus
}
#endif

#endif /* MQTT_OS_H_ */