msgpck.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /**
  2. * Copyright (C) 2014 SINTEF <nicolas.harrand@gmail.com>
  3. *
  4. * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3, 29 June 2007;
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.gnu.org/licenses/lgpl-3.0.txt
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef heads_msgpck_h
  17. #define heads_msgpck_h
  18. #include "Arduino.h"
  19. // ************************** Look up *****************************/
  20. /**
  21. * Function: msgpck_what_next
  22. * Description: Look at Stream s's buffer and return the type of the first data in it.
  23. *
  24. * Parameter: Stream * s : input stream.
  25. *
  26. * return: type among:
  27. * - msgpck_empty 0xff
  28. * - msgpck_nil 0xc0
  29. * - msgpck_bool 0xc2
  30. * - msgpck_uint 0xcc
  31. * - msgpck_sint 0xd0
  32. * - msgpck_float 0xca
  33. * - msgpck_string 0xd9
  34. * - msgpck_bin 0xc4
  35. * - msgpck_ext 0xc7
  36. * - msgpck_array 0xdc
  37. * - msgpck_map 0xde
  38. * - msgpck_unknown 0x00
  39. *
  40. */
  41. uint8_t msgpck_what_next(Stream * s);
  42. /**
  43. * Function: msgpck_nil_next
  44. * Description: Look at Stream s's buffer and return the type of the first data in it.
  45. *
  46. * Parameter: Stream * s : input stream.
  47. *
  48. * return: true if next data is nil, false if not
  49. *
  50. */
  51. bool msgpck_nil_next(Stream * s);
  52. /**
  53. * Function: msgpck_bool_next
  54. * Description: Look at Stream s's buffer and return the type of the first data in it.
  55. *
  56. * Parameter: Stream * s : input stream.
  57. *
  58. * return: true if next data is a bool, false if not
  59. *
  60. */
  61. bool msgpck_bool_next(Stream * s);
  62. /**
  63. * Function: msgpck_integer_next
  64. * Description: Look at Stream s's buffer and return the type of the first data in it.
  65. *
  66. * Parameter: Stream * s : input stream.
  67. *
  68. * return: true if next data is an integer (signed or not), false if not
  69. *
  70. */
  71. bool msgpck_integer_next(Stream * s);
  72. /**
  73. * Function: msgpck_signed_next
  74. * Description: Look at Stream s's buffer and return the type of the first data in it.
  75. *
  76. * Parameter: Stream * s : input stream.
  77. *
  78. * return: true if next data is a signed integer, false if not
  79. *
  80. */
  81. bool msgpck_signed_next(Stream * s);
  82. /**
  83. * Function: msgpck_float_next
  84. * Description: Look at Stream s's buffer and return the type of the first data in it.
  85. *
  86. * Parameter: Stream * s : input stream.
  87. *
  88. * return: true if next data is a float (on 4 bytes), false if not
  89. *
  90. */
  91. bool msgpck_float_next(Stream * s);
  92. /**
  93. * Function: msgpck_string_next
  94. * Description: Look at Stream s's buffer and return the type of the first data in it.
  95. *
  96. * Parameter: Stream * s : input stream.
  97. *
  98. * return: true if next data is a String, false if not
  99. *
  100. */
  101. bool msgpck_string_next(Stream * s);
  102. /**
  103. * Function: msgpck_bin_next
  104. * Description: Look at Stream s's buffer and return the type of the first data in it.
  105. *
  106. * Parameter: Stream * s : input stream.
  107. *
  108. * return: true if next data is a binary format, false if not
  109. *
  110. */
  111. bool msgpck_bin_next(Stream * s);
  112. /**
  113. * Function: msgpck_array_next
  114. * Description: Look at Stream s's buffer and return the type of the first data in it.
  115. *
  116. * Parameter: Stream * s : input stream.
  117. *
  118. * return: true if next data is an array, false if not
  119. *
  120. */
  121. bool msgpck_array_next(Stream * s);
  122. /**
  123. * Function: msgpck_map_next
  124. * Description: Look at Stream s's buffer and return the type of the first data in it.
  125. *
  126. * Parameter: Stream * s : input stream.
  127. *
  128. * return: true if next data is a map, false if not
  129. *
  130. */
  131. bool msgpck_map_next(Stream * s);
  132. // ************************** Readers *****************************/
  133. /**
  134. * Function: msgpck_read_nil
  135. * Description: Read the first data of Stream s's buffer if it is a nil.
  136. *
  137. * Parameter: Stream * s : input stream.
  138. *
  139. * return: true if next data has been read correctly, false if not
  140. *
  141. */
  142. bool msgpck_read_nil(Stream * s);
  143. /**
  144. * Function: msgpck_read_bool
  145. * Description: Read the first data of Stream s's buffer if it is a bool.
  146. *
  147. * Parameter: Stream * s : input stream.
  148. * bool * b: pointer to the read value.
  149. *
  150. * return: true if next data has been read correctly, false if not
  151. *
  152. */
  153. bool msgpck_read_bool(Stream * s, bool *b);
  154. /**
  155. * Function: msgpck_read_integer
  156. * Description: Read the first data of Stream s's buffer if it is an integer.
  157. *
  158. * Parameter: Stream * s : input stream.
  159. * byte * b: pointer to the read value.
  160. * uint8_t max_size: max size in byte of the expected value.
  161. *
  162. * return: true if next data has been read correctly, false if not
  163. *
  164. */
  165. bool msgpck_read_integer(Stream * s, byte *b, uint8_t max_size);
  166. /**
  167. * Function: msgpck_read_float
  168. * Description: Read the first data of Stream s's buffer if it is a float.
  169. *
  170. * Parameter: Stream * s : input stream.
  171. * float * f: pointer to the read value.
  172. *
  173. * return: true if next data has been read correctly, false if not
  174. *
  175. */
  176. bool msgpack_read_float(Stream * s, float *f);
  177. /**
  178. * Function: msgpck_read_string
  179. * Description: Read the first data of Stream s's buffer if it is a string.
  180. *
  181. * Parameter: Stream * s : input stream.
  182. * char * str: pointer to the read value.
  183. * uint32_t max_size: max size in byte of the expected value.
  184. * uint32_t *str_size: pointer to the actual size of the read string
  185. *
  186. * return: true if next data has been read correctly, false if not
  187. *
  188. */
  189. bool msgpck_read_string(Stream * s, char * str, uint32_t max_size, uint32_t *str_size);
  190. /**
  191. * Function: msgpck_read_string
  192. * Description: Read the first data of Stream s's buffer if it is a string.
  193. *
  194. * Parameter: Stream * s : input stream.
  195. * char * str: pointer to the read value.
  196. * uint32_t max_size: max size in byte of the expected value.
  197. *
  198. * return: true if next data has been read correctly, false if not
  199. *
  200. */
  201. bool msgpck_read_string(Stream * s, char * str, uint32_t max_size);
  202. /**
  203. * Function: msgpck_read_bin
  204. * Description: Read the first data of Stream s's buffer if it is a bin.
  205. *
  206. * Parameter: Stream * s : input stream.
  207. * byte * bin: pointer to the read value.
  208. * uint32_t max_size: max size in byte of the expected value.
  209. * uint32_t *str_size: pointer to the actual size of the read bin
  210. *
  211. * return: true if next data has been read correctly, false if not
  212. *
  213. */
  214. bool msgpck_read_bin(Stream * s, byte * bin, uint32_t max_size, uint32_t *bin_size);
  215. /**
  216. * Function: msgpck_read_bin
  217. * Description: Read the first data of Stream s's buffer if it is a bin.
  218. *
  219. * Parameter: Stream * s : input stream.
  220. * byte * bin: pointer to the read value.
  221. * uint32_t max_size: max size in byte of the expected value.
  222. *
  223. * return: true if next data has been read correctly, false if not
  224. *
  225. */
  226. bool msgpck_read_bin(Stream * s, byte * bin, uint32_t max_size);
  227. /**
  228. * Function: msgpck_read_array_size
  229. * Description: Read the header of the incoming array.
  230. *
  231. * Parameter: Stream * s : input stream.
  232. * uint32_t * array_size: pointer to the read size of the incoming array
  233. *
  234. * return: true if next data has been read correctly, false if not
  235. *
  236. */
  237. bool msgpck_read_array_size(Stream * s, uint32_t * array_size);
  238. /**
  239. * Function: msgpck_read_map_size
  240. * Description: Read the header of the incoming map.
  241. *
  242. * Parameter: Stream * s : input stream.
  243. * uint32_t * map_size: pointer to the read size of the incoming map
  244. *
  245. * Warning: map_size will contain the number of pair (key, element)
  246. *
  247. * return: true if next data has been read correctly, false if not
  248. *
  249. */
  250. bool msgpck_read_map_size(Stream * s, uint32_t * map_size);
  251. // ************************** Writers *****************************/
  252. /**
  253. * Function: msgpck_write_nil
  254. * Description: Write nil on the output stream
  255. *
  256. * Parameter: Stream * s : output stream.
  257. *
  258. */
  259. void msgpck_write_nil(Stream * s);
  260. /**
  261. * Function: msgpck_write_bool
  262. * Description: Write a bool data on the output stream
  263. *
  264. * Parameter: Stream * s : output stream.
  265. * bool b: bool value to write
  266. *
  267. */
  268. void msgpck_write_bool(Stream * s, bool b);
  269. /**
  270. * Function: msgpck_write_integer
  271. * Description: Write an integer data on the output stream
  272. *
  273. * Parameter: Stream * s : output stream.
  274. * uint8_t u: integer value to write
  275. *
  276. */
  277. void msgpck_write_integer(Stream * s, uint8_t u);
  278. /**
  279. * Function: msgpck_write_integer
  280. * Description: Write an integer data on the output stream
  281. *
  282. * Parameter: Stream * s : output stream.
  283. * uint16_t u: integer value to write
  284. *
  285. */
  286. void msgpck_write_integer(Stream * s, uint16_t u);
  287. /**
  288. * Function: msgpck_write_integer
  289. * Description: Write an integer data on the output stream
  290. *
  291. * Parameter: Stream * s : output stream.
  292. * uint32_t u: integer value to write
  293. *
  294. */
  295. void msgpck_write_integer(Stream * s, uint32_t u);
  296. /**
  297. * Function: msgpck_write_integer
  298. * Description: Write an integer data on the output stream
  299. *
  300. * Parameter: Stream * s : output stream.
  301. * uint64_t u: integer value to write
  302. *
  303. */
  304. void msgpck_write_integer(Stream * s, uint64_t u);
  305. /**
  306. * Function: msgpck_write_integer
  307. * Description: Write an integer data on the output stream
  308. *
  309. * Parameter: Stream * s : output stream.
  310. * int8_t i: integer value to write
  311. *
  312. */
  313. void msgpck_write_integer(Stream * s, int8_t i);
  314. /**
  315. * Function: msgpck_write_integer
  316. * Description: Write an integer data on the output stream
  317. *
  318. * Parameter: Stream * s : output stream.
  319. * int16_t i: integer value to write
  320. *
  321. */
  322. void msgpck_write_integer(Stream * s, int16_t i);
  323. /**
  324. * Function: msgpck_write_integer
  325. * Description: Write an integer data on the output stream
  326. *
  327. * Parameter: Stream * s : output stream.
  328. * int32_t i: integer value to write
  329. *
  330. */
  331. void msgpck_write_integer(Stream * s, int32_t i);
  332. /**
  333. * Function: msgpck_write_integer
  334. * Description: Write an integer data on the output stream
  335. *
  336. * Parameter: Stream * s : output stream.
  337. * int64_t i: integer value to write
  338. *
  339. */
  340. void msgpck_write_integer(Stream * s, int64_t i);
  341. /**
  342. * Function: msgpck_write_float
  343. * Description: Write a float data on the output stream
  344. *
  345. * Parameter: Stream * s : output stream.
  346. * float f: float value to write
  347. *
  348. */
  349. void msgpck_write_float(Stream *s, float f);
  350. /**
  351. * Function: msgpck_write_string
  352. * Description: Write string on the output stream
  353. *
  354. * Parameter: Stream * s : output stream.
  355. * char * str: a string to write
  356. * uint32_t str_size: size of the string to write
  357. *
  358. */
  359. void msgpck_write_string(Stream * s, char * str, uint32_t str_size);
  360. /**
  361. * Function: msgpck_write_string
  362. * Description: Write a string on the output stream
  363. *
  364. * Parameter: Stream * s : output stream.
  365. * String str: string to write
  366. *
  367. */
  368. void msgpck_write_string(Stream * s, String str);
  369. /**
  370. * Function: msgpck_write_bin
  371. * Description: Write a bin data on the output stream
  372. *
  373. * Parameter: Stream * s : output stream.
  374. * byte * b: bin to write
  375. * uint32_t bin_size: size of the bin to write
  376. *
  377. */
  378. void msgpck_write_bin(Stream * s, byte * b, uint32_t bin_size);
  379. /**
  380. * Function: msgpck_write_array_header
  381. * Description: Write the header of an array on the output stream
  382. *
  383. * Parameter: Stream * s : output stream.
  384. * uint32_t ar_size: numder of element in the array
  385. *
  386. */
  387. void msgpck_write_array_header(Stream * s, uint32_t ar_size);
  388. /**
  389. * Function: msgpck_write_map_header
  390. * Description: Write the header of a map on the output stream
  391. *
  392. * Parameter: Stream * s : output stream.
  393. * uint32_t ar_size: numder of pair (key, element) in the map
  394. *
  395. */
  396. void msgpck_write_map_header(Stream * s, uint32_t map_size);
  397. /**
  398. * Function: msgpck_to_json
  399. * Description: read messagepack data on the input and write json on the ouput
  400. *
  401. * Parameter: Stream * output : output stream.
  402. * Stream * input : input stream.
  403. *
  404. */
  405. void msgpck_to_json(Stream * output, Stream * input);
  406. #endif