Browse Source

Use struct pdbs_dpm_data for DPM private variables

Again, shining example and whatnot.
Clara Hobbs 6 years ago
parent
commit
ad3b2399fa
3 changed files with 36 additions and 28 deletions
  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 View File

30
 /* The current draw when the output is disabled */
30
 /* The current draw when the output is disabled */
31
 #define DPM_MIN_CURRENT PD_MA2PDI(100)
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
 bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
33
 bool pdbs_dpm_evaluate_capability(struct pdb_config *cfg,
46
         const union pd_msg *capabilities, union pd_msg *request)
34
         const union pd_msg *capabilities, union pd_msg *request)
47
 {
35
 {
70
     }
58
     }
71
 
59
 
72
     /* Get whether or not the power supply is constrained */
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
     /* Make sure we have configuration */
63
     /* Make sure we have configuration */
76
     if (scfg != NULL && dpm_data->output_enabled) {
64
     if (scfg != NULL && dpm_data->output_enabled) {
105
                 }
93
                 }
106
 
94
 
107
                 /* Update requested voltage */
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
                 return true;
99
                 return true;
112
             }
100
             }
113
         }
101
         }
130
     }
118
     }
131
 
119
 
132
     /* Update requested voltage */
120
     /* Update requested voltage */
133
-    dpm_requested_voltage = PD_MV2PDV(5000);
121
+    dpm_data->_requested_voltage = PD_MV2PDV(5000);
134
 
122
 
135
     /* At this point, we have a capability match iff the output is disabled */
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
     return !dpm_data->output_enabled;
125
     return !dpm_data->output_enabled;
138
 }
126
 }
139
 
127
 
167
     }
155
     }
168
 
156
 
169
     /* Set the unconstrained power flag. */
157
     /* Set the unconstrained power flag. */
170
-    if (dpm_unconstrained_power) {
158
+    if (dpm_data->_unconstrained_power) {
171
         cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
159
         cap->obj[0] |= PD_PDO_SNK_FIXED_UNCONSTRAINED;
172
     }
160
     }
173
 
161
 
196
     struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
184
     struct pdbs_dpm_data *dpm_data = cfg->dpm_data;
197
 
185
 
198
     /* We don't control the voltage anymore; it will always be 5 V. */
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
     /* Make the present Type-C Current advertisement available to the rest of
189
     /* Make the present Type-C Current advertisement available to the rest of
202
      * the DPM */
190
      * the DPM */
205
     /* If we have no configuration or don't want 5 V, Type-C Current can't
193
     /* If we have no configuration or don't want 5 V, Type-C Current can't
206
      * possibly satisfy our needs */
194
      * possibly satisfy our needs */
207
     if (scfg == NULL || scfg->v != PD_MV2PDV(5000)) {
195
     if (scfg == NULL || scfg->v != PD_MV2PDV(5000)) {
208
-        dpm_capability_match = false;
196
+        dpm_data->_capability_match = false;
209
         return false;
197
         return false;
210
     }
198
     }
211
 
199
 
212
     /* If 1.5 A is available and we want no more than that, great. */
200
     /* If 1.5 A is available and we want no more than that, great. */
213
     if (tcc == OnePointFiveAmps && scfg->i <= 150) {
201
     if (tcc == OnePointFiveAmps && scfg->i <= 150) {
214
-        dpm_capability_match = true;
202
+        dpm_data->_capability_match = true;
215
         return true;
203
         return true;
216
     }
204
     }
217
     /* If 3 A is available and we want no more than that, that's great too. */
205
     /* If 3 A is available and we want no more than that, that's great too. */
218
     if (tcc == ThreePointZeroAmps && scfg->i <= 300) {
206
     if (tcc == ThreePointZeroAmps && scfg->i <= 300) {
219
-        dpm_capability_match = true;
207
+        dpm_data->_capability_match = true;
220
         return true;
208
         return true;
221
     }
209
     }
222
     /* We're overly cautious if USB default current is available, since that
210
     /* We're overly cautious if USB default current is available, since that
224
      * and since we're really supposed to enumerate in order to request more
212
      * and since we're really supposed to enumerate in order to request more
225
      * than 100 mA.  This could be changed in the future. */
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
     return false;
216
     return false;
229
 }
217
 }
230
 
218
 
244
 static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
232
 static void dpm_output_set(struct pdbs_dpm_data *dpm_data, bool state)
245
 {
233
 {
246
     /* Update the present voltage */
234
     /* Update the present voltage */
247
-    dpm_present_voltage = dpm_requested_voltage;
235
+    dpm_data->_present_voltage = dpm_data->_requested_voltage;
248
 
236
 
249
     /* Set the power output */
237
     /* Set the power output */
250
     if (state && dpm_data->output_enabled) {
238
     if (state && dpm_data->output_enabled) {
264
 
252
 
265
 void pdbs_dpm_transition_default(struct pdb_config *cfg)
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
     /* Pretend we requested 5 V */
258
     /* Pretend we requested 5 V */
268
-    dpm_requested_voltage = PD_MV2PDV(5000);
259
+    dpm_data->_requested_voltage = PD_MV2PDV(5000);
269
     /* Turn the output off */
260
     /* Turn the output off */
270
     dpm_output_set(cfg->dpm_data, false);
261
     dpm_output_set(cfg->dpm_data, false);
271
 }
262
 }
277
 
268
 
278
 void pdbs_dpm_transition_standby(struct pdb_config *cfg)
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
     /* If the voltage is changing, enter Sink Standby */
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
         /* For the PD Buddy Sink, entering Sink Standby is equivalent to
276
         /* For the PD Buddy Sink, entering Sink Standby is equivalent to
283
          * turning the output off.  However, we don't want to change the LED
277
          * turning the output off.  However, we don't want to change the LED
284
          * state for standby mode. */
278
          * state for standby mode. */
288
 
282
 
289
 void pdbs_dpm_transition_requested(struct pdb_config *cfg)
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 View File

37
     bool led_pd_status;
37
     bool led_pd_status;
38
     /* Whether the device is capable of USB communications */
38
     /* Whether the device is capable of USB communications */
39
     bool usb_comms;
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 View File

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

Loading…
Cancel
Save