Browse Source

Refactor pdb_dpm_get_sink_capability()

Before, it was repetitive and hard to expand.  It would have become a
big ugly decision tree upon adding support for variable and battery
PDOs.

Now it's been refactored to be a procedure, appending PDOs to the end of
the list and building the message header at the end.  The code isn't
repetitive, and new features can be added more easily.
Clara Hobbs 7 years ago
parent
commit
bd6a1a87bf
1 changed files with 21 additions and 28 deletions
  1. 21
    28
      src/device_policy_manager.c

+ 21
- 28
src/device_policy_manager.c View File

@@ -70,7 +70,7 @@ bool pdb_dpm_evaluate_capability(const union pd_msg *capabilities, union pd_msg
70 70
             }
71 71
         }
72 72
     }
73
-    /* Nothing matched, so get 5 V */
73
+    /* Nothing matched (or no configuration), so get 5 V */
74 74
     request->hdr = PD_MSGTYPE_REQUEST | PD_DATAROLE_UFP |
75 75
         PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
76 76
     request->obj[0] = PD_RDO_FV_MAX_CURRENT_SET(10)
@@ -82,46 +82,39 @@ bool pdb_dpm_evaluate_capability(const union pd_msg *capabilities, union pd_msg
82 82
 
83 83
 void pdb_dpm_get_sink_capability(union pd_msg *cap)
84 84
 {
85
+    /* Keep track of how many PDOs we've added */
86
+    int numobj = 0;
85 87
     /* Get the current configuration */
86 88
     struct pdb_config *cfg = pdb_config_flash_read();
87 89
 
88
-    /* If we have no configuration, request 0.1 A at 5 V. */
89
-    if (cfg == NULL) {
90
-        /* Sink_Capabilities message */
91
-        cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP
92
-            | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
93
-        /* vSafe5V at the desired current. */
94
-        cap->obj[0] = PD_PDO_TYPE_FIXED
90
+    /* If we have no configuration or want something other than 5 V, add a PDO
91
+     * for vSafe5V */
92
+    if (cfg == NULL || cfg->v != 100) {
93
+        /* 100 mA, 5 V, and higher capability. */
94
+        cap->obj[numobj++] = PD_PDO_TYPE_FIXED
95 95
             | PD_PDO_SNK_FIXED_VOLTAGE_SET(100)
96 96
             | PD_PDO_SNK_FIXED_CURRENT_SET(10);
97
-    /* If we want 5 V, we need to send only one PDO */
98
-    } else if (cfg->v == 100) {
99
-        /* Sink_Capabilities message */
100
-        cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP
101
-            | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(1);
102
-        /* vSafe5V at the desired current. */
103
-        cap->obj[0] = PD_PDO_TYPE_FIXED
104
-            | PD_PDO_SNK_FIXED_VOLTAGE_SET(cfg->v)
105
-            | PD_PDO_SNK_FIXED_CURRENT_SET(cfg->i);
106
-    /* Otherwise, send two PDOs, one for 5 V and one for the desired power. */
107
-    } else {
108
-        /* Sink_Capabilities message */
109
-        cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP
110
-            | PD_SPECREV_2_0 | PD_POWERROLE_SINK | PD_NUMOBJ(2);
111
-        /* First, vSafe5V.  100 mA, 5 V, and higher capability. */
112
-        cap->obj[0] = PD_PDO_TYPE_FIXED
113
-            | PD_PDO_SNK_FIXED_VOLTAGE_SET(100)
114
-            | PD_PDO_SNK_FIXED_CURRENT_SET(10);
115
-        /* Next, desired_v and desired_i */
116
-        cap->obj[1] = PD_PDO_TYPE_FIXED
97
+    }
98
+
99
+    /* Add a PDO for the desired power. */
100
+    if (cfg != NULL) {
101
+        cap->obj[numobj++] = PD_PDO_TYPE_FIXED
117 102
             | PD_PDO_SNK_FIXED_VOLTAGE_SET(cfg->v)
118 103
             | PD_PDO_SNK_FIXED_CURRENT_SET(cfg->i);
104
+        /* If we want more than 5 V, set the Higher Capability flag */
105
+        if (cfg->v != 100) {
106
+            cap->obj[0] |= PD_PDO_SNK_FIXED_HIGHER_CAP;
107
+        }
119 108
     }
120 109
 
121 110
     /* Set the unconstrained power flag. */
122 111
     if (dpm_unconstrained_power) {
123 112
         cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
124 113
     }
114
+
115
+    /* Set the Sink_Capabilities message header */
116
+    cap->hdr = PD_MSGTYPE_SINK_CAPABILITIES | PD_DATAROLE_UFP | PD_SPECREV_2_0
117
+        | PD_POWERROLE_SINK | PD_NUMOBJ(numobj);
125 118
 }
126 119
 
127 120
 void pdb_dpm_output_on(void)

Loading…
Cancel
Save