uplink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "uplink.h"
  2. #include "device.h"
  3. #include "../dev_mgr/gateway/gateway.h"
  4. #include "../network_mgr/net_proc.h"
  5. #include "../tax/tax.h"
  6. #include "../../BSP/BSP.h"
  7. #include "downlink.h"
  8. #include "me3616.h"
  9. #include "../network_mgr/sx1268/lora.h"
  10. extern ME3616 air;
  11. extern volatile uint32_t TickSec;
  12. //uplink_tax_t uplink_tax;
  13. /* 上报的业务数据类(0x03) 0x2001*/
  14. int uplink_tax_comb(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun,uint32_t seq_no)
  15. {
  16. uplink_tax_t *p_msg = (uplink_tax_t *)outdata;
  17. p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
  18. p_msg->collect_id = downlink_config.collect_conf[node].collect_no; //nodeConf.taxconf[node].device_id;
  19. p_msg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id;//nodeConf.taxconf[node].port[port].tax_id;
  20. p_msg->gun_no = downlink_config.collect_conf[node].tax_conf[port].gunid[gun];//nodeConf.taxconf[node].port[port].gun[gun].gun_no;
  21. p_msg->seq_no = seq_no;//此号需要加BCD码 nodeConf.taxconf[node].port[port].gun[gun].rcv_seq;
  22. p_msg->unit_price = rcv_data_temp.unit_price;
  23. p_msg->oil_volume = rcv_data_temp.oil_volume;
  24. p_msg->price = rcv_data_temp.price;
  25. p_msg->total_oil_volume = rcv_data_temp.total_oil_volume;
  26. p_msg->total_price = rcv_data_temp.total_price;
  27. p_msg->time_day = rcv_data_temp.time_day;
  28. p_msg->time_hour = rcv_data_temp.time_hour;
  29. p_msg->time_minute = rcv_data_temp.time_minute;
  30. *len = sizeof(uplink_tax_t);
  31. return 0;
  32. }
  33. /* 上报的业务的错误数据 数据类(0x03) 0x2002*/
  34. int uplink_tax_comb_2002(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun,uint8_t errorcode)
  35. {
  36. uplink_tax_error_t *p_msg = (uplink_tax_error_t *)outdata;
  37. p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
  38. p_msg->collect_id = downlink_config.collect_conf[node].collect_no; //nodeConf.taxconf[node].device_id;
  39. p_msg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id;//nodeConf.taxconf[node].port[port].tax_id;
  40. p_msg->gun_no = downlink_config.collect_conf[node].tax_conf[port].gunid[gun];//nodeConf.taxconf[node].port[port].gun[gun].gun_no;
  41. p_msg->errorcode = errorcode;
  42. p_msg->reserve = 0;
  43. *len = sizeof(uplink_tax_error_t);
  44. return 0;
  45. }
  46. /* 上报密文的原始数据 0x2071 */
  47. void uplink_data_cmd_0x2071(uint8_t *outdata, uint8_t *len, uint32_t serialNo, uint8_t cIndex, uint8_t portId, uint8_t gunId,rcv_data_0x8C_t * msg)
  48. {
  49. dataCmd0x2071 *p_msg = (dataCmd0x2071 *)outdata;
  50. p_msg->gatewaySn = downlink_config.gateway_id;
  51. p_msg->collSn = downlink_config.collect_conf[cIndex].collect_no;
  52. p_msg->taxId = portId+1;
  53. p_msg->gunId = gunId+1;
  54. p_msg->serialNo = serialNo;
  55. p_msg->dir = 0;
  56. p_msg->result = 0;
  57. p_msg->shakedown1[0] = 0;
  58. p_msg->shakedown1[1] = 0;
  59. p_msg->shakedown1[2] = 0;
  60. p_msg->reserve = 0;
  61. p_msg->da11len = msg->data11len;
  62. p_msg->da14len = msg->data14len;
  63. memcpy(p_msg->serialNum,msg->monitor_serino,10);
  64. memcpy(p_msg->data,msg->data14,msg->data14len);
  65. memcpy(&p_msg->data[msg->data14len],msg->data11,msg->data11len);
  66. *len = sizeof(dataCmd0x2071) - 128 + msg->data11len + msg->data14len;
  67. }
  68. int uplink_tax_comb_error(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun,uint32_t id,uint8_t flag)
  69. {
  70. uplink_tax_t *p_msg = (uplink_tax_t *)outdata;
  71. p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
  72. // if(flag == 0xA0) {
  73. // p_msg->collect_id = downlink_config.collect_conf[node].collect_no; //nodeConf.taxconf[node].device_id;
  74. // p_msg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id|flag;//nodeConf.taxconf[node].port[port].tax_id;
  75. // p_msg->gun_no = downlink_config.collect_conf[node].tax_conf[port].gunid[gun];//nodeConf.taxconf[node].port[port].gun[gun].gun_no;
  76. // }
  77. // else {
  78. p_msg->collect_id = id;
  79. p_msg->tax_no = port|flag;
  80. p_msg->gun_no = gun;
  81. // }
  82. p_msg->seq_no = 0;//seq_no;//此号需要加BCD码 nodeConf.taxconf[node].port[port].gun[gun].rcv_seq;
  83. p_msg->unit_price = 0;//rcv_data_temp.unit_price;
  84. p_msg->oil_volume = 0;//rcv_data_temp.oil_volume;
  85. p_msg->price = 0;//rcv_data_temp.price;
  86. p_msg->total_oil_volume = 0;//rcv_data_temp.total_oil_volume;
  87. p_msg->total_price = 0;//rcv_data_temp.total_price;
  88. p_msg->time_day = 0;//rcv_data_temp.time_day;
  89. p_msg->time_hour = 0;//rcv_data_temp.time_hour;
  90. p_msg->time_minute = 0;//rcv_data_temp.time_minute;
  91. *len = sizeof(uplink_tax_t);
  92. return 0;
  93. }
  94. /* 上报编码器的数据(0x03) 0x2051 */
  95. void uplink_encoder_data0x2051(uint8_t *outdata,uint8_t *len,uint8_t node,uint8_t port, uint8_t gun,uint16_t palen, uint8_t *da)
  96. {
  97. updataencoder0x2051 *p_msg = (updataencoder0x2051*)outdata;
  98. p_msg->gateway_id = downlink_config.gateway_id;//GATEWAY_ID;
  99. p_msg->collect_id = downlink_config.collect_conf[node].collect_no;
  100. p_msg->taxno = port;
  101. p_msg->gunno = gun;
  102. p_msg->reserve = 0;
  103. p_msg->paralen = palen;
  104. memcpy(p_msg->para,da,palen);
  105. // *len = sizeof(updataencoder0x2051)-32+20;
  106. *len = 15+palen;
  107. }
  108. /*上报的系统类(0x01)的 税控序列号 以及枪的个数 0x2011*/
  109. void uplink_tax_systemcmd_0x2011(uint8_t *outdata,uint8_t *len, uint8_t node,uint8_t port,uint8_t gun)
  110. {
  111. syscmd0x2011 *pmsg = (syscmd0x2011 *)outdata;
  112. pmsg->gateway_sn = downlink_config.gateway_id; // 网关sn号
  113. pmsg->coll_sn = downlink_config.collect_conf[node].collect_no;; // 采集器SN
  114. pmsg->tax_no = downlink_config.collect_conf[node].tax_conf[port].tax_id; // 报税口号
  115. pmsg->encrypt = downlink_config.collect_conf[node].tax_type; //明文 密文
  116. pmsg->tax_factory = downlink_config.collect_conf[node].tax_conf[port].factory; // 厂家
  117. pmsg->gun_num = downlink_config.collect_conf[node].tax_conf[port].gun_num;
  118. memcpy(pmsg->minor_num,downlink_config.collect_conf[node].tax_conf[port].monitor_serino+5,10);
  119. *len = sizeof(syscmd0x2011);
  120. return ;
  121. }
  122. /* 上报的系统类(0x01)的 升级信息状态 0x2001 */
  123. void uplink_update_systemcmd_0x2001(uint8_t *outdata,uint8_t *len,uint16_t type,uint32_t sn,uint16_t taskNo,uint8_t status)
  124. {
  125. syscmd0x2001 *pmsg = (syscmd0x2001*)outdata;
  126. pmsg->targetType = type;
  127. pmsg->deviceSn = sn;
  128. pmsg->taskNo = taskNo;
  129. pmsg->status = status;
  130. *len = sizeof(syscmd0x2001);
  131. return;
  132. }
  133. /* 上报的系统类(0x01)的 lora参数 0x2017 */
  134. void uplink_update_systemcmd_0x2017(uint8_t *outdata,uint8_t *len,uint32_t coll_sn,uint8_t result,uint8_t index)
  135. {
  136. syscmd0x2017 *pmsg = (syscmd0x2017*)outdata;
  137. pmsg->gateway_sn = downlink_config.gateway_id;
  138. pmsg->coll_sn = coll_sn;
  139. pmsg->result = result;
  140. pmsg->loraIndex = index;
  141. pmsg->freq = 0;
  142. pmsg->power = 0;
  143. pmsg->bandwidth = 0;
  144. pmsg->spreadingfactor = 0;
  145. pmsg->codingrate = 0;
  146. pmsg->preambleLen = 0;
  147. *len = sizeof(syscmd0x2017);
  148. return;
  149. }
  150. /* 上报的系统类(0x01)的 拓盛密文实时读取 0x2051 */
  151. void uplink_update_systemcmd_0x2051(uint8_t *outdata,uint8_t *len,uint32_t coll_sn,uint8_t taxno,uint8_t benable)
  152. {
  153. syscmd0x2051 *pmsg = (syscmd0x2051*)outdata;
  154. pmsg->gateway_sn = downlink_config.gateway_id;
  155. pmsg->coll_sn = coll_sn;
  156. pmsg->taxNo = taxno;
  157. pmsg->benable = benable;
  158. pmsg->reserve = 0;
  159. *len = sizeof(syscmd0x2051);
  160. return;
  161. }
  162. /*上报的状态类(0x02)的 固件信息 0x2001*/
  163. void uplink_tax_statusmcmd_0x2001(uint8_t *outdata,uint8_t *len,uint16_t type, uint8_t index)
  164. {
  165. statusCmd0x2001 *pmsg = (statusCmd0x2001 *)outdata;
  166. memset(pmsg->Uuid,0,12);//UUID
  167. memset(pmsg->Imei,0,15); //IMEI
  168. memset(pmsg->Iccid1,0,20);//ICCID1
  169. memset(pmsg->Iccid2,0,20);//ICCID2
  170. memset(pmsg->Iccid3,0,20);//ICCID3
  171. pmsg->gateway_Sn = g_firmwareMsg.gatewayMsg.hardwareMsg.gateway_sn; //网关编号
  172. pmsg->target_Type = type;//设备类型(0x0101:网关;0x0201;采集器:0x0301:液位仪采集器;0x0401:屏采集器)
  173. switch (type){
  174. case DEVICE_GATEWAY: // 网关的
  175. pmsg->sn = g_firmwareMsg.gatewayMsg.hardwareMsg.gateway_sn;//设备SN
  176. pmsg->bootloader_Version= g_firmwareMsg.gatewayMsg.verMsg.gate_bootloaderVr; //bootloader版本
  177. pmsg->app_Version = g_firmwareMsg.gatewayMsg.verMsg.gate_appVr;//APP版本
  178. pmsg->reset_Times = g_firmwareMsg.gatewayMsg.gate_resetnum;//复位次数
  179. pmsg->last_Reset_Type = g_firmwareMsg.gatewayMsg.gate_resetType;//最后一次复位类型
  180. memcpy(pmsg->Uuid ,g_firmwareMsg.gatewayMsg.Uuid,12);//UUID
  181. memcpy(pmsg->Imei ,g_firmwareMsg.gatewayMsg.Imei,15); //IMEI
  182. memcpy(pmsg->Iccid1,g_firmwareMsg.gatewayMsg.Iccid1,20);//ICCID1
  183. memcpy(pmsg->Iccid2,g_firmwareMsg.gatewayMsg.Iccid2,20);//ICCID2
  184. memcpy(pmsg->Iccid3,g_firmwareMsg.gatewayMsg.Iccid3,20);//ICCID3
  185. break;
  186. case DEVICE_COLLECT: case 0x0202:// 采集器的
  187. pmsg->sn = g_firmwareMsg.collMsg[index].device_sn;//设备SN
  188. pmsg->bootloader_Version= g_firmwareMsg.collMsg[index].bootloaderVr; //bootloader版本
  189. pmsg->app_Version = g_firmwareMsg.collMsg[index].appVr;//APP版本
  190. pmsg->reset_Times = g_firmwareMsg.collMsg[index].resetNum;//复位次数
  191. pmsg->last_Reset_Type = g_firmwareMsg.collMsg[index].resetType;//最后一次复位类型
  192. memcpy(pmsg->Uuid,g_firmwareMsg.collMsg[index].uuid,12);// UUID
  193. break;
  194. case DEVICE_OILL: // 油罐车的
  195. break;
  196. case DEVICE_LED: // 显示屏的
  197. break;
  198. default:
  199. break;
  200. }
  201. *len = sizeof(statusCmd0x2001);
  202. return;
  203. }
  204. /*上报的状态类(0x02)的 状态信息 0x2002*/
  205. void uplink_tax_statusmcmd_0x2002(uint8_t *outdata, uint8_t *len, uint16_t type, uint8_t index)
  206. {
  207. statusCmd0x2002 *pmsg = (statusCmd0x2002 *)outdata;
  208. pmsg->gateway_sn = downlink_config.gateway_id; // 网关sn号
  209. pmsg->target_Type = type;
  210. pmsg->loraPaIndex = g_runData.curloraIndex; // lora信号的通道号
  211. g_runData.rssi_4G = air.State.dBm +1000;
  212. g_firmwareMsg.gatewayMsg.gate_runTime = TickCounter/1000 + TickSec*4294967;
  213. switch (type)
  214. {
  215. case DEVICE_GATEWAY: // 网关的
  216. pmsg->sn = downlink_config.gateway_id; //
  217. pmsg->rssi_lora = 0;//g_runData.rssi_lora;
  218. pmsg->rssi_4G = g_runData.rssi_4G;
  219. pmsg->status = 1;
  220. pmsg->taxstatus[0] = 0;
  221. pmsg->taxstatus[1] = 0;
  222. pmsg->run_Time = g_firmwareMsg.gatewayMsg.gate_runTime;//运行时常
  223. pmsg->voltage = g_firmwareMsg.gatewayMsg.fVol.voltage; // 电压
  224. pmsg->tempperature = g_firmwareMsg.gatewayMsg.fTemper.temperature; // 温度
  225. //pmsg->loraPaIndex = g_runData.curloraIndex;
  226. pmsg->loraRcvNum = g_loraCollMsg.lora_rcvCount;
  227. pmsg->loraSendNum = g_loraCollMsg.lora_sendCount;
  228. pmsg->data = g_firmwareMsg.gatewayMsg.configmsg.bInitMsg.bInit.benable_qei&0xFF;
  229. pmsg->collerr = g_runData.collerr;
  230. pmsg->reserve = 0x00;
  231. break;
  232. case DEVICE_COLLECT: case 0x0202:// 采集器的
  233. pmsg->sn = downlink_config.collect_conf[index].collect_no;//g_firmwareMsg.collMsg[index].device_sn;
  234. pmsg->rssi_lora = g_runData.collrunMsg[index].lorastatus;
  235. pmsg->rssi_4G = 0;//g_runData.rssi_4G;
  236. pmsg->status = downlink_config.collect_conf[index].collect_status;// 采集器的状态 1:在线 0:离线
  237. pmsg->taxstatus[0] = downlink_config.collect_conf[index].tax_conf[0].status; // 报税口的状态 1: 在线 0:离线
  238. pmsg->taxstatus[1] = downlink_config.collect_conf[index].tax_conf[1].status; // 报税口的状态 1:在线 0:离线
  239. pmsg->run_Time = g_firmwareMsg.collMsg[index].runTime;//运行时常
  240. pmsg->voltage = g_firmwareMsg.collMsg[index].voltage; // 电压
  241. pmsg->tempperature = g_firmwareMsg.collMsg[index].temperature; // 温度
  242. pmsg->loraRcvNum = g_loraCollMsg.info[index].rcvNum;
  243. pmsg->loraSendNum = g_loraCollMsg.info[index].sendNum;
  244. pmsg->data = downlink_config.collect_conf[index].tax_type;
  245. pmsg->collerr = g_runData.collerr;
  246. pmsg->reserve = 0x00;
  247. break;
  248. case DEVICE_OILL: // 油罐车的
  249. break;
  250. case DEVICE_LED: // 显示屏的
  251. break;
  252. default:
  253. break;
  254. }
  255. *len = sizeof(statusCmd0x2002);
  256. return;
  257. }
  258. void uplink_tax_log_da(uint8_t *outdata,uint8_t *len, uint8_t type,uint8_t node,uint8_t port,uint8_t gun,uint32_t id,uint8_t cmd,uint8_t err)
  259. {
  260. char temp[64] = {0},i=0;
  261. uint16_t tempda = 0;
  262. uint16_t alsn = 0;
  263. switch(type){
  264. case LOG_BASIC_MSG:
  265. sprintf((char*)outdata,"DT:%04x|G:%010u|V:%04x|N:%01x",\
  266. 0x0101,\
  267. downlink_config.gateway_id,\
  268. g_firmwareMsg.gatewayMsg.verMsg.gate_appVr&0xFFFF,\
  269. downlink_config.collect_num);
  270. for(i=node;i<node+err;i++){
  271. memset(temp,0,32);
  272. tempda = downlink_config.collect_conf[i].tax_type;
  273. alsn = (downlink_config.collect_conf[i].tax_conf[0].alrd_sn<<2) |downlink_config.collect_conf[i].tax_conf[1].alrd_sn;
  274. alsn = (alsn<<8);
  275. sprintf(temp,"|%010u,%03x,%04x",\
  276. downlink_config.collect_conf[i].collect_no,\
  277. alsn|(downlink_config.collect_conf[i].tax_conf[0].gun_num<<4)|downlink_config.collect_conf[i].tax_conf[1].gun_num,\
  278. (tempda<<8)|(downlink_config.collect_conf[i].tax_conf[0].factory<<4)|downlink_config.collect_conf[i].tax_conf[1].factory);
  279. strcat((char*)outdata,temp);
  280. }
  281. break;
  282. case LOG_TAX_DA:
  283. switch(err){
  284. case LOG_00: strcpy(temp,(char*)"执行错误"); break;
  285. case LOG_01: strcpy(temp,(char*)"执行成功"); break;
  286. case LOG_02: strcpy(temp,(char*)"数据模块忙,稍后再试"); break;
  287. case LOG_03: strcpy(temp,(char*)"监控处理器忙"); break;
  288. case LOG_04: strcpy(temp,(char*)"监控处理器通讯超时"); break;
  289. case LOG_05: strcpy(temp,(char*)"查询记录不存在"); break;
  290. case LOG_10: strcpy(temp,(char*)"监控处理器序列号不存在"); break;
  291. case LOG_11: strcpy(temp,(char*)"密文数据解密错误"); break;
  292. case LOG_CRC: strcpy(temp,(char*)"校验错误"); break;
  293. case LOG_XSP: strcpy(temp,(char*)"显示屏"); break;
  294. case LOG_NOTAXID: strcpy(temp,(char*)"报税口不存在"); break;
  295. case LOG_NOGUNID: strcpy(temp,(char*)"枪号不存在"); break;
  296. case LOG_COLL_TIMOUT: strcpy(temp,(char*)"采-2超时"); break;
  297. case LOG_COLL_ENCPYST: strcpy(temp,(char*)"采-3"); break;
  298. case LOG_COLL_BUSY: strcpy(temp,(char*)"采-6"); break;
  299. default : strcpy(temp,(char*)"其它"); break;
  300. }
  301. sprintf((char*)outdata,"DT:%04x|GW:%010u|ET:%01d|C:%010u|T:%01d|SC:%02x|G:%02x|CM:%02x|ER:%2x%s",\
  302. 0x0101,downlink_config.gateway_id,0x01,id,port,\
  303. (downlink_config.collect_conf[node].tax_type<<4)|downlink_config.collect_conf[node].tax_conf[port-1].factory,\
  304. ((downlink_config.collect_conf[node].tax_conf[port-1].gun_num<<4)|gun),cmd,err,temp);
  305. break;
  306. case LOG_TAX_COLLSN: // 不是本网关下的采集器的SN
  307. sprintf((char*)outdata,"SN:%010u,不是本网关下的采集器\r\n",id);
  308. break;
  309. case LOG_LORA_ERROR:
  310. case LOG_CRC1: strcpy(temp,(char*)"lora接收检验错误"); break;
  311. case LOG_LORA_HEADERROR: strcpy(temp,(char*)"lora接收数据头不是fefe"); break;
  312. default : break;
  313. }
  314. *len = strlen((char*)outdata);
  315. return;
  316. }