From c6b7629bcaa26dc6bb506ca2187092cdaa4e3c68 Mon Sep 17 00:00:00 2001 From: Junsu Choi Date: Fri, 22 Nov 2019 11:15:56 +0900 Subject: [HICN-408] add a face manager interface for face priority control Change-Id: I166cb56e7376fe896d41d6d371ecb3727b7563ce Signed-off-by: Junsu Choi --- ctrl/facemgr/src/api.c | 36 +++++- ctrl/facemgr/src/interfaces/CMakeLists.txt | 1 + .../interfaces/priority_controller/CMakeLists.txt | 30 +++++ .../priority_controller/priority_controller.c | 132 +++++++++++++++++++++ 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 ctrl/facemgr/src/interfaces/priority_controller/CMakeLists.txt create mode 100644 ctrl/facemgr/src/interfaces/priority_controller/priority_controller.c diff --git a/ctrl/facemgr/src/api.c b/ctrl/facemgr/src/api.c index 13b811288..01b6e52f3 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 @@ -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 @@ -1865,6 +1872,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 +1932,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 +1973,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 +2026,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 +2136,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/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 +#include +#include +#include +#include +#include + +#include + +#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, +}; -- cgit 1.2.3-korg