ua2json.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. */
  7. /* Enable POSIX features */
  8. #if !defined(_XOPEN_SOURCE)
  9. # define _XOPEN_SOURCE 600
  10. #endif
  11. #ifndef _DEFAULT_SOURCE
  12. # define _DEFAULT_SOURCE
  13. #endif
  14. /* On older systems we need to define _BSD_SOURCE.
  15. * _DEFAULT_SOURCE is an alias for that. */
  16. #ifndef _BSD_SOURCE
  17. # define _BSD_SOURCE
  18. #endif
  19. #include <open62541/types.h>
  20. #include <stdio.h>
  21. /* Internal headers */
  22. #include <open62541/types_generated.h>
  23. #include <open62541/types_generated_handling.h>
  24. #include "ua_pubsub_networkmessage.h"
  25. #include "ua_types_encoding_binary.h"
  26. #include "ua_types_encoding_json.h"
  27. static UA_StatusCode
  28. encode(const UA_ByteString *buf, UA_ByteString *out,
  29. const UA_DataType *type) {
  30. void *data = malloc(type->memSize);
  31. if(!data)
  32. return UA_STATUSCODE_BADOUTOFMEMORY;
  33. size_t offset = 0;
  34. UA_StatusCode retval = UA_decodeBinary(buf, &offset, data, type, NULL);
  35. if(retval != UA_STATUSCODE_GOOD) {
  36. free(data);
  37. return retval;
  38. }
  39. if(offset != buf->length) {
  40. UA_delete(data, type);
  41. fprintf(stderr, "Input buffer not completely read\n");
  42. return UA_STATUSCODE_BADINTERNALERROR;
  43. }
  44. size_t jsonLength = UA_calcSizeJson(data, type, NULL, 0, NULL, 0, true);
  45. retval = UA_ByteString_allocBuffer(out, jsonLength);
  46. if(retval != UA_STATUSCODE_GOOD) {
  47. UA_delete(data, type);
  48. return retval;
  49. }
  50. uint8_t *bufPos = &out->data[0];
  51. const uint8_t *bufEnd = &out->data[out->length];
  52. retval = UA_encodeJson(data, type, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
  53. UA_delete(data, type);
  54. if(retval != UA_STATUSCODE_GOOD) {
  55. UA_ByteString_deleteMembers(out);
  56. return retval;
  57. }
  58. out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
  59. return UA_STATUSCODE_GOOD;
  60. }
  61. static UA_StatusCode
  62. decode(const UA_ByteString *buf, UA_ByteString *out,
  63. const UA_DataType *type) {
  64. void *data = malloc(type->memSize);
  65. if(!data)
  66. return UA_STATUSCODE_BADOUTOFMEMORY;
  67. UA_StatusCode retval = UA_decodeJson(buf, data, type);
  68. if(retval != UA_STATUSCODE_GOOD) {
  69. free(data);
  70. return retval;
  71. }
  72. size_t binLength = UA_calcSizeBinary(data, type);
  73. retval = UA_ByteString_allocBuffer(out, binLength);
  74. if(retval != UA_STATUSCODE_GOOD) {
  75. UA_delete(data, type);
  76. return retval;
  77. }
  78. uint8_t *bufPos = &out->data[0];
  79. const uint8_t *bufEnd = &out->data[out->length];
  80. retval = UA_encodeBinary(data, type, &bufPos, &bufEnd, NULL, NULL);
  81. UA_delete(data, type);
  82. if(retval != UA_STATUSCODE_GOOD) {
  83. UA_ByteString_deleteMembers(out);
  84. return retval;
  85. }
  86. out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
  87. return UA_STATUSCODE_GOOD;
  88. }
  89. #ifdef UA_ENABLE_PUBSUB
  90. static UA_StatusCode
  91. encodeNetworkMessage(const UA_ByteString *buf, UA_ByteString *out) {
  92. size_t offset = 0;
  93. UA_NetworkMessage msg;
  94. UA_StatusCode retval = UA_NetworkMessage_decodeBinary(buf, &offset, &msg);
  95. if(retval != UA_STATUSCODE_GOOD)
  96. return retval;
  97. if(offset != buf->length) {
  98. UA_NetworkMessage_deleteMembers(&msg);
  99. fprintf(stderr, "Input buffer not completely read\n");
  100. return UA_STATUSCODE_BADINTERNALERROR;
  101. }
  102. size_t jsonLength = UA_NetworkMessage_calcSizeJson(&msg, NULL, 0, NULL, 0, true);
  103. retval = UA_ByteString_allocBuffer(out, jsonLength);
  104. if(retval != UA_STATUSCODE_GOOD) {
  105. UA_NetworkMessage_deleteMembers(&msg);
  106. return retval;
  107. }
  108. uint8_t *bufPos = &out->data[0];
  109. const uint8_t *bufEnd = &out->data[out->length];
  110. retval = UA_NetworkMessage_encodeJson(&msg, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
  111. UA_NetworkMessage_deleteMembers(&msg);
  112. if(retval != UA_STATUSCODE_GOOD) {
  113. UA_ByteString_deleteMembers(out);
  114. return retval;
  115. }
  116. out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
  117. return UA_STATUSCODE_GOOD;
  118. }
  119. static UA_StatusCode
  120. decodeNetworkMessage(const UA_ByteString *buf, UA_ByteString *out) {
  121. UA_NetworkMessage msg;
  122. UA_StatusCode retval = UA_NetworkMessage_decodeJson(&msg, buf);
  123. if(retval != UA_STATUSCODE_GOOD)
  124. return retval;
  125. size_t binLength = UA_NetworkMessage_calcSizeBinary(&msg);
  126. retval = UA_ByteString_allocBuffer(out, binLength);
  127. if(retval != UA_STATUSCODE_GOOD) {
  128. UA_NetworkMessage_deleteMembers(&msg);
  129. return retval;
  130. }
  131. uint8_t *bufPos = &out->data[0];
  132. const uint8_t *bufEnd = &out->data[out->length];
  133. retval = UA_NetworkMessage_encodeBinary(&msg, &bufPos, bufEnd);
  134. UA_NetworkMessage_deleteMembers(&msg);
  135. if(retval != UA_STATUSCODE_GOOD) {
  136. UA_ByteString_deleteMembers(out);
  137. return retval;
  138. }
  139. out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
  140. return UA_STATUSCODE_GOOD;
  141. }
  142. #endif
  143. static void
  144. usage(void) {
  145. printf("Usage: ua2json [encode|decode] [-t dataType] [-o outputFile] [inputFile]\n"
  146. "- encode/decode: Translate UA binary input to UA JSON / "
  147. "Translate UA JSON input to UA binary (required)\n"
  148. "- dataType: UA DataType of the input (default: Variant)\n"
  149. "- outputFile: Output target (default: write to stdout)\n"
  150. "- inputFile: Input source (default: read from stdin)\n");
  151. }
  152. int main(int argc, char **argv) {
  153. UA_Boolean encode_option = true;
  154. UA_Boolean pubsub = false;
  155. const char *datatype_option = "Variant";
  156. const char *input_option = NULL;
  157. const char *output_option = NULL;
  158. UA_ByteString outbuf = UA_BYTESTRING_NULL;
  159. UA_ByteString buf = UA_BYTESTRING_NULL;
  160. FILE *in = stdin;
  161. FILE *out = stdout;
  162. int retcode = -1;
  163. /* Read the command line options */
  164. if(argc < 2) {
  165. usage();
  166. return 0;
  167. }
  168. if(strcmp(argv[1], "encode") == 0) {
  169. encode_option = true;
  170. } else if(strcmp(argv[1], "decode") == 0) {
  171. encode_option = false;
  172. } else {
  173. fprintf(stderr, "Error: The first argument must be \"encode\" or \"decode\"\n");
  174. return -1;
  175. }
  176. for(int argpos = 2; argpos < argc; argpos++) {
  177. if(strcmp(argv[argpos], "--help") == 0) {
  178. usage();
  179. return 0;
  180. }
  181. if(strcmp(argv[argpos], "-t") == 0) {
  182. if(argpos + 1 == argc) {
  183. usage();
  184. return -1;
  185. }
  186. argpos++;
  187. datatype_option = argv[argpos];
  188. continue;
  189. }
  190. if(strcmp(argv[argpos], "-o") == 0) {
  191. if(argpos + 1 == argc) {
  192. usage();
  193. return -1;
  194. }
  195. argpos++;
  196. output_option = argv[argpos];
  197. continue;
  198. }
  199. if(argpos + 1 == argc) {
  200. input_option = argv[argpos];
  201. continue;
  202. }
  203. usage();
  204. return -1;
  205. }
  206. /* Find the data type */
  207. const UA_DataType *type = NULL;
  208. if(strcmp(datatype_option, "PubSub") == 0) {
  209. pubsub = true;
  210. } else {
  211. for(size_t i = 0; i < UA_TYPES_COUNT; ++i) {
  212. if(strcmp(datatype_option, UA_TYPES[i].typeName) == 0) {
  213. type = &UA_TYPES[i];
  214. break;
  215. }
  216. }
  217. if(!type) {
  218. fprintf(stderr, "Error: Datatype not found\n");
  219. return -1;
  220. }
  221. }
  222. /* Open files */
  223. if(input_option) {
  224. in = fopen(input_option, "rb");
  225. if(!in) {
  226. fprintf(stderr, "Could not open input file %s\n", input_option);
  227. goto cleanup;
  228. }
  229. }
  230. if(output_option) {
  231. out = fopen(output_option, "wb");
  232. if(!out) {
  233. fprintf(stderr, "Could not open output file %s\n", output_option);
  234. goto cleanup;
  235. }
  236. }
  237. /* Read input until EOF */
  238. size_t pos = 0;
  239. size_t length = 128;
  240. while(true) {
  241. if(pos >= buf.length) {
  242. length = length * 8;
  243. UA_Byte *r = (UA_Byte*)realloc(buf.data, length);
  244. if(!r) {
  245. fprintf(stderr, "Out of memory\n");
  246. goto cleanup;
  247. }
  248. buf.length = length;
  249. buf.data = r;
  250. }
  251. ssize_t c = read(fileno(in), &buf.data[pos], length - pos);
  252. if(c == 0)
  253. break;
  254. if(c < 0) {
  255. fprintf(stderr, "Reading from input failed\n");
  256. goto cleanup;
  257. }
  258. pos += (size_t)c;
  259. }
  260. if(pos == 0) {
  261. fprintf(stderr, "No input\n");
  262. goto cleanup;
  263. }
  264. buf.length = pos;
  265. /* Convert */
  266. UA_StatusCode result = UA_STATUSCODE_BADNOTIMPLEMENTED;
  267. #ifdef UA_ENABLE_PUBSUB
  268. if(pubsub && encode_option) {
  269. result = encodeNetworkMessage(&buf, &outbuf);
  270. } else if(pubsub) {
  271. result = decodeNetworkMessage(&buf, &outbuf);
  272. } else
  273. #endif
  274. if(encode_option) {
  275. result = encode(&buf, &outbuf, type);
  276. } else {
  277. result = decode(&buf, &outbuf, type);
  278. }
  279. if(result != UA_STATUSCODE_GOOD) {
  280. fprintf(stderr, "Error: Parsing failed with code %s\n",
  281. UA_StatusCode_name(result));
  282. goto cleanup;
  283. }
  284. /* Print the output and quit */
  285. fwrite(outbuf.data, 1, outbuf.length, out);
  286. retcode = 0;
  287. cleanup:
  288. UA_ByteString_deleteMembers(&buf);
  289. UA_ByteString_deleteMembers(&outbuf);
  290. if(in != stdin && in)
  291. fclose(in);
  292. if(out != stdout && out)
  293. fclose(out);
  294. return retcode;
  295. }