1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_transfer/transfer.h"
16
17 #include "pw_assert/check.h"
18 #include "pw_log/log.h"
19 #include "pw_status/try.h"
20 #include "pw_transfer/internal/chunk.h"
21
22 namespace pw::transfer {
23
HandleChunk(ConstByteSpan message,internal::TransferType type)24 void TransferService::HandleChunk(ConstByteSpan message,
25 internal::TransferType type) {
26 Result<internal::Chunk> chunk = internal::Chunk::Parse(message);
27 if (!chunk.ok()) {
28 PW_LOG_ERROR("Failed to decode transfer chunk: %d", chunk.status().code());
29 return;
30 }
31
32 if (chunk->IsInitialChunk()) {
33 uint32_t session_id =
34 chunk->is_legacy() ? chunk->session_id() : GenerateNewSessionId();
35 uint32_t resource_id =
36 chunk->is_legacy() ? chunk->session_id() : chunk->resource_id().value();
37
38 thread_.StartServerTransfer(type,
39 chunk->protocol_version(),
40 session_id,
41 resource_id,
42 message,
43 max_parameters_,
44 chunk_timeout_,
45 max_retries_,
46 max_lifetime_retries_);
47 } else {
48 thread_.ProcessServerChunk(message);
49 }
50 }
51
52 } // namespace pw::transfer
53