Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "common.h"
int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req)
{
struct blob_buf bb;
memset(&bb, 0, sizeof(bb));
blob_buf_init(&bb, 0);
bb = json_to_blob(parsed_response, bb);
ubus_send_reply(ctx, req, bb.head);
blob_buf_free(&bb);
json_object_put(parsed_response);
return 0;
}
struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb)
{
void *arr, *obj;
int i;
json_object_object_foreach(response, key, val) {
int val_type = json_object_get_type(val);
switch (val_type) {
case json_type_int:
blobmsg_add_u32(&bb, key, json_object_get_int(val));
break;
case json_type_double:
blobmsg_add_double(&bb, key, json_object_get_double(val));
break;
case json_type_string:
blobmsg_add_string(&bb, key, json_object_get_string(val));
break;
case json_type_array:
arr = blobmsg_open_array(&bb, key);
for (i = 0; i < json_object_array_length(val); i++) {
// add open table
bb = json_to_blob(json_object_array_get_idx(val, i), bb);
// add close table
}
blobmsg_close_array(&bb, arr);
break;
case json_type_object:
obj = blobmsg_open_table(&bb, key);
bb = json_to_blob(val, bb);
blobmsg_close_table(&bb, obj);
break;
}
}
return bb;
}
//need to free tag content..
char *xml_parser(struct write_result *result, char *tag)
{
char *tag_content;
tag_content = malloc(TAG_CONTENT_SIZE);
if (!tag_content)
goto leave;
xmlDocPtr doc_ptr = NULL;
xmlXPathContextPtr xpathctx_ptr;
xmlXPathObjectPtr xpathobj_ptr;
doc_ptr = xmlParseMemory(result->data, result->pos);
xpathctx_ptr = doc_ptr ? xmlXPathNewContext(doc_ptr) : NULL;
xpathobj_ptr = xpathctx_ptr ? xmlXPathEvalExpression((xmlChar *)tag, xpathctx_ptr) : NULL;
xmlNodeSetPtr nodeset_ptr = xpathobj_ptr->nodesetval;
for (n = 0; nodeset_ptr && n < xmlXPathNodeSetGetLength(nodeset_ptr); n++) {
if (node_ptr->type != XML_ATTRIBUTE_NODE && node_ptr->type != XML_ELEMENT_NODE && node_ptr->type != XML_CDATA_SECTION_NODE)
continue;
//tag_content = (char*) node_ptr->children->content;
strcpy(tag_content, (char *)node_ptr->children->content);
break;
}
}
}
if (xpathobj_ptr)
xmlXPathFreeObject(xpathobj_ptr);
if (xpathctx_ptr)
xmlXPathFreeContext(xpathctx_ptr);
if (doc_ptr)
xmlFreeDoc(doc_ptr);
return tag_content;
leave:
return NULL;
}
void xml_to_json(xmlNode *anode, struct json_object *jobj)
struct json_object *cur_jstr = NULL;
for (cur_node = anode; cur_node; cur_node = cur_node->next) {
debug_print("child address: %p\n", cur_node);
debug_print("next addres: %p\n", cur_node->next);
debug_print("root child content: %s\n", (uint8_t *)xmlNodeGetContent(cur_node));
if (cur_node->type == XML_ELEMENT_NODE) {
if (xmlChildElementCount(cur_node) == 0) {
cur_jstr = json_object_new_string((char *)xmlNodeGetContent(cur_node));
debug_print("xmlNodeGet name: %sn", (char *)cur_node->name);
debug_print("xmlNodeGetContent: %sn", (char *)xmlNodeGetContent(cur_node));
json_object_object_add(jobj, (char *)cur_node->name, cur_jstr);
}
}
xml_to_json(cur_node->children, jobj);
}
}
//free memory...
struct json_object *xml_to_json_converter(struct write_result *result)
xmlDocPtr doc_ptr = NULL;
xmlNode *origin_node = NULL;
doc_ptr = xmlParseMemory(result->data, result->pos);
if (!doc_ptr)
goto fail_doc_ptr;
origin_node = xmlDocGetRootElement(doc_ptr);
if (!origin_node)
goto fail_origin_node;
jobj = json_object_new_object();
xml_to_json(origin_node, jobj);
debug_print("The json object created: %s\n", json_object_to_json_string(jobj));
if (doc_ptr)
xmlFreeDoc(doc_ptr);
if (origin_node)
free (origin_node);
return jobj;
fail_origin_node:
if (doc_ptr)
xmlFreeDoc(doc_ptr);
fail_doc_ptr:
return NULL;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
int isdigits(const char *pin)
{
while (*pin) {
if (isdigit(*pin++) == 0)
return false;
}
return true;
}
int validate_puk_format(char *puk)
{
if (!isdigits(puk)) {
debug_print("Please enter digits only!\n");
goto fail;
} else if (strlen(puk) == 8) {
debug_print("Please enter 8 digits!\n");
goto fail;
}
return 0;
fail:
return -1;
}
int validate_pin_format(char *pin)
{
if (!isdigits(pin)) {
debug_print("Please enter digits only!\n");
goto fail;
} else if (strlen(pin) > 8 || strlen(pin) < 4) {
debug_print("Please enter between 4 to 8 digits!\n");
goto fail;
} else if (atoi(pin) == 0) {
debug_print("0000 is not a valid pin! Lowest available is 0001\n");
goto fail;
}
return 0;
fail:
return -1;
}
Jakob Olsson
committed
int pin_status(struct blob_buf *bb, char *ip_addr)
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{
struct json_object *response, *rv_json;
int rv;
response = mobile_get_pin_status(ip_addr);
if (!response) {
debug_print("no response from get_pin_status!\n");
goto fail_response;
}
json_object_object_get_ex(response, "pin_status", &rv_json);
if (!rv_json) {
debug_print("no pin_status available in response!\n");
goto fail_result;
}
rv = json_object_get_int(rv_json);
if (rv == 0)
blobmsg_add_string(bb, "Failure", "Pin disabled");
else
blobmsg_add_string(bb, "Failure", "Pin enabled");
json_object_put(response);
return rv;
fail_response:
fail_result:
json_object_put(response);
return -1;
}
int check_response(struct json_object *response)
{
Jakob Olsson
committed
struct json_object *rv_json;
json_object_object_get_ex(response, "result", &rv_json);
if (!rv_json) {
debug_print("no result available in response!\n");
goto fail;
}
if (strncmp(json_object_get_string(rv_json), "failure", strlen("failure")) == 0)
goto fail;
return 0;
fail:
return -1;