Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / arduino / caipnwmonitor_wifi.cpp
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  * @file
22  * This file is to keep design in sync with other platforms.  Right now
23  * there is no api for network monitoring in arduino.
24  */
25
26 #include "caipinterface.h"
27
28 #include <Arduino.h>
29 #include <WiFi.h>
30 #include <WiFiUdp.h>
31 #include <SPI.h>
32 #include <utility/server_drv.h>
33 #include <utility/wifi_drv.h>
34 #include <IPAddress.h>
35
36 #include "logger.h"
37 #include "cacommon.h"
38 #include "caipadapter.h"
39 #include "caadapterutils.h"
40 #include "oic_malloc.h"
41 #include "oic_string.h"
42
43 #define TAG "IPNW"
44
45 // Since the CA abstraction expects a value for "family", AF_INET will be
46 // defined & used (as-is defined in the linux socket headers).
47 #define AF_INET (2)
48
49 CAResult_t CAIPStartNetworkMonitor()
50 {
51     return CA_STATUS_OK;
52 }
53
54 CAResult_t CAIPStopNetworkMonitor()
55 {
56     return CA_STATUS_OK;
57 }
58
59 /// Retrieves the IP address assigned to Arduino WiFi shield
60 void CAArduinoGetInterfaceAddress(uint32_t *address)
61 {
62     OIC_LOG(DEBUG, TAG, "IN");
63     if (WiFi.status() != WL_CONNECTED)
64     {
65         OIC_LOG(DEBUG, TAG, "No WIFI");
66         return;
67     }
68
69     VERIFY_NON_NULL_VOID(address, TAG, "Invalid address");
70
71     IPAddress ip = WiFi.localIP();
72     *address = (uint32_t) ip;
73
74     OIC_LOG_V(DEBUG, TAG, "Wifi shield address is: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
75     OIC_LOG(DEBUG, TAG, "OUT");
76     return;
77 }
78
79 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
80 {
81     bool result = true;
82
83     u_arraylist_t *iflist = u_arraylist_create();
84     if (!iflist)
85     {
86         OIC_LOG(ERROR, TAG, "Failed to create iflist");
87         return NULL;
88     }
89
90     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
91     if (!ifitem)
92     {
93         OIC_LOG(ERROR, TAG, "Malloc failed");
94         goto exit;
95     }
96
97     // Since Arduino currently only supports one interface, the next 4 lines are sufficient.
98     OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, "WIFI");
99     ifitem->index = 1;
100     ifitem->family = AF_INET;
101     ifitem->flags = 0;
102     CAArduinoGetInterfaceAddress(&ifitem->ipv4addr);
103
104     result = u_arraylist_add(iflist, ifitem);
105     if (!result)
106     {
107         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
108         goto exit;
109     }
110
111     OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
112
113     return iflist;
114
115 exit:
116     u_arraylist_destroy(iflist);
117     return NULL;
118 }
119