pocCmds.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "pocCmds.h"
  2. #include "protobuf-c.h"
  3. #include "ptt.pb-c.h"
  4. #include "poc_code.h"
  5. #include "poc_tcp.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "mallocSw.h"//if no this file,just include stdlib.h if system supported
  9. #define PLATFORM "CAT 1"
  10. #define DEVICE "LS"
  11. //Login
  12. static T_UINT16 pa_login(T_UINT8 *buffer, T_UINT16 len, T_INT8 *account, T_INT8 *pass){
  13. Login _login;
  14. T_UINT8 *pb=NULL;
  15. T_UINT16 rlen;
  16. login__init(&_login);
  17. _login.account=account;
  18. _login.password=pass;
  19. _login.platform=PLATFORM;
  20. _login.device=DEVICE;
  21. _login.meid=pass;
  22. _login.has_expect_payload=1;
  23. _login.expect_payload=CODEC_EVRC_8K;
  24. rlen=login__get_packed_size(&_login);
  25. if(rlen>0){
  26. pb=(T_UINT8 *)wmalloc(rlen);
  27. if(pb==NULL){
  28. wlog_error("login malloc err");
  29. return 0;
  30. }
  31. memset(pb, 0, rlen);
  32. rlen=login__pack(&_login, pb);
  33. }else rlen=0;
  34. rlen=packet_encode(buffer, len, U_LOGIN, pb, rlen);
  35. if(NULL != pb) wfree(pb);
  36. return rlen;
  37. }
  38. T_BOOL pocTryLogin(T_INT32 tcpSocket, T_UINT32 PSN, T_INT8 *pass){
  39. T_UINT8 buff[80];//实际只需要66个字节左右
  40. T_INT8 psn[20];
  41. T_UINT16 plen;
  42. memset(buff,0,sizeof(buff));
  43. snprintf(psn, sizeof(psn), "%d",PSN);
  44. plen=pa_login(buff,sizeof(buff),psn,pass);
  45. return poc_tx_data(tcpSocket,buff,plen);
  46. }
  47. //Logout
  48. static T_UINT16 pa_logout(T_BYTE *buf, T_UINT16 len){
  49. Logout _logout;
  50. T_UINT8 *pb=NULL;
  51. T_UINT16 rlen;
  52. logout__init(&_logout);
  53. rlen=logout__get_packed_size(&_logout);
  54. if(rlen>0){
  55. pb=(T_UINT8 *)wmalloc(rlen);
  56. if(pb == NULL){
  57. wlog_error("logout malloc err");
  58. return 0;
  59. }
  60. memset(pb, 0, rlen);
  61. rlen=logout__pack(&_logout,pb);
  62. }else rlen=0;
  63. rlen=packet_encode(buf,len,U_LOGOUT,pb,rlen);
  64. if(NULL != pb) wfree(pb);
  65. return rlen;
  66. }
  67. T_BOOL pocTryLogout(T_INT32 tcpSocket){
  68. T_UINT8 buff[10];//实际只需要5个字节即可
  69. T_UINT16 plen;
  70. memset(buff,0,sizeof(buff));
  71. plen=pa_logout(buff,sizeof(buff));
  72. return poc_tx_data(tcpSocket,buff,plen);
  73. }
  74. //query group
  75. static T_UINT16 pa_query_group(T_BYTE* buffer,T_UINT32 plen, T_UINT32 queryGroupNum){
  76. T_UINT16 rlen;
  77. T_UINT8 *pb=NULL;
  78. QueryGroup msg;
  79. query_group__init(&msg);
  80. msg.has_max_groups=1;
  81. msg.max_groups=queryGroupNum;
  82. rlen=query_group__get_packed_size(&msg);
  83. if(rlen>0){
  84. pb=(T_UINT8 *)wmalloc(rlen);
  85. if(pb == NULL){
  86. wlog_error("query group malloc err");
  87. return 0;
  88. }
  89. memset(pb, 0, rlen);
  90. rlen = query_group__pack(&msg, pb);
  91. }else rlen=0;
  92. rlen = packet_encode(buffer, plen, U_QUERY_GROUP, pb, rlen);
  93. if(NULL != pb) wfree(pb);
  94. return rlen;
  95. }
  96. T_BOOL pocTryQueryGroup(T_INT32 tcpSocket, T_UINT32 queryGroupNum){
  97. T_UINT8 buff[20];//实际需要10个字节
  98. T_UINT16 plen;
  99. memset(buff, 0, sizeof(buff));
  100. plen=pa_query_group(buff, sizeof(buff),queryGroupNum);
  101. return poc_tx_data(tcpSocket,buff,plen);
  102. }
  103. //join group
  104. static T_UINT16 pa_active_group(T_BYTE* buffer,T_UINT32 len,T_GID gid)
  105. {
  106. T_UINT16 rlen;
  107. JoinGroup msg;
  108. T_UINT8 *pb=NULL;
  109. join_group__init(&msg);
  110. msg.gid = gid;
  111. rlen=join_group__get_packed_size(&msg);
  112. if(rlen>0){
  113. pb=(T_UINT8 *)wmalloc(rlen);
  114. if(pb == NULL){
  115. wlog_error("join malloc err");
  116. return 0;
  117. }
  118. memset(pb, 0, rlen);
  119. rlen = join_group__pack(&msg, pb);
  120. }else rlen=0;;
  121. rlen = packet_encode(buffer, len, U_ENTER_GROUP, pb, rlen);
  122. if(NULL != pb) wfree(pb);
  123. return rlen;
  124. }
  125. T_BOOL pocTryJoinGroup(T_INT32 tcpSocket){
  126. T_UINT8 buff[14];//实际需要7个字节
  127. T_UINT16 plen;
  128. memset(buff, 0, sizeof(buff));
  129. plen=pa_active_group(buff, sizeof(buff),get_gid());
  130. return poc_tx_data(tcpSocket,buff,plen);
  131. }
  132. //request mic
  133. static int pa_request_mic(T_BYTE* buffer,T_UINT32 len,T_GID gid,T_UID uid){
  134. T_UINT16 rlen;
  135. RequestMic msg;
  136. T_UINT8 *pb=NULL;
  137. request_mic__init(&msg);
  138. msg.has_gid=1;
  139. msg.gid = gid;
  140. msg.has_uid=1;
  141. msg.uid=uid;
  142. rlen=request_mic__get_packed_size(&msg);
  143. if(rlen>0){
  144. pb=(T_UINT8 *)wmalloc(rlen);
  145. if(pb == NULL){
  146. wlog_error("requestmic malloc err");
  147. return 0;
  148. }
  149. memset(pb, 0, rlen);
  150. rlen = request_mic__pack(&msg, pb);
  151. }else rlen=0;
  152. rlen = packet_encode(buffer, len, U_REQUEST_MIC, pb, rlen);
  153. if(NULL!=pb) wfree(pb);
  154. return rlen;
  155. }
  156. T_BOOL pocTryReqMic(T_INT32 tcpSocket){
  157. T_UINT8 buff[18];//实际需要9个字节
  158. T_UINT16 plen;
  159. memset(buff, 0, sizeof(buff));
  160. plen=pa_request_mic(buff,sizeof(buff),get_gid(),get_uid());
  161. return poc_tx_data(tcpSocket,buff,plen);
  162. }
  163. //release mic
  164. static int pa_release_mic(T_BYTE* buffer,T_UINT32 len,T_GID gid,T_UID uid){
  165. T_UINT16 rlen;
  166. ReleaseMic msg;
  167. T_UINT8 *pb=NULL;
  168. release_mic__init(&msg);
  169. msg.has_gid=1;
  170. msg.gid = gid;
  171. msg.has_uid=1;
  172. msg.uid=uid;
  173. rlen=release_mic__get_packed_size(&msg);
  174. if(rlen>0){
  175. pb=(T_UINT8 *)wmalloc(rlen);
  176. if(pb == NULL){
  177. wlog_error("releasemic malloc err");
  178. return 0;
  179. }
  180. memset(pb, 0, rlen);
  181. rlen = release_mic__pack(&msg, pb);
  182. }else rlen=0;;
  183. rlen = packet_encode(buffer, len, U_RELEASE_MIC, pb, rlen);
  184. if(NULL != pb) wfree(pb);
  185. return rlen;
  186. }
  187. T_BOOL pocTryReleaseMic(T_INT32 tcpSocket){
  188. T_UINT8 buff[18];//实际需要9个字节
  189. T_UINT16 plen;
  190. //thisTalk->pttReqStatus=FALSE;
  191. memset(buff, 0, sizeof(buff));
  192. plen=pa_release_mic(buff,sizeof(buff),get_gid(),get_uid());
  193. return poc_tx_data(tcpSocket,buff,plen);
  194. }
  195. //tempCall
  196. static int pa_call(T_BYTE* buffer,T_UINT32 len,T_UINT32 uid){
  197. T_UINT16 rlen;
  198. Call msg;
  199. T_UINT8 *pb=NULL;
  200. call__init(&msg);
  201. msg.n_uid=1;
  202. msg.uid=&uid;
  203. rlen=call__get_packed_size(&msg);
  204. if(rlen>0){
  205. pb=(T_UINT8 *)wmalloc(rlen);
  206. if(pb == NULL)
  207. {
  208. wlog_error("pa_call malloc err");
  209. return 0;
  210. }
  211. memset(pb, 0, rlen);
  212. rlen = call__pack(&msg, pb);
  213. }else rlen=0;
  214. rlen = packet_encode(buffer, len, U_MAKE_TMP_CALL, pb, rlen);
  215. if(NULL != pb) wfree(pb);
  216. return rlen;
  217. }
  218. T_BOOL pocTryTempCall(T_INT32 tcpSocket,T_UID tuid){
  219. T_UINT8 buff[14];//实际需要7个字节
  220. T_UINT16 plen;
  221. memset(buff, 0, sizeof(buff));
  222. plen=pa_call(buff,sizeof(buff),tuid);
  223. return poc_tx_data(tcpSocket,buff,plen);
  224. }
  225. //query members
  226. static T_UINT16 pa_query_member(T_BYTE* buffer,T_UINT32 len,T_GID *gid,T_UINT32 queryNum){
  227. T_UINT16 rlen;
  228. T_UINT8 *pb=NULL;
  229. QueryMembers msg;
  230. query_members__init(&msg);
  231. msg.n_gids=1;
  232. msg.gids=(uint32_t *)gid;
  233. msg.has_detail=1;
  234. msg.detail=QUERY_MEMBERS__DETAIL_LEVEL__USER_INFO;
  235. msg.has_max_members=1;
  236. msg.max_members=queryNum;
  237. rlen=query_members__get_packed_size(&msg);
  238. if(rlen>0){
  239. pb=(T_UINT8 *)wmalloc(rlen);
  240. if(pb == NULL){
  241. wlog_error("query members malloc err");
  242. return 0;
  243. }
  244. memset(pb, 0, rlen);
  245. rlen = query_members__pack(&msg, pb);
  246. }else rlen=0;
  247. rlen = packet_encode(buffer, len, U_QUERY_MEMBERS, pb, rlen);
  248. if(NULL != pb) wfree(pb);
  249. return rlen;
  250. }
  251. T_BOOL pocTryQuerymember(T_INT32 tcpSocket,T_GID *gid,T_UINT32 queryNum){
  252. T_UINT8 buff[22];//实际需要11个字节
  253. T_UINT16 plen;
  254. memset(buff, 0, sizeof(buff));
  255. plen=pa_query_member(buff, sizeof(buff),gid,queryNum);
  256. return poc_tx_data(tcpSocket,buff,plen);
  257. }
  258. //tcp heartTick
  259. T_UINT16 pa_ping(T_BYTE* buffer,T_UINT32 len)
  260. {
  261. T_UINT8 *pb=NULL;
  262. T_UINT16 rlen;
  263. Ping _ping;
  264. ping__init(&_ping);
  265. _ping.has_timestamp = 0;
  266. rlen=ping__get_packed_size(&_ping);
  267. if(rlen>0){
  268. pb=(T_UINT8 *)wmalloc(rlen);
  269. if(pb == NULL){
  270. wlog_error("ping malloc err");
  271. return 0;
  272. }
  273. memset(pb, 0, rlen);
  274. rlen = ping__pack(&_ping, pb);
  275. }else rlen=0;;
  276. rlen=packet_encode(buffer, len, PUSH_PING_ACK, pb, rlen);
  277. if(NULL != pb) wfree(pb);
  278. return rlen;
  279. }
  280. T_BOOL pocTryTcpHeart(T_INT32 tcpSocket){
  281. T_UINT8 buff_f[10];//实际需要5个字节
  282. T_UINT16 plen;
  283. memset(buff_f, 0, sizeof(buff_f));
  284. plen = pa_ping(buff_f,sizeof(buff_f));
  285. return poc_tx_data(tcpSocket, buff_f, plen);
  286. }