Implementation of following functionality
[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_singlethread.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,
146                                CAHeaderOption_t *options,
147                                uint8_t numOptions)
148 {
149     return CADetachMessageResourceUri(resourceUri, token, options, numOptions);
150 }
151
152 CAResult_t CASelectNetwork(const uint32_t interestedNetwork)
153 {
154     OIC_LOG_V(DEBUG, TAG, "Selected n/W=%d", interestedNetwork);
155
156     if (!(interestedNetwork & 0xf))
157     {
158         return CA_NOT_SUPPORTED;
159     }
160     CAResult_t res;
161
162     if (interestedNetwork & CA_ETHERNET)
163     {
164         res = CAAddNetworkType(CA_ETHERNET);
165         if (res != CA_STATUS_OK)
166         {
167             return res;
168         }
169     }
170
171     if (interestedNetwork & CA_WIFI)
172     {
173         res = CAAddNetworkType(CA_WIFI);
174         if (res != CA_STATUS_OK)
175         {
176             return res;
177         }
178     }
179
180     if (interestedNetwork & CA_EDR)
181     {
182         res = CAAddNetworkType(CA_EDR);
183         if (res != CA_STATUS_OK)
184         {
185             return res;
186         }
187     }
188
189     if (interestedNetwork & CA_LE)
190     {
191         res = CAAddNetworkType(CA_LE);
192         if (res != CA_STATUS_OK)
193         {
194             return res;
195         }
196     }
197     OIC_LOG(DEBUG, TAG, "OUT");
198     return CA_STATUS_OK;
199 }
200
201 CAResult_t CAUnSelectNetwork(const uint32_t nonInterestedNetwork)
202 {
203     OIC_LOG_V(DEBUG, TAG, "unselected n/w=%d", nonInterestedNetwork);
204
205     if (!(nonInterestedNetwork & 0xf))
206     {
207         return CA_NOT_SUPPORTED;
208     }
209
210     CAResult_t res;
211
212     if (nonInterestedNetwork & CA_ETHERNET)
213     {
214         res = CARemoveNetworkType(CA_ETHERNET);
215         if (res != CA_STATUS_OK)
216         {
217             return res;
218         }
219     }
220
221     if (nonInterestedNetwork & CA_WIFI)
222     {
223         res = CARemoveNetworkType(CA_WIFI);
224         if (res != CA_STATUS_OK)
225         {
226             return res;
227         }
228     }
229
230     if (nonInterestedNetwork & CA_EDR)
231     {
232         res = CARemoveNetworkType(CA_EDR);
233         if (res != CA_STATUS_OK)
234         {
235             return res;
236         }
237     }
238
239     if (nonInterestedNetwork & CA_LE)
240     {
241         res = CARemoveNetworkType(CA_LE);
242         if (res != CA_STATUS_OK)
243         {
244             return res;
245         }
246     }
247     OIC_LOG(DEBUG, TAG, "OUT");
248     return CA_STATUS_OK;
249 }
250
251 CAResult_t CAHandleRequestResponse()
252 {
253     CAHandleRequestResponseCallbacks();
254     return CA_STATUS_OK;
255 }
256