1 /******************************************************************************
3 * Copyright (c) 2019 Intel.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 *******************************************************************************/
20 * @brief This file provides implementation of synchronization related APIs (PTP/1588)
23 * @file xran_sync_api.c
24 * @ingroup group_lte_source_xran
25 * @author Intel Corporation
40 #include "xran_sync_api.h"
41 #include "xran_printf.h"
44 #define PROC_DIR "/proc"
45 #define COMM_FILE "comm"
46 #define PMC_CMD "pmc -u -b 0 'GET PORT_DATA_SET'"
47 #define PTP4L_PROC_NAME "ptp4l"
48 #define PHC2SYS_PROC_NAME "phc2sys"
50 static int find_substr(const char *str, const unsigned int str_len,
51 const char *substr, const unsigned int substr_len)
57 while (ind + substr_len <= str_len) {
58 if (0 == strncmp(&str[ind], substr, substr_len))
65 static int is_process_running(char *pname)
67 char full_path[BUF_LEN] = {0};
68 char read_proc_name[BUF_LEN] = {0};
71 int dir_fd = dirfd((DIR*)PROC_DIR);
72 DIR *dir = fdopendir(dir_fd);
77 struct dirent *entry = NULL;
78 while ((entry = readdir(dir))) {
79 long pid = atol(entry->d_name);
83 snprintf(full_path, BUF_LEN,"%s/%ld/%s", PROC_DIR, pid, COMM_FILE);
84 int proc_name_file = open(full_path, O_RDONLY);
85 if (null == proc_name_file)
87 fgets( read_proc_name, BUF_LEN, (FILE*)proc_name_file);
88 if (0 == strncmp(read_proc_name, pname, strlen(pname))) {
90 close(proc_name_file);
93 close(proc_name_file);
99 static int check_ptp_status()
101 char pmc_out_line[BUF_LEN];
102 const char *keywords[2] = {"portState", "SLAVE"};
104 FILE *pmc_pipe = popen(PMC_CMD, "r");
105 if (NULL == pmc_pipe)
108 while(fgets(pmc_out_line, BUF_LEN, pmc_pipe)) {
109 if (0 == find_substr(pmc_out_line, strlen(pmc_out_line), keywords[0],
110 strlen(keywords[0]))) {
111 if (0 == find_substr(pmc_out_line, strlen(pmc_out_line),
112 keywords[1], strlen(keywords[1]))) {
122 int xran_is_synchronized()
125 res |= is_process_running(PTP4L_PROC_NAME);
126 print_dbg("PTP4L_PROC_NAME %d\n", res);
127 res |= is_process_running(PHC2SYS_PROC_NAME);
128 print_dbg("PHC2SYS_PROC_NAME %d\n", res);
129 res |= check_ptp_status();
130 print_dbg("check_ptp_status %d\n", res);