Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / arduino / caipnwmonitor_eth.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 <Ethernet.h>
30 #include <socket.h>
31 #include <w5100.h>
32 #include <EthernetUdp.h>
33 #include <IPAddress.h>
34
35 #include "logger.h"
36 #include "cacommon.h"
37 #include "caipadapter.h"
38 #include "caadapterutils.h"
39 #include "oic_malloc.h"
40 #include "oic_string.h"
41
42 #define TAG "IPNW"
43
44 // Since the CA abstraction expects a value for "family", AF_INET will be
45 // defined & used (as-is defined in the linux socket headers).
46 #define AF_INET (2)
47
48 CAResult_t CAIPStartNetworkMonitor()
49 {
50     return CA_STATUS_OK;
51 }
52
53 CAResult_t CAIPStopNetworkMonitor()
54 {
55     return CA_STATUS_OK;
56 }
57
58 /// Retrieves the IP address assigned to Arduino Ethernet shield
59 void CAArduinoGetInterfaceAddress(uint32_t *address)
60 {
61     OIC_LOG(DEBUG, TAG, "IN");
62     VERIFY_NON_NULL_VOID(address, TAG, "address");
63
64     //TODO : Fix this for scenarios when this API is invoked when device is not connected
65     uint8_t rawIPAddr[4];
66     W5100.getIPAddress(rawIPAddr);
67     *address = (uint32_t) rawIPAddr;
68
69     OIC_LOG_V(DEBUG, TAG, "address:%d.%d.%d.%d", rawIPAddr[0], rawIPAddr[1],
70               rawIPAddr[2], rawIPAddr[3]);
71     OIC_LOG(DEBUG, TAG, "OUT");
72     return;
73 }
74
75 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
76 {
77     bool result = true;
78
79     u_arraylist_t *iflist = u_arraylist_create();
80     if (!iflist)
81     {
82         OIC_LOG(ERROR, TAG, "Failed to create iflist");
83         return NULL;
84     }
85
86     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
87     if (!ifitem)
88     {
89         OIC_LOG(ERROR, TAG, "Malloc failed");
90         goto exit;
91     }
92
93     // Since Arduino currently only supports one interface, the next 4 lines are sufficient.
94     OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, "ETH");
95     ifitem->index = 1;
96     ifitem->family = AF_INET;
97     ifitem->flags = 0;
98     CAArduinoGetInterfaceAddress(&ifitem->ipv4addr);
99
100     result = u_arraylist_add(iflist, ifitem);
101     if (!result)
102     {
103         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
104         goto exit;
105     }
106
107     OIC_LOG_V(DEBUG, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
108
109     return iflist;
110
111 exit:
112     u_arraylist_destroy(iflist);
113     return NULL;
114 }