.yang 檔案
rpc insert-food {
description "Operation to order the oven to put the prepared food inside.";
input {
leaf time {
description "Parameter determining when to perform the operation.";
type enumeration {
enum now {
description "Put the food in the oven immediately.";
}
enum on-oven-ready {
description
"Put the food in once the temperature inside
the oven is at least the configured one. If it
is already, the behaviour is similar to 'now'.";
}
}
}
}
output{
container result {
list user_module {
key "name";
leaf name {
type string;
}
leaf age {
type uint8;
}
}
}
}
}
plugin 實作
1. 回調函數設定
rc = sr_rpc_subscribe_tree(session,"/oven:insert-food",oven_insert_food_tree_cb,NULL,SR_SUBSCR_CTX_REUSE,&subscription);
if (rc != SR_ERR_OK)
{
goto error;
}
2. 回調函數實作
static int oven_insert_food_tree_cb(const char *xpath, const sr_node_t *input, const size_t input_cnt,
sr_node_t **output, size_t *output_cnt, void *private_ctx)
{
sr_node_t *list = NULL, *leaf = NULL;
int rc = SR_ERR_OK;
rc = sr_new_trees(1, &(*output));
if (SR_ERR_OK != rc) {
return rc;
}
rc = sr_node_set_name(&(*output)[0], "result");
if (SR_ERR_OK != rc) {
return rc;
}
(*output)[0].type = SR_CONTAINER_T;
for (size_t i = 0; i < 5; ++i) {
/* - list instance */
rc = sr_node_add_child(&(*output)[0], "user_module", NULL, &list);
if (SR_ERR_OK != rc) {
return rc;
}
list->type = SR_LIST_T;
rc = sr_node_add_child(list, "name", NULL, &leaf);
if (SR_ERR_OK != rc) {
return rc;
}
sr_node_build_str_data(leaf, SR_STRING_T, "%c", 'A'+i);
rc = sr_node_add_child(list, "age", NULL, &leaf);
if (SR_ERR_OK != rc) {
return rc;
}
leaf->type = SR_UINT8_T;
leaf->data.uint8_val = i;
}
*output_cnt = 1;
return rc;
}
NetOpeer2 調用以及傳回
輸入:
user-rpc
<insert-food xmlns="urn:sysrepo:oven">
<time>on-oven-ready</time>
</insert-food>
輸出:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLzcGVOBTSE1UMFpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL5YTM2AzN1YTM1EDNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)