Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/battor_agent/battor_agent.h" | 5 #include "tools/battor_agent/battor_agent.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
| 9 #include "tools/battor_agent/battor_connection_impl.h" | 9 #include "tools/battor_agent/battor_connection_impl.h" |
| 10 | 10 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 PerformAction(Action::READ_SET_GAIN_ACK); | 107 PerformAction(Action::READ_SET_GAIN_ACK); |
| 108 break; | 108 break; |
| 109 case Action::SEND_START_TRACING: | 109 case Action::SEND_START_TRACING: |
| 110 PerformAction(Action::READ_START_TRACING_ACK); | 110 PerformAction(Action::READ_START_TRACING_ACK); |
| 111 break; | 111 break; |
| 112 default: | 112 default: |
| 113 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | 113 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 | 116 |
| 117 void BattOrAgent::OnBytesRead(bool success, | 117 void BattOrAgent::OnMessageRead(bool success, |
| 118 BattOrMessageType type, | 118 BattOrMessageType type, |
| 119 scoped_ptr<vector<char>> bytes) { | 119 scoped_ptr<vector<char>> bytes) { |
| 120 if (!success) { | 120 if (!success) { |
| 121 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); | 121 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); |
| 122 return; | 122 return; |
| 123 } | 123 } |
| 124 | 124 |
| 125 switch (last_action_) { | 125 switch (last_action_) { |
| 126 case Action::READ_INIT_ACK: | 126 case Action::READ_INIT_ACK: |
| 127 if (!IsAckOfControlCommand(type, BATTOR_CONTROL_MESSAGE_TYPE_INIT, | 127 if (!IsAckOfControlCommand(type, BATTOR_CONTROL_MESSAGE_TYPE_INIT, |
| 128 *bytes)) { | 128 *bytes)) { |
| 129 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | 129 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0); | 175 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_RESET, 0, 0); |
| 176 break; | 176 break; |
| 177 case Action::SEND_INIT: | 177 case Action::SEND_INIT: |
| 178 // After resetting the BattOr, we need to make sure to flush the serial | 178 // After resetting the BattOr, we need to make sure to flush the serial |
| 179 // stream. Strange data may have been written into it during the reset. | 179 // stream. Strange data may have been written into it during the reset. |
| 180 connection_->Flush(); | 180 connection_->Flush(); |
| 181 | 181 |
| 182 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0); | 182 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0); |
| 183 break; | 183 break; |
| 184 case Action::READ_INIT_ACK: | 184 case Action::READ_INIT_ACK: |
| 185 connection_->ReadBytes(sizeof(BattOrControlMessageAck)); | 185 connection_->ReadMessage(sizeof(BattOrControlMessageAck)); |
|
nednguyen
2016/01/06 22:03:13
Can yo abstract this to ReadMessage(message_type),
charliea (OOO until 10-5)
2016/01/07 16:00:12
I like this idea a lot. Done.
| |
| 186 break; | 186 break; |
| 187 case Action::SEND_SET_GAIN: | 187 case Action::SEND_SET_GAIN: |
| 188 // Set the BattOr's gain. Setting the gain tells the BattOr the range of | 188 // Set the BattOr's gain. Setting the gain tells the BattOr the range of |
| 189 // power measurements that we expect to see. | 189 // power measurements that we expect to see. |
| 190 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, BATTOR_GAIN_LOW, | 190 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, BATTOR_GAIN_LOW, |
| 191 0); | 191 0); |
| 192 break; | 192 break; |
| 193 case Action::READ_SET_GAIN_ACK: | 193 case Action::READ_SET_GAIN_ACK: |
| 194 connection_->ReadBytes(sizeof(BattOrControlMessageAck)); | 194 connection_->ReadMessage(sizeof(BattOrControlMessageAck)); |
| 195 break; | 195 break; |
| 196 case Action::SEND_START_TRACING: | 196 case Action::SEND_START_TRACING: |
| 197 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0, 0); | 197 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0, 0); |
| 198 break; | 198 break; |
| 199 case Action::READ_START_TRACING_ACK: | 199 case Action::READ_START_TRACING_ACK: |
| 200 connection_->ReadBytes(sizeof(BattOrControlMessageAck)); | 200 connection_->ReadMessage(sizeof(BattOrControlMessageAck)); |
| 201 break; | 201 break; |
| 202 case Action::INVALID: | 202 case Action::INVALID: |
| 203 NOTREACHED(); | 203 NOTREACHED(); |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 | 206 |
| 207 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { | 207 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { |
| 208 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 208 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 209 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), | 209 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), |
| 210 delay); | 210 delay); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 226 break; | 226 break; |
| 227 case Command::INVALID: | 227 case Command::INVALID: |
| 228 NOTREACHED(); | 228 NOTREACHED(); |
| 229 } | 229 } |
| 230 | 230 |
| 231 last_action_ = Action::INVALID; | 231 last_action_ = Action::INVALID; |
| 232 command_ = Command::INVALID; | 232 command_ = Command::INVALID; |
| 233 } | 233 } |
| 234 | 234 |
| 235 } // namespace battor | 235 } // namespace battor |
| OLD | NEW |