NodeLinkMaster.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #include "nodelinkmaster.h"
  2. #include "NodeLinkCommon.h"
  3. #include "encrypt_xxtea.h"
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include "stm32f10x.h"
  7. #include "core_cm3.h"
  8. #include "board.h"
  9. #include "global.h"
  10. unsigned char LifeSignal;
  11. unsigned char ResetFlg;
  12. //unsigned char slave_info[17];
  13. sw_timer_t decode_err_timer;
  14. unsigned char BeginRecvFlg;
  15. /**
  16. * 超时处理
  17. *
  18. * @author lxz (042620 16:02:40)
  19. *
  20. * @param device
  21. */
  22. static void deivce0_on_timeout(struct nodelink_device *device) {
  23. }
  24. /**
  25. * 接收事件处理
  26. *
  27. * @author lxz (042620 16:02:58)
  28. *
  29. * @param device
  30. * @param data
  31. */
  32. static void device0_on_recv(struct nodelink_device *device, unsigned char *data) {
  33. }
  34. /**
  35. * 发送事件
  36. *
  37. * @author lxz (042620 16:03:17)
  38. *
  39. * @param device
  40. * @param data
  41. * @param len
  42. */
  43. static int device0_on_send(struct nodelink_device *device, unsigned char *data) {
  44. return 0;
  45. }
  46. static nodelink_device_ops device0 = {
  47. device0_on_recv,
  48. device0_on_send,
  49. deivce0_on_timeout
  50. };
  51. /**
  52. * 执行主站初始化
  53. *
  54. * @author LXZ (070720)
  55. *
  56. * @param master
  57. */
  58. void nodelink_master_init(nodelink_master_t *master) {
  59. unsigned char code[16];
  60. unsigned char i;
  61. unsigned long cDelay=1000000;
  62. //产生主站随机code,这里使用定时器最后8位,
  63. //由于晶振细微差异,可以保证每次上电不同
  64. for(i=0;i<16;i++)
  65. {
  66. while(cDelay--);
  67. code[i]=(unsigned char)(SysTick->VAL & 0xff);
  68. cDelay=1000000;
  69. }
  70. //清空内存
  71. memset(master, 0, sizeof(nodelink_master_t));
  72. master->device[0].ops = &device0;
  73. //设置code
  74. nodelink_master_set_code(master,code);
  75. //设置开始扫描从站
  76. nodelink_master_reset(master);
  77. BeginRecvFlg=0;
  78. }
  79. /**
  80. * 通过ID查找设备
  81. *
  82. * @author LXZ (070720)
  83. *
  84. * @param master
  85. */
  86. unsigned char nodelink_find_device_by_ID(nodelink_master_t *master,unsigned char id) {
  87. int i;
  88. for(i=1;i<=master->dev_count;i++)
  89. {
  90. if(master->device[i].id == id) return i;
  91. }
  92. return 0;
  93. }
  94. /**
  95. * 执行主站的接收动作
  96. *
  97. * @author lxz (041820 16:51:42)
  98. *
  99. * @param master 主站对象
  100. * @param src 接收缓冲
  101. * @param len 接收长度
  102. *
  103. */
  104. void nodelink_master_recv(nodelink_master_t *master,
  105. unsigned char *src,
  106. int len) {
  107. int i = 0;
  108. unsigned char id;
  109. unsigned long length;
  110. length = nodelink_decode(&(master->data_in[0]), src, len);
  111. if (length > 0) {
  112. //复位
  113. if(master->data_in[1]==0xA5 && master->data_in[2]==1 && master->bus_status != 0xFF)
  114. {
  115. //复位一下总线
  116. ResetFlg=1;
  117. }
  118. else if(master->data_in[1]==0xA4 && master->data_in[2]==0x81)
  119. {
  120. //memcpy(slave_info,&master->data_in[0],17);
  121. for(i=0;i<17;i++)
  122. {
  123. user_datas[512+i]=master->data_in[i];
  124. }
  125. }
  126. //分配ID过程中
  127. else if (master->bus_status == 0xF0)
  128. {
  129. if (master->step == 0)//发送了KEY,接收返回信息
  130. {
  131. if(master->data_in[1]==0xA0)//数据帧类型
  132. {
  133. if (memcmp(&(master->data_in[2]), master->code, 16) == 0) //判断设备合法性
  134. {
  135. master->step=1;
  136. master->count = 5;
  137. sw_timer_start(&decode_err_timer, 1, 0);
  138. }
  139. }
  140. }
  141. else if(master->step == 1)//Key验证通过,接收物理ID
  142. {
  143. if(master->data_in[1]==0xA1)//数据帧类型
  144. {
  145. for(i=length-3;i>=2;i-=3)//从队尾(靠近主站的板卡)开始添加设备
  146. {
  147. master->dev_count++;
  148. master->count = 5;
  149. master->device[master->dev_count].id = master->data_in[i];
  150. master->device[master->dev_count].uniqueid = master->data_in[i+1];
  151. master->device[master->dev_count].model = master->data_in[i+2];
  152. if (master->callback != 0) {
  153. master->callback(master,NL_MASTER_EVENT_FOUND_DEVICE, &master->device[master->dev_count]);
  154. }
  155. }
  156. }
  157. if(master->dev_count>0)
  158. {
  159. master->step = 2;//主板通知配置完成
  160. master->count=50;
  161. sw_timer_start(&decode_err_timer, 0, 500000);
  162. }
  163. }
  164. else
  165. {
  166. sw_timer_start(&decode_err_timer, 0, 500000);
  167. }
  168. }
  169. //正常通信模式
  170. else if(master->bus_status == 0xF1)
  171. {
  172. if(master->data_in[1]==0xA3)//数据帧类型
  173. {
  174. id = nodelink_find_device_by_ID(master,master->data_in[0]);
  175. if(id>0)
  176. {
  177. if (master->device[id].ops != 0)
  178. master->device[id].ops->on_recv(&master->device[id], &(master->data_in[0]));
  179. sw_timer_start(&decode_err_timer, 0, 200000);
  180. }
  181. }
  182. }
  183. }
  184. if (BeginRecvFlg==1 && sw_timer_expire(&decode_err_timer))
  185. {
  186. ResetFlg=1;
  187. }
  188. }
  189. /**
  190. *
  191. *
  192. * @author lxz (042620 16:49:13)
  193. *
  194. * @param master
  195. */
  196. void nodelink_master_reset(nodelink_master_t *master) {
  197. master->bus_status = 0xFF;
  198. master->step = 0;
  199. master->count = 100;
  200. ResetFlg=0;
  201. BeginRecvFlg=0;
  202. }
  203. /**
  204. *
  205. *
  206. * @author lxz (042620 16:49:17)
  207. *
  208. * @param master
  209. */
  210. void nodelink_master_begin_scan(nodelink_master_t *master) {
  211. master->bus_status = 0xF0;
  212. master->step = 0;
  213. master->dev_count = 0;
  214. master->count = 100;
  215. memset(&master->device[0],0,sizeof(master->device));
  216. }
  217. /**
  218. * 进入工作模式
  219. *
  220. * @author LXZ (070720)
  221. *
  222. * @param master
  223. */
  224. void nodelink_master_begin_working(nodelink_master_t *master) {
  225. master->bus_status = 0xF1;
  226. master->step = 0;
  227. master->count = 5;
  228. }
  229. /**
  230. * 注册16字节识别码
  231. *
  232. * @author LXZ (070720)
  233. *
  234. * @param master
  235. * @param code
  236. */
  237. void nodelink_master_set_code(nodelink_master_t * master, unsigned char * code)
  238. {
  239. memcpy(master->code,code,16);
  240. }
  241. /**
  242. * 注册回调事件
  243. *
  244. * @author LXZ (070720)
  245. *
  246. * @param master
  247. */
  248. void nodelink_master_set_callback(nodelink_master_t * master,
  249. void(*callback)(nodelink_master_t * master, char , void *))
  250. {
  251. master->callback = callback;
  252. }
  253. /**
  254. * 执行主站输入输出处理
  255. *
  256. * @author lxz (041820 16:20:35)
  257. *
  258. * @param master 主站对象
  259. * @param dst 目标缓冲
  260. * @param src 当前缓冲
  261. * @param len 长度
  262. */
  263. int nodelink_master_send(nodelink_master_t *master,
  264. unsigned char *dst) {
  265. int i = 0;
  266. int res = 0;
  267. int length = 0;
  268. uint32_t key[4] = { 0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF };
  269. uint32_t code[4];
  270. //static uint8_t k;
  271. static unsigned char curDevice;
  272. if (master->bus_status == 0xFF) {
  273. //复位总线,发送指定个数的包
  274. if (master->count > 0) {
  275. if(master->count % 2)
  276. master->data_out[0] = 0x80;//广播ID
  277. else
  278. master->data_out[0] = 0x81;//广播ID
  279. master->data_out[1] = 0xAA;//复位命令
  280. master->data_out[2] = 4;//帧长度,不包含校验
  281. master->data_out[3] = LifeSignal++;//生命信号
  282. length = nodelink_encode(&dst[0], &(master->data_out[0]), 4);
  283. res = length;
  284. master->count--;
  285. }
  286. else {
  287. //进入配置ID模式
  288. nodelink_master_begin_scan(master);
  289. sw_timer_start(&decode_err_timer, 0, 500000);
  290. BeginRecvFlg=1;
  291. }
  292. }
  293. else if (master->bus_status == 0xF0) {
  294. //定向分配ID
  295. switch (master->step) {
  296. case 0:
  297. //需要先执行设置设备进入ID配置模式
  298. //if (master->count > 0) {
  299. code[0] = (master->code[0]) | (master->code[1] << 8) | (master->code[2] << 16) | (master->code[3] << 24);
  300. code[1] = (master->code[4]) | (master->code[5] << 8) | (master->code[6] << 16) | (master->code[7] << 24);
  301. code[2] = (master->code[8]) | (master->code[9] << 8) | (master->code[10] << 16) | (master->code[11] << 24);
  302. code[3] = (master->code[12]) | (master->code[13] << 8) | (master->code[14] << 16) | (master->code[15] << 24);
  303. xxtea_uint_encrypt(code, 4, key);
  304. master->data_out[0] = 0x80;//广播ID
  305. master->data_out[1] = 0xA0;//验证CODE命令
  306. memcpy(&master->data_out[2], code, 16);
  307. master->data_out[18] = 20;//帧长度
  308. master->data_out[19] = LifeSignal++;//生命信号
  309. res = nodelink_encode(dst, &(master->data_out[0]), 20);
  310. //master->count--;
  311. //}
  312. break;
  313. case 1:
  314. code[0] = (master->code[0]) | (master->code[1] << 8) | (master->code[2] << 16) | (master->code[3] << 24);
  315. code[1] = (master->code[4]) | (master->code[5] << 8) | (master->code[6] << 16) | (master->code[7] << 24);
  316. code[2] = (master->code[8]) | (master->code[9] << 8) | (master->code[10] << 16) | (master->code[11] << 24);
  317. code[3] = (master->code[12]) | (master->code[13] << 8) | (master->code[14] << 16) | (master->code[15] << 24);
  318. xxtea_uint_encrypt(code, 4, key);
  319. master->data_out[0] = 0x80;//广播ID
  320. master->data_out[1] = 0xA0;//验证CODE命令
  321. memcpy(&master->data_out[2], code, 16);
  322. master->data_out[18] = 20;//帧长度
  323. master->data_out[19] = LifeSignal++;//生命信号
  324. res = nodelink_encode(dst, &(master->data_out[0]), 20);
  325. curDevice=1;
  326. break;
  327. case 2:
  328. //配置成功通知
  329. if (master->count > 0) {
  330. master->data_out[0] = master->device[curDevice].id;
  331. master->data_out[1] = 0xA2;//通知命令
  332. master->data_out[2] = master->device[curDevice].uniqueid;//发送唯一码
  333. master->data_out[3] = 5;//帧长度
  334. master->data_out[4] = LifeSignal++;//生命信号
  335. res = nodelink_encode(dst, &(master->data_out[0]), 5);
  336. master->count--;
  337. }
  338. else
  339. {
  340. if(curDevice<master->dev_count)
  341. {
  342. curDevice++;
  343. master->count=50;
  344. }
  345. else
  346. {
  347. curDevice=1;
  348. master->count = 0;
  349. //进入工作模式
  350. nodelink_master_begin_working(master);
  351. }
  352. }
  353. break;
  354. }
  355. }
  356. else if (master->bus_status == 0xF1) {
  357. //执行周期数据
  358. if(curDevice<master->dev_count)
  359. {
  360. curDevice++;
  361. }
  362. else
  363. {
  364. curDevice=1;
  365. }
  366. //让设备执行输入
  367. if (master->device[curDevice].ops != 0) {
  368. length = master->device[curDevice].ops->on_send(&master->device[curDevice], &(master->data_out[0]));
  369. if (length > 0) {
  370. master->data_out[length] = length+2;//帧长度
  371. length++;
  372. master->data_out[length] = LifeSignal++;//生命信号
  373. length++;
  374. length = nodelink_encode(&dst[0], &(master->data_out[0]), length);
  375. res = length;
  376. master->device[curDevice].txcount++;
  377. }
  378. }
  379. }
  380. return res;
  381. }