Browse Source

Use PE private variables from struct pdb_config

Clara Hobbs 7 years ago
parent
commit
5bc226847f
2 changed files with 123 additions and 130 deletions
  1. 8
    2
      lib/include/pdb_pe.h
  2. 115
    128
      lib/src/policy_engine.c

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

29
     THD_WORKING_AREA(_wa, 128);
29
     THD_WORKING_AREA(_wa, 128);
30
     thread_t *thread;
30
     thread_t *thread;
31
 
31
 
32
+    /* PE mailbox for received PD messages */
32
     mailbox_t mailbox;
33
     mailbox_t mailbox;
33
 
34
 
34
-    union pd_msg *_policy_engine_message;
35
+    /* The received message we're currently working with */
36
+    union pd_msg *_message;
37
+    /* The most recent Request from the DPM */
35
     union pd_msg *_last_dpm_request;
38
     union pd_msg *_last_dpm_request;
36
-    bool _capability_match;
39
+    /* Whether or not we have an explicit contract */
37
     bool _explicit_contract;
40
     bool _explicit_contract;
41
+    /* Whether or not we're receiving minimum power */
38
     bool _min_power;
42
     bool _min_power;
43
+    /* The number of hard resets we've sent */
39
     int8_t _hard_reset_counter;
44
     int8_t _hard_reset_counter;
45
+    /* Queue for the PE mailbox */
40
     msg_t _mailbox_queue[PDB_MSG_POOL_SIZE];
46
     msg_t _mailbox_queue[PDB_MSG_POOL_SIZE];
41
 };
47
 };
42
 
48
 

+ 115
- 128
lib/src/policy_engine.c View File

48
     PESinkSourceUnresponsive
48
     PESinkSourceUnresponsive
49
 };
49
 };
50
 
50
 
51
-/* The received message we're currently working with */
52
-static union pd_msg *policy_engine_message = NULL;
53
-/* The most recent Request from the DPM */
54
-static union pd_msg *last_dpm_request = NULL;
55
-/* Whether or not the source capabilities match our required power */
56
-static bool capability_match = false;
57
-/* Whether or not we have an explicit contract */
58
-static bool explicit_contract = false;
59
-/* Whether or not we're receiving minimum power*/
60
-static bool min_power = false;
61
-/* Keep track of how many hard resets we've sent */
62
-static int hard_reset_counter = 0;
63
-
64
 static enum policy_engine_state pe_sink_startup(struct pdb_config *cfg)
51
 static enum policy_engine_state pe_sink_startup(struct pdb_config *cfg)
65
 {
52
 {
66
     /* We don't have an explicit contract currently */
53
     /* We don't have an explicit contract currently */
67
-    explicit_contract = false;
54
+    cfg->pe._explicit_contract = false;
68
     /* Tell the DPM that we've started negotiations */
55
     /* Tell the DPM that we've started negotiations */
69
     cfg->dpm.pd_start(cfg);
56
     cfg->dpm.pd_start(cfg);
70
 
57
 
107
     /* If we got a message */
94
     /* If we got a message */
108
     if (evt & PDB_EVT_PE_MSG_RX) {
95
     if (evt & PDB_EVT_PE_MSG_RX) {
109
         /* Get the message */
96
         /* Get the message */
110
-        if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
97
+        if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &cfg->pe._message, TIME_IMMEDIATE) == MSG_OK) {
111
             /* If we got a Source_Capabilities message, read it. */
98
             /* If we got a Source_Capabilities message, read it. */
112
-            if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
113
-                    && PD_NUMOBJ_GET(policy_engine_message) > 0) {
99
+            if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOURCE_CAPABILITIES
100
+                    && PD_NUMOBJ_GET(cfg->pe._message) > 0) {
114
                 return PESinkEvalCap;
101
                 return PESinkEvalCap;
115
             /* If the message was a Soft_Reset, do the soft reset procedure */
102
             /* If the message was a Soft_Reset, do the soft reset procedure */
116
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
117
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
118
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
119
-                policy_engine_message = NULL;
103
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOFT_RESET
104
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
105
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
106
+                cfg->pe._message = NULL;
120
                 return PESinkSoftReset;
107
                 return PESinkSoftReset;
121
             /* If we got an unexpected message, reset */
108
             /* If we got an unexpected message, reset */
122
             } else {
109
             } else {
123
                 /* Free the received message */
110
                 /* Free the received message */
124
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
111
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
125
                 return PESinkHardReset;
112
                 return PESinkHardReset;
126
             }
113
             }
127
         }
