Imported Upstream version 0.9.1
[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(DEBUG, TAG, "CAInitialize");
44
45     return CAInitializeMessageHandler();;
46 }
47
48 void CATerminate()
49 {
50     OIC_LOG(DEBUG, TAG, "CATerminate");
51
52     CATerminateMessageHandler();
53
54     CATerminateNetworkType();
55 }
56
57 CAResult_t CAStartListeningServer()
58 {
59     OIC_LOG(DEBUG, TAG, "CAStartListeningServer");
60
61     return CAStartListeningServerAdapters();
62 }
63
64 CAResult_t CAStartDiscoveryServer()
65 {
66     OIC_LOG(DEBUG, TAG, "CAStartDiscoveryServer");
67
68     return CAStartDiscoveryServerAdapters();
69 }
70
71 void CARegisterHandler(CARequestCallback ReqHandler, CAResponseCallback RespHandler)
72 {
73     OIC_LOG(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, const CATransportType_t transportType,
89                                   CARemoteEndpoint_t **remoteEndpoint)
90 {
91     OIC_LOG(DEBUG, TAG, "CACreateRemoteEndpoint");
92
93     CARemoteEndpoint_t *remote = CACreateRemoteEndpointUriInternal(uri, transportType);
94
95     if (remote == NULL)
96     {
97         OIC_LOG(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(DEBUG, TAG, "CADestroyRemoteEndpoint");
109
110     CADestroyRemoteEndpointInternal(rep);
111 }
112
113 CAResult_t CAGenerateToken(CAToken_t *token, uint8_t tokenLength)
114 {
115     OIC_LOG(DEBUG, TAG, "CAGenerateToken");
116
117     return CAGenerateTokenInternal(token, tokenLength);
118 }
119
120 void CADestroyToken(CAToken_t token)
121 {
122     OIC_LOG(DEBUG, TAG, "CADestroyToken");
123
124     CADestroyTokenInternal(token);
125 }
126
127 CAResult_t CAGetNetworkInformation(CALocalConnectivity_t **info, uint32_t *size)
128 {
129     OIC_LOG(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(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(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(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(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(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(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
196     if (interestedNetwork & CA_IPV4)
197     {
198         res = CAAddNetworkType(CA_IPV4);
199         OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_IPV4) function returns error : %d", res);
200     }
201
202     if (interestedNetwork & CA_EDR)
203     {
204         res = CAAddNetworkType(CA_EDR);
205         OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_EDR) function returns error : %d", res);
206     }
207
208     if (interestedNetwork & CA_LE)
209     {
210         res = CAAddNetworkType(CA_LE);
211         OIC_LOG_V(ERROR, TAG, "CAAddNetworkType(CA_LE) function returns error : %d", res);
212     }
213
214     return res;
215 }
216
217 CAResult_t CAUnSelectNetwork(const uint32_t nonInterestedNetwork)
218 {
219     OIC_LOG_V(DEBUG, TAG, "unselected network : %d", nonInterestedNetwork);
220
221     if (!(nonInterestedNetwork & 0xf))
222     {
223         return CA_NOT_SUPPORTED;
224     }
225
226     CAResult_t res = CA_STATUS_OK;
227
228     if (nonInterestedNetwork & CA_IPV4)
229     {
230         res = CARemoveNetworkType(CA_IPV4);
231         OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_IPV4) function returns error : %d", res);
232     }
233
234     if (nonInterestedNetwork & CA_EDR)
235     {
236         res = CARemoveNetworkType(CA_EDR);
237         OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_EDR) function returns error : %d", res);
238     }
239
240     if (nonInterestedNetwork & CA_LE)
241     {
242         res = CARemoveNetworkType(CA_LE);
243         OIC_LOG_V(ERROR, TAG, "CARemoveNetworkType(CA_LE) function returns error : %d", res);
244     }
245
246     return res;
247 }
248
249 CAResult_t CAHandleRequestResponse()
250 {
251     OIC_LOG(DEBUG, TAG, "CAHandleRequestResponse");
252
253     CAHandleRequestResponseCallbacks();
254
255     return CA_STATUS_OK;
256 }
257
258