poc_code.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "poc_code.h"
  2. #include "netio.h"
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #define MOD_ADLER 45521
  7. #define PTT_VOICE_DATA_FORMAT_2 //?????UDP????,rtp_hdr?????
  8. #ifdef PTT_VOICE_DATA_FORMAT_2
  9. #define UDP_VOICE_FORMAT_V 2
  10. #define COOKIE_RESERVE_BYTES 3 // 3???cookie
  11. #endif
  12. T_UINT32 pro_adler32(T_UINT8 *data, size_t len) /* data: Pointer to the data to be summed; len is in bytes */
  13. {
  14. T_UINT32 a = 1, b = 0;
  15. size_t index;
  16. if (NULL == data)
  17. {
  18. return 0;
  19. }
  20. /* Loop over each byte of data, in order */
  21. for (index = 0; index < len; ++index)
  22. {
  23. a = (a + data[index]) % MOD_ADLER;
  24. b = (b + a) % MOD_ADLER;
  25. }
  26. b = (b & 0xFF);
  27. a = (a & 0xFF);
  28. return (b << 8) | a;
  29. }
  30. T_UINT32 packet_encode(
  31. T_UINT8* buf,T_UINT32 buf_size,
  32. T_UINT8 name_cmd,
  33. const T_UINT8* protobuf,T_UINT32 protosize) {
  34. struct Protocol_Descriptor descriptor;
  35. if( buf == NULL)
  36. return 0;
  37. descriptor.packet_ptr = buf;
  38. descriptor.name_ptr = (char*)buf; //?????????
  39. descriptor.msg_size = protosize;
  40. descriptor.msg_ptr = buf + sizeof(T_UINT16) + sizeof(T_UINT8); // 1Byte cmd, 2 Bytes len
  41. descriptor.packet_size = sizeof(T_UINT8) + protosize + sizeof(T_UINT16); // 2????,1?????,?????protosize
  42. if( descriptor.packet_size + sizeof(T_UINT32) > buf_size ) {
  43. wlog_error( "packet_encode== buf_size:%d,pkt_size:%d", buf_size, descriptor.packet_size);
  44. return 0;
  45. }
  46. write_uint8(descriptor.packet_ptr, name_cmd);
  47. write_uint16(descriptor.packet_ptr+sizeof(T_UINT8),descriptor.packet_size);
  48. if( descriptor.msg_ptr && descriptor.msg_size > 0 ) {
  49. memcpy(descriptor.msg_ptr,protobuf,descriptor.msg_size);
  50. }
  51. descriptor.check_sum = pro_adler32(descriptor.packet_ptr,descriptor.packet_size);
  52. write_uint16(descriptor.packet_ptr + descriptor.packet_size,descriptor.check_sum);
  53. return descriptor.packet_size + sizeof(T_UINT16); // 2 ??CRC
  54. }
  55. T_UINT32 packet_decode(
  56. T_UINT8* buf,T_UINT32 buf_size,
  57. struct Protocol_Descriptor* descriptor) {
  58. T_UINT32 expected_checksum;
  59. descriptor->packet_ptr = buf;
  60. descriptor->name_ptr = (char*) descriptor->packet_ptr;
  61. read_uint16(descriptor->packet_ptr + sizeof(T_UINT8), &(descriptor->packet_size));
  62. if( descriptor->packet_size + sizeof(T_UINT16) > buf_size ) {
  63. return PROTOCOL_ERROR;
  64. }
  65. read_uint16(descriptor->packet_ptr + descriptor->packet_size,&(descriptor->check_sum));
  66. if( descriptor->check_sum != 0 ) {
  67. expected_checksum = pro_adler32(descriptor->packet_ptr,descriptor->packet_size);
  68. if( descriptor->check_sum != expected_checksum ) {
  69. return PROTOCOL_ERROR;
  70. }
  71. }
  72. descriptor->msg_ptr = descriptor->packet_ptr + sizeof(T_UINT16) + sizeof(T_UINT8);
  73. descriptor->msg_size = descriptor->packet_size - sizeof(T_UINT8) - sizeof(T_UINT16);
  74. return descriptor->packet_size + sizeof(T_UINT16); //??2??CRC
  75. }
  76. T_UINT32 hdr_rtp_encode(
  77. T_UINT8* buf,T_UINT32 buf_size,
  78. rtp_hdr_t* rtp_hdr) {
  79. // 1000 0000 0[payload] [seq] [ts] [ssrc]
  80. // 0x80
  81. T_UINT8 i = 0;
  82. T_UINT8 value8 = 0;
  83. T_UINT32 offset = 0;
  84. value8 = ( (rtp_hdr->version << 6) & 0xC0) | ((rtp_hdr->p << 5) & 0x20) | ((rtp_hdr->x << 4) & 0x10) | (rtp_hdr->cc & 0x0F) ;
  85. offset += write_uint8(buf,value8);
  86. value8 = ((rtp_hdr->pt & 0x7F) | ( rtp_hdr->m << 7) );
  87. offset += write_uint8(buf+offset,value8);
  88. offset += write_uint16(buf+offset,rtp_hdr->seq);
  89. offset += write_uint32(buf+offset,rtp_hdr->ts);
  90. offset += write_uint32(buf+offset,rtp_hdr->ssrc);
  91. for(i = 0; i < rtp_hdr->cc; ++i ) {
  92. if( offset > buf_size - sizeof(T_UINT32) ) {
  93. offset = 0;
  94. break;
  95. }
  96. offset += write_uint32(buf+offset,rtp_hdr->csrc[i]);
  97. }
  98. return offset;
  99. }
  100. T_UINT32 hdr_rtp_decode(const T_UINT8* buf,T_UINT32 buf_size,rtp_hdr_t* rtp_hdr) {
  101. T_UINT32 offset = 0;
  102. T_UINT8 i = 0;
  103. T_UINT8 value8 = 0;
  104. if( buf == 0 || buf_size < 12 )
  105. return 0;
  106. memset(rtp_hdr,0,sizeof(rtp_hdr_t));
  107. offset += read_uint8(buf+offset,&value8);
  108. rtp_hdr->version = ((value8 & 0xC0) >> 6);
  109. rtp_hdr->p = ((value8 & 0x20) >> 5);
  110. rtp_hdr->x = ((value8 & 0x10) >> 4);
  111. rtp_hdr->cc = (value8 & 0xF);
  112. if( buf_size < (sizeof(T_UINT32) * rtp_hdr->cc + 12) )
  113. return 0;
  114. offset += read_uint8(buf+offset,&value8);
  115. rtp_hdr->m = (( value8 & 0x80 ) >> 7);
  116. rtp_hdr->pt = (value8 & 0x7F);
  117. offset += read_uint16(buf+offset,&(rtp_hdr->seq));
  118. offset += read_uint32(buf+offset,&(rtp_hdr->ts));
  119. offset += read_uint32(buf+offset,&(rtp_hdr->ssrc));
  120. for(i = 0; i < rtp_hdr->cc; ++i) {
  121. offset += read_uint32(buf+offset,&(rtp_hdr->csrc[i]));
  122. }
  123. return offset;
  124. }
  125. void rtp_hdr_init(rtp_hdr_t* rtp_hdr) {
  126. memset(rtp_hdr,0,sizeof(rtp_hdr_t));
  127. rtp_hdr->version = UDP_VOICE_FORMAT_V;
  128. rtp_hdr->ssrc = get_uid();
  129. }
  130. T_UINT32 pa_rtp_heartbeat(T_BYTE* buffer,int len,int uid)
  131. {
  132. T_UINT32 l;
  133. rtp_hdr_t rtp;
  134. rtp_hdr_init(&rtp);
  135. rtp.ssrc = uid;
  136. rtp.pt = CODEC_PROTOBUF;
  137. l = hdr_rtp_encode(buffer,len,&rtp);
  138. // l += cp_heartbeat(ptl,buffer + l,len - l);
  139. return l;
  140. }
  141. T_UINT32 packet_rtp_encode(
  142. T_UINT8 * buf, T_UINT32 buf_size,
  143. rtp_hdr_t* rtp_hdr,
  144. const T_UINT8 * data, T_UINT32 size)
  145. {
  146. T_UINT32 offset = hdr_rtp_encode(buf,buf_size,rtp_hdr);
  147. if(offset>0 && offset + size <= buf_size)
  148. {
  149. memcpy(buf+offset,data,size);
  150. offset += size;
  151. }
  152. return offset;
  153. }