Amxb_Ubus  3.3.1
Ambiorix Ubus API
amxb_ubus_blob.c File Reference
#include <stdlib.h>
#include "amxb_ubus.h"

Go to the source code of this file.

Data Structures

union  _ubus_double
 

Typedefs

typedef union _ubus_double ubus_double_t
 

Functions

static void amxb_ubus_set_var (amxc_var_t *var, struct blob_attr *attr, void *data, int len)
 
int PRIVATE amxb_ubus_parse_blob (amxc_var_t *var, struct blob_attr *attr, bool table)
 
int PRIVATE amxb_ubus_parse_blob_table (amxc_var_t *var, struct blob_attr *attr, int len)
 
int PRIVATE amxb_ubus_parse_blob_array (amxc_var_t *var, struct blob_attr *attr, int len)
 
int PRIVATE amxb_ubus_format_blob (amxc_var_t *data, const char *key, struct blob_buf *b)
 
int PRIVATE amxb_ubus_format_blob_table (const amxc_htable_t *table, struct blob_buf *b)
 
int PRIVATE amxb_ubus_format_blob_array (const amxc_llist_t *list, struct blob_buf *b)
 

Typedef Documentation

◆ ubus_double_t

typedef union _ubus_double ubus_double_t

Function Documentation

◆ amxb_ubus_format_blob()

int PRIVATE amxb_ubus_format_blob ( amxc_var_t *  data,
const char *  key,
struct blob_buf *  b 
)

Definition at line 166 of file amxb_ubus_blob.c.

168  {
169  void* c;
170 
171  switch(amxc_var_type_of(data)) {
172  case AMXC_VAR_ID_BOOL: {
173  blobmsg_add_u8(b, key, (uint8_t) amxc_var_constcast(bool, data));
174  }
175  break;
176 
177  case AMXC_VAR_ID_INT8: {
178  blobmsg_add_u8(b, key, (uint8_t) amxc_var_constcast(int8_t, data));
179  }
180  break;
181  case AMXC_VAR_ID_UINT8: {
182  if(amxc_var_constcast(uint8_t, data) > INT8_MAX) {
183  blobmsg_add_u16(b, key, (uint16_t) amxc_var_constcast(uint8_t, data));
184  } else {
185  blobmsg_add_u8(b, key, (uint8_t) amxc_var_constcast(uint8_t, data));
186  }
187  }
188  break;
189  case AMXC_VAR_ID_INT16: {
190  blobmsg_add_u16(b, key, (uint16_t) amxc_var_constcast(int16_t, data));
191  }
192  break;
193  case AMXC_VAR_ID_UINT16: {
194  if(amxc_var_constcast(uint16_t, data) > INT16_MAX) {
195  blobmsg_add_u32(b, key, (uint32_t) amxc_var_constcast(uint16_t, data));
196  } else {
197  blobmsg_add_u16(b, key, (uint16_t) amxc_var_constcast(uint16_t, data));
198  }
199  }
200  break;
201  case AMXC_VAR_ID_INT32: {
202  blobmsg_add_u32(b, key, (uint32_t) amxc_var_constcast(int32_t, data));
203  }
204  break;
205  case AMXC_VAR_ID_UINT32: {
206  if(amxc_var_constcast(uint32_t, data) > INT32_MAX) {
207  blobmsg_add_u64(b, key, (uint64_t) amxc_var_constcast(uint32_t, data));
208  } else {
209  blobmsg_add_u32(b, key, (uint32_t) amxc_var_constcast(uint32_t, data));
210  }
211  }
212  break;
213  case AMXC_VAR_ID_INT64: {
214  blobmsg_add_u64(b, key, (uint64_t) amxc_var_constcast(int64_t, data));
215  }
216  break;
217  case AMXC_VAR_ID_UINT64: {
218  blobmsg_add_u64(b, key, (uint64_t) amxc_var_constcast(uint64_t, data));
219  }
220  break;
221 
222  case AMXC_VAR_ID_HTABLE: {
223  const amxc_htable_t* table = amxc_var_constcast(amxc_htable_t, data);
224  c = blobmsg_open_table(b, key);
225  amxb_ubus_format_blob_table(table, b);
226  blobmsg_close_table(b, c);
227  }
228  break;
229 
230  case AMXC_VAR_ID_LIST: {
231  const amxc_llist_t* list = amxc_var_constcast(amxc_llist_t, data);
232  c = blobmsg_open_array(b, key);
234  blobmsg_close_array(b, c);
235  }
236  break;
237 
238  default: {
239  char* string = amxc_var_dyncast(cstring_t, data);
240  blobmsg_add_string(b, key, string == NULL ? "" : string);
241  free(string);
242  }
243  break;
244  }
245 
246  return 0;
247 }
int PRIVATE amxb_ubus_format_blob_array(const amxc_llist_t *list, struct blob_buf *b)
int PRIVATE amxb_ubus_format_blob_table(const amxc_htable_t *table, struct blob_buf *b)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ amxb_ubus_format_blob_array()

int PRIVATE amxb_ubus_format_blob_array ( const amxc_llist_t *  list,
struct blob_buf *  b 
)

Definition at line 265 of file amxb_ubus_blob.c.

