b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010-2012 Felix Fietkau <nbd@openwrt.org> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | #include <libubox/blobmsg.h> |
| 17 | #include <mxml.h> |
| 18 | #include "blobmsg_xml.h" |
| 19 | |
| 20 | |
| 21 | #define XML_ELEMENT_LIST_ITEM "Item" |
| 22 | #define XML_LIST_ATTR_INDEX "index" |
| 23 | |
| 24 | void blobmsg_format_xml(struct blob_attr *attr, int len, bool array) |
| 25 | { |
| 26 | struct blob_attr *pos; |
| 27 | int rem = len; |
| 28 | |
| 29 | mxml_node_t *xml; |
| 30 | mxml_node_t *rgw; |
| 31 | mxml_node_t *node; |
| 32 | |
| 33 | xml = mxmlNewXML("1.0"); |
| 34 | rgw = mxmlNewElement(xml,"RGW"); |
| 35 | node = rgw; |
| 36 | |
| 37 | __blob_for_each_attr(pos, attr, rem) |
| 38 | { |
| 39 | blobmsg_format_xml_element(&node, pos, array); |
| 40 | } |
| 41 | } |
| 42 | void blobmsg_format_xml_list_item(mxml_node_t *node, struct blob_attr *attr, int len, bool array) |
| 43 | { |
| 44 | int field; |
| 45 | mxml_node_t *cur; |
| 46 | struct blob_attr *pos; |
| 47 | int rem = len; |
| 48 | __blob_for_each_attr(pos, attr, rem) |
| 49 | { |
| 50 | //For every table item |
| 51 | blobmsg_format_xml_element(&node, pos, array); |
| 52 | //cur=mxmlNewElement(node, blobmsg_name(attr)); |
| 53 | //mxmlAdd(node, MXML_ADD_AFTER, NULL, cur); |
| 54 | //mxmlNewOpaque(cur, blobmsg_data(attr)); |
| 55 | } |
| 56 | return; |
| 57 | |
| 58 | } |
| 59 | void blobmsg_format_xml_list(mxml_node_t *node, struct blob_attr *attr, int len, bool array) |
| 60 | { |
| 61 | mxml_node_t *cur; |
| 62 | int index=0; |
| 63 | struct blob_attr *pos; |
| 64 | int rem = len; |
| 65 | char index_str[10] = {'\0'}; |
| 66 | int size; |
| 67 | __blob_for_each_attr(pos, attr, rem) |
| 68 | { |
| 69 | // For every table in parent array |
| 70 | //New param node <Item></Item> |
| 71 | cur=mxmlNewElement(node, XML_ELEMENT_LIST_ITEM); |
| 72 | mxmlAdd(node, MXML_ADD_AFTER, NULL, cur); |
| 73 | snprintf(index_str, 10, "%d", ++index); |
| 74 | mxmlElementSetAttr(cur, XML_LIST_ATTR_INDEX, index_str); |
| 75 | //skip blobmsg_name(pos) |
| 76 | //pass table items for further process |
| 77 | //blobmsg_format_xml_list_item(cur,blobmsg_data(pos),blobmsg_data_len(pos),false); |
| 78 | size = blob_pad_len(pos); |
| 79 | blobmsg_format_xml_list_item(cur,pos, blob_pad_len(pos),false); |
| 80 | } |
| 81 | return; |
| 82 | } |
| 83 | void blobmsg_format_xml_element( mxml_node_t **node, struct blob_attr *attr, bool array) |
| 84 | { |
| 85 | #define DEBUG_ON_OFF 1 |
| 86 | const char *data_str; |
| 87 | char buf[32]; |
| 88 | void *data; |
| 89 | int len; |
| 90 | int str_size; |
| 91 | mxml_node_t *cur; |
| 92 | |
| 93 | #if DEBUG_ON_OFF |
| 94 | FILE *fp; |
| 95 | fp = fopen("/tmp/blob_c_xml.log", "w+"); |
| 96 | if(!fp) |
| 97 | { |
| 98 | return; |
| 99 | } |
| 100 | #endif |
| 101 | if (!blobmsg_check_attr(attr, false)) |
| 102 | return; |
| 103 | |
| 104 | if (blobmsg_name(attr)[0]) { |
| 105 | if(!strcmp(blobmsg_name(attr), "ril_id")) |
| 106 | { |
| 107 | //for RIL response, no need RIL |
| 108 | return; |
| 109 | } |
| 110 | if(!strcmp(blobmsg_name(attr), "module_name")) |
| 111 | { |
| 112 | cur = mxmlNewElement(*node, blobmsg_data(attr)); |
| 113 | *node = cur; |
| 114 | return; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | fprintf(fp,"tag name %s\n",blobmsg_name(attr)); |
| 119 | cur = mxmlNewElement(*node, blobmsg_name(attr)); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | data = blobmsg_data(attr); |
| 124 | len = blobmsg_data_len(attr); |
| 125 | |
| 126 | data_str = buf; |
| 127 | switch(blob_id(attr)) { |
| 128 | case BLOBMSG_TYPE_UNSPEC: |
| 129 | sprintf(buf, "null"); |
| 130 | break; |
| 131 | case BLOBMSG_TYPE_BOOL: |
| 132 | |
| 133 | sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false"); |
| 134 | fprintf(fp,"bool %s\n",buf); |
| 135 | break; |
| 136 | case BLOBMSG_TYPE_INT16: |
| 137 | sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data)); |
| 138 | fprintf(fp,"INT16 %s\n",buf); |
| 139 | break; |
| 140 | case BLOBMSG_TYPE_INT32: |
| 141 | sprintf(buf, "%d", (int32_t) be32_to_cpu(*(uint32_t *)data)); |
| 142 | fprintf(fp,"INT32 %s\n",buf); |
| 143 | break; |
| 144 | case BLOBMSG_TYPE_INT64: |
| 145 | sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data)); |
| 146 | fprintf(fp,"INT64 %s\n",buf); |
| 147 | break; |
| 148 | case BLOBMSG_TYPE_STRING: |
| 149 | data_str = data; |
| 150 | fprintf(fp,"STRING %s\n",data_str); |
| 151 | break; |
| 152 | case BLOBMSG_TYPE_ARRAY: |
| 153 | fprintf(fp,"ARRAY %p %p %u\n",*node, data, len); |
| 154 | blobmsg_format_xml_list(cur, data, len, true); |
| 155 | fclose(fp); |
| 156 | return; |
| 157 | case BLOBMSG_TYPE_TABLE: |
| 158 | fprintf(fp,"TABLE %p %p %u\n",cur, data, len); |
| 159 | blobmsg_format_xml_list_item(cur, data, len, false); |
| 160 | fclose(fp); |
| 161 | return; |
| 162 | } |
| 163 | fprintf(fp,"data_str %s\n",data_str); |
| 164 | fclose(fp); |
| 165 | mxmlNewText(cur, 0, data_str); |
| 166 | } |
| 167 | |
| 168 | void blobmsg_format_xml_tree(mxml_node_t *node, struct blob_attr *attr, int len, bool array) |
| 169 | { |
| 170 | struct blob_attr *pos; |
| 171 | int rem = len; |
| 172 | |
| 173 | mxml_node_t *cur; |
| 174 | cur = node; |
| 175 | |
| 176 | |
| 177 | __blob_for_each_attr(pos, attr, rem) |
| 178 | { |
| 179 | blobmsg_format_xml_element(&cur, pos, array); |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |