| 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 99 |
| 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 | |
| 111 } // namespace | 109 } // namespace |
| 112 | 110 |
| 113 BattOrAgent::BattOrAgent( | 111 BattOrAgent::BattOrAgent( |
| 114 const std::string& path, | 112 const std::string& path, |
| 115 Listener* listener, | 113 Listener* listener, |
| 116 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 114 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
| 117 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) | 115 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
| 118 : connection_(new BattOrConnectionImpl(path, | 116 : connection_(new BattOrConnectionImpl(path, |
| 119 this, | 117 this, |
| 120 file_thread_task_runner, | 118 file_thread_task_runner, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 150 } |
| 153 | 151 |
| 154 void BattOrAgent::RecordClockSyncMarker(const std::string& marker) { | 152 void BattOrAgent::RecordClockSyncMarker(const std::string& marker) { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 153 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 | 154 |
| 157 command_ = Command::RECORD_CLOCK_SYNC_MARKER; | 155 command_ = Command::RECORD_CLOCK_SYNC_MARKER; |
| 158 pending_clock_sync_marker_ = marker; | 156 pending_clock_sync_marker_ = marker; |
| 159 PerformAction(Action::REQUEST_CONNECTION); | 157 PerformAction(Action::REQUEST_CONNECTION); |
| 160 } | 158 } |
| 161 | 159 |
| 160 void BattOrAgent::GetFirmwareGitHash() { |
| 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 162 |
| 163 command_ = Command::GET_FIRMWARE_GIT_HASH; |
| 164 PerformAction(Action::REQUEST_CONNECTION); |
| 165 } |
| 166 |
| 162 void BattOrAgent::BeginConnect() { | 167 void BattOrAgent::BeginConnect() { |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | 168 DCHECK(thread_checker_.CalledOnValidThread()); |
| 164 | 169 |
| 165 connection_->Open(); | 170 connection_->Open(); |
| 166 } | 171 } |
| 167 | 172 |
| 168 void BattOrAgent::OnConnectionOpened(bool success) { | 173 void BattOrAgent::OnConnectionOpened(bool success) { |
| 169 // Return immediately if opening the connection already timed out. | 174 // Return immediately if opening the connection already timed out. |
| 170 if (timeout_callback_.IsCancelled()) | 175 if (timeout_callback_.IsCancelled()) |
| 171 return; | 176 return; |
| 172 timeout_callback_.Cancel(); | 177 timeout_callback_.Cancel(); |
| 173 | 178 |
| 174 if (!success) { | 179 if (!success) { |
| 175 CompleteCommand(BATTOR_ERROR_CONNECTION_FAILED); | 180 CompleteCommand(BATTOR_ERROR_CONNECTION_FAILED); |
| 176 return; | 181 return; |
| 177 } | 182 } |
| 178 | 183 |
| 179 switch (command_) { | 184 switch (command_) { |
| 180 case Command::START_TRACING: | 185 case Command::START_TRACING: |
| 181 num_init_attempts_ = 1; | 186 num_init_attempts_ = 1; |
| 182 PerformAction(Action::SEND_INIT); | 187 PerformAction(Action::SEND_INIT); |
| 183 return; | 188 return; |
| 184 case Command::STOP_TRACING: | 189 case Command::STOP_TRACING: |
| 185 PerformAction(Action::SEND_EEPROM_REQUEST); | 190 PerformAction(Action::SEND_EEPROM_REQUEST); |
| 186 return; | 191 return; |
| 187 case Command::RECORD_CLOCK_SYNC_MARKER: | 192 case Command::RECORD_CLOCK_SYNC_MARKER: |
| 188 PerformAction(Action::SEND_CURRENT_SAMPLE_REQUEST); | 193 PerformAction(Action::SEND_CURRENT_SAMPLE_REQUEST); |
| 189 return; | 194 return; |
| 195 case Command::GET_FIRMWARE_GIT_HASH: |
| 196 PerformAction(Action::SEND_GIT_HASH_REQUEST); |
| 197 return; |
| 190 case Command::INVALID: | 198 case Command::INVALID: |
| 191 NOTREACHED(); | 199 NOTREACHED(); |
| 192 } | 200 } |
| 193 } | 201 } |
| 194 | 202 |
| 195 void BattOrAgent::OnBytesSent(bool success) { | 203 void BattOrAgent::OnBytesSent(bool success) { |
| 196 DCHECK(thread_checker_.CalledOnValidThread()); | 204 DCHECK(thread_checker_.CalledOnValidThread()); |
| 197 | 205 |
| 198 // Return immediately if whatever action we were trying to perform already | 206 // Return immediately if whatever action we were trying to perform already |
| 199 // timed out. | 207 // timed out. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 221 PerformAction(Action::READ_EEPROM); | 229 PerformAction(Action::READ_EEPROM); |
| 222 return; | 230 return; |
| 223 case Action::SEND_SAMPLES_REQUEST: | 231 case Action::SEND_SAMPLES_REQUEST: |
| 224 num_read_attempts_ = 1; | 232 num_read_attempts_ = 1; |
| 225 PerformAction(Action::READ_CALIBRATION_FRAME); | 233 PerformAction(Action::READ_CALIBRATION_FRAME); |
| 226 return; | 234 return; |
| 227 case Action::SEND_CURRENT_SAMPLE_REQUEST: | 235 case Action::SEND_CURRENT_SAMPLE_REQUEST: |
| 228 num_read_attempts_ = 1; | 236 num_read_attempts_ = 1; |
| 229 PerformAction(Action::READ_CURRENT_SAMPLE); | 237 PerformAction(Action::READ_CURRENT_SAMPLE); |
| 230 return; | 238 return; |
| 239 case Action::SEND_GIT_HASH_REQUEST: |
| 240 num_read_attempts_ = 1; |
| 241 PerformAction(Action::READ_GIT_HASH); |
| 242 return; |
| 231 default: | 243 default: |
| 232 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | 244 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| 233 } | 245 } |
| 234 } | 246 } |
| 235 | 247 |
| 236 void BattOrAgent::OnMessageRead(bool success, | 248 void BattOrAgent::OnMessageRead(bool success, |
| 237 BattOrMessageType type, | 249 BattOrMessageType type, |
| 238 std::unique_ptr<vector<char>> bytes) { | 250 std::unique_ptr<vector<char>> bytes) { |
| 239 // Return immediately if whatever action we were trying to perform already | 251 // Return immediately if whatever action we were trying to perform already |
| 240 // timed out. | 252 // timed out. |
| 241 if (timeout_callback_.IsCancelled()) | 253 if (timeout_callback_.IsCancelled()) |
| 242 return; | 254 return; |
| 243 timeout_callback_.Cancel(); | 255 timeout_callback_.Cancel(); |
| 244 | 256 |
| 245 if (!success) { | 257 if (!success) { |
| 246 switch (last_action_) { | 258 switch (last_action_) { |
| 259 case Action::READ_GIT_HASH: |
| 247 case Action::READ_EEPROM: | 260 case Action::READ_EEPROM: |
| 248 case Action::READ_CALIBRATION_FRAME: | 261 case Action::READ_CALIBRATION_FRAME: |
| 249 case Action::READ_DATA_FRAME: | 262 case Action::READ_DATA_FRAME: |
| 250 case Action::READ_CURRENT_SAMPLE: | 263 case Action::READ_CURRENT_SAMPLE: |
| 251 if (num_read_attempts_++ > kMaxReadAttempts) { | 264 if (num_read_attempts_++ > kMaxReadAttempts) { |
| 252 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); | 265 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); |
| 253 return; | 266 return; |
| 254 } | 267 } |
| 255 | 268 |
| 256 PerformDelayedAction(last_action_, base::TimeDelta::FromMilliseconds( | 269 PerformDelayedAction(last_action_, base::TimeDelta::FromMilliseconds( |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 return; | 393 return; |
| 381 } | 394 } |
| 382 | 395 |
| 383 uint32_t sample_num; | 396 uint32_t sample_num; |
| 384 memcpy(&sample_num, bytes->data(), sizeof(uint32_t)); | 397 memcpy(&sample_num, bytes->data(), sizeof(uint32_t)); |
| 385 clock_sync_markers_[sample_num] = pending_clock_sync_marker_; | 398 clock_sync_markers_[sample_num] = pending_clock_sync_marker_; |
| 386 last_clock_sync_time_ = base::TimeTicks::Now(); | 399 last_clock_sync_time_ = base::TimeTicks::Now(); |
| 387 CompleteCommand(BATTOR_ERROR_NONE); | 400 CompleteCommand(BATTOR_ERROR_NONE); |
| 388 return; | 401 return; |
| 389 | 402 |
| 403 case Action::READ_GIT_HASH: |
| 404 if (type != BATTOR_MESSAGE_TYPE_CONTROL_ACK ){ |
| 405 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| 406 return; |
| 407 } |
| 408 firmware_git_hash_ = std::string(bytes->begin(), bytes->end()); |
| 409 CompleteCommand(BATTOR_ERROR_NONE); |
| 410 return; |
| 411 |
| 390 default: | 412 default: |
| 391 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | 413 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| 392 } | 414 } |
| 393 } | 415 } |
| 394 | 416 |
| 395 void BattOrAgent::PerformAction(Action action) { | 417 void BattOrAgent::PerformAction(Action action) { |
| 396 DCHECK(thread_checker_.CalledOnValidThread()); | 418 DCHECK(thread_checker_.CalledOnValidThread()); |
| 397 | 419 |
| 398 timeout_callback_.Reset( | 420 timeout_callback_.Reset( |
| 399 base::Bind(&BattOrAgent::OnActionTimeout, AsWeakPtr())); | 421 base::Bind(&BattOrAgent::OnActionTimeout, AsWeakPtr())); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 return; | 488 return; |
| 467 | 489 |
| 468 // The following actions are required for RecordClockSyncMarker: | 490 // The following actions are required for RecordClockSyncMarker: |
| 469 case Action::SEND_CURRENT_SAMPLE_REQUEST: | 491 case Action::SEND_CURRENT_SAMPLE_REQUEST: |
| 470 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_READ_SAMPLE_COUNT, 0, 0); | 492 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_READ_SAMPLE_COUNT, 0, 0); |
| 471 return; | 493 return; |
| 472 case Action::READ_CURRENT_SAMPLE: | 494 case Action::READ_CURRENT_SAMPLE: |
| 473 connection_->ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK); | 495 connection_->ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK); |
| 474 return; | 496 return; |
| 475 | 497 |
| 498 case Action::SEND_GIT_HASH_REQUEST: |
| 499 SendControlMessage( |
| 500 BATTOR_CONTROL_MESSAGE_TYPE_GET_FIRMWARE_GIT_HASH, 0, 0); |
| 501 return; |
| 502 |
| 503 case Action::READ_GIT_HASH: |
| 504 connection_->ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK); |
| 505 return; |
| 506 |
| 476 case Action::INVALID: | 507 case Action::INVALID: |
| 477 NOTREACHED(); | 508 NOTREACHED(); |
| 478 } | 509 } |
| 479 } | 510 } |
| 480 | 511 |
| 481 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { | 512 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { |
| 482 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 513 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 483 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), | 514 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), |
| 484 delay); | 515 delay); |
| 485 } | 516 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 base::ThreadTaskRunnerHandle::Get()->PostTask( | 554 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 524 FROM_HERE, | 555 FROM_HERE, |
| 525 base::Bind(&Listener::OnStopTracingComplete, | 556 base::Bind(&Listener::OnStopTracingComplete, |
| 526 base::Unretained(listener_), SamplesToString(), error)); | 557 base::Unretained(listener_), SamplesToString(), error)); |
| 527 break; | 558 break; |
| 528 case Command::RECORD_CLOCK_SYNC_MARKER: | 559 case Command::RECORD_CLOCK_SYNC_MARKER: |
| 529 base::ThreadTaskRunnerHandle::Get()->PostTask( | 560 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 530 FROM_HERE, base::Bind(&Listener::OnRecordClockSyncMarkerComplete, | 561 FROM_HERE, base::Bind(&Listener::OnRecordClockSyncMarkerComplete, |
| 531 base::Unretained(listener_), error)); | 562 base::Unretained(listener_), error)); |
| 532 break; | 563 break; |
| 564 case Command::GET_FIRMWARE_GIT_HASH: |
| 565 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 566 FROM_HERE, base::Bind(&Listener::OnGetFirmwareGitHashComplete, |
| 567 base::Unretained(listener_), |
| 568 firmware_git_hash_, error)); |
| 569 break; |
| 533 case Command::INVALID: | 570 case Command::INVALID: |
| 534 NOTREACHED(); | 571 NOTREACHED(); |
| 535 } | 572 } |
| 536 | 573 |
| 537 last_action_ = Action::INVALID; | 574 last_action_ = Action::INVALID; |
| 538 command_ = Command::INVALID; | 575 command_ = Command::INVALID; |
| 539 pending_clock_sync_marker_.clear(); | 576 pending_clock_sync_marker_.clear(); |
| 540 battor_eeprom_.reset(); | 577 battor_eeprom_.reset(); |
| 541 calibration_frame_.clear(); | 578 calibration_frame_.clear(); |
| 542 samples_.clear(); | 579 samples_.clear(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 if (clock_sync_marker != clock_sync_markers_.end()) | 614 if (clock_sync_marker != clock_sync_markers_.end()) |
| 578 trace_stream << " <" << clock_sync_marker->second << ">"; | 615 trace_stream << " <" << clock_sync_marker->second << ">"; |
| 579 | 616 |
| 580 trace_stream << std::endl; | 617 trace_stream << std::endl; |
| 581 } | 618 } |
| 582 | 619 |
| 583 return trace_stream.str(); | 620 return trace_stream.str(); |
| 584 } | 621 } |
| 585 | 622 |
| 586 } // namespace battor | 623 } // namespace battor |
| OLD | NEW |