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