Skip to content
Snippets Groups Projects
Commit 4c7d1ccf authored by Jakob Olsson's avatar Jakob Olsson
Browse files

update to get ip address from router.net

parent a0a0baf2
Branches
No related tags found
No related merge requests found
CC = gcc CC = gcc
CFLAGS = -g -Wall CFLAGS = -g -Wall
LIBS = -ljson-c -lubox -lubus -lcurl -lmobile LIBS = -ljson-c -lubox -lubus -lcurl -lmobile -lblobmsg_json
all: libmobile1 libmobile2 common dongle_apn dongle_pin dongle_network dongle all: libmobile1 libmobile2 common dongle_apn dongle_pin dongle_network dongle
...@@ -22,20 +22,20 @@ common: ${COBJS} ...@@ -22,20 +22,20 @@ common: ${COBJS}
DAOBJS = dongle_apn.o DAOBJS = dongle_apn.o
DASRCS = dongle_apn.c DASRCS = dongle_apn.c
dongle_apn: ${DAOBJS} dongle_apn: ${DAOBJS}
${CC} -c ${CFLAGS} ${DASRCS} ${COBJS} -o ${DAOBJS} -L . ${LIBS} #${MOBJS} ${CC} -c ${CFLAGS} ${DASRCS} ${COBJS} -o ${DAOBJS} -L . ${LIBS}
DPOBJS = dongle_pin.o DPOBJS = dongle_pin.o
DPSRCS = dongle_pin.c DPSRCS = dongle_pin.c
dongle_pin: ${DPOBJS} dongle_pin: ${DPOBJS}
${CC} -c ${CFLAGS} ${DPSRCS} ${COBJS} -o ${DPOBJS} -L . ${LIBS} #${MOBJS} ${CC} -c ${CFLAGS} ${DPSRCS} ${COBJS} -o ${DPOBJS} -L . ${LIBS}
DNOBJS = dongle_network.o DNOBJS = dongle_network.o
DNSRCS = dongle_network.c DNSRCS = dongle_network.c
dongle_network: ${DNOBJS} dongle_network: ${DNOBJS}
${CC} -c ${CFLAGS} ${DNSRCS} ${COBJS} -o ${DNOBJS} -L . ${LIBS} #${MOBJS} ${CC} -c ${CFLAGS} ${DNSRCS} ${COBJS} -o ${DNOBJS} -L . ${LIBS}
dongle: dongle.o dongle: dongle.o
${CC} ${CFLAGS} dongle.o ${COBJS} ${DNOBJS} ${DPOBJS} ${DAOBJS} -o dongle -L . ${LIBS} #${MOBJS} ${CC} ${CFLAGS} dongle.o ${COBJS} ${DNOBJS} ${DPOBJS} ${DAOBJS} -o dongle -L . ${LIBS}
clean: clean:
rm -f dongle_apn dongle_pin dongle_network *.o *.so rm -f dongle_apn dongle_pin dongle_network *.o *.so
......
...@@ -12,17 +12,18 @@ TODO: ...@@ -12,17 +12,18 @@ TODO:
1. Implement by invoking ubus 1. Implement by invoking ubus
*/ */
int get_ip(char *if_name) char *get_ip(char *if_name)
{ {
char response[1024] = {0}; char response[1024] = {0};
size_t filesize;
int rv;
FILE *fp = popen("ubus call router.net ipv4_routes | grep -B 7 \"usb0\" | grep \"flag.*H\" -B 4 | grep destination | cut -d ' ' -f2 | cut -d '\"' -f2", "r"); FILE *fp = popen("ubus call router.net ipv4_routes | grep -B 7 \"usb0\" | grep \"flag.*H\" -B 4 | grep destination | cut -d ' ' -f2 | cut -d '\"' -f2", "r");
fscanf(fp, "%s", response); fscanf(fp, "%s", response);
pclose(fp); pclose(fp);
printf("%s\n", response); printf("%s\n", response);
return strdup(response);
return 0;
} }
int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req) int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx, struct ubus_request_data *req)
......
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
#include "libmobile.h" #include "libmobile.h"
#include <json-c/json.h> #include <json-c/json.h>
#include <string.h> #include <string.h>
#include <libubox/blobmsg.h> //#include <libubox/blobmsg.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h> #include <libubus.h>
extern int debug; extern int debug;
...@@ -27,7 +28,7 @@ extern int debug; ...@@ -27,7 +28,7 @@ extern int debug;
fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, __VA_ARGS__); \
} while (0) } while (0)
int get_ip(char *if_name); char *get_ip(char *if_name);
/** /**
* Function: print_to_ubus * Function: print_to_ubus
* *
......
#include "dongle_network.h" #include "dongle_network.h"
char *dongle_ip;
static enum { static enum {
IP, IP,
DEV, DEV,
...@@ -217,6 +219,76 @@ int test_get_ip(struct ubus_context *ctx, struct ubus_object *obj, ...@@ -217,6 +219,76 @@ int test_get_ip(struct ubus_context *ctx, struct ubus_object *obj,
return 0; return 0;
} }
static void parse_devices(struct ubus_request *req, int type, struct blob_attr *msg)
{
char *json_str, *flags, *ip;
struct json_object *json_msg, *routes, *route, *json_ip, *json_flags;
int i;
json_str = blobmsg_format_json(msg, true);
if (!json_str)
return;
json_msg = json_tokener_parse(json_str);
if (!json_msg)
goto out_str;
if (!json_object_is_type(json_msg, json_type_object))
goto out_json;
json_object_object_get_ex(json_msg, "routes", &routes);
if (!routes)
goto out_json;
for (i = 0; i < json_object_array_length(routes); i++) {
route = json_object_array_get_idx(routes, i);
json_object_object_get_ex(route, "flags", &json_flags);
flags = json_object_get_string(json_flags);
if (!strchr(flags, 'H'))
continue;
json_object_object_get_ex(route, "destination", &json_ip);
ip = json_object_get_string(json_ip);
goto out;
}
goto out_json;
out:
dongle_ip = strdup(ip);
out_json:
json_object_put(json_msg);
out_str:
free(json_str);
}
int get_ip_invoke(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_buf buf;
int obj_id, rv;
memset(&buf, 0, sizeof(buf));
if (!ctx) {
debug_print("No ubus context!\n");
goto fail;
}
ubus_lookup_id(ctx, "router.net", &obj_id);
if (!obj_id) {
debug_print("%s: ubus_lookup_id error\n", __func__);
goto fail;
}
rv = ubus_invoke(ctx, obj_id, "ipv4_routes", NULL, parse_devices, NULL, 5000);
if (rv > 0)
printf("ubus_invoke error code %d\n", rv);
fail:
return 0;
}
struct ubus_method network_object_methods[] = { struct ubus_method network_object_methods[] = {
UBUS_METHOD("signal_strength", get_signal_strength, ip_policy), UBUS_METHOD("signal_strength", get_signal_strength, ip_policy),
UBUS_METHOD("connect", connect_network, ip_policy), UBUS_METHOD("connect", connect_network, ip_policy),
...@@ -225,7 +297,8 @@ struct ubus_method network_object_methods[] = { ...@@ -225,7 +297,8 @@ struct ubus_method network_object_methods[] = {
UBUS_METHOD("enable_roaming", enable_roaming, ip_policy), UBUS_METHOD("enable_roaming", enable_roaming, ip_policy),
UBUS_METHOD("disable_roaming", disable_roaming, ip_policy), UBUS_METHOD("disable_roaming", disable_roaming, ip_policy),
UBUS_METHOD("roam_status", roam_status, ip_policy), UBUS_METHOD("roam_status", roam_status, ip_policy),
UBUS_METHOD("get_ip", test_get_ip, ip_policy) UBUS_METHOD("get_ip", test_get_ip, ip_policy),
UBUS_METHOD_NOARG("get_ip_invoke", get_ip_invoke)
}; };
struct ubus_object_type network_object_type = UBUS_OBJECT_TYPE("dongle", network_object_methods); struct ubus_object_type network_object_type = UBUS_OBJECT_TYPE("dongle", network_object_methods);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment