diff options
Diffstat (limited to 'ctrl/facemgr/src/interfaces')
3 files changed, 163 insertions, 0 deletions
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/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..5c5afdb6c --- /dev/null +++ b/ctrl/facemgr/src/interfaces/priority_controller/priority_controller.c @@ -0,0 +1,132 @@ +#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"); + return -1; + } + + 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"); + return -1; + } + + 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"); + return -1; + } + interface_register_fd(interface, data->fd, NULL); + + INFO("Priority controller successfully initialized"); + return data->fd; +} + +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; + + struct sockaddr_in addr; + 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); + + 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); + + facelet_free(facelet_w); + facelet_free(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, +}; |