Implementation of connectivity abstraction feature Release v0.3
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / caconnectivitymanager.c
1 /******************************************************************
2  *
3  * Copyright 2014 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 <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24
25 #include "cainterface.h"
26 #include "caremotehandler.h"
27 #include "camessagehandler.h"
28 #include "caprotocolmessage.h"
29 #include "canetworkconfigurator.h"
30 #include "cainterfacecontroller.h"
31 #include "logger.h"
32
33 #define TAG PCF("CA")
34
35 CAResult_t CAInitialize()
36 {
37     OIC_LOG_V(DEBUG, TAG, "CAInitialize");
38
39     CAResult_t res = CAInitializeMessageHandler();
40
41     if (res != CA_STATUS_OK)
42     {
43         return res;
44     }
45
46     return CA_STATUS_OK;
47 }
48
49 void CATerminate()
50 {
51     OIC_LOG_V(DEBUG, TAG, "CATerminate");
52
53     CATerminateMessageHandler();
54 }
55
56 CAResult_t CAStartListeningServer()
57 {
58     OIC_LOG_V(DEBUG, TAG, "CAStartListeningServer");
59
60     return CAStartListeningServerAdapters();
61 }
62
63 CAResult_t CAStartDiscoveryServer()
64 {
65     OIC_LOG_V(DEBUG, TAG, "CAStartDiscoveryServer");
66
67     return CAStartDiscoveryServerAdapters();
68 }
69
70 CAResult_t CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
71 {
72     OIC_LOG_V(DEBUG, TAG, "CARegisterHandler");
73
74     CASetRequestResponseCallbacks(ReqHandler, RespHandler);
75
76     return CA_STATUS_OK;
77 }
78
79 CAResult_t CACreateRemoteEndpoint(const CAURI_t uri, CARemoteEndpoint_t** remoteEndpoint)
80 {
81     OIC_LOG_V(DEBUG, TAG, "CACreateRemoteEndpoint");
82
83     CARemoteEndpoint_t* remote = CACreateRemoteEndpointUriInternal(uri);
84
85     *remoteEndpoint = remote;
86
87     if (remote == NULL)
88         return CA_STATUS_FAILED;
89
90     return CA_STATUS_OK;
91 }
92
93 void CADestroyRemoteEndpoint(CARemoteEndpoint_t* rep)
94 {
95     OIC_LOG_V(DEBUG, TAG, "CADestroyRemoteEndpoint");
96
97     CADestroyRemoteEndpointInternal(rep);
98 }
99
100 CAResult_t CAGenerateToken(CAToken_t* token)
101 {
102     OIC_LOG_V(DEBUG, TAG, "CAGenerateToken");
103
104     return CAGenerateTokenInternal(token);
105 }
106
107 void CADestroyToken(CAToken_t token)
108 {
109     OIC_LOG_V(DEBUG, TAG, "CADestroyToken");
110
111     CADestroyTokenInternal(token);
112 }
113
114 CAResult_t CAGetNetworkInformation(CALocalConnectivity_t **info, uint32_t* size)
115 {
116     OIC_LOG_V(DEBUG, TAG, "CAGetNetworkInformation");
117
118     return CAGetNetworkInformationInternal(info, size);
119 }
120
121 CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token)
122 {
123     OIC_LOG_V(DEBUG, TAG, "CAFindResource");
124
125     return CADetachMessageResourceUri(resourceUri, token, NULL, 0);
126
127 }
128
129 CAResult_t CASendRequest(const CARemoteEndpoint_t* object, CARequestInfo_t* requestInfo)
130 {
131     OIC_LOG_V(DEBUG, TAG, "CASendGetRequest");
132
133     return CADetachRequestMessage(object, requestInfo);
134 }
135
136 CAResult_t CASendNotification(const CARemoteEndpoint_t* object, CAResponseInfo_t* responseInfo)
137 {
138     OIC_LOG_V(DEBUG, TAG, "CASendNotification");
139
140     return CADetachResponseMessage(object, responseInfo);
141
142 }
143
144 CAResult_t CASendResponse(const CARemoteEndpoint_t* object, CAResponseInfo_t* responseInfo)
145 {
146     OIC_LOG_V(DEBUG, TAG, "CASendResponse");
147
148     return CADetachResponseMessage(object, responseInfo);
149
150 }
151
152 CAResult_t CAAdvertiseResource(const CAURI_t resourceUri, CAToken_t token, CAHeaderOption_t* options,
153         uint8_t numOptions)
154 {
155     OIC_LOG_V(DEBUG, TAG, "CAAdvertiseResource");
156
157     return CADetachMessageResourceUri(resourceUri, token, options, numOptions);
158
159 }
160
161 CAResult_t CASelectNetwork(const uint32_t interestedNetwork)
162 {
163     OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
164
165     if (!(interestedNetwork & 0xf))
166     {
167         return CA_NOT_SUPPORTED;
168     }
169     CAResult_t res;
170
171     if (interestedNetwork & CA_ETHERNET)
172     {
173         res = CAAddNetworkType(CA_ETHERNET);
174         if (res != CA_STATUS_OK)
175         {
176             return res;
177         }
178     }
179
180     if (interestedNetwork & CA_WIFI)
181     {
182         res = CAAddNetworkType(CA_WIFI);
183         if (res != CA_STATUS_OK)
184         {
185             return res;
186         }
187     }
188
189     if (interestedNetwork & CA_EDR)
190     {
191         res = CAAddNetworkType(CA_EDR);
192         if (res != CA_STATUS_OK)
193         {
194             return res;
195         }
196     }
197
198     if (interestedNetwork & CA_LE)
199     {
200         res = CAAddNetworkType(CA_LE);
201         if (res != CA_STATUS_OK)
202         {
203             return res;
204         }
205     }
206
207     return CA_STATUS_OK;
208 }
209
210 CAResult_t CAUnSelectNetwork(const uint32_t nonInterestedNetwork)
211 {
212     OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
213
214     if (!(nonInterestedNetwork & 0xf))
215     {
216         return CA_NOT_SUPPORTED;
217     }
218
219     CAResult_t res;
220
221     if (nonInterestedNetwork & CA_ETHERNET)
222     {
223         res = CARemoveNetworkType(CA_ETHERNET);
224         if (res != CA_STATUS_OK)
225         {
226             return res;
227         }
228     }
229
230     if (nonInterestedNetwork & CA_WIFI)
231     {
232         res = CARemoveNetworkType(CA_WIFI);
233         if (res != CA_STATUS_OK)
234         {
235             return res;
236         }
237     }
238
239     if (nonInterestedNetwork & CA_EDR)
240     {
241         res = CARemoveNetworkType(CA_EDR);
242         if (res != CA_STATUS_OK)
243         {
244             return res;
245         }
246     }
247
248     if (nonInterestedNetwork & CA_LE)
249     {
250         res = CARemoveNetworkType(CA_LE);
251         if (res != CA_STATUS_OK)
252         {
253             return res;
254         }
255     }
256
257     return CA_STATUS_OK;
258 }
259
260 CAResult_t CAHandleRequestResponse()
261 {
262     OIC_LOG_V(DEBUG, TAG, "CAHandleRequestResponse");
263
264     CAHandleRequestResponseCallbacks();
265
266     return CA_STATUS_OK;
267 }
268