net_ctrl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include "includes.h"
  2. #include "net_ctrl.h"
  3. #include "net_proc.h"
  4. #include "at_module.h"
  5. #include "me3616.h"
  6. #include "tax_ctrl.h"
  7. #include "gateway_collect.h"
  8. sverMsgHeader g_msg;
  9. extern uint16_t _crc16_get(uint8_t *_buff,uint32_t _len);
  10. extern ME3616 air;
  11. uint8_t net_send_buf[1024],net_send_len=0;
  12. uint8_t net_seng_state = 0;
  13. uint8_t net_rcv_data[512];
  14. uint16_t net_rcv_len = 0;
  15. uint8_t net_rcv_state = 0;
  16. //collect_para_t collect_para[4];
  17. //tax_para_t tax_para[4][4];
  18. uint8_t AIR_MQTT_PUB(char * topic, char *data, uint32_t len);
  19. uint16_t crc16_get(uint8_t *data, uint8_t size)
  20. {
  21. uint16_t crc=0;
  22. int i;
  23. for(i = 0; i < size; i++)
  24. {
  25. crc += data[i];
  26. }
  27. return crc;
  28. }
  29. int tax_net_send(uint8_t *tx_data, uint16_t len, uint8_t typeFirst,uint16_t typeSecd)
  30. {
  31. char *msg = NULL;
  32. OS_ERR err;
  33. static uint32_t seq_num = 0;
  34. uint16_t crc = 0;
  35. // sverMsgHeader p_msg;// = (sverMsgHeader *)net_send_buf;
  36. // memset(net_send_buf, 0, sizeof(net_send_buf));
  37. if(sys_net.net_hdl==NULL) return 0;
  38. memset(g_msg.info,0,sizeof(g_msg.info));
  39. g_msg.frame_header = 0xfefe;
  40. g_msg.proto_ver = 0x01;
  41. g_msg.seq_no = seq_num++;
  42. g_msg.msgtypeFirst = typeFirst;
  43. g_msg.msgtypeSecd = typeSecd;
  44. g_msg.len = len + 2;
  45. //p_msg->fcs = 0;
  46. memcpy(g_msg.info, tx_data, len);
  47. crc = _crc16_get((uint8_t *)&g_msg,len+12);
  48. g_msg.info[len] = crc&0xff;
  49. g_msg.info[len+1] = (crc>>8)&0xff;
  50. net_send_len = len+FRAME_HEADER_LEN+2;
  51. msg = (char *)net_queue_mem_calloc_must();
  52. if(msg) {
  53. memcpy(msg, (char*)&g_msg, len + FRAME_HEADER_LEN+2);
  54. net_queue_insert((char *)msg, len + FRAME_HEADER_LEN+2);
  55. }
  56. else
  57. {
  58. net_queue_mem_free(msg);
  59. }
  60. return 0;
  61. }
  62. //void serino_init(void)
  63. //{
  64. // uint8_t monitor_serino[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  65. // uint8_t coder_serino[20] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x11,0x22,0x33,0x44,0x55};
  66. // uint8_t dispaly_serino[16] = {0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,0x9A,0xAB,0xBC,0xCD,0xDE,0xEF,0xF0,0xFF};
  67. // collect_para[0].device_id = 0x01;;
  68. // collect_para[0].device_status = 0x00;
  69. // collect_para[0].tax_control_count = 0x02;
  70. //
  71. // collect_para[1].device_id = 0x02;;
  72. // collect_para[1].device_status = 0x00;
  73. // collect_para[1].tax_control_count = 0x03;
  74. //
  75. // memcpy(tax_para[0][0].tax_serino,monitor_serino,16);
  76. // memcpy(tax_para[0][0].coder_serino,coder_serino,20);
  77. // memcpy(tax_para[0][0].dispaly_serino,dispaly_serino,16);
  78. // tax_para[0][0].gun_count = 0x02;
  79. //}
  80. //int tax_data(uint8_t *data, uint8_t *len)
  81. //{
  82. // uint8_t monitor_serino[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  83. // uint8_t coder_serino[20] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0,0x11,0x22,0x33,0x44,0x55};
  84. // uint8_t dispaly_serino[16] = {0x12,0x23,0x34,0x45,0x56,0x67,0x78,0x89,0x9A,0xAB,0xBC,0xCD,0xDE,0xEF,0xF0,0xFF};
  85. //
  86. // uint8_t tx_data[128];
  87. //
  88. // tax_comm_info_t *p_msg = (tax_comm_info_t *)tx_data;
  89. //
  90. // memset(tx_data,0,sizeof(tx_data));
  91. // p_msg->device_type = 0x01;
  92. // p_msg->device_id = 0x02;
  93. // p_msg->device_status = 0x00;
  94. // p_msg->tax_control_count = 0x01;
  95. //
  96. // memcpy(p_msg->tax_control_info.monitor_serino,monitor_serino,16);
  97. // memcpy(p_msg->tax_control_info.coder_serino,coder_serino,20);
  98. // memcpy(p_msg->tax_control_info.dispaly_serino,dispaly_serino,16);
  99. // p_msg->tax_control_info.gun_count = 0x01;
  100. //
  101. // p_msg->tax_control_info.guns_info.gun_number = 0x01;
  102. // p_msg->tax_control_info.guns_info.gun_status = 0x00;
  103. // p_msg->tax_control_info.guns_info.total_price = 0x4358;
  104. // p_msg->tax_control_info.guns_info.total_oil_volume = 0x2567;
  105. // p_msg->tax_control_info.guns_info.last_unit_price = 0x96;
  106. // p_msg->tax_control_info.guns_info.last_oil_volume = 0x84;
  107. // p_msg->tax_control_info.guns_info.last_price = 0x200;
  108. //
  109. // *len = 85;
  110. // memcpy(data,tx_data,*len);
  111. //}
  112. //int tax_net_data_analyze(void *puser,uint8_t collect, uint8_t tax, uint8_t *outdata, uint8_t *len)
  113. //{
  114. //// uint8_t tx_data[128];
  115. // int ret = 0;
  116. // gun_info_t * p_gun = (gun_info_t *)puser;
  117. //
  118. // tax_comm_info_t *p_msg = (tax_comm_info_t *)outdata;
  119. // p_msg->device_type = 1;
  120. // p_msg->device_id = collect_para[collect].device_id;
  121. // p_msg->device_status = collect_para[collect].device_status;
  122. // p_msg->tax_control_count = collect_para[collect].tax_control_count;
  123. //
  124. //// printf("device_id : %d\n",p_msg->device_id);
  125. //// printf("device_status : %d\n",p_msg->device_status);
  126. //// printf("tax_control_count : %d\n",p_msg->tax_control_count);
  127. //
  128. // memcpy(p_msg->tax_control_info.monitor_serino,tax_para[collect][tax].tax_serino,16);
  129. // memcpy(p_msg->tax_control_info.coder_serino,tax_para[collect][tax].coder_serino,20);
  130. // memcpy(p_msg->tax_control_info.dispaly_serino,tax_para[collect][tax].dispaly_serino,16);
  131. // p_msg->tax_control_info.gun_count = tax_para[collect][tax].gun_count;
  132. //
  133. // p_msg->tax_control_info.guns_info.gun_number = p_gun->id;
  134. // p_msg->tax_control_info.guns_info.gun_status = p_gun->status;
  135. // p_msg->tax_control_info.guns_info.gun_type = p_gun->gun_type;
  136. // p_msg->tax_control_info.guns_info.gun_msgid = p_gun->gun_msgid;
  137. // if(p_gun->gun_type == SINGLE_DATA)
  138. // {
  139. // p_msg->tax_control_info.guns_info.last_oil_volume = p_gun->last_oil_volume;
  140. // p_msg->tax_control_info.guns_info.last_price = p_gun->last_price;
  141. // p_msg->tax_control_info.guns_info.last_unit_price = p_gun->last_unit_price;
  142. // }
  143. // else if(p_gun->gun_type == CUMULATIVE_DATA)
  144. // {
  145. // p_msg->tax_control_info.guns_info.total_oil_volume = p_gun->total_oil_volume;
  146. // p_msg->tax_control_info.guns_info.total_price = p_gun->total_price;
  147. // }
  148. //
  149. // *len = COLLECT_COM;
  150. // return ret;
  151. //}
  152. //void gw_net_comm(uint8_t *data, uint8_t len)
  153. //{
  154. // uint8_t send_buf[128],send_size;
  155. // uint8_t tmp_buf[128];
  156. //
  157. //
  158. //// collect_rcv_analyze(data,len,tmp_buf);
  159. //// tax_net_data_analyze();
  160. // tax_net_send(send_buf,send_size,1);
  161. //}
  162. //void gw_net_comm_test(void)
  163. //{
  164. // uint8_t send_buf[128],send_size;
  165. //
  166. // tax_data(send_buf, &send_size);
  167. //
  168. // tax_net_send(send_buf,send_size,1);
  169. //}
  170. int air_mqtt_pub(char *topic, char *data, uint8_t len)
  171. {
  172. int slen,slen1,send_len=0;
  173. uint8_t sendbuf[512];
  174. if(!air.State.MQTT_State) {
  175. ME3616_INFO("Cannot Connect MQTT");
  176. return 0;
  177. }
  178. slen=sprintf((char *)sendbuf,"AT+MPUB=\"%s\",0,0,\"",topic);
  179. send_len = slen;
  180. memcpy(sendbuf+send_len,data,len);
  181. send_len += len;
  182. slen1=sprintf((char *)sendbuf+send_len,"\"\r\n");
  183. send_len += slen1;
  184. uart_msg_send(UART3_ID, (char *)sendbuf, send_len);
  185. // uart_msg_send(UART1_ID, (char *)sendbuf, send_len);
  186. if(Module_Read_A_CRC("OK", 300)) {
  187. //ME3616_INFO("Pub Successful \r\n[%s]<-[%s]",topic,data);
  188. return 1;
  189. } else {
  190. ME3616_INFO("Pub ERROR");
  191. return 0;
  192. }
  193. }
  194. void gw_net_send(char *data, uint32_t len)
  195. {
  196. int i;
  197. // char send_buf[320];
  198. int send_len=0;
  199. if(air.State.MQTT_State)
  200. {
  201. memset(net_send_buf,0,sizeof(net_send_buf));
  202. for(i = 0;i < len;i++) {
  203. send_len += snprintf((char *)net_send_buf + send_len, sizeof(net_send_buf), "%02x", data[i]);
  204. }
  205. g_data4G.bconnect4G = AIR_MQTT_PUB((char*)g_upLinkTopic,(char*)net_send_buf,send_len);
  206. // AIR_MQTT_PUB((char*)g_upLinkTopic,data,len);
  207. // AIR_MQTT_PUB((char*)g_upLinkTopic,data,len);
  208. }
  209. }
  210. ////////////////////////////////
  211. void net_msg_recv1(void)
  212. {
  213. int len = 0;
  214. char *str,*str1,*str2;
  215. char *msg = NULL;
  216. OS_ERR err;
  217. int slen,data_len;
  218. if(!air.State.MQTT_State) {
  219. return;
  220. }
  221. return;
  222. memset(sys_net.str, 0, sizeof(sys_net.str));
  223. len = Module_Blocking_Read(sys_net.str, 1);
  224. if(len) {
  225. len = Module_Blocking_Read(sys_net.str+len, 100);
  226. str = strstr(sys_net.str, "byte,");
  227. str1 = strstr(sys_net.str,"\r\n");
  228. if(strstr(sys_net.str,"+MQTTSTATU:")){
  229. printf("\r\n aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n");
  230. }
  231. if(str && str1) {
  232. slen = net_data_copy(str+5,(uint8_t *)sys_net.str);
  233. net_rcv_len = net_rcvdata_ch((char *)sys_net.str,net_rcv_data,slen);
  234. data_dump("NET RECB", net_rcv_data, net_rcv_len);
  235. msg = (char *)net_queue_mem_calloc_must();
  236. if(msg) {
  237. memcpy(msg, net_rcv_data, net_rcv_len);
  238. net_queue_insert((char *)msg, net_rcv_len); //½«Êý¾Ý·Åµ½ÏûÏ¢¶ÓÁÐÖÐ
  239. }
  240. else
  241. {
  242. net_queue_mem_free(msg);
  243. }
  244. }
  245. }
  246. }
  247. unsigned char HexToChar(unsigned char bChar)
  248. {
  249. if((bChar>=0x30)&&(bChar<=0x39))
  250. {
  251. bChar -= 0x30;
  252. }
  253. else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
  254. {
  255. bChar -= 0x37;
  256. }
  257. else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
  258. {
  259. bChar -= 0x57;
  260. }
  261. else
  262. {
  263. bChar = 0xff;
  264. }
  265. return bChar;
  266. }
  267. int net_rcvdata_ch(char *data, uint8_t *out_data, int len)
  268. {
  269. int i,slen=0;
  270. uint8_t temp1,temp2;
  271. for(i = 0; i < len; i+=2)
  272. {
  273. temp1 = HexToChar(data[i]);
  274. temp2 = HexToChar(data[i+1]);
  275. out_data[slen] = (temp1<<4) | temp2;
  276. slen++;
  277. }
  278. return slen;
  279. }
  280. int net_data_copy(char *data, uint8_t *out_data)
  281. {
  282. int len = 0;
  283. int i=0;
  284. while(1)
  285. {
  286. if(data[len] == '\r'||data[len] == '\n')
  287. break;
  288. out_data[len] = data[len];
  289. len++;
  290. // if(len >= 512)
  291. // break;
  292. }
  293. return len;
  294. }