114
         }
134
 static enum policy_engine_state pe_sink_eval_cap(struct pdb_config *cfg)
121
 static enum policy_engine_state pe_sink_eval_cap(struct pdb_config *cfg)
135
 {
122
 {
136
     /* Get a message object for the request if we don't have one already */
123
     /* Get a message object for the request if we don't have one already */
137
-    if (last_dpm_request == NULL) {
138
-        last_dpm_request = chPoolAlloc(&pdb_msg_pool);
124
+    if (cfg->pe._last_dpm_request == NULL) {
125
+        cfg->pe._last_dpm_request = chPoolAlloc(&pdb_msg_pool);
139
     }
126
     }
140
     /* Ask the DPM what to request */
127
     /* Ask the DPM what to request */
141
-    capability_match = cfg->dpm.evaluate_capability(cfg, policy_engine_message,
142
-            last_dpm_request);
128
+    cfg->dpm.evaluate_capability(cfg, cfg->pe._message,
129
+            cfg->pe._last_dpm_request);
143
     /* It's up to the DPM to free the Source_Capabilities message, which it can
130
     /* It's up to the DPM to free the Source_Capabilities message, which it can
144
      * do whenever it sees fit.  Just remove our reference to it since we won't
131
      * do whenever it sees fit.  Just remove our reference to it since we won't
145
      * know when it's no longer valid. */
132
      * know when it's no longer valid. */
146
-    policy_engine_message = NULL;
133
+    cfg->pe._message = NULL;
147
 
134
 
148
     return PESinkSelectCap;
135
     return PESinkSelectCap;
149
 }
136
 }
151
 static enum policy_engine_state pe_sink_select_cap(struct pdb_config *cfg)
138
 static enum policy_engine_state pe_sink_select_cap(struct pdb_config *cfg)
152
 {
139
 {
153
     /* Transmit the request */
140
     /* Transmit the request */
154
-    chMBPost(&cfg->prl.tx_mailbox, (msg_t) last_dpm_request, TIME_IMMEDIATE);
141
+    chMBPost(&cfg->prl.tx_mailbox, (msg_t) cfg->pe._last_dpm_request, TIME_IMMEDIATE);
155
     chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
142
     chEvtSignal(cfg->prl.tx_thread, PDB_EVT_PRLTX_MSG_TX);
156
     eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
143
     eventmask_t evt = chEvtWaitAny(PDB_EVT_PE_TX_DONE | PDB_EVT_PE_TX_ERR
157
             | PDB_EVT_PE_RESET);
144
             | PDB_EVT_PE_RESET);
178
     }
165
     }
179
 
166
 
180
     /* Get the response message */
167
     /* Get the response message */
181
-    if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
168
+    if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &cfg->pe._message, TIME_IMMEDIATE) == MSG_OK) {
182
         /* If the source accepted our request, wait for the new power */
169
         /* If the source accepted our request, wait for the new power */
183
-        if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
184
-                && PD_NUMOBJ_GET(policy_engine_message) == 0) {
170
+        if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_ACCEPT
171
+                && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
185
             /* Transition to Sink Standby if necessary */
172
             /* Transition to Sink Standby if necessary */
186
             cfg->dpm.transition_standby(cfg);
173
             cfg->dpm.transition_standby(cfg);
187
 
174
 
188
-            min_power = false;
175
+            cfg->pe._min_power = false;
189
 
176
 
190
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
191
-            policy_engine_message = NULL;
177
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
178
+            cfg->pe._message = NULL;
192
             return PESinkTransitionSink;
179
             return PESinkTransitionSink;
193
         /* If the message was a Soft_Reset, do the soft reset procedure */
180
         /* If the message was a Soft_Reset, do the soft reset procedure */
194
-        } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
195
-                && PD_NUMOBJ_GET(policy_engine_message) == 0) {
196
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
197
-            policy_engine_message = NULL;
181
+        } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOFT_RESET
182
+                && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
183
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
184
+            cfg->pe._message = NULL;
198
             return PESinkSoftReset;
