Imported Upstream version 0.9.2
[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 caipnwmonitor.cpp
22  * @brief This file is to keep design in sync with other platforms.  Right now there is no
23  *        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 /// Retrieves the IP address assigned to Arduino WiFi shield
50 void CAArduinoGetInterfaceAddress(uint32_t *address)
51 {
52     OIC_LOG(DEBUG, TAG, "IN");
53     if (WiFi.status() != WL_CONNECTED)
54     {
55         OIC_LOG(DEBUG, TAG, "No WIFI");
56         return;
57     }
58
59     VERIFY_NON_NULL_VOID(address, TAG, "Invalid address");
60
61     IPAddress ip = WiFi.localIP();
62     *address = (uint32_t) ip;
63
64     OIC_LOG_V(DEBUG, TAG, "Wifi shield address is: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
65     OIC_LOG(DEBUG, TAG, "OUT");
66     return;
67 }
68
69 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
70 {
71     CAResult_t result = CA_STATUS_OK;
72
73     u_arraylist_t *iflist = u_arraylist_create();
74     if (!iflist)
75     {
76         OIC_LOG(ERROR, TAG, "Failed to create iflist");
77         return NULL;
78     }
79
80     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
81     if (!ifitem)
82     {
83         OIC_LOG(ERROR, TAG, "Malloc failed");
84         goto exit;
85     }
86
87     // Since Arduino currently only supports one interface, the next 4 lines are sufficient.
88     OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, "WIFI");
89     ifitem->index = 1;
90     ifitem->family = AF_INET;
91     ifitem->flags = 0;
92     CAArduinoGetInterfaceAddress(&ifitem->ipv4addr);
93
94     result = u_arraylist_add(iflist, ifitem);
95     if (CA_STATUS_OK != result)
96     {
97         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
98         goto exit;
99     }
100
101     OIC_LOG_V(ERROR, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
102
103     return iflist;
104
105 exit:
106     u_arraylist_destroy(iflist);
107     return NULL;
108 }
109