Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / src / darwin / Framework / CHIP / CHIPDeviceStatusDelegateBridge.mm
1 /**
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 #import "CHIPDeviceStatusDelegateBridge.h"
19 #import <Foundation/Foundation.h>
20
21 CHIPDeviceStatusDelegateBridge::CHIPDeviceStatusDelegateBridge(void)
22     : mDelegate(nil)
23 {
24 }
25
26 CHIPDeviceStatusDelegateBridge::~CHIPDeviceStatusDelegateBridge() {}
27
28 void CHIPDeviceStatusDelegateBridge::setDelegate(id<CHIPDeviceStatusDelegate> delegate, dispatch_queue_t queue)
29 {
30     if (delegate && queue) {
31         mDelegate = delegate;
32         mQueue = queue;
33     } else {
34         mDelegate = nil;
35         mQueue = nil;
36     }
37 }
38
39 void CHIPDeviceStatusDelegateBridge::OnMessage(chip::System::PacketBufferHandle message)
40 {
41     NSLog(@"DeviceStatusDelegate received message from the device");
42
43     size_t data_len = message->DataLength();
44     // convert to NSData
45     NSMutableData * dataBuffer = [[NSMutableData alloc] initWithBytes:message->Start() length:data_len];
46     message.FreeHead();
47
48     while (!message.IsNull()) {
49         data_len = message->DataLength();
50         [dataBuffer appendBytes:message->Start() length:data_len];
51         message.FreeHead();
52     }
53
54     id<CHIPDeviceStatusDelegate> strongDelegate = mDelegate;
55     if (strongDelegate && mQueue) {
56         dispatch_async(mQueue, ^{
57             [strongDelegate onMessageReceived:dataBuffer];
58         });
59     }
60 }
61
62 void CHIPDeviceStatusDelegateBridge::OnStatusChange() { NSLog(@"CHIPDeviceStatusDelegateBridge device status changed"); }