185
             return PESinkSoftReset;
199
         /* If the message was Wait or Reject */
186
         /* If the message was Wait or Reject */
200
-        } else if ((PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REJECT
201
-                    || PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT)
202
-                && PD_NUMOBJ_GET(policy_engine_message) == 0) {
187
+        } else if ((PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_REJECT
188
+                    || PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_WAIT)
189
+                && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
203
             /* If we don't have an explicit contract, wait for capabilities */
190
             /* If we don't have an explicit contract, wait for capabilities */
204
-            if (!explicit_contract) {
205
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
206
-                policy_engine_message = NULL;
191
+            if (!cfg->pe._explicit_contract) {
192
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
193
+                cfg->pe._message = NULL;
207
                 return PESinkWaitCap;
194
                 return PESinkWaitCap;
208
             /* If we do have an explicit contract, go to the ready state */
195
             /* If we do have an explicit contract, go to the ready state */
209
             } else {
196
             } else {
210
                 /* If we got here from a Wait message, we Should run
197
                 /* If we got here from a Wait message, we Should run
211
                  * SinkRequestTimer in the Ready state. */
198
                  * SinkRequestTimer in the Ready state. */
212
-                min_power = (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_WAIT);
199
+                cfg->pe._min_power = (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_WAIT);
213
 
200
 
214
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
215
-                policy_engine_message = NULL;
201
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
202
+                cfg->pe._message = NULL;
216
                 return PESinkReady;
203
                 return PESinkReady;
217
             }
204
             }
218
         } else {
205
         } else {
219
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
220
-            policy_engine_message = NULL;
206
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
207
+            cfg->pe._message = NULL;
221
             return PESinkSendSoftReset;
208
             return PESinkSendSoftReset;
222
         }
209
         }
223
     }
210
     }
239
     }
226
     }
240
 
227
 
241
     /* If we received a message, read it */
228
     /* If we received a message, read it */
242
-    if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
229
+    if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &cfg->pe._message, TIME_IMMEDIATE) == MSG_OK) {
243
         /* If we got a PS_RDY, handle it */
230
         /* If we got a PS_RDY, handle it */
244
-        if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PS_RDY
245
-                && PD_NUMOBJ_GET(policy_engine_message) == 0) {
231
+        if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_PS_RDY
232
+                && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
246
             /* We just finished negotiating an explicit contract */
233
             /* We just finished negotiating an explicit contract */
247
-            explicit_contract = true;
234
+            cfg->pe._explicit_contract = true;
248
 
235
 
249
             /* Set the output appropriately */
236
             /* Set the output appropriately */
250
-            if (!min_power) {
237
+            if (!cfg->pe._min_power) {
251
                 cfg->dpm.transition_requested(cfg);
238
                 cfg->dpm.transition_requested(cfg);
252
             }
239
             }
253
 
240
 
254
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
255
-            policy_engine_message = NULL;
241
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
242
+            cfg->pe._message = NULL;
256
             return PESinkReady;
243
             return PESinkReady;
257
         /* If there was a protocol error, send a hard reset */
244
         /* If there was a protocol error, send a hard reset */
258
         } else {
245
         } else {
261
              */
248
              */
262
             cfg->dpm.transition_default(cfg);
249
             cfg->dpm.transition_default(cfg);
263
 
250
 
264
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
265
-            policy_engine_message = NULL;
251
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
252
+            cfg->pe._message = NULL;
266
             return PESinkHardReset;
253
             return PESinkHardReset;
267
         }
254
         }
268
     }
255
     }
275
     eventmask_t evt;
262
     eventmask_t evt;
276
 
263
 
277
     /* Wait for an event */
264
     /* Wait for an event */
