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 | |
| 5 #include "tools/battor_agent/battor_agent.h" | 4 #include "tools/battor_agent/battor_agent.h" |
| 6 | 5 |
| 7 #include <iomanip> | 6 #include <iomanip> |
| 8 | 7 |
| 9 #include "base/bind.h" | 8 #include "base/bind.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "tools/battor_agent/battor_connection_impl.h" | 10 #include "tools/battor_agent/battor_connection_impl.h" |
| 12 #include "tools/battor_agent/battor_sample_converter.h" | 11 #include "tools/battor_agent/battor_sample_converter.h" |
| 13 | 12 |
| 14 using std::vector; | 13 using std::vector; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 size_t remaining_bytes = msg.size() - sizeof(BattOrFrameHeader); | 100 size_t remaining_bytes = msg.size() - sizeof(BattOrFrameHeader); |
| 102 if (remaining_bytes != frame_header->length) | 101 if (remaining_bytes != frame_header->length) |
| 103 return false; | 102 return false; |
| 104 | 103 |
| 105 samples->resize(remaining_bytes / sizeof(RawBattOrSample)); | 104 samples->resize(remaining_bytes / sizeof(RawBattOrSample)); |
| 106 memcpy(samples->data(), frame_ptr, remaining_bytes); | 105 memcpy(samples->data(), frame_ptr, remaining_bytes); |
| 107 | 106 |
| 108 return true; | 107 return true; |
| 109 } | 108 } |
| 110 | 109 |
| 110 bool ParseGitHashFrame(BattOrMessageType type, | |
|
charliea (OOO until 10-5)
2016/10/18 23:46:29
I think it's probably okay to do this inline in On
charliea (OOO until 10-5)
2016/10/18 23:46:29
I think that, if you use an std::string, this enti
rnephew (Reviews Here)
2016/10/19 18:23:47
Done.
| |
| 111 const vector<char>& msg, | |
| 112 char* git_hash){ | |
| 113 if (type != BATTOR_MESSAGE_TYPE_CONTROL_ACK) | |
| 114 return false; | |
| 115 memcpy(git_hash, msg.data(), msg.size()); | |
| 116 return true; | |
| 117 } | |
| 111 } // namespace | 118 } // namespace |
| 112 | 119 |
| 113 BattOrAgent::BattOrAgent( | 120 BattOrAgent::BattOrAgent( |
| 114 const std::string& path, | 121 const std::string& path, |
| 115 Listener* listener, | 122 Listener* listener, |
| 116 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 123 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
| 117 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) | 124 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
| 118 : connection_(new BattOrConnectionImpl(path, | 125 : connection_(new BattOrConnectionImpl(path, |
| 119 this, | 126 this, |
| 120 file_thread_task_runner, | 127 file_thread_task_runner, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 } | 159 } |
| 153 | 160 |
| 154 void BattOrAgent::RecordClockSyncMarker(const std::string& marker) { | 161 void BattOrAgent::RecordClockSyncMarker(const std::string& marker) { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 162 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 | 163 |
| 157 command_ = Command::RECORD_CLOCK_SYNC_MARKER; | 164 command_ = Command::RECORD_CLOCK_SYNC_MARKER; |
| 158 pending_clock_sync_marker_ = marker; | 165 pending_clock_sync_marker_ = marker; |
| 159 PerformAction(Action::REQUEST_CONNECTION); | 166 PerformAction(Action::REQUEST_CONNECTION); |
| 160 } | 167 } |
| 161 | 168 |
| 169 void BattOrAgent::GetVersion() { | |
| 170 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 171 | |
| 172 command_ = Command::GET_VERSION; | |
| 173 PerformAction(Action::REQUEST_CONNECTION); | |
| 174 } | |
| 175 | |
| 162 void BattOrAgent::BeginConnect() { | 176 void BattOrAgent::BeginConnect() { |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | 177 DCHECK(thread_checker_.CalledOnValidThread()); |
| 164 | 178 |
| 165 connection_->Open(); | 179 connection_->Open(); |
| 166 } | 180 } |
| 167 | 181 |
| 168 void BattOrAgent::OnConnectionOpened(bool success) { | 182 void BattOrAgent::OnConnectionOpened(bool success) { |
| 169 // Return immediately if opening the connection already timed out. | 183 // Return immediately if opening the connection already timed out. |
| 170 if (timeout_callback_.IsCancelled()) | 184 if (timeout_callback_.IsCancelled()) |
| 171 return; | 185 return; |
| 172 timeout_callback_.Cancel(); | 186 timeout_callback_.Cancel(); |
| 173 | 187 |
| 174 if (!success) { | 188 if (!success) { |
| 175 CompleteCommand(BATTOR_ERROR_CONNECTION_FAILED); | 189 CompleteCommand(BATTOR_ERROR_CONNECTION_FAILED); |
| 176 return; | 190 return; |
| 177 } | 191 } |
| 178 | 192 |
| 179 switch (command_) { | 193 switch (command_) { |
| 180 case Command::START_TRACING: | 194 case Command::START_TRACING: |
| 181 num_init_attempts_ = 1; | 195 num_init_attempts_ = 1; |
| 182 PerformAction(Action::SEND_INIT); | 196 PerformAction(Action::SEND_INIT); |
| 183 return; | 197 return; |
| 184 case Command::STOP_TRACING: | 198 case Command::STOP_TRACING: |
| 185 PerformAction(Action::SEND_EEPROM_REQUEST); | 199 PerformAction(Action::SEND_EEPROM_REQUEST); |
| 186 return; | 200 return; |
| 187 case Command::RECORD_CLOCK_SYNC_MARKER: | 201 case Command::RECORD_CLOCK_SYNC_MARKER: |
| 188 PerformAction(Action::SEND_CURRENT_SAMPLE_REQUEST); | 202 PerformAction(Action::SEND_CURRENT_SAMPLE_REQUEST); |
| 189 return; | 203 return; |
| 204 case Command::GET_VERSION: | |
| 205 PerformAction(Action::SEND_GIT_HASH_REQUEST); | |
| 206 return; | |
| 190 case Command::INVALID: | 207 case Command::INVALID: |
| 191 NOTREACHED(); | 208 NOTREACHED(); |
| 192 } | 209 } |
| 193 } | 210 } |
| 194 | 211 |
| 195 void BattOrAgent::OnBytesSent(bool success) { | 212 void BattOrAgent::OnBytesSent(bool success) { |
| 196 DCHECK(thread_checker_.CalledOnValidThread()); | 213 DCHECK(thread_checker_.CalledOnValidThread()); |
| 197 | 214 |
| 198 // Return immediately if whatever action we were trying to perform already | 215 // Return immediately if whatever action we were trying to perform already |
| 199 // timed out. | 216 // timed out. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 221 PerformAction(Action::READ_EEPROM); | 238 PerformAction(Action::READ_EEPROM); |
| 222 return; | 239 return; |
| 223 case Action::SEND_SAMPLES_REQUEST: | 240 case Action::SEND_SAMPLES_REQUEST: |
| 224 num_read_attempts_ = 1; | 241 num_read_attempts_ = 1; |
| 225 PerformAction(Action::READ_CALIBRATION_FRAME); | 242 PerformAction(Action::READ_CALIBRATION_FRAME); |
| 226 return; | 243 return; |
| 227 case Action::SEND_CURRENT_SAMPLE_REQUEST: | 244 case Action::SEND_CURRENT_SAMPLE_REQUEST: |
| 228 num_read_attempts_ = 1; | 245 num_read_attempts_ = 1; |
| 229 PerformAction(Action::READ_CURRENT_SAMPLE); | 246 PerformAction(Action::READ_CURRENT_SAMPLE); |
| 230 return; | 247 return; |
| 248 case Action::SEND_GIT_HASH_REQUEST: | |
| 249 num_read_attempts_ = 1; | |
| 250 PerformAction(Action::READ_GIT_HASH); | |
| 251 return; | |
| 231 default: | 252 default: |
| 232 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | 253 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| 233 } | 254 } |
| 234 } | 255 } |
| 235 | 256 |
| 236 void BattOrAgent::OnMessageRead(bool success, | 257 void BattOrAgent::OnMessageRead(bool success, |
| 237 BattOrMessageType type, | 258 BattOrMessageType type, |
| 238 std::unique_ptr<vector<char>> bytes) { | 259 std::unique_ptr<vector<char>> bytes) { |
| 239 // Return immediately if whatever action we were trying to perform already | 260 // Return immediately if whatever action we were trying to perform already |
| 240 // timed out. | 261 // timed out. |
| 241 if (timeout_callback_.IsCancelled()) | 262 if (timeout_callback_.IsCancelled()) |
| 242 return; | 263 return; |
| 243 timeout_callback_.Cancel(); | 264 timeout_callback_.Cancel(); |
| 244 | 265 |
| 245 if (!success) { | 266 if (!success) { |
| 246 switch (last_action_) { | 267 switch (last_action_) { |
| 268 case Action::READ_GIT_HASH: | |
| 247 case Action::READ_EEPROM: | 269 case Action::READ_EEPROM: |
| 248 case Action::READ_CALIBRATION_FRAME: | 270 case Action::READ_CALIBRATION_FRAME: |
| 249 case Action::READ_DATA_FRAME: | 271 case Action::READ_DATA_FRAME: |
| 250 case Action::READ_CURRENT_SAMPLE: | 272 case Action::READ_CURRENT_SAMPLE: |
| 251 if (num_read_attempts_++ > kMaxReadAttempts) { | 273 if (num_read_attempts_++ > kMaxReadAttempts) { |
| 252 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); | 274 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); |
| 253 return; | 275 return; |
| 254 } | 276 } |
| 255 | 277 |
| 256 PerformDelayedAction(last_action_, base::TimeDelta::FromMilliseconds( | 278 PerformDelayedAction(last_action_, base::TimeDelta::FromMilliseconds( |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 return; | 402 return; |
| 381 } | 403 } |
| 382 | 404 |
| 383 uint32_t sample_num; | 405 uint32_t sample_num; |
| 384 memcpy(&sample_num, bytes->data(), sizeof(uint32_t)); | 406 memcpy(&sample_num, bytes->data(), sizeof(uint32_t)); |
| 385 clock_sync_markers_[sample_num] = pending_clock_sync_marker_; | 407 clock_sync_markers_[sample_num] = pending_clock_sync_marker_; |
| 386 last_clock_sync_time_ = base::TimeTicks::Now(); | 408 last_clock_sync_time_ = base::TimeTicks::Now(); |
| 387 CompleteCommand(BATTOR_ERROR_NONE); | 409 CompleteCommand(BATTOR_ERROR_NONE); |
| 388 return; | 410 return; |
| 389 | 411 |
| 412 case Action::READ_GIT_HASH: | |
| 413 // Get size to allocate space for git_hash_. | |
| 414 if (bytes->size() <= 0 ){ | |
|
charliea (OOO until 10-5)
2016/10/18 23:46:29
Also don't think this should be necessary once we
rnephew (Reviews Here)
2016/10/19 18:23:47
Done.
| |
| 415 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | |
| 416 return; | |
| 417 } | |
| 418 git_hash_ = (char*) malloc(bytes->size()); | |
| 419 | |
| 420 if(!ParseGitHashFrame(type, *bytes, git_hash_)) { | |
| 421 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | |
| 422 return; | |
| 423 } | |
| 424 CompleteCommand(BATTOR_ERROR_NONE); | |
| 425 return; | |
| 426 | |
| 390 default: | 427 default: |
| 391 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | 428 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| 392 } | 429 } |
| 393 } | 430 } |
| 394 | 431 |
| 395 void BattOrAgent::PerformAction(Action action) { | 432 void BattOrAgent::PerformAction(Action action) { |
| 396 DCHECK(thread_checker_.CalledOnValidThread()); | 433 DCHECK(thread_checker_.CalledOnValidThread()); |
| 397 | 434 |
| 398 timeout_callback_.Reset( | 435 timeout_callback_.Reset( |
| 399 base::Bind(&BattOrAgent::OnActionTimeout, AsWeakPtr())); | 436 base::Bind(&BattOrAgent::OnActionTimeout, AsWeakPtr())); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 466 return; | 503 return; |
| 467 | 504 |
| 468 // The following actions are required for RecordClockSyncMarker: | 505 // The following actions are required for RecordClockSyncMarker: |
| 469 case Action::SEND_CURRENT_SAMPLE_REQUEST: | 506 case Action::SEND_CURRENT_SAMPLE_REQUEST: |
| 470 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_READ_SAMPLE_COUNT, 0, 0); | 507 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_READ_SAMPLE_COUNT, 0, 0); |
| 471 return; | 508 return; |
| 472 case Action::READ_CURRENT_SAMPLE: | 509 case Action::READ_CURRENT_SAMPLE: |
| 473 connection_->ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK); | 510 connection_->ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK); |
| 474 return; | 511 return; |
| 475 | 512 |
| 513 case Action::SEND_GIT_HASH_REQUEST: | |
| 514 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_GET_GIT_HASH, 0, 0); | |
| 515 return; | |
| 516 | |
| 517 case Action::READ_GIT_HASH: | |
| 518 connection_->ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK); | |
| 519 return; | |
| 520 | |
| 476 case Action::INVALID: | 521 case Action::INVALID: |
| 477 NOTREACHED(); | 522 NOTREACHED(); |
| 478 } | 523 } |
| 479 } | 524 } |
| 480 | 525 |
| 481 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { | 526 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { |
| 482 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 527 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 483 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), | 528 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), |
| 484 delay); | 529 delay); |
| 485 } | 530 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 base::ThreadTaskRunnerHandle::Get()->PostTask( | 568 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 524 FROM_HERE, | 569 FROM_HERE, |
| 525 base::Bind(&Listener::OnStopTracingComplete, | 570 base::Bind(&Listener::OnStopTracingComplete, |
| 526 base::Unretained(listener_), SamplesToString(), error)); | 571 base::Unretained(listener_), SamplesToString(), error)); |
| 527 break; | 572 break; |
| 528 case Command::RECORD_CLOCK_SYNC_MARKER: | 573 case Command::RECORD_CLOCK_SYNC_MARKER: |
| 529 base::ThreadTaskRunnerHandle::Get()->PostTask( | 574 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 530 FROM_HERE, base::Bind(&Listener::OnRecordClockSyncMarkerComplete, | 575 FROM_HERE, base::Bind(&Listener::OnRecordClockSyncMarkerComplete, |
| 531 base::Unretained(listener_), error)); | 576 base::Unretained(listener_), error)); |
| 532 break; | 577 break; |
| 578 case Command::GET_VERSION: | |
| 579 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 580 FROM_HERE, base::Bind(&Listener::OnGetVersionComplete, | |
| 581 base::Unretained(listener_), | |
| 582 git_hash_, error)); | |
| 583 break; | |
| 533 case Command::INVALID: | 584 case Command::INVALID: |
| 534 NOTREACHED(); | 585 NOTREACHED(); |
| 535 } | 586 } |
| 536 | 587 |
| 537 last_action_ = Action::INVALID; | 588 last_action_ = Action::INVALID; |
| 538 command_ = Command::INVALID; | 589 command_ = Command::INVALID; |
| 539 pending_clock_sync_marker_.clear(); | 590 pending_clock_sync_marker_.clear(); |
| 540 battor_eeprom_.reset(); | 591 battor_eeprom_.reset(); |
| 541 calibration_frame_.clear(); | 592 calibration_frame_.clear(); |
| 542 samples_.clear(); | 593 samples_.clear(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 if (clock_sync_marker != clock_sync_markers_.end()) | 628 if (clock_sync_marker != clock_sync_markers_.end()) |
| 578 trace_stream << " <" << clock_sync_marker->second << ">"; | 629 trace_stream << " <" << clock_sync_marker->second << ">"; |
| 579 | 630 |
| 580 trace_stream << std::endl; | 631 trace_stream << std::endl; |
| 581 } | 632 } |
| 582 | 633 |
| 583 return trace_stream.str(); | 634 return trace_stream.str(); |
| 584 } | 635 } |
| 585 | 636 |
| 586 } // namespace battor | 637 } // namespace battor |
| OLD | NEW |