common.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "common.h"
  2. #include "log.h"
  3. #include <string.h>
  4. void pdb_msg_dbg(struct pdb_msg msg) {
  5. int i;
  6. LERR("version = %d\n", msg.hdr.version);
  7. LERR("type = %d\n", msg.hdr.type);
  8. LERR("code = %d\n", msg.hdr.code);
  9. LERR("id = %d\n", msg.hdr.id);
  10. LERR("len = %d\n", msg.hdr.length);
  11. LERR("payload = ");
  12. if(msg.hdr.length > sizeof(msg.hdr)) {
  13. for (i = 0; i < msg.hdr.length - sizeof(msg.hdr); i++) {
  14. LERR("%02X ", msg.bdy.payload[i]);
  15. }
  16. } else {
  17. LERR("Incorrect value in msg.hdr.length \n");
  18. }
  19. LERR("\n");
  20. return ;
  21. }
  22. int pdb_msg_format_send(struct pdb_msg *msg,
  23. uint8_t version, uint8_t type,
  24. uint8_t code, uint16_t id,
  25. char *payload, uint16_t payload_len)
  26. {
  27. msg->hdr.version = version;
  28. msg->hdr.type = type;
  29. msg->hdr.code = code;
  30. msg->hdr.id = id;
  31. if (payload == NULL) {
  32. /* just ignore the NULL buff (called when just want to set the len) */
  33. msg->hdr.length = sizeof(struct pdb_hdr);
  34. return 0;
  35. } else {
  36. msg->hdr.length = sizeof(struct pdb_hdr) + payload_len;
  37. memcpy(msg->bdy.payload, payload, payload_len);
  38. return 0;
  39. }
  40. return 0;
  41. }