Imported Upstream version 0.9.2
[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 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 <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 /// Retrieves the IP address assigned to Arduino Ethernet shield
49 void CAArduinoGetInterfaceAddress(uint32_t *address)
50 {
51     OIC_LOG(DEBUG, TAG, "IN");
52     VERIFY_NON_NULL_VOID(address, TAG, "address");
53
54     //TODO : Fix this for scenarios when this API is invoked when device is not connected
55     uint8_t rawIPAddr[4];
56     W5100.getIPAddress(rawIPAddr);
57     *address = (uint32_t) rawIPAddr;
58
59     OIC_LOG_V(DEBUG, TAG, "address:%d.%d.%d.%d", rawIPAddr[0], rawIPAddr[1],
60               rawIPAddr[2], rawIPAddr[3]);
61     OIC_LOG(DEBUG, TAG, "OUT");
62     return;
63 }
64
65 u_arraylist_t *CAIPGetInterfaceInformation(int desiredIndex)
66 {
67     CAResult_t result;
68
69     u_arraylist_t *iflist = u_arraylist_create();
70     if (!iflist)
71     {
72         OIC_LOG(ERROR, TAG, "Failed to create iflist");
73         return NULL;
74     }
75
76     CAInterface_t *ifitem = (CAInterface_t *)OICCalloc(1, sizeof(CAInterface_t));
77     if (!ifitem)
78     {
79         OIC_LOG(ERROR, TAG, "Malloc failed");
80         goto exit;
81     }
82
83     // Since Arduino currently only supports one interface, the next 4 lines are sufficient.
84     OICStrcpy(ifitem->name, INTERFACE_NAME_MAX, "ETH");
85     ifitem->index = 1;
86     ifitem->family = AF_INET;
87     ifitem->flags = 0;
88     CAArduinoGetInterfaceAddress(&ifitem->ipv4addr);
89
90     result = u_arraylist_add(iflist, ifitem);
91     if (CA_STATUS_OK != result)
92     {
93         OIC_LOG(ERROR, TAG, "u_arraylist_add failed.");
94         goto exit;
95     }
96
97     OIC_LOG_V(ERROR, TAG, "Added interface: %s (%d)", ifitem->name, ifitem->family);
98
99     return iflist;
100
101 exit:
102     u_arraylist_destroy(iflist);
103     return NULL;
104 }
105