PD Buddy Sink Firmware
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

policy_engine.c 22KB

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