Browse Source

Use a template for PD message headers

The new tempate variable sets the data role, power role, and
specification revision in one fell swoop.  It's used by the PE and DPM
alike for creating messages, and in the future it may have a part in
determining if we're doing PD 2.0 or 3.0.
Clara Hobbs 7 years ago
parent
commit
6ba7bfdbf3
3 changed files with 14 additions and 14 deletions
  1. 2
    0
      lib/include/pdb_pe.h
  2. 7
    8
      lib/src/policy_engine.c
  3. 5
    6
      src/device_policy_manager.c

+ 2
- 0
lib/include/pdb_pe.h View File

@@ -45,6 +45,8 @@ struct pdb_pe {
45 45
 
46 46
     /* PE mailbox for received PD messages */
47 47
     mailbox_t mailbox;
48
+    /* PD message header template */
49
+    uint16_t hdr_template;
48 50
 
49 51
     /* The received message we're currently working with */
50 52
     union pd_msg *_message;

+ 7
- 8
lib/src/policy_engine.c View File

@@ -409,8 +409,8 @@ static enum policy_engine_state pe_sink_get_source_cap(struct pdb_config *cfg)
409 409
     /* Get a message object */
410 410
     union pd_msg *get_source_cap = chPoolAlloc(&pdb_msg_pool);
411 411
     /* Make a Get_Source_Cap message */
412
-    get_source_cap->hdr = PD_MSGTYPE_GET_SOURCE_CAP | PD_DATAROLE_UFP
413
-        | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(0);
412
+    get_source_cap->hdr = cfg->pe.hdr_template | PD_MSGTYPE_GET_SOURCE_CAP
413
+        | PD_NUMOBJ(0);
414 414
     /* Transmit the Get_Source_Cap */
415 415
     chMBPost(&cfg->prl.tx_mailbox, (msg_t) get_source_cap, TIME_IMMEDIATE);
416 416
     chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
@@ -503,8 +503,7 @@ static enum policy_engine_state pe_sink_soft_reset(struct pdb_config *cfg)
503 503
     /* Get a message object */
504 504
     union pd_msg *accept = chPoolAlloc(&pdb_msg_pool);
505 505
     /* Make an Accept message */
506
-    accept->hdr = PD_MSGTYPE_ACCEPT | PD_DATAROLE_UFP | PD_SPECREV_2_0
507
-        | PD_POWERROLE_SINK | PD_NUMOBJ(0);
506
+    accept->hdr = cfg->pe.hdr_template | PD_MSGTYPE_ACCEPT | PD_NUMOBJ(0);
508 507
     /* Transmit the Accept */
509 508
     chMBPost(&cfg->prl.tx_mailbox, (msg_t) accept, TIME_IMMEDIATE);
510 509
     chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
@@ -533,8 +532,7 @@ static enum policy_engine_state pe_sink_send_soft_reset(struct pdb_config *cfg)
533 532
     /* Get a message object */
534 533
     union pd_msg *softrst = chPoolAlloc(&pdb_msg_pool);
535 534
     /* Make a Soft_Reset message */
536
-    softrst->hdr = PD_MSGTYPE_SOFT_RESET | PD_DATAROLE_UFP | PD_SPECREV_2_0
537
-        | PD_POWERROLE_SINK | PD_NUMOBJ(0);
535
+    softrst->hdr = cfg->pe.hdr_template | PD_MSGTYPE_SOFT_RESET | PD_NUMOBJ(0);
538 536
     /* Transmit the soft reset */
539 537
     chMBPost(&cfg->prl.tx_mailbox, (msg_t) softrst, TIME_IMMEDIATE);
540 538
     chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
@@ -593,8 +591,7 @@ static enum policy_engine_state pe_sink_send_reject(struct pdb_config *cfg)
593 591
     /* Get a message object */
594 592
     union pd_msg *reject = chPoolAlloc(&pdb_msg_pool);
595 593
     /* Make a Reject message */
596
-    reject->hdr = PD_MSGTYPE_REJECT | PD_DATAROLE_UFP | PD_SPECREV_2_0
597
-        | PD_POWERROLE_SINK | PD_NUMOBJ(0);
594
+    reject->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REJECT | PD_NUMOBJ(0);
598 595
 
599 596
     /* Transmit the message */
600 597
     chMBPost(&cfg->prl.tx_mailbox, (msg_t) reject, TIME_IMMEDIATE);
@@ -655,6 +652,8 @@ static THD_FUNCTION(PolicyEngine, vcfg) {
655 652
     chMBObjectInit(&cfg->pe.mailbox, cfg->pe._mailbox_queue, PDB_MSG_POOL_SIZE);
656 653
     /* Initialize the old_tcc_match */
657 654
     cfg->pe._old_tcc_match = -1;
655
+    /* Initialize the PD message header template */
656
+    cfg->pe.hdr_template = PD_DATAROLE_UFP | PD_SPECREV_2_0 | PD_POWERROLE_SINK;
658 657
 
659 658
     while (true) {
660 659
         switch (state) {

+ 5
- 6
src/device_policy_manager.c View File

@@ -75,8 +75,8 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
75 75
             if (PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities, i) == scfg->v
76 76
                     && PD_PDO_SRC_FIXED_CURRENT_GET(capabilities, i) >= scfg->i) {
77 77
                 /* We got what we wanted, so build a request for that */
78
-                request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
79
-                    PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
78
+                request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST
79
+                    | PD_NUMOBJ(1);
80 80
                 if (scfg->flags & PDBS_CONFIG_FLAGS_GIVEBACK) {
81 81
                     /* GiveBack enabled */
82 82
                     request->obj[0] = PD_RDO_FV_MIN_CURRENT_SET(DPM_MIN_CURRENT)
@@ -102,8 +102,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
102 102
         }
103 103
     }
104 104
     /* Nothing matched (or no configuration), so get 5 V at low current */
105
-    request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
106
-        PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
105
+    request->hdr = cfg->pe.hdr_template | PD_MSGTYPE_REQUEST | PD_NUMOBJ(1);
107 106
     request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(DPM_MIN_CURRENT)
108 107
         | PD_RDO_FV_CURRENT_SET(DPM_MIN_CURRENT)
109 108
         | PD_RDO_NO_USB_SUSPEND
@@ -166,8 +165,8 @@ void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
166 165
     }
167 166
 
168 167
     /* Set the Sink_Capabilities message header */
169
-    cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP | PD_SPECREV_2_0
170
-        | PD_POWERROLE_SINK | PD_NUMOBJ(numobj);
168
+    cap->hdr = cfg->pe.hdr_template | PD_MSGTYPE_SINK_CAPABILITIES
169
+        | PD_NUMOBJ(numobj);
171 170
 }
172 171
 
173 172
 bool pdbs_dpm_giveback_enabled(struct pdb_config *cfg)

Loading…
Cancel
Save