1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 
21 #include "stats/stats.h"
22 #include "mgmt/mgmt.h"
23 #include "sysinit/sysinit.h"
24 #include "stat_mgmt/stat_mgmt.h"
25 #include "stat_mgmt/stat_mgmt_impl.h"
26 #include "stat_mgmt/stat_mgmt_config.h"
27 
28 struct mynewt_stat_mgmt_walk_arg {
29     stat_mgmt_foreach_entry_fn *cb;
30     void *arg;
31 };
32 
33 int
stat_mgmt_impl_get_group(int idx,const char ** out_name)34 stat_mgmt_impl_get_group(int idx, const char **out_name)
35 {
36     const struct stats_hdr *cur;
37     int i;
38     int rc;
39 
40     rc = MGMT_ERR_ENOENT;
41 
42     cur = NULL;
43     i = 0;
44     STAILQ_FOREACH(cur, &g_stats_registry, s_next) {
45         if (i == idx) {
46             rc = 0;
47             break;
48         }
49         i++;
50     }
51 
52     if (!rc) {
53         *out_name = cur->s_name;
54     }
55 
56     return rc;
57 }
58 
59 static int
mynewt_stat_mgmt_walk_cb(struct stats_hdr * hdr,void * arg,char * name,uint16_t off)60 mynewt_stat_mgmt_walk_cb(struct stats_hdr *hdr, void *arg,
61                          char *name, uint16_t off)
62 {
63     struct mynewt_stat_mgmt_walk_arg *walk_arg;
64     struct stat_mgmt_entry entry;
65     void *stat_val;
66 
67     walk_arg = arg;
68 
69     stat_val = (uint8_t *)hdr + off;
70     switch (hdr->s_size) {
71     case sizeof (uint16_t):
72         entry.value = *(uint16_t *) stat_val;
73         break;
74     case sizeof (uint32_t):
75         entry.value = *(uint32_t *) stat_val;
76         break;
77     case sizeof (uint64_t):
78         entry.value = *(uint64_t *) stat_val;
79         break;
80     default:
81         return MGMT_ERR_EUNKNOWN;
82     }
83     entry.name = name;
84 
85     return walk_arg->cb(&entry, walk_arg->arg);
86 }
87 
88 int
stat_mgmt_impl_foreach_entry(const char * group_name,stat_mgmt_foreach_entry_fn * cb,void * arg)89 stat_mgmt_impl_foreach_entry(const char *group_name,
90                              stat_mgmt_foreach_entry_fn *cb,
91                              void *arg)
92 {
93     struct mynewt_stat_mgmt_walk_arg walk_arg;
94     struct stats_hdr *hdr;
95 
96     hdr = stats_group_find(group_name);
97     if (hdr == NULL) {
98         return MGMT_ERR_ENOENT;
99     }
100 
101     walk_arg = (struct mynewt_stat_mgmt_walk_arg) {
102         .cb = cb,
103         .arg = arg,
104     };
105 
106     return stats_walk(hdr, mynewt_stat_mgmt_walk_cb, &walk_arg);
107 }
108 
109 void
stat_mgmt_module_init(void)110 stat_mgmt_module_init(void)
111 {
112     /* Ensure this function only gets called by sysinit. */
113     SYSINIT_ASSERT_ACTIVE();
114 
115     stat_mgmt_register_group();
116 }
117