PD Buddy Sink Firmware
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

policy_engine.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "policy_engine.h"
  19. #include "messages.h"
  20. #include "priorities.h"
  21. #include "device_policy_manager.h"
  22. #include "protocol_tx.h"
  23. #include "hard_reset.h"
  24. #include "pd.h"
  25. thread_t *pdb_pe_thread;
  26. enum policy_engine_state {
  27. PESinkStartup,
  28. PESinkDiscovery,
  29. PESinkWaitCap,
  30. PESinkEvalCap,
  31. PESinkSelectCap,
  32. PESinkTransitionSink,
  33. PESinkReady,
  34. PESinkGetSourceCap,
  35. PESinkGiveSinkCap,
  36. PESinkHardReset,
  37. PESinkTransitionDefault,
  38. PESinkSoftReset,
  39. PESinkSendSoftReset,
  40. PESinkSendReject
  41. };
  42. /* The received message we're currently working with */
  43. static union pd_msg *policy_engine_message = NULL;
  44. /* Whether or not the source capabilities match our required power */
  45. static bool capability_match = false;
  46. /* Whether or not we have an explicit contract */
  47. static bool explicit_contract = false;
  48. /* Policy Engine thread mailbox */
  49. static msg_t pdb_pe_mailbox_queue[PDB_MSG_POOL_SIZE];
  50. mailbox_t pdb_pe_mailbox;
  51. static enum policy_engine_state pe_sink_startup(void)
  52. {
  53. explicit_contract = false;
  54. /* No need to reset the protocol layer here. There are two ways into this
  55. * state: startup and exiting hard reset. On startup, the protocol layer
  56. * is reset by the startup procedure. When exiting hard reset, the
  57. * protocol layer is reset by the hard reset state machine. Since it's
  58. * already done somewhere else, there's no need to do it again here. */
  59. return PESinkDiscovery;
  60. }
  61. static enum policy_engine_state pe_sink_discovery(void)
  62. {
  63. /* Wait for VBUS. Since it's our only power source, we already know that
  64. * we have it, so just move on. */
  65. return PESinkWaitCap;
  66. }
  67. static enum policy_engine_state pe_sink_wait_cap(void)
  68. {
  69. /* Fetch a message from the protocol layer */
  70. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_TYPEC_SINK_WAIT_CAP);
  71. /* If we timed out waiting for Source_Capabilities, send a hard reset */
  72. if (evt == 0) {
  73. return PESinkHardReset;
  74. }
  75. /* Get the message */
  76. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  77. /* If we got a Source_Capabilities message, read it. */
  78. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  79. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  80. return PESinkEvalCap;
  81. /* If the message was a Soft_Reset, do the soft reset procedure */
  82. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  83. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  84. chPoolFree(&pdb_msg_pool, policy_engine_message);
  85. policy_engine_message = NULL;
  86. return PESinkSoftReset;
  87. /* If we got an unexpected message, reset */
  88. } else {
  89. /* Free the received message */
  90. chPoolFree(&pdb_msg_pool, policy_engine_message);
  91. return PESinkHardReset;
  92. }
  93. }
  94. /* If we failed to get a message, send a hard reset */
  95. return PESinkHardReset;
  96. }
  97. static enum policy_engine_state pe_sink_eval_cap(void)
  98. {
  99. /* Get a message object */
  100. union pd_msg *request = chPoolAlloc(&pdb_msg_pool);
  101. /* Ask the DPM what to request */
  102. capability_match = pdb_dpm_evaluate_capability(policy_engine_message, request);
  103. /* Free the Source_Capabilities message */
  104. chPoolFree(&pdb_msg_pool, policy_engine_message);
  105. /* Put the request into policy_engine_message */
  106. policy_engine_message = request;
  107. return PESinkSelectCap;
  108. }
  109. static enum policy_engine_state pe_sink_select_cap(void)
  110. {
  111. /* Transmit the request */
  112. chMBPost(&pdb_prltx_mailbox, (msg_t) policy_engine_message, TIME_IMMEDIATE);
  113. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  114. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  115. /* Free the sent message */
  116. chPoolFree(&pdb_msg_pool, policy_engine_message);
  117. policy_engine_message = NULL;
  118. /* If the message transmission failed, send a hard reset */
  119. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  120. return PESinkHardReset;
  121. }
  122. /* Wait for a response */
  123. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_SENDER_RESPONSE);
  124. /* If we didn't get a response before the timeout, send a hard reset */
  125. if (evt == 0) {
  126. return PESinkHardReset;
  127. }
  128. /* Get the response message */
  129. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  130. /* If the source accepted our request, wait for the new power */
  131. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  132. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  133. chPoolFree(&pdb_msg_pool, policy_engine_message);
  134. policy_engine_message = NULL;
  135. return PESinkTransitionSink;
  136. /* If the message was a Soft_Reset, do the soft reset procedure */
  137. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  138. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  139. chPoolFree(&pdb_msg_pool, policy_engine_message);
  140. policy_engine_message = NULL;
  141. return PESinkSoftReset;
  142. /* If the message was Wait or Reject */
  143. } else if ((PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REJECT
  144. || PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT)
  145. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  146. /* If we don't have an explicit contract, wait for capabilities */
  147. if (!explicit_contract) {
  148. chPoolFree(&pdb_msg_pool, policy_engine_message);
  149. policy_engine_message = NULL;
  150. return PESinkWaitCap;
  151. /* If we do have an explicit contract, go to the ready state */
  152. } else {
  153. /* TODO: we should take note if we got here from a Wait
  154. * message, because we Should run the SinkRequestTimer in the
  155. * Ready state if that's the case. */
  156. chPoolFree(&pdb_msg_pool, policy_engine_message);
  157. policy_engine_message = NULL;
  158. return PESinkReady;
  159. }
  160. } else {
  161. chPoolFree(&pdb_msg_pool, policy_engine_message);
  162. policy_engine_message = NULL;
  163. return PESinkSendSoftReset;
  164. }
  165. }
  166. return PESinkHardReset;
  167. }
  168. static enum policy_engine_state pe_sink_transition_sink(void)
  169. {
  170. /* Wait for the PS_RDY message */
  171. eventmask_t evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_PS_TRANSITION);
  172. /* If no message was received, send a hard reset */
  173. if (evt == 0) {
  174. return PESinkHardReset;
  175. }
  176. /* If we received a message, read it */
  177. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  178. /* If we got a PS_RDY, handle it */
  179. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PS_RDY
  180. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  181. /* We just finished negotiating an explicit contract */
  182. explicit_contract = true;
  183. /* Set the output appropriately */
  184. if (capability_match) {
  185. pdb_dpm_output_on();
  186. } else {
  187. pdb_dpm_output_off();
  188. }
  189. chPoolFree(&pdb_msg_pool, policy_engine_message);
  190. policy_engine_message = NULL;
  191. return PESinkReady;
  192. /* If there was a protocol error, send a hard reset */
  193. } else {
  194. /* Turn off the power output before this hard reset to make sure we
  195. * don't supply an incorrect voltage to the device we're powering.
  196. */
  197. pdb_dpm_output_off();
  198. chPoolFree(&pdb_msg_pool, policy_engine_message);
  199. policy_engine_message = NULL;
  200. return PESinkHardReset;
  201. }
  202. }
  203. return PESinkHardReset;
  204. }
  205. static enum policy_engine_state pe_sink_ready(void)
  206. {
  207. /* Wait for an event */
  208. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET);
  209. /* If we got reset signaling, transition to default */
  210. if (evt & PDB_EVT_PE_RESET) {
  211. return PESinkTransitionDefault;
  212. }
  213. /* If we received a message */
  214. if (evt & PDB_EVT_PE_MSG_RX) {
  215. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  216. /* Ignore vendor-defined messages */
  217. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VENDOR_DEFINED
  218. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  219. chPoolFree(&pdb_msg_pool, policy_engine_message);
  220. policy_engine_message = NULL;
  221. return PESinkReady;
  222. /* Ignore Ping messages */
  223. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PING
  224. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  225. chPoolFree(&pdb_msg_pool, policy_engine_message);
  226. policy_engine_message = NULL;
  227. return PESinkReady;
  228. /* Reject DR_Swap messages */
  229. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_DR_SWAP
  230. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  231. chPoolFree(&pdb_msg_pool, policy_engine_message);
  232. policy_engine_message = NULL;
  233. return PESinkSendReject;
  234. /* Reject Get_Source_Cap messages */
  235. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SOURCE_CAP
  236. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  237. chPoolFree(&pdb_msg_pool, policy_engine_message);
  238. policy_engine_message = NULL;
  239. return PESinkSendReject;
  240. /* Reject PR_Swap messages */
  241. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PR_SWAP
  242. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  243. chPoolFree(&pdb_msg_pool, policy_engine_message);
  244. policy_engine_message = NULL;
  245. return PESinkSendReject;
  246. /* Reject VCONN_Swap messages */
  247. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VCONN_SWAP
  248. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  249. chPoolFree(&pdb_msg_pool, policy_engine_message);
  250. policy_engine_message = NULL;
  251. return PESinkSendReject;
  252. /* Reject Request messages */
  253. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REQUEST
  254. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  255. chPoolFree(&pdb_msg_pool, policy_engine_message);
  256. policy_engine_message = NULL;
  257. return PESinkSendReject;
  258. /* Reject Sink_Capabilities messages */
  259. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SINK_CAPABILITIES
  260. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  261. chPoolFree(&pdb_msg_pool, policy_engine_message);
  262. policy_engine_message = NULL;
  263. return PESinkSendReject;
  264. /* Reject GotoMin messages
  265. * Until we actually support GiveBack, this is the correct
  266. * behavior according to S. 6.3.4 */
  267. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GOTOMIN
  268. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  269. chPoolFree(&pdb_msg_pool, policy_engine_message);
  270. policy_engine_message = NULL;
  271. return PESinkSendReject;
  272. /* Evaluate new Source_Capabilities */
  273. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
  274. && PD_NUMOBJ_GET(policy_engine_message) > 0) {
  275. /* Don't free the message: we need to keep the
  276. * Source_Capabilities message so we can evaluate it. */
  277. return PESinkEvalCap;
  278. /* Give sink capabilities when asked */
  279. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SINK_CAP
  280. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  281. chPoolFree(&pdb_msg_pool, policy_engine_message);
  282. policy_engine_message = NULL;
  283. return PESinkGiveSinkCap;
  284. /* If the message was a Soft_Reset, do the soft reset procedure */
  285. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  286. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  287. chPoolFree(&pdb_msg_pool, policy_engine_message);
  288. policy_engine_message = NULL;
  289. return PESinkSoftReset;
  290. /* If we got an unknown message, send a soft reset */
  291. } else {
  292. chPoolFree(&pdb_msg_pool, policy_engine_message);
  293. policy_engine_message = NULL;
  294. return PESinkSendSoftReset;
  295. }
  296. }
  297. }
  298. return PESinkReady;
  299. }
  300. static enum policy_engine_state pe_sink_get_source_cap(void)
  301. {
  302. /* Stubbed until we actually have a need for this */
  303. return PESinkReady;
  304. }
  305. static enum policy_engine_state pe_sink_give_sink_cap(void)
  306. {
  307. /* Get a message object */
  308. union pd_msg *snk_cap = chPoolAlloc(&pdb_msg_pool);
  309. /* Get our capabilities from the DPM */
  310. pdb_dpm_get_sink_capability(snk_cap);
  311. /* Transmit our capabilities */
  312. chMBPost(&pdb_prltx_mailbox, (msg_t) snk_cap, TIME_IMMEDIATE);
  313. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  314. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  315. /* Free the Sink_Capabilities message */
  316. chPoolFree(&pdb_msg_pool, snk_cap);
  317. snk_cap = NULL;
  318. /* If the message transmission failed, send a hard reset */
  319. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  320. return PESinkHardReset;
  321. }
  322. return PESinkReady;
  323. }
  324. static enum policy_engine_state pe_sink_hard_reset(void)
  325. {
  326. /* Generate a hard reset signal */
  327. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_RESET);
  328. chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
  329. /* TODO: maintain HardResetCounter */
  330. return PESinkTransitionDefault;
  331. }
  332. static enum policy_engine_state pe_sink_transition_default(void)
  333. {
  334. explicit_contract = false;
  335. /* Tell the DPM to turn off the output */
  336. pdb_dpm_output_off();
  337. /* There is no local hardware to reset. */
  338. /* Since we never change our data role from UFP, there is no reason to set
  339. * it here. */
  340. /* Tell the protocol layer we're done with the reset */
  341. chEvtSignal(pdb_hardrst_thread, PDB_EVT_HARDRST_DONE);
  342. return PESinkStartup;
  343. }
  344. static enum policy_engine_state pe_sink_soft_reset(void)
  345. {
  346. /* No need to explicitly reset the protocol layer here. It resets itself
  347. * when a Soft_Reset message is received. */
  348. /* Get a message object */
  349. union pd_msg *accept = chPoolAlloc(&pdb_msg_pool);
  350. /* Make an Accept message */
  351. accept->hdr = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  352. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  353. /* Transmit the Accept */
  354. chMBPost(&pdb_prltx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
  355. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  356. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  357. /* Free the sent message */
  358. chPoolFree(&pdb_msg_pool, accept);
  359. accept = NULL;
  360. /* If the message transmission failed, send a hard reset */
  361. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  362. return PESinkHardReset;
  363. }
  364. return PESinkWaitCap;
  365. }
  366. static enum policy_engine_state pe_sink_send_soft_reset(void)
  367. {
  368. /* No need to explicitly reset the protocol layer here. It resets itself
  369. * just before a Soft_Reset message is transmitted. */
  370. /* Get a message object */
  371. union pd_msg *softrst = chPoolAlloc(&pdb_msg_pool);
  372. /* Make a Soft_Reset message */
  373. softrst->hdr = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
  374. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  375. /* Transmit the soft reset */
  376. chMBPost(&pdb_prltx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
  377. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  378. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  379. /* Free the sent message */
  380. chPoolFree(&pdb_msg_pool, softrst);
  381. softrst = NULL;
  382. /* If the message transmission failed, send a hard reset */
  383. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  384. return PESinkHardReset;
  385. }
  386. /* Wait for a response */
  387. evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX, PD_T_SENDER_RESPONSE);
  388. /* If we didn't get a response before the timeout, send a hard reset */
  389. if (evt == 0) {
  390. return PESinkHardReset;
  391. }
  392. /* Get the response message */
  393. if (chMBFetch(&pdb_pe_mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
  394. /* If the source accepted our soft reset, wait for capabilities. */
  395. if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
  396. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  397. chPoolFree(&pdb_msg_pool, policy_engine_message);
  398. policy_engine_message = NULL;
  399. return PESinkWaitCap;
  400. /* If the message was a Soft_Reset, do the soft reset procedure */
  401. } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
  402. && PD_NUMOBJ_GET(policy_engine_message) == 0) {
  403. chPoolFree(&pdb_msg_pool, policy_engine_message);
  404. policy_engine_message = NULL;
  405. return PESinkSoftReset;
  406. /* Otherwise, send a hard reset */
  407. } else {
  408. chPoolFree(&pdb_msg_pool, policy_engine_message);
  409. policy_engine_message = NULL;
  410. return PESinkHardReset;
  411. }
  412. }
  413. return PESinkHardReset;
  414. }
  415. static enum policy_engine_state pe_sink_send_reject(void)
  416. {
  417. /* Get a message object */
  418. union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
  419. /* Make a Reject message */
  420. reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
  421. | PD_POWERROLE_SINK | PD_NUMOBJ(0);
  422. /* Transmit the message */
  423. chMBPost(&pdb_prltx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
  424. chEvtSignal(pdb_prltx_thread, PDB_EVT_PRLTX_MSG_TX);
  425. eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR);
  426. /* Free the Reject message */
  427. chPoolFree(&pdb_msg_pool, reject);
  428. reject = NULL;
  429. /* If the message transmission failed, send a soft reset */
  430. if ((evt & PDB_EVT_PE_TX_DONE) == 0) {
  431. return PESinkSendSoftReset;
  432. }
  433. return PESinkReady;
  434. }
  435. /*
  436. * Policy Engine state machine thread
  437. */
  438. static THD_WORKING_AREA(waPolicyEngine, 128);
  439. static THD_FUNCTION(PolicyEngine, arg) {
  440. (void) arg;
  441. enum policy_engine_state state = PESinkStartup;
  442. /* Initialize the mailbox */
  443. chMBObjectInit(&pdb_pe_mailbox, pdb_pe_mailbox_queue, PDB_MSG_POOL_SIZE);
  444. while (true) {
  445. switch (state) {
  446. case PESinkStartup:
  447. state = pe_sink_startup();
  448. break;
  449. case PESinkDiscovery:
  450. state = pe_sink_discovery();
  451. break;
  452. case PESinkWaitCap:
  453. state = pe_sink_wait_cap();
  454. break;
  455. case PESinkEvalCap:
  456. state = pe_sink_eval_cap();
  457. break;
  458. case PESinkSelectCap:
  459. state = pe_sink_select_cap();
  460. break;
  461. case PESinkTransitionSink:
  462. state = pe_sink_transition_sink();
  463. break;
  464. case PESinkReady:
  465. state = pe_sink_ready();
  466. break;
  467. case PESinkGetSourceCap:
  468. state = pe_sink_get_source_cap();
  469. break;
  470. case PESinkGiveSinkCap:
  471. state = pe_sink_give_sink_cap();
  472. break;
  473. case PESinkHardReset:
  474. state = pe_sink_hard_reset();
  475. break;
  476. case PESinkTransitionDefault:
  477. state = pe_sink_transition_default();
  478. break;
  479. case PESinkSoftReset:
  480. state = pe_sink_soft_reset();
  481. break;
  482. case PESinkSendSoftReset:
  483. state = pe_sink_send_soft_reset();
  484. break;
  485. case PESinkSendReject:
  486. state = pe_sink_send_reject();
  487. break;
  488. default:
  489. /* This is an error. It really shouldn't happen. We might
  490. * want to handle it anyway, though. */
  491. state = PESinkStartup;
  492. break;
  493. }
  494. }
  495. }
  496. void pdb_pe_run(void)
  497. {
  498. pdb_pe_thread = chThdCreateStatic(waPolicyEngine, sizeof(waPolicyEngine),
  499. PDB_PRIO_PE, PolicyEngine, NULL);
  500. }