ソースを参照

Use struct pdbs_dpm_data for DPM private variables

Again, shining example and whatnot.
Clara Hobbs 7年前
コミット
ad3b2399fa
3個のファイルの変更36行の追加28行の削除
  1. 24
    27
      src/device_policy_manager.c
  2. 9
    0
      src/device_policy_manager.h
  3. 3
    1
      src/main.c

+ 24
- 27
src/device_policy_manager.c ファイルの表示

@@ -30,18 +30,6 @@
30 30
 /* The current draw when the output is disabled */
31 31
 #define DPM_MIN_CURRENT PD_MA2PDI(100)
32 32
 
33
-/* Whether or not the power supply is unconstrained */
34
-static bool dpm_unconstrained_power;
35
-
36
-/* The last explicitly or implicitly negotiated voltage in PDV */
37
-static int dpm_present_voltage = PD_MV2PDV(5000);
38
-
39
-/* The requested voltage */
40
-static int dpm_requested_voltage;
41
-
42
-/* Whether our capabilities matched or not */
43
-static bool dpm_capability_match;
44
-
45 33
 bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
46 34
         const union pd_msg *capabilities, union pd_msg *request)
47 35
 {
@@ -70,7 +58,7 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
70 58
     }
71 59
 
72 60
     /* Get whether or not the power supply is constrained */
73
-    dpm_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
61
+    dpm_data->_unconstrained_power = capabilities->obj[0] & PD_PDO_SRC_FIXED_UNCONSTRAINED;
74 62
 
75 63
     /* Make sure we have configuration */
76 64
     if (scfg != NULL && dpm_data->output_enabled) {
@@ -105,9 +93,9 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
105 93
                 }
106 94
 
107 95
                 /* Update requested voltage */
108
-                dpm_requested_voltage = scfg->v;
96
+                dpm_data->_requested_voltage = scfg->v;
109 97
 
110
-                dpm_capability_match = true;
98
+                dpm_data->_capability_match = true;
111 99
                 return true;
112 100
             }
113 101
         }
@@ -130,10 +118,10 @@ bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
130 118
     }
131 119
 
132 120
     /* Update requested voltage */
133
-    dpm_requested_voltage = PD_MV2PDV(5000);
121
+    dpm_data->_requested_voltage = PD_MV2PDV(5000);
134 122
 
135 123
     /* At this point, we have a capability match iff the output is disabled */
136
-    dpm_capability_match = !dpm_data->output_enabled;
124
+    dpm_data->_capability_match = !dpm_data->output_enabled;
137 125
     return !dpm_data->output_enabled;
138 126
 }
139 127
 
@@ -167,7 +155,7 @@ void pdbs_dpm_get_sink_capability(struct pdb_config *cfg, union pd_msg *cap)
167 155
     }
168 156
 
169 157
     /* Set the unconstrained power flag. */
170
-    if (dpm_unconstrained_power) {
158
+    if (dpm_data->_unconstrained_power) {
171 159
         cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
172 160
     }
173 161
 
@@ -196,7 +184,7 @@ bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
196 184
     struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
197 185
 
198 186
     /* We don't control the voltage anymore; it will always be 5 V. */
199
-    dpm_requested_voltage = PD_MV2PDV(5000);
187
+    dpm_data->_requested_voltage = PD_MV2PDV(5000);
200 188
 
201 189
     /* Make the present Type-C Current advertisement available to the rest of
202 190
      * the DPM */
@@ -205,18 +193,18 @@ bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
205 193
     /* If we have no configuration or don't want 5 V, Type-C Current can't
206 194
      * possibly satisfy our needs */
207 195
     if (scfg == NULL || scfg->v != PD_MV2PDV(5000)) {
208
-        dpm_capability_match = false;
196
+        dpm_data->_capability_match = false;
209 197
         return false;
210 198
     }
211 199
 
212 200
     /* If 1.5 A is available and we want no more than that, great. */
213 201
     if (tcc == OnePointFiveAmps && scfg->i <= 150) {
214
-        dpm_capability_match = true;
202
+        dpm_data->_capability_match = true;
215 203
         return true;
216 204
     }
217 205
     /* If 3 A is available and we want no more than that, that's great too. */
218 206
     if (tcc == ThreePointZeroAmps && scfg->i <= 300) {
219
-        dpm_capability_match = true;
207
+        dpm_data->_capability_match = true;
220 208
         return true;
221 209
     }
222 210
     /* We're overly cautious if USB default current is available, since that
@@ -224,7 +212,7 @@ bool pdbs_dpm_evaluate_typec_current(struct pdb_config *cfg,
224 212
      * and since we're really supposed to enumerate in order to request more
225 213
      * than 100 mA.  This could be changed in the future. */
226 214
 
227
-    dpm_capability_match = false;
215
+    dpm_data->_capability_match = false;
228 216
     return false;
229 217
 }
