|
@@ -575,29 +575,32 @@ rt_uint8_t rt_object_get_type(rt_object_t object)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief This function will find specified name object from object
|
|
|
+ * @brief This function will iterate through each object from object
|
|
|
* container.
|
|
|
*
|
|
|
- * @param name is the specified name of object.
|
|
|
- *
|
|
|
* @param type is the type of object
|
|
|
+ * @param iter is the iterator
|
|
|
+ * @param data is the specified data passed to iterator
|
|
|
*
|
|
|
- * @return the found object or RT_NULL if there is no this object
|
|
|
- * in object container.
|
|
|
+ * @return RT_EOK on succeed, otherwise the error from `iter`
|
|
|
*
|
|
|
* @note this function shall not be invoked in interrupt status.
|
|
|
*/
|
|
|
-rt_object_t rt_object_find(const char *name, rt_uint8_t type)
|
|
|
+rt_err_t rt_object_for_each(rt_uint8_t type, rt_object_iter_t iter, void *data)
|
|
|
{
|
|
|
struct rt_object *object = RT_NULL;
|
|
|
struct rt_list_node *node = RT_NULL;
|
|
|
struct rt_object_information *information = RT_NULL;
|
|
|
rt_base_t level;
|
|
|
+ rt_err_t error;
|
|
|
|
|
|
information = rt_object_get_information((enum rt_object_class_type)type);
|
|
|
|
|
|
/* parameter check */
|
|
|
- if ((name == RT_NULL) || (information == RT_NULL)) return RT_NULL;
|
|
|
+ if (information == RT_NULL)
|
|
|
+ {
|
|
|
+ return -RT_EINVAL;
|
|
|
+ }
|
|
|
|
|
|
/* which is invoke in interrupt status */
|
|
|
RT_DEBUG_NOT_IN_INTERRUPT;
|
|
@@ -609,16 +612,62 @@ rt_object_t rt_object_find(const char *name, rt_uint8_t type)
|
|
|
rt_list_for_each(node, &(information->object_list))
|
|
|
{
|
|
|
object = rt_list_entry(node, struct rt_object, list);
|
|
|
- if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
|
|
|
+ if ((error = iter(object, data)) != RT_EOK)
|
|
|
{
|
|
|
rt_spin_unlock_irqrestore(&(information->spinlock), level);
|
|
|
|
|
|
- return object;
|
|
|
+ return error >= 0 ? RT_EOK : error;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
rt_spin_unlock_irqrestore(&(information->spinlock), level);
|
|
|
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+static rt_err_t _match_name(struct rt_object *obj, void *data)
|
|
|
+{
|
|
|
+ const char *name = *(const char **)data;
|
|
|
+ if (rt_strncmp(obj->name, name, RT_NAME_MAX) == 0)
|
|
|
+ {
|
|
|
+ *(rt_object_t *)data = obj;
|
|
|
+
|
|
|
+ /* notify an early break of loop, but not on error */
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief This function will find specified name object from object
|
|
|
+ * container.
|
|
|
+ *
|
|
|
+ * @param name is the specified name of object.
|
|
|
+ *
|
|
|
+ * @param type is the type of object
|
|
|
+ *
|
|
|
+ * @return the found object or RT_NULL if there is no this object
|
|
|
+ * in object container.
|
|
|
+ *
|
|
|
+ * @note this function shall not be invoked in interrupt status.
|
|
|
+ */
|
|
|
+rt_object_t rt_object_find(const char *name, rt_uint8_t type)
|
|
|
+{
|
|
|
+ void *data = (void *)name;
|
|
|
+
|
|
|
+ /* parameter check */
|
|
|
+ if (name == RT_NULL) return RT_NULL;
|
|
|
+
|
|
|
+ /* which is invoke in interrupt status */
|
|
|
+ RT_DEBUG_NOT_IN_INTERRUPT;
|
|
|
+
|
|
|
+ rt_object_for_each(type, _match_name, &data);
|
|
|
+ if (data != name)
|
|
|
+ {
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
return RT_NULL;
|
|
|
}
|
|
|
|