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 #include <iostream> |
| 5 #include "tools/battor_agent/battor_agent.h" | 5 #include "tools/battor_agent/battor_agent.h" |
| 6 | 6 |
| 7 #include <iomanip> | 7 #include <iomanip> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "tools/battor_agent/battor_connection_impl.h" | 11 #include "tools/battor_agent/battor_connection_impl.h" |
| 12 #include "tools/battor_agent/battor_sample_converter.h" | 12 #include "tools/battor_agent/battor_sample_converter.h" |
| 13 | 13 |
| 14 using std::vector; | 14 using std::vector; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 } | 152 } |
| 153 | 153 |
| 154 void BattOrAgent::RecordClockSyncMarker(const std::string& marker) { | 154 void BattOrAgent::RecordClockSyncMarker(const std::string& marker) { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 155 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 | 156 |
| 157 command_ = Command::RECORD_CLOCK_SYNC_MARKER; | 157 command_ = Command::RECORD_CLOCK_SYNC_MARKER; |
| 158 pending_clock_sync_marker_ = marker; | 158 pending_clock_sync_marker_ = marker; |
| 159 PerformAction(Action::REQUEST_CONNECTION); | 159 PerformAction(Action::REQUEST_CONNECTION); |
| 160 } | 160 } |
| 161 | 161 |
| 162 void BattOrAgent::GetVersion() { | |
| 163 std::cout << "START GetVersion()\n"; | |
| 164 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 165 | |
| 166 command_ = Command::GET_VERSION; | |
| 167 PerformAction(Action::REQUEST_CONNECTION); | |
| 168 } | |
| 169 | |
| 162 void BattOrAgent::BeginConnect() { | 170 void BattOrAgent::BeginConnect() { |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 164 | 172 |
| 165 connection_->Open(); | 173 connection_->Open(); |
| 166 } | 174 } |
| 167 | 175 |
| 168 void BattOrAgent::OnConnectionOpened(bool success) { | 176 void BattOrAgent::OnConnectionOpened(bool success) { |
| 169 // Return immediately if opening the connection already timed out. | 177 // Return immediately if opening the connection already timed out. |
| 170 if (timeout_callback_.IsCancelled()) | 178 if (timeout_callback_.IsCancelled()) |
| 171 return; | 179 return; |
| 172 timeout_callback_.Cancel(); | 180 timeout_callback_.Cancel(); |
| 173 | 181 |
| 174 if (!success) { | 182 if (!success) { |
| 175 CompleteCommand(BATTOR_ERROR_CONNECTION_FAILED); | 183 CompleteCommand(BATTOR_ERROR_CONNECTION_FAILED); |
| 176 return; | 184 return; |
| 177 } | 185 } |
| 178 | 186 |
| 179 switch (command_) { | 187 switch (command_) { |
| 180 case Command::START_TRACING: | 188 case Command::START_TRACING: |
| 181 num_init_attempts_ = 1; | 189 num_init_attempts_ = 1; |
| 182 PerformAction(Action::SEND_INIT); | 190 PerformAction(Action::SEND_INIT); |
| 183 return; | 191 return; |
| 184 case Command::STOP_TRACING: | 192 case Command::STOP_TRACING: |
| 185 PerformAction(Action::SEND_EEPROM_REQUEST); | 193 PerformAction(Action::SEND_EEPROM_REQUEST); |
| 186 return; | 194 return; |
| 187 case Command::RECORD_CLOCK_SYNC_MARKER: | 195 case Command::RECORD_CLOCK_SYNC_MARKER: |
| 188 PerformAction(Action::SEND_CURRENT_SAMPLE_REQUEST); | 196 PerformAction(Action::SEND_CURRENT_SAMPLE_REQUEST); |
| 189 return; | 197 return; |
| 198 case Command::GET_VERSION: | |
| 199 std::cout << "OnConnectionOpened.GET_VERSION\n"; | |
| 200 PerformAction(Action::SEND_GIT_HASH_REQUEST); | |
| 201 return; | |
| 190 case Command::INVALID: | 202 case Command::INVALID: |
| 191 NOTREACHED(); | 203 NOTREACHED(); |
| 192 } | 204 } |
| 193 } | 205 } |
| 194 | 206 |
| 195 void BattOrAgent::OnBytesSent(bool success) { | 207 void BattOrAgent::OnBytesSent(bool success) { |
| 196 DCHECK(thread_checker_.CalledOnValidThread()); | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 197 | 209 |
| 198 // Return immediately if whatever action we were trying to perform already | 210 // Return immediately if whatever action we were trying to perform already |
| 199 // timed out. | 211 // timed out. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 221 PerformAction(Action::READ_EEPROM); | 233 PerformAction(Action::READ_EEPROM); |
| 222 return; | 234 return; |
| 223 case Action::SEND_SAMPLES_REQUEST: | 235 case Action::SEND_SAMPLES_REQUEST: |
| 224 num_read_attempts_ = 1; | 236 num_read_attempts_ = 1; |
| 225 PerformAction(Action::READ_CALIBRATION_FRAME); | 237 PerformAction(Action::READ_CALIBRATION_FRAME); |
| 226 return; | 238 return; |
| 227 case Action::SEND_CURRENT_SAMPLE_REQUEST: | 239 case Action::SEND_CURRENT_SAMPLE_REQUEST: |
| 228 num_read_attempts_ = 1; | 240 num_read_attempts_ = 1; |
| 229 PerformAction(Action::READ_CURRENT_SAMPLE); | 241 PerformAction(Action::READ_CURRENT_SAMPLE); |
| 230 return; | 242 return; |
| 243 case Action::SEND_GIT_HASH_REQUEST: | |
| 244 std::cout << "OnBytesSent SEND_GIT_HASH_REQUEST\n"; | |
| 245 num_read_attempts_ = 1; | |
| 246 PerformAction(Action::READ_GIT_HASH); | |
| 231 default: | 247 default: |
| 232 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); | 248 CompleteCommand(BATTOR_ERROR_UNEXPECTED_MESSAGE); |
| 233 } | 249 } |
| 234 } | 250 } |
| 235 | 251 |
| 236 void BattOrAgent::OnMessageRead(bool success, | 252 void BattOrAgent::OnMessageRead(bool success, |
| 237 BattOrMessageType type, | 253 BattOrMessageType type, |
| 238 std::unique_ptr<vector<char>> bytes) { | 254 std::unique_ptr<vector<char>> bytes) { |
| 239 // Return immediately if whatever action we were trying to perform already | 255 // Return immediately if whatever action we were trying to perform already |
| 240 // timed out. | 256 // timed out. |
| 241 if (timeout_callback_.IsCancelled()) | 257 if (timeout_callback_.IsCancelled()) |
| 242 return; | 258 return; |
| 243 timeout_callback_.Cancel(); | 259 timeout_callback_.Cancel(); |
| 244 | 260 |
| 245 if (!success) { | 261 if (!success) { |
| 246 switch (last_action_) { | 262 switch (last_action_) { |
| 263 case Action::READ_GIT_HASH: | |
| 247 case Action::READ_EEPROM: | 264 case Action::READ_EEPROM: |
| 248 case Action::READ_CALIBRATION_FRAME: | 265 case Action::READ_CALIBRATION_FRAME: |
| 249 case Action::READ_DATA_FRAME: | 266 case Action::READ_DATA_FRAME: |
| 250 case Action::READ_CURRENT_SAMPLE: | 267 case Action::READ_CURRENT_SAMPLE: |
| 251 if (num_read_attempts_++ > kMaxReadAttempts) { | 268 if (num_read_attempts_++ > kMaxReadAttempts) { |
| 252 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); | 269 CompleteCommand(BATTOR_ERROR_RECEIVE_ERROR); |
| 253 return; | 270 return; |
| 254 } | 271 } |
| 255 | 272 |
| 256 PerformDelayedAction(last_action_, base::TimeDelta::FromMilliseconds( | 273 PerformDelayedAction(last_action_, base::TimeDelta::FromMilliseconds( |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 return; | 397 return; |
| 381 } | 398 } |
| 382 | 399 |
| 383 uint32_t sample_num; | 400 uint32_t sample_num; |
| 384 memcpy(&sample_num, bytes->data(), sizeof(uint32_t)); | 401 memcpy(&sample_num, bytes->data(), sizeof(uint32_t)); |
| 385 clock_sync_markers_[sample_num] = pending_clock_sync_marker_; | 402 clock_sync_markers_[sample_num] = pending_clock_sync_marker_; |
| 386 last_clock_sync_time_ = base::TimeTicks::Now(); | 403 last_clock_sync_time_ = base::TimeTicks::Now(); |
| 387 CompleteCommand(BATTOR_ERROR_NONE); | 404 CompleteCommand(BATTOR_ERROR_NONE); |
| 388 return; | 405 return; |
| 389 | 406 |
| 407 case Action::READ_GIT_HASH: | |
|
rnephew (Reviews Here)
2016/10/05 17:14:21
Note to self: This isn't being triggered ever. Why
| |
| 408 std::cout << "OnMessageRead READ_GIT_HASH\n"; | |
| 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 std::cout << "PerformAction SEND_GIT_HASH_REQUEST\n"; | |
| 500 SendControlMessage(BATTOR_CONTROL_MESSAGE_TYPE_GET_GIT_HASH, 0, 0); | |
| 501 return; | |
| 502 | |
| 503 case Action::READ_GIT_HASH: | |
| 504 std::cout << "PerformAction READ_GIT_HASH\n"; | |
| 505 connection_->ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK); | |
| 506 return; | |
| 507 | |
| 476 case Action::INVALID: | 508 case Action::INVALID: |
| 477 NOTREACHED(); | 509 NOTREACHED(); |
| 478 } | 510 } |
| 479 } | 511 } |
| 480 | 512 |
| 481 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { | 513 void BattOrAgent::PerformDelayedAction(Action action, base::TimeDelta delay) { |
| 482 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 514 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 483 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), | 515 FROM_HERE, base::Bind(&BattOrAgent::PerformAction, AsWeakPtr(), action), |
| 484 delay); | 516 delay); |
| 485 } | 517 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 base::ThreadTaskRunnerHandle::Get()->PostTask( | 555 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 524 FROM_HERE, | 556 FROM_HERE, |
| 525 base::Bind(&Listener::OnStopTracingComplete, | 557 base::Bind(&Listener::OnStopTracingComplete, |
| 526 base::Unretained(listener_), SamplesToString(), error)); | 558 base::Unretained(listener_), SamplesToString(), error)); |
| 527 break; | 559 break; |
| 528 case Command::RECORD_CLOCK_SYNC_MARKER: | 560 case Command::RECORD_CLOCK_SYNC_MARKER: |
| 529 base::ThreadTaskRunnerHandle::Get()->PostTask( | 561 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 530 FROM_HERE, base::Bind(&Listener::OnRecordClockSyncMarkerComplete, | 562 FROM_HERE, base::Bind(&Listener::OnRecordClockSyncMarkerComplete, |
| 531 base::Unretained(listener_), error)); | 563 base::Unretained(listener_), error)); |
| 532 break; | 564 break; |
| 565 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | |
| 566 // Need to get proper version number. | |
| 567 case Command::GET_VERSION: | |
| 568 std::cout << "Complete GET_VERSION\n"; | |
| 569 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 570 FROM_HERE, base::Bind(&Listener::OnGetVersionComplete, | |
| 571 base::Unretained(listener_), | |
| 572 1, error)); | |
| 573 break; | |
| 533 case Command::INVALID: | 574 case Command::INVALID: |
| 534 NOTREACHED(); | 575 NOTREACHED(); |
| 535 } | 576 } |
| 536 | 577 |
| 537 last_action_ = Action::INVALID; | 578 last_action_ = Action::INVALID; |
| 538 command_ = Command::INVALID; | 579 command_ = Command::INVALID; |
| 539 pending_clock_sync_marker_.clear(); | 580 pending_clock_sync_marker_.clear(); |
| 540 battor_eeprom_.reset(); | 581 battor_eeprom_.reset(); |
| 541 calibration_frame_.clear(); | 582 calibration_frame_.clear(); |
| 542 samples_.clear(); | 583 samples_.clear(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 if (clock_sync_marker != clock_sync_markers_.end()) | 618 if (clock_sync_marker != clock_sync_markers_.end()) |
| 578 trace_stream << " <" << clock_sync_marker->second << ">"; | 619 trace_stream << " <" << clock_sync_marker->second << ">"; |
| 579 | 620 |
| 580 trace_stream << std::endl; | 621 trace_stream << std::endl; |
| 581 } | 622 } |
| 582 | 623 |
| 583 return trace_stream.str(); | 624 return trace_stream.str(); |
| 584 } | 625 } |
| 585 | 626 |
| 586 } // namespace battor | 627 } // namespace battor |
| OLD | NEW |