PD Buddy Sink Firmware
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.

policy_engine.c 22KB

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