iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / wifi_adapter / linux / cawifiadapter.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 <string.h>
24
25 #include "cawifiadapter.h"
26
27 #include "config.h"
28 #include "coap.h"
29
30 #include "cawificore.h"
31
32 #include "logger.h"
33
34 #define TAG PCF("CA")
35
36 // received packet callback
37 static CANetworkPacketReceivedCallback gWifiReceivedCallback = NULL;
38
39 static void CAWiFiPacketReceiveCallback(const char* address, const char* data)
40 {
41     OIC_LOG_V(DEBUG, TAG,
42             "CAWiFiPacketReceiveCallback, from: %s, data: %s", address, data);
43
44     // call the callback
45     if (gWifiReceivedCallback != NULL)
46     {
47         CARemoteEndpoint_t* endpoint = NULL;
48         endpoint = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
49
50         // set address
51         memset((void*) endpoint->addressInfo.IP.ipAddress, 0, CA_IPADDR_SIZE);
52         if (CA_IPADDR_SIZE > strlen(address))
53             strcpy((char*) endpoint->addressInfo.IP.ipAddress, address);
54
55         // set connectivity type
56         endpoint->connectivityType = CA_WIFI;
57
58         gWifiReceivedCallback(endpoint, data, strlen(data));
59     }
60 }
61
62 CAResult_t CAInitializeWifi(CARegisterConnectivityCallback registerCallback,
63         CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback)
64 {
65     OIC_LOG(DEBUG, TAG, "IntializeWifi");
66
67     gWifiReceivedCallback = reqRespCallback;
68
69     // register handlers
70     CAConnectivityHandler_t handler;
71     memset(&handler, 0, sizeof(CAConnectivityHandler_t));
72
73     handler.startAdapter = CAStartWIFI;
74     handler.startListenServer = CAStartWIFIListeningServer;
75     handler.startDiscoverServer = CAStartWIFIDiscoveryServer;
76     handler.sendData = CASendWIFIUnicastData;
77     handler.sendDataToAll = CASendWIFIMulticastData;
78     handler.startNotifyServer = CAStartWIFINotifyRecvServers;
79     handler.sendNotification = CASendWIFINotification;
80     handler.GetnetInfo = CAGetWIFIInterfaceInformation;
81     handler.readData = CAReadWIFIData;
82     handler.stopAdapter = CAStopWIFI;
83     handler.terminate = CATerminateWIfI;
84
85     registerCallback(handler, CA_WIFI);
86
87     CAWiFiSetCallback(CAWiFiPacketReceiveCallback);
88
89     return 0;
90 }
91
92 void CATerminateWIfI()
93 {
94     OIC_LOG(DEBUG, TAG, "TerminateWifi");
95
96     CAWiFiTerminate();
97 }
98
99 CAResult_t CAStartWIFI()
100 {
101     OIC_LOG(DEBUG, TAG, "CAStartWIFI");
102     CAWiFiInitialize();
103
104     OIC_LOG(DEBUG, TAG, "CAWiFiStartUnicastServer");
105     CAWiFiStartUnicastServer("0.0.0.0", atoi("5283"));
106
107     return 0;
108 }
109
110 CAResult_t CAStopWIFI()
111 {
112     OIC_LOG(DEBUG, TAG, "CAStopWIFI");
113
114     // ToDo:
115
116     return 0;
117 }
118
119 CAResult_t CAStartWIFIListeningServer()
120 {
121     OIC_LOG(DEBUG, TAG, "StartWIFIListeningServer");
122
123     CAWiFiStartMulticastServer("0.0.0.0", atoi("5283"));
124
125     return 0;
126 }
127
128 CAResult_t CAStartWIFIDiscoveryServer()
129 {
130     OIC_LOG(DEBUG, TAG, "StartWIFIDiscoveryServer");
131
132     CAWiFiStartMulticastServer("0.0.0.0", atoi("5283"));
133
134     return 0;
135 }
136
137 uint32_t CASendWIFIUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
138 {
139     OIC_LOG(DEBUG, TAG, "SendWIFIUnicastData");
140
141     CAWiFiSendUnicastMessage(endpoint->addressInfo.IP.ipAddress, data, dataLen);
142
143     return 0;
144 }
145
146 uint32_t CASendWIFIMulticastData(void* data, uint32_t dataLen)
147 {
148     OIC_LOG(DEBUG, TAG, "CASendWIFIMulticastData");
149
150     CAWiFiSendMulticastMessage("0.0.0.0", (char*) data);
151
152     return 0;
153 }
154
155 CAResult_t CAStartWIFINotifyRecvServers()
156 {
157     OIC_LOG(DEBUG, TAG, "StartWIFINotifyRecvServers");
158
159     // ToDo:
160
161     return 0;
162 }
163
164 uint32_t CASendWIFINotification(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
165 {
166     OIC_LOG(DEBUG, TAG, "SendWIFINotification");
167
168     // ToDo:
169
170     return 0;
171 }
172
173 CAResult_t CAGetWIFIInterfaceInformation(CALocalConnectivityt_t** info, uint32_t* size)
174 {
175     OIC_LOG(DEBUG, TAG, "GetWIFIInterfaceInformation");
176
177     // ToDo:
178
179     return 0;
180 }
181
182 CAResult_t CAReadWIFIData()
183 {
184     OIC_LOG(DEBUG, TAG, "Read WIFI Data");
185
186     // ToDo:
187
188     return 0;
189 }
190