blob: 8bd7aeef3d4961ae2c4cf98299074ebbcbf7ce1f [file] [log] [blame]
/******************************************************************************
*(C) Copyright 2015 Marvell International Ltd.
* All Rights Reserved
******************************************************************************/
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libubox/blob.h>
#include <libubox/blobmsg.h>
#include <libubox/ustream.h>
#include <libubus.h>
#include <ubusmsg.h>
#include <uci.h>
#include <include/log.h>
#include "chl_ubus.h"
#include "chl_uci.h"
#include "chl_util.h"
#include "chl_network.h"
#define UBUS_NETWORK "network"
#define UBUS_NETWORK_IF "network.interface"
#define NW_REQ_TIMEOUT 20000
static unsigned int network_id;
static unsigned int network_if_id;
static struct blob_buf blob;
int chl_network_register(void)
{
int ret;
ret = chl_ubus_lookup_id(UBUS_NETWORK, &network_id);
if (ret) {
CHL_ERR("Failed to look up [%s] object\n", UBUS_NETWORK);
return ret;
}
ret = chl_ubus_lookup_id(UBUS_NETWORK_IF, &network_if_id);
if (ret)
CHL_ERR("Failed to look up [%s] object\n", UBUS_NETWORK_IF);
return ret;
}
int chl_network_reload(void)
{
int ret = 0;
CHL_INFO("reloading network\n");
ret = chl_ubus_invoke(NULL, network_id, "reload", NULL, NULL,
NW_REQ_TIMEOUT);
if (ret)
CHL_ERR("invoke reload failed: [%s]\n", ubus_strerror(ret));
return ret;
}
static int chl_nw_updown_invoke(char *sec, bool up)
{
int ret;
char *cmd = up ? "up" : "down";
blob_buf_init(&blob, 0);
blobmsg_add_string(&blob, "interface", sec);
ret = chl_ubus_invoke(&blob, network_if_id, cmd, NULL, NULL,
NW_REQ_TIMEOUT);
if (ret)
CHL_ERR("invoke [%s] [%s] failed: [%s]\n" ,sec, cmd,
ubus_strerror(ret));
return ret;
}
static int chl_network_updown(struct chl_pdp *pdp, bool up)
{
int ret;
char section[UCI_MAX_SEC_STR];
cid_to_section(pdp->cid, section);
ret = chl_nw_updown_invoke(section, up);
if (ret || !(pdp->type & IPV6))
goto done;
cid_to_section6(pdp->cid, section);
ret = chl_nw_updown_invoke(section, up);
done:
return ret;
}
int chl_network_ifup(struct chl_pdp *pdp)
{
return chl_network_updown(pdp, true);
}
int chl_network_ifdown(struct chl_pdp *pdp)
{
return chl_network_updown(pdp, false);
}