278
-    if (min_power) {
265
+    if (cfg->pe._min_power) {
279
         evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
266
         evt = chEvtWaitAnyTimeout(PDB_EVT_PE_MSG_RX | PDB_EVT_PE_RESET
280
                 | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP
267
                 | PDB_EVT_PE_I_OVRTEMP | PDB_EVT_PE_GET_SOURCE_CAP
281
                 | PDB_EVT_PE_NEW_POWER, PD_T_SINK_REQUEST);
268
                 | PDB_EVT_PE_NEW_POWER, PD_T_SINK_REQUEST);
306
      * design of this firmware. */
293
      * design of this firmware. */
307
     if (evt & PDB_EVT_PE_NEW_POWER) {
294
     if (evt & PDB_EVT_PE_NEW_POWER) {
308
         /* Make sure we're evaluating NULL capabilities to use the old ones */
295
         /* Make sure we're evaluating NULL capabilities to use the old ones */
309
-        if (policy_engine_message != NULL) {
310
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
311
-            policy_engine_message = NULL;
296
+        if (cfg->pe._message != NULL) {
297
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
298
+            cfg->pe._message = NULL;
312
         }
299
         }
313
         return PESinkEvalCap;
300
         return PESinkEvalCap;
314
     }
301
     }
321
 
308
 
322
     /* If we received a message */
309
     /* If we received a message */
323
     if (evt & PDB_EVT_PE_MSG_RX) {
310
     if (evt & PDB_EVT_PE_MSG_RX) {
324
-        if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
311
+        if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &cfg->pe._message, TIME_IMMEDIATE) == MSG_OK) {
325
             /* Ignore vendor-defined messages */
312
             /* Ignore vendor-defined messages */
326
-            if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VENDOR_DEFINED
327
-                    && PD_NUMOBJ_GET(policy_engine_message) > 0) {
328
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
329
-                policy_engine_message = NULL;
313
+            if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_VENDOR_DEFINED
314
+                    && PD_NUMOBJ_GET(cfg->pe._message) > 0) {
315
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
316
+                cfg->pe._message = NULL;
330
                 return PESinkReady;
317
                 return PESinkReady;
331
             /* Ignore Ping messages */
318
             /* Ignore Ping messages */
332
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PING
333
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
334
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
335
-                policy_engine_message = NULL;
319
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_PING
320
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
321
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
322
+                cfg->pe._message = NULL;
336
                 return PESinkReady;
323
                 return PESinkReady;
337
             /* Reject DR_Swap messages */
324
             /* Reject DR_Swap messages */
338
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_DR_SWAP
339
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
340
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
341
-                policy_engine_message = NULL;
325
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_DR_SWAP
326
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
327
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
328
+                cfg->pe._message = NULL;
342
                 return PESinkSendReject;
329
                 return PESinkSendReject;
343
             /* Reject Get_Source_Cap messages */
330
             /* Reject Get_Source_Cap messages */
344
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SOURCE_CAP
345
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
346
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
347
-                policy_engine_message = NULL;
331
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_GET_SOURCE_CAP
332
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
333
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
334
+                cfg->pe._message = NULL;
348
                 return PESinkSendReject;
335
                 return PESinkSendReject;
349
             /* Reject PR_Swap messages */
336
             /* Reject PR_Swap messages */
350
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_PR_SWAP
351
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
352
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
353
-                policy_engine_message = NULL;
337
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_PR_SWAP
338
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
339
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
340
+                cfg->pe._message = NULL;
354
                 return PESinkSendReject;
341
                 return PESinkSendReject;
355
             /* Reject VCONN_Swap messages */
342
             /* Reject VCONN_Swap messages */
356
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_VCONN_SWAP
357
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
358
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
359
-                policy_engine_message = NULL;
343
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_VCONN_SWAP
344
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
345
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
346
+                cfg->pe._message = NULL;
360
                 return PESinkSendReject;
347
                 return PESinkSendReject;
361
             /* Reject Request messages */
348
             /* Reject Request messages */
362
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_REQUEST
363
-                    && PD_NUMOBJ_GET(policy_engine_message) > 0) {
364
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
365
-                policy_engine_message = NULL;
349
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_REQUEST
350
+                    && PD_NUMOBJ_GET(cfg->pe._message) > 0) {
351
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
352
+                cfg->pe._message = NULL;
366
                 return PESinkSendReject;
353
                 return PESinkSendReject;
367
             /* Reject Sink_Capabilities messages */
354
             /* Reject Sink_Capabilities messages */
368
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SINK_CAPABILITIES
369
-                    && PD_NUMOBJ_GET(policy_engine_message) > 0) {
370
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
371
-                policy_engine_message = NULL;
355
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SINK_CAPABILITIES
356
+                    && PD_NUMOBJ_GET(cfg->pe._message) > 0) {
357
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
358
+                cfg->pe._message = NULL;
372
                 return PESinkSendReject;
359
                 return PESinkSendReject;
373
             /* Handle GotoMin messages */
360
             /* Handle GotoMin messages */
374
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GOTOMIN
375
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
361
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_GOTOMIN
362
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
376
                 if (cfg->dpm.giveback_enabled(cfg)) {
363
                 if (cfg->dpm.giveback_enabled(cfg)) {
377
                     /* Transition to the minimum current level */
364
                     /* Transition to the minimum current level */
378
                     cfg->dpm.transition_min(cfg);
365
                     cfg->dpm.transition_min(cfg);
379
-                    min_power = true;
366
+                    cfg->pe._min_power = true;
380
 
367
 
381
-                    chPoolFree(&pdb_msg_pool, policy_engine_message);
382
-                    policy_engine_message = NULL;
368
+                    chPoolFree(&pdb_msg_pool, cfg->pe._message);
369
+                    cfg->pe._message = NULL;
383
                     return PESinkTransitionSink;
370
                     return PESinkTransitionSink;
384
                 } else {
371
                 } else {
385
                     /* We don't support GiveBack, so send a Reject */
372
                     /* We don't support GiveBack, so send a Reject */
386
-                    chPoolFree(&pdb_msg_pool, policy_engine_message);
387
-                    policy_engine_message = NULL;
373
+                    chPoolFree(&pdb_msg_pool, cfg->pe._message);
374
+                    cfg->pe._message = NULL;
388
                     return PESinkSendReject;
375
                     return PESinkSendReject;
389
                 }
376
                 }
390
             /* Evaluate new Source_Capabilities */
377
             /* Evaluate new Source_Capabilities */
391
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOURCE_CAPABILITIES
392
-                    && PD_NUMOBJ_GET(policy_engine_message) > 0) {
378
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOURCE_CAPABILITIES
379
+                    && PD_NUMOBJ_GET(cfg->pe._message) > 0) {
393
                 /* Don't free the message: we need to keep the
380
                 /* Don't free the message: we need to keep the
394
                  * Source_Capabilities message so we can evaluate it. */
381
                  * Source_Capabilities message so we can evaluate it. */
395
                 return PESinkEvalCap;
382
                 return PESinkEvalCap;
396
             /* Give sink capabilities when asked */
383
             /* Give sink capabilities when asked */
397
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_GET_SINK_CAP
398
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
399
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
400
-                policy_engine_message = NULL;
384
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_GET_SINK_CAP
385
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
386
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
387
+                cfg->pe._message = NULL;
401
                 return PESinkGiveSinkCap;
388
                 return PESinkGiveSinkCap;
402
             /* If the message was a Soft_Reset, do the soft reset procedure */
389
             /* If the message was a Soft_Reset, do the soft reset procedure */
403
-            } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
404
-                    && PD_NUMOBJ_GET(policy_engine_message) == 0) {
405
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
406
-                policy_engine_message = NULL;
390
+            } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOFT_RESET
391
+                    && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
392
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
393
+                cfg->pe._message = NULL;
407
                 return PESinkSoftReset;
