PLCHostBoot.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*"
  42. *
  43. * PLCHostBoot.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Nathaniel Houghton
  49. *
  50. *--------------------------------------------------------------------*/
  51. #ifndef PLCHOSTBOOT_SOURCE
  52. #define PLCHOSTBOOT_SOURCE
  53. #include <unistd.h>
  54. #include <errno.h>
  55. #include <sys/socket.h>
  56. #include <sys/un.h>
  57. #include "../tools/error.h"
  58. #include "../tools/files.h"
  59. #include "../tools/flags.h"
  60. #include "../plc/plc.h"
  61. #define MESSAGE "Starting PLC Host Server\n"
  62. #define SOCKETNAME "/tmp/socket"
  63. static bool done = false;
  64. void terminate (signo_t signal)
  65. {
  66. done = true;
  67. return;
  68. }
  69. static signed opensocket (char const * socketname)
  70. {
  71. signed fd;
  72. struct sockaddr_un sockaddr_un =
  73. {
  74. #if defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  75. 0,
  76. #endif
  77. AF_UNIX,
  78. "/tmp/socket"
  79. };
  80. strncpy (sockaddr_un.sun_path, socketname, sizeof (sockaddr_un.sun_path));
  81. #if defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  82. sockaddr_un.sun_len = SUN_LEN (&sockaddr_un);
  83. #endif
  84. if (unlink (sockaddr_un.sun_path))
  85. {
  86. if (errno != ENOENT)
  87. {
  88. error (1, errno, "%s", sockaddr_un.sun_path);
  89. }
  90. }
  91. if ((fd = socket (AF_UNIX, SOCK_DGRAM, 0)) == -1)
  92. {
  93. error (1, errno, "%s", sockaddr_un.sun_path);
  94. }
  95. if (bind (fd, (struct sockaddr *)(&sockaddr_un), sizeof (sockaddr_un)) == -1)
  96. {
  97. error (1, errno, "%s", sockaddr_un.sun_path);
  98. }
  99. if (chmod (sockaddr_un.sun_path, 0666) == -1)
  100. {
  101. error (1, errno, "%s", sockaddr_un.sun_path);
  102. }
  103. return (fd);
  104. }
  105. /*====================================================================*
  106. *
  107. * signed PLCHostBoot (struct plc * plc, char const * socket, signed timer);
  108. *
  109. * int6k.h
  110. *
  111. * wait indefinitely for VS_HOST_ACTION messages; service the device
  112. * only; it will stop dead - like a bug! - on error;
  113. *
  114. *
  115. * Contributor(s):
  116. * Charles Maier
  117. *
  118. *--------------------------------------------------------------------*/
  119. signed PLCHostBoot (struct plc * plc, char const * socket, unsigned timer)
  120. {
  121. byte buffer [3000];
  122. struct plctopology * plctopology = (struct plctopology *)(buffer);
  123. struct channel * channel = (struct channel *)(plc->channel);
  124. struct message * message = (struct message *)(plc->message);
  125. #ifndef __GNUC__
  126. #pragma pack (push,1)
  127. #endif
  128. struct __packed vs_host_action_ind
  129. {
  130. struct ethernet_hdr ethernet;
  131. struct qualcomm_hdr intellon;
  132. uint8_t MACTION;
  133. uint8_t MAJOR_VERSION;
  134. uint8_t MINOR_VERSION;
  135. }
  136. * indicate = (struct vs_host_action_ind *) (message);
  137. #ifndef __GNUC__
  138. #pragma pack (pop)
  139. #endif
  140. signed fd = opensocket (socket);
  141. char const * NVM = plc->NVM.name;
  142. char const * PIB = plc->PIB.name;
  143. unsigned action;
  144. signed status;
  145. memset (buffer, 0, sizeof (buffer));
  146. write (fd, MESSAGE, strlen (MESSAGE));
  147. while (!done)
  148. {
  149. status = ReadMME (plc, 0, (VS_HOST_ACTION | MMTYPE_IND));
  150. if (status < 0)
  151. {
  152. break;
  153. }
  154. if (status < 1)
  155. {
  156. PLCTopology (channel, message, plctopology);
  157. PLCTopologyPrint (plctopology);
  158. continue;
  159. }
  160. action = indicate->MACTION;
  161. memcpy (channel->peer, indicate->ethernet.OSA, sizeof (channel->peer));
  162. if (HostActionResponse (plc))
  163. {
  164. return (-1);
  165. }
  166. if (action == 0x00)
  167. {
  168. if (BootDevice1 (plc))
  169. {
  170. return (-1);
  171. }
  172. if (_anyset (plc->flags, PLC_FLASH_DEVICE))
  173. {
  174. FlashDevice1 (plc);
  175. }
  176. continue;
  177. }
  178. if (action == 0x01)
  179. {
  180. close (plc->NVM.file);
  181. if (ReadFirmware1 (plc))
  182. {
  183. return (-1);
  184. }
  185. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  186. {
  187. error (1, errno, "%s", plc->NVM.name);
  188. }
  189. if (ResetDevice (plc))
  190. {
  191. return (-1);
  192. }
  193. continue;
  194. }
  195. if (action == 0x02)
  196. {
  197. close (plc->PIB.file);
  198. if (ReadParameters (plc))
  199. {
  200. return (-1);
  201. }
  202. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  203. {
  204. error (1, errno, "%s", plc->PIB.name);
  205. }
  206. if (ResetDevice (plc))
  207. {
  208. return (-1);
  209. }
  210. continue;
  211. }
  212. if (action == 0x03)
  213. {
  214. close (plc->PIB.file);
  215. if (ReadParameters (plc))
  216. {
  217. return (-1);
  218. }
  219. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  220. {
  221. error (1, errno, "%s", plc->PIB.name);
  222. }
  223. close (plc->NVM.file);
  224. if (ReadFirmware1 (plc))
  225. {
  226. return (-1);
  227. }
  228. if ((plc->NVM.file = open (plc->NVM.name = plc->nvm.name, O_BINARY|O_RDONLY)) == -1)
  229. {
  230. error (1, errno, "%s", plc->NVM.name);
  231. }
  232. if (ResetDevice (plc))
  233. {
  234. return (-1);
  235. }
  236. continue;
  237. }
  238. if (action == 0x04)
  239. {
  240. if (InitDevice (plc))
  241. {
  242. return (-1);
  243. }
  244. continue;
  245. }
  246. if (action == 0x05)
  247. {
  248. close (plc->NVM.file);
  249. if ((plc->NVM.file = open (plc->NVM.name = NVM, O_BINARY|O_RDONLY)) == -1)
  250. {
  251. error (1, errno, "%s", plc->NVM.name);
  252. }
  253. close (plc->PIB.file);
  254. if ((plc->PIB.file = open (plc->PIB.name = PIB, O_BINARY|O_RDONLY)) == -1)
  255. {
  256. error (1, errno, "%s", plc->PIB.name);
  257. }
  258. if (ResetDevice (plc))
  259. {
  260. return (-1);
  261. }
  262. continue;
  263. }
  264. if (action == 0x06)
  265. {
  266. close (plc->PIB.file);
  267. if (ReadParameters (plc))
  268. {
  269. return (-1);
  270. }
  271. if ((plc->PIB.file = open (plc->PIB.name = plc->pib.name, O_BINARY|O_RDONLY)) == -1)
  272. {
  273. error (1, errno, "%s", plc->PIB.name);
  274. }
  275. continue;
  276. }
  277. error (0, ENOSYS, "Host Action 0x%02X", action);
  278. }
  279. close (fd);
  280. return (0);
  281. }
  282. #endif