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 *******************************************************************************/
19 * @brief This file provides implementation of synchronization related APIs (PTP/1588)
22 * @file xran_sync_api.c
23 * @ingroup group_lte_source_xran
24 * @author Intel Corporation
34 #include "xran_sync_api.h"
35 #include "xran_printf.h"
38 #define PROC_DIR "/proc"
39 #define COMM_FILE "comm"
40 #define PMC_CMD "pmc -u -b 0 'GET PORT_DATA_SET'"
41 #define PTP4L_PROC_NAME "ptp4l"
42 #define PHC2SYS_PROC_NAME "phc2sys"
44 static int find_substr(const char *str, const unsigned int str_len,
45 const char *substr, const unsigned int substr_len)
51 while (ind + substr_len <= str_len) {
52 if (0 == strncmp(&str[ind], substr, substr_len))
59 static int is_process_running(char *pname)
61 char full_path[BUF_LEN] = {0};
62 char read_proc_name[BUF_LEN] = {0};
64 DIR *dir = opendir(PROC_DIR);
69 struct dirent *entry = NULL;
70 while (entry = readdir(dir)) {
71 long pid = atol(entry->d_name);
74 sprintf(full_path, "%s/%ld/%s", PROC_DIR, pid, COMM_FILE);
75 FILE *proc_name_file = fopen(full_path, "r");
76 if (NULL == proc_name_file)
78 fgets( read_proc_name, BUF_LEN, proc_name_file);
79 if (0 == strncmp(read_proc_name, pname, strlen(pname))) {
81 fclose(proc_name_file);
84 fclose(proc_name_file);
90 static int check_ptp_status()
92 char pmc_out_line[BUF_LEN];
93 const char *keywords[2] = {"portState", "SLAVE"};
95 FILE *pmc_pipe = popen(PMC_CMD, "r");
99 while(fgets(pmc_out_line, BUF_LEN, pmc_pipe)) {
100 if (0 == find_substr(pmc_out_line, strlen(pmc_out_line), keywords[0],
101 strlen(keywords[0]))) {
102 if (0 == find_substr(pmc_out_line, strlen(pmc_out_line),
103 keywords[1], strlen(keywords[1]))) {
113 int xran_is_synchronized()
116 res |= is_process_running(PTP4L_PROC_NAME);
117 print_dbg("PTP4L_PROC_NAME %d\n", res);
118 res |= is_process_running(PHC2SYS_PROC_NAME);
119 print_dbg("PHC2SYS_PROC_NAME %d\n", res);
120 res |= check_ptp_status();
121 print_dbg("check_ptp_status %d\n", res);