394
                 return PESinkSoftReset;
408
             /* If we got an unknown message, send a soft reset */
395
             /* If we got an unknown message, send a soft reset */
409
             } else {
396
             } else {
410
-                chPoolFree(&pdb_msg_pool, policy_engine_message);
411
-                policy_engine_message = NULL;
397
+                chPoolFree(&pdb_msg_pool, cfg->pe._message);
398
+                cfg->pe._message = NULL;
412
                 return PESinkSendSoftReset;
399
                 return PESinkSendSoftReset;
413
             }
400
             }
414
         }
401
         }
477
 {
464
 {
478
     /* If we've already sent the maximum number of hard resets, assume the
465
     /* If we've already sent the maximum number of hard resets, assume the
479
      * source is unresponsive. */
466
      * source is unresponsive. */
480
-    if (hard_reset_counter > PD_N_HARD_RESET_COUNT) {
467
+    if (cfg->pe._hard_reset_counter > PD_N_HARD_RESET_COUNT) {
481
         return PESinkSourceUnresponsive;
468
         return PESinkSourceUnresponsive;
482
     }
469
     }
483
 
470
 
486
     chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
473
     chEvtWaitAny(PDB_EVT_PE_HARD_SENT);
487
 
474
 
488
     /* Increment HardResetCounter */
475
     /* Increment HardResetCounter */
489
-    hard_reset_counter++;
476
+    cfg->pe._hard_reset_counter++;
490
 
477
 
491
     return PESinkTransitionDefault;
478
     return PESinkTransitionDefault;
492
 }
479
 }
