Merge branch 'tizen' into tizen_5.5
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / caipnwmonitor_common.c
1 /******************************************************************
2 *
3 * Copyright 2019 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20
21 #include "caipnwmonitor.h"
22 #include "caipnwmonitor_common.h"
23
24 #include "utlist.h"
25 #include "logger.h"
26
27 #define TAG "OIC_CA_IP_MONITOR_COMMON"
28
29 /**
30  * @todo For Windows platform: Implement network interface monitoring.
31  * Not used in win32, but caipserver currently requires this function
32  * be defined. not critical.
33  */
34 int CAGetPollingInterval(int interval)
35 {
36     return interval;
37 }
38
39 void CAIPPassNetworkChangesToAdapterInternal(CANetworkStatus_t status,
40     CAIPCBData_t *adapterCallbackList, CAIPAdapterPlatform_t platform)
41 {
42     CAIPCBData_t *cbitem = NULL;
43     LL_FOREACH(adapterCallbackList, cbitem)
44     {
45         if (cbitem && cbitem->adapter)
46         {
47             cbitem->callback(cbitem->adapter, status);
48             if (platform == CA_IP_NW_COMMON_LINUX)
49             {
50                 CALogAdapterStateInfo(cbitem->adapter, status);
51             }
52         }
53     }
54 }
55
56 CAResult_t CAIPSetNetworkMonitorCallbackInternal(CAIPAdapterStateChangeCallback callback,
57                                          CATransportAdapter_t adapter, CAIPCBData_t *adapterCallbackList)
58 {
59     if (!callback)
60     {
61         OIC_LOG(ERROR, TAG, "callback is null");
62         return CA_STATUS_INVALID_PARAM;
63     }
64
65     CAIPCBData_t *cbitem = NULL;
66     LL_FOREACH(adapterCallbackList, cbitem)
67     {
68         if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback)
69         {
70             OIC_LOG(DEBUG, TAG, "this callback is already added");
71             return CA_STATUS_OK;
72         }
73     }
74
75     cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem));
76     if (!cbitem)
77     {
78         OIC_LOG(ERROR, TAG, "Malloc failed");
79         return CA_STATUS_FAILED;
80     }
81
82     cbitem->adapter = adapter;
83     cbitem->callback = callback;
84     LL_APPEND(adapterCallbackList, cbitem);
85
86     return CA_STATUS_OK;
87 }
88
89 CAResult_t CAIPUnSetNetworkMonitorCallbackInternal(CATransportAdapter_t adapter,
90                                          CAIPCBData_t *adapterCallbackList)
91 {
92     CAIPCBData_t *cbitem = NULL;
93     CAIPCBData_t *tmpCbitem = NULL;
94     LL_FOREACH_SAFE(adapterCallbackList, cbitem, tmpCbitem)
95     {
96         if (cbitem && adapter == cbitem->adapter)
97         {
98             OIC_LOG(DEBUG, TAG, "remove specific callback");
99             LL_DELETE(adapterCallbackList, cbitem);
100             OICFree(cbitem);
101             return CA_STATUS_OK;
102         }
103     }
104     return CA_STATUS_OK;
105 }