1 /******************************************************************************
\r
3 * Copyright (c) 2019 Intel.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
17 *******************************************************************************/
\r
19 * @brief This file provides implementation of synchronization related APIs (PTP/1588)
\r
22 * @file xran_sync_api.c
\r
23 * @ingroup group_lte_source_xran
\r
24 * @author Intel Corporation
\r
34 #include "xran_sync_api.h"
\r
35 #include "xran_printf.h"
\r
38 #define PROC_DIR "/proc"
\r
39 #define COMM_FILE "comm"
\r
40 #define PMC_CMD "pmc -u -b 0 'GET PORT_DATA_SET'"
\r
41 #define PTP4L_PROC_NAME "ptp4l"
\r
42 #define PHC2SYS_PROC_NAME "phc2sys"
\r
44 static int find_substr(const char *str, const unsigned int str_len,
\r
45 const char *substr, const unsigned int substr_len)
\r
49 unsigned int ind = 0;
\r
51 while (ind + substr_len <= str_len) {
\r
52 if (0 == strncmp(&str[ind], substr, substr_len))
\r
59 static int is_process_running(char *pname)
\r
61 char full_path[BUF_LEN] = {0};
\r
62 char read_proc_name[BUF_LEN] = {0};
\r
64 DIR *dir = opendir(PROC_DIR);
\r
69 struct dirent *entry = NULL;
\r
70 while (entry = readdir(dir)) {
\r
71 long pid = atol(entry->d_name);
\r
74 snprintf(full_path, sizeof(full_path), "%s/%ld/%s", PROC_DIR, pid, COMM_FILE);
\r
75 FILE *proc_name_file = fopen(full_path, "r");
\r
76 if (NULL == proc_name_file)
\r
78 fgets( read_proc_name, BUF_LEN, proc_name_file);
\r
79 if (0 == strncmp(read_proc_name, pname, strlen(pname))) {
\r
81 fclose(proc_name_file);
\r
84 fclose(proc_name_file);
\r
90 static int check_ptp_status()
\r
92 char pmc_out_line[BUF_LEN];
\r
93 const char *keywords[2] = {"portState", "SLAVE"};
\r
95 FILE *pmc_pipe = popen(PMC_CMD, "r");
\r
96 if (NULL == pmc_pipe)
\r
99 while(fgets(pmc_out_line, BUF_LEN, pmc_pipe)) {
\r
100 if (0 == find_substr(pmc_out_line, strlen(pmc_out_line), keywords[0],
\r
101 strlen(keywords[0]))) {
\r
102 if (0 == find_substr(pmc_out_line, strlen(pmc_out_line),
\r
103 keywords[1], strlen(keywords[1]))) {
\r
113 int xran_is_synchronized()
\r
116 res |= is_process_running(PTP4L_PROC_NAME);
\r
117 print_dbg("PTP4L_PROC_NAME %d\n", res);
\r
118 res |= is_process_running(PHC2SYS_PROC_NAME);
\r
119 print_dbg("PHC2SYS_PROC_NAME %d\n", res);
\r
120 res |= check_ptp_status();
\r
121 print_dbg("check_ptp_status %d\n", res);
\r