493
 
480
 
494
 static enum policy_engine_state pe_sink_transition_default(struct pdb_config *cfg)
481
 static enum policy_engine_state pe_sink_transition_default(struct pdb_config *cfg)
495
 {
482
 {
496
-    explicit_contract = false;
483
+    cfg->pe._explicit_contract = false;
497
 
484
 
498
     /* Tell the DPM to transition to default power */
485
     /* Tell the DPM to transition to default power */
499
     cfg->dpm.transition_default(cfg);
486
     cfg->dpm.transition_default(cfg);
578
     }
565
     }
579
 
566
 
580
     /* Get the response message */
567
     /* Get the response message */
581
-    if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &policy_engine_message, TIME_IMMEDIATE) == MSG_OK) {
568
+    if (chMBFetch(&cfg->pe.mailbox, (msg_t *) &cfg->pe._message, TIME_IMMEDIATE) == MSG_OK) {
582
         /* If the source accepted our soft reset, wait for capabilities. */
569
         /* If the source accepted our soft reset, wait for capabilities. */
583
-        if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_ACCEPT
584
-                && PD_NUMOBJ_GET(policy_engine_message) == 0) {
585
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
586
-            policy_engine_message = NULL;
570
+        if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_ACCEPT
571
+                && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
572
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
573
+            cfg->pe._message = NULL;
587
             return PESinkWaitCap;
574
             return PESinkWaitCap;
588
         /* If the message was a Soft_Reset, do the soft reset procedure */
575
         /* If the message was a Soft_Reset, do the soft reset procedure */
589
-        } else if (PD_MSGTYPE_GET(policy_engine_message) == PD_MSGTYPE_SOFT_RESET
590
-                && PD_NUMOBJ_GET(policy_engine_message) == 0) {
591
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
592
-            policy_engine_message = NULL;
576
+        } else if (PD_MSGTYPE_GET(cfg->pe._message) == PD_MSGTYPE_SOFT_RESET
577
+                && PD_NUMOBJ_GET(cfg->pe._message) == 0) {
578
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
579
+            cfg->pe._message = NULL;
593
             return PESinkSoftReset;
580
             return PESinkSoftReset;
594
         /* Otherwise, send a hard reset */
581
         /* Otherwise, send a hard reset */
595
         } else {
582
         } else {
596
-            chPoolFree(&pdb_msg_pool, policy_engine_message);
597
-            policy_engine_message = NULL;
583
+            chPoolFree(&pdb_msg_pool, cfg->pe._message);
584
+            cfg->pe._message = NULL;
598
             return PESinkHardReset;
585
             return PESinkHardReset;
599
         }
586
         }
600
     }
587
     }

Loading…
Cancel
Save