|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2022 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <app/ChunkedWriteCallback.h> |
| 20 | + |
| 21 | +namespace chip { |
| 22 | +namespace app { |
| 23 | + |
| 24 | +void ChunkedWriteCallback::OnResponse(const WriteClient * apWriteClient, const ConcreteDataAttributePath & aPath, StatusIB aStatus) |
| 25 | +{ |
| 26 | + // We may send a chunked list. To make the behavior consistent whether a list is being chunked or not, |
| 27 | + // we merge the write responses for a chunked list here and provide our consumer with a single status response. |
| 28 | + if (mLastAttributePath.HasValue()) |
| 29 | + { |
| 30 | + // This is not the first write response. |
| 31 | + if (IsAppendingToLastItem(aPath)) |
| 32 | + { |
| 33 | + // This is a response on the same path as what we already have stored. Report the first |
| 34 | + // failure status we encountered, and ignore subsequent ones. |
| 35 | + if (mAttributeStatus.IsSuccess()) |
| 36 | + { |
| 37 | + mAttributeStatus = aStatus; |
| 38 | + } |
| 39 | + return; |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + // This is a response to another attribute write. Report the final result of last attribute write. |
| 44 | + callback->OnResponse(apWriteClient, mLastAttributePath.Value(), mAttributeStatus); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // This is the first report for a new attribute. We assume it will never be a list item operation. |
| 49 | + if (aPath.IsListItemOperation()) |
| 50 | + { |
| 51 | + aStatus = StatusIB(CHIP_ERROR_INCORRECT_STATE); |
| 52 | + } |
| 53 | + |
| 54 | + mLastAttributePath.SetValue(aPath); |
| 55 | + mAttributeStatus = aStatus; |
| 56 | + // For the last status in the response, we will call the application callback in OnDone() |
| 57 | +} |
| 58 | + |
| 59 | +void ChunkedWriteCallback::OnError(const WriteClient * apWriteClient, CHIP_ERROR aError) |
| 60 | +{ |
| 61 | + callback->OnError(apWriteClient, aError); |
| 62 | +} |
| 63 | + |
| 64 | +void ChunkedWriteCallback::OnDone(WriteClient * apWriteClient) |
| 65 | +{ |
| 66 | + if (mLastAttributePath.HasValue()) |
| 67 | + { |
| 68 | + // We have a cached status that has yet to be reported to the application so report it now. |
| 69 | + // If we failed to receive the response, or we received a malformed response, OnResponse won't be called, |
| 70 | + // mLastAttributePath will be Missing() in this case. |
| 71 | + callback->OnResponse(apWriteClient, mLastAttributePath.Value(), mAttributeStatus); |
| 72 | + } |
| 73 | + |
| 74 | + callback->OnDone(apWriteClient); |
| 75 | +} |
| 76 | + |
| 77 | +bool ChunkedWriteCallback::IsAppendingToLastItem(const ConcreteDataAttributePath & aPath) |
| 78 | +{ |
| 79 | + if (!aPath.IsListItemOperation()) |
| 80 | + { |
| 81 | + return false; |
| 82 | + } |
| 83 | + if (!mLastAttributePath.HasValue() || !(mLastAttributePath.Value() == aPath)) |
| 84 | + { |
| 85 | + return false; |
| 86 | + } |
| 87 | + return aPath.mListOp == ConcreteDataAttributePath::ListOperation::AppendItem; |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace app |
| 91 | +} // namespace chip |
0 commit comments