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