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