/** \brief Find a sensor hardware descriptor
 *
 * This routine returns the address of a sensor hardware descriptor, based on
 * the sensor type specified in the \c type parameter.  The first hardware
 * entry with the correct type will be used.
 *
 * This utility is currently used to abstract the details of the device list
 * implementation from the rest of the API.  However, it will not be retained in
 * future versions of the API.  As a result applications and library code should
 * not use this routine.  If list iteration is required in API clients, use the
 * sensor_device_enum() function.
 *
 * \param   type   The type of sensor to find in hardware descriptor list
 *
 * \return  The address of the sensor hardware descriptor, or NULL if not found
 */
sensor_hal_t *sensor_find(sensor_type_t type)
{
	size_t dev_count;
	sensor_hal_t *const dev_list = sensor_list(&dev_count);

	/* Find the specified sensor type in the device list. */

	for (int index = 0; index < dev_count; ++index) {
		/* Test device against input type(s) bitmask. */

		if ((dev_list[index].dev_type & type) == type) {
			return &dev_list [index];
		}
	}

	return 0;
}
/**
 * @brief Enumerate sensor devices.
 *
 * This routine is a sensor device enumeration function that will call a
 * user-defined callback \c func for each sensor of a specified \c type
 * configured into the system.  The user-callback function will be passed the
 * address of a \a sensor_hal_t \c HAL descriptor corresponding to the sensor
 * type along with a user-specified address \c arg storing any additional
 * function arguments.
 *
 * If the specified sensor \c type is \c SENSOR_TYPE_UNKNOWN, then the user
 * \c func will be called for every sensor device in the system.
 *
 * Once called, the user-callback function must return a boolean \a true
 * value to continue iterating the list, else \a false to cease device list
 * iteration.  This routine returns when there are not more sensors of the
 * requested \c type in the device list, or when the user function returns
 * zero (false).
 *
 * Note that this function will not be included in the API by default.  Build
 * a sensor system with \c INCLUDE_SENSOR_ENUM defined to include this routine.
 *
 * @param   type    Specifies the sensor type to enumerate.
 * @param   func    Specifies the address of a user callback function.
 * @param   arg     Specifies the address of a callback function argument.
 * @return  bool    true if the call succeeds, else false is returned.
 */
bool sensor_device_enum
	(sensor_type_t type, sensor_enum_callback func, void *arg)
{
#ifdef INCLUDE_SENSOR_ENUM
	size_t count;
	const sensor_hal_t *hal = sensor_list(&count);

	if (func != 0) {
		for (int n = 0; n < count; ++n, ++hal) {
			if (((type == SENSOR_TYPE_UNKNOWN) ||
					(type == hal->dev_type)) &&
					(false == (func)(hal, arg))) {
				return true;
			}
		}
	}
#endif /* INCLUDE_SENSOR_ENUM */

	return false;
}