TLVPack.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * void * TLVPack (void const * memory, length_t extent, uint8_t type, uint16_t length, void const * value);
  11. *
  12. * lldp.h
  13. *
  14. * write an IEEE 802.1AB TLV into memory and return the address of
  15. * the next free memory location; type and length are converted to
  16. * 7-bits and 9-bits respectively per the standard;
  17. *
  18. * use macro TLV_HEAD to merge type and length into the header;
  19. *
  20. *--------------------------------------------------------------------*/
  21. #ifndef TLVPACK_SOURCE
  22. #define TLVPACK_SOURCE
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include "../tools/types.h"
  27. #include "../tools/memory.h"
  28. #include "../lldp/lldp.h"
  29. void * TLVPack (void const * memory, size_t extent, uint8_t type, uint16_t length, void const * value)
  30. {
  31. byte * offset = (byte *) (memory);
  32. struct tlv * tlv = (struct tlv *) (memory);
  33. if ((sizeof (* tlv) + length) <= extent)
  34. {
  35. tlv->head = TLV_HEAD (type, length);
  36. memcpy (tlv->data, value, length);
  37. offset += sizeof (* tlv) + length;
  38. }
  39. return ((void *) (offset));
  40. }
  41. #endif