function tftp_server_modem($tftpserver, $ch, $connect_url, $connect_api)
{
    include "config.php";
    $func_role = basename(__FILE__) . " " . __FUNCTION__;
    curl_setopt($ch, CURLOPT_URL, $connect_url . "get_tftp_modem_change");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"id_tftp_server\":\"" . $tftpserver . "\",\"api\":\"" . $connect_api . "\"}");
    $return = curl_exec($ch);
    if (curl_errno($ch)) {
        echo system_addlog($func_role, "1", "Curl error: " . curl_error($ch));
    } else {
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpcode == "200") {
            $tftp = json_decode($return, true);
            foreach ($tftp as $row) {
                // Start change TFTP modem
                $id = $row["id"];
                $idm = $row["idm"];
                $act = $row["act"];
                $file_task = $tftp_cm_process . "m" . $idm . ".task";
                if (file_exists($file_task)) {
                    echo message_addlog($func_role, "Task {$file_task} is runing by other process. Wait.");
                } else {
                    // get paramerets and create yml file
                    $config_data = tftp_generate_config_data($id, $idm, $ch, $connect_url, $connect_api);
                    // set main parameters
                    $execute_mode = $config_data["execute"]["mode"];
                    $execute_name = $driver_dir . $config_data["execute"]["name"];
                    // generate .src local config if necessary
                    if ($execute_mode == "local") {
                        if ($act == "1") {
                            tftp_generate_local_src($idm, $ch, $connect_url, $connect_api);
                        }
                    }
                    // build command and execute in thread
                    curl_setopt($ch, CURLOPT_URL, $connect_url . "set_tftp_modem_process");
                    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"id\":\"" . $id . "\",\"api\":\"" . $connect_api . "\"}");
                    $return = curl_exec($ch);
                    if (curl_errno($ch)) {
                        echo system_addlog($func_role, "1", "Curl error: " . curl_error($ch));
                    } else {
                        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                        if ($httpcode == "200") {
                            $result_data = json_decode($return, true);
                            $result = $result_data["result"];
                            if ($result == "success") {
                                $execute_command = $execute_name . " " . $id . " " . $idm . " " . $act . " < /dev/null >> " . $log_dir . "vivabill.log &";
                                $return = system_exec($execute_command);
                                echo system_addlog($func_role, $return, $execute_command);
                            }
                        } else {
                            $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
                            echo system_addlog($func_role, "1", "Error [" . $httpcode . "] " . $last_url);
                        }
                    }
                }
            }
            // Finish change TFTP modems
        } else {
            $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            echo system_addlog($func_role, "1", "Error [" . $httpcode . "] " . $last_url);
        }
    }
}
function cmanager_comx_task($id_comx_manager, $ch, $connect_url, $connect_api)
{
    include "config.php";
    $func_role = basename(__FILE__) . " " . __FUNCTION__;
    curl_setopt($ch, CURLOPT_URL, $connect_url . "get_cmanager_comx_list");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"id_comx_manager\":\"" . $id_comx_manager . "\",\"api\":\"" . $connect_api . "\"}");
    $return = curl_exec($ch);
    if (curl_errno($ch)) {
        echo system_addlog($func_role, "1", "Curl error: " . curl_error($ch));
    } else {
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpcode == "200") {
            $r_comx = json_decode($return, true);
            foreach ($r_comx as $comx) {
                $id = $comx["id"];
                $data_file = $comx_dir . $id . ".data";
                $task_file = $comx_dir . $id . ".task";
                if (!file_exists($task_file)) {
                    $fp = fopen($data_file, "w");
                    fwrite($fp, json_encode($comx));
                    fclose($fp);
                    $execute_mode = $comx["execute"]["mode"];
                    $id_driver_mode = $comx["model"]["id_driver_mode"];
                    $vlan_check = $comx["model"]["vlan_check"];
                    $vlan_mib = $comx["model"]["vlan_mib"];
                    // begin create Local config
                    if ($execute_mode == "local") {
                        $file_exp = $tftp_cm_config . $comx["id"] . ".exp";
                        $file_tmp = $tftp_cm_config . $comx["id"] . ".tmp";
                        $id = $comx["id"];
                        $id_comx = $comx["id_comx"];
                        // TFTP mode
                        if ($id_driver_mode == "1") {
                            cmanager_comx_driver($file_exp, "0", $id_comx, $id, $ch, $connect_url, $connect_api);
                            cmanager_comx_driver($file_tmp, $id_driver_mode, $id_comx, $id, $ch, $connect_url, $connect_api);
                        }
                        // EXPECT mode
                        if ($id_driver_mode == "2") {
                            cmanager_comx_driver($file_exp, $id_driver_mode, $id_comx, $id, $ch, $connect_url, $connect_api);
                        }
                        // EXPECT Wi-Fi mode
                        if ($id_driver_mode == "3") {
                            cmanager_comx_driver($file_tmp, $id_driver_mode, $id_comx, $id, $ch, $connect_url, $connect_api);
                        }
                    }
                    // end create Local config
                    $execute_name = $driver_dir . $comx["execute"]["name"];
                    if (cmanager_comx_action($id, $ch, $connect_url, $connect_api, "prepare", "Prepare to run by " . $comx["execute"]["name"]) == "success") {
                        $execute_command = $execute_name . " " . $id . " < /dev/null >> " . $log_dir . "vivabill.log &";
                        $return = system_exec($execute_command);
                        echo system_addlog($func_role, $return, $execute_command);
                    }
                } else {
                    echo message_addlog($func_role, "Task " . $id . " is exists");
                }
            }
        } else {
            $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            echo system_addlog($func_role, "1", "Error [" . $httpcode . "] " . $last_url);
        }
    }
}
function cmanager_ovsp_task($id_comx_manager, $ch, $connect_url, $connect_api)
{
    include "config.php";
    $func_role = basename(__FILE__) . " " . __FUNCTION__;
    curl_setopt($ch, CURLOPT_URL, $connect_url . "get_cmanager_ovsp_list");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"id_comx_manager\":\"" . $id_comx_manager . "\",\"api\":\"" . $connect_api . "\"}");
    $return = curl_exec($ch);
    if (curl_errno($ch)) {
        echo system_addlog($func_role, "1", "Curl error: " . curl_error($ch));
    } else {
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpcode == "200") {
            $r_ovsp = json_decode($return, true);
            foreach ($r_ovsp as $ovsp) {
                $id = $ovsp["id"];
                $data_file = $ovsp_dir . $id . ".data";
                $task_file = $ovsp_dir . $id . ".task";
                if (!file_exists($task_file)) {
                    $fp = fopen($data_file, "w");
                    fwrite($fp, json_encode($ovsp));
                    fclose($fp);
                    $execute_name = $driver_dir . $ovsp["execute"]["name"];
                    if (cmanager_ovsp_action($id, $ch, $connect_url, $connect_api, "process", "") == "success") {
                        $execute_command = $execute_name . " " . $id . " < /dev/null >> " . $log_dir . "vivabill.log &";
                        $return = system_exec($execute_command);
                        echo system_addlog($func_role, $return, $execute_command);
                    }
                } else {
                    echo message_addlog($func_role, "Task " . $id . " is exists");
                }
            }
        } else {
            $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            echo system_addlog($func_role, "1", "Error [" . $httpcode . "] " . $last_url);
        }
    }
}