You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

282 regels
8.2 KiB

  1. #include "define.h"
  2. #include "MasterCtrlInterface.h"
  3. #ifdef USE_WINC1500
  4. #else
  5. #include "TCPIP_Stack/TCPIP.h"
  6. #endif
  7. #include "NetworkProtocol.h"
  8. #include "ProtocolDefs.h"
  9. #include "timer.h"
  10. #include <stdio.h>
  11. #include "ValveCtrl.h"
  12. #include "FlowMeter.h"
  13. #ifdef USE_WINC1500
  14. #else
  15. TCP_SOCKET MySocket;
  16. static BYTE MasterIP[] = "192.168.0.100";
  17. DWORD MasterPort = 2182;
  18. #endif
  19. int mConnectionState = MASTER_STATE_DISCONNECTED;
  20. int InitMasterCtrlIF()
  21. {
  22. ProtocolInit();
  23. TimerStart(MASTER_CONNECTION_TIMER,MASTER_RECONNECTION_TIMEOUT);
  24. return 1;
  25. }
  26. int ConnectToMasterCtrl()
  27. {
  28. #ifdef USE_WINC1500
  29. #else
  30. MySocket = TCPOpen((DWORD) (PTR_BASE) & MasterIP[0], TCP_OPEN_RAM_HOST, MasterPort, TCP_PURPOSE_GENERIC_TCP_CLIENT);
  31. // Abort operation if no TCP socket of type TCP_PURPOSE_GENERIC_TCP_CLIENT is available
  32. // If this ever happens, you need to go add one to TCPIPConfig.h
  33. if (MySocket == INVALID_SOCKET)
  34. {
  35. printf("Could not open socket to MasterCtrl\n");
  36. return 0;
  37. }
  38. printf("MasterCtrl Socket opened\n");
  39. #endif
  40. return 1;
  41. }
  42. void TickMasterCtrlInterface()
  43. {
  44. #ifdef USE_WINC1500
  45. #else
  46. WORD Pending = TCPIsGetReady(MySocket);
  47. if (Pending != 0)
  48. {
  49. // printf("Rx %d bytes\n",Pending);
  50. int i = 0;
  51. for(i = 0; i < Pending; i++)
  52. {
  53. BYTE Byte;
  54. if(TCPGet(MySocket,&Byte) == TRUE)
  55. {
  56. ProtocolAnalyzeNewData(Byte);
  57. }
  58. }
  59. }
  60. #ifdef CONNECT_DEVICE_TO_NETWORK
  61. if(mConnectionState == MASTER_STATE_DISCONNECTED)
  62. {
  63. if(IsTimerExpired(MASTER_CONNECTION_TIMER))
  64. {
  65. TimerStart(MASTER_CONNECTION_TIMER,MASTER_RESPONSE_TIMEOUT);
  66. mConnectionState = MASTER_STATE_CONNECTING;
  67. ConnectToMasterCtrl();
  68. }
  69. }
  70. else if(mConnectionState == MASTER_STATE_CONNECTING)
  71. {
  72. // if(IsTimerExpired(MASTER_CONNECTION_TIMER) == true) //
  73. // {
  74. // TCPClose(MySocket);
  75. // TimerStart(MASTER_CONNECTION_TIMER,MASTER_RESPONSE_TIMEOUT);
  76. // ConnectToMasterCtrl();
  77. // }
  78. }
  79. else if(mConnectionState == MASTER_STATE_CONNECTED)
  80. {
  81. // if(IsTimerExpired(MASTER_CONNECTION_TIMER))
  82. {
  83. if(TCPIsConnected(MySocket) == FALSE)
  84. {
  85. //we got disconnected...
  86. mConnectionState = MASTER_STATE_DISCONNECTED;
  87. TCPClose(MySocket);
  88. printf("Connection with MasterCtrl lost..\n");
  89. }
  90. TimerStart(MASTER_CONNECTION_TIMER,MASTER_RECONNECTION_TIMEOUT);
  91. }
  92. }
  93. #endif
  94. #endif
  95. }
  96. void NewMasterMessageReceived(char* Message)
  97. {
  98. // char Sender = Message[FRAME_SENDER_DEVICE_ID_INDEX];
  99. char Target = Message[FRAME_DEST_DEVICE_ID_INDEX];
  100. unsigned char Command = Message[FRAME_COMMAND_INDEX];
  101. char *Data = &Message[FRAME_DATA_INDEX];
  102. // printf("MasterMsgReceived %d \n",Command);
  103. if(Target == ID_ETHERNET_VIRTUAL)
  104. {
  105. switch(Command)
  106. {
  107. case ETH_NETWK_DEVICE_INFO_REQUEST:
  108. {
  109. char Data[2];
  110. Data[0] = ID_SPRINKLER_DEVICE;
  111. Data[1] = MY_DEVICE_ADDRESS; //Address
  112. SendFrame(ID_MASTER,MASTER_ADDRESS,ID_ETHERNET_VIRTUAL, ETH_NETWK_DEVICE_INFO_RESPONSE, Data,2,0);
  113. printf("Rx Device info request\n");
  114. // int FrameSize = -1;
  115. // unsigned char *FramePtr = ProtocolGetFrame(ID_MASTER,MASTER_ADDRESS,ID_ETHERNET_VIRTUAL, ETH_NETWK_DEVICE_INFO_RESPONSE, Data,2,0, &FrameSize);
  116. //
  117. // if(FrameSize > 0)
  118. // {
  119. // TCPPutArray(MySocket,FramePtr,FrameSize);
  120. // }
  121. break;
  122. }
  123. case ETH_NETWK_CONNECTION_REFUSED:
  124. {
  125. printf("Connection to server refused\n");
  126. break;
  127. }
  128. case ETH_NETWK_SET_DEVICE_INFO_ACK:
  129. {
  130. //Connected!
  131. mConnectionState = MASTER_STATE_CONNECTED;
  132. TimerStart(MASTER_CONNECTION_TIMER,MASTER_RECONNECTION_TIMEOUT);
  133. printf("Connected to server\n");
  134. break;
  135. }
  136. case ETH_NETWK_DEVICE_INFO_RESPONSE:
  137. default:
  138. {
  139. //error...
  140. break;
  141. }
  142. }
  143. }
  144. else if(Target == ID_SPRINKLER_DEVICE)
  145. {
  146. switch(Command)
  147. {
  148. case SPRINKLER_DEVICE_ACK:
  149. {
  150. break;
  151. }
  152. case SPRINKLER_DEVICE_STATUS_REQUEST:
  153. {
  154. unsigned char data[6];
  155. data[0] = (unsigned char)GetValveState();
  156. GetCurrentFlowBytes(&data[1]);
  157. data[3] = 0;
  158. data[4] = 0;
  159. data[5] = 0;
  160. SendFrame(ID_MASTER,MASTER_ADDRESS,ID_SPRINKLER_DEVICE, SPRINKLER_DEVICE_STATUS_RESPONSE, data,6,0);
  161. // printf("Status sent\n");
  162. break;
  163. }
  164. case SPRINKLER_DEVICE_SET_SPRINKLER_STATE_REQUEST:
  165. {
  166. unsigned char *MsgData;
  167. char data;
  168. MsgData = ProtocolMsgDataPtr();
  169. if(*MsgData == 1)
  170. {
  171. SetValve(VALVE_ON);
  172. data = 1;
  173. }
  174. else if(*MsgData == 1)
  175. {
  176. SetValve(VALVE_OFF);
  177. data = 1;
  178. }
  179. else
  180. {
  181. data = 0;
  182. }
  183. SendFrame(ID_MASTER,MASTER_ADDRESS,ID_SPRINKLER_DEVICE, SPRINKLER_DEVICE_SET_SPRINKLER_STATE_ACK,&data,1,0);
  184. break;
  185. }
  186. case SPRINKLER_DEVICE_GET_SPRINKLER_STATE_REQUEST:
  187. {
  188. unsigned char data;
  189. data = (unsigned char)GetValveState();
  190. SendFrame(ID_MASTER,MASTER_ADDRESS,ID_SPRINKLER_DEVICE, SPRINKLER_DEVICE_GET_SPRINKLER_STATE_RESPONSE,&data,1,0);
  191. break;
  192. }
  193. case SPRINKLER_DEVICE_GET_WATER_FLOW_REQUEST:
  194. {
  195. unsigned char data[2];
  196. GetCurrentFlowBytes(data);
  197. SendFrame(ID_MASTER,MASTER_ADDRESS,ID_SPRINKLER_DEVICE, SPRINKLER_DEVICE_GET_WATER_FLOW_RESPONSE,data,2,0);
  198. break;
  199. }
  200. case SPRINKLER_DEVICE_GET_MOISTURE_REQUEST:
  201. {
  202. break;
  203. }
  204. case SPRINKLER_DEVICE_SET_PROGRAM_REQUEST:
  205. {
  206. break;
  207. }
  208. case SPRINKLER_DEVICE_GET_PROGRAM_REQUEST:
  209. {
  210. break;
  211. }
  212. case SPRINKLER_DEVICE_SET_PARAMETERS_REQUEST:
  213. {
  214. break;
  215. }
  216. case SPRINKLER_DEVICE_GET_PARAMETERS_REQUEST:
  217. {
  218. break;
  219. }
  220. case SPRINKLER_DEVICE_STATUS_RESPONSE:
  221. case SPRINKLER_DEVICE_SET_SPRINKLER_STATE_ACK:
  222. case SPRINKLER_DEVICE_GET_SPRINKLER_STATE_RESPONSE:
  223. case SPRINKLER_DEVICE_GET_WATER_FLOW_RESPONSE:
  224. case SPRINKLER_DEVICE_GET_MOISTURE_RESPONSE:
  225. case SPRINKLER_DEVICE_SET_PROGRAM_ACK:
  226. case SPRINKLER_DEVICE_GET_PROGRAM_RESPONSE:
  227. case SPRINKLER_DEVICE_SET_PARAMETERS_ACK:
  228. case SPRINKLER_DEVICE_GET_PARAMETERS_RESPONSE:
  229. default:
  230. {
  231. //error
  232. break;
  233. }
  234. }
  235. }
  236. else
  237. {
  238. //Ignore..???
  239. }
  240. }
  241. bool SendFrame(unsigned char DestDevice,unsigned char DestAddress, unsigned char SenderDevice, unsigned char Cmd, unsigned char *Data,unsigned int Size,unsigned char Flags)
  242. {
  243. int FrameSize = -1;
  244. unsigned char *FramePtr = ProtocolGetFrame(DestDevice,DestAddress,SenderDevice, Cmd, Data,Size,Flags,&FrameSize);
  245. if(FrameSize > 0)
  246. {
  247. #ifdef USE_WINC1500
  248. #else
  249. TCPPutArray(MySocket,FramePtr,FrameSize);
  250. #endif
  251. }
  252. else
  253. {
  254. return false;
  255. }
  256. return true;
  257. }