230 218
 
@@ -244,7 +232,7 @@ void pdbs_dpm_pd_start(struct pdb_config *cfg)
244 232
 static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
245 233
 {
246 234
     /* Update the present voltage */
247
-    dpm_present_voltage = dpm_requested_voltage;
235
+    dpm_data->_present_voltage = dpm_data->_requested_voltage;
248 236
 
249 237
     /* Set the power output */
250 238
     if (state && dpm_data->output_enabled) {
@@ -264,8 +252,11 @@ static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
264 252
 
265 253
 void pdbs_dpm_transition_default(struct pdb_config *cfg)
266 254
 {
255
+    /* Cast the dpm_data to the right type */
256
+    struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
257
+
267 258
     /* Pretend we requested 5 V */
268
-    dpm_requested_voltage = PD_MV2PDV(5000);
259
+    dpm_data->_requested_voltage = PD_MV2PDV(5000);
269 260
     /* Turn the output off */
270 261
     dpm_output_set(cfg->dpm_data, false);
271 262
 }
@@ -277,8 +268,11 @@ void pdbs_dpm_transition_min(struct pdb_config *cfg)
277 268
 
278 269
 void pdbs_dpm_transition_standby(struct pdb_config *cfg)
279 270
 {
271
+    /* Cast the dpm_data to the right type */
272
+    struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
273
+
280 274
     /* If the voltage is changing, enter Sink Standby */
281
-    if (dpm_requested_voltage != dpm_present_voltage) {
275
+    if (dpm_data->_requested_voltage != dpm_data->_present_voltage) {
282 276
         /* For the PD Buddy Sink, entering Sink Standby is equivalent to
283 277
          * turning the output off.  However, we don't want to change the LED
284 278
          * state for standby mode. */
@@ -288,5 +282,8 @@ void pdbs_dpm_transition_standby(struct pdb_config *cfg)
288 282
 
289 283
 void pdbs_dpm_transition_requested(struct pdb_config *cfg)
290 284
 {
291
-    dpm_output_set(cfg->dpm_data, dpm_capability_match);
285
+    /* Cast the dpm_data to the right type */
286
+    struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
287
+
288
+    dpm_output_set(cfg->dpm_data, dpm_data->_capability_match);
292 289
 }

+ 9
- 0
src/device_policy_manager.h ファイルの表示

@@ -37,6 +37,15 @@ struct pdbs_dpm_data {
37 37
     bool led_pd_status;
38 38
     /* Whether the device is capable of USB communications */
39 39
     bool usb_comms;
40
+
41
+    /* Whether or not the power supply is unconstrained */
42
+    bool _unconstrained_power;
43
+    /* Whether our capabilities matched or not */
44
+    bool _capability_match;
45
+    /* The last explicitly or implicitly negotiated voltage in PDV */
46
+    int _present_voltage;
47
+    /* The requested voltage */
48
+    int _requested_voltage;
40 49
 };
41 50
 
42 51
 

+ 3
- 1
src/main.c ファイルの表示

@@ -41,6 +41,7 @@
41 41
 #include <pdb.h>
42 42
 #include "led.h"
43 43
 #include "device_policy_manager.h"
44
+#include "pd.h"
44 45
 
45 46
 /*
46 47
  * I2C configuration object.
@@ -61,7 +62,8 @@ static struct pdbs_dpm_data dpm_data = {
61 62
     None,
62 63
     true,
63 64
     true,
64
-    false
65
+    false,
66
+    ._present_voltage = PD_MV2PDV(5000)
65 67
 };
66 68
 
67 69
 /*

読み込み中…
キャンセル
保存