Skip to content

Commit

Permalink
Adopted for Arduino environment
Browse files Browse the repository at this point in the history
  • Loading branch information
achilikin committed Nov 16, 2015
1 parent 03a86ba commit cf4b475
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 10 deletions.
110 changes: 110 additions & 0 deletions YAHL/netif.c
@@ -0,0 +1,110 @@
/* Apache 2.0 License
Copyright (c) 2015 Andrey Chilikin https://github.com/achilikin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <Arduino.h>

#ifdef __ARDUINO_X86__

#include <stdio.h>

#include "netif.h"

int get_netif_info(const char *name, netif_t *netif)
{
iostat_t *pstat;
char *val, str[512];
int i, ifound = 0;

if (name == NULL || netif == NULL)
return -1;

memset(netif, 0, sizeof(netif_t));

sprintf(str, "ifconfig %s", name);
FILE *pf = popen(str, "r");
if (pf == NULL)
return -1;

while(fgets(str, 512, pf)) {
if (strstr(str, name)) {
strcpy(netif->name, name);
ifound = 1;
}

if (!ifound)
continue;

if ((val = strstr(str, "HWaddr ")) != NULL) {
val = strchr(val, ' ') + 1;
for(i = 0; val[i] != '\0' && i < 17; i++)
netif->hwas[i] = val[i];
netif->hwas[i] = 0;
continue;
}

if ((val = strstr(str, " inet addr:")) != NULL) {
val = strchr(val, ':') + 1;
while(*val == ' ') val++;
for(i = 0; val[i] != ' '; i++)
netif->ip4as[i] = val[i];
netif->ip4as[i] = 0;
continue;
}

if ((val = strstr(str, " inet6 addr:")) != NULL) {
val = strchr(val, ':') + 1;
while(*val == ' ') val++;
for(i = 0; val[i] != ' ' && val[i] != '/'; i++)
netif->ip6as[i] = val[i];
netif->ip6as[i] = 0;
continue;
}

if (strstr(str, "RX "))
pstat = &netif->rx;

if (strstr(str, "TX "))
pstat = &netif->tx;

if ((val = strstr(str, " packets:")) != NULL) {
val = strchr(val, ':') + 1;
pstat->packets = strtoul(val, &val, 10);
}
if ((val = strstr(str, " errors:")) != NULL) {
val = strchr(val, ':') + 1;
pstat->errors = strtoul(val, &val, 10);
}
if ((val = strstr(str, " dropped:")) != NULL) {
val = strchr(val, ':') + 1;
pstat->dropped = strtoul(val, &val, 10);
}

if ((val = strstr(str, "RX bytes:")) != NULL) {
val = strchr(val, ':') + 1;
netif->rx.bytes = strtoul(val, &val, 10);
}
if ((val = strstr(str, "TX bytes:")) != NULL) {
val = strchr(val, ':') + 1;
netif->tx.bytes = strtoul(val, &val, 10);
break;
}
}

fclose(pf);
return ifound ? 0 : -1;
}

#endif
65 changes: 65 additions & 0 deletions YAHL/netif.h
@@ -0,0 +1,65 @@
/* Apache 2.0 License
Copyright (c) 2015 Andrey Chilikin https://github.com/achilikin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __YAHL_NET_IF_H__
#define __YAHL_NET_IF_H__

#ifdef __ARDUINO_X86__

#include <stdint.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>

/*
Network interface information from 'ifconfig' command
*/

#ifdef __cplusplus
extern "C" {
#endif

#define HW_ADDRSTRLEN 18

// network interface statistics
typedef struct iostat_s
{
uint32_t packets;
uint32_t errors;
uint32_t dropped;
uint32_t bytes; // increment to uint64_t if needed
} iostat_t;

// network interface information
typedef struct netif_s
{
char name[IFNAMSIZ];
char hwas[HW_ADDRSTRLEN]; // HW address string
char ip4as[INET_ADDRSTRLEN]; // IPv4 address string
char ip6as[INET6_ADDRSTRLEN]; // IPv4 address string
iostat_t rx;
iostat_t tx;
} netif_t;

// interface name to get information for
int get_netif_info(const char *name, netif_t *netif);

#ifdef __cplusplus
}
#endif

#endif
#endif
25 changes: 15 additions & 10 deletions YAHL/ticker.h
@@ -1,5 +1,5 @@
/* BSD License
Copyright (c) 2014 Andrey Chilikin https://github.com/achilikin
Copyright (c) 2015 Andrey Chilikin https://github.com/achilikin
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -33,25 +33,29 @@
Simple timer handler, create it and then call tick(millis()) periodically
*/

#define TICK_SEC(x) (x*1000)
#define TICK_MIN(x) (x*60*1000)
#define TICK_HOUR(x) (x*60*60*1000)
#define TICK_SEC(x) (((uint32_t)x)*1000l)
#define TICK_MIN(x) (((uint32_t)x)*60l*1000l)
#define TICK_HOUR(x) (((uint32_t)x)*60l*60l*1000l)

// tick handler, called every time when time interval is expired
typedef int tickerHandler(void *data);
typedef int8_t tickerHandler(void *data);

class ticker_t {
public:
// tick time interval (milliseconds) and tick handler
ticker_t(uint32_t period, tickerHandler *phanler);

// checks if time interval expired and call tick handler
int tick(unsigned long current_millis, void *data);
int8_t tick(unsigned long current_millis, void *data);

private:
uint32_t interval;
unsigned long last_call;
uint32_t last_call;
tickerHandler *handler;
public:
uint32_t set_interval(uint32_t span) { uint32_t current = interval; interval = span; return current; }
uint32_t get_interval(void) { return interval; }
void reset(void) { last_call = 0; }
};

ticker_t::ticker_t(uint32_t period, tickerHandler *phanler)
Expand All @@ -61,10 +65,11 @@ ticker_t::ticker_t(uint32_t period, tickerHandler *phanler)
last_call = 0;
}

int ticker_t::tick(unsigned long current_millis, void *data)
int8_t ticker_t::tick(uint32_t current_millis, void *data)
{
if ((current_millis - last_call) >= interval) {
last_call = current_millis;
uint32_t span = current_millis - last_call;
if (span >= interval) {
last_call = current_millis - (span - interval);
return handler(data);
}
return 0;
Expand Down

0 comments on commit cf4b475

Please sign in to comment.