Line data Source code
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 33 : 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 33 : if (mProcessingAttributePath.HasValue()) 29 : { 30 : // This is not the first write response. 31 26 : 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 20 : if (mAttributeStatus.IsSuccess()) 36 : { 37 8 : mAttributeStatus = aStatus; 38 : } 39 20 : return; 40 : } 41 : 42 : // This is a response to another attribute write. Report the final result of last attribute write. 43 6 : callback->OnResponse(apWriteClient, mProcessingAttributePath.Value(), mAttributeStatus); 44 : } 45 : 46 : // This is the first report for a new attribute. We assume it will never be a list item operation. 47 13 : if (aPath.IsListItemOperation()) 48 : { 49 0 : aStatus = StatusIB(CHIP_ERROR_INCORRECT_STATE); 50 : } 51 : 52 13 : mProcessingAttributePath.SetValue(aPath); 53 13 : mAttributeStatus = aStatus; 54 : // For the last status in the response, we will call the application callback in OnDone() 55 : } 56 : 57 0 : void ChunkedWriteCallback::OnError(const WriteClient * apWriteClient, CHIP_ERROR aError) 58 : { 59 0 : callback->OnError(apWriteClient, aError); 60 0 : } 61 : 62 7 : void ChunkedWriteCallback::OnDone(WriteClient * apWriteClient) 63 : { 64 7 : if (mProcessingAttributePath.HasValue()) 65 : { 66 : // We have a cached status that has yet to be reported to the application so report it now. 67 : // If we failed to receive the response, or we received a malformed response, OnResponse won't be called, 68 : // mProcessingAttributePath will be Missing() in this case. 69 7 : callback->OnResponse(apWriteClient, mProcessingAttributePath.Value(), mAttributeStatus); 70 : } 71 : 72 7 : mProcessingAttributePath = NullOptional; 73 7 : mAttributeStatus = StatusIB(); 74 : 75 7 : callback->OnDone(apWriteClient); 76 7 : } 77 : 78 26 : bool ChunkedWriteCallback::IsAppendingToLastItem(const ConcreteDataAttributePath & aPath) 79 : { 80 26 : if (!aPath.IsListItemOperation()) 81 : { 82 6 : return false; 83 : } 84 20 : if (!mProcessingAttributePath.HasValue() || !(mProcessingAttributePath.Value() == aPath)) 85 : { 86 0 : return false; 87 : } 88 20 : return aPath.mListOp == ConcreteDataAttributePath::ListOperation::AppendItem; 89 : } 90 : 91 : } // namespace app 92 : } // namespace chip