common.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. for (i = 0; i < msg.hdr.length - sizeof(msg.hdr); i++) {
  13. LERR("%02X ", msg.bdy.payload[i]);
  14. }
  15. LERR("\n");
  16. return ;
  17. }
  18. int pdb_msg_format_send(struct pdb_msg *msg,
  19. uint8_t version, uint8_t type,
  20. uint8_t code, uint16_t id,
  21. char *payload, uint16_t payload_len)
  22. {
  23. msg->hdr.version = version;
  24. msg->hdr.type = type;
  25. msg->hdr.code = code;
  26. msg->hdr.id = id;
  27. if (payload == NULL) {
  28. /* just ignore the NULL buff (called when just want to set the len) */
  29. msg->hdr.length = sizeof(struct pdb_hdr);
  30. return 0;
  31. } else {
  32. msg->hdr.length = sizeof(struct pdb_hdr) + payload_len;
  33. memcpy(msg->bdy.payload, payload, payload_len);
  34. return 0;
  35. }
  36. return 0;
  37. }