devMsg.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "devMsg.h"
  2. #include "ProtocolPack.h"
  3. #include "log.h"
  4. #include "records.h"
  5. #include "main.h"
  6. #include "app.h"
  7. DEVICEINFO_DEF *gDevInfo=&sut_saveRecDev.record;
  8. GNSSINFO_DEF *gGnssInfo=&sut_saveRecGnss.record;
  9. NEARINFO_DEF *gNearInfo=&sut_saveRecNear.record;
  10. /*打印保存的设备信息记录*/
  11. void DevRecShow(void){
  12. wlog_info("[DevInfo]Date:%02x-%02x-%02x %02x:%02x:%02x",gDevInfo->BCDTime[0],gDevInfo->BCDTime[1],gDevInfo->BCDTime[2],gDevInfo->BCDTime[3],gDevInfo->BCDTime[4],gDevInfo->BCDTime[5]);
  13. wlog_info("[DevInfo]Motion:ax=%d,ay=%d,az=%d,step=%d",gDevInfo->aspeed_x,gDevInfo->aspeed_y,gDevInfo->aspeed_z,gDevInfo->steps);
  14. wlog_info("[DevInfo]Bat=%d,CSQ=%d",gDevInfo->bat,gDevInfo->csq);
  15. wlog_info("[DevInfo]Lbs:mcc=%d,mnc=%d,lac=%d,cellid=%08x",gDevInfo->mcc,gDevInfo->mnc,gDevInfo->lac,gDevInfo->cellid);
  16. }
  17. /*打印保存的GNSS信息记录*/
  18. void GnssRecShow(void){
  19. wlog_info("[GnssInfo]Date:%02x-%02x-%02x %02x:%02x:%02x",gGnssInfo->BCDTime[0],gGnssInfo->BCDTime[1],gGnssInfo->BCDTime[2],gGnssInfo->BCDTime[3],gGnssInfo->BCDTime[4],gGnssInfo->BCDTime[5]);
  20. wlog_info("[GnssInfo]Location:L=%d,lat=%d,lon=%d,height=%d,speed=%d,dir=%d",gGnssInfo->located,gGnssInfo->latitude,gGnssInfo->longitude,gGnssInfo->height,gGnssInfo->speed,gGnssInfo->direction);
  21. wlog_info("[GnssInfo]onUseGps=%d,onViewGps=%d",gGnssInfo->onUseGps,gGnssInfo->onViewGps);
  22. }
  23. /*打印保存的NEAR信息记录*/
  24. void NearRecShow(void){
  25. int i;
  26. wlog_info("[NearInfo]Date:%02x-%02x-%02x %02x:%02x:%02x",gNearInfo->BCDTime[0],gNearInfo->BCDTime[1],gNearInfo->BCDTime[2],gNearInfo->BCDTime[3],gNearInfo->BCDTime[4],gNearInfo->BCDTime[5]);
  27. for(i=0;i<gNearInfo->num;i++){
  28. wlog_info("[NearInfo][%d]mac:%02x%02x%02x%02x%02x%02x,rssi:%d",i,gNearInfo->info[i].mac[0],gNearInfo->info[i].mac[1],gNearInfo->info[i].mac[2],gNearInfo->info[i].mac[3],gNearInfo->info[i].mac[4],gNearInfo->info[i].mac[5],gNearInfo->info[i].rssi);
  29. }
  30. }
  31. /*获取LBS与MOTION前清除缓存*/
  32. void clearLbsMotionOfDevinfo(void){
  33. gDevInfo->aspeed_x=0;
  34. gDevInfo->aspeed_y=0;
  35. gDevInfo->aspeed_z=0;
  36. gDevInfo->steps=0;
  37. gDevInfo->mcc=0;
  38. gDevInfo->mnc=0;
  39. gDevInfo->lac=0;
  40. gDevInfo->cellid=0;
  41. }
  42. /*填充设备状态信息*/
  43. int fillDevInfo(unsigned char *pBuf,DEVICEINFO_DEF *devinfo){
  44. int i,j;
  45. SUTDS ds;
  46. SUTDL dl;
  47. i=0;
  48. //设置补传状态
  49. if(app.recBuChuanFlag==FALSE) devinfo->status=1;
  50. else devinfo->status=2;
  51. //上传类型
  52. pBuf[i++]=devinfo->status;
  53. //时间
  54. for(j=0;j<sizeof(devinfo->BCDTime);j++) pBuf[i++]=devinfo->BCDTime[j];
  55. //硬件状态
  56. pBuf[i++]=devinfo->hardWareStatus;
  57. //告警状态
  58. pBuf[i++]=devinfo->warnStatus;
  59. //加速度x轴
  60. ds.Data.usData=devinfo->aspeed_x;
  61. pBuf[i++] = ds.Data.ucData.b2;
  62. pBuf[i++] = ds.Data.ucData.b1;
  63. //加速度y轴
  64. ds.Data.usData=devinfo->aspeed_y;
  65. pBuf[i++] = ds.Data.ucData.b2;
  66. pBuf[i++] = ds.Data.ucData.b1;
  67. //加速度z轴
  68. ds.Data.usData=devinfo->aspeed_z;
  69. pBuf[i++] = ds.Data.ucData.b2;
  70. pBuf[i++] = ds.Data.ucData.b1;
  71. //步数
  72. dl.Data.ulData=devinfo->steps;
  73. pBuf[i++]=dl.Data.ucData.b4;
  74. pBuf[i++]=dl.Data.ucData.b3;
  75. pBuf[i++]=dl.Data.ucData.b2;
  76. pBuf[i++]=dl.Data.ucData.b1;
  77. //电池
  78. pBuf[i++]=devinfo->bat;
  79. //信号强度
  80. pBuf[i++]=devinfo->csq;
  81. //国家代号
  82. ds.Data.usData=devinfo->mcc;
  83. pBuf[i++] = ds.Data.ucData.b2;
  84. pBuf[i++] = ds.Data.ucData.b1;
  85. //移动网络号
  86. pBuf[i++]=devinfo->mnc;
  87. //位置区码
  88. ds.Data.usData=devinfo->lac;
  89. pBuf[i++] = ds.Data.ucData.b2;
  90. pBuf[i++] = ds.Data.ucData.b1;
  91. //移动基站
  92. dl.Data.ulData=devinfo->cellid;
  93. pBuf[i++]=dl.Data.ucData.b4;
  94. pBuf[i++]=dl.Data.ucData.b3;
  95. pBuf[i++]=dl.Data.ucData.b2;
  96. pBuf[i++]=dl.Data.ucData.b1;
  97. return i;
  98. }
  99. /*读取一则设备状态信息,实际时要从本地读出来,这里每次只模拟一则
  100. 返回0表示没有数据了
  101. */
  102. char getNextDevStatusInfo(DEV_SAVE_DEF *devinfo){
  103. #ifdef USE_DEMO_DATA
  104. static char flag=1;
  105. char ret;
  106. DEVICEINFO_DEF *record=&devinfo->record;
  107. memset((unsigned char *)record, 0, sizeof(DEVICEINFO_DEF));
  108. ret=flag;
  109. //正常上报
  110. record->status=1;
  111. //时间
  112. record->BCDTime[0]=0x20;
  113. record->BCDTime[1]=0x06;
  114. record->BCDTime[2]=0x19;
  115. record->BCDTime[3]=0x12;
  116. record->BCDTime[4]=0x00;
  117. record->BCDTime[5]=0x00;
  118. //硬件状态
  119. record->hardWareStatus=0x00;
  120. //告警状态
  121. record->warnStatus=WARN_NORMAL;
  122. //x,y,z
  123. record->aspeed_x=0;
  124. record->aspeed_y=0;
  125. record->aspeed_z=0;
  126. //步数
  127. record->steps=0;
  128. //电池电量
  129. record->bat=41;
  130. //信号强度
  131. record->csq=30;
  132. //MCC
  133. record->mcc=460;
  134. //MNC
  135. record->mnc=0x00;
  136. //LAC
  137. record->lac=0x5555;
  138. //CELLID
  139. record->cellid=0x11223344;
  140. if(flag==1) flag=0;
  141. else flag=1;
  142. return ret;
  143. #else
  144. char ret;
  145. ret=recordRead(REC_DEVINFO, (void *)devinfo);
  146. if(ret != 0){
  147. //没有记录或出错了
  148. wlog_warn("DevInfo read end");
  149. return 0;
  150. }
  151. //读取一条成功
  152. //wlog_info("DevInfo read one ok");
  153. return 1;
  154. #endif
  155. }
  156. /*打包设备状态信息条数及设备状态信息*/
  157. int GetDeviceStatusInfo(unsigned char *pBuf,int leftsize){
  158. DEV_SAVE_DEF saveinfo;
  159. DEVICEINFO_DEF *devinfo=&saveinfo.record;
  160. unsigned char *infoNum=pBuf;
  161. unsigned char *infoBuf=pBuf+1;
  162. int i,k,ls;
  163. unsigned char j,num=0;
  164. i=0;ls=leftsize;
  165. //每次读取,将临时索引修正为读索引
  166. app.devReadTmp=app.devReadIndex;
  167. while(1){
  168. if(ls<sizeof(DEVICEINFO_DEF)){
  169. wlog_warn("buffer fill full:dev");
  170. break;
  171. }
  172. if(0==getNextDevStatusInfo(&saveinfo)) break;//设备信息读完了
  173. k=fillDevInfo(infoBuf+i,devinfo);
  174. i += k;//填充buffer索引增加
  175. num ++;//填充设备个数增加
  176. ls -= k;//剩余缓存字节减小
  177. }
  178. *infoNum=num;
  179. return (i+1);
  180. }
  181. /*填充GNSS状态信息*/
  182. int fillGnssInfo(unsigned char *pBuf,GNSSINFO_DEF *gnssInfo){
  183. int i,j;
  184. SUTDS ds;
  185. SUTDL dl;
  186. i=0;
  187. //设置补传状态
  188. if(app.recBuChuanFlag==FALSE) gnssInfo->status=1;
  189. else gnssInfo->status=2;
  190. //上传类型
  191. pBuf[i++]=gnssInfo->status;
  192. //时间
  193. for(j=0;j<sizeof(gnssInfo->BCDTime);j++) pBuf[i++]=gnssInfo->BCDTime[j];
  194. //定位
  195. pBuf[i++]=gnssInfo->located;
  196. //纬度
  197. dl.Data.ulData=gnssInfo->latitude;
  198. pBuf[i++]=dl.Data.ucData.b4;
  199. pBuf[i++]=dl.Data.ucData.b3;
  200. pBuf[i++]=dl.Data.ucData.b2;
  201. pBuf[i++]=dl.Data.ucData.b1;
  202. //经度
  203. dl.Data.ulData=gnssInfo->longitude;
  204. pBuf[i++]=dl.Data.ucData.b4;
  205. pBuf[i++]=dl.Data.ucData.b3;
  206. pBuf[i++]=dl.Data.ucData.b2;
  207. pBuf[i++]=dl.Data.ucData.b1;
  208. //高程
  209. ds.Data.usData=gnssInfo->height;
  210. pBuf[i++] = ds.Data.ucData.b2;
  211. pBuf[i++] = ds.Data.ucData.b1;
  212. //速度
  213. ds.Data.usData=gnssInfo->speed;
  214. pBuf[i++] = ds.Data.ucData.b2;
  215. pBuf[i++] = ds.Data.ucData.b1;
  216. //方向
  217. ds.Data.usData=gnssInfo->direction;
  218. pBuf[i++] = ds.Data.ucData.b2;
  219. pBuf[i++] = ds.Data.ucData.b1;
  220. //GPS个数
  221. pBuf[i++]=gnssInfo->onUseGps;
  222. //BD个数
  223. pBuf[i++]=gnssInfo->onViewGps;
  224. return i;
  225. }
  226. /*读取一则设备状态信息,实际时要从本地读出来,这里每次只模拟一则*/
  227. char getNextGnssInfo(GNSS_SAVE_DEF *devinfo){
  228. #ifdef USE_DEMO_DATA
  229. GNSSINFO_DEF *record=&devinfo->record;
  230. static char flag=1;
  231. char ret;
  232. ret=flag;
  233. memset((unsigned char *)record, 0, sizeof(GNSSINFO_DEF));
  234. //正常上报位置
  235. record->status=1;
  236. //时间
  237. record->BCDTime[0]=0x20;
  238. record->BCDTime[1]=0x06;
  239. record->BCDTime[2]=0x19;
  240. record->BCDTime[3]=0x12;
  241. record->BCDTime[4]=0x00;
  242. record->BCDTime[5]=0x00;
  243. //定位状态
  244. record->located=0x00;
  245. if(flag==1) flag=0;
  246. else flag=1;
  247. return ret;
  248. #else
  249. char ret;
  250. ret=recordRead(REC_GNSSINFO, (void *)devinfo);
  251. if(ret != 0){
  252. //没有记录或出错了
  253. wlog_warn("GnssInfo read end");
  254. return 0;
  255. }
  256. //读取一条成功
  257. //wlog_info("GnssInfo read one ok");
  258. return 1;
  259. #endif
  260. }
  261. /*打包GNSS位置信息条数及GNSS位置信息*/
  262. int GetGnssInfo(unsigned char *pBuf,int leftsize){
  263. GNSS_SAVE_DEF saveinfo;
  264. GNSSINFO_DEF *gnssInfo=&saveinfo.record;
  265. unsigned char *infoNum=pBuf;
  266. unsigned char *infoBuf=pBuf+1;
  267. int i,k,ls;
  268. unsigned char j,num=0;
  269. i=0;ls=leftsize;
  270. //每次读取,将临时索引修正为读索引
  271. app.gnssReadTmp=app.gnssReadIndex;
  272. while(1){
  273. if(ls<sizeof(GNSSINFO_DEF)){
  274. wlog_warn("buffer fill full:gnss");
  275. break;
  276. }
  277. if(0==getNextGnssInfo(&saveinfo)) break;//GNSS信息读完了
  278. k=fillGnssInfo(infoBuf+i,gnssInfo);
  279. i += k;//填充buffer索引增加
  280. num ++;//填充设备个数增加
  281. ls -= k;//剩余缓存字节减小
  282. }
  283. *infoNum=num;
  284. return (i+1);
  285. }
  286. /*填充NEAR状态信息*/
  287. int fillNearInfo(unsigned char *pBuf,NEARINFO_DEF *nearInfo){
  288. int i,j,k;
  289. i=0;
  290. //时间
  291. for(j=0;j<sizeof(nearInfo->BCDTime);j++) pBuf[i++]=nearInfo->BCDTime[j];
  292. //设备个数
  293. pBuf[i++]=nearInfo->num;
  294. for(j=0;j<nearInfo->num;j++){
  295. //mac
  296. for(k=0;k<sizeof(nearInfo->info[j].mac);k++) pBuf[i++]=nearInfo->info[j].mac[k];
  297. //rssi
  298. pBuf[i++]=nearInfo->info[j].rssi;
  299. }
  300. return i;
  301. }
  302. /*读取一则设备状态信息,实际时要从本地读出来,这里每次只模拟一则*/
  303. char getNextNearInfo(NEAR_SAVE_DEF *devinfo){
  304. #ifdef USE_DEMO_DATA
  305. NEARINFO_DEF *record=&devinfo->record;
  306. static char flag=1;
  307. char ret;
  308. ret=flag;
  309. memset((unsigned char *)record, 0, sizeof(NEARINFO_DEF));
  310. //时间
  311. record->BCDTime[0]=0x20;
  312. record->BCDTime[1]=0x07;
  313. record->BCDTime[2]=0x01;
  314. record->BCDTime[3]=0x12;
  315. record->BCDTime[4]=0x00;
  316. record->BCDTime[5]=0x00;
  317. record->num=0;
  318. if(flag==1) flag=0;
  319. else flag=1;
  320. return ret;
  321. #else
  322. char ret;
  323. ret=recordRead(REC_NEARINFO, (void *)devinfo);
  324. if(ret != 0){
  325. //没有记录或出错了
  326. wlog_warn("NearInfo read end");
  327. return 0;
  328. }
  329. //读取一条成功
  330. //wlog_info("NearInfo read one ok");
  331. return 1;
  332. #endif
  333. }
  334. /*打包附近设备信息条数及附近设备信息*/
  335. int GetNearbyDeviceInfo(unsigned char *pBuf,int leftsize){
  336. NEAR_SAVE_DEF saveinfo;
  337. NEARINFO_DEF *nearInfo=&saveinfo.record;
  338. unsigned char *infoNum=pBuf;
  339. unsigned char *infoBuf=pBuf+1;
  340. int i,k,ls;
  341. unsigned char j,num=0;
  342. i=0;ls=leftsize;
  343. //每次读取,将临时索引修正为读索引
  344. app.nearReadTmp=app.nearReadIndex;
  345. while(1){
  346. if(ls<sizeof(NEARINFO_DEF)){
  347. wlog_warn("buffer fill full:near");
  348. break;
  349. }
  350. if(0==getNextNearInfo(&saveinfo)) break;//NEAR信息读完了
  351. k=fillNearInfo(infoBuf+i,nearInfo);
  352. i += k;//填充buffer索引增加
  353. num ++;//填充设备个数增加
  354. ls -= k;//剩余缓存字节减小
  355. }
  356. *infoNum=num;
  357. return (i+1);
  358. }