API for DTLS registration and Scon script addition
[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 #ifdef __WITH_DTLS__
36 // CAAdapterNetDTLS will register the callback.
37 // Taking callback all the way through adapters not the right approach, hence calling here.
38 extern void CADTLSSetCredentialsCallback(CAGetDTLSCredentialsHandler credCallback);
39 #endif
40
41 CAResult_t CAInitialize()
42 {
43     OIC_LOG_V(DEBUG, TAG, "CAInitialize");
44
45     CAResult_t res = CAInitializeMessageHandler();
46
47     if (res != CA_STATUS_OK)
48     {
49         return res;
50     }
51
52     return CA_STATUS_OK;
53 }
54
55 void CATerminate()
56 {
57     OIC_LOG_V(DEBUG, TAG, "CATerminate");
58
59     CATerminateMessageHandler();
60
61     CATerminateNetworkType();
62 }
63
64 CAResult_t CAStartListeningServer()
65 {
66     OIC_LOG_V(DEBUG, TAG, "CAStartListeningServer");
67
68     return CAStartListeningServerAdapters();
69 }
70
71 CAResult_t CAStartDiscoveryServer()
72 {
73     OIC_LOG_V(DEBUG, TAG, "CAStartDiscoveryServer");
74
75     return CAStartDiscoveryServerAdapters();
76 }
77
78 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
79 {
80     OIC_LOG_V(DEBUG, TAG, "CARegisterHandler");
81
82     CASetRequestResponseCallbacks(ReqHandler, RespHandler);
83 }
84
85 #ifdef __WITH_DTLS__
86 CAResult_t CARegisterDTLSCredentialsHandler(
87                                              CAGetDTLSCredentialsHandler GetDTLSCredentialsHandler)
88 {
89     OIC_LOG(DEBUG, TAG, "CARegisterDTLSCredentialsHandler");
90     CADTLSSetCredentialsCallback(GetDTLSCredentialsHandler);
91     return CA_STATUS_OK;
92 }
93 #endif //__WITH_DTLS__
94
95 CAResult_t CACreateRemoteEndpoint(const CAURI_t uri,
96     const CAConnectivityType_t connectivityType,CARemoteEndpoint_t **remoteEndpoint)
97 {
98     OIC_LOG_V(DEBUG, TAG, "CACreateRemoteEndpoint");
99
100     CARemoteEndpoint_t *remote = CACreateRemoteEndpointUriInternal(uri, connectivityType);
101
102     *remoteEndpoint = remote;
103
104     if (remote == NULL)
105         return CA_STATUS_FAILED;
106
107     return CA_STATUS_OK;
108 }
109
110 void CADestroyRemoteEndpoint(CARemoteEndpoint_t *rep)
111 {
112     OIC_LOG_V(DEBUG, TAG, "CADestroyRemoteEndpoint");
113
114     CADestroyRemoteEndpointInternal(rep);
115 }
116
117 CAResult_t CAGenerateToken(CAToken_t *token)
118 {
119     OIC_LOG_V(DEBUG, TAG, "CAGenerateToken");
120
121     return CAGenerateTokenInternal(token);
122 }
123
124 void CADestroyToken(CAToken_t token)
125 {
126     OIC_LOG_V(DEBUG, TAG, "CADestroyToken");
127
128     CADestroyTokenInternal(token);
129 }
130
131 CAResult_t CAGetNetworkInformation(CALocalConnectivity_t **info, uint32_t *size)
132 {
133     OIC_LOG_V(DEBUG, TAG, "CAGetNetworkInformation");
134
135     return CAGetNetworkInformationInternal(info, size);
136 }
137
138 CAResult_t CAFindResource(const CAURI_t resourceUri, const CAToken_t token)
139 {
140     OIC_LOG_V(DEBUG, TAG, "CAFindResource");
141
142     return CADetachMessageResourceUri(resourceUri, token, NULL, 0);
143
144 }
145
146 CAResult_t CASendRequest(const CARemoteEndpoint_t *object, CARequestInfo_t *requestInfo)
147 {
148     OIC_LOG_V(DEBUG, TAG, "CASendGetRequest");
149
150     return CADetachRequestMessage(object, requestInfo);
151 }
152
153 CAResult_t CASendRequestToAll(const CAGroupEndpoint_t *object,
154                               const CARequestInfo_t *requestInfo)
155 {
156     OIC_LOG_V(DEBUG, TAG, "CASendRequestToAll");
157
158     return CADetachRequestToAllMessage(object, requestInfo);
159 }
160
161 CAResult_t CASendNotification(const CARemoteEndpoint_t *object, CAResponseInfo_t *responseInfo)
162 {
163     OIC_LOG_V(DEBUG, TAG, "CASendNotification");
164
165     return CADetachResponseMessage(object, responseInfo);
166
167 }
168
169 CAResult_t CASendResponse(const CARemoteEndpoint_t *object, CAResponseInfo_t *responseInfo)
170 {
171     OIC_LOG_V(DEBUG, TAG, "CASendResponse");
172
173     return CADetachResponseMessage(object, responseInfo);
174
175 }
176
177 CAResult_t CAAdvertiseResource(const CAURI_t resourceUri, CAToken_t token,
178                                CAHeaderOption_t *options, uint8_t numOptions)
179 {
180     OIC_LOG_V(DEBUG, TAG, "CAAdvertiseResource");
181
182     return CADetachMessageResourceUri(resourceUri, token, options, numOptions);
183
184 }
185
186 CAResult_t CASelectNetwork(const uint32_t interestedNetwork)
187 {
188     OIC_LOG_V(DEBUG, TAG, "Selected network : %d", interestedNetwork);
189
190     if (!(interestedNetwork & 0xf))
191     {
192         return CA_NOT_SUPPORTED;
193     }
194     CAResult_t res;
195
196     if (interestedNetwork & CA_ETHERNET)
197     {
198         res = CAAddNetworkType(CA_ETHERNET);
199         if (res != CA_STATUS_OK)
200         {
201             return res;
202         }
203     }
204
205     if (interestedNetwork & CA_WIFI)
206     {
207         res = CAAddNetworkType(CA_WIFI);
208         if (res != CA_STATUS_OK)
209         {
210             return res;
211         }
212     }
213
214     if (interestedNetwork & CA_EDR)
215     {
216         res = CAAddNetworkType(CA_EDR);
217         if (res != CA_STATUS_OK)
218         {
219             return res;
220         }
221     }
222
223     if (interestedNetwork & CA_LE)
224     {
225         res = CAAddNetworkType(CA_LE);
226         if (res != CA_STATUS_OK)
227         {
228             return res;
229         }
230     }
231
232     return CA_STATUS_OK;
233 }
234
235 CAResult_t CAUnSelectNetwork(const uint32_t nonInterestedNetwork)
236 {
237     OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
238
239     if (!(nonInterestedNetwork & 0xf))
240     {
241         return CA_NOT_SUPPORTED;
242     }
243
244     CAResult_t res;
245
246     if (nonInterestedNetwork & CA_ETHERNET)
247     {
248         res = CARemoveNetworkType(CA_ETHERNET);
249         if (res != CA_STATUS_OK)
250         {
251             return res;
252         }
253     }
254
255     if (nonInterestedNetwork & CA_WIFI)
256     {
257         res = CARemoveNetworkType(CA_WIFI);
258         if (res != CA_STATUS_OK)
259         {
260             return res;
261         }
262     }
263
264     if (nonInterestedNetwork & CA_EDR)
265     {
266         res = CARemoveNetworkType(CA_EDR);
267         if (res != CA_STATUS_OK)
268         {
269             return res;
270         }
271     }
272
273     if (nonInterestedNetwork & CA_LE)
274     {
275         res = CARemoveNetworkType(CA_LE);
276         if (res != CA_STATUS_OK)
277         {
278             return res;
279         }
280     }
281
282     return CA_STATUS_OK;
283 }
284
285 CAResult_t CAHandleRequestResponse()
286 {
287     OIC_LOG_V(DEBUG, TAG, "CAHandleRequestResponse");
288
289     CAHandleRequestResponseCallbacks();
290
291     return CA_STATUS_OK;
292 }
293