266  {
267  int retval = -1;
268 
269  amxc_llist_for_each(it, list) {
270  amxc_var_t* data = amxc_var_from_llist_it(it);
271  when_failed(amxb_ubus_format_blob(data, NULL, b), exit);
272  }
273 
274  retval = 0;
275 
276 exit:
277  return retval;
278 }
int PRIVATE amxb_ubus_format_blob(amxc_var_t *data, const char *key, struct blob_buf *b)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ amxb_ubus_format_blob_table()

int PRIVATE amxb_ubus_format_blob_table ( const amxc_htable_t *  table,
struct blob_buf *  b 
)

Definition at line 249 of file amxb_ubus_blob.c.

250  {
251  int retval = -1;
252 
253  amxc_htable_for_each(it, table) {
254  const char* key = amxc_htable_it_get_key(it);
255  amxc_var_t* data = amxc_var_from_htable_it(it);
256  when_failed(amxb_ubus_format_blob(data, key, b), exit);
257  }
258 
259  retval = 0;
260 
261 exit:
262  return retval;
263 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ amxb_ubus_parse_blob()

int PRIVATE amxb_ubus_parse_blob ( amxc_var_t *  var,
struct blob_attr *  attr,
bool  table 
)

Definition at line 111 of file amxb_ubus_blob.c.

113  {
114  int len;
115  int off = 0;
116  void* data;
117 
118  if(!blobmsg_check_attr(attr, false)) {
119  return 0;
120  }
121 
122  if(table) {
123  if(blobmsg_name(attr)[0] != 0) {
124  var = amxc_var_add_new_key(var, blobmsg_name(attr));
125  off++;
126  } else {
127  return 0;
128  }
129  }
130 
131  data = blobmsg_data(attr);
132  len = blobmsg_data_len(attr);
133  amxb_ubus_set_var(var, attr, data, len);
134 
135  return off + 1;
136 }
static void amxb_ubus_set_var(amxc_var_t *var, struct blob_attr *attr, void *data, int len)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ amxb_ubus_parse_blob_array()

int PRIVATE amxb_ubus_parse_blob_array ( amxc_var_t *  var,
struct blob_attr *  attr,
int  len 
)

Definition at line 152 of file amxb_ubus_blob.c.

154  {
155  int rem = len;
156  struct blob_attr* pos;
157 
158  __blob_for_each_attr(pos, attr, rem) {
159  amxc_var_t* item = amxc_var_add_new(var);
160  amxb_ubus_parse_blob(item, pos, false);
161  }
162 
163  return 1;
164 }
int PRIVATE amxb_ubus_parse_blob(amxc_var_t *var, struct blob_attr *attr, bool table)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ amxb_ubus_parse_blob_table()

int PRIVATE amxb_ubus_parse_blob_table ( amxc_var_t *  var,
struct blob_attr *  attr,
int  len 
)

Definition at line 138 of file amxb_ubus_blob.c.

140  {
141  int rem = len;
142  struct blob_attr* pos;
143 
144  __blob_for_each_attr(pos, attr, rem) {
145  amxb_ubus_parse_blob(var, pos, true);
146  }
147 
148  return 1;
149 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ amxb_ubus_set_var()

static void amxb_ubus_set_var ( amxc_var_t *  var,
struct blob_attr *  attr,
void *  data,
int  len 
)
static

Definition at line 64 of file amxb_ubus_blob.c.

67  {
68  switch(blob_id(attr)) {
69  case BLOBMSG_TYPE_INT8:
70  amxc_var_set(int8_t, var, *(int8_t*) data);
71  break;
72 
73  case BLOBMSG_TYPE_INT16:
74  amxc_var_set(int16_t, var, (int16_t) be16_to_cpu(*(uint16_t*) data));
75  break;
76 
77  case BLOBMSG_TYPE_INT32:
78  amxc_var_set(int32_t, var, (int32_t) be32_to_cpu(*(uint32_t*) data));
79  break;
80 
81  case BLOBMSG_TYPE_INT64:
82  amxc_var_set(int64_t, var, (int64_t) be64_to_cpu(*(uint64_t*) data));
83  break;
84 
85  case BLOBMSG_TYPE_DOUBLE: {
86  ubus_double_t v;
87  v.u64 = be64_to_cpu(*(uint64_t*) data);
88  amxc_var_set(double, var, v.d);
89  }
90  break;
91 
92  case BLOBMSG_TYPE_STRING:
93  amxc_var_set(cstring_t, var, (const char*) data);
94  break;
95 
96  case BLOBMSG_TYPE_ARRAY:
97  amxc_var_set_type(var, AMXC_VAR_ID_LIST);
98  amxb_ubus_parse_blob_array(var, (struct blob_attr*) data, len);
99  break;
100 
101  case BLOBMSG_TYPE_TABLE:
102  amxc_var_set_type(var, AMXC_VAR_ID_HTABLE);
103  amxb_ubus_parse_blob_table(var, (struct blob_attr*) data, len);
104  break;
105 
106  default:
107  break;
108  }
109 }
int PRIVATE amxb_ubus_parse_blob_array(amxc_var_t *var, struct blob_attr *attr, int len)
int PRIVATE amxb_ubus_parse_blob_table(amxc_var_t *var, struct blob_attr *attr, int len)
uint64_t u64
Here is the call graph for this function:
Here is the caller graph for this function: