setting.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. #include "main.h"
  2. #include "flash.h"
  3. #include "setting.h"
  4. #define EEROM_SETTING_ADDRESS(x) FLASH_SETTING_ADDRESS((x)+(FLASH_EEROMBASE_ADDRESS))
  5. const stProductionInformation* pProductInfo = (const stProductionInformation*)(FLASH_SETTING_ADDRESS(INFORMATION_ADDRESS));
  6. const stRunStatus* pRunStatus = (const stRunStatus*)(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS));
  7. const stRunStatus* pRunStatus2 = (const stRunStatus*)(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS2));
  8. const stStationConfig* pDwsConfig = (const stStationConfig*)(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS));
  9. const stStationConfig* pDwsConfigBak = (const stStationConfig*)(FLASH_SETTING_ADDRESS(BAK_DW1000_ADDRESS));
  10. void asciiDotAddressTouint32(uint8_t *hexAddr, int8_t *asciiDotAddress)
  11. {
  12. uint8_t i;
  13. // loop counter
  14. uint8_t cBufPos;
  15. // which char in the ASCII representation
  16. uint8_t byteNum;
  17. // which byte in the 32BitHexAddress
  18. uint8_t cBuf[4];
  19. // character buffer
  20. byteNum = 3;
  21. cBufPos = 0;
  22. for (i = 0; i < strlen((char *)asciiDotAddress); i++) {
  23. // this will take care of the first 3 octets
  24. if (asciiDotAddress[i] == '.')
  25. {
  26. // we are at the end of the address octet
  27. cBuf[cBufPos] = 0;
  28. // terminate the string
  29. cBufPos = 0;
  30. // reset for the next char
  31. hexAddr[byteNum--] = (uint8_t)atoi((char *)cBuf);
  32. // convert the strint to an integer
  33. }
  34. else {
  35. cBuf[cBufPos++] = asciiDotAddress[i];
  36. }
  37. }
  38. // handle the last octet
  39. // // we are at the end of the string with no .
  40. cBuf[cBufPos] = asciiDotAddress[i];
  41. // terminate the string
  42. hexAddr[byteNum] = (uint8_t)atoi((char *)cBuf);
  43. // convert the strint to an integer
  44. }
  45. int isSettingEmpty(void)
  46. {
  47. if ((0xffff == pDwsConfig->address && 0xffff == pDwsConfig->panID) || (0 == pDwsConfig->address && 0 == pDwsConfig->panID))
  48. {
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. int isSettingError(void)
  54. {
  55. if ((0 == pDwsConfig->address && 0 == pDwsConfig->panID))
  56. {
  57. return 1;
  58. }
  59. if ((0 == ethServer.sip[0]) && (0 == ethServer.sip[1]) && (0 == ethServer.sip[2]) && (0 == ethServer.sip[3]))
  60. {
  61. return 1;
  62. }
  63. if ((0xFF == ethServer.sip[0]) && (0xFF == ethServer.sip[1]) && (0xFF == ethServer.sip[2]) && (0xFF == ethServer.sip[3]))
  64. {
  65. return 1;
  66. }
  67. if ((0 == ethServer.port) || (0xFF == ethServer.port) || (0xFFFF == ethServer.port))
  68. {
  69. return 1;
  70. }
  71. if ((0 == ethConfig.lip[0]) && (0 == ethConfig.lip[1]) && (0 == ethConfig.lip[2]) && (0 == ethConfig.lip[3]))
  72. {
  73. return 1;
  74. }
  75. if ((0xFF == ethConfig.lip[0]) && (0xFF == ethConfig.lip[1]) && (0xFF == ethConfig.lip[2]) && (0xFF == ethConfig.lip[3]))
  76. {
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. int isSettingBakError(void)
  82. {
  83. if ((0 == pDwsConfigBak->address && 0 == pDwsConfigBak->panID))
  84. {
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. int isRunStatusEmpty(void)
  90. {
  91. return (0xff == pRunStatus->status[0]
  92. && 0xff == pRunStatus->status[1]
  93. && 0xff == pRunStatus->status[2]
  94. && 0xff == pRunStatus->status[3]);
  95. }
  96. int isRunStatusHold(void)
  97. {
  98. return (1 == pRunStatus->status[0] || 1 == pRunStatus2->status[0]);
  99. }
  100. int SaveInformation(const uint8_t* buf, int len)
  101. {
  102. if(len < sizeof(stProductionInformation))
  103. {
  104. return -1;
  105. }
  106. int ret=0;
  107. if(len > INFORMATION_LENGTH)
  108. {
  109. len = INFORMATION_LENGTH;
  110. }
  111. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  112. memcpy(debugBuffer, buf, len);
  113. ret = ProgramFlashPage(FLASH_SETTING_ADDRESS(INFORMATION_ADDRESS), debugBuffer, len);
  114. return ret;
  115. }
  116. int SaveRunStatus(uint8_t pos, uint8_t val)
  117. {
  118. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  119. memcpy(debugBuffer, pRunStatus, RUN_STATUS_LENGTH);
  120. debugBuffer[pos] = val;
  121. return ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS), debugBuffer, RUN_STATUS_LENGTH);
  122. }
  123. int SaveRunStatus2(uint8_t pos, uint8_t val)
  124. {
  125. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  126. memcpy(debugBuffer, pRunStatus2, RUN_STATUS_LENGTH);
  127. debugBuffer[pos] = val;
  128. return ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS2), debugBuffer, RUN_STATUS_LENGTH);
  129. }
  130. int SaveDwmConfig(const uint8_t* buf)
  131. {
  132. int ret = 0;
  133. //get settings
  134. dwsConfig.panID = buf[0] + buf[1]*256;
  135. dwsConfig.address = buf[2] + buf[3]*256;
  136. uint32_t u = buf[4] + (buf[5]<<8) + (buf[6]<<16) + (buf[7]<<24);
  137. memcpy(&dwsConfig.signal, &u, 4);
  138. dwsConfig.fixPower = buf[8] + ((uint32_t)buf[9] << 8) + ((uint32_t)buf[10] << 16) + ((uint32_t)buf[11] << 24);
  139. debugLen = 0;
  140. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  141. //DW1000 config
  142. memcpy(debugBuffer, &dwsConfig, EEROM_DW1000_LENGTH);
  143. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS), debugBuffer, EEROM_DW1000_LENGTH);
  144. return ret;
  145. }
  146. int SaveEthernetServerConfig(const uint8_t* buf)
  147. {
  148. int ret = 0;
  149. stServerAddress tmpServer;
  150. memcpy(&tmpServer, &ethServer, sizeof(stServerAddress));
  151. //get settings
  152. tmpServer.sip[0] = buf[0];
  153. tmpServer.sip[1] = buf[1];
  154. tmpServer.sip[2] = buf[2];
  155. tmpServer.sip[3] = buf[3];
  156. tmpServer.port = buf[4];
  157. tmpServer.port += buf[5]*256;
  158. debugLen = 0;
  159. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  160. //eth server
  161. memcpy(debugBuffer, &tmpServer, EEROM_ETHSVR_LENGTH);
  162. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHSVR_ADDRESS), debugBuffer, EEROM_ETHSVR_LENGTH);
  163. return ret;
  164. }
  165. int SaveEthernetDbgServerConfig(const uint8_t* buf)
  166. {
  167. int ret = 0;
  168. stServerAddress tmpServer;
  169. memcpy(&tmpServer, &ethDbgServer, sizeof(stServerAddress));
  170. //get settings
  171. tmpServer.sip[0] = buf[0];
  172. tmpServer.sip[1] = buf[1];
  173. tmpServer.sip[2] = buf[2];
  174. tmpServer.sip[3] = buf[3];
  175. tmpServer.port = buf[4];
  176. tmpServer.port += buf[5]*256;
  177. debugLen = 0;
  178. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  179. //eth dbg server
  180. memcpy(debugBuffer, &tmpServer, EEROM_ETHDBGSVR_LENGTH);
  181. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHDBGSVR_ADDRESS), debugBuffer, EEROM_ETHDBGSVR_LENGTH);
  182. return ret;
  183. }
  184. int SaveEthernetLocalConfig(const uint8_t* buf)
  185. {
  186. int ret = 0;
  187. stEthernetConfig tmpCfg;
  188. memcpy(&tmpCfg, &ethConfig, sizeof(stEthernetConfig));
  189. tmpCfg.state = buf[0];
  190. tmpCfg.extMode = buf[1];
  191. tmpCfg.lip[0] = buf[2];
  192. tmpCfg.lip[1] = buf[3];
  193. tmpCfg.lip[2] = buf[4];
  194. tmpCfg.lip[3] = buf[5];
  195. tmpCfg.sub[0] = buf[6];
  196. tmpCfg.sub[1] = buf[7];
  197. tmpCfg.sub[2] = buf[8];
  198. tmpCfg.sub[3] = buf[9];
  199. tmpCfg.gw[0] = buf[10];
  200. tmpCfg.gw[1] = buf[11];
  201. tmpCfg.gw[2] = buf[12];
  202. tmpCfg.gw[3] = buf[13];
  203. debugLen = 0;
  204. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  205. //DW1000 config
  206. memcpy(debugBuffer, &tmpCfg, EEROM_ETHCFG_LENGTH);
  207. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHCFG_ADDRESS), debugBuffer, EEROM_ETHCFG_LENGTH);
  208. return ret;
  209. }
  210. int SaveEthernetMacAddressConfig(const uint8_t* buf)
  211. {
  212. int ret = 0;
  213. ethConfig.mac[0] = buf[0];
  214. ethConfig.mac[1] = buf[1];
  215. ethConfig.mac[2] = buf[2];
  216. ethConfig.mac[3] = buf[3];
  217. ethConfig.mac[4] = buf[4];
  218. ethConfig.mac[5] = buf[5];
  219. debugLen = 0;
  220. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  221. //DW1000 config
  222. memcpy(debugBuffer, &ethConfig, EEROM_ETHCFG_LENGTH);
  223. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHCFG_ADDRESS), debugBuffer, EEROM_ETHCFG_LENGTH);
  224. return ret;
  225. }
  226. int SaveWifiApNameConfig(const uint8_t* buf, uint8_t len)
  227. {
  228. int ret = 0;
  229. if(len > EEROM_WIFIAP_LENGTH + EEROM_WIFICONFIG_LENGTH)
  230. {
  231. len = EEROM_WIFIAP_LENGTH + EEROM_WIFICONFIG_LENGTH;
  232. }
  233. len -= EEROM_WIFICONFIG_LENGTH;
  234. //config len=4
  235. stWifiConfig tmpWifiConfig;
  236. tmpWifiConfig.type = buf[0];//
  237. tmpWifiConfig.mode = buf[1];//
  238. tmpWifiConfig.rfpower = buf[2];//
  239. tmpWifiConfig.dhcp = buf[3];//
  240. memset((void*)tmpWifiConfig.name,0,WIFIAPSSIDLEN);
  241. strncpy((char*)tmpWifiConfig.name, (const char*)(buf + 4),len);
  242. //save wifi password below
  243. strncpy((char*)tmpWifiConfig.password, (const char*)(buf + 4 + 32),len);
  244. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  245. memcpy(debugBuffer, &tmpWifiConfig, EEROM_WIFICONFIG_LENGTH);
  246. debugLen = EEROM_WIFIAP_ADDRESS - EEROM_WIFICONFIG_ADDRESS;
  247. memcpy(debugBuffer + debugLen, tmpWifiConfig.name, EEROM_WIFIAP_LENGTH);
  248. debugLen = EEROM_WIFIAP_ADDRESS + EEROM_WIFIAP_LENGTH - EEROM_WIFICONFIG_ADDRESS;
  249. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFICONFIG_ADDRESS), debugBuffer, debugLen);
  250. //save wifi password below
  251. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  252. memcpy(debugBuffer, tmpWifiConfig.password, EEROM_WIFIPSWD_LENGTH);
  253. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFIPSWD_ADDRESS), debugBuffer, EEROM_WIFIPSWD_LENGTH);
  254. return ret;
  255. }
  256. int SaveWifiApPasswordConfig(const uint8_t* buf, uint8_t len)
  257. {
  258. int ret = 0;
  259. if(len > EEROM_WIFIPSWD_LENGTH) len = EEROM_WIFIPSWD_LENGTH;
  260. memset(wifiConfig.password,0,WIFIAPPSWDLEN);
  261. strncpy((char*)wifiConfig.password, (const char*)buf,len);
  262. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  263. memcpy(debugBuffer, wifiConfig.password, EEROM_WIFIPSWD_LENGTH);
  264. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFIPSWD_ADDRESS), debugBuffer, EEROM_WIFIPSWD_LENGTH);
  265. return ret;
  266. }
  267. int SaveWifiServerConfig(const uint8_t* buf)
  268. {
  269. int ret = 0;
  270. //get settings
  271. memset(&wifiServer,0,sizeof(wifiServer));
  272. ret = sprintf((char*)debugBuffer,"%d.%d.%d.%d",buf[0],buf[1],buf[2],buf[3]);
  273. strncpy((char*)wifiServer.ip,(const char*)debugBuffer,ret);
  274. ret = sprintf((char*)debugBuffer,"%d",(buf[4] + buf[5]*256));
  275. strncpy((char*)wifiServer.port,(const char*)debugBuffer,ret);
  276. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  277. memcpy(debugBuffer, &wifiServer, EEROM_WIFISVR_LENGTH);
  278. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFISVR_ADDRESS), debugBuffer, EEROM_WIFISVR_LENGTH);
  279. return ret;
  280. }
  281. int SaveWifiNetworkConfig(const uint8_t* buf)
  282. {
  283. int ret = 0;
  284. memset(&wifiNetwork,0,sizeof(wifiNetwork));
  285. ret = sprintf((char*)debugBuffer, "%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]);
  286. strncpy((char*)wifiNetwork.ip, (const char*)debugBuffer, ret);
  287. ret = sprintf((char*)debugBuffer, "%d.%d.%d.%d", buf[4], buf[5], buf[6], buf[7]);
  288. strncpy((char*)wifiNetwork.subnetmask, (const char*)debugBuffer, ret);
  289. ret = sprintf((char*)debugBuffer, "%d.%d.%d.%d", buf[8], buf[9], buf[10], buf[11]);
  290. strncpy((char*)wifiNetwork.gateway, (const char*)debugBuffer, ret);
  291. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  292. memcpy(debugBuffer, &wifiNetwork, EEROM_WIFINET_LENGTH);
  293. ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFINET_ADDRESS), debugBuffer, EEROM_WIFINET_LENGTH);
  294. return ret;
  295. }
  296. void ReadConfigFromEEROM(void)
  297. {
  298. int len = 0;
  299. debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH;
  300. memcpy(debugBuffer, (void*)EEROM_SETTING_ADDRESS(0), debugLen);
  301. //DW1000 config
  302. len = sizeof(dwsConfig);//EEROM_DW1000_LENGTH
  303. memcpy(&dwsConfig, debugBuffer + EEROM_DW1000_ADDRESS, len);
  304. //Eth Server IP&Port
  305. len = sizeof(ethServer);//EEROM_ETHSVR_LENGTH
  306. memcpy(&ethServer, debugBuffer + EEROM_ETHSVR_ADDRESS, len);
  307. //Eth Server IP&Port
  308. len = sizeof(ethDbgServer);//EEROM_ETHDBGSVR_LENGTH
  309. memcpy(&ethDbgServer, debugBuffer + EEROM_ETHDBGSVR_ADDRESS, len);
  310. //EthConfig
  311. len = sizeof(ethConfig);//EEROM_ETHCFG_LENGTH
  312. memcpy(&ethConfig, debugBuffer + EEROM_ETHCFG_ADDRESS,len);
  313. //WIFI config
  314. memcpy(&wifiConfig, debugBuffer + EEROM_WIFICONFIG_ADDRESS , EEROM_WIFICONFIG_LENGTH);
  315. //WIFI AP SSID
  316. memcpy(wifiConfig.name, debugBuffer + EEROM_WIFIAP_ADDRESS, EEROM_WIFIAP_LENGTH);
  317. //WIFI AP Password
  318. memcpy(wifiConfig.password, debugBuffer + EEROM_WIFIPSWD_ADDRESS, EEROM_WIFIPSWD_LENGTH);
  319. //WIFI Server IP&Port
  320. len = sizeof(wifiServer);//EEROM_WIFISVR_LENGTH
  321. memcpy(&wifiServer, debugBuffer + EEROM_WIFISVR_ADDRESS, len);
  322. //WIFI network
  323. len = sizeof(wifiNetwork);//EEROM_WIFINET_LENGTH
  324. memcpy(&wifiNetwork, debugBuffer + EEROM_WIFINET_ADDRESS, len);
  325. }
  326. void ReadBakConfigFromEEROM(void)
  327. {
  328. int len = 0;
  329. debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH;
  330. memcpy(debugBuffer, (void*)FLASH_SETTING_ADDRESS(0), debugLen);
  331. //DW1000 config
  332. len = sizeof(dwsConfig);//EEROM_DW1000_LENGTH
  333. memcpy(&dwsConfig, debugBuffer + EEROM_DW1000_ADDRESS, len);
  334. //Eth Server IP&Port
  335. len = sizeof(ethServer);//EEROM_ETHSVR_LENGTH
  336. memcpy(&ethServer, debugBuffer + EEROM_ETHSVR_ADDRESS, len);
  337. //Eth Server IP&Port
  338. len = sizeof(ethDbgServer);//EEROM_ETHDBGSVR_LENGTH
  339. memcpy(&ethDbgServer, debugBuffer + EEROM_ETHDBGSVR_ADDRESS, len);
  340. //EthConfig
  341. len = sizeof(ethConfig);//EEROM_ETHCFG_LENGTH
  342. memcpy(&ethConfig, debugBuffer + EEROM_ETHCFG_ADDRESS,len);
  343. //WIFI config
  344. memcpy(&wifiConfig, debugBuffer + EEROM_WIFICONFIG_ADDRESS , EEROM_WIFICONFIG_LENGTH);
  345. //WIFI AP SSID
  346. memcpy(wifiConfig.name, debugBuffer + EEROM_WIFIAP_ADDRESS, EEROM_WIFIAP_LENGTH);
  347. //WIFI AP Password
  348. memcpy(wifiConfig.password, debugBuffer + EEROM_WIFIPSWD_ADDRESS, EEROM_WIFIPSWD_LENGTH);
  349. //WIFI Server IP&Port
  350. len = sizeof(wifiServer);//EEROM_WIFISVR_LENGTH
  351. memcpy(&wifiServer, debugBuffer + EEROM_WIFISVR_ADDRESS, len);
  352. //WIFI network
  353. len = sizeof(wifiNetwork);//EEROM_WIFINET_LENGTH
  354. memcpy(&wifiNetwork, debugBuffer + EEROM_WIFINET_ADDRESS, len);
  355. }
  356. extern void DWM_SetDefault(void);
  357. extern void Wifi_SetDefault(void);
  358. extern void Ethernet_SetDefault(void);
  359. void WriteDefaultConfigToEEROM(void)
  360. {
  361. DWM_SetDefault();
  362. Ethernet_SetDefault();
  363. Wifi_SetDefault();
  364. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  365. debugLen = 0;
  366. //DW1000 config
  367. memcpy(debugBuffer + EEROM_DW1000_ADDRESS, &dwsConfig, EEROM_DW1000_LENGTH);
  368. //Eth Server IP&Port
  369. memcpy(debugBuffer + EEROM_ETHSVR_ADDRESS, &ethServer, EEROM_ETHSVR_LENGTH);
  370. //Eth DBG Server IP&Port
  371. memcpy(debugBuffer + EEROM_ETHDBGSVR_ADDRESS, &ethDbgServer, EEROM_ETHDBGSVR_LENGTH);
  372. //EthConfig
  373. memcpy(debugBuffer + EEROM_ETHCFG_ADDRESS,&ethConfig, EEROM_ETHCFG_LENGTH);
  374. //WIFI config
  375. memcpy(debugBuffer + EEROM_WIFICONFIG_ADDRESS , &wifiConfig, EEROM_WIFICONFIG_LENGTH);
  376. //WIFI AP SSID
  377. memcpy(debugBuffer + EEROM_WIFIAP_ADDRESS, wifiConfig.name, EEROM_WIFIAP_LENGTH);
  378. //WIFI AP Password
  379. memcpy(debugBuffer + EEROM_WIFIPSWD_ADDRESS, wifiConfig.password, EEROM_WIFIPSWD_LENGTH);
  380. //WIFI Server IP&Port
  381. memcpy(debugBuffer + EEROM_WIFISVR_ADDRESS, &wifiServer, EEROM_WIFISVR_LENGTH);
  382. //WIFI network
  383. memcpy(debugBuffer + EEROM_WIFINET_ADDRESS, &wifiNetwork, EEROM_WIFINET_LENGTH);
  384. debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH - EEROM_DW1000_ADDRESS;
  385. int retval = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS), debugBuffer + EEROM_DW1000_ADDRESS, debugLen);
  386. }
  387. void WriteDefaultRunStatusToEEROM(void)
  388. {
  389. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  390. memcpy(debugBuffer, pRunStatus, RUN_STATUS_LENGTH);
  391. memcpy(debugBuffer, pRunStatus2, RUN_STATUS_LENGTH);
  392. ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS), debugBuffer, RUN_STATUS_LENGTH);
  393. ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS2), debugBuffer, RUN_STATUS_LENGTH);
  394. }
  395. void WriteConfigToEEROM(void)
  396. {
  397. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  398. debugLen = 0;
  399. //DW1000 config
  400. memcpy(debugBuffer + EEROM_DW1000_ADDRESS, &dwsConfig, EEROM_DW1000_LENGTH);
  401. //Eth Server IP&Port
  402. memcpy(debugBuffer + EEROM_ETHSVR_ADDRESS, &ethServer, EEROM_ETHSVR_LENGTH);
  403. //Eth DBG Server IP&Port
  404. memcpy(debugBuffer + EEROM_ETHDBGSVR_ADDRESS, &ethDbgServer, EEROM_ETHDBGSVR_LENGTH);
  405. //EthConfig
  406. memcpy(debugBuffer + EEROM_ETHCFG_ADDRESS,&ethConfig, EEROM_ETHCFG_LENGTH);
  407. //WIFI config
  408. memcpy(debugBuffer + EEROM_WIFICONFIG_ADDRESS , &wifiConfig, EEROM_WIFICONFIG_LENGTH);
  409. //WIFI AP SSID
  410. memcpy(debugBuffer + EEROM_WIFIAP_ADDRESS, wifiConfig.name, EEROM_WIFIAP_LENGTH);
  411. //WIFI AP Password
  412. memcpy(debugBuffer + EEROM_WIFIPSWD_ADDRESS, wifiConfig.password, EEROM_WIFIPSWD_LENGTH);
  413. //WIFI Server IP&Port
  414. memcpy(debugBuffer + EEROM_WIFISVR_ADDRESS, &wifiServer, EEROM_WIFISVR_LENGTH);
  415. //WIFI network
  416. memcpy(debugBuffer + EEROM_WIFINET_ADDRESS, &wifiNetwork, EEROM_WIFINET_LENGTH);
  417. debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH - EEROM_DW1000_ADDRESS;
  418. int retval = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS), debugBuffer + EEROM_DW1000_ADDRESS, debugLen);
  419. }
  420. void WriteBakConfigToEEROM(void)
  421. {
  422. memset(debugBuffer, 0, DEBUG_BUF_LEN );
  423. debugLen = 0;
  424. //DW1000 config
  425. memcpy(debugBuffer + EEROM_DW1000_ADDRESS, &dwsConfig, EEROM_DW1000_LENGTH);
  426. //Eth Server IP&Port
  427. memcpy(debugBuffer + EEROM_ETHSVR_ADDRESS, &ethServer, EEROM_ETHSVR_LENGTH);
  428. //Eth DBG Server IP&Port
  429. memcpy(debugBuffer + EEROM_ETHDBGSVR_ADDRESS, &ethDbgServer, EEROM_ETHDBGSVR_LENGTH);
  430. //EthConfig
  431. memcpy(debugBuffer + EEROM_ETHCFG_ADDRESS,&ethConfig, EEROM_ETHCFG_LENGTH);
  432. //WIFI config
  433. memcpy(debugBuffer + EEROM_WIFICONFIG_ADDRESS , &wifiConfig, EEROM_WIFICONFIG_LENGTH);
  434. //WIFI AP SSID
  435. memcpy(debugBuffer + EEROM_WIFIAP_ADDRESS, wifiConfig.name, EEROM_WIFIAP_LENGTH);
  436. //WIFI AP Password
  437. memcpy(debugBuffer + EEROM_WIFIPSWD_ADDRESS, wifiConfig.password, EEROM_WIFIPSWD_LENGTH);
  438. //WIFI Server IP&Port
  439. memcpy(debugBuffer + EEROM_WIFISVR_ADDRESS, &wifiServer, EEROM_WIFISVR_LENGTH);
  440. //WIFI network
  441. memcpy(debugBuffer + EEROM_WIFINET_ADDRESS, &wifiNetwork, EEROM_WIFINET_LENGTH);
  442. debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH - EEROM_DW1000_ADDRESS;
  443. int retval = ProgramFlashPage(FLASH_SETTING_ADDRESS(BAK_DW1000_ADDRESS), debugBuffer + EEROM_DW1000_ADDRESS, debugLen);
  444. }
  445. int GetInformation(uint8_t* retbuf, uint32_t* len)
  446. {
  447. int size = sizeof(pProductInfo->serialID);
  448. memcpy(retbuf, pProductInfo->serialID, size);
  449. #if defined(STM32F405_BOOTLOADER)
  450. retbuf[size++] = BSP_BL_VERSION() & 0xff;
  451. retbuf[size++] = (BSP_BL_VERSION()>>8)&0xff;
  452. retbuf[size++] = (BSP_BL_VERSION()>>16)&0xff;
  453. retbuf[size++] = (BSP_BL_VERSION()>>24)&0xff;
  454. #else
  455. retbuf[size++] = BSP_APP_VERSION() & 0xff;
  456. retbuf[size++] = (BSP_APP_VERSION()>>8)&0xff;
  457. retbuf[size++] = (BSP_APP_VERSION()>>16)&0xff;
  458. retbuf[size++] = (BSP_APP_VERSION()>>24)&0xff;
  459. #endif
  460. *len = size;
  461. return 0;
  462. }
  463. uint8_t GetRunStatus(uint8_t pos)
  464. {
  465. if (pos >=0 && pos < RUN_STATUS_LENGTH)
  466. {
  467. return pRunStatus->status[pos];
  468. }
  469. return 0;
  470. }
  471. int GetTimeSetResult(uint8_t* retbuf, uint32_t* len)
  472. {
  473. retbuf[0] = 1;
  474. retbuf[0] = 1;
  475. *len = 2;
  476. return 0;
  477. }
  478. int GetCurrentTime(uint8_t* retbuf, uint32_t* len)
  479. {
  480. retbuf[0] = stDateTime.year;
  481. retbuf[1] = stDateTime.month;
  482. retbuf[2] = stDateTime.day;
  483. retbuf[3] = stDateTime.hour;
  484. retbuf[4] = stDateTime.minute;
  485. retbuf[5] = stDateTime.second;
  486. *len = 6;
  487. return 0;
  488. }
  489. int GetRangeConfiguration(uint8_t* retbuf, uint32_t* len)
  490. {
  491. int index = 0;
  492. //get settings
  493. retbuf[index++] = (uint8_t)(dwsConfig.panID);
  494. retbuf[index++] = (uint8_t)(dwsConfig.panID>>8);
  495. retbuf[index++] = (uint8_t)(dwsConfig.address);
  496. retbuf[index++] = (uint8_t)(dwsConfig.address>>8);
  497. uint32_t u = 0;
  498. memcpy(&u, &dwsConfig.signal, 4);
  499. retbuf[index++] = (uint8_t)(u);
  500. retbuf[index++] = (uint8_t)(u>>8);
  501. retbuf[index++] = (uint8_t)(u>>16);
  502. retbuf[index++] = (uint8_t)(u>>24);
  503. retbuf[index++] = (uint8_t)(dwsConfig.fixPower & 0xff);
  504. retbuf[index++] = (uint8_t)(dwsConfig.fixPower >> 8);
  505. retbuf[index++] = (uint8_t)(dwsConfig.fixPower >> 16);
  506. retbuf[index++] = (uint8_t)(dwsConfig.fixPower >> 24);
  507. *len = index;
  508. return 0;
  509. }
  510. int GetEthernetServer(uint8_t* retbuf, uint32_t* len)
  511. {
  512. retbuf[0] = ethServer.sip[0];
  513. retbuf[1] = ethServer.sip[1];
  514. retbuf[2] = ethServer.sip[2];
  515. retbuf[3] = ethServer.sip[3];
  516. retbuf[4] = ethServer.port & 0xff;
  517. retbuf[5] = (ethServer.port>>8) & 0xff;
  518. *len = 6;
  519. return 0;
  520. }
  521. int GetEthernetDbgServer(uint8_t* retbuf, uint32_t* len)
  522. {
  523. retbuf[0] = ethDbgServer.sip[0];
  524. retbuf[1] = ethDbgServer.sip[1];
  525. retbuf[2] = ethDbgServer.sip[2];
  526. retbuf[3] = ethDbgServer.sip[3];
  527. retbuf[4] = ethDbgServer.port & 0xff;
  528. retbuf[5] = (ethDbgServer.port>>8) & 0xff;
  529. *len = 6;
  530. return 0;
  531. }
  532. int GetEthernetLocal(uint8_t* retbuf, uint32_t* len)
  533. {
  534. retbuf[0] = ethConfig.state;
  535. retbuf[1] = ethConfig.extMode;
  536. retbuf[2] = ethConfig.lip[0];
  537. retbuf[3] = ethConfig.lip[1];
  538. retbuf[4] = ethConfig.lip[2];
  539. retbuf[5] = ethConfig.lip[3];
  540. retbuf[6] = ethConfig.sub[0];
  541. retbuf[7] = ethConfig.sub[1];
  542. retbuf[8] = ethConfig.sub[2];
  543. retbuf[9] = ethConfig.sub[3];
  544. retbuf[10] = ethConfig.gw[0];
  545. retbuf[11] = ethConfig.gw[1];
  546. retbuf[12] = ethConfig.gw[2];
  547. retbuf[13] = ethConfig.gw[3];
  548. *len = 14;
  549. return 0;
  550. }
  551. int GetEthernetMAC(uint8_t* retbuf, uint32_t* len)
  552. {
  553. retbuf[0] = ethConfig.mac[0];
  554. retbuf[1] = ethConfig.mac[1];
  555. retbuf[2] = ethConfig.mac[2];
  556. retbuf[3] = ethConfig.mac[3];
  557. retbuf[4] = ethConfig.mac[4];
  558. retbuf[5] = ethConfig.mac[5];
  559. *len = 6;
  560. return 0;
  561. }
  562. int GetWifiConfigSSID(uint8_t* retbuf, uint32_t* len)
  563. {
  564. int index = 0;
  565. //config len=4
  566. retbuf[index++] = wifiConfig.type;//
  567. retbuf[index++] = wifiConfig.mode;//
  568. retbuf[index++] = wifiConfig.rfpower;//
  569. retbuf[index++] = wifiConfig.dhcp;//
  570. memcpy((void*)(retbuf+index), (const void*)wifiConfig.name, WIFIAPSSIDLEN);
  571. index += WIFIAPSSIDLEN;
  572. memcpy((void*)(retbuf+index), (const void*)wifiConfig.password, WIFIAPPSWDLEN);
  573. index += WIFIAPPSWDLEN;
  574. *len = index;
  575. return 0;
  576. }
  577. int GetWifiPassword(uint8_t* retbuf, uint32_t* len)
  578. {
  579. memcpy((void*)retbuf, wifiConfig.password, WIFIAPPSWDLEN);
  580. *len = WIFIAPPSWDLEN;
  581. return 0;
  582. }
  583. int GetWifiServer(uint8_t* retbuf, uint32_t* len)
  584. {
  585. int index = 0;
  586. uConverterUint2Bytes converter;
  587. asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiServer.ip);
  588. //config ip=4
  589. retbuf[index++] = converter.u8Data[3];//
  590. retbuf[index++] = converter.u8Data[2];//
  591. retbuf[index++] = converter.u8Data[1];//
  592. retbuf[index++] = converter.u8Data[0];//
  593. converter.u32Data = atoi((const char*)wifiServer.port);
  594. //config port=2
  595. retbuf[index++] = converter.u8Data[0];//
  596. retbuf[index++] = converter.u8Data[1];//
  597. *len = index;
  598. return 0;
  599. }
  600. int GetWifiLocal(uint8_t* retbuf, uint32_t* len)
  601. {
  602. int index = 0;
  603. uConverterUint2Bytes converter;
  604. asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiNetwork.ip);
  605. //config ip=4
  606. retbuf[index++] = converter.u8Data[3];//
  607. retbuf[index++] = converter.u8Data[2];//
  608. retbuf[index++] = converter.u8Data[1];//
  609. retbuf[index++] = converter.u8Data[0];//
  610. asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiNetwork.subnetmask);
  611. //config subnetmask=4
  612. retbuf[index++] = converter.u8Data[3];//
  613. retbuf[index++] = converter.u8Data[2];//
  614. retbuf[index++] = converter.u8Data[1];//
  615. retbuf[index++] = converter.u8Data[0];//
  616. asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiNetwork.gateway);
  617. //config gateway=4
  618. retbuf[index++] = converter.u8Data[3];//
  619. retbuf[index++] = converter.u8Data[2];//
  620. retbuf[index++] = converter.u8Data[1];//
  621. retbuf[index++] = converter.u8Data[0];//
  622. *len = index;
  623. return 0;
  624. }
  625. int GetResetSignResponse(uint8_t* retbuf, uint32_t* len)
  626. {
  627. int index = 0;
  628. retbuf[index++] = (uint8_t)(dwsConfig.address | GATEWAY_ANCHOR_ADDR);
  629. retbuf[index++] = (uint8_t)((dwsConfig.address | GATEWAY_ANCHOR_ADDR)>>8);
  630. retbuf[index++] = 0xff;//
  631. retbuf[index++] = 0xff;//
  632. retbuf[index++] = 0xff;//
  633. retbuf[index++] = 0xff;//
  634. *len = index;
  635. return 0;
  636. }
  637. int BuildResponse(uint32_t cmd, uint8_t* retbuf, uint32_t* retlen)
  638. {
  639. uint32_t datalen;
  640. uint32_t len;
  641. int ret;
  642. //Build response
  643. memset(retbuf, 0, MAX_MESSAGE_LEN);
  644. retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_FLAG;
  645. switch(cmd)
  646. {
  647. case CFG_COMMAND_GETINFORM:
  648. ret = GetInformation(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  649. break;
  650. case CFG_COMMAND_TIMESET:
  651. ret = GetTimeSetResult(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  652. break;
  653. case CFG_COMMAND_GETTIME:
  654. ret = GetCurrentTime(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  655. break;
  656. case CFG_COMMAND_GETCONFIGRANGE:
  657. ret = GetRangeConfiguration(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  658. break;
  659. case CFG_COMMAND_GETCONFIGETHDBGSERVER:
  660. ret = GetEthernetDbgServer(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  661. break;
  662. case CFG_COMMAND_GETCONFIGETHSERVER:
  663. ret = GetEthernetServer(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  664. break;
  665. case CFG_COMMAND_GETCONFIGETHLOCAL:
  666. ret = GetEthernetLocal(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  667. break;
  668. case CFG_COMMAND_GETCONFIGETHMAC:
  669. ret = GetEthernetMAC(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  670. break;
  671. case CFG_COMMAND_GETCONFIGAPSSID:
  672. ret = GetWifiConfigSSID(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  673. break;
  674. case CFG_COMMAND_GETCONFIGAPPSWD:
  675. ret = GetWifiPassword(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  676. break;
  677. case CFG_COMMAND_GETCONFIGWIFISERVER:
  678. ret = GetWifiServer(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  679. break;
  680. case CFG_COMMAND_GETCONFIGWIFILOCAL:
  681. ret = GetWifiLocal(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  682. break;
  683. case CFG_COMMAND_RESETSIGN:
  684. ret = GetResetSignResponse(retbuf + PROTOCAL_DATASTART_INDEX, &datalen);
  685. break;
  686. default:
  687. *retlen = 0;
  688. return -1;
  689. }
  690. len = datalen + PROTOCAL_CHECKSUM_LENGTH + 1;
  691. retbuf[PROTOCAL_LENLOW_INDEX] = len;
  692. retbuf[PROTOCAL_LENHIGH_INDEX] = len>>8;
  693. retbuf[PROTOCAL_CMD_INDEX] = cmd;
  694. uint32_t remainder = datalen % 8;
  695. if (remainder)
  696. {
  697. remainder = 8 - remainder;
  698. }
  699. while (remainder)
  700. {
  701. len++;
  702. datalen++;
  703. remainder--;
  704. }
  705. if (dwsConfig.signal.encrypt && CFG_COMMAND_RESETSIGN != cmd)
  706. {
  707. retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_ENCRYPT_FLAG;
  708. EncryptTEA(retbuf, PROTOCAL_DATASTART_INDEX, datalen);
  709. }
  710. retbuf[PROTOCAL_LENLOW_INDEX] = (uint8_t)len;
  711. retbuf[PROTOCAL_LENHIGH_INDEX] = (uint8_t)(len>>8);
  712. uint16_t checkval = CalculateCRC16(retbuf, datalen + PROTOCAL_HEAD_LENGTH);
  713. retbuf[datalen + PROTOCAL_HEAD_LENGTH] = checkval;
  714. retbuf[datalen + PROTOCAL_HEAD_LENGTH + 1] = checkval>>8;
  715. *retlen = datalen + PROTOCAL_HEAD_LENGTH + PROTOCAL_CHECKSUM_LENGTH;
  716. return ret;
  717. }
  718. int BuildSettingResponse(uint32_t cmd, int32_t status, uint8_t* retbuf, uint32_t* retlen)
  719. {
  720. int datalen = 2;
  721. int len = datalen + PROTOCAL_CHECKSUM_LENGTH + 1;
  722. //Build response
  723. memset(retbuf, 0, MAX_MESSAGE_LEN);
  724. retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_FLAG;
  725. retbuf[PROTOCAL_LENLOW_INDEX] = len;
  726. retbuf[PROTOCAL_LENHIGH_INDEX] = len>>8;
  727. retbuf[PROTOCAL_CMD_INDEX] = CFG_COMMAND_SETTINGRESPONES;
  728. retbuf[PROTOCAL_DATASTART_INDEX+0] = cmd;
  729. retbuf[PROTOCAL_DATASTART_INDEX+1] = status;
  730. uint32_t remainder = datalen % 8;
  731. if (remainder)
  732. {
  733. remainder = 8 - remainder;
  734. }
  735. while (remainder)
  736. {
  737. len++;
  738. datalen++;
  739. remainder--;
  740. }
  741. if (dwsConfig.signal.encrypt)
  742. {
  743. retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_ENCRYPT_FLAG;
  744. EncryptTEA(retbuf, PROTOCAL_DATASTART_INDEX, datalen);
  745. }
  746. retbuf[PROTOCAL_LENLOW_INDEX] = (uint8_t)len;
  747. retbuf[PROTOCAL_LENHIGH_INDEX] = (uint8_t)(len>>8);
  748. uint16_t checkval = CalculateCRC16(retbuf, datalen + PROTOCAL_HEAD_LENGTH);
  749. retbuf[datalen + PROTOCAL_HEAD_LENGTH] = checkval;
  750. retbuf[datalen + PROTOCAL_HEAD_LENGTH + 1] = checkval>>8;
  751. *retlen = datalen + PROTOCAL_HEAD_LENGTH + PROTOCAL_CHECKSUM_LENGTH;
  752. return 0;
  753. }