diff options
Diffstat (limited to 'ctrl')
-rw-r--r-- | ctrl/facemgr/src/api.c | 56 | ||||
-rw-r--r-- | ctrl/facemgr/src/interfaces/CMakeLists.txt | 1 | ||||
-rw-r--r-- | ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c | 2 | ||||
-rw-r--r-- | ctrl/facemgr/src/interfaces/priority_controller/CMakeLists.txt | 30 | ||||
-rw-r--r-- | ctrl/facemgr/src/interfaces/priority_controller/priority_controller.c | 135 | ||||
-rw-r--r-- | ctrl/libhicnctrl/src/api.c | 4 |
6 files changed, 204 insertions, 24 deletions
diff --git a/ctrl/facemgr/src/api.c b/ctrl/facemgr/src/api.c index 13b811288..18a25d6a1 100644 --- a/ctrl/facemgr/src/api.c +++ b/ctrl/facemgr/src/api.c @@ -20,6 +20,7 @@ #ifdef __ANDROID__ //#define WITH_ANDROID_UTILITY +#define WITH_PRIORITY_CONTROLLER #endif /* __ANDROID__ */ #include <assert.h> @@ -85,6 +86,9 @@ extern interface_ops_t bonjour_ops; #ifdef WITH_ANDROID_UTILITY extern interface_ops_t android_utility_ops; #endif /* WITH_ANDROID_UTILITY */ +#ifdef WITH_PRIORITY_CONTROLLER +extern interface_ops_t priority_controller_ops; +#endif #ifdef WITH_EXAMPLE_DUMMY extern interface_ops_t dummy_ops; #endif @@ -139,13 +143,16 @@ struct facemgr_s { interface_t * au; /* android_utility */ #endif /* WITH_ANDROID_UTILITY */ +#ifdef WITH_PRIORITY_CONTROLLER + interface_t * pc; +#endif + #ifdef __APPLE__ interface_t * nf; /* network_framework */ #endif /* __APPLE__ */ #ifdef __linux__ interface_t * nl; /* netlink */ - /* * We maintain a map of dynamically created bonjour interfaces, one for each * found netdevice @@ -542,26 +549,6 @@ facelet_cache_lookup(const facelet_set_t * facelet_cache, facelet_t * facelet, facelet_t ***cached_facelets) { assert(facelet); - if (!facelet_has_family(facelet)) - return 0; -#if 0 // key is no more sufficient now that we support multiple faces per interface - /* - * If the facelet is uniquely identified by its key, it is used to perform - * an efficient lookup directly... - */ - if (facelet_has_key(facelet)) { - facelet_t * found = NULL; - if (facelet_set_get(facelet_cache, facelet, &found) < 0) { - ERROR("[facelet_cache_lookup] Error during cache lookup"); - return -1; - } - if (!found) - return 0; - *cached_facelets = malloc(sizeof(facelet_t*)); - *cached_facelets[0] = found; - return 1; - } -#endif /* ...otherwise, we iterate over the facelet * cache to find matching elements. @@ -1865,6 +1852,15 @@ facemgr_bootstrap(facemgr_t * facemgr) } #endif /* WITH_ANDROID_UTILITY */ +#ifdef WITH_PRIORITY_CONTROLLER + INFO("[facemgr_bootstrap] registering priority_controller interface"); + rc = interface_register(&priority_controller_ops); + if (rc < 0) { + ERROR("[facemgr_bootstrap] Error registering priority_controller interface"); + goto ERR_REGISTER; + } +#endif + #ifdef WITH_EXAMPLE_DUMMY rc = interface_register(&dummy_ops); if (rc < 0) { @@ -1916,6 +1912,15 @@ facemgr_bootstrap(facemgr_t * facemgr) } #endif /* WITH_ANDROID_UTILITY */ +#ifdef WITH_PRIORITY_CONTROLLER + INFO("[facemgr_bootstrap] creating priority_controller interface"); + rc = facemgr_create_interface(facemgr, "pc", "priority_controller", NULL, &facemgr->pc); + if (rc < 0) { + ERROR("Error creating 'Priority Controller' interface\n"); + goto ERR_PC_CREATE; + } +#endif + #ifdef WITH_EXAMPLE_DUMMY rc = facemgr_create_interface(facemgr, "dummy0", "dummy", NULL, &facemgr->dummy); if (rc < 0) { @@ -1948,6 +1953,10 @@ ERR_DUMMY_CREATE: facemgr_delete_interface(facemgr, facemgr->au); ERR_AU_CREATE: #endif /* WITH_ANDROID_UTILITY */ +#ifdef WITH_PRIORITY_CONTROLLER + facemgr_delete_interface(facemgr, facemgr->pc); +ERR_PC_CREATE: +#endif #ifdef __linux__ facemgr_delete_interface(facemgr, facemgr->nl); ERR_NL_CREATE: @@ -1997,6 +2006,10 @@ void facemgr_stop(facemgr_t * facemgr) facemgr_delete_interface(facemgr, facemgr->au); #endif /* WITH_ANDROID_UTILITY */ +#ifdef WITH_PRIORITY_CONTROLLER + facemgr_delete_interface(facemgr, facemgr->pc); +#endif + facemgr_delete_interface(facemgr, facemgr->hl); #ifdef WITH_EXAMPLE_DUMMY @@ -2103,3 +2116,4 @@ ERR: free(facelet_array); return rc; } + diff --git a/ctrl/facemgr/src/interfaces/CMakeLists.txt b/ctrl/facemgr/src/interfaces/CMakeLists.txt index 8d079612a..e4d4423e9 100644 --- a/ctrl/facemgr/src/interfaces/CMakeLists.txt +++ b/ctrl/facemgr/src/interfaces/CMakeLists.txt @@ -29,6 +29,7 @@ endif() if(ANDROID) add_subdirectory(android_utility) +add_subdirectory(priority_controller) endif() if(WITH_EXAMPLE_DUMMY) diff --git a/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c b/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c index 4ba4c5b1f..cb1a0040d 100644 --- a/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c +++ b/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c @@ -449,7 +449,7 @@ int hl_on_event(interface_t * interface, const facelet_t * facelet) } INFO("Admin state updated"); } - if (facelet_get_admin_state_status(facelet) == FACELET_ATTR_STATUS_DIRTY) { + if (facelet_get_priority_status(facelet) == FACELET_ATTR_STATUS_DIRTY) { hc_face.face = *face; hc_face_t * face_found; diff --git a/ctrl/facemgr/src/interfaces/priority_controller/CMakeLists.txt b/ctrl/facemgr/src/interfaces/priority_controller/CMakeLists.txt new file mode 100644 index 000000000..8d18800db --- /dev/null +++ b/ctrl/facemgr/src/interfaces/priority_controller/CMakeLists.txt @@ -0,0 +1,30 @@ +# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# 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. + +list(APPEND HEADER_FILES +) + +list(APPEND SOURCE_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/priority_controller.c +) + +list(APPEND INCLUDE_DIRS +) + +list(APPEND LIBRARIES +) + +set(SOURCE_FILES ${SOURCE_FILES} PARENT_SCOPE) +set(HEADER_FILES ${HEADER_FILES} PARENT_SCOPE) +set(INCLUDE_DIRS ${INCLUDE_DIRS} PARENT_SCOPE) +set(LIBRARIES ${LIBRARIES} PARENT_SCOPE) diff --git a/ctrl/facemgr/src/interfaces/priority_controller/priority_controller.c b/ctrl/facemgr/src/interfaces/priority_controller/priority_controller.c new file mode 100644 index 000000000..b8e7271bb --- /dev/null +++ b/ctrl/facemgr/src/interfaces/priority_controller/priority_controller.c @@ -0,0 +1,135 @@ +#include <assert.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <hicn/facemgr.h> + +#include "../../common.h" +#include "../../interface.h" + +typedef struct { + int fd; +} pc_data_t; + +int priority_controller_initialize(interface_t * interface, void * cfg) +{ + INFO("Initializing priority controller"); + struct sockaddr_in addr; + + pc_data_t * data = malloc(sizeof(pc_data_t)); + if (!data) { + INFO("Priority controller data memory allocation error"); + goto ERR_MALLOC; + } + + interface->data = data; + + data->fd = socket(AF_INET, SOCK_DGRAM, 0); + //data->fd = socket(AF_INET, SOCK_STREAM, 0); + if (data->fd < 0) { + INFO("Priority controller socket error"); + perror("socket error"); + goto ERR_SOCKET; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + addr.sin_port = htons(9533); + + if (bind(data->fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + INFO("Priority controller socket bind error"); + perror("bind error"); + goto ERR_BIND; + } + if (interface_register_fd(interface, data->fd, NULL) < 0) { + ERROR("[priority_controller_initialize] Error registering fd"); + goto ERR_FD; + } + + INFO("Priority controller successfully initialized"); + return 0; + +ERR_FD: +ERR_BIND: + close(data->fd); +ERR_SOCKET: + free(data); +ERR_MALLOC: + return -1; +} + +int priority_controller_finalize(interface_t * interface) +{ + pc_data_t * data = (pc_data_t*)interface->data; + + if (data->fd > 0) {close(data->fd);} + free(data); + + return 0; +} + +int priority_controller_callback(interface_t * interface, int fd, void * unused) +{ + pc_data_t * data = (pc_data_t*)interface->data; + char buf[100]; + int rc; + + INFO("Priority controller receiving command"); + + rc = recv(data->fd, buf, 100, 0); + + if (rc < 0) { + INFO("Priority controller read error"); + return -1; + } + + INFO("Priority controller received command: %02X", buf[0]); + + facelet_t * facelet_w = facelet_create(); + facelet_t * facelet_c = facelet_create(); + facelet_set_netdevice_type(facelet_w, NETDEVICE_TYPE_WIFI); + facelet_set_netdevice_type(facelet_c, NETDEVICE_TYPE_CELLULAR); + facelet_set_status(facelet_w, FACELET_STATUS_CLEAN); + facelet_set_status(facelet_c, FACELET_STATUS_CLEAN); + switch(buf[0]) { + case '\0': + facelet_set_priority(facelet_w, 0); + facelet_set_priority(facelet_c, 10); + INFO("Priority controller configuring Cellular preferred"); + break; + case '\1': + facelet_set_priority(facelet_w, 10); + facelet_set_priority(facelet_c, 0); + INFO("Priority controller configuring Wi-Fi preferred"); + break; + case '\2': + facelet_set_priority(facelet_w, 0); + facelet_set_priority(facelet_c, 0); + INFO("Priority controller configuring both Cellular and Wi-Fi preferred"); + break; + default: + INFO("Priority cntroller invalid data received from updown server. Ignoring..."); + facelet_free(facelet_w); + facelet_free(facelet_c); + return -1; + } + + facelet_set_event(facelet_w, FACELET_EVENT_UPDATE); + facelet_set_event(facelet_c, FACELET_EVENT_UPDATE); + + interface_raise_event(interface, facelet_w); + interface_raise_event(interface, facelet_c); + + return 0; +} + +interface_ops_t priority_controller_ops = { + .type = "priority_controller", + .initialize = priority_controller_initialize, + .finalize = priority_controller_finalize, + .callback = priority_controller_callback, +}; diff --git a/ctrl/libhicnctrl/src/api.c b/ctrl/libhicnctrl/src/api.c index 8540addcf..e7caf4f1c 100644 --- a/ctrl/libhicnctrl/src/api.c +++ b/ctrl/libhicnctrl/src/api.c @@ -2283,7 +2283,7 @@ hc_face_get(hc_sock_t * s, hc_face_t * face, hc_face_t ** face_found) *face_found = NULL; return 0; } - *face_found = malloc(sizeof(face_t)); + *face_found = malloc(sizeof(hc_face_t)); hc_connection_to_face(connection_found, *face_found); free(connection_found); break; @@ -2299,7 +2299,7 @@ hc_face_get(hc_sock_t * s, hc_face_t * face, hc_face_t ** face_found) *face_found = NULL; return 0; } - *face_found = malloc(sizeof(face_t)); + *face_found = malloc(sizeof(hc_face_t)); hc_listener_to_face(listener_found, *face_found); free(listener_found); break; |